+
+ def bash_filter(s)
+ # check if we like the location of the page
+ loc = Utils.check_location(s, %r{http://(?:www\.)?bash\.org/\?})
+ return unless loc
+ # check if there are any quotes
+ quotes = get_html_quotes(s[:text])
+ return if quotes.empty?
+ title = s[:text].ircify_html_title
+ # return the first quote
+ return {
+ :title => title,
+ :content => quotes.first.to_s,
+ :bash_quotes => quotes
+ }
+ end
+
+ def initialize
+ super
+
+ @bot.register_filter(:bash, :htmlinfo) { |s| bash_filter(s) }
+ end
+
+ def bash(m, params)
+ id = params[:id]
+ case @bot.config['bash.access'].intern
+ when :xml
+ xml_bash(m, id)
+ else
+ html_bash(m, :id => id)
+ end
+ end
+
+ 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