Skip to content

Commit

Permalink
More work related to vcard4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jun 10, 2024
1 parent e61279e commit bfc0d9c
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 46 deletions.
11 changes: 11 additions & 0 deletions src/xmpp/xmpp-im/xmpp_discoitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ QDomElement DiscoItem::toDiscoInfoResult(QDomDocument *doc) const
return q;
}

bool DiscoItem::hasPersistentStorage() const
{
bool hasPEP = false;
for (const DiscoItem::Identity &i : d->identities) {
if (i.category == "pubsub" && i.type == "pep") {
hasPEP = true;
}
}
return hasPEP && d->features.test("http://jabber.org/protocol/pubsub#publish-options");
}

const Jid &DiscoItem::jid() const { return d->jid; }

void DiscoItem::setJid(const Jid &j) { d->jid = j; }
Expand Down
2 changes: 2 additions & 0 deletions src/xmpp/xmpp-im/xmpp_discoitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class DiscoItem {
static DiscoItem fromDiscoInfoResult(const QDomElement &x);
QDomElement toDiscoInfoResult(QDomDocument *doc) const;

bool hasPersistentStorage() const;

private:
QSharedDataPointer<DiscoItemPrivate> d;
};
Expand Down
9 changes: 9 additions & 0 deletions src/xmpp/xmpp-im/xmpp_features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ bool Features::hasVCard() const
return test(ns);
}

#define FID_VCARD4 "urn:ietf:params:xml:ns:vcard-4.0"
bool Features::hasVCard4() const
{
QSet<QString> ns;
ns << FID_VCARD4;

return test(ns);
}

