mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-07-20 16:40:26 +02:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
a79c9451f6 |
@ -25,8 +25,9 @@
|
|||||||
module "irc"
|
module "irc"
|
||||||
|
|
||||||
--- Create a new IRC object. Use <code>irc:connect</code> to connect to a server.
|
--- 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</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.
|
-- @return Returns a new <code>irc</code> object.
|
||||||
function new(user)
|
function new(user)
|
||||||
|
42
handlers.lua
42
handlers.lua
@ -74,36 +74,6 @@ handlers["NICK"] = function(o, prefix, newnick)
|
|||||||
else
|
else
|
||||||
o:invoke("NickChange", user, newnick)
|
o:invoke("NickChange", user, newnick)
|
||||||
end
|
end
|
||||||
if user.nick == o.nick then
|
|
||||||
o.nick = newnick
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function needNewNick(o, prefix, 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
|
|
||||||
|
|
||||||
-- RPL_ISUPPORT
|
|
||||||
handlers["005"] = function(o, prefix, nick, ...)
|
|
||||||
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
|
end
|
||||||
|
|
||||||
--NAMES list
|
--NAMES list
|
||||||
@ -126,6 +96,18 @@ handlers["366"] = function(o, prefix, me, channel, msg)
|
|||||||
end
|
end
|
||||||
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
|
--no topic
|
||||||
handlers["331"] = function(o, prefix, me, channel)
|
handlers["331"] = function(o, prefix, me, channel)
|
||||||
o:invoke("OnTopic", channel, nil)
|
o:invoke("OnTopic", channel, nil)
|
||||||
|
13
init.lua
13
init.lua
@ -31,17 +31,16 @@ function meta_preconnect.__index(o, k)
|
|||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
|
|
||||||
function new(data)
|
function new(user)
|
||||||
local o = {
|
local o = {
|
||||||
nick = assert(data.nick, "Field 'nick' is required");
|
nick = assert(user.nick, "Field 'nick' is required");
|
||||||
username = data.username or "lua";
|
username = user.username or "lua";
|
||||||
realname = data.realname or "Lua owns";
|
realname = user.realname or "Lua owns";
|
||||||
nickGenerator = data.nickGenerator or defaultNickGenerator;
|
|
||||||
hooks = {};
|
hooks = {};
|
||||||
track_users = true;
|
track_users = true;
|
||||||
supports = {};
|
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)
|
return setmetatable(o, meta_preconnect)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
27
util.lua
27
util.lua
@ -1,12 +1,9 @@
|
|||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local sub = string.sub
|
|
||||||
local byte = string.byte
|
|
||||||
local char = string.char
|
local char = string.char
|
||||||
local table = table
|
local table = table
|
||||||
local assert = assert
|
local assert = assert
|
||||||
local tostring = tostring
|
local tostring = tostring
|
||||||
local type = type
|
local type = type
|
||||||
local random = math.random
|
|
||||||
|
|
||||||
module "irc"
|
module "irc"
|
||||||
|
|
||||||
@ -110,27 +107,3 @@ local underlineByte = char(31)
|
|||||||
function underline(text)
|
function underline(text)
|
||||||
return underlineByte..text..underlineByte
|
return underlineByte..text..underlineByte
|
||||||
end
|
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
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user