diff options
Diffstat (limited to 'framework/domain/mimetreeparser/interface.cpp')
-rw-r--r-- | framework/domain/mimetreeparser/interface.cpp | 262 |
1 files changed, 210 insertions, 52 deletions
diff --git a/framework/domain/mimetreeparser/interface.cpp b/framework/domain/mimetreeparser/interface.cpp index ceb8caf3..0bcbfec4 100644 --- a/framework/domain/mimetreeparser/interface.cpp +++ b/framework/domain/mimetreeparser/interface.cpp | |||
@@ -23,6 +23,10 @@ | |||
23 | #include "stringhtmlwriter.h" | 23 | #include "stringhtmlwriter.h" |
24 | #include "objecttreesource.h" | 24 | #include "objecttreesource.h" |
25 | 25 | ||
26 | #include <Libkleo/KeyListJob> | ||
27 | #include <gpgme++/key.h> | ||
28 | #include <gpgme++/keylistresult.h> | ||
29 | |||
26 | #include <KMime/Content> | 30 | #include <KMime/Content> |
27 | #include <MimeTreeParser/ObjectTreeParser> | 31 | #include <MimeTreeParser/ObjectTreeParser> |
28 | #include <MimeTreeParser/MessagePart> | 32 | #include <MimeTreeParser/MessagePart> |
@@ -162,6 +166,131 @@ QByteArray MailMime::decodedContent() const | |||
162 | return d->mNode->decodedContent(); | 166 | return d->mNode->decodedContent(); |
163 | } | 167 | } |
164 | 168 | ||
169 | class KeyPrivate | ||
170 | { | ||
171 | public: | ||
172 | Key *q; | ||
173 | GpgME::Key mKey; | ||
174 | }; | ||
175 | |||
176 | Key::Key() | ||
177 | :d(std::unique_ptr<KeyPrivate>(new KeyPrivate)) | ||
178 | { | ||
179 | d->q = this; | ||
180 | } | ||
181 | |||
182 | |||
183 | Key::Key(KeyPrivate *d_ptr) | ||
184 | :d(std::unique_ptr<KeyPrivate>(d_ptr)) | ||
185 | { | ||
186 | d->q = this; | ||
187 | } | ||
188 | |||
189 | Key::~Key() | ||
190 | { | ||
191 | |||
192 | } | ||
193 | |||
194 | QString Key::keyid() const | ||
195 | { | ||
196 | return d->mKey.keyID(); | ||
197 | } | ||
198 | |||
199 | QString Key::name() const | ||
200 | { | ||
201 | //FIXME: is this the correct way to get the primary UID? | ||
202 | return d->mKey.userID(0).name(); | ||
203 | } | ||
204 | |||
205 | QString Key::email() const | ||
206 | { | ||
207 | return d->mKey.userID(0).email(); | ||
208 | } | ||
209 | |||
210 | QString Key::comment() const | ||
211 | { | ||
212 | return d->mKey.userID(0).comment(); | ||
213 | } | ||
214 | |||
215 | class SignaturePrivate | ||
216 | { | ||
217 | public: | ||
218 | Signature *q; | ||
219 | GpgME::Signature mSignature; | ||
220 | Key::Ptr mKey; | ||
221 | }; | ||
222 | |||
223 | Signature::Signature() | ||
224 | :d(std::unique_ptr<SignaturePrivate>(new SignaturePrivate)) | ||
225 | { | ||
226 | d->q = this; | ||
227 | } | ||
228 | |||
229 | |||
230 | Signature::Signature(SignaturePrivate *d_ptr) | ||
231 | :d(std::unique_ptr<SignaturePrivate>(d_ptr)) | ||
232 | { | ||
233 | d->q = this; | ||
234 | |||
235 | } | ||
236 | |||
237 | Signature::~Signature() | ||
238 | { | ||
239 | |||
240 | } | ||
241 | |||
242 | QDateTime Signature::creationDateTime() const | ||
243 | { | ||
244 | QDateTime dt; | ||
245 | dt.setTime_t(d->mSignature.creationTime()); | ||
246 | return dt; | ||
247 | } | ||
248 | |||
249 | QDateTime Signature::expirationDateTime() const | ||
250 | { | ||
251 | QDateTime dt; | ||
252 | dt.setTime_t(d->mSignature.expirationTime()); | ||
253 | return dt; | ||
254 | } | ||
255 | |||
256 | bool Signature::neverExpires() const | ||
257 | { | ||
258 | return d->mSignature.neverExpires(); | ||
259 | } | ||
260 | |||
261 | Key::Ptr Signature::key() const | ||
262 | { | ||
263 | return d->mKey; | ||
264 | } | ||
265 | |||
266 | class EncryptionPrivate | ||
267 | { | ||
268 | public: | ||
269 | Encryption *q; | ||
270 | std::vector<Key::Ptr> mRecipients; | ||
271 | }; | ||
272 | |||
273 | Encryption::Encryption(EncryptionPrivate *d_ptr) | ||
274 | :d(std::unique_ptr<EncryptionPrivate>(d_ptr)) | ||
275 | { | ||
276 | d->q = this; | ||
277 | } | ||
278 | |||
279 | Encryption::Encryption() | ||
280 | :d(std::unique_ptr<EncryptionPrivate>(new EncryptionPrivate)) | ||
281 | { | ||
282 | d->q = this; | ||
283 | } | ||
284 | |||
285 | Encryption::~Encryption() | ||
286 | { | ||
287 | |||
288 | } | ||
289 | |||
290 | std::vector<Key::Ptr> Encryption::recipients() const | ||
291 | { | ||
292 | return d->mRecipients; | ||
293 | } | ||
165 | 294 | ||
166 | class PartPrivate | 295 | class PartPrivate |
167 | { | 296 | { |
@@ -179,7 +308,9 @@ public: | |||
179 | void createMailMime(const MimeTreeParser::AlternativeMessagePart::Ptr &part); | 308 | void createMailMime(const MimeTreeParser::AlternativeMessagePart::Ptr &part); |
180 | void createMailMime(const MimeTreeParser::HtmlMessagePart::Ptr &part); | 309 | void createMailMime(const MimeTreeParser::HtmlMessagePart::Ptr &part); |
181 | 310 | ||
311 | static Encryption::Ptr createEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr& part); | ||
182 | void appendEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr &part); | 312 | void appendEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr &part); |
313 | static QVector<Signature::Ptr> createSignature(const MimeTreeParser::SignedMessagePart::Ptr& part); | ||
183 | void appendSignature(const MimeTreeParser::SignedMessagePart::Ptr &part); | 314 | void appendSignature(const MimeTreeParser::SignedMessagePart::Ptr &part); |
184 | 315 | ||
185 | void setSignatures(const QVector<Signature::Ptr> &sigs); | 316 | void setSignatures(const QVector<Signature::Ptr> &sigs); |
@@ -233,9 +364,43 @@ void PartPrivate::appendSubPart(Part::Ptr subpart) | |||
233 | mSubParts.append(subpart); | 364 | mSubParts.append(subpart); |
234 | } | 365 | } |
235 | 366 | ||
367 | Encryption::Ptr PartPrivate::createEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr& part) | ||
368 | { | ||
369 | Kleo::KeyListJob *job = part->mCryptoProto->keyListJob(false); // local, no sigs | ||
370 | if (!job) { | ||
371 | qWarning() << "The Crypto backend does not support listing keys. "; | ||
372 | return Encryption::Ptr(); | ||
373 | } | ||
374 | |||
375 | auto encpriv = new EncryptionPrivate(); | ||
376 | foreach(const auto &recipient, part->mDecryptRecipients) { | ||
377 | std::vector<GpgME::Key> found_keys; | ||
378 | const auto &keyid = recipient.keyID(); | ||
379 | GpgME::KeyListResult res = job->exec(QStringList(QLatin1String(keyid)), false, found_keys); | ||
380 | if (res.error()) { | ||
381 | qWarning() << "Error while searching key for Fingerprint: " << keyid; | ||
382 | continue; | ||
383 | } | ||
384 | if (found_keys.size() > 1) { | ||
385 | // Should not Happen | ||
386 | qWarning() << "Oops: Found more then one Key for Fingerprint: " << keyid; | ||
387 | } | ||
388 | if (found_keys.size() != 1) { | ||
389 | // Should not Happen at this point | ||
390 | qWarning() << "Oops: Found no Key for Fingerprint: " << keyid; | ||
391 | } else { | ||
392 | auto key = found_keys[0]; | ||
393 | auto keypriv = new KeyPrivate; | ||
394 | keypriv->mKey = key; | ||
395 | encpriv->mRecipients.push_back(Key::Ptr(new Key(keypriv))); | ||
396 | } | ||
397 | } | ||
398 | return Encryption::Ptr(new Encryption(encpriv)); | ||
399 | } | ||
400 | |||
236 | void PartPrivate::appendEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr& part) | 401 | void PartPrivate::appendEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr& part) |
237 | { | 402 | { |
238 | mEncryptions.append(Encryption::Ptr(new Encryption)); | 403 | mEncryptions.append(createEncryption(part)); |
239 | } | 404 | } |
240 | 405 | ||
241 | void PartPrivate::setEncryptions(const QVector< Encryption::Ptr >& encs) | 406 | void PartPrivate::setEncryptions(const QVector< Encryption::Ptr >& encs) |
@@ -243,9 +408,50 @@ void PartPrivate::setEncryptions(const QVector< Encryption::Ptr >& encs) | |||
243 | mEncryptions = encs; | 408 | mEncryptions = encs; |
244 | } | 409 | } |
245 | 410 | ||
411 | QVector<Signature::Ptr> PartPrivate::createSignature(const MimeTreeParser::SignedMessagePart::Ptr& part) | ||
412 | { | ||
413 | QVector<Signature::Ptr> sigs; | ||
414 | Kleo::KeyListJob *job = part->mCryptoProto->keyListJob(false); // local, no sigs | ||
415 | if (!job) { | ||
416 | qWarning() << "The Crypto backend does not support listing keys. "; | ||
417 | return sigs; | ||
418 | } | ||
419 | |||
420 | foreach(const auto &sig, part->mSignatures) { | ||
421 | auto sigpriv = new SignaturePrivate(); | ||
422 | sigpriv->mSignature = sig; | ||
423 | auto signature = std::make_shared<Signature>(sigpriv); | ||
424 | sigs.append(signature); | ||
425 | |||
426 | std::vector<GpgME::Key> found_keys; | ||
427 | const auto &keyid = sig.fingerprint(); | ||
428 | GpgME::KeyListResult res = job->exec(QStringList(QLatin1String(keyid)), false, found_keys); | ||
429 | if (res.error()) { | ||
430 | qWarning() << "Error while searching key for Fingerprint: " << keyid; | ||
431 | continue; | ||
432 | } | ||
433 | if (found_keys.size() > 1) { | ||
434 | // Should not Happen | ||
435 | qWarning() << "Oops: Found more then one Key for Fingerprint: " << keyid; | ||
436 | continue; | ||
437 | } | ||
438 | if (found_keys.size() != 1) { | ||
439 | // Should not Happen at this point | ||
440 | qWarning() << "Oops: Found no Key for Fingerprint: " << keyid; | ||
441 | continue; | ||
442 | } else { | ||
443 | auto key = found_keys[0]; | ||
444 | auto keypriv = new KeyPrivate; | ||
445 | keypriv->mKey = key; | ||
446 | sigpriv->mKey = Key::Ptr(new Key(keypriv)); | ||
447 | } | ||
448 | } | ||
449 | return sigs; | ||
450 | } | ||
451 | |||
246 | void PartPrivate::appendSignature(const MimeTreeParser::SignedMessagePart::Ptr& part) | 452 | void PartPrivate::appendSignature(const MimeTreeParser::SignedMessagePart::Ptr& part) |
247 | { | 453 | { |
248 | mSignatures.append(Signature::Ptr(new Signature)); | 454 | mSignatures.append(createSignature(part)); |
249 | } | 455 | } |
250 | 456 | ||
251 | 457 | ||
@@ -361,12 +567,12 @@ public: | |||
361 | 567 | ||
362 | void ContentPrivate::appendEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr& enc) | 568 | void ContentPrivate::appendEncryption(const MimeTreeParser::EncryptedMessagePart::Ptr& enc) |
363 | { | 569 | { |
364 | mEncryptions.append(Encryption::Ptr(new Encryption)); | 570 | mEncryptions.append(PartPrivate::createEncryption(enc)); |
365 | } | 571 | } |
366 | 572 | ||
367 | void ContentPrivate::appendSignature(const MimeTreeParser::SignedMessagePart::Ptr& sig) | 573 | void ContentPrivate::appendSignature(const MimeTreeParser::SignedMessagePart::Ptr& sig) |
368 | { | 574 | { |
369 | mSignatures.append(Signature::Ptr(new Signature)); | 575 | mSignatures.append(PartPrivate::createSignature(sig)); |
370 | } | 576 | } |
371 | 577 | ||
372 | 578 | ||
@@ -638,54 +844,6 @@ PartPrivate* SinglePart::reachParentD() const | |||
638 | return Part::d.get(); | 844 | return Part::d.get(); |
639 | } | 845 | } |
640 | 846 | ||
641 | class SignaturePrivate | ||
642 | { | ||
643 | public: | ||
644 | Signature *q; | ||
645 | }; | ||
646 | |||
647 | Signature::Signature() | ||
648 | :d(std::unique_ptr<SignaturePrivate>(new SignaturePrivate)) | ||
649 | { | ||
650 | d->q = this; | ||
651 | } | ||
652 | |||
653 | |||
654 | Signature::Signature(SignaturePrivate *d_ptr) | ||
655 | :d(std::unique_ptr<SignaturePrivate>(d_ptr)) | ||
656 | { | ||
657 | d->q = this; | ||
658 | } | ||
659 | |||
660 | Signature::~Signature() | ||
661 | { | ||
662 | |||
663 | } | ||
664 | |||
665 | |||
666 | class EncryptionPrivate | ||
667 | { | ||
668 | public: | ||
669 | Encryption *q; | ||
670 | }; | ||
671 | |||
672 | Encryption::Encryption(EncryptionPrivate *d_ptr) | ||
673 | :d(std::unique_ptr<EncryptionPrivate>(d_ptr)) | ||
674 | { | ||
675 | d->q = this; | ||
676 | } | ||
677 | |||
678 | Encryption::Encryption() | ||
679 | :d(std::unique_ptr<EncryptionPrivate>(new EncryptionPrivate)) | ||
680 | { | ||
681 | d->q = this; | ||
682 | } | ||
683 | |||
684 | Encryption::~Encryption() | ||
685 | { | ||
686 | |||
687 | } | ||
688 | |||
689 | ParserPrivate::ParserPrivate(Parser* parser) | 847 | ParserPrivate::ParserPrivate(Parser* parser) |
690 | : q(parser) | 848 | : q(parser) |
691 | , mNodeHelper(std::make_shared<MimeTreeParser::NodeHelper>()) | 849 | , mNodeHelper(std::make_shared<MimeTreeParser::NodeHelper>()) |