1
0
mirror of https://github.com/ShadowNinja/LuaIRC.git synced 2025-09-15 10:25:20 +02:00

Cleaned up whitespace, now all indentation is done with 1 tab character

This commit is contained in:
Jakob Ovrum
2010-07-17 11:48:32 +09:00
parent 8f8feb00d7
commit 48ee749bb7
3 changed files with 300 additions and 300 deletions

View File

@@ -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
View File

@@ -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
end
break break
end
end end
end end
local handlers = {} local handlers = {}
o:send("PONG :%s", query) handlers["PING"] = function(o, prefix, query)
o:send("PONG :%s", query) o:send("PONG :%s", query)
end end
o.authed = true handlers["001"] = function(o)
o.authed = true o.authed = true
end end
o:invoke("OnChat", parsePrefix(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
o:invoke("OnNotice", parsePrefix(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
local user = parsePrefix(prefix) handlers["JOIN"] = function(o, prefix, channel)
if o.track_users then local user = parsePrefix(prefix)
if user.nick == o.nick then if o.track_users then
o.channels[channel] = {users = {}} if user.nick == o.nick then
else o.channels[channel] = {users = {}}
o.channels[channel].users[user.nick] = user else
end o.channels[channel].users[user.nick] = user
end end
end end
o:invoke("OnJoin", user, channel)
o:invoke("OnJoin", user, channel) o:invoke("OnJoin", user, channel)
end end
local user = parsePrefix(prefix) handlers["PART"] = function(o, prefix, channel, reason)
if o.track_users then local user = parsePrefix(prefix)
if user.nick == o.nick then if o.track_users then
o.channels[channel] = nil if user.nick == o.nick then
else o.channels[channel] = nil
o.channels[channel].users[user.nick] = nil else
end o.channels[channel].users[user.nick] = nil
end end
o:invoke("OnPart", user, channel, reason) end
o:invoke("OnPart", user, channel, reason) o:invoke("OnPart", user, channel, reason)
end end
local user = parsePrefix(prefix) handlers["QUIT"] = function(o, prefix, msg)
if o.track_users then local user = parsePrefix(prefix)
for channel, v in pairs(o.channels) do if o.track_users then
v.users[user.nick] = nil for channel, v in pairs(o.channels) do
end v.users[user.nick] = nil
end end
o:invoke("OnQuit", user, msg) end
o:invoke("OnQuit", user, msg) o:invoke("OnQuit", user, msg)
end end
local user = parsePrefix(prefix) handlers["NICK"] = function(o, prefix, newnick)
if o.track_users then local user = parsePrefix(prefix)
for channel, v in pairs(o.channels) do if o.track_users then
local users = v.users for channel, v in pairs(o.channels) do
local oldinfo = users[user.nick] local users = v.users
if oldinfo then local oldinfo = users[user.nick]
users[newnick] = oldinfo if oldinfo then
users[user.nick] = nil users[newnick] = oldinfo
o:invoke("NickChange", user, newnick, channel) users[user.nick] = nil
end o:invoke("NickChange", user, newnick, channel)
end end
else end
o:invoke("NickChange", user, newnick) else
end o:invoke("NickChange", user, newnick)
end end
end end
--NAMES list --NAMES list
if o.track_users then handlers["353"] = function(o, prefix, me, chanType, channel, names)
o.channels[channel] = o.channels[channel] or {users = {}, type = chanType} 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
for nick in names:gmatch("(%S+)") do local users = o.channels[channel].users
local access, name = parseNick(nick) for nick in names:gmatch("(%S+)") do
users[name] = {type = access} local access, name = parseNick(nick)
end users[name] = {type = access}
end end
end end
end end
--end of NAMES --end of NAMES
if o.track_users then handlers["366"] = function(o, prefix, me, channel, msg)
o:invoke("NameList", channel, msg) if o.track_users then
end o:invoke("NameList", channel, msg)
end end
end end
--no topic --no topic
o:invoke("OnTopic", channel, nil) handlers["331"] = function(o, prefix, me, channel)
o:invoke("OnTopic", channel, nil) o:invoke("OnTopic", channel, nil)
end end
--new topic --new topic
o:invoke("OnTopic", channel, topic) handlers["TOPIC"] = function(o, prefix, channel, topic)
o:invoke("OnTopic", channel, topic) o:invoke("OnTopic", channel, topic)
end end
o:invoke("OnTopic", 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
o:invoke("OnTopicInfo", channel, nick, tonumber(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
o:invoke("OnDisconnect", message, true) handlers["ERROR"] = function(o, prefix, message)
o:shutdown() o:invoke("OnDisconnect", message, true)
error(message, 3) o:shutdown()
error(message, 3) error(message, 3)
end end
local handler = handlers[cmd] function meta:handle(prefix, cmd, params)
if handler then local handler = handlers[cmd]
handler(self, prefix, unpack(params)) if handler then
end handler(self, prefix, unpack(params))
end end
end end

View File

@@ -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)