Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SoObjects/Contacts/SOGoContactLDIFEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ - (id) initWithName: (NSString *) newName
withLDIFEntry: (NSDictionary *) newEntry
inContainer: (id) newContainer
{
if ([newName length] == 0) {
newName = @"Unnamed Contact";
}
if ((self = [self initWithName: newName inContainer: newContainer]))
{
ASSIGN (ldifEntry, newEntry);
Expand Down
131 changes: 89 additions & 42 deletions SoObjects/Mailer/SOGoDraftObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -1771,52 +1771,99 @@ - (BOOL) isEmptyValue: (id) _value
return NO;
}

- (NSString *) _quoteSpecials: (NSString *) address
{
NSString *result, *part, *s2;
int i, len;

// We want to correctly send mails to recipients such as :
// foo.bar
// foo (bar) <foo@zot.com>
// bar, foo <foo@zot.com>
if ([address indexOf: '('] >= 0 || [address indexOf: ')'] >= 0
|| [address indexOf: '<'] >= 0 || [address indexOf: '>'] >= 0
|| [address indexOf: '@'] >= 0 || [address indexOf: ','] >= 0
|| [address indexOf: ';'] >= 0 || [address indexOf: ':'] >= 0
|| [address indexOf: '\\'] >= 0 || [address indexOf: '"'] >= 0
|| [address indexOf: '.'] >= 0
|| [address indexOf: '['] >= 0 || [address indexOf: ']'] >= 0)
{
// We search for the first instance of < from the end
// and we quote what was before if we need to
len = [address length];
i = -1;
while (len--)
if ([address characterAtIndex: len] == '<')
{
i = len;
break;
}
#ZN|- (NSString *) _quoteSpecials: (NSString *) address
{
if (!address || [address length] == 0)
return address;

if (i > 0)
{
part = [address substringToIndex: i - 1];
s2 = [[part stringByReplacingString: @"\\" withString: @"\\\\"]
stringByReplacingString: @"\"" withString: @"\\\""];
result = [NSString stringWithFormat: @"\"%@\" %@", s2, [address substringFromIndex: i]];
}
else
{
s2 = [[address stringByReplacingString: @"\\" withString: @"\\\\"]
stringByReplacingString: @"\"" withString: @"\\\""];
result = [NSString stringWithFormat: @"\"%@\"", s2];
}
// Find the last occurrence of '<' to separate display name from email
NSRange bracketRange = [address rangeOfString: @"<" options: NSBackwardsSearch];

if (bracketRange.location == NSNotFound)
{
// No address notation - treat entire string as potential display name
// Quote if it contains RFC 5322 special characters in the phrase context
if ([self _needsQuotingForPhrase: address])
return [self _quoteAndEscape: address];
return address;
}

// We have <...> notation
NSString *displayName = nil;
NSString *emailPart = nil;

// Extract display name part (everything before <)
if (bracketRange.location > 0)
{
displayName = [address substringToIndex: bracketRange.location];
// Trim trailing whitespace
displayName = [displayName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
}

// Extract email part (from < onwards)
if (bracketRange.location < [address length])
{
emailPart = [address substringFromIndex: bracketRange.location];
}

// If no display name, return the email part as-is
if (!displayName || [displayName length] == 0)
return emailPart;

// Check if display name is already properly formatted
if ([self _alreadyProperlyFormatted: displayName])
return [NSString stringWithFormat: @"%@ %@", displayName, emailPart];

// Quote the display name if it contains special characters
NSString *quotedDisplay;
if ([self _needsQuotingForPhrase: displayName])
quotedDisplay = [self _quoteAndEscape: displayName];
else
result = address;
quotedDisplay = displayName;

return result;
return [NSString stringWithFormat: @"%@ %@", quotedDisplay, emailPart];
}

- (BOOL) _needsQuotingForPhrase: (NSString *) phrase
{
// RFC 5322: Characters that require quoting in a phrase
// Space, comma, semicolon, colon, at-sign, period, angle brackets,
// square brackets, parentheses, backslash, quote
NSCharacterSet *needsQuotingChars = [NSCharacterSet characterSetWithCharactersInString:
@" ,;:@.<>\[\]()\\\""];

return [phrase rangeOfCharacterFromSet: needsQuotingChars].location != NSNotFound;
}

- (BOOL) _alreadyProperlyFormatted: (NSString *) displayName
{
NSString *trimmed = [displayName stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];

// Check if it's already quoted (starts and ends with ")
if ([trimmed length] >= 2 &&
[trimmed characterAtIndex: 0] == '"' &&
[trimmed characterAtIndex: [trimmed length] - 1] == '"')
return YES;

// Check if it's an RFC 2047 encoded word (starts with =? and ends with ?=)
if ([trimmed hasPrefix: @"=?"] && [trimmed hasSuffix: @"?="])
return YES;

return NO;
}

- (NSString *) _quoteAndEscape: (NSString *) unquoted
{
// Trim whitespace first
NSString *trimmed = [unquoted stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];

// Escape backslashes and double quotes according to RFC 5322
NSString *escaped = [trimmed stringByReplacingString: @"\\" withString: @"\\\\"];
escaped = [escaped stringByReplacingString: @"\"" withString: @"\\\\\""];

return [NSString stringWithFormat: @"\"%@\"", escaped];
}

- (NSArray *) _quoteSpecialsInArray: (NSArray *) addresses
Expand Down
1 change: 1 addition & 0 deletions SoObjects/SOGo/SOGoCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@

- (void) setMessageSubmissionsCount: (int) theCount
recipientsCount: (int) theRecipientsCount
isBlocked: (bool) blocked
forLogin: (NSString *) theLogin;

- (NSDictionary *) messageSubmissionsCountForLogin: (NSString *) theLogin;
Expand Down
2 changes: 2 additions & 0 deletions SoObjects/SOGo/SOGoCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ - (NSDictionary *) failedCountForLogin: (NSString *) theLogin
//
- (void) setMessageSubmissionsCount: (int) theCount
recipientsCount: (int) theRecipientsCount
isBlocked: (bool) blocked
forLogin: (NSString *) theLogin
{
NSNumber *messages_count, *recipients_count;
Expand All @@ -555,6 +556,7 @@ - (void) setMessageSubmissionsCount: (int) theCount

[d setObject: messages_count forKey: @"MessagesCount"];
[d setObject: recipients_count forKey: @"RecipientsCount"];
[d setObject: [NSNumber numberWithBool:blocked] forKey: @"isBlocked"];

[self _cacheValues: [d jsonRepresentation]
ofType: @"messagesubmissions"
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $(TEST_TOOL)_OBJC_FILES += \
TestNSURL+misc.m \
TestNGMailAddressParser.m \
TestNGInternetSocketAddress.m \
TestSOGoDraftObjectQuoteSpecials.m \
\
TestRTFHandler.m \
\
Expand Down
Loading