diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-03-02 12:07:18 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-03-02 12:07:18 +0100 |
commit | f28ec43dca5b2915deb69d54fb942ddf1303f48c (patch) | |
tree | 7122808144542c4f3e70f5e9b52925e5016d36f7 /framework/src/domain/mime/mailtemplates.cpp | |
parent | 03e8aabf1754f5061f207d2c9c082ba6199db0e5 (diff) | |
download | kube-f28ec43dca5b2915deb69d54fb942ddf1303f48c.tar.gz kube-f28ec43dca5b2915deb69d54fb942ddf1303f48c.zip |
Support encrypted mails forwarding
Summary:
Some notes:
- What we do is: if the mail is encrypted, decrypt it and copy its content into a new message (with plaintext, html and attachments, if any), and use this message as attachment for forwarding
- The `isEncrypted` function from KMime doesn't seem to detect every kind of encrypted mails. AFAIK this structure is not detected:
- `multipart/mixed`
- `text/plain`
- `application/pgp-encrypted` (attachement, named "ATT00001")
- `application/octet-stream` (attachment named "encrypted.asc")
Reviewers: cmollekopf
Tags: PHID-PROJ-6npnfcmppynqynn7slmv
Maniphest Tasks: T8112, T7024
Differential Revision: https://phabricator.kde.org/D10966
Diffstat (limited to 'framework/src/domain/mime/mailtemplates.cpp')
-rw-r--r-- | framework/src/domain/mime/mailtemplates.cpp | 83 |
1 files changed, 77 insertions, 6 deletions
diff --git a/framework/src/domain/mime/mailtemplates.cpp b/framework/src/domain/mime/mailtemplates.cpp index 8e644b34..513f0353 100644 --- a/framework/src/domain/mime/mailtemplates.cpp +++ b/framework/src/domain/mime/mailtemplates.cpp | |||
@@ -53,6 +53,18 @@ static bool operator==(const KMime::Types::Mailbox &left, const KMime::Types::Ma | |||
53 | return (left.addrSpec().asString() == right.addrSpec().asString()); | 53 | return (left.addrSpec().asString() == right.addrSpec().asString()); |
54 | } | 54 | } |
55 | } | 55 | } |
56 | |||
57 | Message* contentToMessage(Content* content) { | ||
58 | content->assemble(); | ||
59 | const auto encoded = content->encodedContent(); | ||
60 | |||
61 | auto message = new Message(); | ||
62 | message->setContent(encoded); | ||
63 | message->parse(); | ||
64 | |||
65 | return message; | ||
66 | } | ||
67 | |||
56 | } | 68 | } |
57 | 69 | ||
58 | static KMime::Types::Mailbox::List stripMyAddressesFromAddressList(const KMime::Types::Mailbox::List &list, const KMime::Types::AddrSpecList me) | 70 | static KMime::Types::Mailbox::List stripMyAddressesFromAddressList(const KMime::Types::Mailbox::List &list, const KMime::Types::AddrSpecList me) |
@@ -223,6 +235,19 @@ KMime::Content *createMultipartAlternativeContent(const QString &plainBody, cons | |||
223 | return multipartAlternative; | 235 | return multipartAlternative; |
224 | } | 236 | } |
225 | 237 | ||
238 | KMime::Message *createMultipartMixedContent(QVector<KMime::Content *> contents) | ||
239 | { | ||
240 | KMime::Message *multiPartMixed = new KMime::Message(); | ||
241 | multiPartMixed->contentType()->setMimeType("multipart/mixed"); | ||
242 | multiPartMixed->contentType()->setBoundary(KMime::multiPartBoundary()); | ||
243 | |||
244 | for (const auto &content : contents) { | ||
245 | multiPartMixed->addContent(content); | ||
246 | } | ||
247 | |||
248 | return multiPartMixed; | ||
249 | } | ||
250 | |||
226 | void addProcessedBodyToMessage(const KMime::Message::Ptr &msg, const QString &plainBody, const QString &htmlBody, bool forward) | 251 | void addProcessedBodyToMessage(const KMime::Message::Ptr &msg, const QString &plainBody, const QString &htmlBody, bool forward) |
227 | { | 252 | { |
228 | //FIXME | 253 | //FIXME |
@@ -864,27 +889,73 @@ void MailTemplates::reply(const KMime::Message::Ptr &origMsg, const std::functio | |||
864 | }); | 889 | }); |
865 | } | 890 | } |
866 | 891 | ||
867 | void MailTemplates::forward(const KMime::Message::Ptr &origMsg, const std::function<void(const KMime::Message::Ptr &result)> &callback) | 892 | void MailTemplates::forward(const KMime::Message::Ptr &origMsg, |
893 | const std::function<void(const KMime::Message::Ptr &result)> &callback) | ||
868 | { | 894 | { |
869 | KMime::Message::Ptr wrapperMsg(new KMime::Message); | 895 | KMime::Message::Ptr wrapperMsg(new KMime::Message); |
870 | 896 | ||
871 | wrapperMsg->to()->clear(); | 897 | wrapperMsg->to()->clear(); |
872 | wrapperMsg->cc()->clear(); | 898 | wrapperMsg->cc()->clear(); |
873 | 899 | ||
874 | wrapperMsg->subject()->fromUnicodeString(forwardSubject(origMsg->subject()->asUnicodeString()), "utf-8"); | 900 | // Decrypt the original message, it will be encrypted again in the composer |
901 | // for the right recipient | ||
902 | KMime::Message::Ptr forwardedMessage; | ||
903 | if (isEncrypted(origMsg.data())) { | ||
904 | qDebug() << "Original message was encrypted, decrypting it"; | ||
905 | MimeTreeParser::ObjectTreeParser otp; | ||
906 | otp.parseObjectTree(origMsg.data()); | ||
907 | otp.decryptParts(); | ||
875 | 908 | ||
876 | const QByteArray refStr = getRefStr(origMsg); | 909 | auto htmlContent = otp.htmlContent(); |
910 | KMime::Content *recreatedMsg = | ||
911 | htmlContent.isEmpty() ? createPlainPartContent(otp.plainTextContent()) : | ||
912 | createMultipartAlternativeContent(otp.plainTextContent(), htmlContent); | ||
913 | |||
914 | if (hasAttachment(origMsg.data())) { | ||
915 | QVector<KMime::Content *> contents = {recreatedMsg}; | ||
916 | contents.append(origMsg->attachments()); | ||
917 | |||
918 | auto msg = createMultipartMixedContent(contents); | ||
919 | |||
920 | forwardedMessage.reset(KMime::contentToMessage(msg)); | ||
921 | } else { | ||
922 | forwardedMessage.reset(KMime::contentToMessage(recreatedMsg)); | ||
923 | } | ||
924 | |||
925 | forwardedMessage->subject()->from7BitString(origMsg->subject()->as7BitString()); | ||
926 | |||
927 | for (const auto &addr : origMsg->to()->mailboxes()) { | ||
928 | forwardedMessage->to()->addAddress(addr); | ||
929 | } | ||
930 | |||
931 | for (const auto &addr : origMsg->cc()->mailboxes()) { | ||
932 | forwardedMessage->cc()->addAddress(addr); | ||
933 | } | ||
934 | |||
935 | for (const auto &addr : origMsg->bcc()->mailboxes()) { | ||
936 | forwardedMessage->bcc()->addAddress(addr); | ||
937 | } | ||
938 | |||
939 | } else { | ||
940 | qDebug() << "Original message was not encrypted, using it as-is"; | ||
941 | forwardedMessage = origMsg; | ||
942 | } | ||
943 | |||
944 | wrapperMsg->subject()->fromUnicodeString( | ||
945 | forwardSubject(forwardedMessage->subject()->asUnicodeString()), "utf-8"); | ||
946 | |||
947 | const QByteArray refStr = getRefStr(forwardedMessage); | ||
877 | if (!refStr.isEmpty()) { | 948 | if (!refStr.isEmpty()) { |
878 | wrapperMsg->references()->fromUnicodeString(QString::fromLocal8Bit(refStr), "utf-8"); | 949 | wrapperMsg->references()->fromUnicodeString(QString::fromLocal8Bit(refStr), "utf-8"); |
879 | } | 950 | } |
880 | 951 | ||
881 | KMime::Content* fwdAttachment = new KMime::Content; | 952 | KMime::Content *fwdAttachment = new KMime::Content; |
882 | 953 | ||
883 | fwdAttachment->contentDisposition()->setDisposition(KMime::Headers::CDinline); | 954 | fwdAttachment->contentDisposition()->setDisposition(KMime::Headers::CDinline); |
884 | fwdAttachment->contentType()->setMimeType("message/rfc822"); | 955 | fwdAttachment->contentType()->setMimeType("message/rfc822"); |
885 | fwdAttachment->contentDisposition()->setFilename(origMsg->subject()->asUnicodeString() + ".eml"); | 956 | fwdAttachment->contentDisposition()->setFilename(forwardedMessage->subject()->asUnicodeString() + ".eml"); |
886 | // The mail was parsed in loadMessage before, so no need to assemble it | 957 | // The mail was parsed in loadMessage before, so no need to assemble it |
887 | fwdAttachment->setBody(origMsg->encodedContent()); | 958 | fwdAttachment->setBody(forwardedMessage->encodedContent()); |
888 | 959 | ||
889 | wrapperMsg->addContent(fwdAttachment); | 960 | wrapperMsg->addContent(fwdAttachment); |
890 | wrapperMsg->assemble(); | 961 | wrapperMsg->assemble(); |