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 |
@ -21,8 +21,6 @@ function meta:send(msg, ...)
|
||||
end
|
||||
end
|
||||
|
||||
meta.queue = meta.send
|
||||
|
||||
local function verify(str, errLevel)
|
||||
if str:find("^:") or str:find("%s%z") then
|
||||
error(("malformed parameter '%s' to irc command"):format(str), errLevel)
|
||||
@ -34,28 +32,28 @@ end
|
||||
function meta:sendChat(target, msg)
|
||||
-- Split the message into segments if it includes newlines.
|
||||
for line in msg:gmatch("([^\r\n]+)") do
|
||||
self:queue("PRIVMSG %s :%s", verify(target, 3), line)
|
||||
self:send("PRIVMSG %s :%s", verify(target, 3), line)
|
||||
end
|
||||
end
|
||||
|
||||
function meta:sendNotice(target, msg)
|
||||
-- Split the message into segments if it includes newlines.
|
||||
for line in msg:gmatch("([^\r\n]+)") do
|
||||
self:queue("NOTICE %s :%s", verify(target, 3), line)
|
||||
self:send("NOTICE %s :%s", verify(target, 3), line)
|
||||
end
|
||||
end
|
||||
|
||||
function meta:join(channel, key)
|
||||
if key then
|
||||
self:queue("JOIN %s :%s", verify(channel, 3), verify(key, 3))
|
||||
self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3))
|
||||
else
|
||||
self:queue("JOIN %s", verify(channel, 3))
|
||||
self:send("JOIN %s", verify(channel, 3))
|
||||
end
|
||||
end
|
||||
|
||||
function meta:part(channel)
|
||||
channel = verify(channel, 3)
|
||||
self:queue("PART %s", channel)
|
||||
self:send("PART %s", channel)
|
||||
if self.track_users then
|
||||
self.channels[channel] = nil
|
||||
end
|
||||
@ -85,5 +83,5 @@ function meta:setMode(t)
|
||||
mode = table.concat{mode, "-", verify(rem, 3)}
|
||||
end
|
||||
|
||||
self:queue("MODE %s %s", verify(target, 3), mode)
|
||||
self:send("MODE %s %s", verify(target, 3), mode)
|
||||
end
|
||||
|
@ -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)
|
||||
@ -159,12 +160,3 @@ function irc:shutdown()
|
||||
-- Apart from <code>nick</code>, fields may be missing. To fill them in, enable user tracking and use irc:whois.
|
||||
-- @name User
|
||||
-- @class table
|
||||
|
||||
-- If you need a simple queue you can enable one by requiring "irc.queue".
|
||||
-- Doind so will add the following features:
|
||||
|
||||
--- Queue a raw line of IRC to be sent to the server as soon as possible.
|
||||
-- @param msg Line to be sent, excluding newline characters.
|
||||
-- @param ... Format parameters for <code>msg</code>, with <code>string.format</code> semantics. [optional]
|
||||
function irc:queue(msg, ...)
|
||||
|
||||
|
28
handlers.lua
28
handlers.lua
@ -8,7 +8,7 @@ module "irc"
|
||||
handlers = {}
|
||||
|
||||
handlers["PING"] = function(o, prefix, query)
|
||||
o:queue("PONG :%s", query)
|
||||
o:send("PONG :%s", query)
|
||||
end
|
||||
|
||||
handlers["001"] = function(o, prefix, me)
|
||||
@ -74,21 +74,7 @@ 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:queue("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)
|
||||
@ -110,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)
|
||||
|
24
init.lua
24
init.lua
@ -31,16 +31,16 @@ function meta_preconnect.__index(o, k)
|
||||
return v
|
||||
end
|
||||
|
||||
function new(data)
|
||||
function new(user)
|
||||
local o = {
|
||||
nick = assert(data.nick, "Field 'nick' is required");
|
||||
username = data.username or "lua";
|
||||
realname = data.realname or "Lua owns";
|
||||
nickGenerator = data.nickGenerator or defaultNickGenerator;
|
||||
nick = assert(user.nick, "Field 'nick' is required");
|
||||
username = user.username or "lua";
|
||||
realname = user.realname or "Lua owns";
|
||||
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
|
||||
|
||||
@ -119,17 +119,17 @@ function meta_preconnect:connect(_host, _port)
|
||||
self.socket = s
|
||||
setmetatable(self, meta)
|
||||
|
||||
self:queue("CAP REQ multi-prefix")
|
||||
self:send("CAP REQ multi-prefix")
|
||||
|
||||
self:invoke("PreRegister", self)
|
||||
self:queue("CAP END")
|
||||
self:send("CAP END")
|
||||
|
||||
if password then
|
||||
self:queue("PASS %s", password)
|
||||
self:send("PASS %s", password)
|
||||
end
|
||||
|
||||
self:queue("NICK %s", self.nick)
|
||||
self:queue("USER %s 0 * :%s", self.username, self.realname)
|
||||
self:send("NICK %s", self.nick)
|
||||
self:send("USER %s 0 * :%s", self.username, self.realname)
|
||||
|
||||
self.channels = {}
|
||||
|
||||
@ -227,6 +227,6 @@ function meta:whois(nick)
|
||||
end
|
||||
|
||||
function meta:topic(channel)
|
||||
self:queue("TOPIC %s", channel)
|
||||
self:send("TOPIC %s", channel)
|
||||
end
|
||||
|
||||
|
45
queue.lua
45
queue.lua
@ -1,45 +0,0 @@
|
||||
local getmetatable = getmetatable
|
||||
local clock = os.clock
|
||||
local remove = table.remove
|
||||
local insert = table.insert
|
||||
local select = select
|
||||
|
||||
module "irc"
|
||||
|
||||
local meta = _META
|
||||
|
||||
local old_new = new
|
||||
function new(...)
|
||||
local o = old_new(...)
|
||||
o.messageQueue = {}
|
||||
o.lastThought = 0
|
||||
o.recentMessages = 0
|
||||
return o
|
||||
end
|
||||
|
||||
local old_think = meta.think
|
||||
function meta:think(...)
|
||||
old_think(self, ...) -- Call old meta:think
|
||||
|
||||
-- Handle outgoing message queue
|
||||
self.recentMessages = self.recentMessages - ((clock() - self.lastThought) * 8000)
|
||||
if self.recentMessages < 0 then
|
||||
self.recentMessages = 0
|
||||
end
|
||||
for i = 1, #self.messageQueue do
|
||||
if self.recentMessages > 4 then
|
||||
break
|
||||
end
|
||||
self:send(remove(self.messageQueue, 1))
|
||||
self.recentMessages = self.recentMessages + 1
|
||||
end
|
||||
self.lastThought = clock()
|
||||
end
|
||||
|
||||
function meta:queue(msg, ...)
|
||||
if select("#", ...) > 0 then
|
||||
msg = msg:format(...)
|
||||
end
|
||||
insert(self.messageQueue, msg)
|
||||
end
|
||||
|
27
util.lua
27
util.lua
@ -1,12 +1,9 @@
|
||||
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"
|
||||
|
||||
@ -110,27 +107,3 @@ 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
|
||||
|
||||
|
Reference in New Issue
Block a user