|
# File rbot/rfc2812.rb, line 857
def process(serverstring)
data = Hash.new
data["SERVERSTRING"] = serverstring
unless serverstring =~ /^(:(\S+)\s)?(\S+)(\s(.*))?/
raise "Unparseable Server Message!!!: #{serverstring}"
end
prefix, command, params = $2, $3, $5
if prefix != nil
data['SOURCE'] = prefix
if prefix =~ /^(\S+)!(\S+)$/
data['SOURCENICK'] = $1
data['SOURCEADDRESS'] = $2
end
end
# split parameters in an array
argv = []
params.scan(/(?!:)(\S+)|:(.*)/) { argv << ($1 || $2) } if params
case command
when 'PING'
data['PINGID'] = argv[0]
handle('PING', data)
when /^(\d+)$/ # numeric server message
num=command.to_i
case num
when ERR_NICKNAMEINUSE
# "* <nick> :Nickname is already in use"
data['NICK'] = argv[1]
data['MESSAGE'] = argv[2]
handle('NICKTAKEN', data)
when ERR_ERRONEUSNICKNAME
# "* <nick> :Erroneous nickname"
data['NICK'] = argv[1]
data['MESSAGE'] = argv[2]
handle('BADNICK', data)
when RPL_TOPIC
data['CHANNEL'] = argv[0]
data['TOPIC'] = argv[1]
handle('TOPIC', data)
when RPL_TOPIC_INFO
data['CHANNEL'] = argv[0]
data['NICK'] = argv[1]
data['UNIXTIME'] = argv[2]
handle('TOPICINFO', data)
when RPL_NAMREPLY
# "( "=" / "*" / "@" ) <channel>
# :[ "@" / "+" ] <nick> *( " " [ "@" / "+" ] <nick> )
# - "@" is used for secret channels, "*" for private
# channels, and "=" for others (public channels).
argv[3..-1].each { |u|
if(u =~ /^([@+])?(.*)$/)
umode = $1 || ""
user = $2
@users << [user, umode]
end
}
when RPL_ENDOFNAMES
data['CHANNEL'] = argv[1]
data['USERS'] = @users
handle('NAMES', data)
@users = Array.new
when RPL_WELCOME
# "Welcome to the Internet Relay Network
# <nick>!<user>@<host>"
case argv[1]
when /((\S+)!(\S+))/
data['NETMASK'] = $1
data['NICK'] = $2
data['ADDRESS'] = $3
when /Welcome to the Internet Relay Network\s(\S+)/
data['NICK'] = $1
when /Welcome.*Network\s+(\S+)/
data['NICK'] = $1
end
handle('WELCOME', data)
when RPL_MOTDSTART
# "<nick> :- <server> Message of the Day -"
if argv[1] =~ /^-\s+(\S+)\s/
server = $1
@motd = ""
end
when RPL_MOTD
if(argv[1] =~ /^-\s+(.*)$/)
@motd << $1
@motd << "\n"
end
when RPL_ENDOFMOTD
data['MOTD'] = @motd
handle('MOTD', data)
else
handle('UNKNOWN', data)
end
# end of numeric replies
when 'PRIVMSG'
# you can either bind to 'PRIVMSG', to get every one and
# parse it yourself, or you can bind to 'MSG', 'PUBLIC',
# etc and get it all nicely split up for you.
data['TARGET'] = argv[0]
data['MESSAGE'] = argv[1]
handle('PRIVMSG', data)
# Now we split it
if(data['TARGET'] =~ /^(#|&).*/)
handle('PUBLIC', data)
else
handle('MSG', data)
end
when 'KICK'
data['CHANNEL'] = argv[0]
data['TARGET'] = argv[1]
data['MESSAGE'] = argv[2]
handle('KICK', data)
when 'PART'
data['CHANNEL'] = argv[0]
data['MESSAGE'] = argv[1]
handle('PART', data)
when 'QUIT'
data['MESSAGE'] = argv[0]
handle('QUIT', data)
when 'JOIN'
data['CHANNEL'] = argv[0]
handle('JOIN', data)
when 'TOPIC'
data['CHANNEL'] = argv[0]
data['TOPIC'] = argv[1]
handle('CHANGETOPIC', data)
when 'INVITE'
data['TARGET'] = argv[0]
data['CHANNEL'] = argv[1]
handle('INVITE', data)
when 'NICK'
data['NICK'] = argv[0]
handle('NICK', data)
when 'MODE'
data['CHANNEL'] = argv[0]
data['MODESTRING'] = argv[1]
data['TARGETS'] = argv[2]
handle('MODE', data)
when 'NOTICE'
data['TARGET'] = argv[0]
data['MESSAGE'] = argv[1]
if data['SOURCENICK']
handle('NOTICE', data)
else
# "server notice" (not from user, noone to reply to
handle('SNOTICE', data)
end
else
handle('UNKNOWN', data)
end
end
|