]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
url plugin: customizable max amount of data to retrieve to look for a title. return...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index 22a6dc18884b78f8c2236b712c586e216f961256..f85ccc13327672aab429e325792c7fd8a414ddb7 100644 (file)
@@ -1,29 +1,32 @@
 #-- vim:sw=2:et\r
 #++\r
 #\r
-# RSS feed plugin for RubyBot\r
-# (c) 2004 Stanislav Karchebny <berkus@madfire.net>\r
-# (c) 2005 Ian Monroe <ian@monroe.nu>\r
-# (c) 2005 Mark Kretschmann <markey@web.de>\r
-# (c) 2006 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\r
+# :title: RSS feed plugin for rbot\r
 #\r
-# Licensed under MIT License.\r
-\r
-require 'rss/parser'\r
-require 'rss/1.0'\r
-require 'rss/2.0'\r
-require 'rss/dublincore'\r
-# begin\r
-#   require 'rss/dublincore/2.0'\r
-# rescue\r
-#   warning "Unable to load RSS libraries, RSS plugin functionality crippled"\r
-# end\r
-\r
-class ::String\r
-  def riphtml\r
-    self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')\r
-  end\r
-end\r
+# Author:: Stanislav Karchebny <berkus@madfire.net>\r
+# Author:: Ian Monroe <ian@monroe.nu>\r
+# Author:: Mark Kretschmann <markey@web.de>\r
+# Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\r
+#\r
+# Copyright:: (C) 2004 Stanislav Karchebny\r
+# Copyright:: (C) 2005 Ian Monroe, Mark Kretschmann\r
+# Copyright:: (C) 2006-2007 Giuseppe Bilotta\r
+#\r
+# License:: MIT license\r
+\r
+# require 'rss/parser'\r
+# require 'rss/1.0'\r
+# require 'rss/2.0'\r
+# require 'rss/dublincore'\r
+# # begin\r
+# #   require 'rss/dublincore/2.0'\r
+# # rescue\r
+# #   warning "Unable to load RSS libraries, RSS plugin functionality crippled"\r
+# # end\r
+#\r
+# GB: Let's just go for the simple stuff:\r
+#\r
+require 'rss'\r
 \r
 class ::RssBlob\r
   attr_accessor :url\r
@@ -54,11 +57,13 @@ class ::RssBlob
   end\r
 \r
   def dup\r
-    self.class.new(@url,\r
-                   @handle,\r
-                   @type ? @type.dup : nil,\r
-                   @watchers.dup,\r
-                   @xml ? @xml.dup : nil)\r
+    @mutex.synchronize do\r
+      self.class.new(@url,\r
+                     @handle,\r
+                     @type ? @type.dup : nil,\r
+                     @watchers.dup,\r
+                     @xml ? @xml.dup : nil)\r
+    end\r
   end\r
 \r
   # Downcase all watchers, possibly turning them into Strings if they weren't\r
@@ -82,12 +87,16 @@ class ::RssBlob
     if watched_by?(who)\r
       return nil\r
     end\r
-    @watchers << who.downcase\r
+    @mutex.synchronize do\r
+      @watchers << who.downcase\r
+    end\r
     return who\r
   end\r
 \r
   def rm_watch(who)\r
-    @watchers.delete(who.downcase)\r
+    @mutex.synchronize do\r
+      @watchers.delete(who.downcase)\r
+    end\r
   end\r
 \r
   def to_a\r
@@ -152,12 +161,7 @@ class RSSFeedsPlugin < Plugin
   def save\r
     unparsed = Hash.new()\r
     @feeds.each { |k, f|\r
-      f.mutex.synchronize do\r
-        unparsed[k] = f.dup\r
-      end\r
-    }\r
-    unparsed.each { |k, f|\r
-      debug f.inspect\r
+      unparsed[k] = f.dup\r
     }\r
     @registry[:feeds] = unparsed\r
   end\r
@@ -187,7 +191,9 @@ class RSSFeedsPlugin < Plugin
     when "list"\r
       "rss list [#{Bold}handle#{Bold}] : list all rss feeds (matching #{Bold}handle#{Bold})"\r
     when "watched"\r
