]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
ircbot: refactor reconnection into its own method
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 15 Jan 2009 03:32:56 +0000 (04:32 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 15 Jan 2009 03:48:23 +0000 (04:48 +0100)
lib/rbot/ircbot.rb

index f8dbc5013cae68321c4476bc3dfee6ab387ca617..0ae1bce0e424028f4656ffe7f2f09d94152ed1c9 100644 (file)
@@ -872,14 +872,28 @@ class Bot
     myself.user = @config['irc.user']
   end
 
+  # disconnect the bot from IRC, if connected, and then connect (again)
+  def reconnect(message=nil, too_fast=false)
+    if @socket.connected?
+      disconnect(message)
+
+      log "\n\nDisconnected\n\n"
+
+      quit if $interrupted > 0
+
+      log "\n\nWaiting to reconnect\n\n"
+      sleep @config['server.reconnect_wait']
+      sleep 10*@config['server.reconnect_wait'] if too_fast
+    end
+    connect
+  end
+
   # begin event handling loop
   def mainloop
     while true
       too_fast = false
       begin
         quit if $interrupted > 0
-        connect
-
         quit_msg = nil
         while @socket.connected?
           quit if $interrupted > 0
@@ -925,16 +939,7 @@ class Bot
         log_session_end
         exit 2
       end
-
-      disconnect(quit_msg)
-
-      log "\n\nDisconnected\n\n"
-
-      quit if $interrupted > 0
-
-      log "\n\nWaiting to reconnect\n\n"
-      sleep @config['server.reconnect_wait']
-      sleep 10*@config['server.reconnect_wait'] if too_fast
+      reconnect(quit_msg, too_fast)
     end
   end