]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
ircbot: refactor and clean up botclass dir handling
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 14 Feb 2009 22:56:49 +0000 (23:56 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 14 Feb 2009 23:15:44 +0000 (00:15 +0100)
Use File.join across the board, and refactor some botclass directory
handling. Most important changes:
 * failure to create the registry and safe_save directory is now fatal;
 * failure to create the local plugin directory prevents it from being
   added to the plugin path (with a warning);
 * botclass directory update from templates is now a standalone routine
   called during init, making it possible to use it in other cases too.

lib/rbot/ircbot.rb

index f491b212e1d6b8928cc9305cc3eda946ac318284..6ae3e4edfa624d5ea0b69567527c596e076faad1 100644 (file)
@@ -437,36 +437,25 @@ class Bot
           botclass.gsub!("\\","/")
         end
       end
-      botclass += "/.rbot"
+      botclass = File.join(botclass, ".rbot")
     end
     botclass = File.expand_path(botclass)
     @botclass = botclass.gsub(/\/$/, "")
 
-    if FileTest.directory? botclass
-      # compare the templates dir with the current botclass, and fill it in with
-      # any missing file.
-      # Sadly, FileUtils.cp_r doesn't have an :update option, so we have to do it
-      # manually
-      template = File.join Config::datadir, 'templates'
-      # note that we use the */** pattern because we don't want to match
-      # keywords.rbot, which gets deleted on load and would therefore be missing always
-      missing = Dir.chdir(template) { Dir.glob('*/**') } - Dir.chdir(botclass) { Dir.glob('*/**') }
-      missing.map do |f|
-        dest = File.join(botclass, f)
-        FileUtils.mkdir_p(File.dirname dest)
-        FileUtils.cp File.join(template, f), dest
-      end
-    else
-      log "no #{botclass} directory found, creating from templates.."
-      if FileTest.exist? botclass
-        error "file #{botclass} exists but isn't a directory"
-        exit 2
-      end
-      FileUtils.cp_r Config::datadir+'/templates', botclass
-    end
+    repopulate_botclass_directory
 
-    Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
-    Dir.mkdir("#{botclass}/safe_save") unless File.exist?("#{botclass}/safe_save")
+    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
+      error "safe save location #{save_dir} is not a directory"
+      exit 2
+    end
 
     # Time at which the last PING was sent
     @last_ping = nil
@@ -490,7 +479,9 @@ class Bot
 
     @logfile = @config['log.file']
     if @logfile.class!=String || @logfile.empty?
-      @logfile = "#{botclass}/#{File.basename(botclass).gsub(/^\.+/,'')}.log"
+      logfname =  File.basename(botclass).gsub(/^\.+/,'')
+      logfname << ".log"
+      @logfile = File.join(botclass, logfname)
       debug "Using `#{@logfile}' as debug log"
     end
 
@@ -549,7 +540,7 @@ class Bot
       end
     end
 
-    File.open($opts['pidfile'] || "#{@botclass}/rbot.pid", 'w') do |pf|
+    File.open($opts['pidfile'] || File.join(@botclass, 'rbot.pid'), 'w') do |pf|
       pf << "#{$$}\n"
     end
 
@@ -579,7 +570,6 @@ class Bot
     @auth.everyone.set_default_permission("*", true)
     @auth.botowner.password= @config['auth.password']
 
-    Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
     @plugins = Plugins::manager
     @plugins.bot_associate(self)
     setup_plugins_path()
@@ -776,14 +766,47 @@ class Bot
     trap_sigs
   end
 
+  def repopulate_botclass_directory
+    template_dir = File.join Config::datadir, 'templates'
+    if FileTest.directory? @botclass
+      # compare the templates dir with the current botclass dir, filling up the
+      # latter with any missing file. Sadly, FileUtils.cp_r doesn't have an
+      # :update option, so we have to do it manually.
+      # Note that we use the */** pattern because we don't want to match
+      # keywords.rbot, which gets deleted on load and would therefore be missing
+      # always
+      missing = Dir.chdir(template_dir) { Dir.glob('*/**') } - Dir.chdir(@botclass) { Dir.glob('*/**') }
+      missing.map do |f|
+        dest = File.join(@botclass, f)
+        FileUtils.mkdir_p(File.dirname dest)
+        FileUtils.cp File.join(template_dir, f), dest
+      end
+    else
+      log "no #{@botclass} directory found, creating from templates..."
+      if FileTest.exist? @botclass
+        error "file #{@botclass} exists but isn't a directory"
+        exit 2
+      end
+      FileUtils.cp_r template_dir, @botclass
+    end
+  end
+
   def setup_plugins_path
+    plugdir_default = File.join(Config::datadir, 'plugins')
+    plugdir_local = File.join(@botclass, 'plugins')
+    Dir.mkdir(plugdir_local) unless File.exist?(plugdir_local)
+
     @plugins.clear_botmodule_dirs
-    @plugins.add_botmodule_dir(Config::coredir + "/utils")
+    @plugins.add_botmodule_dir(File.join(Config::coredir, 'utils'))
     @plugins.add_botmodule_dir(Config::coredir)
-    @plugins.add_botmodule_dir("#{botclass}/plugins")
+    if FileTest.directory? plugdir_local
+      @plugins.add_botmodule_dir(plugdir_local)
+    else
+      warning "local plugin location #{plugdir_local} is not a directory"
+    end
 
     @config['plugins.path'].each do |_|
-        path = _.sub(/^\(default\)/, Config::datadir + '/plugins')
+        path = _.sub(/^\(default\)/, plugdir_default)
         @plugins.add_botmodule_dir(path)
     end
   end