-      "rss watched [#{Bold}handle#{Bold}] : list all watched rss feeds (matching #{Bold}handle#{Bold})"\r
+      "rss watched [#{Bold}handle#{Bold}] [in #{Bold}chan#{Bold}]: list all watched rss feeds (matching #{Bold}handle#{Bold}) (in channel #{Bold}chan#{Bold})"\r
+    when "who", "watches", "who watches"\r
+      "rss who watches [#{Bold}handle#{Bold}]]: list all watchers for rss feeds (matching #{Bold}handle#{Bold})"\r
     when "add"\r
       "rss add #{Bold}handle#{Bold} #{Bold}url#{Bold} [#{Bold}type#{Bold}] : add a new rss called #{Bold}handle#{Bold} from url #{Bold}url#{Bold} (of type #{Bold}type#{Bold})"\r
     when "change"\r
@@ -199,9 +205,9 @@ class RSSFeedsPlugin < Plugin
     when "forcereplace"\r
       "rss forcereplace #{Bold}handle#{Bold} #{Bold}url#{Bold} [#{Bold}type#{Bold}] : replace the url of rss called #{Bold}handle#{Bold} with #{Bold}url#{Bold} (of type #{Bold}type#{Bold})"\r
     when "watch"\r
-      "rss watch #{Bold}handle#{Bold} [#{Bold}url#{Bold} [#{Bold}type#{Bold}]] : watch rss #{Bold}handle#{Bold} for changes; when the other parameters are present, it will be created if it doesn't exist yet"\r
+      "rss watch #{Bold}handle#{Bold} [#{Bold}url#{Bold} [#{Bold}type#{Bold}]]  [in #{Bold}chan#{Bold}]: watch rss #{Bold}handle#{Bold} for changes (in channel #{Bold}chan#{Bold}); when the other parameters are present, the feed will be created if it doesn't exist yet"\r
     when /(un|rm)watch/\r
-      "rss unwatch|rmwatch #{Bold}handle#{Bold} : stop watching rss #{Bold}handle#{Bold} for changes"\r
+      "rss unwatch|rmwatch #{Bold}handle#{Bold} [in #{Bold}chan#{Bold}]: stop watching rss #{Bold}handle#{Bold} (in channel #{Bold}chan#{Bold}) for changes"\r
     when "rewatch"\r
       "rss rewatch : restart threads that watch for changes in watched rss"\r
     else\r
@@ -298,10 +304,11 @@ class RSSFeedsPlugin < Plugin
 \r
   def watched_rss(m, params)\r
     wanted = params[:handle]\r
