Skip to content

Commit

Permalink
Added possibility to QXmppServer to route a stanza to a specific reso…
Browse files Browse the repository at this point in the history
…urce without the need to add the resource to the stanza's JID. Closes qxmpp-project#7
  • Loading branch information
servonic committed Sep 22, 2015
1 parent 90b681f commit 0ebce1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/server/QXmppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -645,22 +646,33 @@ 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;
QXmlStreamWriter xmlStream(&data);
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.
Expand Down
4 changes: 2 additions & 2 deletions src/server/QXmppServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

2 comments on commit 0ebce1f

@jlaine
Copy link

@jlaine jlaine commented on 0ebce1f Sep 22, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I agree with this change, it makes it possible to route stanzas inconsistently. If routing decisions need to be taken, shouldn't that be straight in "routeData"?

@servonic
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this would make it possible to route a stanza to another resource than specified in the "to" attribute.
If I didn't misinterpret the code, routeData does not have a clue about the client connections' message priorities and thus cannot do the decision.

Would it be OK if I changed the code to only accept a resource when the "to" element is a bare JID and reject sending (return false) when "to" is a fullJID and a resource has been specified?

Edit: Or do you mean I shall pass the destination resource into routeData(...) and there do the check whether "to" is a bare or a full JID?

Please sign in to comment.