]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
fortune plugin: add header
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index 483355c748fe107980f7b329daeec3907ce1868e..4161460947cb2f4d73786142570a4d348f94e094 100644 (file)
@@ -298,7 +298,7 @@ class RSSFeedsPlugin < Plugin
         @feeds.delete(k)\r
       }\r
       @feeds.each { |k, f|\r
-        f.mutex = Mutex.new unless f.mutex\r
+        f.mutex = Mutex.new\r
         f.sanitize_watchers\r
         parseRss(f) if f.xml\r
       }\r
@@ -319,6 +319,7 @@ class RSSFeedsPlugin < Plugin
 \r
   def cleanup\r
     stop_watches\r
+    super\r
   end\r
 \r
   def save\r
@@ -414,7 +415,25 @@ class RSSFeedsPlugin < Plugin
 \r
     m.reply "lemme fetch it..."\r
     title = items = nil\r
-    fetched = fetchRss(feed, m, false)\r
+    we_were_watching = false\r
+\r
+    if @watch.key?(feed.handle)\r
+      # If a feed is being watched, we run the watcher thread\r
+      # so that all watchers can be informed of changes to\r
+      # the feed. Before we do that, though, we remove the\r
+      # show requester from the watchlist, if present, lest\r
+      # he gets the update twice.\r
+      if feed.watched_by?(m.replyto)\r
+        we_were_watching = true\r
+        feed.rm_watch(m.replyto)\r
+      end\r
+      @bot.timer.reschedule(@watch[feed.handle], 0)\r
+      if we_were_watching\r
+        feed.add_watch(m.replyto)\r
+      end\r
+    else\r
+      fetched = fetchRss(feed, m, false)\r
+    end\r
     return unless fetched or feed.xml\r
     if not fetched and feed.items\r
       m.reply "using old data"\r
@@ -660,7 +679,7 @@ class RSSFeedsPlugin < Plugin
     status = Hash.new\r
     status[:failures] = 0\r
     status[:first_run] = true\r
-    @watch[feed.handle] = @bot.timer.add(0, status) {\r
+    @watch[feed.handle] = @bot.timer.add(0) {\r
       debug "watcher for #{feed} started"\r
       failures = status[:failures]\r
       first_run = status.delete(:first_run)\r
@@ -742,12 +761,16 @@ class RSSFeedsPlugin < Plugin
 \r
       status[:failures] = failures\r
 \r
+      timer = nil\r
+      seconds = @bot.config['rss.thread_sleep']\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
+        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
   end\r