Differential D3140 Diff 8050 plugins/messageviewer/bodypartformatter/gnupgwks/gnupgwksurlhandler.cpp
Changeset View
Changeset View
Standalone View
Standalone View
plugins/messageviewer/bodypartformatter/gnupgwks/gnupgwksurlhandler.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> | ||||
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 | #include "gnupgwksurlhandler.h" | ||||
21 | #include "gnupgwksmessagepart.h" | ||||
22 | #include "gnupgwks_debug.h" | ||||
23 | | ||||
24 | #include <QString> | ||||
25 | #include <QUrlQuery> | ||||
26 | #include <QProcess> | ||||
27 | | ||||
28 | #include <MimeTreeParser/BodyPart> | ||||
29 | #include <MimeTreeParser/NodeHelper> | ||||
30 | #include <MessageViewer/Viewer> | ||||
31 | | ||||
32 | #include <QGpgME/Protocol> | ||||
33 | #include <QGpgME/WKSPublishJob> | ||||
34 | | ||||
35 | #include <MailTransport/Transport> | ||||
36 | #include <MailTransport/TransportManager> | ||||
37 | #include <MailTransport/MessageQueueJob> | ||||
38 | | ||||
39 | #include <KIdentityManagement/IdentityManager> | ||||
40 | #include <KIdentityManagement/Identity> | ||||
41 | | ||||
42 | #include <KMime/Message> | ||||
43 | #include <KMime/Util> | ||||
44 | | ||||
45 | #include <AkonadiCore/ItemDeleteJob> | ||||
46 | | ||||
47 | #include <KLocalizedString> | ||||
48 | | ||||
49 | using namespace MimeTreeParser::Interface; | ||||
50 | | ||||
51 | bool ApplicationGnuPGWKSUrlHandler::handleContextMenuRequest(BodyPart *, | ||||
52 | const QString &, | ||||
53 | const QPoint &) const | ||||
54 | { | ||||
55 | return false; | ||||
56 | } | ||||
57 | | ||||
58 | bool ApplicationGnuPGWKSUrlHandler::handleClick(MessageViewer::Viewer *viewerInstance, | ||||
59 | BodyPart *part, const QString &path) const | ||||
60 | { | ||||
61 | Q_UNUSED(viewerInstance); | ||||
62 | | ||||
63 | if (!path.startsWith(QLatin1String("gnupgwks?"))) { | ||||
64 | return false; | ||||
65 | } | ||||
66 | | ||||
67 | const QUrlQuery q(path.mid(sizeof("gnupgwks?") - 1)); | ||||
68 | if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("show")) { | ||||
69 | QProcess::startDetached(QStringLiteral("kleopatra"), | ||||
70 | { QStringLiteral("--query"), q.queryItemValue(QStringLiteral("fpr")) }); | ||||
71 | return true; | ||||
72 | } else if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("confirm")) { | ||||
73 | GnuPGWKSMessagePart mp(part); | ||||
74 | if (!sendConfirmation(viewerInstance, mp)) { | ||||
75 | part->nodeHelper()->setProperty("__GnuPGWKS" + mp.fingerprint().toLatin1(), QStringLiteral("error")); | ||||
76 | } | ||||
77 | return true; | ||||
78 | } | ||||
79 | | ||||
80 | return false; | ||||
81 | } | ||||
82 | | ||||
83 | QString ApplicationGnuPGWKSUrlHandler::statusBarMessage(BodyPart* part, const QString &path) const | ||||
84 | { | ||||
85 | Q_UNUSED(part); | ||||
86 | | ||||
87 | if (!path.startsWith(QLatin1String("gnupgwks?"))) { | ||||
88 | return QString(); | ||||
89 | } | ||||
90 | | ||||
91 | const QUrlQuery q(path.mid(sizeof("gnupgwks?") - 1)); | ||||
92 | if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("show")) { | ||||
93 | return i18n("Display key details"); | ||||
94 | } else if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("confirm")) { | ||||
95 | return i18n("Publish the key"); | ||||
96 | } | ||||
97 | return QString(); | ||||
98 | } | ||||
99 | | ||||
100 | | ||||
101 | QByteArray ApplicationGnuPGWKSUrlHandler::createConfirmation(const KMime::Message::Ptr &msg) const | ||||
102 | { | ||||
103 | auto job = QGpgME::openpgp()->wksPublishJob(); | ||||
104 | QEventLoop el; | ||||
105 | QByteArray result; | ||||
106 | QObject::connect(job, &QGpgME::WKSPublishJob::result, | ||||
107 | [&el, &result](const GpgME::Error &, const QByteArray &returnedData, | ||||
108 | const QByteArray &returnedError) | ||||
109 | { | ||||
110 | if (returnedData.isEmpty()) { | ||||
111 | qCWarning(GNUPGWKS_LOG) << "GPG:" << returnedError; | ||||
112 | } | ||||
113 | result = returnedData; | ||||
114 | el.quit(); | ||||
115 | }); | ||||
116 | job->startReceive(msg->encodedContent()); | ||||
117 | el.exec(); | ||||
118 | | ||||
119 | return result; | ||||
120 | } | ||||
121 | | ||||
122 | | ||||
123 | bool ApplicationGnuPGWKSUrlHandler::sendConfirmation(MessageViewer::Viewer *viewerInstance, | ||||
124 | const GnuPGWKSMessagePart &mp) const | ||||
125 | { | ||||
126 | | ||||
127 | | ||||
128 | const QByteArray data = createConfirmation(viewerInstance->message()); | ||||
129 | if (data.isEmpty()) { | ||||
130 | return false; | ||||
131 | } | ||||
132 | | ||||
133 | auto msg = KMime::Message::Ptr::create(); | ||||
134 | msg->setContent(KMime::CRLFtoLF(data)); | ||||
135 | msg->parse(); | ||||
136 | | ||||
137 | // Find identity | ||||
138 | const auto identity = KIdentityManagement::IdentityManager::self()->identityForAddress(mp.address()); | ||||
139 | const bool nullIdentity = (identity == KIdentityManagement::Identity::null()); | ||||
140 | if (!nullIdentity) { | ||||
141 | KMime::Headers::Generic *x_header = new KMime::Headers::Generic("X-KMail-Identity"); | ||||
142 | x_header->from7BitString(QByteArray::number(identity.uoid())); | ||||
143 | msg->setHeader(x_header); | ||||
144 | } | ||||
145 | | ||||
146 | // Find transport set in the identity, fallback to default transport | ||||
147 | auto transportMgr = MailTransport::TransportManager::self(); | ||||
148 | const bool identityHasTransport = !identity.transport().isEmpty(); | ||||
149 | int transportId = -1; | ||||
150 | if (!nullIdentity && identityHasTransport) { | ||||
151 | transportId = identity.transport().toInt(); | ||||
152 | } else { | ||||
153 | transportId = transportMgr->defaultTransportId(); | ||||
154 | } | ||||
155 | // No transport exists, ask user to create one | ||||
156 | if (transportId == -1) { | ||||
157 | if (!transportMgr->showTransportCreationDialog(0, MailTransport::TransportManager::IfNoTransportExists)) { | ||||
158 | return false; | ||||
159 | } | ||||
160 | transportId = transportMgr->defaultTransportId(); | ||||
161 | } | ||||
162 | auto header = new KMime::Headers::Generic("X-KMail-Transport"); | ||||
163 | header->fromUnicodeString(QString::number(transportId), "utf-8"); | ||||
164 | msg->setHeader(header); | ||||
165 | | ||||
166 | // Build the message | ||||
167 | msg->assemble(); | ||||
168 | | ||||
169 | // Move to outbox | ||||
170 | auto transport = transportMgr->transportById(transportId); | ||||
171 | auto job = new MailTransport::MessageQueueJob; | ||||
172 | job->addressAttribute().setTo({ msg->to(false)->asUnicodeString() }); | ||||
173 | job->transportAttribute().setTransportId(transport->id()); | ||||
174 | job->addressAttribute().setFrom(msg->from(false)->asUnicodeString()); | ||||
175 | job->sentBehaviourAttribute().setSentBehaviour(MailTransport::SentBehaviourAttribute::Delete); | ||||
176 | job->sentBehaviourAttribute().setSendSilently(true); | ||||
177 | job->setMessage(msg); | ||||
178 | | ||||
179 | // Send | ||||
180 | if (!job->exec()) { | ||||
181 | qCWarning(GNUPGWKS_LOG) << "Error queuing message in output:" << job->errorText(); | ||||
182 | return false; | ||||
183 | } | ||||
184 | | ||||
185 | // Delete the original request | ||||
186 | // Don't use viewerInstance->deleteMessage(), which triggers Move To Trash, | ||||
187 | // we want to get rid of the message for good. | ||||
188 | new Akonadi::ItemDeleteJob(viewerInstance->messageItem()); | ||||
189 | return true; | ||||
190 | } |