Skip to content

Commit

Permalink
chore: improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-virkus committed Jan 20, 2024
1 parent 3cf5f8a commit 04a8553
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 86 deletions.
36 changes: 25 additions & 11 deletions lib/src/private/imap/quota_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ class QuotaParser extends ResponseParser<QuotaResult> {

@override
QuotaResult? parse(
ImapResponse imapResponse, Response<QuotaResult> response) =>
ImapResponse imapResponse,
Response<QuotaResult> response,
) =>
response.isOkStatus ? _quota : null;

@override
bool parseUntagged(
ImapResponse imapResponse, Response<QuotaResult>? response) {
ImapResponse imapResponse,
Response<QuotaResult>? response,
) {
var details = imapResponse.parseText;
String? rootName;
if (details.startsWith('QUOTA ')) {
Expand All @@ -35,11 +39,13 @@ class QuotaParser extends ResponseParser<QuotaResult> {
final buffer = <ResourceLimit>[];
for (var index = 0; index < listEntries.length; index += 3) {
buffer.add(ResourceLimit(
listEntries[index],
int.tryParse(listEntries[index + 1]),
int.tryParse(listEntries[index + 2])));
listEntries[index],
int.tryParse(listEntries[index + 1]),
int.tryParse(listEntries[index + 2]),
));
}
_quota = QuotaResult(rootName, buffer);

return true;
} else {
return super.parseUntagged(imapResponse, response);
Expand All @@ -53,12 +59,16 @@ class QuotaRootParser extends ResponseParser<QuotaRootResult> {

@override
QuotaRootResult? parse(
ImapResponse imapResponse, Response<QuotaRootResult> response) =>
ImapResponse imapResponse,
Response<QuotaRootResult> response,
) =>
response.isOkStatus ? _quotaRoot : null;

@override
bool parseUntagged(
ImapResponse imapResponse, Response<QuotaRootResult>? response) {
ImapResponse imapResponse,
Response<QuotaRootResult>? response,
) {
var details = imapResponse.parseText;
String? rootName;
if (details.startsWith('QUOTA ')) {
Expand All @@ -79,16 +89,19 @@ class QuotaRootParser extends ResponseParser<QuotaRootResult> {
final buffer = <ResourceLimit>[];
for (var index = 0; index < listEntries.length; index += 3) {
buffer.add(ResourceLimit(
listEntries[index],
int.tryParse(listEntries[index + 1]),
int.tryParse(listEntries[index + 2])));
listEntries[index],
int.tryParse(listEntries[index + 1]),
int.tryParse(listEntries[index + 2]),
));
}
_quotaRoot!.quotaRoots[rootName] = QuotaResult(rootName, buffer);
_quotaRoot?.quotaRoots[rootName] = QuotaResult(rootName, buffer);

return true;
} else if (details.startsWith('QUOTAROOT ')) {
details = details.substring('QUOTAROOT '.length);
final entries = _parseStringEntries(details);
_quotaRoot = QuotaRootResult(entries.first, entries.sublist(1));

return true;
} else {
return super.parseUntagged(imapResponse, response);
Expand All @@ -106,6 +119,7 @@ class QuotaRootParser extends ResponseParser<QuotaRootResult> {
output.add(item);
}
}

return output;
}
}
7 changes: 5 additions & 2 deletions lib/src/private/imap/status_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ class StatusParser extends ResponseParser<Mailbox> {
break;
default:
print(
'unexpected STATUS: $entry=${listEntries[i + 1]}\nin $details');
'unexpected STATUS: $entry=${listEntries[i + 1]}\nin $details',
);
}
}

