1 /*
2   SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
3 
4    SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "gnupgwksmessagepart.h"
8 
9 #include <KMime/Content>
10 #include <MimeTreeParser/BodyPart>
11 
GnuPGWKSMessagePart(MimeTreeParser::Interface::BodyPart * part)12 GnuPGWKSMessagePart::GnuPGWKSMessagePart(MimeTreeParser::Interface::BodyPart *part)
13     : MimeTreeParser::MessagePart(part->objectTreeParser(), QString())
14 {
15     setContent(part->content());
16     parseContent(content());
17 }
18 
confirmationType() const19 GnuPGWKSMessagePart::ConfirmationType GnuPGWKSMessagePart::confirmationType() const
20 {
21     return mType;
22 }
23 
address() const24 QString GnuPGWKSMessagePart::address() const
25 {
26     return mAddress;
27 }
28 
sender() const29 QString GnuPGWKSMessagePart::sender() const
30 {
31     return mSender;
32 }
33 
fingerprint() const34 QString GnuPGWKSMessagePart::fingerprint() const
35 {
36     return mFingerprint;
37 }
38 
nonce() const39 QString GnuPGWKSMessagePart::nonce() const
40 {
41     return mNonce;
42 }
43 
stringToType(const QStringRef & str)44 GnuPGWKSMessagePart::ConfirmationType GnuPGWKSMessagePart::stringToType(const QStringRef &str)
45 {
46     if (str == QLatin1String("confirmation-request")) {
47         return ConfirmationRequest;
48     } else if (str == QLatin1String("confirmation-response")) {
49         return ConfirmationResponse;
50     } else {
51         return UnknownType;
52     }
53 }
54 
parseContent(KMime::Content * node)55 void GnuPGWKSMessagePart::parseContent(KMime::Content *node)
56 {
57     const auto text = QString::fromUtf8(node->decodedContent());
58     const auto lines = text.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
59     // https://tools.ietf.org/id/draft-koch-openpgp-webkey-service-02.txt
60     // sections 4.3 and 4.4
61     for (const auto &line : lines) {
62         if (line.startsWith(QLatin1String("type:"))) {
63             mType = stringToType(line.midRef(sizeof("type:") - 1).trimmed());
64         } else if (line.startsWith(QLatin1String("sender:"))) {
65             mSender = line.midRef(sizeof("sender:") - 1).trimmed().toString();
66         } else if (line.startsWith(QLatin1String("address:"))) {
67             mAddress = line.midRef(sizeof("address:") - 1).trimmed().toString();
68         } else if (line.startsWith(QLatin1String("fingerprint:"))) {
69             mFingerprint = line.midRef(sizeof("fingerprint:") - 1).trimmed().toString();
70         } else if (line.startsWith(QLatin1String("nonce:"))) {
71             mNonce = line.midRef(sizeof("nonce:") - 1).trimmed().toString();
72         }
73     }
74 }
75