1 # RSS feed plugin for RubyBot
\r
2 # (c) 2004 Stanislav Karchebny <berkus@madfire.net>
\r
3 # (c) 2005 Ian Monroe <ian@monroe.nu>
\r
4 # (c) 2005 Mark Kretschmann <markey@web.de>
\r
5 # Licensed under MIT License.
\r
10 require 'rss/dublincore'
\r
12 # require 'rss/dublincore/2.0'
\r
14 warning "Unable to load RSS libraries, RSS plugin functionality crippled"
\r
19 if self.length > limit
\r
20 self+". " =~ /^(.{#{limit}}[^.!;?]*[.!;?])/mi
\r
27 self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')
\r
31 self.gsub(/'/, "''")
\r
41 def initialize(url,handle=nil,type=nil,watchers=[])
\r
49 @watchers = watchers
\r
56 def watched_by?(who)
\r
57 @watchers.include?(who)
\r
64 @watchers << who unless watched_by?(who)
\r
69 @watchers.delete(who)
\r
73 [@handle,@url,@type,@watchers]
\r
76 def to_s(watchers=false)
\r
78 a = self.to_a.flatten
\r
86 class RSSFeedsPlugin < Plugin
\r
87 BotConfig.register BotConfigIntegerValue.new('rss.head_max',
\r
88 :default => 30, :validate => Proc.new{|v| v > 0 && v < 200},
\r
89 :desc => "How many characters to use of a RSS item header")
\r
91 BotConfig.register BotConfigIntegerValue.new('rss.text_max',
\r
92 :default => 90, :validate => Proc.new{|v| v > 0 && v < 400},
\r
93 :desc => "How many characters to use of a RSS item text")
\r
95 BotConfig.register BotConfigIntegerValue.new('rss.thread_sleep',
\r
96 :default => 120, :validate => Proc.new{|v| v > 30},
\r
97 :desc => "How many characters to use of a RSS item text")
\r
99 @@watchThreads = Hash.new
\r
100 @@mutex = Mutex.new
\r
105 if @registry.has_key?(:feeds)
\r
106 @feeds = @registry[:feeds]
\r
114 @feeds.select { |h, f| f.watched? }
\r
122 @registry[:feeds] = @feeds
\r
126 @@mutex.synchronize {
\r
127 # Abort all running threads.
\r
128 @@watchThreads.each { |url, thread|
\r
129 debug "Killing thread for #{url}"
\r
132 @@watchThreads = Hash.new
\r
136 def help(plugin,topic="")
\r
139 "rss show #{Bold}handle#{Bold} [#{Bold}limit#{Bold}] : show #{Bold}limit#{Bold} (default: 5, max: 15) entries from rss #{Bold}handle#{Bold}"
\r
141 "rss list [#{Bold}handle#{Bold}] : list all rss feeds (matching #{Bold}handle#{Bold})"
\r
143 "rss watched [#{Bold}handle#{Bold}] : list all watched rss feeds (matching #{Bold}handle#{Bold})"
\r
145 "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
146 when /^(del(ete)?|rm)$/
\r
147 "rss del(ete)|rm #{Bold}handle#{Bold} : delete rss feed #{Bold}handle#{Bold}"
\r
149 "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
150 when "forcereplace"
\r
151 "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
153 "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
154 when /(un|rm)watch/
\r
155 "rss unwatch|rmwatch #{Bold}handle#{Bold} : stop watching rss #{Bold}handle#{Bold} for changes"
\r
157 "rss rewatch : restart threads that watch for changes in watched rss"
\r
159 "manage RSS feeds: rss show|list|watched|add|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch"
\r
163 def report_problem(report, e=nil, m=nil)
\r
164 if m && m.respond_to?(reply)
\r
171 debug e.backtrace.join("\n") if e.class >= Exception
\r
175 def show_rss(m, params)
\r
176 handle = params[:handle]
\r
177 limit = params[:limit].to_i
\r
178 limit = 15 if limit > 15
\r
179 limit = 1 if limit <= 0
\r
180 feed = @feeds.fetch(handle, nil)
\r
182 m.reply "I don't know any feeds named #{handle}"
\r
185 m.reply("Please wait, querying...")
\r
186 title = items = nil
\r
187 @@mutex.synchronize {
\r
188 title, items = fetchRss(feed, m)
\r
190 return unless items
\r
191 m.reply("Channel : #{title}")
\r
192 # TODO: optional by-date sorting if dates present
\r
193 items[0...limit].reverse.each do |item|
\r
194 printFormattedRss(feed, item, {:places=>[m.replyto],:handle=>nil,:date=>true})
\r
198 def list_rss(m, params)
\r
199 wanted = params[:handle]
\r
201 @@mutex.synchronize {
\r
202 @feeds.each { |handle, feed|
\r
203 next if wanted and !handle.match(wanted)
\r
204 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"
\r
205 (reply << " (watched)") if feed.watched_by?(m.replyto)
\r
210 reply = "no feeds found"
\r
211 reply << " matching #{wanted}" if wanted
\r
216 def watched_rss(m, params)
\r
217 wanted = params[:handle]
\r
219 @@mutex.synchronize {
\r
220 watchlist.each { |handle, feed|
\r
221 next if wanted and !handle.match(wanted)
\r
222 next unless feed.watched_by?(m.replyto)
\r
223 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})\n"
\r
227 reply = "no watched feeds"
\r
228 reply << " matching #{wanted}" if wanted
\r
233 def add_rss(m, params, force=false)
\r
234 handle = params[:handle]
\r
236 unless url.match(/https?/)
\r
237 m.reply "I only deal with feeds from HTTP sources, so I can't use #{url} (maybe you forgot the handle?)"
\r
240 type = params[:type]
\r
241 if @feeds.fetch(handle, nil) && !force
\r
242 m.reply "There is already a feed named #{handle} (URL: #{@feeds[handle].url})"
\r
246 m.reply "You must specify both a handle and an url to add an RSS feed"
\r
249 @@mutex.synchronize {
\r
250 @feeds[handle] = RssBlob.new(url,handle,type)
\r
252 reply = "Added RSS #{url} named #{handle}"
\r
254 reply << " (format: #{type})"
\r
260 def del_rss(m, params, pass=false)
\r
261 feed = unwatch_rss(m, params, true)
\r
263 m.reply "someone else is watching #{feed.handle}, I won't remove it from my list"
\r
266 @@mutex.synchronize {
\r
267 @feeds.delete(feed.handle)
\r
273 def replace_rss(m, params)
\r
274 handle = params[:handle]
\r
275 if @feeds.key?(handle)
\r
276 del_rss(m, {:handle => handle}, true)
\r
278 if @feeds.key?(handle)
\r
279 m.reply "can't replace #{feed.handle}"
\r
281 add_rss(m, params, true)
\r
285 def forcereplace_rss(m, params)
\r
286 add_rss(m, params, true)
\r
289 def watch_rss(m, params)
\r
290 handle = params[:handle]
\r
292 type = params[:type]
\r
297 @@mutex.synchronize {
\r
298 feed = @feeds.fetch(handle, nil)
\r
301 @@mutex.synchronize {
\r
302 if feed.add_watch(m.replyto)
\r
306 m.reply "Already watching #{feed.handle}"
\r
310 m.reply "Couldn't watch feed #{handle} (no such feed found)"
\r
314 def unwatch_rss(m, params, pass=false)
\r
315 handle = params[:handle]
\r
316 unless @feeds.has_key?(handle)
\r
317 m.reply("dunno that feed")
\r
320 feed = @feeds[handle]
\r
321 if feed.rm_watch(m.replyto)
\r
322 m.reply "#{m.replyto} has been removed from the watchlist for #{feed.handle}"
\r
324 m.reply("#{m.replyto} wasn't watching #{feed.handle}") unless pass
\r
327 @@mutex.synchronize {
\r
328 if @@watchThreads[handle].kind_of? Thread
\r
329 @@watchThreads[handle].kill
\r
330 debug "rmwatch: Killed thread for #{handle}"
\r
331 @@watchThreads.delete(handle)
\r
338 def rewatch_rss(m=nil)
\r
341 # Read watches from list.
\r
342 watchlist.each{ |handle, feed|
\r
349 def watchRss(feed, m=nil)
\r
350 if @@watchThreads.has_key?(feed.handle)
\r
351 report_problem("watcher thread for #{feed.handle} is already running", nil, m)
\r
354 @@watchThreads[feed.handle] = Thread.new do
\r
355 debug "watcher for #{feed} started"
\r
360 debug "fetching #{feed}"
\r
361 title = newItems = nil
\r
362 @@mutex.synchronize {
\r
363 title, newItems = fetchRss(feed)
\r
366 m.reply "no items in feed"
\r
369 debug "Checking if new items are available for #{feed}"
\r
371 debug "First run, we'll see next time"
\r
374 otxt = oldItems.map { |item| item.to_s }
\r
375 dispItems = newItems.reject { |item|
\r
376 otxt.include?(item.to_s)
\r
378 if dispItems.length > 0
\r
379 debug "Found #{dispItems.length} new items in #{feed}"
\r
380 dispItems.each { |item|
\r
381 @@mutex.synchronize {
\r
382 printFormattedRss(feed, item)
\r
386 debug "No new items found in #{feed}"
\r
389 oldItems = newItems.dup
\r
390 rescue Exception => e
\r
391 error "IO failed: #{e.inspect}"
\r
392 debug e.backtrace.join("\n")
\r
395 seconds = @bot.config['rss.thread_sleep']
\r
396 seconds += seconds * rand(50)/100
\r
397 debug "watcher for #{feed} going to sleep #{seconds} seconds.."
\r
403 def printFormattedRss(feed, item, opts=nil)
\r
404 places = feed.watchers
\r
405 handle = "::#{feed.handle}:: "
\r
408 places = opts[:places] if opts.key?(:places)
\r
409 handle = opts[:handle].to_s if opts.key?(:handle)
\r
410 date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S") + " :: " if (opts.key?(:date) && opts[:date] && item.pubDate)
\r
412 title = "#{Bold}#{item.title.chomp.riphtml}#{Bold}" if item.title
\r
413 desc = item.description.gsub(/\s+/,' ').strip.riphtml.shorten(@bot.config['rss.text_max']) if item.description
\r
414 link = item.link.chomp if item.link
\r
415 places.each { |loc|
\r
418 @bot.say loc, "#{handle}#{date}#{item.category.content} blogged at #{link}"
\r
419 @bot.say loc, "#{handle}#{title} - #{desc}"
\r
421 @bot.say loc, "#{handle}#{date}#{title}#{' @ ' if item.title && item.link}#{link}"
\r
423 @bot.say loc, "#{handle}#{date}#{item.title} has been edited by #{item.dc_creator}. #{desc} #{link}"
\r
425 @bot.say loc, "#{handle}#{date}Message #{title} sent by #{item.dc_creator}. #{desc}"
\r
427 @bot.say loc, "#{handle}#{date}#{title} @ #{link}"
\r
428 unless item.title =~ /^Changeset \[(\d+)\]/
\r
429 @bot.say loc, "#{handle}#{desc}"
\r
432 @bot.say loc, "#{handle}#{date}#{title}#{' @ ' if item.title && item.link}#{link}"
\r
437 def fetchRss(feed, m=nil)
\r
439 # Use 60 sec timeout, cause the default is too low
\r
440 xml = @bot.httputil.get_cached(feed.url,60,60)
\r
441 rescue URI::InvalidURIError, URI::BadURIError => e
\r
442 report_problem("invalid rss feed #{feed.url}", e, m)
\r
445 debug "fetched #{feed}"
\r
447 report_problem("reading feed #{feed} failed", nil, m)
\r
452 ## do validate parse
\r
453 rss = RSS::Parser.parse(xml)
\r
454 debug "parsed #{feed}"
\r
455 rescue RSS::InvalidRSSError
\r
456 ## do non validate parse for invalid RSS 1.0
\r
458 rss = RSS::Parser.parse(xml, false)
\r
459 rescue RSS::Error => e
\r
460 report_problem("parsing rss stream failed, whoops =(", e, m)
\r
463 rescue RSS::Error => e
\r
464 report_problem("parsing rss stream failed, oioi", e, m)
\r
467 report_problem("processing error occured, sorry =(", e, m)
\r
472 report_problem("#{feed} does not include RSS 1.0 or 0.9x/2.0", nil, m)
\r
475 rss.output_encoding = 'UTF-8'
\r
476 rescue RSS::UnknownConvertMethod => e
\r
477 report_problem("bah! something went wrong =(", e, m)
\r
480 rss.channel.title ||= "Unknown"
\r
481 title = rss.channel.title
\r
482 rss.items.each do |item|
\r
483 item.title ||= "Unknown"
\r
489 report_problem("no items found in the feed, maybe try weed?", e, m)
\r
492 return [title, items]
\r
496 plugin = RSSFeedsPlugin.new
\r
498 plugin.map 'rss show :handle :limit',
\r
499 :action => 'show_rss',
\r
500 :requirements => {:limit => /^\d+$/},
\r
501 :defaults => {:limit => 5}
\r
502 plugin.map 'rss list :handle',
\r
503 :action => 'list_rss',
\r
504 :defaults => {:handle => nil}
\r
505 plugin.map 'rss watched :handle',
\r
506 :action => 'watched_rss',
\r
507 :defaults => {:handle => nil}
\r
508 plugin.map 'rss add :handle :url :type',
\r
509 :action => 'add_rss',
\r
510 :defaults => {:type => nil}
\r
511 plugin.map 'rss del :handle',
\r
512 :action => 'del_rss'
\r
513 plugin.map 'rss delete :handle',
\r
514 :action => 'del_rss'
\r
515 plugin.map 'rss rm :handle',
\r
516 :action => 'del_rss'
\r
517 plugin.map 'rss replace :handle :url :type',
\r
518 :action => 'replace_rss',
\r
519 :defaults => {:type => nil}
\r
520 plugin.map 'rss forcereplace :handle :url :type',
\r
521 :action => 'forcereplace_rss',
\r
522 :defaults => {:type => nil}
\r
523 plugin.map 'rss watch :handle :url :type',
\r
524 :action => 'watch_rss',
\r
525 :defaults => {:url => nil, :type => nil}
\r
526 plugin.map 'rss unwatch :handle',
\r
527 :action => 'unwatch_rss'
\r
528 plugin.map 'rss rmwatch :handle',
\r
529 :action => 'unwatch_rss'
\r
530 plugin.map 'rss rewatch :handle',
\r
531 :action => 'rewatch_rss'
\r