File indexing completed on 2022-11-29 19:53:39

0001 /*
0002     kmime_parsers.h
0003 
0004     KMime, the KDE Internet mail/usenet news message library.
0005     SPDX-FileCopyrightText: 2001 the KMime authors.
0006     See file AUTHORS for details
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 #pragma once
0011 
0012 #include <QByteArray>
0013 #include <QVector>
0014 
0015 namespace KMime
0016 {
0017 
0018 namespace Parser
0019 {
0020 
0021 /** Helper-class: splits a multipart-message into single
0022     parts as described in RFC 2046
0023     @internal
0024 */
0025 class MultiPart
0026 {
0027 public:
0028     MultiPart(const QByteArray &src, const QByteArray &boundary);
0029 
0030     Q_REQUIRED_RESULT bool parse();
0031     Q_REQUIRED_RESULT QVector<QByteArray> parts() const
0032     {
0033         return m_parts;
0034     }
0035     Q_REQUIRED_RESULT QByteArray preamble() const
0036     {
0037         return m_preamble;
0038     }
0039     Q_REQUIRED_RESULT QByteArray epilouge() const
0040     {
0041         return m_epilouge;
0042     }
0043 
0044 private:
0045     QByteArray m_src;
0046     const QByteArray m_boundary;
0047     QByteArray m_preamble;
0048     QByteArray m_epilouge;
0049     QVector<QByteArray> m_parts;
0050 };
0051 
0052 /** Helper-class: abstract base class of all parsers for
0053     non-mime binary data (uuencoded, yenc)
0054     @internal
0055 */
0056 class NonMimeParser
0057 {
0058 public:
0059     explicit NonMimeParser(const QByteArray &src);
0060     virtual ~NonMimeParser();
0061     virtual bool parse() = 0;
0062     Q_REQUIRED_RESULT bool isPartial() const
0063     {
0064         return (m_partNr > -1 && m_totalNr > -1 && m_totalNr != 1);
0065     }
0066     Q_REQUIRED_RESULT int partialNumber() const
0067     {
0068         return m_partNr;
0069     }
0070     Q_REQUIRED_RESULT int partialCount() const
0071     {
0072         return m_totalNr;
0073     }
0074     Q_REQUIRED_RESULT bool hasTextPart() const
0075     {
0076         return (m_text.length() > 1);
0077     }
0078     Q_REQUIRED_RESULT QByteArray textPart() const
0079     {
0080         return m_text;
0081     }
0082     Q_REQUIRED_RESULT QVector<QByteArray> binaryParts() const
0083     {
0084         return m_bins;
0085     }
0086     Q_REQUIRED_RESULT QVector<QByteArray> filenames() const
0087     {
0088         return m_filenames;
0089     }
0090     Q_REQUIRED_RESULT QVector<QByteArray> mimeTypes() const
0091     {
0092         return m_mimeTypes;
0093     }
0094 
0095 protected:
0096     static QByteArray guessMimeType(const QByteArray &fileName);
0097 
0098     QByteArray m_src, m_text;
0099     QVector<QByteArray> m_bins, m_filenames, m_mimeTypes;
0100     int m_partNr, m_totalNr;
0101 };
0102 
0103 /** Helper-class: tries to extract the data from a possibly
0104     uuencoded message
0105     @internal
0106 */
0107 class UUEncoded : public NonMimeParser
0108 {
0109 public:
0110     UUEncoded(const QByteArray &src, const QByteArray &subject);
0111 
0112     Q_REQUIRED_RESULT bool parse() override;
0113 
0114 private:
0115     QByteArray m_subject;
0116 };
0117 
0118 /** Helper-class: tries to extract the data from a possibly
0119     yenc encoded message
0120     @internal
0121 */
0122 class YENCEncoded : public NonMimeParser
0123 {
0124 public:
0125     explicit YENCEncoded(const QByteArray &src);
0126 
0127     Q_REQUIRED_RESULT bool parse() override;
0128 
0129 private:
0130     static bool yencMeta(QByteArray &src, const QByteArray &name, int *value);
0131 };
0132 
0133 } // namespace Parser
0134 
0135 } // namespace KMime
0136