X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fircbot.rb;h=2e808b9ee5d5503cc459b7f88173553266b5a3e2;hb=69173fe810e61ede90f21d0fdd0b802c0607fc48;hp=53235edb94da97b6e01f05c56e24dd6be3e02511;hpb=2a03358b3e17b0c253a665bd460f46914b7b3666;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index 53235edb..2e808b9e 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -7,6 +7,7 @@ require 'thread' require 'etc' +require 'date' require 'fileutils' require 'logger' @@ -14,10 +15,11 @@ $debug = false unless $debug $daemonize = false unless $daemonize $dateformat = "%Y/%m/%d %H:%M:%S" -$logger = Logger.new($stderr) +$logger = Logger.new(STDERR) $logger.datetime_format = $dateformat $logger.level = $cl_loglevel if defined? $cl_loglevel $logger.level = 0 if $debug +$logger_stderr = $logger $log_queue = Queue.new $log_thread = nil @@ -56,19 +58,16 @@ def rawlog(level, message=nil, who_pos=1) # messages originating at the same time, we blank #{who} after the first message # is output. # Also, we output strings as-is but for other objects we use pretty_inspect - case message - when String - str = message - else - str = message.pretty_inspect rescue '?' - end + message = message.kind_of?(String) ? message : (message.pretty_inspect rescue '?') qmsg = Array.new - str.each_line { |l| + message.each_line { |l| qmsg.push [level, l.chomp, who] who = ' ' * who.size } - if level == Logger::Severity::ERROR or level == Logger::Severity::FATAL and not $daemonize - $stderr.puts str + if level >= Logger::Severity::WARN and not $daemonize + qmsg.each do |l| + $logger_stderr.add(*l) + end end $log_queue.push qmsg end @@ -128,12 +127,6 @@ def fatal(message=nil, who_pos=1) rawlog(Logger::Severity::FATAL, message, who_pos) end -debug "debug test" -log "log test" -warning "warning test" -error "error test" -fatal "fatal test" - # The following global is used for the improved signal handling. $interrupted = 0 @@ -153,6 +146,7 @@ require 'rbot/rfc2812' require 'rbot/ircsocket' require 'rbot/botuser' require 'rbot/timer' +require 'rbot/registry' require 'rbot/plugins' require 'rbot/message' require 'rbot/language' @@ -162,8 +156,8 @@ module Irc # Main bot class, which manages the various components, receives messages, # handles them or passes them to plugins, and contains core functionality. class Bot - COPYRIGHT_NOTICE = "(c) Tom Gilbert and the rbot development team" - SOURCE_URL = "http://ruby-rbot.org" + COPYRIGHT_NOTICE = "(c) Giuseppe Bilotta and the rbot development team" + SOURCE_URL = "https://ruby-rbot.org" # the bot's Auth data attr_reader :auth @@ -195,6 +189,15 @@ class Bot # proxies etc as defined by the bot configuration/environment attr_accessor :httputil + # mechanize agent factory + attr_accessor :agent + + # loads and opens new registry databases, used by the plugins + attr_accessor :registry_factory + + # web service + attr_accessor :webservice + # server we are connected to # TODO multiserver def server @@ -285,7 +288,7 @@ class Bot :desc => "The CA file used to verify the SSL connection.", :wizard => true) Config.register Config::StringValue.new('server.ssl_ca_path', - :default => '', :requires_restart => true, + :default => default_ssl_ca_path, :requires_restart => true, :desc => "Alternativly a directory that includes CA PEM files used to verify the SSL connection.", :wizard => true) Config.register Config::StringValue.new('server.password', @@ -428,11 +431,11 @@ class Bot }, :desc => "Percentage of IRC penalty to consider when sending messages to prevent being disconnected for excess flood. Set to 0 to disable penalty control.") Config.register Config::StringValue.new('core.db', - :default => "dbm", - :wizard => true, :default => "dbm", - :validate => Proc.new { |v| ["dbm"].include? v }, + :default => default_db, :store_default => true, + :wizard => true, + :validate => Proc.new { |v| Registry::formats.include? v }, :requires_restart => true, - :desc => "DB adaptor to use for storing the plugin data/registries. Options: dbm (included in ruby)") + :desc => "DB adaptor to use for storing the plugin data/registries. Options: " + Registry::formats.join(', ')) @argv = params[:argv] @run_dir = params[:run_dir] || Dir.pwd @@ -469,12 +472,6 @@ class Bot repopulate_botclass_directory - registry_dir = File.join(@botclass, 'registry') - Dir.mkdir(registry_dir) unless File.exist?(registry_dir) - unless FileTest.directory? registry_dir - error "registry storage location #{registry_dir} is not a directory" - exit 2 - end save_dir = File.join(@botclass, 'safe_save') Dir.mkdir(save_dir) unless File.exist?(save_dir) unless FileTest.directory? save_dir @@ -502,12 +499,8 @@ class Bot $daemonize = true end - case @config["core.db"] - when "dbm" - require 'rbot/registry/dbm' - else - raise _("Unknown DB adaptor: %s") % @config["core.db"] - end + @registry_factory = Registry.new @config['core.db'] + @registry_factory.migrate_registry_folder(path) @logfile = @config['log.file'] if @logfile.class!=String || @logfile.empty? @@ -535,15 +528,17 @@ class Bot # File.umask 0000 # Ensure sensible umask. Adjust as needed. end - logger = Logger.new(@logfile, - @config['log.keep'], - @config['log.max_size']*1024*1024) - logger.datetime_format= $dateformat - logger.level = @config['log.level'] - logger.level = $cl_loglevel if defined? $cl_loglevel - logger.level = 0 if $debug + unless $debug + logger = Logger.new(@logfile, + @config['log.keep'], + @config['log.max_size']*1024*1024) + logger.datetime_format= $dateformat + logger.level = @config['log.level'] + logger.level = $cl_loglevel if defined? $cl_loglevel + logger.level = 0 if $debug - restart_logger(logger) + restart_logger(logger) + end log_session_start @@ -826,6 +821,21 @@ class Bot end end + def default_ssl_ca_path + file = default_ssl_ca_file + File.dirname file if file + end + + # Determine if tokyocabinet is installed, if it is use it as a default. + def default_db + begin + require 'tokyocabinet' + return 'tc' + rescue LoadError + return 'dbm' + end + end + def repopulate_botclass_directory template_dir = File.join Config::datadir, 'templates' if FileTest.directory? @botclass @@ -1132,6 +1142,12 @@ class Bot where = ds[:dest] filtered = ds[:text] + if defined? WebServiceUser and where.instance_of? WebServiceUser + debug 'sendmsg to web service!' + where.response << filtered + return + end + # For starters, set up appropriate queue channels and rings mchan = opts[:queue_channel] mring = opts[:queue_ring] @@ -1377,20 +1393,26 @@ class Bot end end - # call the save method for all of the botmodules - def save + # call the save method for all or the specified botmodule + # + # :botmodule :: + # optional botmodule to save + def save(botmodule=nil) @save_mutex.synchronize do - @plugins.save + @plugins.save(botmodule) end end - # call the rescan method for all of the botmodules - def rescan + # call the rescan method for all or just the specified botmodule + # + # :botmodule :: + # instance of the botmodule to rescan + def rescan(botmodule=nil) debug "\tstopping timer..." @timer.stop @save_mutex.synchronize do - @lang.rescan - @plugins.rescan + # @lang.rescan + @plugins.rescan(botmodule) end @timer.start end