]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
lart plugin: replace "me" with sourcenick
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index 5a87e7893cdf19c59b25a70df9dd29ab293ab8cd..f8d8c99795647db4672edbd791ee2675c012eb15 100644 (file)
@@ -24,8 +24,13 @@ module ::RSS
   #\r
   def RSS.item_uid_for_bot(item, opts={})\r
     options = { :show_updated => true}.merge(opts)\r
-    desc = options[:show_updated] ? item.description : nil\r
-    [item.title, item.link, desc].hash\r
+    desc = nil\r
+    if options[:show_updated]\r
+      desc = item.content.content rescue item.description rescue nil\r
+    end\r
+    [(item.title.content rescue item.title rescue nil),\r
+     (item.link.href rescue item.link),\r
+     desc].hash\r
   end\r
 \r
   # Add support for Slashdot namespace in RDF. The code is just an adaptation\r
@@ -157,17 +162,10 @@ end
 \r
 \r
 class ::RssBlob\r
-  attr_accessor :url\r
-  attr_accessor :handle\r
-  attr_accessor :type\r
-  attr :watchers\r
-  attr_accessor :refresh_rate\r
-  attr_accessor :xml\r
-  attr_accessor :title\r
-  attr_accessor :items\r
-  attr_accessor :mutex\r
-\r
-  def initialize(url,handle=nil,type=nil,watchers=[], xml=nil)\r
+  attr_accessor :url, :handle, :type, :refresh_rate, :xml, :title, :items,\r
+    :mutex, :watchers, :last_fetched\r
+\r
+  def initialize(url,handle=nil,type=nil,watchers=[], xml=nil, lf = nil)\r
     @url = url\r
     if handle\r
       @handle = handle\r
@@ -181,6 +179,7 @@ class ::RssBlob
     @title = nil\r
     @items = nil\r
     @mutex = Mutex.new\r
+    @last_fetched = lf\r
     sanitize_watchers(watchers)\r
   end\r
 \r
@@ -190,7 +189,8 @@ class ::RssBlob
                      @handle,\r
                      @type ? @type.dup : nil,\r
                      @watchers.dup,\r
-                     @xml ? @xml.dup : nil)\r
+                     @xml ? @xml.dup : nil,\r
+                     @last_fetched)\r
     end\r
   end\r
 \r
@@ -283,15 +283,15 @@ class RSSFeedsPlugin < Plugin
       # the restore to work.\r
       #\r
       # This is actually pretty safe for a number of reasons:\r
