4 # :title: Fortune plugin
6 class FortunePlugin < Plugin
7 Config.register Config::StringValue.new('fortune.path',
9 :desc => "Full path to the fortune executable")
11 def help(plugin, topic="")
12 "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories"
16 fortune = @bot.config['fortune.path']
17 return fortune if fortune
20 "/usr/share/bin/fortune",
22 "/usr/local/games/fortune",
23 "/usr/local/bin/fortune"].each do |f|
24 if FileTest.executable? f
30 return nil unless fortune
32 # Try setting the config entry
33 config_par = {:key => 'fortune.path', :value => [fortune], :silent => true }
34 debug "Setting fortune.path to #{fortune}"
35 set_path = @bot.plugins['config'].handle_set(m, config_par)
37 debug "fortune.path set to #{@bot.config['fortune.path']}"
39 debug "couldn't set fortune.path"
46 def fortune(m, params)
48 fortune = find_fortune
49 m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
52 ret = Utils.safe_exec(fortune, "-n", "350", "-s", db)
55 ret = ret.split(/\n+/).map do |l|
56 # check if this is a " -- Some Dood" line
57 if l =~ /^\s+-{1,3}\s+\w/
59 l.gsub!(/^\s+-\s/, '-- ')
63 # just remove leading and trailing whitespace
69 ret = "failed to execute fortune"
70 # TODO reset fortune.path when execution fails
76 # Print the fortune categories
77 def categories(m, params)
78 fortune = find_fortune
79 m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
81 ## list all fortune databases
82 categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
89 m.reply "Fortune categories: #{categories.join ', '}"
93 plugin = FortunePlugin.new
94 plugin.map 'fortune categories', :action => "categories"
95 plugin.map 'fortune list', :action => "categories"
96 plugin.map 'fortune :db', :defaults => {:db => ''},
97 :requirements => {:db => /^[^-][\w-]+$/}