mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-07-20 16:40:26 +02:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
a79c9451f6 | |||
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 can be automatically generated from the doc/irc.luadoc file, pre-generated documentation can be found in the gh-branch.
|
||||
|
||||
You can also browse it [online](http://jakobovrum.github.com/LuaIRC/doc/modules/irc.html).
|
||||
|
||||
Documentation is generated by [LuaDoc](http://luadoc.luaforge.net/)
|
||||
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).
|
||||
|
||||
|
@ -1,11 +1,18 @@
|
||||
local table = table
|
||||
local assert = assert
|
||||
local select = select
|
||||
|
||||
module "irc"
|
||||
|
||||
local meta = _META
|
||||
|
||||
function meta:send(fmt, ...)
|
||||
local bytes, err = self.socket:send(fmt:format(...) .. "\r\n")
|
||||
function meta:send(msg, ...)
|
||||
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
|
||||
self:invoke("OnDisconnect", err, true)
|
||||
|
@ -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)
|
||||
@ -71,9 +72,9 @@ function irc:whois(nick)
|
||||
function irc:topic(channel)
|
||||
|
||||
--- Send a raw line of IRC to the server.
|
||||
-- @param fmt Line to be sent, excluding newline characters.
|
||||
-- @param ... Format parameters for <code>fmt</code>, with <code>string.format</code> semantics.
|
||||
function irc:send(fmt, ...)
|
||||
-- @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:send(msg, ...)
|
||||
|
||||
--- Send a message to a channel or user.
|
||||
-- @param target Nick or channel to send to.
|
||||
@ -125,7 +126,9 @@ function irc:shutdown()
|
||||
|
||||
--- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function.
|
||||
-- <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>OnSend(line)</code></li>
|
||||
-- <li><code>OnDisconnect(message, errorOccurred)</code></li>
|
||||
-- <li><code>OnChat(user, channel, message)</code></li>
|
||||
-- <li><code>OnNotice(user, channel, message)</code></li>
|
||||
@ -137,8 +140,9 @@ function irc:shutdown()
|
||||
-- <li><code>OnTopic(channel, topic)</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>OnUserModeIs(modes)</code></li>
|
||||
-- <li><code>OnChannelModeIs(user, channel, modes)</code></li>
|
||||
-- <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>
|
||||
-- </ul>
|
||||
-- * Event also invoked for yourself.
|
||||
-- <20> Channel passed only when user tracking is enabled
|
||||
@ -151,7 +155,7 @@ 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. 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>
|
||||
-- Apart from <code>nick</code>, fields may be missing. To fill them in, enable user tracking and use irc:whois.
|
||||
-- @name User
|
||||
|
172
handlers.lua
Normal file
172
handlers.lua
Normal file
@ -0,0 +1,172 @@
|
||||
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
|
||||
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] = {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
|
||||
|
||||
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)
|
||||
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
|
155
init.lua
155
init.lua
@ -19,13 +19,14 @@ _META = meta
|
||||
|
||||
require "irc.util"
|
||||
require "irc.asyncoperations"
|
||||
require "irc.handlers"
|
||||
|
||||
local meta_preconnect = {}
|
||||
function meta_preconnect.__index(o, k)
|
||||
local v = rawget(meta_preconnect, k)
|
||||
|
||||
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
|
||||
return v
|
||||
end
|
||||
@ -37,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
|
||||
@ -116,12 +119,17 @@ function meta_preconnect:connect(_host, _port)
|
||||
self.socket = s
|
||||
setmetatable(self, meta)
|
||||
|
||||
self:send("CAP REQ multi-prefix")
|
||||
|
||||
self:invoke("PreRegister", self)
|
||||
self:send("CAP END")
|
||||
|
||||
if password then
|
||||
self:send("PASS %s", password)
|
||||
end
|
||||
|
||||
self:send("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 = {}
|
||||
|
||||
@ -149,21 +157,19 @@ end
|
||||
local function getline(self, errlevel)
|
||||
local line, err = self.socket:receive("*l")
|
||||
|
||||
if line then
|
||||
return line
|
||||
end
|
||||
|
||||
if err ~= "timeout" and err ~= "wantread" then
|
||||
if not line and err ~= "timeout" and err ~= "wantread" then
|
||||
self:invoke("OnDisconnect", err, true)
|
||||
self:close()
|
||||
self:shutdown()
|
||||
error(err, errlevel)
|
||||
end
|
||||
|
||||
return line
|
||||
end
|
||||
|
||||
function meta:think()
|
||||
while true do
|
||||
local line = getline(self, 3)
|
||||
if line then
|
||||
if line and #line > 0 then
|
||||
if not self:invoke("OnRaw", line) then
|
||||
self:handle(parse(line))
|
||||
end
|
||||
@ -173,136 +179,7 @@ function meta:think()
|
||||
end
|
||||
end
|
||||
|
||||
local 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
|
||||
local handlers = handlers
|
||||
|
||||
function meta:handle(prefix, cmd, params)
|
||||
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
|
17
util.lua
17
util.lua
@ -48,17 +48,30 @@ function parse(line)
|
||||
end
|
||||
|
||||
function parseNick(nick)
|
||||
return nick:match("^([%+@]?)(.+)$")
|
||||
local access, name = nick:match("^([%+@]*)(.+)$")
|
||||
return parseAccess(access or ""), name
|
||||
end
|
||||
|
||||
function parsePrefix(prefix)
|
||||
local user = {}
|
||||
if prefix then
|
||||
user.access, 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 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)
|
||||
color = {
|
||||
black = 1,
|
||||
|
Reference in New Issue
Block a user