From 7077786fa916076f2427cef89588e6b835976f0e Mon Sep 17 00:00:00 2001 From: Jeremy Lainé Date: Thu, 15 Jul 2010 15:37:56 +0000 Subject: add support for generating XEP-0115 verification strings --- source/QXmppDiscoveryIq.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'source/QXmppDiscoveryIq.cpp') diff --git a/source/QXmppDiscoveryIq.cpp b/source/QXmppDiscoveryIq.cpp index a308161a..866c82f7 100644 --- a/source/QXmppDiscoveryIq.cpp +++ b/source/QXmppDiscoveryIq.cpp @@ -21,11 +21,37 @@ * */ +#include +#include + #include "QXmppConstants.h" #include "QXmppDiscoveryIq.h" #include "QXmppUtils.h" -#include +static bool identityLessThan(const QXmppDiscoveryIq::Identity &i1, const QXmppDiscoveryIq::Identity &i2) +{ + if (i1.category() < i2.category()) + return true; + else if (i1.category() > i2.category()) + return false; + + if (i1.type() < i2.type()) + return true; + else if (i1.type() > i2.type()) + return false; + + if (i1.language() < i2.language()) + return true; + else if (i1.language() > i2.language()) + return false; + + if (i1.name() < i2.name()) + return true; + else if (i1.name() > i2.name()) + return false; + + return false; +} QString QXmppDiscoveryIq::Identity::category() const { @@ -37,6 +63,16 @@ void QXmppDiscoveryIq::Identity::setCategory(const QString &category) m_category = category; } +QString QXmppDiscoveryIq::Identity::language() const +{ + return m_language; +} + +void QXmppDiscoveryIq::Identity::setLanguage(const QString &language) +{ + m_language = language; +} + QString QXmppDiscoveryIq::Identity::name() const { return m_name; @@ -137,6 +173,24 @@ void QXmppDiscoveryIq::setQueryType(enum QXmppDiscoveryIq::QueryType type) m_queryType = type; } +/// Calculate the verification string for XEP-0115 : Entity Capabilities + +QByteArray QXmppDiscoveryIq::verificationString() const +{ + QString S; + QList sortedIdentities = m_identities; + qSort(sortedIdentities.begin(), sortedIdentities.end(), identityLessThan); + QStringList sortedFeatures = m_features; + qSort(sortedFeatures); + foreach (const QXmppDiscoveryIq::Identity &identity, sortedIdentities) + S += QString("%1/%2/%3/%4<").arg(identity.category(), identity.type(), identity.language(), identity.name()); + foreach (const QString &feature, sortedFeatures) + S += feature + QLatin1String("<"); + QCryptographicHash hasher(QCryptographicHash::Sha1); + hasher.addData(S.toUtf8()); + return hasher.result(); +} + bool QXmppDiscoveryIq::isDiscoveryIq(const QDomElement &element) { QDomElement queryElement = element.firstChildElement("query"); @@ -161,6 +215,7 @@ void QXmppDiscoveryIq::parseElementFromChild(const QDomElement &element) m_features.append(itemElement.attribute("var")); } else if (itemElement.tagName() == "identity") { QXmppDiscoveryIq::Identity identity; + identity.setLanguage(itemElement.attribute("xml:lang")); identity.setCategory(itemElement.attribute("category")); identity.setName(itemElement.attribute("name")); identity.setType(itemElement.attribute("type")); @@ -193,6 +248,7 @@ void QXmppDiscoveryIq::toXmlElementFromChild(QXmlStreamWriter *writer) const foreach (const QXmppDiscoveryIq::Identity& identity, m_identities) { writer->writeStartElement("identity"); + helperToXmlAddAttribute(writer, "xml:lang", identity.language()); helperToXmlAddAttribute(writer, "category", identity.category()); helperToXmlAddAttribute(writer, "name", identity.name()); helperToXmlAddAttribute(writer, "type", identity.type()); -- cgit v1.2.3