]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/chucknorris.rb
some awesome plugins from Chris Gahan >:)
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / chucknorris.rb
1 require 'rexml/document'
2 require 'uri/common'
3
4 class ChuckNorrisPlugin < Plugin
5   include REXML
6
7   def help(plugin, topic="")
8     "chucknorris [<howmany>=1] => show a random chuck norris quote, or specify <howmany> quotes you want (maximum is 6)."
9   end
10   
11   def chucknorris(m, params)
12     howmany = params[:howmany].to_i
13     howmany = 6 if howmany > 6
14
15     factdata = @bot.httputil.get(URI.parse('http://www.4q.cc/chuck/rss.php'))
16     unless factdata
17       m.reply "Chuck Norris' facts roundhouse kicked the internet connection and totally wasted it."
18       return
19     end
20
21     begin
22       doc = Document.new factdata
23       doc.get_elements('rss/channel/item')[1..howmany].each do |fact|
24         m.reply fact.elements['description'].text
25       end
26
27     rescue ParseException => e
28       puts "Error parsing chuck norris quote: #{e.inspect}"
29       m.reply "Chuck Norris' facts were so intense that they blew up my XML parser."
30
31     end
32
33   end
34
35 end
36
37 plugin = ChuckNorrisPlugin.new
38 plugin.map 'chucknorris :howmany', :defaults => {:howmany => 1},
39                                    :requirements => {:howmany => /^-?\d+$/}