mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-01-09 17:40:29 +01:00
Generate a new nickname when it is in use or erroneous and check the
provided nickname for validity
This commit is contained in:
parent
8a2e47a326
commit
5ab24e9ad1
11
handlers.lua
11
handlers.lua
@ -79,6 +79,17 @@ handlers["NICK"] = function(o, prefix, newnick)
|
|||||||
end
|
end
|
||||||
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
|
||||||
|
|
||||||
--NAMES list
|
--NAMES list
|
||||||
handlers["353"] = function(o, prefix, me, chanType, channel, names)
|
handlers["353"] = function(o, prefix, me, chanType, channel, names)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
|
10
init.lua
10
init.lua
@ -31,14 +31,16 @@ function meta_preconnect.__index(o, k)
|
|||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
|
|
||||||
function new(user)
|
function new(data)
|
||||||
local o = {
|
local o = {
|
||||||
nick = assert(user.nick, "Field 'nick' is required");
|
nick = assert(data.nick, "Field 'nick' is required");
|
||||||
username = user.username or "lua";
|
username = data.username or "lua";
|
||||||
realname = user.realname or "Lua owns";
|
realname = data.realname or "Lua owns";
|
||||||
|
nickGenerator = data.nickGenerator or defaultNickGenerator;
|
||||||
hooks = {};
|
hooks = {};
|
||||||
track_users = true;
|
track_users = true;
|
||||||
}
|
}
|
||||||
|
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,9 +1,12 @@
|
|||||||
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"
|
||||||
|
|
||||||
@ -107,3 +110,27 @@ 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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user