squawk/qomemo/qomemo.cpp

95 lines
2.2 KiB
C++
Raw Permalink Normal View History

/*
* Created by victoria on 2021-05-12.
*/
#include "qomemo.h"
#include "sce.h"
#include <shared/qxmppfactories.h>
#include <QBuffer>
#include <QDebug>
#include <QDomDocument>
using namespace QXmpp::Factories;
QXmppElement QXmpp::Omemo::EncryptedMessage::header() const {
2021-05-13 14:54:37 +00:00
auto result = createElement("header");
result.setAttribute("sid", QString::number(fromDeviceId));
2021-05-13 14:54:37 +00:00
auto ivNode = createElement("iv");
ivNode.setValue(iv);
2021-05-13 14:54:37 +00:00
for (const auto &key : keys) {
result.appendChild(key.toXml());
}
2021-05-13 14:54:37 +00:00
return result;
}
QXmppElement QXmpp::Omemo::EncryptedMessage::toXml() const {
2021-05-13 14:54:37 +00:00
auto result = createElement("encrypted", "eu.siacs.conversations.axolotl");
2021-05-13 14:54:37 +00:00
result.appendChild(header());
// TODO: Payload is optional
result.appendChild(payload());
2021-05-13 14:54:37 +00:00
return result;
}
QXmppElement QXmpp::Omemo::EncryptedMessage::payload() const {
2021-05-13 14:54:37 +00:00
QBuffer buffer;
buffer.open(QIODevice::ReadWrite);
QXmlStreamWriter writer(&buffer);
message.toXml(&writer);
2021-05-13 14:54:37 +00:00
QDomDocument doc;
doc.setContent(buffer.data(), true);
2021-05-13 14:54:37 +00:00
QXmppElement root(doc.documentElement());
root.setTagName("payload");
2021-05-13 14:54:37 +00:00
return root;
}
QXmppElement QXmpp::Omemo::EncryptedMessage::content() const {
2021-05-13 14:54:37 +00:00
auto envelope = createElement("content", "urn:xmpp:sce:0");
2021-05-13 14:54:37 +00:00
envelope.appendChild(payload());
2021-05-13 14:54:37 +00:00
if (!from.isEmpty()) {
auto fromNode = createElement("from");
fromNode.setAttribute("jid", from);
envelope.appendChild(fromNode);
}
2021-05-13 14:54:37 +00:00
if (!to.isEmpty()) {
auto toNode = createElement("to");
toNode.setAttribute("jid", to);
envelope.appendChild(toNode);
}
2021-05-13 14:54:37 +00:00
if (!timestamp.isNull()) {
auto timeNode = createElement("time");
timeNode.setAttribute("stamp", timestamp.toString(Qt::DateFormat::ISODate));
envelope.appendChild(timeNode);
}
2021-05-13 14:54:37 +00:00
auto rpad = createElement("rpad");
rpad.setValue(QXmpp::Sce::generatePadding());
envelope.appendChild(rpad);
2021-05-13 14:54:37 +00:00
return envelope;
}
QXmppElement QXmpp::Omemo::MessageKey::toXml() const {
2021-05-13 14:54:37 +00:00
auto result = createElement("key");
2021-05-13 14:54:37 +00:00
result.setAttribute("rid", QString::number(receivingDeviceId));
if (prekey)
result.setAttribute("prekey", "true");
2021-05-13 14:54:37 +00:00
result.setValue(key);
2021-05-13 14:54:37 +00:00
return result;
}