Skip to content

Commit

Permalink
chore: improve code style, add error details to ImapException
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-virkus committed Oct 31, 2023
1 parent bdea469 commit 57a1c85
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/src/imap/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum ResponseStatus {

/// The command is supported but the client send a wrong request
/// or is a wrong state
bad
bad,
}

/// Base class for command responses.
Expand Down
10 changes: 7 additions & 3 deletions lib/src/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class MailClient {
if (auth is OauthAuthentication && auth.token.isExpired) {
OauthToken? refreshed;
try {
_incomingMailClient.log('Refreshing token...');
refreshed = await refresh(this, auth.token);
} catch (e, s) {
final message = 'Unable to refresh token: $e $s';
Expand Down Expand Up @@ -314,7 +315,8 @@ class MailClient {
await onConfigChanged(account);
} catch (e, s) {
_incomingMailClient.log(
'Unable to handle onConfigChanged $onConfigChanged: $e $s');
'Unable to handle onConfigChanged $onConfigChanged: $e $s',
);
}
}
}
Expand Down Expand Up @@ -2053,8 +2055,10 @@ class _IncomingImapClient extends _IncomingMailClient {
final serverConfig = _config.serverConfig;
final isSecure = serverConfig.socketType == SocketType.ssl;
await _imapClient.connectToServer(
serverConfig.hostname!, serverConfig.port!,
isSecure: isSecure);
serverConfig.hostname!,
serverConfig.port!,
isSecure: isSecure,
);
if (!isSecure) {
if (_imapClient.serverInfo.supportsStartTls &&
(serverConfig.socketType != SocketType.plainNoStartTls)) {
Expand Down
16 changes: 13 additions & 3 deletions lib/src/private/imap/capability_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@ class CapabilityParser extends ResponseParser<List<Capability>> {

@override
List<Capability>? parse(
ImapResponse imapResponse, Response<List<Capability>> response) {
ImapResponse imapResponse,
Response<List<Capability>> response,
) {
if (response.isOkStatus) {
if (imapResponse.parseText.startsWith('OK [CAPABILITY ')) {
parseCapabilities(
imapResponse.first.line!, 'OK [CAPABILITY '.length, info);
imapResponse.first.line!,
'OK [CAPABILITY '.length,
info,
);
_capabilities = info.capabilities;
}

return _capabilities ?? [];
}

return null;
}

@override
bool parseUntagged(
ImapResponse imapResponse, Response<List<Capability>>? response) {
ImapResponse imapResponse,
Response<List<Capability>>? response,
) {
final line = imapResponse.parseText;
if (line.startsWith('OK [CAPABILITY ')) {
parseCapabilities(line, 'OK [CAPABILITY '.length, info);
Expand All @@ -40,6 +49,7 @@ class CapabilityParser extends ResponseParser<List<Capability>> {
_capabilities = info.capabilities;
return true;
}

return super.parseUntagged(imapResponse, response);
}

Expand Down
11 changes: 9 additions & 2 deletions lib/src/private/imap/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@ class CommandTask<T> {
if (imapResponse.parseText.startsWith('OK ')) {
response.status = ResponseStatus.ok;
} else if (imapResponse.parseText.startsWith('NO ')) {
response.status = ResponseStatus.no;
response
..status = ResponseStatus.no
..details = imapResponse.parseText.length > 3
? imapResponse.parseText.substring(3)
: imapResponse.parseText;
} else {
response.status = ResponseStatus.bad;
response
..status = ResponseStatus.bad
..details = imapResponse.parseText;
}
response.result = parser.parse(imapResponse, response);

return response;
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/private/imap/imap_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ class ImapResponse {
if (current.parent != null) {
current = current.parent!;
} else {
print('Warning: no parent for closing parentheses, '
'last parentheses type $lastType');
print(
'Warning: no parent for closing parentheses, '
'last parentheses type $lastType',
);
}
} else if (char != AsciiRunes.runeSpace) {
isInValue = true;
Expand Down Expand Up @@ -192,8 +194,10 @@ class ImapValueIterator {
bool next() {
if (_currentIndex < values.length - 1) {
_currentIndex++;

return true;
}

return false;
}
}
Expand Down
14 changes: 10 additions & 4 deletions lib/src/private/imap/response_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ abstract class ResponseParser<T> {

/// Helper method to parse list entries in a line [details].
List<String>? parseListEntries(
String details, int startIndex, String? endCharacter,
[String separator = ' ']) =>
String details,
int startIndex,
String? endCharacter, [
String separator = ' ',
]) =>
ParserHelper.parseListEntries(
details, startIndex, endCharacter, separator);

/// Helper method to parse a list of integer values in a line [details].
List<int>? parseListIntEntries(
String details, int startIndex, String endCharacter,
[String separator = ' ']) =>
String details,
int startIndex,
String endCharacter, [
String separator = ' ',
]) =>
ParserHelper.parseListIntEntries(
details, startIndex, endCharacter, separator);
}

0 comments on commit 57a1c85

Please sign in to comment.