Class: Tkellem::IrcMessage

Inherits:
Struct
  • Object
show all
Defined in:
lib/tkellem/irc_message.rb

Constant Summary collapse

RE =
%r{(:[^ ]+ )?([^ ]*)(.*)}i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



7
8
9
# File 'lib/tkellem/irc_message.rb', line 7

def args
  @args
end

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



7
8
9
# File 'lib/tkellem/irc_message.rb', line 7

def command
  @command
end

#ctcpObject

Returns the value of attribute ctcp

Returns:

  • (Object)

    the current value of ctcp



7
8
9
# File 'lib/tkellem/irc_message.rb', line 7

def ctcp
  @ctcp
end

#prefixObject

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



7
8
9
# File 'lib/tkellem/irc_message.rb', line 7

def prefix
  @prefix
end

Class Method Details

.parse(line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tkellem/irc_message.rb', line 10

def self.parse(line)
  md = RE.match(line) or raise("invalid input: #{line.inspect}")

  prefix = md[1] && md[1][1..-1].strip
  command = md[2].upcase
  args = md[3]

  args.strip!
  idx = args.index(":")
  if idx && (idx == 0 || args[idx-1] == " "[0])
    args = args[0...idx].split(' ') + [args[idx+1..-1]]
  else
    args = args.split(' ')
  end

  msg = self.new(prefix, command, args)

  if args.last && args.last.match(%r{#{"\x01"}([^ ]+)([^\1]*)#{"\x01"}})
    msg.ctcp = $1.upcase
    msg.args[-1] = $2.strip
  end

  msg
end

.parse_client_command(line) ⇒ Object

parse a command as it’d come from a client, e.g. /nick newnick

or

/msg #someroom hey guys



39
40
41
42
43
44
45
46
47
# File 'lib/tkellem/irc_message.rb', line 39

def self.parse_client_command(line)
  return nil unless line[0] == '/'[0]
  if line =~ %r{^/msg\s+(\S+)\s+(.*)$}
    line = "/PRIVMSG #{$1} :#{$2}"
  end
  msg = parse(line[1..-1])
  return nil unless msg
  msg
end

Instance Method Details

#action?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/tkellem/irc_message.rb', line 53

def action?
  self.ctcp == 'ACTION'
end

#ctcp?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tkellem/irc_message.rb', line 49

def ctcp?
  self.ctcp.present?
end

#readdress_to(nick) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tkellem/irc_message.rb', line 94

def readdress_to(nick)
  privmsg = args.first[0] != '#'[0]

  if prefix
    # to this user
    if privmsg
      args[0] = nick
    else
      # do nothing, it's good to send
    end
  else
    # from this user
    if privmsg
      # a one-on-one chat -- every client i've seen doesn't know how to
      # display messages from themselves here, so we fake it by just
      # adding an arrow and pretending the other user said it. shame.
      prefix = args.first
      args[0] = nick
      args[-1] = "-> #{args.last}"
    else
      # it's a room, we can just replay
      prefix = nick
    end
  end
end

#replayObject Also known as: to_s



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tkellem/irc_message.rb', line 57

def replay
  line = []
  line << ":#{prefix}" unless prefix.nil?
  line << command
  ext_arg = args.last if args.last && args.last.match(%r{^:|\s})
  line += ext_arg ? args[0...-1] : args
  if ctcp?
    line << ":\x01#{ctcp} #{ext_arg}\x01"
  else
    line << ":#{ext_arg}" unless ext_arg.nil?
  end
  line.join ' '
end

#target_userObject



72
73
74
75
76
77
78
# File 'lib/tkellem/irc_message.rb', line 72

def target_user
  if prefix && md = %r{^([^!]+)}.match(prefix)
    md[1]
  else
    nil
  end
end

#with_timestamp(timestamp) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tkellem/irc_message.rb', line 80

def with_timestamp(timestamp)
  if timestamp <= 24.hours.ago
    timestring = timestamp.strftime("%Y-%m-%d %H:%M:%S")
  else
    timestring = timestamp.strftime("%H:%M:%S")
  end
  args = self.args
  if args && args[-1]
    args = args.dup
    args[-1] = "#{timestring}> #{args[-1]}"
  end
  IrcMessage.new(prefix, command, args, ctcp)
end