mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-07-20 16:40:26 +02:00
Compare commits
1 Commits
hook_any_c
...
nick_gen
Author | SHA1 | Date | |
---|---|---|---|
a79c9451f6 |
@ -25,8 +25,9 @@
|
||||
module "irc"
|
||||
|
||||
--- Create a new IRC object. Use <code>irc:connect</code> to connect to a server.
|
||||
-- @param user Table with fields <code>nick</code>, <code>username</code> and <code>realname</code>.
|
||||
-- @param user Table with fields <code>nick</code>, <code>username</code>, <code>realname</code>, and <code>nick_generator</code>.
|
||||
-- The <code>nick</code> field is required.
|
||||
-- The <code>nick_generator</code> field is a fuction that should return a new nick name given the old one.
|
||||
--
|
||||
-- @return Returns a new <code>irc</code> object.
|
||||
function new(user)
|
||||
@ -142,7 +143,6 @@ function irc:shutdown()
|
||||
-- <li><code>OnUserMode(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>DoX(user, ...)</code>'X' is any IRC command or numeric with the first letter capitalized (eg, DoPing and Do001)</li>
|
||||
-- </ul>
|
||||
-- * Event also invoked for yourself.
|
||||
-- <20> Channel passed only when user tracking is enabled
|
||||
|
77
handlers.lua
77
handlers.lua
@ -7,24 +7,25 @@ module "irc"
|
||||
|
||||
handlers = {}
|
||||
|
||||
handlers["PING"] = function(o, user, query)
|
||||
handlers["PING"] = function(o, prefix, query)
|
||||
o:send("PONG :%s", query)
|
||||
end
|
||||
|
||||
handlers["001"] = function(o, user, me)
|
||||
handlers["001"] = function(o, prefix, me)
|
||||
o.authed = true
|
||||
o.nick = me
|
||||
end
|
||||
|
||||
handlers["PRIVMSG"] = function(o, user, channel, message)
|
||||
o:invoke("OnChat", user, channel, message)
|
||||
handlers["PRIVMSG"] = function(o, prefix, channel, message)
|
||||
o:invoke("OnChat", parsePrefix(prefix), channel, message)
|
||||
end
|
||||
|
||||
handlers["NOTICE"] = function(o, user, channel, message)
|
||||
o:invoke("OnNotice", user, channel, message)
|
||||
handlers["NOTICE"] = function(o, prefix, channel, message)
|
||||
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
|
||||
end
|
||||
|
||||
handlers["JOIN"] = function(o, user, channel)
|
||||
handlers["JOIN"] = function(o, prefix, channel)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
if user.nick == o.nick then
|
||||
o.channels[channel] = {users = {}}
|
||||
@ -36,7 +37,8 @@ handlers["JOIN"] = function(o, user, channel)
|
||||
o:invoke("OnJoin", user, channel)
|
||||
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 user.nick == o.nick then
|
||||
o.channels[channel] = nil
|
||||
@ -47,7 +49,8 @@ handlers["PART"] = function(o, user, channel, reason)
|
||||
o:invoke("OnPart", user, channel, reason)
|
||||
end
|
||||
|
||||
handlers["QUIT"] = function(o, user, msg)
|
||||
handlers["QUIT"] = function(o, prefix, msg)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
for channel, v in pairs(o.channels) do
|
||||
v.users[user.nick] = nil
|
||||
@ -56,7 +59,8 @@ handlers["QUIT"] = function(o, user, msg)
|
||||
o:invoke("OnQuit", user, msg)
|
||||
end
|
||||
|
||||
handlers["NICK"] = function(o, user, newnick)
|
||||
handlers["NICK"] = function(o, prefix, newnick)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
for channel, v in pairs(o.channels) do
|
||||
local users = v.users
|
||||
@ -70,24 +74,10 @@ handlers["NICK"] = function(o, user, newnick)
|
||||
else
|
||||
o:invoke("NickChange", user, newnick)
|
||||
end
|
||||
if user.nick == o.nick then
|
||||
o.nick = newnick
|
||||
end
|
||||
end
|
||||
|
||||
local function needNewNick(o, user, target, badnick)
|
||||
local newnick = o.nickGenerator(badnick)
|
||||
o:send("NICK %s", newnick)
|
||||
end
|
||||
|
||||
-- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)
|
||||
handlers["432"] = needNewNick
|
||||
|
||||
-- ERR_NICKNAMEINUSE
|
||||
handlers["433"] = needNewNick
|
||||
|
||||
--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
|
||||
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
|
||||
|
||||
@ -100,48 +90,60 @@ handlers["353"] = function(o, user, me, chanType, channel, names)
|
||||
end
|
||||
|
||||
--end of NAMES
|
||||
handlers["366"] = function(o, user, me, channel, msg)
|
||||
handlers["366"] = function(o, prefix, me, channel, msg)
|
||||
if o.track_users then
|
||||
o:invoke("NameList", channel, msg)
|
||||
end
|
||||
end
|
||||
|
||||
local function handle_bad_nick(o, prefix, x, badnick)
|
||||
o.nick = o.nick_generator(badnick)
|
||||
o.send("NICK %s", o.nick)
|
||||
o.send("USER %s 0 * :%s", o.username, o.realname)
|
||||
end
|
||||
|
||||
-- ERR_ERRONEOUSNICKNAME
|
||||
handlers["432"] = handle_bad_nick
|
||||
|
||||
-- ERR_NICKNAMEINUSE
|
||||
handlers["433"] = handle_bad_nick
|
||||
|
||||
--no topic
|
||||
handlers["331"] = function(o, user, me, channel)
|
||||
handlers["331"] = function(o, prefix, me, channel)
|
||||
o:invoke("OnTopic", channel, nil)
|
||||
end
|
||||
|
||||
--new topic
|
||||
handlers["TOPIC"] = function(o, user, channel, topic)
|
||||
handlers["TOPIC"] = function(o, prefix, channel, topic)
|
||||
o:invoke("OnTopic", channel, topic)
|
||||
end
|
||||
|
||||
handlers["332"] = function(o, user, me, channel, topic)
|
||||
handlers["332"] = function(o, prefix, me, channel, topic)
|
||||
o:invoke("OnTopic", channel, topic)
|
||||
end
|
||||
|
||||
--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))
|
||||
end
|
||||
|
||||
handlers["KICK"] = function(o, user, channel, kicked, reason)
|
||||
o:invoke("OnKick", channel, kicked, user, reason)
|
||||
handlers["KICK"] = function(o, prefix, channel, kicked, reason)
|
||||
o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason)
|
||||
end
|
||||
|
||||
--RPL_UMODEIS
|
||||
--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)
|
||||
end
|
||||
|
||||
--RPL_CHANNELMODEIS
|
||||
--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)
|
||||
end
|
||||
|
||||
handlers["MODE"] = function(o, user, target, modes, ...)
|
||||
handlers["MODE"] = function(o, prefix, target, modes, ...)
|
||||
if o.track_users and target ~= o.nick then
|
||||
local add = true
|
||||
local optList = {...}
|
||||
@ -160,12 +162,11 @@ handlers["MODE"] = function(o, user, target, modes, ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
o:invoke("OnModeChange", user, target, modes, ...)
|
||||
o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
|
||||
end
|
||||
|
||||
handlers["ERROR"] = function(o, user, message)
|
||||
handlers["ERROR"] = function(o, prefix, message)
|
||||
o:invoke("OnDisconnect", message, true)
|
||||
o:shutdown()
|
||||
error(message, 3)
|
||||
end
|
||||
|
||||
|
16
init.lua
16
init.lua
@ -31,16 +31,16 @@ function meta_preconnect.__index(o, k)
|
||||
return v
|
||||
end
|
||||
|
||||
function new(data)
|
||||
function new(user)
|
||||
local o = {
|
||||
nick = assert(data.nick, "Field 'nick' is required");
|
||||
username = data.username or "lua";
|
||||
realname = data.realname or "Lua owns";
|
||||
nickGenerator = data.nickGenerator or defaultNickGenerator;
|
||||
nick = assert(user.nick, "Field 'nick' is required");
|
||||
username = user.username or "lua";
|
||||
realname = user.realname or "Lua owns";
|
||||
hooks = {};
|
||||
track_users = true;
|
||||
nick_generator = user.nick_generator or
|
||||
function(oldnick) error("Nick already in use.") end
|
||||
}
|
||||
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
|
||||
return setmetatable(o, meta_preconnect)
|
||||
end
|
||||
|
||||
@ -182,12 +182,10 @@ end
|
||||
local handlers = handlers
|
||||
|
||||
function meta:handle(prefix, cmd, params)
|
||||
local user = parsePrefix(prefix)
|
||||
local handler = handlers[cmd]
|
||||
if handler then
|
||||
handler(self, user, unpack(params))
|
||||
return handler(self, prefix, unpack(params))
|
||||
end
|
||||
self:invoke("Do"..capitalize(cmd), user, unpack(params))
|
||||
end
|
||||
|
||||
local whoisHandlers = {
|
||||
|
33
util.lua
33
util.lua
@ -1,12 +1,9 @@
|
||||
local setmetatable = setmetatable
|
||||
local sub = string.sub
|
||||
local byte = string.byte
|
||||
local char = string.char
|
||||
local table = table
|
||||
local assert = assert
|
||||
local tostring = tostring
|
||||
local type = type
|
||||
local random = math.random
|
||||
|
||||
module "irc"
|
||||
|
||||
@ -110,33 +107,3 @@ local underlineByte = char(31)
|
||||
function underline(text)
|
||||
return underlineByte..text..underlineByte
|
||||
end
|
||||
|
||||
function checkNick(nick)
|
||||
return nick:find("^[a-zA-Z_%-%[|%]%^{|}`][a-zA-Z0-9_%-%[|%]%^{|}`]*$") ~= nil
|
||||
end
|
||||
|
||||
function defaultNickGenerator(nick)
|
||||
-- LuaBot -> LuaCot -> LuaCou -> ...
|
||||
-- We change a random charachter rather than appending to the
|
||||
-- nickname as otherwise the new nick could exceed the ircd's
|
||||
-- maximum nickname length.
|
||||
local randindex = random(1, #nick)
|
||||
local randchar = sub(nick, randindex, randindex)
|
||||
local b = byte(randchar)
|
||||
b = b + 1
|
||||
if b < 65 or b > 125 then
|
||||
b = 65
|
||||
end
|
||||
-- Get the halves before and after the changed character
|
||||
local first = sub(nick, 1, randindex - 1)
|
||||
local last = sub(nick, randindex + 1, #nick)
|
||||
nick = first..char(b)..last -- Insert the new charachter
|
||||
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
|
||||
|
||||
|
Reference in New Issue
Block a user