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 @@watchThreads = Hash.new
\r
101 if @registry.has_key?(:feeds)
\r
102 @feeds = @registry[:feeds]
\r
110 @feeds.select { |h, f| f.watched? }
\r
118 @registry[:feeds] = @feeds
\r
122 @@mutex.synchronize {
\r
123 # Abort all running threads.
\r
124 @@watchThreads.each { |url, thread|
\r
125 debug "Killing thread for #{url}"
\r
128 @@watchThreads = Hash.new
\r
132 def help(plugin,topic="")
\r
135 "rss show #{Bold}handle#{Bold} [#{Bold}limit#{Bold}] : show #{Bold}limit#{Bold} (default: 5, max: 15) entries from rss #{Bold}handle#{Bold}"
\r
137 "rss list [#{Bold}handle#{Bold}] : list all rss feeds (matching #{Bold}handle#{Bold})"
\r
139 "rss watched [#{Bold}handle#{Bold}] : list all watched rss feeds (matching #{Bold}handle#{Bold})"
\r
141 "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
142 when /^(del(ete)?|rm)$/
\r
143 "rss del(ete)|rm #{Bold}handle#{Bold} : delete rss feed #{Bold}handle#{Bold}"
\r
145 "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
146 when "forcereplace"
\r
147 "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
149 "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
150 when /(un|rm)watch/
\r
151 "rss unwatch|rmwatch #{Bold}handle#{Bold} : stop watching rss #{Bold}handle#{Bold} for changes"
\r
153 "rss rewatch : restart threads that watch for changes in watched rss"
\r
155 "manage RSS feeds: rss show|list|watched|add|del(ete)|rm|(force)replace|watch|unwatch|rmwatch|rewatch"
\r
159 def report_problem(report, m=nil)
\r
167 def show_rss(m, params)
\r
168 handle = params[:handle]
\r
169 limit = params[:limit].to_i
\r
170 limit = 15 if limit > 15
\r
171 limit = 1 if limit <= 0
\r
172 feed = @feeds.fetch(handle, nil)
\r
174 m.reply "I don't know any feeds named #{handle}"
\r
177 m.reply("Please wait, querying...")
\r
178 title = items = nil
\r
179 @@mutex.synchronize {
\r
180 title, items = fetchRss(feed, m)
\r
182 return unless items
\r
183 m.reply("Channel : #{title}")
\r
184 # TODO: optional by-date sorting if dates present
\r
185 items[0...limit].reverse.each do |item|
\r
186 printRssItem(m.replyto,item)
\r
190 def list_rss(m, params)
\r
191 wanted = params[:handle]
\r
193 @@mutex.synchronize {
\r
194 @feeds.each { |handle, feed|
\r
195 next if wanted and !handle.match(wanted)
\r
196 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"
\r
197 (reply << " (watched)") if feed.watched_by?(m.replyto)
\r
202 reply = "no feeds found"
\r
203 reply << " matching #{wanted}" if wanted
\r
208 def watched_rss(m, params)
\r
209 wanted = params[:handle]
\r
211 @@mutex.synchronize {
\r
212 watchlist.each { |handle, feed|
\r
213 next if wanted and !handle.match(wanted)
\r
214 next unless feed.watched_by?(m.replyto)
\r
215 reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})\n"
\r
219 reply = "no watched feeds"
\r
220 reply << " matching #{wanted}" if wanted
\r
225 def add_rss(m, params, force=false)
\r
226 handle = params[:handle]
\r
228 type = params[:type]
\r
229 if @feeds.fetch(handle, nil) && !force
\r
230 m.reply "There is already a feed named #{handle} (URL: #{@feeds[handle].url})"
\r
234 m.reply "You must specify both a handle and an url to add an RSS feed"
\r
237 @@mutex.synchronize {
\r
238 @feeds[handle] = RssBlob.new(url,handle,type)
\r
240 reply = "Added RSS #{url} named #{handle}"
\r
242 reply << " (format: #{type})"
\r
248 def del_rss(m, params, pass=false)
\r
249 feed = unwatch_rss(m, params, true)
\r
251 m.reply "someone else is watching #{feed.handle}, I won't remove it from my list"
\r
254 @@mutex.synchronize {
\r
255 @feeds.delete(feed.handle)
\r
261 def replace_rss(m, params)
\r
262 handle = params[:handle]
\r
263 if @feeds.key?(handle)
\r
264 del_rss(m, {:handle => handle}, true)
\r
266 if @feeds.key?(handle)
\r
267 m.reply "can't replace #{feed.handle}"
\r
269 add_rss(m, params, true)
\r
273 def forcereplace_rss(m, params)
\r
274 add_rss(m, params, true)
\r
277 def watch_rss(m, params)
\r
278 handle = params[:handle]
\r
280 type = params[:type]
\r
285 @@mutex.synchronize {
\r
286 feed = @feeds.fetch(handle, nil)
\r
289 @@mutex.synchronize {
\r
290 if feed.add_watch(m.replyto)
\r
294 m.reply "Already watching #{feed.handle}"
\r
298 m.reply "Couldn't watch feed #{handle} (no such feed found)"
\r
302 def unwatch_rss(m, params, pass=false)
\r
303 handle = params[:handle]
\r
304 unless @feeds.has_key?(handle)
\r
305 m.reply("dunno that feed")
\r
308 feed = @feeds[handle]
\r
309 if feed.rm_watch(m.replyto)
\r
310 m.reply "#{m.replyto} has been removed from the watchlist for #{feed.handle}"
\r
312 m.reply("#{m.replyto} wasn't watching #{feed.handle}") unless pass
\r
315 @@mutex.synchronize {
\r
316 if @@watchThreads[handle].kind_of? Thread
\r
317 @@watchThreads[handle].kill
\r
318 debug "rmwatch: Killed thread for #{handle}"
\r
319 @@watchThreads.delete(handle)
\r
326 def rewatch_rss(m=nil)
\r
329 # Read watches from list.
\r
330 watchlist.each{ |handle, feed|
\r
337 def watchRss(feed, m=nil)
\r
338 if @@watchThreads.has_key?(feed.handle)
\r
339 report_problem("watcher thread for #{feed.handle} is already running", m)
\r
342 @@watchThreads[feed.handle] = Thread.new do
\r
343 debug 'watchRss thread started.'
\r
348 debug 'Fetching rss feed...'
\r
349 title = newItems = nil
\r
350 @@mutex.synchronize {
\r
351 title, newItems = fetchRss(feed)
\r
354 m.reply "no items in feed"
\r
357 debug "Checking if new items are available"
\r
359 debug "First run, we'll see next time"
\r
362 otxt = oldItems.map { |item| item.to_s }
\r
363 dispItems = newItems.reject { |item|
\r
364 otxt.include?(item.to_s)
\r
366 if dispItems.length > 0
\r
367 debug "Found #{dispItems.length} new items"
\r
368 dispItems.each { |item|
\r
369 debug "showing #{item.title}"
\r
370 @@mutex.synchronize {
\r
371 printFormattedRss(feed, item)
\r
375 debug "No new items found"
\r
378 oldItems = newItems.dup
\r
379 rescue Exception => e
\r
380 error "IO failed: #{e.inspect}"
\r
381 debug e.backtrace.join("\n")
\r
384 seconds = 150 + rand(100)
\r
385 debug "Thread going to sleep #{seconds} seconds.."
\r
391 def printRssItem(loc,item)
\r
392 if item.kind_of?(RSS::RDF::Item)
\r
393 @bot.say loc, item.title.chomp.riphtml.shorten(@bot.config['rss.head_max']) + " @ " + item.link
\r
396 desc << item.pubDate.to_s.chomp + ": " if item.pubDate
\r
397 desc << item.title.chomp.riphtml.shorten(@bot.config['rss.head_max']) + " :: " if item.title
\r
398 desc << " @ " + item.link.chomp if item.link
\r
403 def printFormattedRss(feed, item)
\r
404 debug "Printing formatted item #{item.inspect} for feed #{feed.to_s}"
\r
405 feed.watchers.each { |loc|
\r
408 @bot.say loc, "::#{feed.handle}:: #{item.category.content} just blogged at #{item.link}::"
\r
409 @bot.say loc, "::#{feed.handle}:: #{item.title.chomp.riphtml} - #{item.description.chomp.riphtml.shorten(@bot.config['rss.text_max'])}::"
\r
411 @bot.say loc, "::#{feed.handle}:: #{item.pubDate.to_s.chomp+": " if item.pubDate}#{item.title.chomp.riphtml+" :: " if item.title}#{" @ "+item.link.chomp if item.link}"
\r
413 @bot.say loc, "::#{feed.handle}:: #{item.title} has been edited by #{item.dc_creator}. #{item.description.split("\n")[0].chomp.riphtml.shorten(@bot.config['rss.text_max'])} #{item.link} ::"
\r
415 @bot.say loc, "::#{feed.handle}:: Message #{item.title} sent by #{item.dc_creator}. #{item.description.split("\n")[0].chomp.riphtml.shorten(@bot.config['rss.text_max'])} ::"
\r
417 @bot.say loc, "::#{feed.handle}:: #{item.title} :: #{item.link}"
\r
418 @bot.say loc, "::#{feed.handle}:: #{item.description.gsub(/\s+/,' ').strip.riphtml.shorten(@bot.config['rss.text_max'])}"
\r
420 printRssItem(loc,item)
\r
425 def fetchRss(feed, m=nil)
\r
427 # Use 60 sec timeout, cause the default is too low
\r
428 xml = @bot.httputil.get_cached(feed.url,60,60)
\r
429 rescue URI::InvalidURIError, URI::BadURIError => e
\r
430 report_problem("invalid rss feed #{feed.url}", m)
\r
435 report_problem("reading feed #{url} failed", m)
\r
440 ## do validate parse
\r
441 rss = RSS::Parser.parse(xml)
\r
443 rescue RSS::InvalidRSSError
\r
444 ## do non validate parse for invalid RSS 1.0
\r
446 rss = RSS::Parser.parse(xml, false)
\r
448 report_problem("parsing rss stream failed, whoops =(", m)
\r
452 report_problem("parsing rss stream failed, oioi", m)
\r
455 report_problem("processing error occured, sorry =(", m)
\r
457 debug e.backtrace.join("\n")
\r
462 report_problem("#{feed.url} does not include RSS 1.0 or 0.9x/2.0",m)
\r
465 rss.output_encoding = "euc-jp"
\r
466 rescue RSS::UnknownConvertMethod
\r
467 report_problem("bah! something went wrong =(",m)
\r
470 rss.channel.title ||= "Unknown"
\r
471 title = rss.channel.title
\r
472 rss.items.each do |item|
\r
473 item.title ||= "Unknown"
\r
479 report_problem("no items found in the feed, maybe try weed?",m)
\r
482 return [title, items]
\r
486 plugin = RSSFeedsPlugin.new
\r
488 plugin.map 'rss show :handle :limit',
\r
489 :action => 'show_rss',
\r
490 :requirements => {:limit => /^\d+$/},
\r
491 :defaults => {:limit => 5}
\r
492 plugin.map 'rss list :handle',
\r
493 :action => 'list_rss',
\r
494 :defaults => {:handle => nil}
\r
495 plugin.map 'rss watched :handle',
\r
496 :action => 'watched_rss',
\r
497 :defaults => {:handle => nil}
\r
498 plugin.map 'rss add :handle :url :type',
\r
499 :action => 'add_rss',
\r
500 :defaults => {:type => nil}
\r
501 plugin.map 'rss del :handle',
\r
502 :action => 'del_rss'
\r
503 plugin.map 'rss delete :handle',
\r
504 :action => 'del_rss'
\r
505 plugin.map 'rss rm :handle',
\r
506 :action => 'del_rss'
\r
507 plugin.map 'rss replace :handle :url :type',
\r
508 :action => 'replace_rss',
\r
509 :defaults => {:type => nil}
\r
510 plugin.map 'rss forcereplace :handle :url :type',
\r
511 :action => 'forcereplace_rss',
\r
512 :defaults => {:type => nil}
\r
513 plugin.map 'rss watch :handle :url :type',
\r
514 :action => 'watch_rss',
\r
515 :defaults => {:url => nil, :type => nil}
\r
516 plugin.map 'rss unwatch :handle',
\r
517 :action => 'unwatch_rss'
\r
518 plugin.map 'rss rmwatch :handle',
\r
519 :action => 'unwatch_rss'
\r
520 plugin.map 'rss rewatch :handle',
\r
521 :action => 'rewatch_rss'
\r