From d4df4b6d871e7d63c80e85ffa0425e9e32786997 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Sat, 14 Nov 2020 04:25:34 +0000 Subject: [PATCH] Add methods to control size of in-memory queue os emails --- Headers/GNUstepBase/GSMime.h | 11 ++++++++++ Source/Additions/GSMime.m | 41 ++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Headers/GNUstepBase/GSMime.h b/Headers/GNUstepBase/GSMime.h index c74889cad..856014f3b 100644 --- a/Headers/GNUstepBase/GSMime.h +++ b/Headers/GNUstepBase/GSMime.h @@ -434,6 +434,10 @@ GS_GSMimeSMTPClient_IVARS; */ - (NSError*) lastError; +/** Returns the number of messages currently in the queue. + */ +- (NSUInteger) queueSize; + /** Add the message to the queue of emails to be sent by the receiver. */ - (void) send: (GSMimeDocument*)message; @@ -464,6 +468,13 @@ GS_GSMimeSMTPClient_IVARS; */ - (void) setIdentity: (NSString*)s; +/** Sets the maximum number of messages which may remain in the queue. + * If this is exceeded then any unsuccessful send attempt results in + * excess queued messages discarded as unsent.
+ * The method returns the previous setting. + */ +- (NSUInteger) setMaximum: (NSUInteger)m; + /** Set the originator for any emails sent by the SMTP client.
* This overrides the value in the 'from' header of an email.
* If this is not set (or is set to nil) then the GSMimeSMTPClientOriginator diff --git a/Source/Additions/GSMime.m b/Source/Additions/GSMime.m index 4abc01328..e690d8e29 100644 --- a/Source/Additions/GSMime.m +++ b/Source/Additions/GSMime.m @@ -69,6 +69,7 @@ GSMimeDocument *current;\ GSMimeHeader *version;\ NSMutableArray *queue;\ + NSUInteger maximum;\ NSMutableArray *pending;\ NSInputStream *istream;\ NSOutputStream *ostream;\ @@ -8052,7 +8053,7 @@ GS_PRIVATE_INTERNAL(GSMimeSMTPClient) @implementation GSMimeSMTPClient -/* Shuts the connection down, fails any message in progress, anbd discards all +/* Shuts the connection down, fails any message in progress, and discards all * queued messages as 'unsent' */ - (void) abort @@ -8147,6 +8148,11 @@ GS_PRIVATE_INTERNAL(GSMimeSMTPClient) return internal->lastError; } +- (NSUInteger) queueSize +{ + return [internal->queue count]; +} + - (void) send: (GSMimeDocument*)message { [self send: message envelopeID: nil]; @@ -8194,6 +8200,14 @@ GS_PRIVATE_INTERNAL(GSMimeSMTPClient) ASSIGNCOPY(internal->identity, s); } +- (NSUInteger) setMaximum: (NSUInteger)m +{ + NSUInteger old = internal->maximum; + + internal->maximum = m; + return old; +} + - (void) setOriginator: (NSString*)s { ASSIGNCOPY(internal->originator, s); @@ -8944,9 +8958,28 @@ GS_PRIVATE_INTERNAL(GSMimeSMTPClient) [internal->pending removeAllObjects]; ASSIGN(internal->lastError, e); - if (internal->current != nil) + if (nil == internal->current) { - GSMimeDocument *d = [internal->current retain]; + while ([self queueSize] > internal->maximum) + { + GSMimeDocument *d = RETAIN([internal->queue objectAtIndex: 0]); + + [internal->queue removeObjectAtIndex: 0]; + if (nil == internal->delegate) + { + NSDebugMLLog(@"GSMime", @"-smtpClient:mimeUnsent: %@ %@", + self, d); + } + else + { + [internal->delegate smtpClient: self mimeUnsent: d]; + } + RELEASE(d); + } + } + else + { + GSMimeDocument *d = RETAIN(internal->current); [internal->queue removeObjectAtIndex: 0]; internal->current = nil; @@ -8958,7 +8991,7 @@ GS_PRIVATE_INTERNAL(GSMimeSMTPClient) { [internal->delegate smtpClient: self mimeFailed: d]; } - [d release]; + RELEASE(d); } if ([internal->queue count] > 0) {