#define FID_MESSAGECARBONS "urn:xmpp:carbons:2"
bool Features::hasMessageCarbons() const
{
Expand Down
40 changes: 21 additions & 19 deletions src/xmpp/xmpp-im/xmpp_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,27 @@ class Features {

// features
inline bool isEmpty() const { return _list.isEmpty(); }
bool hasRegister() const;
bool hasSearch() const;
bool hasMulticast() const;
bool hasGroupchat() const;
bool hasVoice() const;
bool hasDisco() const;
bool hasChatState() const;
bool hasCommand() const;
bool hasGateway() const;
bool hasVersion() const;
bool hasVCard() const;
bool hasMessageCarbons() const;
bool hasJingleFT() const;
bool hasJingleIceUdp() const;
bool hasJingleIce() const;
bool hasCaps() const;
bool hasCapsOptimize() const;
bool hasDirectMucInvite() const;
bool hasAvatarConversion() const;

bool hasRegister() const;
bool hasSearch() const;
bool hasMulticast() const;
bool hasGroupchat() const;
bool hasVoice() const;
bool hasDisco() const;
bool hasChatState() const;
bool hasCommand() const;
bool hasGateway() const;
bool hasVersion() const;
bool hasVCard() const;
bool hasVCard4() const;
bool hasMessageCarbons() const;
bool hasJingleFT() const;
bool hasJingleIceUdp() const;
bool hasJingleIce() const;
bool hasCaps() const;
bool hasCapsOptimize() const;
bool hasDirectMucInvite() const;
bool hasAvatarConversion() const;

[[deprecated]] inline bool canRegister() const { return hasRegister(); }
[[deprecated]] inline bool canSearch() const { return hasSearch(); }
Expand Down
43 changes: 22 additions & 21 deletions src/xmpp/xmpp-im/xmpp_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ struct HashDesc {
Hash::Type hashType;
const char *const *synonims = nullptr;
};

// hash types in priority order mostly by speed
static const std::array hashTypes {
HashDesc { "unknown", Hash::Type::Unknown }, HashDesc { "sha-1", Hash::Type::Sha1, sha1_synonims },
HashDesc { "sha-256", Hash::Type::Sha256 }, HashDesc { "sha-512", Hash::Type::Sha512 },
HashDesc { "sha3-256", Hash::Type::Sha3_256 }, HashDesc { "sha3-512", Hash::Type::Sha3_512 },
HashDesc { "blake2b-256", Hash::Type::Blake2b256 }, HashDesc { "blake2b-512", Hash::Type::Blake2b512 }
};
HashDesc { "blake2b-512", Hash::Type::Blake2b512 },
HashDesc { "blake2b-256", Hash::Type::Blake2b256 },
HashDesc { "sha-1", Hash::Type::Sha1, sha1_synonims },
HashDesc { "sha-512", Hash::Type::Sha512 },
HashDesc { "sha-256", Hash::Type::Sha256 },
HashDesc { "sha3-512", Hash::Type::Sha3_512 },
HashDesc { "sha3-256", Hash::Type::Sha3_256 },
}; // HashDesc { "unknown", Hash::Type::Unknown },

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
using HashVariant = std::variant<std::nullptr_t, QCryptographicHash, QCA::Hash, Blake2Hash>;
Expand Down Expand Up @@ -143,9 +148,13 @@ Hash::Hash(const QDomElement &el)
QString Hash::stringType() const
{
if (!v_type || int(v_type) > LastType)
return QString(); // must be empty. other code relies on it
static_assert(LastType + 1 == hashTypes.size(), "hashType and enum are not in sync");
return QLatin1String(hashTypes[int(v_type)].text);
return {}; // must be empty. other code relies on it
static_assert(LastType == hashTypes.size(), "hashType and enum are not in sync");
auto it = std::ranges::find_if(hashTypes, [this](auto const &v) { return v.hashType == v_type; });
if (it == hashTypes.end()) {
throw std::logic_error("hashTypes array is inconsistent");
}
return QLatin1String(it->text);
}

Hash::Type Hash::parseType(const QStringView &algo)
Expand Down Expand Up @@ -310,21 +319,13 @@ Hash Hash::from(const QStringView &str)

Hash Hash::fastestHash(const Features &features)
{
std::array qcaAlgos = { "blake2b_512", "blake2b_256", "sha1", "sha512", "sha256", "sha3_256", "sha3_512" };
std::array qcaMap = { Blake2b512, Blake2b256, Sha1, Sha512, Sha256, Sha3_256, Sha3_512 };
QStringList priorityFeatures;
priorityFeatures.reserve(int(qcaAlgos.size()));
for (auto t : qcaMap) {
priorityFeatures.append(QString(QLatin1String("urn:xmpp:hash-function-text-names:"))
+ QLatin1String(hashTypes[int(t)].text));
// REVIEW modify hashTypes with priority info instead?
}
for (std::size_t i = 0; i < qcaAlgos.size(); i++) {
if (QCA::isSupported(qcaAlgos[i]) && features.test(priorityFeatures[i])) {
return Hash(qcaMap[i]);
for (auto const &h : hashTypes) {
auto feature = QString(QLatin1String("urn:xmpp:hash-function-text-names:")) + QLatin1String(h.text);
if (features.test(feature)) {
return Hash(h.hashType);
}
}
return Hash(); // qca is the fastest and it defintiely has sha1. so no reason to use qt or custom blake
return {};
}

class StreamHashPrivate {
Expand Down
2 changes: 1 addition & 1 deletion src/xmpp/xmpp-im/xmpp_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Features;

class Hash {
public:
// NB: keep this in sync with Hash::fastestHash() and with hashTypes in cpp the file!
// NB: we have only supported algorithms here. if more is needed then do extra checks
enum Type { // XEP-0300 Version 0.5.3 (2018-02-14)
Unknown, // not standard, just a default
Sha1, // SHOULD NOT
Expand Down
2 changes: 1 addition & 1 deletion src/xmpp/xmpp-im/xmpp_serverinfomanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void ServerInfoManager::queryServicesList()
auto jtitems = new JT_DiscoItems(_client->rootTask());
connect(
jtitems, &JT_DiscoItems::finished, this,
[=]() {
[this, jtitems]() {
_servicesInfo.clear(); //
if (jtitems->success()) {
_servicesListState = ST_Ready;
Expand Down
Loading

0 comments on commit bfc0d9c

Please sign in to comment.