diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-04-06 16:32:42 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-04-06 16:32:42 +0000 |
commit | e6e854d581090cc79855dc78be063eca5b28cbf4 (patch) | |
tree | 1aee7ef10e3eb09cca438eb28b683396af5f8437 | |
parent | 9efe37db63efbe8c2e360fed93382e59bf1b417b (diff) |
bash plugin: revamped and enhanced
-rw-r--r-- | data/rbot/plugins/bash.rb | 149 |
1 files changed, 117 insertions, 32 deletions
diff --git a/data/rbot/plugins/bash.rb b/data/rbot/plugins/bash.rb index 2a5bedad..520346be 100644 --- a/data/rbot/plugins/bash.rb +++ b/data/rbot/plugins/bash.rb @@ -1,37 +1,118 @@ -# bash.org xml plugin for rbot -# by Robin Kearney (robin@riviera.org.uk) +#-- vim:sw=2:et +#++ # -# its a bit of a quick hack, but it works for us :) +# :title: bash.org quote retrieval # +# Author:: Robin Kearney <robin@riviera.org.uk> +# Author:: cs +# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com> +# +# Copyright:: (C) 2005 Robin Kearney +# Copyright:: (C) 2007 cs, Giuseppe Bilotta +# +# License:: public domain +# +# TODO improve output of quote +# TODO show more than one quote +# TODO allow selection of only quotes with vote > 0 + require 'rexml/document' require 'uri/common' +class ::BashQuote + attr_accessor :num, :text, :vote + + def initialize(num, text, vote) + @num = num.to_i + @text = text + @vote = vote + end + + def url + "http://www.bash.org/?#{@num}" + end + +end + class BashPlugin < Plugin + + BotConfig.register BotConfigEnumValue.new('bash.access', + :values => ['xml', 'html'], :default => 'html', + :desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages") + include REXML def help(plugin, topic="") "bash => print a random quote from bash.org, bash quote_id => print that quote id from bash.org, bash latest => print the latest quote from bash.org (currently broken, need to get josh@bash.org to fix the xml)" end - def privmsg(m) - if m.params && m.params =~ /^([-\d]+)$/ - id = $1 - bash m, id - elsif(m.params == "latest") - bash m, id + + def bash(m, params) + id = params[:id] + case @bot.config['bash.access'].intern + when :xml + xml_bash(m, id) else - bash m + html_bash(m, :id => id) end end - - def bash(m, id=0) - - if(id != 0) - xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1") - elsif(id == "latest") - xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1") - else - xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", - :cache => false) - end + + def search(m, params) + esc = URI.escape(params[:words].to_s) + html = @bot.httputil.get("http://bash.org/?search=#{esc}") + html_bash(m, :html => html) + end + + def html_bash(m, opts={}) + quotes = [] + + html = opts[:html] + if not html + id = opts[:id] + case id + when 'latest' + html = @bot.httputil.get("http://bash.org/?latest") + when nil + html = @bot.httputil.get("http://bash.org/?random", :cache => false) + else + html = @bot.httputil.get("http://bash.org/?" + id) + end + end + + html_quotes = html.split(/<p class="quote">/) + html_quotes.each { |htqt| + # debug htqt.inspect + if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m) + num = $1 + vote = $2 + text = $3 + quotes << BashQuote.new(num, text, vote) + end + } + + case quotes.length + when 0 + m.reply "no quotes found" + return + when 1 + quote = quotes.first + else + # For the time being, we only echo the first quote, but in the future we + # may want to echo more than one for latest/random + quote = quotes.first + end + # TODO: the gsub of br tags to | should be an ircify_html option + m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.text.gsub(/(?:<br \/>\s*)+/, ' | ').ircify_html] + end + + def xml_bash(m, id=nil) + case id + when 'latest' + xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1") + when nil + xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false) + else + xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1") + end + unless xml m.reply "bash.org rss parse failed" return @@ -41,17 +122,21 @@ class BashPlugin < Plugin m.reply "bash.org rss parse failed" return end - doc.elements.each("*/item") {|e| - if(id != 0) - reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n" - reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n") - else - reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n" - reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n") - end - m.reply reply - } + doc.elements.each("*/item") {|e| + if(id != 0) + reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n" + reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n") + else + reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n" + reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n") + end + m.reply reply + } end end + plugin = BashPlugin.new -plugin.register("bash") + +plugin.map "bash search *words", :action => :search +plugin.map "bash [:id]" + |