-
- puts "+ connecting to #{url.host}:#{url.port}"
- http = @bot.httputil.get_proxy(url)
- title = http.start { |http|
- url.path = '/' if url.path == ''\r
-\r
- http.request_get(url.path, "User-Agent" => "rbot-url_plugin/666.666") { |response|\r
-
- case response
- when Net::HTTPRedirection then
- # call self recursively if this is a redirect
- redirect_to = response['location'] || './'
- puts "+ redirect location: #{redirect_to.inspect}"
- url = URI.join url.to_s, redirect_to
- puts "+ whee, redirecting to #{url.to_s}!"
- title = get_title_for_url(url.to_s, depth-1)
- when Net::HTTPSuccess then
- if response['content-type'] =~ /^text\//
- # since the content is 'text/*' and is small enough to
- # be a webpage, retrieve the title from the page
- puts "+ getting #{url.request_uri}"\r
- data = read_data_from_response(response, 50000)\r
- return get_title_from_html(data)
- else
- # content doesn't have title, just display info.
- size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
- return "[Link Info] type: #{response['content-type']}#{size ? ", size: #{size} bytes" : ""}"
- end
- when Net::HTTPClientError then
- return "[Link Info] Error getting link (#{response.code} - #{response.message})"
- when Net::HTTPServerError then
- return "[Link Info] Error getting link (#{response.code} - #{response.message})"
- end # end of "case response"\r
- \r
- } # end of request block
- } # end of http start block\r
-
- rescue SocketError => e
- return "[Link Info] Error connecting to site (#{e.message})"
+
+ # also check the ip, the canonical name and the aliases
+ begin
+ checks = TCPSocket.gethostbyname(url.host)
+ checks.delete_at(-2)
+ rescue => e
+ return "Unable to retrieve info for #{url.host}: #{e.message}"
+ end
+
+ checks << url.host
+ checks.flatten!
+
+ unless checks.grep(@no_info_hosts).empty?
+ return "Sorry, info retrieval for #{url.host} (#{checks.first}) is disabled"
+ end
+
+ logopts = opts.dup
+
+ title = nil
+ extra = []
+
+ begin
+ debug "+ getting info for #{url.request_uri}"
+ info = @bot.filter(:htmlinfo, url)
+ debug info
+ resp = info[:headers]
+
+ logopts[:title] = title = info[:title]
+
+ if info[:content]
+ logopts[:extra] = info[:content]
+ extra << "#{Bold}text#{Bold}: #{info[:content]}" if @bot.config['url.first_par']
+ else
+ logopts[:extra] = String.new
+ logopts[:extra] << "Content Type: #{resp['content-type']}"
+ extra << "#{Bold}type#{Bold}: #{resp['content-type']}" unless title
+ if enc = resp['content-encoding']
+ logopts[:extra] << ", encoding: #{enc}"
+ extra << "#{Bold}encoding#{Bold}: #{enc}" if @bot.config['url.first_par'] or not title
+ end
+
+ size = resp['content-length'].first.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil
+ if size
+ logopts[:extra] << ", size: #{size} bytes"
+ extra << "#{Bold}size#{Bold}: #{size} bytes" if @bot.config['url.first_par'] or not title
+ end
+ end
+ rescue Exception => e
+ case e
+ when UrlLinkError
+ raise e
+ else
+ error e
+ raise "connecting to site/processing information (#{e.message})"
+ end
+ end
+
+ call_event(:url_added, url.to_s, logopts)
+ if title
+ extra.unshift("#{Bold}title#{Bold}: #{title}")
+ end
+ return extra.join(", ") if title or not @bot.config['url.titles_only']