-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircbot.py
541 lines (363 loc) · 17.3 KB
/
ircbot.py
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#! /usr/bin/python3
from enum import Enum
import math
import select
import socket
import sys
import threading
import time
import traceback
class more():
limit = 450
def __init__(self, channel, command, channels):
self.channel = channel
self.command = command
self.more = dict()
for channel in channels:
self.more[channel] = ''
def has_more(self, channel):
return self.more[channel] != ''
def send(self, channel, text):
try:
if len(text) > more.limit:
self.more[channel] = text
self.send_more(channel)
else:
if channel[0] == '\\':
channel = channel[1:]
self.channel.send(f'{self.command} {channel} :{text}')
self.more[channel] = ''
except Exception as e:
print(f'more::send: exception "{e}" at {e.__traceback__.tb_lineno}')
def send_more(self, channel):
try:
if not channel in self.more or self.more[channel] == '':
if channel[0] == '\\':
channel = channel[1:]
self.channel.send(f'{self.command} {channel} :No more more')
else:
space = self.more[channel].find(' ', 450, more.limit - 25)
if space == -1:
space = more.limit - 25
current_more = self.more[channel][0:space].strip()
if len(self.more[channel]) > space:
self.more[channel] = self.more[channel][space:].strip()
else:
self.more[channel] = ''
n = math.ceil(len(self.more[channel]) / more.limit)
if channel[0] == '\\':
channel = channel[1:]
self.channel.send(f'{self.command} {channel} :{current_more} \3{4}({n} more)')
except Exception as e:
print(f'more::send_more: exception "{e}" at {e.__traceback__.tb_lineno}')
class ircbot(threading.Thread):
class session_state(Enum):
DISCONNECTED = 0x00 # setup socket, connect to host
CONNECTED_PASS = 0x01 # send PASS
CONNECTED_NICK = 0x02 # send NICK
CONNECTED_USER = 0x03 # send USER
USER_WAIT = 0x08 # wait for USER ack
CONNECTED_JOIN = 0x10 # send JOIN
CONNECTED_WAIT = 0x11 # wait for 'JOIN' indicating that the JOIN succeeded
RUNNING = 0xf0 # go
DISCONNECTING = 0xff
state_timeout = 30 # state changes must not take longer than this
def __init__(self, host, port, nick, password, channels):
super().__init__()
self.host = host
self.port = port
self.nick = nick
self.password = password
self.channels = channels
self.joined_ch = dict()
self.fd = None
self.owner = 'flok'
self.state = self.session_state.DISCONNECTED
self.state_since = time.time()
self.users = dict()
self.cond_352 = threading.Condition()
self.topics = dict()
self.more_priv = more(self, 'PRIVMSG', channels)
self.more_noti = more(self, 'NOTICE', channels)
for channel in channels:
self.joined_ch[channel] = False
def _set_state(self, s):
print(f'_set_state: state changes from {self.state} to {s}')
self.state = s
self.state_since = time.time()
def get_state(self):
return self.state
def send(self, s):
try:
print(s)
self.fd.send(f'{s}\r\n'.encode('utf-8'))
return True
except Exception as e:
print(f'irc::send: failed transmitting to IRC server: {e}')
self.fd.close()
self._set_state(self.session_state.DISCONNECTED)
return False
def send_notice(self, channel, text):
self.more_noti.send(channel, text)
def send_ok(self, channel, text):
self.more_priv.send(channel, text)
def send_more(self, channel):
if self.more_noti.has_more(channel):
self.more_noti.send_more(channel)
elif self.more_priv.has_more(channel):
self.more_priv.send_more(channel)
else:
self.send_ok(channel, 'No more more')
def send_error(self, channel, text):
self.more_priv.send(channel, f'\3{4}ERROR: \2{text}')
def send_error_notice(self, channel, text):
self.more_noti.send(channel, f'\3{4}ERROR: \2{text}')
def parse_irc_line(self, s):
# from https://stackoverflow.com/questions/930700/python-parsing-irc-messages
prefix = ''
trailing = []
if s[0] == ':':
prefix, s = s[1:].split(' ', 1)
if s.find(' :') != -1:
s, trailing = s.split(' :', 1)
args = s.split()
args.append(trailing)
else:
args = s.split()
command = args.pop(0)
return prefix, command, args
def invoke_who_and_wait(self, user):
self.send(f'WHO {user}')
start = time.time()
while self.check_user_known(user) == False:
t_diff = time.time() - start
if t_diff >= 5.0:
break
with self.cond_352:
self.cond_352.wait(5.0 - t_diff)
def similar_to(self, wrong):
assert False
def invoke_internal_commands(self, prefix, command, splitted_args, channel):
return self.internal_command_rc.NOT_INTERNAL
def handle_irc_commands(self, prefix, command, args):
if len(command) == 3 and command.isnumeric():
if command == '001':
if self.state == self.session_state.USER_WAIT:
self._set_state(self.session_state.CONNECTED_JOIN)
else:
print(f'irc::run: invalid state for "001" command {self.state}')
self._set_state(self.session_state.DISCONNECTING)
elif command == '352': # reponse to 'WHO'
#print(prefix, command, args)
self.users[args[5].lower()] = f'{args[5]}!{args[2]}@{args[3]}'
elif command == '353': # users in the channel
for user in args[3].split(' '):
self.users[user.lower()] = '?'
elif command == '331' or command == '332': # no topic set / topic
self.topics[args[1][1:]] = args[2]
self.mqtt.publish(f'from/irc/{args[1][1:]}/topic', args[2])
# 315 is 'end of who'
if command == '352' or command == '315':
with self.cond_352:
self.cond_352.notify_all()
elif command == 'JOIN':
if self.state == self.session_state.CONNECTED_WAIT:
self.joined_ch[args[0]] = True
all_joined = True
for channel in self.joined_ch:
if self.joined_ch[channel] == False:
all_joined = False
break
if all_joined:
self._set_state(self.session_state.RUNNING)
self.users[prefix.split('!')[0].lower()] = prefix.lower()
elif command == 'PART' or command == 'QUIT':
#print(prefix, command)
nick = prefix.split('!')[0].lower()
if nick in self.users:
del self.users[nick]
elif command == 'KICK':
nick = args[1].lower()
if nick in self.users:
del self.users[nick]
elif command == 'NICK':
try:
old_lower_prefix = prefix.lower()
excl_mark = old_lower_prefix.find('!')
old_user = old_lower_prefix[0:excl_mark]
if old_user in self.users:
del self.users[old_user]
new_user = args[0].lower()
new_prefix = new_user + old_lower_prefix[excl_mark:]
self.users[new_user] = new_prefix
#print(f'{old_lower_prefix} => {new_prefix}')
except Exception as e:
send_notice(self.owner, f'irc::handle_irc_command: exception "{e}" during execution of IRC command NICK at line number: {e.__traceback__.tb_lineno}')
elif command == 'PING':
if len(args) >= 1:
self.send(f'PONG {args[0]}')
else:
self.send(f'PONG')
elif command == 'PRIVMSG':
#print(args)
if len(args) >= 2 and len(args[1]) >= 2:
channel = args[0]
text = args[1]
if text[0] == self.cmd_prefix:
for i in range(0, 8): # to prevent infinite alias-loops
is_command, new_text, is_notice = self.check_aliasses(text[1:], prefix)
if new_text != None:
if not is_command:
if is_notice:
self.send_notice(channel, new_text)
else:
self.send_ok(channel, new_text)
return
text = self.cmd_prefix + new_text
if text[0] == self.cmd_prefix:
parts = text[1:].split(' ')
command = parts[0]
if not command in self.plugins:
nick = prefix.split('!')[0].lower()
method = self.send_error_notice
if channel == self.nick:
channel = prefix
if '!' in channel:
channel = channel[0:channel.find('!')]
method = self.send_error
if command in self.plugins_gone:
method(channel, f'{nick}: command "{command}" is unresponsive for {time.time() - self.plugins_gone[command]:.2f} seconds')
else:
suggestions = set([x for x in self.similar_to(command) if x != None])
method(channel, f'{nick}: command "{command}" is not known (maybe {" or ".join(suggestions)}?)')
else:
access_granted, group_for_command = self.check_acls(prefix, command)
response_channel = (prefix[0:prefix.find('!')] if '!' in prefix else prefix) if channel == self.nick else channel
if access_granted:
# returns False when the command is not internal
rc = self.invoke_internal_commands(prefix, command, parts, channel)
if rc == self.internal_command_rc.HANDLED:
pass
elif rc == self.internal_command_rc.NOT_INTERNAL:
if channel == self.nick:
person = prefix
if '!' in person:
person = person[0:person.find('!')]
self.mqtt.publish(f'from/irc/\\{person}/{prefix}/{command}', text)
else:
self.mqtt.publish(f'from/irc/{channel[1:]}/{prefix}/{command}', text)
elif rc == self.internal_command_rc.ERROR:
pass
else:
self.send_error(response_channel, f'irc::run: unexpected return code from internal commands handler ({rc})')
else:
self.send_error(response_channel, f'Command "{command}" denied for user "{prefix}", one must be in {group_for_command}')
else:
self.mqtt.publish(f'from/irc/{channel[1:]}/{prefix}/message', args[1])
elif command == 'NOTICE':
if len(args) >= 2:
self.mqtt.publish(f'from/irc/{args[0][1:]}/{prefix}/notice', args[1])
elif command == 'TOPIC':
self.topics[args[0][1:]] = args[1]
self.mqtt.publish(f'from/irc/{args[0][1:]}/topic', args[1])
elif command == 'INVITE':
# do not enter any channel, only the selected
for channel in self.channels:
if self.send(f'JOIN {channel}') == False:
self._set_state(self.session_state.DISCONNECTING)
break
else:
print(f'irc::run: command "{command}" is not known (for {prefix})')
def irc_command_insertion_point(self, prefix, command, arguments):
return True
def handle_irc_command_thread_wrapper(self, prefix, command, arguments):
try:
if self.irc_command_insertion_point(prefix, command, arguments):
self.handle_irc_commands(prefix, command, arguments)
except Exception as e:
print(f'irc::handle_irc_command_thread_wrapper: exception "{e}" during execution of IRC command "{command}" at line number: {e.__traceback__.tb_lineno}')
traceback.print_exc(file=sys.stdout)
def run(self):
print('irc::run: started')
buffer = ''
while True:
if self.state == self.session_state.DISCONNECTING:
self.fd.close()
self._set_state(self.session_state.DISCONNECTED)
elif self.state == self.session_state.DISCONNECTED:
self.fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(f'irc::run: connecting to [{self.host}]:{self.port}')
try:
self.fd.connect((self.host, self.port))
self.poller = select.poll()
self.poller.register(self.fd, select.POLLIN)
self._set_state(self.session_state.CONNECTED_PASS)
except Exception as e:
print(f'irc::run: failed to connect: {e}')
self.fd.close()
elif self.state == self.session_state.CONNECTED_PASS:
if self.send(f'PASS {self.password}'):
self._set_state(self.session_state.CONNECTED_NICK)
elif self.state == self.session_state.CONNECTED_NICK:
# apparently only error responses are returned, no acks
if self.send(f'NICK {self.nick}'):
self._set_state(self.session_state.CONNECTED_USER)
elif self.state == self.session_state.CONNECTED_USER:
if self.send(f'USER {self.nick} 0 * :{self.nick}'):
self._set_state(self.session_state.USER_WAIT)
elif self.state == self.session_state.CONNECTED_JOIN:
all_ok = True
for channel in self.channels:
if self.send(f'JOIN {channel}') == False:
all_ok = False
break
if all_ok:
self._set_state(self.session_state.CONNECTED_WAIT)
elif self.state == self.session_state.USER_WAIT:
# handled elsewhere
pass
elif self.state == self.session_state.CONNECTED_WAIT:
# handled elsewhere
pass
elif self.state == self.session_state.RUNNING:
pass
else:
print(f'irc::run: internal error, invalid state {self.state}')
if self.state != self.session_state.DISCONNECTED and (len(buffer) > 0 or len(self.poller.poll(100)) > 0):
lf_index = buffer.find('\n')
if lf_index == -1:
try:
buffer += self.fd.recv(4096).decode('utf-8')
except Exception as e:
print(f'irc::run: cannot decode text from irc-server')
lf_index = buffer.find('\n')
if lf_index == -1:
continue
line = buffer[0:lf_index].rstrip('\r').strip()
buffer = buffer[lf_index + 1:]
prefix, command, arguments = self.parse_irc_line(line)
t = threading.Thread(target=self.handle_irc_command_thread_wrapper, args=(prefix, command, arguments), daemon=True)
t.name = 'GHBot input'
t.start()
if not self.state in [ self.session_state.DISCONNECTED, self.session_state.DISCONNECTING, self.session_state.RUNNING ]:
takes = time.time() - self.state_since
if takes > ircbot.state_timeout:
#print(f'irc::run: state {self.state} timeout ({takes} > {ircbot.state_timeout})')
self._set_state(self.session_state.DISCONNECTING)
class irc_keepalive(threading.Thread):
def __init__(self, i):
super().__init__()
self.i = i
self.name = 'GHBot keepalive'
self.start()
def run(self):
while True:
try:
if self.i.get_state() == ircbot.session_state.RUNNING:
self.i.send('TIME')
time.sleep(30)
else:
time.sleep(5)
except Exception as e:
print(f'irc_keepalive::run: exception {e}')
time.sleep(1)