#-- vim:sw=2:et\r
#++\r
#\r
-# RSS feed plugin for RubyBot\r
-# (c) 2004 Stanislav Karchebny <berkus@madfire.net>\r
-# (c) 2005 Ian Monroe <ian@monroe.nu>\r
-# (c) 2005 Mark Kretschmann <markey@web.de>\r
-# (c) 2006 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\r
+# :title: RSS feed plugin for rbot\r
#\r
-# Licensed under MIT License.\r
-\r
-require 'rss/parser'\r
-require 'rss/1.0'\r
-require 'rss/2.0'\r
-require 'rss/dublincore'\r
-# begin\r
-# require 'rss/dublincore/2.0'\r
-# rescue\r
-# warning "Unable to load RSS libraries, RSS plugin functionality crippled"\r
-# end\r
-\r
-class ::String\r
- def riphtml\r
- self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')\r
+# Author:: Stanislav Karchebny <berkus@madfire.net>\r
+# Author:: Ian Monroe <ian@monroe.nu>\r
+# Author:: Mark Kretschmann <markey@web.de>\r
+# Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>\r
+#\r
+# Copyright:: (C) 2004 Stanislav Karchebny\r
+# Copyright:: (C) 2005 Ian Monroe, Mark Kretschmann\r
+# Copyright:: (C) 2006-2007 Giuseppe Bilotta\r
+#\r
+# License:: MIT license\r
+\r
+require 'rss'\r
+\r
+# Add support for Slashdot namespace in RDF. The code is just an adaptation of\r
+# the DublinCore code.\r
+module ::RSS\r
+\r
+ unless defined?(SLASH_PREFIX)\r
+ SLASH_PREFIX = 'slash'\r
+ SLASH_URI = "http://purl.org/rss/1.0/modules/slash/"\r
+\r
+ RDF.install_ns(SLASH_PREFIX, SLASH_URI)\r
+\r
+ module BaseSlashModel\r
+ def append_features(klass)\r
+ super\r
+\r
+ return if klass.instance_of?(Module)\r
+ SlashModel::ELEMENT_NAME_INFOS.each do |name, plural_name|\r
+ plural = plural_name || "#{name}s"\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
+ 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
+\r
+ def #{full_name}\r
+ @#{full_name}.first and @#{full_name}.first.value\r
+ end\r
+\r
+ def #{full_name}=(new_value)\r
+ @#{full_name}[0] = Utils.new_with_value_if_need(#{klass_name}, new_value)\r
+ end\r
+ alias set_#{full_name} #{full_name}=\r
+ EOC\r
+ end\r
+ end\r
+ end\r
+\r
+ module SlashModel\r
+ extend BaseModel\r
+ extend BaseSlashModel\r
+\r
+ TEXT_ELEMENTS = {\r
+ "department" => nil,\r
+ "section" => nil,\r
+ "comments" => nil,\r
+ "hit_parade" => nil\r
+ }\r
+\r
+ ELEMENT_NAME_INFOS = SlashModel::TEXT_ELEMENTS.to_a\r
+\r
+ ELEMENTS = TEXT_ELEMENTS.keys\r
+\r
+ ELEMENTS.each do |name, plural_name|\r
+ module_eval(<<-EOC, *get_file_and_line_from_caller(0))\r
+ class Slash#{Utils.to_class_name(name)} < Element\r
+ include RSS10\r
+\r
+ content_setup\r
+\r
+ class << self\r
+ def required_prefix\r
+ SLASH_PREFIX\r
+ end\r
+\r
+ def required_uri\r
+ SLASH_URI\r
+ end\r
+ end\r
+\r
+ @tag_name = #{name.dump}\r
+\r
+ alias_method(:value, :content)\r
+ alias_method(:value=, :content=)\r
+\r
+ def initialize(*args)\r
+ if Utils.element_initialize_arguments?(args)\r
+ super\r
+ else\r
+ super()\r
+ self.content = args[0]\r
+ end\r
+ end\r
+\r
+ def full_name\r
+ tag_name_with_prefix(SLASH_PREFIX)\r
+ end\r
+\r
+ def maker_target(target)\r
+ target.new_#{name}\r
+ end\r
+\r
+ def setup_maker_attributes(#{name})\r
+ #{name}.content = content\r
+ end\r
+ end\r
+ EOC\r
+ end\r
+ end\r
+\r
+ class RDF\r
+ class Item; include SlashModel; end\r
+ end\r
+\r
+ SlashModel::ELEMENTS.each do |name|\r
+ class_name = Utils.to_class_name(name)\r
+ BaseListener.install_class_name(SLASH_URI, name, "Slash#{class_name}")\r
+ end\r
+\r
+ SlashModel::ELEMENTS.collect! {|name| "#{SLASH_PREFIX}_#{name}"}\r
end\r
end\r
\r
+\r
class ::RssBlob\r
attr_accessor :url\r
attr_accessor :handle\r
end\r
\r
def dup\r
- self.class.new(@url,\r
- @handle,\r
- @type ? @type.dup : nil,\r
- @watchers.dup,\r
- @xml ? @xml.dup : nil)\r
+ @mutex.synchronize do\r
+ self.class.new(@url,\r
+ @handle,\r
+ @type ? @type.dup : nil,\r
+ @watchers.dup,\r
+ @xml ? @xml.dup : nil)\r
+ end\r
end\r
\r
# Downcase all watchers, possibly turning them into Strings if they weren't\r
if watched_by?(who)\r
return nil\r
end\r
- @watchers << who.downcase\r
+ @mutex.synchronize do\r
+ @watchers << who.downcase\r
+ end\r
return who\r
end\r
\r
def rm_watch(who)\r
- @watchers.delete(who.downcase)\r
+ @mutex.synchronize do\r
+ @watchers.delete(who.downcase)\r
+ end\r
end\r
\r
def to_a\r
:default => 300, :validate => Proc.new{|v| v > 30},\r
:desc => "How many seconds to sleep before checking RSS feeds again")\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
+ #\r
+ # When migrating 1.8.6 => 1.8.5, all we need to do is define an empty\r
+ # #marshal_load() method for Mutex. For 1.8.5 => 1.8.6 we need something\r
+ # dirtier, as seen later on in the initialization code.\r
+ unless Mutex.new.respond_to?(:marshal_load)\r
+ class ::Mutex\r
+ def marshal_load(str)\r
+ return\r
+ end\r
+ end\r
+ end\r
+\r
def initialize\r
super\r
if @registry.has_key?(:feeds)\r
+ # When migrating from Ruby 1.8.5 to 1.8.6, dumped Mutexes may render the\r
+ # data unrestorable. If this happens, we patch the data, thus allowing\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
+ @registry.recovery = Proc.new { |val|\r
+ patched = val.sub(":\v@mutexo:\nMutex", ":\v@mutexo:\vObject")\r
+ ret = Marshal.restore(patched)\r
+ ret.each_value { |blob|\r
+ blob.mutex = nil\r
+ blob\r
+ }\r
+ }\r
+\r
@feeds = @registry[:feeds]\r
+\r
+ @registry.recovery = nil\r
+\r
@feeds.keys.grep(/[A-Z]/) { |k|\r
@feeds[k.downcase] = @feeds[k]\r
@feeds.delete(k)\r
def save\r
unparsed = Hash.new()\r
@feeds.each { |k, f|\r
- f.mutex.synchronize do\r
- unparsed[k] = f.dup\r
- end\r
- }\r
- unparsed.each { |k, f|\r
- debug f.inspect\r
+ unparsed[k] = f.dup\r
+ # we don't want to save the mutex\r
+ unparsed[k].mutex = nil\r
}\r
@registry[:feeds] = unparsed\r
end\r
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
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
\r
m.reply "lemme fetch it..."\r
title = items = nil\r
- fetched = fetchRss(feed, m)\r
+ fetched = fetchRss(feed, m, false)\r
return unless fetched or feed.xml\r
if not fetched and feed.items\r
m.reply "using old data"\r
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'})"\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
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
\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
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
\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
date += " :: "\r
end\r
end\r
+\r
title = "#{Bold}#{item.title.chomp.riphtml}#{Bold}" if item.title\r
+\r
desc = item.description.gsub(/\s+/,' ').strip.riphtml if item.description\r
+\r
link = item.link.chomp if item.link\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
+\r
line1 = nil\r
line2 = nil\r
+\r
+ at = ((item.title && item.link) ? ' @ ' : '')\r
case feed.type\r
when 'blog'\r
- line1 = "#{handle}#{date}#{item.category.content} blogged at #{link}"\r
+ abt = category ? "about #{category} " : ""\r
+ line1 = "#{handle}#{date}#{author} blogged #{abt}at #{link}"\r
line2 = "#{handle}#{title} - #{desc}"\r
when 'forum'\r
- line1 = "#{handle}#{date}#{title}#{' @ ' if item.title && item.link}#{link}"\r
+ line1 = "#{handle}#{date}#{title}#{at}#{link}"\r
when 'wiki'\r
- line1 = "#{handle}#{date}#{title}#{' @ ' if item.title && item.link}#{link} has been edited by #{item.dc_creator}. #{desc}"\r
+ line1 = "#{handle}#{date}#{title}#{at}#{link} has been edited by #{author}. #{desc}"\r
when 'gmane'\r
- line1 = "#{handle}#{date}Message #{title} sent by #{item.dc_creator}. #{desc}"\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
line2 = "#{handle}#{date}#{desc}"\r
end\r
+ when '/.'\r
+ dept = "(from the #{item.slash_department} dept) " rescue nil\r
+ sec = " in section #{item.slash_section}" rescue nil\r
+\r
+ line1 = "#{handle}#{date}#{dept}#{title}#{at}#{link} (posted by #{author}#{sec})"\r
else\r
- line1 = "#{handle}#{date}#{title}#{' @ ' if item.title && item.link}#{link}"\r
+ line1 = "#{handle}#{date}#{title}#{at}#{link}"\r
end\r
places.each { |loc|\r
@bot.say loc, line1, :overlong => :truncate\r
}\r
end\r
\r
- def fetchRss(feed, m=nil)\r
+ def fetchRss(feed, m=nil, cache=true)\r
begin\r
# Use 60 sec timeout, cause the default is too low\r
- xml = @bot.httputil.get_cached(feed.url, 60, 60)\r
+ xml = @bot.httputil.get(feed.url,\r
+ :read_timeout => 60,\r
+ :open_timeout => 60,\r
+ :cache => cache)\r
rescue URI::InvalidURIError, URI::BadURIError => e\r
report_problem("invalid rss feed #{feed.url}", e, m)\r
return nil\r
report_problem("reading feed #{feed} failed", nil, m)\r
return nil\r
end\r
+ # Ok, 0.9 feeds are not supported, maybe because\r
+ # Netscape happily removed the DTD. So what we do is just to\r
+ # reassign the 0.9 RDFs to 1.0, and hope it goes right.\r
+ xml.gsub!("xmlns=\"http://my.netscape.com/rdf/simple/0.9/\"",\r
+ "xmlns=\"http://purl.org/rss/1.0/\"")\r
feed.mutex.synchronize do\r
feed.xml = xml\r
end\r
begin\r
## do validate parse\r
rss = RSS::Parser.parse(xml)\r
- debug "parsed #{feed}"\r
+ debug "parsed and validated #{feed}"\r
rescue RSS::InvalidRSSError\r
## do non validate parse for invalid RSS 1.0\r
begin\r
rss = RSS::Parser.parse(xml, false)\r
+ debug "parsed but not validated #{feed}"\r
rescue RSS::Error => e\r
report_problem("parsing rss stream failed, whoops =(", e, m)\r
return nil\r
: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 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