]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/fortune.rb
webhook: define number for watch/star actions too
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / fortune.rb
index adb6a839a656a0897387508d6792ed63792b222d..fc54aebc353cda9220640cc8130b068a6878450c 100644 (file)
@@ -7,43 +7,58 @@ class FortunePlugin < Plugin
   Config.register Config::StringValue.new('fortune.path',
     :default => '',
     :desc => "Full path to the fortune executable")
+  Config.register Config::ArrayValue.new('fortune.options',
+    :default => ['-n', '350', '-s'],
+    :desc => "Options to be passed on to fortune")
 
   def help(plugin, topic="")
     "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories"
   end
 
+  def find_fortune(m)
+    fortune = @bot.config['fortune.path']
+    return fortune if fortune and not fortune.empty?
+
+    ["/usr/bin/fortune",
+     "/usr/share/bin/fortune",
+     "/usr/games/fortune",
+     "/usr/local/games/fortune",
+     "/usr/local/bin/fortune"].each do |f|
+      if FileTest.executable? f
+        fortune = f
+        break
+      end
+    end
+
+    return nil unless fortune
+
+    # Try setting the config entry
+    config_par = {:key => 'fortune.path', :value => [fortune], :silent => true }
+    debug "Setting fortune.path to #{fortune}"
+    set_path = @bot.plugins['config'].handle_set(m, config_par)
+    if set_path
+      debug "fortune.path set to #{@bot.config['fortune.path']}"
+    else
+      debug "couldn't set fortune.path"
+    end
+
+    return fortune
+  end
 
   ## Pick a fortune
   def fortune(m, params)
-    db = params[:db]
-    fortune = @bot.config['fortune.path']
-    if fortune.empty?
-      ["/usr/bin/fortune",
-       "/usr/share/bin/fortune",
-       "/usr/games/fortune",
-       "/usr/local/games/fortune",
-       "/usr/local/bin/fortune"].each do |f|
-          if FileTest.executable? f
-            fortune = f
-
-            # Try setting the config entry
-            config_par = {:key => 'fortune.path', :value => [f], :silent => true }
-           debug "Setting fortune.path to #{f}"
-            set_path = @bot.plugins['config'].handle_set(m, config_par)
-            if set_path
-              debug "fortune.path set to #{@bot.config['fortune.path']}"
-            else
-              debug "couldn't set fortune.path"
-            end
-
-            break
-          end
-        end
+    fortune = find_fortune(m)
+    if not fortune or fortune.empty?
+      m.reply "fortune executable not found (try setting the 'fortune.path' variable)"
+      return
     end
-    m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
+
+    command = [fortune] + @bot.config['fortune.options']
+    command << params[:db]
+    command.compact!
 
     begin
-      ret = Utils.safe_exec(fortune, "-n", "350", "-s", db)
+      ret = Utils.safe_exec(*command)
 
       ## cleanup ret
       ret = ret.split(/\n+/).map do |l|
@@ -67,21 +82,25 @@ class FortunePlugin < Plugin
     m.reply ret
   end
 
-
   # Print the fortune categories
   def categories(m, params)
-    ## list all fortune files in /usr/share/games/fortune
-    categories = Dir["/usr/share/games/fortune/*"].select{ |f|
-      File.split(f).last.match(/^\w+$/)
+    fortune = find_fortune(m)
+    if not fortune or fortune.empty?
+      m.reply "fortune executable not found (try setting the 'fortune.path' variable)"
+      return
+    end
+
+    ## list all fortune databases
+    categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
+      f.split[1]
     }.select{ |f|
-      File.file?(f)
-    }.map{ |p|
-      File.split(p).last
+      f[0..0] != '/'
     }.sort
+
     ## say 'em!
     m.reply "Fortune categories: #{categories.join ', '}"
   end
+
 end
 plugin = FortunePlugin.new
 plugin.map 'fortune categories', :action => "categories"