first ever received and cached client data!

This commit is contained in:
Blue 2022-08-25 01:41:06 +03:00
parent 037dabbe06
commit c50cd1140e
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
11 changed files with 297 additions and 60 deletions

View File

@ -634,16 +634,16 @@ void Core::Account::onDiscoveryInfoReceived(const QXmppDiscoveryIq& info)
QString node = info.queryNode(); QString node = info.queryNode();
if (!node.isEmpty()) { if (!node.isEmpty()) {
QStringList feats = info.features(); QStringList feats = info.features();
std::list<Shared::Identity> identities; std::set<Shared::Identity> identities;
std::set<QString> features(feats.begin(), feats.end()); std::set<QString> features(feats.begin(), feats.end());
QList<QXmppDiscoveryIq::Identity> idents = info.identities(); QList<QXmppDiscoveryIq::Identity> idents = info.identities();
for (const QXmppDiscoveryIq::Identity& ident : idents) { for (const QXmppDiscoveryIq::Identity& ident : idents) {
identities.emplace_back(); Shared::Identity identity;
Shared::Identity& identity = identities.back();
identity.category = ident.category(); identity.category = ident.category();
identity.language = ident.language(); identity.language = ident.language();
identity.name = ident.name(); identity.name = ident.name();
identity.type = ident.type(); identity.type = ident.type();
identities.insert(identity);
qDebug() << " " << identity.name << identity.category << identity.type; qDebug() << " " << identity.name << identity.category << identity.type;
} }
@ -655,6 +655,16 @@ void Core::Account::onDiscoveryInfoReceived(const QXmppDiscoveryIq& info)
} }
} }
void Core::Account::discoverInfo(const QString& address, const QString& node) {
if (state == Shared::ConnectionState::connected) {
dm->requestInfo(address, node);
} else {
qDebug() << "An attempt to send a discover info by account" << name <<
"sending request to" << address << "about node" << node <<
"but the account is not in the connected state, skipping";
}
}
void Core::Account::handleDisconnection() void Core::Account::handleDisconnection()
{ {
cm->setCarbonsEnabled(false); cm->setCarbonsEnabled(false);

View File

@ -155,7 +155,7 @@ signals:
void uploadFile(const QFileInfo& file, const QUrl& set, const QUrl& get, QMap<QString, QString> headers); void uploadFile(const QFileInfo& file, const QUrl& set, const QUrl& get, QMap<QString, QString> headers);
void uploadFileError(const QString& jid, const QString& messageId, const QString& error); void uploadFileError(const QString& jid, const QString& messageId, const QString& error);
void needPassword(); void needPassword();
void infoDiscovered(const QString& address, const QString& node, const std::list<Shared::Identity>& identities, const std::set<QString>& features); void infoDiscovered(const QString& address, const QString& node, const std::set<Shared::Identity>& identities, const std::set<QString>& features);
private: private:
QString name; QString name;

View File

@ -14,12 +14,14 @@
// 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 "clientcache.h" #include "clientcache.h"
#include <QDebug>
Core::ClientCache::ClientCache(): Core::ClientCache::ClientCache():
requested(), requested(),
cache("clients") cache("clients"),
specific()
{ {
cache.open(); cache.open();
} }
@ -39,9 +41,9 @@ bool Core::ClientCache::checkClient(const QString& node, const QString& ver, con
QString id = node + "/" + ver; QString id = node + "/" + ver;
if (requested.count(id) == 0 && !cache.checkRecord(id)) { if (requested.count(id) == 0 && !cache.checkRecord(id)) {
Shared::ClientInfo& info = requested.insert(std::make_pair(id, Shared::ClientInfo())).first->second; Shared::ClientInfo& info = requested.insert(std::make_pair(id, Shared::ClientInfo())).first->second;
info.capabilitiesNode = node; info.node = node;
info.capabilitiesVerification = ver; info.verification = ver;
info.capabilitiesHash = hash; info.hash = hash;
emit requestClientInfo(id); emit requestClientInfo(id);
return false; return false;
} }
@ -49,7 +51,28 @@ bool Core::ClientCache::checkClient(const QString& node, const QString& ver, con
return true; return true;
} }
bool Core::ClientCache::registerClientInfo (
const QString& sourceFullJid,
const QString& id,
const std::set<Shared::Identity>& identities,
const std::set<QString>& features)
{
std::map<QString, Shared::ClientInfo>::iterator itr = requested.find(id);
if (itr != requested.end()) {
Shared::ClientInfo& info = itr->second;
info.identities = identities;
info.extensions = features;
bool Core::ClientCache::registerClientInfo(const QString& sourceFullJid, const QString& id, const Shared::Identity& identity, const std::set<QString>& features) { bool valid = info.valid();
if (valid) {
cache.addRecord(id, info);
} else {
info.specificPresence = sourceFullJid;
specific.insert(std::make_pair(sourceFullJid, info));
}
requested.erase(id);
return valid;
} else {
return false;
}
} }

View File

@ -43,11 +43,12 @@ signals:
public slots: public slots:
bool checkClient(const QString& node, const QString& ver, const QString& hash); bool checkClient(const QString& node, const QString& ver, const QString& hash);
bool registerClientInfo(const QString& sourceFullJid, const QString& id, const Shared::Identity& identity, const std::set<QString>& features); bool registerClientInfo(const QString& sourceFullJid, const QString& id, const std::set<Shared::Identity>& identities, const std::set<QString>& features);
private: private:
std::map<QString, Shared::ClientInfo> requested; std::map<QString, Shared::ClientInfo> requested;
Cache<Shared::ClientInfo> cache; Cache<Shared::ClientInfo> cache;
std::map<QString, Shared::ClientInfo> specific;
}; };
} }

