Generate a new nick if it is used or erroneous

This commit is contained in:
ShadowNinja 2013-07-16 13:00:51 -04:00
parent bc79606de0
commit a79c9451f6
3 changed files with 16 additions and 1 deletions

View File

@ -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)

View File

@ -96,6 +96,18 @@ handlers["366"] = function(o, prefix, me, 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, prefix, me, channel)
o:invoke("OnTopic", channel, nil)

View File

@ -38,6 +38,8 @@ function new(user)
realname = user.realname or "Lua owns";
hooks = {};
track_users = true;
nick_generator = user.nick_generator or
function(oldnick) error("Nick already in use.") end
}
return setmetatable(o, meta_preconnect)
end