From 82b3004ced0e2308e17470f882bfadb2f8e27e7e Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Mon, 18 Aug 2008 19:01:02 +0000 Subject: [PATCH] Monotone-Parent: 5a485db5775478811d6c38cfd017ebee3f75514c Monotone-Revision: 6abac7888de0863a3426a8b20916425be34e85f9 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2008-08-18T19:01:02 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 4 ++ SoObjects/Mailer/NSString+Mail.h | 1 + SoObjects/Mailer/NSString+Mail.m | 14 +++++++ SoObjects/Mailer/SOGoDraftObject.m | 65 +++++++++++++++++++++++++++--- 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 40e35f914..0c216435c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2008-08-18 Ludovic Marcotte + * SoObjects/Mailer/SOGoDraftObject.m ([SOGoDraftObject + -mimeHeaderMapWithHeaders:_headers]): properly quote atoms in mail + addresses. + * UI/MailPartViewers/UIxMailPartMessageViewer.m ([UIxMailPartMessageViewer -formattedComponents:components]): don't report nil components. diff --git a/SoObjects/Mailer/NSString+Mail.h b/SoObjects/Mailer/NSString+Mail.h index 3b34491cb..7dbe514d9 100644 --- a/SoObjects/Mailer/NSString+Mail.h +++ b/SoObjects/Mailer/NSString+Mail.h @@ -28,6 +28,7 @@ @interface NSString (SOGoExtension) - (NSString *) htmlToText; +- (int) indexOf: (unichar) _c; - (NSString *) decodedSubject; @end diff --git a/SoObjects/Mailer/NSString+Mail.m b/SoObjects/Mailer/NSString+Mail.m index 084280a26..a17658273 100644 --- a/SoObjects/Mailer/NSString+Mail.m +++ b/SoObjects/Mailer/NSString+Mail.m @@ -349,6 +349,20 @@ return [handler result]; } +- (int) indexOf: (unichar) _c +{ + int i, len; + + len = [self length]; + + for (i = 0; i < len; i++) + { + if ([self characterAtIndex: i] == _c) return i; + } + + return -1; +} + - (NSString *) decodedSubject { NSString *decodedSubject; diff --git a/SoObjects/Mailer/SOGoDraftObject.m b/SoObjects/Mailer/SOGoDraftObject.m index 10f45b944..2b6e5d3ef 100644 --- a/SoObjects/Mailer/SOGoDraftObject.m +++ b/SoObjects/Mailer/SOGoDraftObject.m @@ -60,6 +60,7 @@ #import #import "NSData+Mail.h" +#import "NSString+Mail.h" #import "SOGoMailAccount.h" #import "SOGoMailFolder.h" #import "SOGoMailObject.h" @@ -237,7 +238,7 @@ static BOOL showTextAttachmentsInline = NO; if ([self _ensureDraftFolderPath]) { - infos = [NSMutableDictionary new]; + infos = [NSMutableDictionary new]; [infos setObject: headers forKey: @"headers"]; if (text) [infos setObject: text forKey: @"text"]; @@ -1134,6 +1135,60 @@ static BOOL showTextAttachmentsInline = NO; return NO; } +- (NSArray *) _quoteSpecials: (NSArray *) _array +{ + NSMutableArray *a; + NSString *s1, *s2; + int i, j, len; + + a = [NSMutableArray array]; + + // We want to correctly send mails to recipients such as : + // foo.bar + // foo (bar) + // bar, foo + for (i = 0; i < [_array count]; i++) + { + s1 = [_array objectAtIndex: i]; + + if ([s1 indexOf: '('] >= 0 || [s1 indexOf: ')'] >= 0 + || [s1 indexOf: '<'] >= 0 || [s1 indexOf: '>'] >= 0 + || [s1 indexOf: '@'] >= 0 || [s1 indexOf: ','] >= 0 + || [s1 indexOf: ';'] >= 0 || [s1 indexOf: ':'] >= 0 + || [s1 indexOf: '\\'] >= 0 || [s1 indexOf: '"'] >= 0 + || [s1 indexOf: '.'] >= 0 + || [s1 indexOf: '['] >= 0 || [s1 indexOf: ']'] >= 0) + { + // We search for the first instance of < from the end + // and we quote what was before if we need to + len = [s1 length]; + j = -1; + while (len--) + if ([s1 characterAtIndex: len] == '<') + { + j = len; + break; + } + + if (j > 0) + { + s2 = [s1 substringToIndex: j-1]; + s2 = [NSString stringWithFormat: @"\"%@\" %@", s2, [s1 substringFromIndex: j]]; + } + else + { + s2 = [NSString stringWithFormat: @"\"%@\"", s1]; + } + + [a addObject: s2]; + } + else + [a addObject: s1]; + } + + return a; +} + - (NGMutableHashMap *) mimeHeaderMapWithHeaders: (NSDictionary *) _headers { NGMutableHashMap *map; @@ -1144,13 +1199,13 @@ static BOOL showTextAttachmentsInline = NO; map = [[[NGMutableHashMap alloc] initWithCapacity:16] autorelease]; /* add recipients */ - + if ((emails = [headers objectForKey: @"to"]) != nil) - [map setObjects: emails forKey: @"to"]; + [map setObjects: [self _quoteSpecials: emails] forKey: @"to"]; if ((emails = [headers objectForKey: @"cc"]) != nil) - [map setObjects:emails forKey: @"cc"]; + [map setObjects: [self _quoteSpecials: emails] forKey: @"cc"]; if ((emails = [headers objectForKey: @"bcc"]) != nil) - [map setObjects:emails forKey: @"bcc"]; + [map setObjects: [self _quoteSpecials: emails] forKey: @"bcc"]; /* add senders */