X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ffortune.rb;h=adb6a839a656a0897387508d6792ed63792b222d;hb=0f277c32dd269937fbbe6427b416214ae70c70e2;hp=184b6b1382d344d9b51ef9e9003ee5a1ee021395;hpb=21949774b91eaec6ecde4eaa8ad121e2c0a36b87;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/fortune.rb b/data/rbot/plugins/fortune.rb index 184b6b13..adb6a839 100644 --- a/data/rbot/plugins/fortune.rb +++ b/data/rbot/plugins/fortune.rb @@ -1,22 +1,90 @@ +#-- vim:sw=2:et +#++ +# +# :title: Fortune plugin + class FortunePlugin < Plugin + Config.register Config::StringValue.new('fortune.path', + :default => '', + :desc => "Full path to the fortune executable") + def help(plugin, topic="") - "fortune [] => get a (short) fortune, optionally specifying fortune db" + "fortune [] => get a (short) fortune, optionally specifying fortune category || fortune categories => show categories" end + + + ## Pick a fortune def fortune(m, params) db = params[:db] - fortune = nil - ["/usr/games/fortune", "/usr/bin/fortune", "/usr/local/bin/fortune"].each {|f| - if FileTest.executable? f - fortune = f - break - end - } - m.reply "fortune binary not found" unless fortune - ret = Utils.safe_exec(fortune, "-n", "255", "-s", db) - m.reply ret.gsub(/\t/, " ").split(/\n/).join(" ") - return + 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 + + # 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 + + break + end + end + end + m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune + + begin + ret = Utils.safe_exec(fortune, "-n", "350", "-s", db) + + ## cleanup ret + ret = ret.split(/\n+/).map do |l| + # check if this is a " -- Some Dood" line + if l =~ /^\s+-{1,3}\s+\w/ + # turn "-" into "--" + l.gsub!(/^\s+-\s/, '-- ') + # extra space + " " + l.strip + else + # just remove leading and trailing whitespace + l.strip + end + end.join(" ") + + rescue + ret = "failed to execute fortune" + # TODO reset fortune.path when execution fails + end + + 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 + ## say 'em! + m.reply "Fortune categories: #{categories.join ', '}" + end + end plugin = FortunePlugin.new -plugin.map 'fortune :db', :defaults => {:db => 'fortunes'}, +plugin.map 'fortune categories', :action => "categories" +plugin.map 'fortune list', :action => "categories" +plugin.map 'fortune :db', :defaults => {:db => ''}, :requirements => {:db => /^[^-][\w-]+$/}