]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
Fix misc RSS stuff: reverse publishing order of watched feeds, correct a config optio...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index fcea9197b67a77d04091ba797dfb3c94038ba851..e276d0a44bc3343d208e1a19f8e34e6b37e28323 100644 (file)
@@ -1,7 +1,12 @@
+#-- 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
+#\r
 # Licensed under MIT License.\r
 \r
 require 'rss/parser'\r
@@ -54,19 +59,28 @@ class ::RssBlob
   end\r
 \r
   def watched_by?(who)\r
-    @watchers.include?(who)\r
+    # We need to check bot 'who' itself and the String form, because rss\r
+    # watches added before the new Irc framework represented watchers as\r
+    # Strings whereas they are now Channels.\r
+    #\r
+    @watchers.include?(who) || @watchers.include?(who.to_s) \r
   end\r
 \r
   def add_watch(who)\r
     if watched_by?(who)\r
       return nil\r
     end\r
-    @watchers << who unless watched_by?(who)\r
+    # TODO FIXME? should we just store watchers as Strings instead?\r
+    # This should then be @watchers << who.downcase\r
+    @watchers << who\r
     return who\r
   end\r
 \r
   def rm_watch(who)\r
+    # See comment to watched_by?\r
+    #\r
     @watchers.delete(who)\r
+    @watchers.delete(who.to_s)\r
   end\r
 \r
   def to_a\r
@@ -94,7 +108,7 @@ class RSSFeedsPlugin < Plugin
 \r
   BotConfig.register BotConfigIntegerValue.new('rss.thread_sleep',\r
     :default => 300, :validate => Proc.new{|v| v > 30},\r
-    :desc => "How many characters to use of a RSS item text")\r
+    :desc => "How many seconds to sleep before checking RSS feeds again")\r
 \r
   @@watchThreads = Hash.new\r
   @@mutex = Mutex.new\r
@@ -210,9 +224,17 @@ class RSSFeedsPlugin < Plugin
     end\r
   end\r
 \r
+  def itemDate(item,ex=nil)\r
+    return item.pubDate if item.respond_to?(:pubDate)\r
+    return item.date if item.respond_to?(:date)\r
+    return ex\r
+  end\r
+\r
   def freshness_sort(items)\r
     notime = Time.at(0)\r
-    items.sort { |a, b| (b.pubDate || notime) <=> (a.pubDate || notime) }\r
+    items.sort { |a, b|\r
+      itemDate(b, notime) <=> itemDate(a, notime)\r
+    }\r
   end\r
 \r
   def list_rss(m, params)\r
@@ -355,7 +377,7 @@ class RSSFeedsPlugin < Plugin
     return feed\r
   end\r
 \r
-  def rewatch_rss(m=nil)\r
+  def rewatch_rss(m=nil, params=nil)\r
     kill_threads\r
 \r
     # Read watches from list.\r
@@ -398,7 +420,8 @@ class RSSFeedsPlugin < Plugin
               }\r
               if dispItems.length > 0\r
                 debug "Found #{dispItems.length} new items in #{feed}"\r
-                dispItems.each { |item|\r
+                # When displaying watched feeds, publish them from older to newer\r
+                dispItems.reverse.each { |item|\r
                   @@mutex.synchronize {\r
                     printFormattedRss(feed, item)\r
                   }\r
@@ -430,7 +453,24 @@ class RSSFeedsPlugin < Plugin
     if opts\r
       places = opts[:places] if opts.key?(:places)\r
       handle = opts[:handle].to_s if opts.key?(:handle)\r
-      date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S") + " :: " if (opts.key?(:date) && opts[:date] && item.pubDate)\r
+      if opts.key?(:date) && opts[:date]\r
+        if item.respond_to?(:pubDate) \r
+          if item.pubDate.class <= Time\r
+            date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S")\r
+          else\r
+            date = item.pubDate.to_s\r
+          end\r
+        elsif  item.respond_to?(:date)\r
+          if item.date.class <= Time\r
+            date = item.date.strftime("%Y/%m/%d %H.%M.%S")\r
+          else\r
+            date = item.date.to_s\r
+          end\r
+        else\r
+          date = "(no date)"\r
+        end\r
+        date += " :: "\r
+      end\r
     end\r
     title = "#{Bold}#{item.title.chomp.riphtml}#{Bold}" if item.title\r
     desc = item.description.gsub(/\s+/,' ').strip.riphtml.shorten(@bot.config['rss.text_max']) if item.description\r
@@ -460,7 +500,9 @@ class RSSFeedsPlugin < Plugin
   def fetchRss(feed, m=nil)\r
     begin\r
       # Use 60 sec timeout, cause the default is too low\r
-      xml = @bot.httputil.get_cached(feed.url,60,60)\r
+      # Do not use get_cached for RSS until we have proper cache handling\r
+      # xml = @bot.httputil.get_cached(feed.url,60,60)\r
+      xml = @bot.httputil.get(feed.url,60,60)\r
     rescue URI::InvalidURIError, URI::BadURIError => e\r
       report_problem("invalid rss feed #{feed.url}", e, m)\r
       return\r
@@ -553,6 +595,6 @@ plugin.map 'rss unwatch :handle',
   :action => 'unwatch_rss'\r
 plugin.map 'rss rmwatch :handle',\r
   :action => 'unwatch_rss'\r
-plugin.map 'rss rewatch :handle',\r
+plugin.map 'rss rewatch',\r
   :action => 'rewatch_rss'\r
 \r