4 # :title: RSS feed plugin for rbot
\r
6 # Author:: Stanislav Karchebny <berkus@madfire.net>
\r
7 # Author:: Ian Monroe <ian@monroe.nu>
\r
8 # Author:: Mark Kretschmann <markey@web.de>
\r
9 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
\r
11 # Copyright:: (C) 2004 Stanislav Karchebny
\r
12 # Copyright:: (C) 2005 Ian Monroe, Mark Kretschmann
\r
13 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
\r
15 # License:: MIT license
\r
21 # Make an 'unique' ID for a given item, based on appropriate bot options
\r
22 # Currently only suppored is bot.config['rss.show_updated']: when true, the
\r
23 # description is included in the uid hashing, otherwise it's not
\r
25 def RSS.item_uid_for_bot(item, opts={})
\r
26 options = { :show_updated => true}.merge(opts)
\r
27 desc = options[:show_updated] ? item.description : nil
\r
28 [item.title, item.link, desc].hash
\r
31 # Add support for Slashdot namespace in RDF. The code is just an adaptation
\r
32 # of the DublinCore code.
\r
33 unless defined?(SLASH_PREFIX)
\r
34 SLASH_PREFIX = 'slash'
\r
35 SLASH_URI = "http://purl.org/rss/1.0/modules/slash/"
\r
37 RDF.install_ns(SLASH_PREFIX, SLASH_URI)
\r
39 module BaseSlashModel
\r
40 def append_features(klass)
\r
43 return if klass.instance_of?(Module)
\r
44 SlashModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
\r
45 plural = plural_name || "#{name}s"
\r
46 full_name = "#{SLASH_PREFIX}_#{name}"
\r
47 full_plural_name = "#{SLASH_PREFIX}_#{plural}"
\r
48 klass_name = "Slash#{Utils.to_class_name(name)}"
\r
50 # This will fail with older version of the Ruby RSS module
\r
52 klass.install_have_children_element(name, SLASH_URI, "*",
\r
53 full_name, full_plural_name)
\r
54 klass.install_must_call_validator(SLASH_PREFIX, SLASH_URI)
\r
55 rescue ArgumentError
\r
56 klass.module_eval("install_have_children_element(#{full_name.dump}, #{full_plural_name.dump})")
\r
59 klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0))
\r
60 remove_method :#{full_name} if method_defined? :#{full_name}
\r
61 remove_method :#{full_name}= if method_defined? :#{full_name}=
\r
62 remove_method :set_#{full_name} if method_defined? :set_#{full_name}
\r
65 @#{full_name}.first and @#{full_name}.first.value
\r
68 def #{full_name}=(new_value)
\r
69 @#{full_name}[0] = Utils.new_with_value_if_need(#{klass_name}, new_value)
\r
71 alias set_#{full_name} #{full_name}=
\r
79 extend BaseSlashModel
\r
82 "department" => nil,
\r
88 ELEMENT_NAME_INFOS = SlashModel::TEXT_ELEMENTS.to_a
\r
90 ELEMENTS = TEXT_ELEMENTS.keys
\r
92 ELEMENTS.each do |name, plural_name|
\r
93 module_eval(<<-EOC, *get_file_and_line_from_caller(0))
\r
94 class Slash#{Utils.to_class_name(name)} < Element
\r
100 def required_prefix
\r
109 @tag_name = #{name.dump}
\r
111 alias_method(:value, :content)
\r
112 alias_method(:value=, :content=)
\r
114 def initialize(*args)
\r
116 if Utils.element_initialize_arguments?(args)
\r
120 self.content = args[0]
\r
122 # Older Ruby RSS module
\r
123 rescue NoMethodError
\r
125 self.content = args[0]
\r
130 tag_name_with_prefix(SLASH_PREFIX)
\r
133 def maker_target(target)
\r
137 def setup_maker_attributes(#{name})
\r
138 #{name}.content = content
\r
146 class Item; include SlashModel; end
\r
149 SlashModel::ELEMENTS.each do |name|
\r
150 class_name = Utils.to_class_name(name)
\r
151 BaseListener.install_class_name(SLASH_URI, name, "Slash#{class_name}")
\r
154 SlashModel::ELEMENTS.collect! {|name| "#{SLASH_PREFIX}_#{name}"}
\r
161 attr_accessor :handle
\r
162 attr_accessor :type
\r
164 attr_accessor :refresh_rate
\r
166 attr_accessor :title
\r
167 attr_accessor :items
\r
168 attr_accessor :mutex
\r
170 def initialize(url,handle=nil,type=nil,watchers=[], xml=nil)
\r
179 @refresh_rate = nil
\r
184 sanitize_watchers(watchers)
\r
188 @mutex.synchronize do
\r
189 self.class.new(@url,
\r
191 @type ? @type.dup : nil,
\r
193 @xml ? @xml.dup : nil)
\r
197 # Downcase all watchers, possibly turning them into Strings if they weren't
\r
198 def sanitize_watchers(list=@watchers)
\r
210 def watched_by?(who)
\r
211 @watchers.include?(who.downcase)
\r
215 if watched_by?(who)
\r
218 @mutex.synchronize do
\r
219 @watchers << who.downcase
\r
225 @mutex.synchronize do
\r
226 @watchers.delete(who.downcase)
\r
231 [@handle,@url,@type,@refresh_rate,@watchers]
\r
234 def to_s(watchers=false)
\r
236 a = self.to_a.flatten
\r
240 a.compact.join(" | ")
\r
244 class RSSFeedsPlugin < Plugin
\r
245 Config.register Config::IntegerValue.new('rss.head_max',
\r
246 :default => 100, :validate => Proc.new{|v| v > 0 && v < 200},
\r
247 :desc => "How many characters to use of a RSS item header")
\r
249 Config.register Config::IntegerValue.new('rss.text_max',
\r
250 :default => 200, :validate => Proc.new{|v| v > 0 && v < 400},
\r
251 :desc => "How many characters to use of a RSS item text")
\r
253 Config.register Config::IntegerValue.new('rss.thread_sleep',
\r
254 :default => 300, :validate => Proc.new{|v| v > 30},
\r
255 :desc => "How many seconds to sleep before checking RSS feeds again")
\r
257 Config.register Config::BooleanValue.new('rss.show_updated',
\r
259 :desc => "Whether feed items for which the description was changed should be shown as new")
\r
261 # We used to save the Mutex with the RssBlob, which was idiotic. And
\r
262 # since Mutexes dumped in one version might not be resotrable in another,
\r
263 # we need a few tricks to be able to restore data from other versions of Ruby
\r
265 # When migrating 1.8.6 => 1.8.5, all we need to do is define an empty
\r
266 # #marshal_load() method for Mutex. For 1.8.5 => 1.8.6 we need something
\r
267 # dirtier, as seen later on in the initialization code.
\r
268 unless Mutex.new.respond_to?(:marshal_load)
\r
270 def marshal_load(str)
\r
280 if @registry.has_key?(:feeds)
\r
281 # When migrating from Ruby 1.8.5 to 1.8.6, dumped Mutexes may render the
\r
282 # data unrestorable. If this happens, we patch the data, thus allowing
\r
283 # the restore to work.
\r
285 # This is actually pretty safe for a number of reasons:
\r
286 # * the code is only called if standard marshalling fails
\r
287 # * the string we look for is quite unlikely to appear randomly
\r
288 # * if the string appears somewhere and the patched string isn't recoverable
\r
289 # either, we'll get another (unrecoverable) error, which makes the rss
\r
290 # plugin unsable, just like it was if no recovery was attempted
\r
291 # * if the string appears somewhere and the patched string is recoverable,
\r
292 # we may get a b0rked feed, which is eventually overwritten by a clean
\r
293 # one, so the worst thing that can happen is that a feed update spams
\r
294 # the watchers once
\r
295 @registry.recovery = Proc.new { |val|
\r
296 patched = val.sub(":\v@mutexo:\nMutex", ":\v@mutexo:\vObject")
\r
297 ret = Marshal.restore(patched)
\r
298 ret.each_value { |blob|
\r
304 @feeds = @registry[:feeds]
\r
305 raise unless @feeds
\r
307 @registry.recovery = nil
\r
309 @feeds.keys.grep(/[A-Z]/) { |k|
\r
310 @feeds[k.downcase] = @feeds[k]
\r
313 @feeds.each { |k, f|
\r
314 f.mutex = Mutex.new
\r
315 f.sanitize_watchers
\r
316 parseRss(f) if f.xml
\r
330 @feeds.select { |h, f| f.watched? }
\r
339 unparsed = Hash.new()
\r
340 @feeds.each { |k, f|
\r
341 unparsed[k] = f.dup
\r
342 # we don't want to save the mutex
\r
343 unparsed[k].mutex = nil
\r
345 @registry[:feeds] = unparsed
\r
348 def stop_watch(handle)
\r
349 if @watch.has_key?(handle)
\r
351 debug "Stopping watch #{handle}"
\r
352 @bot.timer.remove(@watch[handle])
\r
353 @watch.delete(handle)
\r
354 rescue Exception => e
\r
355 report_problem("Failed to stop watch for #{handle}", e, nil)
\r
361 @watch.each_key { |k|
\r
366 def help(plugin,topic="")
\r
369 "rss show #{Bold}handle#{Bold} [#{Bold}limit#{Bold}] : show #{Bold}limit#{Bold} (default: 5, max: 15) entries from rss #{Bold}handle#{Bold}; #{Bold}limit#{Bold} can also be in the form a..b, to display a specific range of items"
\r
371 "rss list [#{Bold}handle#{Bold}] : list all rss feeds (matching #{Bold}handle#{Bold})"
\r
373 "rss watched [#{Bold}handle#{Bold}] [in #{Bold}chan#{Bold}]: list all watched rss feeds (matching #{Bold}handle#{Bold}) (in channel #{Bold}chan#{Bold})"
\r
374 when "who", "watches", "who watches"
\r
375 "rss who watches [#{Bold}handle#{Bold}]]: list all watchers for rss feeds (matching #{Bold}handle#{Bold})"
\r
377 "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
379 "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
380 when /^(del(ete)?|rm)$/
\r
381 "rss del(ete)|rm #{Bold}handle#{Bold} : delete rss feed #{Bold}handle#{Bold}"
\r
383 "rss replace #{Bold}handle#{Bold} #{Bold}url#{Bold} [#{Bold}type#{Bold}] : try to replace the url of rss called #{Bold}handle#{Bold} with #{Bold}url#{Bold} (of type #{Bold}type#{Bold}); only works if nobody else is watching it"
\r
384 when "forcereplace"
\r
385 "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
387 "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
388 when /(un|rm)watch/
\r
389 "rss unwatch|rmwatch #{Bold}handle#{Bold} [in #{Bold}chan#{Bold}]: stop watching rss #{Bold}handle#{Bold} (in channel #{Bold}chan#{Bold}) for changes"
\r
391 "rss rewatch : restart threads that watch for changes in watched rss"
\r
393 "manage RSS feeds: rss show|list|watched|add|change|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch"
\r
397 def report_problem(report, e=nil, m=nil)
\r
398 if m && m.respond_to?(:reply)
\r
405 debug e.backtrace.join("\n") if e.respond_to?(:backtrace)
\r
409 def show_rss(m, params)
\r
410 handle = params[:handle]
\r
411 lims = params[:limit].to_s.match(/(\d+)(?:..(\d+))?/)
\r
412 debug lims.to_a.inspect
\r
414 ll = [[lims[1].to_i-1,lims[2].to_i-1].min, 0].max
\r
415 ul = [[lims[1].to_i-1,lims[2].to_i-1].max, 14].min
\r
416 rev = lims[1].to_i > lims[2].to_i
\r
419 ul = [[lims[1].to_i-1, 0].max, 14].min
\r
423 feed = @feeds.fetch(handle.downcase, nil)
\r
425 m.reply "I don't know any feeds named #{handle}"
\r
429 m.reply "lemme fetch it..."
\r
430 title = items = nil
\r
431 we_were_watching = false
\r
433 if @watch.key?(feed.handle)
\r
434 # If a feed is being watched, we run the watcher thread
\r
435 # so that all watchers can be informed of changes to
\r
436 # the feed. Before we do that, though, we remove the
\r
437 # show requester from the watchlist, if present, lest
\r
438 # he gets the update twice.
\r
439 if feed.watched_by?(m.replyto)
\r
440 we_were_watching = true
\r
441 feed.rm_watch(m.replyto)
\r
443 @bot.timer.reschedule(@watch[feed.handle], 0)
\r
444 if we_were_watching
\r
445 feed.add_watch(m.replyto)
\r
448 fetched = fetchRss(feed, m, false)
\r
450 return unless fetched or feed.xml
\r
451 if not fetched and feed.items
\r
452 m.reply "using old data"
\r
454 parsed = parseRss(feed, m)
\r
455 m.reply "using old data" unless parsed
\r
457 return unless feed.items
\r
461 # We sort the feeds in freshness order (newer ones first)
\r
462 items = freshness_sort(items)
\r
463 disp = items[ll..ul]
\r
464 disp.reverse! if rev
\r
466 m.reply "Channel : #{title}"
\r
467 disp.each do |item|
\r
468 printFormattedRss(feed, item, {:places=>[m.replyto],:handle=>nil,:date=>true})
\r
472 def itemDate(item,ex=nil)
\r
473 return item.pubDate if item.respond_to?(:pubDate) and item.pubDate
\r
474 return item.date if item.respond_to?(:date) and item.date
\r
478 def freshness_sort(items)
\r
479 notime = Time.at(0)
\r
480 items.sort { |a, b|
\r
481 itemDate(b, notime) <=> itemDate(a, notime)
\r
485 def list_rss(m, params)
\r
486 wanted = params[:handle]
\r
488 @feeds.each { |handle, feed|
\r
489 next if wanted and !handle.match(/#{wanted}/i)
\r
490 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"
\r
491 (reply << " refreshing every #{Utils.secs_to_string(feed.refresh_rate)}") if feed.refresh_rate
\r
492 (reply << " (watched)") if feed.watched_by?(m.replyto)
\r
496 reply = "no feeds found"
\r
497 reply << " matching #{wanted}" if wanted
\r
499 m.reply reply, :max_lines => reply.length
\r
502 def watched_rss(m, params)
\r
503 wanted = params[:handle]
\r
504 chan = params[:chan] || m.replyto
\r
506 watchlist.each { |handle, feed|
\r
507 next if wanted and !handle.match(/#{wanted}/i)
\r
508 next unless feed.watched_by?(chan)
\r
509 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"
\r
510 (reply << " refreshing every #{Utils.secs_to_string(feed.refresh_rate)}") if feed.refresh_rate
\r
514 reply = "no watched feeds"
\r
515 reply << " matching #{wanted}" if wanted
\r
520 def who_watches(m, params)
\r
521 wanted = params[:handle]
\r
523 watchlist.each { |handle, feed|
\r
524 next if wanted and !handle.match(/#{wanted}/i)
\r
525 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"
\r
526 (reply << " refreshing every #{Utils.secs_to_string(feed.refresh_rate)}") if feed.refresh_rate
\r
527 reply << ": watched by #{feed.watchers.join(', ')}"
\r
531 reply = "no watched feeds"
\r
532 reply << " matching #{wanted}" if wanted
\r
537 def add_rss(m, params, force=false)
\r
538 handle = params[:handle]
\r
540 unless url.match(/https?/)
\r
541 m.reply "I only deal with feeds from HTTP sources, so I can't use #{url} (maybe you forgot the handle?)"
\r
544 type = params[:type]
\r
545 if @feeds.fetch(handle.downcase, nil) && !force
\r
546 m.reply "There is already a feed named #{handle} (URL: #{@feeds[handle.downcase].url})"
\r
550 m.reply "You must specify both a handle and an url to add an RSS feed"
\r
553 @feeds[handle.downcase] = RssBlob.new(url,handle,type)
\r
554 reply = "Added RSS #{url} named #{handle}"
\r
556 reply << " (format: #{type})"
\r
562 def change_rss(m, params)
\r
563 handle = params[:handle].downcase
\r
564 feed = @feeds.fetch(handle, nil)
\r
566 m.reply "No such feed with handle #{handle}"
\r
569 case params[:what].intern
\r
571 new = params[:new].downcase
\r
572 if @feeds.key?(new) and @feeds[new]
\r
573 m.reply "There already is a feed with handle #{new}"
\r
576 feed.mutex.synchronize do
\r
578 @feeds.delete(handle)
\r
585 feed.mutex.synchronize do
\r
588 when :format, :type
\r
590 new = nil if new == 'default'
\r
591 feed.mutex.synchronize do
\r
595 new = params[:new].to_i
\r
596 new = nil if new == 0
\r
597 feed.mutex.synchronize do
\r
598 feed.refresh_rate = new
\r
601 m.reply "Don't know how to change #{params[:what]} for feeds"
\r
604 m.reply "Feed changed:"
\r
605 list_rss(m, {:handle => handle})
\r
608 def del_rss(m, params, pass=false)
\r
609 feed = unwatch_rss(m, params, true)
\r
612 m.reply "someone else is watching #{feed.handle}, I won't remove it from my list"
\r
615 @feeds.delete(feed.handle.downcase)
\r
620 def replace_rss(m, params)
\r
621 handle = params[:handle]
\r
622 if @feeds.key?(handle.downcase)
\r
623 del_rss(m, {:handle => handle}, true)
\r
625 if @feeds.key?(handle.downcase)
\r
626 m.reply "can't replace #{feed.handle}"
\r
628 add_rss(m, params, true)
\r
632 def forcereplace_rss(m, params)
\r
633 add_rss(m, params, true)
\r
636 def watch_rss(m, params)
\r
637 handle = params[:handle]
\r
638 chan = params[:chan] || m.replyto
\r
640 type = params[:type]
\r
644 feed = @feeds.fetch(handle.downcase, nil)
\r
646 if feed.add_watch(chan)
\r
650 m.reply "Already watching #{feed.handle} in #{chan}"
\r
653 m.reply "Couldn't watch feed #{handle} (no such feed found)"
\r
657 def unwatch_rss(m, params, pass=false)
\r
658 handle = params[:handle].downcase
\r
659 chan = params[:chan] || m.replyto
\r
660 unless @feeds.has_key?(handle)
\r
661 m.reply("dunno that feed")
\r
664 feed = @feeds[handle]
\r
665 if feed.rm_watch(chan)
\r
666 m.reply "#{chan} has been removed from the watchlist for #{feed.handle}"
\r
668 m.reply("#{chan} wasn't watching #{feed.handle}") unless pass
\r
676 def rewatch_rss(m=nil, params=nil)
\r
679 # Read watches from list.
\r
680 watchlist.each{ |handle, feed|
\r
687 def watchRss(feed, m=nil)
\r
688 if @watch.has_key?(feed.handle)
\r
689 report_problem("watcher thread for #{feed.handle} is already running", nil, m)
\r
693 status[:failures] = 0
\r
694 status[:first_run] = true
\r
695 @watch[feed.handle] = @bot.timer.add(0) {
\r
696 debug "watcher for #{feed} started"
\r
697 failures = status[:failures]
\r
698 first_run = status.delete(:first_run)
\r
700 debug "fetching #{feed}"
\r
701 oldxml = feed.xml ? feed.xml.dup : nil
\r
702 unless fetchRss(feed)
\r
706 debug "first run for #{feed}, getting items"
\r
708 elsif oldxml and oldxml == feed.xml
\r
709 debug "xml for #{feed} didn't change"
\r
710 failures -= 1 if failures > 0
\r
713 debug "no previous items in feed #{feed}"
\r
715 failures -= 1 if failures > 0
\r
717 # This one is used for debugging
\r
720 # These are used for checking new items vs old ones
\r
721 uid_opts = { :show_updated => @bot.config['rss.show_updated'] }
\r
722 oids = Set.new feed.items.map { |item|
\r
723 uid = RSS.item_uid_for_bot(item, uid_opts)
\r
725 debug [uid, item].inspect
\r
726 debug [uid, otxt.last].inspect
\r
730 unless parseRss(feed)
\r
731 debug "no items in feed #{feed}"
\r
734 debug "Checking if new items are available for #{feed}"
\r
735 failures -= 1 if failures > 0
\r
741 dispItems = feed.items.reject { |item|
\r
742 uid = RSS.item_uid_for_bot(item, uid_opts)
\r
744 if oids.include?(uid)
\r
745 debug "rejecting old #{uid} #{item.inspect}"
\r
746 debug [uid, txt].inspect
\r
749 debug "accepting new #{uid} #{item.inspect}"
\r
750 debug [uid, txt].inspect
\r
751 warning "same text! #{txt}" if otxt.include?(txt)
\r
756 if dispItems.length > 0
\r
757 debug "Found #{dispItems.length} new items in #{feed}"
\r
758 # When displaying watched feeds, publish them from older to newer
\r
759 dispItems.reverse.each { |item|
\r
760 printFormattedRss(feed, item)
\r
763 debug "No new items found in #{feed}"
\r
769 rescue Exception => e
\r
770 error "Error watching #{feed}: #{e.inspect}"
\r
771 debug e.backtrace.join("\n")
\r
775 status[:failures] = failures
\r
778 seconds = @bot.config['rss.thread_sleep']
\r
779 feed.mutex.synchronize do
\r
780 timer = @watch[feed.handle]
\r
781 seconds = feed.refresh_rate if feed.refresh_rate
\r
783 seconds *= failures + 1
\r
784 seconds += seconds * (rand(100)-50)/100
\r
785 debug "watcher for #{feed} going to sleep #{seconds} seconds.."
\r
786 @bot.timer.reschedule(timer, seconds) rescue warning "watcher for #{feed} failed to reschedule: #{$!.inspect}"
\r
788 debug "watcher for #{feed} added"
\r
791 def printFormattedRss(feed, item, opts=nil)
\r
792 places = feed.watchers
\r
793 handle = "::#{feed.handle}:: "
\r
796 places = opts[:places] if opts.key?(:places)
\r
797 handle = opts[:handle].to_s if opts.key?(:handle)
\r
798 if opts.key?(:date) && opts[:date]
\r
799 if item.respond_to?(:pubDate)
\r
800 if item.pubDate.class <= Time
\r
801 date = item.pubDate.strftime("%Y/%m/%d %H:%M")
\r
803 date = item.pubDate.to_s
\r
805 elsif item.respond_to?(:date)
\r
806 if item.date.class <= Time
\r
807 date = item.date.strftime("%Y/%m/%d %H:%M")
\r
809 date = item.date.to_s
\r
819 # Twitters don't need a cap on the title length since they have a hard
\r
820 # limit to 160 characters, and most of them are under 140 characters
\r
821 tit_opt[:limit] = @bot.config['rss.head_max'] unless feed.type == 'twitter'
\r
823 title = "#{Bold}#{item.title.ircify_html(tit_opt)}#{Bold}" if item.title
\r
826 :limit => @bot.config['rss.text_max'],
\r
827 :a_href => :link_out
\r
829 desc = item.description.ircify_html(desc_opt) if item.description
\r
831 link = item.link.chomp if item.link
\r
834 category = item.dc_subject rescue item.category.content rescue nil
\r
835 category = nil if category and category.empty?
\r
836 author = item.dc_creator rescue item.author rescue nil
\r
837 author = nil if author and author.empty?
\r
842 at = ((item.title && item.link) ? ' @ ' : '')
\r
845 author << " " if author
\r
846 abt = category ? "about #{category} " : ""
\r
847 line1 = "#{handle}#{date}#{author}blogged #{abt}at #{link}"
\r
848 line2 = "#{handle}#{title} - #{desc}"
\r
850 line1 = "#{handle}#{date}#{title}#{at}#{link}"
\r
852 line1 = "#{handle}#{date}#{title}#{at}#{link} has been edited by #{author}. #{desc}"
\r
854 line1 = "#{handle}#{date}Message #{title} sent by #{author}. #{desc}"
\r
856 line1 = "#{handle}#{date}#{title} @ #{link}"
\r
857 unless item.title =~ /^Changeset \[(\d+)\]/
\r
858 line2 = "#{handle}#{date}#{desc}"
\r
861 dept = "(from the #{item.slash_department} dept) " rescue nil
\r
862 sec = " in section #{item.slash_section}" rescue nil
\r
864 line1 = "#{handle}#{date}#{dept}#{title}#{at}#{link} (posted by #{author}#{sec})"
\r
866 line1 = "#{handle}#{date}#{title}#{at}#{link}"
\r
868 places.each { |loc|
\r
869 @bot.say loc, line1, :overlong => :truncate
\r
871 @bot.say loc, line2, :overlong => :truncate
\r
875 def fetchRss(feed, m=nil, cache=true)
\r
877 # Use 60 sec timeout, cause the default is too low
\r
878 xml = @bot.httputil.get(feed.url,
\r
879 :read_timeout => 60,
\r
880 :open_timeout => 60,
\r
882 rescue URI::InvalidURIError, URI::BadURIError => e
\r
883 report_problem("invalid rss feed #{feed.url}", e, m)
\r
886 report_problem("error getting #{feed.url}", e, m)
\r
889 debug "fetched #{feed}"
\r
891 report_problem("reading feed #{feed} failed", nil, m)
\r
894 # Ok, 0.9 feeds are not supported, maybe because
\r
895 # Netscape happily removed the DTD. So what we do is just to
\r
896 # reassign the 0.9 RDFs to 1.0, and hope it goes right.
\r
897 xml.gsub!("xmlns=\"http://my.netscape.com/rdf/simple/0.9/\"",
\r
898 "xmlns=\"http://purl.org/rss/1.0/\"")
\r
899 feed.mutex.synchronize do
\r
905 def parseRss(feed, m=nil)
\r
906 return nil unless feed.xml
\r
907 feed.mutex.synchronize do
\r
910 ## do validate parse
\r
911 rss = RSS::Parser.parse(xml)
\r
912 debug "parsed and validated #{feed}"
\r
913 rescue RSS::InvalidRSSError
\r
914 ## do non validate parse for invalid RSS 1.0
\r
916 rss = RSS::Parser.parse(xml, false)
\r
917 debug "parsed but not validated #{feed}"
\r
918 rescue RSS::Error => e
\r
919 report_problem("parsing rss stream failed, whoops =(", e, m)
\r
922 rescue RSS::Error => e
\r
923 report_problem("parsing rss stream failed, oioi", e, m)
\r
926 report_problem("processing error occured, sorry =(", e, m)
\r
931 report_problem("#{feed} does not include RSS 1.0 or 0.9x/2.0", nil, m)
\r
934 rss.output_encoding = 'UTF-8'
\r
935 rescue RSS::UnknownConvertMethod => e
\r
936 report_problem("bah! something went wrong =(", e, m)
\r
939 rss.channel.title ||= "Unknown"
\r
940 title = rss.channel.title
\r
941 rss.items.each do |item|
\r
942 item.title ||= "Unknown"
\r
948 report_problem("no items found in the feed, maybe try weed?", e, m)
\r
958 plugin = RSSFeedsPlugin.new
\r
960 plugin.map 'rss show :handle :limit',
\r
961 :action => 'show_rss',
\r
962 :requirements => {:limit => /^\d+(?:\.\.\d+)?$/},
\r
963 :defaults => {:limit => 5}
\r
964 plugin.map 'rss list :handle',
\r
965 :action => 'list_rss',
\r
966 :defaults => {:handle => nil}
\r
967 plugin.map 'rss watched :handle [in :chan]',
\r
968 :action => 'watched_rss',
\r
969 :defaults => {:handle => nil}
\r
970 plugin.map 'rss who watches :handle',
\r
971 :action => 'who_watches',
\r
972 :defaults => {:handle => nil}
\r
973 plugin.map 'rss add :handle :url :type',
\r
974 :action => 'add_rss',
\r
975 :defaults => {:type => nil}
\r
976 plugin.map 'rss change :what of :handle to :new',
\r
977 :action => 'change_rss',
\r
978 :requirements => { :what => /handle|url|format|type|refresh/ }
\r
979 plugin.map 'rss change :what for :handle to :new',
\r
980 :action => 'change_rss',
\r
981 :requirements => { :what => /handle|url|format|type|refesh/ }
\r
982 plugin.map 'rss del :handle',
\r
983 :action => 'del_rss'
\r
984 plugin.map 'rss delete :handle',
\r
985 :action => 'del_rss'
\r
986 plugin.map 'rss rm :handle',
\r
987 :action => 'del_rss'
\r
988 plugin.map 'rss replace :handle :url :type',
\r
989 :action => 'replace_rss',
\r
990 :defaults => {:type => nil}
\r
991 plugin.map 'rss forcereplace :handle :url :type',
\r
992 :action => 'forcereplace_rss',
\r
993 :defaults => {:type => nil}
\r
994 plugin.map 'rss watch :handle [in :chan]',
\r
995 :action => 'watch_rss',
\r
996 :defaults => {:url => nil, :type => nil}
\r
997 plugin.map 'rss watch :handle :url :type [in :chan]',
\r
998 :action => 'watch_rss',
\r
999 :defaults => {:url => nil, :type => nil}
\r
1000 plugin.map 'rss unwatch :handle [in :chan]',
\r
1001 :action => 'unwatch_rss'
\r
1002 plugin.map 'rss rmwatch :handle [in :chan]',
\r
1003 :action => 'unwatch_rss'
\r
1004 plugin.map 'rss rewatch',
\r
1005 :action => 'rewatch_rss'
\r