View File

@ -320,6 +320,8 @@ void Core::Squawk::onAccountAddPresence(const QString& jid, const QString& name,
Account* acc = static_cast<Account*>(sender()); Account* acc = static_cast<Account*>(sender());
emit addPresence(acc->getName(), jid, name, data); emit addPresence(acc->getName(), jid, name, data);
//it's equal if a MUC sends its status with presence of the same jid (ex: muc@srv.im/muc@srv.im), it's not a client, so, no need to request
if (jid != name) {
QString node = data["capabilityNode"].toString(); QString node = data["capabilityNode"].toString();
QString ver = data["capabilityVer"].toString(); QString ver = data["capabilityVer"].toString();
QString hash = data["capabilityHash"].toString(); QString hash = data["capabilityHash"].toString();
@ -327,18 +329,18 @@ void Core::Squawk::onAccountAddPresence(const QString& jid, const QString& name,
acc->discoverInfo(jid + "/" + name, node + "/" + ver); acc->discoverInfo(jid + "/" + name, node + "/" + ver);
} }
} }
}
void Core::Squawk::onAccountInfoDiscovered( void Core::Squawk::onAccountInfoDiscovered(
const QString& address, const QString& address,
const QString& node, const QString& node,
const std::list<Shared::Identity>& identities, const std::set<Shared::Identity>& identities,
const std::set<QString>& features) const std::set<QString>& features)
{ {
Account* acc = static_cast<Account*>(sender()); Account* acc = static_cast<Account*>(sender());
if (identities.size() != 1 || clientCache.registerClientInfo(address, node, identities.back(), features)) { if (!clientCache.registerClientInfo(address, node, identities, features)) {
qDebug() << "Account" << acc->getName() << "received an ill-formed client discovery response from" << address << "about" << node; qDebug() << "Account" << acc->getName() << "received an ill-formed client discovery response from" << address << "about" << node;
return;
} }
} }

View File

@ -183,7 +183,7 @@ private slots:
void onWalletOpened(bool success); void onWalletOpened(bool success);
void onWalletRejectPassword(const QString& login); void onWalletRejectPassword(const QString& login);
void onAccountInfoDiscovered(const QString& address, const QString& node, const std::list<Shared::Identity>& identities, const std::set<QString>& features); void onAccountInfoDiscovered(const QString& address, const QString& node, const std::set<Shared::Identity>& identities, const std::set<QString>& features);
private: private:
void readSettings(); void readSettings();

View File

@ -21,4 +21,5 @@ target_sources(squawk PRIVATE
clientinfo.h clientinfo.h
clientinfo.cpp clientinfo.cpp
identity.h identity.h
identity.cpp
) )

View File

