File indexing completed on 2025-01-19 04:46:49

0001 /*
0002   SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "gnupgwksmessagepart.h"
0008 
0009 #include <KMime/Content>
0010 #include <MimeTreeParser/BodyPart>
0011 
0012 GnuPGWKSMessagePart::GnuPGWKSMessagePart(MimeTreeParser::Interface::BodyPart *part)
0013     : MimeTreeParser::MessagePart(part->objectTreeParser(), QString())
0014 {
0015     setContent(part->content());
0016     parseContent(content());
0017 }
0018 
0019 GnuPGWKSMessagePart::ConfirmationType GnuPGWKSMessagePart::confirmationType() const
0020 {
0021     return mType;
0022 }
0023 
0024 QString GnuPGWKSMessagePart::address() const
0025 {
0026     return mAddress;
0027 }
0028 
0029 QString GnuPGWKSMessagePart::sender() const
0030 {
0031     return mSender;
0032 }
0033 
0034 QString GnuPGWKSMessagePart::fingerprint() const
0035 {
0036     return mFingerprint;
0037 }
0038 
0039 QString GnuPGWKSMessagePart::nonce() const
0040 {
0041     return mNonce;
0042 }
0043 
0044 GnuPGWKSMessagePart::ConfirmationType GnuPGWKSMessagePart::stringToType(const QStringView &str)
0045 {
0046     if (str == QLatin1StringView("confirmation-request")) {
0047         return ConfirmationRequest;
0048     } else if (str == QLatin1StringView("confirmation-response")) {
0049         return ConfirmationResponse;
0050     } else {
0051         return UnknownType;
0052     }
0053 }
0054 
0055 void GnuPGWKSMessagePart::parseContent(KMime::Content *node)
0056 {
0057     const auto text = QString::fromUtf8(node->decodedContent());
0058     const auto lines = text.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
0059     // https://tools.ietf.org/id/draft-koch-openpgp-webkey-service-02.txt
0060     // sections 4.3 and 4.4
0061     for (const auto &line : lines) {
0062         if (line.startsWith(QLatin1StringView("type:"))) {
0063             mType = stringToType(QStringView(line).mid(sizeof("type:") - 1).trimmed());
0064         } else if (line.startsWith(QLatin1StringView("sender:"))) {
0065             mSender = QStringView(line).mid(sizeof("sender:") - 1).trimmed().toString();
0066         } else if (line.startsWith(QLatin1StringView("address:"))) {
0067             mAddress = QStringView(line).mid(sizeof("address:") - 1).trimmed().toString();
0068         } else if (line.startsWith(QLatin1StringView("fingerprint:"))) {
0069             mFingerprint = QStringView(line).mid(sizeof("fingerprint:") - 1).trimmed().toString();
0070         } else if (line.startsWith(QLatin1StringView("nonce:"))) {
0071             mNonce = QStringView(line).mid(sizeof("nonce:") - 1).trimmed().toString();
0072         }
0073     }
0074 }
0075 
0076 #include "moc_gnupgwksmessagepart.cpp"