def validate
    ['from', 'to'].each do |s|
      raise Error, "'#{s}' must be a valid IMAP URI (e.g. imap://example.com)" unless fetch(s) =~ IMAP::REGEX_URI
    end

    unless Logger::LEVELS.has_key?(verbosity.to_sym)
      raise Error, "'verbosity' must be one of: #{Logger::LEVELS.keys.join(', ')}"
    end

    if exclude_file
      raise Error, "exclude file not found: #{exclude_file}" unless File.file?(exclude_file)
      raise Error, "exclude file cannot be read: #{exclude_file}" unless File.readable?(exclude_file)
    end

    if @cached['all'] || @cached['all-subscribed']
      # A specific source folder wins over 'all' and 'all-subscribed'
      if @cached['from-folder']
        @cached['all']              = false
        @cached['all-subscribed']   = false
        @cached['to-folder']      ||= @cached['from-folder']

      elsif @cached['all'] && @cached['all-subscribed']
        # 'all' wins over 'all-subscribed'
        @cached['all-subscribed'] = false
      end

      # 'no-recurse' is not compatible with 'all' and 'all-subscribed'
      raise Error, "'no-recurse' option cannot be used with 'all' or 'all-subscribed'" if @cached['no-recurse']

    else
      @cached['from-folder'] ||= 'INBOX'
      @cached['to-folder']   ||= 'INBOX'
    end

    @cached['exclude'].flatten!
  end
def validate
    ['from', 'to'].each do |s|
      raise Error, "'#{s}' must be a valid IMAP URI (e.g. imap://example.com)" unless fetch(s) =~ IMAP::REGEX_URI
    end

    unless Logger::LEVELS.has_key?(verbosity.to_sym)
      raise Error, "'verbosity' must be one of: #{Logger::LEVELS.keys.join(', ')}"
    end

    if exclude_file
      raise Error, "exclude file not found: #{exclude_file}" unless File.file?(exclude_file)
      raise Error, "exclude file cannot be read: #{exclude_file}" unless File.readable?(exclude_file)
    end

    if @cached['all'] || @cached['all-subscribed']
      # A specific source folder wins over 'all' and 'all-subscribed'
      if @cached['from-folder']
        @cached['all']              = false
        @cached['all-subscribed']   = false
        @cached['to-folder']      ||= @cached['from-folder']

      elsif @cached['all'] && @cached['all-subscribed']
        # 'all' wins over 'all-subscribed'
        @cached['all-subscribed'] = false
      end

      # 'no-recurse' is not compatible with 'all' and 'all-subscribed'
      raise Error, "'no-recurse' option cannot be used with 'all' or 'all-subscribed'" if @cached['no-recurse']

    else
      @cached['from-folder'] ||= 'INBOX'
      @cached['to-folder']   ||= 'INBOX'
    end

    @cached['exclude'].flatten!
  end
Validates the config and resolves conflicting settings.