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)
118 xml = @bot.httputil.get_cached(url)
120 debug "Unable to retrieve #{url}"
123 # We get the first par after the first main heading, if possible
124 header_found = xml.match(/<h1(?:\s+[^>]*)?>(.*?)<\/h1>/im)
127 debug "Found header: #{header_found[1].inspect}"
130 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im].ircify_html
131 break unless candidate
132 txt.replace candidate
135 # If we haven't found a first par yet, try to get it from the whole
138 txt = xml[/<p(?:\s+[^>]*)?>.*?<\/p>/im].ircify_html
141 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im].ircify_html
142 break unless candidate
143 txt.replace candidate
146 # Nothing yet, give up
148 debug "No first par found\n#{xml}"
151 m.reply "[#{idx}] #{txt}".omissis_after(400)
156 def wikipedia(m, params)
158 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
159 debug "Looking up things on #{site}"
161 params[:filter] = / - Wikipedia.*$/
162 params[:hits] = @bot.config['wikipedia.hits']
163 params[:firstpar] = @bot.config['wikipedia.first_par']
164 return google(m, params)
168 plugin = SearchPlugin.new
170 plugin.map "search *words", :action => 'google'
171 plugin.map "google *words", :action => 'google'
172 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
173 plugin.map "wp *words", :action => 'wikipedia'