summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/bash.rb
blob: 2a5bedad8520f3dca32125a7c2cb9ab9d2fe25a9 (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
# bash.org xml plugin for rbot
# by Robin Kearney (robin@riviera.org.uk)
#
# its a bit of a quick hack, but it works for us :)
#
require 'rexml/document'
require 'uri/common'

class BashPlugin < Plugin
  include REXML
  def help(plugin, topic="")
    "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)"
  end
  def privmsg(m)
    if m.params && m.params =~ /^([-\d]+)$/
      id = $1
      bash m, id
	elsif(m.params == "latest")
	  bash m, id
    else
      bash m
    end
  end
  
  def bash(m, id=0)

	if(id != 0)
    	xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
	elsif(id == "latest")
    	xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
	else
    	xml = @bot.httputil.get("http://bash.org/xml/?random&num=1",
                                :cache => false)
	end	
    unless xml
      m.reply "bash.org rss parse failed"
      return
    end
    doc = Document.new xml
    unless doc
      m.reply "bash.org rss parse failed"
      return
    end
	doc.elements.each("*/item") {|e|
		if(id != 0) 
			reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
			reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
		else
			reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
			reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
		end
		m.reply reply
	}
  end
end
plugin = BashPlugin.new
plugin.register("bash")