]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
fix help text, ticket #36
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index 1374929dce1f02cb97f944f9917c03a9eeb896d4..ddc53d982bf677e217461fb4323f3ad380ff6d17 100644 (file)
@@ -5,7 +5,8 @@ require 'fileutils'
 $debug = false unless $debug
 # print +message+ if debugging is enabled
 def debug(message=nil)
-  print "DEBUG: #{message}\n" if($debug && message)
+  stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
+  print "D: [#{stamp}] #{message}\n" if($debug && message)
   #yield
 end
 
@@ -51,9 +52,6 @@ class IrcBot
   # bot's Language data
   attr_reader :lang
 
-  # bot's configured addressing prefixes
-  attr_reader :addressing_prefixes
-
   # channel info for channels the bot is in
   attr_reader :channels
 
@@ -311,15 +309,15 @@ class IrcBot
   # connect the bot to IRC
   def connect
     begin
+      trap("SIGINT") { quit }
       trap("SIGTERM") { quit }
       trap("SIGHUP") { quit }
-      trap("SIGINT") { quit }
     rescue
       debug "failed to trap signals, probably running on windows?"
     end
     begin
       @socket.connect
-      rescue => e
+    rescue => e
       raise "failed to connect to IRC server at #{@config['server.name']} #{@config['server.port']}: " + e
     end
     @socket.puts "PASS " + @config['server.password'] if @config['server.password']
@@ -330,10 +328,10 @@ class IrcBot
   # begin event handling loop
   def mainloop
     while true
+      begin
       connect
       @timer.start
       
-      begin
         while true
           if @socket.select
             break unless reply = @socket.gets
@@ -344,8 +342,10 @@ class IrcBot
       # exceptions that ARENT SocketError's. How am I supposed to handle
       # that?
       #rescue TimeoutError, SocketError => e
+      rescue SystemExit
+        exit 0
       rescue Exception => e
-        puts "network exception: connection closed: #{e}"
+        puts "network exception: connection closed: #{e.inspect}"
         puts e.backtrace.join("\n")
         @socket.shutdown # now we reconnect
       rescue => e
@@ -355,6 +355,7 @@ class IrcBot
       end
       
       puts "disconnected"
+      @last_ping = nil
       @channels.clear
       @socket.clearq
       
@@ -453,9 +454,13 @@ class IrcBot
 
   # disconnect from the server and cleanup all plugins and modules
   def shutdown(message = nil)
-    trap("SIGTERM", "DEFAULT")
-    trap("SIGHUP", "DEFAULT")
-    trap("SIGINT", "DEFAULT")
+    begin
+      trap("SIGINT", "DEFAULT")
+      trap("SIGTERM", "DEFAULT")
+      trap("SIGHUP", "DEFAULT")
+    rescue
+      debug "failed to trap signals, probably running on windows?"
+    end
     message = @lang.get("quit") if (message.nil? || message.empty?)
     @socket.clearq
     save
@@ -463,18 +468,21 @@ class IrcBot
     @channels.each_value {|v|
       log "@ quit (#{message})", v.name
     }
+    @registry.close
     @socket.puts "QUIT :#{message}"
     @socket.flush
     @socket.shutdown
-    @registry.close
     puts "rbot quit (#{message})"
   end
   
   # message:: optional IRC quit message
   # quit IRC, shutdown the bot
   def quit(message=nil)
-    shutdown(message)
-    exit 0
+    begin
+      shutdown(message)
+    ensure
+      exit 0
+    end
   end
 
   # totally shutdown and respawn the bot
@@ -568,6 +576,7 @@ class IrcBot
   # we'll ping the server every 30 seconds or so, and expect a response
   # before the next one come around..
   def start_server_pings
+    @last_ping = nil
     # stop existing timers if running
     unless @ping_timer.nil?
       @timer.remove @ping_timer
@@ -593,6 +602,7 @@ class IrcBot
           rescue
             debug "couldn't shutdown connection (already shutdown?)"
           end
+          @last_ping = nil
         end
       end
     }