(feat) added S/MIME support for content fetch when replying/forwarding mails

pull/239/head
Ludovic Marcotte 2017-12-23 06:35:38 -05:00
parent e27e558cb2
commit 71dd9d7d91
2 changed files with 99 additions and 16 deletions

View File

@ -1,8 +1,6 @@
/* SOGoMailObject+Draft.h - this file is part of SOGo /* SOGoMailObject+Draft.h - this file is part of SOGo
* *
* Copyright (C) 2007 Inverse inc. * Copyright (C) 2007-2017 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* *
* This file is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,8 +1,6 @@
/* SOGoMailObject+Draft.m - this file is part of SOGo /* SOGoMailObject+Draft.m - this file is part of SOGo
* *
* Copyright (C) 2007-2008 Inverse inc. * Copyright (C) 2007-2017 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
* *
* This file is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -27,16 +25,23 @@
#import <NGObjWeb/WOContext+SoObjects.h> #import <NGObjWeb/WOContext+SoObjects.h>
#import <NGExtensions/NSString+misc.h> #import <NGExtensions/NSString+misc.h>
#import <NGExtensions/NSObject+Logs.h> #import <NGExtensions/NSObject+Logs.h>
#import <NGMail/NGMimeMessage.h>
#import <NGMime/NGMimeBodyPart.h>
#import <NGMime/NGMimeMultipartBody.h>
#import <NGMime/NGMimeType.h>
#import <SoObjects/SOGo/NSArray+Utilities.h> #import <SoObjects/SOGo/NSArray+Utilities.h>
#import <SoObjects/SOGo/SOGoUser.h> #import <SoObjects/SOGo/SOGoUser.h>
#import <SoObjects/SOGo/SOGoUserDefaults.h> #import <SoObjects/SOGo/SOGoUserDefaults.h>
#import "NSData+Mail.h"
#import "NSData+SMIME.h"
#import "NSString+Mail.h" #import "NSString+Mail.h"
#import "SOGoMailAccount.h"
#import "SOGoMailObject+Draft.h" #import "SOGoMailObject+Draft.h"
#import "SOGoMailReply.h" #import "SOGoMailReply.h"
#define maxFilenameLength 64 #define MAX_FILENAME_LENGTH 64
// //
// //
@ -165,21 +170,101 @@
return [content autorelease]; return [content autorelease];
} }
//
//
//
- (NSString *) _preferredContentFromPart: (id) thePart
favorHTML: (BOOL) favorHTML
{
NSString *type, *subtype;
id body;
if ([thePart isKindOfClass: [NGMimeBodyPart class]])
{
type = [[thePart contentType] type];
subtype = [[thePart contentType] subType];
body = [thePart body];
if ([type isEqualToString: @"text"])
{
if ([subtype isEqualToString: @"html"] && favorHTML)
return [[body stringByEscapingHTMLString] stringByConvertingCRLNToHTML];
else if ([subtype isEqualToString: @"html"] && !favorHTML)
return [body htmlToText];
else if ([subtype isEqualToString: @"plain"])
return body;
}
}
else if ([thePart isKindOfClass: [NGMimeMultipartBody class]])
{
NSArray *parts;
int i;
parts = [thePart parts];
for (i = 0; i < [parts count]; i++)
{
type = [[[parts objectAtIndex: i] contentType] type];
if ([type isEqualToString: @"text"] || [type isEqualToString: @"multipart"])
return [self _preferredContentFromPart: [parts objectAtIndex: i]
favorHTML: favorHTML];
}
}
return nil;
}
//
//
//
- (NSString *) _contentForEditingFromEncryptedMail
{
NSData *certificate;
certificate = [[self mailAccountFolder] certificate];
if (certificate)
{
SOGoUserDefaults *ud;
NGMimeMessage *m;
m = [[self content] messageFromEncryptedDataAndCertificate: certificate];
ud = [[context activeUser] userDefaults];
return [self _preferredContentFromPart: [m body]
favorHTML: [[ud mailComposeMessageType] isEqualToString: @"html"]];
}
return nil;
}
// //
// //
// //
- (NSString *) contentForEditing - (NSString *) contentForEditing
{ {
NSMutableArray *keys; NSMutableArray *keys;
NSArray *acceptedTypes; NSString *output;
acceptedTypes = [NSArray arrayWithObjects: @"text/plain", @"text/html", nil]; output = nil;
keys = [NSMutableArray array];
[self addRequiredKeysOfStructure: [self bodyStructure]
path: @"" toArray: keys acceptedTypes: acceptedTypes
withPeek: NO];
return [self _contentForEditingFromKeys: keys]; if ([self isEncrypted])
output = [self _contentForEditingFromEncryptedMail];
// If not encrypted or if decryption failed, we fallback
// to the normal content fetching code.
if (!output)
{
keys = [NSMutableArray array];
[self addRequiredKeysOfStructure: [self bodyStructure]
path: @""
toArray: keys
acceptedTypes: [NSArray arrayWithObjects: @"text/plain", @"text/html", nil]
withPeek: NO];
output = [self _contentForEditingFromKeys: keys];
}
return output;
} }
// //
@ -224,8 +309,8 @@
length = [subject length]; length = [subject length];
} }
if (length > maxFilenameLength) if (length > MAX_FILENAME_LENGTH)
length = maxFilenameLength; length = MAX_FILENAME_LENGTH;
newSubject = [NSMutableString newSubject = [NSMutableString
stringWithString: [subject substringToIndex: length]]; stringWithString: [subject substringToIndex: length]];
count = 0; count = 0;