File indexing completed on 2024-12-22 05:05:20

0001 // SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
0002 // SPDX-FileCopyrightText: 2003 Marc Mutz <mutz@kde.org>
0003 // SPDX-FileCopyrightText: 2002-2003, 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
0004 // SPDX-FileCopyrightText: 2009 Andras Mantia <andras@kdab.net>
0005 // SPDX-License-Identifier: LGPL-2.0-or-later
0006 
0007 #pragma once
0008 
0009 #include "messagepart.h"
0010 
0011 #include "mimetreeparser_core_export.h"
0012 #include <QSharedPointer>
0013 #include <functional>
0014 
0015 class QString;
0016 
0017 namespace KMime
0018 {
0019 class Content;
0020 }
0021 
0022 namespace MimeTreeParser
0023 {
0024 
0025 /**
0026     Entry point to parse mime messages.
0027 
0028     Content returned by the ObjectTreeParser (including messageparts),
0029     is normalized to not contain any CRLF's but only LF's (just like KMime).
0030 */
0031 class MIMETREEPARSER_CORE_EXPORT ObjectTreeParser
0032 {
0033     // Disable copy
0034     ObjectTreeParser(const ObjectTreeParser &other);
0035 
0036 public:
0037     explicit ObjectTreeParser() = default;
0038     virtual ~ObjectTreeParser() = default;
0039 
0040     QString structureAsString() const;
0041     void print();
0042 
0043     /// The text of the message, ie. what would appear in the
0044     /// composer's text editor if this was edited or replied to.
0045     /// This is usually the content of the first text/plain MIME part.
0046     QString plainTextContent();
0047 
0048     /// Similar to plainTextContent(), but returns the HTML source of
0049     /// the first text/html MIME part.
0050     QString htmlContent();
0051 
0052     /// Returns whether the parsed message contains encrypted parts.
0053     bool hasEncryptedParts() const;
0054 
0055     /// Returns whether the parsed message contains signed parts.
0056     ///
0057     /// \warning This doesn't check whether signed parts exists inside
0058     /// encrypted parts that have not been decrypted yet.
0059     bool hasSignedParts() const;
0060 
0061     /** Parse beginning at a given node and recursively parsing
0062       the children of that node and it's next sibling. */
0063     void parseObjectTree(KMime::Content *node);
0064     void parseObjectTree(const QByteArray &mimeMessage);
0065     MessagePart::Ptr parsedPart() const;
0066     KMime::Content *find(const std::function<bool(KMime::Content *)> &select);
0067     MessagePart::List collectContentParts();
0068     MessagePart::List collectContentParts(MessagePart::Ptr start);
0069     MessagePart::List collectAttachmentParts();
0070 
0071     /** Decrypt parts and verify signatures */
0072     void decryptAndVerify();
0073 
0074     /** Embedd content referenced by cid by inlining */
0075     QString resolveCidLinks(const QString &html);
0076 
0077 private:
0078     /**
0079      * Does the actual work for parseObjectTree. Unlike parseObjectTree(), this does not change the
0080      * top-level content.
0081      */
0082     MessagePart::Ptr parseObjectTreeInternal(KMime::Content *node, bool mOnlyOneMimePart);
0083     MessagePart::List processType(KMime::Content *node, const QByteArray &mediaType, const QByteArray &subType);
0084 
0085     MessagePart::List defaultHandling(KMime::Content *node);
0086 
0087     QByteArray codecNameFor(KMime::Content *node) const;
0088 
0089     KMime::Content *mTopLevelContent{nullptr};
0090     MessagePart::Ptr mParsedPart;
0091 
0092     KMime::Message::Ptr mMsg;
0093 
0094     friend class MessagePart;
0095     friend class EncryptedMessagePart;
0096     friend class SignedMessagePart;
0097     friend class EncapsulatedRfc822MessagePart;
0098     friend class TextMessagePart;
0099     friend class HtmlMessagePart;
0100     friend class TextPlainBodyPartFormatter;
0101     friend class MultiPartSignedBodyPartFormatter;
0102     friend class ApplicationPkcs7MimeBodyPartFormatter;
0103 };
0104 
0105 }