]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/chucknorris.rb
chucknorris.rb now supports Vin Diesel and Mr. T, and is rewritten a lot.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / chucknorris.rb
1 require 'uri/common'
2 require 'cgi'
3 \r
4 FACTMAP = { "mrt" => "Mr\. T",\r
5             "vin" => "Vin Diesel",\r
6             "chuck" => "Chuck Norris" }\r
7
8 class ChuckNorrisPlugin < Plugin
9
10   def help(plugin, topic="")
11     "getfact => show a random Chuck Norris or Vin Diesel or Mr. T fact || chucknorris => show a random Chuck Norris quote || vindiesel => show a random Vin Diesel quote || mrt => I pity the foo who can't figure this one out."
12   end
13   \r
14   def getfact(m, params)\r
15     who = params[:who]\r
16     m.reply "Errorn!!!" unless who\r
17     \r
18     if who == 'random'\r
19         who = FACTMAP.keys[rand(FACTMAP.length)]\r
20     end\r
21     \r
22     longwho = FACTMAP[who]\r
23     unless longwho\r
24         m.reply "Who the crap is #{who}?!?!"\r
25         return\r
26     end\r
27     \r
28     matcher = %r{<h1> And now a random fact about #{longwho}...</h1>(.+?)<hr />}\r
29       
30     factdata = @bot.httputil.get(URI.parse("http://www.4q.cc/index.php?pid=fact&person=#{who}"))
31     unless factdata
32       m.reply "This #{longwho} fact punched my teeth in. (HTTP error)"
33     end
34
35     if factdata =~ matcher
36       m.reply(CGI::unescapeHTML($1))
37     else
38       m.reply "This #{longwho} fact made my brain explode. (Parse error)"
39     end
40 \r
41   end
42
43 end
44
45 plugin = ChuckNorrisPlugin.new\r
46 plugin.map 'getfact :who', :action => 'getfact',
47                           :defaults => {:who => 'random'}
48 plugin.map 'chucknorris :who', :action => 'getfact', :defaults => {:who => "chuck"}\r
49 plugin.map 'mrt :who', :action => 'getfact', :defaults => {:who => "mrt"}\r
50 plugin.map 'vindiesel :who', :action => 'getfact', :defaults => {:who => "vin"}\r