4 # :title: Fortune plugin
6 class FortunePlugin < Plugin
7 Config.register Config::StringValue.new('fortune.path',
9 :desc => "Full path to the fortune executable")
10 Config.register Config::ArrayValue.new('fortune.options',
11 :default => ['-n', '350', '-s'],
12 :desc => "Options to be passed on to fortune")
14 def help(plugin, topic="")
15 "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories"
19 fortune = @bot.config['fortune.path']
20 return fortune if fortune and not fortune.empty?
23 "/usr/share/bin/fortune",
25 "/usr/local/games/fortune",
26 "/usr/local/bin/fortune"].each do |f|
27 if FileTest.executable? f
33 return nil unless fortune
35 # Try setting the config entry
36 config_par = {:key => 'fortune.path', :value => [fortune], :silent => true }
37 debug "Setting fortune.path to #{fortune}"
38 set_path = @bot.plugins['config'].handle_set(m, config_par)
40 debug "fortune.path set to #{@bot.config['fortune.path']}"
42 debug "couldn't set fortune.path"
49 def fortune(m, params)
50 fortune = find_fortune(m)
51 m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
53 command = [fortune] + @bot.config['fortune.options']
54 command << params[:db]
58 ret = Utils.safe_exec(*command)
61 ret = ret.split(/\n+/).map do |l|
62 # check if this is a " -- Some Dood" line
63 if l =~ /^\s+-{1,3}\s+\w/
65 l.gsub!(/^\s+-\s/, '-- ')
69 # just remove leading and trailing whitespace
75 ret = "failed to execute fortune"
76 # TODO reset fortune.path when execution fails
82 # Print the fortune categories
83 def categories(m, params)
84 fortune = find_fortune(m)
85 m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
87 ## list all fortune databases
88 categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
95 m.reply "Fortune categories: #{categories.join ', '}"
99 plugin = FortunePlugin.new
100 plugin.map 'fortune categories', :action => "categories"
101 plugin.map 'fortune list', :action => "categories"
102 plugin.map 'fortune :db', :defaults => {:db => ''},
103 :requirements => {:db => /^[^-][\w-]+$/}