gnupgwksurlhandler.cpp 6.36 KB
Newer Older
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
/*
  Copyright (c) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>

   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.
*/

#include "gnupgwksurlhandler.h"
#include "gnupgwksmessagepart.h"
#include "gnupgwks_debug.h"

#include <QString>
#include <QUrlQuery>
#include <QProcess>

#include <MimeTreeParser/BodyPart>
#include <MimeTreeParser/NodeHelper>
#include <MessageViewer/Viewer>

#include <QGpgME/Protocol>
#include <QGpgME/WKSPublishJob>

#include <MailTransport/Transport>
#include <MailTransport/TransportManager>
Laurent Montel's avatar
Laurent Montel committed
37
#include <MailTransportAkonadi/MessageQueueJob>
38 39 40 41 42 43 44 45 46 47 48 49 50

#include <KIdentityManagement/IdentityManager>
#include <KIdentityManagement/Identity>

#include <KMime/Message>
#include <KMime/Util>

#include <AkonadiCore/ItemDeleteJob>

#include <KLocalizedString>

using namespace MimeTreeParser::Interface;

Laurent Montel's avatar
Laurent Montel committed
51
bool ApplicationGnuPGWKSUrlHandler::handleContextMenuRequest(BodyPart *, const QString &, const QPoint &) const
52 53 54 55
{
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
56
bool ApplicationGnuPGWKSUrlHandler::handleClick(MessageViewer::Viewer *viewerInstance, BodyPart *part, const QString &path) const
57 58 59 60 61 62 63 64 65 66
{
    Q_UNUSED(viewerInstance);

    if (!path.startsWith(QLatin1String("gnupgwks?"))) {
        return false;
    }

    const QUrlQuery q(path.mid(sizeof("gnupgwks?") - 1));
    if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("show")) {
        QProcess::startDetached(QStringLiteral("kleopatra"),
Laurent Montel's avatar
Laurent Montel committed
67
                                { QStringLiteral("--query"), q.queryItemValue(QStringLiteral("fpr")) });
68 69 70 71 72 73 74 75 76 77 78 79
        return true;
    } else if (q.queryItemValue(QStringLiteral("action")) == QLatin1String("confirm")) {
        GnuPGWKSMessagePart mp(part);
        if (!sendConfirmation(viewerInstance, mp)) {
            part->nodeHelper()->setProperty("__GnuPGWKS" + mp.fingerprint().toLatin1(), QStringLiteral("error"));
        }
        return true;
    }

    return false;
}

Laurent Montel's avatar
Laurent Montel committed
80
QString ApplicationGnuPGWKSUrlHandler::statusBarMessage(BodyPart *part, const QString &path) const
81 82 83 84 85 86 87 88
{
    Q_UNUSED(part);

    if (!path.startsWith(QLatin1String("gnupgwks?"))) {
        return QString();
    }

    const QUrlQuery q(path.mid(sizeof("gnupgwks?") - 1));
Laurent Montel's avatar
Laurent Montel committed
89 90
    const QString actionStr = q.queryItemValue(QStringLiteral("action"));
    if (actionStr == QLatin1String("show")) {
91
        return i18n("Display key details");
Laurent Montel's avatar
Laurent Montel committed
92
    } else if (actionStr == QLatin1String("confirm")) {
93 94 95 96 97 98 99 100 101 102 103
        return i18n("Publish the key");
    }
    return QString();
}

QByteArray ApplicationGnuPGWKSUrlHandler::createConfirmation(const KMime::Message::Ptr &msg) const
{
    auto job = QGpgME::openpgp()->wksPublishJob();
    QEventLoop el;
    QByteArray result;
    QObject::connect(job, &QGpgME::WKSPublishJob::result,
Laurent Montel's avatar
Laurent Montel committed
104 105
                     [&el, &result](const GpgME::Error &, const QByteArray &returnedData,
                                    const QByteArray &returnedError) {
Laurent Montel's avatar
Laurent Montel committed
106 107 108 109 110 111
        if (returnedData.isEmpty()) {
            qCWarning(GNUPGWKS_LOG) << "GPG:" << returnedError;
        }
        result = returnedData;
        el.quit();
    });
112 113 114 115 116 117
    job->startReceive(msg->encodedContent());
    el.exec();

    return result;
}

Laurent Montel's avatar
Laurent Montel committed
118
bool ApplicationGnuPGWKSUrlHandler::sendConfirmation(MessageViewer::Viewer *viewerInstance, const GnuPGWKSMessagePart &mp) const
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
{
    const QByteArray data = createConfirmation(viewerInstance->message());
    if (data.isEmpty()) {
        return false;
    }

    auto msg = KMime::Message::Ptr::create();
    msg->setContent(KMime::CRLFtoLF(data));
    msg->parse();

    // Find identity
    const auto identity = KIdentityManagement::IdentityManager::self()->identityForAddress(mp.address());
    const bool nullIdentity = (identity == KIdentityManagement::Identity::null());
    if (!nullIdentity) {
        KMime::Headers::Generic *x_header = new KMime::Headers::Generic("X-KMail-Identity");
        x_header->from7BitString(QByteArray::number(identity.uoid()));
        msg->setHeader(x_header);
    }

    // Find transport set in the identity, fallback to default transport
    auto transportMgr = MailTransport::TransportManager::self();
    const bool identityHasTransport = !identity.transport().isEmpty();
    int transportId = -1;
    if (!nullIdentity && identityHasTransport) {
        transportId = identity.transport().toInt();
    } else {
        transportId = transportMgr->defaultTransportId();
    }
    // No transport exists, ask user to create one
    if (transportId == -1) {
Laurent Montel's avatar
Laurent Montel committed
149
        if (!transportMgr->showTransportCreationDialog(nullptr, MailTransport::TransportManager::IfNoTransportExists)) {
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
            return false;
        }
        transportId = transportMgr->defaultTransportId();
    }
    auto header = new KMime::Headers::Generic("X-KMail-Transport");
    header->fromUnicodeString(QString::number(transportId), "utf-8");
    msg->setHeader(header);

    // Build the message
    msg->assemble();

    // Move to outbox
    auto transport = transportMgr->transportById(transportId);
    auto job = new MailTransport::MessageQueueJob;
    job->addressAttribute().setTo({ msg->to(false)->asUnicodeString() });
    job->transportAttribute().setTransportId(transport->id());
    job->addressAttribute().setFrom(msg->from(false)->asUnicodeString());
    job->sentBehaviourAttribute().setSentBehaviour(MailTransport::SentBehaviourAttribute::Delete);
    job->sentBehaviourAttribute().setSendSilently(true);
    job->setMessage(msg);

    // Send
    if (!job->exec()) {
        qCWarning(GNUPGWKS_LOG) << "Error queuing message in output:" << job->errorText();
        return false;
    }

    // Delete the original request
    // Don't use viewerInstance->deleteMessage(), which triggers Move To Trash,
    // we want to get rid of the message for good.
    new Akonadi::ItemDeleteJob(viewerInstance->messageItem());
    return true;
}