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

KDE's Doxygen guidelines are available online.