def self.load_list(filename, type)
unless [:blacklist, :whitelist].include?(type)
raise "Invalid list type: #{type}"
end
compiled_list = Hash.new
compiled_list[:hash] = ''
compiled_list[:regexps] = Array.new
unless File.exist?(filename)
if type == :whitelist
return compiled_list
else
raise "File not found: #{filename}"
end
end
$stderr.puts "Using #{type} #{filename}" if $VERBOSE
list = File.read(filename)
list_hash = Digest::SHA1.hexdigest(list)
unless ReferrerCopConfig::CACHE_PATH.nil?
cache_file = File.join(ReferrerCopConfig::CACHE_PATH, "#{type}_compiled.refcop")
if File.exist?(cache_file)
begin
compiled_list = Marshal.load(File.read(cache_file))
if compiled_list[:hash] == list_hash
$stderr.puts "Loaded compiled #{type} from cache." if $VERBOSE
return compiled_list
end
rescue => e
$stderr.puts "Error loading #{type} from cache." if $VERBOSE
end
end
end
compiled_list = Hash.new
compiled_list[:hash] = ''
compiled_list[:regexps] = Array.new
length = 0
list.each do |line|
line.sub!(/#.*/, '')
line.strip!
next if line.empty?
length += 1
if line =~ /^\/(.+)\/$/
compiled_list[:regexps] << Regexp.new($1)
next
end
address = line[REGEXPS[:address], 1]
firstchar = address[0]
secondchar = address[1]
thirdchar = address[2]
if compiled_list.key?(firstchar)
if compiled_list[firstchar].key?(secondchar)
if compiled_list[firstchar][secondchar].key?(thirdchar)
compiled_list[firstchar][secondchar][thirdchar] << address
else
compiled_list[firstchar][secondchar][thirdchar] = [address]
end
else
compiled_list[firstchar][secondchar] = { thirdchar => [address] }
end
else
compiled_list[firstchar] = { secondchar => { thirdchar => [address] } }
end
end
compiled_list[:hash] = list_hash
unless ReferrerCopConfig::CACHE_PATH.nil?
begin
File.open(cache_file, 'w') do |file|
file.write(Marshal.dump(compiled_list))
end
rescue => e
$stderr.puts "Unable to create cache file: #{cache_file}" if $VERBOSE
end
end
$stderr.puts "Compiled #{length} #{type} entries" if $VERBOSE
return compiled_list
end