Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-virkus committed Oct 5, 2023
2 parents e656b6c + 38c8947 commit bed8824
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/src/private/imap/status_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import 'response_parser.dart';
/// Parses status responses
class StatusParser extends ResponseParser<Mailbox> {
/// Creates a new parser
StatusParser(this.box);
StatusParser(this.box) : _regex = RegExp(r'(STATUS "[^"]+?" )(.*)');

/// The current mailbox
Mailbox box;

final RegExp _regex;

@override
Mailbox? parse(ImapResponse imapResponse, Response<Mailbox> response) =>
response.isOkStatus ? box : null;
Expand All @@ -19,7 +21,7 @@ class StatusParser extends ResponseParser<Mailbox> {
bool parseUntagged(ImapResponse imapResponse, Response<Mailbox>? response) {
final details = imapResponse.parseText;
if (details.startsWith('STATUS ')) {
final startIndex = details.indexOf('(');
final startIndex = _findStartIndex(details);
if (startIndex == -1) {
return false;
}
Expand Down Expand Up @@ -56,4 +58,12 @@ class StatusParser extends ResponseParser<Mailbox> {
return super.parseUntagged(imapResponse, response);
}
}

int _findStartIndex(String details) {
final matches = _regex.allMatches(details);
if (matches.isNotEmpty && matches.first.groupCount == 2) {
return matches.first.group(1)!.length;
}
return -1;
}
}
17 changes: 17 additions & 0 deletions test/src/imap/status_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ void main() {
expect(box.uidValidity, 2222);
expect(box.uidNext, 876);
});

test('Status of Mailbox with name containing brackets', () {
const responseText =
'STATUS "upper level.Funny folder (with brackets)" (MESSAGES 2)';
final details = ImapResponse()..add(ImapResponseLine(responseText));
final box = Mailbox(
encodedName: 'Funny folder (with brackets)',
encodedPath: 'upper level.Funny folder (with brackets)',
flags: [MailboxFlag.junk],
pathSeparator: '.',
);
final parser = StatusParser(box);
final response = Response<Mailbox>()..status = ResponseStatus.ok;
final processed = parser.parseUntagged(details, response);
expect(processed, true);
expect(box.messagesExists, 2);
});
}

0 comments on commit bed8824

Please sign in to comment.