2010-07-21 02:52:02 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local error = error
|
|
|
|
local tonumber = tonumber
|
2013-06-26 00:17:52 +02:00
|
|
|
local table = table
|
2010-07-21 02:52:02 +02:00
|
|
|
|
|
|
|
module "irc"
|
|
|
|
|
|
|
|
handlers = {}
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["PING"] = function(o, user, query)
|
2014-03-17 19:52:09 +01:00
|
|
|
o:send(Message("PONG", {query}))
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["001"] = function(o, user, me)
|
2010-07-21 02:52:02 +02:00
|
|
|
o.authed = true
|
2011-11-07 20:15:18 +01:00
|
|
|
o.nick = me
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["PRIVMSG"] = function(o, user, channel, message)
|
|
|
|
o:invoke("OnChat", user, channel, message)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["NOTICE"] = function(o, user, channel, message)
|
|
|
|
o:invoke("OnNotice", user, channel, message)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["JOIN"] = function(o, user, channel)
|
2010-07-21 02:52:02 +02:00
|
|
|
if o.track_users then
|
|
|
|
if user.nick == o.nick then
|
|
|
|
o.channels[channel] = {users = {}}
|
|
|
|
else
|
|
|
|
o.channels[channel].users[user.nick] = user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
o:invoke("OnJoin", user, channel)
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["PART"] = function(o, user, channel, reason)
|
2010-07-21 02:52:02 +02:00
|
|
|
if o.track_users then
|
|
|
|
if user.nick == o.nick then
|
|
|
|
o.channels[channel] = nil
|
|
|
|
else
|
|
|
|
o.channels[channel].users[user.nick] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o:invoke("OnPart", user, channel, reason)
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["QUIT"] = function(o, user, msg)
|
2010-07-21 02:52:02 +02:00
|
|
|
if o.track_users then
|
|
|
|
for channel, v in pairs(o.channels) do
|
|
|
|
v.users[user.nick] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o:invoke("OnQuit", user, msg)
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["NICK"] = function(o, user, newnick)
|
2010-07-21 02:52:02 +02:00
|
|
|
if o.track_users then
|
|
|
|
for channel, v in pairs(o.channels) do
|
|
|
|
local users = v.users
|
|
|
|
local oldinfo = users[user.nick]
|
|
|
|
if oldinfo then
|
|
|
|
users[newnick] = oldinfo
|
|
|
|
users[user.nick] = nil
|
|
|
|
o:invoke("NickChange", user, newnick, channel)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
o:invoke("NickChange", user, newnick)
|
|
|
|
end
|
2013-08-06 00:33:33 +02:00
|
|
|
if user.nick == o.nick then
|
|
|
|
o.nick = newnick
|
|
|
|
end
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
local function needNewNick(o, user, target, badnick)
|
2013-10-07 02:24:56 +02:00
|
|
|
local newnick = o.nickGenerator(badnick)
|
2014-03-17 19:52:09 +01:00
|
|
|
o:send(msgs.nick(newnick))
|
2013-10-07 02:24:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)
|
|
|
|
handlers["432"] = needNewNick
|
|
|
|
|
|
|
|
-- ERR_NICKNAMEINUSE
|
|
|
|
handlers["433"] = needNewNick
|
|
|
|
|
2014-02-26 20:36:40 +01:00
|
|
|
-- RPL_ISUPPORT
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["005"] = function(o, user, nick, ...)
|
2014-02-26 20:36:40 +01:00
|
|
|
local list = {...}
|
|
|
|
local listlen = #list
|
|
|
|
-- Skip last parameter (info)
|
|
|
|
for i = 1, listlen - 1 do
|
|
|
|
local item = list[i]
|
|
|
|
local pos = item:find("=")
|
|
|
|
if pos then
|
|
|
|
o.supports[item:sub(1, pos - 1)] = item:sub(pos + 1)
|
|
|
|
else
|
|
|
|
o.supports[item] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:44:17 +01:00
|
|
|
-- RPL_MOTDSTART
|
|
|
|
handlers["375"] = function(o, user, info)
|
|
|
|
o.motd = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
-- RPL_MOTD
|
|
|
|
handlers["372"] = function(o, user, nick, line)
|
|
|
|
-- MOTD lines have a "- " prefix, strip it.
|
|
|
|
o.motd = o.motd..line:sub(3)..'\n'
|
|
|
|
end
|
|
|
|
|
2010-07-21 02:52:02 +02:00
|
|
|
--NAMES list
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["353"] = function(o, user, me, chanType, channel, names)
|
2010-07-21 02:52:02 +02:00
|
|
|
if o.track_users then
|
|
|
|
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
|
|
|
|
|
|
|
|
local users = o.channels[channel].users
|
|
|
|
for nick in names:gmatch("(%S+)") do
|
2014-02-27 05:45:29 +01:00
|
|
|
local access, name = parseNick(o, nick)
|
2013-06-26 00:17:52 +02:00
|
|
|
users[name] = {access = access}
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--end of NAMES
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["366"] = function(o, user, me, channel, msg)
|
2010-07-21 02:52:02 +02:00
|
|
|
if o.track_users then
|
|
|
|
o:invoke("NameList", channel, msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--no topic
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["331"] = function(o, user, me, channel)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnTopic", channel, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
--new topic
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["TOPIC"] = function(o, user, channel, topic)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnTopic", channel, topic)
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["332"] = function(o, user, me, channel, topic)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnTopic", channel, topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
--topic creation info
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["333"] = function(o, user, me, channel, nick, time)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["KICK"] = function(o, user, channel, kicked, reason)
|
|
|
|
o:invoke("OnKick", channel, kicked, user, reason)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--RPL_UMODEIS
|
|
|
|
--To answer a query about a client's own mode, RPL_UMODEIS is sent back
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["221"] = function(o, user, user, modes)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnUserMode", modes)
|
|
|
|
end
|
|
|
|
|
|
|
|
--RPL_CHANNELMODEIS
|
|
|
|
--The result from common irc servers differs from that defined by the rfc
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["324"] = function(o, user, user, channel, modes)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnChannelMode", channel, modes)
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["MODE"] = function(o, user, target, modes, ...)
|
2013-06-26 00:17:52 +02:00
|
|
|
if o.track_users and target ~= o.nick then
|
|
|
|
local add = true
|
|
|
|
local optList = {...}
|
2014-02-27 05:45:29 +01:00
|
|
|
updatePrefixModes(o)
|
2013-06-26 00:17:52 +02:00
|
|
|
for c in modes:gmatch(".") do
|
|
|
|
if c == "+" then add = true
|
|
|
|
elseif c == "-" then add = false
|
2014-02-27 05:45:29 +01:00
|
|
|
elseif o.modeprefix[c] then
|
|
|
|
local nick = table.remove(optList, 1)
|
|
|
|
local access = o.channels[target].users[nick].access
|
|
|
|
access[o.modeprefix[c]] = add
|
|
|
|
if c == "o" then access.op = add
|
|
|
|
elseif c == "v" then access.voice = add
|
|
|
|
end
|
2013-06-26 00:17:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-02-26 20:23:20 +01:00
|
|
|
o:invoke("OnModeChange", user, target, modes, ...)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:20 +01:00
|
|
|
handlers["ERROR"] = function(o, user, message)
|
2010-07-21 02:52:02 +02:00
|
|
|
o:invoke("OnDisconnect", message, true)
|
|
|
|
o:shutdown()
|
|
|
|
error(message, 3)
|
|
|
|
end
|
2014-02-26 20:23:20 +01:00
|
|
|
|