X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fircbot.rb;h=b36cd36ded25a65ae74fd798ec7094c32939c615;hb=8246bd135ebf319beae8fec39620f4c4c7e54601;hp=2443ec95d7ae0865423a2b4a4117245c55cb77ea;hpb=5a06676cdec9d4b43767f39ef2dea3b735a964a5;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index 2443ec95..b36cd36d 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -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,22 +302,22 @@ 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 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'] @@ -328,8 +329,8 @@ class IrcBot def mainloop while true begin - connect - @timer.start + connect + @timer.start while true if @socket.select @@ -341,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 @@ -436,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 @@ -451,9 +455,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 @@ -461,18 +469,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 @@ -486,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 @@ -589,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