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="")
71 _("bash => print a random quote from bash.org"),
72 _("bash quote_id => print that quote id from bash.org"),
73 _("bash search <terms> => print the first bash.org quote matching <terms>"),
74 _("bash latest => print the latest quote from bash.org")
79 # check if we like the location of the page
80 loc = Utils.check_location(s, %r{http://(?:www\.)?bash\.org/\?})
82 # check if there are any quotes
83 quotes = get_html_quotes(s[:text])
84 return if quotes.empty?
85 title = s[:text].ircify_html_title
86 # return the first quote
89 :content => quotes.first.to_s,
90 :bash_quotes => quotes
97 @bot.register_filter(:bash, :htmlinfo) { |s| bash_filter(s) }
102 case @bot.config['bash.access'].intern
106 html_bash(m, :id => id)
110 def search(m, params)
111 esc = CGI.escape(params[:words].to_s)
112 html = @bot.httputil.get("http://bash.org/?search=#{esc}")
113 html_bash(m, :html => html)
116 def get_html_quotes(html)
119 html_quotes = html.split(/<p class="quote">/)
120 html_quotes.each { |htqt|
122 if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m)
126 quotes << BashQuote.new(num, text, vote)
132 def html_bash(m, opts={})
138 html = @bot.httputil.get("http://bash.org/?latest")
140 html = @bot.httputil.get("http://bash.org/?random", :cache => false)
142 html = @bot.httputil.get("http://bash.org/?" + id)
147 m.reply "unable to retrieve quotes"
151 quotes = get_html_quotes(html)
155 m.reply "no quotes found"
160 # For the time being, we only echo the first quote, but in the future we
161 # may want to echo more than one for latest/random
164 m.reply quote.to_s, :split_at => /\s+\|\s+/
167 def xml_bash(m, id=nil)
170 xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
172 xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false)
174 xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
178 m.reply "bash.org rss parse failed"
181 doc = Document.new xml
183 m.reply "bash.org rss parse failed"
186 doc.elements.each("*/item") {|e|
188 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
189 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
191 reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
192 reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
199 plugin = BashPlugin.new
201 plugin.map "bash search *words", :action => :search
202 plugin.map "bash [:id]"