-require 'uri/common'
-require 'cgi'
-\r
-FACTMAP = { "mrt" => "Mr\. T",\r
- "vin" => "Vin Diesel",\r
- "chuck" => "Chuck Norris" }\r
+require 'yaml'
+require 'zlib'
+MIN_RATING = 6.0
+MIN_VOTES = 25
+
+# the plugin
class ChuckNorrisPlugin < Plugin
- def help(plugin, topic="")
- "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."
- end
- \r
- def getfact(m, params)
- who = params[:who]
- valid_people = FACTMAP.keys + ["random"]
-
- # if the person wants a fact about themselves, then it'll substitute the name.
- if valid_people.include? who
- substitute_name = nil
+ # Loadez les factes
+ def initialize
+ if path = find_facts_file('chucknorris.yml.gz')
+ fyml = Zlib::GzipReader.open(path)
+ elsif path = find_facts_File('chucknorris.yml')
+ fyml = open(path)
else
- substitute_name = who
- who = 'random'
- end\r
+ raise "Error: Couldn't find chucknorris.yml[.gz]"
+ end
- # pick a random person\r
- if who == 'random'\r
- who = FACTMAP.keys[rand(FACTMAP.length)]\r
- end\r
+ debug "+ [chucknorris] Loading #{path}..."
- # get the long name\r
- longwho = FACTMAP[who]\r
- unless longwho\r
- m.reply "Who the crap is #{who}?!?!"\r
- return\r
- end\r
- \r
- matcher = %r{<h1> And now a random fact about #{longwho}...</h1>(.+?)<hr />}\r
-
- # get the fact
- factdata = @bot.httputil.get(URI.parse("http://www.4q.cc/index.php?pid=fact&person=#{who}"))
- unless factdata
- m.reply "This #{longwho} fact punched my teeth in. (HTTP error)"
- end
+ @@facts = YAML.load(fyml).map{|fact,(score,votes)| votes >= MIN_VOTES ? [score,fact] : nil}.compact
+ debug "+ [chucknorris] #{@@facts.length} Chuck Norris facts loaded..."
+ debug " Random fact: #{@@facts[rand(@@facts.size)].inspect}"
+
+ super
+ end
- # parse the fact
- if factdata =~ matcher
- fact = CGI::unescapeHTML($1)
- fact.gsub!(longwho, substitute_name) if substitute_name
- m.reply fact
+ def name
+ "chucknorris"
+ end
+
+ # Just a little helper for the initialize method...
+ def find_facts_file(name)
+ full_path = File.join Config::datadir, "plugins", name
+ found_files = Dir[full_path]
+ if found_files.empty?
+ nil
else
- m.reply "This #{longwho} fact made my brain explode. (Parse error)"
+ found_files[0]
end
-\r
+ end
+
+ # HELP!
+ def help(plugin, topic="chuck")
+ "chuck|norris|chucknorris [min_rating] => show a random Chuck Norris fact (optional minimum rating from 1-10, default=6.0)."
+ #\"fact [person]\" shows a fact about someone in the channel.
+ end
+
+ # The meat.
+ def fact(m, params)
+ min = params[:minrating].to_f
+ debug "+ Getting Chuck Norris fact (rating > #{min})..."
+
+ viable_facts = @@facts.select {|rating, fact| rating >= min}
+ if viable_facts.empty?
+ debug " - no facts found with rating >= #{min}"
+ m.reply "Are you nuts?!? There are no facts better than #{min}!!!"
+ return
+ end
+
+ rating, fact = viable_facts[rand(viable_facts.length)]
+ m.reply "#{fact} [score=#{rating}]"
end
end
-plugin = ChuckNorrisPlugin.new\r
-plugin.map 'getfact :who', :action => 'getfact',
- :defaults => {:who => 'random'}
-plugin.map 'chucknorris :who', :action => 'getfact', :defaults => {:who => "chuck"}\r
-plugin.map 'mrt :who', :action => 'getfact', :defaults => {:who => "mrt"}\r
-plugin.map 'vindiesel :who', :action => 'getfact', :defaults => {:who => "vin"}\r
+plugin = ChuckNorrisPlugin.new
+
+# plugin.map 'fact :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+plugin.map 'chucknorris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+plugin.map 'chuck :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+plugin.map 'norris :minrating', :action => 'fact', :defaults => {:minrating=>MIN_RATING}
+