]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/chucknorris.rb
07421fdafa93d61c5928fbad6d26ac843ff23cc6
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / chucknorris.rb
1 require 'uri/common'
2 require 'cgi'
3
4 # the 4q.cc "id => full name" mapping\r
5 FACTMAP = { "mrt" => "Mr\. T",\r
6             "vin" => "Vin Diesel",\r
7             "chuck" => "Chuck Norris" }\r
8
9 MIN_RATING = 6.0
10
11 PISSED_EXPRESSIONS = [
12     "fuck this, i'm going to go get toed up.",
13     "screw this, i'm going to get hammered.",
14     "forget this, i'm going to iron some shirts.",
15     "disregard this, i'm going out to kill me some prostitutes.",
16 ]
17
18 # exceptions
19 class HTTPError < Exception; end
20 class ParseError < Exception; end
21
22 # the plugin
23 class ChuckNorrisPlugin < Plugin
24
25   def help(plugin, topic="")
26     "fact [person] => \"fact\" shows a random Chuck Norris, Vin Diesel, or Mr. T fact. \"fact [person]\" shows a fact about someone in the channel. || chucknorris, chuck, norris => random Chuck Norris fact || vindiesel, vin, diesel => random Vin Diesel fact || mrt => I pity the foo who can't figure this one out."
27   end
28
29   def getfact(who)
30       raise "Unknown name: #{who}" unless FACTMAP.keys.include? who
31       # get the fact
32       factdata = @bot.httputil.get(URI.parse("http://www.4q.cc/index.php?pid=fact&person=#{who}"))
33       unless factdata
34         raise HTTPError
35       end
36     
37       longwho = FACTMAP[who]
38
39       # regexes
40       fact_matcher = %r{<h1> And now a random fact about #{longwho}...</h1>(.+?)<hr />}
41       rating_matcher = %r{Current Rating: <b>(\d+\.\d+)</b>}
42
43       # parse the fact
44       if factdata =~ fact_matcher
45         fact = CGI::unescapeHTML($1)
46         if factdata =~ rating_matcher
47             rating = $1.to_f
48             puts "fact=[#{fact}], rating=[#{rating}]"
49             return [fact, rating]
50         end
51       end
52         
53       raise ParseError
54     
55   end
56
57   def fact(m, params)
58     who = params[:who]
59     max_tries = (params[:tries] or "10").to_i
60     
61     valid_people = FACTMAP.keys + ["random"]
62     
63     # if the person wants a fact about themselves, then it'll substitute the name.
64     if valid_people.include? who
65       substitute_name = nil
66     else
67       substitute_name = who
68       who = 'random'
69     end\r
70     
71     # pick a random person\r
72     if who == 'random'
73       if substitute_name
74         # take out the Mr. T facts if you're inserting someone's name
75         # beacuse tons of them suck, and most of them revolve around
76         # "pitying" someone or something.
77         people = FACTMAP.keys - ["mrt"]
78         who = people[rand(people.length)]
79       else
80         who = FACTMAP.keys[rand(FACTMAP.length)]
81       end\r
82     end\r
83     
84     # get the long name\r
85     longwho = FACTMAP[who]\r
86     unless longwho\r
87       m.reply "Who the crap is #{who}?!?!"\r
88       return\r
89     end\r
90     \r
91     # get the fact
92
93     m.reply "alright, let's see if I can find a good one..."
94
95     tries = 0
96     results = []
97     loop do
98         
99         begin
100         
101             puts "[chucknorris] Try number #{tries}/#{max_tries}..."
102
103             tries += 1
104             fact, rating = getfact(who)
105             
106             if rating >= MIN_RATING
107                 fact.gsub!(longwho, substitute_name) if substitute_name
108                 m.reply "#{results.join(', ') + "... "}hrm, this one's not bad:"
109                 m.reply "#{fact} [rating: #{rating}]"
110                 return
111             else
112                 results << "lame"
113             end
114     
115             if tries > max_tries
116                 m.reply "#{results.join(', ')}... these all suck. #{PISSED_EXPRESSIONS[rand(PISSED_EXPRESSIONS.length)]}"
117                 return
118             end
119             
120         rescue HTTPError
121           #m.reply "This #{longwho} fact punched my teeth in. (HTTP error)"
122           results << "DOH!"
123           tries += 1
124         rescue ParseError
125           #m.reply "This #{longwho} fact made my brain explode. (Parse error)"
126           results << "wtf?"
127           tries += 1
128         end
129       
130     end
131   
132   end
133
134
135 end
136
137 plugin = ChuckNorrisPlugin.new\r
138
139 plugin.map 'fact :who :tries', :action => 'fact',
140                           :defaults => {:who => 'random', :tries=>10}
141
142 plugin.map 'chucknorris :who', :action => 'fact', :defaults => {:who => "chuck"}
143 plugin.map 'chuck :who', :action => 'fact', :defaults => {:who => "chuck"}
144 plugin.map 'norris :who', :action => 'fact', :defaults => {:who => "chuck"}
145
146 plugin.map 'vindiesel :who', :action => 'fact', :defaults => {:who => "vin"}
147 plugin.map 'diesel :who', :action => 'fact', :defaults => {:who => "vin"}
148 plugin.map 'vin :who', :action => 'fact', :defaults => {:who => "vin"}
149
150 plugin.map 'mrt :who', :action => 'fact', :defaults => {:who => "mrt"}
151