summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mimetreeparser/interface.h
blob: 05ad32b937a2ea6eb47e8cfdf7100c502e035be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
    Copyright (c) 2016 Sandro Knauß <knauss@kolabsystems.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#pragma once

#include <functional>
#include <memory>
#include <vector>

#include <QDateTime>
#include <QUrl>
#include <QMimeType>

class Part;
class PartPrivate;

class MailMime;
class MailMimePrivate;

class AlternativePart;
class AlternativePartPrivate;

class SinglePart;
class SinglePartPrivate;

class EncryptionPart;
class EncryptionPartPrivate;

class EncapsulatedPart;
class EncapsulatedPartPrivate;

class Content;
class ContentPrivate;

class CertContent;
class CertContentPrivate;

class EncryptionError;

class Key;
class KeyPrivate;
class Signature;
class SignaturePrivate;
class Encryption;
class EncryptionPrivate;

typedef std::shared_ptr<Signature> SignaturePtr;
typedef std::shared_ptr<Encryption> EncryptionPtr;

class Parser;
class ParserPrivate;

/* 
 * A MessagePart that is based on a KMime::Content
 */
class MailMime
{
public:
    typedef std::shared_ptr<MailMime> Ptr;
    /**
     *  Various possible values for the "Content-Disposition" header.
     */
    enum Disposition {
        Invalid,           ///< Default, invalid value
        Inline,            ///< inline
        Attachment        ///< attachment
    };

    MailMime();

    // interessting header parts of a KMime::Content
    QMimeType mimetype() const;
    Disposition disposition() const;
    QUrl label() const;
    QByteArray cid() const;
    QByteArray charset() const;
    QString filename() const;
    bool isText() const;

    // Unique identifier to ecactly this KMime::Content
    QByteArray link() const;

    QByteArray content() const;
    //Use default charset
    QString encodedContent() const;

    // overwrite default charset with given charset
    QString encodedContent(QByteArray charset) const;

    QByteArray decodedContent() const;

    bool isFirstTextPart() const;
    bool isFirstPart() const;
    bool isTopLevelPart() const;

    MailMime::Ptr parent() const;

private:
    std::unique_ptr<MailMimePrivate> d;

    friend class PartPrivate;
};

class Content
{
public:
    typedef std::shared_ptr<Content> Ptr;
    Content(const QByteArray &content, Part *parent);
    Content(ContentPrivate *d_ptr);
    virtual ~Content();

    QByteArray content() const;

    QByteArray charset() const;

    //Use default charset
    QString encodedContent() const;

    // overwrite default charset with given charset
    QString encodedContent(const QByteArray &charset) const;

    QVector<SignaturePtr> signatures() const;
    QVector<EncryptionPtr> encryptions() const;
    MailMime::Ptr mailMime() const;
    virtual QByteArray type() const;
    Part* parent() const;
private:
    std::unique_ptr<ContentPrivate> d;
};

class PlainTextContent : public Content
{
public:
    PlainTextContent(const QByteArray &content, Part *parent);
    PlainTextContent(ContentPrivate *d_ptr);
    QByteArray type() const Q_DECL_OVERRIDE;
};

class HtmlContent : public Content
{
public:
    HtmlContent(const QByteArray &content, Part *parent);
    HtmlContent(ContentPrivate* d_ptr);
    QByteArray type() const Q_DECL_OVERRIDE;
};

/*
 * importing a cert GpgMe::ImportResult
 * checking a cert (if it is a valid cert)
 */

class CertContent : public Content
{
public:
    typedef std::shared_ptr<CertContent> Ptr;
    CertContent(const QByteArray &content, Part *parent);

    QByteArray type() const Q_DECL_OVERRIDE;
    enum CertType {
        Pgp,
        SMime
    };

    enum CertSubType {
        Public,
        Private
    };

    CertType certType() const;
    CertSubType certSubType() const;
    int keyLength() const;

private:
    std::unique_ptr<CertContentPrivate> d;
};

class Part
{
public:
    typedef std::shared_ptr<Part> Ptr;
    Part();
    virtual QByteArray type() const;

    virtual QVector<QByteArray> availableContents() const;
    virtual QVector<Content::Ptr> content(const QByteArray& ct) const;
    QVector<Content::Ptr> content() const;

    bool hasSubParts() const;
    QVector<Part::Ptr> subParts() const;
    Part *parent() const;

    QVector<SignaturePtr> signatures() const;
    QVector<EncryptionPtr> encryptions() const;
    virtual MailMime::Ptr mailMime() const;
protected:
    std::unique_ptr<PartPrivate> d;
private:
    friend class ParserPrivate;
    friend class PartPrivate;
};

