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
|
2014-03-18 08:16:13 +01:00
|
|
|
local unpack = unpack
|
2010-07-21 02:52:02 +02:00
|
|
|
|
|
|
|
module "irc"
|
|
|
|
|
|
|
|
handlers = {}
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["PING"] = function(conn, msg)
|
|
|
|
conn:send(Message("PONG", msg.args))
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["001"] = function(conn, msg)
|
|
|
|
conn.authed = true
|
|
|
|
conn.nick = msg.args[1]
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["PRIVMSG"] = function(conn, msg)
|
|
|
|
conn:invoke("OnChat", msg.user, msg.args[1], msg.args[2])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
|
|
|
|
handlers["NOTICE"] = function(conn, msg)
|
|
|
|
conn:invoke("OnNotice", msg.user, msg.args[1], msg.args[2])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["JOIN"] = function(conn, msg)
|
|
|
|
local channel = msg.args[1]
|
|
|
|
if conn.track_users then
|
|
|
|
if msg.user.nick == conn.nick then
|
|
|
|
conn.channels[channel] = {users = {}}
|
2010-07-21 02:52:02 +02:00
|
|
|
else
|
2014-03-18 08:16:13 +01:00
|
|
|
conn.channels[channel].users[msg.user.nick] = user
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
conn:invoke("OnJoin", msg.user, msg.args[1])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["PART"] = function(conn, msg)
|
|
|
|
local channel = msg.args[1]
|
|
|
|
if conn.track_users then
|
|
|
|
if msg.user.nick == conn.nick then
|
|
|
|
conn.channels[channel] = nil
|
2010-07-21 02:52:02 +02:00
|
|
|
else
|
2014-03-18 08:16:13 +01:00
|
|
|
conn.channels[channel].users[msg.user.nick] = nil
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
2014-03-18 08:16:13 +01:00
|
|
|
conn:invoke("OnPart", msg.user, msg.args[1], msg.args[2])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["QUIT"] = function(conn, msg)
|
|
|
|
if conn.track_users then
|
|
|
|
for chanName, chan in pairs(conn.channels) do
|
|
|
|
chan.users[msg.user.nick] = nil
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
2014-03-18 08:16:13 +01:00
|
|
|
conn:invoke("OnQuit", msg.user, msg.args[1], msg.args[2])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["NICK"] = function(conn, msg)
|
|
|
|
local newNick = msg.args[1]
|
|
|
|
if conn.track_users then
|
|
|
|
for chanName, chan in pairs(conn.channels) do
|
|
|
|
local users = chan.users
|
|
|
|
local oldinfo = users[msg.user.nick]
|
2010-07-21 02:52:02 +02:00
|
|
|
if oldinfo then
|
2014-03-18 08:16:13 +01:00
|
|
|
users[newNick] = oldinfo
|
|
|
|
users[msg.user.nick] = nil
|
|
|
|
conn:invoke("NickChange", msg.user, newNick, chanName)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2014-03-18 08:16:13 +01:00
|
|
|
conn:invoke("NickChange", msg.user, newNick)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
2014-03-18 08:16:13 +01:00
|
|
|
if msg.user.nick == conn.nick then
|
|
|
|
conn.nick = newnick
|
2013-08-06 00:33:33 +02:00
|
|
|
end
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
local function needNewNick(conn, msg)
|
|
|
|
local newnick = conn.nickGenerator(msg.args[2])
|
2013-10-23 15:37:21 +02:00
|
|
|
conn:queue(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-03-18 08:16:13 +01:00
|
|
|
handlers["005"] = function(conn, msg)
|
|
|
|
local arglen = #msg.args
|
|
|
|
-- Skip first and last parameters (nick and info)
|
|
|
|
for i = 2, arglen - 1 do
|
|
|
|
local item = msg.args[i]
|
2014-02-26 20:36:40 +01:00
|
|
|
local pos = item:find("=")
|
|
|
|
if pos then
|
2014-03-18 08:16:13 +01:00
|
|
|
conn.supports[item:sub(1, pos - 1)] = item:sub(pos + 1)
|
2014-02-26 20:36:40 +01:00
|
|
|
else
|
2014-03-18 08:16:13 +01:00
|
|
|
conn.supports[item] = true
|
2014-02-26 20:36:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-26 20:44:17 +01:00
|
|
|
-- RPL_MOTDSTART
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["375"] = function(conn, msg)
|
|
|
|
conn.motd = ""
|
2014-02-26 20:44:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- RPL_MOTD
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["372"] = function(conn, msg)
|
2014-02-26 20:44:17 +01:00
|
|
|
-- MOTD lines have a "- " prefix, strip it.
|
2014-03-18 08:16:13 +01:00
|
|
|
conn.motd = conn.motd .. msg.args[2]:sub(3) .. '\n'
|
2014-02-26 20:44:17 +01:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
-- NAMES list
|
|
|
|
handlers["353"] = function(conn, msg)
|
|
|
|
local chanType = msg.args[2]
|
|
|
|
local channel = msg.args[3]
|
|
|
|
local names = msg.args[4]
|
|
|
|
if conn.track_users then
|
|
|
|
conn.channels[channel] = conn.channels[channel] or {users = {}, type = chanType}
|
2010-07-21 02:52:02 +02:00
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
local users = conn.channels[channel].users
|
2010-07-21 02:52:02 +02:00
|
|
|
for nick in names:gmatch("(%S+)") do
|
2014-03-18 08:16:13 +01:00
|
|
|
local access, name = parseNick(conn, nick)
|
2013-06-26 00:17:52 +02:00
|
|
|
users[name] = {access = access}
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
-- End of NAMES list
|
|
|
|
handlers["366"] = function(conn, msg)
|
|
|
|
if conn.track_users then
|
|
|
|
conn:invoke("NameList", msg.args[2], msg.args[3])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
-- No topic
|
|
|
|
handlers["331"] = function(conn, msg)
|
|
|
|
conn:invoke("OnTopic", msg.args[2], nil)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["TOPIC"] = function(conn, msg)
|
|
|
|
conn:invoke("OnTopic", msg.args[1], msg.args[2])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["332"] = function(conn, msg)
|
|
|
|
conn:invoke("OnTopic", msg.args[2], msg.args[3])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
-- Topic creation info
|
|
|
|
handlers["333"] = function(conn, msg)
|
|
|
|
conn:invoke("OnTopicInfo", msg.args[2], msg.args[3], tonumber(msg.args[4]))
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["KICK"] = function(conn, msg)
|
|
|
|
conn:invoke("OnKick", msg.args[1], msg.args[2], msg.user, msg.args[3])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
-- RPL_UMODEIS
|
|
|
|
-- To answer a query about a client's own mode, RPL_UMODEIS is sent back
|
|
|
|
handlers["221"] = function(conn, msg)
|
|
|
|
conn:invoke("OnUserMode", msg.args[2])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
-- RPL_CHANNELMODEIS
|
|
|
|
-- The result from common irc servers differs from that defined by the rfc
|
|
|
|
handlers["324"] = function(conn, msg)
|
|
|
|
conn:invoke("OnChannelMode", msg.args[2], msg.args[3])
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["MODE"] = function(conn, msg)
|
|
|
|
local target = msg.args[1]
|
|
|
|
local modes = msg.args[2]
|
|
|
|
local optList = {}
|
|
|
|
for i = 3, #msg.args do
|
|
|
|
table.insert(optList, msg.args[i])
|
|
|
|
end
|
|
|
|
if conn.track_users and target ~= conn.nick then
|
2013-06-26 00:17:52 +02:00
|
|
|
local add = true
|
2014-03-18 08:16:13 +01:00
|
|
|
local argNum = 1
|
|
|
|
updatePrefixModes(conn)
|
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-03-18 08:16:13 +01:00
|
|
|
elseif conn.modeprefix[c] then
|
|
|
|
local nick = optList[argNum]
|
|
|
|
argNum = argNum + 1
|
|
|
|
local access = conn.channels[target].users[nick].access
|
|
|
|
access[conn.modeprefix[c]] = add
|
2014-02-27 05:45:29 +01:00
|
|
|
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-03-18 08:16:13 +01:00
|
|
|
conn:invoke("OnModeChange", msg.user, target, modes, unpack(optList))
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
|
|
|
|
2014-03-18 08:16:13 +01:00
|
|
|
handlers["ERROR"] = function(conn, msg)
|
|
|
|
conn:invoke("OnDisconnect", msg.args[1], true)
|
|
|
|
conn:shutdown()
|
|
|
|
error(msg.args[1], 3)
|
2010-07-21 02:52:02 +02:00
|
|
|
end
|
2014-02-26 20:23:20 +01:00
|
|
|
|