mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-02-08 08:00:17 +01:00
commit
bc79606de0
@ -1,12 +1,18 @@
|
|||||||
local table = table
|
local table = table
|
||||||
local assert = assert
|
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>
|
||||||
@ -139,7 +141,7 @@ function irc:shutdown()
|
|||||||
-- <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>OnUserMode(modes)</code></li>
|
-- <li><code>OnUserMode(modes)</code></li>
|
||||||
-- <li><code>OnChannelMode(user, channel, modes)</code></li>
|
-- <li><code>OnChannelMode(user, channel, modes)</code></li>
|
||||||
-- <li><code>OnModeChange(user, target, 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.
|
||||||
-- † Channel passed only when user tracking is enabled
|
-- † Channel passed only when user tracking is enabled
|
||||||
@ -152,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
|
||||||
|
25
handlers.lua
25
handlers.lua
@ -1,6 +1,7 @@
|
|||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local error = error
|
local error = error
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
|
local table = table
|
||||||
|
|
||||||
module "irc"
|
module "irc"
|
||||||
|
|
||||||
@ -83,7 +84,7 @@ handlers["353"] = function(o, prefix, me, chanType, channel, names)
|
|||||||
local users = o.channels[channel].users
|
local users = o.channels[channel].users
|
||||||
for nick in names:gmatch("(%S+)") do
|
for nick in names:gmatch("(%S+)") do
|
||||||
local access, name = parseNick(nick)
|
local access, name = parseNick(nick)
|
||||||
users[name] = {type = access}
|
users[name] = {access = access}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -130,8 +131,26 @@ handlers["324"] = function(o, prefix, user, channel, modes)
|
|||||||
o:invoke("OnChannelMode", channel, modes)
|
o:invoke("OnChannelMode", channel, modes)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["MODE"] = function(o, prefix, target, modes)
|
handlers["MODE"] = function(o, prefix, target, modes, ...)
|
||||||
o:invoke("OnModeChange", parsePrefix(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
|
end
|
||||||
|
|
||||||
handlers["ERROR"] = function(o, prefix, message)
|
handlers["ERROR"] = function(o, prefix, message)
|
||||||
|
7
init.lua
7
init.lua
@ -26,7 +26,7 @@ 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
|
||||||
@ -117,6 +117,11 @@ 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
|
||||||
|
17
util.lua
17
util.lua
@ -48,17 +48,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,
|
||||||
|
Loading…
Reference in New Issue
Block a user