diff options
| author | Kevin Smith <git@kismith.co.uk> | 2012-02-06 12:12:00 (GMT) |
|---|---|---|
| committer | Kevin Smith <git@kismith.co.uk> | 2012-02-06 13:39:43 (GMT) |
| commit | f3d68b13e120f471326449f68aa4140587d444fc (patch) | |
| tree | a13586525ee07d8aff8ec9ec459caf37fd5a941c /Swiften/Parser | |
| parent | 42a1bbc568aab7e5b29c7fe16c13659291d01cae (diff) | |
| download | swift-contrib-f3d68b13e120f471326449f68aa4140587d444fc.zip swift-contrib-f3d68b13e120f471326449f68aa4140587d444fc.tar.bz2 | |
Parse mediated invite stuff
Diffstat (limited to 'Swiften/Parser')
| -rw-r--r-- | Swiften/Parser/PayloadParsers/MUCUserPayloadParser.cpp | 19 | ||||
| -rw-r--r-- | Swiften/Parser/PayloadParsers/UnitTest/MUCUserPayloadParserTest.cpp | 18 |
2 files changed, 37 insertions, 0 deletions
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 @@ -1,39 +1,58 @@ /* * Copyright (c) 2010 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <Swiften/Parser/PayloadParsers/MUCUserPayloadParser.h> #include <boost/lexical_cast.hpp> #include <Swiften/Parser/PayloadParserFactoryCollection.h> #include <Swiften/Parser/PayloadParserFactory.h> #include <Swiften/Base/foreach.h> #include <Swiften/Elements/MUCOccupant.h> #include <Swiften/Parser/Tree/TreeReparser.h> namespace Swift { void MUCUserPayloadParser::handleTree(ParserElement::ref root) { foreach (ParserElement::ref child, root->getAllChildren()) { if (child->getName() == "item" && child->getNamespace() == root->getNamespace()) { 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 { status.code = boost::lexical_cast<int>(child->getAttributes().getAttribute("code").c_str()); getPayloadInternal()->addStatusCode(status); } catch (boost::bad_lexical_cast&) { } } else { getPayloadInternal()->setPayload(TreeReparser::parseTree(child, factories)); } } } } 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 @@ -1,76 +1,94 @@ /* * Copyright (c) 2011 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include <Swiften/Base/foreach.h> #include <Swiften/Parser/PayloadParsers/MUCUserPayloadParser.h> #include <Swiften/Elements/MUCDestroyPayload.h> #include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> using namespace Swift; class MUCUserPayloadParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(MUCUserPayloadParserTest); CPPUNIT_TEST(testParseEmpty); CPPUNIT_TEST(testParse); CPPUNIT_TEST(testParseDestroy); + CPPUNIT_TEST(testParseInvite); CPPUNIT_TEST_SUITE_END(); public: MUCUserPayloadParserTest() {} void testParse() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse("<x xmlns=\"http://jabber.org/protocol/muc#user\"><status code='110'/><item affiliation=\"owner\" role=\"visitor\"><actor jid=\"kev@tester.lit\"/><reason>malice</reason></item><status code='210'/></x>")); bool found110 = false; bool found210 = false; MUCUserPayload::ref payload = boost::dynamic_pointer_cast<MUCUserPayload>(parser.getPayload()); foreach (MUCUserPayload::StatusCode status, payload->getStatusCodes()) { if (status.code == 110) found110 = true; if (status.code == 210) found210 = true; } MUCItem item = payload->getItems()[0]; CPPUNIT_ASSERT_EQUAL(MUCOccupant::Owner, item.affiliation.get()); CPPUNIT_ASSERT_EQUAL(MUCOccupant::Visitor, item.role.get()); CPPUNIT_ASSERT_EQUAL(JID("kev@tester.lit"), item.actor.get()); CPPUNIT_ASSERT_EQUAL(std::string("malice"), item.reason.get()); CPPUNIT_ASSERT(found110); CPPUNIT_ASSERT(found210); } void testParseEmpty() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse("<x xmlns=\"http://jabber.org/protocol/muc#user\"/>")); MUCUserPayload::ref payload = boost::dynamic_pointer_cast<MUCUserPayload>(parser.getPayload()); CPPUNIT_ASSERT(payload); CPPUNIT_ASSERT(payload->getItems().empty()); } void testParseDestroy() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse("<x xmlns=\"http://jabber.org/protocol/muc#user\"><destroy jid='alice@wonderland.lit'><reason>bert</reason></destroy></x>")); MUCUserPayload::ref payload = boost::dynamic_pointer_cast<MUCUserPayload>(parser.getPayload()); CPPUNIT_ASSERT(payload); MUCDestroyPayload::ref destroy = boost::dynamic_pointer_cast<MUCDestroyPayload>(payload->getPayload()); CPPUNIT_ASSERT(destroy); 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("<x xmlns=\"http://jabber.org/protocol/muc#user\"><invite from='crone1@shakespeare.lit/desktop' to='alice@wonderland.lit/xxx'> <reason>Hey Hecate, this is the place for all good witches!</reason> </invite> <password>cauldronburn</password></x>")); + + MUCUserPayload::ref payload = boost::dynamic_pointer_cast<MUCUserPayload>(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); |
Swift