summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/rss.rb
blob: e5b8c110116fd0b6b5e453c251b2bebbeb0ee385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# RSS feed plugin for RubyBot
# (c) 2004 Stanislav Karchebny <berkus@madfire.net>
# (c) 2005 Ian Monroe <ian@monroe.nu>
# (c) 2005 Mark Kretschmann <markey@web.de>
# Licensed under MIT License.

require 'rss/parser'
require 'rss/1.0'
require 'rss/2.0'
require 'rss/dublincore'
begin
  # require 'rss/dublincore/2.0'
rescue
  warning "Unable to load RSS libraries, RSS plugin functionality crippled"
end

class ::String
  def shorten(limit)
    if self.length > limit
      self+". " =~ /^(.{#{limit}}[^.!;?]*[.!;?])/mi
      return $1
    end
    self
  end

  def riphtml
    self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
  end

  def mysqlize
    self.gsub(/'/, "''")
  end
end

class ::RssBlob
  attr :url
  attr :handle
  attr :type
  attr :watchers

  def initialize(url,handle=nil,type=nil,watchers=[])
    @url = url
    if handle
      @handle = handle
    else
      @handle = url
    end
    @type = type
    @watchers = watchers
  end

  def watched?
    !@watchers.empty?
  end

  def watched_by?(who)
    @watchers.include?(who)
  end

  def add_watch(who)
    if watched_by?(who)
      return nil
    end
    @watchers << who unless watched_by?(who)
    return who
  end

  def rm_watch(who)
    @watchers.delete(who)
  end

  #  def to_ary
  #    [@handle,@url,@type,@watchers]
  #  end
end

class RSSFeedsPlugin < Plugin
  @@watchThreads = Hash.new
  @@mutex = Mutex.new

  # Keep a 1:1 relation between commands and handlers
  @@handlers = {
    "rss" => "handle_rss",
    "addrss" => "handle_addrss",
    "rmrss" => "handle_rmrss",
    "rmwatch" => "handle_rmwatch",
    "listrss" => "handle_listrss",
    "listwatches" => "handle_listrsswatch",
    "rewatch" => "handle_rewatch",
    "watchrss" => "handle_watchrss",
  }

  def initialize
    super
    kill_threads
    if @registry.has_key?(:feeds)
      @feeds = @registry[:feeds]
    else
      @feeds = Hash.new
    end
    handle_rewatch
  end

  def watchlist
    @feeds.select { |h, f| f.watched? }
  end

  def cleanup
    kill_threads
  end

  def save
    @registry[:feeds] = @feeds
  end

  def kill_threads
    @@mutex.synchronize {
      # Abort all running threads.
      @@watchThreads.each { |url, thread|
        debug "Killing thread for #{url}"
        thread.kill
      }
      # @@watchThreads.each { |url, thread|
      #   debug "Joining on killed thread for #{url}"
      #   thread.join
      # }
      @@watchThreads = Hash.new
    }
  end

  def help(plugin,topic="")
    "RSS Reader: rss name [limit] => read a named feed [limit maximum posts, default 5], addrss [force] name url => add a feed, listrss => list all available feeds, rmrss name => remove the named feed, watchrss url [type] => watch a rss feed for changes (type may be 'amarokblog', 'amarokforum', 'mediawiki', 'gmame' or empty - it defines special formatting of feed items), rewatch => restart all rss watches, rmwatch url => stop watching for changes in url, listwatches => see a list of watched feeds"
  end

  def report_problem(report, m=nil)
      if m
        m.reply report
      else
        warning report
      end
  end

  def privmsg(m)
    meth = self.method(@@handlers[m.plugin])
    meth.call(m)
  end

  def handle_rss(m)
    unless m.params
      m.reply("incorrect usage: " + help(m.plugin))
      return
    end
    limit = 5
    if m.params =~ /\s+(\d+)$/
      limit = $1.to_i
      if limit < 1 || limit > 15
        m.reply("weird, limit not in [1..15], reverting to default")
        limit = 5
      end
      m.params.gsub!(/\s+\d+$/, '')
    end

    url = ''
    if m.params =~ /^https?:\/\//
      url = m.params
      @@mutex.synchronize {
        @feeds[url] = RssBlob.new(url)
        feed = @feeds[url]
      }
    else
      feed = @feeds.fetch(m.params, nil)
      unless feed
        m.reply(m.params + "? what is that feed about?")
        return
      end
    end

    m.reply("Please wait, querying...")
    title = items = nil
    @@mutex.synchronize {
      title, items = fetchRss(feed, m)
    }
    return unless items
    m.reply("Channel : #{title}")
    # TODO: optional by-date sorting if dates present
    items[0...limit].each do |item|
      printRssItem(m.replyto,item)
    end
  end

  def handle_addrss(m)
    unless m.params
      m.reply "incorrect usage: " + help(m.plugin)
      return
    end
    if m.params =~ /^force /
      forced = true
      m.params.gsub!(/^force /, '')
    end
    feed = m.params.match(/^(\S+)\s+(\S+)$/)
    if feed.nil?
      m.reply("incorrect usage: " + help(m.plugin))
    end
    handle = feed[1]
    url = feed[2]
    debug "Handle: #{handle.inspect}, Url: #{url.inspect}"
    if @feeds.fetch(handle, nil) && !forced
      m.reply("But there is already a feed named #{handle} with url #{@feeds[handle].url}")
      return
    end
    handle.gsub!("|", '_')
    @@mutex.synchronize {
      @feeds[handle] = RssBlob.new(url,handle)
    }
    m.reply "RSS: Added #{url} with name #{handle}"
    return handle
  end

  def handle_rmrss(m)
    feed = handle_rmwatch(m, true)
    if feed.watched?
      m.reply "someone else is watching #{feed.handle}, I won't remove it from my list"
      return
    end
    @@mutex.synchronize {
      @feeds.delete(feed.handle)
    }
    m.okay
    return
  end

  def handle_rmwatch(m,pass=false)
    unless m.params
      m.reply "incorrect usage: " + help(m.plugin)
      return
    end
    handle = m.params
    unless @feeds.has_key?(handle)
      m.reply("dunno that feed")
      return
    end
    feed = @feeds[handle]
    if feed.rm_watch(m.replyto)
      m.reply "#{m.replyto} has been removed from the watchlist for #{feed.handle}"
    else
      m.reply("#{m.replyto} wasn't watching #{feed.handle}") unless pass
    end
    if !feed.watched?
      @@mutex.synchronize {
        if @@watchThreads[handle].kind_of? Thread
          @@watchThreads[handle].kill
          debug "rmwatch: Killed thread for #{handle}"
          @@watchThreads.delete(handle)
        end
      }
    end
    return feed
  end

  def handle_listrss(m)
    reply = ''
    if @feeds.length == 0
      reply = "No feeds yet."
    else
      @@mutex.synchronize {
        @feeds.each { |handle, feed|
          reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"
          (reply << " (watched)") if feed.watched_by?(m.replyto)
          reply << "\n"
          debug reply
        }
      }
    end
    m.reply reply
  end

  def handle_listrsswatch(m)
    reply = ''
    if watchlist.length == 0
      reply = "No watched feeds yet."
    else
      watchlist.each { |handle, feed|
        (reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})\n") if feed.watched_by?(m.replyto)
        debug reply
      }
    end
    m.reply reply
  end

  def handle_rewatch(m=nil)
    kill_threads

    # Read watches from list.
    watchlist.each{ |handle, feed|
      watchRss(feed, m)
    }
    m.okay if m
  end

  def handle_watchrss(m)
    unless m.params
      m.reply "incorrect usage: " + help(m.plugin)
      return
    end
    if m.params =~ /\s+/
      handle = handle_addrss(m)
    else
      handle = m.params
    end
    feed = nil
    @@mutex.synchronize {
      feed = @feeds.fetch(handle, nil)
    }
    if feed
      @@mutex.synchronize {
        if feed.add_watch(m.replyto)
          watchRss(feed, m)
          m.okay
        else
          m.reply "Already watching #{feed.handle}"
        end
      }
    else
      m.reply "Couldn't watch feed #{handle} (no such feed found)"
    end
  end

  private
  def watchRss(feed, m=nil)
    if @@watchThreads.has_key?(feed.handle)
      report_problem("watcher thread for #{feed.handle} is already running", m)
      return
    end
    @@watchThreads[feed.handle] = Thread.new do
      debug 'watchRss thread started.'
      oldItems = []
      firstRun = true
      loop do
        begin
          debug 'Fetching rss feed...'
          title = newItems = nil
          @@mutex.synchronize {
            title, newItems = fetchRss(feed)
          }
          unless newItems
            m.reply "no items in feed"
            break
          end
          debug "Checking if new items are available"
          if firstRun
            debug "First run, we'll see next time"
            firstRun = false
          else
            dispItems = newItems.reject { |item|
              oldItems.include?(item)
            }
            if dispItems.length > 0
              debug "Found #{dispItems.length} new items"
              dispItems.each { |item|
                debug "showing #{item.title}"
                @@mutex.synchronize {
                  printFormattedRss(feed.watchers, item, feed.type)
                }
              }
            else
              debug "No new items found"
            end
          end
          oldItems = newItems
        rescue Exception => e
          error "IO failed: #{e.inspect}"
          debug e.backtrace.join("\n")
        end

        seconds = 150 + rand(100)
        debug "Thread going to sleep #{seconds} seconds.."
        sleep seconds
      end
    end
  end

  def printRssItem(loc,item)
    if item.kind_of?(RSS::RDF::Item)
      @bot.say loc, item.title.chomp.riphtml.shorten(20) + " @ " + item.link
    else
      @bot.say loc, "#{item.pubDate.to_s.chomp+": " if item.pubDate}#{item.title.chomp.riphtml.shorten(20)+" :: " if item.title}#{" @ "+item.link.chomp if item.link}"
    end
  end

  def printFormattedRss(locs, item, type)
    locs.each { |loc|
      case type
      when 'amarokblog'
        @bot.say loc, "::#{item.category.content} just blogged at #{item.link}::"
        @bot.say loc, "::#{item.title.chomp.riphtml} - #{item.description.chomp.riphtml.shorten(60)}::"
      when 'amarokforum'
        @bot.say loc, "::Forum:: #{item.pubDate.to_s.chomp+": " if item.pubDate}#{item.title.chomp.riphtml+" :: " if item.title}#{" @ "+item.link.chomp if item.link}"
      when 'mediawiki'
        @bot.say loc, "::Wiki:: #{item.title} has been edited by #{item.dc_creator}. #{item.description.split("\n")[0].chomp.riphtml.shorten(60)} #{item.link} ::"
        debug "mediawiki #{item.title}"
      when "gmame"
        @bot.say loc, "::amarok-devel:: Message #{item.title} sent by #{item.dc_creator}. #{item.description.split("\n")[0].chomp.riphtml.shorten(60)}::"
      else
        printRssItem(loc,item)
      end
    }
  end

  def fetchRss(feed, m=nil)
    begin
      # Use 60 sec timeout, cause the default is too low
      xml = @bot.httputil.get_cached(feed.url,60,60)
    rescue URI::InvalidURIError, URI::BadURIError => e
      report_problem("invalid rss feed #{feed.url}", m)
      return
    end
    debug 'fetched'
    unless xml
      report_problem("reading feed #{url} failed", m)
      return
    end

    begin
      ## do validate parse
      rss = RSS::Parser.parse(xml)
      debug 'parsed'
    rescue RSS::InvalidRSSError
      ## do non validate parse for invalid RSS 1.0
      begin
        rss = RSS::Parser.parse(xml, false)
      rescue RSS::Error
        report_problem("parsing rss stream failed, whoops =(", m)
        return
      end
    rescue RSS::Error
      report_problem("parsing rss stream failed, oioi", m)
      return
    rescue => e
      report_problem("processing error occured, sorry =(", m)
      debug e.inspect
      debug e.backtrace.join("\n")
      return
    end
    items = []
    if rss.nil?
      report_problem("#{feed.url} does not include RSS 1.0 or 0.9x/2.0",m)
    else
      begin
        rss.output_encoding = "euc-jp"
      rescue RSS::UnknownConvertMethod
        report_problem("bah! something went wrong =(",m)
        return
      end
      rss.channel.title ||= "Unknown"
      title = rss.channel.title
      rss.items.each do |item|
        item.title ||= "Unknown"
        items << item
      end
    end

    if items.empty?
      report_problem("no items found in the feed, maybe try weed?",m)
      return
    end
    return [title, items]
  end
end

plugin = RSSFeedsPlugin.new
plugin.register("rss")
plugin.register("addrss")
plugin.register("rmrss")
plugin.register("rmwatch")
plugin.register("listrss")
plugin.register("rewatch")
plugin.register("watchrss")
plugin.register("listwatches")