X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ffortune.rb;h=3e2ffdf719a7b7dbd1cfa729bc280c0d1cb3fb7e;hb=efbd4bea3ef18d2a3649dc7a5159e0e910ba7149;hp=e530ae3e84c82d5067e3efa5f3748d30b9db9f86;hpb=72c8aadb6e251a8d956259a572261781c1a45289;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/fortune.rb b/data/rbot/plugins/fortune.rb index e530ae3e..3e2ffdf7 100644 --- a/data/rbot/plugins/fortune.rb +++ b/data/rbot/plugins/fortune.rb @@ -1,49 +1,103 @@ +#-- 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") + Config.register Config::ArrayValue.new('fortune.options', + :default => ['-n', '350', '-s'], + :desc => "Options to be passed on to fortune") 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 - def fortune(m, params) - db = params[:db] + + def find_fortune(m) fortune = @bot.config['fortune.path'] - if fortune.empty? - ["/usr/share/games/fortune", - "/usr/share/bin/fortune", - "/usr/games/fortune", - "/usr/bin/fortune", - "/usr/local/games/fortune", - "/usr/local/bin/fortune"].each {|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 - } + return fortune if fortune and not 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 + break + end end - m.reply "fortune binary not found" unless fortune + + 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) + fortune = find_fortune(m) + m.reply "fortune executable not found (try setting the 'fortune.path' variable)" unless fortune + + command = [fortune] + @bot.config['fortune.options'] + command << params[:db] + command.compact! + begin - ret = Utils.safe_exec(fortune, "-n", "255", "-s", db) + ret = Utils.safe_exec(*command) + + ## 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.gsub(/\t/, " ").split(/\n/).join(" ") - return + + m.reply ret end + + # Print the fortune categories + def categories(m, params) + fortune = find_fortune(m) + 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 + 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-]+$/}