-      #  * the code is only called if standard marshalling fails\r
-      #  * the string we look for is quite unlikely to appear randomly\r
-      #  * if the string appears somewhere and the patched string isn't recoverable\r
-      #    either, we'll get another (unrecoverable) error, which makes the rss\r
-      #    plugin unsable, just like it was if no recovery was attempted\r
-      #  * if the string appears somewhere and the patched string is recoverable,\r
-      #    we may get a b0rked feed, which is eventually overwritten by a clean\r
-      #    one, so the worst thing that can happen is that a feed update spams\r
-      #    the watchers once\r
+      # * the code is only called if standard marshalling fails\r
+      # * the string we look for is quite unlikely to appear randomly\r
+      # * if the string appears somewhere and the patched string isn't recoverable\r
+      #   either, we'll get another (unrecoverable) error, which makes the rss\r
+      #   plugin unsable, just like it was if no recovery was attempted\r
+      # * if the string appears somewhere and the patched string is recoverable,\r
+      #   we may get a b0rked feed, which is eventually overwritten by a clean\r
+      #   one, so the worst thing that can happen is that a feed update spams\r
+      #   the watchers once\r
       @registry.recovery = Proc.new { |val|\r
         patched = val.sub(":\v@mutexo:\nMutex", ":\v@mutexo:\vObject")\r
         ret = Marshal.restore(patched)\r
@@ -387,10 +387,12 @@ class RSSFeedsPlugin < Plugin
       "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} [in #{Bold}chan#{Bold}]: stop watching rss #{Bold}handle#{Bold} (in channel #{Bold}chan#{Bold}) for changes"\r
+    when  /who(?: watche?s?)?/\r
+      "rss who watches #{Bold}handle#{Bold}: lists watches for rss #{Bold}handle#{Bold}"\r
     when "rewatch"\r
       "rss rewatch : restart threads that watch for changes in watched rss"\r
     else\r
-      "manage RSS feeds: rss show|list|watched|add|change|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch"\r
+      "manage RSS feeds: rss show|list|watched|add|change|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch|who watches"\r
     end\r
   end\r
 \r
@@ -674,13 +676,23 @@ class RSSFeedsPlugin < Plugin
   end\r
 \r
   def rewatch_rss(m=nil, params=nil)\r
-    stop_watches\r
+    if params and handle = params[:handle]\r
+      feed = @feeds.fetch(handle.downcase, nil)\r
+      if feed\r
+        @bot.timer.reschedule(@watch[feed.handle], 0)\r
+        m.okay if m\r
+      else\r
+        m.reply _("no such feed %{handle}") % { :handle => handle } if m\r
+      end\r
+    else\r
+      stop_watches\r
 \r
-    # Read watches from list.\r
-    watchlist.each{ |handle, feed|\r
-      watchRss(feed, m)\r
-    }\r
-    m.okay if m\r
+      # Read watches from list.\r
+      watchlist.each{ |handle, feed|\r
+        watchRss(feed, m)\r
+      }\r
+      m.okay if m\r
+    end\r
   end\r
 \r
   private\r
@@ -691,13 +703,18 @@ class RSSFeedsPlugin < Plugin
     end\r
     status = Hash.new\r
     status[:failures] = 0\r
-    status[:first_run] = true\r
-    @watch[feed.handle] = @bot.timer.add(0) {\r
-      debug "watcher for #{feed} started"\r
+    tmout = 0\r
+    if feed.last_fetched\r
+      tmout = feed.last_fetched + calculate_timeout(feed) - Time.now\r
+      tmout = 0 if tmout < 0\r
+    end\r
+    debug "scheduling a watcher for #{feed} in #{tmout} seconds"\r
+    @watch[feed.handle] = @bot.timer.add(tmout) {\r
+      debug "watcher for #{feed} wakes up"\r
       failures = status[:failures]\r
-      first_run = status.delete(:first_run)\r
       begin\r
         debug "fetching #{feed}"\r
+        first_run = !feed.last_fetched\r
         oldxml = feed.xml ? feed.xml.dup : nil\r
         unless fetchRss(feed)\r
           failures += 1\r
@@ -774,21 +791,35 @@ class RSSFeedsPlugin < Plugin
 \r
       status[:failures] = failures\r
 \r
-      timer = nil\r
+      seconds = calculate_timeout(feed, failures)\r
+      debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
+      begin\r
+        @bot.timer.reschedule(@watch[feed.handle], seconds)\r
+      rescue\r
+        warning "watcher for #{feed} failed to reschedule: #{$!.inspect}"\r
+      end\r
+    }\r
+    debug "watcher for #{feed} added"\r
+  end\r
+\r
+  def calculate_timeout(feed, failures = 0)\r
       seconds = @bot.config['rss.thread_sleep']\r
       feed.mutex.synchronize do\r
-        timer = @watch[feed.handle]\r
         seconds = feed.refresh_rate if feed.refresh_rate\r
       end\r
       seconds *= failures + 1\r
       seconds += seconds * (rand(100)-50)/100\r
-      debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
-      @bot.timer.reschedule(timer, seconds) rescue warning "watcher for #{feed} failed to reschedule: #{$!.inspect}"\r
-    }\r
-    debug "watcher for #{feed} added"\r
+      return seconds\r
+  end\r
+\r
+  def select_nonempty(*ar)\r
+    debug ar\r
+    ret = ar.map { |i| (i && i.empty?) ? nil : i }.compact.first\r
+    (ret && ret.empty?) ? nil : ret\r
   end\r
 \r
   def printFormattedRss(feed, item, opts=nil)\r
+    debug item\r
     places = feed.watchers\r
     handle = "::#{feed.handle}:: "\r
     date = String.new\r
@@ -796,13 +827,25 @@ class RSSFeedsPlugin < Plugin
       places = opts[:places] if opts.key?(:places)\r
       handle = opts[:handle].to_s if opts.key?(:handle)\r
       if opts.key?(:date) && opts[:date]\r
-        if item.respond_to?(:pubDate) \r
+        if item.respond_to?(:updated)\r
+          if item.updated.content.class <= Time\r
+            date = item.updated.content.strftime("%Y/%m/%d %H:%M")\r
+          else\r
+            date = item.updated.content.to_s\r
+          end\r
+        elsif item.respond_to?(:source) and item.source.respond_to?(:updated)\r
+          if item.source.updated.content.class <= Time\r
+            date = item.source.updated.content.strftime("%Y/%m/%d %H:%M")\r
+          else\r
+            date = item.source.updated.content.to_s\r
+          end\r
+        elsif item.respond_to?(:pubDate) \r
           if item.pubDate.class <= Time\r
             date = item.pubDate.strftime("%Y/%m/%d %H:%M")\r
           else\r
             date = item.pubDate.to_s\r
           end\r
-        elsif  item.respond_to?(:date)\r
+        elsif item.respond_to?(:date)\r
           if item.date.class <= Time\r
             date = item.date.strftime("%Y/%m/%d %H:%M")\r
           else\r
@@ -815,28 +858,57 @@ class RSSFeedsPlugin < Plugin
       end\r
     end\r
 \r
-    title = "#{Bold}#{item.title.ircify_html :limit => @bot.config['rss.head_max']}#{Bold}" if item.title\r
+    tit_opt = {}\r
+    # Twitters don't need a cap on the title length since they have a hard\r
+    # limit to 160 characters, and most of them are under 140 characters\r
+    tit_opt[:limit] = @bot.config['rss.head_max'] unless feed.type == 'twitter'\r
+\r
+    if item.title\r
+      base_title = item.title.to_s.dup\r
+      # git changesets are SHA1 hashes (40 hex digits), way too long, get rid of them, as they are\r
+      # visible in the URL anyway\r
+      # TODO make this optional?\r
+      base_title.sub!(/^Changeset \[([\da-f]{40})\]:/) { |c| "(git commit)"} if feed.type = 'trac'\r
+      title = "#{Bold}#{base_title.ircify_html(tit_opt)}#{Bold}"\r
+    end\r
+\r
+    desc_opt = {\r
+      :limit => @bot.config['rss.text_max'],\r
+      :a_href => :link_out\r
+    }\r
 \r
-    desc = item.description.ircify_html(:limit => @bot.config['rss.text_max'], :a_href => :link_out) if item.description\r
+    if item.respond_to? :description\r
+      desc = item.description.ircify_html(desc_opt) if item.description\r
+    else\r
+      if item.content.type == "html"\r
+        desc = item.content.content.ircify_html(desc_opt)\r
+      else\r
+        desc = item.content.content\r
+        if desc.size > desc_opt[:limit]\r
+          desc = desc.slice(0, desc_opt[:limit]) + "#{Reverse}...#{Reverse}"\r
+        end\r
+      end\r
+    end\r
 \r
-    link = item.link.chomp if item.link\r
+    link = item.link.href rescue item.link.chomp rescue nil\r
 \r
-    debug item.inspect\r
-    category = item.dc_subject rescue item.category.content rescue nil\r
-    category = nil if category and category.empty?\r
-    author = item.dc_creator rescue item.author rescue nil\r
-    author = nil if author and author.empty?\r
+    category = select_nonempty((item.category.content rescue nil), (idem.dc_subject rescue nil))\r
+    author = select_nonempty((item.author.name.content rescue nil), (item.dc_creator rescue nil), (item.author rescue nil))\r
 \r
     line1 = nil\r
     line2 = nil\r
 \r
     at = ((item.title && item.link) ? ' @ ' : '')\r
+\r
     case feed.type\r
     when 'blog'\r
-      author << " " if author\r
+      author += " " if author\r
       abt = category ? "about #{category} " : ""\r
       line1 = "#{handle}#{date}#{author}blogged #{abt}at #{link}"\r
       line2 = "#{handle}#{title} - #{desc}"\r
+    when 'git'\r
+      author += " " if author\r
+      line1 = "#{handle}#{date}#{author}commited #{title} @ #{link}"\r
     when 'forum'\r
       line1 = "#{handle}#{date}#{title}#{at}#{link}"\r
     when 'wiki'\r
@@ -845,7 +917,7 @@ class RSSFeedsPlugin < Plugin
       line1 = "#{handle}#{date}Message #{title} sent by #{author}. #{desc}"\r
     when 'trac'\r
       line1 = "#{handle}#{date}#{title} @ #{link}"\r
-      unless item.title =~ /^Changeset \[(\d+)\]/\r
+      unless item.title =~ /^(?:Changeset \[(?:[\da-f]+)\]|\(git commit\))/\r
         line2 = "#{handle}#{date}#{desc}"\r
       end\r
     when '/.'\r
@@ -855,6 +927,7 @@ class RSSFeedsPlugin < Plugin
       line1 = "#{handle}#{date}#{dept}#{title}#{at}#{link} (posted by #{author}#{sec})"\r
     else\r
       line1 = "#{handle}#{date}#{title}#{at}#{link}"\r
+      line1 << " (by #{author})" if author\r
     end\r
     places.each { |loc|\r
       @bot.say loc, line1, :overlong => :truncate\r
@@ -864,6 +937,7 @@ class RSSFeedsPlugin < Plugin
   end\r
 \r
   def fetchRss(feed, m=nil, cache=true)\r
+    feed.last_fetched = Time.now\r
     begin\r
       # Use 60 sec timeout, cause the default is too low\r
       xml = @bot.httputil.get(feed.url,\r
@@ -927,8 +1001,12 @@ class RSSFeedsPlugin < Plugin
           report_problem("bah! something went wrong =(", e, m)\r
           return nil\r
         end\r
-        rss.channel.title ||= "Unknown"\r
-        title = rss.channel.title\r
+        if rss.respond_to? :channel\r
+          rss.channel.title ||= "Unknown"\r
+          title = rss.channel.title\r
+        else\r
+          title = rss.title.content\r
+        end\r
         rss.items.each do |item|\r
           item.title ||= "Unknown"\r
           items << item\r
@@ -948,6 +1026,9 @@ end
 \r
 plugin = RSSFeedsPlugin.new\r
 \r
+plugin.default_auth( 'edit', false )\r
+plugin.default_auth( 'edit:add', true)\r
+\r
 plugin.map 'rss show :handle :limit',\r
   :action => 'show_rss',\r
   :requirements => {:limit => /^\d+(?:\.\.\d+)?$/},\r
@@ -963,23 +1044,31 @@ plugin.map 'rss who watches :handle',
   :defaults => {:handle => nil}\r
 plugin.map 'rss add :handle :url :type',\r
   :action => 'add_rss',\r
+  :auth_path => 'edit',\r
   :defaults => {:type => nil}\r
 plugin.map 'rss change :what of :handle to :new',\r
   :action => 'change_rss',\r
+  :auth_path => 'edit',\r
   :requirements => { :what => /handle|url|format|type|refresh/ }\r
 plugin.map 'rss change :what for :handle to :new',\r
   :action => 'change_rss',\r
+  :auth_path => 'edit',\r
   :requirements => { :what => /handle|url|format|type|refesh/ }\r
 plugin.map 'rss del :handle',\r
+  :auth_path => 'edit:rm!',\r
   :action => 'del_rss'\r
 plugin.map 'rss delete :handle',\r
+  :auth_path => 'edit:rm!',\r
   :action => 'del_rss'\r
 plugin.map 'rss rm :handle',\r
+  :auth_path => 'edit:rm!',\r
   :action => 'del_rss'\r
 plugin.map 'rss replace :handle :url :type',\r
+  :auth_path => 'edit',\r
   :action => 'replace_rss',\r
   :defaults => {:type => nil}\r
 plugin.map 'rss forcereplace :handle :url :type',\r
+  :auth_path => 'edit',\r
   :action => 'forcereplace_rss',\r
   :defaults => {:type => nil}\r
 plugin.map 'rss watch :handle [in :chan]',\r
@@ -992,6 +1081,5 @@ plugin.map 'rss unwatch :handle [in :chan]',
   :action => 'unwatch_rss'\r
 plugin.map 'rss rmwatch :handle [in :chan]',\r
   :action => 'unwatch_rss'\r
-plugin.map 'rss rewatch',\r
+plugin.map 'rss rewatch [:handle]',\r
   :action => 'rewatch_rss'\r
-\r