Skip to content

Commit

Permalink
Merge pull request JakobOvrum#14 from ShadowNinja/nickgen
Browse files Browse the repository at this point in the history
Generate a new nickname when it is in use or erroneous and check the...
  • Loading branch information
JakobOvrum committed Oct 8, 2013
2 parents be620c4 + 5ab24e9 commit b1cbbf1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
11 changes: 11 additions & 0 deletions handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ handlers["NICK"] = function(o, prefix, 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

--NAMES list
handlers["353"] = function(o, prefix, me, chanType, channel, names)
if o.track_users then
Expand Down
10 changes: 6 additions & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ function meta_preconnect.__index(o, k)
return v
end

function new(user)
function new(data)
local o = {
nick = assert(user.nick, "Field 'nick' is required");
username = user.username or "lua";
realname = user.realname or "Lua owns";
nick = assert(data.nick, "Field 'nick' is required");
username = data.username or "lua";
realname = data.realname or "Lua owns";
nickGenerator = data.nickGenerator or defaultNickGenerator;
hooks = {};
track_users = true;
}
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
return setmetatable(o, meta_preconnect)
end

Expand Down
27 changes: 27 additions & 0 deletions util.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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"

Expand Down Expand Up @@ -107,3 +110,27 @@ 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

0 comments on commit b1cbbf1

Please sign in to comment.