5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
10 return self[0...len].sub(/\s+\S*$/,"...")
18 txt.gsub!(/<\/?b\s*>/, "#{Bold}")
19 txt.gsub!(/<\/?i\s*>/, "#{Underline}")
20 ## This would be a nice addition, but the results are horrible
21 ## Maybe make it configurable?
22 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
23 txt.gsub!(/<\/?(p|br)>/, ' ')
25 txt.gsub!(/<[^>]+>/, '')
27 return Utils.decode_html_entities(txt).strip!
31 class SearchPlugin < Plugin
32 BotConfig.register BotConfigIntegerValue.new('google.hits',
34 :desc => "Number of hits to return from Google searches")
35 BotConfig.register BotConfigIntegerValue.new('google.first_par',
37 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
38 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
40 :desc => "Number of hits to return from Wikipedia searches")
41 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
43 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
45 def help(plugin, topic="")
47 when "search", "google"
48 "#{topic} <string> => search google for <string>"
50 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
52 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
57 what = params[:words].to_s
58 searchfor = URI.escape what
59 # This method is also called by other methods to restrict searching to some sites
61 site = "site:#{params[:site]}+"
65 # It is also possible to choose a filter to remove constant parts from the titles
66 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
67 filter = params[:filter] || ""
69 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
71 hits = params[:hits] || @bot.config['google.hits']
74 wml = @bot.httputil.get_cached(url)
76 m.reply "error googling for #{what}"
79 results = wml.scan(GOOGLE_WAP_LINK)
80 if results.length == 0
81 m.reply "no results found for #{what}"
85 results = results[0...hits].map { |res|
87 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
88 u = URI.unescape res[1]
90 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
93 m.reply "Results for #{what}: #{results}"
95 first_pars = params[:firstpar] || @bot.config['google.first_par']
98 while first_pars > 0 and urls.length > 0
99 url.replace(urls.shift)
101 xml = @bot.httputil.get_cached(url)
103 debug "Unable to retrieve #{url}"
106 # We get the first par after the first main heading, if possible
107 header_found = xml.match(/<h1( [^>]*)?>.*?<\/h1>/im)
110 txt = header_found.post_match[/<p( [^>]*)?>.*?<\/p>/im]
112 # If we haven't found a first par yet, try to get it from the whole
115 txt = xml[/<p( [^>]*)?>.*?<\/p>/im]
117 # Nothing yet, give up
119 debug "No first par found\n#{xml}"
122 m.reply "[#{idx}] #{txt.ircify_html}".omissis_after(400)
127 def wikipedia(m, params)
129 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
130 debug "Looking up things on #{site}"
132 params[:filter] = / - Wikipedia.*$/
133 params[:hits] = @bot.config['wikipedia.hits']
134 params[:firstpar] = @bot.config['wikipedia.first_par']
135 return google(m, params)
139 plugin = SearchPlugin.new
141 plugin.map "search *words", :action => 'google'
142 plugin.map "google *words", :action => 'google'
143 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
144 plugin.map "wp *words", :action => 'wikipedia'