1
0
mirror of https://github.com/ShadowNinja/LuaIRC.git synced 2025-07-20 16:40:26 +02:00

5 Commits

Author SHA1 Message Date
2ccee2a935 Sleep between thoughts when connecting to avoid eating CPU 2014-02-22 17:44:03 -05:00
b1cbbf1963 Merge pull request #14 from ShadowNinja/nickgen
Generate a new nickname when it is in use or erroneous and check the...
2013-10-07 20:27:41 -07:00
5ab24e9ad1 Generate a new nickname when it is in use or erroneous and check the
provided nickname for validity
2013-10-07 16:15:27 -04:00
be620c428e Merge pull request #12 from ShadowNinja/master
Track own nick changes
2013-08-22 05:49:26 -07:00
8a2e47a326 Track own nick changes 2013-08-05 18:33:33 -04:00
4 changed files with 49 additions and 20 deletions

View File

@ -25,9 +25,8 @@
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>, <code>realname</code>, and <code>nick_generator</code>.
-- @param user Table with fields <code>nick</code>, <code>username</code> and <code>realname</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

@ -74,7 +74,21 @@ handlers["NICK"] = function(o, prefix, newnick)
else
o:invoke("NickChange", user, newnick)
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
--NAMES list
handlers["353"] = function(o, prefix, me, chanType, channel, names)
@ -96,18 +110,6 @@ 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

@ -31,16 +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;
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
@ -137,6 +137,7 @@ function meta_preconnect:connect(_host, _port)
repeat
self:think()
socket.select(nil, nil, 0.1) -- Sleep so that we don't eat CPU
until self.authed
end

View File

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