]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
quiz plugin: initial support for unicode (UTF-8) answers
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index 75fc9b472b3a8b22ce760dfc880ca2d7482580f8..6b5e62be8030e2d8cf9ec658b7c5776e720e2a60 100644 (file)
@@ -19,17 +19,12 @@ require 'rss/dublincore'
 #   warning "Unable to load RSS libraries, RSS plugin functionality crippled"\r
 # end\r
 \r
-class ::String\r
-  def riphtml\r
-    self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')\r
-  end\r
-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
@@ -44,6 +39,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 +85,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
@@ -154,9 +150,6 @@ class RSSFeedsPlugin < Plugin
         unparsed[k] = f.dup\r
       end\r
     }\r
-    unparsed.each { |k, f|\r
-      debug f.inspect\r
-    }\r
     @registry[:feeds] = unparsed\r
   end\r
 \r
@@ -185,11 +178,13 @@ class RSSFeedsPlugin < Plugin
     when "list"\r
       "rss list [#{Bold}handle#{Bold}] : list all rss feeds (matching #{Bold}handle#{Bold})"\r
     when "watched"\r
-      "rss watched [#{Bold}handle#{Bold}] : list all watched rss feeds (matching #{Bold}handle#{Bold})"\r
+      "rss watched [#{Bold}handle#{Bold}] [in #{Bold}chan#{Bold}]: list all watched rss feeds (matching #{Bold}handle#{Bold}) (in channel #{Bold}chan#{Bold})"\r
+    when "who", "watches", "who watches"\r
+      "rss who watches [#{Bold}handle#{Bold}]]: list all watchers for rss feeds (matching #{Bold}handle#{Bold})"\r
     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
@@ -197,9 +192,9 @@ class RSSFeedsPlugin < Plugin
     when "forcereplace"\r
       "rss forcereplace #{Bold}handle#{Bold} #{Bold}url#{Bold} [#{Bold}type#{Bold}] : replace the url of rss called #{Bold}handle#{Bold} with #{Bold}url#{Bold} (of type #{Bold}type#{Bold})"\r
     when "watch"\r
-      "rss watch #{Bold}handle#{Bold} [#{Bold}url#{Bold} [#{Bold}type#{Bold}]] : watch rss #{Bold}handle#{Bold} for changes; when the other parameters are present, it will be created if it doesn't exist yet"\r
+      "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} : stop watching rss #{Bold}handle#{Bold} for changes"\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 "rewatch"\r
       "rss rewatch : restart threads that watch for changes in watched rss"\r
     else\r
@@ -283,6 +278,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
@@ -294,12 +290,32 @@ class RSSFeedsPlugin < Plugin
   end\r
 \r
   def watched_rss(m, params)\r
+    wanted = params[:handle]\r
+    chan = params[:chan] || m.replyto\r
+    reply = String.new\r
+    watchlist.each { |handle, feed|\r
+      next if wanted and !handle.match(/#{wanted}/i)\r
+      next unless feed.watched_by?(chan)\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
+      reply << " matching #{wanted}" if wanted\r
+    end\r
+    m.reply reply\r
+  end\r
+\r
+  def who_watches(m, params)\r
     wanted = params[:handle]\r
     reply = String.new\r
     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 << ": watched by #{feed.watchers.join(', ')}"\r
+      reply << "\n"\r
     }\r
     if reply.empty?\r
       reply = "no watched feeds"\r
@@ -336,7 +352,10 @@ class RSSFeedsPlugin < Plugin
   def change_rss(m, params)\r
     handle = params[:handle].downcase\r
     feed = @feeds.fetch(handle, nil)\r
-    return m.reply "No such feed with handle #{handle}" unless feed\r
+    unless feed\r
+      m.reply "No such feed with handle #{handle}"\r
+      return\r
+    end\r
     case params[:what].intern\r
     when :handle\r
       new = params[:new].downcase\r
@@ -344,9 +363,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 +381,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
@@ -397,6 +424,7 @@ class RSSFeedsPlugin < Plugin
 \r
   def watch_rss(m, params)\r
     handle = params[:handle]\r
+    chan = params[:chan] || m.replyto\r
     url = params[:url]\r
     type = params[:type]\r
     if url\r
@@ -404,11 +432,11 @@ class RSSFeedsPlugin < Plugin
     end\r
     feed = @feeds.fetch(handle.downcase, nil)\r
     if feed\r
-      if feed.add_watch(m.replyto)\r
+      if feed.add_watch(chan)\r
         watchRss(feed, m)\r
         m.okay\r
       else\r
-        m.reply "Already watching #{feed.handle}"\r
+        m.reply "Already watching #{feed.handle} in #{chan}"\r
       end\r
     else\r
       m.reply "Couldn't watch feed #{handle} (no such feed found)"\r
@@ -417,15 +445,16 @@ class RSSFeedsPlugin < Plugin
 \r
   def unwatch_rss(m, params, pass=false)\r
     handle = params[:handle].downcase\r
+    chan = params[:chan] || m.replyto\r
     unless @feeds.has_key?(handle)\r
       m.reply("dunno that feed")\r
       return\r
     end\r
     feed = @feeds[handle]\r
-    if feed.rm_watch(m.replyto)\r
-      m.reply "#{m.replyto} has been removed from the watchlist for #{feed.handle}"\r
+    if feed.rm_watch(chan)\r
+      m.reply "#{chan} has been removed from the watchlist for #{feed.handle}"\r
     else\r
-      m.reply("#{m.replyto} wasn't watching #{feed.handle}") unless pass\r
+      m.reply("#{chan} wasn't watching #{feed.handle}") unless pass\r
     end\r
     if !feed.watched?\r
       stop_watch(handle)\r
@@ -500,10 +529,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
@@ -646,19 +677,22 @@ plugin.map 'rss show :handle :limit',
   :defaults => {:limit => 5}\r
 plugin.map 'rss list :handle',\r
   :action => 'list_rss',\r
-  :defaults =>  {:handle => nil}\r
-plugin.map 'rss watched :handle',\r
+  :defaults => {:handle => nil}\r
+plugin.map 'rss watched :handle [in :chan]',\r
   :action => 'watched_rss',\r
-  :defaults =>  {:handle => nil}\r
+  :defaults => {:handle => nil}\r
+plugin.map 'rss who watches :handle',\r
+  :action => 'who_watches',\r
+  :defaults => {:handle => nil}\r
 plugin.map 'rss add :handle :url :type',\r
   :action => 'add_rss',\r
   :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
@@ -671,12 +705,12 @@ plugin.map 'rss replace :handle :url :type',
 plugin.map 'rss forcereplace :handle :url :type',\r
   :action => 'forcereplace_rss',\r
   :defaults => {:type => nil}\r
-plugin.map 'rss watch :handle :url :type',\r
+plugin.map 'rss watch :handle :url :type [in :chan]',\r
   :action => 'watch_rss',\r
   :defaults => {:url => nil, :type => nil}\r
-plugin.map 'rss unwatch :handle',\r
+plugin.map 'rss unwatch :handle [in :chan]',\r
   :action => 'unwatch_rss'\r
-plugin.map 'rss rmwatch :handle',\r
+plugin.map 'rss rmwatch :handle [in :chan]',\r
   :action => 'unwatch_rss'\r
 plugin.map 'rss rewatch',\r
   :action => 'rewatch_rss'\r