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 {
182 NoIcon = 0,
183 IconExternal,
184 IconInline
185};
186/**
187 * @brief The TextMessagePart class
188 */
189class MIMETREEPARSER_EXPORT TextMessagePart : public MessagePartList
190{
191 Q_OBJECT
192 Q_PROPERTY(bool showLink READ showLink CONSTANT)
193 Q_PROPERTY(bool isFirstTextPart READ isFirstTextPart CONSTANT)
194 Q_PROPERTY(bool hasLabel READ hasLabel CONSTANT)
195 Q_PROPERTY(QString label READ label CONSTANT)
196 Q_PROPERTY(QString comment READ comment CONSTANT)
197public:
199 TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool decryptMessage);
200 ~TextMessagePart() override;
201
202 KMMsgSignatureState signatureState() const;
203 KMMsgEncryptionState encryptionState() const;
204
205 [[nodiscard]] bool decryptMessage() const;
206
207 [[nodiscard]] bool showLink() const;
208 [[nodiscard]] bool isFirstTextPart() const;
209 [[nodiscard]] bool hasLabel() const;
210
211 /** The attachment filename, or the closest approximation thereof we have. */
212 [[nodiscard]] QString label() const;
213 /** A description of this attachment, if provided. */
214 [[nodiscard]] QString comment() const;
215 /** Temporary file containing the part content. */
216 [[nodiscard]] QString temporaryFilePath() const;
217
218private:
219 MIMETREEPARSER_NO_EXPORT void parseContent();
220
221 KMMsgSignatureState mSignatureState;
222 KMMsgEncryptionState mEncryptionState;
223 const bool mDecryptMessage;
224};
225/**
226 * @brief The AttachmentMessagePart class
227 */
228class MIMETREEPARSER_EXPORT AttachmentMessagePart : public TextMessagePart
229{
230 Q_OBJECT
231public:
234 ~AttachmentMessagePart() override;
235};
236/**
237 * @brief The HtmlMessagePart class
238 */
239class MIMETREEPARSER_EXPORT HtmlMessagePart : public MessagePart
240{
241 Q_OBJECT
242public:
245 ~HtmlMessagePart() override;
246
247 [[nodiscard]] QString text() const override;
248 [[nodiscard]] QString plaintextContent() const override;
249
250 void fix() const override;
251 [[nodiscard]] bool isHtml() const override;
252
253 [[nodiscard]] QString bodyHtml() const;
254
255private:
256 QString mBodyHTML;
257 QByteArray mCharset;
258};
259/**
260 * @brief The AlternativeMessagePart class
261 */
262class MIMETREEPARSER_EXPORT AlternativeMessagePart : public MessagePart
263{
264 Q_OBJECT
265public:
268 ~AlternativeMessagePart() override;
269
270 [[nodiscard]] QString text() const override;
271
272 [[nodiscard]] Util::HtmlMode preferredMode() const;
273 void setPreferredMode(Util::HtmlMode preferredMode);
274
275 [[nodiscard]] bool isHtml() const override;
276
277 [[nodiscard]] QString plaintextContent() const override;
278 [[nodiscard]] QString htmlContent() const override;
279
280 [[nodiscard]] QList<Util::HtmlMode> availableModes();
281
282 void fix() const override;
283
284 const QMap<Util::HtmlMode, MimeMessagePart::Ptr> &childParts() const;
285
286private:
287 Util::HtmlMode mPreferredMode;
288
291};
292/**
293 * @brief The CertMessagePart class
294 */
295class MIMETREEPARSER_EXPORT CertMessagePart : public MessagePart
296{
297 Q_OBJECT
298public:
300 CertMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, const QGpgME::Protocol *cryptoProto, bool autoImport);
301 ~CertMessagePart() override;
302
303 [[nodiscard]] QString text() const override;
304
305 const GpgME::ImportResult &importResult() const;
306
307private:
308 bool mAutoImport;
309 GpgME::ImportResult mImportResult;
310 const QGpgME::Protocol *mCryptoProto;
311};
312/**
313 * @brief The EncapsulatedRfc822MessagePart class
314 */
315class MIMETREEPARSER_EXPORT EncapsulatedRfc822MessagePart : public MessagePart
316{
317 Q_OBJECT
318public:
322
323 [[nodiscard]] QString text() const override;
324
325 void fix() const override;
326
327 const KMime::Message::Ptr message() const;
328
329private:
330 const KMime::Message::Ptr mMessage;
331};
332/**
333 * @brief The EncryptedMessagePart class
334 */
335class MIMETREEPARSER_EXPORT EncryptedMessagePart : public MessagePart
336{
337 Q_OBJECT
338 Q_PROPERTY(bool decryptMessage READ decryptMessage WRITE setDecryptMessage)
339 Q_PROPERTY(bool isEncrypted READ isEncrypted)
340 Q_PROPERTY(bool isNoSecKey READ isNoSecKey)
341 Q_PROPERTY(bool passphraseError READ passphraseError)
342public:
344 EncryptedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node);
345
346 ~EncryptedMessagePart() override;
347
348 [[nodiscard]] QString text() const override;
349
350 void setDecryptMessage(bool decrypt);
351 [[nodiscard]] bool decryptMessage() const;
352
353 void setIsEncrypted(bool encrypted);
354 [[nodiscard]] bool isEncrypted() const;
355
356 [[nodiscard]] bool isDecryptable() const;
357
358 [[nodiscard]] bool isNoSecKey() const;
359 [[nodiscard]] bool passphraseError() const;
360
361 void startDecryption(const QByteArray &text, QByteArrayView aCodec);
362 void startDecryption(KMime::Content *data = nullptr);
363
364 void setMementoName(const QByteArray &name);
365 [[nodiscard]] QByteArray mementoName() const;
366
367 [[nodiscard]] QString plaintextContent() const override;
368 [[nodiscard]] QString htmlContent() const override;
369
370 const QGpgME::Protocol *cryptoProto() const;
371 [[nodiscard]] QString fromAddress() const;
372
373 const std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>> &decryptRecipients() const;
374
375 [[nodiscard]] bool hasHeader(const char *headerType) const override;
376 const KMime::Headers::Base *header(const char *headerType) const override;
377 QList<KMime::Headers::Base *> headers(const char *headerType) const override;
378
379 QByteArray mDecryptedData;
380
381private:
382 /** Handles the decryption of a given content
383 * returns true if the decryption was successful
384 * if used in async mode, check if mMetaData.inPogress is true, it initiates a running decryption process.
385 */
386 [[nodiscard]] bool okDecryptMIME(KMime::Content &data);
387
388protected:
389 bool mPassphraseError;
390 bool mNoSecKey;
391 bool mDecryptMessage;
392 const QGpgME::Protocol *mCryptoProto;
393 QString mFromAddress;
394 QByteArray mVerifiedText;
395 QByteArray mMementoName;
396 std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>> mDecryptRecipients;
397 std::shared_ptr<const Kleo::KeyCache> mKeyCache;
398
399 friend class EncryptedBodyPartFormatter;
400};
401/**
402 * @brief The SignedMessagePart class
403 */
404class MIMETREEPARSER_EXPORT SignedMessagePart : public MessagePart
405{
406 Q_OBJECT
407 Q_PROPERTY(bool isSigned READ isSigned)
408public:
410 SignedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node);
411
412 ~SignedMessagePart() override;
413
414 void setIsSigned(bool isSigned);
415 [[nodiscard]] bool isSigned() const;
416
417 void startVerification(const QByteArray &text, QByteArrayView aCodec);
418 void startVerificationDetached(const QByteArray &text, KMime::Content *textNode, const QByteArray &signature);
419
420 void setMementoName(const QByteArray &name);
421 [[nodiscard]] QByteArray mementoName() const;
422
423 QByteArray mDecryptedData;
424 std::vector<GpgME::Signature> mSignatures;
425
426 [[nodiscard]] QString plaintextContent() const override;
427 [[nodiscard]] QString htmlContent() const override;
428
429 const QGpgME::Protocol *cryptoProto() const;
430 [[nodiscard]] QString fromAddress() const;
431
432 [[nodiscard]] bool hasHeader(const char *headerType) const override;
433 const KMime::Headers::Base *header(const char *headerType) const override;
434 [[nodiscard]] QList<KMime::Headers::Base *> headers(const char *headerType) const override;
435
436private:
437 /** Handles the verification of data
438 * If signature is empty it is handled as inline signature otherwise as detached signature mode.
439 * Returns true if the verification was successful and the block is signed.
440 * If used in async mode, check if mMetaData.inProgress is true, it initiates a running verification process.
441 */
442 [[nodiscard]] MIMETREEPARSER_NO_EXPORT bool okVerify(const QByteArray &data, const QByteArray &signature, KMime::Content *textNode);
443
444 MIMETREEPARSER_NO_EXPORT void sigStatusToMetaData();
445
446 MIMETREEPARSER_NO_EXPORT void setVerificationResult(const CompositeMemento *m, KMime::Content *textNode);
447
448protected:
449 const QGpgME::Protocol *mCryptoProto;
450 QString mFromAddress;
451 QByteArray mVerifiedText;
452 QByteArray mMementoName;
453 std::shared_ptr<const Kleo::KeyCache> mKeyCache;
454
456};
457}
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 Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.