return true;
} else {
return super.parseUntagged(imapResponse, response);
Expand All @@ -62,8 +64,9 @@ class StatusParser extends ResponseParser<Mailbox> {
int _findStartIndex(String details) {
final matches = _regex.allMatches(details);
if (matches.isNotEmpty && matches.first.groupCount == 2) {
return matches.first.group(1)!.length;
return matches.first.group(1)?.length ?? -1;
}

return -1;
}
}
10 changes: 6 additions & 4 deletions lib/src/private/smtp/commands/smtp_auth_cram_md5_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class SmtpAuthCramMd5Command extends SmtpCommand {
SmtpAuthCramMd5Command(this._userName, this._password)
: super('AUTH CRAM-MD5');

final String? _userName;
final String? _password;
final String _userName;
final String _password;
bool _authSent = false;

@override
Expand All @@ -35,7 +35,8 @@ S: 235 Authentication succeeded
}
if (!_authSent) {
_authSent = true;
final base64Nonce = response.message!;
final base64Nonce = response.message ?? '';

return getBase64EncodedData(base64Nonce);
} else {
return null;
Expand All @@ -46,7 +47,7 @@ S: 235 Authentication succeeded
String getBase64EncodedData(String base64Nonce) {
// BASE64(USERNAME, " ",
// MD5((SECRET XOR opad),MD5((SECRET XOR ipad), NONCE)))
var password = utf8.encode(_password!);
var password = utf8.encode(_password);
if (password.length > 64) {
final passwordDigest = md5.convert(password);
password = Uint8List.fromList(passwordDigest.bytes);
Expand All @@ -57,6 +58,7 @@ S: 235 Authentication succeeded
final input = '$_userName $hmacNonce';
final complete = utf8.encode(input);
final authBase64Text = base64.encode(complete);

return authBase64Text;
}

Expand Down
17 changes: 10 additions & 7 deletions lib/src/private/smtp/commands/smtp_auth_login_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class SmtpAuthLoginCommand extends SmtpCommand {
/// Creates a new AUTH LOGIN command
SmtpAuthLoginCommand(this._userName, this._password) : super('AUTH LOGIN');

final String? _userName;
final String? _password;
final String _userName;
final String _password;
final Base64Codec _codec = const Base64Codec();
bool _userNameSent = false;
bool _userPasswordSent = false;
Expand All @@ -21,16 +21,19 @@ class SmtpAuthLoginCommand extends SmtpCommand {
String? nextCommand(SmtpResponse response) {
if (response.code != 334 && response.code != 235) {
print(
'Warning: Unexpected status code during AUTH LOGIN: ${response.code}.'
'Expected: 334 or 235. \nuserNameSent=$_userNameSent, '
'userPasswordSent=$_userPasswordSent');
'Warning: Unexpected status code during AUTH LOGIN: ${response.code}.'
'Expected: 334 or 235. \nuserNameSent=$_userNameSent, '
'userPasswordSent=$_userPasswordSent',
);
}
if (!_userNameSent) {
_userNameSent = true;
return _codec.encode(_userName!.codeUnits);

return _codec.encode(_userName.codeUnits);
} else if (!_userPasswordSent) {
_userPasswordSent = true;
return _codec.encode(_password!.codeUnits);

return _codec.encode(_password.codeUnits);
} else {
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/private/smtp/commands/smtp_sendmail_command.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:developer';

import '../../../../enough_mail.dart';
import '../smtp_command.dart';

Expand All @@ -25,6 +23,7 @@ class _SmtpSendCommand extends SmtpCommand {
if (use8BitEncoding) {
return 'MAIL FROM:<$fromEmail> BODY=8BITMIME';
}

return 'MAIL FROM:<$fromEmail>';
}

Expand All @@ -40,9 +39,11 @@ class _SmtpSendCommand extends SmtpCommand {
final index = _recipientIndex;
if (index < recipientEmails.length) {
_recipientIndex++;

return _getRecipientToCommand(recipientEmails[index]);
} else if (response.type == SmtpResponseType.success) {
_currentStep = _SmtpSendCommandSequence.data;

return 'DATA';
} else {
return null;
Expand All @@ -66,6 +67,7 @@ class _SmtpSendCommand extends SmtpCommand {
if (_currentStep == _SmtpSendCommandSequence.data) {
return response.code == 354;
}

return (response.type != SmtpResponseType.success) ||
(_currentStep == _SmtpSendCommandSequence.done);
}
Expand Down
Loading

0 comments on commit 04a8553

Please sign in to comment.