]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/bash.rb
webhook: gitlab support
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / bash.rb
index cb33a4d48353076c75c66a3d9610b0681fc5158c..8dac9671792b7b1c2e0375a751674464aac799ab 100644 (file)
@@ -32,6 +32,10 @@ class ::BashQuote
     "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
@@ -57,13 +61,40 @@ 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")
 
   include REXML
   def help(plugin, topic="")
-    "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)"
+    [
+      _("bash => print a random quote from bash.org"),
+      _("bash quote_id => print that quote id from bash.org"),
+      _("bash search <terms> => print the first bash.org quote matching <terms>"),
+      _("bash latest => print the latest quote from bash.org")
+    ].join(", ")
+  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)
@@ -82,9 +113,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(/<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]
@@ -98,16 +143,12 @@ class BashPlugin < Plugin
       end
     end
 
-    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
-    }
+    if not html
+      m.reply "unable to retrieve quotes"
+      return
+    end
+
+    quotes = get_html_quotes(html)
 
     case quotes.length
     when 0
@@ -120,7 +161,7 @@ class BashPlugin < Plugin
       # may want to echo more than one for latest/random
       quote = quotes.first
     end
-    m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.irc_text]
+    m.reply quote.to_s, :split_at => /\s+\|\s+/
   end
 
   def xml_bash(m, id=nil)
@@ -131,7 +172,7 @@ class BashPlugin < Plugin
       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        
+    end
 
     unless xml
       m.reply "bash.org rss parse failed"
@@ -143,7 +184,7 @@ class BashPlugin < Plugin
       return
     end
     doc.elements.each("*/item") {|e|
-      if(id != 0) 
+      if(id != 0)
         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
       else