X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/6de27fd45ab8ed23503aab4249fac2e2638e7c3e..2b2ae2811cec8832a8d9c73e683e183206be283c:/bot.py diff --git a/bot.py b/bot.py index 2feb253..11b7ac5 100644 --- a/bot.py +++ b/bot.py @@ -6,6 +6,12 @@ import socket, sys, time, threading, os, random from collections import deque +class MyTimer(threading._Timer): + def __init__(self, *args, **kwargs): + threading._Timer.__init__(self, *args, **kwargs) + self.daemon = True + + #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): @@ -28,6 +34,9 @@ class Bot(object): self.conn = BotConnection(self, bind, server, port) + self.lastreceived = time.time() #time we last received a line from the server + self.watchdog() + self.msgqueue = deque() self.slowmsgqueue = deque() self.makemsgtimer() @@ -38,6 +47,10 @@ class Bot(object): curs.execute("UPDATE bots SET connected = 0 WHERE nick = %s", (self.nick,)) curs.close() + def watchdog(self): + if time.time() > self.parent.cfg.get('watchdog', 'maxtime', default=300)+self.lastreceived: + self.parse("ERROR :Fake-error from watchdog timer.") + self.watchdogtimer = MyTimer(self.parent.cfg.get('watchdog', 'interval', default=30), self.watchdog) def log(self, *args, **kwargs): self.parent.log(self.nick, *args, **kwargs) @@ -47,6 +60,7 @@ class Bot(object): self.parent.newfd(self, self.conn.socket.fileno()) def getdata(self): + self.lastreceived = time.time() return self.conn.read() def _checknick(self): # check if we're using the right nick, try changing @@ -98,7 +112,9 @@ class Bot(object): def _gotping(self, pieces): self.conn.send("PONG %s" % (pieces[1])) self._checknick() - def _goterror(self, pieces): #TODO handle more gracefully + def _goterror(self, pieces): + try: self.quit("Error detected: %s" % ' '.join(pieces)) + except: pass curs = self.parent.db.cursor() curs.execute("UPDATE bots SET connected = 0") curs.close() @@ -125,16 +141,16 @@ class Bot(object): msg = ' '.join(pieces[3:])[1:] self.parsemsg(user, target, msg) def _got353(self, pieces): + prefixes = {'@': 'op', '+': 'voice'} chan = self.parent.channel(pieces[4]) names = pieces[5:] names[0] = names[0][1:] #remove colon for n in names: - user = self.parent.user(n.lstrip('@+')) - if n[0] == '@': - chan.userjoin(user, 'op') - elif n[0] == '+': - chan.userjoin(user, 'voice') + if n[0] in prefixes: + user = self.parent.user(n[1:]) + chan.userjoin(user, prefixes[n[0]]) else: + user = self.parent.user(n) chan.userjoin(user) user.join(chan) def _got354(self, pieces): @@ -238,52 +254,57 @@ class Bot(object): def parsemsg(self, user, target, msg): + if user.glevel <= -2: return # short circuit if user is IGNORED chan = None chanparam = None # was the channel specified as part of the command? if len(msg) == 0: return - triggerused = msg[0] == self.parent.trigger - if triggerused: msg = msg[1:] + triggerused = msg.startswith(self.parent.trigger) + if triggerused: msg = msg[len(self.parent.trigger):] pieces = msg.split() if target == self.nick: - if msg[0] == "\001": #ctcp + if msg.startswith("\001"): #ctcp msg = msg.strip("\001") if msg == "VERSION": self.msg(user, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self.parent.APIVERSION, self.parent.RELEASE)) return - if len(pieces) > 1: - chanword = pieces[1] - if chanword[0] == '#': - chanparam = self.parent.channel(chanword) if target != self.nick: # message was sent to a channel - chan = self.parent.channel(target) try: - if msg[0] == '*': # message may be addressed to bot by "*BOTNICK" trigger? + if msg.startswith('*'): # message may be addressed to bot by "*BOTNICK" trigger? if pieces[0][1:].lower() == self.nick.lower(): pieces.pop(0) # command actually starts with next word msg = ' '.join(pieces) # command actually starts with next word - elif not triggerused: - if self.parent.haschanhook(target.lower()): - for callback in self.parent.getchanhook(target.lower()): - try: - cbret = callback(self, user, chan, *pieces) - except NotImplementedError: - self.msg(user, "Command not implemented.") - except: - self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000)) - self.__debug_cbexception("chanhook", user=user, target=target, msg=msg) - return # not to bot, don't process! + triggerused = True except IndexError: return # "message" is empty + if len(pieces) > 1: + chanword = pieces[1] + if chanword.startswith('#'): + chanparam = self.parent.channel(chanword) + + if target != self.nick: # message was sent to a channel + chan = self.parent.channel(target) + if not triggerused: + if self.parent.haschanhook(target.lower()): + for callback in self.parent.getchanhook(target.lower()): + try: + cbret = callback(self, user, chan, *pieces) + except NotImplementedError: + self.msg(user, "Command not implemented.") + except: + self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000)) + self.__debug_cbexception("chanhook", user=user, target=target, msg=msg) + return # not to bot, don't process! + cmd = pieces[0].lower() rancmd = False if self.parent.hashook(cmd): for callback in self.parent.gethook(cmd): - if chanparam is not None and callback.needchan: + if chanparam is not None and (callback.needchan or callback.wantchan): chan = chanparam pieces.pop(1) if chan is None and callback.needchan: @@ -342,7 +363,7 @@ class Bot(object): target = str(target) - if target[0] == '#': command = "PRIVMSG %s :%s" % (target, msg) + if target.startswith('#'): command = "PRIVMSG %s :%s" % (target, msg) else: command = "NOTICE %s :%s" % (target, msg) return command