]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/fortune.rb
webhook: include target ref for pull/merge requests
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / fortune.rb
index d83d6a88c050d857bb80ada6e3cd0b0d4bdb4b46..fc54aebc353cda9220640cc8130b068a6878450c 100644 (file)
@@ -7,6 +7,9 @@ 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"
@@ -28,7 +31,7 @@ class FortunePlugin < Plugin
     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}"
@@ -44,12 +47,18 @@ class FortunePlugin < Plugin
 
   ## Pick a fortune
   def fortune(m, params)
-    db = params[:db]
     fortune = find_fortune(m)
-    m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
+    if not fortune or fortune.empty?
+      m.reply "fortune executable not found (try setting the 'fortune.path' variable)"
+      return
+    end
+
+    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|
@@ -76,7 +85,10 @@ class FortunePlugin < Plugin
   # Print the fortune categories
   def categories(m, params)
     fortune = find_fortune(m)
-    m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
+    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|
@@ -88,7 +100,7 @@ class FortunePlugin < Plugin
     ## say 'em!
     m.reply "Fortune categories: #{categories.join ', '}"
   end
+
 end
 plugin = FortunePlugin.new
 plugin.map 'fortune categories', :action => "categories"