From 1741dcf3761082c47c8961385331a4fa062fa232 Mon Sep 17 00:00:00 2001 From: servonic Date: Tue, 22 Sep 2015 15:27:48 +0200 Subject: [PATCH 1/2] Added possibility to QXmppServer to route a stanza to a specific resource without the need to add the resource to the stanza's JID. Closes #7 --- src/server/QXmppServer.cpp | 20 ++++++++++++++++---- src/server/QXmppServer.h | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/server/QXmppServer.cpp b/src/server/QXmppServer.cpp index 192a00932..11888c6f8 100644 --- a/src/server/QXmppServer.cpp +++ b/src/server/QXmppServer.cpp @@ -635,8 +635,9 @@ bool QXmppServer::listenForServers(const QHostAddress &address, quint16 port) /// Route an XMPP stanza. /// /// \param element +/// \param recipient -bool QXmppServer::sendElement(const QDomElement &element) +bool QXmppServer::sendElement(const QDomElement &element, const QString& resource) { // serialize data QByteArray data; @@ -645,14 +646,20 @@ bool QXmppServer::sendElement(const QDomElement &element) helperToXmlAddDomElement(&xmlStream, element, omitNamespaces); // route data - return d->routeData(element.attribute("to"), data); + if (!resource.isEmpty()) { + // ... to explicitly specified resource. Override original resource from "to" (if any) + return d->routeData(QXmppUtils::jidToBareJid(element.attribute("to")) + "/" + resource, data); + } else { + return d->routeData(element.attribute("to"), data); + } } /// Route an XMPP packet. /// /// \param packet +/// \param recipient -bool QXmppServer::sendPacket(const QXmppStanza &packet) +bool QXmppServer::sendPacket(const QXmppStanza &packet, const QString& resource) { // serialize data QByteArray data; @@ -660,7 +667,12 @@ bool QXmppServer::sendPacket(const QXmppStanza &packet) packet.toXml(&xmlStream); // route data - return d->routeData(packet.to(), data); + if (!resource.isEmpty()) { + // ... to explicitly specified resource. Override original resource from "to" (if any) + return d->routeData(QXmppUtils::jidToBareJid(packet.to()) + "/" + resource, data); + } else { + return d->routeData(packet.to(), data); + } } /// Add a new incoming client \a stream. diff --git a/src/server/QXmppServer.h b/src/server/QXmppServer.h index f6866f92f..52d9f143e 100644 --- a/src/server/QXmppServer.h +++ b/src/server/QXmppServer.h @@ -89,8 +89,8 @@ class QXMPP_EXPORT QXmppServer : public QXmppLoggable bool listenForClients(const QHostAddress &address = QHostAddress::Any, quint16 port = 5222); bool listenForServers(const QHostAddress &address = QHostAddress::Any, quint16 port = 5269); - bool sendElement(const QDomElement &element); - bool sendPacket(const QXmppStanza &stanza); + bool sendElement(const QDomElement &element, const QString& resource = QString()); + bool sendPacket(const QXmppStanza &stanza, const QString& resource = QString()); void addIncomingClient(QXmppIncomingClient *stream); From 0a069579d36704a3a494dcdd13b9b79d8ff1c234 Mon Sep 17 00:00:00 2001 From: servonic Date: Tue, 29 Sep 2015 19:08:24 +0200 Subject: [PATCH 2/2] moved decision for routing a stanza to a specific resource into routeData --- src/server/QXmppServer.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/server/QXmppServer.cpp b/src/server/QXmppServer.cpp index 11888c6f8..83c2939f9 100644 --- a/src/server/QXmppServer.cpp +++ b/src/server/QXmppServer.cpp @@ -76,7 +76,7 @@ class QXmppServerPrivate public: QXmppServerPrivate(QXmppServer *qq); void loadExtensions(QXmppServer *server); - bool routeData(const QString &to, const QByteArray &data); + bool routeData(const QString &to, const QByteArray &data, const QString& resource); void startExtensions(); void stopExtensions(); @@ -125,7 +125,7 @@ QXmppServerPrivate::QXmppServerPrivate(QXmppServer *qq) /// \param data /// -bool QXmppServerPrivate::routeData(const QString &to, const QByteArray &data) +bool QXmppServerPrivate::routeData(const QString &to, const QByteArray &data, const QString& resource) { // refuse to route packets to empty destination, own domain or sub-domains const QString toDomain = QXmppUtils::jidToDomain(to); @@ -136,7 +136,17 @@ bool QXmppServerPrivate::routeData(const QString &to, const QByteArray &data) // look for a client connection QList found; - if (QXmppUtils::jidToResource(to).isEmpty()) { + // issue#7: Is a specific destination resource requested? + if (!resource.isEmpty()){ + if (QXmppUtils::jidToResource(to).isEmpty()) { + QXmppIncomingClient *conn = incomingClientsByJid.value(to + "/" + resource); + if (conn) + found << conn; + } else { + // refuse to route to a specific resource if stanza is for a full JID already + return false; + } + } else if (QXmppUtils::jidToResource(to).isEmpty()) { foreach (QXmppIncomingClient *conn, incomingClientsByBareJid.value(to)) found << conn; } else { @@ -646,12 +656,7 @@ bool QXmppServer::sendElement(const QDomElement &element, const QString& resourc helperToXmlAddDomElement(&xmlStream, element, omitNamespaces); // route data - if (!resource.isEmpty()) { - // ... to explicitly specified resource. Override original resource from "to" (if any) - return d->routeData(QXmppUtils::jidToBareJid(element.attribute("to")) + "/" + resource, data); - } else { - return d->routeData(element.attribute("to"), data); - } + return d->routeData(element.attribute("to"), data, resource); } /// Route an XMPP packet. @@ -667,12 +672,7 @@ bool QXmppServer::sendPacket(const QXmppStanza &packet, const QString& resource) packet.toXml(&xmlStream); // route data - if (!resource.isEmpty()) { - // ... to explicitly specified resource. Override original resource from "to" (if any) - return d->routeData(QXmppUtils::jidToBareJid(packet.to()) + "/" + resource, data); - } else { - return d->routeData(packet.to(), data); - } + return d->routeData(packet.to(), data, resource); } /// Add a new incoming client \a stream.