fix(mail): pick proper "from" address when replying/forwarding

Fixes #5056
This commit is contained in:
Francis Lachapelle
2020-07-06 12:43:55 -04:00
parent 089935297c
commit c99170b9bc
7 changed files with 173 additions and 78 deletions
+67 -13
View File
@@ -50,6 +50,7 @@
#import <SOGo/NSArray+Utilities.h>
#import <SOGo/NSCalendarDate+SOGo.h>
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoBuild.h>
#import <SOGo/SOGoDomainDefaults.h>
@@ -661,6 +662,53 @@ static NSString *userAgent = nil;
}
}
//
//
//
- (NSString *) _emailFromIdentity: (NSDictionary *) identity
{
NSString *fullName, *format;
fullName = [identity objectForKey: @"fullName"];
if ([fullName length])
format = @"%{fullName} <%{email}>";
else
format = @"%{email}";
return [identity keysWithFormat: format];
}
- (void) _fillInFromAddress: (NSMutableDictionary *) _info
fromSentMailbox: (BOOL) _fromSentMailbox
envelope: (NGImap4Envelope *) _envelope
{
NSDictionary *identity;
NSMutableArray *addrs;
NSString *email;
int i;
/* Pick the first email matching one of the account's identities */
addrs = [NSMutableArray array];
if (_fromSentMailbox)
[self _addRecipients: [_envelope from] toArray: addrs];
else
[self _addRecipients: [_envelope to] toArray: addrs];
if ([addrs count])
{
identity = nil;
for (i = 0; !identity && i < [addrs count]; i++)
{
email = [addrs objectAtIndex: i];
identity = [[[self container] mailAccountFolder] identityForEmail: email];
}
if (identity)
{
[_info setObject: [self _emailFromIdentity: identity] forKey: @"from"];
}
}
}
//
//
//
@@ -705,11 +753,9 @@ static NSString *userAgent = nil;
int i;
identities = [[[self container] mailAccountFolder] identities];
for (i = 0; i < [identities count]; i++)
{
email = [[identities objectAtIndex: i] objectForKey: @"email"];
if (email)
[allRecipients addObject: email];
}
@@ -726,7 +772,7 @@ static NSString *userAgent = nil;
else
[addrs setArray: [_envelope from]];
[self _purgeRecipients: allRecipients fromAddresses: addrs];
[self _purgeRecipients: allRecipients fromAddresses: addrs]; // addrs contain the recipient addresses without the any of the sender's addresses
[self _addEMailsOfAddresses: addrs toArray: to];
[self _addRecipients: addrs toArray: allRecipients];
[_info setObject: to forKey: @"to"];
@@ -741,6 +787,11 @@ static NSString *userAgent = nil;
[self _addEMailsOfAddresses: [_envelope from] toArray: to];
}
/* Pick the first email matching one of the account's identities */
[self _fillInFromAddress: _info
fromSentMailbox: _fromSentMailbox
envelope: _envelope];
/* If we have no To but we have Cc recipients, let's move the Cc
to the To bucket... */
if ([[_info objectForKey: @"to"] count] == 0 && [_info objectForKey: @"cc"])
@@ -954,7 +1005,6 @@ static NSString *userAgent = nil;
{
BOOL fromSentMailbox;
NSString *msgID;
NSMutableArray *addresses;
NSMutableDictionary *info;
NGImap4Envelope *sourceEnvelope;
SOGoUserDefaults *ud;
@@ -974,11 +1024,6 @@ static NSString *userAgent = nil;
if ([msgID length] > 0)
[self setInReplyTo: msgID];
addresses = [NSMutableArray array];
[self _addEMailsOfAddresses: [sourceEnvelope to] toArray: addresses];
if ([addresses count])
[info setObject: [addresses objectAtIndex: 0] forKey: @"from"];
ud = [[context activeUser] userDefaults];
[self setText: [sourceMail contentForReply]];
@@ -994,19 +1039,28 @@ static NSString *userAgent = nil;
- (void) fetchMailForForwarding: (SOGoMailObject *) sourceMail
{
NSDictionary *info, *attachment;
BOOL fromSentMailbox;
NGImap4Envelope *sourceEnvelope;
NSDictionary *attachment;
NSMutableDictionary *info;
NSString *signature, *nl, *space;
SOGoUserDefaults *ud;
fromSentMailbox = [[sourceMail container] isKindOfClass: [SOGoSentFolder class]];
[sourceMail fetchCoreInfos];
sourceEnvelope = [sourceMail envelope];
info = [NSMutableDictionary dictionaryWithCapacity: 2];
if ([sourceMail subjectForForward])
{
info = [NSDictionary dictionaryWithObject: [sourceMail subjectForForward]
forKey: @"subject"];
[self setHeaders: info];
[info setObject: [sourceMail subjectForForward] forKey: @"subject"];
}
[self _fillInFromAddress: info
fromSentMailbox: fromSentMailbox
envelope: sourceEnvelope];
[self setHeaders: info];
[self setSourceURL: [sourceMail imap4URLString]];
[self setSourceFlag: @"$Forwarded"];
[self setSourceIMAP4ID: [[sourceMail nameInContainer] intValue]];
+1
View File
@@ -88,6 +88,7 @@ typedef enum {
- (NSArray *) identities;
- (NSDictionary *) defaultIdentity;
- (NSDictionary *) identityForEmail: (NSString *) email;
- (NSString *) signature;
- (NSString *) encryption;
+24
View File
@@ -694,6 +694,30 @@ static NSString *inboxFolderName = @"INBOX";
return defaultIdentity; // can be nil
}
- (NSDictionary *) identityForEmail: (NSString *) email
{
NSDictionary *identity, *currentIdentity;
NSString *currentEmail;
unsigned int count, max;
identity = nil;
[self identities];
max = [identities count];
for (count = 0; count < max; count++)
{
currentIdentity = [identities objectAtIndex: count];
currentEmail = [currentIdentity objectForKey: @"email"];
if ([currentEmail caseInsensitiveCompare: email] == NSOrderedSame)
{
identity = currentIdentity;
break;
}
}
return identity; // can be nil
}
- (NSString *) signature
{
NSDictionary *identity;
+38 -11
View File
@@ -1,6 +1,6 @@
/* SOGoMailForward.m - this file is part of SOGo
*
* Copyright (C) 2007-2017 Inverse inc.
* Copyright (C) 2007-2020 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
#import <Foundation/NSDictionary.h>
#import <NGImap4/NGImap4EnvelopeAddress.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGExtensions/NSString+misc.h>
@@ -30,6 +31,7 @@
#import "SOGoMailAccount.h"
#import "SOGoMailObject+Draft.h"
#import "SOGoMailForward.h"
#import "SOGoSentFolder.h"
@implementation SOGoMailForward
@@ -234,18 +236,43 @@
- (NSString *) signature
{
NSString *signature, *mailSignature, *nl, *space;
signature = [[sourceMail mailAccountFolder] signature];
BOOL fromSentMailbox;
NGImap4EnvelopeAddress *address;
NSArray *addresses;
NSDictionary *identity;
NSString *email, *signature, *mailSignature, *nl, *space;
int count, max;
if ([signature length])
{
nl = (htmlComposition ? @"<br />" : @"\n");
space = (htmlComposition ? @"&nbsp;" : @" ");
mailSignature = [NSString stringWithFormat: @"--%@%@%@", space, nl, signature];
}
fromSentMailbox = [[sourceMail container] isKindOfClass: [SOGoSentFolder class]];
if (fromSentMailbox)
addresses = [sourceMail fromEnvelopeAddresses];
else
mailSignature = @"";
addresses = [sourceMail toEnvelopeAddresses];
identity = nil;
mailSignature = @"";
max = [addresses count];
if (max)
{
// Pick the first email matching one of the account's identities
for (count = 0; !identity && count < max; count++)
{
address = [addresses objectAtIndex: count];
email = [address baseEMail];
identity = [[sourceMail mailAccountFolder] identityForEmail: email];
}
}
if (identity)
{
signature = [identity objectForKey: @"signature"];
if ([signature length])
{
nl = (htmlComposition ? @"<br />" : @"\n");
space = (htmlComposition ? @"&nbsp;" : @" ");
mailSignature = [NSString stringWithFormat: @"%@--%@%@%@", nl, space, nl, signature];
}
}
return mailSignature;
}