]> jfr.im git - erebus.git/blobdiff - bot.py
bot - allow hooks for numerics/commands that get sent without source
[erebus.git] / bot.py
diff --git a/bot.py b/bot.py
index db5859d0c59932218384a7c1e0253a88d838cab0..793e27f6b6c923ad162e0f70ee41317c105a40b1 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -92,13 +92,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 +115,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():
@@ -145,8 +145,7 @@ class Bot(object):
                        curs = self.parent.query("UPDATE bots SET connected = 0")
                        curs.close()
                except: pass
-               sys.exit(2)
-               os._exit(2)
+               os._exit(2) # can't use sys.exit since we might be in a sub-thread
        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()