]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
Whitespace cleanup
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index 271d0d70ea358d9590872fed932c377bfa37b119..b2c7a88f91b4cef106066ed3946f831684e1bb67 100644 (file)
@@ -124,6 +124,7 @@ class IrcBot
     
     botclass = "#{Etc.getpwuid(Process::Sys.geteuid)[:dir]}/.rbot" unless botclass
     #botclass = "#{ENV['HOME']}/.rbot" unless botclass
+    botclass = File.expand_path(botclass)
     @botclass = botclass.gsub(/\/$/, "")
 
     unless FileTest.directory? botclass
@@ -136,6 +137,7 @@ class IrcBot
     end
     
     Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
+    Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
 
     @ping_timer = nil
     @pong_timer = nil
@@ -329,8 +331,8 @@ class IrcBot
   def mainloop
     while true
       begin
-      connect
-      @timer.start
+        connect
+        @timer.start
       
         while true
           if @socket.select
@@ -437,9 +439,9 @@ class IrcBot
   # log message +message+ to a file determined by +where+. +where+ can be a
   # channel name, or a nick for private message logging
   def log(message, where="server")
-    message.chomp!
+    message = message.chomp
     stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
-    where.gsub!(/[:!?$*()\/\\<>|"']/, "_")
+    where = where.gsub(/[:!?$*()\/\\<>|"']/, "_")
     unless(@logs.has_key?(where))
       @logs[where] = File.new("#{@botclass}/logs/#{where}", "a")
       @logs[where].sync = true
@@ -463,15 +465,25 @@ class IrcBot
       debug "failed to trap signals, probably running on windows?"
     end
     message = @lang.get("quit") if (message.nil? || message.empty?)
+    debug "Clearing socket"
     @socket.clearq
+    debug "Saving"
     save
+    debug "Cleaning up"
     @plugins.cleanup
+    debug "Logging quits"
     @channels.each_value {|v|
       log "@ quit (#{message})", v.name
     }
-    @registry.close
+    # debug "Closing registries"
+    # @registry.close
+    debug "Cleaning up the db environment"
+    DBTree.cleanup_env
+    debug "Sending quit message"
     @socket.puts "QUIT :#{message}"
+    debug "Flushing socket"
     @socket.flush
+    debug "Shutting down socket"
     @socket.shutdown
     puts "rbot quit (#{message})"
   end
@@ -497,11 +509,11 @@ 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
     @plugins.save
+    DBTree.cleanup_logs
   end
 
   # call the rescan method for the bot's lang, keywords and all plugins
@@ -571,7 +583,8 @@ class IrcBot
   def status
     secs_up = Time.new - @startup_time
     uptime = Utils.secs_to_string secs_up
-    return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
+    # return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
+    return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
   end
 
   # we'll ping the server every 30 seconds or so, and expect a response
@@ -636,8 +649,8 @@ class IrcBot
         return "say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>"
       when "action"
         return "action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>"
-      when "topic"
-        return "topic <channel> <message> => set topic of <channel> to <message>"
+       #       when "topic"
+       #         return "topic <channel> <message> => set topic of <channel> to <message>"
       when "quiet"
         return "quiet [in here|<channel>] => with no arguments, stop speaking in all channels, if \"in here\", stop speaking in this channel, or stop speaking in <channel>"
       when "talk"
@@ -680,6 +693,7 @@ class IrcBot
     end
 
     if(m.address?)
+      delegate_privmsg(m)
       case m.message
         when (/^join\s+(\S+)\s+(\S+)$/i)
           join $1, $2 if(@auth.allow?("join", m.source, m.replyto))
@@ -706,16 +720,19 @@ class IrcBot
           say $1, $2 if(@auth.allow?("say", m.source, m.replyto))
         when (/^action\s+(\S+)\s+(.*)$/i)
           action $1, $2 if(@auth.allow?("say", m.source, m.replyto))
-        when (/^topic\s+(\S+)\s+(.*)$/i)
-          topic $1, $2 if(@auth.allow?("topic", m.source, m.replyto))
+         # when (/^topic\s+(\S+)\s+(.*)$/i)
+          #   topic $1, $2 if(@auth.allow?("topic", m.source, m.replyto))
         when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i)
           mode $1, $2, $3 if(@auth.allow?("mode", m.source, m.replyto))
         when (/^ping$/i)
           say m.replyto, "pong"
         when (/^rescan$/i)
           if(@auth.allow?("config", m.source, m.replyto))
-            m.okay
+            m.reply "Saving ..."
+            save
+            m.reply "Rescanning ..."
             rescan
+            m.okay
           end
         when (/^quiet$/i)
           if(auth.allow?("talk", m.source, m.replyto))
@@ -760,8 +777,6 @@ class IrcBot
         when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)
           say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)
           say m.replyto, @lang.get("hello") if(m.private?)
-        else
-          delegate_privmsg(m)
       end
     else
       # stuff to handle when not addressed