Class: IRC::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/irc/message.rb

Overview

Public: Represents a Message. This class is intended to be subclassed. Its `.parse` and `#valid?` methods are not implemented and should be redefined.

.parse should take a message String and return a four-element Array. #valid? should return true or false.

Direct Known Subclasses

RFC2812::Message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Message

Returns a new instance of Message.


29
30
31
32
33
34
35
36
37
38
# File 'lib/irc/message.rb', line 29

def initialize(raw)
  parsed = self.class.parse raw

  @raw     = raw
  @prefix  = parsed[0] # TODO: Maybe store a Prefix. But how to know which one?
  @command = String(parsed[1]).downcase.to_sym
  @params  = parsed[2]
  @trail   = parsed[3]
  @time    = Time.now
end

Instance Attribute Details

#commandObject (readonly)

Public: Gets the command Symbol.


15
16
17
# File 'lib/irc/message.rb', line 15

def command
  @command
end

#paramsObject (readonly)

Public: Gets the parameters Array. It does not include the trail.


18
19
20
# File 'lib/irc/message.rb', line 18

def params
  @params
end

#prefixObject (readonly)

Public: Gets the prefix.


12
13
14
# File 'lib/irc/message.rb', line 12

def prefix
  @prefix
end

#rawObject (readonly)

Public: Gets the raw message String.


9
10
11
# File 'lib/irc/message.rb', line 9

def raw
  @raw
end

#timeObject (readonly)

Public: Gets the Time the message was parsed at.


27
28
29
# File 'lib/irc/message.rb', line 27

def time
  @time
end

#trailObject (readonly) Also known as: to_s

Public: Gets the trail String.


21
22
23
# File 'lib/irc/message.rb', line 21

def trail
  @trail
end

Instance Method Details

#==(other) ⇒ Object

Public: Checks the equality. If `other` is an IRC::Message the raw message is compared. If a String-like object (responds to #to_str) is given, the trail of the message is compared.

other - An IRC::Message or String-like object.


45
46
47
48
49
50
51
52
53
# File 'lib/irc/message.rb', line 45

def ==(other)
  if other.respond_to? :raw
    return self.raw.chomp == other.raw.chomp
  elsif other.respond_to? :to_str
    self.trail == other.to_str
  else
    raise TypeError, "no implicit conversion of #{other.class} into String"
  end
end

#=~(other) ⇒ Object

Public: Checks the equality of the trail and other with the “spermy operator” (`=~`).

other - A String or regexp.


59
60
61
# File 'lib/irc/message.rb', line 59

def =~(other)
  self.trail =~ other
end

#match(command, pattern = nil, &block) ⇒ Object

Public: Checks the command for equality. Optionally takes a pattern (a String or Regexp) which is checked for equality against the trail. If a block is given it is executed when both the command and the pattern match.

command - A command Symbol, String or Integer. pattern - A String or Regexp pattern (default: nil). block - The block that should be executed (optional).

Examples

message.match :privmsg, "!help" do
  "..."
end

message.match :command, /^!help (\S+)$/ do |plugin|
  "..."
end

Returns false if pattern does not match. Returns true or the result of the block if the pattern does match.


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/irc/message.rb', line 83

def match(command, pattern = nil, &block)
  unless command_matches?(command)
    return false
  end 

  unless block_given?
    block = proc { true }
  end

  if pattern.nil? || (pattern.respond_to?(:to_str) && self == pattern)
    block.call
  elsif pattern.is_a?(Regexp)
    matchdata = pattern.match(self.trail)

    if matchdata.nil?
      return false
    end
    
    block.call(*matchdata.captures)
  else
    return false
  end

  return true
end

#strip_colorsObject

Public: Returns a new Message with all mIRC color codes removed.


110
111
112
# File 'lib/irc/message.rb', line 110

def strip_colors
  self.class.new self.raw.gsub(/\x03(?:[019]?[0-9](?:,[019]?[0-9])?)?/, "")
end