+
+ def search(m, params)
+ esc = CGI.escape(params[:words].to_s)
+ html = @bot.httputil.get("http://bash.org/?search=#{esc}")
+ html_bash(m, :html => html)
+ end
+
+ def get_html_quotes(html)
+ quotes = []
+
+ 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
+ }
+ return quotes
+ end
+
+ def html_bash(m, opts={})
+ 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
+
+ if not html
+ m.reply "unable to retrieve quotes"
+ return
+ end
+
+ quotes = get_html_quotes(html)
+
+ 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
+ m.reply quote.to_s, :split_at => /\s+\|\s+/
+ 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
+