]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/slashdot.rb
Fri Jul 29 13:07:56 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / slashdot.rb
index b09ac7a789706a477a88bfa19b3fce935295aed9..1a70de08c4ceb8091a1c6e4b8ee1a19384b3ab70 100644 (file)
@@ -6,23 +6,11 @@ class SlashdotPlugin < Plugin
   def help(plugin, topic="")
     "slashdot search <string> [<max>=4] => search slashdot for <string>, slashdot [<max>=4] => return up to <max> slashdot headlines (use negative max to return that many headlines, but all on one line.)"
   end
-  def privmsg(m)
-    if m.params && m.params =~ /^search\s+(.*)\s+(\d+)$/
-      search = $1
-      limit = $2.to_i
-      search_slashdot m, search, limit
-    elsif m.params && m.params =~ /^search\s+(.*)$/
-      search = $1
-      search_slashdot m, search
-    elsif m.params && m.params =~ /^([-\d]+)$/
-      limit = $1.to_i
-      slashdot m, limit
-    else
-      slashdot m
-    end
-  end
   
-  def search_slashdot(m, search, max=4)
+  def search_slashdot(m, params)
+   max = params[:limit].to_i
+   search = params[:search].to_s
+
     begin
       xml = @bot.httputil.get(URI.parse("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}"))
     rescue URI::InvalidURIError, URI::BadURIError => e
@@ -33,6 +21,7 @@ class SlashdotPlugin < Plugin
       m.reply "search for #{search} failed"
       return
     end
+    puts xml.inspect
     begin
       doc = Document.new xml
     rescue REXML::ParseException => e
@@ -44,6 +33,7 @@ class SlashdotPlugin < Plugin
       m.reply "search for #{search} failed"
       return
     end
+    puts doc.inspect
     max = 8 if max > 8
     done = 0
     doc.elements.each("*/item") {|e|
@@ -54,9 +44,15 @@ class SlashdotPlugin < Plugin
       done += 1
       break if done >= max
     }
+    unless done > 0
+      m.reply "search for #{search} failed"
+    end
   end
   
-  def slashdot(m, max=4)
+  def slashdot(m, params)
+    puts params.inspect
+    max = params[:limit].to_i
+    puts "max is #{max}"
     xml = @bot.httputil.get(URI.parse("http://slashdot.org/slashdot.xml"))
     unless xml
       m.reply "slashdot news parse failed"
@@ -92,4 +88,7 @@ class SlashdotPlugin < Plugin
   end
 end
 plugin = SlashdotPlugin.new
-plugin.register("slashdot")
+plugin.map 'slashdot search :limit *search', :action => 'search_slashdot',
+           :defaults => {:limit => 4}, :requirements => {:limit => /^-?\d+$/}
+plugin.map 'slashdot :limit', :defaults => {:limit => 4},
+                              :requirements => {:limit => /^-?\d+$/}