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"
17 def fortune(m, params)
19 fortune = @bot.config['fortune.path']
22 "/usr/share/bin/fortune",
24 "/usr/local/games/fortune",
25 "/usr/local/bin/fortune"].each do |f|
26 if FileTest.executable? f
29 # Try setting the config entry
30 config_par = {:key => 'fortune.path', :value => [f], :silent => true }
31 debug "Setting fortune.path to #{f}"
32 set_path = @bot.plugins['config'].handle_set(m, config_par)
34 debug "fortune.path set to #{@bot.config['fortune.path']}"
36 debug "couldn't set fortune.path"
43 m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
46 ret = Utils.safe_exec(fortune, "-n", "350", "-s", db)
49 ret = ret.split(/\n+/).map do |l|
50 # check if this is a " -- Some Dood" line
51 if l =~ /^\s+-{1,3}\s+\w/
53 l.gsub!(/^\s+-\s/, '-- ')
57 # just remove leading and trailing whitespace
63 ret = "failed to execute fortune"
64 # TODO reset fortune.path when execution fails
71 # Print the fortune categories
72 def categories(m, params)
73 ## list all fortune files in /usr/share/games/fortune
74 categories = Dir["/usr/share/games/fortune/*"].select{ |f|
75 File.split(f).last.match(/^\w+$/)
82 m.reply "Fortune categories: #{categories.join ', '}"
86 plugin = FortunePlugin.new
87 plugin.map 'fortune categories', :action => "categories"
88 plugin.map 'fortune list', :action => "categories"
89 plugin.map 'fortune :db', :defaults => {:db => ''},
90 :requirements => {:db => /^[^-][\w-]+$/}