X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fircbot.rb;h=564403f5d3bdfb7868708e6a874bc1cb0ae59b4e;hb=3c9454d8a1f649f62a4f45461337434a791b1109;hp=ddbc6eb6f28fcb6c3e060e90b6a9790ad7ce0679;hpb=f092b7db45e01b459aa899e8c0d076b97c137c16;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index ddbc6eb6..564403f5 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -50,6 +50,7 @@ require 'rbot/registry' require 'rbot/plugins' require 'rbot/message' require 'rbot/language' +require 'rbot/httputil' module Irc @@ -372,13 +373,6 @@ class Bot repopulate_botclass_directory - save_dir = File.join(@botclass, 'safe_save') - Dir.mkdir(save_dir) unless File.exist?(save_dir) - unless FileTest.directory? save_dir - error "safe save location #{save_dir} is not a directory" - exit 2 - end - # Time at which the last PING was sent @last_ping = nil # Time at which the last line was RECV'd from the server @@ -409,6 +403,8 @@ class Bot debug "Using `#{@logfile}' as debug log" end + LoggerManager.instance.flush + # See http://blog.humlab.umu.se/samuel/archives/000107.html # for the backgrounding code if $daemonize @@ -427,12 +423,18 @@ class Bot # File.umask 0000 # Ensure sensible umask. Adjust as needed. end - # setup logger based on bot configuration - LoggerManager.instance.set_level(@config['log.level']) + # setup logger based on bot configuration, if not set from the command line + loglevel_set = $opts.has_key?('debug') or $opts.has_key?('loglevel') + LoggerManager.instance.set_level(@config['log.level']) unless loglevel_set + + # Set the logfile LoggerManager.instance.set_logfile(@logfile, @config['log.keep'], @config['log.max_size']) if $daemonize - log "Redirecting standard input/output/error" + log "Redirecting standard input/output/error, console logger disabled" + LoggerManager.instance.flush + LoggerManager.instance.disable_console_logger + [$stdin, $stdout, $stderr].each do |fd| begin fd.reopen "/dev/null" @@ -442,18 +444,22 @@ class Bot end end - def $stdout.write(str=nil) + def $stdout.write(*args) + str = args.map { |s| s.to_s }.join("") log str, 2 - return str.to_s.size + return str.bytesize end - def $stdout.write(str=nil) + def $stderr.write(*args) + str = args.map { |s| s.to_s }.join("") if str.to_s.match(/:\d+: warning:/) warning str, 2 else error str, 2 end - return str.to_s.size + return str.bytesize end + + LoggerManager.instance.log_session_start end File.open($opts['pidfile'] || File.join(@botclass, 'rbot.pid'), 'w') do |pf| @@ -471,6 +477,7 @@ class Bot @plugins = nil @lang = Language.new(self, @config['core.language']) + @httputil = Utils::HttpUtil.new(self) begin @auth = Auth::manager @@ -582,7 +589,7 @@ class Bot # to inform us that our nick has been changed. if data[:target] == '*' debug "setting my connection nick to #{new}" - nick = new + @client.user.nick = new end @plugins.delegate "nicktaken", data[:nick] } @@ -924,7 +931,8 @@ class Bot # begin event handling loop def mainloop - while true + @keep_looping = true + while @keep_looping too_fast = 0 quit_msg = nil valid_recv = false # did we receive anything (valid) from the server yet? @@ -954,7 +962,8 @@ class Bot # exceptions that ARENT SocketError's. How am I supposed to handle # that? rescue SystemExit - exit 0 + @keep_looping = false + break rescue Errno::ETIMEDOUT, Errno::ECONNABORTED, TimeoutError, SocketError => e error "network exception: #{e.pretty_inspect}" quit_msg = e.to_s @@ -1236,6 +1245,7 @@ class Bot debug "\tignoring cleanup error: #{$!}" end end + @httputil.cleanup # debug "\tstopping timers ..." # @timer.stop # debug "Closing registries" @@ -1250,7 +1260,7 @@ class Bot begin shutdown(message) ensure - exit 0 + @keep_looping = false end end @@ -1260,6 +1270,10 @@ class Bot :wait => @config['server.reconnect_wait'] } if (!message || message.empty?) shutdown(message) + + Irc::Bot::LoggerManager.instance.flush + Irc::Bot::LoggerManager.instance.log_session_end + sleep @config['server.reconnect_wait'] begin # now we re-exec