Skip to content

Commit

Permalink
add MailClient.disconnect() to close connections
Browse files Browse the repository at this point in the history
Close #71
  • Loading branch information
Robert Virkus committed Jul 3, 2020
1 parent dc9bb5d commit e1c72f7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
40 changes: 40 additions & 0 deletions lib/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,22 @@ class MailClient {
//Future<MailResponse<List<MimeMessage>>> poll(Mailbox mailbox) {}

/// Connects and authenticates with the specified incoming mail server.
/// Also compare [disconnect()].
Future<MailResponse> connect() {
return _incomingMailClient.connect();
}

/// Disconnects from the mail service.
/// Also compare [connect()].
Future disconnect() async {
if (_incomingMailClient != null) {
await _incomingMailClient.disconnect();
}
if (_outgoingMailClient != null) {
await _outgoingMailClient.disconnect();
}
}

// Future<MailResponse> tryAuthenticate(
// ServerConfig serverConfig, MailAuthentication authentication) {
// return authentication.authenticate(this, serverConfig);
Expand Down Expand Up @@ -202,6 +214,8 @@ abstract class _IncomingMailClient {

Future<MailResponse> connect();

Future disconnect();

Future<MailResponse<List<Mailbox>>> listMailboxes();

Future<MailResponse<Mailbox>> selectMailbox(Mailbox mailbox,
Expand Down Expand Up @@ -378,6 +392,14 @@ class _IncomingImapClient extends _IncomingMailClient {
return response;
}

@override
Future disconnect() {
if (_imapClient != null) {
return _imapClient.closeConnection();
}
return Future.value();
}

@override
Future<MailResponse<List<Mailbox>>> listMailboxes() async {
var mailboxResponse = await _imapClient.listMailboxes(recursive: true);
Expand Down Expand Up @@ -574,6 +596,14 @@ class _IncomingPopClient extends _IncomingMailClient {
return authResponse;
}

@override
Future disconnect() {
if (_popClient != null) {
return _popClient.closeConnection();
}
return Future.value();
}

@override
Future<MailResponse<List<Mailbox>>> listMailboxes() {
_config.pathSeparator = '/';
Expand Down Expand Up @@ -683,6 +713,8 @@ class _IncomingPopClient extends _IncomingMailClient {

abstract class _OutgoingMailClient {
Future<MailResponse> sendMessage(MimeMessage message);

Future disconnect();
}

class _OutgoingSmtpClient extends _OutgoingMailClient {
Expand Down Expand Up @@ -734,4 +766,12 @@ class _OutgoingSmtpClient extends _OutgoingMailClient {
}
return MailResponseHelper.success(sendResponse.code);
}

@override
Future disconnect() {
if (_smtpClient != null) {
return _smtpClient.closeConnection();
}
return Future.value();
}
}
2 changes: 1 addition & 1 deletion lib/pop/pop_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class PopClient {
}
}

Future<dynamic> close() {
Future<dynamic> closeConnection() {
_isSocketClosingExpected = true;
return _socket?.close();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/smtp/smtp_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ class SmtpClient {
}
}

Future<dynamic> close() {
/// Closes the connection to the remote SMTP server.
Future<dynamic> closeConnection() {
_isSocketClosingExpected = true;
return _socket?.close();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/pop/commands/pop_quit_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PopQuitCommand extends PopCommand<String> {

@override
String nextCommand(PopResponse response) {
_client.close();
_client.closeConnection();
return null;
}
}
2 changes: 1 addition & 1 deletion lib/src/smtp/commands/smtp_quit_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SmtpQuitCommand extends SmtpCommand {

@override
String nextCommand(SmtpResponse response) {
_client.close();
_client.closeConnection();
return null;
}
}

0 comments on commit e1c72f7

Please sign in to comment.