mirror of
https://github.com/ShadowNinja/LuaIRC.git
synced 2025-01-10 10:00:28 +01:00
Cleaned up whitespace, now all indentation is done with 1 tab character
This commit is contained in:
parent
8f8feb00d7
commit
48ee749bb7
@ -33,9 +33,9 @@ end
|
|||||||
|
|
||||||
function meta:part(channel)
|
function meta:part(channel)
|
||||||
self:send("PART %s", channel)
|
self:send("PART %s", channel)
|
||||||
if self.track_users then
|
if self.track_users then
|
||||||
self.channels[channel] = nil
|
self.channels[channel] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:trackUsers(b)
|
function meta:trackUsers(b)
|
||||||
|
236
init.lua
236
init.lua
@ -22,85 +22,86 @@ 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 '"..k.."' is not accessible before connecting", 2)
|
||||||
end
|
end
|
||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
|
|
||||||
function new(user)
|
function new(user)
|
||||||
local o = {
|
local o = {
|
||||||
nick = assert(user.nick, "Field 'nick' is required");
|
nick = assert(user.nick, "Field 'nick' is required");
|
||||||
username = user.username or "lua";
|
username = user.username or "lua";
|
||||||
realname = user.realname or "Lua owns";
|
realname = user.realname or "Lua owns";
|
||||||
hooks = {};
|
hooks = {};
|
||||||
track_users = true;
|
track_users = true;
|
||||||
}
|
}
|
||||||
return setmetatable(o, meta_preconnect)
|
return setmetatable(o, meta_preconnect)
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:hook(name, id, f)
|
function meta:hook(name, id, f)
|
||||||
f = f or id
|
f = f or id
|
||||||
self.hooks[name] = self.hooks[name] or {}
|
self.hooks[name] = self.hooks[name] or {}
|
||||||
self.hooks[name][id] = f
|
self.hooks[name][id] = f
|
||||||
return id or f
|
return id or f
|
||||||
end
|
end
|
||||||
meta_preconnect.hook = meta.hook
|
meta_preconnect.hook = meta.hook
|
||||||
|
|
||||||
|
|
||||||
function meta:unhook(name, id)
|
function meta:unhook(name, id)
|
||||||
local hooks = self.hooks[name]
|
local hooks = self.hooks[name]
|
||||||
|
|
||||||
assert(hooks, "no hooks exist for this event")
|
assert(hooks, "no hooks exist for this event")
|
||||||
assert(hooks[id], "hook ID not found")
|
assert(hooks[id], "hook ID not found")
|
||||||
|
|
||||||
hooks[id] = nil
|
hooks[id] = nil
|
||||||
end
|
end
|
||||||
meta_preconnect.unhook = meta.unhook
|
meta_preconnect.unhook = meta.unhook
|
||||||
|
|
||||||
function meta:invoke(name, ...)
|
function meta:invoke(name, ...)
|
||||||
local hooks = self.hooks[name]
|
local hooks = self.hooks[name]
|
||||||
if hooks then
|
if hooks then
|
||||||
for id,f in pairs(hooks) do
|
for id,f in pairs(hooks) do
|
||||||
f(...)
|
f(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta_preconnect:connect(server, port, timeout)
|
function meta_preconnect:connect(server, port, timeout)
|
||||||
port = port or 6667
|
port = port or 6667
|
||||||
|
|
||||||
local s = socket.tcp()
|
local s = socket.tcp()
|
||||||
self.socket = s
|
self.socket = s
|
||||||
s:settimeout(timeout or 30)
|
s:settimeout(timeout or 30)
|
||||||
assert(s:connect(server, port))
|
assert(s:connect(server, port))
|
||||||
|
|
||||||
setmetatable(self, meta)
|
setmetatable(self, meta)
|
||||||
|
|
||||||
self:send("USER %s 0 * :%s", self.username, self.realname)
|
self:send("USER %s 0 * :%s", self.username, self.realname)
|
||||||
self:send("NICK %s", self.nick)
|
self:send("NICK %s", self.nick)
|
||||||
|
|
||||||
self.channels = {}
|
self.channels = {}
|
||||||
|
|
||||||
s:settimeout(0)
|
s:settimeout(0)
|
||||||
repeat
|
|
||||||
self:think()
|
repeat
|
||||||
until self.authed
|
self:think()
|
||||||
|
until self.authed
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:disconnect(message)
|
function meta:disconnect(message)
|
||||||
local message = message or "Bye!"
|
local message = message or "Bye!"
|
||||||
|
|
||||||
self:invoke("OnDisconnect", message, false)
|
self:invoke("OnDisconnect", message, false)
|
||||||
self:send("QUIT :%s", message)
|
self:send("QUIT :%s", message)
|
||||||
|
|
||||||
self:shutdown()
|
self:shutdown()
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:shutdown()
|
function meta:shutdown()
|
||||||
self.socket:shutdown()
|
self.socket:shutdown()
|
||||||
self.socket:close()
|
self.socket:close()
|
||||||
setmetatable(self, nil)
|
setmetatable(self, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getline(self, errlevel)
|
local function getline(self, errlevel)
|
||||||
@ -116,136 +117,135 @@ local function getline(self, errlevel)
|
|||||||
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 then
self:handle(parse(line))
|
||||||
self:handle(parse(line))
|
else
|
||||||
else
|
break
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local handlers = {}
|
local handlers = {}
|
||||||
|
|
||||||
handlers["PING"] = function(o, prefix, query)
|
handlers["PING"] = function(o, prefix, query)
|
||||||
o:send("PONG :%s", query)
|
o:send("PONG :%s", query)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["001"] = function(o)
|
handlers["001"] = function(o)
|
||||||
o.authed = true
|
o.authed = true
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["PRIVMSG"] = function(o, prefix, channel, message)
|
handlers["PRIVMSG"] = function(o, prefix, channel, message)
|
||||||
o:invoke("OnChat", parsePrefix(prefix), channel, message)
|
o:invoke("OnChat", parsePrefix(prefix), channel, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["NOTICE"] = function(o, prefix, channel, message)
|
handlers["NOTICE"] = function(o, prefix, channel, message)
|
||||||
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
|
o:invoke("OnNotice", parsePrefix(prefix), channel, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["JOIN"] = function(o, prefix, channel)
|
handlers["JOIN"] = function(o, prefix, channel)
|
||||||
local user = parsePrefix(prefix)
|
local user = parsePrefix(prefix)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
if user.nick == o.nick then
|
if user.nick == o.nick then
|
||||||
o.channels[channel] = {users = {}}
|
o.channels[channel] = {users = {}}
|
||||||
else
|
else
|
||||||
o.channels[channel].users[user.nick] = user
|
o.channels[channel].users[user.nick] = user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
o:invoke("OnJoin", user, channel)
|
o:invoke("OnJoin", user, channel)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["PART"] = function(o, prefix, channel, reason)
|
handlers["PART"] = function(o, prefix, channel, reason)
|
||||||
local user = parsePrefix(prefix)
|
local user = parsePrefix(prefix)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
if user.nick == o.nick then
|
if user.nick == o.nick then
|
||||||
o.channels[channel] = nil
|
o.channels[channel] = nil
|
||||||
else
|
else
|
||||||
o.channels[channel].users[user.nick] = nil
|
o.channels[channel].users[user.nick] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
o:invoke("OnPart", user, channel, reason)
|
o:invoke("OnPart", user, channel, reason)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["QUIT"] = function(o, prefix, msg)
|
handlers["QUIT"] = function(o, prefix, msg)
|
||||||
local user = parsePrefix(prefix)
|
local user = parsePrefix(prefix)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
for channel, v in pairs(o.channels) do
|
for channel, v in pairs(o.channels) do
|
||||||
v.users[user.nick] = nil
|
v.users[user.nick] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
o:invoke("OnQuit", user, msg)
|
o:invoke("OnQuit", user, msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["NICK"] = function(o, prefix, newnick)
|
handlers["NICK"] = function(o, prefix, newnick)
|
||||||
local user = parsePrefix(prefix)
|
local user = parsePrefix(prefix)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
for channel, v in pairs(o.channels) do
|
for channel, v in pairs(o.channels) do
|
||||||
local users = v.users
|
local users = v.users
|
||||||
local oldinfo = users[user.nick]
|
local oldinfo = users[user.nick]
|
||||||
if oldinfo then
|
if oldinfo then
|
||||||
users[newnick] = oldinfo
|
users[newnick] = oldinfo
|
||||||
users[user.nick] = nil
|
users[user.nick] = nil
|
||||||
o:invoke("NickChange", user, newnick, channel)
|
o:invoke("NickChange", user, newnick, channel)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
o:invoke("NickChange", user, newnick)
|
o:invoke("NickChange", user, newnick)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--NAMES list
|
--NAMES list
|
||||||
handlers["353"] = function(o, prefix, me, chanType, channel, names)
|
handlers["353"] = function(o, prefix, me, chanType, channel, names)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
|
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
|
||||||
|
|
||||||
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] = {type = access}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--end of NAMES
|
--end of NAMES
|
||||||
handlers["366"] = function(o, prefix, me, channel, msg)
|
handlers["366"] = function(o, prefix, me, channel, msg)
|
||||||
if o.track_users then
|
if o.track_users then
|
||||||
o:invoke("NameList", channel, msg)
|
o:invoke("NameList", channel, msg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--no topic
|
--no topic
|
||||||
handlers["331"] = function(o, prefix, me, channel)
|
handlers["331"] = function(o, prefix, me, channel)
|
||||||
o:invoke("OnTopic", channel, nil)
|
o:invoke("OnTopic", channel, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
--new topic
|
--new topic
|
||||||
handlers["TOPIC"] = function(o, prefix, channel, topic)
|
handlers["TOPIC"] = function(o, prefix, channel, topic)
|
||||||
o:invoke("OnTopic", channel, topic)
|
o:invoke("OnTopic", channel, topic)
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["332"] = function(o, prefix, me, channel, topic)
|
handlers["332"] = function(o, prefix, me, channel, topic)
|
||||||
o:invoke("OnTopic", channel, topic)
|
o:invoke("OnTopic", channel, topic)
|
||||||
end
|
end
|
||||||
|
|
||||||
--topic creation info
|
--topic creation info
|
||||||
handlers["333"] = function(o, prefix, me, channel, nick, time)
|
handlers["333"] = function(o, prefix, me, channel, nick, time)
|
||||||
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
|
o:invoke("OnTopicInfo", channel, nick, tonumber(time))
|
||||||
end
|
end
|
||||||
|
|
||||||
handlers["ERROR"] = function(o, prefix, message)
|
handlers["ERROR"] = function(o, prefix, message)
|
||||||
o:invoke("OnDisconnect", message, true)
|
o:invoke("OnDisconnect", message, true)
|
||||||
o:shutdown()
|
o:shutdown()
|
||||||
error(message, 3)
|
error(message, 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
function meta:handle(prefix, cmd, params)
|
function meta:handle(prefix, cmd, params)
|
||||||
local handler = handlers[cmd]
|
local handler = handlers[cmd]
|
||||||
if handler then
|
if handler then
|
||||||
handler(self, prefix, unpack(params))
|
handler(self, prefix, unpack(params))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local whoisHandlers = {
|
local whoisHandlers = {
|
||||||
|
42
util.lua
42
util.lua
@ -9,23 +9,23 @@ module "irc"
|
|||||||
|
|
||||||
--protocol parsing
|
--protocol parsing
|
||||||
function parse(line)
|
function parse(line)
|
||||||
local prefix
|
local prefix
|
||||||
local lineStart = 1
|
local lineStart = 1
|
||||||
if line:sub(1,1) == ":" then
|
if line:sub(1,1) == ":" then
|
||||||
local space = line:find(" ")
|
local space = line:find(" ")
|
||||||
prefix = line:sub(2, space-1)
|
prefix = line:sub(2, space-1)
|
||||||
lineStart = space
|
lineStart = space
|
||||||
end
|
end
|
||||||
|
|
||||||
local trailToken = line:find(":", lineStart)
|
local trailToken = line:find(":", lineStart)
|
||||||
local lineStop = line:len()
|
local lineStop = line:len()
|
||||||
local trailing
|
local trailing
|
||||||
if trailToken then
|
if trailToken then
|
||||||
trailing = line:sub(trailToken + 1)
|
trailing = line:sub(trailToken + 1)
|
||||||
lineStop = trailToken - 2
|
lineStop = trailToken - 2
|
||||||
end
|
end
|
||||||
|
|
||||||
local params = {}
|
local params = {}
|
||||||
|
|
||||||
local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
|
local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
|
||||||
local pos = cmdEnd + 1
|
local pos = cmdEnd + 1
|
||||||
@ -40,23 +40,23 @@ function parse(line)
|
|||||||
params[#params + 1] = param
|
params[#params + 1] = param
|
||||||
end
|
end
|
||||||
|
|
||||||
if trailing then
|
if trailing then
|
||||||
params[#params + 1] = trailing
|
params[#params + 1] = trailing
|
||||||
end
|
end
|
||||||
|
|
||||||
return prefix, cmd, params
|
return prefix, cmd, params
|
||||||
end
|
end
|
||||||
|
|
||||||
function parseNick(nick)
|
function parseNick(nick)
|
||||||
return nick:match("^([%+@]?)(.+)$")
|
return nick:match("^([%+@]?)(.+)$")
|
||||||
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
|
||||||
return user
|
return user
|
||||||
end
|
end
|
||||||
|
|
||||||
--mIRC markup scheme (de-facto standard)
|
--mIRC markup scheme (de-facto standard)
|
||||||
|
Loading…
Reference in New Issue
Block a user