module BeerBot::Codecs::IRC
IRC
Protcol module.
Main method is parse, whose job is to receive incoming irc strings and convert to a generalised format that higher layers like the dispatcher can use.
Constants
- ACTION_REGEX
Public Class Methods
Send a /me-style action to channel or nick.
# File lib/beerbot/02.codecs/irc.rb, line 275 def self.action to,str "PRIVMSG #{to} :\u0001#{'ACTION'} #{str}\u0001" end
Parse raw irc string and then yield or return a generic representation of the event.
Returns [event,*args]
Parse’s job is to take the constituents parts of an irc message, identify the type of event and return a generic representation of it.
So for instance, an irc privmsg (message) is represented as
[:msg,from,to,msg]
Note that connection readiness and PONG protocol are handled by the irc connection, not here.
# File lib/beerbot/02.codecs/irc.rb, line 35 def self.decode str m = IRCMessage.new(str) result = [] case m[:command] when 'NICK' # change of nick case s=m.check(:prefix,:nick,:trailing) when Symbol puts "* NICK expected #{s}" return nil end old = m[:prefix][:nick] nick = m[:trailing] result = [:nick,old,nick] when 'QUIT' # someone leaves irc case s=m.check(:prefix,:nick,:trailing) when Symbol puts "* QUIT expected #{s}" return nil end nick = m[:prefix][:nick] msg = m[:trailing] result = [:quit,nick,msg] when 'PART' # someone leaves channel case s=m.check(:prefix,:nick,:params) when Symbol puts "* PART expected #{s}" return nil end if channel=m[:params][0] then elsif channel=m[:trailing].strip.split(/\s+/).first then else channel=nil puts "* PART can't determine what is being parted from: '#{str}'" return nil end nick = m[:prefix][:nick] result = [:part,nick,channel] when 'INVITE' # someone invites us, oo ournick = m[:params][0] chan = m[:trailing].strip.split(/\s+/).first result = [:invite,chan] when 'JOIN' # someone joins channel case s=m.check(:prefix,:nick,:trailing) when Symbol puts "* JOIN expected #{s}" return nil end channel = m[:trailing] nick = m[:prefix][:nick] result = [:join,nick,channel] when '353' # channel user list when we join the channel case s=m.check(:params,:trailing) when Symbol puts "* 353 expected #{s}" return nil end channel = m[:params][2] users = m[:trailing].split(/\s+/) result = [:chanlist,channel,users] #puts "[decode/353] #{result}" when '366' # end of 353 result = [:chanlistend] when 'PRIVMSG' case s=m.check(:prefix,:nick,:params,:trailing) when Symbol #puts "* PRIVMSG expected #{s}" return nil end msg = m[:trailing].strip from = m[:prefix][:nick].strip to = m[:params][0].strip unless m[:params].empty? if action = self.match_action(msg) then result = [:action,from,to,action] else result = [:msg,from,to,msg] end else # command we don't handle result = [:default,m] end result end
Return irc-conformat string from a botmsg.
Generates nil if it can’t handle ‘botmsg’.
# File lib/beerbot/02.codecs/irc.rb, line 238 def self.encode botmsg case botmsg when Hash to = botmsg[:to] return nil unless to if botmsg[:action] then self.action(to,botmsg[:action]) elsif botmsg[:msg] then self.msg(to,botmsg[:msg]) else nil end when Array botmsg.map{|reply| self.encode reply } when Proc #p botmsg.call self.encode(botmsg.call) else nil end end
Join a channel
# File lib/beerbot/02.codecs/irc.rb, line 297 def self.join chan "JOIN #{chan}" end
Leave channel.
# File lib/beerbot/02.codecs/irc.rb, line 303 def self.leave chan "PART #{chan}" end
Returns string matching the action (the bit after ACTION) or nil.
eg
danb does something => "does something"
# File lib/beerbot/02.codecs/irc.rb, line 286 def self.match_action str m = ACTION_REGEX.match(str) if m then m[1].strip else nil end end
Send PRIVMSG to channel or nick.
# File lib/beerbot/02.codecs/irc.rb, line 266 def self.msg to,str "PRIVMSG #{to} :#{str}" end