]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/chucknorris.rb
You can now add the nick of someone in the channel to get a fact about them.
[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 fact, or append someone's name to get a fact about that person (eg. !getfact epitron)|| 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)
15     who = params[:who]
16     valid_people = FACTMAP.keys + ["random"]
17     
18     # if the person wants a fact about themselves, then it'll substitute the name.
19     if valid_people.include? who
20       substitute_name = nil
21     else
22       substitute_name = who
23       who = 'random'
24     end\r
25     
26     # pick a random person\r
27     if who == 'random'\r
28       who = FACTMAP.keys[rand(FACTMAP.length)]\r
29     end\r
30     
31     # get the long name\r
32     longwho = FACTMAP[who]\r
33     unless longwho\r
34       m.reply "Who the crap is #{who}?!?!"\r
35       return\r
36     end\r
37     \r
38     matcher = %r{<h1> And now a random fact about #{longwho}...</h1>(.+?)<hr />}\r
39       
40     # get the fact
41     factdata = @bot.httputil.get(URI.parse("http://www.4q.cc/index.php?pid=fact&person=#{who}"))
42     unless factdata
43       m.reply "This #{longwho} fact punched my teeth in. (HTTP error)"
44     end
45
46     # parse the fact
47     if factdata =~ matcher
48       fact = CGI::unescapeHTML($1)
49       fact.gsub!(longwho, substitute_name) if substitute_name
50       m.reply fact
51     else
52       m.reply "This #{longwho} fact made my brain explode. (Parse error)"
53     end
54 \r
55   end
56
57 end
58
59 plugin = ChuckNorrisPlugin.new\r
60 plugin.map 'getfact :who', :action => 'getfact',
61                           :defaults => {:who => 'random'}
62 plugin.map 'chucknorris :who', :action => 'getfact', :defaults => {:who => "chuck"}\r
63 plugin.map 'mrt :who', :action => 'getfact', :defaults => {:who => "mrt"}\r
64 plugin.map 'vindiesel :who', :action => 'getfact', :defaults => {:who => "vin"}\r