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

KDE's Doxygen guidelines are available online.