X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Frss.rb;h=22aa742ff6bc3c08f98a1020123f9c8e19ace2f2;hb=3f8710a19b6989cd1c67629a92fd992968579456;hp=6451996d155fc9697a732f47b835c63a6ffa24d2;hpb=088cea5b9473cbf5796aac0e6104442138a11f07;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb index 6451996d..22aa742f 100644 --- a/data/rbot/plugins/rss.rb +++ b/data/rbot/plugins/rss.rb @@ -46,13 +46,20 @@ module ::RSS full_name = "#{SLASH_PREFIX}_#{name}" full_plural_name = "#{SLASH_PREFIX}_#{plural}" klass_name = "Slash#{Utils.to_class_name(name)}" - klass.install_must_call_validator(SLASH_PREFIX, SLASH_URI) - klass.install_have_children_element(name, SLASH_URI, "*", - full_name, full_plural_name) + + # This will fail with older version of the Ruby RSS module + begin + klass.install_have_children_element(name, SLASH_URI, "*", + full_name, full_plural_name) + klass.install_must_call_validator(SLASH_PREFIX, SLASH_URI) + rescue ArgumentError + klass.module_eval("install_have_children_element(#{full_name.dump}, #{full_plural_name.dump})") + end + klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0)) - remove_method :#{full_name} - remove_method :#{full_name}= - remove_method :set_#{full_name} + remove_method :#{full_name} if method_defined? :#{full_name} + remove_method :#{full_name}= if method_defined? :#{full_name}= + remove_method :set_#{full_name} if method_defined? :set_#{full_name} def #{full_name} @#{full_name}.first and @#{full_name}.first.value @@ -105,9 +112,15 @@ module ::RSS alias_method(:value=, :content=) def initialize(*args) - if Utils.element_initialize_arguments?(args) - super - else + begin + if Utils.element_initialize_arguments?(args) + super + else + super() + self.content = args[0] + end + # Older Ruby RSS module + rescue NoMethodError super() self.content = args[0] end @@ -229,19 +242,19 @@ class ::RssBlob end class RSSFeedsPlugin < Plugin - BotConfig.register BotConfigIntegerValue.new('rss.head_max', + Config.register Config::IntegerValue.new('rss.head_max', :default => 100, :validate => Proc.new{|v| v > 0 && v < 200}, :desc => "How many characters to use of a RSS item header") - BotConfig.register BotConfigIntegerValue.new('rss.text_max', + Config.register Config::IntegerValue.new('rss.text_max', :default => 200, :validate => Proc.new{|v| v > 0 && v < 400}, :desc => "How many characters to use of a RSS item text") - BotConfig.register BotConfigIntegerValue.new('rss.thread_sleep', + Config.register Config::IntegerValue.new('rss.thread_sleep', :default => 300, :validate => Proc.new{|v| v > 30}, :desc => "How many seconds to sleep before checking RSS feeds again") - BotConfig.register BotConfigBooleanValue.new('rss.show_updated', + Config.register Config::BooleanValue.new('rss.show_updated', :default => true, :desc => "Whether feed items for which the description was changed should be shown as new") @@ -270,15 +283,15 @@ class RSSFeedsPlugin < Plugin # the restore to work. # # This is actually pretty safe for a number of reasons: - # * the code is only called if standard marshalling fails - # * the string we look for is quite unlikely to appear randomly - # * if the string appears somewhere and the patched string isn't recoverable - # either, we'll get another (unrecoverable) error, which makes the rss - # plugin unsable, just like it was if no recovery was attempted - # * if the string appears somewhere and the patched string is recoverable, - # we may get a b0rked feed, which is eventually overwritten by a clean - # one, so the worst thing that can happen is that a feed update spams - # the watchers once + # * the code is only called if standard marshalling fails + # * the string we look for is quite unlikely to appear randomly + # * if the string appears somewhere and the patched string isn't recoverable + # either, we'll get another (unrecoverable) error, which makes the rss + # plugin unsable, just like it was if no recovery was attempted + # * if the string appears somewhere and the patched string is recoverable, + # we may get a b0rked feed, which is eventually overwritten by a clean + # one, so the worst thing that can happen is that a feed update spams + # the watchers once @registry.recovery = Proc.new { |val| patched = val.sub(":\v@mutexo:\nMutex", ":\v@mutexo:\vObject") ret = Marshal.restore(patched) @@ -679,7 +692,7 @@ class RSSFeedsPlugin < Plugin status = Hash.new status[:failures] = 0 status[:first_run] = true - @watch[feed.handle] = @bot.timer.add(0, status) { + @watch[feed.handle] = @bot.timer.add(0) { debug "watcher for #{feed} started" failures = status[:failures] first_run = status.delete(:first_run) @@ -762,13 +775,15 @@ class RSSFeedsPlugin < Plugin status[:failures] = failures timer = nil + seconds = @bot.config['rss.thread_sleep'] feed.mutex.synchronize do - timer = @watch[feed.handle] - seconds = (feed.refresh_rate || @bot.config['rss.thread_sleep']) * (failures + 1) + timer = @watch[feed.handle] + seconds = feed.refresh_rate if feed.refresh_rate end + seconds *= failures + 1 seconds += seconds * (rand(100)-50)/100 debug "watcher for #{feed} going to sleep #{seconds} seconds.." - @bot.timer.reschedule(timer, seconds) + @bot.timer.reschedule(timer, seconds) rescue warning "watcher for #{feed} failed to reschedule: #{$!.inspect}" } debug "watcher for #{feed} added" end @@ -800,9 +815,18 @@ class RSSFeedsPlugin < Plugin end end - title = "#{Bold}#{item.title.ircify_html :limit => @bot.config['rss.head_max']}#{Bold}" if item.title + tit_opt = {} + # Twitters don't need a cap on the title length since they have a hard + # limit to 160 characters, and most of them are under 140 characters + tit_opt[:limit] = @bot.config['rss.head_max'] unless feed.type == 'twitter' - desc = item.description.ircify_html(:limit => @bot.config['rss.text_max'], :a_href => :link_out) if item.description + title = "#{Bold}#{item.title.ircify_html(tit_opt)}#{Bold}" if item.title + + desc_opt = { + :limit => @bot.config['rss.text_max'], + :a_href => :link_out + } + desc = item.description.ircify_html(desc_opt) if item.description link = item.link.chomp if item.link