Skip to content

Commit

Permalink
fix renaming mailbox without having selected any
Browse files Browse the repository at this point in the history
This fixes #160, #164, #165
  • Loading branch information
robert-virkus committed Jun 16, 2021
1 parent e568bcb commit afc8c79
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/imap/imap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,9 @@ class ImapClient extends ClientBase {
/// Compare [unselectMailbox]
/// Compare [expunge]
Future<Mailbox?> closeMailbox() {
if (_selectedMailbox == null) {
return Future.value();
}
final cmd = Command('CLOSE');
final parser = NoResponseParser(_selectedMailbox);
_selectedMailbox = null;
Expand All @@ -966,6 +969,9 @@ class ImapClient extends ClientBase {
///
/// Compare [selectMailbox]
Future<void> unselectMailbox() {
if (_selectedMailbox == null) {
return Future.value();
}
final cmd = Command('UNSELECT');
final parser = NoResponseParser(_selectedMailbox);
_selectedMailbox = null;
Expand Down Expand Up @@ -1335,19 +1341,19 @@ class ImapClient extends ClientBase {
return sendCommand<Mailbox>(cmd, parser);
}

/// Creates the specified mailbox
///
/// Spefify the name with [path]
/// Creates a new mailbox with the specified [path]
Future<Mailbox> createMailbox(String path) async {
final encodedPath = _encodeMailboxPath(path);
final cmd = Command('CREATE $encodedPath');
final response =
await sendCommand<Mailbox>(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<Mailbox>(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
Expand All @@ -1366,8 +1372,8 @@ class ImapClient extends ClientBase {
newName = _encodeMailboxPath(newName);

final cmd = Command('RENAME $path $newName');
final response =
await sendCommand<Mailbox>(cmd, NoopParser(this, _selectedMailbox));
final response = await sendCommand<Mailbox>(
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,
Expand Down

0 comments on commit afc8c79

Please sign in to comment.