]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
* unicode.rb: @old_kcode --> @@old_kcode [we aren't always called as an instance]
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index 01b9d5b636018a06c429ecd458ab120951e7d343..baf520056dc3dbfc92c69008c6a7400b02d23ba0 100644 (file)
@@ -463,17 +463,20 @@ class Bot
       Dir.chdir botclass
       # File.umask 0000                # Ensure sensible umask. Adjust as needed.
       log "Redirecting standard input/output/error"
-      begin
-        STDIN.reopen "/dev/null"
-      rescue Errno::ENOENT
-        # On Windows, there's not such thing as /dev/null
-        STDIN.reopen "NUL"
+      [$stdin, $stdout, $stderr].each do |fd|
+        begin
+          fd.reopen "/dev/null"
+        rescue Errno::ENOENT
+          # On Windows, there's not such thing as /dev/null
+          fd.reopen "NUL"
+        end
       end
-      def STDOUT.write(str=nil)
+
+      def $stdout.write(str=nil)
         log str, 2
         return str.to_s.size
       end
-      def STDERR.write(str=nil)
+      def $stdout.write(str=nil)
         if str.to_s.match(/:\d+: warning:/)
           warning str, 2
         else
@@ -701,6 +704,8 @@ class Bot
       :split_at => Regexp.new(@config['send.split_at']),
       :purge_split => @config['send.purge_split'],
       :truncate_text => @config['send.truncate_text'].dup
+
+    trap_sigs
   end
 
   def setup_plugins_path
@@ -758,10 +763,10 @@ class Bot
   end
 
   # things to do when we receive a signal
-  def got_sig(sig)
-    debug "received #{sig}, queueing quit"
+  def got_sig(sig, func=:quit)
+    debug "received #{sig}, queueing #{func}"
     $interrupted += 1
-    quit unless @quit_mutex.locked?
+    self.send(func) unless @quit_mutex.locked?
     debug "interrupted #{$interrupted} times"
     if $interrupted >= 3
       debug "drastic!"
@@ -770,17 +775,21 @@ class Bot
     end
   end
 
-  # connect the bot to IRC
-  def connect
+  # trap signals
+  def trap_sigs
     begin
       trap("SIGINT") { got_sig("SIGINT") }
       trap("SIGTERM") { got_sig("SIGTERM") }
-      trap("SIGHUP") { got_sig("SIGHUP") }
+      trap("SIGHUP") { got_sig("SIGHUP", :restart) }
     rescue ArgumentError => e
       debug "failed to trap signals (#{e.pretty_inspect}): running on Windows?"
     rescue Exception => e
       debug "failed to trap signals: #{e.pretty_inspect}"
     end
+  end
+
+  # connect the bot to IRC
+  def connect
     begin
       quit if $interrupted > 0
       @socket.connect