-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathasyncoperations.lua
89 lines (72 loc) · 1.84 KB
/
asyncoperations.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local table = table
local assert = assert
local error = error
local select = select
local pairs = pairs
module "irc"
local meta = _META
function meta:send(msg, ...)
if select("#", ...) > 0 then
msg = msg:format(...)
end
self:invoke("OnSend", msg)
local bytes, err = self.socket:send(msg .. "\r\n")
if not bytes and err ~= "timeout" and err ~= "wantwrite" then
self:invoke("OnDisconnect", err, true)
self:shutdown()
error(err, errlevel)
end
end
local function verify(str, errLevel)
if str:find("^:") or str:find("%s%z") then
error(("malformed parameter '%s' to irc command"):format(str), errLevel)
end
return str
end
function meta:sendChat(target, msg)
-- Split the message into segments if it includes newlines.
for line in msg:gmatch("([^\r\n]+)") do
self:send("PRIVMSG %s :%s", verify(target, 3), line)
end
end
function meta:sendNotice(target, msg)
-- Split the message into segments if it includes newlines.
for line in msg:gmatch("([^\r\n]+)") do
self:send("NOTICE %s :%s", verify(target, 3), line)
end
end
function meta:join(channel, key)
if key then
self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3))
else
self:send("JOIN %s", verify(channel, 3))
end
end
function meta:part(channel)
channel = verify(channel, 3)
self:send("PART %s", channel)
if self.track_users then
self.channels[channel] = nil
end
end
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
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'")
if add then
mode = table.concat{"+", verify(add, 3)}
end
if rem then
mode = table.concat{mode, "-", verify(rem, 3)}
end
self:send("MODE %s %s", verify(target, 3), mode)
end