From b5cf15c4bb874a0a3868f5e539f950d2891c216b Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 17 Feb 2015 10:00:48 -0500 Subject: [PATCH] Added logic to ImapFolder.GetSubfolders() to filter out unexpected folders This shouldn't be necessary, but broken IMAP servers like SmarterMail 13.0 (which is apparently not very smart at all) ruin it for the rest of us. Fixes issue #149 --- MailKit/Net/Imap/ImapFolder.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/MailKit/Net/Imap/ImapFolder.cs b/MailKit/Net/Imap/ImapFolder.cs index 799f9be2f3..7ff18ffb71 100644 --- a/MailKit/Net/Imap/ImapFolder.cs +++ b/MailKit/Net/Imap/ImapFolder.cs @@ -805,26 +805,37 @@ static void QResyncFetch (ImapEngine engine, ImapCommand ic, int index) { CheckState (false, false); - var pattern = EncodedName.Length > 0 ? EncodedName + DirectorySeparator + "%" : "%"; + var pattern = EncodedName.Length > 0 ? EncodedName + DirectorySeparator : string.Empty; var command = subscribedOnly ? "LSUB" : "LIST"; + var children = new List (); var list = new List (); - var ic = new ImapCommand (Engine, cancellationToken, null, command + " \"\" %S\r\n", pattern); + var ic = new ImapCommand (Engine, cancellationToken, null, command + " \"\" %S\r\n", pattern + "%"); ic.RegisterUntaggedHandler (command, ImapUtils.ParseFolderList); ic.UserData = list; Engine.QueueCommand (ic); Engine.Wait (ic); - foreach (var folder in list) + // Note: Some broken IMAP servers (*cough* SmarterMail 13.0 *cough*) return folders + // that are not children of the folder we requested, so we need to filter those + // folders out of the list we'll be returning to our caller. + // + // See https://github.com/jstedfast/MailKit/issues/149 for more details. + foreach (var folder in list) { + if (folder.FullName != pattern + folder.Name) + continue; + folder.ParentFolder = this; + children.Add (folder); + } ProcessResponseCodes (ic, null); if (ic.Result != ImapCommandResult.Ok) throw ImapCommandException.Create (subscribedOnly ? "LSUB" : "LIST", ic); - return list; + return children; } ///