aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2012-07-18 13:38:34 +0200
committerJeremy Lainé <jeremy.laine@m4x.org>2012-07-18 13:38:34 +0200
commitfd9999bd871da6181c561eeacd2aed984dae4699 (patch)
tree7606b6bb68080a1cf670137382c812424676735f
parentcf26a0767ffb97c3ed4931c2af84ebd38ed87fdf (diff)
downloadqxmpp-fd9999bd871da6181c561eeacd2aed984dae4699.tar.gz
add QXmppRegisterIq as defined by XEP-0077: In-Band Registration
-rw-r--r--src/base/QXmppRegisterIq.cpp140
-rw-r--r--src/base/QXmppRegisterIq.h74
-rw-r--r--src/base/base.pri2
-rw-r--r--tests/register.cpp147
-rw-r--r--tests/register.h36
-rw-r--r--tests/tests.cpp4
-rw-r--r--tests/tests.pro2
7 files changed, 405 insertions, 0 deletions
diff --git a/src/base/QXmppRegisterIq.cpp b/src/base/QXmppRegisterIq.cpp
new file mode 100644
index 00000000..323f7487
--- /dev/null
+++ b/src/base/QXmppRegisterIq.cpp
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2008-2012 The QXmpp developers
+ *
+ * Author:
+ * Jeremy Lainé
+ *
+ * Source:
+ * http://code.google.com/p/qxmpp
+ *
+ * This file is a part of QXmpp library.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+#include <QDomElement>
+
+#include "QXmppConstants.h"
+#include "QXmppRegisterIq.h"
+
+/// Returns the email for this registration IQ.
+
+QString QXmppRegisterIq::email() const
+{
+ return m_email;
+}
+
+/// Sets the \a email for this registration IQ.
+
+void QXmppRegisterIq::setEmail(const QString &email)
+{
+ m_email = email;
+}
+
+/// Returns the QXmppDataForm for this registration IQ.
+
+QXmppDataForm QXmppRegisterIq::form() const
+{
+ return m_form;
+}
+
+/// Sets the QXmppDataForm for this registration IQ.
+///
+/// \param form
+
+void QXmppRegisterIq::setForm(const QXmppDataForm &form)
+{
+ m_form = form;
+}
+
+/// Returns the instructions for this registration IQ.
+
+QString QXmppRegisterIq::instructions() const
+{
+ return m_instructions;
+}
+
+/// Sets the \a instructions for this registration IQ.
+
+void QXmppRegisterIq::setInstructions(const QString &instructions)
+{
+ m_instructions = instructions;
+}
+
+/// Returns the password for this registration IQ.
+
+QString QXmppRegisterIq::password() const
+{
+ return m_password;
+}
+
+/// Sets the \a password for this registration IQ.
+
+void QXmppRegisterIq::setPassword(const QString &password)
+{
+ m_password = password;
+}
+
+/// Returns the username for this registration IQ.
+
+QString QXmppRegisterIq::username() const
+{
+ return m_username;
+}
+
+/// Sets the \a username for this registration IQ.
+
+void QXmppRegisterIq::setUsername(const QString &username)
+{
+ m_username = username;
+}
+
+bool QXmppRegisterIq::isRegisterIq(const QDomElement &element)
+{
+ return (element.firstChildElement("query").namespaceURI() == ns_register);
+}
+
+void QXmppRegisterIq::parseElementFromChild(const QDomElement &element)
+{
+ QDomElement queryElement = element.firstChildElement("query");
+ m_instructions = queryElement.firstChildElement("instructions").text();
+ m_username = queryElement.firstChildElement("username").text();
+ m_password = queryElement.firstChildElement("password").text();
+ m_email = queryElement.firstChildElement("email").text();
+ m_form.parse(queryElement.firstChildElement("x"));
+}
+
+void QXmppRegisterIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
+{
+ writer->writeStartElement("query");
+ writer->writeAttribute("xmlns", ns_register);
+ if (!m_instructions.isEmpty())
+ writer->writeTextElement("instructions", m_instructions);
+
+ if (!m_username.isEmpty())
+ writer->writeTextElement("username", m_username);
+ else if (!m_username.isNull())
+ writer->writeEmptyElement("username");
+
+ if (!m_password.isEmpty())
+ writer->writeTextElement("password", m_password);
+ else if (!m_password.isNull())
+ writer->writeEmptyElement("password");
+
+ if (!m_email.isEmpty())
+ writer->writeTextElement("email", m_email);
+ else if (!m_email.isNull())
+ writer->writeEmptyElement("email");
+
+ m_form.toXml(writer);
+ writer->writeEndElement();
+}
diff --git a/src/base/QXmppRegisterIq.h b/src/base/QXmppRegisterIq.h
new file mode 100644
index 00000000..8c1b3d21
--- /dev/null
+++ b/src/base/QXmppRegisterIq.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2008-2012 The QXmpp developers
+ *
+ * Author:
+ * Jeremy Lainé
+ *
+ * Source:
+ * http://code.google.com/p/qxmpp
+ *
+ * This file is a part of QXmpp library.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+
+#ifndef QXMPPREGISTERIQ_H
+#define QXMPPREGISTERIQ_H
+
+#include "QXmppDataForm.h"
+#include "QXmppIq.h"
+
+/// \brief The QXmppRegisterIq class represents a registration IQ
+/// as defined by XEP-0077: In-Band Registration.
+///
+/// It is used to create an account on the server.
+///
+/// \ingroup Stanzas
+
+class QXMPP_EXPORT QXmppRegisterIq : public QXmppIq
+{
+public:
+ QString email() const;
+ void setEmail(const QString &email);
+
+ QXmppDataForm form() const;
+ void setForm(const QXmppDataForm &form);
+
+ QString instructions() const;
+ void setInstructions(const QString &instructions);
+
+ QString password() const;
+ void setPassword(const QString &username);
+
+ QString username() const;
+ void setUsername(const QString &username);
+
+ /// \cond
+ static bool isRegisterIq(const QDomElement &element);
+ /// \endcond
+
+protected:
+ /// \cond
+ void parseElementFromChild(const QDomElement &element);
+ void toXmlElementFromChild(QXmlStreamWriter *writer) const;
+ /// \endcond
+
+private:
+ QXmppDataForm m_form;
+ QString m_email;
+ QString m_instructions;
+ QString m_password;
+ QString m_username;
+};
+
+#endif
diff --git a/src/base/base.pri b/src/base/base.pri
index a76459a8..c50c49a6 100644
--- a/src/base/base.pri
+++ b/src/base/base.pri
@@ -23,6 +23,7 @@ INSTALL_HEADERS += \
base/QXmppPingIq.h \
base/QXmppPresence.h \
base/QXmppPubSubIq.h \
+ base/QXmppRegisterIq.h \
base/QXmppResultSet.h \
base/QXmppRosterIq.h \
base/QXmppRpcIq.h \
@@ -62,6 +63,7 @@ SOURCES += \
base/QXmppPingIq.cpp \
base/QXmppPresence.cpp \
base/QXmppPubSubIq.cpp \
+ base/QXmppRegisterIq.cpp \
base/QXmppResultSet.cpp \
base/QXmppRosterIq.cpp \
base/QXmppRpcIq.cpp \
diff --git a/tests/register.cpp b/tests/register.cpp
new file mode 100644
index 00000000..b8b9b547
--- /dev/null
+++ b/tests/register.cpp
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2008-2012 The QXmpp developers
+ *
+ * Author:
+ * Jeremy Lainé
+ *
+ * Source:
+ * http://code.google.com/p/qxmpp
+ *
+ * This file is a part of QXmpp library.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+#include "QXmppRegisterIq.h"
+
+#include "tests.h"
+#include "register.h"
+
+void tst_QXmppRegisterIq::testGet()
+{
+ const QByteArray xml(
+ "<iq id=\"reg1\" to=\"shakespeare.lit\" type=\"get\">"
+ "<query xmlns=\"jabber:iq:register\"/>"
+ "</iq>");
+
+ QXmppRegisterIq iq;
+ parsePacket(iq, xml);
+ QCOMPARE(iq.id(), QLatin1String("reg1"));
+ QCOMPARE(iq.to(), QLatin1String("shakespeare.lit"));
+ QCOMPARE(iq.from(), QString());
+ QCOMPARE(iq.type(), QXmppIq::Get);
+ QCOMPARE(iq.instructions(), QString());
+ QVERIFY(iq.username().isNull());
+ QVERIFY(iq.password().isNull());
+ QVERIFY(iq.email().isNull());
+ QVERIFY(iq.form().isNull());
+ serializePacket(iq, xml);
+}
+
+void tst_QXmppRegisterIq::testResult()
+{
+ const QByteArray xml(
+ "<iq id=\"reg1\" type=\"result\">"
+ "<query xmlns=\"jabber:iq:register\">"
+ "<instructions>Choose a username and password for use with this service. Please also provide your email address.</instructions>"
+ "<username/>"
+ "<password/>"
+ "<email/>"
+ "</query>"
+ "</iq>");
+
+ QXmppRegisterIq iq;
+ parsePacket(iq, xml);
+ QCOMPARE(iq.id(), QLatin1String("reg1"));
+ QCOMPARE(iq.to(), QString());
+ QCOMPARE(iq.from(), QString());
+ QCOMPARE(iq.type(), QXmppIq::Result);
+ QCOMPARE(iq.instructions(), QLatin1String("Choose a username and password for use with this service. Please also provide your email address."));
+ QVERIFY(!iq.username().isNull());
+ QVERIFY(iq.username().isEmpty());
+ QVERIFY(!iq.password().isNull());
+ QVERIFY(iq.password().isEmpty());
+ QVERIFY(!iq.email().isNull());
+ QVERIFY(iq.email().isEmpty());
+ QVERIFY(iq.form().isNull());
+ serializePacket(iq, xml);
+}
+
+void tst_QXmppRegisterIq::testResultWithForm()
+{
+ const QByteArray xml(
+ "<iq id=\"reg3\" to=\"juliet@capulet.com/balcony\" from=\"contests.shakespeare.lit\" type=\"result\">"
+ "<query xmlns=\"jabber:iq:register\">"
+ "<instructions>Use the enclosed form to register. If your Jabber client does not support Data Forms, visit http://www.shakespeare.lit/contests.php</instructions>"
+ "<x xmlns=\"jabber:x:data\" type=\"form\">"
+ "<title>Contest Registration</title>"
+ "<instructions>"
+ "Please provide the following information"
+ "to sign up for our special contests!"
+ "</instructions>"
+ "<field type=\"hidden\" var=\"FORM_TYPE\">"
+ "<value>jabber:iq:register</value>"
+ "</field>"
+ "<field type=\"text-single\" label=\"Given Name\" var=\"first\">"
+ "<required/>"
+ "</field>"
+ "<field type=\"text-single\" label=\"Family Name\" var=\"last\">"
+ "<required/>"
+ "</field>"
+ "<field type=\"text-single\" label=\"Email Address\" var=\"email\">"
+ "<required/>"
+ "</field>"
+ "<field type=\"list-single\" label=\"Gender\" var=\"x-gender\">"
+ "<option label=\"Male\"><value>M</value></option>"
+ "<option label=\"Female\"><value>F</value></option>"
+ "</field>"
+ "</x>"
+ "</query>"
+ "</iq>");
+
+ 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());
+ serializePacket(iq, xml);
+}
+
+void tst_QXmppRegisterIq::testSet()
+{
+ const QByteArray xml(
+ "<iq id=\"reg2\" type=\"set\">"
+ "<query xmlns=\"jabber:iq:register\">"
+ "<username>bill</username>"
+ "<password>Calliope</password>"
+ "<email>bard@shakespeare.lit</email>"
+ "</query>"
+ "</iq>");
+
+ QXmppRegisterIq iq;
+ parsePacket(iq, xml);
+ QCOMPARE(iq.id(), QLatin1String("reg2"));
+ QCOMPARE(iq.to(), QString());
+ QCOMPARE(iq.from(), QString());
+ QCOMPARE(iq.type(), QXmppIq::Set);
+ QCOMPARE(iq.username(), QLatin1String("bill"));
+ QCOMPARE(iq.password(), QLatin1String("Calliope"));
+ QCOMPARE(iq.email(), QLatin1String("bard@shakespeare.lit"));
+ QVERIFY(iq.form().isNull());
+ serializePacket(iq, xml);
+}
diff --git a/tests/register.h b/tests/register.h
new file mode 100644
index 00000000..f2ff6283
--- /dev/null
+++ b/tests/register.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2008-2012 The QXmpp developers
+ *
+ * Author:
+ * Jeremy Lainé
+ *
+ * Source:
+ * http://code.google.com/p/qxmpp
+ *
+ * This file is a part of QXmpp library.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ */
+
+#include <QObject>
+
+class tst_QXmppRegisterIq : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void testGet();
+ void testResult();
+ void testResultWithForm();
+ void testSet();
+};
+
diff --git a/tests/tests.cpp b/tests/tests.cpp
index 24ce8a2e..c9cd03f1 100644
--- a/tests/tests.cpp
+++ b/tests/tests.cpp
@@ -54,6 +54,7 @@
#include "QXmppEntityTimeIq.h"
#include "dataform.h"
+#include "register.h"
#include "rtp.h"
#include "tests.h"
@@ -1743,6 +1744,9 @@ int main(int argc, char *argv[])
TestPubSub testPubSub;
errors += QTest::qExec(&testPubSub);
+ tst_QXmppRegisterIq testRegister;
+ errors += QTest::qExec(&testRegister);
+
TestRsm testRsm;
errors += QTest::qExec(&testRsm);
diff --git a/tests/tests.pro b/tests/tests.pro
index 68e85927..47961519 100644
--- a/tests/tests.pro
+++ b/tests/tests.pro
@@ -7,10 +7,12 @@ TARGET = qxmpp-tests
RESOURCES += tests.qrc
SOURCES += \
dataform.cpp \
+ register.cpp \
rtp.cpp \
tests.cpp
HEADERS += \
dataform.h \
+ register.h \
rtp.h \
tests.h