+    chan = params[:chan] || m.replyto\r
     reply = String.new\r
     watchlist.each { |handle, feed|\r
       next if wanted and !handle.match(/#{wanted}/i)\r
-      next unless feed.watched_by?(m.replyto)\r
+      next unless feed.watched_by?(chan)\r
       reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"\r
       (reply << " refreshing every #{Utils.secs_to_string(feed.refresh_rate)}") if feed.refresh_rate\r
       reply << "\n"\r
@@ -313,6 +320,23 @@ class RSSFeedsPlugin < Plugin
     m.reply reply\r
   end\r
 \r
+  def who_watches(m, params)\r
+    wanted = params[:handle]\r
+    reply = String.new\r
+    watchlist.each { |handle, feed|\r
+      next if wanted and !handle.match(/#{wanted}/i)\r
+      reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"\r
+      (reply << " refreshing every #{Utils.secs_to_string(feed.refresh_rate)}") if feed.refresh_rate\r
+      reply << ": watched by #{feed.watchers.join(', ')}"\r
+      reply << "\n"\r
+    }\r
+    if reply.empty?\r
+      reply = "no watched feeds"\r
+      reply << " matching #{wanted}" if wanted\r
+    end\r
+    m.reply reply\r
+  end\r
+\r
   def add_rss(m, params, force=false)\r
     handle = params[:handle]\r
     url = params[:url]\r
@@ -341,7 +365,10 @@ class RSSFeedsPlugin < Plugin
   def change_rss(m, params)\r
     handle = params[:handle].downcase\r
     feed = @feeds.fetch(handle, nil)\r
-    return m.reply "No such feed with handle #{handle}" unless feed\r
+    unless feed\r
+      m.reply "No such feed with handle #{handle}"\r
+      return\r
+    end\r
     case params[:what].intern\r
     when :handle\r
       new = params[:new].downcase\r
@@ -410,6 +437,7 @@ class RSSFeedsPlugin < Plugin
 \r
   def watch_rss(m, params)\r
     handle = params[:handle]\r
+    chan = params[:chan] || m.replyto\r
     url = params[:url]\r
     type = params[:type]\r
     if url\r
@@ -417,11 +445,11 @@ class RSSFeedsPlugin < Plugin
     end\r
     feed = @feeds.fetch(handle.downcase, nil)\r
     if feed\r
-      if feed.add_watch(m.replyto)\r
+      if feed.add_watch(chan)\r
         watchRss(feed, m)\r
         m.okay\r
       else\r
-        m.reply "Already watching #{feed.handle}"\r
+        m.reply "Already watching #{feed.handle} in #{chan}"\r
       end\r
     else\r
       m.reply "Couldn't watch feed #{handle} (no such feed found)"\r
@@ -430,15 +458,16 @@ class RSSFeedsPlugin < Plugin
 \r
   def unwatch_rss(m, params, pass=false)\r
     handle = params[:handle].downcase\r
+    chan = params[:chan] || m.replyto\r
     unless @feeds.has_key?(handle)\r
       m.reply("dunno that feed")\r
       return\r
     end\r
     feed = @feeds[handle]\r
-    if feed.rm_watch(m.replyto)\r
-      m.reply "#{m.replyto} has been removed from the watchlist for #{feed.handle}"\r
+    if feed.rm_watch(chan)\r
+      m.reply "#{chan} has been removed from the watchlist for #{feed.handle}"\r
     else\r
-      m.reply("#{m.replyto} wasn't watching #{feed.handle}") unless pass\r
+      m.reply("#{chan} wasn't watching #{feed.handle}") unless pass\r
     end\r
     if !feed.watched?\r
       stop_watch(handle)\r
@@ -595,6 +624,11 @@ class RSSFeedsPlugin < Plugin
       report_problem("reading feed #{feed} failed", nil, m)\r
       return nil\r
     end\r
+    # Ok, 0.9 feeds are not supported, maybe because\r
+    # Netscape happily removed the DTD. So what we do is just to\r
+    # reassign the 0.9 RDFs to 1.0, and hope it goes right.\r
+    xml.gsub!("xmlns=\"http://my.netscape.com/rdf/simple/0.9/\"",\r
+              "xmlns=\"http://purl.org/rss/1.0/\"")\r
     feed.mutex.synchronize do\r
       feed.xml = xml\r
     end\r
@@ -608,11 +642,12 @@ class RSSFeedsPlugin < Plugin
       begin\r
         ## do validate parse\r
         rss = RSS::Parser.parse(xml)\r
-        debug "parsed #{feed}"\r
+        debug "parsed and validated #{feed}"\r
       rescue RSS::InvalidRSSError\r
         ## do non validate parse for invalid RSS 1.0\r
         begin\r
           rss = RSS::Parser.parse(xml, false)\r
+          debug "parsed but not validated #{feed}"\r
         rescue RSS::Error => e\r
           report_problem("parsing rss stream failed, whoops =(", e, m)\r
           return nil\r
@@ -661,10 +696,13 @@ plugin.map 'rss show :handle :limit',
   :defaults => {:limit => 5}\r
 plugin.map 'rss list :handle',\r
   :action => 'list_rss',\r
-  :defaults =>  {:handle => nil}\r
-plugin.map 'rss watched :handle',\r
+  :defaults => {:handle => nil}\r
+plugin.map 'rss watched :handle [in :chan]',\r
   :action => 'watched_rss',\r
-  :defaults =>  {:handle => nil}\r
+  :defaults => {:handle => nil}\r
+plugin.map 'rss who watches :handle',\r
+  :action => 'who_watches',\r
+  :defaults => {:handle => nil}\r
 plugin.map 'rss add :handle :url :type',\r
   :action => 'add_rss',\r
   :defaults => {:type => nil}\r
@@ -686,12 +724,12 @@ plugin.map 'rss replace :handle :url :type',
 plugin.map 'rss forcereplace :handle :url :type',\r
   :action => 'forcereplace_rss',\r
   :defaults => {:type => nil}\r
-plugin.map 'rss watch :handle :url :type',\r
+plugin.map 'rss watch :handle :url :type [in :chan]',\r
   :action => 'watch_rss',\r
   :defaults => {:url => nil, :type => nil}\r
-plugin.map 'rss unwatch :handle',\r
+plugin.map 'rss unwatch :handle [in :chan]',\r
   :action => 'unwatch_rss'\r
-plugin.map 'rss rmwatch :handle',\r
+plugin.map 'rss rmwatch :handle [in :chan]',\r
   :action => 'unwatch_rss'\r
 plugin.map 'rss rewatch',\r
   :action => 'rewatch_rss'\r