File indexing completed on 2024-05-12 16:25:55

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "otr.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonObject>
0010 
0011 Otr::Otr() = default;
0012 
0013 Otr::~Otr() = default;
0014 
0015 void Otr::extractRoomUser(const QJsonObject &obj)
0016 {
0017     mRoomId = obj.value(QLatin1String("roomId")).toString();
0018     mUserId = obj.value(QLatin1String("userId")).toString();
0019 }
0020 
0021 void Otr::parseOtr(const QJsonArray &contents)
0022 {
0023     // QJsonArray(["handshake",{"publicKey":"{\"crv\":\"P-256\",\"ext\":true,\"key_ops\":[],\"kty\":\"EC\",\"x\":\"R9TKy7SvVpbJurHngvOICZ5oBHvLt_P19RiBX7-ChBs\",\"y\":\"Ama4y0Sk5DWFRAImF8_4u--qKknOa44EP5hr0VXuEvM\"}","roomId":"4faACeGzSvG7xMcTyYbwG4T2uB3wZSZSKB","userId":"YbwG4T2uB3wZSZSKB"}])
0024     qCDebug(RUQOLA_LOG) << " contents " << contents;
0025     const QString type = contents.at(0).toString();
0026     if (type == QLatin1String("end")) {
0027         const QJsonObject obj = contents.at(1).toObject();
0028         extractRoomUser(obj);
0029         qCDebug(RUQOLA_LOG) << " END" << obj << " roomId " << mRoomId << " userId " << mUserId;
0030         mType = Otr::End;
0031     } else if (type == QLatin1String("handshake")) {
0032         // qDebug() << " HANDSHAKE" << contents.at(1).toObject();
0033         const QJsonObject obj = contents.at(1).toObject();
0034         extractRoomUser(obj);
0035         const QString publicKey = obj.value(QLatin1String("publicKey")).toString();
0036         qCDebug(RUQOLA_LOG) << " HANDSHAKE" << obj << " roomId " << mRoomId << " userId " << mUserId << " publicKey " << publicKey;
0037         mType = Otr::Handshake;
0038         parseCryptoSettings(publicKey);
0039     } else if (type == QLatin1String("deny")) {
0040         qCDebug(RUQOLA_LOG) << " Deny " << contents;
0041         const QJsonObject obj = contents.at(1).toObject();
0042         extractRoomUser(obj);
0043         mType = Otr::Deny;
0044     } else if (type == QLatin1String("acknowledge")) {
0045         qCDebug(RUQOLA_LOG) << " acknowledge " << contents;
0046         const QJsonObject obj = contents.at(1).toObject();
0047         extractRoomUser(obj);
0048         const QString publicKey = obj.value(QLatin1String("publicKey")).toString();
0049         parseCryptoSettings(publicKey);
0050         mType = Otr::AcknowLedge;
0051     } else {
0052         qCDebug(RUQOLA_LOG) << " unknown" << type;
0053     }
0054 }
0055 
0056 void Otr::parseCryptoSettings(const QString &publicKey)
0057 {
0058     qDebug() << " parseCryptoSettings " << publicKey;
0059     mCrypto.mCrypt = publicKey;
0060 
0061     // TODO parse it.
0062 }
0063 
0064 CryptoSettings Otr::crypto() const
0065 {
0066     return mCrypto;
0067 }
0068 
0069 Otr::OtrType Otr::type() const
0070 {
0071     return mType;
0072 }
0073 
0074 QString Otr::roomId() const
0075 {
0076     return mRoomId;
0077 }
0078 
0079 QString Otr::userId() const
0080 {
0081     return mUserId;
0082 }
0083 
0084 bool Otr::isValid() const
0085 {
0086     return mType != Otr::Unknown;
0087 }
0088 
0089 QDebug operator<<(QDebug d, const Otr &t)
0090 {
0091     d << "isValid: " << t.isValid();
0092     d << "type : " << t.type();
0093     d << "userId: " << t.userId();
0094     d << "roomId: " << t.roomId();
0095     d << "crypto settings " << t.crypto();
0096     return d;
0097 }
0098 
0099 QDebug operator<<(QDebug d, const CryptoSettings &t)
0100 {
0101     d << "mCrypt: " << t.mCrypt;
0102     return d;
0103 }
0104 
0105 bool Otr::operator==(const Otr &other) const
0106 {
0107     return roomId() == other.roomId() && userId() == other.userId() && type() == other.type() && crypto() == other.crypto();
0108 }
0109 
0110 bool CryptoSettings::operator==(const CryptoSettings &other) const
0111 {
0112     return mCrypt == other.mCrypt;
0113 }
0114 
0115 #include "moc_otr.cpp"