class AlternativePart : public Part
{
public:
    typedef std::shared_ptr<AlternativePart> Ptr;

    AlternativePart();
    virtual ~AlternativePart();

    QVector<QByteArray> availableContents() const Q_DECL_OVERRIDE;
    QVector<Content::Ptr> content(const QByteArray& ct) const Q_DECL_OVERRIDE;

    QByteArray type() const Q_DECL_OVERRIDE;

private:
    PartPrivate *reachParentD() const;
    std::unique_ptr<AlternativePartPrivate> d;

    friend class ParserPrivate;
    friend class AlternativePartPrivate;
};

class SinglePart : public Part
{
 public:
    typedef std::shared_ptr<SinglePart> Ptr;

    SinglePart();
    virtual ~SinglePart();

    QVector<Content::Ptr> content(const QByteArray& ct) const Q_DECL_OVERRIDE;
    QVector<QByteArray> availableContents() const Q_DECL_OVERRIDE;

    QByteArray type() const Q_DECL_OVERRIDE;
private:
    PartPrivate *reachParentD() const;
    std::unique_ptr<SinglePartPrivate> d;

    friend class ParserPrivate;
    friend class SinglePartPrivate;
};

/*
 * we want to request complete headers like:
 * from/to...
 */

class EncapsulatedPart : public SinglePart
{
public:
    typedef std::shared_ptr<EncapsulatedPart> Ptr;
    QByteArray type() const Q_DECL_OVERRIDE;

    //template <class T> QByteArray header<T>();
private:
    std::unique_ptr<EncapsulatedPartPrivate> d;
};

class EncryptionError
{
public:
    int errorId() const;
    QString errorString() const;
};

class Key
{
public:
    typedef std::shared_ptr<Key> Ptr;
    Key();
    Key(KeyPrivate *);
    ~Key();

    QString keyid() const;
    QString name() const;
    QString email() const;
    QString comment() const;
    QVector<QString> emails() const;
    enum KeyTrust {
        Unknown, Undefined, Never, Marginal, Full, Ultimate
    };
    KeyTrust keyTrust() const;

    bool isRevokation() const;
    bool isInvalid() const;
    bool isExpired() const;

    std::vector<Key::Ptr> subkeys();
    Key parentkey() const;
private:
    std::unique_ptr<KeyPrivate> d;
};

class Signature
{
public:
    typedef std::shared_ptr<Signature> Ptr;
    Signature();
    Signature(SignaturePrivate *);
    ~Signature();

    Key::Ptr key() const;
    QDateTime creationDateTime() const;
    QDateTime expirationDateTime() const;
    bool neverExpires() const;

    //template <> StatusObject<SignatureVerificationResult> verify() const;
    private:
        std::unique_ptr<SignaturePrivate> d;
};

/*
 * Normally the Keys for encryption are subkeys
 * for clients the parentkeys are "more interessting", because they store the name, email etc.
 * but a client may also wants show to what subkey the mail is really encrypted, an if this subkey isRevoked or something else
 */
class Encryption
{
public:
    enum ErrorType {
        NoError,
        PassphraseError,
        KeyMissing,
        UnknownError
    };
    typedef std::shared_ptr<Encryption> Ptr;
    Encryption();
    Encryption(EncryptionPrivate *);
    ~Encryption();
    std::vector<Key::Ptr> recipients() const;
    QString errorString();
    ErrorType errorType();
private:
    std::unique_ptr<EncryptionPrivate> d;
};

class Parser
{
public:
    typedef std::shared_ptr<Parser> Ptr;
    Parser(const QByteArray &mimeMessage);
    ~Parser();

    Part::Ptr getPart(const QUrl &url);

    QVector<Part::Ptr> collect(const Part::Ptr &start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const Content::Ptr &)> filter) const;
    Part::Ptr find(const Part::Ptr &start, std::function<bool(const Part::Ptr &)> select) const;
    QVector<Part::Ptr> collectContentParts() const;
    QVector<Part::Ptr> collectAttachmentParts() const;
    //template <> QVector<ContentPart::Ptr> collect<ContentPart>() const;

    //template <> static StatusObject<SignatureVerificationResult> verifySignature(const Signature signature) const;
    //template <> static StatusObject<Part> decrypt(const EncryptedPart part) const;

signals:
    void partsChanged();

private:
    std::unique_ptr<ParserPrivate> d;

    friend class InterfaceTest;
};