diff --git a/src/resolver/resolver.cpp b/src/resolver/resolver.cpp index 57a6f31..34243c2 100644 --- a/src/resolver/resolver.cpp +++ b/src/resolver/resolver.cpp @@ -1,177 +1,176 @@ /* Copyright (C) 2018 by Intevation GmbH * * This file is free software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ #include "resolver.h" #include "util/overlay.h" #include #include #include #include #include #include #include class Resolver::Private { public: Private(Resolver *qq): q(qq) { } void printResolvedKeys(const Kleo::KeyResolver *kr) { const auto sigMap = kr->signingKeys(); for (const auto fmt: sigMap.keys()) { for (const auto &key: sigMap[fmt]) { std::string fmtStr; if (fmt == GpgME::CMS) { fmtStr = "smime"; } else { fmtStr = "openpgp"; } if (!key.isNull()) { std::cout << "sig:" << fmtStr << ":" << key.primaryFingerprint() << std::endl; } } } const auto encMap = kr->encryptionKeys(); for (const auto fmt: encMap.keys()) { for (const auto &mbox: encMap[fmt].keys()) { for (const auto &key: encMap[fmt][mbox]) { std::string fmtStr; if (fmt == GpgME::CMS) { fmtStr = "smime"; } else { fmtStr = "openpgp"; } if (!key.isNull()) { std::cout << "enc:" << fmtStr << ":" << key.primaryFingerprint() << ":" << mbox.toUtf8().constData() << std::endl; } } } } } void newOverlay(WId wid, const QString &text) { m_overlay = std::shared_ptr(new Overlay(wid, text)); } void newResolver(const QCommandLineParser &parser) { const auto proto = parser.value(QStringLiteral("protocol")).toLower(); GpgME::Protocol format; if (proto == QStringLiteral("cms")) { format = GpgME::CMS; } else if (proto == QStringLiteral("pgp")) { format = GpgME::OpenPGP; } else { format = GpgME::UnknownProtocol; } const auto preferredVal = parser.value(QStringLiteral("preferred-protocol")).toLower(); GpgME::Protocol preferred = GpgME::UnknownProtocol; if (preferredVal == QStringLiteral("cms")) { preferred = GpgME::CMS; } else if (preferredVal == QStringLiteral("pgp")) { preferred = GpgME::OpenPGP; } QMap > overrides; for (const QString &oride: parser.values("override")) { const QStringList split = oride.split(QLatin1Char(':')); GpgME::Protocol fmt = GpgME::UnknownProtocol; if (split.size() < 2 || split.size() > 3) { qDebug() << "Invalid override format"; std::cout << "cancel" << std::endl; qApp->quit(); } if (split.size() == 3) { const QString fmtStr = split[2].toLower(); if (fmtStr == "openpgp") { fmt = GpgME::OpenPGP; } else if (fmtStr == "smime") { fmt = GpgME::CMS; } else if (fmtStr == "auto") { fmt = GpgME::UnknownProtocol; } else { qDebug() << "Invalid override format string"; std::cout << "cancel" << std::endl; qApp->quit(); } } const QStringList fingerprints = split[1].split(QLatin1Char(',')); auto map = overrides.value(fmt); map.insert(split[0], fingerprints); overrides.insert(fmt, map); qDebug () << "Passing overrides" << fingerprints << split[0]; } const auto recps = parser.positionalArguments(); auto *kr = new Kleo::KeyResolver (!recps.isEmpty() || parser.isSet(QStringLiteral("encrypt")) /* encrypt */, parser.isSet(QStringLiteral("sign")) /*sign*/, format /* CryptoMesssageFormat */, parser.isSet("allowMixed")/* AllowMixed */); kr->setRecipients(recps); kr->setSender(parser.value("sender")); kr->setOverrideKeys(overrides); kr->setPreferredProtocol(preferred); connect (kr, &Kleo::KeyResolver::keysResolved, q, [this, kr] (bool success, bool sendUnencrypted) { if (!success) { std::cout << "cancel" << std::endl; } else if (sendUnencrypted) { std::cout << "unencrypted" << std::endl; } else { printResolvedKeys(kr); } delete kr; qApp->quit(); }); kr->setDialogWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); kr->start(parser.isSet(QStringLiteral("alwaysShow")), m_overlay.get()); } Resolver *q; std::shared_ptr m_overlay; }; Resolver::Resolver(int &argc, char *argv[]) : QApplication(argc, argv), d(new Private(this)) { } -QString Resolver::newInstance(const QCommandLineParser &parser, - const QString &workingDirectry) +QString Resolver::newInstance(const QCommandLineParser &parser) { const auto hwnd = parser.value(QStringLiteral("hwnd")); if (!hwnd.isEmpty()) { bool ok; WId id = (WId) hwnd.toInt(&ok); if (!ok) { std::cerr << "invalid hwnd value" << std::endl; exit(EXIT_FAILURE); } d->newOverlay(id, parser.value(QStringLiteral("overlayText"))); } d->newResolver(parser); return QString(); } diff --git a/src/resolver/resolver.h b/src/resolver/resolver.h index 5c59fa9..b686bc5 100644 --- a/src/resolver/resolver.h +++ b/src/resolver/resolver.h @@ -1,62 +1,60 @@ #ifndef RESOLVER_H #define RESOLVER_H /* Copyright (C) 2018 by Intevation GmbH * * This file is free software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ #include #include class QCommandLineParser; class QStringList; class QString; /** @brief Resolver Application entry point. * * The Resolver application exists to resolve Mailboxes * to key fingerprints. A typical task for a MUA. * * The main user of this app is GpgOL so it is tailored * to GpgOL's needs. * * This is the global qApp singleton. */ class Resolver: public QApplication { Q_OBJECT public: /** Create a new Application obejct. You have to * make sure to call init afterwards to get a valid object. * This is to delay initialisation after the UniqueService * call is done and our init / call might be forwarded to * another instance. */ explicit Resolver(int &argc, char *argv[]); static Resolver *instance() { return static_cast (qApp); } /** Starts a new instance or a command from the command line. * * Handles the parser options and starts the according commands. * The parser should have been initialized with options and * already processed. * * @param parser: The command line parser to use. - * @param workingDirectory: Optional working directory for file arguments. * * @returns an empty QString on success. A error message otherwise. * */ - QString newInstance(const QCommandLineParser &parser, - const QString &workingDirectory = QString()); + QString newInstance(const QCommandLineParser &parser); private: class Private; std::shared_ptr d; }; #endif