]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
rss plugin: the refresh rate of each rss can be set independently of the global one...
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 5 Feb 2007 19:36:48 +0000 (19:36 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Mon, 5 Feb 2007 19:36:48 +0000 (19:36 +0000)
data/rbot/plugins/rss.rb

index 75fc9b472b3a8b22ce760dfc880ca2d7482580f8..22a6dc18884b78f8c2236b712c586e216f961256 100644 (file)
@@ -30,6 +30,7 @@ class ::RssBlob
   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
@@ -44,6 +45,7 @@ class ::RssBlob
     end\r
     @type = type\r
     @watchers=[]\r
+    @refresh_rate = nil\r
     @xml = xml\r
     @title = nil\r
     @items = nil\r
@@ -89,7 +91,7 @@ class ::RssBlob
   end\r
 \r
   def to_a\r
-    [@handle,@url,@type,@watchers]\r
+    [@handle,@url,@type,@refresh_rate,@watchers]\r
   end\r
 \r
   def to_s(watchers=false)\r
@@ -189,7 +191,7 @@ class RSSFeedsPlugin < Plugin
     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
-      "rss change #{Bold}what#{Bold} of #{Bold}handle#{Bold} to #{Bold}new#{Bold} : change the handle, url or type of rss called #{Bold}handle#{Bold} to value #{Bold}new#{Bold}"\r
+      "rss change #{Bold}what#{Bold} of #{Bold}handle#{Bold} to #{Bold}new#{Bold} : change the #{Underline}handle#{Underline}, #{Underline}url#{Underline}, #{Underline}type#{Underline} or #{Underline}refresh#{Underline} rate of rss called #{Bold}handle#{Bold} to value #{Bold}new#{Bold}"\r
     when /^(del(ete)?|rm)$/\r
       "rss del(ete)|rm #{Bold}handle#{Bold} : delete rss feed #{Bold}handle#{Bold}"\r
     when "replace"\r
@@ -283,6 +285,7 @@ class RSSFeedsPlugin < Plugin
     @feeds.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)") if feed.watched_by?(m.replyto)\r
       reply << "\n"\r
     }\r
@@ -299,7 +302,9 @@ class RSSFeedsPlugin < Plugin
     watchlist.each { |handle, feed|\r
       next if wanted and !handle.match(/#{wanted}/i)\r
       next unless feed.watched_by?(m.replyto)\r
-      reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})\n"\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
     }\r
     if reply.empty?\r
       reply = "no watched feeds"\r
@@ -344,9 +349,11 @@ class RSSFeedsPlugin < Plugin
         m.reply "There already is a feed with handle #{new}"\r
         return\r
       else\r
-        @feeds[new] = feed\r
-        @feeds.delete(handle)\r
-        feed.handle = new\r
+        feed.mutex.synchronize do\r
+          @feeds[new] = feed\r
+          @feeds.delete(handle)\r
+          feed.handle = new\r
+        end\r
         handle = new\r
       end\r
     when :url\r
@@ -360,6 +367,12 @@ class RSSFeedsPlugin < Plugin
       feed.mutex.synchronize do\r
         feed.type = new\r
       end\r
+    when :refresh\r
+      new = params[:new].to_i\r
+      new = nil if new == 0\r
+      feed.mutex.synchronize do\r
+        feed.refresh_rate = new\r
+      end\r
     else\r
       m.reply "Don't know how to change #{params[:what]} for feeds"\r
       return\r
@@ -500,10 +513,12 @@ class RSSFeedsPlugin < Plugin
 \r
       status[:failures] = failures\r
 \r
-      seconds = @bot.config['rss.thread_sleep'] * (failures + 1)\r
-      seconds += seconds * (rand(100)-50)/100\r
-      debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
-      @bot.timer.reschedule(@watch[feed.handle], seconds)\r
+      feed.mutex.synchronize do\r
+        seconds = (feed.refresh_rate || @bot.config['rss.thread_sleep']) * (failures + 1)\r
+        seconds += seconds * (rand(100)-50)/100\r
+        debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
+        @bot.timer.reschedule(@watch[feed.handle], seconds)\r
+      end\r
     }\r
     debug "watcher for #{feed} added"\r
   end\r
@@ -655,10 +670,10 @@ plugin.map 'rss add :handle :url :type',
   :defaults => {:type => nil}\r
 plugin.map 'rss change :what of :handle to :new',\r
   :action => 'change_rss',\r
-  :requirements => { :what => /handle|url|format|type/ }\r
+  :requirements => { :what => /handle|url|format|type|refresh/ }\r
 plugin.map 'rss change :what for :handle to :new',\r
   :action => 'change_rss',\r
-  :requirements => { :what => /handle|url|format|type/ }\r
+  :requirements => { :what => /handle|url|format|type|refesh/ }\r
 plugin.map 'rss del :handle',\r
   :action => 'del_rss'\r
 plugin.map 'rss delete :handle',\r