# Erebus IRC bot - Author: John Runyon
# "Bot" and "BotConnection" classes (handling a specific "arm")
-import socket, sys, time, threading, os, random, struct
+import os, random, socket, struct, sys, threading, time, traceback
from collections import deque
if sys.version_info.major < 3:
#bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
class Bot(object):
def __init__(self, parent, nick, user, bind, authname, authpass, server, port, realname):
+ self.maxlen = 510
+
self.parent = parent
self.nick = nick
self.permnick = nick
try:
callback(self, line)
except Exception:
- self.__debug_cbexception("numhook", line)
+ self._cbexception("numhook", line)
if numeric in dispatch:
dispatch[numeric](pieces)
else:
pass # don't care about other modes
- def __debug_cbexception(self, source, *args, **kwargs):
+ def _cbexception(self, source, *args, chained=False, **kwargs):
+ if not chained: # skip hooks if we were caused by a hook
+ exc = sys.exception()
+ if self.parent.hasexceptionhook(exc):
+ for callback in self.parent.getexceptionhook(exc):
+ try:
+ callback(self, exc, source, *args, **kwargs)
+ except Exception:
+ self._cbexception('exceptionhook', chained=True, module=callback.__module__, function=callback.__name__, underlying=(source, args, kwargs))
if self.parent.cfg.getboolean('debug', 'cbexc'):
self.conn.send("PRIVMSG %s :%09.3f \ 34\1f!!! CBEXC\1f\ 3 %s" % (self.parent.cfg.get('debug', 'owner'), time.time() % 100000, source))
- __import__('traceback').print_exc()
+ traceback.print_exc(chain=not chained)
self.log('!', "CBEXC %s %r %r" % (source, args, kwargs))
-# print "%09.3f %s [!] CBEXC %s %r %r" % (time.time() % 100000, self.nick, source, args, kwargs)
def parsemsg(self, user, target, msg):
self.reply(target, user, cbret)
except:
self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000))
- self.__debug_cbexception("chanhook", user=user, target=target, msg=msg)
+ self._cbexception("chanhook", user=user, target=target, msg=msg)
return # not to bot, don't process!
cmd = pieces[0].lower()
self.reply(target, user, cbret)
except Exception:
self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000))
- self.__debug_cbexception("hook", user=user, target=target, msg=msg)
+ self._cbexception("hook", user=user, target=target, msg=msg)
except SystemExit as e:
self.parent.mustquit = e
try:
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))
self.log('!', "!!! NOMSG")
# print "%09.3f %s [!] %s" % (time.time() % 100000, self.nick, "!!! NOMSG")
- __import__('traceback').print_stack()
+ traceback.print_stack()
def reply(self, chan, user, msg):
if self.parent.cfg.getboolean('erebus', 'nofakelag'): append_callback = self.conn.send
cmd = self._formatmsg(target, msg, msgtype)
- # The max length is much shorter than recvq (510) because of the length the server adds on about the source (us).
+ # The max length is much shorter than conn.maxlen (510) because of the length the server adds on about the source (us).
# If you know your hostmask, you can of course figure the exact length, but it's very difficult to reliably know your hostmask.
- maxlen = (
- self.conn.recvq
- - 63 # max hostname len
- - 11 # max ident len
- - 3 # the symbols in :nick!user@host
- - len(self.nick)
- )
+ maxlen = self.maxmsglen()
if len(cmd) > maxlen:
if not truncate:
return False
def quit(self, reason="Shutdown"):
self.conn.send("QUIT :%s" % (reason))
+ def maxmsglen(self):
+ return (
+ self.maxlen
+ - 63 # max hostname len
+ - 11 # max ident len
+ - 3 # the symbols in :nick!user@host
+ - len(self.nick)
+ )
+
def __str__(self): return self.nick
def __repr__(self): return "<Bot %r>" % (self.nick)
self.state = 0 # 0=disconnected, 1=registering, 2=connected
self.bytessent = 0
- self.recvq = 510
+ self.recvq = 510 # How much we can send per period
self.exceeded = False
self._nowrite = False