]> jfr.im git - erebus.git/blobdiff - bot.py
fix watchdog, fix exiting when network goes away, etc
[erebus.git] / bot.py
diff --git a/bot.py b/bot.py
index 366eda2db45efc2f789dcc419f6f2f65885f6a79..6ea812815eaadb4d282886a5e0e4fb49baeefbfd 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -69,7 +69,8 @@ class Bot(object):
        def watchdog(self):
                if time.time() > int(self.parent.cfg.get('watchdog', 'maxtime', default=300))+self.lastreceived:
                        self.parse("ERROR :Fake-error from watchdog timer.")
-               self.watchdogtimer = MyTimer(int(self.parent.cfg.get('watchdog', 'interval', default=30)), self.watchdog)
+               watchdogtimer = MyTimer(int(self.parent.cfg.get('watchdog', 'interval', default=30)), self.watchdog)
+               watchdogtimer.start()
 
        def log(self, *args, **kwargs):
                self.parent.log(self.nick, *args, **kwargs)
@@ -92,13 +93,13 @@ class Bot(object):
                        self.log('I', line)
                pieces = line.split()
 
+               if pieces[0][0] == ":":
+                       numeric = pieces[1]
+               else:
+                       numeric = pieces[0]
+
                # dispatch dict
-               zero = { #things to look for without source
-                       'NOTICE': self._gotconnected,
-                       'PING': self._gotping,
-                       'ERROR': self._goterror,
-               }
-               one = { #things to look for after source
+               dispatch = { #things to look for after source
                        'NOTICE': self._gotconnected,
                        '001': self._got001,
                        '004': self._got004,
@@ -115,20 +116,20 @@ class Bot(object):
                        'QUIT': self._gotquit,
                        'NICK': self._gotnick,
                        'MODE': self._gotmode,
+                       'PING': self._gotping,
+                       'ERROR': self._goterror,
                }
 
-               if self.parent.hasnumhook(pieces[1]):
-                       hooks = self.parent.getnumhook(pieces[1])
+               if self.parent.hasnumhook(numeric):
+                       hooks = self.parent.getnumhook(numeric)
                        for callback in hooks:
                                try:
                                        callback(self, line)
                                except Exception:
                                        self.__debug_cbexception("numhook", line)
 
-               if pieces[0] in zero:
-                       zero[pieces[0]](pieces)
-               elif pieces[1] in one:
-                       one[pieces[1]](pieces)
+               if numeric in dispatch:
+                       dispatch[numeric](pieces)
 
        def _gotconnected(self, pieces):
                if not self.conn.registered():
@@ -138,6 +139,7 @@ class Bot(object):
                self._checknick()
        def _goterror(self, pieces):
                # TODO: better handling, just reconnect that single bot
+               self.parent.mustquit = Exception(' '.join(pieces))
                try:
                        self.quit("Error detected: %s" % ' '.join(pieces))
                except: pass
@@ -146,7 +148,6 @@ class Bot(object):
                        curs.close()
                except: pass
                sys.exit(2)
-               os._exit(2)
        def _got001(self, pieces):
                # We wait until the end of MOTD instead to consider ourselves registered, but consider uptime as of 001
                self.connecttime = time.time()
@@ -305,7 +306,7 @@ class Bot(object):
                        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))
+                                       self.msg(user, "\001VERSION Erebus v%d.%d - http://jfr.im/git/erebus.git" % (self.parent.APIVERSION, self.parent.RELEASE))
                                return
 
                triggerused = msg.startswith(self.parent.trigger)
@@ -365,6 +366,7 @@ class Bot(object):
                                                self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000))
                                                self.__debug_cbexception("hook", user=user, target=target, msg=msg)
                                        except SystemExit as e:
+                                               self.parent.mustquit = e
                                                try:
                                                        curs = self.parent.query("UPDATE bots SET connected = 0")
                                                        curs.close()
@@ -396,7 +398,7 @@ class Bot(object):
                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.
        """
        def _msg(self, target, msg, truncate, append_callback, msgtype):
-               if self.parent.cfg.getboolean('erebus', 'nofakelag'): return self.fastmsg(target, 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).