]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/bash.rb
lart plugin: replace "me" with sourcenick
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / bash.rb
index 520346be4bc480f78575e5fe0df709234712a5d2..0524204a4f2ef549fe7d06d6b0090647f2e341b8 100644 (file)
 # TODO allow selection of only quotes with vote > 0
 
 require 'rexml/document'
-require 'uri/common'
 
 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
 
+  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(/(?:<br \/>\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")
 
@@ -56,7 +77,7 @@ class BashPlugin < Plugin
   end
 
   def search(m, params)
-    esc = URI.escape(params[:words].to_s)
+    esc = CGI.escape(params[:words].to_s)
     html = @bot.httputil.get("http://bash.org/?search=#{esc}")
     html_bash(m, :html => html)
   end
@@ -99,8 +120,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(/(?:<br \/>\s*)+/, ' | ').ircify_html]
+    m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.irc_text]
   end
 
   def xml_bash(m, id=nil)