mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-07-20 16:40:26 +02:00
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
2ccee2a935 | |||
b1cbbf1963 | |||
5ab24e9ad1 | |||
be620c428e | |||
8a2e47a326 | |||
bc79606de0 | |||
d443e2eeeb | |||
ac2a9d03fc | |||
9206f01b88 | |||
ddb788883e | |||
1d0480adba | |||
ad53b2aeb1 | |||
c181583f39 | |||
e3b566407e | |||
4c1e2248f8 | |||
d1c0b2d271 | |||
32d9a8b774 | |||
07f50242f4 | |||
c4d7278c4b | |||
4f12f28684 | |||
d6b4872384 | |||
ca767c8b42 | |||
d3c6ab849b | |||
1d8509a4f8 | |||
04dbe436f3 | |||
98ccd99f4d | |||
da7c1c9a20 |
@ -14,9 +14,5 @@ Dependencies
|
|||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
-------------
|
-------------
|
||||||
Documentation can be automatically generated from the doc/irc.luadoc file, pre-generated documentation can be found in the gh-branch.
|
Documentation can be automatically generated by passing irc.luadoc (in doc/) to [LuaDoc](http://luadoc.luaforge.net/), or pre-generated documentation can be found in the 'gh-pages' branch, which can also be browsed [online](http://jakobovrum.github.com/LuaIRC/doc/modules/irc.html).
|
||||||
|
|
||||||
You can also browse it [online](http://jakobovrum.github.com/LuaIRC/doc/modules/irc.html).
|
|
||||||
|
|
||||||
Documentation is generated by [LuaDoc](http://luadoc.luaforge.net/)
|
|
||||||
|
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
local table = table
|
local table = table
|
||||||
|
local assert = assert
|
||||||
|
local select = select
|
||||||
|
|
||||||
module "irc"
|
module "irc"
|
||||||
|
|
||||||
local meta = _META
|
local meta = _META
|
||||||
|
|
||||||
function meta:send(fmt, ...)
|
function meta:send(msg, ...)
|
||||||
local bytes, err = self.socket:send(fmt:format(...) .. "\r\n")
|
if select("#", ...) > 0 then
|
||||||
|
msg = msg:format(...)
|
||||||
|
end
|
||||||
|
self:invoke("OnSend", msg)
|
||||||
|
|
||||||
|
local bytes, err = self.socket:send(msg .. "\r\n")
|
||||||
|
|
||||||
if not bytes and err ~= "timeout" and err ~= "wantwrite" then
|
if not bytes and err ~= "timeout" and err ~= "wantwrite" then
|
||||||
self:invoke("OnDisconnect", err, true)
|
self:invoke("OnDisconnect", err, true)
|
||||||
|
@ -71,9 +71,9 @@ function irc:whois(nick)
|
|||||||
function irc:topic(channel)
|
function irc:topic(channel)
|
||||||
|
|
||||||
--- Send a raw line of IRC to the server.
|
--- Send a raw line of IRC to the server.
|
||||||
-- @param fmt Line to be sent, excluding newline characters.
|
-- @param msg Line to be sent, excluding newline characters.
|
||||||
-- @param ... Format parameters for <code>fmt</code>, with <code>string.format</code> semantics.
|
-- @param ... Format parameters for <code>msg</code>, with <code>string.format</code> semantics. [optional]
|
||||||
function irc:send(fmt, ...)
|
function irc:send(msg, ...)
|
||||||
|
|
||||||
--- Send a message to a channel or user.
|
--- Send a message to a channel or user.
|
||||||
-- @param target Nick or channel to send to.
|
-- @param target Nick or channel to send to.
|
||||||
@ -125,7 +125,9 @@ function irc:shutdown()
|
|||||||
|
|
||||||
--- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function.
|
--- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function.
|
||||||
-- <ul>
|
-- <ul>
|
||||||
|
-- <li><code>PreRegister(connection)</code>Useful for CAP commands and SASL.</li>
|
||||||
-- <li><code>OnRaw(line) - (any non false/nil return value assumes line handled and will not be further processed)</code></li>
|
-- <li><code>OnRaw(line) - (any non false/nil return value assumes line handled and will not be further processed)</code></li>
|
||||||
|
-- <li><code>OnSend(line)</code></li>
|
||||||
-- <li><code>OnDisconnect(message, errorOccurred)</code></li>
|
-- <li><code>OnDisconnect(message, errorOccurred)</code></li>
|
||||||
-- <li><code>OnChat(user, channel, message)</code></li>
|
-- <li><code>OnChat(user, channel, message)</code></li>
|
||||||
-- <li><code>OnNotice(user, channel, message)</code></li>
|
-- <li><code>OnNotice(user, channel, message)</code></li>
|
||||||
@ -137,8 +139,9 @@ function irc:shutdown()
|
|||||||
-- <li><code>OnTopic(channel, topic)</code></li>
|
-- <li><code>OnTopic(channel, topic)</code></li>
|
||||||
-- <li><code>OnTopicInfo(channel, creator, timeCreated)</code></li>
|
-- <li><code>OnTopicInfo(channel, creator, timeCreated)</code></li>
|
||||||
-- <li><code>OnKick(channel, nick, kicker, reason)</code>* (kicker is a <code>user</code> table)</li>
|
-- <li><code>OnKick(channel, nick, kicker, reason)</code>* (kicker is a <code>user</code> table)</li>
|
||||||
-- <li><code>OnUserModeIs(modes)</code></li>
|
-- <li><code>OnUserMode(modes)</code></li>
|
||||||
-- <li><code>OnChannelModeIs(user, channel, 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>
|
||||||
-- </ul>
|
-- </ul>
|
||||||
-- * Event also invoked for yourself.
|
-- * Event also invoked for yourself.
|
||||||
-- <20> Channel passed only when user tracking is enabled
|
-- <20> Channel passed only when user tracking is enabled
|
||||||
@ -151,7 +154,7 @@ function irc:shutdown()
|
|||||||
-- <li><code>username</code> - User username.</li>
|
-- <li><code>username</code> - User username.</li>
|
||||||
-- <li><code>host</code> - User hostname.</li>
|
-- <li><code>host</code> - User hostname.</li>
|
||||||
-- <li><code>realname</code> - User real name.</li>
|
-- <li><code>realname</code> - User real name.</li>
|
||||||
-- <li><code>access</code> - User access, available in channel-oriented callbacks. Can be '+', '@', and others, depending on the server.</li>
|
-- <li><code>access</code> - User access, available in channel-oriented callbacks. A table containing the boolean fields 'op', 'halfop', and 'voice'.</li>
|
||||||
-- </ul>
|
-- </ul>
|
||||||
-- Apart from <code>nick</code>, fields may be missing. To fill them in, enable user tracking and use irc:whois.
|
-- Apart from <code>nick</code>, fields may be missing. To fill them in, enable user tracking and use irc:whois.
|
||||||
-- @name User
|
-- @name User
|
||||||
|
174
handlers.lua
Normal file
174
handlers.lua
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
local pairs = pairs
|
||||||
|
local error = error
|
||||||
|
local tonumber = tonumber
|
||||||
|
local table = table
|
||||||
|
|
||||||
|
module "irc"
|
||||||
|
|
||||||
|
handlers = {}
|
||||||
|
|
||||||
|
handlers["PING"] = function(o, prefix, query)
|
||||||
|
o:send("PONG :%s", query)
|
||||||
|
end
|
||||||
|
|
||||||
|
handlers["001"] = function(o, prefix, me)
|
||||||
|
o.authed = true
|
||||||
|
o.nick = me
|
||||||
|
end
|
||||||
|
|
||||||
|
handlers["PRIVMSG"] = function(o, prefix, channel, message)
|
||||||
|
o:invoke("OnChat", parsePrefix(prefix), channel, message)
|
||||||
|
end
|
||||||
|
|
||||||
|
handlers["NOTICE"] = function(o, prefix, channel, message)
|
||||||
|
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
|
||||||
|
end
|
||||||
|
|
||||||
|
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 = {}}
|
||||||
|
else
|
||||||
|
o.channels[channel].users[user.nick] = user
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
o:invoke("OnJoin", user, channel)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
else
|
||||||
|
o.channels[channel].users[user.nick] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
o:invoke("OnPart", user, channel, reason)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
o:invoke("OnQuit", user, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
local oldinfo = users[user.nick]
|
||||||
|
if oldinfo then
|
||||||
|
users[newnick] = oldinfo
|
||||||
|
users[user.nick] = nil
|
||||||
|
o:invoke("NickChange", user, newnick, channel)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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)
|
||||||
|
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(nick)
|
||||||
|
users[name] = {access = access}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--end of NAMES
|
||||||
|
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, prefix, me, channel)
|
||||||
|
o:invoke("OnTopic", channel, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
--new topic
|
||||||
|
handlers["TOPIC"] = function(o, prefix, channel, topic)
|
||||||
|
o:invoke("OnTopic", channel, topic)
|
||||||
|
end
|
||||||
|
|
||||||
|
handlers["332"] = function(o, prefix, me, channel, topic)
|
||||||
|
o:invoke("OnTopic", channel, topic)
|
||||||
|
end
|
||||||
|
|
||||||
|
--topic creation info
|
||||||
|
handlers["333"] = function(o, prefix, me, channel, nick, time)
|
||||||
|
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
|
||||||
|
end
|
||||||
|
|
||||||
|
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, 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, prefix, user, channel, modes)
|
||||||
|
o:invoke("OnChannelMode", channel, modes)
|
||||||
|
end
|
||||||
|
|
||||||
|
handlers["MODE"] = function(o, prefix, target, modes, ...)
|
||||||
|
if o.track_users and target ~= o.nick then
|
||||||
|
local add = true
|
||||||
|
local optList = {...}
|
||||||
|
for c in modes:gmatch(".") do
|
||||||
|
if c == "+" then add = true
|
||||||
|
elseif c == "-" then add = false
|
||||||
|
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
|
||||||
|
o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
handlers["ERROR"] = function(o, prefix, message)
|
||||||
|
o:invoke("OnDisconnect", message, true)
|
||||||
|
o:shutdown()
|
||||||
|
error(message, 3)
|
||||||
|
end
|
164
init.lua
164
init.lua
@ -19,25 +19,28 @@ _META = meta
|
|||||||
|
|
||||||
require "irc.util"
|
require "irc.util"
|
||||||
require "irc.asyncoperations"
|
require "irc.asyncoperations"
|
||||||
|
require "irc.handlers"
|
||||||
|
|
||||||
local meta_preconnect = {}
|
local meta_preconnect = {}
|
||||||
function meta_preconnect.__index(o, k)
|
function meta_preconnect.__index(o, k)
|
||||||
local v = rawget(meta_preconnect, k)
|
local v = rawget(meta_preconnect, k)
|
||||||
|
|
||||||
if not v and meta[k] then
|
if not v and meta[k] then
|
||||||
error("field '"..k.."' is not accessible before connecting", 2)
|
error(("field '%s' is not accessible before connecting"):format(k), 2)
|
||||||
end
|
end
|
||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
|
|
||||||
function new(user)
|
function new(data)
|
||||||
local o = {
|
local o = {
|
||||||
nick = assert(user.nick, "Field 'nick' is required");
|
nick = assert(data.nick, "Field 'nick' is required");
|
||||||
username = user.username or "lua";
|
username = data.username or "lua";
|
||||||
realname = user.realname or "Lua owns";
|
realname = data.realname or "Lua owns";
|
||||||
|
nickGenerator = data.nickGenerator or defaultNickGenerator;
|
||||||
hooks = {};
|
hooks = {};
|
||||||
track_users = true;
|
track_users = true;
|
||||||
}
|
}
|
||||||
|
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
|
||||||
return setmetatable(o, meta_preconnect)
|
return setmetatable(o, meta_preconnect)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -116,12 +119,17 @@ function meta_preconnect:connect(_host, _port)
|
|||||||
self.socket = s
|
self.socket = s
|
||||||
setmetatable(self, meta)
|
setmetatable(self, meta)
|
||||||
|
|
||||||
|
self:send("CAP REQ multi-prefix")
|
||||||
|
|
||||||
|
self:invoke("PreRegister", self)
|
||||||
|
self:send("CAP END")
|
||||||
|
|
||||||
if password then
|
if password then
|
||||||
self:send("PASS %s", password)
|
self:send("PASS %s", password)
|
||||||
end
|
end
|
||||||
|
|
||||||
self:send("USER %s 0 * :%s", self.username, self.realname)
|
|
||||||
self:send("NICK %s", self.nick)
|
self:send("NICK %s", self.nick)
|
||||||
|
self:send("USER %s 0 * :%s", self.username, self.realname)
|
||||||
|
|
||||||
self.channels = {}
|
self.channels = {}
|
||||||
|
|
||||||
@ -129,6 +137,7 @@ function meta_preconnect:connect(_host, _port)
|
|||||||
|
|
||||||
repeat
|
repeat
|
||||||
self:think()
|
self:think()
|
||||||
|
socket.select(nil, nil, 0.1) -- Sleep so that we don't eat CPU
|
||||||
until self.authed
|
until self.authed
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -149,21 +158,19 @@ end
|
|||||||
local function getline(self, errlevel)
|
local function getline(self, errlevel)
|
||||||
local line, err = self.socket:receive("*l")
|
local line, err = self.socket:receive("*l")
|
||||||
|
|
||||||
if line then
|
if not line and err ~= "timeout" and err ~= "wantread" then
|
||||||
return line
|
|
||||||
end
|
|
||||||
|
|
||||||
if err ~= "timeout" and err ~= "wantread" then
|
|
||||||
self:invoke("OnDisconnect", err, true)
|
self:invoke("OnDisconnect", err, true)
|
||||||
self:close()
|
self:shutdown()
|
||||||
error(err, errlevel)
|
error(err, errlevel)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return line
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:think()
|
function meta:think()
|
||||||
while true do
|
while true do
|
||||||
local line = getline(self, 3)
|
local line = getline(self, 3)
|
||||||
if line then
|
if line and #line > 0 then
|
||||||
if not self:invoke("OnRaw", line) then
|
if not self:invoke("OnRaw", line) then
|
||||||
self:handle(parse(line))
|
self:handle(parse(line))
|
||||||
end
|
end
|
||||||
@ -173,136 +180,7 @@ function meta:think()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local handlers = {}
|
local handlers = handlers
|
||||||
|
|
||||||
handlers["PING"] = function(o, prefix, query)
|
|
||||||
o:send("PONG :%s", query)
|
|
||||||
end
|
|
||||||
|
|
||||||
handlers["001"] = function(o)
|
|
||||||
o.authed = true
|
|
||||||
end
|
|
||||||
|
|
||||||
handlers["PRIVMSG"] = function(o, prefix, channel, message)
|
|
||||||
o:invoke("OnChat", parsePrefix(prefix), channel, message)
|
|
||||||
end
|
|
||||||
|
|
||||||
handlers["NOTICE"] = function(o, prefix, channel, message)
|
|
||||||
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
|
|
||||||
end
|
|
||||||
|
|
||||||
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 = {}}
|
|
||||||
else
|
|
||||||
o.channels[channel].users[user.nick] = user
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
o:invoke("OnJoin", user, channel)
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
else
|
|
||||||
o.channels[channel].users[user.nick] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
o:invoke("OnPart", user, channel, reason)
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
o:invoke("OnQuit", user, msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
local oldinfo = users[user.nick]
|
|
||||||
if oldinfo then
|
|
||||||
users[newnick] = oldinfo
|
|
||||||
users[user.nick] = nil
|
|
||||||
o:invoke("NickChange", user, newnick, channel)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
o:invoke("NickChange", user, newnick)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--NAMES list
|
|
||||||
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(nick)
|
|
||||||
users[name] = {type = access}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--end of NAMES
|
|
||||||
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, prefix, me, channel)
|
|
||||||
o:invoke("OnTopic", channel, nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
--new topic
|
|
||||||
handlers["TOPIC"] = function(o, prefix, channel, topic)
|
|
||||||
o:invoke("OnTopic", channel, topic)
|
|
||||||
end
|
|
||||||
|
|
||||||
handlers["332"] = function(o, prefix, me, channel, topic)
|
|
||||||
o:invoke("OnTopic", channel, topic)
|
|
||||||
end
|
|
||||||
|
|
||||||
--topic creation info
|
|
||||||
handlers["333"] = function(o, prefix, me, channel, nick, time)
|
|
||||||
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
|
|
||||||
end
|
|
||||||
|
|
||||||
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, prefix, modes)
|
|
||||||
o:invoke("OnUserModeIs", modes)
|
|
||||||
end
|
|
||||||
|
|
||||||
--RPL_CHANNELMODEIS
|
|
||||||
--The result from common irc servers differs from that defined by the rfc
|
|
||||||
handlers["324"] = function(o, prefix, user, channel, modes)
|
|
||||||
o:invoke("OnChannelModeIs", user, channel, modes)
|
|
||||||
end
|
|
||||||
|
|
||||||
handlers["ERROR"] = function(o, prefix, message)
|
|
||||||
o:invoke("OnDisconnect", message, true)
|
|
||||||
o:shutdown()
|
|
||||||
error(message, 3)
|
|
||||||
end
|
|
||||||
|
|
||||||
function meta:handle(prefix, cmd, params)
|
function meta:handle(prefix, cmd, params)
|
||||||
local handler = handlers[cmd]
|
local handler = handlers[cmd]
|
||||||
|
56
set.lua
Normal file
56
set.lua
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
local select = require "socket".select
|
||||||
|
|
||||||
|
local setmetatable = setmetatable
|
||||||
|
local insert = table.insert
|
||||||
|
local remove = table.remove
|
||||||
|
local ipairs = ipairs
|
||||||
|
local error = error
|
||||||
|
|
||||||
|
module "irc.set"
|
||||||
|
|
||||||
|
local set = {}
|
||||||
|
set.__index = set
|
||||||
|
|
||||||
|
function new(t)
|
||||||
|
t.connections = {}
|
||||||
|
t.sockets = {}
|
||||||
|
return setmetatable(t, set)
|
||||||
|
end
|
||||||
|
|
||||||
|
function set:add(connection)
|
||||||
|
local socket = connection.socket
|
||||||
|
insert(self.sockets, socket)
|
||||||
|
|
||||||
|
self.connections[socket] = connection
|
||||||
|
insert(self.connections, connection)
|
||||||
|
end
|
||||||
|
|
||||||
|
function set:remove(connection)
|
||||||
|
local socket = connection.socket
|
||||||
|
self.connections[socket] = nil
|
||||||
|
for k, s in ipairs(self.sockets) do
|
||||||
|
if socket == s then
|
||||||
|
remove(self.sockets, k)
|
||||||
|
remove(self.connections, k)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function set:select()
|
||||||
|
local read, write, err = select(self.sockets, nil, self.timeout)
|
||||||
|
|
||||||
|
if read then
|
||||||
|
for k, socket in ipairs(read) do
|
||||||
|
read[k] = self.connections[socket]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return read, err
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Select - but if it times out, it returns all connections.
|
||||||
|
function set:poll()
|
||||||
|
local read, err = self:select()
|
||||||
|
return err == "timeout" and self.connections or read
|
||||||
|
end
|
44
util.lua
44
util.lua
@ -1,9 +1,12 @@
|
|||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
|
local sub = string.sub
|
||||||
|
local byte = string.byte
|
||||||
local char = string.char
|
local char = string.char
|
||||||
local table = table
|
local table = table
|
||||||
local assert = assert
|
local assert = assert
|
||||||
local tostring = tostring
|
local tostring = tostring
|
||||||
local type = type
|
local type = type
|
||||||
|
local random = math.random
|
||||||
|
|
||||||
module "irc"
|
module "irc"
|
||||||
|
|
||||||
@ -48,17 +51,30 @@ function parse(line)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function parseNick(nick)
|
function parseNick(nick)
|
||||||
return nick:match("^([%+@]?)(.+)$")
|
local access, name = nick:match("^([%+@]*)(.+)$")
|
||||||
|
return parseAccess(access or ""), name
|
||||||
end
|
end
|
||||||
|
|
||||||
function parsePrefix(prefix)
|
function parsePrefix(prefix)
|
||||||
local user = {}
|
local user = {}
|
||||||
if prefix then
|
if prefix then
|
||||||
user.access, user.nick, user.username, user.host = prefix:match("^([%+@]?)(.+)!(.+)@(.+)$")
|
user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
|
||||||
end
|
end
|
||||||
|
user.access = parseAccess(user.access or "")
|
||||||
return user
|
return user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
return access
|
||||||
|
end
|
||||||
|
|
||||||
--mIRC markup scheme (de-facto standard)
|
--mIRC markup scheme (de-facto standard)
|
||||||
color = {
|
color = {
|
||||||
black = 1,
|
black = 1,
|
||||||
@ -94,3 +110,27 @@ local underlineByte = char(31)
|
|||||||
function underline(text)
|
function underline(text)
|
||||||
return underlineByte..text..underlineByte
|
return underlineByte..text..underlineByte
|
||||||
end
|
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