From 469c27ccef2f7e50629ee485e810bb45a76767aa Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Thu, 14 Sep 2023 16:53:39 -0400 Subject: [PATCH] minor refactoring --- MailKit/Net/Imap/ImapFolder.cs | 35 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/MailKit/Net/Imap/ImapFolder.cs b/MailKit/Net/Imap/ImapFolder.cs index 5b471af029..3b4e804b31 100644 --- a/MailKit/Net/Imap/ImapFolder.cs +++ b/MailKit/Net/Imap/ImapFolder.cs @@ -191,6 +191,15 @@ void CheckAllowIndexes () throw new InvalidOperationException ("Indexes and '*' cannot be used while MessageNew/MessageExpunge is registered with NOTIFY for SELECTED."); } + void CheckValidDestination (IMailFolder destination) + { + if (destination == null) + throw new ArgumentNullException (nameof (destination)); + + if (destination is not ImapFolder target || (target.Engine != Engine)) + throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (destination)); + } + internal void Reset () { // basic state @@ -1503,7 +1512,7 @@ async Task> GetSubfoldersAsync (StatusItems items, bool subsc // in order to reduce the list of folders returned by our LIST command. var pattern = new StringBuilder (EncodedName.Length + 2); pattern.Append (EncodedName); - for (int i = 0; i < EncodedName.Length; i++) { + for (int i = 0; i < pattern.Length; i++) { if (pattern[i] == '*') pattern[i] = '%'; } @@ -4931,11 +4940,7 @@ async Task CopyToAsync (IList uids, IMailFolder destinati if (uids == null) throw new ArgumentNullException (nameof (uids)); - if (destination == null) - throw new ArgumentNullException (nameof (destination)); - - if (destination is not ImapFolder target || (target.Engine != Engine)) - throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (destination)); + CheckValidDestination (destination); CheckState (true, false); @@ -5106,11 +5111,7 @@ async Task MoveToAsync (IList uids, IMailFolder destinati if (uids == null) throw new ArgumentNullException (nameof (uids)); - if (destination == null) - throw new ArgumentNullException (nameof (destination)); - - if (destination is not ImapFolder || ((ImapFolder) destination).Engine != Engine) - throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (destination)); + CheckValidDestination (destination); CheckState (true, true); @@ -5278,11 +5279,7 @@ async Task CopyToAsync (IList indexes, IMailFolder destination, bool doAsyn if (indexes == null) throw new ArgumentNullException (nameof (indexes)); - if (destination == null) - throw new ArgumentNullException (nameof (destination)); - - if (destination is not ImapFolder || ((ImapFolder) destination).Engine != Engine) - throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (destination)); + CheckValidDestination (destination); CheckState (true, false); CheckAllowIndexes (); @@ -5424,11 +5421,7 @@ async Task MoveToAsync (IList indexes, IMailFolder destination, bool doAsyn if (indexes == null) throw new ArgumentNullException (nameof (indexes)); - if (destination == null) - throw new ArgumentNullException (nameof (destination)); - - if (destination is not ImapFolder || ((ImapFolder) destination).Engine != Engine) - throw new ArgumentException ("The destination folder does not belong to this ImapClient.", nameof (destination)); + CheckValidDestination (destination); CheckState (true, true); CheckAllowIndexes ();