diff --git a/doc/irc.luadoc b/doc/irc.luadoc
index c8c5d8e..34b2a4d 100644
--- a/doc/irc.luadoc
+++ b/doc/irc.luadoc
@@ -142,6 +142,7 @@ function irc:shutdown()
--
OnUserMode(modes)
-- OnChannelMode(user, channel, modes)
-- OnModeChange(user, target, modes, ...)
* ('...' contains mode options such as banmasks)
+-- DoX(user, ...)
'X' is any IRC command or numeric with the first letter capitalized (eg, DoPing and Do001)
--
-- * Event also invoked for yourself.
-- † Channel passed only when user tracking is enabled
diff --git a/handlers.lua b/handlers.lua
index d6abb12..56314bd 100644
--- a/handlers.lua
+++ b/handlers.lua
@@ -7,25 +7,24 @@ module "irc"
handlers = {}
-handlers["PING"] = function(o, prefix, query)
+handlers["PING"] = function(o, user, query)
o:send("PONG :%s", query)
end
-handlers["001"] = function(o, prefix, me)
+handlers["001"] = function(o, user, me)
o.authed = true
o.nick = me
end
-handlers["PRIVMSG"] = function(o, prefix, channel, message)
- o:invoke("OnChat", parsePrefix(prefix), channel, message)
+handlers["PRIVMSG"] = function(o, user, channel, message)
+ o:invoke("OnChat", user, channel, message)
end
-handlers["NOTICE"] = function(o, prefix, channel, message)
- o:invoke("OnNotice", parsePrefix(prefix), channel, message)
+handlers["NOTICE"] = function(o, user, channel, message)
+ o:invoke("OnNotice", user, channel, message)
end
-handlers["JOIN"] = function(o, prefix, channel)
- local user = parsePrefix(prefix)
+handlers["JOIN"] = function(o, user, channel)
if o.track_users then
if user.nick == o.nick then
o.channels[channel] = {users = {}}
@@ -37,8 +36,7 @@ handlers["JOIN"] = function(o, prefix, channel)
o:invoke("OnJoin", user, channel)
end
-handlers["PART"] = function(o, prefix, channel, reason)
- local user = parsePrefix(prefix)
+handlers["PART"] = function(o, user, channel, reason)
if o.track_users then
if user.nick == o.nick then
o.channels[channel] = nil
@@ -49,8 +47,7 @@ handlers["PART"] = function(o, prefix, channel, reason)
o:invoke("OnPart", user, channel, reason)
end
-handlers["QUIT"] = function(o, prefix, msg)
- local user = parsePrefix(prefix)
+handlers["QUIT"] = function(o, user, msg)
if o.track_users then
for channel, v in pairs(o.channels) do
v.users[user.nick] = nil
@@ -59,8 +56,7 @@ handlers["QUIT"] = function(o, prefix, msg)
o:invoke("OnQuit", user, msg)
end
-handlers["NICK"] = function(o, prefix, newnick)
- local user = parsePrefix(prefix)
+handlers["NICK"] = function(o, user, newnick)
if o.track_users then
for channel, v in pairs(o.channels) do
local users = v.users
@@ -79,7 +75,7 @@ handlers["NICK"] = function(o, prefix, newnick)
end
end
-local function needNewNick(o, prefix, target, badnick)
+local function needNewNick(o, user, target, badnick)
local newnick = o.nickGenerator(badnick)
o:send("NICK %s", newnick)
end
@@ -107,7 +103,7 @@ handlers["005"] = function(o, prefix, nick, ...)
end
--NAMES list
-handlers["353"] = function(o, prefix, me, chanType, channel, names)
+handlers["353"] = function(o, user, me, chanType, channel, names)
if o.track_users then
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
@@ -120,48 +116,48 @@ handlers["353"] = function(o, prefix, me, chanType, channel, names)
end
--end of NAMES
-handlers["366"] = function(o, prefix, me, channel, msg)
+handlers["366"] = function(o, user, me, channel, msg)
if o.track_users then
o:invoke("NameList", channel, msg)
end
end
--no topic
-handlers["331"] = function(o, prefix, me, channel)
+handlers["331"] = function(o, user, me, channel)
o:invoke("OnTopic", channel, nil)
end
--new topic
-handlers["TOPIC"] = function(o, prefix, channel, topic)
+handlers["TOPIC"] = function(o, user, channel, topic)
o:invoke("OnTopic", channel, topic)
end
-handlers["332"] = function(o, prefix, me, channel, topic)
+handlers["332"] = function(o, user, me, channel, topic)
o:invoke("OnTopic", channel, topic)
end
--topic creation info
-handlers["333"] = function(o, prefix, me, channel, nick, time)
+handlers["333"] = function(o, user, me, channel, nick, time)
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
end
-handlers["KICK"] = function(o, prefix, channel, kicked, reason)
- o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason)
+handlers["KICK"] = function(o, user, channel, kicked, reason)
+ o:invoke("OnKick", channel, kicked, user, reason)
end
--RPL_UMODEIS
--To answer a query about a client's own mode, RPL_UMODEIS is sent back
-handlers["221"] = function(o, prefix, user, modes)
+handlers["221"] = function(o, user, user, modes)
o:invoke("OnUserMode", modes)
end
--RPL_CHANNELMODEIS
--The result from common irc servers differs from that defined by the rfc
-handlers["324"] = function(o, prefix, user, channel, modes)
+handlers["324"] = function(o, user, user, channel, modes)
o:invoke("OnChannelMode", channel, modes)
end
-handlers["MODE"] = function(o, prefix, target, modes, ...)
+handlers["MODE"] = function(o, user, target, modes, ...)
if o.track_users and target ~= o.nick then
local add = true
local optList = {...}
@@ -180,11 +176,12 @@ handlers["MODE"] = function(o, prefix, target, modes, ...)
end
end
end
- o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
+ o:invoke("OnModeChange", user, target, modes, ...)
end
-handlers["ERROR"] = function(o, prefix, message)
+handlers["ERROR"] = function(o, user, message)
o:invoke("OnDisconnect", message, true)
o:shutdown()
error(message, 3)
end
+
diff --git a/init.lua b/init.lua
index 5537ac3..2229ce2 100644
--- a/init.lua
+++ b/init.lua
@@ -183,10 +183,12 @@ end
local handlers = handlers
function meta:handle(prefix, cmd, params)
+ local user = parsePrefix(prefix)
local handler = handlers[cmd]
if handler then
- return handler(self, prefix, unpack(params))
+ handler(self, user, unpack(params))
end
+ self:invoke("Do"..capitalize(cmd), user, unpack(params))
end
local whoisHandlers = {
diff --git a/util.lua b/util.lua
index 2bdb222..0b22e91 100644
--- a/util.lua
+++ b/util.lua
@@ -134,3 +134,9 @@ function defaultNickGenerator(nick)
return nick
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
+