diff options
Diffstat (limited to 'Swiften/Serializer')
6 files changed, 320 insertions, 0 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index 080e3df..72b6128 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -45,6 +45,8 @@ #include <Swiften/Serializer/PayloadSerializers/MAMFinSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MAMQuerySerializer.h> #include <Swiften/Serializer/PayloadSerializers/MAMResultSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/MIXDestroySerializer.h> #include <Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCAdminPayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCDestroyPayloadSerializer.h> @@ -156,6 +158,8 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new ResultSetSerializer()); serializers_.push_back(new ForwardedSerializer(this)); serializers_.push_back(new MIXParticipantSerializer()); + serializers_.push_back(new MIXJoinSerializer()); + serializers_.push_back(new MIXDestroySerializer()); serializers_.push_back(new MAMResultSerializer(this)); serializers_.push_back(new MAMQuerySerializer()); serializers_.push_back(new MAMFinSerializer()); diff --git a/Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.cpp new file mode 100644 index 0000000..cae15c2 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.h> + +#include <memory> + +#include <Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/FormSerializer.h> +#include <Swiften/Serializer/XML/XMLElement.h> +#include <Swiften/Serializer/XML/XMLRawTextNode.h> + +using namespace Swift; + +MIXJoinSerializer::MIXJoinSerializer() { +} + +MIXJoinSerializer::~MIXJoinSerializer() { +} + +std::string MIXJoinSerializer::serializePayload(std::shared_ptr<MIXJoin> payload) const { + if (!payload) { + return ""; + } + XMLElement element("join", "urn:xmpp:mix:1"); + if (payload->getChannel()) { + element.setAttribute("channel", *payload->getChannel()); + } + if (payload->getJID()) { + element.setAttribute("jid", *payload->getJID()); + } + for (const auto& item : payload->getSubscriptions()) { + element.addNode(std::make_shared<XMLRawTextNode>(MIXSubscribeSerializer().serialize(item))); + } + if (payload->getForm()) { + element.addNode(std::make_shared<XMLRawTextNode>(FormSerializer().serialize(payload->getForm()))); + } + return element.serialize(); +} diff --git a/Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.h b/Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.h new file mode 100644 index 0000000..a53fbe3 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <memory> + +#include <Swiften/Base/API.h> +#include <Swiften/Base/Override.h> +#include <Swiften/Elements/MIXJoin.h> +#include <Swiften/Serializer/GenericPayloadSerializer.h> + +namespace Swift { + + class SWIFTEN_API MIXJoinSerializer : public GenericPayloadSerializer<MIXJoin> { + public: + MIXJoinSerializer(); + virtual ~MIXJoinSerializer(); + + virtual std::string serializePayload(std::shared_ptr<MIXJoin>) const SWIFTEN_OVERRIDE; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.cpp new file mode 100644 index 0000000..3ce6d89 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.h> + +#include <memory> + +#include <Swiften/Serializer/XML/XMLElement.h> +#include <Swiften/Serializer/XML/XMLRawTextNode.h> + +using namespace Swift; + +MIXSubscribeSerializer::MIXSubscribeSerializer() { +} + +MIXSubscribeSerializer::~MIXSubscribeSerializer() { +} + +std::string MIXSubscribeSerializer::serializePayload(std::shared_ptr<MIXSubscribe> payload) const { + if (!payload) { + return ""; + } + XMLElement element("subscribe"); + element.setAttribute("node", payload->getNode()); + return element.serialize(); +} diff --git a/Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.h b/Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.h new file mode 100644 index 0000000..b343f86 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXSubscribeSerializer.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <memory> + +#include <Swiften/Base/API.h> +#include <Swiften/Base/Override.h> +#include <Swiften/Elements/MIXSubscribe.h> +#include <Swiften/Serializer/GenericPayloadSerializer.h> + +namespace Swift { + + class SWIFTEN_API MIXSubscribeSerializer : public GenericPayloadSerializer<MIXSubscribe> { + public: + MIXSubscribeSerializer(); + virtual ~MIXSubscribeSerializer(); + + virtual std::string serializePayload(std::shared_ptr<MIXSubscribe>) const SWIFTEN_OVERRIDE; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/MIXJoinSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/MIXJoinSerializerTest.cpp new file mode 100644 index 0000000..61d8a4a --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/MIXJoinSerializerTest.cpp @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <gtest/gtest.h> + +#include <Swiften/Serializer/PayloadSerializers/MIXJoinSerializer.h> + +using namespace Swift; + +TEST(MIXJoinSerializerTest, XEP0369_Example22) { + MIXJoinSerializer testling; + + auto join = std::make_shared<MIXJoin>(); + join->setChannel(JID("coven@mix.shakespeare.example")); + + std::shared_ptr<MIXSubscribe> node1(new MIXSubscribe()); + node1->setNode(std::string("urn:xmpp:mix:nodes:messages")); + join->addSubscription(node1); + std::shared_ptr<MIXSubscribe> node2(new MIXSubscribe()); + node2->setNode(std::string("urn:xmpp:mix:nodes:presence")); + join->addSubscription(node2); + std::shared_ptr<MIXSubscribe> node3(new MIXSubscribe()); + node3->setNode(std::string("urn:xmpp:mix:nodes:participants")); + join->addSubscription(node3); + std::shared_ptr<MIXSubscribe> node4(new MIXSubscribe()); + node4->setNode(std::string("urn:xmpp:mix:nodes:config")); + join->addSubscription(node4); + + std::string expectedResult = "<join channel=\"coven@mix.shakespeare.example\" xmlns=\"urn:xmpp:mix:1\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:presence\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:participants\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:config\"/>" + "</join>"; + ASSERT_EQ(expectedResult, testling.serialize(join)); +} + +TEST(MIXJoinSerializerTest, XEP0369_Example23) { + MIXJoinSerializer testling; + + std::shared_ptr<MIXJoin> join(new MIXJoin()); + + std::shared_ptr<MIXSubscribe> node1(new MIXSubscribe()); + node1->setNode(std::string("urn:xmpp:mix:nodes:messages")); + join->addSubscription(node1); + std::shared_ptr<MIXSubscribe> node2(new MIXSubscribe()); + node2->setNode(std::string("urn:xmpp:mix:nodes:presence")); + join->addSubscription(node2); + std::shared_ptr<MIXSubscribe> node3(new MIXSubscribe()); + node3->setNode(std::string("urn:xmpp:mix:nodes:participants")); + join->addSubscription(node3); + std::shared_ptr<MIXSubscribe> node4(new MIXSubscribe()); + node4->setNode(std::string("urn:xmpp:mix:nodes:config")); + join->addSubscription(node4); + + std::string expectedResult = "<join xmlns=\"urn:xmpp:mix:1\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:presence\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:participants\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:config\"/>" + "</join>"; + ASSERT_EQ(expectedResult, testling.serialize(join)); +} + +TEST(MIXJoinSerializerTest, XEP0369_Example24) { + MIXJoinSerializer testling; + + std::shared_ptr<MIXJoin> join(new MIXJoin()); + join->setJID(JID("123456#coven@mix.shakespeare.example")); + + std::shared_ptr<MIXSubscribe> node1(new MIXSubscribe()); + node1->setNode(std::string("urn:xmpp:mix:nodes:messages")); + join->addSubscription(node1); + std::shared_ptr<MIXSubscribe> node2(new MIXSubscribe()); + node2->setNode(std::string("urn:xmpp:mix:nodes:presence")); + join->addSubscription(node2); + std::shared_ptr<MIXSubscribe> node3(new MIXSubscribe()); + node3->setNode(std::string("urn:xmpp:mix:nodes:participants")); + join->addSubscription(node3); + std::shared_ptr<MIXSubscribe> node4(new MIXSubscribe()); + node4->setNode(std::string("urn:xmpp:mix:nodes:config")); + join->addSubscription(node4); + + std::string expectedResult = "<join jid=\"123456#coven@mix.shakespeare.example\" xmlns=\"urn:xmpp:mix:1\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:presence\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:participants\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:config\"/>" + "</join>"; + ASSERT_EQ(expectedResult, testling.serialize(join)); +} + +TEST(MIXJoinSerializerTest, XEP0369_Example29) { + MIXJoinSerializer testling; + + std::shared_ptr<MIXJoin> join(new MIXJoin()); + + std::shared_ptr<MIXSubscribe> node1(new MIXSubscribe()); + node1->setNode(std::string("urn:xmpp:mix:nodes:messages")); + join->addSubscription(node1); + std::shared_ptr<MIXSubscribe> node2(new MIXSubscribe()); + node2->setNode(std::string("urn:xmpp:mix:nodes:presence")); + join->addSubscription(node2); + + std::shared_ptr<Form> parameters(std::make_shared<Form>()); + parameters->setType(Form::Type::SubmitType); + + std::shared_ptr<FormField> fieldType = std::make_shared<FormField>(FormField::HiddenType); + fieldType->setName("FORM_TYPE"); + fieldType->addValue("urn:xmpp:mix:1"); + parameters->addField(fieldType); + + std::shared_ptr<FormField> fieldJIDVisibility = std::make_shared<FormField>(); + fieldJIDVisibility->setName("JID Visibility"); + fieldJIDVisibility->addValue("never"); + parameters->addField(fieldJIDVisibility); + + join->setForm(parameters); + + std::string expectedResult = "<join xmlns=\"urn:xmpp:mix:1\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:presence\"/>" + "<x type=\"submit\" xmlns=\"jabber:x:data\">" + "<field type=\"hidden\" var=\"FORM_TYPE\">" + "<value>urn:xmpp:mix:1</value>" + "</field>" + "<field var=\"JID Visibility\">" + "<value>never</value>" + "</field>" + "</x>" + "</join>"; + ASSERT_EQ(expectedResult, testling.serialize(join)); +} + +TEST(MIXJoinSerializerTest, XEP0369_Example30) { + MIXJoinSerializer testling; + + std::shared_ptr<MIXJoin> join(new MIXJoin()); + join->setJID(JID("hag66@shakespeare.example")); + + std::shared_ptr<MIXSubscribe> node1(new MIXSubscribe()); + node1->setNode(std::string("urn:xmpp:mix:nodes:messages")); + join->addSubscription(node1); + std::shared_ptr<MIXSubscribe> node2(new MIXSubscribe()); + node2->setNode(std::string("urn:xmpp:mix:nodes:presence")); + join->addSubscription(node2); + + std::shared_ptr<Form> parameters(std::make_shared<Form>()); + parameters->setType(Form::Type::ResultType); + + std::shared_ptr<FormField> fieldType = std::make_shared<FormField>(FormField::HiddenType); + fieldType->setName("FORM_TYPE"); + fieldType->addValue("urn:xmpp:mix:1"); + parameters->addField(fieldType); + + std::shared_ptr<FormField> fieldJIDVisibility = std::make_shared<FormField>(); + fieldJIDVisibility->setName("JID Visibility"); + fieldJIDVisibility->addValue("never"); + parameters->addField(fieldJIDVisibility); + + std::shared_ptr<FormField> fieldprivateMessages = std::make_shared<FormField>(); + fieldprivateMessages->setName("Private Messages"); + fieldprivateMessages->addValue("allow"); + parameters->addField(fieldprivateMessages); + + std::shared_ptr<FormField> vCard = std::make_shared<FormField>(); + vCard->setName("vCard"); + vCard->addValue("block"); + parameters->addField(vCard); + + join->setForm(parameters); + + std::string expectedResult = "<join jid=\"hag66@shakespeare.example\" xmlns=\"urn:xmpp:mix:1\">" + "<subscribe node=\"urn:xmpp:mix:nodes:messages\"/>" + "<subscribe node=\"urn:xmpp:mix:nodes:presence\"/>" + "<x type=\"result\" xmlns=\"jabber:x:data\">" + "<field type=\"hidden\" var=\"FORM_TYPE\">" + "<value>urn:xmpp:mix:1</value>" + "</field>" + "<field var=\"JID Visibility\">" + "<value>never</value>" + "</field>" + "<field var=\"Private Messages\">" + "<value>allow</value>" + "</field>" + "<field var=\"vCard\">" + "<value>block</value>" + "</field>" + "</x>" + "</join>"; + ASSERT_EQ(expectedResult, testling.serialize(join)); +} |