From b8c1d6fb59bd4ae528d807fc30b02dab45aafabf Mon Sep 17 00:00:00 2001 From: Tarun Gupta <tarun1995gupta@gmail.com> Date: Sat, 10 Jun 2017 22:53:25 +0530 Subject: Adds MIX Participant Element, its Parsers and Serializers. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Adds tests for MIX Participant Parser and Serializer from XEP-0369, which passes. Tested on Ubuntu 16.04 LTS. Change-Id: Iee2d96c8ebdf9461c28518f8f8c28c11d2a94524 diff --git a/Swiften/Elements/MIXParticipant.h b/Swiften/Elements/MIXParticipant.h new file mode 100644 index 0000000..22133b8 --- /dev/null +++ b/Swiften/Elements/MIXParticipant.h @@ -0,0 +1,47 @@ +/* + * 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 <string> + +#include <boost/optional.hpp> + +#include <Swiften/Base/API.h> +#include <Swiften/Elements/Payload.h> +#include <Swiften/JID/JID.h> + +namespace Swift { + class SWIFTEN_API MIXParticipant : public Payload { + public: + using ref = std::shared_ptr<MIXParticipant>; + + public: + + MIXParticipant() {} + + const boost::optional<std::string>& getNick() const { + return nick_; + } + + void setNick(const std::string& nick) { + nick_ = nick; + } + + const boost::optional<JID>& getJID() const { + return jid_; + } + + void setJID(const JID& jid) { + jid_ = jid; + } + + private: + boost::optional<JID> jid_; + boost::optional<std::string> nick_; + }; +} diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index 4ad943a..f657e11 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -51,6 +51,7 @@ #include <Swiften/Parser/PayloadParsers/MAMFinParser.h> #include <Swiften/Parser/PayloadParsers/MAMQueryParser.h> #include <Swiften/Parser/PayloadParsers/MAMResultParser.h> +#include <Swiften/Parser/PayloadParsers/MIXParticipantParserFactory.h> #include <Swiften/Parser/PayloadParsers/MUCAdminPayloadParser.h> #include <Swiften/Parser/PayloadParsers/MUCDestroyPayloadParser.h> #include <Swiften/Parser/PayloadParsers/MUCInvitationPayloadParser.h> @@ -129,6 +130,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(std::make_shared<PrivateStorageParserFactory>(this)); factories_.push_back(std::make_shared<ChatStateParserFactory>()); factories_.push_back(std::make_shared<ClientStateParserFactory>()); + factories_.push_back(std::make_shared<MIXParticipantParserFactory>()); factories_.push_back(std::make_shared<MUCUserPayloadParserFactory>(this)); factories_.push_back(std::make_shared<MUCOwnerPayloadParserFactory>(this)); factories_.push_back(std::make_shared<GenericPayloadParserFactory<MUCInvitationPayloadParser> >("x", "jabber:x:conference")); diff --git a/Swiften/Parser/PayloadParsers/MIXParticipantParser.cpp b/Swiften/Parser/PayloadParsers/MIXParticipantParser.cpp new file mode 100644 index 0000000..0ab2b87 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXParticipantParser.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Parser/PayloadParsers/MIXParticipantParser.h> + +namespace Swift { + +void MIXParticipantParser::handleTree(ParserElement::ref root) { + for (const auto& child : root->getAllChildren()) { + if (child->getName() == "nick" && child->getNamespace() == root->getNamespace()) { + getPayloadInternal()->setNick(child->getText()); + } + else if (child->getName() == "jid" && child->getNamespace() == root->getNamespace()) { + if (boost::optional<JID> jid = JID::parse(child->getText())) { + getPayloadInternal()->setJID(*jid); + } + } + } +} + +} diff --git a/Swiften/Parser/PayloadParsers/MIXParticipantParser.h b/Swiften/Parser/PayloadParsers/MIXParticipantParser.h new file mode 100644 index 0000000..0a3ea57 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXParticipantParser.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Base/API.h> +#include <Swiften/Elements/MIXParticipant.h> +#include <Swiften/Parser/GenericPayloadTreeParser.h> + +namespace Swift { + class SWIFTEN_API MIXParticipantParser : public GenericPayloadTreeParser<MIXParticipant> { + public: + MIXParticipantParser() {} + virtual void handleTree(ParserElement::ref root); + }; +} diff --git a/Swiften/Parser/PayloadParsers/MIXParticipantParserFactory.h b/Swiften/Parser/PayloadParsers/MIXParticipantParserFactory.h new file mode 100644 index 0000000..cd7c17e --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MIXParticipantParserFactory.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Base/API.h> +#include <Swiften/Parser/GenericPayloadParserFactory.h> +#include <Swiften/Parser/PayloadParsers/MIXParticipantParser.h> + +namespace Swift { + class SWIFTEN_API MIXParticipantParserFactory : public PayloadParserFactory { + public: + MIXParticipantParserFactory() { + } + + virtual bool canParse(const std::string& element, const std::string& ns, const AttributeMap&) const { + return element == "participant" && ns == "urn:xmpp:mix:0"; + } + + virtual PayloadParser* createPayloadParser() { + return new MIXParticipantParser(); + } + }; +} diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MIXParticipantParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MIXParticipantParserTest.cpp new file mode 100644 index 0000000..27c02e7 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/MIXParticipantParserTest.cpp @@ -0,0 +1,59 @@ +/* + * 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/Elements/MIXParticipant.h> +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> + +using namespace Swift; + +TEST(MIXParticipantParserTest, XEP0369_Example1_ParticipantWithNick) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "<participant xmlns='urn:xmpp:mix:0'> <nick>thirdwitch</nick> </participant>" + )); + + MIXParticipant::ref payload = parser.getPayload<MIXParticipant>(); + ASSERT_TRUE(payload); + + ASSERT_TRUE(payload->getNick()); + std::string nick = *payload->getNick(); + ASSERT_EQ("thirdwitch", nick); + + ASSERT_FALSE(payload->getJID()); +} + +TEST(MIXParticipantParserTest, XEP0369_Example2_ParticipantWithJID) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "<participant xmlns='urn:xmpp:mix:0'> <jid>hecate@mix.shakespeare.example</jid> </participant>" + )); + + MIXParticipant::ref payload = parser.getPayload<MIXParticipant>(); + ASSERT_TRUE(payload); + + ASSERT_TRUE(payload->getJID()); + JID jid = *payload->getJID(); + ASSERT_EQ("hecate@mix.shakespeare.example", jid.toString()); + + ASSERT_FALSE(payload->getNick()); +} + +TEST(MIXParticipantParserTest, XEP0369_Example27_ParticipantEmpty) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "<participant xmlns='urn:xmpp:mix:0'/>" + )); + + MIXParticipant::ref payload = parser.getPayload<MIXParticipant>(); + ASSERT_TRUE(payload); + ASSERT_FALSE(payload->getNick()); + ASSERT_FALSE(payload->getJID()); +} + + + diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index 1797de1..3ef6237 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -67,6 +67,7 @@ sources = [ "PayloadParsers/VCardParser.cpp", "PayloadParsers/VCardUpdateParser.cpp", "PayloadParsers/DelayParser.cpp", + "PayloadParsers/MIXParticipantParser.cpp", "PayloadParsers/MUCUserPayloadParser.cpp", "PayloadParsers/MUCAdminPayloadParser.cpp", "PayloadParsers/MUCOwnerPayloadParser.cpp", diff --git a/Swiften/SConscript b/Swiften/SConscript index c77d80f..c2c3cb6 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -207,6 +207,7 @@ if env["SCONS_STAGE"] == "build" : "Serializer/PayloadSerializers/DiscoItemsSerializer.cpp", "Serializer/PayloadSerializers/ErrorSerializer.cpp", "Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp", + "Serializer/PayloadSerializers/MIXParticipantSerializer.cpp", "Serializer/PayloadSerializers/MUCPayloadSerializer.cpp", "Serializer/PayloadSerializers/MUCUserPayloadSerializer.cpp", "Serializer/PayloadSerializers/MUCAdminPayloadSerializer.cpp", @@ -430,6 +431,7 @@ if env["SCONS_STAGE"] == "build" : File("Parser/PayloadParsers/UnitTest/IBBParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/InBandRegistrationPayloadParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/JingleParserTest.cpp"), + File("Parser/PayloadParsers/UnitTest/MIXParticipantParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/SearchPayloadParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/SecurityLabelParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/SecurityLabelsCatalogParserTest.cpp"), @@ -509,6 +511,7 @@ if env["SCONS_STAGE"] == "build" : File("Serializer/PayloadSerializers/UnitTest/PrivateStorageSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/ReplaceSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MUCAdminPayloadSerializerTest.cpp"), + File("Serializer/PayloadSerializers/UnitTest/MIXParticipantSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/JingleSerializersTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/DeliveryReceiptSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/IdleSerializerTest.cpp"), diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index bb3632b..080e3df 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -45,6 +45,7 @@ #include <Swiften/Serializer/PayloadSerializers/MAMFinSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MAMQuerySerializer.h> #include <Swiften/Serializer/PayloadSerializers/MAMResultSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCAdminPayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCDestroyPayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCInvitationPayloadSerializer.h> @@ -154,6 +155,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new ResultSetSerializer()); serializers_.push_back(new ForwardedSerializer(this)); + serializers_.push_back(new MIXParticipantSerializer()); serializers_.push_back(new MAMResultSerializer(this)); serializers_.push_back(new MAMQuerySerializer()); serializers_.push_back(new MAMFinSerializer()); diff --git a/Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.cpp new file mode 100644 index 0000000..3f79dfa --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h> + +#include <memory> + +#include <Swiften/Serializer/XML/XMLElement.h> +#include <Swiften/Serializer/XML/XMLRawTextNode.h> +#include <Swiften/Serializer/XML/XMLTextNode.h> + +namespace Swift { + +MIXParticipantSerializer::MIXParticipantSerializer() : GenericPayloadSerializer<MIXParticipant>() { +} + +std::string MIXParticipantSerializer::serializePayload(std::shared_ptr<MIXParticipant> payload) const { + XMLElement participantElement("participant", "urn:xmpp:mix:0"); + + if (payload->getNick()) { + std::shared_ptr<XMLElement> nickElement = std::make_shared<XMLElement>("nick"); + nickElement->addNode(std::make_shared<XMLTextNode>(*payload->getNick())); + participantElement.addNode(nickElement); + } + + if (payload->getJID()) { + std::shared_ptr<XMLElement> jidElement = std::make_shared<XMLElement>("jid"); + jidElement->addNode(std::make_shared<XMLTextNode>(*payload->getJID())); + participantElement.addNode(jidElement); + } + return participantElement.serialize(); +} + +} diff --git a/Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h b/Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h new file mode 100644 index 0000000..ff6ca3a --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MIXParticipantSerializer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Base/API.h> +#include <Swiften/Elements/MIXParticipant.h> +#include <Swiften/Serializer/GenericPayloadSerializer.h> + +namespace Swift { + class SWIFTEN_API MIXParticipantSerializer : public GenericPayloadSerializer<MIXParticipant> { + public: + MIXParticipantSerializer(); + + virtual std::string serializePayload(std::shared_ptr<MIXParticipant> participant) const; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/MIXParticipantSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/MIXParticipantSerializerTest.cpp new file mode 100644 index 0000000..a298d78 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/MIXParticipantSerializerTest.cpp @@ -0,0 +1,59 @@ +/* + * 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/MIXParticipantSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> + +using namespace Swift; + +TEST(MIXParticipantSerializerTest, testSerializeEmpty) { + MIXParticipantSerializer testling; + + std::shared_ptr<MIXParticipant> participant(new MIXParticipant()); + + std::string expectedResult = "<participant xmlns=\"urn:xmpp:mix:0\"/>"; + ASSERT_EQ(expectedResult, testling.serialize(participant)); +} + +TEST(MIXParticipantSerializerTest, testSerializeNick) { + MIXParticipantSerializer testling; + + std::shared_ptr<MIXParticipant> participant(new MIXParticipant()); + participant->setNick("thirdwitch"); + + std::string expectedResult = "<participant xmlns=\"urn:xmpp:mix:0\">" + "<nick>thirdwitch</nick>" + "</participant>"; + ASSERT_EQ(expectedResult, testling.serialize(participant)); +} + +TEST(MIXParticipantSerializerTest, testSerializeJID) { + MIXParticipantSerializer testling; + + std::shared_ptr<MIXParticipant> participant(new MIXParticipant()); + participant->setJID(JID("hecate@mix.shakespeare.example")); + + std::string expectedResult = "<participant xmlns=\"urn:xmpp:mix:0\">" + "<jid>hecate@mix.shakespeare.example</jid>" + "</participant>"; + ASSERT_EQ(expectedResult, testling.serialize(participant)); +} + +TEST(MIXParticipantSerializerTest, testSerializeJIDAndNick) { + MIXParticipantSerializer testling; + + std::shared_ptr<MIXParticipant> participant(new MIXParticipant()); + participant->setNick("thirdwitch"); + participant->setJID(JID("hecate@mix.shakespeare.example")); + + std::string expectedResult = "<participant xmlns=\"urn:xmpp:mix:0\">" + "<nick>thirdwitch</nick>" + "<jid>hecate@mix.shakespeare.example</jid>" + "</participant>"; + ASSERT_EQ(expectedResult, testling.serialize(participant)); +} -- cgit v0.10.2-6-g49f6