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, :irc_text, :vote
24 def initialize(num, text, vote)
28 @irc_text = mk_irc_text
32 "http://www.bash.org/?#{@num}"
36 "#%d (%d): %s" % [self.num, self.vote, self.irc_text]
45 debug "line: #{l.inspect}"
46 cur_nick = l.match(/^\s*(<.*?>|\(.*?\)|.*?:)\s/)[1] rescue nil
47 debug "nick: #{cur_nick.inspect}; last: #{last_nick.inspect}"
48 if cur_nick and cur_nick == last_nick
49 text << l.sub(cur_nick,"")
51 last_nick = cur_nick.dup if cur_nick
56 # TODO: the gsub of br tags to | should be an ircify_html option
57 text.gsub(/(?:<br \/>\s*)+/, ' | ').ircify_html
62 class BashPlugin < Plugin
64 Config.register Config::EnumValue.new('bash.access',
65 :values => ['xml', 'html'], :default => 'html',
66 :desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages")
69 def help(plugin, topic="")
70 "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)"
74 # check if we like the location of the page
75 loc = Utils.check_location(s, %r{http://(?:www\.)?bash\.org/\?})
77 # check if there are any quotes
78 quotes = get_html_quotes(s[:text])
79 return if quotes.empty?
80 title = s[:text].ircify_html_title
81 # return the first quote
84 :content => quotes.first.to_s,
85 :bash_quotes => quotes
92 @bot.register_filter(:bash, :htmlinfo) { |s| bash_filter(s) }
97 case @bot.config['bash.access'].intern
101 html_bash(m, :id => id)
105 def search(m, params)
106 esc = CGI.escape(params[:words].to_s)
107 html = @bot.httputil.get("http://bash.org/?search=#{esc}")
108 html_bash(m, :html => html)
111 def get_html_quotes(html)
114 html_quotes = html.split(/<p class="quote">/)
115 html_quotes.each { |htqt|
117 if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m)
121 quotes << BashQuote.new(num, text, vote)
127 def html_bash(m, opts={})
133 html = @bot.httputil.get("http://bash.org/?latest")
135 html = @bot.httputil.get("http://bash.org/?random", :cache => false)
137 html = @bot.httputil.get("http://bash.org/?" + id)
142 m.reply "unable to retrieve quotes"
146 quotes = get_html_quotes(html)
150 m.reply "no quotes found"
155 # For the time being, we only echo the first quote, but in the future we
156 # may want to echo more than one for latest/random
162 def xml_bash(m, id=nil)
165 xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
167 xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false)
169 xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
173 m.reply "bash.org rss parse failed"
176 doc = Document.new xml
178 m.reply "bash.org rss parse failed"
181 doc.elements.each("*/item") {|e|
183 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
184 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
186 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
187 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
194 plugin = BashPlugin.new
196 plugin.map "bash search *words", :action => :search
197 plugin.map "bash [:id]"