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 if not fortune or fortune.empty?
52 m.reply "fortune executable not found (try setting the 'fortune.path' variable)"
56 command = [fortune] + @bot.config['fortune.options']
57 command << params[:db]
61 ret = Utils.safe_exec(*command)
64 ret = ret.split(/\n+/).map do |l|
65 # check if this is a " -- Some Dood" line
66 if l =~ /^\s+-{1,3}\s+\w/
68 l.gsub!(/^\s+-\s/, '-- ')
72 # just remove leading and trailing whitespace
78 ret = "failed to execute fortune"
79 # TODO reset fortune.path when execution fails
85 # Print the fortune categories
86 def categories(m, params)
87 fortune = find_fortune(m)
88 if not fortune or fortune.empty?
89 m.reply "fortune executable not found (try setting the 'fortune.path' variable)"
93 ## list all fortune databases
94 categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
101 m.reply "Fortune categories: #{categories.join ', '}"
105 plugin = FortunePlugin.new
106 plugin.map 'fortune categories', :action => "categories"
107 plugin.map 'fortune list', :action => "categories"
108 plugin.map 'fortune :db', :defaults => {:db => ''},
109 :requirements => {:db => /^[^-][\w-]+$/}