1 require 'rexml/document'
3 class SlashdotPlugin < Plugin
5 def help(plugin, topic="")
6 "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.)"
9 def search_slashdot(m, params)
10 max = params[:limit].to_i
11 search = params[:search].to_s
13 xml = @bot.httputil.get("http://slashdot.org/search.pl?content_type=rss&query=#{CGI.escape(search)}")
15 m.reply "search for #{search} failed"
20 doc = Document.new xml
21 rescue REXML::ParseException => e
23 m.reply "couldn't parse output XML: #{e.class}"
27 m.reply "search for #{search} failed"
33 doc.elements.each("*/item") {|e|
34 desc = e.elements["title"].text
35 desc.gsub!(/(.{150}).*/, '\1..')
36 reply = sprintf("%s | %s", e.elements["link"].text, desc.ircify_html)
42 m.reply "search for #{search} failed"
46 def slashdot(m, params)
48 max = params[:limit].to_i
50 xml = @bot.httputil.get('http://slashdot.org/slashdot.xml')
52 m.reply "slashdot news parse failed"
55 doc = Document.new xml
57 m.reply "slashdot news parse failed (invalid xml)"
68 doc.elements.each("*/story") {|e|
69 matches << [ e.elements["title"].text,
70 e.elements["author"].text,
71 e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
76 m.reply matches.collect{|mat| mat[0]}.join(" | ")
79 m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
84 plugin = SlashdotPlugin.new
85 plugin.map 'slashdot search :limit *search', :action => 'search_slashdot',
86 :defaults => {:limit => 4}, :requirements => {:limit => /^-?\d+$/}
87 plugin.map 'slashdot :limit', :defaults => {:limit => 4},
88 :requirements => {:limit => /^-?\d+$/}