(fix) check if EAS identity is ok and use it (fixes #3698)

This commit is contained in:
Ludovic Marcotte 2016-06-23 11:06:44 -04:00
parent b09bdab3ca
commit c0bfb42c12

View file

@ -2692,6 +2692,38 @@ void handle_eas_terminate(int signum)
return nil; return nil;
} }
- (BOOL) _isEMailValid: (NSString *) email
{
NSArray *identities;
int i;
identities = [[context activeUser] allIdentities];
for (i = 0; i < [identities count]; i++)
{
if ([email isEqualToString: [[identities objectAtIndex: i] objectForKey: @"email"]])
return YES;
}
return NO;
}
- (NSString *) _fullNameForEMail: (NSString *) email
{
NSArray *identities;
int i;
identities = [[context activeUser] allIdentities];
for (i = 0; i < [identities count]; i++)
{
if ([email isEqualToString: [[identities objectAtIndex: i] objectForKey: @"email"]])
return [[identities objectAtIndex: i] objectForKey: @"fullName"];
}
return nil;
}
// //
// //
// //
@ -2705,10 +2737,12 @@ void handle_eas_terminate(int signum)
NSData *new_from_header; NSData *new_from_header;
NSDictionary *identity; NSDictionary *identity;
NSString *fullName, *email; NSString *fullName, *email;
NSArray *from;
const char *bytes; const char *bytes;
int i, e, len; int i, e, len;
BOOL found_header; BOOL found_header;
email = nil;
// We get the mail's data // We get the mail's data
data = [NSMutableData dataWithData: [[[[(id)[theDocumentElement getElementsByTagName: @"MIME"] lastObject] textValue] stringByDecodingBase64] dataUsingEncoding: NSUTF8StringEncoding]]; data = [NSMutableData dataWithData: [[[[(id)[theDocumentElement getElementsByTagName: @"MIME"] lastObject] textValue] stringByDecodingBase64] dataUsingEncoding: NSUTF8StringEncoding]];
@ -2718,10 +2752,25 @@ void handle_eas_terminate(int signum)
message = [parser parsePartFromData: data]; message = [parser parsePartFromData: data];
RELEASE(parser); RELEASE(parser);
identity = [[context activeUser] primaryIdentity]; from = [message headersForKey: @"from"];
if (![from count] || ![self _isEMailValid: [[from objectAtIndex: 0] pureEMailAddress]] ||
[[[from objectAtIndex: 0] pureEMailAddress] isEqualToString: [from objectAtIndex: 0]] ||
[[NSString stringWithFormat: @"<%@>", [[from objectAtIndex: 0] pureEMailAddress]] isEqualToString: [from objectAtIndex: 0]])
{
if ([from count] && [self _isEMailValid: [[from objectAtIndex: 0] pureEMailAddress]])
{
// We have a valid email address, lets fill in the fullname.
email = [[from objectAtIndex: 0] pureEMailAddress];
fullName = [self _fullNameForEMail: email];
}
else
{
// Fallback to primary identity.
identity = [[context activeUser] primaryIdentity];
fullName = [identity objectForKey: @"fullName"]; fullName = [identity objectForKey: @"fullName"];
email = [identity objectForKey: @"email"]; email = [identity objectForKey: @"email"];
}
if ([fullName length]) if ([fullName length])
new_from_header = [[NSString stringWithFormat: @"From: %@ <%@>\r\n", [fullName asQPSubjectString: @"utf-8"], email] dataUsingEncoding: NSUTF8StringEncoding]; new_from_header = [[NSString stringWithFormat: @"From: %@ <%@>\r\n", [fullName asQPSubjectString: @"utf-8"], email] dataUsingEncoding: NSUTF8StringEncoding];
@ -2794,6 +2843,7 @@ void handle_eas_terminate(int signum)
withBytes: [new_from_header bytes] withBytes: [new_from_header bytes]
length: [new_from_header length]]; length: [new_from_header length]];
} }
}
error = [self _sendMail: data error = [self _sendMail: data
recipients: [message allRecipients] recipients: [message allRecipients]
@ -2931,7 +2981,7 @@ void handle_eas_terminate(int signum)
NSMutableArray *attachments, *references; NSMutableArray *attachments, *references;
id body, bodyFromSmartForward, htmlPart, textPart; id body, bodyFromSmartForward, htmlPart, textPart;
NSString *fullName, *email, *charset, *s; NSString *fullName, *email, *charset, *s, *from;
NSDictionary *identity; NSDictionary *identity;
int a; int a;
@ -2957,14 +3007,32 @@ void handle_eas_terminate(int signum)
map = [NGHashMap hashMapWithDictionary: [messageFromSmartForward headers]]; map = [NGHashMap hashMapWithDictionary: [messageFromSmartForward headers]];
[map setObject: @"multipart/mixed" forKey: @"content-type"]; [map setObject: @"multipart/mixed" forKey: @"content-type"];
from = [map objectForKey: @"from"];
if (![from length] || ![self _isEMailValid: [from pureEMailAddress]] ||
[[from pureEMailAddress] isEqualToString: from] ||
[[NSString stringWithFormat: @"<%@>", [from pureEMailAddress]] isEqualToString: from])
{
if ([from length] && [self _isEMailValid: [from pureEMailAddress]])
{
// We have a valid email address, lets fill in the fullname.
email = [from pureEMailAddress];
fullName = [self _fullNameForEMail: email];
}
else
{
// Fallback to primary identity.
identity = [[context activeUser] primaryIdentity]; identity = [[context activeUser] primaryIdentity];
fullName = [identity objectForKey: @"fullName"]; fullName = [identity objectForKey: @"fullName"];
email = [identity objectForKey: @"email"]; email = [identity objectForKey: @"email"];
}
if ([fullName length]) if ([fullName length])
[map setObject: [NSString stringWithFormat: @"%@ <%@>", fullName, email] forKey: @"from"]; [map setObject: [NSString stringWithFormat: @"%@ <%@>", fullName, email] forKey: @"from"];
else else
[map setObject: email forKey: @"from"]; [map setObject: email forKey: @"from"];
}
if ([mailObject messageId]) if ([mailObject messageId])
{ {