]> 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 2689b3c3a354a6f85e37c4dc59fba6300af284eb..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
@@ -676,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
@@ -693,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
@@ -776,18 +791,31 @@ 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
@@ -835,7 +863,14 @@ class RSSFeedsPlugin < Plugin
     # 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
-    title = "#{Bold}#{item.title.to_s.ircify_html(tit_opt)}#{Bold}" if item.title\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
@@ -857,10 +892,8 @@ class RSSFeedsPlugin < Plugin
 \r
     link = item.link.href rescue item.link.chomp rescue nil\r
 \r
-    category = item.category.content rescue item.dc_subject rescue nil\r
-    category = nil if category and category.empty?\r
-    author = item.author.name.content rescue 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
@@ -869,10 +902,13 @@ class RSSFeedsPlugin < Plugin
 \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
@@ -881,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
@@ -891,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
@@ -900,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
@@ -988,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
@@ -1003,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
@@ -1032,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