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



4
5
6
# File 'lib/tkellem/irc_message.rb', line 4

def args
  @args
end

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



4
5
6
# File 'lib/tkellem/irc_message.rb', line 4

def command
  @command
end

#ctcpObject

Returns the value of attribute ctcp

Returns:

  • (Object)

    the current value of ctcp



4
5
6
# File 'lib/tkellem/irc_message.rb', line 4

def ctcp
  @ctcp
end

#prefixObject

Returns the value of attribute prefix

Returns:

  • (Object)

    the current value of prefix



4
5
6
# File 'lib/tkellem/irc_message.rb', line 4

def prefix
  @prefix
end

Class Method Details

.parse(line) ⇒ Object



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

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



36
37
38
39
40
41
42
43
44
# File 'lib/tkellem/irc_message.rb', line 36

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)


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

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

#ctcp?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/tkellem/irc_message.rb', line 46

def ctcp?
  self.ctcp.present?
end

#replayObject Also known as: to_s



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tkellem/irc_message.rb', line 54

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



69
70
71
72
73
74
75
# File 'lib/tkellem/irc_message.rb', line 69

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

#with_timestamp(timestamp) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tkellem/irc_message.rb', line 77

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