Line data Source code
1 : /*
2 : qgpgmekeyformailboxjob.cpp
3 :
4 : This file is part of qgpgme, the Qt API binding for gpgme
5 : Copyright (c) 2016 Intevation GmbH
6 :
7 : QGpgME is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License as
9 : published by the Free Software Foundation; either version 2 of the
10 : License, or (at your option) any later version.
11 :
12 : QGpgME is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 :
21 : In addition, as a special exception, the copyright holders give
22 : permission to link the code of this program with any edition of
23 : the Qt library by Trolltech AS, Norway (or with modified versions
24 : of Qt that use the same license as Qt), and distribute linked
25 : combinations including the two. You must obey the GNU General
26 : Public License in all respects for all of the code used other than
27 : Qt. If you modify this file, you may extend this exception to
28 : your version of the file, but you are not obligated to do so. If
29 : you do not wish to do so, delete this exception statement from
30 : your version.
31 : */
32 :
33 : #ifdef HAVE_CONFIG_H
34 : #include "config.h"
35 : #endif
36 :
37 : #include "qgpgmekeyformailboxjob.h"
38 : #include "qgpgmekeylistjob.h"
39 :
40 : #include <QStringList>
41 :
42 : #include <tuple>
43 :
44 : using namespace GpgME;
45 : using namespace QGpgME;
46 :
47 0 : QGpgMEKeyForMailboxJob::QGpgMEKeyForMailboxJob(Context *context)
48 0 : : mixin_type(context)
49 : {
50 0 : lateInitialization();
51 0 : }
52 :
53 0 : QGpgMEKeyForMailboxJob::~QGpgMEKeyForMailboxJob() {}
54 :
55 0 : static bool keyIsOk(const Key k)
56 : {
57 0 : return !k.isExpired() && !k.isRevoked() && !k.isInvalid() && !k.isDisabled();
58 : }
59 :
60 0 : static bool uidIsOk(const UserID uid)
61 : {
62 0 : return keyIsOk(uid.parent()) && !uid.isRevoked() && !uid.isInvalid();
63 : }
64 :
65 0 : static bool subkeyIsOk(const Subkey s)
66 : {
67 0 : return !s.isRevoked() && !s.isInvalid() && !s.isDisabled();
68 : }
69 :
70 0 : static QGpgMEKeyForMailboxJob::result_type do_work(Context *ctx, const QString &mailbox, bool canEncrypt)
71 : {
72 : /* Do a Keylisting. */
73 0 : ctx->setKeyListMode(GpgME::Extern | GpgME::Local | GpgME::Signatures | GpgME::Validate);
74 0 : std::vector<Key> keys;
75 0 : QGpgMEKeyListJob *keylist = new QGpgMEKeyListJob(ctx);
76 :
77 0 : KeyListResult result = keylist->exec(QStringList() << mailbox, false, keys);
78 :
79 0 : if (result.error()) {
80 0 : return std::make_tuple(result, Key(), UserID(), QString(), Error());
81 : }
82 :
83 : // This should ideally be decided by GnuPG and this Job changed
84 : // to just call the according API in GpgME
85 : // See: https://bugs.gnupg.org/gnupg/issue2359
86 0 : Key keyC;
87 0 : UserID uidC;
88 0 : Q_FOREACH (const Key k, keys) {
89 0 : if (canEncrypt && !k.canEncrypt()) {
90 0 : continue;
91 : }
92 : /* First get the uid that matches the mailbox */
93 0 : Q_FOREACH (const UserID u, k.userIDs()) {
94 0 : if (QString::fromUtf8(u.email()).toLower() == mailbox.toLower()) {
95 0 : if (uidC.isNull()) {
96 0 : keyC = k;
97 0 : uidC = u;
98 0 : } else if ((!uidIsOk(uidC) && uidIsOk(u)) || uidC.validity() < u.validity()) {
99 : /* Validity of the new key is better. */
100 0 : uidC = u;
101 0 : keyC = k;
102 0 : } else if (uidC.validity() == u.validity() && uidIsOk(u)) {
103 : /* Both are the same check which one is newer. */
104 0 : time_t oldTime = 0;
105 0 : Q_FOREACH (const Subkey s, keyC.subkeys()) {
106 0 : if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
107 0 : oldTime = s.creationTime();
108 : }
109 0 : }
110 0 : time_t newTime = 0;
111 0 : Q_FOREACH (const Subkey s, k.subkeys()) {
112 0 : if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
113 0 : newTime = s.creationTime();
114 : }
115 0 : }
116 0 : if (newTime > oldTime) {
117 0 : uidC = u;
118 0 : keyC = k;
119 : }
120 : }
121 : }
122 0 : }
123 0 : }
124 0 : return std::make_tuple(result, keyC, uidC, QString(), Error());
125 : }
126 :
127 0 : Error QGpgMEKeyForMailboxJob::start(const QString &mailbox, bool canEncrypt)
128 : {
129 0 : run(std::bind(&do_work, std::placeholders::_1, mailbox, canEncrypt));
130 0 : return Error();
131 : }
132 :
133 0 : KeyListResult QGpgMEKeyForMailboxJob::exec(const QString &mailbox, bool canEncrypt, Key &key, UserID &uid)
134 : {
135 0 : const result_type r = do_work(context(), mailbox, canEncrypt);
136 0 : resultHook(r);
137 0 : key = std::get<1>(r);
138 0 : uid = std::get<2>(r);
139 0 : return std::get<0>(r);
140 : }
141 :
142 : #include "qgpgmekeyformailboxjob.moc"
|