]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/bash.rb
* (plugins/translator) fixed a diagnostic message
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / bash.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: bash.org quote retrieval
5 #
6 # Author:: Robin Kearney <robin@riviera.org.uk>
7 # Author:: cs
8 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 #
10 # Copyright:: (C) 2005 Robin Kearney
11 # Copyright:: (C) 2007 cs, Giuseppe Bilotta
12 #
13 # License:: public domain
14 #
15 # TODO improve output of quote
16 # TODO show more than one quote
17 # TODO allow selection of only quotes with vote > 0
18
19 require 'rexml/document'
20
21 class ::BashQuote
22   attr_accessor :num, :text, :vote
23
24   def initialize(num, text, vote)
25     @num = num.to_i
26     @text = text
27     @vote = vote
28   end
29
30   def url
31     "http://www.bash.org/?#{@num}"
32   end
33
34 end
35
36 class BashPlugin < Plugin
37
38   BotConfig.register BotConfigEnumValue.new('bash.access',
39     :values => ['xml', 'html'], :default => 'html',
40     :desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages")
41
42   include REXML
43   def help(plugin, topic="")
44     "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)"
45   end
46
47   def bash(m, params)
48     id = params[:id]
49     case @bot.config['bash.access'].intern
50     when :xml
51       xml_bash(m, id)
52     else
53       html_bash(m, :id => id)
54     end
55   end
56
57   def search(m, params)
58     esc = CGI.escape(params[:words].to_s)
59     html = @bot.httputil.get("http://bash.org/?search=#{esc}")
60     html_bash(m, :html => html)
61   end
62
63   def html_bash(m, opts={})
64     quotes = []
65
66     html = opts[:html]
67     if not html
68       id = opts[:id]
69       case id
70       when 'latest'
71         html = @bot.httputil.get("http://bash.org/?latest")
72       when nil
73         html = @bot.httputil.get("http://bash.org/?random", :cache => false)
74       else
75         html = @bot.httputil.get("http://bash.org/?" + id)
76       end
77     end
78
79     html_quotes = html.split(/<p class="quote">/)
80     html_quotes.each { |htqt|
81       # debug htqt.inspect
82       if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m)
83         num = $1
84         vote = $2
85         text = $3
86         quotes << BashQuote.new(num, text, vote)
87       end
88     }
89
90     case quotes.length
91     when 0
92       m.reply "no quotes found"
93       return
94     when 1
95       quote = quotes.first
96     else
97       # For the time being, we only echo the first quote, but in the future we
98       # may want to echo more than one for latest/random
99       quote = quotes.first
100     end
101     # TODO: the gsub of br tags to | should be an ircify_html option
102     m.reply "#%d (%d): %s" % [quote.num, quote.vote, quote.text.gsub(/(?:<br \/>\s*)+/, ' | ').ircify_html]
103   end
104
105   def xml_bash(m, id=nil)
106     case id
107     when 'latest'
108       xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
109     when nil
110       xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false)
111     else
112       xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
113     end 
114
115     unless xml
116       m.reply "bash.org rss parse failed"
117       return
118     end
119     doc = Document.new xml
120     unless doc
121       m.reply "bash.org rss parse failed"
122       return
123     end
124     doc.elements.each("*/item") {|e|
125       if(id != 0) 
126         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
127         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
128       else
129         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
130         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
131       end
132       m.reply reply
133     }
134   end
135 end
136
137 plugin = BashPlugin.new
138
139 plugin.map "bash search *words", :action => :search
140 plugin.map "bash [:id]"
141