Changed internals for better error handling

This commit is contained in:
Jakob Ovrum 2010-02-17 18:29:28 +09:00
parent 8b9467d72a
commit bd748aca29
1 changed files with 59 additions and 62 deletions

121
irc.lua
View File

@ -38,8 +38,6 @@ local tostring = tostring
local table = table local table = table
local type = type local type = type
local coroutine = coroutine
module(...) module(...)
local function assert(b, err, errlevel) local function assert(b, err, errlevel)
@ -72,7 +70,6 @@ function new(user)
o.username = user.username or "lua" o.username = user.username or "lua"
o.realname = user.realname or "Lua owns" o.realname = user.realname or "Lua owns"
o.socket = socket.tcp() o.socket = socket.tcp()
o.thread = coroutine.wrap(meta.think)
o.hooks = {} o.hooks = {}
o.connected = false o.connected = false
@ -80,46 +77,10 @@ function new(user)
end end
function think() function think()
local abort local think = meta.think
for _,o in ipairs(clients) do for _,o in ipairs(clients) do
abort = o:thread() think(o)
end end
return not abort
end
color = {
black = 1,
blue = 2,
green = 3,
red = 4,
lightred = 5,
purple = 6,
brown = 7,
yellow = 8,
lightgreen = 9,
navy = 10,
cyan = 11,
lightblue = 12,
violet = 13,
gray = 14,
lightgray = 15,
white = 16
}
local colByte = string.char(3)
setmetatable(color, {__call = function(_, text, colornum)
colornum = type(colornum) == "string" and assert(color[colornum], "Invalid color '"..colornum.."'") or colornum
return table.concat{colByte, tostring(colornum), text, colByte}
end})
local boldByte = string.char(2)
function bold(text)
return boldByte..text..boldByte
end
local underlineByte = string.char(31)
function underline(text)
return underlineByte..text..underlineByte
end end
function meta:hook(name, id, f) function meta:hook(name, id, f)
@ -187,24 +148,18 @@ function meta:shutdown()
end end
function meta:think() function meta:think()
while true do local line, err
local line, err = self.socket:receive("*l") while true do
if not line then line, err = self.socket:receive("*l")
if err ~= "timeout" then if not line then break end
self:shutdown() self:handle(parse(line))
error(err) end
return true
end
else
if debug then print(line) end
local prefix, cmd, params = parse(line)
self:handle(prefix, cmd, params) if err ~= "timeout" then
o:invoke("OnDisconnect", err, true)
if not self.connected then return true end self:shutdown()
end error(err, 3)
coroutine.yield() end
end
end end
function parsePrefix(prefix) function parsePrefix(prefix)
@ -286,8 +241,15 @@ function meta:send(fmt, ...)
self.socket:send(fmt:format(...) .. "\r\n") self.socket:send(fmt:format(...) .. "\r\n")
end end
function meta:sendChat(channel, fmt, ...) function meta:sendChat(channel, msg)
self:send("PRIVMSG %s :"..fmt, channel, ...) toChannel = ("PRIVMSG %s :"):format(channel)
if msg:find("\r\n") then
for line in msg:gmatch("(.+\r\n)") do
self.socket:send(toChannel..line)
end
else
self.socket:send(table.concat{toChannel, msg, "\r\n"})
end
end end
function meta:join(channel) function meta:join(channel)
@ -297,3 +259,38 @@ end
function meta:part(channel) function meta:part(channel)
self:send("PART %s", channel) self:send("PART %s", channel)
end end
color = {
black = 1,
blue = 2,
green = 3,
red = 4,
lightred = 5,
purple = 6,
brown = 7,
yellow = 8,
lightgreen = 9,
navy = 10,
cyan = 11,
lightblue = 12,
violet = 13,
gray = 14,
lightgray = 15,
white = 16
}
local colByte = string.char(3)
setmetatable(color, {__call = function(_, text, colornum)
colornum = type(colornum) == "string" and assert(color[colornum], "Invalid color '"..colornum.."'") or colornum
return table.concat{colByte, tostring(colornum), text, colByte}
end})
local boldByte = string.char(2)
function bold(text)
return boldByte..text..boldByte
end
local underlineByte = string.char(31)
function underline(text)
return underlineByte..text..underlineByte
end