irc / ircbot.py

Diff from to

ircbot.py

 
 from irclib import *
 
+# Every RECONNECT_INTERVAL seconds, check if the bot is connected and
+# reconnect it if it isn't.
+RECONNECT_INTERVAL = 60
+
 class SingleServerIRCBot:
     def __init__(self, server_list, nickname, realname, channels_file="bot.channels"):
         self.channels = {}
 
         self.nickname = nickname
         self.realname = realname
-        self.connect()
+        self.connection = self.irc.server()
         for numeric in all_events:
             self.connection.add_global_handler(numeric, self.event_dispatcher)
+        self.connection.execute_delayed(RECONNECT_INTERVAL, self.__connected_checker, ())
+        self.connect()
+
+    def __connected_checker(self):
+        self.connection.execute_delayed(RECONNECT_INTERVAL, self.__connected_checker, ())
+        if not self.connection.is_connected():
+            self.jump_server()
 
     def connect(self):
         password = None
         if len(self.server_list[self.current_server]) > 2:
             password = self.server_list[self.current_server][2]
-        self.connection = self.irc.server_connect(self.server_list[self.current_server][0],
-                                                  self.server_list[self.current_server][1],
-                                                  self.nickname,
-                                                  self.nickname,
-                                                  self.realname,
-                                                  password)
+        try:
+            self.connection.connect(self.server_list[self.current_server][0],
+                                    self.server_list[self.current_server][1],
+                                    self.nickname,
+                                    self.nickname,
+                                    self.realname,
+                                    password)
+        except ServerConnectionError:
+            pass
 
     def jump_server(self):
-        self.get_connection().quit("Jumping servers")
+        if self.connection.is_connected():
+            self.get_connection().quit("Jumping servers")
         self.current_server = (self.current_server + 1) % len(self.server_list)
         self.connect()
 
                 c.ctcp_reply(nick_from_nickmask(e.source()), "PING " + e.arguments()[1])
     
     def on_error(self, c, e):
-        self.jump_server()
         # XXX join channels here, etc.
+        pass
         
     def on_join(self, c, e):
         self.channels[lower_irc_string(e.target())].add_nick(e.source())
             return self.modes["k"]
         else:
             return None
-
-    def is_(self):
-        return self.has_mode("")
-
-    def is_(self):
-        return self.has_mode("")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.