]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
oops bug
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index 91afd94ea41be0e1360693c05bfe16ae67942ac1..b36cd36ded25a65ae74fd798ec7094c32939c615 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
 
@@ -121,8 +122,8 @@ class IrcBot
       exit 2
     end
     
-    #botclass = "#{Etc.getpwnam(Etc.getlogin).dir}/.rbot" unless botclass
-    botclass = "#{ENV['HOME']}/.rbot" unless botclass
+    botclass = "#{Etc.getpwuid(Process::Sys.geteuid)[:dir]}/.rbot" unless botclass
+    #botclass = "#{ENV['HOME']}/.rbot" unless botclass
     @botclass = botclass.gsub(/\/$/, "")
 
     unless FileTest.directory? botclass
@@ -301,16 +302,16 @@ class IrcBot
     }
     @client[:unknown] = proc {|data|
       #debug "UNKNOWN: #{data[:serverstring]}"
-      log data[:serverstring], ":unknown"
+      log data[:serverstring], ".unknown"
     }
   end
 
   # 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
@@ -328,8 +329,8 @@ class IrcBot
   def mainloop
     while true
       begin
-      connect
-      @timer.start
+        connect
+        @timer.start
       
         while true
           if @socket.select
@@ -438,6 +439,7 @@ class IrcBot
   def log(message, where="server")
     message.chomp!
     stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
+    where.gsub!(/[:!?$*()\/\\<>|"']/, "_")
     unless(@logs.has_key?(where))
       @logs[where] = File.new("#{@botclass}/logs/#{where}", "a")
       @logs[where].sync = true
@@ -454,9 +456,9 @@ class IrcBot
   # disconnect from the server and cleanup all plugins and modules
   def shutdown(message = nil)
     begin
+      trap("SIGINT", "DEFAULT")
       trap("SIGTERM", "DEFAULT")
       trap("SIGHUP", "DEFAULT")
-      trap("SIGINT", "DEFAULT")
     rescue
       debug "failed to trap signals, probably running on windows?"
     end
@@ -495,7 +497,6 @@ class IrcBot
 
   # call the save method for bot's config, keywords, auth and all plugins
   def save
-    @registry.flush
     @config.save
     @keywords.save
     @auth.save
@@ -598,6 +599,8 @@ class IrcBot
           debug "no PONG from server for #{diff} seconds, reconnecting"
           begin
             @socket.shutdown
+            # TODO
+            # raise an exception to get back to the mainloop
           rescue
             debug "couldn't shutdown connection (already shutdown?)"
           end