]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
[webservice] expose webservice to plugins
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index 06a91c4d29fb1b87edf23bafaae074378d83898f..18b9e1d4ac35ff2e05e5b47d226e6a6e9b26b1f7 100644 (file)
@@ -67,6 +67,9 @@ def rawlog(level, message=nil, who_pos=1)
     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
+  end
   $log_queue.push qmsg
 end
 
@@ -144,13 +147,13 @@ end
 
 require 'rbot/load-gettext'
 require 'rbot/config'
-require 'rbot/config-compat'
 
 require 'rbot/irc'
 require 'rbot/rfc2812'
 require 'rbot/ircsocket'
 require 'rbot/botuser'
 require 'rbot/timer'
+require 'rbot/registry'
 require 'rbot/plugins'
 require 'rbot/message'
 require 'rbot/language'
@@ -186,11 +189,6 @@ class Bot
   # TODO multiserver
   attr_reader :socket
 
-  # bot's object registry, plugins get an interface to this for persistant
-  # storage (hash interface tied to a db file, plugins use Accessors to store
-  # and restore objects in their own namespaces.)
-  attr_reader :registry
-
   # bot's plugins. This is an instance of class Plugins
   attr_reader :plugins
 
@@ -198,6 +196,14 @@ 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
+
+  attr_accessor :webservice
+
   # server we are connected to
   # TODO multiserver
   def server
@@ -288,7 +294,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',
@@ -431,11 +437,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 => "bdb",
-      :wizard => true, :default => "bdb",
-      :validate => Proc.new { |v| ["bdb", "tc"].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 settings and plugin data. Options are: bdb (Berkeley DB, stable adaptor, but troublesome to install and unmaintained), tc (Tokyo Cabinet, new adaptor, fast and furious, but may be not available and contain bugs)")
+      :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
@@ -472,12 +478,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
@@ -505,14 +505,8 @@ class Bot
       $daemonize = true
     end
 
-    case @config["core.db"]
-      when "bdb"
-        require 'rbot/registry/bdb'
-      when "tc"
-        require 'rbot/registry/tc'
-      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?
@@ -581,8 +575,6 @@ class Bot
       pf << "#{$$}\n"
     end
 
-    @registry = Registry.new self
-
     @timer = Timer.new
     @save_mutex = Mutex.new
     if @config['core.save_every'] > 0
@@ -833,6 +825,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
@@ -1026,11 +1033,6 @@ class Bot
     rescue SystemExit
       log_session_end
       exit 0
-    rescue DBFatal => e
-      fatal "fatal db error: #{e.pretty_inspect}"
-      DBTree.stats
-      log_session_end
-      exit 2
     rescue Exception => e
       error e
       will_wait = true
@@ -1109,13 +1111,6 @@ class Bot
           log "Killed by server, extra delay multiplier #{oldtf} -> #{too_fast}"
         end
         retry
-      rescue DBFatal => e
-        fatal "fatal db error: #{e.pretty_inspect}"
-        DBTree.stats
-        # Why restart? DB problems are serious stuff ...
-        # restart("Oops, we seem to have registry problems ...")
-        log_session_end
-        exit 2
       rescue Exception => e
         error "non-net exception: #{e.pretty_inspect}"
         quit_msg = e.to_s
@@ -1151,6 +1146,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]
@@ -1359,8 +1360,6 @@ class Bot
       # @timer.stop
       # debug "Closing registries"
       # @registry.close
-      debug "\t\tcleaning up the db environment ..."
-      DBTree.cleanup_env
       log "rbot quit (#{message})"
     end
   end
@@ -1402,7 +1401,6 @@ class Bot
   def save
     @save_mutex.synchronize do
       @plugins.save
-      DBTree.cleanup_logs
     end
   end