diff options
author | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2008-06-23 18:39:12 +0200 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-06-23 19:46:47 +0200 |
commit | 0d9a8ad81eac5a3b8ee704cc8b4e586d1bc62d2b (patch) | |
tree | a6f80405bfdc10e0445a0530588d3f51d4568b47 /data/rbot | |
parent | e844032d80cac393c4ddf720bf359982fdc7c93a (diff) |
Properly find out the available fortune categories.
Instead of hardcoding the fortune categories path, ask fortune itself
to report which categories you can get the quotes from.
This fixes the fortune plugin on Gentoo for instance where the
databases are installed in /usr/share/fortune rather than
/usr/games/share/fortune.
Diffstat (limited to 'data/rbot')
-rw-r--r-- | data/rbot/plugins/fortune.rb | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/data/rbot/plugins/fortune.rb b/data/rbot/plugins/fortune.rb index adb6a839..79a900fd 100644 --- a/data/rbot/plugins/fortune.rb +++ b/data/rbot/plugins/fortune.rb @@ -12,34 +12,40 @@ class FortunePlugin < Plugin "fortune [<category>] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories" end - - ## Pick a fortune - def fortune(m, params) - db = params[:db] + def find_fortune fortune = @bot.config['fortune.path'] - if fortune.empty? - ["/usr/bin/fortune", - "/usr/share/bin/fortune", - "/usr/games/fortune", - "/usr/local/games/fortune", - "/usr/local/bin/fortune"].each do |f| - if FileTest.executable? f - fortune = f + return fortune if fortune - # Try setting the config entry - config_par = {:key => 'fortune.path', :value => [f], :silent => true } - debug "Setting fortune.path to #{f}" - set_path = @bot.plugins['config'].handle_set(m, config_par) - if set_path - debug "fortune.path set to #{@bot.config['fortune.path']}" - else - debug "couldn't set fortune.path" - end + ["/usr/bin/fortune", + "/usr/share/bin/fortune", + "/usr/games/fortune", + "/usr/local/games/fortune", + "/usr/local/bin/fortune"].each do |f| + if FileTest.executable? f + fortune = f + break + end + end - break - end - end + return nil unless fortune + + # Try setting the config entry + config_par = {:key => 'fortune.path', :value => [fortune], :silent => true } + debug "Setting fortune.path to #{fortune}" + set_path = @bot.plugins['config'].handle_set(m, config_par) + if set_path + debug "fortune.path set to #{@bot.config['fortune.path']}" + else + debug "couldn't set fortune.path" end + + return fortune + end + + ## Pick a fortune + def fortune(m, params) + db = params[:db] + fortune = find_fortune m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune begin @@ -67,17 +73,18 @@ class FortunePlugin < Plugin m.reply ret end - # Print the fortune categories def categories(m, params) - ## list all fortune files in /usr/share/games/fortune - categories = Dir["/usr/share/games/fortune/*"].select{ |f| - File.split(f).last.match(/^\w+$/) + fortune = find_fortune + m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune + + ## list all fortune databases + categories = Utils.safe_exec(fortune, "-f").split(/\n+ */).map{ |f| + f.split[1] }.select{ |f| - File.file?(f) - }.map{ |p| - File.split(p).last + f[0..0] != '/' }.sort + ## say 'em! m.reply "Fortune categories: #{categories.join ', '}" end |