@ -16,30 +16,42 @@
#include "clientinfo.h" #include "clientinfo.h"
const std::map<QString, QCryptographicHash::Algorithm> Shared::ClientInfo::hashes = {
//md2 is missing
{"md5", QCryptographicHash::Md5},
{"sha-1", QCryptographicHash::Sha1},
{"sha-224", QCryptographicHash::Sha224},
{"sha-256", QCryptographicHash::Sha256},
{"sha-384", QCryptographicHash::Sha384},
{"sha-512", QCryptographicHash::Sha512},
//shake128 is missing
//shake256 is missing
};
Shared::ClientInfo::ClientInfo(): Shared::ClientInfo::ClientInfo():
name(), identities(),
category(), extensions(),
type(), node(),
capabilitiesNode(), verification(),
capabilitiesVerification(), hash(),
capabilitiesHash(), specificPresence() {}
specificPresence(),
capabilitiesExtensions() {}
QString Shared::ClientInfo::getId() const { QString Shared::ClientInfo::getId() const {
return capabilitiesNode + "/" + capabilitiesVerification; return node + "/" + verification;
} }
QDataStream & Shared::ClientInfo::operator >> (QDataStream& stream) const { QDataStream & Shared::ClientInfo::operator >> (QDataStream& stream) const {
stream << name; stream << node;
stream << category; stream << verification;
stream << type; stream << hash;
stream << capabilitiesNode; stream << (quint8)identities.size();
stream << capabilitiesVerification; for (const Shared::Identity& identity : identities) {
stream << capabilitiesHash; stream << identity;
stream << (quint8)capabilitiesExtensions.size(); }
for (const QString& ext : capabilitiesExtensions) { stream << (quint8)extensions.size();
for (const QString& ext : extensions) {
stream << ext; stream << ext;
} }
@ -47,29 +59,53 @@ QDataStream & Shared::ClientInfo::operator >> (QDataStream& stream) const {
} }
QDataStream & Shared::ClientInfo::operator << (QDataStream& stream) { QDataStream & Shared::ClientInfo::operator << (QDataStream& stream) {
stream >> name; stream >> node;
stream >> category; stream >> verification;
stream >> type; stream >> hash;
stream >> capabilitiesNode;
stream >> capabilitiesVerification;
stream >> capabilitiesHash;
quint8 size; quint8 size;
stream >> size;
for (quint8 i = 0; i < size; ++i) {
Shared::Identity identity;
stream >> identity;
identities.insert(identity);
}
stream >> size; stream >> size;
for (quint8 i = 0; i < size; ++i) { for (quint8 i = 0; i < size; ++i) {
QString ext; QString ext;
stream >> ext; stream >> ext;
capabilitiesExtensions.insert(ext); extensions.insert(ext);
} }
return stream; return stream;
} }
QDataStream& operator<< (QDataStream& stream, const Shared::ClientInfo& image) { bool Shared::ClientInfo::valid() const {
image >> stream; std::map<QString, QCryptographicHash::Algorithm>::const_iterator itr = hashes.find(hash);
if (itr == hashes.end()) {
return false;
}
QCryptographicHash calc(itr->second);
QString validationString = "";
for (const Identity& identity : identities) {
calc.addData((identity.category + "/" + identity.type + "/" + identity.language + "/" + identity.name + "<").toUtf8());
}
for (const QString& ext : extensions) {
calc.addData((ext + "<").toUtf8());
}
QString result = calc.result().toBase64();
return result == verification;
}
QDataStream& operator << (QDataStream& stream, const Shared::ClientInfo& info) {
info >> stream;
return stream; return stream;
} }
QDataStream& operator>> (QDataStream& stream, Shared::ClientInfo& image) { QDataStream& operator >> (QDataStream& stream, Shared::ClientInfo& info) {
image << stream; info << stream;
return stream; return stream;
} }

View File

@ -21,6 +21,9 @@
#include <QDataStream> #include <QDataStream>
#include <QString> #include <QString>
#include <QCryptographicHash>
#include <shared/identity.h>
namespace Shared { namespace Shared {
@ -30,24 +33,26 @@ public:
ClientInfo(); ClientInfo();
QString getId() const; QString getId() const;
bool valid() const;
QDataStream& operator << (QDataStream& stream); QDataStream& operator << (QDataStream& stream);
QDataStream& operator >> (QDataStream& stream) const; QDataStream& operator >> (QDataStream& stream) const;
public: public:
QString name; std::set<Identity> identities;
QString category; std::set<QString> extensions;
QString type; QString node;
QString capabilitiesNode; QString verification;
QString capabilitiesVerification; QString hash;
QString capabilitiesHash;
QString specificPresence; QString specificPresence;
std::set<QString> capabilitiesExtensions;
private:
static const std::map<QString, QCryptographicHash::Algorithm> hashes;
}; };
} }
QDataStream& operator<< (QDataStream& stream, const Shared::ClientInfo& image); QDataStream& operator << (QDataStream& stream, const Shared::ClientInfo& info);
QDataStream& operator>> (QDataStream& stream, Shared::ClientInfo& image); QDataStream& operator >> (QDataStream& stream, Shared::ClientInfo& info);
#endif // SHARED_CLIENTINFO_H #endif // SHARED_CLIENTINFO_H

