diff options
author | Sandro Knauß <sknauss@kde.org> | 2016-07-14 12:55:08 +0200 |
---|---|---|
committer | Sandro Knauß <sknauss@kde.org> | 2016-07-19 09:26:49 +0200 |
commit | 234daf6935043775ffce6b5a3ca78e06f56fd81e (patch) | |
tree | ec8d91efef8c3ed9ae8e44c90752b389f2082f09 /framework/domain | |
parent | c18f960ac7b21f1925885395fd04f3ee1d284167 (diff) | |
download | kube-234daf6935043775ffce6b5a3ca78e06f56fd81e.tar.gz kube-234daf6935043775ffce6b5a3ca78e06f56fd81e.zip |
First thoughts for the new messagepart interface
Diffstat (limited to 'framework/domain')
-rw-r--r-- | framework/domain/mimetreeparser/interface.h | 327 | ||||
-rw-r--r-- | framework/domain/mimetreeparser/test.cpp | 146 |
2 files changed, 473 insertions, 0 deletions
diff --git a/framework/domain/mimetreeparser/interface.h b/framework/domain/mimetreeparser/interface.h new file mode 100644 index 00000000..a9e394db --- /dev/null +++ b/framework/domain/mimetreeparser/interface.h | |||
@@ -0,0 +1,327 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Sandro Knauß <knauss@kolabsystems.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #pragma once | ||
21 | |||
22 | #include <QUrl> | ||
23 | #include <QMimeType> | ||
24 | |||
25 | class Part; | ||
26 | class EncryptionPart; | ||
27 | class SignaturePart; | ||
28 | |||
29 | class MimePart; | ||
30 | class MimePartPrivate; | ||
31 | |||
32 | class ContentPart; | ||
33 | class ContentPartPrivate; | ||
34 | |||
35 | class EncryptionErrorPart; | ||
36 | class EncryptionErrorPartPrivate; | ||
37 | |||
38 | class AttachmentPart; | ||
39 | class AttachmentPartPrivate; | ||
40 | |||
41 | class EncapsulatedPart; | ||
42 | class EncapsulatedPart; | ||
43 | |||
44 | class CertPart; | ||
45 | class CertPart; | ||
46 | |||
47 | class Key; | ||
48 | class Signature; | ||
49 | class Encryption; | ||
50 | |||
51 | class Parser; | ||
52 | class ParserPrivate; | ||
53 | |||
54 | class Parser | ||
55 | { | ||
56 | public: | ||
57 | Parser(const QByteArray &mimeMessage); | ||
58 | |||
59 | std::shared_ptr<Part> getPart(QUrl url); | ||
60 | |||
61 | QVector<std::shared_ptr<AttachmentPart>> collect<AttachmentPart>() const; | ||
62 | QVector<std::shared_ptr<ContentPart>> collect<ContentPart>() const; | ||
63 | QVector<std::shared_ptr<T>> collect<T>(Part start, std::function<bool(const Part &)> select, std::function<bool(const std::shared_ptr<T> &)> filter) const; | ||
64 | |||
65 | private: | ||
66 | std::unique_ptr<ParserPrivate> d; | ||
67 | }; | ||
68 | |||
69 | class Part | ||
70 | { | ||
71 | public: | ||
72 | virtual QByteArray type() const = 0; | ||
73 | |||
74 | bool hasSubParts() const; | ||
75 | QList<Part> subParts() const; | ||
76 | Part partent() const; | ||
77 | }; | ||
78 | |||
79 | /* | ||
80 | * A MessagePart that is based on a KMime::Content | ||
81 | */ | ||
82 | class MimePart : public Part | ||
83 | { | ||
84 | public: | ||
85 | /** | ||
86 | * Various possible values for the "Content-Disposition" header. | ||
87 | */ | ||
88 | enum Disposition { | ||
89 | Invalid, ///< Default, invalid value | ||
90 | Inline, ///< inline | ||
91 | Attachment, ///< attachment | ||
92 | Parallel ///< parallel (invalid, do not use) | ||
93 | }; | ||
94 | |||
95 | // interessting header parts of a KMime::Content | ||
96 | QByteArray content() const; | ||
97 | QMimeType mimetype() const; | ||
98 | Disposition dispossition() const | ||
99 | QUrl label() const; | ||
100 | QByteArray cid() const; | ||
101 | QByteArray charset() const; | ||
102 | |||
103 | // we wanna overrwrite the charset of the content, because some clients set the charset wrong | ||
104 | void setCharset(QByteArray charset); | ||
105 | |||
106 | // Unique identifier to ecactly this KMime::Content | ||
107 | QByteArray link() const; | ||
108 | |||
109 | |||
110 | QByteArray type() const Q_DECL_OVERRIDE; | ||
111 | private: | ||
112 | std::unique_ptr<MimePartPrivate> d; | ||
113 | }; | ||
114 | |||
115 | /* | ||
116 | * The main ContentPart | ||
117 | * is MimePart a good parent class? | ||
118 | * do we wanna need parts of the header of the connected KMime::Contents | ||
119 | * usecases: | ||
120 | * - | ||
121 | * for htmlonly it is representating only one MimePart (ok) | ||
122 | * for plaintext only also only one MimePart (ok) | ||
123 | * for alternative, we are represating three messageparts | ||
124 | * - "headers" do we return?, we can use setType to make it possible to select and than return these headers | ||
125 | */ | ||
126 | class ContentPart : public MimePart | ||
127 | { | ||
128 | public: | ||
129 | enum Types { | ||
130 | PlainText, | ||
131 | Html | ||
132 | }; | ||
133 | Q_DECLARE_FLAGS(Types, Type) | ||
134 | |||
135 | QByteArray content(Content::Type ct) const; | ||
136 | |||
137 | // convert content with charset | ||
138 | QString content(Content::Type ct) const; | ||
139 | |||
140 | Content::Types availableContent() const; | ||
141 | QVector<Signature> signature() const; | ||
142 | QVector<Encryption> encryption() const; | ||
143 | |||
144 | QByteArray type() const Q_DECL_OVERRIDE; | ||
145 | |||
146 | private: | ||
147 | std::unique_ptr<ContentPartPrivate> d; | ||
148 | }; | ||
149 | |||
150 | Q_DECLARE_OPERATORS_FOR_FLAGS(ContentPart::Type) | ||
151 | |||
152 | class AttachmentPart : public MimePart | ||
153 | { | ||
154 | public: | ||
155 | QByteArray type() const Q_DECL_OVERRIDE; | ||
156 | |||
157 | private: | ||
158 | std::unique_ptr<AttachmentPartPrivate> d; | ||
159 | }; | ||
160 | |||
161 | /* | ||
162 | * Faild to decrypt part | ||
163 | * thigs liks this can happen: | ||
164 | * decryption in progress | ||
165 | * have not tried at all to decrypt | ||
166 | * wrong passphrase | ||
167 | * no private key | ||
168 | * cryptobackend is not configured correctly (no gpg available) | ||
169 | * -> S/Mime and PGP have different meaning in their errors | ||
170 | * | ||
171 | * Open Questions: | ||
172 | * - How to make the string translateable for multiple clients, so that multiple clients can show same error messages, | ||
173 | * that helps users to understand what is going on ? | ||
174 | * - Does openpgp have translations already? | ||
175 | */ | ||
176 | class EncryptionErrorPart : public Part | ||
177 | { | ||
178 | public: | ||
179 | Error errorId() const; | ||
180 | |||
181 | CryptoBackend cryptoBackend(); | ||
182 | |||
183 | QByteArray type() const Q_DECL_OVERRIDE; | ||
184 | |||
185 | private: | ||
186 | std::unique_ptr<EncryptionErrorPartPrivate> d; | ||
187 | }; | ||
188 | |||
189 | /* | ||
190 | * we want to request complete headers like: | ||
191 | * from/to... | ||
192 | */ | ||
193 | |||
194 | class EncapsulatedPart :: public AttachmentPart | ||
195 | { | ||
196 | public: | ||
197 | QByteArray type() const Q_DECL_OVERRIDE; | ||
198 | |||
199 | QByteArray header<Type>(); | ||
200 | private: | ||
201 | std::unique_ptr<EncryptionErrorPartPrivate> d; | ||
202 | }; | ||
203 | |||
204 | /* | ||
205 | * importing a cert GpgMe::ImportResult | ||
206 | * checking a cert (if it is a valid cert) | ||
207 | */ | ||
208 | |||
209 | class CertPart :: public AttachmentPart | ||
210 | { | ||
211 | public: | ||
212 | QByteArray type() const Q_DECL_OVERRIDE; | ||
213 | |||
214 | bool checkCert() const; | ||
215 | Status importCert() const; | ||
216 | |||
217 | private: | ||
218 | std::unique_ptr<CertPartPrivate> d; | ||
219 | }; | ||
220 | |||
221 | /* | ||
222 | the ggme error class | ||
223 | |||
224 | // class GPGMEPP_EXPORT ErrorImportResult | ||
225 | { | ||
226 | public: | ||
227 | Error() : mErr(0), mMessage() {} | ||
228 | explicit Error(unsigned int e) : mErr(e), mMessage() {} | ||
229 | |||
230 | const char *source() const; | ||
231 | const char *asString() const; | ||
232 | |||
233 | int code() const; | ||
234 | int sourceID() const; | ||
235 | |||
236 | bool isCanceled() const; | ||
237 | |||
238 | unsigned int encodedError() const | ||
239 | { | ||
240 | return mErr; | ||
241 | } | ||
242 | int toErrno() const; | ||
243 | |||
244 | static bool hasSystemError(); | ||
245 | static Error fromSystemError(unsigned int src = GPGMEPP_ERR_SOURCE_DEFAULT); | ||
246 | static void setSystemError(gpg_err_code_t err); | ||
247 | static void setErrno(int err); | ||
248 | static Error fromErrno(int err, unsigned int src = GPGMEPP_ERR_SOURCE_DEFAULT); | ||
249 | static Error fromCode(unsigned int err, unsigned int src = GPGMEPP_ERR_SOURCE_DEFAULT); | ||
250 | |||
251 | GPGMEPP_MAKE_SAFE_BOOL_OPERATOR(mErr &&!isCanceled()) | ||
252 | private: | ||
253 | unsigned int mErr; | ||
254 | mutable std::string mMessage; | ||
255 | }; | ||
256 | */ | ||
257 | |||
258 | /* | ||
259 | * a used smime/PGP key | ||
260 | * in the end we also need things like: | ||
261 | bool isRevokation() const; | ||
262 | bool isInvalid() const; | ||
263 | bool isExpired() const; | ||
264 | |||
265 | -> so we end up wrapping GpgME::Key | ||
266 | */ | ||
267 | class Key | ||
268 | { | ||
269 | QString keyid() const; | ||
270 | QString name() const; | ||
271 | QString email() const; | ||
272 | QString comment() const; | ||
273 | QVector<QString> emails() const; | ||
274 | KeyTrust keyTrust() const; | ||
275 | CryptoBackend cryptoBackend() const; | ||
276 | |||
277 | std::vector<Key> subkeys(); | ||
278 | Key parentkey() const; | ||
279 | }; | ||
280 | |||
281 | class Signature | ||
282 | { | ||
283 | Key key() const; | ||
284 | QDateTime creationDateTime() const; | ||
285 | QDateTime expirationTime() const; | ||
286 | bool neverExpires() const; | ||
287 | |||
288 | bool inProgress(); //if the verfication is inProgress | ||
289 | |||
290 | enum Validity { | ||
291 | Unknown, Undefined, Never, Marginal, Full, Ultimate | ||
292 | }; | ||
293 | Validity validity() const; | ||
294 | |||
295 | // to determine if we need this in our usecase (email) | ||
296 | // GpgME::VerificationResult | ||
297 | enum Summary { | ||
298 | None = 0x000, | ||
299 | Valid = 0x001, | ||
300 | Green = 0x002, | ||
301 | Red = 0x004, | ||
302 | KeyRevoked = 0x008, | ||
303 | KeyExpired = 0x010, | ||
304 | SigExpired = 0x020, | ||
305 | KeyMissing = 0x040, | ||
306 | CrlMissing = 0x080, | ||
307 | CrlTooOld = 0x100, | ||
308 | BadPolicy = 0x200, | ||
309 | SysError = 0x400 | ||
310 | }; | ||
311 | Summary summary() const; | ||
312 | |||
313 | const char *policyURL() const; | ||
314 | GpgME::Notation notation(unsigned int index) const; | ||
315 | std::vector<GpgME::Notation> notations() const; | ||
316 | |||
317 | }; | ||
318 | |||
319 | /* | ||
320 | * Normally the Keys for encryption are subkeys | ||
321 | * for clients the parentkeys are "more interessting", because they store the name, email etc. | ||
322 | * but a client may also wants show to what subkey the mail is really encrypted, an if this subkey isRevoked or something else | ||
323 | */ | ||
324 | class Encryption | ||
325 | { | ||
326 | std::vector<Key> recipients() const; | ||
327 | }; \ No newline at end of file | ||
diff --git a/framework/domain/mimetreeparser/test.cpp b/framework/domain/mimetreeparser/test.cpp new file mode 100644 index 00000000..e096ea78 --- /dev/null +++ b/framework/domain/mimetreeparser/test.cpp | |||
@@ -0,0 +1,146 @@ | |||
1 | Usecases: | ||
2 | |||
3 | # plaintext msg + attachment | ||
4 | * ContentPart => cp1 | ||
5 | * AttachmentPart => ap1 | ||
6 | |||
7 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
8 | (ap1) == collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
9 | |||
10 | (PlainText) == cp1.availableContent() | ||
11 | |||
12 | # html msg + related attachment + normal attachment | ||
13 | * ContentPart => cp1 | ||
14 | * AttachmentPart(mimetype="*/related", cid="12345678") => ap1 | ||
15 | * AttachmentPart => ap2 | ||
16 | |||
17 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
18 | (ap1, ap2) == collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
19 | (ap2) == collect<AttachmentParts>(select=NoEncapsulatedMessages, filter=filterelated) | ||
20 | |||
21 | ap1 == getPart("cid:12345678") | ||
22 | |||
23 | (Html) == cp1.availableContent() | ||
24 | |||
25 | # alternative msg + attachment | ||
26 | * ContentPart(html="HTML", plaintext="Text") => cp1 | ||
27 | * AttachmentPart => ap1 | ||
28 | |||
29 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
30 | (ap1) == collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
31 | |||
32 | (Html, PlainText) == cp1.availableContent() | ||
33 | "HTML" == cp1.content(Html) | ||
34 | "text" == cp1.content(Plaintext) | ||
35 | |||
36 | # alternative msg with GPGInline | ||
37 | * ContentPart(html="HTML", plaintext="Text cypted<foo>") => cp1 | ||
38 | * TextPart(text="Text") | ||
39 | * TextPart(text=foo, encryption=(enc1) | ||
40 | |||
41 | (Html, PlainText) == cp1.availableContent() | ||
42 | |||
43 | TODO: but how to get plaintext/html content? | ||
44 | |||
45 | # encrypted msg (not encrypted/error) with unencrypted attachment | ||
46 | * EncryptionErrorPart => cp1 | ||
47 | * AttachmentPart => ap1 | ||
48 | |||
49 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
50 | (ap1) == collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
51 | |||
52 | #encrypted msg (decrypted with attachment) + unencrypted attachment | ||
53 | * encrytion=(rec1,rec2) => enc1 | ||
54 | * ContentPart(encrytion = (enc1,)) => cp1 | ||
55 | * AttachmentPart(encryption = (enc1,)) => ap1 | ||
56 | * AttachmentPart => ap2 | ||
57 | |||
58 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
59 | (ap1, ap2) == collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
60 | |||
61 | #INLINE GPG encrypted msg + attachment | ||
62 | * ContentPart => cp1 | ||
63 | * TextPart | ||
64 | * TextPart(encrytion = (enc1(rec1,rec2),)) | ||
65 | * TextPart(signed = (sig1,)) | ||
66 | * TextPart | ||
67 | * AttachmentPart => ap1 | ||
68 | |||
69 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
70 | (ap1) == collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
71 | |||
72 | #forwared encrypted msg + attachments | ||
73 | * ContentPart => cp1 | ||
74 | * EncapsulatedPart => ep1 | ||
75 | * Encrytion=(rec1,rec2) => enc1 | ||
76 | * Signature => sig1 | ||
77 | * ContentPart(encrytion = (enc1,), signature = (sig1,)) => cp2 | ||
78 | * TextPart(encrytion = (enc1,), signature = (sig1,)) | ||
79 | * TextPart(encrytion = (enc1, enc2(rec3,rec4),), signature = (sig1,)) | ||
80 | * AttachmentPart(encrytion = (enc1,), signature = (sig1,)) => ap1 | ||
81 | * AttachmentPart => ap2 | ||
82 | |||
83 | (cp1) = collect<ContentPart>(select=NoEncapsulatedMessages) | ||
84 | (ap2) = collect<AttachmentParts>(select=NoEncapsulatedMessages) | ||
85 | |||
86 | (cp2) = collect<ContentPart>(ep1, select=NoEncapsulatedMessages) | ||
87 | (ap1) = collect<AttachmentParts>(ep1, select=NoEncapsulatedMessages) | ||
88 | |||
89 | (cp1, cp2) == collect<ContentPart>() | ||
90 | (ap1, ap2) == collect<AttachmentParts>() | ||
91 | |||
92 | |||
93 | # plaintext msg + attachment + cert | ||
94 | * ContentPart => cp1 | ||
95 | * AttachmentPart => ap1 | ||
96 | * CertPart => cep1 | ||
97 | |||
98 | (cp1) == collect<ContentPart>(select=NoEncapsulatedMessages) | ||
99 | (ap1, cep1) == collect<AttachmentPart>(select=NoEncapsulatedMessages) | ||
100 | (ap1) == collect<AttachmentPart>(select=NoEncapsulatedMessages, filter=filterSubAttachmentParts) | ||
101 | |||
102 | (cep1) == collect<CertPart>(select=NoEncapsulatedMessages) | ||
103 | |||
104 | |||
105 | collect function: | ||
106 | |||
107 | bool noEncapsulatedMessages(Part part) | ||
108 | { | ||
109 | if (is<EncapsulatedPart>(part)) { | ||
110 | return false; | ||
111 | } | ||
112 | return true; | ||
113 | } | ||
114 | |||
115 | bool filterRelated(T part) | ||
116 | { | ||
117 | if (part.mimetype == related && !part.cid.isEmpty()) { | ||
118 | return false; //filter out related parts | ||
119 | } | ||
120 | return true; | ||
121 | } | ||
122 | |||
123 | bool filterSubAttachmentParts(AttachmentPart part) | ||
124 | { | ||
125 | if (isSubPart<AttachmentPart>(part)) { | ||
126 | return false; // filter out CertPart f.ex. | ||
127 | } | ||
128 | return true; | ||
129 | } | ||
130 | |||
131 | List<T> collect<T>(Part start, std::function<bool(const Part &)> select, std::function<bool(const std::shared_ptr<T> &)> filter) { | ||
132 | List<T> col; | ||
133 | if (!select(start)) { | ||
134 | return col; | ||
135 | } | ||
136 | |||
137 | if(isOrSubTypeIs<T>(start) && filter(start.staticCast<T>)){ | ||
138 | col.append(p); | ||
139 | } | ||
140 | foreach(childs as child) { | ||
141 | if (select(child)) { | ||
142 | col.expand(collect(child,select,filter); | ||
143 | } | ||
144 | } | ||
145 | return col; | ||
146 | } \ No newline at end of file | ||