From 740ff9089932e8648e7ffd77dc4e711331fa3918 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 26 Dec 2023 15:12:27 -0500 Subject: [PATCH] Added ImapCommand.ThrowIfNotOk() to reduce code duplication --- MailKit/Net/Imap/ImapClient.cs | 27 +++------ MailKit/Net/Imap/ImapCommand.cs | 12 ++++ MailKit/Net/Imap/ImapEngine.cs | 12 ++-- MailKit/Net/Imap/ImapFolder.cs | 91 ++++++++++------------------ MailKit/Net/Imap/ImapFolderFlags.cs | 12 ++-- MailKit/Net/Imap/ImapFolderSearch.cs | 6 +- 6 files changed, 62 insertions(+), 98 deletions(-) diff --git a/MailKit/Net/Imap/ImapClient.cs b/MailKit/Net/Imap/ImapClient.cs index 390c2932f4..12ef90019d 100644 --- a/MailKit/Net/Imap/ImapClient.cs +++ b/MailKit/Net/Imap/ImapClient.cs @@ -384,8 +384,7 @@ bool TryQueueEnableQuickResyncCommand (CancellationToken cancellationToken, out static void ProcessEnableResponse (ImapCommand ic) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("ENABLE", ic); + ic.ThrowIfNotOk ("ENABLE"); } /// @@ -547,8 +546,7 @@ ImapCommand QueueIdentifyCommand (ImapImplementation clientImplementation, Cance static ImapImplementation ProcessIdentifyResponse (ImapCommand ic) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("ID", ic); + ic.ThrowIfNotOk ("ID"); return (ImapImplementation) ic.UserData; } @@ -1807,8 +1805,7 @@ ImapCommand QueueNoOpCommand (CancellationToken cancellationToken) static void ProcessNoOpResponse (ImapCommand ic) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("NOOP", ic); + ic.ThrowIfNotOk ("NOOP"); } /// @@ -1890,8 +1887,7 @@ ImapCommand QueueIdleCommand (ImapIdleContext context, CancellationToken cancell static void ProcessIdleResponse (ImapCommand ic) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("IDLE", ic); + ic.ThrowIfNotOk ("IDLE"); } /// @@ -2004,8 +2000,7 @@ ImapCommand QueueNotifyCommand (bool status, IList eventGroups, void ProcessNotifyResponse (ImapCommand ic, bool notifySelectedNewExpunge) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("NOTIFY", ic); + ic.ThrowIfNotOk ("NOTIFY"); engine.NotifySelectedNewExpunge = notifySelectedNewExpunge; } @@ -2085,8 +2080,7 @@ ImapCommand QueueDisableNotifyCommand (CancellationToken cancellationToken) void ProcessDisableNotifyResponse (ImapCommand ic) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("NOTIFY", ic); + ic.ThrowIfNotOk ("NOTIFY"); engine.NotifySelectedNewExpunge = false; } @@ -2430,8 +2424,7 @@ ImapCommand QueueGetMetadataCommand (MetadataTag tag, CancellationToken cancella string ProcessGetMetadataResponse (ImapCommand ic, MetadataTag tag) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("GETMETADATA", ic); + ic.ThrowIfNotOk ("GETMETADATA"); var metadata = (MetadataCollection) ic.UserData; string value = null; @@ -2557,8 +2550,7 @@ bool TryQueueGetMetadataCommand (MetadataOptions options, IEnumerable 0 && ic.RespCodes[ic.RespCodes.Count - 1].Type == ImapResponseCodeType.Metadata) { var metadata = (MetadataResponseCode) ic.RespCodes[ic.RespCodes.Count - 1]; @@ -2663,8 +2655,7 @@ bool TryQueueSetMetadataCommand (MetadataCollection metadata, CancellationToken static void ProcessSetMetadataResponse (ImapCommand ic) { - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SETMETADATA", ic); + ic.ThrowIfNotOk ("SETMETADATA"); } /// diff --git a/MailKit/Net/Imap/ImapCommand.cs b/MailKit/Net/Imap/ImapCommand.cs index fae9e50c41..22abab74b8 100644 --- a/MailKit/Net/Imap/ImapCommand.cs +++ b/MailKit/Net/Imap/ImapCommand.cs @@ -1132,5 +1132,17 @@ public ImapResponseCode GetResponseCode (ImapResponseCodeType type) return null; } + + /// + /// Throw an if the response was not OK. + /// + /// + /// Throws an if the response was not OK. + /// + public void ThrowIfNotOk (string command) + { + if (Response != ImapCommandResponse.Ok) + throw ImapCommandException.Create (command, this); + } } } diff --git a/MailKit/Net/Imap/ImapEngine.cs b/MailKit/Net/Imap/ImapEngine.cs index b5a92e41ae..1829472678 100644 --- a/MailKit/Net/Imap/ImapEngine.cs +++ b/MailKit/Net/Imap/ImapEngine.cs @@ -3302,8 +3302,7 @@ ImapFolder ProcessGetQuotaRootResponse (ImapCommand ic, string quotaRoot, out Li list = (List) ic.UserData; - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("LIST", ic); + ic.ThrowIfNotOk ("LIST"); if ((folder = GetFolder (list, quotaRoot)) == null) { folder = CreateImapFolder (quotaRoot, FolderAttributes.NonExistent, '.'); @@ -3386,8 +3385,7 @@ static ImapFolder ProcessGetFolderResponse (ImapCommand ic, string path, string list = (List) ic.UserData; - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("LIST", ic); + ic.ThrowIfNotOk ("LIST"); if ((folder = GetFolder (list, encodedName)) == null) throw new FolderNotFoundException (path); @@ -3578,8 +3576,7 @@ public IList GetFolders (FolderNamespace @namespace, StatusItems it Run (ic); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create (ic.Lsub ? "LSUB" : "LIST", ic); + ic.ThrowIfNotOk (ic.Lsub ? "LSUB" : "LIST"); LookupParentFolders (list, cancellationToken); @@ -3611,8 +3608,7 @@ public async Task> GetFoldersAsync (FolderNamespace @namespac await RunAsync (ic).ConfigureAwait (false); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create (ic.Lsub ? "LSUB" : "LIST", ic); + ic.ThrowIfNotOk (ic.Lsub ? "LSUB" : "LIST"); await LookupParentFoldersAsync (list, cancellationToken).ConfigureAwait (false); diff --git a/MailKit/Net/Imap/ImapFolder.cs b/MailKit/Net/Imap/ImapFolder.cs index b05e494b53..677b571330 100644 --- a/MailKit/Net/Imap/ImapFolder.cs +++ b/MailKit/Net/Imap/ImapFolder.cs @@ -374,8 +374,7 @@ void ProcessOpenResponse (ImapCommand ic, FolderAccess access) { ProcessResponseCodes (ic, this); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create (access == FolderAccess.ReadOnly ? "EXAMINE" : "SELECT", ic); + ic.ThrowIfNotOk (access == FolderAccess.ReadOnly ? "EXAMINE" : "SELECT"); } FolderAccess Open () @@ -690,8 +689,7 @@ void ProcessCloseResponse (ImapCommand ic, bool expunge) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create (expunge ? "CLOSE" : "UNSELECT", ic); + ic.ThrowIfNotOk (expunge ? "CLOSE" : "UNSELECT"); } void Close () @@ -814,8 +812,7 @@ IMailFolder ProcessGetCreatedFolderResponse (ImapCommand ic, string encodedName, ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("LIST", ic); + ic.ThrowIfNotOk ("LIST"); if ((folder = ImapEngine.GetFolder (list, encodedName)) != null) { folder.ParentFolder = this; @@ -1187,8 +1184,7 @@ void ProcessRenameResponse (ImapCommand ic, IMailFolder parent, string name, str ProcessResponseCodes (ic, this); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("RENAME", ic); + ic.ThrowIfNotOk ("RENAME"); Engine.FolderCache.Remove (EncodedName); Engine.FolderCache[encodedName] = this; @@ -1335,8 +1331,7 @@ void ProcessDeleteResponse (ImapCommand ic) { ProcessResponseCodes (ic, this); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("DELETE", ic); + ic.ThrowIfNotOk ("DELETE"); Reset (); @@ -1444,8 +1439,7 @@ void ProcessSubscribeResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SUBSCRIBE", ic); + ic.ThrowIfNotOk ("SUBSCRIBE"); if ((Attributes & FolderAttributes.Subscribed) == 0) { Attributes |= FolderAttributes.Subscribed; @@ -1540,8 +1534,7 @@ void ProcessUnsubscribeResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("UNSUBSCRIBE", ic); + ic.ThrowIfNotOk ("UNSUBSCRIBE"); if ((Attributes & FolderAttributes.Subscribed) != 0) { Attributes &= ~FolderAttributes.Subscribed; @@ -1731,8 +1724,7 @@ IList ProcessGetSubfoldersResponse (ImapCommand ic, List list, s ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("LIST", ic); + ic.ThrowIfNotOk ("LIST"); if ((folder = ImapEngine.GetFolder (list, encodedName)) != null) folder.ParentFolder = this; @@ -2026,8 +2017,7 @@ void ProcessCheckResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("CHECK", ic); + ic.ThrowIfNotOk ("CHECK"); } /// @@ -2136,8 +2126,7 @@ void ProcessStatusResponse (ImapCommand ic, bool throwNotFound) { ProcessResponseCodes (ic, this, throwNotFound); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("STATUS", ic); + ic.ThrowIfNotOk ("STATUS"); } internal void Status (StatusItems items, bool throwNotFound, CancellationToken cancellationToken) @@ -2333,8 +2322,7 @@ AccessControlList ProcessGetAccessControlListResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("GETACL", ic); + ic.ThrowIfNotOk ("GETACL"); return (AccessControlList) ic.UserData; } @@ -2496,8 +2484,7 @@ AccessRights ProcessGetAccessRightsResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("LISTRIGHTS", ic); + ic.ThrowIfNotOk ("LISTRIGHTS"); return (AccessRights) ic.UserData; } @@ -2646,8 +2633,7 @@ AccessRights ProcessGetMyAccessRightsResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("MYRIGHTS", ic); + ic.ThrowIfNotOk ("MYRIGHTS"); return (AccessRights) ic.UserData; } @@ -2757,8 +2743,7 @@ void ProcessModifyAccessRightsResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SETACL", ic); + ic.ThrowIfNotOk ("SETACL"); } /// @@ -3075,8 +3060,7 @@ void ProcessRemoveAccessResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("DELETEACL", ic); + ic.ThrowIfNotOk ("DELETEACL"); } /// @@ -3191,8 +3175,8 @@ string ProcessGetMetadataResponse (ImapCommand ic, MetadataTag tag) ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("GETMETADATA", ic); + ic.ThrowIfNotOk ("GETMETADATA"); + string value = null; for (int i = 0; i < metadata.Count; i++) { @@ -3358,8 +3342,7 @@ MetadataCollection ProcessGetMetadataResponse (ImapCommand ic, MetadataOptions o { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("GETMETADATA", ic); + ic.ThrowIfNotOk ("GETMETADATA"); var metadata = (MetadataResponseCode) ic.GetResponseCode (ImapResponseCodeType.Metadata); if (metadata != null && metadata.SubType == MetadataResponseCodeSubType.LongEntries) @@ -3514,8 +3497,7 @@ void ProcessSetMetadataResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SETMETADATA", ic); + ic.ThrowIfNotOk ("SETMETADATA"); } /// @@ -3837,8 +3819,7 @@ bool TryProcessGetQuotaResponse (ImapCommand ic, out string encodedName, out Quo ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("GETQUOTAROOT", ic); + ic.ThrowIfNotOk ("GETQUOTAROOT"); for (int i = 0; i < ctx.QuotaRoots.Count; i++) { encodedName = ctx.QuotaRoots[i]; @@ -3997,8 +3978,7 @@ FolderQuota ProcessSetQuotaResponse (ImapCommand ic) ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SETQUOTA", ic); + ic.ThrowIfNotOk ("SETQUOTA"); if (ctx.Quotas.TryGetValue (EncodedName, out var quota)) { return new FolderQuota (this) { @@ -4113,8 +4093,7 @@ void ProcessExpungeResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("EXPUNGE", ic); + ic.ThrowIfNotOk ("EXPUNGE"); } /// @@ -4482,8 +4461,7 @@ ImapCommand QueueAppend (FormatOptions options, IAppendRequest request, Cancella { ProcessResponseCodes (ic, this); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("APPEND", ic); + ic.ThrowIfNotOk ("APPEND"); var append = (AppendUidResponseCode) ic.GetResponseCode (ImapResponseCodeType.AppendUid); @@ -4682,8 +4660,7 @@ IList ProcessMultiAppendResponse (ImapCommand ic) { ProcessResponseCodes (ic, this); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("APPEND", ic); + ic.ThrowIfNotOk ("APPEND"); var append = (AppendUidResponseCode) ic.GetResponseCode (ImapResponseCodeType.AppendUid); @@ -4928,8 +4905,7 @@ ImapCommand QueueReplace (FormatOptions options, UniqueId uid, IReplaceRequest r { ProcessResponseCodes (ic, this); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("REPLACE", ic); + ic.ThrowIfNotOk ("REPLACE"); var append = (AppendUidResponseCode) ic.GetResponseCode (ImapResponseCodeType.AppendUid); @@ -5323,8 +5299,7 @@ IList ProcessGetIndexesResponse (ImapCommand ic) ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SEARCH", ic); + ic.ThrowIfNotOk ("SEARCH"); var indexes = new int[results.UniqueIds.Count]; for (int i = 0; i < indexes.Length; i++) @@ -5363,8 +5338,7 @@ void ProcessCopyToResponse (ImapCommand ic, IMailFolder destination, ref UniqueI { ProcessResponseCodes (ic, destination); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("COPY", ic); + ic.ThrowIfNotOk ("COPY"); var copy = (CopyUidResponseCode) ic.GetResponseCode (ImapResponseCodeType.CopyUid); @@ -5544,8 +5518,7 @@ void ProcessMoveToResponse (ImapCommand ic, IMailFolder destination, ref UniqueI { ProcessResponseCodes (ic, destination); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("MOVE", ic); + ic.ThrowIfNotOk ("MOVE"); var copy = (CopyUidResponseCode) ic.GetResponseCode (ImapResponseCodeType.CopyUid); @@ -5779,8 +5752,7 @@ void ProcessCopyToResponse (ImapCommand ic, IMailFolder destination) { ProcessResponseCodes (ic, destination); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("COPY", ic); + ic.ThrowIfNotOk ("COPY"); } /// @@ -5927,8 +5899,7 @@ void ProcessMoveToResponse (ImapCommand ic, IMailFolder destination) { ProcessResponseCodes (ic, destination); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("MOVE", ic); + ic.ThrowIfNotOk ("MOVE"); } /// diff --git a/MailKit/Net/Imap/ImapFolderFlags.cs b/MailKit/Net/Imap/ImapFolderFlags.cs index 1ee9ca1fa5..5360170d51 100644 --- a/MailKit/Net/Imap/ImapFolderFlags.cs +++ b/MailKit/Net/Imap/ImapFolderFlags.cs @@ -120,8 +120,7 @@ async Task> StoreAsync (IList uids, IStoreFlagsRequest ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("STORE", ic); + ic.ThrowIfNotOk ("STORE"); ProcessUnmodified (ic, ref unmodified, request.UnchangedSince); } @@ -293,8 +292,7 @@ async Task> StoreAsync (IList indexes, IStoreFlagsRequest reques ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("STORE", ic); + ic.ThrowIfNotOk ("STORE"); return GetUnmodified (ic, request.UnchangedSince); } @@ -497,8 +495,7 @@ async Task> StoreAsync (IList uids, IStoreLabelsReques ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("STORE", ic); + ic.ThrowIfNotOk ("STORE"); ProcessUnmodified (ic, ref unmodified, request.UnchangedSince); } @@ -670,8 +667,7 @@ async Task> StoreAsync (IList indexes, IStoreLabelsRequest reque ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("STORE", ic); + ic.ThrowIfNotOk ("STORE"); return GetUnmodified (ic, request.UnchangedSince); } diff --git a/MailKit/Net/Imap/ImapFolderSearch.cs b/MailKit/Net/Imap/ImapFolderSearch.cs index 2b04970980..937af2c877 100644 --- a/MailKit/Net/Imap/ImapFolderSearch.cs +++ b/MailKit/Net/Imap/ImapFolderSearch.cs @@ -857,8 +857,7 @@ SearchResults ProcessSearchResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SEARCH", ic); + ic.ThrowIfNotOk ("SEARCH"); return (SearchResults) ic.UserData; } @@ -1193,8 +1192,7 @@ SearchResults ProcessSortResponse (ImapCommand ic) { ProcessResponseCodes (ic, null); - if (ic.Response != ImapCommandResponse.Ok) - throw ImapCommandException.Create ("SORT", ic); + ic.ThrowIfNotOk ("SORT"); return (SearchResults) ic.UserData; }