X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ffortune.rb;h=79a900fdd38c0ebba10d53229ade2b7f12255cc8;hb=22e6cefa54de681b131ecb97fc9383ff5e990dfe;hp=467d452dffce28521da225d3b40425f813f6c033;hpb=0109f67cc75ceded351b0023c6fb77d9bcccbcd1;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/fortune.rb b/data/rbot/plugins/fortune.rb index 467d452d..79a900fd 100644 --- a/data/rbot/plugins/fortune.rb +++ b/data/rbot/plugins/fortune.rb @@ -1,5 +1,10 @@ +#-- vim:sw=2:et +#++ +# +# :title: Fortune plugin + class FortunePlugin < Plugin - BotConfig.register BotConfigStringValue.new('fortune.path', + Config.register Config::StringValue.new('fortune.path', :default => '', :desc => "Full path to the fortune executable") @@ -7,34 +12,40 @@ class FortunePlugin < Plugin "fortune [] => 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 @@ -62,11 +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+$/}.select{|f|File.file? f}.map{|p|File.split(p).last}.sort + 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| + f[0..0] != '/' + }.sort + ## say 'em! m.reply "Fortune categories: #{categories.join ', '}" end