#++ # # :title: Douglas Crockford / Bruce Schneier facts plugin for rbot # # Author:: Matthew M. Boedicker # # Copyright:: (C) 2010 Matthew M. Boedicker # # License:: GPL v3 # # Version:: 0.2 # # Retrieves a random Crockford or Schneier fact. %w{ hpricot htmlentities }.each { |m| require m } class CrockfordPlugin < Plugin def help(plugin, topic='') 'show a random Douglas Crockford or Bruce Schneier fact: crockford|schneier' end def get_fact(url, xpath) data = @bot.httputil.get(url, :cache => false) doc = Hpricot(data) HTMLEntities.new.decode((doc/xpath).inner_html).strip end def crockford(m, params) m.reply(html_to_irc(get_fact('http://crockfordfacts.com/', 'div#dcf/h2'))) end def schneier(m, params) m.reply(html_to_irc(get_fact('http://www.schneierfacts.com/', 'p.fact'))) end def html_to_irc(s) s.gsub(/<(i)>(.*?)<\/\1>/i, "#{Italic}\\2#{NormalText}"). gsub(/<(u)>(.*?)<\/\1>/i, "#{Underline}\\2#{NormalText}"). gsub(/<(b|em)>(.*?)<\/\1>/i, "#{Bold}\\2#{NormalText}") end end plugin = CrockfordPlugin.new plugin.map('crockford', :action => 'crockford') plugin.map('schneier', :action => 'schneier')