From f3d68b13e120f471326449f68aa4140587d444fc Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Mon, 6 Feb 2012 12:12:00 +0000 Subject: Parse mediated invite stuff diff --git a/Swiften/Elements/MUCUserPayload.h b/Swiften/Elements/MUCUserPayload.h index c9ea62c..fcb585e 100644 --- a/Swiften/Elements/MUCUserPayload.h +++ b/Swiften/Elements/MUCUserPayload.h @@ -27,17 +27,26 @@ namespace Swift { int code; }; - // struct Password { + //struct Password { - // } + //} // struct History { // } - // struct Invite { - - // } + /** + * reason is optional. + * from and to are mutually exclusive. + * From is used for MUC sending to invited client. To is used sending to MUC from inviting client. + * from is the JID the MUC claims the invite is from. + * to is the JID to send the invite to. + */ + struct Invite { + std::string reason; + JID from; + JID to; + }; MUCUserPayload() { } @@ -58,9 +67,29 @@ namespace Swift { payload_ = p; } + const boost::optional& getPassword() const { + return password_; + } + + void setPassword(const std::string& password) { + password_ = password; + } + + + const boost::optional& getInvite() const { + return invite_; + } + + void setInvite(const Invite& invite) { + invite_ = invite; + } + + private: std::vector items_; std::vector statusCodes_; boost::shared_ptr payload_; + boost::optional password_; + boost::optional invite_; }; } diff --git a/Swiften/Parser/PayloadParsers/MUCUserPayloadParser.cpp b/Swiften/Parser/PayloadParsers/MUCUserPayloadParser.cpp index 2da8b35..d206010 100644 --- a/Swiften/Parser/PayloadParsers/MUCUserPayloadParser.cpp +++ b/Swiften/Parser/PayloadParsers/MUCUserPayloadParser.cpp @@ -22,6 +22,25 @@ void MUCUserPayloadParser::handleTree(ParserElement::ref root) { MUCItem item = MUCItemParser::itemFromTree(child); getPayloadInternal()->addItem(item); } + else if (child->getName() == "password" && child->getNamespace() == root->getNamespace()) { + getPayloadInternal()->setPassword(child->getText()); + } + else if (child->getName() == "invite" && child->getNamespace() == root->getNamespace()) { + MUCUserPayload::Invite invite; + std::string to = child->getAttributes().getAttribute("to"); + if (!to.empty()) { + invite.to = to; + } + std::string from = child->getAttributes().getAttribute("from"); + if (!from.empty()) { + invite.from = from; + } + ParserElement::ref reason = child->getChild("reason", root->getNamespace()); + if (reason) { + invite.reason = reason->getText(); + } + getPayloadInternal()->setInvite(invite); + } else if (child->getName() == "status" && child->getNamespace() == root->getNamespace()) { MUCUserPayload::StatusCode status; try { diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MUCUserPayloadParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MUCUserPayloadParserTest.cpp index 45862e2..40d7358 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/MUCUserPayloadParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/MUCUserPayloadParserTest.cpp @@ -20,6 +20,7 @@ class MUCUserPayloadParserTest : public CppUnit::TestFixture CPPUNIT_TEST(testParseEmpty); CPPUNIT_TEST(testParse); CPPUNIT_TEST(testParseDestroy); + CPPUNIT_TEST(testParseInvite); CPPUNIT_TEST_SUITE_END(); public: @@ -71,6 +72,23 @@ class MUCUserPayloadParserTest : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(std::string("bert"), destroy->getReason()); CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit"), destroy->getNewVenue()); } + + void testParseInvite() { + PayloadsParserTester parser; + + CPPUNIT_ASSERT(parser.parse(" Hey Hecate, this is the place for all good witches! cauldronburn")); + + MUCUserPayload::ref payload = boost::dynamic_pointer_cast(parser.getPayload()); + CPPUNIT_ASSERT(payload); + CPPUNIT_ASSERT(payload->getInvite()); + CPPUNIT_ASSERT(payload->getPassword()); + CPPUNIT_ASSERT_EQUAL(std::string("cauldronburn"), *payload->getPassword()); + MUCUserPayload::Invite invite = *payload->getInvite(); + CPPUNIT_ASSERT_EQUAL(std::string("Hey Hecate, this is the place for all good witches!"), invite.reason); + CPPUNIT_ASSERT_EQUAL(JID("crone1@shakespeare.lit/desktop"), invite.from); + CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/xxx"), invite.to); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(MUCUserPayloadParserTest); diff --git a/Swiften/Serializer/PayloadSerializers/MUCUserPayloadSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MUCUserPayloadSerializer.cpp index 96fba90..66ca5d0 100644 --- a/Swiften/Serializer/PayloadSerializers/MUCUserPayloadSerializer.cpp +++ b/Swiften/Serializer/PayloadSerializers/MUCUserPayloadSerializer.cpp @@ -34,6 +34,28 @@ std::string MUCUserPayloadSerializer::serializePayload(boost::shared_ptrgetItems()) { mucElement.addNode(MUCItemSerializer::itemToElement(item)); } + + if (payload->getPassword()) { + boost::shared_ptr passwordElement = boost::make_shared("password"); + passwordElement->addNode(boost::make_shared(*payload->getPassword())); + } + + if (payload->getInvite()) { + MUCUserPayload::Invite invite = *payload->getInvite(); + boost::shared_ptr inviteElement = boost::make_shared("invite"); + if (invite.to.isValid()) { + inviteElement->setAttribute("to", invite.to.toString()); + } + if (invite.from.isValid()) { + inviteElement->setAttribute("from", invite.from.toString()); + } + if (!invite.reason.empty()) { + boost::shared_ptr reasonElement = boost::make_shared("reason"); + reasonElement->addNode(boost::make_shared(invite.reason)); + } + mucElement.addNode(inviteElement); + } + boost::shared_ptr childPayload = payload->getPayload(); if (childPayload) { PayloadSerializer* serializer = serializers->getPayloadSerializer(childPayload); -- cgit v0.10.2-6-g49f6