mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-07-20 16:40:26 +02:00
Compare commits
1 Commits
statusmode
...
queue
Author | SHA1 | Date | |
---|---|---|---|
7f475070dd |
@ -21,6 +21,8 @@ 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)
|
||||
@ -32,28 +34,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:send("PRIVMSG %s :%s", verify(target, 3), line)
|
||||
self:queue("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:send("NOTICE %s :%s", verify(target, 3), line)
|
||||
self:queue("NOTICE %s :%s", verify(target, 3), line)
|
||||
end
|
||||
end
|
||||
|
||||
function meta:join(channel, key)
|
||||
if key then
|
||||
self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3))
|
||||
self:queue("JOIN %s :%s", verify(channel, 3), verify(key, 3))
|
||||
else
|
||||
self:send("JOIN %s", verify(channel, 3))
|
||||
self:queue("JOIN %s", verify(channel, 3))
|
||||
end
|
||||
end
|
||||
|
||||
function meta:part(channel)
|
||||
channel = verify(channel, 3)
|
||||
self:send("PART %s", channel)
|
||||
self:queue("PART %s", channel)
|
||||
if self.track_users then
|
||||
self.channels[channel] = nil
|
||||
end
|
||||
@ -83,5 +85,5 @@ function meta:setMode(t)
|
||||
mode = table.concat{mode, "-", verify(rem, 3)}
|
||||
end
|
||||
|
||||
self:send("MODE %s %s", verify(target, 3), mode)
|
||||
self:queue("MODE %s %s", verify(target, 3), mode)
|
||||
end
|
||||
|
@ -142,7 +142,6 @@ function irc:shutdown()
|
||||
-- <li><code>OnUserMode(modes)</code></li>
|
||||
-- <li><code>OnChannelMode(user, channel, modes)</code></li>
|
||||
-- <li><code>OnModeChange(user, target, modes, ...)</code>* ('...' contains mode options such as banmasks)</li>
|
||||
-- <li><code>DoX(user, ...)</code>'X' is any IRC command or numeric with the first letter capitalized (eg, DoPing and Do001)</li>
|
||||
-- </ul>
|
||||
-- * Event also invoked for yourself.
|
||||
-- <20> Channel passed only when user tracking is enabled
|
||||
@ -155,8 +154,17 @@ function irc:shutdown()
|
||||
-- <li><code>username</code> - User username.</li>
|
||||
-- <li><code>host</code> - User hostname.</li>
|
||||
-- <li><code>realname</code> - User real name.</li>
|
||||
-- <li><code>access</code> - User access, available in channel-oriented callbacks. A table containing boolean fields for each access mode that the server supports. Eg: 'o', and 'v'.</li>
|
||||
-- <li><code>access</code> - User access, available in channel-oriented callbacks. A table containing the boolean fields 'op', 'halfop', and 'voice'.</li>
|
||||
-- </ul>
|
||||
-- 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, ...)
|
||||
|
||||
|
92
handlers.lua
92
handlers.lua
@ -7,24 +7,25 @@ module "irc"
|
||||
|
||||
handlers = {}
|
||||
|
||||
handlers["PING"] = function(o, user, query)
|
||||
o:send("PONG :%s", query)
|
||||
handlers["PING"] = function(o, prefix, query)
|
||||
o:queue("PONG :%s", query)
|
||||
end
|
||||
|
||||
handlers["001"] = function(o, user, me)
|
||||
handlers["001"] = function(o, prefix, me)
|
||||
o.authed = true
|
||||
o.nick = me
|
||||
end
|
||||
|
||||
handlers["PRIVMSG"] = function(o, user, channel, message)
|
||||
o:invoke("OnChat", user, channel, message)
|
||||
handlers["PRIVMSG"] = function(o, prefix, channel, message)
|
||||
o:invoke("OnChat", parsePrefix(prefix), channel, message)
|
||||
end
|
||||
|
||||
handlers["NOTICE"] = function(o, user, channel, message)
|
||||
o:invoke("OnNotice", user, channel, message)
|
||||
handlers["NOTICE"] = function(o, prefix, channel, message)
|
||||
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
|
||||
end
|
||||
|
||||
handlers["JOIN"] = function(o, user, channel)
|
||||
handlers["JOIN"] = function(o, prefix, channel)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
if user.nick == o.nick then
|
||||
o.channels[channel] = {users = {}}
|
||||
@ -36,7 +37,8 @@ handlers["JOIN"] = function(o, user, channel)
|
||||
o:invoke("OnJoin", user, channel)
|
||||
end
|
||||
|
||||
handlers["PART"] = function(o, user, channel, reason)
|
||||
handlers["PART"] = function(o, prefix, channel, reason)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
if user.nick == o.nick then
|
||||
o.channels[channel] = nil
|
||||
@ -47,7 +49,8 @@ handlers["PART"] = function(o, user, channel, reason)
|
||||
o:invoke("OnPart", user, channel, reason)
|
||||
end
|
||||
|
||||
handlers["QUIT"] = function(o, user, msg)
|
||||
handlers["QUIT"] = function(o, prefix, msg)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
for channel, v in pairs(o.channels) do
|
||||
v.users[user.nick] = nil
|
||||
@ -56,7 +59,8 @@ handlers["QUIT"] = function(o, user, msg)
|
||||
o:invoke("OnQuit", user, msg)
|
||||
end
|
||||
|
||||
handlers["NICK"] = function(o, user, newnick)
|
||||
handlers["NICK"] = function(o, prefix, newnick)
|
||||
local user = parsePrefix(prefix)
|
||||
if o.track_users then
|
||||
for channel, v in pairs(o.channels) do
|
||||
local users = v.users
|
||||
@ -75,9 +79,9 @@ handlers["NICK"] = function(o, user, newnick)
|
||||
end
|
||||
end
|
||||
|
||||
local function needNewNick(o, user, target, badnick)
|
||||
local function needNewNick(o, prefix, target, badnick)
|
||||
local newnick = o.nickGenerator(badnick)
|
||||
o:send("NICK %s", newnick)
|
||||
o:queue("NICK %s", newnick)
|
||||
end
|
||||
|
||||
-- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)
|
||||
@ -86,101 +90,85 @@ handlers["432"] = needNewNick
|
||||
-- ERR_NICKNAMEINUSE
|
||||
handlers["433"] = needNewNick
|
||||
|
||||
-- RPL_ISUPPORT
|
||||
handlers["005"] = function(o, prefix, nick, ...)
|
||||
local list = {...}
|
||||
local listlen = #list
|
||||
-- Skip last parameter (info)
|
||||
for i = 1, listlen - 1 do
|
||||
local item = list[i]
|
||||
local pos = item:find("=")
|
||||
if pos then
|
||||
o.supports[item:sub(1, pos - 1)] = item:sub(pos + 1)
|
||||
else
|
||||
o.supports[item] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--NAMES list
|
||||
handlers["353"] = function(o, user, me, chanType, channel, names)
|
||||
handlers["353"] = function(o, prefix, me, chanType, channel, names)
|
||||
if o.track_users then
|
||||
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
|
||||
|
||||
local users = o.channels[channel].users
|
||||
for nick in names:gmatch("(%S+)") do
|
||||
local access, name = parseNick(o, nick)
|
||||
local access, name = parseNick(nick)
|
||||
users[name] = {access = access}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--end of NAMES
|
||||
handlers["366"] = function(o, user, me, channel, msg)
|
||||
handlers["366"] = function(o, prefix, me, channel, msg)
|
||||
if o.track_users then
|
||||
o:invoke("NameList", channel, msg)
|
||||
end
|
||||
end
|
||||
|
||||
--no topic
|
||||
handlers["331"] = function(o, user, me, channel)
|
||||
handlers["331"] = function(o, prefix, me, channel)
|
||||
o:invoke("OnTopic", channel, nil)
|
||||
end
|
||||
|
||||
--new topic
|
||||
handlers["TOPIC"] = function(o, user, channel, topic)
|
||||
handlers["TOPIC"] = function(o, prefix, channel, topic)
|
||||
o:invoke("OnTopic", channel, topic)
|
||||
end
|
||||
|
||||
handlers["332"] = function(o, user, me, channel, topic)
|
||||
handlers["332"] = function(o, prefix, me, channel, topic)
|
||||
o:invoke("OnTopic", channel, topic)
|
||||
end
|
||||
|
||||
--topic creation info
|
||||
handlers["333"] = function(o, user, me, channel, nick, time)
|
||||
handlers["333"] = function(o, prefix, me, channel, nick, time)
|
||||
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
|
||||
end
|
||||
|
||||
handlers["KICK"] = function(o, user, channel, kicked, reason)
|
||||
o:invoke("OnKick", channel, kicked, user, reason)
|
||||
handlers["KICK"] = function(o, prefix, channel, kicked, reason)
|
||||
o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason)
|
||||
end
|
||||
|
||||
--RPL_UMODEIS
|
||||
--To answer a query about a client's own mode, RPL_UMODEIS is sent back
|
||||
handlers["221"] = function(o, user, user, modes)
|
||||
handlers["221"] = function(o, prefix, user, modes)
|
||||
o:invoke("OnUserMode", modes)
|
||||
end
|
||||
|
||||
--RPL_CHANNELMODEIS
|
||||
--The result from common irc servers differs from that defined by the rfc
|
||||
handlers["324"] = function(o, user, user, channel, modes)
|
||||
handlers["324"] = function(o, prefix, user, channel, modes)
|
||||
o:invoke("OnChannelMode", channel, modes)
|
||||
end
|
||||
|
||||
handlers["MODE"] = function(o, user, target, modes, ...)
|
||||
handlers["MODE"] = function(o, prefix, target, modes, ...)
|
||||
if o.track_users and target ~= o.nick then
|
||||
local add = true
|
||||
local optList = {...}
|
||||
updatePrefixModes(o)
|
||||
for c in modes:gmatch(".") do
|
||||
if c == "+" then add = true
|
||||
elseif c == "-" then add = false
|
||||
elseif o.modeprefix[c] then
|
||||
local nick = table.remove(optList, 1)
|
||||
local access = o.channels[target].users[nick].access
|
||||
access[o.modeprefix[c]] = add
|
||||
if c == "o" then access.op = add
|
||||
elseif c == "v" then access.voice = add
|
||||
elseif c == "o" then
|
||||
local user = table.remove(optList, 1)
|
||||
o.channels[target].users[user].access.op = add
|
||||
elseif c == "h" then
|
||||
local user = table.remove(optList, 1)
|
||||
o.channels[target].users[user].access.halfop = add
|
||||
elseif c == "v" then
|
||||
local user = table.remove(optList, 1)
|
||||
o.channels[target].users[user].access.voice = add
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
o:invoke("OnModeChange", user, target, modes, ...)
|
||||
o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
|
||||
end
|
||||
|
||||
handlers["ERROR"] = function(o, user, message)
|
||||
handlers["ERROR"] = function(o, prefix, message)
|
||||
o:invoke("OnDisconnect", message, true)
|
||||
o:shutdown()
|
||||
error(message, 3)
|
||||
end
|
||||
|
||||
|
17
init.lua
17
init.lua
@ -39,7 +39,6 @@ function new(data)
|
||||
nickGenerator = data.nickGenerator or defaultNickGenerator;
|
||||
hooks = {};
|
||||
track_users = true;
|
||||
supports = {};
|
||||
}
|
||||
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
|
||||
return setmetatable(o, meta_preconnect)
|
||||
@ -120,17 +119,17 @@ function meta_preconnect:connect(_host, _port)
|
||||
self.socket = s
|
||||
setmetatable(self, meta)
|
||||
|
||||
self:send("CAP REQ multi-prefix")
|
||||
self:queue("CAP REQ multi-prefix")
|
||||
|
||||
self:invoke("PreRegister", self)
|
||||
self:send("CAP END")
|
||||
self:queue("CAP END")
|
||||
|
||||
if password then
|
||||
self:send("PASS %s", password)
|
||||
self:queue("PASS %s", password)
|
||||
end
|
||||
|
||||
self:send("NICK %s", self.nick)
|
||||
self:send("USER %s 0 * :%s", self.username, self.realname)
|
||||
self:queue("NICK %s", self.nick)
|
||||
self:queue("USER %s 0 * :%s", self.username, self.realname)
|
||||
|
||||
self.channels = {}
|
||||
|
||||
@ -183,12 +182,10 @@ end
|
||||
local handlers = handlers
|
||||
|
||||
function meta:handle(prefix, cmd, params)
|
||||
local user = parsePrefix(prefix)
|
||||
local handler = handlers[cmd]
|
||||
if handler then
|
||||
handler(self, user, unpack(params))
|
||||
return handler(self, prefix, unpack(params))
|
||||
end
|
||||
self:invoke("Do"..capitalize(cmd), user, unpack(params))
|
||||
end
|
||||
|
||||
local whoisHandlers = {
|
||||
@ -230,6 +227,6 @@ function meta:whois(nick)
|
||||
end
|
||||
|
||||
function meta:topic(channel)
|
||||
self:send("TOPIC %s", channel)
|
||||
self:queue("TOPIC %s", channel)
|
||||
end
|
||||
|
||||
|
45
queue.lua
Normal file
45
queue.lua
Normal file
@ -0,0 +1,45 @@
|
||||
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
|
||||
|
52
util.lua
52
util.lua
@ -50,51 +50,29 @@ function parse(line)
|
||||
return prefix, cmd, params
|
||||
end
|
||||
|
||||
function parseNick(conn, nick)
|
||||
local access = {}
|
||||
updatePrefixModes(conn)
|
||||
local namestart = 1
|
||||
for i = 1, #nick - 1 do
|
||||
local c = nick:sub(i, i)
|
||||
if conn.prefixmode[c] then
|
||||
access[conn.prefixmode[c]] = true
|
||||
else
|
||||
namestart = i
|
||||
break
|
||||
end
|
||||
end
|
||||
access.op = access.o
|
||||
access.voice = access.v
|
||||
local name = nick:sub(namestart)
|
||||
return access, name
|
||||
function parseNick(nick)
|
||||
local access, name = nick:match("^([%+@]*)(.+)$")
|
||||
return parseAccess(access or ""), name
|
||||
end
|
||||
|
||||
function parsePrefix(prefix)
|
||||
local user = {}
|
||||
if prefix then
|
||||
user.nick, user.username, user.host = prefix:match("^(.+)!(.+)@(.+)$")
|
||||
user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
|
||||
end
|
||||
user.access = parseAccess(user.access or "")
|
||||
return user
|
||||
end
|
||||
|
||||
function updatePrefixModes(conn)
|
||||
if conn.prefixmode and conn.modeprefix then
|
||||
return
|
||||
function parseAccess(accessString)
|
||||
local access = {op = false, halfop = false, voice = false}
|
||||
for c in accessString:gmatch(".") do
|
||||
if c == "@" then access.op = true
|
||||
elseif c == "%" then access.halfop = true
|
||||
elseif c == "+" then access.voice = true
|
||||
end
|
||||
conn.prefixmode = {}
|
||||
conn.modeprefix = {}
|
||||
if conn.supports.PREFIX then
|
||||
local modes, prefixes = conn.supports.PREFIX:match("%(([^%)]*)%)(.*)")
|
||||
for i = 1, #modes do
|
||||
conn.prefixmode[prefixes:sub(i, i)] = modes:sub(i, i)
|
||||
conn.modeprefix[ modes:sub(i, i)] = prefixes:sub(i, i)
|
||||
end
|
||||
else
|
||||
conn.prefixmode['@'] = 'o'
|
||||
conn.prefixmode['+'] = 'v'
|
||||
conn.modeprefix['o'] = '@'
|
||||
conn.modeprefix['v'] = '+'
|
||||
end
|
||||
return access
|
||||
end
|
||||
|
||||
--mIRC markup scheme (de-facto standard)
|
||||
@ -156,9 +134,3 @@ function defaultNickGenerator(nick)
|
||||
return nick
|
||||
end
|
||||
|
||||
function capitalize(text)
|
||||
-- Converts first character to upercase and the rest to lowercase.
|
||||
-- "PING" -> "Ping" | "hello" -> "Hello" | "123" -> "123"
|
||||
return text:sub(1, 1):upper()..text:sub(2):lower()
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user