summaryrefslogtreecommitdiffstats
path: root/bot.lua
blob: 13c9f86904d736fb62023efbbcef71571a4241ca (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448

local cqueues = require("cqueues")
local socket = require("cqueues.socket")
local errno = require("cqueues.errno")
local condition = require("cqueues.condition")

local loop = cqueues.new()

local host, port, socketpath = ...


--------------------------------------------------------------------------------
-- fifo queue

local fifo = {}
function fifo.new()
	local list = {}
	list.next = list
	list.prev = list
	return setmetatable({list = list, condvar = condition.new()}, {__index = fifo})
end

function fifo:enqueue(data)
	local node = { data = data}
	node.next = self.list.next
	node.prev = self.list
	self.list.next.prev = node
	self.list.next = node
	self:signal()
	return self
end

function fifo:dequeue()
	local node = self.list.prev
	self.list.prev = node.prev
	self.list.prev.next = node.next
	return node.data
end

function fifo:empty()
	return self.list == self.list.next
end

function fifo:signal()
	self.condvar:signal()
end

function fifo:getcv()
	return self.condvar
end

--------------------------------------------------------------------------------
-- irc session
local irc = { callbacks = {} }

function irc.new(nick, username, password, realname)
	local self = {
		nick = nick or "albotty",
		username = username or "albotty",
		password = password,
		realname = realname or "albotty",
		hooks = {},
		channels = {},
		msgq = fifo.new()
	}
	return setmetatable(self, { __index = irc })
end

function irc:autojoin(channels)
	for _,chan in pairs(channels) do
		self.channels[chan] = self.channels[chan] or {}
		self.channels[chan].autojoin = true
		self.channels[chan].joined = false
	end
end

function irc:connect(host, port)
	local hostname, password, tls, timeout
	if type(host) == "table" then
		hostname = host.host
		port = host.port
		password = host.password
		tls = host.tls
		timeout = host.timeout
	else
		hostname = host
	end
	self.conn = socket.connect(hostname, port)
	self.conn:settimeout(timeout or 10)

	self:send("NICK %s", self.nick)
	self:send("USER %s 0 * :%s", self.username, self.realname)
	return self
end

function irc:pollfd()
	return self.conn:pollfd()
end

function irc:events()
	return self.conn:events()
end

function irc:send(msg, ...)
	if select("#", ...) > 0 then
		msg = msg:format(...)
	end
	self:run_hooks("OnSend", msg)
	self.conn:write(msg.."\r\n")
	self.conn:flush()
end

function irc:send_chat(target, message)
	for line in message:gmatch("([^\r\n]+)") do
		self:send("PRIVMSG %s :%s", target, line)
	end
end

function irc:join(channel)
	self.channels[channel] = self.channels[channel] or {}
	self.channels[channel].joined = true
	self:send("JOIN %s", channel)
end

function irc:hook(event, id, func)
	func = func or id
	self.hooks[event] = self.hooks[event] or {}
	self.hooks[event][id] = func
	return self
end

function irc:run_hooks(event, ...)
	for id, func in pairs(self.hooks[event] or {}) do
		if func(self, ...) then
			return true
		end
	end
end

local function irc_parse(line)
	local prefix
	local lineStart = 1
	if line:sub(1,1) == ":" then
		local space = line:find(" ")
		prefix = line:sub(2, space-1)
		lineStart = space
	end

	local _, trailToken = line:find("%s+:", lineStart)
	local lineStop = line:len()
	local trailing
	if trailToken then
		trailing = line:sub(trailToken + 1)
		lineStop = trailToken - 2
	end

	local params = {}

	local _, cmdEnd, cmd = line:find("(%S+)", lineStart)
	local pos = cmdEnd + 1
	while true do
		local _, stop, param = line:find("(%S+)", pos)
		if not param or stop > lineStop then
			break
		end

		pos = stop + 1
		params[#params + 1] = param
	end

	if trailing then
		params[#params + 1] = trailing
	end

	return prefix, cmd, params
end

local function parse_prefix(prefix)
	local user = {}
	if prefix then
		user.access, user.nick, user.username, user.host = prefix:match("^([%+@]*)(.+)!(.+)@(.+)$")
	end
	return user
end

irc.callbacks["001"] = function(self, prefix, me)
	self.authed = true
	self.nick = me
	for name, chan in pairs(self.channels) do
		if chan.autojoin then
			self:join(name)
		end
	end
end

irc.callbacks["NICK"] = function(self, prefix, newnick)
	local user = parse_prefix(prefix)
	if self.nick == user.nick then
		self.nick = newnick
	end
end

irc.callbacks["NICK"] = function(self, prefix, newnick)
	local user = parse_prefix(prefix)
	if self.nick == user.nick then
		self.nick = newnick
	end
	self:run_hooks("OnNick", user, newnick)
end

irc.callbacks["PING"] = function(self, prefix, query)
	self:send("PONG :%s", query)
end

irc.callbacks["PRIVMSG"] = function(self, prefix, channel, message)
	self:run_hooks("OnChat", parse_prefix(prefix), channel, message)
end

irc.callbacks["NOTICE"] = function(self, prefix, channel, message)
	self:run_hooks("OnNotice", parse_prefix(prefix), channel, message)
end

irc.callbacks["JOIN"] = function(self, prefix, channel)
	self:run_hooks("OnJoin", parse_prefix(prefix), channel)
end

irc.callbacks["PART"] = function(self, prefix, channel, reason)
	local user = parse_prefix(prefix)
	self:run_hooks("OnPart", user, channel, reason)
end

irc.callbacks["KICK"] = function(self, prefix, channel, kicked, reason)
	self:run_hooks("OnKick", channel, kicked, parse_prefix(prefix), reason)
end

irc.callbacks["QUIT"] = function(self, prefix, message)
	local user = parse_prefix(prefix)
	self:run_hooks("OnQuit", user, message)
end

-- nick in use
irc.callbacks["432"] = function(self, prefix, target, badnick)
	local n,i = string.match(self.nick, "(.*)(%d+)$")
	local newnick
	newnick = string.format("%s%d",n or self.nick, i+1)
	self:send("NICK %s", newnick)
end

irc.callbacks["433"] = irc.callbacks["422"]

-- no topic
irc.callbacks["331"] = function(self, prefix, me, channel)
	self:run_hooks("OnTopic", channel, nil)
end

irc.callbacks["332"] = function(self, prefix, me, channel, topic)
	self:run_hooks("OnTopic", channel, topic)
end

irc.callbacks["333"] = function(self, prefix, me, channel, nick, time)
	self:run_hooks("OnTopicInfo", channel, nick, tonumber(time))
end

irc.callbacks["TOPIC"] = function(self, prefix, channel, topic)
	self:run_hooks("OnTopic", channel, topic)
end

-- RPL_UMODEIS
irc.callbacks["221"] = function(self, prefix, user, modes)
	self:run_hooks("OnUserMode", modes)
end

-- RPL_CHANNELMODEIS
irc.callbacks["324"] = function(self, prefix, channel, modes)
	self:run_hooks("OnChannelMode", channel, modes)
end

irc.callbacks["MODE"] = function(self, prefix, target, modes, ...)
	self:run_hooks("OnModeChange", parse_prefix(prefix), target, modes, ...)
end


function irc:handle(line)
	local prefix, cmd, params = irc_parse(line)
	callback = self.callbacks[cmd]
	if type(callback) == "function" then
		return callback(self, prefix, table.unpack(params))
	else
		print("DEBUG: no handler for:", cmd, "prefix:",prefix)
	end
end


function irc:step()
	local line, why = self.conn:recv("*L")
	if not line or #line == 0 then
		return nil, why
	end
	self:handle(line)
	return line
end

function irc:loop()
	repeat
		repeat
			local ok, err = self:step()
		until not ok
		cqueues.poll(self)
	until self.conn:eof()
end

function irc:msgq_loop()
	while true do
		self.msgq:getcv():wait()
		local loop = cqueues.new()
		loop:wrap(function()
			while not self.msgq:empty() do
				local m = self.msgq:dequeue()
				self:send_chat(m.target, m.message)
				cqueues.sleep(1.0)
			end
		end)
		loop:loop()
	end
end

function irc:enqueue(target, msg)
	self.msgq:enqueue{target=target, message =msg}
end

--------------------------------------------------------------------------
-- sockserver

local sockserver = { callbacks = {}}
function sockserver.new(ircsession, socketpath, timeout)
	local path = socketpath or "/tmp/albotty.sock"
	local self = {
		path = path,
		ircsess = ircsession,
		socket = socket.listen{path=path},
		timeout = timeout,
	}
	return setmetatable(self, { __index = sockserver })
end

sockserver.callbacks["/msg"] = function(self, data)
	local dest,msg = string.match(data or "", "^([^ ]+) (.*)")
	if dest then
		self.ircsess:enqueue(dest, msg)
		return "ok"
	end
	return "error: msg format"
end

sockserver.callbacks["bye"] = function(serf, data)
	return "bye!"
end

function sockserver:run_callback(line)
	local cmd = string.match(line, "^([^ ]+)")
	local data = string.match(line, "^[^ ]+ (.*)")
	if not cmd or type(self.callbacks[cmd]) ~= "function" then
		return nil
	end
	return cmd, self.callbacks[cmd](self, data)
end

function sockserver:loop(loop)
	for conn in self.socket:clients(self.timeout) do
		loop:wrap(function()
			for line in conn:lines("*l") do
				print("DEBUG: got line:", line)
				local cmd, resp = self:run_callback(line)
				if not cmd then
					conn:write("error: unknown command\n")
				end
				if resp then
					conn:write(resp.."\n")
				end
				if cmd == "bye" then
					conn:shutdown("w")
					break
				end
			end
		end)
	end
end


---------------------------------------------------------------------------
-- main

sess = irc.new()

sess:hook("OnChat", function(self, user, channel, message)
	print("PRIVMSG:")
	print("  nick:", user.nick)
	print("  channel:", channel)
	print("  message:", message)
	if channel == self.nick then
		self:send_chat(user.nick, "Hi there")
	else
		self:send_chat(channel, "Hi "..user.nick)
	end
end)

sess:hook("OnJoin", function(self, user, channel)
	print("JOIN:")
	print("  nick:", user.nick)
	print("  channel:", channel)
end)

sess:hook("OnKick", function(self, channel, kicked, user, reason)
	print("Kicked:")
	print(("  %s kicked %s from %s"):format(user.nick, kicked, channel))
	if kicked == self.nick then
		self:join(channel)
	end
end)

sess:autojoin{"#alpine-test", "#alpine"}
sess:connect("172.16.3.54", "6667")

print("CONNECTED!")

local srv = sockserver.new(sess)

loop:wrap(function()
	sess:loop()
end)

loop:wrap(function()
	sess:msgq_loop()
end)

loop:wrap(function()
	srv:loop(loop)
end)

while not loop:empty() do
	local ok, err = loop:step()
	print("DEBUG: mainloop")
	if not ok then
		os.remove(srv.path)
		error("loop.step: "..err)
	end
end
os.remove(srv.path)