From afc8c7976d3e16e09b9864cb19a8bd145b6b4dd9 Mon Sep 17 00:00:00 2001 From: Robert Virkus Date: Wed, 16 Jun 2021 15:36:17 +0200 Subject: [PATCH] fix renaming mailbox without having selected any This fixes #160, #164, #165 --- lib/imap/imap_client.dart | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/imap/imap_client.dart b/lib/imap/imap_client.dart index 0cb28fa9..efa3b14c 100644 --- a/lib/imap/imap_client.dart +++ b/lib/imap/imap_client.dart @@ -956,6 +956,9 @@ class ImapClient extends ClientBase { /// Compare [unselectMailbox] /// Compare [expunge] Future closeMailbox() { + if (_selectedMailbox == null) { + return Future.value(); + } final cmd = Command('CLOSE'); final parser = NoResponseParser(_selectedMailbox); _selectedMailbox = null; @@ -966,6 +969,9 @@ class ImapClient extends ClientBase { /// /// Compare [selectMailbox] Future unselectMailbox() { + if (_selectedMailbox == null) { + return Future.value(); + } final cmd = Command('UNSELECT'); final parser = NoResponseParser(_selectedMailbox); _selectedMailbox = null; @@ -1335,19 +1341,19 @@ class ImapClient extends ClientBase { return sendCommand(cmd, parser); } - /// Creates the specified mailbox - /// - /// Spefify the name with [path] + /// Creates a new mailbox with the specified [path] Future createMailbox(String path) async { final encodedPath = _encodeMailboxPath(path); final cmd = Command('CREATE $encodedPath'); - final response = - await sendCommand(cmd, NoopParser(this, _selectedMailbox)); - final mailboxesResponse = await listMailboxes(path: path); - if (mailboxesResponse.isNotEmpty) { - return mailboxesResponse.first; + final parser = NoopParser(this, + _selectedMailbox ?? Mailbox.setup(path, path, [MailboxFlag.noSelect])); + await sendCommand(cmd, parser); + final matchingBoxes = await listMailboxes(path: path); + if (matchingBoxes.isNotEmpty) { + return matchingBoxes.first; } - return response; + throw ImapException(this, + 'Unable to find just created mailbox with the path [$path]. Please report this problem.'); } /// Removes the specified mailbox @@ -1366,8 +1372,8 @@ class ImapClient extends ClientBase { newName = _encodeMailboxPath(newName); final cmd = Command('RENAME $path $newName'); - final response = - await sendCommand(cmd, NoopParser(this, _selectedMailbox)); + final response = await sendCommand( + cmd, NoopParser(this, _selectedMailbox ?? box)); if (box.name == 'INBOX') { /* Renaming INBOX is permitted, and has special behavior. It moves all messages in INBOX to a new mailbox with the given name,