142
shared/identity.cpp Normal file
View File

@ -0,0 +1,142 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "identity.h"
Shared::Identity::Identity():
category(),
type(),
language(),
name() {}
bool Shared::Identity::operator==(const Shared::Identity& other) const {
return category == other.category && type == other.type && language == other.language && name == other.name;
}
bool Shared::Identity::operator!=(const Shared::Identity& other) const {
return category != other.category || type != other.type || language != other.language || name != other.name;
}
bool Shared::Identity::operator > (const Shared::Identity& other) const {
if (category > other.category) {
return true;
} else if (category < other.category) {
return false;
} else if (type > other.type) {
return true;
} else if (type < other.type) {
return false;
} else if (language > other.language) {
return true;
} else if (language < other.language) {
return false;
} else if (name > other.name) {
return true;
} else {
return false;
}
}
bool Shared::Identity::operator < (const Shared::Identity& other) const {
if (category < other.category) {
return true;
} else if (category > other.category) {
return false;
} else if (type < other.type) {
return true;
} else if (type > other.type) {
return false;
} else if (language < other.language) {
return true;
} else if (language > other.language) {
return false;
} else if (name < other.name) {
return true;
} else {
return false;
}
}
bool Shared::Identity::operator >= (const Shared::Identity& other) const {
if (category > other.category) {
return true;
} else if (category < other.category) {
return false;
} else if (type > other.type) {
return true;
} else if (type < other.type) {
return false;
} else if (language > other.language) {
return true;
} else if (language < other.language) {
return false;
} else if (name > other.name) {
return true;
} else if (name < other.name) {
return false;
} else {
return true;
}
}
bool Shared::Identity::operator <= (const Shared::Identity& other) const {
if (category < other.category) {
return true;
} else if (category > other.category) {
return false;
} else if (type < other.type) {
return true;
} else if (type > other.type) {
return false;
} else if (language < other.language) {
return true;
} else if (language > other.language) {
return false;
} else if (name < other.name) {
return true;
} else if (name > other.name) {
return false;
} else {
return true;
}
}
QDataStream & Shared::Identity::operator >> (QDataStream& stream) const {
stream << category;
stream << type;
stream << language;
stream << name;
}
QDataStream & Shared::Identity::operator << (QDataStream& stream) {
stream >> category;
stream >> type;
stream >> language;
stream >> name;
}
QDataStream & operator >> (QDataStream& stream, Shared::Identity& identity) {
identity << stream;
return stream;
}
QDataStream & operator << (QDataStream& stream, const Shared::Identity& identity) {
identity >> stream;
return stream;
}

View File

@ -19,17 +19,34 @@
#ifndef SHARED_IDENTITY_H #ifndef SHARED_IDENTITY_H
#define SHARED_IDENTITY_H #define SHARED_IDENTITY_H
#include <QDataStream>
#include <QString> #include <QString>
namespace Shared { namespace Shared {
struct Identity { class Identity {
public:
Identity();
QDataStream& operator << (QDataStream& stream);
QDataStream& operator >> (QDataStream& stream) const;
bool operator < (const Identity& other) const;
bool operator > (const Identity& other) const;
bool operator >= (const Identity& other) const;
bool operator <= (const Identity& other) const;
bool operator == (const Identity& other) const;
bool operator != (const Identity& other) const;
public:
QString category; QString category;
QString type;
QString language; QString language;
QString name; QString name;
QString type;
}; };
} }
QDataStream& operator << (QDataStream& stream, const Shared::Identity& identity);
QDataStream& operator >> (QDataStream& stream, Shared::Identity& identity);
#endif //SHARED_IDENTITY_H #endif //SHARED_IDENTITY_H