4 # :title: bash.org quote retrieval
6 # Author:: Robin Kearney <robin@riviera.org.uk>
8 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
10 # Copyright:: (C) 2005 Robin Kearney
11 # Copyright:: (C) 2007 cs, Giuseppe Bilotta
13 # License:: public domain
15 # TODO improve output of quote
16 # TODO show more than one quote
17 # TODO allow selection of only quotes with vote > 0
19 require 'rexml/document'
22 attr_accessor :num, :text, :vote
24 def initialize(num, text, vote)
31 "http://www.bash.org/?#{@num}"
36 class BashPlugin < Plugin
38 BotConfig.register BotConfigEnumValue.new('bash.access',
39 :values => ['xml', 'html'], :default => 'html',
40 :desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages")
43 def help(plugin, topic="")
44 "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)"
49 case @bot.config['bash.access'].intern
53 html_bash(m, :id => id)
58 esc = CGI.escape(params[:words].to_s)
59 html = @bot.httputil.get("http://bash.org/?search=#{esc}")
60 html_bash(m, :html => html)
63 def html_bash(m, opts={})
71 html = @bot.httputil.get("http://bash.org/?latest")
73 html = @bot.httputil.get("http://bash.org/?random", :cache => false)
75 html = @bot.httputil.get("http://bash.org/?" + id)
79 html_quotes = html.split(/<p class="quote">/)
80 html_quotes.each { |htqt|
82 if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m)
86 quotes << BashQuote.new(num, text, vote)
92 m.reply "no quotes found"
97 # For the time being, we only echo the first quote, but in the future we
98 # may want to echo more than one for latest/random
101 # TODO: the gsub of br tags to | should be an ircify_html option
102 m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.text.gsub(/(?:<br \/>\s*)+/, ' | ').ircify_html]
105 def xml_bash(m, id=nil)
108 xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
110 xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false)
112 xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
116 m.reply "bash.org rss parse failed"
119 doc = Document.new xml
121 m.reply "bash.org rss parse failed"
124 doc.elements.each("*/item") {|e|
126 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
127 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
129 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
130 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
137 plugin = BashPlugin.new
139 plugin.map "bash search *words", :action => :search
140 plugin.map "bash [:id]"