]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/fortune.rb
plugin(search): fix search and gcalc, closes #28, #29
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / fortune.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Fortune plugin
5
6 class FortunePlugin < Plugin
7   Config.register Config::StringValue.new('fortune.path',
8     :default => '',
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")
13
14   def help(plugin, topic="")
15     "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories"
16   end
17
18   def find_fortune(m)
19     fortune = @bot.config['fortune.path']
20     return fortune if fortune and not fortune.empty?
21
22     ["/usr/bin/fortune",
23      "/usr/share/bin/fortune",
24      "/usr/games/fortune",
25      "/usr/local/games/fortune",
26      "/usr/local/bin/fortune"].each do |f|
27       if FileTest.executable? f
28         fortune = f
29         break
30       end
31     end
32
33     return nil unless fortune
34
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)
39     if set_path
40       debug "fortune.path set to #{@bot.config['fortune.path']}"
41     else
42       debug "couldn't set fortune.path"
43     end
44
45     return fortune
46   end
47
48   ## Pick a fortune
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)"
53       return
54     end
55
56     command = [fortune] + @bot.config['fortune.options']
57     command << params[:db]
58     command.compact!
59
60     begin
61       ret = Utils.safe_exec(*command)
62
63       ## cleanup ret
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/
67           # turn "-" into "--"
68           l.gsub!(/^\s+-\s/, '-- ')
69           # extra space
70           " " + l.strip
71         else
72           # just remove leading and trailing whitespace
73           l.strip
74         end
75       end.join(" ")
76
77     rescue
78       ret = "failed to execute fortune"
79       # TODO reset fortune.path when execution fails
80     end
81
82     m.reply ret
83   end
84
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)"
90       return
91     end
92
93     ## list all fortune databases
94     categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
95       f.split[1]
96     }.select{ |f|
97       f[0..0] != '/'
98     }.sort
99
100     ## say 'em!
101     m.reply "Fortune categories: #{categories.join ', '}"
102   end
103
104 end
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-]+$/}