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

KDE's Doxygen guidelines are available online.