1 class FortunePlugin < Plugin
2 BotConfig.register BotConfigStringValue.new('fortune.path',
4 :desc => "Full path to the fortune executable")
6 def help(plugin, topic="")
7 "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories"
12 def fortune(m, params)
14 fortune = @bot.config['fortune.path']
17 "/usr/share/bin/fortune",
19 "/usr/local/games/fortune",
20 "/usr/local/bin/fortune"].each do |f|
21 if FileTest.executable? f
24 # Try setting the config entry
25 config_par = {:key => 'fortune.path', :value => [f], :silent => true }
26 debug "Setting fortune.path to #{f}"
27 set_path = @bot.plugins['config'].handle_set(m, config_par)
29 debug "fortune.path set to #{@bot.config['fortune.path']}"
31 debug "couldn't set fortune.path"
38 m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
41 ret = Utils.safe_exec(fortune, "-n", "350", "-s", db)
44 ret = ret.split(/\n+/).map do |l|
45 # check if this is a " -- Some Dood" line
46 if l =~ /^\s+-{1,3}\s+\w/
48 l.gsub!(/^\s+-\s/, '-- ')
52 # just remove leading and trailing whitespace
58 ret = "failed to execute fortune"
59 # TODO reset fortune.path when execution fails
66 # Print the fortune categories
67 def categories(m, params)
68 ## list all fortune files in /usr/share/games/fortune
69 categories = Dir["/usr/share/games/fortune/*"].select{|f|File.split(f).last.match /^\w+$/}.select{|f|File.file? f}.map{|p|File.split(p).last}.sort
71 m.reply "Fortune categories: #{categories.join ', '}"
75 plugin = FortunePlugin.new
76 plugin.map 'fortune categories', :action => "categories"
77 plugin.map 'fortune list', :action => "categories"
78 plugin.map 'fortune :db', :defaults => {:db => ''},
79 :requirements => {:db => /^[^-][\w-]+$/}