1
0
mirror of https://github.com/ShadowNinja/LuaIRC.git synced 2025-07-20 16:40:26 +02:00

1 Commits

Author SHA1 Message Date
58dfd5043a Save the server's MOTD 2014-02-26 14:44:17 -05:00
4 changed files with 61 additions and 94 deletions

View File

@ -142,7 +142,6 @@ function irc:shutdown()
-- <li><code>OnUserMode(modes)</code></li> -- <li><code>OnUserMode(modes)</code></li>
-- <li><code>OnChannelMode(user, channel, modes)</code></li> -- <li><code>OnChannelMode(user, channel, modes)</code></li>
-- <li><code>OnModeChange(user, target, modes, ...)</code>* ('...' contains mode options such as banmasks)</li> -- <li><code>OnModeChange(user, target, modes, ...)</code>* ('...' contains mode options such as banmasks)</li>
-- <li><code>DoX(user, ...)</code>'X' is any IRC command or numeric with the first letter capitalized (eg, DoPing and Do001)</li>
-- </ul> -- </ul>
-- * Event also invoked for yourself. -- * Event also invoked for yourself.
-- <20> Channel passed only when user tracking is enabled -- <20> Channel passed only when user tracking is enabled
@ -155,7 +154,7 @@ function irc:shutdown()
-- <li><code>username</code> - User username.</li> -- <li><code>username</code> - User username.</li>
-- <li><code>host</code> - User hostname.</li> -- <li><code>host</code> - User hostname.</li>
-- <li><code>realname</code> - User real name.</li> -- <li><code>realname</code> - User real name.</li>
-- <li><code>access</code> - User access, available in channel-oriented callbacks. A table containing boolean fields for each access mode that the server supports. Eg: 'o', and 'v'.</li> -- <li><code>access</code> - User access, available in channel-oriented callbacks. A table containing the boolean fields 'op', 'halfop', and 'voice'.</li>
-- </ul> -- </ul>
-- Apart from <code>nick</code>, fields may be missing. To fill them in, enable user tracking and use irc:whois. -- Apart from <code>nick</code>, fields may be missing. To fill them in, enable user tracking and use irc:whois.
-- @name User -- @name User

View File

