From db1b9a4c0b49efa170a50c3be3053e83db9566f0 Mon Sep 17 00:00:00 2001 From: Melvin Keskin Date: Wed, 7 Apr 2021 21:19:41 +0200 Subject: RegisterIq: Add out-of-band URL --- src/base/QXmppRegisterIq.cpp | 40 +++++++++++- src/base/QXmppRegisterIq.h | 3 + tests/qxmppregisteriq/tst_qxmppregisteriq.cpp | 87 +++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/base/QXmppRegisterIq.cpp b/src/base/QXmppRegisterIq.cpp index 27447f6d..18a92573 100644 --- a/src/base/QXmppRegisterIq.cpp +++ b/src/base/QXmppRegisterIq.cpp @@ -46,6 +46,7 @@ public: bool isRegistered; bool isRemove; QXmppBitsOfBinaryDataList bitsOfBinaryData; + QString outOfBandUrl; }; QXmppRegisterIqPrivate::QXmppRegisterIqPrivate() @@ -259,6 +260,26 @@ void QXmppRegisterIq::setBitsOfBinaryData(const QXmppBitsOfBinaryDataList &bitsO d->bitsOfBinaryData = bitsOfBinaryData; } +/// +/// Returns a \xep{0066, Out of Band Data} URL used for out-of-band registration. +/// +/// \since QXmpp 1.5 +/// +QString QXmppRegisterIq::outOfBandUrl() const +{ + return d->outOfBandUrl; +} + +/// +/// Sets a \xep{0066, Out of Band Data} URL used for out-of-band registration. +/// +/// \since QXmpp 1.5 +/// +void QXmppRegisterIq::setOutOfBandUrl(const QString &outOfBandUrl) +{ + d->outOfBandUrl = outOfBandUrl; +} + /// \cond bool QXmppRegisterIq::isRegisterIq(const QDomElement &element) { @@ -272,7 +293,17 @@ void QXmppRegisterIq::parseElementFromChild(const QDomElement &element) d->username = queryElement.firstChildElement(QStringLiteral("username")).text(); d->password = queryElement.firstChildElement(QStringLiteral("password")).text(); d->email = queryElement.firstChildElement(QStringLiteral("email")).text(); - d->form.parse(queryElement.firstChildElement(QStringLiteral("x"))); + + for (auto xElement = queryElement.firstChildElement(QStringLiteral("x")); + !xElement.isNull(); + xElement = xElement.nextSiblingElement(QStringLiteral("x"))) { + if (xElement.namespaceURI() == ns_data) { + d->form.parse(xElement); + } else if (xElement.namespaceURI() == ns_oob) { + d->outOfBandUrl = xElement.firstChildElement(QStringLiteral("url")).text(); + } + } + d->isRegistered = !queryElement.firstChildElement(ELEMENT_REGISTERED).isNull(); d->isRemove = !queryElement.firstChildElement(ELEMENT_REMOVE).isNull(); d->bitsOfBinaryData.parse(queryElement); @@ -309,6 +340,13 @@ void QXmppRegisterIq::toXmlElementFromChild(QXmlStreamWriter *writer) const d->form.toXml(writer); d->bitsOfBinaryData.toXml(writer); + if (!d->outOfBandUrl.isEmpty()) { + writer->writeStartElement(QStringLiteral("x")); + writer->writeDefaultNamespace(ns_oob); + writer->writeTextElement(QStringLiteral("url"), d->outOfBandUrl); + writer->writeEndElement(); + } + writer->writeEndElement(); } diff --git a/src/base/QXmppRegisterIq.h b/src/base/QXmppRegisterIq.h index 2e82576d..58e73e08 100644 --- a/src/base/QXmppRegisterIq.h +++ b/src/base/QXmppRegisterIq.h @@ -75,6 +75,9 @@ public: QXmppBitsOfBinaryDataList &bitsOfBinaryData(); void setBitsOfBinaryData(const QXmppBitsOfBinaryDataList &bitsOfBinaryData); + QString outOfBandUrl() const; + void setOutOfBandUrl(const QString &outOfBandUrl); + /// \cond static bool isRegisterIq(const QDomElement &element); /// \endcond diff --git a/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp b/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp index 7071ac2a..fc962d80 100644 --- a/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp +++ b/tests/qxmppregisteriq/tst_qxmppregisteriq.cpp @@ -38,6 +38,8 @@ private slots: void testGet(); void testResult(); void testResultWithForm(); + void testResultWithRedirection(); + void testResultWithFormAndRedirection(); void testSet(); void testSetWithForm(); void testBobData(); @@ -67,6 +69,7 @@ void tst_QXmppRegisterIq::testGet() QVERIFY(iq.password().isNull()); QVERIFY(iq.email().isNull()); QVERIFY(iq.form().isNull()); + QVERIFY(iq.outOfBandUrl().isNull()); serializePacket(iq, xml); } @@ -96,6 +99,7 @@ void tst_QXmppRegisterIq::testResult() QVERIFY(!iq.email().isNull()); QVERIFY(iq.email().isEmpty()); QVERIFY(iq.form().isNull()); + QVERIFY(iq.outOfBandUrl().isNull()); serializePacket(iq, xml); } @@ -143,6 +147,87 @@ void tst_QXmppRegisterIq::testResultWithForm() QVERIFY(iq.email().isNull()); QVERIFY(!iq.form().isNull()); QCOMPARE(iq.form().title(), QLatin1String("Contest Registration")); + QVERIFY(iq.outOfBandUrl().isNull()); + serializePacket(iq, xml); +} + +void tst_QXmppRegisterIq::testResultWithRedirection() +{ + const QByteArray xml( + "" + "" + "" + "To register, visit http://www.shakespeare.lit/contests.php" + "" + "" + "http://www.shakespeare.lit/contests.php" + "" + "" + ""); + + QXmppRegisterIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.id(), QLatin1String("reg3")); + QCOMPARE(iq.to(), QString()); + QCOMPARE(iq.from(), QString()); + QCOMPARE(iq.type(), QXmppIq::Result); + QCOMPARE(iq.instructions(), QLatin1String("To register, visit http://www.shakespeare.lit/contests.php")); + QVERIFY(iq.username().isNull()); + QVERIFY(iq.password().isNull()); + QVERIFY(iq.email().isNull()); + QVERIFY(iq.form().isNull()); + QCOMPARE(iq.outOfBandUrl(), QLatin1String("http://www.shakespeare.lit/contests.php")); + serializePacket(iq, xml); +} + +void tst_QXmppRegisterIq::testResultWithFormAndRedirection() +{ + const QByteArray xml( + "" + "" + "Use the enclosed form to register. If your Jabber client does not support Data Forms, visit http://www.shakespeare.lit/contests.php" + "" + "Contest Registration" + "" + "Please provide the following information" + "to sign up for our special contests!" + "" + "" + "jabber:iq:register" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "http://www.shakespeare.lit/contests.php" + "" + "" + ""); + + QXmppRegisterIq iq; + parsePacket(iq, xml); + QCOMPARE(iq.id(), QLatin1String("reg3")); + QCOMPARE(iq.to(), QLatin1String("juliet@capulet.com/balcony")); + QCOMPARE(iq.from(), QLatin1String("contests.shakespeare.lit")); + QCOMPARE(iq.type(), QXmppIq::Result); + QCOMPARE(iq.instructions(), QLatin1String("Use the enclosed form to register. If your Jabber client does not support Data Forms, visit http://www.shakespeare.lit/contests.php")); + QVERIFY(iq.username().isNull()); + QVERIFY(iq.password().isNull()); + QVERIFY(iq.email().isNull()); + QVERIFY(!iq.form().isNull()); + QCOMPARE(iq.form().title(), QLatin1String("Contest Registration")); + QCOMPARE(iq.outOfBandUrl(), QLatin1String("http://www.shakespeare.lit/contests.php")); serializePacket(iq, xml); } @@ -167,6 +252,7 @@ void tst_QXmppRegisterIq::testSet() QCOMPARE(iq.password(), QLatin1String("Calliope")); QCOMPARE(iq.email(), QLatin1String("bard@shakespeare.lit")); QVERIFY(iq.form().isNull()); + QVERIFY(iq.outOfBandUrl().isNull()); serializePacket(iq, xml); } @@ -205,6 +291,7 @@ void tst_QXmppRegisterIq::testSetWithForm() QVERIFY(iq.password().isNull()); QVERIFY(iq.email().isNull()); QVERIFY(!iq.form().isNull()); + QVERIFY(iq.outOfBandUrl().isNull()); serializePacket(iq, xml); QXmppRegisterIq sIq; -- cgit v1.2.3