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'
23 attr_accessor :num, :text, :vote
25 def initialize(num, text, vote)
32 "http://www.bash.org/?#{@num}"
37 class BashPlugin < Plugin
39 BotConfig.register BotConfigEnumValue.new('bash.access',
40 :values => ['xml', 'html'], :default => 'html',
41 :desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages")
44 def help(plugin, topic="")
45 "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)"
50 case @bot.config['bash.access'].intern
54 html_bash(m, :id => id)
59 esc = URI.escape(params[:words].to_s)
60 html = @bot.httputil.get("http://bash.org/?search=#{esc}")
61 html_bash(m, :html => html)
64 def html_bash(m, opts={})
72 html = @bot.httputil.get("http://bash.org/?latest")
74 html = @bot.httputil.get("http://bash.org/?random", :cache => false)
76 html = @bot.httputil.get("http://bash.org/?" + id)
80 html_quotes = html.split(/<p class="quote">/)
81 html_quotes.each { |htqt|
83 if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m)
87 quotes << BashQuote.new(num, text, vote)
93 m.reply "no quotes found"
98 # For the time being, we only echo the first quote, but in the future we
99 # may want to echo more than one for latest/random
102 # TODO: the gsub of br tags to | should be an ircify_html option
103 m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.text.gsub(/(?:<br \/>\s*)+/, ' | ').ircify_html]
106 def xml_bash(m, id=nil)
109 xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
111 xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false)
113 xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
117 m.reply "bash.org rss parse failed"
120 doc = Document.new xml
122 m.reply "bash.org rss parse failed"
125 doc.elements.each("*/item") {|e|
127 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
128 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
130 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
131 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
138 plugin = BashPlugin.new
140 plugin.map "bash search *words", :action => :search
141 plugin.map "bash [:id]"