]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/bash.rb
bash plugin: refactor and localize help
[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, :irc_text, :vote
23
24   def initialize(num, text, vote)
25     @num = num.to_i
26     @text = text
27     @vote = vote
28     @irc_text = mk_irc_text
29   end
30
31   def url
32     "http://www.bash.org/?#{@num}"
33   end
34
35   def to_s
36     "#%d (%d): %s" % [self.num, self.vote, self.irc_text]
37   end
38
39   private
40   def mk_irc_text
41     cur_nick = nil
42     last_nick = nil
43     text = String.new
44     @text.each_line { |l|
45       debug "line: #{l.inspect}"
46       cur_nick = l.match(/^\s*(&lt;.*?&gt;|\(.*?\)|.*?:)\s/)[1] rescue nil
47       debug "nick: #{cur_nick.inspect}; last: #{last_nick.inspect}"
48       if cur_nick and cur_nick == last_nick
49         text << l.sub(cur_nick,"")
50       else
51         last_nick = cur_nick.dup if cur_nick
52         text << l
53       end
54     }
55     debug text
56     # TODO: the gsub of br tags to | should be an ircify_html option
57     text.gsub(/(?:<br \/>\s*)+/, ' | ').ircify_html
58   end
59
60 end
61
62 class BashPlugin < Plugin
63
64   Config.register Config::EnumValue.new('bash.access',
65     :values => ['xml', 'html'], :default => 'html',
66     :desc => "Which method the bot should use to access bash.org quotes: xml files or standard webpages")
67
68   include REXML
69   def help(plugin, topic="")
70     [
71       _("bash => print a random quote from bash.org"),
72       _("bash quote_id => print that quote id from bash.org"),
73       _("bash latest => print the latest quote from bash.org (currently broken, need to get josh@bash.org to fix the xml)")
74     ].join(", ")
75   end
76
77   def bash_filter(s)
78     # check if we like the location of the page
79     loc = Utils.check_location(s, %r{http://(?:www\.)?bash\.org/\?})
80     return unless loc
81     # check if there are any quotes
82     quotes = get_html_quotes(s[:text])
83     return if quotes.empty?
84     title = s[:text].ircify_html_title
85     # return the first quote
86     return {
87           :title => title,
88           :content => quotes.first.to_s,
89           :bash_quotes => quotes
90     }
91   end
92
93   def initialize
94     super
95
96     @bot.register_filter(:bash, :htmlinfo) { |s| bash_filter(s) }
97   end
98
99   def bash(m, params)
100     id = params[:id]
101     case @bot.config['bash.access'].intern
102     when :xml
103       xml_bash(m, id)
104     else
105       html_bash(m, :id => id)
106     end
107   end
108
109   def search(m, params)
110     esc = CGI.escape(params[:words].to_s)
111     html = @bot.httputil.get("http://bash.org/?search=#{esc}")
112     html_bash(m, :html => html)
113   end
114
115   def get_html_quotes(html)
116     quotes = []
117
118     html_quotes = html.split(/<p class="quote">/)
119     html_quotes.each { |htqt|
120       # debug htqt.inspect
121       if htqt.match(/<a href="\?(\d+)"[^>]*>.*?\((-?\d+)\).*?<p class="qt">(.*)<\/p>\s+(?:<\/td>.*)?\z/m)
122         num = $1
123         vote = $2
124         text = $3
125         quotes << BashQuote.new(num, text, vote)
126       end
127     }
128     return quotes
129   end
130
131   def html_bash(m, opts={})
132     html = opts[:html]
133     if not html
134       id = opts[:id]
135       case id
136       when 'latest'
137         html = @bot.httputil.get("http://bash.org/?latest")
138       when nil
139         html = @bot.httputil.get("http://bash.org/?random", :cache => false)
140       else
141         html = @bot.httputil.get("http://bash.org/?" + id)
142       end
143     end
144
145     if not html
146       m.reply "unable to retrieve quotes"
147       return
148     end
149
150     quotes = get_html_quotes(html)
151
152     case quotes.length
153     when 0
154       m.reply "no quotes found"
155       return
156     when 1
157       quote = quotes.first
158     else
159       # For the time being, we only echo the first quote, but in the future we
160       # may want to echo more than one for latest/random
161       quote = quotes.first
162     end
163     m.reply quote.to_s
164   end
165
166   def xml_bash(m, id=nil)
167     case id
168     when 'latest'
169       xml = @bot.httputil.get("http://bash.org/xml/?latest&num=1")
170     when nil
171       xml = @bot.httputil.get("http://bash.org/xml/?random&num=1", :cache => false)
172     else
173       xml = @bot.httputil.get("http://bash.org/xml/?" + id + "&num=1")
174     end 
175
176     unless xml
177       m.reply "bash.org rss parse failed"
178       return
179     end
180     doc = Document.new xml
181     unless doc
182       m.reply "bash.org rss parse failed"
183       return
184     end
185     doc.elements.each("*/item") {|e|
186       if(id != 0) 
187         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
188         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
189       else
190         reply = e.elements["title"].text.gsub(/QDB: /,"") + " " + e.elements["link"].text.gsub(/QDB: /,"") + "\n"
191         reply = reply + e.elements["description"].text.gsub(/\<br \/\>/, "\n")
192       end
193       m.reply reply
194     }
195   end
196 end
197
198 plugin = BashPlugin.new
199
200 plugin.map "bash search *words", :action => :search
201 plugin.map "bash [:id]"
202