@ -7,24 +7,25 @@ module "irc"
handlers = {} handlers = {}
handlers["PING"] = function(o, user, query) handlers["PING"] = function(o, prefix, query)
o:send("PONG :%s", query) o:send("PONG :%s", query)
end end
handlers["001"] = function(o, user, me) handlers["001"] = function(o, prefix, me)
o.authed = true o.authed = true
o.nick = me o.nick = me
end end
handlers["PRIVMSG"] = function(o, user, channel, message) handlers["PRIVMSG"] = function(o, prefix, channel, message)
o:invoke("OnChat", user, channel, message) o:invoke("OnChat", parsePrefix(prefix), channel, message)
end end
handlers["NOTICE"] = function(o, user, channel, message) handlers["NOTICE"] = function(o, prefix, channel, message)
o:invoke("OnNotice", user, channel, message) o:invoke("OnNotice", parsePrefix(prefix), channel, message)
end end
handlers["JOIN"] = function(o, user, channel) handlers["JOIN"] = function(o, prefix, channel)
local user = parsePrefix(prefix)
if o.track_users then if o.track_users then
if user.nick == o.nick then if user.nick == o.nick then
o.channels[channel] = {users = {}} o.channels[channel] = {users = {}}
@ -36,7 +37,8 @@ handlers["JOIN"] = function(o, user, channel)
o:invoke("OnJoin", user, channel) o:invoke("OnJoin", user, channel)
end end
handlers["PART"] = function(o, user, channel, reason) handlers["PART"] = function(o, prefix, channel, reason)
local user = parsePrefix(prefix)
if o.track_users then if o.track_users then
if user.nick == o.nick then if user.nick == o.nick then
o.channels[channel] = nil o.channels[channel] = nil
@ -47,7 +49,8 @@ handlers["PART"] = function(o, user, channel, reason)
o:invoke("OnPart", user, channel, reason) o:invoke("OnPart", user, channel, reason)
end end
handlers["QUIT"] = function(o, user, msg) handlers["QUIT"] = function(o, prefix, msg)
local user = parsePrefix(prefix)
if o.track_users then if o.track_users then
for channel, v in pairs(o.channels) do for channel, v in pairs(o.channels) do
v.users[user.nick] = nil v.users[user.nick] = nil
@ -56,7 +59,8 @@ handlers["QUIT"] = function(o, user, msg)
o:invoke("OnQuit", user, msg) o:invoke("OnQuit", user, msg)
end end
handlers["NICK"] = function(o, user, newnick) handlers["NICK"] = function(o, prefix, newnick)
local user = parsePrefix(prefix)
if o.track_users then if o.track_users then
for channel, v in pairs(o.channels) do for channel, v in pairs(o.channels) do
local users = v.users local users = v.users
@ -75,7 +79,7 @@ handlers["NICK"] = function(o, user, newnick)
end end
end end
local function needNewNick(o, user, target, badnick) local function needNewNick(o, prefix, target, badnick)
local newnick = o.nickGenerator(badnick) local newnick = o.nickGenerator(badnick)
o:send("NICK %s", newnick) o:send("NICK %s", newnick)
end end
@ -86,101 +90,96 @@ handlers["432"] = needNewNick
-- ERR_NICKNAMEINUSE -- ERR_NICKNAMEINUSE
handlers["433"] = needNewNick handlers["433"] = needNewNick
-- RPL_ISUPPORT -- RPL_MOTDSTART
handlers["005"] = function(o, prefix, nick, ...) handlers["375"] = function(o, prefix, info)
local list = {...} o.motd = ""
local listlen = #list end
-- Skip last parameter (info)
for i = 1, listlen - 1 do -- RPL_MOTD
local item = list[i] handlers["372"] = function(o, prefix, nick, line)
local pos = item:find("=") -- MOTD lines have a "- " prefix, strip it.
if pos then o.motd = o.motd..line:sub(3)..'\n'
o.supports[item:sub(1, pos - 1)] = item:sub(pos + 1)
else
o.supports[item] = true
end
end
end end
--NAMES list --NAMES list
handlers["353"] = function(o, user, me, chanType, channel, names) handlers["353"] = function(o, prefix, me, chanType, channel, names)
if o.track_users then if o.track_users then
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType} o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
local users = o.channels[channel].users local users = o.channels[channel].users
for nick in names:gmatch("(%S+)") do for nick in names:gmatch("(%S+)") do
local access, name = parseNick(o, nick) local access, name = parseNick(nick)
users[name] = {access = access} users[name] = {access = access}
end end
end end
end end
--end of NAMES --end of NAMES
handlers["366"] = function(o, user, me, channel, msg) handlers["366"] = function(o, prefix, me, channel, msg)
if o.track_users then if o.track_users then
o:invoke("NameList", channel, msg) o:invoke("NameList", channel, msg)
end end
end end
--no topic --no topic
handlers["331"] = function(o, user, me, channel) handlers["331"] = function(o, prefix, me, channel)
o:invoke("OnTopic", channel, nil) o:invoke("OnTopic", channel, nil)
end end
--new topic --new topic
handlers["TOPIC"] = function(o, user, channel, topic) handlers["TOPIC"] = function(o, prefix, channel, topic)
o:invoke("OnTopic", channel, topic) o:invoke("OnTopic", channel, topic)
end end
handlers["332"] = function(o, user, me, channel, topic) handlers["332"] = function(o, prefix, me, channel, topic)
o:invoke("OnTopic", channel, topic) o:invoke("OnTopic", channel, topic)
end end
--topic creation info --topic creation info
handlers["333"] = function(o, user, me, channel, nick, time) handlers["333"] = function(o, prefix, me, channel, nick, time)
o:invoke("OnTopicInfo", channel, nick, tonumber(time)) o:invoke("OnTopicInfo", channel, nick, tonumber(time))
end end
handlers["KICK"] = function(o, user, channel, kicked, reason) handlers["KICK"] = function(o, prefix, channel, kicked, reason)
o:invoke("OnKick", channel, kicked, user, reason) o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason)
end end
--RPL_UMODEIS --RPL_UMODEIS
--To answer a query about a client's own mode, RPL_UMODEIS is sent back --To answer a query about a client's own mode, RPL_UMODEIS is sent back
handlers["221"] = function(o, user, user, modes) handlers["221"] = function(o, prefix, user, modes)
o:invoke("OnUserMode", modes) o:invoke("OnUserMode", modes)
end end
--RPL_CHANNELMODEIS --RPL_CHANNELMODEIS
--The result from common irc servers differs from that defined by the rfc --The result from common irc servers differs from that defined by the rfc
handlers["324"] = function(o, user, user, channel, modes) handlers["324"] = function(o, prefix, user, channel, modes)
o:invoke("OnChannelMode", channel, modes) o:invoke("OnChannelMode", channel, modes)
end end
handlers["MODE"] = function(o, user, target, modes, ...) handlers["MODE"] = function(o, prefix, target, modes, ...)
if o.track_users and target ~= o.nick then if o.track_users and target ~= o.nick then
local add = true local add = true
local optList = {...} local optList = {...}
updatePrefixModes(o)
for c in modes:gmatch(".") do for c in modes:gmatch(".") do
if c == "+" then add = true if c == "+" then add = true
elseif c == "-" then add = false elseif c == "-" then add = false
elseif o.modeprefix[c] then elseif c == "o" then
local nick = table.remove(optList, 1) local user = table.remove(optList, 1)
local access = o.channels[target].users[nick].access o.channels[target].users[user].access.op = add
access[o.modeprefix[c]] = add elseif c == "h" then
if c == "o" then access.op = add local user = table.remove(optList, 1)
elseif c == "v" then access.voice = add o.channels[target].users[user].access.halfop = add
end elseif c == "v" then
local user = table.remove(optList, 1)
o.channels[target].users[user].access.voice = add
end end
end end
end end
o:invoke("OnModeChange", user, target, modes, ...) o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
end end
handlers["ERROR"] = function(o, user, message) handlers["ERROR"] = function(o, prefix, message)
o:invoke("OnDisconnect", message, true) o:invoke("OnDisconnect", message, true)
o:shutdown() o:shutdown()
error(message, 3) error(message, 3)
end end

