]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | # vim: fileencoding=utf-8 | |
3 | ||
4 | # Erebus IRC bot - Author: John Runyon | |
5 | # "Bot" and "BotConnection" classes (handling a specific "arm") | |
6 | ||
7 | import socket, sys, time, threading, os, random, struct | |
8 | from collections import deque | |
9 | ||
10 | if sys.version_info.major < 3: | |
11 | timerbase = threading._Timer | |
12 | stringbase = basestring | |
13 | else: | |
14 | timerbase = threading.Timer | |
15 | stringbase = str | |
16 | ||
17 | class MyTimer(timerbase): | |
18 | def __init__(self, *args, **kwargs): | |
19 | timerbase.__init__(self, *args, **kwargs) | |
20 | self.daemon = True | |
21 | ||
22 | if sys.version_info.major < 3: | |
23 | stringbase = basestring | |
24 | else: | |
25 | stringbase = str | |
26 | ||
27 | #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')} | |
28 | class Bot(object): | |
29 | def __init__(self, parent, nick, user, bind, authname, authpass, server, port, realname): | |
30 | self.parent = parent | |
31 | self.nick = nick | |
32 | self.permnick = nick | |
33 | self.user = user | |
34 | self.realname = realname | |
35 | ||
36 | self.authname = authname | |
37 | self.authpass = authpass | |
38 | ||
39 | self.connecttime = 0 # time at which we received numeric 001 | |
40 | self.server = server # the address we try to (re-)connect to | |
41 | self.port = port | |
42 | self.servername = server # the name of the server we got connected to | |
43 | ||
44 | curs = self.parent.query("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.permnick,)) | |
45 | if curs: | |
46 | chansres = curs.fetchall() | |
47 | curs.close() | |
48 | self.chans = [self.parent.newchannel(self, row['chname']) for row in chansres] | |
49 | else: | |
50 | self.chans = [] | |
51 | ||
52 | self.conn = BotConnection(self, bind, server, port) | |
53 | ||
54 | self.lastreceived = time.time() #time we last received a line from the server | |
55 | self.watchdog() | |
56 | ||
57 | self.msgqueue = deque() | |
58 | self.slowmsgqueue = deque() | |
59 | self._makemsgtimer() | |
60 | self._msgtimer.start() | |
61 | ||
62 | def __del__(self): | |
63 | try: | |
64 | curs = self.parent.query("UPDATE bots SET connected = 0 WHERE nick = %s", (self.permnick,)) | |
65 | curs.close() | |
66 | except: pass | |
67 | ||
68 | def watchdog(self): | |
69 | if time.time() > int(self.parent.cfg.get('watchdog', 'maxtime', default=300))+self.lastreceived: | |
70 | self.parse("ERROR :Fake-error from watchdog timer.") | |
71 | self.watchdogtimer = MyTimer(int(self.parent.cfg.get('watchdog', 'interval', default=30)), self.watchdog) | |
72 | ||
73 | def log(self, *args, **kwargs): | |
74 | self.parent.log(self.nick, *args, **kwargs) | |
75 | ||
76 | def connect(self): | |
77 | self.log('!', "Connecting") | |
78 | if self.conn.connect(): | |
79 | self.parent.newfd(self, self.conn.socket.fileno()) | |
80 | ||
81 | def getdata(self): | |
82 | self.lastreceived = time.time() | |
83 | return self.conn.read() | |
84 | ||
85 | def _checknick(self): # check if we're using the right nick, try changing | |
86 | if self.nick != self.permnick and self.conn.registered(): | |
87 | self.conn.send("NICK %s" % (self.permnick)) | |
88 | ||
89 | def parse(self, line): | |
90 | if self.parent.cfg.getboolean('debug', 'io'): | |
91 | self.log('I', line) | |
92 | pieces = line.split() | |
93 | ||
94 | # dispatch dict | |
95 | zero = { #things to look for without source | |
96 | 'NOTICE': self._gotconnected, | |
97 | 'PING': self._gotping, | |
98 | 'ERROR': self._goterror, | |
99 | } | |
100 | one = { #things to look for after source | |
101 | 'NOTICE': self._gotconnected, | |
102 | '001': self._got001, | |
103 | '004': self._got004, | |
104 | '376': self._gotRegistered, | |
105 | '422': self._gotRegistered, | |
106 | 'PRIVMSG': self._gotprivmsg, | |
107 | '353': self._got353, #NAMES | |
108 | '354': self._got354, #WHO | |
109 | '396': self._gotHiddenHost, # hidden host has been set | |
110 | '433': self._got433, #nick in use | |
111 | 'JOIN': self._gotjoin, | |
112 | 'PART': self._gotpart, | |
113 | 'KICK': self._gotkick, | |
114 | 'QUIT': self._gotquit, | |
115 | 'NICK': self._gotnick, | |
116 | 'MODE': self._gotmode, | |
117 | } | |
118 | ||
119 | if self.parent.hasnumhook(pieces[1]): | |
120 | hooks = self.parent.getnumhook(pieces[1]) | |
121 | for callback in hooks: | |
122 | try: | |
123 | callback(self, line) | |
124 | except Exception: | |
125 | self.__debug_cbexception("numhook", line) | |
126 | ||
127 | if pieces[0] in zero: | |
128 | zero[pieces[0]](pieces) | |
129 | elif pieces[1] in one: | |
130 | one[pieces[1]](pieces) | |
131 | ||
132 | def _gotconnected(self, pieces): | |
133 | if not self.conn.registered(): | |
134 | self.conn.register() | |
135 | def _gotping(self, pieces): | |
136 | self.conn.send("PONG %s" % (pieces[1])) | |
137 | self._checknick() | |
138 | def _goterror(self, pieces): | |
139 | # TODO: better handling, just reconnect that single bot | |
140 | try: | |
141 | self.quit("Error detected: %s" % ' '.join(pieces)) | |
142 | except: pass | |
143 | try: | |
144 | curs = self.parent.query("UPDATE bots SET connected = 0") | |
145 | curs.close() | |
146 | except: pass | |
147 | sys.exit(2) | |
148 | os._exit(2) | |
149 | def _got001(self, pieces): | |
150 | # We wait until the end of MOTD instead to consider ourselves registered, but consider uptime as of 001 | |
151 | self.connecttime = time.time() | |
152 | def _got004(self, pieces): | |
153 | self.servername = pieces[3] | |
154 | def _gotRegistered(self, pieces): | |
155 | self.conn.registered(True) | |
156 | ||
157 | curs = self.parent.query("UPDATE bots SET connected = 1 WHERE nick = %s", (self.permnick,)) | |
158 | if curs: curs.close() | |
159 | ||
160 | self.conn.send("MODE %s +x" % (pieces[2])) | |
161 | if self.authname is not None and self.authpass is not None: | |
162 | self.conn.send("AUTH %s %s" % (self.authname, self.authpass)) | |
163 | if not self.parent.cfg.getboolean('erebus', 'wait_for_hidden_host'): | |
164 | for c in self.chans: | |
165 | self.join(c.name) | |
166 | def _gotHiddenHost(self, pieces): | |
167 | if self.parent.cfg.getboolean('erebus', 'wait_for_hidden_host'): | |
168 | for c in self.chans: | |
169 | self.join(c.name) | |
170 | def _gotprivmsg(self, pieces): | |
171 | nick = pieces[0].split('!')[0][1:] | |
172 | user = self.parent.user(nick) | |
173 | target = pieces[2] | |
174 | msg = ' '.join(pieces[3:])[1:] | |
175 | self.parsemsg(user, target, msg) | |
176 | def _got353(self, pieces): | |
177 | prefixes = {'@': 'op', '+': 'voice'} | |
178 | chan = self.parent.channel(pieces[4]) | |
179 | names = pieces[5:] | |
180 | names[0] = names[0][1:] #remove colon | |
181 | for n in names: | |
182 | if n[0] in prefixes: | |
183 | user = self.parent.user(n[1:]) | |
184 | chan.userjoin(user, prefixes[n[0]]) | |
185 | else: | |
186 | user = self.parent.user(n) | |
187 | chan.userjoin(user) | |
188 | user.join(chan) | |
189 | def _got354(self, pieces): | |
190 | qt = int(pieces[3]) | |
191 | if qt < 3: | |
192 | nick, auth = pieces[4:6] | |
193 | chan = None | |
194 | else: | |
195 | chan, nick, auth = pieces[4:7] | |
196 | chan = self.parent.channel(chan) | |
197 | user = self.parent.user(nick) | |
198 | user.authed(auth) | |
199 | ||
200 | if chan is not None: | |
201 | user.join(chan) | |
202 | chan.userjoin(user) | |
203 | ||
204 | if qt == 2: # triggered by !auth | |
205 | if user.isauthed(): | |
206 | if user.glevel > 0: | |
207 | self.msg(nick, "You are now known as #%s (access level: %s)" % (auth, user.glevel)) | |
208 | else: | |
209 | self.msg(nick, "You are now known as #%s (not staff)" % (auth)) | |
210 | else: | |
211 | self.msg(nick, "I tried, but you're not authed!") | |
212 | def _got433(self, pieces): | |
213 | if not self.conn.registered(): #we're trying to connect | |
214 | newnick = "%s%d" % (self.nick, random.randint(111, 999)) | |
215 | self.conn.send("NICK %s" % (newnick)) | |
216 | self.nick = newnick | |
217 | def _gotjoin(self, pieces): | |
218 | nick = pieces[0].split('!')[0][1:] | |
219 | chan = self.parent.channel(pieces[2]) | |
220 | ||
221 | if nick == self.nick: | |
222 | self.conn.send("WHO %s c%%cant,3" % (chan)) | |
223 | else: | |
224 | user = self.parent.user(nick, justjoined=True) | |
225 | chan.userjoin(user) | |
226 | user.join(chan) | |
227 | def _clientLeft(self, nick, chan): | |
228 | if nick != self.nick: | |
229 | gone = self.parent.user(nick).part(chan) | |
230 | chan.userpart(self.parent.user(nick)) | |
231 | if gone: | |
232 | self.parent.user(nick).quit() | |
233 | del self.parent.users[nick.lower()] | |
234 | def _gotpart(self, pieces): | |
235 | nick = pieces[0].split('!')[0][1:] | |
236 | chan = self.parent.channel(pieces[2]) | |
237 | self._clientLeft(nick, chan) | |
238 | def _gotkick(self, pieces): | |
239 | nick = pieces[3] | |
240 | chan = self.parent.channel(pieces[2]) | |
241 | self._clientLeft(nick, chan) | |
242 | def _gotquit(self, pieces): | |
243 | nick = pieces[0].split('!')[0][1:] | |
244 | if nick != self.nick: | |
245 | for chan in self.parent.user(nick).chans: | |
246 | chan.userpart(self.parent.user(nick)) | |
247 | self.parent.user(nick).quit() | |
248 | del self.parent.users[nick.lower()] | |
249 | def _gotnick(self, pieces): | |
250 | oldnick = pieces[0].split('!')[0][1:] | |
251 | newnick = pieces[2][1:] | |
252 | if oldnick == self.nick: | |
253 | self.nick = newnick | |
254 | else: | |
255 | if newnick.lower() != oldnick.lower(): | |
256 | self.parent.users[newnick.lower()] = self.parent.users[oldnick.lower()] | |
257 | del self.parent.users[oldnick.lower()] | |
258 | self.parent.users[newnick.lower()].nickchange(newnick) | |
259 | def _gotmode(self, pieces): | |
260 | source = pieces[0].split('!')[0][1:] | |
261 | chan = pieces[2] | |
262 | if not chan.startswith("#"): return | |
263 | chan = self.parent.channel(pieces[2]) | |
264 | mode = pieces[3] | |
265 | args = pieces[4:] | |
266 | ||
267 | adding = True | |
268 | for c in mode: | |
269 | if c == '+': | |
270 | adding = True | |
271 | elif c == '-': | |
272 | adding = False | |
273 | elif c == 'o': | |
274 | if adding: | |
275 | chan.userop(self.parent.user(args.pop(0))) | |
276 | else: | |
277 | chan.userdeop(self.parent.user(args.pop(0))) | |
278 | elif c == 'v': | |
279 | if adding: | |
280 | chan.uservoice(self.parent.user(args.pop(0))) | |
281 | else: | |
282 | chan.userdevoice(self.parent.user(args.pop(0))) | |
283 | else: | |
284 | pass # don't care about other modes | |
285 | ||
286 | def __debug_cbexception(self, source, *args, **kwargs): | |
287 | if self.parent.cfg.getboolean('debug', 'cbexc'): | |
288 | self.conn.send("PRIVMSG %s :%09.3f \ 34\1f!!! CBEXC\1f\ 3 %s" % (self.parent.cfg.get('debug', 'owner'), time.time() % 100000, source)) | |
289 | __import__('traceback').print_exc() | |
290 | self.log('!', "CBEXC %s %r %r" % (source, args, kwargs)) | |
291 | # print "%09.3f %s [!] CBEXC %s %r %r" % (time.time() % 100000, self.nick, source, args, kwargs) | |
292 | ||
293 | ||
294 | def parsemsg(self, user, target, msg): | |
295 | if user.glevel <= -2: return # short circuit if user is IGNORED | |
296 | chan = None | |
297 | chanparam = None # was the channel specified as part of the command? | |
298 | if len(msg) == 0: | |
299 | return | |
300 | ||
301 | if target == self.nick: | |
302 | if msg.startswith("\001"): #ctcp | |
303 | msg = msg.strip("\001") | |
304 | if msg == "VERSION": | |
305 | self.msg(user, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self.parent.APIVERSION, self.parent.RELEASE)) | |
306 | return | |
307 | ||
308 | triggerused = msg.startswith(self.parent.trigger) | |
309 | if triggerused: msg = msg[len(self.parent.trigger):] | |
310 | pieces = msg.split() | |
311 | ||
312 | if len(pieces) == 0: | |
313 | return | |
314 | ||
315 | if target != self.nick: # message was sent to a channel | |
316 | try: | |
317 | if pieces[0][:-1].lower() == self.nick.lower() and (pieces[0][-1] == ":" or pieces[0][-1] == ","): | |
318 | pieces.pop(0) # command actually starts with next word | |
319 | if len(pieces) == 0: # is there still anything left? | |
320 | return | |
321 | msg = ' '.join(pieces) | |
322 | triggerused = True | |
323 | except IndexError: | |
324 | return # "message" is empty | |
325 | ||
326 | if len(pieces) > 1: | |
327 | chanword = pieces[1] | |
328 | if chanword.startswith('#'): | |
329 | chanparam = self.parent.channel(chanword) | |
330 | ||
331 | if target != self.nick: # message was sent to a channel | |
332 | chan = self.parent.channel(target) | |
333 | if not triggerused: | |
334 | if self.parent.haschanhook(target.lower()): | |
335 | for callback in self.parent.getchanhook(target.lower()): | |
336 | try: | |
337 | cbret = callback(self, user, chan, *pieces) | |
338 | if isinstance(cbret, stringbase): | |
339 | self.reply(target, user, cbret) | |
340 | except: | |
341 | self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000)) | |
342 | self.__debug_cbexception("chanhook", user=user, target=target, msg=msg) | |
343 | return # not to bot, don't process! | |
344 | ||
345 | cmd = pieces[0].lower() | |
346 | rancmd = False | |
347 | if self.parent.hashook(cmd): | |
348 | for callback in self.parent.gethook(cmd): | |
349 | if chanparam is not None and (callback.needchan or callback.wantchan): | |
350 | chan = chanparam | |
351 | pieces.pop(1) | |
352 | if chan is None and callback.needchan: | |
353 | rancmd = True | |
354 | self.msg(user, "You need to specify a channel for that command.") | |
355 | elif user.glevel >= callback.reqglevel and (not callback.needchan or chan.levelof(user.auth) >= callback.reqclevel): | |
356 | rancmd = True | |
357 | try: | |
358 | cbret = callback(self, user, chan, target, *pieces[1:]) | |
359 | if isinstance(cbret, stringbase): | |
360 | self.reply(target, user, cbret) | |
361 | except Exception: | |
362 | self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000)) | |
363 | self.__debug_cbexception("hook", user=user, target=target, msg=msg) | |
364 | except SystemExit as e: | |
365 | try: | |
366 | curs = self.parent.query("UPDATE bots SET connected = 0") | |
367 | curs.close() | |
368 | except: pass | |
369 | raise e | |
370 | else: | |
371 | rancmd = True | |
372 | self.msg(user, "I don't know that command.") | |
373 | if not rancmd: | |
374 | self.msg(user, "You don't have enough access to run that command.") | |
375 | ||
376 | def __debug_nomsg(self, target, msg): | |
377 | if self.parent.cfg.getboolean('debug', 'nomsg'): | |
378 | self.conn.send("PRIVMSG %s :%09.3f \ 34\1f!!! NOMSG\1f\ 3 %r, %r" % (self.parent.cfg.get('debug', 'owner'), time.time() % 100000, target, msg)) | |
379 | self.log('!', "!!! NOMSG") | |
380 | # print "%09.3f %s [!] %s" % (time.time() % 100000, self.nick, "!!! NOMSG") | |
381 | __import__('traceback').print_stack() | |
382 | ||
383 | ||
384 | def reply(self, chan, user, msg): | |
385 | if chan is not None and (isinstance(chan, self.parent.Channel) or (isinstance(chan, stringbase) and chan[0] == "#")): | |
386 | self.msg(chan, "%s: %s" % (user, msg)) | |
387 | else: | |
388 | self.msg(user, msg) | |
389 | ||
390 | """ | |
391 | Does the work for msg/slowmsg/fastmsg. Uses the append_callback to append to the correct queue. | |
392 | ||
393 | In the case of fastmsg, self.conn.exceeded may be True, however, in this case append_callback=self.conn.send, so it will still be sent immediately. | |
394 | """ | |
395 | def _msg(self, target, msg, truncate, append_callback): | |
396 | if self.parent.cfg.getboolean('erebus', 'nofakelag'): return self.fastmsg(target, msg) | |
397 | ||
398 | cmd = self._formatmsg(target, msg) | |
399 | # The max length is much shorter than recvq (510) because of the length the server adds on about the source (us). | |
400 | # If you know your hostmask, you can of course figure the exact length, but it's very difficult to reliably know your hostmask. | |
401 | maxlen = ( | |
402 | self.conn.recvq | |
403 | - 63 # max hostname len | |
404 | - 11 # max ident len | |
405 | - 3 # the symbols in :nick!user@host | |
406 | - len(self.nick) | |
407 | ) | |
408 | if len(cmd) > maxlen: | |
409 | if not truncate: | |
410 | return False | |
411 | else: | |
412 | cmd = cmd[:maxlen] | |
413 | ||
414 | if self.conn.exceeded or self.conn.bytessent+len(cmd) >= self.conn.recvq: | |
415 | append_callback(cmd) | |
416 | else: | |
417 | self.conn.send(cmd) | |
418 | ||
419 | self.conn.exceeded = True | |
420 | return True | |
421 | ||
422 | def msg(self, target, msg, truncate=False): | |
423 | return self._msg(target, msg, truncate, self.msgqueue.append) | |
424 | ||
425 | def slowmsg(self, target, msg, truncate=False): | |
426 | return self._msg(target, msg, truncate, self.slowmsgqueue.append) | |
427 | ||
428 | def fastmsg(self, target, msg, truncate=False): | |
429 | return self._msg(target, msg, truncate, self.conn.send) | |
430 | ||
431 | def _formatmsg(self, target, msg): | |
432 | if target is None or msg is None: | |
433 | return self.__debug_nomsg(target, msg) | |
434 | ||
435 | target = str(target) | |
436 | ||
437 | if target.startswith('#'): command = "PRIVMSG %s :%s" % (target, msg) | |
438 | else: command = "NOTICE %s :%s" % (target, msg) | |
439 | ||
440 | return command | |
441 | ||
442 | def _popmsg(self): | |
443 | self._makemsgtimer() | |
444 | self.conn.bytessent -= self.conn.recvq/3 | |
445 | if self.conn.bytessent < 0: self.conn.bytessent = 0 | |
446 | self.conn.exceeded = True | |
447 | ||
448 | cmd = None | |
449 | try: | |
450 | cmd = self.msgqueue.popleft() | |
451 | except IndexError: | |
452 | try: | |
453 | cmd = self.slowmsgqueue.popleft() | |
454 | except IndexError: | |
455 | pass | |
456 | ||
457 | if cmd is not None: | |
458 | if self.conn.bytessent+len(cmd) > self.conn.recvq: # If it's too long | |
459 | if len(cmd) > self.conn.recvq: # Is the command itself somehow over max length??? | |
460 | self._msgtimer.start() | |
461 | raise ValueError('Somehow a command that was too long made it into the message queue. Uhoh!', cmd) | |
462 | # Discard the message. | |
463 | self.msgqueue.appendleft(cmd) # Phew, we've just sent too much recently. Put it (back) on the (primary) queue. | |
464 | else: | |
465 | self.conn.send(cmd) | |
466 | ||
467 | self._msgtimer.start() | |
468 | ||
469 | def _makemsgtimer(self): | |
470 | self._msgtimer = MyTimer(3, self._popmsg) | |
471 | ||
472 | def join(self, chan): | |
473 | self.conn.send("JOIN %s" % (chan)) | |
474 | ||
475 | def part(self, chan): | |
476 | self.conn.send("PART %s" % (chan)) | |
477 | ||
478 | def quit(self, reason="Shutdown"): | |
479 | self.conn.send("QUIT :%s" % (reason)) | |
480 | ||
481 | def __str__(self): return self.nick | |
482 | def __repr__(self): return "<Bot %r>" % (self.nick) | |
483 | ||
484 | class BotConnection(object): | |
485 | def __init__(self, parent, bind, server, port): | |
486 | self.parent = parent | |
487 | self.buffer = bytearray() | |
488 | self.socket = None | |
489 | ||
490 | self.bind = bind | |
491 | self.server = server | |
492 | self.port = int(port) | |
493 | ||
494 | self.state = 0 # 0=disconnected, 1=registering, 2=connected | |
495 | ||
496 | self.bytessent = 0 | |
497 | self.recvq = 510 | |
498 | self.exceeded = False | |
499 | self._nowrite = False | |
500 | ||
501 | def connect(self): | |
502 | if self.parent.parent.cfg.getboolean('erebus', 'tls'): | |
503 | import ssl | |
504 | undersocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
505 | context = ssl.create_default_context() | |
506 | self.socket = context.wrap_socket(undersocket, server_hostname=self.server) | |
507 | else: | |
508 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
509 | self.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) # Does Python make SOL_TCP portable? Who knows, it's not documented, and it appears to come from the _socket C lib. | |
510 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 0, 0)) | |
511 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) | |
512 | self.socket.bind((self.bind, 0)) | |
513 | self.socket.connect((self.server, self.port)) | |
514 | return True | |
515 | def register(self): | |
516 | if self.state == 0: | |
517 | self.send("NICK %s" % (self.parent.nick)) | |
518 | self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) | |
519 | self.state = 1 | |
520 | return True | |
521 | ||
522 | def registered(self, done=False): | |
523 | if done: self.state = 2 | |
524 | return self.state == 2 | |
525 | ||
526 | def send(self, line): | |
527 | if not self._nowrite: | |
528 | if self.parent.parent.cfg.getboolean('debug', 'io'): | |
529 | self.parent.log('O', line) | |
530 | self.bytessent += len(line) | |
531 | try: | |
532 | self._write(line) | |
533 | except socket.error as e: | |
534 | self._nowrite = True | |
535 | self.parent._goterror(repr(e)) | |
536 | else: | |
537 | if self.parent.parent.cfg.getboolean('debug', 'io'): | |
538 | self.parent.log('X', line) | |
539 | ||
540 | def _write(self, line): | |
541 | self.socket.sendall(line.encode('utf-8', 'backslashreplace')+b"\r\n") | |
542 | ||
543 | def read(self): | |
544 | self.buffer += self.socket.recv(8192) | |
545 | lines = [] | |
546 | ||
547 | while b"\r\n" in self.buffer: | |
548 | pieces = self.buffer.split(b"\r\n", 1) | |
549 | lines.append(pieces[0].decode('utf-8', 'backslashreplace')) | |
550 | self.buffer = pieces[1] | |
551 | ||
552 | return lines | |
553 | ||
554 | def __str__(self): return self.parent.nick | |
555 | def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick) |