-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathd0009e.py
executable file
·522 lines (443 loc) · 14.7 KB
/
d0009e.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
import time
import threading
import traceback
import _thread
import random
import sys
import re
import signal
import configparser
from imp import reload
class ChannelStats:
def __init__(self):
self.lastMessage = time.time()
self.names = []
class Bot:
def __init__(self):
self.loadSettings()
self.channels = {}
for chan in self.chans:
self.channels[chan.upper()] = ChannelStats()
self.names_cur_channel = None
self.sock = None
self.msg_re = re.compile(b'^(:([^ ]+))? *([^ ]+) +:?([^ ]*) *:?(.*)$')
self.joined = False
self.commands = {}
self.contentCommands = {}
self.queryCommands = {}
self.authorized_users = []
self.nextTalk = time.time() + 15
self.loadPlugins()
def loadPlugins(self):
if "plugins" in dir(self):
self.saveSettings()
self.help = {}
self.commands = {}
self.byteCommands = {}
self.contentCommands = {}
self.queryCommands = {}
self.plugins = []
try:
reload(__import__('plugins'))
for i in __import__('plugins').__all__:
try:
plugin = __import__('plugins.%s' % i, fromlist=[None])
reload(plugin)
if "mainclass" in dir(plugin):
print("Loading", i)
obj = plugin.mainclass(self)
if self.config.has_section(plugin.__name__):
conflist = self.config.items(plugin.__name__)
conf = {}
for c in conflist:
conf[c[0]] = c[1]
obj.setConfig(conf)
else:
print("No config for plugin %s" % (plugin.__name__))
self.plugins.append(obj)
except:
traceback.print_exc()
except:
traceback.print_exc()
def loadSettings(self):
self.config = configparser.RawConfigParser()
self.config.read('d0009e.cfg')
self.irc_server = (self.config.get('connection', 'server'),
self.config.getint('connection', 'port'))
self.nick = self.config.get('connection', 'nick')
self.chans = self.config.get('connection', 'channels').split()
admins = self.config.get('users', 'admins')
self.users = [tuple(i.split(":")) for i in admins.split()]
def saveSettings(self):
self.lastSaveSettings = time.time()
for plugin in self.plugins:
conf = plugin.getConfigDict()
for key in list(conf.keys()):
self.config.set(plugin.__module__, key, conf[key])
self.config.set('connection', 'channels', " ".join(self.chans))
admins = " ".join([":".join(i) for i in self.users])
self.config.set('users', 'admins', admins)
try:
f = open("d0009e.cfg", "w")
self.config.write(f)
f.close()
except Exception as e:
print("saveSettings failed")
traceback.print_exc()
def run(self):
self.lastSaveSettings = 0
self.running = True
self.recvThread = RecvThread(self.sock)
self.recvThread.start()
self.connect()
while self.running:
if not self.recvThread.connected:
print("Not connected, reconnecting")
time.sleep(120)
self.connect()
try:
self.handleCommands()
if (time.time() - self.lastSaveSettings > 600):
self.saveSettings()
except:
traceback.print_exc()
if self.joined:
for plugin in self.plugins:
try:
plugin.on_tick(self)
except:
traceback.print_exc()
time.sleep(0.1)
self.sock.close()
def quit(self):
print("Saving settings")
self.saveSettings()
print("Quitting")
self.sendMessage("QUIT", ":SIGINT")
self.running = False
self.recvThread.quit = True
self.recvThread.join()
def connect(self):
while True:
try:
self.recvThread.connecting = True
self.joined = False
if self.sock is not None:
self.sock.close()
for res in socket.getaddrinfo(self.irc_server[0], self.irc_server[1], socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
print("Connecting")
print("af:",af)
print("socktype:", socktype)
print("proto:", proto)
print("canonname:", canonname)
print("sa:", sa)
try:
self.sock = socket.socket(af, socktype, proto)
keep = self.sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
if keep == 0:
print('Socket keepalive off, enabling.')
keep = self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
print('setsockopt=%s' % (keep))
# overrides value (in seconds) shown by sysctl net.ipv4.tcp_keepalive_time
self.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 1800)
# overrides value shown by sysctl net.ipv4.tcp_keepalive_probes
self.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 4)
# overrides value shown by sysctl net.ipv4.tcp_keepalive_intvl
self.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 15)
else:
print('Socket keepalive already on')
except socket.error as msg:
print("Creating socket error", msg)
self.sock = None
continue
try:
self.sock.connect(sa)
except socket.error as msg:
print("Connect: Socket error", msg)
self.sock.close()
self.sock = None
continue
break
if self.sock is None:
print("Could not open socket")
time.sleep(10)
continue
self.recvThread.sock = self.sock
self.recvThread.connecting = False
self.sendMessage('USER', '%s 8 *' % self.nick, 'Botten')
self.sendMessage('NICK', self.nick)
self.recvThread.connected = True
return
except Exception as e:
traceback.print_exc()
print("Connection failed, retrying in 10 seconds")
time.sleep(10)
def registerCommand(self, command, func):
self.commands[command] = func
def registerByteCommand(self, command, func):
self.byteCommands[command] = func
def registerContentCommand(self, regex, func):
self.contentCommands[regex] = func
def registerQueryCommand(self, command, func):
self.queryCommands[command] = func
def addHelp(self, command, helpMessage):
self.help[command] = helpMessage
def sendMessage(self, action, target, message = ""):
if not hasattr(self.sendMessage, "history"):
self.sendMessage.__func__.history = [] # static variable
if type(message) != type([]):
message = [message]
for i,m in enumerate(message):
# check history to see if we should do flood protection
while True:
# clean up history, remove everything older than 5 seconds
self.sendMessage.__func__.history = [x for x in self.sendMessage.history if x > time.time() - 5]
# pause if more than 10 messages was sent during the last 5 seconds
# and no more than 1 message every 0.1 seconds
if len(self.sendMessage.history) >= 10 or (len(self.sendMessage.history) >= 1 and self.sendMessage.history[-1] > time.time()-0.1):
time.sleep(0.1)
else:
break
# add new message to history
self.sendMessage.history.append(time.time())
m = str(m)
m = m.replace("\r", "")
m = m.replace("\n", "")
if len(m) >= 450:
m = "Error: Message too long"
if m:
# Julpynt
tm = time.localtime()
if action == "PRIVMSG" and tm.tm_mon == 12:
colorstr = '\x0f\x03%02d'
m = (colorstr % (random.randint(0,15))) + m
buf = "%s %s :%s\r\n" % (action, target, m)
else:
buf = "%s %s\r\n" % (action, target)
print("[031m>>[0m", buf, end=' ')
while buf:
try:
sent = self.sock.send(buf.encode())
buf = buf[sent:]
except:
traceback.print_exc()
print("Failed to send message")
self.recvThread.connected = False
def sendByteMessage(self, action, target, message = b""):
self.sendMessage(action.decode('utf-8'), target.decode('utf-8'),
message.decode('utf-8'))
def handleCommands(self):
while self.recvThread.commands != []:
line = self.recvThread.commands.pop()
print("[032m<<[0m", line)
m = self.msg_re.match(line)
source, action, target, message = m.group(2, 3, 4, 5)
command = message
args = ""
if b" " in message:
command = message.split()[0].lower()
args = message.split()[1:]
if action.upper() == b"001" and not self.joined:
for chan in self.chans:
self.sendMessage("JOIN", chan)
time.sleep(0.5)
self.joined = True
elif action.upper() == b"PING":
self.sendMessage("PONG", target)
elif action.upper() == b"ERROR":
# Reconnect
time.sleep(30)
self.connect()
elif action.upper() == b"353": # begin names
m = re.search(b'. ([^ ]+) :?(.+)$', message)
if not m:
continue
channel, names = m.group(1, 2)
if self.names_cur_channel != channel:
self.channels[channel.upper().decode('utf-8')].names = []
self.names_cur_channel = channel
# Remove prefixes
names = re.sub(br'[^0-9a-zA-Z\\^\[\]^_`{|} -]', b'', names)
self.channels[channel.upper().decode('utf-8')].names.extend(names.split())
elif action.upper() == b"366": # end names
self.names_cur_channel = None
elif action.upper() == b"JOIN":
nick = source.split(b"!")[0]
if not nick in self.channels[target.upper().decode('utf-8')].names:
self.channels[target.upper().decode('utf-8')].names.append(nick)
elif action.upper() == b"PART":
nick = source.split(b"!")[0]
self.channels[target.upper().decode('utf-8')].names.remove(nick)
elif action.upper() == b"QUIT":
nick = source.split(b"!")[0]
for i in list(self.channels.values()):
if nick in i.names:
i.names.remove(nick)
elif action.upper() == b"KICK":
nick = message.split()[0]
self.channels[target.upper().decode('utf-8')].names.remove(nick)
elif action.upper() == b"NICK":
nick = source.split(b"!")[0]
newnick = target
for i in list(self.channels.values()):
if nick in i.names:
i.names.remove(nick)
i.names.append(newnick)
elif command.upper() == b"!RELOAD" and args == "":
if target.upper() == self.nick.upper():
target = source.split(b"!")[0]
if not source in self.authorized_users:
self.sendByteMessage(b"PRIVMSG", target,
b"reload: Access denied")
continue
print("Reloading")
self.loadPlugins()
self.sendByteMessage(b"PRIVMSG", target, b"reload successful")
elif action.upper() == b"PRIVMSG":
if target.upper() == self.nick.encode().upper():
if message.upper() == b"\001VERSION\001":
nick = source.split(b"!")[0].lstrip(b":")
if nick[0] == b":": nick = nick[1:]
self.sendByteMessage(b"NOTICE", nick,
"\001VERSION En bot i sina bästa år\001".encode())
elif message.upper() == b"\001TIME\001":
nick = source.split(b"!")[0].lstrip(b":")
self.sendByteMessage(b"NOTICE", nick,
"\001Time 13:37 - The time of leet\001".encode())
elif command.upper() == b"AUTH":
nick, userhost = source.split(b"!")
nick = nick.decode('utf-8')
if len(args) >= 2:
args[0] = args[0].decode('utf-8')
args[1] = args[1].decode('utf-8')
if len(args) >= 2 and (args[0].lower(), args[1]) in self.users:
self.authorized_users.append(source)
self.sendMessage("NOTICE", nick,
"You are now authenticated as %s" % (args[0]))
else:
self.sendMessage(b"NOTICE", nick,
"Wrong username or password")
else:
nick, userhost = source.split(b"!")
try:
if command in self.queryCommands:
strargs = [x.decode('utf-8') for x in args]
_thread.start_new_thread(self.queryCommands[command],
(self, nick.decode('utf-8'), args))
if command in self.byteCommands:
_thread.start_new_thread(self.byteCommands[command],
(self, nick, args))
if command in self.commands:
strargs = [x.decode('utf-8') for x in args]
_thread.start_new_thread(self.commands[command.decode('utf-8')],
(self, nick.decode('utf-8'), strargs))
except Exception as e:
traceback.print_exc()
self.sendByteMessage(b"PRIVMSG", target, b"Error!!")
elif target.upper().decode('utf-8') in self.channels:
chanstats = self.channels[target.upper().decode('utf-8')]
chanstats.lastMessage = time.time()
try:
strcommand = None
try:
strcommand = command.decode('utf-8')
except:
pass
if command in self.byteCommands:
_thread.start_new_thread(self.byteCommands[command],
(self, target, args))
elif strcommand and strcommand in self.commands:
strargs = [x.decode('utf-8') for x in args]
_thread.start_new_thread(self.commands[strcommand],
(self, target.decode('utf-8'), strargs))
else:
strmessage = None
try:
strmessage = message.decode('utf-8')
except:
pass
if strmessage:
for contentCmd in self.contentCommands:
if re.search(contentCmd, strmessage):
_thread.start_new_thread(
self.contentCommands[contentCmd],
(self, target.decode('utf-8'),
strmessage))
break
except Exception as e:
traceback.print_exc()
self.sendByteMessage(b"PRIVMSG", target, b"Error!!")
class RecvThread(threading.Thread):
def __init__(self, sock):
self.sock = sock
self.quit = False
self.connected = False
self.connecting = True
self.commands = []
threading.Thread.__init__(self)
def run(self):
buffer = b""
socket.setdefaulttimeout(30)
endpoint_notconnected_count = 0
while not self.quit:
if self.connecting:
time.sleep(0.1)
continue # wait until connected before doing anything
if self.sock is None:
self.connected = False
continue
try:
buffer += self.sock.recv(1024)
commands = buffer.split(b"\r\n")[:-1]
buffer = buffer[buffer.rfind(b"\r\n")+2:]
for command in commands:
self.addCommand(command)
endpoint_notconnected_count = 0
except socket.timeout:
print("Timeout")
#self.connected = False
except socket.error as xxx_todo_changeme:
(value, message) = xxx_todo_changeme.args
if value == 103: # software caused connection reset
print(value, message)
self.connected = False
elif value == 104: # connection reset by peer
print(value, message)
self.connected = False
elif value == 107: # transport endpoint not connected
print(value, message)
endpoint_notconnected_count += 1
if endpoint_notconnected_count > 20:
self.connected = False
endpoint_notconnected_count = 0
time.sleep(2)
elif value == 110: # connection timed out
print(value, message)
print("Sleeping 10 seconds, then retrying")
time.sleep(10)
self.connected = False
elif value == 9: # Bad socket descriptor
print(value, message)
print("Sleeping 10 seconds, then retrying")
time.sleep(10)
self.connected = False
else:
traceback.print_exc()
print("else:", value, message)
time.sleep(0.1)
except Exception as e:
print("other exception")
traceback.print_exc()
time.sleep(0.1)
def addCommand(self, command):
self.commands.append(command)
b = Bot()
def keyint(signum, frame):
print("Received signal:", signum)
b.quit()
signal.signal(signal.SIGINT, keyint)
b.run()