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

@ -14,7 +14,7 @@ local function sendByMethod(self, method, target, msg)
self.socket:send(table.concat{toChannel, line, "\r\n"}) self.socket:send(table.concat{toChannel, line, "\r\n"})
end end
end end
function meta:sendChat(target, msg) function meta:sendChat(target, msg)
sendByMethod(self, "PRIVMSG", target, msg) sendByMethod(self, "PRIVMSG", target, msg)
end end
@ -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)

550
init.lua
View File

@ -1,291 +1,291 @@
local socket = require "socket" local socket = require "socket"
local error = error local error = error
local setmetatable = setmetatable local setmetatable = setmetatable
local rawget = rawget local rawget = rawget
local unpack = unpack local unpack = unpack
local pairs = pairs local pairs = pairs
local assert = assert local assert = assert
local require = require local require = require
local tonumber = tonumber local tonumber = tonumber
module "irc" module "irc"
local meta = {} local meta = {}
meta.__index = meta meta.__index = meta
_META = meta _META = meta
require "irc.util" require "irc.util"
require "irc.asyncoperations" require "irc.asyncoperations"
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 '"..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()
end until self.authed
end
function meta:disconnect(message)
local message = message or "Bye!" function meta:disconnect(message)
local message = message or "Bye!"
self:invoke("OnDisconnect", message, false)
self:send("QUIT :%s", message) self:invoke("OnDisconnect", message, false)
self:send("QUIT :%s", message)
self:shutdown()
end self:shutdown()
end
function meta:shutdown()
self.socket:shutdown() function meta:shutdown()
self.socket:close() self.socket:shutdown()
setmetatable(self, nil) self.socket:close()
end setmetatable(self, nil)
end
local function getline(self, errlevel)
line, err = self.socket:receive("*l") local function getline(self, errlevel)
line, err = self.socket:receive("*l")
if not line and err ~= "timeout" then
self:invoke("OnDisconnect", err, true) if not line and err ~= "timeout" then
self:shutdown() self:invoke("OnDisconnect", err, true)
error(err, errlevel) self:shutdown()
end error(err, errlevel)
end
return line
end return line
end
function meta:think()
while true do function meta:think()
local line = getline(self, 3) while true do
if line then local line = getline(self, 3)
self:handle(parse(line)) if line then 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 = {
["311"] = "userinfo"; ["311"] = "userinfo";
["312"] = "node"; ["312"] = "node";
["319"] = "channels"; ["319"] = "channels";
["330"] = "account"; -- Freenode ["330"] = "account"; -- Freenode
["307"] = "registered"; -- Unreal ["307"] = "registered"; -- Unreal
} }
function meta:whois(nick) function meta:whois(nick)
self:send("WHOIS %s", nick) self:send("WHOIS %s", nick)
local result = {} local result = {}
while true do while true do
local line = getline(self, 3) local line = getline(self, 3)
if line then if line then
local prefix, cmd, args = parse(line) local prefix, cmd, args = parse(line)
local handler = whoisHandlers[cmd] local handler = whoisHandlers[cmd]
if handler then if handler then
result[handler] = args result[handler] = args
elseif cmd == "318" then elseif cmd == "318" then
break break
else else
self:handle(prefix, cmd, args) self:handle(prefix, cmd, args)
end end
end end
end end
if result.account then if result.account then
result.account = result.account[3] result.account = result.account[3]
elseif result.registered then elseif result.registered then
result.account = result.registered[2] result.account = result.registered[2]
end end
return result return result
end end
function meta:topic(channel) function meta:topic(channel)
self:send("TOPIC %s", channel) self:send("TOPIC %s", channel)
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)