*** (httputil) major rework, new caching implementation, unified request
[rbot] / data / rbot / plugins / bash.rb
1 # bash.org xml plugin for rbot
2 # by Robin Kearney (robin@riviera.org.uk)
3 #
4 # its a bit of a quick hack, but it works for us :)
5 #
6 require 'rexml/document'
7 require 'uri/common'
8
9 class BashPlugin < Plugin
10   include REXML
11   def help(plugin, topic="")
12     "bash => print a random quote from bash.org, bash quote_id => print that quote id from bash.org, bash latest => print the latest quote from bash.org (currently broken, need to get josh@bash.org to fix the xml)"
13   end
14   def privmsg(m)
15     if m.params && m.params =~ /^([-\d]+)$/
16       id = $1
17       bash m, id
18         elsif(m.params == "latest")
19           bash m, id
20     else
21       bash m
22     end
23   end
24   
25   def bash(m, id=0)
26
27         if(id != 0)
28         xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
29         elsif(id == "latest")
30         xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
31         else
32         xml = @bot.httputil.get("http://bash.org/xml/?random&num=1",
33                                 :cache => false)
34         end     
35     unless xml
36       m.reply "bash.org rss parse failed"
37       return
38     end
39     doc = Document.new xml
40     unless doc
41       m.reply "bash.org rss parse failed"
42       return
43     end
44         doc.elements.each("*/item") {|e|
45                 if(id != 0) 
46                         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
47                         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
48                 else
49                         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
50                         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
51                 end
52                 m.reply reply
53         }
54   end
55 end
56 plugin = BashPlugin.new
57 plugin.register("bash")