]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/bash.rb
AUTHORS update.
[user/henk/code/ruby/rbot.git] / 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 URI.parse("http://bash.org/xml/?" + id + "&num=1")
29         elsif(id == "latest")
30         xml = @bot.httputil.get URI.parse("http://bash.org/xml/?latest&num=1")
31         else
32         xml = @bot.httputil.get URI.parse("http://bash.org/xml/?random&num=1")
33         end     
34     unless xml
35       m.reply "bash.org rss parse failed"
36       return
37     end
38     doc = Document.new xml
39     unless doc
40       m.reply "bash.org rss parse failed"
41       return
42     end
43         doc.elements.each("*/item") {|e|
44                 if(id != 0) 
45                         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
46                         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
47                 else
48                         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
49                         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
50                 end
51                 m.reply reply
52         }
53   end
54 end
55 plugin = BashPlugin.new
56 plugin.register("bash")