Class: BeerBot::Codecs::IRC::IRCMessage

Inherits:
Hash
  • Object
show all
Defined in:
lib/beerbot/02.codecs/irc.rb

Overview

This class represents an irc message broken down into its major constituent parts.

These include the prefix, command, parameters and trailing components of an irc message.

Constant Summary collapse

CMD =

This regexp captures the basic pattern used for prefixed and unprefixed irc commands sent by the server to a client.

See: tools.ietf.org/html/rfc1459.html sec 2.3.1

Regexp.new(
# nick!~user@host:
'^(:(?<prefix>[^:\s]+(@\S+)?)\s+)?'+
# eg 'PRIVMSG' , 3-digit code:
'(?<command>[^:\s]+)'+
# Bit after the command (one or more words):
'(\s+(?<params>[^:\s]+(\s+[^:\s]+)*))?\s*'+
# Bit after second ':' the msg in PRIVMSG
'\s:\s*(?<trailing>.*)$'
)

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ IRCMessage

Returns a new instance of IRCMessage.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/beerbot/02.codecs/irc.rb', line 155

def initialize raw

  @valid = false
  @has_prefix = false
  @user_prefix = false # message came from nick
  self[:prefix] = {}
  self[:command] = :unknown
  self[:raw] = raw
  self[:params] = []
  self[:trailing] = nil

  if m = CMD.match(raw) then
    @valid = true
    if m[:prefix] then
      @has_prefix = true
      nick,host = m[:prefix].split('!')
      if host then
        @user_prefix = true
        user,host = host.split('@')
        self[:prefix][:nick] = nick
        self[:prefix][:user] = user
        self[:prefix][:host] = host
      else
        # It aint a user prefix, so just bung it in host for the
        # moment.
        self[:prefix][:host] = nick
      end
    end

    self[:command] = m[:command].strip
    params = if m[:params] then m[:params].strip else "" end
    self[:params] = params.split(/\s+/)
    self[:trailing]= m[:trailing].strip
  end

end

Instance Method Details

#check(*syms) ⇒ Object

Check that syms exist in the hash otherwise return the missing sym.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/beerbot/02.codecs/irc.rb', line 213

def check *syms
  result = true
  syms.each{|sym|
    case sym
    when :prefix,:nick,:user,:host
      return :prefix unless self[:prefix]
      case sym
      when :prefix
      else
        return sym unless self[:prefix][sym]
      end
    else
      return sym unless self[sym]
    end
  }
  result
end

#prefix?Boolean

Is a prefixed irc string sent by server.

Returns:

  • (Boolean)


200
201
202
# File 'lib/beerbot/02.codecs/irc.rb', line 200

def prefix?
  @has_prefix
end

#user_prefix?Boolean

Is prefixed and the prefix is nick!~user@host .

Returns:

  • (Boolean)


206
207
208
# File 'lib/beerbot/02.codecs/irc.rb', line 206

def user_prefix?
  @user_prefix
end

#valid?Boolean

We couldn’t parse the command if not valid.

Returns:

  • (Boolean)


194
195
196
# File 'lib/beerbot/02.codecs/irc.rb', line 194

def valid?
  @valid
end