2010-04-04 16:55:12 +02:00
|
|
|
local table = table
|
|
|
|
|
|
|
|
module "irc"
|
|
|
|
|
|
|
|
local meta = _META
|
|
|
|
|
|
|
|
function meta:send(fmt, ...)
|
2010-07-17 15:12:42 +02:00
|
|
|
local bytes, err = self.socket:send(fmt:format(...) .. "\r\n")
|
|
|
|
|
|
|
|
if bytes then
|
|
|
|
return
|
|
|
|
end
|
2010-04-04 16:55:12 +02:00
|
|
|
|
2010-07-17 15:16:01 +02:00
|
|
|
if err ~= "timeout" and err ~= "wantwrite" then
|
|
|
|
self:invoke("OnDisconnect", err, true)
|
|
|
|
self:shutdown()
|
|
|
|
error(err, errlevel)
|
2010-04-04 16:55:12 +02:00
|
|
|
end
|
|
|
|
end
|
2010-07-17 15:16:01 +02:00
|
|
|
|
2010-07-18 10:26:04 +02:00
|
|
|
local function verify(str, errLevel)
|
|
|
|
if str:find("^:") or find("%s%z") then
|
|
|
|
error(("bad characters in '%s'"):format(str), errLevel)
|
|
|
|
end
|
|
|
|
|
|
|
|
return str
|
2010-07-17 15:16:01 +02:00
|
|
|
end
|
|
|
|
|
2010-06-01 01:39:34 +02:00
|
|
|
function meta:sendChat(target, msg)
|
2010-07-18 09:24:13 +02:00
|
|
|
-- Split the message into segments if it includes newlines.
|
|
|
|
for line in msg:gmatch("([^\r\n]+)")
|
2010-07-18 13:08:33 +02:00
|
|
|
self:send("PRIVMSG %s :%s", verify(target, 3), msg)
|
2010-07-18 09:24:13 +02:00
|
|
|
end
|
2010-06-01 01:39:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function meta:sendNotice(target, msg)
|
2010-07-18 09:24:13 +02:00
|
|
|
-- Split the message into segments if it includes newlines.
|
|
|
|
for line in msg:gmatch("([^\r\n]+)")
|
2010-07-18 13:08:33 +02:00
|
|
|
self:send("NOTICE %s :%s", verify(target, 3), msg)
|
2010-07-18 09:24:13 +02:00
|
|
|
end
|
2010-06-01 01:39:34 +02:00
|
|
|
end
|
|
|
|
|
2010-04-04 16:55:12 +02:00
|
|
|
function meta:join(channel, key)
|
|
|
|
if key then
|
2010-07-18 13:08:33 +02:00
|
|
|
self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3))
|
2010-04-04 16:55:12 +02:00
|
|
|
else
|
2010-07-18 13:08:33 +02:00
|
|
|
self:send("JOIN %s", verify(channel, 3))
|
2010-04-04 16:55:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function meta:part(channel)
|
2010-07-18 13:08:33 +02:00
|
|
|
channel = verify(channel, 3)
|
2010-06-28 05:58:46 +02:00
|
|
|
self:send("PART %s", channel)
|
2010-07-17 04:48:32 +02:00
|
|
|
if self.track_users then
|
2010-06-28 05:58:46 +02:00
|
|
|
self.channels[channel] = nil
|
2010-07-17 04:48:32 +02:00
|
|
|
end
|
2010-04-04 16:55:12 +02:00
|
|
|
end
|
|
|
|
|
2010-06-01 01:39:34 +02:00
|
|
|
function meta:trackUsers(b)
|
|
|
|
self.track_users = b
|
|
|
|
if not b then
|
|
|
|
for k,v in pairs(self.channels) do
|
|
|
|
self.channels[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-04 16:55:12 +02:00
|
|
|
function meta:setMode(t)
|
|
|
|
local target = t.target or self.nick
|
|
|
|
local mode = ""
|
|
|
|
local add, rem = t.add, t.remove
|
|
|
|
|
|
|
|
assert(add or rem, "table contains neither 'add' nor 'remove'")
|
2010-07-18 11:43:50 +02:00
|
|
|
|
2010-04-04 16:55:12 +02:00
|
|
|
if add then
|
|
|
|
mode = table.concat{"+", add}
|
|
|
|
end
|
|
|
|
|
|
|
|
if rem then
|
|
|
|
mode = table.concat{mode, "-", rem}
|
|
|
|
end
|
2010-07-18 11:43:50 +02:00
|
|
|
|
2010-07-18 13:08:33 +02:00
|
|
|
self:send("MODE %s %s", verify(target, 3), verify(mode, 3))
|
2010-04-04 16:55:12 +02:00
|
|
|
end
|