5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
10 return self[0...len].sub(/\s+\S*$/,"...")
19 # bold and strong -> bold
20 txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
22 # italic, emphasis and underline -> underline
23 txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
25 ## This would be a nice addition, but the results are horrible
26 ## Maybe make it configurable?
27 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
29 # Paragraph and br tags are converted to whitespace.
30 txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
33 # All other tags are just removed
34 txt.gsub!(/<[^>]+>/, '')
36 # Remove double formatting options, since they only waste bytes
37 txt.gsub!(/#{Bold}\s*#{Bold}/,"")
38 txt.gsub!(/#{Underline}\s*#{Underline}/,"")
40 # And finally whitespace is squeezed
43 # Decode entities and strip whitespace
44 return Utils.decode_html_entities(txt).strip!
48 class SearchPlugin < Plugin
49 BotConfig.register BotConfigIntegerValue.new('google.hits',
51 :desc => "Number of hits to return from Google searches")
52 BotConfig.register BotConfigIntegerValue.new('google.first_par',
54 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
55 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
57 :desc => "Number of hits to return from Wikipedia searches")
58 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
60 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
62 def help(plugin, topic="")
64 when "search", "google"
65 "#{topic} <string> => search google for <string>"
67 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
69 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
74 what = params[:words].to_s
75 searchfor = URI.escape what
76 # This method is also called by other methods to restrict searching to some sites
78 site = "site:#{params[:site]}+"
82 # It is also possible to choose a filter to remove constant parts from the titles
83 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
84 filter = params[:filter] || ""
86 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
88 hits = params[:hits] || @bot.config['google.hits']
91 wml = @bot.httputil.get_cached(url)
93 m.reply "error googling for #{what}"
96 results = wml.scan(GOOGLE_WAP_LINK)
97 if results.length == 0
98 m.reply "no results found for #{what}"
102 results = results[0...hits].map { |res|
104 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
105 u = URI.unescape res[1]
107 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
110 m.reply "Results for #{what}: #{results}"
112 first_pars = params[:firstpar] || @bot.config['google.first_par']
115 while first_pars > 0 and urls.length > 0
116 url.replace(urls.shift)
119 # FIXME what happens if some big file is returned? We should share
120 # code with the url plugin to only retrieve partial file content!
121 xml = @bot.httputil.get_cached(url)
123 debug "Unable to retrieve #{url}"
126 # We get the first par after the first main heading, if possible
127 header_found = xml.match(/<h1(?:\s+[^>]*)?>(.*?)<\/h1>/im)
130 debug "Found header: #{header_found[1].inspect}"
133 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
134 break unless candidate
135 txt.replace candidate.ircify_html
138 # If we haven't found a first par yet, try to get it from the whole
143 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
144 break unless candidate
145 txt.replace candidate.ircify_html
149 # Nothing yet, try title
151 debug "No first par found\n#{xml}"
152 # FIXME only do this if the 'url' plugin is loaded
153 txt.replace @bot.plugins['url'].get_title_from_html(xml)
156 m.reply "[#{idx}] #{txt}".omissis_after(400)
161 def wikipedia(m, params)
163 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
164 debug "Looking up things on #{site}"
166 params[:filter] = / - Wikipedia.*$/
167 params[:hits] = @bot.config['wikipedia.hits']
168 params[:firstpar] = @bot.config['wikipedia.first_par']
169 return google(m, params)
173 plugin = SearchPlugin.new
175 plugin.map "search *words", :action => 'google'
176 plugin.map "google *words", :action => 'google'
177 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
178 plugin.map "wp *words", :action => 'wikipedia'