1 require 'rexml/document'
4 class SlashdotPlugin < Plugin
6 def help(plugin, topic="")
7 "slashdot search <string> [<max>=4] => search slashdot for <string>, slashdot [<max>=4] => return up to <max> slashdot headlines (use negative max to return that many headlines, but all on one line.)"
10 if m.params && m.params =~ /^search\s+(.*)\s+(\d+)$/
13 search_slashdot m, search, limit
14 elsif m.params && m.params =~ /^search\s+(.*)$/
16 search_slashdot m, search
17 elsif m.params && m.params =~ /^([-\d]+)$/
25 def search_slashdot(m, search, max=4)
27 xml = @bot.httputil.get(URI.parse("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}"))
28 rescue URI::InvalidURIError, URI::BadURIError => e
29 m.reply "illegal search string #{search}"
33 m.reply "search for #{search} failed"
37 doc = Document.new xml
38 rescue REXML::ParseException => e
40 m.reply "couldn't parse output XML: #{e.class}"
44 m.reply "search for #{search} failed"
49 doc.elements.each("*/item") {|e|
50 desc = e.elements["title"].text
51 desc.gsub!(/(.{150}).*/, '\1..')
52 reply = sprintf("%s | %s", e.elements["link"].text, desc)
59 def slashdot(m, max=4)
60 xml = @bot.httputil.get(URI.parse("http://slashdot.org/slashdot.xml"))
62 m.reply "slashdot news parse failed"
65 doc = Document.new xml
67 m.reply "slashdot news parse failed (invalid xml)"
78 doc.elements.each("*/story") {|e|
79 matches << [ e.elements["title"].text,
80 e.elements["author"].text,
81 e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
86 m.reply matches.collect{|mat| mat[0]}.join(" | ")
89 m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
94 plugin = SlashdotPlugin.new
95 plugin.register("slashdot")