onServerResponse method

void onServerResponse(
  1. List<String> responseTexts
)

Handles server responses

Implementation

void onServerResponse(List<String> responseTexts) {
  if (isLogEnabled) {
    for (final responseText in responseTexts) {
      log(responseText, isClient: false);
    }
  }
  final cmd = _currentCommand;
  if (cmd != null) {
    try {
      // parse inside the guard so that a malformed server reply fails the
      // pending command instead of leaving its completer hanging forever
      final response = SmtpResponse(responseTexts);
      final next = cmd.next(response);
      final text = next?.text;
      final data = next?.data;
      if (text != null) {
        _write(() => writeText(text), cmd);
      } else if (data != null) {
        _write(() => writeData(data), cmd);
      } else if (cmd.isCommandDone(response)) {
        final result = cmd.failureResponse ?? response;
        if (result.isFailedStatus) {
          cmd.completer.completeError(SmtpException(this, result));
        } else {
          cmd.completer.complete(result);
        }
        //_log("Done with command ${_currentCommand.command}");
        _currentCommand = null;
      }
    } catch (exception, stackTrace) {
      log('Error proceeding to nextCommand: $exception');
      _currentCommand?.completer.completeError(exception, stackTrace);
      _currentCommand = null;
    }
  }
}