Some work on omemo handler, NOT DONE, BUILD FAILS!

This commit is contained in:
Blue 2022-12-15 02:08:08 +03:00
parent 820dc845ea
commit 0b61b6e928
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
7 changed files with 187 additions and 12 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "external/qxmpp"] [submodule "external/qxmpp"]
path = external/qxmpp path = external/qxmpp
url = https://github.com/qxmpp-project/qxmpp.git url = https://github.com/qxmpp-project/qxmpp.git
[submodule "external/storage"]
path = external/storage
url = https://git.macaw.me/blue/storage

View File

@ -147,7 +147,13 @@ else ()
endif () endif ()
## LMDB ## LMDB
find_package(LMDB REQUIRED) #find_package(LMDB REQUIRED)
#TODO conditioning!
add_subdirectory(external/storage)
target_include_directories(squawk PRIVATE external/storage)
target_link_libraries(squawk PRIVATE storage)
# Linking # Linking
target_link_libraries(squawk target_link_libraries(squawk

View File

@ -14,4 +14,148 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QDebug>
#include "omemohandler.h" #include "omemohandler.h"
#include "core/account.h"
Core::OmemoHandler::OmemoHandler(Account* account) :
QXmppOmemoStorage(),
acc(account),
ownDevice(std::nullopt),
db("omemo"),
meta(db.addCache<QString, QVariant>("meta")),
devices(db.addCache<QString, QHash<uint32_t, Device>>("devices")),
preKeyPairs(db.addCache<uint32_t, QByteArray>("preKeyPairs")),
signedPreKeyPairs(db.addCache<uint32_t, QXmppOmemoStorage::SignedPreKeyPair>("signedPreKeyPairs"))
{
db.open();
try {
QVariant own = meta->getRecord("ownDevice");
ownDevice.value() = own.value<OwnDevice>();
qDebug() << "Successfully found own device omemo data for account" << acc->getName();
} catch (const DataBase::NotFound& e) {
qDebug() << "No device omemo data was found for account" << acc->getName();
}
}
Core::OmemoHandler::~OmemoHandler() {
db.close();
}
QFuture<void> Core::OmemoHandler::emptyVoidFuture() {
QFutureInterface<QXmppOmemoStorage::OmemoData> result(QFutureInterfaceBase::Started);
result.reportFinished();
return result.future();
}
QFuture<QXmppOmemoStorage::OmemoData> Core::OmemoHandler::allData() {
OmemoData data;
data.ownDevice = ownDevice;
std::map<uint32_t, QByteArray> pkeys = preKeyPairs->readAll();
for (const std::pair<const uint32_t, QByteArray>& pair : pkeys) {
data.preKeyPairs.insert(pair.first, pair.second);
}
std::map<uint32_t, QXmppOmemoStorage::SignedPreKeyPair> spre = signedPreKeyPairs->readAll();
for (const std::pair<const uint32_t, QXmppOmemoStorage::SignedPreKeyPair>& pair : spre) {
data.signedPreKeyPairs.insert(pair.first, pair.second);
}
std::map<QString, QHash<uint32_t, Device>> devs = devices->readAll();
for (const std::pair<const QString, QHash<uint32_t, Device>>& pair : devs) {
data.devices.insert(pair.first, pair.second);
}
QFutureInterface<QXmppOmemoStorage::OmemoData> result(QFutureInterfaceBase::Started);
result.reportResult(std::move(data));
result.reportFinished();
return result.future();
}
QFuture<void> Core::OmemoHandler::addDevice(const QString& jid, uint32_t deviceId, const QXmppOmemoStorage::Device& device) {
QHash<uint32_t, Device> devs;
bool had = true;
try {
devs = devices->getRecord(jid);
} catch (const DataBase::NotFound& error) {
had = false;
}
devs.insert(deviceId, device);
if (had) {
devices->changeRecord(jid, devs);
} else {
devices->addRecord(jid, devs);
}
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::addPreKeyPairs(const QHash<uint32_t, QByteArray>& keyPairs) {
for (QHash<uint32_t, QByteArray>::const_iterator itr = keyPairs.begin(), end = keyPairs.end(); itr != end; ++itr) {
preKeyPairs->forceRecord(itr.key(), itr.value());
}
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::addSignedPreKeyPair(uint32_t keyId, const QXmppOmemoStorage::SignedPreKeyPair& keyPair) {
signedPreKeyPairs->addRecord(keyId, keyPair);
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::removeDevice(const QString& jid, uint32_t deviceId) {
QHash<uint32_t, Device> devs = devices->getRecord(jid);
devs.remove(deviceId);
if (devs.isEmpty()) {
devices->removeRecord(jid);
} else {
devices->changeRecord(jid, devs);
}
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::removeDevices(const QString& jid) {
devices->removeRecord(jid);
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::removePreKeyPair(uint32_t keyId) {
preKeyPairs->removeRecord(keyId);
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::removeSignedPreKeyPair(uint32_t keyId) {
signedPreKeyPairs->removeRecord(keyId);
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::setOwnDevice(const std::optional<OwnDevice>& device) {
bool had = ownDevice.has_value();
ownDevice = device;
if (ownDevice.has_value()) {
if (had) {
meta->changeRecord("ownDevice", QVariant::fromValue(ownDevice.value()));
} else {
meta->addRecord("ownDevice", QVariant::fromValue(ownDevice.value()));
}
} else {
if (had) {
meta->removeRecord("ownDevice");
}
}
return emptyVoidFuture();
}
QFuture<void> Core::OmemoHandler::resetAll() {
ownDevice = std::nullopt;
db.drop();
return emptyVoidFuture();
}

View File

@ -18,14 +18,17 @@
#define CORE_OMEMOHANDLER_H #define CORE_OMEMOHANDLER_H
#include <QXmppOmemoStorage.h> #include <QXmppOmemoStorage.h>
#include <core/storage/cache.h> #include <cache.h>
Q_DECLARE_METATYPE(QXmppOmemoStorage::OwnDevice);
namespace Core { namespace Core {
class Account;
class OmemoHandler : public QXmppOmemoStorage class OmemoHandler : public QXmppOmemoStorage
{ {
public: public:
OmemoHandler(); OmemoHandler(Account* account);
~OmemoHandler() override; ~OmemoHandler() override;
QFuture<OmemoData> allData() override; QFuture<OmemoData> allData() override;
@ -45,13 +48,24 @@ public:
QFuture<void> resetAll() override; QFuture<void> resetAll() override;
private: private:
static QFuture<void> emptyVoidFuture();
private:
Account* acc;
std::optional<OwnDevice> ownDevice; std::optional<OwnDevice> ownDevice;
Cache<QString, QHash<uint32_t, Device>> devices; DataBase db;
Cache<uint32_t, QByteArray> preKeyPairs; DataBase::Cache<QString, QVariant>* meta;
Cache<uint32_t, QXmppOmemoStorage::SignedPreKeyPair> signedPreKeyPairs; DataBase::Cache<QString, QHash<uint32_t, Device>>* devices;
DataBase::Cache<uint32_t, QByteArray>* preKeyPairs;
DataBase::Cache<uint32_t, QXmppOmemoStorage::SignedPreKeyPair>* signedPreKeyPairs;
}; };
} }
QDataStream& operator << (QDataStream &out, const QXmppOmemoStorage::Device& device);
QDataStream& operator >> (QDataStream &out, QXmppOmemoStorage::Device device);
QDataStream& operator << (QDataStream &out, const QXmppOmemoStorage::SignedPreKeyPair& device);
QDataStream& operator >> (QDataStream &out, QXmppOmemoStorage::SignedPreKeyPair device);
#endif // CORE_OMEMOHANDLER_H #endif // CORE_OMEMOHANDLER_H

2
external/qxmpp vendored

@ -1 +1 @@
Subproject commit fe83e9c3d42c3becf682e2b5ecfc9d77b24c614f Subproject commit f6e7591e21b4c55319918ac296b2341b2e9f1988

1
external/storage vendored Submodule

@ -0,0 +1 @@
Subproject commit 08daad48ad36d9f12dc6485a085190e31e4cf49e

View File

@ -33,6 +33,10 @@
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QDir> #include <QDir>
#ifdef WITH_OMEMO
#include <QXmppOmemoStorage.h>
#endif
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
qRegisterMetaType<Shared::Message>("Shared::Message"); qRegisterMetaType<Shared::Message>("Shared::Message");
@ -40,12 +44,15 @@ int main(int argc, char *argv[])
qRegisterMetaType<Shared::VCard>("Shared::VCard"); qRegisterMetaType<Shared::VCard>("Shared::VCard");
qRegisterMetaType<std::list<Shared::Message>>("std::list<Shared::Message>"); qRegisterMetaType<std::list<Shared::Message>>("std::list<Shared::Message>");
qRegisterMetaType<std::list<Shared::MessageInfo>>("std::list<Shared::MessageInfo>"); qRegisterMetaType<std::list<Shared::MessageInfo>>("std::list<Shared::MessageInfo>");
qRegisterMetaType<std::list<Shared::MessageInfo>>("std::list<QString>"); qRegisterMetaType<std::list<QString>>("std::list<QString>");
qRegisterMetaType<std::list<Shared::MessageInfo>>("std::set<QString>"); qRegisterMetaType<std::set<QString>>("std::set<QString>");
qRegisterMetaType<std::list<Shared::MessageInfo>>("std::list<Shared::Identity>"); qRegisterMetaType<std::list<Shared::Identity>>("std::list<Shared::Identity>");
qRegisterMetaType<QSet<QString>>("QSet<QString>"); qRegisterMetaType<QSet<QString>>("QSet<QString>");
qRegisterMetaType<Shared::ConnectionState>("Shared::ConnectionState"); qRegisterMetaType<Shared::ConnectionState>("Shared::ConnectionState");
qRegisterMetaType<Shared::Availability>("Shared::Availability"); qRegisterMetaType<Shared::Availability>("Shared::Availability");
#ifdef WITH_OMEMO
qRegisterMetaType<QXmppOmemoStorage::OwnDevice>("QXmppOmemoStorage::OwnDevice");
#endif
QApplication app(argc, argv); QApplication app(argc, argv);
SignalCatcher sc(&app); SignalCatcher sc(&app);