Messagelib

mimetreeparser/src/messagepart.h
1/*
2 SPDX-FileCopyrightText: 2015 Sandro Knauß <sknauss@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "mimetreeparser_export.h"
10
11#include "mimetreeparser/bodypartformatter.h"
12#include "mimetreeparser/util.h"
13
14#include <KMime/Message>
15
16#include <gpgme++/decryptionresult.h>
17#include <gpgme++/importresult.h>
18#include <gpgme++/key.h>
19#include <gpgme++/verificationresult.h>
20
21#include <QSharedPointer>
22#include <QString>
23
24#include <memory>
25
26namespace GpgME
27{
28class ImportResult;
29}
30
31namespace QGpgME
32{
33class Protocol;
34}
35
36namespace KMime
37{
38class Content;
39}
40
41namespace Kleo
42{
43class KeyCache;
44}
45
46namespace MimeTreeParser
47{
48class CompositeMemento;
49class MessagePartPrivate;
50namespace Interface
51{
52class ObjectTreeSource;
53}
54/**
55 * @brief The MessagePart class
56 */
57class MIMETREEPARSER_EXPORT MessagePart : public QObject
58{
59 Q_OBJECT
60 Q_PROPERTY(QString plaintextContent READ plaintextContent)
61 Q_PROPERTY(QString htmlContent READ htmlContent)
62 Q_PROPERTY(bool isAttachment READ isAttachment)
63 Q_PROPERTY(bool root READ isRoot)
64 Q_PROPERTY(bool isHtml READ isHtml)
65 Q_PROPERTY(bool isImage READ isImage CONSTANT)
66 Q_PROPERTY(bool neverDisplayInline READ neverDisplayInline CONSTANT)
67 Q_PROPERTY(QString attachmentIndex READ attachmentIndex CONSTANT)
68 Q_PROPERTY(QString link READ attachmentLink CONSTANT)
69public:
71 MessagePart(ObjectTreeParser *otp, const QString &text);
72 ~MessagePart() override;
73
74 void setParentPart(MessagePart *parentPart);
75 [[nodiscard]] MessagePart *parentPart() const;
76
77 [[nodiscard]] virtual QString text() const;
78 void setText(const QString &text);
79
80 [[nodiscard]] virtual QString plaintextContent() const;
81 [[nodiscard]] virtual QString htmlContent() const;
82
83 /** The KMime::Content* node that's represented by this part.
84 * Can be @c nullptr, e.g. for sub-parts of an inline signed body part.
85 */
86 [[nodiscard]] KMime::Content *content() const;
87 void setContent(KMime::Content *node);
88
89 /** The KMime::Content* node that's the source of this part.
90 * This is not necessarily the same as content(), for example for
91 * broken-up multipart nodes.
92 */
93 [[nodiscard]] KMime::Content *attachmentContent() const;
94 void setAttachmentContent(KMime::Content *node);
95 [[nodiscard]] bool isAttachment() const;
96 /** @see KMime::Content::index() */
97 [[nodiscard]] QString attachmentIndex() const;
98 /** @see NodeHelper::asHREF */
99 [[nodiscard]] QString attachmentLink() const;
100
101 /** Returns a string representation of an URL that can be used
102 * to invoke a BodyPartURLHandler for this body part.
103 */
104 [[nodiscard]] QString makeLink(const QString &path) const;
105
106 void setIsRoot(bool root);
107 [[nodiscard]] bool isRoot() const;
108
109 virtual bool isHtml() const;
110
111 [[nodiscard]] bool neverDisplayInline() const;
112 void setNeverDisplayInline(bool displayInline);
113 [[nodiscard]] bool isImage() const;
114 void setIsImage(bool image);
115
116 PartMetaData *partMetaData() const;
117
118 Interface::BodyPartMemento *memento() const;
119 void setMemento(Interface::BodyPartMemento *memento);
120
121 /* only a function that should be removed if the refactoring is over */
122 virtual void fix() const;
123
124 void appendSubPart(const MessagePart::Ptr &messagePart);
125 const QList<MessagePart::Ptr> &subParts() const;
126 [[nodiscard]] bool hasSubParts() const;
127 void clearSubParts();
128
129 Interface::ObjectTreeSource *source() const;
130 NodeHelper *nodeHelper() const;
131
132 [[nodiscard]] virtual bool hasHeader(const char *headerType) const;
133 virtual const KMime::Headers::Base *header(const char *headerType) const;
134 virtual QList<KMime::Headers::Base *> headers(const char *headerType) const;
135
136protected:
137 void parseInternal(KMime::Content *node, bool onlyOneMimePart);
138 [[nodiscard]] QString renderInternalText() const;
139
140 ObjectTreeParser *mOtp = nullptr;
141
142private:
143 std::unique_ptr<MessagePartPrivate> d;
144};
145/**
146 * @brief The MimeMessagePart class
147 */
148class MIMETREEPARSER_EXPORT MimeMessagePart : public MessagePart
149{
150 Q_OBJECT
151public:
153 MimeMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool onlyOneMimePart);
154 ~MimeMessagePart() override;
155
156 [[nodiscard]] QString text() const override;
157
158 [[nodiscard]] QString plaintextContent() const override;
159 [[nodiscard]] QString htmlContent() const override;
160
161private:
162 const bool mOnlyOneMimePart;
163};
164/**
165 * @brief The MessagePartList class
166 */
167class MIMETREEPARSER_EXPORT MessagePartList : public MessagePart
168{
169 Q_OBJECT
170public:
173 ~MessagePartList() override;
174
175 [[nodiscard]] QString text() const override;
176
177 [[nodiscard]] QString plaintextContent() const override;
178 [[nodiscard]] QString htmlContent() const override;
179};
180
181enum IconType { NoIcon = 0, IconExternal, IconInline };
182/**
183 * @brief The TextMessagePart class
184 */
185class MIMETREEPARSER_EXPORT TextMessagePart : public MessagePartList
186{
187 Q_OBJECT
188 Q_PROPERTY(bool showLink READ showLink CONSTANT)
189 Q_PROPERTY(bool isFirstTextPart READ isFirstTextPart CONSTANT)
190 Q_PROPERTY(bool hasLabel READ hasLabel CONSTANT)
191 Q_PROPERTY(QString label READ label CONSTANT)
192 Q_PROPERTY(QString comment READ comment CONSTANT)
193public:
195 TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool decryptMessage);
196 ~TextMessagePart() override;
197
198 KMMsgSignatureState signatureState() const;
199 KMMsgEncryptionState encryptionState() const;
200
201 [[nodiscard]] bool decryptMessage() const;
202
203 [[nodiscard]] bool showLink() const;
204 [[nodiscard]] bool isFirstTextPart() const;
205 [[nodiscard]] bool hasLabel() const;
206
207 /** The attachment filename, or the closest approximation thereof we have. */
208 [[nodiscard]] QString label() const;
209 /** A description of this attachment, if provided. */
210 [[nodiscard]] QString comment() const;
211 /** Temporary file containing the part content. */
212 [[nodiscard]] QString temporaryFilePath() const;
213
214private:
215 MIMETREEPARSER_NO_EXPORT void parseContent();
216
217 KMMsgSignatureState mSignatureState;
218 KMMsgEncryptionState mEncryptionState;
219 const bool mDecryptMessage;
220};
221/**
222 * @brief The AttachmentMessagePart class
223 */
224class MIMETREEPARSER_EXPORT AttachmentMessagePart : public TextMessagePart
225{
226 Q_OBJECT
227public:
230 ~AttachmentMessagePart() override;
231};
232/**
233 * @brief The HtmlMessagePart class
234 */
235class MIMETREEPARSER_EXPORT HtmlMessagePart : public MessagePart
236{
237 Q_OBJECT
238public:
241 ~HtmlMessagePart() override;
242
243 [[nodiscard]] QString text() const override;
244 [[nodiscard]] QString plaintextContent() const override;
245
246 void fix() const override;
247 [[nodiscard]] bool isHtml() const override;
248
249 [[nodiscard]] QString bodyHtml() const;
250
251private:
252 QString mBodyHTML;
253 QByteArray mCharset;
254};
255/**
256 * @brief The AlternativeMessagePart class
257 */
258class MIMETREEPARSER_EXPORT AlternativeMessagePart : public MessagePart
259{
260 Q_OBJECT
261public:
264 ~AlternativeMessagePart() override;
265
266 [[nodiscard]] QString text() const override;
267
268 [[nodiscard]] Util::HtmlMode preferredMode() const;
269 void setPreferredMode(Util::HtmlMode preferredMode);
270
271 [[nodiscard]] bool isHtml() const override;
272
273 [[nodiscard]] QString plaintextContent() const override;
274 [[nodiscard]] QString htmlContent() const override;
275
276 [[nodiscard]] QList<Util::HtmlMode> availableModes();
277
278 void fix() const override;
279
280 const QMap<Util::HtmlMode, MimeMessagePart::Ptr> &childParts() const;
281
282private:
283 Util::HtmlMode mPreferredMode;
284
287};
288/**
289 * @brief The CertMessagePart class
290 */
291class MIMETREEPARSER_EXPORT CertMessagePart : public MessagePart
292{
293 Q_OBJECT
294public:
296 CertMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, const QGpgME::Protocol *cryptoProto, bool autoImport);
297 ~CertMessagePart() override;
298
299 [[nodiscard]] QString text() const override;
300
301 const GpgME::ImportResult &importResult() const;
302
303private:
304 bool mAutoImport;
305 GpgME::ImportResult mImportResult;
306 const QGpgME::Protocol *mCryptoProto;
307};
308/**
309 * @brief The EncapsulatedRfc822MessagePart class
310 */
311class MIMETREEPARSER_EXPORT EncapsulatedRfc822MessagePart : public MessagePart
312{
313 Q_OBJECT
314public:
318
319 [[nodiscard]] QString text() const override;
320
321 void fix() const override;
322
323 const KMime::Message::Ptr message() const;
324
325private:
326 const KMime::Message::Ptr mMessage;
327};
328/**
329 * @brief The EncryptedMessagePart class
330 */
331class MIMETREEPARSER_EXPORT EncryptedMessagePart : public MessagePart
332{
333 Q_OBJECT
334 Q_PROPERTY(bool decryptMessage READ decryptMessage WRITE setDecryptMessage)
335 Q_PROPERTY(bool isEncrypted READ isEncrypted)
336 Q_PROPERTY(bool isNoSecKey READ isNoSecKey)
337 Q_PROPERTY(bool passphraseError READ passphraseError)
338public:
340 EncryptedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node);
341
342 ~EncryptedMessagePart() override;
343
344 [[nodiscard]] QString text() const override;
345
346 void setDecryptMessage(bool decrypt);
347 [[nodiscard]] bool decryptMessage() const;
348
349 void setIsEncrypted(bool encrypted);
350 [[nodiscard]] bool isEncrypted() const;
351
352 [[nodiscard]] bool isDecryptable() const;
353
354 [[nodiscard]] bool isNoSecKey() const;
355 [[nodiscard]] bool passphraseError() const;
356
357 void startDecryption(const QByteArray &text, QByteArrayView aCodec);
358 void startDecryption(KMime::Content *data = nullptr);
359
360 void setMementoName(const QByteArray &name);
361 [[nodiscard]] QByteArray mementoName() const;
362
363 [[nodiscard]] QString plaintextContent() const override;
364 [[nodiscard]] QString htmlContent() const override;
365
366 const QGpgME::Protocol *cryptoProto() const;
367 [[nodiscard]] QString fromAddress() const;
368
369 const std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>> &decryptRecipients() const;
370
371 [[nodiscard]] bool hasHeader(const char *headerType) const override;
372 const KMime::Headers::Base *header(const char *headerType) const override;
373 QList<KMime::Headers::Base *> headers(const char *headerType) const override;
374
375 QByteArray mDecryptedData;
376
377private:
378 /** Handles the decryption of a given content
379 * returns true if the decryption was successful
380 * if used in async mode, check if mMetaData.inPogress is true, it initiates a running decryption process.
381 */
382 [[nodiscard]] bool okDecryptMIME(KMime::Content &data);
383
384protected:
385 bool mPassphraseError;
386 bool mNoSecKey;
387 bool mDecryptMessage;
388 const QGpgME::Protocol *mCryptoProto;
389 QString mFromAddress;
390 QByteArray mVerifiedText;
391 QByteArray mMementoName;
392 std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>> mDecryptRecipients;
393 std::shared_ptr<const Kleo::KeyCache> mKeyCache;
394
395 friend class EncryptedBodyPartFormatter;
396};
397/**
398 * @brief The SignedMessagePart class
399 */
400class MIMETREEPARSER_EXPORT SignedMessagePart : public MessagePart
401{
402 Q_OBJECT
403 Q_PROPERTY(bool isSigned READ isSigned)
404public:
406 SignedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node);
407
408 ~SignedMessagePart() override;
409
410 void setIsSigned(bool isSigned);
411 [[nodiscard]] bool isSigned() const;
412
413 void startVerification(const QByteArray &text, QByteArrayView aCodec);
414 void startVerificationDetached(const QByteArray &text, KMime::Content *textNode, const QByteArray &signature);
415
416 void setMementoName(const QByteArray &name);
417 [[nodiscard]] QByteArray mementoName() const;
418
419 QByteArray mDecryptedData;
420 std::vector<GpgME::Signature> mSignatures;
421
422 [[nodiscard]] QString plaintextContent() const override;
423 [[nodiscard]] QString htmlContent() const override;
424
425 const QGpgME::Protocol *cryptoProto() const;
426 [[nodiscard]] QString fromAddress() const;
427
428 [[nodiscard]] bool hasHeader(const char *headerType) const override;
429 const KMime::Headers::Base *header(const char *headerType) const override;
430 [[nodiscard]] QList<KMime::Headers::Base *> headers(const char *headerType) const override;
431
432private:
433 /** Handles the verification of data
434 * If signature is empty it is handled as inline signature otherwise as detached signature mode.
435 * Returns true if the verification was successful and the block is signed.
436 * If used in async mode, check if mMetaData.inProgress is true, it initiates a running verification process.
437 */
438 [[nodiscard]] MIMETREEPARSER_NO_EXPORT bool okVerify(const QByteArray &data, const QByteArray &signature, KMime::Content *textNode);
439
440 MIMETREEPARSER_NO_EXPORT void sigStatusToMetaData();
441
442 MIMETREEPARSER_NO_EXPORT void setVerificationResult(const CompositeMemento *m, KMime::Content *textNode);
443
444protected:
445 const QGpgME::Protocol *mCryptoProto;
446 QString mFromAddress;
447 QByteArray mVerifiedText;
448 QByteArray mMementoName;
449 std::shared_ptr<const Kleo::KeyCache> mKeyCache;
450
452};
453}
interface of classes that implement status for BodyPartFormatters.
Definition bodypart.h:34
Interface for object tree sources.
Parses messages and generates HTML display code out of them.
HtmlMode
Describes the type of the displayed message.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.