2 # vim: fileencoding=utf-8
4 # Erebus IRC bot - Author: John Runyon
5 # "Bot" and "BotConnection" classes (handling a specific "arm")
7 import socket
, sys
, time
, threading
, os
, random
, struct
8 from collections
import deque
10 if sys
.version_info
.major
< 3:
11 timerbase
= threading
._Timer
12 stringbase
= basestring
14 timerbase
= threading
.Timer
17 class MyTimer(timerbase
):
18 def __init__(self
, *args
, **kwargs
):
19 timerbase
.__init
__(self
, *args
, **kwargs
)
22 if sys
.version_info
.major
< 3:
23 stringbase
= basestring
27 #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
29 def __init__(self
, parent
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
):
34 self
.realname
= realname
36 self
.authname
= authname
37 self
.authpass
= authpass
39 self
.connecttime
= 0 # time at which we received numeric 001
40 self
.server
= server
# the address we try to (re-)connect to
42 self
.servername
= server
# the name of the server we got connected to
44 curs
= self
.parent
.query("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self
.permnick
,))
46 chansres
= curs
.fetchall()
48 self
.chans
= [self
.parent
.newchannel(self
, row
['chname']) for row
in chansres
]
52 self
.conn
= BotConnection(self
, bind
, server
, port
)
54 self
.lastreceived
= time
.time() #time we last received a line from the server
57 self
.msgqueue
= deque()
58 self
.slowmsgqueue
= deque()
60 self
._msgtimer
.start()
64 curs
= self
.parent
.query("UPDATE bots SET connected = 0 WHERE nick = %s", (self
.permnick
,))
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
)
73 def log(self
, *args
, **kwargs
):
74 self
.parent
.log(self
.nick
, *args
, **kwargs
)
77 self
.log('!', "Connecting")
78 if self
.conn
.connect():
79 self
.parent
.newfd(self
, self
.conn
.socket
.fileno())
82 self
.lastreceived
= time
.time()
83 return self
.conn
.read()
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
))
89 def parse(self
, line
):
90 if self
.parent
.cfg
.getboolean('debug', 'io'):
95 zero
= { #things to look for without source
96 'NOTICE': self
._gotconnected
,
97 'PING': self
._gotping
,
98 'ERROR': self
._goterror
,
100 one
= { #things to look for after source
101 'NOTICE': self
._gotconnected
,
104 '376': self
._gotRegistered
,
105 '422': self
._gotRegistered
,
106 'PRIVMSG': self
._gotprivmsg
,
107 '353': self
._got
353, #NAMES
108 '354': self
._got
354, #WHO
109 '433': self
._got
433, #nick in use
110 'JOIN': self
._gotjoin
,
111 'PART': self
._gotpart
,
112 'KICK': self
._gotkick
,
113 'QUIT': self
._gotquit
,
114 'NICK': self
._gotnick
,
115 'MODE': self
._gotmode
,
118 if self
.parent
.hasnumhook(pieces
[1]):
119 hooks
= self
.parent
.getnumhook(pieces
[1])
120 for callback
in hooks
:
124 self
.__debug
_cbexception
("numhook", line
)
126 if pieces
[0] in zero
:
127 zero
[pieces
[0]](pieces
)
128 elif pieces
[1] in one
:
129 one
[pieces
[1]](pieces
)
131 def _gotconnected(self
, pieces
):
132 if not self
.conn
.registered():
134 def _gotping(self
, pieces
):
135 self
.conn
.send("PONG %s" % (pieces
[1]))
137 def _goterror(self
, pieces
):
138 # TODO: better handling, just reconnect that single bot
140 self
.quit("Error detected: %s" % ' '.join(pieces
))
143 curs
= self
.parent
.query("UPDATE bots SET connected = 0")
148 def _got001(self
, pieces
):
149 # We wait until the end of MOTD instead to consider ourselves registered, but consider uptime as of 001
150 self
.connecttime
= time
.time()
151 def _got004(self
, pieces
):
152 self
.servername
= pieces
[3]
153 def _gotRegistered(self
, pieces
):
154 self
.conn
.registered(True)
156 curs
= self
.parent
.query("UPDATE bots SET connected = 1 WHERE nick = %s", (self
.permnick
,))
157 if curs
: curs
.close()
159 self
.conn
.send("MODE %s +x" % (pieces
[2]))
160 if self
.authname
is not None and self
.authpass
is not None:
161 self
.conn
.send("AUTH %s %s" % (self
.authname
, self
.authpass
))
164 def _gotprivmsg(self
, pieces
):
165 nick
= pieces
[0].split('!')[0][1:]
166 user
= self
.parent
.user(nick
)
168 msg
= ' '.join(pieces
[3:])[1:]
169 self
.parsemsg(user
, target
, msg
)
170 def _got353(self
, pieces
):
171 prefixes
= {'@': 'op', '+': 'voice'}
172 chan
= self
.parent
.channel(pieces
[4])
174 names
[0] = names
[0][1:] #remove colon
177 user
= self
.parent
.user(n
[1:])
178 chan
.userjoin(user
, prefixes
[n
[0]])
180 user
= self
.parent
.user(n
)
183 def _got354(self
, pieces
):
186 nick
, auth
= pieces
[4:6]
189 chan
, nick
, auth
= pieces
[4:7]
190 chan
= self
.parent
.channel(chan
)
191 user
= self
.parent
.user(nick
)
198 if qt
== 2: # triggered by !auth
201 self
.msg(nick
, "You are now known as #%s (access level: %s)" % (auth
, user
.glevel
))
203 self
.msg(nick
, "You are now known as #%s (not staff)" % (auth
))
205 self
.msg(nick
, "I tried, but you're not authed!")
206 def _got433(self
, pieces
):
207 if not self
.conn
.registered(): #we're trying to connect
208 newnick
= "%s%d" % (self
.nick
, random
.randint(111, 999))
209 self
.conn
.send("NICK %s" % (newnick
))
211 def _gotjoin(self
, pieces
):
212 nick
= pieces
[0].split('!')[0][1:]
213 chan
= self
.parent
.channel(pieces
[2])
215 if nick
== self
.nick
:
216 self
.conn
.send("WHO %s c%%cant,3" % (chan
))
218 user
= self
.parent
.user(nick
, justjoined
=True)
221 def _clientLeft(self
, nick
, chan
):
222 if nick
!= self
.nick
:
223 gone
= self
.parent
.user(nick
).part(chan
)
224 chan
.userpart(self
.parent
.user(nick
))
226 self
.parent
.user(nick
).quit()
227 del self
.parent
.users
[nick
.lower()]
228 def _gotpart(self
, pieces
):
229 nick
= pieces
[0].split('!')[0][1:]
230 chan
= self
.parent
.channel(pieces
[2])
231 self
._clientLeft
(nick
, chan
)
232 def _gotkick(self
, pieces
):
234 chan
= self
.parent
.channel(pieces
[2])
235 self
._clientLeft
(nick
, chan
)
236 def _gotquit(self
, pieces
):
237 nick
= pieces
[0].split('!')[0][1:]
238 if nick
!= self
.nick
:
239 for chan
in self
.parent
.user(nick
).chans
:
240 chan
.userpart(self
.parent
.user(nick
))
241 self
.parent
.user(nick
).quit()
242 del self
.parent
.users
[nick
.lower()]
243 def _gotnick(self
, pieces
):
244 oldnick
= pieces
[0].split('!')[0][1:]
245 newnick
= pieces
[2][1:]
246 if oldnick
== self
.nick
:
249 if newnick
.lower() != oldnick
.lower():
250 self
.parent
.users
[newnick
.lower()] = self
.parent
.users
[oldnick
.lower()]
251 del self
.parent
.users
[oldnick
.lower()]
252 self
.parent
.users
[newnick
.lower()].nickchange(newnick
)
253 def _gotmode(self
, pieces
):
254 source
= pieces
[0].split('!')[0][1:]
256 if not chan
.startswith("#"): return
257 chan
= self
.parent
.channel(pieces
[2])
269 chan
.userop(self
.parent
.user(args
.pop(0)))
271 chan
.userdeop(self
.parent
.user(args
.pop(0)))
274 chan
.uservoice(self
.parent
.user(args
.pop(0)))
276 chan
.userdevoice(self
.parent
.user(args
.pop(0)))
278 pass # don't care about other modes
280 def __debug_cbexception(self
, source
, *args
, **kwargs
):
281 if self
.parent
.cfg
.getboolean('debug', 'cbexc'):
282 self
.conn
.send("PRIVMSG %s :%09.3f \ 34\1f!!! CBEXC\1f\ 3 %s" % (self
.parent
.cfg
.get('debug', 'owner'), time
.time() % 100000, source
))
283 __import__('traceback').print_exc()
284 self
.log('!', "CBEXC %s %r %r" % (source
, args
, kwargs
))
285 # print "%09.3f %s [!] CBEXC %s %r %r" % (time.time() % 100000, self.nick, source, args, kwargs)
288 def parsemsg(self
, user
, target
, msg
):
289 if user
.glevel
<= -2: return # short circuit if user is IGNORED
291 chanparam
= None # was the channel specified as part of the command?
295 if target
== self
.nick
:
296 if msg
.startswith("\001"): #ctcp
297 msg
= msg
.strip("\001")
299 self
.msg(user
, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self
.parent
.APIVERSION
, self
.parent
.RELEASE
))
302 triggerused
= msg
.startswith(self
.parent
.trigger
)
303 if triggerused
: msg
= msg
[len(self
.parent
.trigger
):]
309 if target
!= self
.nick
: # message was sent to a channel
311 if pieces
[0][:-1].lower() == self
.nick
.lower() and (pieces
[0][-1] == ":" or pieces
[0][-1] == ","):
312 pieces
.pop(0) # command actually starts with next word
313 if len(pieces
) == 0: # is there still anything left?
315 msg
= ' '.join(pieces
)
318 return # "message" is empty
322 if chanword
.startswith('#'):
323 chanparam
= self
.parent
.channel(chanword
)
325 if target
!= self
.nick
: # message was sent to a channel
326 chan
= self
.parent
.channel(target
)
328 if self
.parent
.haschanhook(target
.lower()):
329 for callback
in self
.parent
.getchanhook(target
.lower()):
331 cbret
= callback(self
, user
, chan
, *pieces
)
332 if isinstance(cbret
, stringbase
):
333 self
.reply(target
, user
, cbret
)
335 self
.msg(user
, "Command failed. Code: CBEXC%09.3f" % (time
.time() % 100000))
336 self
.__debug
_cbexception
("chanhook", user
=user
, target
=target
, msg
=msg
)
337 return # not to bot, don't process!
339 cmd
= pieces
[0].lower()
341 if self
.parent
.hashook(cmd
):
342 for callback
in self
.parent
.gethook(cmd
):
343 if chanparam
is not None and (callback
.needchan
or callback
.wantchan
):
346 if chan
is None and callback
.needchan
:
348 self
.msg(user
, "You need to specify a channel for that command.")
349 elif user
.glevel
>= callback
.reqglevel
and (not callback
.needchan
or chan
.levelof(user
.auth
) >= callback
.reqclevel
):
352 cbret
= callback(self
, user
, chan
, target
, *pieces
[1:])
353 if isinstance(cbret
, stringbase
):
354 self
.reply(target
, user
, cbret
)
356 self
.msg(user
, "Command failed. Code: CBEXC%09.3f" % (time
.time() % 100000))
357 self
.__debug
_cbexception
("hook", user
=user
, target
=target
, msg
=msg
)
358 except SystemExit as e
:
360 curs
= self
.parent
.query("UPDATE bots SET connected = 0")
366 self
.msg(user
, "I don't know that command.")
368 self
.msg(user
, "You don't have enough access to run that command.")
370 def __debug_nomsg(self
, target
, msg
):
371 if self
.parent
.cfg
.getboolean('debug', 'nomsg'):
372 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
))
373 self
.log('!', "!!! NOMSG")
374 # print "%09.3f %s [!] %s" % (time.time() % 100000, self.nick, "!!! NOMSG")
375 __import__('traceback').print_stack()
378 def reply(self
, chan
, user
, msg
):
379 if chan
is not None and (isinstance(chan
, self
.parent
.Channel
) or (isinstance(chan
, stringbase
) and chan
[0] == "#")):
380 self
.msg(chan
, "%s: %s" % (user
, msg
))
385 Does the work for msg/slowmsg/fastmsg. Uses the append_callback to append to the correct queue.
387 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.
389 def _msg(self
, target
, msg
, truncate
, append_callback
):
390 if self
.parent
.cfg
.getboolean('erebus', 'nofakelag'): return self
.fastmsg(target
, msg
)
392 cmd
= self
._formatmsg
(target
, msg
)
393 # The max length is much shorter than recvq (510) because of the length the server adds on about the source (us).
394 # If you know your hostmask, you can of course figure the exact length, but it's very difficult to reliably know your hostmask.
397 - 63 # max hostname len
399 - 3 # the symbols in :nick!user@host
402 if len(cmd
) > maxlen
:
408 if self
.conn
.exceeded
or self
.conn
.bytessent
+len(cmd
) >= self
.conn
.recvq
:
413 self
.conn
.exceeded
= True
416 def msg(self
, target
, msg
, truncate
=False):
417 return self
._msg
(target
, msg
, truncate
, self
.msgqueue
.append
)
419 def slowmsg(self
, target
, msg
, truncate
=False):
420 return self
._msg
(target
, msg
, truncate
, self
.slowmsgqueue
.append
)
422 def fastmsg(self
, target
, msg
, truncate
=False):
423 return self
._msg
(target
, msg
, truncate
, self
.conn
.send
)
425 def _formatmsg(self
, target
, msg
):
426 if target
is None or msg
is None:
427 return self
.__debug
_nomsg
(target
, msg
)
431 if target
.startswith('#'): command
= "PRIVMSG %s :%s" % (target
, msg
)
432 else: command
= "NOTICE %s :%s" % (target
, msg
)
438 self
.conn
.bytessent
-= self
.conn
.recvq
/3
439 if self
.conn
.bytessent
< 0: self
.conn
.bytessent
= 0
440 self
.conn
.exceeded
= True
444 cmd
= self
.msgqueue
.popleft()
447 cmd
= self
.slowmsgqueue
.popleft()
452 if self
.conn
.bytessent
+len(cmd
) > self
.conn
.recvq
: # If it's too long
453 if len(cmd
) > self
.conn
.recvq
: # Is the command itself somehow over max length???
454 self
._msgtimer
.start()
455 raise ValueError('Somehow a command that was too long made it into the message queue. Uhoh!', cmd
)
456 # Discard the message.
457 self
.msgqueue
.appendleft(cmd
) # Phew, we've just sent too much recently. Put it (back) on the (primary) queue.
461 self
._msgtimer
.start()
463 def _makemsgtimer(self
):
464 self
._msgtimer
= MyTimer(3, self
._popmsg
)
466 def join(self
, chan
):
467 self
.conn
.send("JOIN %s" % (chan
))
469 def part(self
, chan
):
470 self
.conn
.send("PART %s" % (chan
))
472 def quit(self
, reason
="Shutdown"):
473 self
.conn
.send("QUIT :%s" % (reason
))
475 def __str__(self
): return self
.nick
476 def __repr__(self
): return "<Bot %r>" % (self
.nick
)
478 class BotConnection(object):
479 def __init__(self
, parent
, bind
, server
, port
):
481 self
.buffer = bytearray()
486 self
.port
= int(port
)
488 self
.state
= 0 # 0=disconnected, 1=registering, 2=connected
492 self
.exceeded
= False
493 self
._nowrite
= False
496 self
.socket
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
497 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.
498 self
.socket
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_LINGER
, struct
.pack('ii', 0, 0))
499 self
.socket
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_KEEPALIVE
, 1)
500 self
.socket
.bind((self
.bind
, 0))
501 self
.socket
.connect((self
.server
, self
.port
))
505 self
.send("NICK %s" % (self
.parent
.nick
))
506 self
.send("USER %s 0 * :%s" % (self
.parent
.user
, self
.parent
.realname
))
510 def registered(self
, done
=False):
511 if done
: self
.state
= 2
512 return self
.state
== 2
514 def send(self
, line
):
515 if not self
._nowrite
:
516 if self
.parent
.parent
.cfg
.getboolean('debug', 'io'):
517 self
.parent
.log('O', line
)
518 self
.bytessent
+= len(line
)
521 except socket
.error
as e
:
523 self
.parent
._goterror
(repr(e
))
525 if self
.parent
.parent
.cfg
.getboolean('debug', 'io'):
526 self
.parent
.log('X', line
)
528 def _write(self
, line
):
529 self
.socket
.sendall(line
.encode('utf-8', 'backslashreplace')+b
"\r\n")
532 self
.buffer += self
.socket
.recv(8192)
535 while b
"\r\n" in self
.buffer:
536 pieces
= self
.buffer.split(b
"\r\n", 1)
537 lines
.append(pieces
[0].decode('utf-8', 'backslashreplace'))
538 self
.buffer = pieces
[1]
542 def __str__(self
): return self
.parent
.nick
543 def __repr__(self
): return "<BotConnection %r (%r)>" % (self
.socket
.fileno(), self
.parent
.nick
)