5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
11 # bold and strong -> bold
12 txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
14 # italic, emphasis and underline -> underline
15 txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
17 ## This would be a nice addition, but the results are horrible
18 ## Maybe make it configurable?
19 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
21 # Paragraph and br tags are converted to whitespace.
22 txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
25 # All other tags are just removed
26 txt.gsub!(/<[^>]+>/, '')
28 # Remove double formatting options, since they only waste bytes
29 txt.gsub!(/#{Bold}\s*#{Bold}/,"")
30 txt.gsub!(/#{Underline}\s*#{Underline}/,"")
32 # And finally whitespace is squeezed
35 # Decode entities and strip whitespace
36 return Utils.decode_html_entities(txt).strip!
40 class SearchPlugin < Plugin
41 BotConfig.register BotConfigIntegerValue.new('google.hits',
43 :desc => "Number of hits to return from Google searches")
44 BotConfig.register BotConfigIntegerValue.new('google.first_par',
46 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
47 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
49 :desc => "Number of hits to return from Wikipedia searches")
50 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
52 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
54 def help(plugin, topic="")
56 when "search", "google"
57 "#{topic} <string> => search google for <string>"
59 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
61 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
66 what = params[:words].to_s
67 searchfor = URI.escape what
68 # This method is also called by other methods to restrict searching to some sites
70 site = "site:#{params[:site]}+"
74 # It is also possible to choose a filter to remove constant parts from the titles
75 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
76 filter = params[:filter] || ""
78 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
80 hits = params[:hits] || @bot.config['google.hits']
83 wml = @bot.httputil.get_cached(url)
85 m.reply "error googling for #{what}"
88 results = wml.scan(GOOGLE_WAP_LINK)
89 if results.length == 0
90 m.reply "no results found for #{what}"
94 results = results[0...hits].map { |res|
96 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
97 u = URI.unescape res[1]
99 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
102 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
104 first_pars = params[:firstpar] || @bot.config['google.first_par']
107 while first_pars > 0 and urls.length > 0
108 url.replace(urls.shift)
111 # FIXME what happens if some big file is returned? We should share
112 # code with the url plugin to only retrieve partial file content!
113 xml = @bot.httputil.get_cached(url)
115 debug "Unable to retrieve #{url}"
118 # We get the first par after the first main heading, if possible
119 header_found = xml.match(/<h1(?:\s+[^>]*)?>(.*?)<\/h1>/im)
122 debug "Found header: #{header_found[1].inspect}"
125 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
126 break unless candidate
127 txt.replace candidate.ircify_html
130 # If we haven't found a first par yet, try to get it from the whole
135 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
136 break unless candidate
137 txt.replace candidate.ircify_html
141 # Nothing yet, try title
143 debug "No first par found\n#{xml}"
144 # FIXME only do this if the 'url' plugin is loaded
145 txt.replace @bot.plugins['url'].get_title_from_html(xml)
148 m.reply "[#{idx}] #{txt}", :overlong => :truncate
153 def wikipedia(m, params)
155 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
156 debug "Looking up things on #{site}"
158 params[:filter] = / - Wikipedia.*$/
159 params[:hits] = @bot.config['wikipedia.hits']
160 params[:firstpar] = @bot.config['wikipedia.first_par']
161 return google(m, params)
165 plugin = SearchPlugin.new
167 plugin.map "search *words", :action => 'google'
168 plugin.map "google *words", :action => 'google'
169 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
170 plugin.map "wp *words", :action => 'wikipedia'