From e452707fdfbd61be1e5633b516b653b7337e7865 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 29 May 2017 16:17:04 +0200 Subject: Reduced the messagetreeparser to aproximately what we actually require While in a much more managable state it's still not pretty. However, further refactoring can now gradually happen as we need to do further work on it. Things that should happen eventually: * Simplify the logic that creates the messageparts (we don't need the whole formatter plugin complexity) * Get rid of the nodehelper (let the parts hold the necessary data) * Get rid of partmetadata (let the part handleit) --- .../src/domain/mime/mimetreeparser/messagepart.h | 386 +++++++++++++++++++++ 1 file changed, 386 insertions(+) create mode 100644 framework/src/domain/mime/mimetreeparser/messagepart.h (limited to 'framework/src/domain/mime/mimetreeparser/messagepart.h') diff --git a/framework/src/domain/mime/mimetreeparser/messagepart.h b/framework/src/domain/mime/mimetreeparser/messagepart.h new file mode 100644 index 00000000..04fb30c3 --- /dev/null +++ b/framework/src/domain/mime/mimetreeparser/messagepart.h @@ -0,0 +1,386 @@ +/* + 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 "util.h" +#include "enums.h" +#include "partmetadata.h" + +#include + +#include +#include +#include + +#include +#include + +class QTextCodec; +class PartPrivate; + +namespace GpgME +{ +class ImportResult; +} + +namespace QGpgME +{ +class Protocol; +} + +namespace KMime +{ +class Content; +} + +namespace MimeTreeParser +{ +class ObjectTreeParser; +class HTMLBlock; +typedef QSharedPointer HTMLBlockPtr; +class CryptoBodyPartMemento; +class MultiPartAlternativeBodyPartFormatter; + +class SignedMessagePart; +class EncryptedMessagePart; + +class MessagePart : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool attachment READ isAttachment) + Q_PROPERTY(bool root READ isRoot) + Q_PROPERTY(bool isHtml READ isHtml) + Q_PROPERTY(QString plaintextContent READ plaintextContent) + Q_PROPERTY(QString htmlContent READ htmlContent) +public: + enum Disposition { + Inline, + Attachment, + Invalid + }; + typedef QSharedPointer Ptr; + MessagePart(ObjectTreeParser *otp, const QString &text, KMime::Content *node = nullptr); + + virtual ~MessagePart(); + + virtual QString text() const; + void setText(const QString &text); + bool isAttachment() const; + + void setIsRoot(bool root); + bool isRoot() const; + + void setParentPart(MessagePart *parentPart); + MessagePart *parentPart() const; + + virtual QString plaintextContent() const; + virtual QString htmlContent() const; + + virtual bool isHtml() const; + + QByteArray mimeType() const; + QByteArray charset() const; + QString filename() const; + Disposition disposition() const; + bool isText() const; + int error() const; + QString errorString() const; + + PartMetaData *partMetaData(); + + void appendSubPart(const MessagePart::Ptr &messagePart); + const QVector &subParts() const; + bool hasSubParts() const; + + KMime::Content *node() const; + + QVector signatures() const; + QVector encryptions() const; + +protected: + void parseInternal(KMime::Content *node, bool onlyOneMimePart); + QString renderInternalText() const; + + QString mText; + ObjectTreeParser *mOtp; + PartMetaData mMetaData; + MessagePart *mParentPart; + KMime::Content *mNode; + +private: + QVector mBlocks; + bool mRoot; +}; + +class MimeMessagePart : public MessagePart +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + MimeMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool onlyOneMimePart); + virtual ~MimeMessagePart(); + + QString text() const Q_DECL_OVERRIDE; + + QString plaintextContent() const Q_DECL_OVERRIDE; + QString htmlContent() const Q_DECL_OVERRIDE; +private: + friend class AlternativeMessagePart; + friend class ::PartPrivate; +}; + +class MessagePartList : public MessagePart +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + MessagePartList(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node = nullptr); + virtual ~MessagePartList(); + + QString text() const Q_DECL_OVERRIDE; + + QString plaintextContent() const Q_DECL_OVERRIDE; + QString htmlContent() const Q_DECL_OVERRIDE; +}; + +enum IconType { + NoIcon = 0, + IconExternal, + IconInline +}; + +class TextMessagePart : public MessagePartList +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node); + virtual ~TextMessagePart(); + + KMMsgSignatureState signatureState() const; + KMMsgEncryptionState encryptionState() const; + +private: + void parseContent(); + + KMMsgSignatureState mSignatureState; + KMMsgEncryptionState mEncryptionState; + + friend class DefaultRendererPrivate; + friend class ObjectTreeParser; + friend class ::PartPrivate; +}; + +class AttachmentMessagePart : public TextMessagePart +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + AttachmentMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node); + virtual ~AttachmentMessagePart(); + +}; + +class HtmlMessagePart : public MessagePart +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + HtmlMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node); + virtual ~HtmlMessagePart(); + + QString text() const Q_DECL_OVERRIDE; + + bool isHtml() const Q_DECL_OVERRIDE; + +private: + QString mBodyHTML; + QByteArray mCharset; + + friend class DefaultRendererPrivate; + friend class ::PartPrivate; +}; + +class AlternativeMessagePart : public MessagePart +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + AlternativeMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, Util::HtmlMode preferredMode); + virtual ~AlternativeMessagePart(); + + QString text() const Q_DECL_OVERRIDE; + + Util::HtmlMode preferredMode() const; + + bool isHtml() const Q_DECL_OVERRIDE; + + QString plaintextContent() const Q_DECL_OVERRIDE; + QString htmlContent() const Q_DECL_OVERRIDE; + + QList availableModes(); +private: + Util::HtmlMode mPreferredMode; + + QMap mChildNodes; + QMap mChildParts; + + friend class DefaultRendererPrivate; + friend class ObjectTreeParser; + friend class MultiPartAlternativeBodyPartFormatter; + friend class ::PartPrivate; +}; + +class CertMessagePart : public MessagePart +{ + Q_OBJECT +public: + typedef QSharedPointer Ptr; + CertMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, const QGpgME::Protocol *cryptoProto); + virtual ~CertMessagePart(); + + QString text() const Q_DECL_OVERRIDE; + void import(); + +private: + const QGpgME::Protocol *mCryptoProto; + friend class DefaultRendererPrivate; +}; + +class 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 Q_DECL_OVERRIDE; +private: + const KMime::Message::Ptr mMessage; + + friend class DefaultRendererPrivate; +}; + +class EncryptedMessagePart : public MessagePart +{ + Q_OBJECT + 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, KMime::Content *encryptedNode = nullptr); + + virtual ~EncryptedMessagePart(); + + QString text() const Q_DECL_OVERRIDE; + + 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 Q_DECL_OVERRIDE; + QString htmlContent() const Q_DECL_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; + QByteArray mVerifiedText; + std::vector mDecryptRecipients; + KMime::Content *mEncryptedNode; + + friend class DefaultRendererPrivate; + friend class ::PartPrivate; +}; + +class 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, KMime::Content *signedData); + + 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); + void startVerification(); + + QByteArray mDecryptedData; + std::vector mSignatures; + + QString plaintextContent() const Q_DECL_OVERRIDE; + QString htmlContent() const Q_DECL_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; + KMime::Content *mSignedData; + + friend EncryptedMessagePart; + friend class DefaultRendererPrivate; + friend class ::PartPrivate; +}; + +} + +#endif //__MIMETREEPARSER_MESSAGEPART_H__ -- cgit v1.2.3