]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/fortune.rb
6612c11190f3554ec110feb175f1f9ba1ae2c1e2
[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     m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
52
53     command = [fortune] + @bot.config['fortune.options']
54     command << params[:db]
55     command.compact!
56
57     begin
58       ret = Utils.safe_exec(*command)
59
60       ## cleanup ret
61       ret = ret.split(/\n+/).map do |l|
62         # check if this is a "  -- Some Dood" line
63         if l =~ /^\s+-{1,3}\s+\w/
64           # turn "-" into "--"
65           l.gsub!(/^\s+-\s/, '-- ')
66           # extra space
67           " " + l.strip
68         else
69           # just remove leading and trailing whitespace
70           l.strip
71         end
72       end.join(" ")
73
74     rescue
75       ret = "failed to execute fortune"
76       # TODO reset fortune.path when execution fails
77     end
78
79     m.reply ret
80   end
81
82   # Print the fortune categories
83   def categories(m, params)
84     fortune = find_fortune(m)
85     m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
86
87     ## list all fortune databases
88     categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
89       f.split[1]
90     }.select{ |f|
91       f[0..0] != '/'
92     }.sort
93
94     ## say 'em!
95     m.reply "Fortune categories: #{categories.join ', '}"
96   end
97  
98 end
99 plugin = FortunePlugin.new
100 plugin.map 'fortune categories', :action => "categories"
101 plugin.map 'fortune list', :action => "categories"
102 plugin.map 'fortune :db', :defaults => {:db => ''},
103                           :requirements => {:db => /^[^-][\w-]+$/}