squawk/qomemo/bundle.cpp

129 lines
3.2 KiB
C++
Raw Normal View History

2021-05-12 21:32:13 +00:00
/*
* Created by victoria on 2021-05-12.
*/
#include "bundle.h"
#include <QXmppPubSubIq.h>
#include <QDebug>
#include "shared/qxmppfactories.h"
using namespace QXmpp::Factories;
QXmppElement QXmpp::Omemo::PreKey::toXml() const {
2021-05-13 14:54:37 +00:00
auto pk = createElement("preKeyPublic");
pk.setAttribute("preKeyId", QString::number(id));
// TODO: Base64
pk.setValue(data);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return pk;
2021-05-12 21:32:13 +00:00
}
void QXmpp::Omemo::PreKey::fromXml(const QXmppElement &element) {
2021-05-13 14:54:37 +00:00
if (!elementMatches(element, "preKeyPublic"))
return;
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
id = element.attribute("preKeyId").toInt();
// TODO: Base64
data = element.value();
2021-05-12 21:32:13 +00:00
}
QXmppElement QXmpp::Omemo::Bundle::toXml() const {
2021-05-13 14:54:37 +00:00
auto spkNode = createElement("signedPreKeyPublic");
spkNode.setAttribute("signedPreKeyId", QString::number(spkId));
spkNode.setValue(spk);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto spksNode = createElement("signedPreKeySignature");
spksNode.setValue(spks);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto ikNode = createElement("identityKey");
ikNode.setValue(ik);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto prekeysNode = createElement("prekeys");
for (const auto &pk : prekeys) {
prekeysNode.appendChild(pk.toXml());
}
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto result = createElement("bundle", "eu.siacs.conversations.axolotl");
result.appendChild(spkNode);
result.appendChild(spksNode);
result.appendChild(ikNode);
result.appendChild(prekeysNode);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return result;
2021-05-12 21:32:13 +00:00
}
QXmppIq QXmpp::Omemo::Bundle::toIq(int deviceId) const {
2021-05-13 14:54:37 +00:00
QXmppIq iq{};
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
iq.setType(QXmppIq::Set);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto item = createElement("item");
item.appendChild(toXml());
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto publish = createElement("publish");
publish.setAttribute(
"node",
QString("eu.siacs.conversations.axolotl.bundles:%s").arg(deviceId));
publish.appendChild(item);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto pubSub = createElement("pubsub", "http://jabber.org/protocol/pubsub");
pubSub.appendChild(publish);
pubSub.appendChild(createOpenPublishOptions());
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
iq.extensions().push_back(pubSub);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return iq;
2021-05-12 21:32:13 +00:00
}
void QXmpp::Omemo::Bundle::fromXml(const QXmppElement &element) {
2021-05-13 14:54:37 +00:00
if (!elementMatches(element, "bundle", "eu.siacs.conversations.axolotl"))
return;
auto spkNode = element.firstChildElement("spk");
if (spkNode.isNull()) {
qWarning() << "'bundle': missing 'spk'";
return;
}
spk = spkNode.value();
spkId = spkNode.attribute("id").toInt();
auto spksNode = element.firstChildElement("spks");
if (spksNode.isNull()) {
qWarning() << "'bundle': missing 'spks'";
return;
}
spks = spksNode.value();
auto ikNode = element.firstChildElement("ik");
if (ikNode.isNull()) {
qWarning() << "'bundle': missing 'ik'";
return;
}
ik = ikNode.value();
auto prekeysNode = element.firstChildElement("prekeys");
auto pkNode = prekeysNode.firstChildElement("pk");
prekeys.clear();
while (!pkNode.isNull()) {
PreKey pk{};
pk.fromXml(pkNode);
prekeys.push_back(pk);
}
2021-05-12 21:32:13 +00:00
}
QXmppPubSubIq QXmpp::Omemo::Bundle::fetchDeviceBundleIq(int deviceId) {
2021-05-13 14:54:37 +00:00
QXmppPubSubIq iq{};
iq.setType(QXmppIq::Get);
iq.setQueryNode(
QString("eu.siacs.conversations.axolotl.bundles:%1").arg(deviceId));
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
QXmppPubSubItem item{};
item.setId(QString::number(deviceId));
iq.setItems({item});
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return iq;
2021-05-12 21:32:13 +00:00
}