View File

@ -39,7 +39,6 @@ function new(data)
nickGenerator = data.nickGenerator or defaultNickGenerator; nickGenerator = data.nickGenerator or defaultNickGenerator;
hooks = {}; hooks = {};
track_users = true; track_users = true;
supports = {};
} }
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new") assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
return setmetatable(o, meta_preconnect) return setmetatable(o, meta_preconnect)
@ -183,12 +182,10 @@ end
local handlers = handlers local handlers = handlers
function meta:handle(prefix, cmd, params) function meta:handle(prefix, cmd, params)
local user = parsePrefix(prefix)
local handler = handlers[cmd] local handler = handlers[cmd]
if handler then if handler then
handler(self, user, unpack(params)) return handler(self, prefix, unpack(params))
end end
self:invoke("Do"..capitalize(cmd), user, unpack(params))
end end
local whoisHandlers = { local whoisHandlers = {

View File

@ -50,51 +50,29 @@ function parse(line)
return prefix, cmd, params return prefix, cmd, params
end end
function parseNick(conn, nick) function parseNick(nick)
local access = {} local access, name = nick:match("^([%+@]*)(.+)$")
updatePrefixModes(conn) return parseAccess(access or ""), name
local namestart = 1
for i = 1, #nick - 1 do
local c = nick:sub(i, i)
if conn.prefixmode[c] then
access[conn.prefixmode[c]] = true
else
namestart = i
break
end
end
access.op = access.o
access.voice = access.v
local name = nick:sub(namestart)
return access, name
end end
function parsePrefix(prefix) function parsePrefix(prefix)
local user = {} local user = {}
if prefix then if prefix then
user.nick, user.username, user.host = prefix:match("^(.+)!(.+)@(.+)$") user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
end end
user.access = parseAccess(user.access or "")
return user return user
end end
function updatePrefixModes(conn) function parseAccess(accessString)
if conn.prefixmode and conn.modeprefix then local access = {op = false, halfop = false, voice = false}
return for c in accessString:gmatch(".") do
end if c == "@" then access.op = true
conn.prefixmode = {} elseif c == "%" then access.halfop = true
conn.modeprefix = {} elseif c == "+" then access.voice = true
if conn.supports.PREFIX then
local modes, prefixes = conn.supports.PREFIX:match("%(([^%)]*)%)(.*)")
for i = 1, #modes do
conn.prefixmode[prefixes:sub(i, i)] = modes:sub(i, i)
conn.modeprefix[ modes:sub(i, i)] = prefixes:sub(i, i)
end end
else
conn.prefixmode['@'] = 'o'
conn.prefixmode['+'] = 'v'
conn.modeprefix['o'] = '@'
conn.modeprefix['v'] = '+'
end end
return access
end end
--mIRC markup scheme (de-facto standard) --mIRC markup scheme (de-facto standard)
@ -156,9 +134,3 @@ function defaultNickGenerator(nick)
return nick return nick
end end
function capitalize(text)
-- Converts first character to upercase and the rest to lowercase.
-- "PING" -> "Ping" | "hello" -> "Hello" | "123" -> "123"
return text:sub(1, 1):upper()..text:sub(2):lower()
end