diff --git a/mimetreeparser/autotests/data/headeronly/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html b/mimetreeparser/autotests/data/headeronly/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html new file mode 100644 index 00000000..166812fe --- /dev/null +++ b/mimetreeparser/autotests/data/headeronly/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html @@ -0,0 +1,10 @@ + + + + +
+ +
+
+ + diff --git a/mimetreeparser/autotests/data/hidden/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html b/mimetreeparser/autotests/data/hidden/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html new file mode 100644 index 00000000..166812fe --- /dev/null +++ b/mimetreeparser/autotests/data/hidden/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html @@ -0,0 +1,10 @@ + + + + +
+ +
+
+ + diff --git a/mimetreeparser/autotests/data/inlined/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html b/mimetreeparser/autotests/data/inlined/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html new file mode 100644 index 00000000..6d629b31 --- /dev/null +++ b/mimetreeparser/autotests/data/inlined/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html @@ -0,0 +1,17 @@ + + + + +
+ + +
+ + diff --git a/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox new file mode 100644 index 00000000..33d25f0e --- /dev/null +++ b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox @@ -0,0 +1,26 @@ +From unknown@example.org Tue Oct 03 10:05:19 2017 +Return-Path: +To: konqi@example.org +Subject: only an encrypted attachment +X-PHP-Originating-Script: 1008:rcube.php +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="=_XXXXXXXXXXXXXX" +Date: Tue, 03 Oct 2017 12:13:14 +0000 +From: unknown@example.org +Message-ID: +X-Sender: unknown@example.org +User-Agent: Roundcube Webmail/1.1.9 + + +--=_XXXXXXXXXXXXXX +Content-Transfer-Encoding: 7bit +Content-Type: text/PGP; + name=encrypted.txt.pgp +Content-Disposition: attachment; + filename=encrypted.txt.pgp; + size=1402 + +nothing is encrypted here. +--=_XXXXXXXXXXXXXX-- + diff --git a/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html new file mode 100644 index 00000000..cf3d9572 --- /dev/null +++ b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.html @@ -0,0 +1,19 @@ + + + + +
+ + + + diff --git a/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.tree b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.tree new file mode 100644 index 00000000..79a20c8d --- /dev/null +++ b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment-non-encrypted.mbox.tree @@ -0,0 +1,4 @@ + * MimeTreeParser::MessagePartList + * MimeTreeParser::MimeMessagePart + * MimeTreeParser::AttachmentMessagePart + * MimeTreeParser::MessagePart diff --git a/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox new file mode 100644 index 00000000..86c620b4 --- /dev/null +++ b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox @@ -0,0 +1,32 @@ +From unknown@example.org Tue Oct 03 10:05:19 2017 +Return-Path: +To: konqi@example.org +Subject: only an encrypted attachment +X-PHP-Originating-Script: 1008:rcube.php +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="=_XXXXXXXXXXXXXX" +Date: Tue, 03 Oct 2017 12:13:14 +0000 +From: unknown@example.org +Message-ID: +X-Sender: unknown@example.org +User-Agent: Roundcube Webmail/1.1.9 + + +--=_XXXXXXXXXXXXXX +Content-Transfer-Encoding: base64 +Content-Type: text/PGP; + name=Brotzeiten.txt.pgp +Content-Disposition: attachment; + filename=encrypted.txt.pgp; + size=1402 + +hQEMAwzOQ1qnzNo7AQf/Z3qYebORMk5Z4R+Pmb6gBLA20G3yLRFyd0z+ygcEp+rc1jYE1ZqK1pFz +Z6WLgPdMcyrKBLrDeDfsQRbKvimR7NaXLSuNkRHFtgqCmaFDyIUMcf9DobAoKvGvmBJhgMW7WPCA +RUDaiJyqBp29nCQuOIqAiT5fm5LyyUmhRxUb9/CnulcxzW85EiITm0wMos30j1dU++3KDp5R6gcc +9uUVbdCWdiF4j3ilE8flCWg9tARTRGinu4ENc9eIjcn1hCotFS+4ccWYIwf6ZkVDS4wNfU7dDg+9 +ZwNb+RkXuYXT/tFdir+ewV0Njaj0ZWTUrqZSk2ArmdGo9ou7xl/2mrK6YNJIAUKgVsQlWZ1Kx4PI +0k0+TsJBgRzVNpIBHpmu0MI4CE03XHUslFhzdrjOG2Ts26U3EJw+jjssu0W4MVzYc20u3BgRnzHn +/SFA +--=_XXXXXXXXXXXXXX-- + diff --git a/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox.html b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox.html new file mode 100644 index 00000000..db9feb2c --- /dev/null +++ b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox.html @@ -0,0 +1,37 @@ + + + + + + + diff --git a/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox.tree b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox.tree new file mode 100644 index 00000000..7c55bc9f --- /dev/null +++ b/mimetreeparser/autotests/data/openpgp-encrypted-no-text-attachment.mbox.tree @@ -0,0 +1,5 @@ + * MimeTreeParser::MessagePartList + * MimeTreeParser::MimeMessagePart + * MimeTreeParser::EncryptedMessagePart + * MimeTreeParser::TextMessagePart + * MimeTreeParser::MessagePart diff --git a/mimetreeparser/src/CMakeLists.txt b/mimetreeparser/src/CMakeLists.txt index 45f300ae..52ff33a5 100644 --- a/mimetreeparser/src/CMakeLists.txt +++ b/mimetreeparser/src/CMakeLists.txt @@ -1,175 +1,176 @@ add_definitions( -DQT_NO_CAST_FROM_ASCII ) add_definitions( -DQT_NO_CAST_TO_ASCII ) add_definitions(-DTRANSLATION_DOMAIN=\"libmimetreeparser\") # target_include_directories does not handle empty include paths include_directories(${GPGME_INCLUDES}) set(libmimetreeparser_main_SRCS bodyformatter/applicationpgpencrypted.cpp bodyformatter/applicationpkcs7mime.cpp + bodyformatter/encrypted.cpp bodyformatter/mailman.cpp bodyformatter/multipartalternative.cpp bodyformatter/multipartencrypted.cpp bodyformatter/multipartmixed.cpp bodyformatter/multipartsigned.cpp bodyformatter/textplain.cpp bodyformatter/texthtml.cpp bodyformatter/utils.cpp interfaces/bodypartformatter.cpp interfaces/objecttreesource.cpp interfaces/bodypart.cpp interfaces/htmlwriter.cpp job/qgpgmejobexecutor.cpp utils/util.cpp attachmentstrategy.cpp bodypartformatter.cpp bodypartformatterfactory.cpp cryptohelper.cpp nodehelper.cpp objecttreeparser.cpp messagepart.cpp partnodebodypart.cpp memento/cryptobodypartmemento.cpp memento/decryptverifybodypartmemento.cpp memento/verifydetachedbodypartmemento.cpp memento/verifyopaquebodypartmemento.cpp ) set(libmimetreeparser_extra_SRCS #HTML Writer htmlwriter/bufferedhtmlwriter.cpp htmlwriter/filehtmlwriter.cpp ) set(mimetreeparser_temporaryfile_SRCS temporaryfile/attachmenttemporaryfilesdirs.cpp ) ecm_generate_headers(MimeTreeParser_Camelcasemain_HEADERS HEADER_NAMES AttachmentStrategy BodyPartFormatterFactory Enums MessagePart NodeHelper ObjectTreeParser PartMetaData PartNodeBodyPart REQUIRED_HEADERS MimeTreeParser_main_HEADERS PREFIX MimeTreeParser ) ecm_generate_headers(MimeTreeParser_Camelcaseutils_HEADERS HEADER_NAMES Util REQUIRED_HEADERS MimeTreeParser_utils_HEADERS PREFIX MimeTreeParser RELATIVE utils ) ecm_generate_headers(MimeTreeParser_Camelcaseinterfaces_HEADERS HEADER_NAMES BodyPartFormatter BodyPart HtmlWriter ObjectTreeSource REQUIRED_HEADERS MimeTreeParser_interfaces_HEADERS PREFIX MimeTreeParser RELATIVE interfaces ) ecm_generate_headers(MimeTreeParser_Camelcasehtmlwriter_HEADERS HEADER_NAMES BufferedHtmlWriter FileHtmlWriter REQUIRED_HEADERS MimeTreeParser_htmlwriter_HEADERS PREFIX MimeTreeParser RELATIVE htmlwriter ) ecm_generate_headers(MimeTreeParser_Camelcasetemporaryfile_HEADERS HEADER_NAMES AttachmentTemporaryFilesDirs REQUIRED_HEADERS MimeTreeParser_temporaryfile_HEADERS PREFIX MimeTreeParser RELATIVE temporaryfile ) install(FILES ${MimeTreeParser_Camelcasehtmlwriter_HEADERS} ${MimeTreeParser_Camelcaseutils_HEADERS} ${MimeTreeParser_Camelcaseinterfaces_HEADERS} ${MimeTreeParser_Camelcasemain_HEADERS} ${MimeTreeParser_Camelcasetemporaryfile_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/MimeTreeParser COMPONENT Devel ) install(FILES ${MimeTreeParser_htmlwriter_HEADERS} ${MimeTreeParser_utils_HEADERS} ${MimeTreeParser_interfaces_HEADERS} ${MimeTreeParser_main_HEADERS} ${MimeTreeParser_temporaryfile_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/mimetreeparser_export.h ${CMAKE_CURRENT_BINARY_DIR}/mimetreeparser_debug.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/mimetreeparser COMPONENT Devel ) ecm_generate_pri_file(BASE_NAME MimeTreeParser LIB_NAME KF5MimeTreeParser FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/MimeTreeParser ) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR} ) set(libmimetreeparser_SRCS ${libmimetreeparser_main_SRCS} ${libmimetreeparser_extra_SRCS} ${mimetreeparser_temporaryfile_SRCS} ) ecm_qt_declare_logging_category(libmimetreeparser_SRCS HEADER mimetreeparser_debug.h IDENTIFIER MIMETREEPARSER_LOG CATEGORY_NAME org.kde.pim.mimetreeparser) add_library(KF5MimeTreeParser ${libmimetreeparser_SRCS} ) generate_export_header(KF5MimeTreeParser BASE_NAME mimetreeparser) add_library(KF5::MimeTreeParser ALIAS KF5MimeTreeParser) set(mimetreeparser_LINK_LIBRARIES ) target_link_libraries(KF5MimeTreeParser PRIVATE QGpgme KF5::Codecs KF5::I18n KF5::CoreAddons KF5::Mime Qt5::Gui ) install(TARGETS KF5MimeTreeParser EXPORT KF5MimeTreeParserTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS} ${LIBRARY_NAMELINK} ) set_target_properties(KF5MimeTreeParser PROPERTIES VERSION ${MIMETREEPARSER_VERSION_STRING} SOVERSION ${MIMETREEPARSER_SOVERSION} EXPORT_NAME MimeTreeParser ) target_include_directories(KF5MimeTreeParser INTERFACE "$") diff --git a/mimetreeparser/src/bodyformatter/encrypted.cpp b/mimetreeparser/src/bodyformatter/encrypted.cpp new file mode 100644 index 00000000..2452d4cf --- /dev/null +++ b/mimetreeparser/src/bodyformatter/encrypted.cpp @@ -0,0 +1,105 @@ +/* + Copyright (c) 2017 Sandro Knauß + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "encrypted.h" + +#include "utils.h" + +#include "objecttreeparser.h" +#include "messagepart.h" + +#include + +#include + +#include + +#include "mimetreeparser_debug.h" + +using namespace MimeTreeParser; + +const EncryptedBodyPartFormatter *EncryptedBodyPartFormatter::self; + +const Interface::BodyPartFormatter *EncryptedBodyPartFormatter::create() +{ + if (!self) { + self = new EncryptedBodyPartFormatter(); + } + return self; +} + +MessagePart::Ptr EncryptedBodyPartFormatter::process(Interface::BodyPart &part) const +{ + KMime::Content *node = part.content(); + + if (!node->contents().isEmpty()) { + Q_ASSERT(false); + return MessagePart::Ptr(); + } + + const QGpgME::Protocol *useThisCryptProto = nullptr; + + useThisCryptProto = QGpgME::openpgp(); + + //TODO: Load correct crypto Proto + + part.nodeHelper()->setEncryptionState(node, KMMsgFullyEncrypted); + + EncryptedMessagePart::Ptr mp(new EncryptedMessagePart(part.objectTreeParser(), + node->decodedText(), useThisCryptProto, + part.nodeHelper()->fromAsString(node), node)); + mp->setIsEncrypted(true); + mp->setDecryptMessage(part.source()->decryptMessage()); + PartMetaData *messagePart(mp->partMetaData()); + if (!part.source()->decryptMessage()) { + part.nodeHelper()->setNodeProcessed(node, false); // Set the data node to done to prevent it from being processed + } else if (KMime::Content *newNode = part.nodeHelper()->decryptedNodeForContent(node)) { + // if we already have a decrypted node for part.objectTreeParser() encrypted node, don't do the decryption again + return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), newNode, true)); + } else { + const auto codec = QTextCodec::codecForName("utf-8"); + mp->startDecryption(node->decodedContent(), codec); + + qCDebug(MIMETREEPARSER_LOG) << "decrypted, signed?:" << messagePart->isSigned; + + if (!messagePart->inProgress) { + if (!messagePart->isEncrypted) { + return nullptr; + } + auto tempNode = new KMime::Content(); + qDebug() << part.nodeHelper()->codec(node)->name(); + tempNode->contentType()->setCharset("utf-8"); + tempNode->setBody(KMime::CRLFtoLF(part.nodeHelper()->codec(node)->fromUnicode(mp->text()))); + tempNode->parse(); + + if (!tempNode->head().isEmpty()) { + tempNode->contentDescription()->from7BitString("encrypted data"); + } + part.nodeHelper()->cleanExtraContent(node); + mp->clearSubParts(); + + part.nodeHelper()->attachExtraContent(node, tempNode); + + mp->parseInternal(tempNode, false); + + part.nodeHelper()->setNodeProcessed(node, false); // Set the data node to done to prevent it from being processed + } + } + return mp; +} diff --git a/mimetreeparser/src/bodyformatter/encrypted.h b/mimetreeparser/src/bodyformatter/encrypted.h new file mode 100644 index 00000000..9105353c --- /dev/null +++ b/mimetreeparser/src/bodyformatter/encrypted.h @@ -0,0 +1,36 @@ +/* + Copyright (c) 2017 Sandro Knauß + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef __MIMETREEPARSER_BODYFORAMATTER_ENCRYPTED_H__ +#define __MIMETREEPARSER_BODYFORAMATTER_ENCRYPTED_H__ + +#include "interfaces/bodypartformatter.h" +#include "interfaces/bodypart.h" + +namespace MimeTreeParser { +class EncryptedBodyPartFormatter : public Interface::BodyPartFormatter +{ + static const EncryptedBodyPartFormatter *self; +public: + MessagePartPtr process(Interface::BodyPart &part) const override; + static const Interface::BodyPartFormatter *create(); +}; +} + +#endif diff --git a/mimetreeparser/src/bodypartformatter.cpp b/mimetreeparser/src/bodypartformatter.cpp index 27ad56b4..e990754d 100644 --- a/mimetreeparser/src/bodypartformatter.cpp +++ b/mimetreeparser/src/bodypartformatter.cpp @@ -1,187 +1,189 @@ /* -*- c++ -*- bodypartformatter.cpp This file is part of KMail, the KDE mail client. Copyright (c) 2003 Marc Mutz KMail is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. KMail is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include "mimetreeparser_debug.h" #include "bodyformatter/applicationpgpencrypted.h" #include "bodyformatter/applicationpkcs7mime.h" +#include "bodyformatter/encrypted.h" #include "bodyformatter/mailman.h" #include "bodyformatter/multipartalternative.h" #include "bodyformatter/multipartmixed.h" #include "bodyformatter/multipartencrypted.h" #include "bodyformatter/multipartsigned.h" #include "bodyformatter/texthtml.h" #include "bodyformatter/textplain.h" #include "interfaces/bodypartformatter.h" #include "interfaces/bodypart.h" #include "interfaces/htmlwriter.h" #include "bodypartformatterfactory.h" #include "bodypartformatterfactory_p.h" #include "attachmentstrategy.h" #include "objecttreeparser.h" #include "messagepart.h" #include #include using namespace MimeTreeParser; namespace { class AnyTypeBodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter { static const AnyTypeBodyPartFormatter *self; public: MessagePart::Ptr process(Interface::BodyPart &part) const override { KMime::Content *node = part.content(); const auto mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, false, part.source()->decryptMessage())); part.processResult()->setInlineSignatureState(mp->signatureState()); part.processResult()->setInlineEncryptionState(mp->encryptionState()); part.processResult()->setNeverDisplayInline(true); mp->setNeverDisplayInline(true); mp->setIsImage(false); return mp; } static const MimeTreeParser::Interface::BodyPartFormatter *create() { if (!self) { self = new AnyTypeBodyPartFormatter(); } return self; } }; const AnyTypeBodyPartFormatter *AnyTypeBodyPartFormatter::self = nullptr; class ImageTypeBodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter { static const ImageTypeBodyPartFormatter *self; public: static const MimeTreeParser::Interface::BodyPartFormatter *create() { if (!self) { self = new ImageTypeBodyPartFormatter(); } return self; } MessagePart::Ptr process(Interface::BodyPart &part) const override { KMime::Content *node = part.content(); auto mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, false, part.source()->decryptMessage())); mp->setIsImage(true); part.processResult()->setInlineSignatureState(mp->signatureState()); part.processResult()->setInlineEncryptionState(mp->encryptionState()); auto preferredMode = part.source()->preferredMode(); bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml); if (node->parent() && node->parent()->contentType()->subType() == "related" && isHtmlPreferred && !part.objectTreeParser()->showOnlyOneMimePart()) { QString fileName = mp->temporaryFilePath(); QString href = QUrl::fromLocalFile(fileName).url(); QByteArray cid = node->contentID()->identifier(); if (part.objectTreeParser()->htmlWriter()) { part.objectTreeParser()->htmlWriter()->embedPart(cid, href); } part.nodeHelper()->setNodeDisplayedEmbedded(node, true); part.nodeHelper()->setNodeDisplayedHidden(node, true); return mp; } // Show it inline if showOnlyOneMimePart(), which means the user clicked the image // in the message structure viewer manually, and therefore wants to see the full image if (part.objectTreeParser()->showOnlyOneMimePart() && !part.processResult()->neverDisplayInline()) { part.nodeHelper()->setNodeDisplayedEmbedded(node, true); } return mp; } }; const ImageTypeBodyPartFormatter *ImageTypeBodyPartFormatter::self = nullptr; class MessageRfc822BodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter { static const MessageRfc822BodyPartFormatter *self; public: MessagePart::Ptr process(Interface::BodyPart &) const override; static const MimeTreeParser::Interface::BodyPartFormatter *create(); }; const MessageRfc822BodyPartFormatter *MessageRfc822BodyPartFormatter::self; const MimeTreeParser::Interface::BodyPartFormatter *MessageRfc822BodyPartFormatter::create() { if (!self) { self = new MessageRfc822BodyPartFormatter(); } return self; } MessagePart::Ptr MessageRfc822BodyPartFormatter::process(Interface::BodyPart &part) const { const KMime::Message::Ptr message = part.content()->bodyAsMessage(); return MessagePart::Ptr(new EncapsulatedRfc822MessagePart(part.objectTreeParser(), part.content(), message)); } } // anon namespace void BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters() { insert(QStringLiteral("application/pkcs7-mime"), ApplicationPkcs7MimeBodyPartFormatter::create()); insert(QStringLiteral("application/x-pkcs7-mime"), ApplicationPkcs7MimeBodyPartFormatter::create()); insert(QStringLiteral("application/pgp-encrypted"), ApplicationPGPEncryptedBodyPartFormatter::create()); insert(QStringLiteral("application/octet-stream"), ApplicationPkcs7MimeBodyPartFormatter::create()); insert(QStringLiteral("application/octet-stream"), AnyTypeBodyPartFormatter::create()); + insert(QStringLiteral("text/pgp"), EncryptedBodyPartFormatter::create()); insert(QStringLiteral("text/html"), TextHtmlBodyPartFormatter::create()); insert(QStringLiteral("text/rtf"), AnyTypeBodyPartFormatter::create()); insert(QStringLiteral("text/plain"), MailmanBodyPartFormatter::create()); insert(QStringLiteral("text/plain"), TextPlainBodyPartFormatter::create()); insert(QStringLiteral("image/png"), ImageTypeBodyPartFormatter::create()); insert(QStringLiteral("image/jpeg"), ImageTypeBodyPartFormatter::create()); insert(QStringLiteral("image/gif"), ImageTypeBodyPartFormatter::create()); insert(QStringLiteral("image/svg+xml"), ImageTypeBodyPartFormatter::create()); insert(QStringLiteral("image/bmp"), ImageTypeBodyPartFormatter::create()); insert(QStringLiteral("image/vnd.microsoft.icon"), ImageTypeBodyPartFormatter::create()); insert(QStringLiteral("message/rfc822"), MessageRfc822BodyPartFormatter::create()); insert(QStringLiteral("multipart/alternative"), MultiPartAlternativeBodyPartFormatter::create()); insert(QStringLiteral("multipart/encrypted"), MultiPartEncryptedBodyPartFormatter::create()); insert(QStringLiteral("multipart/signed"), MultiPartSignedBodyPartFormatter::create()); insert(QStringLiteral("multipart/mixed"), MultiPartMixedBodyPartFormatter::create()); } diff --git a/mimetreeparser/src/messagepart.cpp b/mimetreeparser/src/messagepart.cpp index 8bcf28d9..35226c0d 100644 --- a/mimetreeparser/src/messagepart.cpp +++ b/mimetreeparser/src/messagepart.cpp @@ -1,1413 +1,1423 @@ /* Copyright (c) 2015 Sandro Knauß This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagepart.h" #include "mimetreeparser_debug.h" #include "attachmentstrategy.h" #include "cryptohelper.h" #include "objecttreeparser.h" #include "job/qgpgmejobexecutor.h" #include "memento/cryptobodypartmemento.h" #include "memento/decryptverifybodypartmemento.h" #include "memento/verifydetachedbodypartmemento.h" #include "memento/verifyopaquebodypartmemento.h" #include "bodyformatter/utils.h" #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace MimeTreeParser; //------MessagePart----------------------- namespace MimeTreeParser { class MessagePartPrivate { public: MessagePart *mParentPart = nullptr; QVector mBlocks; KMime::Content *mNode = nullptr; KMime::Content *mAttachmentNode = nullptr; QString mText; PartMetaData mMetaData; bool mRoot = false; }; } MessagePart::MessagePart(ObjectTreeParser *otp, const QString &text) : mOtp(otp) , d(new MessagePartPrivate) { d->mText = text; } MessagePart::~MessagePart() = default; MessagePart *MessagePart::parentPart() const { return d->mParentPart; } void MessagePart::setParentPart(MessagePart *parentPart) { d->mParentPart = parentPart; } QString MessagePart::htmlContent() const { return text(); } QString MessagePart::plaintextContent() const { return text(); } PartMetaData *MessagePart::partMetaData() const { return &d->mMetaData; } Interface::BodyPartMemento* MessagePart::memento() const { return nodeHelper()->bodyPartMemento(content(), "__plugin__"); } void MessagePart::setMemento(Interface::BodyPartMemento *memento) { nodeHelper()->setBodyPartMemento(content(), "__plugin__", memento); } KMime::Content *MessagePart::content() const { return d->mNode; } void MessagePart::setContent(KMime::Content *node) { d->mNode = node; } KMime::Content *MessagePart::attachmentContent() const { return d->mAttachmentNode; } void MessagePart::setAttachmentContent(KMime::Content *node) { d->mAttachmentNode = node; } bool MessagePart::isAttachment() const { return d->mAttachmentNode; } QString MessagePart::attachmentIndex() const { return attachmentContent()->index().toString(); } QString MessagePart::attachmentLink() const { return mOtp->nodeHelper()->asHREF(content(), QStringLiteral("body")); } QString MessagePart::makeLink(const QString &path) const { // FIXME: use a PRNG for the first arg, instead of a serial number static int serial = 0; if (path.isEmpty()) { return {}; } return QStringLiteral("x-kmail:/bodypart/%1/%2/%3") .arg(serial++).arg(content()->index().toString()) .arg(QString::fromLatin1(QUrl::toPercentEncoding(path, "/"))); } void MessagePart::setIsRoot(bool root) { d->mRoot = root; } bool MessagePart::isRoot() const { return d->mRoot; } QString MessagePart::text() const { return d->mText; } void MessagePart::setText(const QString &text) { d->mText = text; } bool MessagePart::isHtml() const { return false; } bool MessagePart::isHidden() const { return false; } Interface::ObjectTreeSource *MessagePart::source() const { Q_ASSERT(mOtp); return mOtp->mSource; } NodeHelper* MessagePart::nodeHelper() const { Q_ASSERT(mOtp); return mOtp->nodeHelper(); } void MessagePart::parseInternal(KMime::Content *node, bool onlyOneMimePart) { auto subMessagePart = mOtp->parseObjectTreeInternal(node, onlyOneMimePart); d->mRoot = subMessagePart->isRoot(); foreach (const auto &part, subMessagePart->subParts()) { appendSubPart(part); } } QString MessagePart::renderInternalText() const { QString text; foreach (const auto &mp, subParts()) { text += mp->text(); } return text; } void MessagePart::fix() const { foreach (const auto &mp, subParts()) { const auto m = mp.dynamicCast(); if (m) { m->fix(); } } } void MessagePart::appendSubPart(const MessagePart::Ptr &messagePart) { messagePart->setParentPart(this); d->mBlocks.append(messagePart); } const QVector &MessagePart::subParts() const { return d->mBlocks; } bool MessagePart::hasSubParts() const { return !d->mBlocks.isEmpty(); } +void MessagePart::clearSubParts() +{ + d->mBlocks.clear(); +} + //-----MessagePartList---------------------- MessagePartList::MessagePartList(ObjectTreeParser *otp) : MessagePart(otp, QString()) { } MessagePartList::~MessagePartList() { } QString MessagePartList::text() const { return renderInternalText(); } QString MessagePartList::plaintextContent() const { return QString(); } QString MessagePartList::htmlContent() const { return QString(); } //-----TextMessageBlock---------------------- TextMessagePart::TextMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool decryptMessage) : MessagePartList(otp) , mDrawFrame(drawFrame) , mDecryptMessage(decryptMessage) , mIsHidden(false) { if (!node) { qCWarning(MIMETREEPARSER_LOG) << "not a valid node"; return; } setContent(node); mIsHidden = mOtp->nodeHelper()->isNodeDisplayedHidden(node); parseContent(); } TextMessagePart::~TextMessagePart() { } bool TextMessagePart::decryptMessage() const { return mDecryptMessage; } void TextMessagePart::parseContent() { const auto aCodec = mOtp->codecFor(content()); const QString &fromAddress = mOtp->nodeHelper()->fromAsString(content()); mSignatureState = KMMsgNotSigned; mEncryptionState = KMMsgNotEncrypted; const auto blocks = prepareMessageForDecryption(content()->decodedContent()); const auto cryptProto = QGpgME::openpgp(); if (!blocks.isEmpty()) { /* The (overall) signature/encrypted status is broken * if one unencrypted part is at the beginning or in the middle * because mailmain adds an unencrypted part at the end this should not break the overall status * * That's why we first set the tmp status and if one crypted/signed block comes afterwards, than * the status is set to unencryped */ bool fullySignedOrEncrypted = true; bool fullySignedOrEncryptedTmp = true; for (const auto &block : blocks) { if (!fullySignedOrEncryptedTmp) { fullySignedOrEncrypted = false; } if (block.type() == NoPgpBlock && !block.text().trimmed().isEmpty()) { fullySignedOrEncryptedTmp = false; appendSubPart(MessagePart::Ptr(new MessagePart(mOtp, aCodec->toUnicode(block.text())))); } else if (block.type() == PgpMessageBlock) { EncryptedMessagePart::Ptr mp(new EncryptedMessagePart(mOtp, QString(), cryptProto, fromAddress, nullptr)); mp->setDecryptMessage(decryptMessage()); mp->setIsEncrypted(true); appendSubPart(mp); if (!decryptMessage()) { continue; } mp->startDecryption(block.text(), aCodec); if (mp->partMetaData()->inProgress) { continue; } } else if (block.type() == ClearsignedBlock) { SignedMessagePart::Ptr mp(new SignedMessagePart(mOtp, QString(), cryptProto, fromAddress, nullptr)); appendSubPart(mp); mp->startVerification(block.text(), aCodec); } else { continue; } const auto mp = subParts().last().staticCast(); const PartMetaData *messagePart(mp->partMetaData()); if (!messagePart->isEncrypted && !messagePart->isSigned && !block.text().trimmed().isEmpty()) { mp->setText(aCodec->toUnicode(block.text())); } if (messagePart->isEncrypted) { mEncryptionState = KMMsgPartiallyEncrypted; } if (messagePart->isSigned) { mSignatureState = KMMsgPartiallySigned; } } //Do we have an fully Signed/Encrypted Message? if (fullySignedOrEncrypted) { if (mSignatureState == KMMsgPartiallySigned) { mSignatureState = KMMsgFullySigned; } if (mEncryptionState == KMMsgPartiallyEncrypted) { mEncryptionState = KMMsgFullyEncrypted; } } } } KMMsgEncryptionState TextMessagePart::encryptionState() const { return mEncryptionState; } KMMsgSignatureState TextMessagePart::signatureState() const { return mSignatureState; } bool TextMessagePart::isHidden() const { return mIsHidden; } bool TextMessagePart::showLink() const { return !temporaryFilePath().isEmpty(); } bool TextMessagePart::showTextFrame() const { return mDrawFrame; } void TextMessagePart::setShowTextFrame(bool showFrame) { mDrawFrame = showFrame; } QString TextMessagePart::label() const { const QString name = content()->contentType()->name(); QString label = name.isEmpty() ? NodeHelper::fileName(content()) : name; if (label.isEmpty()) { label = i18nc("display name for an unnamed attachment", "Unnamed"); } return label; } QString TextMessagePart::comment() const { const QString comment = content()->contentDescription()->asUnicodeString(); if (comment == label()) { return {}; } return comment; } QString TextMessagePart::temporaryFilePath() const { return nodeHelper()->writeNodeToTempFile(content()); } //-----AttachmentMessageBlock---------------------- AttachmentMessagePart::AttachmentMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool decryptMessage) : TextMessagePart(otp, node, drawFrame, decryptMessage) , mIsImage(false) , mNeverDisplayInline(false) { } AttachmentMessagePart::~AttachmentMessagePart() { } bool AttachmentMessagePart::neverDisplayInline() const { return mNeverDisplayInline; } void AttachmentMessagePart::setNeverDisplayInline(bool displayInline) { mNeverDisplayInline = displayInline; } bool AttachmentMessagePart::isImage() const { return mIsImage; } void AttachmentMessagePart::setIsImage(bool image) { mIsImage = image; } IconType AttachmentMessagePart::asIcon() const { const AttachmentStrategy *const as = mOtp->attachmentStrategy(); const bool defaultHidden(as && as->defaultDisplay(content()) == AttachmentStrategy::None); const bool showOnlyOneMimePart(mOtp->showOnlyOneMimePart()); auto preferredMode = source()->preferredMode(); bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml); QByteArray mediaType("text"); if (content()->contentType(false) && !content()->contentType()->mediaType().isEmpty() && !content()->contentType()->subType().isEmpty()) { mediaType = content()->contentType()->mediaType(); } const bool isTextPart = (mediaType == QByteArrayLiteral("text")); bool defaultAsIcon = true; if (!neverDisplayInline()) { if (as) { defaultAsIcon = as->defaultDisplay(content()) == AttachmentStrategy::AsIcon; } } if (isImage() && showOnlyOneMimePart && !neverDisplayInline()) { defaultAsIcon = false; } // neither image nor text -> show as icon if (!isImage() && !isTextPart) { defaultAsIcon = true; } if (isTextPart) { if (as && as->defaultDisplay(content()) != AttachmentStrategy::Inline) { return MimeTreeParser::IconExternal; } return MimeTreeParser::NoIcon; } else { if (isImage() && isHtmlPreferred && content()->parent() && content()->parent()->contentType()->subType() == "related") { return MimeTreeParser::IconInline; } if (defaultHidden && !showOnlyOneMimePart && content()->parent()) { return MimeTreeParser::IconInline; } if (defaultAsIcon) { return MimeTreeParser::IconExternal; } else if (isImage()) { return MimeTreeParser::IconInline; } else { return MimeTreeParser::NoIcon; } } } bool AttachmentMessagePart::isHidden() const { if (mOtp->showOnlyOneMimePart()) { return false; // never hide when only showing one part, otherwise you'll see nothing } const AttachmentStrategy *const as = mOtp->attachmentStrategy(); const bool defaultHidden(as && as->defaultDisplay(content()) == AttachmentStrategy::None); auto preferredMode = source()->preferredMode(); bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml); QByteArray mediaType("text"); if (content()->contentType(false) && !content()->contentType()->mediaType().isEmpty() && !content()->contentType()->subType().isEmpty()) { mediaType = content()->contentType()->mediaType(); } const bool isTextPart = (mediaType == QByteArrayLiteral("text")); bool defaultAsIcon = true; if (!neverDisplayInline()) { if (as) { defaultAsIcon = as->defaultDisplay(content()) == AttachmentStrategy::AsIcon; } } // neither image nor text -> show as icon if (!isImage() && !isTextPart) { defaultAsIcon = true; } bool hidden(false); if (isTextPart) { hidden = defaultHidden; } else { if (isImage() && isHtmlPreferred && content()->parent() && content()->parent()->contentType()->subType() == "related") { hidden = true; } else { hidden = defaultHidden && content()->parent(); hidden |= defaultAsIcon && defaultHidden; } } mOtp->nodeHelper()->setNodeDisplayedHidden(content(), hidden); return hidden; } //-----HtmlMessageBlock---------------------- HtmlMessagePart::HtmlMessagePart(ObjectTreeParser *otp, KMime::Content *node, Interface::ObjectTreeSource *source) : MessagePart(otp, QString()) , mSource(source) { if (!node) { qCWarning(MIMETREEPARSER_LOG) << "not a valid node"; return; } setContent(node); const QByteArray partBody(node->decodedContent()); mBodyHTML = mOtp->codecFor(node)->toUnicode(partBody); mCharset = NodeHelper::charset(node); } HtmlMessagePart::~HtmlMessagePart() { } void HtmlMessagePart::fix() const { mOtp->mHtmlContent += mBodyHTML; mOtp->mHtmlContentCharset = mCharset; } QString HtmlMessagePart::text() const { return mBodyHTML; } bool HtmlMessagePart::isHtml() const { return true; } //-----MimeMessageBlock---------------------- MimeMessagePart::MimeMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool onlyOneMimePart) : MessagePart(otp, QString()) , mOnlyOneMimePart(onlyOneMimePart) { if (!node) { qCWarning(MIMETREEPARSER_LOG) << "not a valid node"; return; } setContent(node); parseInternal(node, mOnlyOneMimePart); } MimeMessagePart::~MimeMessagePart() { } QString MimeMessagePart::text() const { return renderInternalText(); } QString MimeMessagePart::plaintextContent() const { return QString(); } QString MimeMessagePart::htmlContent() const { return QString(); } //-----AlternativeMessagePart---------------------- AlternativeMessagePart::AlternativeMessagePart(ObjectTreeParser *otp, KMime::Content *node, Util::HtmlMode preferredMode) : MessagePart(otp, QString()) , mPreferredMode(preferredMode) { setContent(node); KMime::Content *dataIcal = findTypeInDirectChilds(node, "text/calendar"); KMime::Content *dataHtml = findTypeInDirectChilds(node, "text/html"); KMime::Content *dataText = findTypeInDirectChilds(node, "text/plain"); if (!dataHtml) { // If we didn't find the HTML part as the first child of the multipart/alternative, it might // be that this is a HTML message with images, and text/plain and multipart/related are the // immediate children of this multipart/alternative node. // In this case, the HTML node is a child of multipart/related. dataHtml = findTypeInDirectChilds(node, "multipart/related"); // Still not found? Stupid apple mail actually puts the attachments inside of the // multipart/alternative, which is wrong. Therefore we also have to look for multipart/mixed // here. // Do this only when prefering HTML mail, though, since otherwise the attachments are hidden // when displaying plain text. if (!dataHtml) { dataHtml = findTypeInDirectChilds(node, "multipart/mixed"); } } if (dataIcal) { mChildNodes[Util::MultipartIcal] = dataIcal; } if (dataText) { mChildNodes[Util::MultipartPlain] = dataText; } if (dataHtml) { mChildNodes[Util::MultipartHtml] = dataHtml; } if (mChildNodes.isEmpty()) { qCWarning(MIMETREEPARSER_LOG) << "no valid nodes"; return; } QMapIterator i(mChildNodes); while (i.hasNext()) { i.next(); mChildParts[i.key()] = MimeMessagePart::Ptr(new MimeMessagePart(mOtp, i.value(), true)); } } AlternativeMessagePart::~AlternativeMessagePart() { } Util::HtmlMode AlternativeMessagePart::preferredMode() const { return mPreferredMode; } QList AlternativeMessagePart::availableModes() { return mChildParts.keys(); } QString AlternativeMessagePart::text() const { if (mChildParts.contains(Util::MultipartPlain)) { return mChildParts[Util::MultipartPlain]->text(); } return QString(); } void AlternativeMessagePart::fix() const { if (mChildParts.contains(Util::MultipartPlain)) { mChildParts[Util::MultipartPlain]->fix(); } const auto mode = preferredMode(); if (mode != Util::MultipartPlain && mChildParts.contains(mode)) { mChildParts[mode]->fix(); } } bool AlternativeMessagePart::isHtml() const { return mChildParts.contains(Util::MultipartHtml); } QString AlternativeMessagePart::plaintextContent() const { return text(); } QString AlternativeMessagePart::htmlContent() const { if (mChildParts.contains(Util::MultipartHtml)) { return mChildParts[Util::MultipartHtml]->text(); } else { return plaintextContent(); } } //-----CertMessageBlock---------------------- CertMessagePart::CertMessagePart(ObjectTreeParser *otp, KMime::Content *node, const QGpgME::Protocol *cryptoProto, bool autoImport) : MessagePart(otp, QString()) , mAutoImport(autoImport) , mCryptoProto(cryptoProto) { if (!node) { qCWarning(MIMETREEPARSER_LOG) << "not a valid node"; return; } setContent(node); if (!mAutoImport) { return; } const QByteArray certData = node->decodedContent(); QGpgME::ImportJob *import = mCryptoProto->importJob(); QGpgMEJobExecutor executor; mImportResult = executor.exec(import, certData); } CertMessagePart::~CertMessagePart() { } QString CertMessagePart::text() const { return QString(); } //-----SignedMessageBlock--------------------- SignedMessagePart::SignedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node) : MessagePart(otp, text) , mCryptoProto(cryptoProto) , mFromAddress(fromAddress) { setContent(node); partMetaData()->technicalProblem = (mCryptoProto == nullptr); partMetaData()->isSigned = true; partMetaData()->isGoodSignature = false; partMetaData()->keyTrust = GpgME::Signature::Unknown; partMetaData()->status = i18n("Wrong Crypto Plug-In."); partMetaData()->status_code = GPGME_SIG_STAT_NONE; } SignedMessagePart::~SignedMessagePart() { } void SignedMessagePart::setIsSigned(bool isSigned) { partMetaData()->isSigned = isSigned; } bool SignedMessagePart::isSigned() const { return partMetaData()->isSigned; } bool SignedMessagePart::okVerify(const QByteArray &data, const QByteArray &signature, KMime::Content *textNode) { NodeHelper *nodeHelper = mOtp->nodeHelper(); partMetaData()->isSigned = false; partMetaData()->technicalProblem = (mCryptoProto == nullptr); partMetaData()->keyTrust = GpgME::Signature::Unknown; partMetaData()->status = i18n("Wrong Crypto Plug-In."); partMetaData()->status_code = GPGME_SIG_STAT_NONE; const QByteArray mementoName = "verification"; CryptoBodyPartMemento *m = dynamic_cast(nodeHelper->bodyPartMemento(content(), mementoName)); Q_ASSERT(!m || mCryptoProto); //No CryptoPlugin and having a bodyPartMemento -> there is something completely wrong if (!m && mCryptoProto) { if (!signature.isEmpty()) { QGpgME::VerifyDetachedJob *job = mCryptoProto->verifyDetachedJob(); if (job) { m = new VerifyDetachedBodyPartMemento(job, mCryptoProto->keyListJob(), signature, data); } } else { QGpgME::VerifyOpaqueJob *job = mCryptoProto->verifyOpaqueJob(); if (job) { m = new VerifyOpaqueBodyPartMemento(job, mCryptoProto->keyListJob(), data); } } if (m) { if (mOtp->allowAsync()) { QObject::connect(m, &CryptoBodyPartMemento::update, nodeHelper, &NodeHelper::update); if (m->start()) { partMetaData()->inProgress = true; mOtp->mHasPendingAsyncJobs = true; } } else { m->exec(); } nodeHelper->setBodyPartMemento(content(), mementoName, m); } } else if (m && m->isRunning()) { partMetaData()->inProgress = true; mOtp->mHasPendingAsyncJobs = true; } else { partMetaData()->inProgress = false; mOtp->mHasPendingAsyncJobs = false; } if (m && !partMetaData()->inProgress) { if (!signature.isEmpty()) { mVerifiedText = data; } setVerificationResult(m, textNode); } if (!m && !partMetaData()->inProgress) { QString errorMsg; QString cryptPlugLibName; QString cryptPlugDisplayName; if (mCryptoProto) { cryptPlugLibName = mCryptoProto->name(); cryptPlugDisplayName = mCryptoProto->displayName(); } if (!mCryptoProto) { if (cryptPlugDisplayName.isEmpty()) { errorMsg = i18n("No appropriate crypto plug-in was found."); } else { errorMsg = i18nc("%1 is either 'OpenPGP' or 'S/MIME'", "No %1 plug-in was found.", cryptPlugDisplayName); } } else { errorMsg = i18n("Crypto plug-in \"%1\" cannot verify signatures.", cryptPlugLibName); } partMetaData()->errorText = i18n("The message is signed, but the " "validity of the signature cannot be " "verified.
" "Reason: %1", errorMsg); } return partMetaData()->isSigned; } static int signatureToStatus(const GpgME::Signature &sig) { switch (sig.status().code()) { case GPG_ERR_NO_ERROR: return GPGME_SIG_STAT_GOOD; case GPG_ERR_BAD_SIGNATURE: return GPGME_SIG_STAT_BAD; case GPG_ERR_NO_PUBKEY: return GPGME_SIG_STAT_NOKEY; case GPG_ERR_NO_DATA: return GPGME_SIG_STAT_NOSIG; case GPG_ERR_SIG_EXPIRED: return GPGME_SIG_STAT_GOOD_EXP; case GPG_ERR_KEY_EXPIRED: return GPGME_SIG_STAT_GOOD_EXPKEY; default: return GPGME_SIG_STAT_ERROR; } } QString prettifyDN(const char *uid) { return QGpgME::DN(uid).prettyDN(); } void SignedMessagePart::sigStatusToMetaData() { GpgME::Key key; if (partMetaData()->isSigned) { GpgME::Signature signature = mSignatures.front(); partMetaData()->status_code = signatureToStatus(signature); partMetaData()->isGoodSignature = partMetaData()->status_code & GPGME_SIG_STAT_GOOD; // save extended signature status flags partMetaData()->sigSummary = signature.summary(); if (partMetaData()->isGoodSignature && !key.keyID()) { // Search for the key by its fingerprint so that we can check for // trust etc. QGpgME::KeyListJob *job = mCryptoProto->keyListJob(false, false, false); // local, no sigs if (!job) { qCDebug(MIMETREEPARSER_LOG) << "The Crypto backend does not support listing keys. "; } else { std::vector found_keys; // As we are local it is ok to make this synchronous GpgME::KeyListResult res = job->exec(QStringList(QLatin1String(signature.fingerprint())), false, found_keys); if (res.error()) { qCDebug(MIMETREEPARSER_LOG) << "Error while searching key for Fingerprint: " << signature.fingerprint(); } if (found_keys.size() > 1) { // Should not Happen qCDebug(MIMETREEPARSER_LOG) << "Oops: Found more then one Key for Fingerprint: " << signature.fingerprint(); } if (found_keys.size() != 1) { // Should not Happen at this point qCDebug(MIMETREEPARSER_LOG) << "Oops: Found no Key for Fingerprint: " << signature.fingerprint(); } else { key = found_keys[0]; } delete job; } } if (key.keyID()) { partMetaData()->keyId = key.keyID(); } if (partMetaData()->keyId.isEmpty()) { partMetaData()->keyId = signature.fingerprint(); } partMetaData()->keyTrust = signature.validity(); if (key.numUserIDs() > 0 && key.userID(0).id()) { partMetaData()->signer = prettifyDN(key.userID(0).id()); } for (uint iMail = 0; iMail < key.numUserIDs(); ++iMail) { // The following if /should/ always result in TRUE but we // won't trust implicitely the plugin that gave us these data. if (key.userID(iMail).email()) { QString email = QString::fromUtf8(key.userID(iMail).email()); // ### work around gpgme 0.3.QString text() const override;x / cryptplug bug where the // ### email addresses are specified as angle-addr, not addr-spec: if (email.startsWith(QLatin1Char('<')) && email.endsWith(QLatin1Char('>'))) { email = email.mid(1, email.length() - 2); } if (!email.isEmpty()) { partMetaData()->signerMailAddresses.append(email); } } } if (signature.creationTime()) { partMetaData()->creationTime.setTime_t(signature.creationTime()); } else { partMetaData()->creationTime = QDateTime(); } if (partMetaData()->signer.isEmpty()) { if (key.numUserIDs() > 0 && key.userID(0).name()) { partMetaData()->signer = prettifyDN(key.userID(0).name()); } if (!partMetaData()->signerMailAddresses.empty()) { if (partMetaData()->signer.isEmpty()) { partMetaData()->signer = partMetaData()->signerMailAddresses.front(); } else { partMetaData()->signer += QLatin1String(" <") + partMetaData()->signerMailAddresses.front() + QLatin1Char('>'); } } } } } void SignedMessagePart::startVerification(const QByteArray &text, const QTextCodec *aCodec) { startVerificationDetached(text, nullptr, QByteArray()); if (!content() && partMetaData()->isSigned) { setText(aCodec->toUnicode(mVerifiedText)); } } void SignedMessagePart::startVerificationDetached(const QByteArray &text, KMime::Content *textNode, const QByteArray &signature) { partMetaData()->isEncrypted = false; partMetaData()->isDecryptable = false; if (textNode) { parseInternal(textNode, false); } okVerify(text, signature, textNode); if (!partMetaData()->isSigned) { partMetaData()->creationTime = QDateTime(); } } void SignedMessagePart::setVerificationResult(const CryptoBodyPartMemento *m, KMime::Content *textNode) { { const auto vm = dynamic_cast(m); if (vm) { mSignatures = vm->verifyResult().signatures(); } } { const auto vm = dynamic_cast(m); if (vm) { mVerifiedText = vm->plainText(); mSignatures = vm->verifyResult().signatures(); } } { const auto vm = dynamic_cast(m); if (vm) { mVerifiedText = vm->plainText(); mSignatures = vm->verifyResult().signatures(); } } partMetaData()->auditLogError = m->auditLogError(); partMetaData()->auditLog = m->auditLogAsHtml(); partMetaData()->isSigned = !mSignatures.empty(); if (partMetaData()->isSigned) { sigStatusToMetaData(); if (content()) { mOtp->nodeHelper()->setSignatureState(content(), KMMsgFullySigned); if (!textNode) { mOtp->nodeHelper()->setPartMetaData(content(), *partMetaData()); if (!mVerifiedText.isEmpty()) { auto tempNode = new KMime::Content(); tempNode->setContent(KMime::CRLFtoLF(mVerifiedText.constData())); tempNode->parse(); if (!tempNode->head().isEmpty()) { tempNode->contentDescription()->from7BitString("signed data"); } mOtp->nodeHelper()->attachExtraContent(content(), tempNode); parseInternal(tempNode, false); } } } } } QString SignedMessagePart::plaintextContent() const { if (!content()) { return MessagePart::text(); } else { return QString(); } } QString SignedMessagePart::htmlContent() const { if (!content()) { return MessagePart::text(); } else { return QString(); } } //-----CryptMessageBlock--------------------- EncryptedMessagePart::EncryptedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node) : MessagePart(otp, text) , mPassphraseError(false) , mNoSecKey(false) , mCryptoProto(cryptoProto) , mFromAddress(fromAddress) , mDecryptMessage(false) { setContent(node); partMetaData()->technicalProblem = (mCryptoProto == nullptr); partMetaData()->isSigned = false; partMetaData()->isGoodSignature = false; partMetaData()->isEncrypted = false; partMetaData()->isDecryptable = false; partMetaData()->keyTrust = GpgME::Signature::Unknown; partMetaData()->status = i18n("Wrong Crypto Plug-In."); partMetaData()->status_code = GPGME_SIG_STAT_NONE; } EncryptedMessagePart::~EncryptedMessagePart() { } void EncryptedMessagePart::setDecryptMessage(bool decrypt) { mDecryptMessage = decrypt; } bool EncryptedMessagePart::decryptMessage() const { return mDecryptMessage; } void EncryptedMessagePart::setIsEncrypted(bool encrypted) { partMetaData()->isEncrypted = encrypted; } bool EncryptedMessagePart::isEncrypted() const { return partMetaData()->isEncrypted; } bool EncryptedMessagePart::isDecryptable() const { return partMetaData()->isDecryptable; } bool EncryptedMessagePart::passphraseError() const { return mPassphraseError; } void EncryptedMessagePart::startDecryption(const QByteArray &text, const QTextCodec *aCodec) { KMime::Content *content = new KMime::Content; content->setBody(text); content->parse(); startDecryption(content); if (!partMetaData()->inProgress && partMetaData()->isDecryptable) { if (hasSubParts()) { auto _mp = (subParts()[0]).dynamicCast(); if (_mp) { _mp->setText(aCodec->toUnicode(mDecryptedData)); } else { setText(aCodec->toUnicode(mDecryptedData)); } } else { setText(aCodec->toUnicode(mDecryptedData)); } } } bool EncryptedMessagePart::okDecryptMIME(KMime::Content &data) { mPassphraseError = false; partMetaData()->inProgress = false; partMetaData()->errorText.clear(); partMetaData()->auditLogError = GpgME::Error(); partMetaData()->auditLog.clear(); bool bDecryptionOk = false; bool cannotDecrypt = false; NodeHelper *nodeHelper = mOtp->nodeHelper(); Q_ASSERT(decryptMessage()); // Check whether the memento contains a result from last time: const DecryptVerifyBodyPartMemento *m = dynamic_cast(nodeHelper->bodyPartMemento(&data, "decryptverify")); Q_ASSERT(!m || mCryptoProto); //No CryptoPlugin and having a bodyPartMemento -> there is something completely wrong if (!m && mCryptoProto) { QGpgME::DecryptVerifyJob *job = mCryptoProto->decryptVerifyJob(); if (!job) { cannotDecrypt = true; } else { const QByteArray ciphertext = data.decodedContent(); DecryptVerifyBodyPartMemento *newM = new DecryptVerifyBodyPartMemento(job, ciphertext); if (mOtp->allowAsync()) { QObject::connect(newM, &CryptoBodyPartMemento::update, nodeHelper, &NodeHelper::update); if (newM->start()) { partMetaData()->inProgress = true; mOtp->mHasPendingAsyncJobs = true; } else { m = newM; } } else { newM->exec(); m = newM; } nodeHelper->setBodyPartMemento(&data, "decryptverify", newM); } } else if (m && m->isRunning()) { partMetaData()->inProgress = true; mOtp->mHasPendingAsyncJobs = true; m = nullptr; } if (m) { const QByteArray &plainText = m->plainText(); const GpgME::DecryptionResult &decryptResult = m->decryptResult(); const GpgME::VerificationResult &verifyResult = m->verifyResult(); partMetaData()->isSigned = verifyResult.signatures().size() > 0; if (verifyResult.signatures().size() > 0) { auto subPart = SignedMessagePart::Ptr(new SignedMessagePart(mOtp, MessagePart::text(), mCryptoProto, mFromAddress, content())); subPart->setVerificationResult(m, nullptr); appendSubPart(subPart); } mDecryptRecipients.clear(); + bDecryptionOk = !decryptResult.error(); + +// std::stringstream ss; +// ss << decryptResult << '\n' << verifyResult; +// qCDebug(MIMETREEPARSER_LOG) << ss.str().c_str(); + for (const auto &recipient : decryptResult.recipients()) { + if (!recipient.status()) { + bDecryptionOk = true; + } GpgME::Key key; QGpgME::KeyListJob *job = mCryptoProto->keyListJob(false, false, false); // local, no sigs if (!job) { qCDebug(MIMETREEPARSER_LOG) << "The Crypto backend does not support listing keys. "; } else { std::vector found_keys; // As we are local it is ok to make this synchronous GpgME::KeyListResult res = job->exec(QStringList(QLatin1String(recipient.keyID())), false, found_keys); if (res.error()) { qCDebug(MIMETREEPARSER_LOG) << "Error while searching key for Fingerprint: " << recipient.keyID(); } if (found_keys.size() > 1) { // Should not Happen qCDebug(MIMETREEPARSER_LOG) << "Oops: Found more then one Key for Fingerprint: " << recipient.keyID(); } if (found_keys.size() != 1) { // Should not Happen at this point qCDebug(MIMETREEPARSER_LOG) << "Oops: Found no Key for Fingerprint: " << recipient.keyID(); } else { key = found_keys[0]; } } mDecryptRecipients.push_back(std::make_pair(recipient, key)); } - bDecryptionOk = !decryptResult.error(); -// std::stringstream ss; -// ss << decryptResult << '\n' << verifyResult; -// qCDebug(MIMETREEPARSER_LOG) << ss.str().c_str(); if (!bDecryptionOk && partMetaData()->isSigned) { //Only a signed part partMetaData()->isEncrypted = false; bDecryptionOk = true; mDecryptedData = plainText; } else { mPassphraseError = decryptResult.error().isCanceled() || decryptResult.error().code() == GPG_ERR_NO_SECKEY; - partMetaData()->isEncrypted = decryptResult.error().code() != GPG_ERR_NO_DATA; + partMetaData()->isEncrypted = bDecryptionOk || decryptResult.error().code() != GPG_ERR_NO_DATA; partMetaData()->errorText = QString::fromLocal8Bit(decryptResult.error().asString()); if (partMetaData()->isEncrypted && decryptResult.numRecipients() > 0) { partMetaData()->keyId = decryptResult.recipient(0).keyID(); } if (bDecryptionOk) { mDecryptedData = plainText; } else { mNoSecKey = true; foreach (const GpgME::DecryptionResult::Recipient &recipient, decryptResult.recipients()) { mNoSecKey &= (recipient.status().code() == GPG_ERR_NO_SECKEY); } if (!mPassphraseError && !mNoSecKey) { // GpgME do not detect passphrase error correctly mPassphraseError = true; } } } } if (!bDecryptionOk) { QString cryptPlugLibName; if (mCryptoProto) { cryptPlugLibName = mCryptoProto->name(); } if (!mCryptoProto) { partMetaData()->errorText = i18n("No appropriate crypto plug-in was found."); } else if (cannotDecrypt) { partMetaData()->errorText = i18n("Crypto plug-in \"%1\" cannot decrypt messages.", cryptPlugLibName); } else if (!passphraseError()) { partMetaData()->errorText = i18n("Crypto plug-in \"%1\" could not decrypt the data.", cryptPlugLibName) + QLatin1String("
") + i18n("Error: %1", partMetaData()->errorText); } } return bDecryptionOk; } void EncryptedMessagePart::startDecryption(KMime::Content *data) { if (!content() && !data) { return; } if (!data) { data = content(); } partMetaData()->isEncrypted = true; bool bOkDecrypt = okDecryptMIME(*data); if (partMetaData()->inProgress) { return; } partMetaData()->isDecryptable = bOkDecrypt; if (!partMetaData()->isDecryptable) { setText(QString::fromUtf8(mDecryptedData.constData())); } if (partMetaData()->isEncrypted && !decryptMessage()) { partMetaData()->isDecryptable = true; } if (content() && !partMetaData()->isSigned) { mOtp->nodeHelper()->setPartMetaData(content(), *partMetaData()); if (decryptMessage()) { auto tempNode = new KMime::Content(); tempNode->setContent(KMime::CRLFtoLF(mDecryptedData.constData())); tempNode->parse(); if (!tempNode->head().isEmpty()) { tempNode->contentDescription()->from7BitString("encrypted data"); } mOtp->nodeHelper()->attachExtraContent(content(), tempNode); parseInternal(tempNode, false); } } } QString EncryptedMessagePart::plaintextContent() const { if (!content()) { return MessagePart::text(); } else { return QString(); } } QString EncryptedMessagePart::htmlContent() const { if (!content()) { return MessagePart::text(); } else { return QString(); } } QString EncryptedMessagePart::text() const { if (hasSubParts()) { auto _mp = (subParts()[0]).dynamicCast(); if (_mp) { return _mp->text(); } else { return MessagePart::text(); } } else { return MessagePart::text(); } } EncapsulatedRfc822MessagePart::EncapsulatedRfc822MessagePart(ObjectTreeParser *otp, KMime::Content *node, const KMime::Message::Ptr &message) : MessagePart(otp, QString()) , mMessage(message) { setContent(node); partMetaData()->isEncrypted = false; partMetaData()->isSigned = false; partMetaData()->isEncapsulatedRfc822Message = true; mOtp->nodeHelper()->setNodeDisplayedEmbedded(node, true); mOtp->nodeHelper()->setPartMetaData(node, *partMetaData()); if (!mMessage) { qCWarning(MIMETREEPARSER_LOG) << "Node is of type message/rfc822 but doesn't have a message!"; return; } // The link to "Encapsulated message" is clickable, therefore the temp file needs to exists, // since the user can click the link and expect to have normal attachment operations there. mOtp->nodeHelper()->writeNodeToTempFile(message.data()); parseInternal(message.data(), false); } EncapsulatedRfc822MessagePart::~EncapsulatedRfc822MessagePart() { } QString EncapsulatedRfc822MessagePart::text() const { return renderInternalText(); } void EncapsulatedRfc822MessagePart::fix() const { } diff --git a/mimetreeparser/src/messagepart.h b/mimetreeparser/src/messagepart.h index 38cbfef4..e8c5a1d7 100644 --- a/mimetreeparser/src/messagepart.h +++ b/mimetreeparser/src/messagepart.h @@ -1,423 +1,425 @@ /* Copyright (c) 2015 Sandro Knauß This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef __MIMETREEPARSER_MESSAGEPART_H__ #define __MIMETREEPARSER_MESSAGEPART_H__ #include "mimetreeparser_export.h" #include "mimetreeparser/bodypartformatter.h" #include "mimetreeparser/util.h" #include #include #include #include #include #include #include #include class QTextCodec; namespace GpgME { class ImportResult; } namespace QGpgME { class Protocol; } namespace KMime { class Content; } namespace MimeTreeParser { class ObjectTreeParser; class CryptoBodyPartMemento; class MessagePartPrivate; class MultiPartAlternativeBodyPartFormatter; namespace Interface { class ObjectTreeSource; } class MIMETREEPARSER_EXPORT MessagePart : public QObject { Q_OBJECT Q_PROPERTY(QString plaintextContent READ plaintextContent) Q_PROPERTY(QString htmlContent READ htmlContent) Q_PROPERTY(bool isAttachment READ isAttachment) Q_PROPERTY(bool root READ isRoot) Q_PROPERTY(bool isHtml READ isHtml) Q_PROPERTY(bool isHidden READ isHidden) Q_PROPERTY(QString attachmentIndex READ attachmentIndex CONSTANT) Q_PROPERTY(QString link READ attachmentLink CONSTANT) public: typedef QSharedPointer Ptr; MessagePart(ObjectTreeParser *otp, const QString &text); ~MessagePart(); void setParentPart(MessagePart *parentPart); MessagePart *parentPart() const; virtual QString text() const; void setText(const QString &text); virtual QString plaintextContent() const; virtual QString htmlContent() const; /** The KMime::Content* node that's represented by this part. * Can be @c nullptr, e.g. for sub-parts of an inline signed body part. */ KMime::Content *content() const; void setContent(KMime::Content *node); /** The KMime::Content* node that's the source of this part. * This is not necessarily the same as content(), for example for * broken-up multipart nodes. */ KMime::Content *attachmentContent() const; void setAttachmentContent(KMime::Content *node); bool isAttachment() const; /** @see KMime::Content::index() */ QString attachmentIndex() const; /** @see NodeHelper::asHREF */ QString attachmentLink() const; /** Returns a string respresentation of an URL that can be used * to invoke a BodyPartURLHandler for this body part. */ QString makeLink(const QString &path) const; void setIsRoot(bool root); bool isRoot() const; virtual bool isHtml() const; virtual bool isHidden() const; PartMetaData *partMetaData() const; Interface::BodyPartMemento *memento() const; void setMemento(Interface::BodyPartMemento *memento); /* only a function that should be removed if the refactoring is over */ virtual void fix() const; void appendSubPart(const MessagePart::Ptr &messagePart); const QVector &subParts() const; bool hasSubParts() const; + void clearSubParts(); Interface::ObjectTreeSource *source() const; NodeHelper* nodeHelper() const; protected: void parseInternal(KMime::Content *node, bool onlyOneMimePart); QString renderInternalText() const; ObjectTreeParser *mOtp = nullptr; private: std::unique_ptr d; }; class MIMETREEPARSER_EXPORT MimeMessagePart : public MessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; MimeMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool onlyOneMimePart); virtual ~MimeMessagePart(); QString text() const override; QString plaintextContent() const override; QString htmlContent() const override; private: bool mOnlyOneMimePart; friend class AlternativeMessagePart; }; class MIMETREEPARSER_EXPORT MessagePartList : public MessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; MessagePartList(MimeTreeParser::ObjectTreeParser *otp); virtual ~MessagePartList(); QString text() const override; QString plaintextContent() const override; QString htmlContent() const override; }; enum IconType { NoIcon = 0, IconExternal, IconInline }; class MIMETREEPARSER_EXPORT TextMessagePart : public MessagePartList { Q_OBJECT Q_PROPERTY(bool showTextFrame READ showTextFrame CONSTANT) Q_PROPERTY(bool showLink READ showLink CONSTANT) Q_PROPERTY(QString label READ label CONSTANT) Q_PROPERTY(QString comment READ comment CONSTANT) public: typedef QSharedPointer Ptr; TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool decryptMessage); virtual ~TextMessagePart(); KMMsgSignatureState signatureState() const; KMMsgEncryptionState encryptionState() const; bool decryptMessage() const; bool isHidden() const override; bool showLink() const; bool showTextFrame() const; void setShowTextFrame(bool showFrame); /** The attachment filename, or the closest approximation thereof we have. */ QString label() const; /** A description of this attachment, if provided. */ QString comment() const; /** Temporary file containing the part content. */ QString temporaryFilePath() const; private: void parseContent(); KMMsgSignatureState mSignatureState; KMMsgEncryptionState mEncryptionState; bool mDrawFrame; bool mDecryptMessage; bool mIsHidden; friend class ObjectTreeParser; }; class MIMETREEPARSER_EXPORT AttachmentMessagePart : public TextMessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; AttachmentMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool decryptMessage); virtual ~AttachmentMessagePart(); IconType asIcon() const; bool neverDisplayInline() const; void setNeverDisplayInline(bool displayInline); bool isImage() const; void setIsImage(bool image); bool isHidden() const override; private: bool mIsImage; bool mNeverDisplayInline; }; class MIMETREEPARSER_EXPORT HtmlMessagePart : public MessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; HtmlMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, MimeTreeParser::Interface::ObjectTreeSource *source); virtual ~HtmlMessagePart(); QString text() const override; void fix() const override; bool isHtml() const override; private: Interface::ObjectTreeSource *mSource; QString mBodyHTML; QByteArray mCharset; friend class DefaultRendererPrivate; }; class MIMETREEPARSER_EXPORT AlternativeMessagePart : public MessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; AlternativeMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, Util::HtmlMode preferredMode); virtual ~AlternativeMessagePart(); QString text() const override; Util::HtmlMode preferredMode() const; bool isHtml() const override; QString plaintextContent() const override; QString htmlContent() const override; QList availableModes(); void fix() const override; private: Util::HtmlMode mPreferredMode; QMap mChildNodes; QMap mChildParts; friend class DefaultRendererPrivate; friend class ObjectTreeParser; friend class MultiPartAlternativeBodyPartFormatter; }; class MIMETREEPARSER_EXPORT CertMessagePart : public MessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; CertMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, const QGpgME::Protocol *cryptoProto, bool autoImport); virtual ~CertMessagePart(); QString text() const override; private: bool mAutoImport; GpgME::ImportResult mImportResult; const QGpgME::Protocol *mCryptoProto; friend class DefaultRendererPrivate; }; class MIMETREEPARSER_EXPORT EncapsulatedRfc822MessagePart : public MessagePart { Q_OBJECT public: typedef QSharedPointer Ptr; EncapsulatedRfc822MessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, const KMime::Message::Ptr &message); virtual ~EncapsulatedRfc822MessagePart(); QString text() const override; void fix() const override; private: const KMime::Message::Ptr mMessage; friend class DefaultRendererPrivate; }; class MIMETREEPARSER_EXPORT EncryptedMessagePart : public MessagePart { Q_OBJECT Q_PROPERTY(bool decryptMessage READ decryptMessage WRITE setDecryptMessage) Q_PROPERTY(bool isEncrypted READ isEncrypted) Q_PROPERTY(bool passphraseError READ passphraseError) public: typedef QSharedPointer Ptr; EncryptedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node); virtual ~EncryptedMessagePart(); QString text() const override; void setDecryptMessage(bool decrypt); bool decryptMessage() const; void setIsEncrypted(bool encrypted); bool isEncrypted() const; bool isDecryptable() const; bool passphraseError() const; void startDecryption(const QByteArray &text, const QTextCodec *aCodec); void startDecryption(KMime::Content *data = nullptr); QByteArray mDecryptedData; QString plaintextContent() const override; QString htmlContent() const override; private: /** Handles the dectyptioon of a given content * returns true if the decryption was successfull * if used in async mode, check if mMetaData.inProgress is true, it inicates a running decryption process. */ bool okDecryptMIME(KMime::Content &data); protected: bool mPassphraseError; bool mNoSecKey; const QGpgME::Protocol *mCryptoProto; QString mFromAddress; bool mDecryptMessage; QByteArray mVerifiedText; std::vector> mDecryptRecipients; friend class DefaultRendererPrivate; + friend class EncryptedBodyPartFormatter; }; class MIMETREEPARSER_EXPORT SignedMessagePart : public MessagePart { Q_OBJECT Q_PROPERTY(bool isSigned READ isSigned) public: typedef QSharedPointer Ptr; SignedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node); virtual ~SignedMessagePart(); void setIsSigned(bool isSigned); bool isSigned() const; void startVerification(const QByteArray &text, const QTextCodec *aCodec); void startVerificationDetached(const QByteArray &text, KMime::Content *textNode, const QByteArray &signature); QByteArray mDecryptedData; std::vector mSignatures; QString plaintextContent() const override; QString htmlContent() const override; private: /** Handles the verification of data * If signature is empty it is handled as inline signature otherwise as detached signature mode. * Returns true if the verfication was successfull and the block is signed. * If used in async mode, check if mMetaData.inProgress is true, it inicates a running verification process. */ bool okVerify(const QByteArray &data, const QByteArray &signature, KMime::Content *textNode); void sigStatusToMetaData(); void setVerificationResult(const CryptoBodyPartMemento *m, KMime::Content *textNode); protected: const QGpgME::Protocol *mCryptoProto; QString mFromAddress; QByteArray mVerifiedText; friend EncryptedMessagePart; friend class DefaultRendererPrivate; }; } #endif //__MIMETREEPARSER_MESSAGEPART_H__ diff --git a/mimetreeparser/src/nodehelper.cpp b/mimetreeparser/src/nodehelper.cpp index cbdcbff9..52f1d1fe 100644 --- a/mimetreeparser/src/nodehelper.cpp +++ b/mimetreeparser/src/nodehelper.cpp @@ -1,1055 +1,1061 @@ /* Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net Copyright (c) 2009 Andras Mantia This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "nodehelper.h" #include "mimetreeparser_debug.h" #include "partmetadata.h" #include "interfaces/bodypart.h" #include "temporaryfile/attachmenttemporaryfilesdirs.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace MimeTreeParser { QStringList replySubjPrefixes(QStringList() << QStringLiteral("Re\\s*:") << QStringLiteral("Re\\[\\d+\\]:") << QStringLiteral("Re\\d+:")); QStringList forwardSubjPrefixes(QStringList() << QStringLiteral("Fwd:") << QStringLiteral("FW:")); NodeHelper::NodeHelper() : mAttachmentFilesDir(new AttachmentTemporaryFilesDirs()) { //TODO(Andras) add methods to modify these prefixes mLocalCodec = QTextCodec::codecForLocale(); // In the case of Japan. Japanese locale name is "eucjp" but // The Japanese mail systems normally used "iso-2022-jp" of locale name. // We want to change locale name from eucjp to iso-2022-jp at KMail only. // (Introduction to i18n, 6.6 Limit of Locale technology): // EUC-JP is the de-facto standard for UNIX systems, ISO 2022-JP // is the standard for Internet, and Shift-JIS is the encoding // for Windows and Macintosh. if (mLocalCodec) { const QByteArray codecNameLower = mLocalCodec->name().toLower(); if (codecNameLower == "eucjp" #if defined Q_OS_WIN || defined Q_OS_MACX || codecNameLower == "shift-jis" // OK? #endif ) { mLocalCodec = QTextCodec::codecForName("jis7"); // QTextCodec *cdc = QTextCodec::codecForName("jis7"); // QTextCodec::setCodecForLocale(cdc); // KLocale::global()->setEncoding(cdc->mibEnum()); } } } NodeHelper::~NodeHelper() { if (mAttachmentFilesDir) { mAttachmentFilesDir->forceCleanTempFiles(); delete mAttachmentFilesDir; mAttachmentFilesDir = nullptr; } clear(); } void NodeHelper::setNodeProcessed(KMime::Content *node, bool recurse) { if (!node) { return; } mProcessedNodes.append(node); qCDebug(MIMETREEPARSER_LOG) << "Node processed: " << node->index().toString() << node->contentType()->as7BitString(); //<< " decodedContent" << node->decodedContent(); if (recurse) { const auto contents = node->contents(); for (KMime::Content *c : contents) { setNodeProcessed(c, true); } } } void NodeHelper::setNodeUnprocessed(KMime::Content *node, bool recurse) { if (!node) { return; } mProcessedNodes.removeAll(node); //avoid double addition of extra nodes, eg. encrypted attachments const QMap >::iterator it = mExtraContents.find(node); if (it != mExtraContents.end()) { Q_FOREACH (KMime::Content *c, it.value()) { KMime::Content *p = c->parent(); if (p) { p->removeContent(c); } } qDeleteAll(it.value()); qCDebug(MIMETREEPARSER_LOG) << "mExtraContents deleted for" << it.key(); mExtraContents.erase(it); } qCDebug(MIMETREEPARSER_LOG) << "Node UNprocessed: " << node; if (recurse) { const auto contents = node->contents(); for (KMime::Content *c : contents) { setNodeUnprocessed(c, true); } } } bool NodeHelper::nodeProcessed(KMime::Content *node) const { if (!node) { return true; } return mProcessedNodes.contains(node); } static void clearBodyPartMemento(QMap &bodyPartMementoMap) { for (QMap::iterator it = bodyPartMementoMap.begin(), end = bodyPartMementoMap.end(); it != end; ++it) { Interface::BodyPartMemento *memento = it.value(); memento->detach(); delete memento; } bodyPartMementoMap.clear(); } void NodeHelper::clear() { mProcessedNodes.clear(); mEncryptionState.clear(); mSignatureState.clear(); mOverrideCodecs.clear(); std::for_each(mBodyPartMementoMap.begin(), mBodyPartMementoMap.end(), &clearBodyPartMemento); mBodyPartMementoMap.clear(); QMap >::ConstIterator end(mExtraContents.constEnd()); for (QMap >::ConstIterator it = mExtraContents.constBegin(); it != end; ++it) { Q_FOREACH (KMime::Content *c, it.value()) { KMime::Content *p = c->parent(); if (p) { p->removeContent(c); } } qDeleteAll(it.value()); qCDebug(MIMETREEPARSER_LOG) << "mExtraContents deleted for" << it.key(); } mExtraContents.clear(); mDisplayEmbeddedNodes.clear(); mDisplayHiddenNodes.clear(); } void NodeHelper::setEncryptionState(const KMime::Content *node, const KMMsgEncryptionState state) { mEncryptionState[node] = state; } KMMsgEncryptionState NodeHelper::encryptionState(const KMime::Content *node) const { return mEncryptionState.value(node, KMMsgNotEncrypted); } void NodeHelper::setSignatureState(const KMime::Content *node, const KMMsgSignatureState state) { mSignatureState[node] = state; } KMMsgSignatureState NodeHelper::signatureState(const KMime::Content *node) const { return mSignatureState.value(node, KMMsgNotSigned); } PartMetaData NodeHelper::partMetaData(KMime::Content *node) { return mPartMetaDatas.value(node, PartMetaData()); } void NodeHelper::setPartMetaData(KMime::Content *node, const PartMetaData &metaData) { mPartMetaDatas.insert(node, metaData); } QString NodeHelper::writeNodeToTempFile(KMime::Content *node) { // If the message part is already written to a file, no point in doing it again. // This function is called twice actually, once from the rendering of the attachment // in the body and once for the header. QUrl existingFileName = tempFileUrlFromNode(node); if (!existingFileName.isEmpty()) { return existingFileName.toLocalFile(); } QString fname = createTempDir(persistentIndex(node)); if (fname.isEmpty()) { return QString(); } QString fileName = NodeHelper::fileName(node); // strip off a leading path int slashPos = fileName.lastIndexOf(QLatin1Char('/')); if (-1 != slashPos) { fileName = fileName.mid(slashPos + 1); } if (fileName.isEmpty()) { fileName = QStringLiteral("unnamed"); } fname += QLatin1Char('/') + fileName; qCDebug(MIMETREEPARSER_LOG) << "Create temp file: " << fname; QByteArray data = node->decodedContent(); if (node->contentType()->isText() && !data.isEmpty()) { // convert CRLF to LF before writing text attachments to disk data = KMime::CRLFtoLF(data); } QFile f(fname); if (!f.open(QIODevice::ReadWrite)) { qCWarning(MIMETREEPARSER_LOG) << "Failed to write note to file:" << f.errorString(); return QString(); } f.write(data); mAttachmentFilesDir->addTempFile(fname); // make file read-only so that nobody gets the impression that he might // edit attached files (cf. bug #52813) f.setPermissions(QFileDevice::ReadUser); f.close(); return fname; } QUrl NodeHelper::tempFileUrlFromNode(const KMime::Content *node) { if (!node) { return QUrl(); } const QString index = persistentIndex(node); foreach (const QString &path, mAttachmentFilesDir->temporaryFiles()) { const int right = path.lastIndexOf(QLatin1Char('/')); int left = path.lastIndexOf(QLatin1String(".index."), right); if (left != -1) { left += 7; } QStringRef storedIndex(&path, left, right - left); if (left != -1 && storedIndex == index) { return QUrl::fromLocalFile(path); } } return QUrl(); } QString NodeHelper::createTempDir(const QString ¶m) { QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + QLatin1String("/messageviewer_XXXXXX") + QLatin1String(".index.") + param); tempFile->open(); const QString fname = tempFile->fileName(); delete tempFile; QFile fFile(fname); if (!(fFile.permissions() & QFileDevice::WriteUser)) { // Not there or not writable if (!QDir().mkpath(fname) || !fFile.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser)) { return QString(); //failed create } } Q_ASSERT(!fname.isNull()); mAttachmentFilesDir->addTempDir(fname); return fname; } void NodeHelper::forceCleanTempFiles() { mAttachmentFilesDir->forceCleanTempFiles(); delete mAttachmentFilesDir; mAttachmentFilesDir = nullptr; } void NodeHelper::removeTempFiles() { //Don't delete it it will delete in class mAttachmentFilesDir->removeTempFiles(); mAttachmentFilesDir = new AttachmentTemporaryFilesDirs(); } void NodeHelper::addTempFile(const QString &file) { mAttachmentFilesDir->addTempFile(file); } bool NodeHelper::isInEncapsulatedMessage(KMime::Content *node) { const KMime::Content *const topLevel = node->topLevel(); const KMime::Content *cur = node; while (cur && cur != topLevel) { const bool parentIsMessage = cur->parent() && cur->parent()->contentType(false) && cur->parent()->contentType()->mimeType().toLower() == "message/rfc822"; if (parentIsMessage && cur->parent() != topLevel) { return true; } cur = cur->parent(); } return false; } QByteArray NodeHelper::charset(KMime::Content *node) { if (node->contentType(false)) { return node->contentType(false)->charset(); } else { return node->defaultCharset(); } } KMMsgEncryptionState NodeHelper::overallEncryptionState(KMime::Content *node) const { KMMsgEncryptionState myState = KMMsgEncryptionStateUnknown; if (!node) { return myState; } KMime::Content *parent = node->parent(); auto contents = parent ? parent->contents() : KMime::Content::List(); if (contents.isEmpty()) { contents.append(node); } int i = contents.indexOf(const_cast(node)); if (i < 0) { return myState; } for (; i < contents.size(); ++i) { auto next = contents.at(i); KMMsgEncryptionState otherState = encryptionState(next); // NOTE: children are tested ONLY when parent is not encrypted if (otherState == KMMsgNotEncrypted && !next->contents().isEmpty()) { otherState = overallEncryptionState(next->contents().at(0)); } if (otherState == KMMsgNotEncrypted && !extraContents(next).isEmpty()) { otherState = overallEncryptionState(extraContents(next).at(0)); } if (next == node) { myState = otherState; } switch (otherState) { case KMMsgEncryptionStateUnknown: break; case KMMsgNotEncrypted: if (myState == KMMsgFullyEncrypted) { myState = KMMsgPartiallyEncrypted; } else if (myState != KMMsgPartiallyEncrypted) { myState = KMMsgNotEncrypted; } break; case KMMsgPartiallyEncrypted: myState = KMMsgPartiallyEncrypted; break; case KMMsgFullyEncrypted: if (myState != KMMsgFullyEncrypted) { myState = KMMsgPartiallyEncrypted; } break; case KMMsgEncryptionProblematic: break; } } qCDebug(MIMETREEPARSER_LOG) << "\n\n KMMsgEncryptionState:" << myState; return myState; } KMMsgSignatureState NodeHelper::overallSignatureState(KMime::Content *node) const { KMMsgSignatureState myState = KMMsgSignatureStateUnknown; if (!node) { return myState; } KMime::Content *parent = node->parent(); auto contents = parent ? parent->contents() : KMime::Content::List(); if (contents.isEmpty()) { contents.append(node); } int i = contents.indexOf(const_cast(node)); if (i < 0) { //Be safe return myState; } for (; i < contents.size(); ++i) { auto next = contents.at(i); KMMsgSignatureState otherState = signatureState(next); // NOTE: children are tested ONLY when parent is not encrypted if (otherState == KMMsgNotSigned && !next->contents().isEmpty()) { otherState = overallSignatureState(next->contents().at(0)); } if (otherState == KMMsgNotSigned && !extraContents(next).isEmpty()) { otherState = overallSignatureState(extraContents(next).at(0)); } if (next == node) { myState = otherState; } switch (otherState) { case KMMsgSignatureStateUnknown: break; case KMMsgNotSigned: if (myState == KMMsgFullySigned) { myState = KMMsgPartiallySigned; } else if (myState != KMMsgPartiallySigned) { myState = KMMsgNotSigned; } break; case KMMsgPartiallySigned: myState = KMMsgPartiallySigned; break; case KMMsgFullySigned: if (myState != KMMsgFullySigned) { myState = KMMsgPartiallySigned; } break; case KMMsgSignatureProblematic: break; } } qCDebug(MIMETREEPARSER_LOG) << "\n\n KMMsgSignatureState:" << myState; return myState; } void NodeHelper::magicSetType(KMime::Content *node, bool aAutoDecode) { const QByteArray body = (aAutoDecode) ? node->decodedContent() : node->body(); QMimeDatabase db; QMimeType mime = db.mimeTypeForData(body); QString mimetype = mime.name(); node->contentType()->setMimeType(mimetype.toLatin1()); } // static QString NodeHelper::replacePrefixes(const QString &str, const QStringList &prefixRegExps, bool replace, const QString &newPrefix) { bool recognized = false; // construct a big regexp that // 1. is anchored to the beginning of str (sans whitespace) // 2. matches at least one of the part regexps in prefixRegExps QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*") .arg(prefixRegExps.join(QStringLiteral(")|(?:"))); QRegExp rx(bigRegExp, Qt::CaseInsensitive); if (!rx.isValid()) { qCWarning(MIMETREEPARSER_LOG) << "bigRegExp = \"" << bigRegExp << "\"\n" << "prefix regexp is invalid!"; // try good ole Re/Fwd: recognized = str.startsWith(newPrefix); } else { // valid rx QString tmp = str; if (rx.indexIn(tmp) == 0) { recognized = true; if (replace) { return tmp.replace(0, rx.matchedLength(), newPrefix + QLatin1Char(' ')); } } } if (!recognized) { return newPrefix + QLatin1Char(' ') + str; } else { return str; } } QString NodeHelper::cleanSubject(KMime::Message *message) { return cleanSubject(message, replySubjPrefixes + forwardSubjPrefixes, true, QString()).trimmed(); } QString NodeHelper::cleanSubject(KMime::Message *message, const QStringList &prefixRegExps, bool replace, const QString &newPrefix) { QString cleanStr; if (message) { cleanStr = NodeHelper::replacePrefixes( message->subject()->asUnicodeString(), prefixRegExps, replace, newPrefix); } return cleanStr; } void NodeHelper::setOverrideCodec(KMime::Content *node, const QTextCodec *codec) { if (!node) { return; } mOverrideCodecs[node] = codec; } const QTextCodec *NodeHelper::codec(KMime::Content *node) { if (!node) { return mLocalCodec; } const QTextCodec *c = mOverrideCodecs.value(node, nullptr); if (!c) { // no override-codec set for this message, try the CT charset parameter: QByteArray charset = node->contentType()->charset(); // utf-8 is a superset of us-ascii, so we don't loose anything, if we it insead // utf-8 is nowadays that widely, that it is a good guess to use it to fix issus with broken clients. if (charset.toLower() == "us-ascii") { charset = "utf-8"; } c = codecForName(charset); } if (!c) { // no charset means us-ascii (RFC 2045), so using local encoding should // be okay c = mLocalCodec; } return c; } const QTextCodec *NodeHelper::codecForName(const QByteArray &_str) { if (_str.isEmpty()) { return nullptr; } QByteArray codec = _str.toLower(); return KCharsets::charsets()->codecForName(QLatin1String(codec)); } QString NodeHelper::fileName(const KMime::Content *node) { QString name = const_cast(node)->contentDisposition()->filename(); if (name.isEmpty()) { name = const_cast(node)->contentType()->name(); } name = name.trimmed(); return name; } //FIXME(Andras) review it (by Marc?) to see if I got it right. This is supposed to be the partNode::internalBodyPartMemento replacement Interface::BodyPartMemento *NodeHelper::bodyPartMemento(KMime::Content *node, const QByteArray &which) const { const QMap< QString, QMap >::const_iterator nit = mBodyPartMementoMap.find(persistentIndex(node)); if (nit == mBodyPartMementoMap.end()) { return nullptr; } const QMap::const_iterator it = nit->find(which.toLower()); return it != nit->end() ? it.value() : nullptr; } //FIXME(Andras) review it (by Marc?) to see if I got it right. This is supposed to be the partNode::internalSetBodyPartMemento replacement void NodeHelper::setBodyPartMemento(KMime::Content *node, const QByteArray &which, Interface::BodyPartMemento *memento) { QMap &mementos = mBodyPartMementoMap[persistentIndex(node)]; const QByteArray whichLower = which.toLower(); const QMap::iterator it = mementos.lowerBound(whichLower); if (it != mementos.end() && it.key() == whichLower) { delete it.value(); if (memento) { it.value() = memento; } else { mementos.erase(it); } } else { mementos.insert(whichLower, memento); } } bool NodeHelper::isNodeDisplayedEmbedded(KMime::Content *node) const { qCDebug(MIMETREEPARSER_LOG) << "IS NODE: " << mDisplayEmbeddedNodes.contains(node); return mDisplayEmbeddedNodes.contains(node); } void NodeHelper::setNodeDisplayedEmbedded(KMime::Content *node, bool displayedEmbedded) { qCDebug(MIMETREEPARSER_LOG) << "SET NODE: " << node << displayedEmbedded; if (displayedEmbedded) { mDisplayEmbeddedNodes.insert(node); } else { mDisplayEmbeddedNodes.remove(node); } } bool NodeHelper::isNodeDisplayedHidden(KMime::Content *node) const { return mDisplayHiddenNodes.contains(node); } void NodeHelper::setNodeDisplayedHidden(KMime::Content *node, bool displayedHidden) { if (displayedHidden) { mDisplayHiddenNodes.insert(node); } else { mDisplayEmbeddedNodes.remove(node); } } /*! Creates a persistent index string that bridges the gap between the permanent nodes and the temporary ones. Used internally for robust indexing. */ QString NodeHelper::persistentIndex(const KMime::Content *node) const { if (!node) { return QString(); } QString indexStr = node->index().toString(); if (indexStr.isEmpty()) { QMapIterator > it(mExtraContents); while (it.hasNext()) { it.next(); const auto &extraNodes = it.value(); for (int i = 0; i < extraNodes.size(); i++) { if (extraNodes[i] == node) { indexStr = QString::fromLatin1("e%1").arg(i); const QString parentIndex = persistentIndex(it.key()); if (!parentIndex.isEmpty()) { indexStr = QString::fromLatin1("%1:%2").arg(parentIndex, indexStr); } return indexStr; } } } } else { const KMime::Content *const topLevel = node->topLevel(); //if the node is an extra node, prepend the index of the extra node to the url QMapIterator > it(mExtraContents); while (it.hasNext()) { it.next(); const QList &extraNodes = extraContents(it.key()); for (int i = 0; i < extraNodes.size(); ++i) { KMime::Content *const extraNode = extraNodes[i]; if (topLevel == extraNode) { indexStr.prepend(QStringLiteral("e%1:").arg(i)); const QString parentIndex = persistentIndex(it.key()); if (!parentIndex.isEmpty()) { indexStr = QStringLiteral("%1:%2").arg(parentIndex, indexStr); } return indexStr; } } } } return indexStr; } KMime::Content *NodeHelper::contentFromIndex(KMime::Content *node, const QString &persistentIndex) const { KMime::Content *c = node->topLevel(); if (c) { const QStringList pathParts = persistentIndex.split(QLatin1Char(':'), QString::SkipEmptyParts); const int pathPartsSize(pathParts.size()); for (int i = 0; i < pathPartsSize; ++i) { const QString &path = pathParts[i]; if (path.startsWith(QLatin1Char('e'))) { const QList &extraParts = mExtraContents.value(c); const int idx = path.midRef(1, -1).toInt(); c = (idx < extraParts.size()) ? extraParts[idx] : nullptr; } else { c = c->content(KMime::ContentIndex(path)); } if (!c) { break; } } } return c; } QString NodeHelper::asHREF(const KMime::Content *node, const QString &place) const { return QStringLiteral("attachment:%1?place=%2").arg(persistentIndex(node), place); } KMime::Content *NodeHelper::fromHREF(const KMime::Message::Ptr &mMessage, const QUrl &url) const { if (url.isEmpty()) { return mMessage.data(); } if (!url.isLocalFile()) { return contentFromIndex(mMessage.data(), url.adjusted(QUrl::StripTrailingSlash).path()); } else { const QString path = url.toLocalFile(); // extract from //qttestn28554.index.2.3:0:2/unnamed -> "2.3:0:2" // start of the index is something that is not a number followed by a dot: \D. // index is only made of numbers,"." and ":": ([0-9.:]+) // index is the last part of the folder name: / const QRegExp rIndex(QStringLiteral("\\D\\.([e0-9.:]+)/")); //search the occurence at most at the end if (rIndex.lastIndexIn(path) != -1) { return contentFromIndex(mMessage.data(), rIndex.cap(1)); } return mMessage.data(); } } QString NodeHelper::fixEncoding(const QString &encoding) { QString returnEncoding = encoding; // According to http://www.iana.org/assignments/character-sets, uppercase is // preferred in MIME headers const QString returnEncodingToUpper = returnEncoding.toUpper(); if (returnEncodingToUpper.contains(QStringLiteral("ISO "))) { returnEncoding = returnEncodingToUpper; returnEncoding.replace(QLatin1String("ISO "), QStringLiteral("ISO-")); } return returnEncoding; } //----------------------------------------------------------------------------- QString NodeHelper::encodingForName(const QString &descriptiveName) { QString encoding = KCharsets::charsets()->encodingForName(descriptiveName); return NodeHelper::fixEncoding(encoding); } QStringList NodeHelper::supportedEncodings(bool usAscii) { QStringList encodingNames = KCharsets::charsets()->availableEncodingNames(); QStringList encodings; QMap mimeNames; QStringList::ConstIterator constEnd(encodingNames.constEnd()); for (QStringList::ConstIterator it = encodingNames.constBegin(); it != constEnd; ++it) { QTextCodec *codec = KCharsets::charsets()->codecForName(*it); QString mimeName = (codec) ? QString::fromLatin1(codec->name()).toLower() : (*it); if (!mimeNames.contains(mimeName)) { encodings.append(KCharsets::charsets()->descriptionForEncoding(*it)); mimeNames.insert(mimeName, true); } } encodings.sort(); if (usAscii) { encodings.prepend(KCharsets::charsets()->descriptionForEncoding(QStringLiteral("us-ascii"))); } return encodings; } QString NodeHelper::fromAsString(KMime::Content *node) const { if (auto topLevel = dynamic_cast(node->topLevel())) { return topLevel->from()->asUnicodeString(); } else { auto realNode = std::find_if(mExtraContents.cbegin(), mExtraContents.cend(), [node](const QList &nodes) { return nodes.contains(node); }); if (realNode != mExtraContents.cend()) { return fromAsString(realNode.key()); } } return QString(); } void NodeHelper::attachExtraContent(KMime::Content *topLevelNode, KMime::Content *content) { qCDebug(MIMETREEPARSER_LOG) << "mExtraContents added for" << topLevelNode << " extra content: " << content; mExtraContents[topLevelNode].append(content); } +void NodeHelper::cleanExtraContent(KMime::Content *topLevelNode) +{ + qCDebug(MIMETREEPARSER_LOG) << "remove all extraContents for" << topLevelNode; + mExtraContents[topLevelNode].clear(); +} + QList< KMime::Content * > NodeHelper::extraContents(KMime::Content *topLevelnode) const { return mExtraContents.value(topLevelnode); } void NodeHelper::mergeExtraNodes(KMime::Content *node) { if (!node) { return; } const QList extraNodes = extraContents(node); for (KMime::Content *extra : extraNodes) { if (node->bodyIsMessage()) { qCWarning(MIMETREEPARSER_LOG) << "Asked to attach extra content to a kmime::message, this does not make sense. Attaching to:" << node <encodedContent() << "\n====== with =======\n" << extra << extra->encodedContent(); continue; } KMime::Content *c = new KMime::Content(node); c->setContent(extra->encodedContent()); c->parse(); node->addContent(c); } Q_FOREACH (KMime::Content *child, node->contents()) { mergeExtraNodes(child); } } void NodeHelper::cleanFromExtraNodes(KMime::Content *node) { if (!node) { return; } const QList extraNodes = extraContents(node); for (KMime::Content *extra : extraNodes) { QByteArray s = extra->encodedContent(); const auto children = node->contents(); for (KMime::Content *c : children) { if (c->encodedContent() == s) { node->removeContent(c); } } } Q_FOREACH (KMime::Content *child, node->contents()) { cleanFromExtraNodes(child); } } KMime::Message *NodeHelper::messageWithExtraContent(KMime::Content *topLevelNode) { /*The merge is done in several steps: 1) merge the extra nodes into topLevelNode 2) copy the modified (merged) node tree into a new node tree 3) restore the original node tree in topLevelNode by removing the extra nodes from it The reason is that extra nodes are assigned by pointer value to the nodes in the original tree. */ if (!topLevelNode) { return nullptr; } mergeExtraNodes(topLevelNode); KMime::Message *m = new KMime::Message; m->setContent(topLevelNode->encodedContent()); m->parse(); cleanFromExtraNodes(topLevelNode); // qCDebug(MIMETREEPARSER_LOG) << "MESSAGE WITH EXTRA: " << m->encodedContent(); // qCDebug(MIMETREEPARSER_LOG) << "MESSAGE WITHOUT EXTRA: " << topLevelNode->encodedContent(); return m; } KMime::Content *NodeHelper::decryptedNodeForContent(KMime::Content *content) const { const QList xc = extraContents(content); if (!xc.empty()) { if (xc.size() == 1) { return xc.front(); } else { qCWarning(MIMETREEPARSER_LOG) << "WTF, encrypted node has multiple extra contents?"; } } return nullptr; } bool NodeHelper::unencryptedMessage_helper(KMime::Content *node, QByteArray &resultingData, bool addHeaders, int recursionLevel) { bool returnValue = false; if (node) { KMime::Content *curNode = node; KMime::Content *decryptedNode = nullptr; const QByteArray type = node->contentType(false) ? QByteArray(node->contentType()->mediaType()).toLower() : "text"; const QByteArray subType = node->contentType(false) ? node->contentType()->subType().toLower() : "plain"; const bool isMultipart = node->contentType(false) && node->contentType()->isMultipart(); bool isSignature = false; qCDebug(MIMETREEPARSER_LOG) << "(" << recursionLevel << ") Looking at" << type << "/" << subType; if (isMultipart) { if (subType == "signed") { isSignature = true; } else if (subType == "encrypted") { decryptedNode = decryptedNodeForContent(curNode); } } else if (type == "application") { if (subType == "octet-stream") { decryptedNode = decryptedNodeForContent(curNode); } else if (subType == "pkcs7-signature") { isSignature = true; } else if (subType == "pkcs7-mime") { // note: subtype pkcs7-mime can also be signed // and we do NOT want to remove the signature! if (encryptionState(curNode) != KMMsgNotEncrypted) { decryptedNode = decryptedNodeForContent(curNode); } } } if (decryptedNode) { qCDebug(MIMETREEPARSER_LOG) << "Current node has an associated decrypted node, adding a modified header " "and then processing the children."; Q_ASSERT(addHeaders); KMime::Content headers; headers.setHead(curNode->head()); headers.parse(); if (decryptedNode->contentType(false)) { headers.contentType()->from7BitString(decryptedNode->contentType()->as7BitString(false)); } else { headers.removeHeader(); } if (decryptedNode->contentTransferEncoding(false)) { headers.contentTransferEncoding()->from7BitString(decryptedNode->contentTransferEncoding()->as7BitString(false)); } else { headers.removeHeader(); } if (decryptedNode->contentDisposition(false)) { headers.contentDisposition()->from7BitString(decryptedNode->contentDisposition()->as7BitString(false)); } else { headers.removeHeader(); } if (decryptedNode->contentDescription(false)) { headers.contentDescription()->from7BitString(decryptedNode->contentDescription()->as7BitString(false)); } else { headers.removeHeader(); } headers.assemble(); resultingData += headers.head() + '\n'; unencryptedMessage_helper(decryptedNode, resultingData, false, recursionLevel + 1); returnValue = true; } else if (isSignature) { qCDebug(MIMETREEPARSER_LOG) << "Current node is a signature, adding it as-is."; // We can't change the nodes under the signature, as that would invalidate it. Add the signature // and its child as-is if (addHeaders) { resultingData += curNode->head() + '\n'; } resultingData += curNode->encodedBody(); returnValue = false; } else if (isMultipart) { qCDebug(MIMETREEPARSER_LOG) << "Current node is a multipart node, adding its header and then processing all children."; // Normal multipart node, add the header and all of its children bool somethingChanged = false; if (addHeaders) { resultingData += curNode->head() + '\n'; } const QByteArray boundary = curNode->contentType()->boundary(); foreach (KMime::Content *child, curNode->contents()) { resultingData += "\n--" + boundary + '\n'; const bool changed = unencryptedMessage_helper(child, resultingData, true, recursionLevel + 1); if (changed) { somethingChanged = true; } } resultingData += "\n--" + boundary + "--\n\n"; returnValue = somethingChanged; } else if (curNode->bodyIsMessage()) { qCDebug(MIMETREEPARSER_LOG) << "Current node is a message, adding the header and then processing the child."; if (addHeaders) { resultingData += curNode->head() + '\n'; } returnValue = unencryptedMessage_helper(curNode->bodyAsMessage().data(), resultingData, true, recursionLevel + 1); } else { qCDebug(MIMETREEPARSER_LOG) << "Current node is an ordinary leaf node, adding it as-is."; if (addHeaders) { resultingData += curNode->head() + '\n'; } resultingData += curNode->body(); returnValue = false; } } qCDebug(MIMETREEPARSER_LOG) << "(" << recursionLevel << ") done."; return returnValue; } KMime::Message::Ptr NodeHelper::unencryptedMessage(const KMime::Message::Ptr &originalMessage) { QByteArray resultingData; const bool messageChanged = unencryptedMessage_helper(originalMessage.data(), resultingData, true); if (messageChanged) { #if 0 qCDebug(MIMETREEPARSER_LOG) << "Resulting data is:" << resultingData; QFile bla("stripped.mbox"); bla.open(QIODevice::WriteOnly); bla.write(resultingData); bla.close(); #endif KMime::Message::Ptr newMessage(new KMime::Message); newMessage->setContent(resultingData); newMessage->parse(); return newMessage; } else { return KMime::Message::Ptr(); } } QVector NodeHelper::attachmentsOfExtraContents() const { QVector result; for (auto it = mExtraContents.begin(), end = mExtraContents.end(); it != end; ++it) { foreach (auto content, it.value()) { if (KMime::isAttachment(content)) { result.push_back(content); } else { result += content->attachments(); } } } return result; } } diff --git a/mimetreeparser/src/nodehelper.h b/mimetreeparser/src/nodehelper.h index 1906d659..83014f27 100644 --- a/mimetreeparser/src/nodehelper.h +++ b/mimetreeparser/src/nodehelper.h @@ -1,281 +1,283 @@ /* Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net Copyright (c) 2009 Andras Mantia This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef __MIMETREEPARSER_NODEHELPER_H__ #define __MIMETREEPARSER_NODEHELPER_H__ #include "mimetreeparser_export.h" #include "mimetreeparser/partmetadata.h" #include "mimetreeparser/enums.h" #include #include #include #include class QUrl; class QTextCodec; namespace MimeTreeParser { class AttachmentTemporaryFilesDirs; namespace Interface { class BodyPartMemento; } } namespace MimeTreeParser { /** * @author Andras Mantia */ class MIMETREEPARSER_EXPORT NodeHelper : public QObject { Q_OBJECT public: NodeHelper(); ~NodeHelper(); void setNodeProcessed(KMime::Content *node, bool recurse); void setNodeUnprocessed(KMime::Content *node, bool recurse); bool nodeProcessed(KMime::Content *node) const; void clear(); void forceCleanTempFiles(); void setEncryptionState(const KMime::Content *node, const KMMsgEncryptionState state); KMMsgEncryptionState encryptionState(const KMime::Content *node) const; void setSignatureState(const KMime::Content *node, const KMMsgSignatureState state); KMMsgSignatureState signatureState(const KMime::Content *node) const; KMMsgSignatureState overallSignatureState(KMime::Content *node) const; KMMsgEncryptionState overallEncryptionState(KMime::Content *node) const; void setPartMetaData(KMime::Content *node, const PartMetaData &metaData); PartMetaData partMetaData(KMime::Content *node); /** * Set the 'Content-Type' by mime-magic from the contents of the body. * If autoDecode is true the decoded body will be used for mime type * determination (this does not change the body itself). */ static void magicSetType(KMime::Content *node, bool autoDecode = true); /** * Return this mails subject, with all "forward" and "reply" * prefixes removed */ static QString cleanSubject(KMime::Message *message); /** Attach an extra node to an existing node */ void attachExtraContent(KMime::Content *topLevelNode, KMime::Content *content); + void cleanExtraContent(KMime::Content *topLevelNode); + /** Get the extra nodes attached to the @param topLevelNode and all sub-nodes of @param topLevelNode */ QList extraContents(KMime::Content *topLevelNode) const; /** Return a modified message (node tree) starting from @param topLevelNode that has the original nodes and the extra nodes. The caller has the responsibility to delete the new message. */ KMime::Message *messageWithExtraContent(KMime::Content *topLevelNode); /** Get a QTextCodec suitable for this message part */ const QTextCodec *codec(KMime::Content *node); /** Set the charset the user selected for the message to display */ void setOverrideCodec(KMime::Content *node, const QTextCodec *codec); Interface::BodyPartMemento *bodyPartMemento(KMime::Content *node, const QByteArray &which) const; void setBodyPartMemento(KMime::Content *node, const QByteArray &which, Interface::BodyPartMemento *memento); // A flag to remember if the node was embedded. This is useful for attachment nodes, the reader // needs to know if they were displayed inline or not. bool isNodeDisplayedEmbedded(KMime::Content *node) const; void setNodeDisplayedEmbedded(KMime::Content *node, bool displayedEmbedded); // Same as above, but this time determines if the node was hidden or not bool isNodeDisplayedHidden(KMime::Content *node) const; void setNodeDisplayedHidden(KMime::Content *node, bool displayedHidden); /** * Writes the given message part to a temporary file and returns the * name of this file or QString() if writing failed. */ QString writeNodeToTempFile(KMime::Content *node); /** * Returns the temporary file path and name where this node was saved, or an empty url * if it wasn't saved yet with writeNodeToTempFile() */ QUrl tempFileUrlFromNode(const KMime::Content *node); /** * Creates a temporary dir for saving attachments, etc. * Will be automatically deleted when another message is viewed. * @param param Optional part of the directory name. */ QString createTempDir(const QString ¶m = QString()); /** * Cleanup the attachment temp files */ void removeTempFiles(); /** * Add a file to the list of managed temporary files */ void addTempFile(const QString &file); // Get a href in the form attachment:?place=, used by ObjectTreeParser and // UrlHandlerManager. QString asHREF(const KMime::Content *node, const QString &place) const; KMime::Content *fromHREF(const KMime::Message::Ptr &mMessage, const QUrl &href) const; /** * @return true if this node is a child or an encapsulated message */ static bool isInEncapsulatedMessage(KMime::Content *node); /** * Returns the charset for the given node. If no charset is specified * for the node, the defaultCharset() is returned. */ static QByteArray charset(KMime::Content *node); /** * Check for prefixes @p prefixRegExps in @p str. If none * is found, @p newPrefix + ' ' is prepended to @p str and the * resulting string is returned. If @p replace is true, any * sequence of whitespace-delimited prefixes at the beginning of * @p str is replaced by @p newPrefix. */ static QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, bool replace, const QString &newPrefix); /** * Return a QTextCodec for the specified charset. * This function is a bit more tolerant, than QTextCodec::codecForName */ static const QTextCodec *codecForName(const QByteArray &_str); /** * Returns a usable filename for a node, that can be the filename from the * content disposition header, or if that one is empty, the name from the * content type header. */ static QString fileName(const KMime::Content *node); /** * Fixes an encoding received by a KDE function and returns the proper, * MIME-compilant encoding name instead. * @see encodingForName */ static QString fixEncoding(const QString &encoding); //TODO(Andras) move to a utility class? /** * Drop-in replacement for KCharsets::encodingForName(). The problem with * the KCharsets function is that it returns "human-readable" encoding names * like "ISO 8859-15" instead of valid encoding names like "ISO-8859-15". * This function fixes this by replacing whitespace with a hyphen. */ static QString encodingForName(const QString &descriptiveName); //TODO(Andras) move to a utility class? /** * Return a list of the supported encodings * @param usAscii if true, US-Ascii encoding will be prepended to the list. */ static QStringList supportedEncodings(bool usAscii); //TODO(Andras) move to a utility class? QString fromAsString(KMime::Content *node) const; KMime::Content *decryptedNodeForContent(KMime::Content *content) const; /** * This function returns the unencrypted message that is based on @p originalMessage. * All encrypted MIME parts are removed and replaced by their decrypted plain-text versions. * Encrypted parts that are within signed parts are not replaced, since that would invalidate * the signature. * * This only works if the message was run through ObjectTreeParser::parseObjectTree() with the * currrent NodeHelper before, because parseObjectTree() actually decrypts the message and stores * the decrypted nodes by calling attachExtraContent(). * * @return the unencrypted message or an invalid pointer if the original message didn't contain * a part that needed to be modified. */ KMime::Message::Ptr unencryptedMessage(const KMime::Message::Ptr &originalMessage); /** * Returns a list of attachments of attached extra content nodes. * This is mainly useful is order to get attachments of encrypted messages. * Note that this does not include attachments from the primary node tree. * @see KMime::Content::attachments(). */ QVector attachmentsOfExtraContents() const; Q_SIGNALS: void update(MimeTreeParser::UpdateMode); private: Q_DISABLE_COPY(NodeHelper) bool unencryptedMessage_helper(KMime::Content *node, QByteArray &resultingData, bool addHeaders, int recursionLevel = 1); /** Check for prefixes @p prefixRegExps in #subject(). If none is found, @p newPrefix + ' ' is prepended to the subject and the resulting string is returned. If @p replace is true, any sequence of whitespace-delimited prefixes at the beginning of #subject() is replaced by @p newPrefix **/ static QString cleanSubject(KMime::Message *message, const QStringList &prefixRegExps, bool replace, const QString &newPrefix); void mergeExtraNodes(KMime::Content *node); void cleanFromExtraNodes(KMime::Content *node); /** Creates a persistent index string that bridges the gap between the permanent nodes and the temporary ones. Used internally for robust indexing. **/ QString persistentIndex(const KMime::Content *node) const; /** Translates the persistentIndex into a node back node: any node of the actually message to what the persistentIndex is interpreded **/ KMime::Content *contentFromIndex(KMime::Content *node, const QString &persistentIndex) const; private: QList mProcessedNodes; QList mNodesUnderProcess; QMap mEncryptionState; QMap mSignatureState; QSet mDisplayEmbeddedNodes; QSet mDisplayHiddenNodes; QTextCodec *mLocalCodec = nullptr; QMap mOverrideCodecs; QMap > mBodyPartMementoMap; QMap mPartMetaDatas; QMap > mExtraContents; AttachmentTemporaryFilesDirs *mAttachmentFilesDir = nullptr; friend class NodeHelperTest; }; } #endif