\r
require 'rss'\r
\r
-# Add support for Slashdot namespace in RDF. The code is just an adaptation of\r
-# the DublinCore code.\r
+# Try to load rss/content/2.0 so we can access the data in <content:encoded> \r
+# tags.\r
+begin\r
+ require 'rss/content/2.0'\r
+rescue LoadError\r
+end\r
+\r
module ::RSS\r
\r
+ # Make an 'unique' ID for a given item, based on appropriate bot options\r
+ # Currently only suppored is bot.config['rss.show_updated']: when true, the\r
+ # description is included in the uid hashing, otherwise it's not\r
+ #\r
+ def RSS.item_uid_for_bot(item, opts={})\r
+ options = { :show_updated => true}.merge(opts)\r
+ desc = nil\r
+ if options[:show_updated]\r
+ desc = item.content.content rescue item.description rescue nil\r
+ end\r
+ [(item.title.content rescue item.title rescue nil),\r
+ (item.link.href rescue item.link),\r
+ desc].hash\r
+ end\r
+\r
+ # Add support for Slashdot namespace in RDF. The code is just an adaptation\r
+ # of the DublinCore code.\r
unless defined?(SLASH_PREFIX)\r
SLASH_PREFIX = 'slash'\r
SLASH_URI = "http://purl.org/rss/1.0/modules/slash/"\r
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
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
\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
- attr_accessor :mutex\r
-\r
- def initialize(url,handle=nil,type=nil,watchers=[], xml=nil)\r
+ attr_accessor :url, :handle, :type, :refresh_rate, :xml, :title, :items,\r
+ :mutex, :watchers, :last_fetched\r
+\r
+ def initialize(url,handle=nil,type=nil,watchers=[], xml=nil, lf = nil)\r
@url = url\r
if handle\r
@handle = handle\r
@title = nil\r
@items = nil\r
@mutex = Mutex.new\r
+ @last_fetched = lf\r
sanitize_watchers(watchers)\r
end\r
\r
@handle,\r
@type ? @type.dup : nil,\r
@watchers.dup,\r
- @xml ? @xml.dup : nil)\r
+ @xml ? @xml.dup : nil,\r
+ @last_fetched)\r
end\r
end\r
\r
end\r
\r
class RSSFeedsPlugin < Plugin\r
- BotConfig.register BotConfigIntegerValue.new('rss.head_max',\r
- :default => 30, :validate => Proc.new{|v| v > 0 && v < 200},\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
- :default => 90, :validate => Proc.new{|v| v > 0 && v < 400},\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
+ 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
+ Config.register Config::BooleanValue.new('rss.show_links',\r
+ :default => true,\r
+ :desc => "Whether to display links from the text of a feed item.")\r
+\r
# We used to save the Mutex with the RssBlob, which was idiotic. And\r
# since Mutexes dumped in one version might not be resotrable in another,\r
# we need a few tricks to be able to restore data from other versions of Ruby\r
# 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
}\r
\r
@feeds = @registry[:feeds]\r
+ raise unless @feeds\r
\r
@registry.recovery = nil\r
\r
@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
\r
def cleanup\r
stop_watches\r
+ super\r
end\r
\r
def save\r
debug "Stopping watch #{handle}"\r
@bot.timer.remove(@watch[handle])\r
@watch.delete(handle)\r
- rescue => e\r
+ rescue Exception => e\r
report_problem("Failed to stop watch for #{handle}", e, nil)\r
end\r
end\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} [in #{Bold}chan#{Bold}]: stop watching rss #{Bold}handle#{Bold} (in channel #{Bold}chan#{Bold}) for changes"\r
+ when /who(?: watche?s?)?/\r
+ "rss who watches #{Bold}handle#{Bold}: lists watches for rss #{Bold}handle#{Bold}"\r
when "rewatch"\r
"rss rewatch : restart threads that watch for changes in watched rss"\r
else\r
- "manage RSS feeds: rss show|list|watched|add|change|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch"\r
+ "manage RSS feeds: rss show|list|watched|add|change|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch|who watches"\r
end\r
end\r
\r
\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
reply = "no feeds found"\r
reply << " matching #{wanted}" if wanted\r
end\r
- m.reply reply\r
+ m.reply reply, :max_lines => reply.length\r
end\r
\r
def watched_rss(m, params)\r
\r
def del_rss(m, params, pass=false)\r
feed = unwatch_rss(m, params, true)\r
+ return unless feed\r
if feed.watched?\r
m.reply "someone else is watching #{feed.handle}, I won't remove it from my list"\r
return\r
end\r
\r
def rewatch_rss(m=nil, params=nil)\r
- stop_watches\r
+ if params and handle = params[:handle]\r
+ feed = @feeds.fetch(handle.downcase, nil)\r
+ if feed\r
+ @bot.timer.reschedule(@watch[feed.handle], 0)\r
+ m.okay if m\r
+ else\r
+ m.reply _("no such feed %{handle}") % { :handle => handle } if m\r
+ end\r
+ else\r
+ stop_watches\r
\r
- # Read watches from list.\r
- watchlist.each{ |handle, feed|\r
- watchRss(feed, m)\r
- }\r
- m.okay if m\r
+ # Read watches from list.\r
+ watchlist.each{ |handle, feed|\r
+ watchRss(feed, m)\r
+ }\r
+ m.okay if m\r
+ end\r
end\r
\r
private\r
end\r
status = Hash.new\r
status[:failures] = 0\r
- status[:first_run] = true\r
- @watch[feed.handle] = @bot.timer.add(0, status) {\r
- debug "watcher for #{feed} started"\r
+ tmout = 0\r
+ if feed.last_fetched\r
+ tmout = feed.last_fetched + calculate_timeout(feed) - Time.now\r
+ tmout = 0 if tmout < 0\r
+ end\r
+ debug "scheduling a watcher for #{feed} in #{tmout} seconds"\r
+ @watch[feed.handle] = @bot.timer.add(tmout) {\r
+ debug "watcher for #{feed} wakes up"\r
failures = status[:failures]\r
- first_run = status.delete(:first_run)\r
begin\r
debug "fetching #{feed}"\r
+ first_run = !feed.last_fetched\r
oldxml = feed.xml ? feed.xml.dup : nil\r
unless fetchRss(feed)\r
failures += 1\r
else\r
- if first_run or (oldxml and oldxml == feed.xml)\r
+ if first_run\r
+ debug "first run for #{feed}, getting items"\r
+ parseRss(feed)\r
+ elsif oldxml and oldxml == feed.xml\r
debug "xml for #{feed} didn't change"\r
failures -= 1 if failures > 0\r
else\r
parseRss(feed)\r
failures -= 1 if failures > 0\r
else\r
- otxt = feed.items.map { |item| item.to_s }\r
+ # This one is used for debugging\r
+ otxt = []\r
+\r
+ # These are used for checking new items vs old ones\r
+ uid_opts = { :show_updated => @bot.config['rss.show_updated'] }\r
+ oids = Set.new feed.items.map { |item|\r
+ uid = RSS.item_uid_for_bot(item, uid_opts)\r
+ otxt << item.to_s\r
+ debug [uid, item].inspect\r
+ debug [uid, otxt.last].inspect\r
+ uid\r
+ }\r
+\r
unless parseRss(feed)\r
debug "no items in feed #{feed}"\r
failures += 1\r
else\r
debug "Checking if new items are available for #{feed}"\r
failures -= 1 if failures > 0\r
+ # debug "Old:"\r
+ # debug oldxml\r
+ # debug "New:"\r
+ # debug feed.xml\r
+\r
dispItems = feed.items.reject { |item|\r
- otxt.include?(item.to_s)\r
+ uid = RSS.item_uid_for_bot(item, uid_opts)\r
+ txt = item.to_s\r
+ if oids.include?(uid)\r
+ debug "rejecting old #{uid} #{item.inspect}"\r
+ debug [uid, txt].inspect\r
+ true\r
+ else\r
+ debug "accepting new #{uid} #{item.inspect}"\r
+ debug [uid, txt].inspect\r
+ warning "same text! #{txt}" if otxt.include?(txt)\r
+ false\r
+ end\r
}\r
+\r
if dispItems.length > 0\r
debug "Found #{dispItems.length} new items in #{feed}"\r
# When displaying watched feeds, publish them from older to newer\r
\r
status[:failures] = failures\r
\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
+ seconds = calculate_timeout(feed, failures)\r
+ debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
+ begin\r
@bot.timer.reschedule(@watch[feed.handle], seconds)\r
+ rescue\r
+ warning "watcher for #{feed} failed to reschedule: #{$!.inspect}"\r
end\r
}\r
debug "watcher for #{feed} added"\r
end\r
\r
+ def calculate_timeout(feed, failures = 0)\r
+ seconds = @bot.config['rss.thread_sleep']\r
+ feed.mutex.synchronize do\r
+ seconds = feed.refresh_rate if feed.refresh_rate\r
+ end\r
+ seconds *= failures + 1\r
+ seconds += seconds * (rand(100)-50)/100\r
+ return seconds\r
+ end\r
+\r
+ def select_nonempty(*ar)\r
+ debug ar\r
+ ret = ar.map { |i| (i && i.empty?) ? nil : i }.compact.first\r
+ (ret && ret.empty?) ? nil : ret\r
+ end\r
+\r
def printFormattedRss(feed, item, opts=nil)\r
+ debug item\r
places = feed.watchers\r
handle = "::#{feed.handle}:: "\r
date = String.new\r
places = opts[:places] if opts.key?(:places)\r
handle = opts[:handle].to_s if opts.key?(:handle)\r
if opts.key?(:date) && opts[:date]\r
- if item.respond_to?(:pubDate) \r
+ if item.respond_to?(:updated)\r
+ if item.updated.content.class <= Time\r
+ date = item.updated.content.strftime("%Y/%m/%d %H:%M")\r
+ else\r
+ date = item.updated.content.to_s\r
+ end\r
+ elsif item.respond_to?(:source) and item.source.respond_to?(:updated)\r
+ if item.source.updated.content.class <= Time\r
+ date = item.source.updated.content.strftime("%Y/%m/%d %H:%M")\r
+ else\r
+ date = item.source.updated.content.to_s\r
+ end\r
+ elsif item.respond_to?(:pubDate) \r
if item.pubDate.class <= Time\r
- date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S")\r
+ date = item.pubDate.strftime("%Y/%m/%d %H:%M")\r
else\r
date = item.pubDate.to_s\r
end\r
- elsif item.respond_to?(:date)\r
+ elsif item.respond_to?(:date)\r
if item.date.class <= Time\r
- date = item.date.strftime("%Y/%m/%d %H.%M.%S")\r
+ date = item.date.strftime("%Y/%m/%d %H:%M")\r
else\r
date = item.date.to_s\r
end\r
end\r
end\r
\r
- title = "#{Bold}#{item.title.ircify_html}#{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
+ if item.title\r
+ base_title = item.title.to_s.dup\r
+ # git changesets are SHA1 hashes (40 hex digits), way too long, get rid of them, as they are\r
+ # visible in the URL anyway\r
+ # TODO make this optional?\r
+ base_title.sub!(/^Changeset \[([\da-f]{40})\]:/) { |c| "(git commit)"} if feed.type == 'trac'\r
+ title = "#{Bold}#{base_title.ircify_html(tit_opt)}#{Bold}"\r
+ end\r
+\r
+ desc_opt = {}\r
+ desc_opt[:limit] = @bot.config['rss.text_max']\r
+ desc_opt[:a_href] = :link_out if @bot.config['rss.show_links']\r
\r
- desc = item.description.ircify_html if item.description\r
+ # We prefer content_encoded here as it tends to provide more html formatting \r
+ # for use with ircify_html.\r
+ if item.respond_to?(:content_encoded) && item.content_encoded\r
+ desc = item.content_encoded.ircify_html(desc_opt)\r
+ elsif item.respond_to?(:description) && item.description\r
+ desc = item.description.ircify_html(desc_opt)\r
+ else\r
+ if item.content.type == "html"\r
+ desc = item.content.content.ircify_html(desc_opt)\r
+ else\r
+ desc = item.content.content\r
+ if desc.size > desc_opt[:limit]\r
+ desc = desc.slice(0, desc_opt[:limit]) + "#{Reverse}...#{Reverse}"\r
+ end\r
+ end\r
+ end\r
\r
- link = item.link.chomp if item.link\r
+ link = item.link.href rescue item.link.chomp rescue nil\r
\r
- debug item.inspect\r
- category = item.dc_subject rescue item.category rescue nil\r
- author = item.dc_creator rescue item.author rescue nil\r
+ category = select_nonempty((item.category.content rescue nil), (item.dc_subject rescue nil))\r
+ author = select_nonempty((item.author.name.content rescue nil), (item.dc_creator rescue nil), (item.author rescue nil))\r
\r
line1 = nil\r
line2 = nil\r
\r
at = ((item.title && item.link) ? ' @ ' : '')\r
+\r
case feed.type\r
when 'blog'\r
+ author += " " if author\r
abt = category ? "about #{category} " : ""\r
- line1 = "#{handle}#{date}#{author} blogged #{abt}at #{link}"\r
+ line1 = "#{handle}#{date}#{author}blogged #{abt}at #{link}"\r
line2 = "#{handle}#{title} - #{desc}"\r
+ when 'git'\r
+ author += " " if author\r
+ line1 = "#{handle}#{date}#{author}commited #{title} @ #{link}"\r
when 'forum'\r
line1 = "#{handle}#{date}#{title}#{at}#{link}"\r
when 'wiki'\r
line1 = "#{handle}#{date}Message #{title} sent by #{author}. #{desc}"\r
when 'trac'\r
line1 = "#{handle}#{date}#{title} @ #{link}"\r
- unless item.title =~ /^Changeset \[(\d+)\]/\r
+ unless item.title =~ /^(?:Changeset \[(?:[\da-f]+)\]|\(git commit\))/\r
line2 = "#{handle}#{date}#{desc}"\r
end\r
when '/.'\r
line1 = "#{handle}#{date}#{dept}#{title}#{at}#{link} (posted by #{author}#{sec})"\r
else\r
line1 = "#{handle}#{date}#{title}#{at}#{link}"\r
+ line1 << " (by #{author})" if author\r
end\r
places.each { |loc|\r
@bot.say loc, line1, :overlong => :truncate\r
end\r
\r
def fetchRss(feed, m=nil, cache=true)\r
+ feed.last_fetched = Time.now\r
begin\r
# Use 60 sec timeout, cause the default is too low\r
xml = @bot.httputil.get(feed.url,\r
report_problem("bah! something went wrong =(", e, m)\r
return nil\r
end\r
- rss.channel.title ||= "Unknown"\r
- title = rss.channel.title\r
+ if rss.respond_to? :channel\r
+ rss.channel.title ||= "Unknown"\r
+ title = rss.channel.title\r
+ else\r
+ title = rss.title.content\r
+ end\r
rss.items.each do |item|\r
item.title ||= "Unknown"\r
items << item\r
\r
plugin = RSSFeedsPlugin.new\r
\r
+plugin.default_auth( 'edit', false )\r
+plugin.default_auth( 'edit:add', true)\r
+\r
plugin.map 'rss show :handle :limit',\r
:action => 'show_rss',\r
:requirements => {:limit => /^\d+(?:\.\.\d+)?$/},\r
:defaults => {:handle => nil}\r
plugin.map 'rss add :handle :url :type',\r
:action => 'add_rss',\r
+ :auth_path => 'edit',\r
:defaults => {:type => nil}\r
plugin.map 'rss change :what of :handle to :new',\r
:action => 'change_rss',\r
+ :auth_path => 'edit',\r
:requirements => { :what => /handle|url|format|type|refresh/ }\r
plugin.map 'rss change :what for :handle to :new',\r
:action => 'change_rss',\r
+ :auth_path => 'edit',\r
:requirements => { :what => /handle|url|format|type|refesh/ }\r
plugin.map 'rss del :handle',\r
+ :auth_path => 'edit:rm!',\r
:action => 'del_rss'\r
plugin.map 'rss delete :handle',\r
+ :auth_path => 'edit:rm!',\r
:action => 'del_rss'\r
plugin.map 'rss rm :handle',\r
+ :auth_path => 'edit:rm!',\r
:action => 'del_rss'\r
plugin.map 'rss replace :handle :url :type',\r
+ :auth_path => 'edit',\r
:action => 'replace_rss',\r
:defaults => {:type => nil}\r
plugin.map 'rss forcereplace :handle :url :type',\r
+ :auth_path => 'edit',\r
:action => 'forcereplace_rss',\r
:defaults => {:type => nil}\r
+plugin.map 'rss watch :handle [in :chan]',\r
+ :action => 'watch_rss',\r
+ :defaults => {:url => nil, :type => nil}\r
plugin.map 'rss watch :handle :url :type [in :chan]',\r
:action => 'watch_rss',\r
:defaults => {:url => nil, :type => nil}\r
:action => 'unwatch_rss'\r
plugin.map 'rss rmwatch :handle [in :chan]',\r
:action => 'unwatch_rss'\r
-plugin.map 'rss rewatch',\r
+plugin.map 'rss rewatch [:handle]',\r
:action => 'rewatch_rss'\r
-\r