]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/fortune.rb
script plugin: store channels as strings
[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
11   def help(plugin, topic="")
12     "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories"
13   end
14
15   def find_fortune
16     fortune = @bot.config['fortune.path']
17     return fortune if fortune
18
19     ["/usr/bin/fortune",
20      "/usr/share/bin/fortune",
21      "/usr/games/fortune",
22      "/usr/local/games/fortune",
23      "/usr/local/bin/fortune"].each do |f|
24       if FileTest.executable? f
25         fortune = f
26         break
27       end
28     end
29
30     return nil unless fortune
31       
32     # Try setting the config entry
33     config_par = {:key => 'fortune.path', :value => [fortune], :silent => true }
34     debug "Setting fortune.path to #{fortune}"
35     set_path = @bot.plugins['config'].handle_set(m, config_par)
36     if set_path
37       debug "fortune.path set to #{@bot.config['fortune.path']}"
38     else
39       debug "couldn't set fortune.path"
40     end
41
42     return fortune
43   end
44
45   ## Pick a fortune
46   def fortune(m, params)
47     db = params[:db]
48     fortune = find_fortune
49     m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
50
51     begin
52       ret = Utils.safe_exec(fortune, "-n", "350", "-s", db)
53
54       ## cleanup ret
55       ret = ret.split(/\n+/).map do |l|
56         # check if this is a "  -- Some Dood" line
57         if l =~ /^\s+-{1,3}\s+\w/
58           # turn "-" into "--"
59           l.gsub!(/^\s+-\s/, '-- ')
60           # extra space
61           " " + l.strip
62         else
63           # just remove leading and trailing whitespace
64           l.strip
65         end
66       end.join(" ")
67
68     rescue
69       ret = "failed to execute fortune"
70       # TODO reset fortune.path when execution fails
71     end
72
73     m.reply ret
74   end
75
76   # Print the fortune categories
77   def categories(m, params)
78     fortune = find_fortune
79     m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune
80
81     ## list all fortune databases
82     categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f|
83       f.split[1]
84     }.select{ |f|
85       f[0..0] != '/'
86     }.sort
87
88     ## say 'em!
89     m.reply "Fortune categories: #{categories.join ', '}"
90   end
91  
92 end
93 plugin = FortunePlugin.new
94 plugin.map 'fortune categories', :action => "categories"
95 plugin.map 'fortune list', :action => "categories"
96 plugin.map 'fortune :db', :defaults => {:db => ''},
97                           :requirements => {:db => /^[^-][\w-]+$/}