2 # vim: fileencoding=utf-8
4 # Erebus IRC bot - Author: John Runyon
5 # "Bot" and "BotConnection" classes (handling a specific "arm")
7 import os
, random
, socket
, struct
, sys
, threading
, time
, traceback
, fcntl
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
):
36 self
.realname
= realname
38 self
.authname
= authname
39 self
.authpass
= authpass
41 self
.connecttime
= 0 # time at which we received numeric 001
42 self
.server
= server
# the address we try to (re-)connect to
44 self
.servername
= server
# the name of the server we got connected to
46 curs
= self
.parent
.query("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self
.permnick
,))
48 chansres
= curs
.fetchall()
50 self
.chans
= [self
.parent
.newchannel(self
, row
['chname']) for row
in chansres
]
54 self
.conn
= BotConnection(self
, bind
, server
, port
)
56 self
.lastreceived
= time
.time() #time we last received a line from the server
59 self
.msgqueue
= deque()
60 self
.slowmsgqueue
= deque()
62 self
._msgtimer
.start()
63 self
.joined_chans
= False
67 curs
= self
.parent
.query("UPDATE bots SET connected = 0 WHERE nick = %s", (self
.permnick
,))
72 if time
.time() > int(self
.parent
.cfg
.get('watchdog', 'maxtime', default
=300))+self
.lastreceived
:
73 self
.parse("ERROR :Fake-error from watchdog timer.")
75 if self
.conn
.registered():
76 self
.conn
.send("PING :%s" % (time
.time()))
78 watchdogtimer
= MyTimer(int(self
.parent
.cfg
.get('watchdog', 'interval', default
=30)), self
.watchdog
)
81 def log(self
, *args
, **kwargs
):
82 self
.parent
.log(self
.nick
, *args
, **kwargs
)
85 self
.log('!', "Connecting")
86 if self
.conn
.connect():
87 self
.log('!', "Connected")
88 self
.parent
.newfd(self
, self
.conn
.socket
.fileno())
92 recvd
= self
.conn
.read()
93 self
.lastreceived
= time
.time()
96 return [":%s ERROR :%s%r" % (self
.nick
, e
.__class
__.__name
__, e
.args
)]
98 def _checknick(self
): # check if we're using the right nick, try changing
99 if self
.nick
!= self
.permnick
and self
.conn
.registered():
100 self
.conn
.send("NICK %s" % (self
.permnick
))
102 def parse(self
, line
):
103 pieces
= line
.split()
105 if pieces
[0][0] == ":":
112 'NOTICE': self
._gotconnected
,
115 '376': self
._gotRegistered
,
116 '422': self
._gotRegistered
,
117 'PRIVMSG': self
._gotprivmsg
,
118 '353': self
._got
353, #NAMES
119 '354': self
._got
354, #WHO
120 '396': self
._gotHiddenHost
, # hidden host has been set
121 '433': self
._got
433, #nick in use
122 '437': self
._got
433, #nick protected
123 'JOIN': self
._gotjoin
,
124 'PART': self
._gotpart
,
125 'KICK': self
._gotkick
,
126 'QUIT': self
._gotquit
,
127 'NICK': self
._gotnick
,
128 'MODE': self
._gotmode
,
129 'PING': self
._gotping
,
130 'ERROR': self
._goterror
,
133 if self
.parent
.hasnumhook(numeric
):
134 hooks
= self
.parent
.getnumhook(numeric
)
135 for callback
in hooks
:
139 self
._cbexception
("numhook", line
)
141 if numeric
in dispatch
:
142 dispatch
[numeric
](pieces
)
144 def _gotconnected(self
, pieces
):
145 if not self
.conn
.registered():
147 def _gotping(self
, pieces
):
148 self
.conn
.send("PONG %s" % (pieces
[1]))
150 def _goterror(self
, pieces
):
151 # TODO: better handling, just reconnect that single bot
152 error
= ' '.join(pieces
)
154 raise Exception(error
)
155 except Exception as e
:
156 self
.parent
.mustquit
= e
158 self
.quit("Error detected: %s" % (error
))
161 curs
= self
.parent
.query("UPDATE bots SET connected = 0")
164 self
.log('!', 'Bot exiting due to: %s' % (error
))
165 def _got001(self
, pieces
):
166 # We wait until the end of MOTD instead to consider ourselves registered, but consider uptime as of 001
167 self
.connecttime
= time
.time()
168 def _got004(self
, pieces
):
169 self
.servername
= pieces
[3]
170 def _gotRegistered(self
, pieces
):
171 self
.conn
.registered(True)
173 curs
= self
.parent
.query("UPDATE bots SET connected = 1 WHERE nick = %s", (self
.permnick
,))
174 if curs
: curs
.close()
176 self
.conn
.send("MODE %s +x" % (pieces
[2]))
177 if self
.authname
is not None and self
.authpass
is not None:
178 self
.conn
.send(self
.parent
.cfg
.get('erebus', 'auth_command', "AUTH %s %s") % (self
.authname
, self
.authpass
))
179 if not self
.parent
.cfg
.getboolean('erebus', 'wait_for_hidden_host'):
182 self
.joined_chans
= True
183 def _gotHiddenHost(self
, pieces
):
184 if not self
.joined_chans
and self
.parent
.cfg
.getboolean('erebus', 'wait_for_hidden_host'):
187 self
.joined_chans
= True
188 def _gotprivmsg(self
, pieces
):
189 nick
= pieces
[0].split('!')[0][1:]
190 user
= self
.parent
.user(nick
)
192 msg
= ' '.join(pieces
[3:])[1:]
193 self
.parsemsg(user
, target
, msg
)
194 def _got353(self
, pieces
):
195 prefixes
= {'@': 'op', '+': 'voice'}
196 chan
= self
.parent
.channel(pieces
[4])
198 names
[0] = names
[0][1:] #remove colon
201 user
= self
.parent
.user(n
[1:])
202 chan
.userjoin(user
, prefixes
[n
[0]])
204 user
= self
.parent
.user(n
)
207 def _got354(self
, pieces
):
210 nick
, auth
= pieces
[4:6]
213 chan
, nick
, auth
= pieces
[4:7]
214 chan
= self
.parent
.channel(chan
)
215 user
= self
.parent
.user(nick
)
222 if qt
== 2: # triggered by !auth
225 self
.msg(nick
, "You are now known as #%s (access level: %s)" % (auth
, user
.glevel
))
227 self
.msg(nick
, "You are now known as #%s (not staff)" % (auth
))
229 self
.msg(nick
, "I tried, but you're not authed!")
230 def _got433(self
, pieces
):
231 if not self
.conn
.registered(): #we're trying to connect
232 newnick
= "%s%d" % (self
.nick
, random
.randint(111, 999))
233 self
.conn
.send("NICK %s" % (newnick
))
235 def _gotjoin(self
, pieces
):
236 nick
= pieces
[0].split('!')[0][1:]
237 chan
= self
.parent
.channel(pieces
[2])
239 if nick
== self
.nick
:
240 self
.conn
.send("WHO %s c%%cant,3" % (chan
))
242 user
= self
.parent
.user(nick
, send_who
=True)
245 def _clientLeft(self
, nick
, chan
):
246 if nick
== self
.nick
:
248 if u
.nick
!= self
.nick
:
249 self
._clientLeft
(u
.nick
, chan
)
251 chan
.bot
.chans
.remove(chan
)
252 del self
.parent
.chans
[chan
.name
.lower()]
255 user
= self
.parent
.user(nick
)
256 gone
= user
.part(chan
)
260 del self
.parent
.users
[nick
.lower()]
261 def _gotpart(self
, pieces
):
262 nick
= pieces
[0].split('!')[0][1:]
263 chan
= self
.parent
.channel(pieces
[2])
264 self
._clientLeft
(nick
, chan
)
265 def _gotkick(self
, pieces
):
267 chan
= self
.parent
.channel(pieces
[2])
268 self
._clientLeft
(nick
, chan
)
269 def _gotquit(self
, pieces
):
270 nick
= pieces
[0].split('!')[0][1:]
271 if nick
!= self
.nick
:
272 for chan
in self
.parent
.user(nick
).chans
:
273 chan
.userpart(self
.parent
.user(nick
))
274 self
.parent
.user(nick
).quit()
275 del self
.parent
.users
[nick
.lower()]
276 def _gotnick(self
, pieces
):
277 oldnick
= pieces
[0].split('!')[0][1:]
278 newnick
= pieces
[2][1:]
279 if oldnick
== self
.nick
:
282 if newnick
.lower() != oldnick
.lower():
283 self
.parent
.users
[newnick
.lower()] = self
.parent
.users
[oldnick
.lower()]
284 del self
.parent
.users
[oldnick
.lower()]
285 self
.parent
.users
[newnick
.lower()].nickchange(newnick
)
286 def _gotmode(self
, pieces
):
287 source
= pieces
[0].split('!')[0][1:]
289 if not chan
.startswith("#"): return
290 chan
= self
.parent
.channel(pieces
[2])
302 chan
.userop(self
.parent
.user(args
.pop(0)))
304 chan
.userdeop(self
.parent
.user(args
.pop(0)))
307 chan
.uservoice(self
.parent
.user(args
.pop(0)))
309 chan
.userdevoice(self
.parent
.user(args
.pop(0)))
311 pass # don't care about other modes
313 def _cbexception(self
, source
, *args
, chained
=False, **kwargs
):
314 if not chained
: # skip hooks if we were caused by a hook
315 exc
= sys
.exception()
316 if self
.parent
.hasexceptionhook(exc
):
317 for callback
in self
.parent
.getexceptionhook(exc
):
319 callback(self
, exc
, source
, *args
, **kwargs
)
321 self
._cbexception
('exceptionhook', chained
=True, module
=callback
.__module
__, function
=callback
.__name
__, underlying
=(source
, args
, kwargs
))
322 if self
.parent
.cfg
.getboolean('debug', 'cbexc'):
323 self
.conn
.send("PRIVMSG %s :%09.3f \ 34\1f!!! CBEXC\1f\ 3 %s" % (self
.parent
.cfg
.get('debug', 'owner'), time
.time() % 100000, source
))
324 traceback
.print_exc(chain
=not chained
)
325 self
.log('!', "CBEXC %s %r %r" % (source
, args
, kwargs
))
328 def parsemsg(self
, user
, target
, msg
):
329 if user
.glevel
<= -2: return # short circuit if user is IGNORED
331 chanparam
= None # was the channel specified as part of the command?
335 if target
== self
.nick
and msg
.startswith("\001"): #ctcp
336 msg
= msg
.strip("\001")
339 if pieces
[0] == "CLIENTINFO":
340 self
.msg(user
, "\001CLIENTINFO VERSION PING\001")
341 elif pieces
[0] == "VERSION":
342 self
.msg(user
, "\001VERSION Erebus v%d.%d - http://jfr.im/git/erebus.git\001" % (self
.parent
.APIVERSION
, self
.parent
.RELEASE
))
343 elif pieces
[0] == "PING":
345 self
.msg(user
, "\001PING %s\001" % (' '.join(pieces
[1:])))
347 self
.msg(user
, "\001PING\001")
350 triggerused
= msg
.startswith(self
.parent
.trigger
)
351 if triggerused
: msg
= msg
[len(self
.parent
.trigger
):]
357 if target
!= self
.nick
: # message was sent to a channel
359 if pieces
[0][:-1].lower() == self
.nick
.lower() and (pieces
[0][-1] == ":" or pieces
[0][-1] == ","):
360 pieces
.pop(0) # command actually starts with next word
361 if len(pieces
) == 0: # is there still anything left?
363 msg
= ' '.join(pieces
)
366 return # "message" is empty
370 if chanword
.startswith('#'):
371 chanparam
= self
.parent
.channel(chanword
)
373 if target
!= self
.nick
: # message was sent to a channel
374 chan
= self
.parent
.channel(target
)
376 if self
.parent
.haschanhook(target
.lower()):
377 for callback
in self
.parent
.getchanhook(target
.lower()):
379 cbret
= callback(self
, user
, chan
, *pieces
)
380 if isinstance(cbret
, stringbase
):
381 self
.reply(target
, user
, cbret
)
383 self
.msg(user
, "Command failed. Code: CBEXC%09.3f" % (time
.time() % 100000))
384 self
._cbexception
("chanhook", user
=user
, target
=target
, msg
=msg
)
385 return # not to bot, don't process!
387 cmd
= pieces
[0].lower()
389 if self
.parent
.hashook(cmd
):
390 for callback
in self
.parent
.gethook(cmd
):
391 if chanparam
is not None and (callback
.needchan
or callback
.wantchan
):
394 if chan
is None and callback
.needchan
:
396 self
.msg(user
, "You need to specify a channel for that command.")
397 elif user
.glevel
>= callback
.reqglevel
and (not callback
.needchan
or chan
.levelof(user
.auth
) >= callback
.reqclevel
):
400 cbret
= callback(self
, user
, chan
, target
, *pieces
[1:])
401 if isinstance(cbret
, stringbase
):
402 self
.reply(target
, user
, cbret
)
404 self
.msg(user
, "Command failed. Code: CBEXC%09.3f" % (time
.time() % 100000))
405 self
._cbexception
("hook", user
=user
, target
=target
, msg
=msg
)
406 except SystemExit as e
:
407 self
.parent
.mustquit
= e
409 curs
= self
.parent
.query("UPDATE bots SET connected = 0")
415 self
.msg(user
, "I don't know that command.")
417 self
.msg(user
, "You don't have enough access to run that command.")
419 def __debug_nomsg(self
, target
, msg
):
420 if self
.parent
.cfg
.getboolean('debug', 'nomsg'):
421 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
))
422 self
.log('!', "!!! NOMSG")
423 # print "%09.3f %s [!] %s" % (time.time() % 100000, self.nick, "!!! NOMSG")
424 traceback
.print_stack()
427 def reply(self
, chan
, user
, msg
):
428 if chan
is not None and (isinstance(chan
, self
.parent
.Channel
) or (isinstance(chan
, stringbase
) and chan
[0] == "#")):
429 self
.msg(chan
, "%s: %s" % (user
, msg
))
434 Does the work for msg/slowmsg/fastmsg. Uses the append_callback to append to the correct queue.
436 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.
438 def _msg(self
, target
, msg
, truncate
, append_callback
, msgtype
):
439 if self
.parent
.cfg
.getboolean('erebus', 'nofakelag'): append_callback
= self
.conn
.send
441 cmd
= self
._formatmsg
(target
, msg
, msgtype
)
442 # The max length is much shorter than conn.maxlen (510) because of the length the server adds on about the source (us).
443 # If you know your hostmask, you can of course figure the exact length, but it's very difficult to reliably know your hostmask.
444 maxlen
= self
.maxmsglen()
445 if len(cmd
) > maxlen
:
451 if self
.conn
.exceeded
or self
.conn
.bytessent
+len(cmd
) >= self
.conn
.recvq
:
456 self
.conn
.exceeded
= True
459 def msg(self
, target
, msg
, truncate
=False, *, msgtype
=None):
460 """msgtype must be a valid IRC command, i.e. NOTICE or PRIVMSG; or leave as None to use default"""
461 return self
._msg
(target
, msg
, truncate
, self
.msgqueue
.append
, msgtype
)
463 def slowmsg(self
, target
, msg
, truncate
=False, *, msgtype
=None):
464 return self
._msg
(target
, msg
, truncate
, self
.slowmsgqueue
.append
, msgtype
)
466 def fastmsg(self
, target
, msg
, truncate
=False, *, msgtype
=None):
467 return self
._msg
(target
, msg
, truncate
, self
.conn
.send
, msgtype
)
469 def _formatmsg(self
, target
, msg
, msgtype
):
470 if target
is None or msg
is None:
471 return self
.__debug
_nomsg
(target
, msg
)
475 if msgtype
is not None: command
= "%s %s :%s" % (msgtype
, target
, msg
)
476 elif target
.startswith('#'): command
= "PRIVMSG %s :%s" % (target
, msg
)
477 else: command
= "NOTICE %s :%s" % (target
, msg
)
483 self
.conn
.bytessent
-= self
.conn
.recvq
/3
484 if self
.conn
.bytessent
< 0: self
.conn
.bytessent
= 0
485 self
.conn
.exceeded
= True
489 cmd
= self
.msgqueue
.popleft()
492 cmd
= self
.slowmsgqueue
.popleft()
497 if self
.conn
.bytessent
+len(cmd
) > self
.conn
.recvq
: # If it's too long
498 if len(cmd
) > self
.conn
.recvq
: # Is the command itself somehow over max length???
499 self
._msgtimer
.start()
500 raise ValueError('Somehow a command that was too long made it into the message queue. Uhoh!', cmd
)
501 # Discard the message.
502 self
.msgqueue
.appendleft(cmd
) # Phew, we've just sent too much recently. Put it (back) on the (primary) queue.
506 self
._msgtimer
.start()
508 def _makemsgtimer(self
):
509 self
._msgtimer
= MyTimer(3, self
._popmsg
)
511 def join(self
, chan
):
512 self
.conn
.send("JOIN %s" % (chan
))
514 def part(self
, chan
):
515 self
.conn
.send("PART %s" % (chan
))
517 def quit(self
, reason
="Shutdown"):
518 self
.conn
.send("QUIT :%s" % (reason
))
523 - 63 # max hostname len
525 - 3 # the symbols in :nick!user@host
529 def __str__(self
): return self
.nick
530 def __repr__(self
): return "<Bot %r>" % (self
.nick
)
532 class BotConnection(object):
533 def __init__(self
, parent
, bind
, server
, port
):
535 self
.buffer = bytearray()
540 self
.port
= int(port
)
542 self
.state
= 0 # 0=disconnected, 1=registering, 2=connected
545 self
.recvq
= 510 # How much we can send per period
546 self
.exceeded
= False
547 self
._nowrite
= False
550 if self
.parent
.parent
.cfg
.getboolean('erebus', 'tls'):
552 undersocket
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
553 context
= ssl
.create_default_context()
554 self
.socket
= context
.wrap_socket(undersocket
, server_hostname
=self
.server
)
556 self
.socket
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
557 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.
558 self
.socket
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_LINGER
, struct
.pack('ii', 0, 0))
559 self
.socket
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_KEEPALIVE
, 1)
560 self
.socket
.bind((self
.bind
, 0))
561 self
._write
_oidentd
()
562 self
.socket
.connect((self
.server
, self
.port
))
566 pss
= self
.parent
.parent
.cfg
.get('erebus', 'pass')
568 self
.send("PASS %s" % (pss
))
569 self
.send("NICK %s" % (self
.parent
.nick
))
570 self
.send("USER %s 0 * :%s" % (self
.parent
.user
, self
.parent
.realname
))
574 def registered(self
, done
=False):
577 self
._unwrite
_oidentd
()
578 return self
.state
== 2
580 def send(self
, line
):
581 if not self
._nowrite
:
582 if self
.parent
.parent
.cfg
.getboolean('debug', 'io'):
583 self
.parent
.log('O', line
)
584 self
.bytessent
+= len(line
)
587 except socket
.error
as e
:
589 self
.parent
._goterror
(repr(e
))
591 if self
.parent
.parent
.cfg
.getboolean('debug', 'io'):
592 self
.parent
.log('X', line
)
594 def _write(self
, line
):
595 self
.socket
.sendall(line
.encode('utf-8', 'surrogateescape')+b
"\r\n")
597 def _getsockerr(self
):
598 try: # SO_ERROR might not exist on all platforms
599 return self
.socket
.getsockopt(socket
.SOL_SOCKET
, socket
.SO_ERROR
)
604 recvd
= self
.socket
.recv(8192)
606 raise EOFError("socket.recv returned empty", self
.parent
.nick
, self
._getsockerr
())
610 while b
"\r\n" in self
.buffer:
611 pieces
= self
.buffer.split(b
"\r\n", 1)
612 lines
.append(pieces
[0].decode('utf-8', 'backslashreplace'))
613 self
.buffer = pieces
[1]
617 def _format_oidentd(self
):
618 ident
= self
.parent
.user
619 fport
= self
.parent
.port
621 lport
= self
.socket
.getsockname()[1]
623 return 'fport %s from %s lport %s { reply "%s" }\n' % (fport
, from_
, lport
, ident
)
625 return 'fport %s lport %s { reply "%s" }\n' % (fport
, lport
, ident
)
626 def _write_oidentd(self
):
627 path
= self
.parent
.parent
.cfg
.get('erebus', 'oidentd_path')
629 with open(path
, 'a') as fh
:
630 fcntl
.lockf(fh
, fcntl
.LOCK_EX
)
631 fh
.write(self
._format
_oidentd
())
632 fcntl
.lockf(fh
, fcntl
.LOCK_UN
)
633 def _unwrite_oidentd(self
):
634 path
= self
.parent
.parent
.cfg
.get('erebus', 'oidentd_path')
636 with open(path
, 'r+') as fh
:
637 fcntl
.lockf(fh
, fcntl
.LOCK_EX
)
639 newdata
= data
.replace(self
._format
_oidentd
(), '')
643 fcntl
.lockf(fh
, fcntl
.LOCK_UN
)
645 def __str__(self
): return self
.parent
.nick
646 def __repr__(self
): return "<BotConnection %r (%r)>" % (self
.socket
.fileno(), self
.parent
.nick
)