]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
rss plugin: add support for twitter format
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index 4161460947cb2f4d73786142570a4d348f94e094..22aa742ff6bc3c08f98a1020123f9c8e19ace2f2 100644 (file)
@@ -46,13 +46,20 @@ module ::RSS
           full_name = "#{SLASH_PREFIX}_#{name}"\r
           full_plural_name = "#{SLASH_PREFIX}_#{plural}"\r
           klass_name = "Slash#{Utils.to_class_name(name)}"\r
-          klass.install_must_call_validator(SLASH_PREFIX, SLASH_URI)\r
-          klass.install_have_children_element(name, SLASH_URI, "*",\r
-                                              full_name, full_plural_name)\r
+\r
+          # This will fail with older version of the Ruby RSS module\r
+          begin\r
+            klass.install_have_children_element(name, SLASH_URI, "*",\r
+                                                full_name, full_plural_name)\r
+            klass.install_must_call_validator(SLASH_PREFIX, SLASH_URI)\r
+          rescue ArgumentError\r
+            klass.module_eval("install_have_children_element(#{full_name.dump}, #{full_plural_name.dump})")\r
+          end\r
+\r
           klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0))\r
-          remove_method :#{full_name}\r
-          remove_method :#{full_name}=\r
-          remove_method :set_#{full_name}\r
+          remove_method :#{full_name}     if method_defined? :#{full_name}\r
+          remove_method :#{full_name}=    if method_defined? :#{full_name}=\r
+          remove_method :set_#{full_name} if method_defined? :set_#{full_name}\r
 \r
           def #{full_name}\r
             @#{full_name}.first and @#{full_name}.first.value\r
@@ -105,9 +112,15 @@ module ::RSS
           alias_method(:value=, :content=)\r
 \r
           def initialize(*args)\r
-            if Utils.element_initialize_arguments?(args)\r
-              super\r
-            else\r
+            begin\r
+              if Utils.element_initialize_arguments?(args)\r
+                super\r
+              else\r
+                super()\r
+                self.content = args[0]\r
+              end\r
+            # Older Ruby RSS module\r
+            rescue NoMethodError\r
               super()\r
               self.content = args[0]\r
             end\r
@@ -229,19 +242,19 @@ class ::RssBlob
 end\r
 \r
 class RSSFeedsPlugin < Plugin\r
-  BotConfig.register BotConfigIntegerValue.new('rss.head_max',\r
+  Config.register Config::IntegerValue.new('rss.head_max',\r
     :default => 100, :validate => Proc.new{|v| v > 0 && v < 200},\r
     :desc => "How many characters to use of a RSS item header")\r
 \r
-  BotConfig.register BotConfigIntegerValue.new('rss.text_max',\r
+  Config.register Config::IntegerValue.new('rss.text_max',\r
     :default => 200, :validate => Proc.new{|v| v > 0 && v < 400},\r
     :desc => "How many characters to use of a RSS item text")\r
 \r
-  BotConfig.register BotConfigIntegerValue.new('rss.thread_sleep',\r
+  Config.register Config::IntegerValue.new('rss.thread_sleep',\r
     :default => 300, :validate => Proc.new{|v| v > 30},\r
     :desc => "How many seconds to sleep before checking RSS feeds again")\r
 \r
-  BotConfig.register BotConfigBooleanValue.new('rss.show_updated',\r
+  Config.register Config::BooleanValue.new('rss.show_updated',\r
     :default => true,\r
     :desc => "Whether feed items for which the description was changed should be shown as new")\r
 \r
@@ -270,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
@@ -802,9 +815,18 @@ 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
-    desc = item.description.ircify_html(:limit => @bot.config['rss.text_max'], :a_href => :link_out) if item.description\r
+    title = "#{Bold}#{item.title.ircify_html(tit_opt)}#{Bold}" if item.title\r
+\r
+    desc_opt = {\r
+      :limit => @bot.config['rss.text_max'],\r
+      :a_href => :link_out\r
+    }\r
+    desc = item.description.ircify_html(desc_opt) if item.description\r
 \r
     link = item.link.chomp if item.link\r
 \r