# File referrercop.rb, line 420
  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

    # Read the list and calculate its SHA1 hash.
    list      = File.read(filename)
    list_hash = Digest::SHA1.hexdigest(list)

    # Check to see if we've cached a compiled version of the 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

    # No cached version; compile the list.
    compiled_list            = Hash.new
    compiled_list[:hash]    = ''
    compiled_list[:regexps] = Array.new

    length = 0

    list.each do |line|
      # Strip comments.
      line.sub!(/#.*/, '')
      line.strip!

      # Skip empty lines.
      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]

      # Build a lookup tree. I know this is a pain to read. Please forgive me.
      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

    # Cache the compiled list if possible.
    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