]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/ircbot.py
make inversion optional
[z_archive/twitter.git] / twitter / ircbot.py
index b9fd895510e0c416a1cdb767afade412b42071ab..463136fa7c0952870e9f891b1ba93ddb05884943 100644 (file)
@@ -154,6 +154,7 @@ class TwitterBot(object):
         self.irc = irclib.IRC()
         self.irc.add_global_handler('privmsg', self.handle_privmsg)
         self.irc.add_global_handler('ctcp', self.handle_ctcp)
+        self.irc.add_global_handler('umode', self.handle_umode)
         self.ircServer = self.irc.server()
 
         self.sched = Scheduler(
@@ -226,6 +227,19 @@ class TwitterBot(object):
             elif args[0] == 'CLIENTINFO':
                 conn.ctcp_reply(source, "CLIENTINFO PING VERSION CLIENTINFO")
 
+    def handle_umode(self, conn, evt):
+        """
+        QuakeNet ignores all your commands until after the MOTD. This
+        handler defers joining until after it sees a magic line. It
+        also tries to join right after connect, but this will just
+        make it join again which should be safe.
+        """
+        args = evt.arguments()
+        if (args and args[0] == '+i'):
+            channels = self.config.get('irc', 'channel').split(',')
+            for channel in channels:
+                self.ircServer.join(channel)
+
     def privmsg_channels(self, msg):
         return_response=True
         channels=self.config.get('irc','channel').split(',')
@@ -274,7 +288,7 @@ class TwitterBot(object):
                 "%s%s has asked me to stop following %s" %(
                     get_prefix('inform'), userNick, name))
 
-    def run(self):
+    def _irc_connect(self):
         self.ircServer.connect(
             self.config.get('irc', 'server'),
             self.config.getint('irc', 'port'),
@@ -283,6 +297,9 @@ class TwitterBot(object):
         for channel in channels:
             self.ircServer.join(channel)
 
+    def run(self):
+        self._irc_connect()
+
         while True:
             try:
                 self.sched.run_forever()
@@ -292,6 +309,10 @@ class TwitterBot(object):
                 # twitter.com is probably down because it
                 # sucks. ignore the fault and keep going
                 pass
+            except irclib.ServerNotConnectedError:
+                # Try and reconnect to IRC.
+                self._irc_connect()
+
 
 def load_config(filename):
     # Note: Python ConfigParser module has the worst interface in the