X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fbash.rb;h=2d52a5ecd09b4d3bb8abb87b44fb3a89d7014c08;hb=1c6b09968776c94b812317dfc4f91f09b5f0817c;hp=93d9c3f2af77f54844bb77b2edd7b79f3dbd4f79;hpb=109fa2a5b63af113df2c6b21d44135efa0d94d70;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git
diff --git a/data/rbot/plugins/bash.rb b/data/rbot/plugins/bash.rb
index 93d9c3f2..2d52a5ec 100644
--- a/data/rbot/plugins/bash.rb
+++ b/data/rbot/plugins/bash.rb
@@ -19,23 +19,49 @@
require 'rexml/document'
class ::BashQuote
- attr_accessor :num, :text, :vote
+ attr_accessor :num, :text, :irc_text, :vote
def initialize(num, text, vote)
@num = num.to_i
@text = text
@vote = vote
+ @irc_text = mk_irc_text
end
def url
"http://www.bash.org/?#{@num}"
end
+ def to_s
+ "#%d (%d): %s" % [self.num, self.vote, self.irc_text]
+ end
+
+ private
+ def mk_irc_text
+ cur_nick = nil
+ last_nick = nil
+ text = String.new
+ @text.each_line { |l|
+ debug "line: #{l.inspect}"
+ cur_nick = l.match(/^\s*(<.*?>|\(.*?\)|.*?:)\s/)[1] rescue nil
+ debug "nick: #{cur_nick.inspect}; last: #{last_nick.inspect}"
+ if cur_nick and cur_nick == last_nick
+ text << l.sub(cur_nick,"")
+ else
+ last_nick = cur_nick.dup if cur_nick
+ text << l
+ end
+ }
+ debug text
+ # TODO: the gsub of br tags to | should be an ircify_html option
+ text.gsub(/(?:
\s*)+/, ' | ').ircify_html
+ end
+
end
class BashPlugin < Plugin
- BotConfig.register BotConfigEnumValue.new('bash.access',
+ Config.register Config::EnumValue.new('bash.access',
:values => ['xml', 'html'], :default => 'html',
:desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages")
@@ -44,6 +70,28 @@ class BashPlugin < Plugin
"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)"
end
+ 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
@@ -60,9 +108,23 @@ class BashPlugin < Plugin
html_bash(m, :html => html)
end
- def html_bash(m, opts={})
+ def get_html_quotes(html)
quotes = []
+ html_quotes = html.split(/
/)
+ html_quotes.each { |htqt|
+ # debug htqt.inspect
+ if htqt.match(/]*>.*?\((-?\d+)\).*? (.*)<\/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]
@@ -76,16 +138,12 @@ class BashPlugin < Plugin
end
end
- html_quotes = html.split(/ /)
- html_quotes.each { |htqt|
- # debug htqt.inspect
- if htqt.match(/]*>.*?\((-?\d+)\).*? (.*)<\/p>\s+(?:<\/td>.*)?\z/m)
- num = $1
- vote = $2
- text = $3
- quotes << BashQuote.new(num, text, vote)
- end
- }
+ if not html
+ m.reply "unable to retrieve quotes"
+ return
+ end
+
+ quotes = get_html_quotes(html)
case quotes.length
when 0
@@ -98,8 +156,7 @@ class BashPlugin < Plugin
# may want to echo more than one for latest/random
quote = quotes.first
end
- # TODO: the gsub of br tags to | should be an ircify_html option
- m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.text.gsub(/(?:
\s*)+/, ' | ').ircify_html]
+ m.reply quote.to_s
end
def xml_bash(m, id=nil)