Messagelib

mimetreeparser/src/messagepart.h
1 /*
2  SPDX-FileCopyrightText: 2015 Sandro KnauƟ <[email protected]>
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 
26 namespace GpgME
27 {
28 class ImportResult;
29 }
30 
31 namespace QGpgME
32 {
33 class Protocol;
34 }
35 
36 namespace KMime
37 {
38 class Content;
39 }
40 
41 namespace Kleo
42 {
43 class KeyCache;
44 }
45 
46 namespace MimeTreeParser
47 {
48 class CompositeMemento;
49 class CryptoBodyPartMemento;
50 class MessagePartPrivate;
51 namespace Interface
52 {
53 class ObjectTreeSource;
54 }
55 /**
56  * @brief The MessagePart class
57  */
58 class 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)
70 public:
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 
137 protected:
138  void parseInternal(KMime::Content *node, bool onlyOneMimePart);
139  [[nodiscard]] QString renderInternalText() const;
140 
141  ObjectTreeParser *mOtp = nullptr;
142 
143 private:
144  std::unique_ptr<MessagePartPrivate> d;
145 };
146 /**
147  * @brief The MimeMessagePart class
148  */
149 class MIMETREEPARSER_EXPORT MimeMessagePart : public MessagePart
150 {
151  Q_OBJECT
152 public:
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 
162 private:
163  const bool mOnlyOneMimePart;
164 };
165 /**
166  * @brief The MessagePartList class
167  */
168 class MIMETREEPARSER_EXPORT MessagePartList : public MessagePart
169 {
170  Q_OBJECT
171 public:
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 
182 enum IconType { NoIcon = 0, IconExternal, IconInline };
183 /**
184  * @brief The TextMessagePart class
185  */
186 class 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)
194 public:
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 
215 private:
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  */
225 class MIMETREEPARSER_EXPORT AttachmentMessagePart : public TextMessagePart
226 {
227  Q_OBJECT
228 public:
231  ~AttachmentMessagePart() override;
232 };
233 /**
234  * @brief The HtmlMessagePart class
235  */
236 class MIMETREEPARSER_EXPORT HtmlMessagePart : public MessagePart
237 {
238  Q_OBJECT
239 public:
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 
252 private:
253  QString mBodyHTML;
254  QByteArray mCharset;
255 };
256 /**
257  * @brief The AlternativeMessagePart class
258  */
259 class MIMETREEPARSER_EXPORT AlternativeMessagePart : public MessagePart
260 {
261  Q_OBJECT
262 public:
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 
283 private:
284  Util::HtmlMode mPreferredMode;
285 
288 };
289 /**
290  * @brief The CertMessagePart class
291  */
292 class MIMETREEPARSER_EXPORT CertMessagePart : public MessagePart
293 {
294  Q_OBJECT
295 public:
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 
304 private:
305  bool mAutoImport;
306  GpgME::ImportResult mImportResult;
307  const QGpgME::Protocol *mCryptoProto;
308 };
309 /**
310  * @brief The EncapsulatedRfc822MessagePart class
311  */
312 class MIMETREEPARSER_EXPORT EncapsulatedRfc822MessagePart : public MessagePart
313 {
314  Q_OBJECT
315 public:
318  ~EncapsulatedRfc822MessagePart() override;
319 
320  [[nodiscard]] QString text() const override;
321 
322  void fix() const override;
323 
324  const KMime::Message::Ptr message() const;
325 
326 private:
327  const KMime::Message::Ptr mMessage;
328 };
329 /**
330  * @brief The EncryptedMessagePart class
331  */
332 class 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)
339 public:
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 
378 private:
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 
385 protected:
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  */
401 class MIMETREEPARSER_EXPORT SignedMessagePart : public MessagePart
402 {
403  Q_OBJECT
404  Q_PROPERTY(bool isSigned READ isSigned)
405 public:
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  QList<KMime::Headers::Base *> headers(const char *headerType) const override;
432 
433 private:
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]] bool okVerify(const QByteArray &data, const QByteArray &signature, KMime::Content *textNode);
440 
441  MIMETREEPARSER_NO_EXPORT void sigStatusToMetaData();
442 
443  void setVerificationResult(const CompositeMemento *m, KMime::Content *textNode);
444 
445 protected:
446  const QGpgME::Protocol *mCryptoProto;
447  QString mFromAddress;
448  QByteArray mVerifiedText;
449  QByteArray mMementoName;
450  std::shared_ptr<const Kleo::KeyCache> mKeyCache;
451 
452  friend EncryptedMessagePart;
453 };
454 }
Parses messages and generates HTML display code out of them.
Interface for object tree sources.
interface of classes that implement status for BodyPartFormatters.
Definition: bodypart.h:33
HtmlMode
Describes the type of the displayed message.
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 3 2023 03:57:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.