diff options
author | Tobias Markmann <tm@ayena.de> | 2014-10-24 09:57:13 (GMT) |
---|---|---|
committer | Swift Review <review@swift.im> | 2014-10-30 18:55:14 (GMT) |
commit | 8096f80861667381b777af774cfd446d6fc8cda8 (patch) | |
tree | 46d050b1e678609c241d162b06cc511fba698888 | |
parent | 5cf50d46aed4d2dac333e5e3b3bc2f57f7a1b835 (diff) | |
download | swift-8096f80861667381b777af774cfd446d6fc8cda8.zip swift-8096f80861667381b777af774cfd446d6fc8cda8.tar.bz2 |
Brining XEP-0313 (MAM) implementation in line with version 3.0.
Added support for <fin/> element, including serializer/parsers and unit tests for
them. Added more unit tests based on XEP examples for existing parsers. Removed
unneccesarry includes from existing MAM implementation.
Test-Information:
Existing and new unit tests pass successfully.
Change-Id: I7e6bf85e0961d59801b452e4559cc1db9e9e6ed8
28 files changed, 375 insertions, 290 deletions
diff --git a/Sluift/ElementConvertors/MAMArchivedConvertor.cpp b/Sluift/ElementConvertors/MAMArchivedConvertor.cpp deleted file mode 100644 index 39ac7df..0000000 --- a/Sluift/ElementConvertors/MAMArchivedConvertor.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include <boost/numeric/conversion/cast.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <lua.hpp> -#include <Sluift/ElementConvertors/MAMArchivedConvertor.h> - -#pragma clang diagnostic ignored "-Wunused-private-field" - -using namespace Swift; - -MAMArchivedConvertor::MAMArchivedConvertor(LuaElementConvertors* convertors) : - GenericLuaElementConvertor<MAMArchived>("mam_archived"), - convertors(convertors) { -} - -MAMArchivedConvertor::~MAMArchivedConvertor() { -} - -boost::shared_ptr<MAMArchived> MAMArchivedConvertor::doConvertFromLua(lua_State* L) { - boost::shared_ptr<MAMArchived> result = boost::make_shared<MAMArchived>(); - lua_getfield(L, -1, "by"); - if (lua_isstring(L, -1)) { - result->setBy(JID(std::string(lua_tostring(L, -1)))); - } - lua_pop(L, 1); - lua_getfield(L, -1, "id"); - if (lua_isstring(L, -1)) { - result->setID(std::string(lua_tostring(L, -1))); - } - lua_pop(L, 1); - return result; -} - -void MAMArchivedConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<MAMArchived> payload) { - lua_createtable(L, 0, 0); - lua_pushstring(L, payload->getBy().toString().c_str()); - lua_setfield(L, -2, "by"); - lua_pushstring(L, payload->getID().c_str()); - lua_setfield(L, -2, "id"); -} - -boost::optional<LuaElementConvertor::Documentation> MAMArchivedConvertor::getDocumentation() const { - return Documentation( - "MAMArchived", - "This table has the following fields:\n\n" - "- `by`: string\n" - "- `id`: string\n" - ); -} diff --git a/Sluift/ElementConvertors/MAMArchivedConvertor.h b/Sluift/ElementConvertors/MAMArchivedConvertor.h deleted file mode 100644 index 9d95e4a..0000000 --- a/Sluift/ElementConvertors/MAMArchivedConvertor.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include <Sluift/GenericLuaElementConvertor.h> -#include <Swiften/Base/Override.h> -#include <Swiften/Elements/MAMArchived.h> - -namespace Swift { - class LuaElementConvertors; - - class MAMArchivedConvertor : public GenericLuaElementConvertor<MAMArchived> { - public: - MAMArchivedConvertor(LuaElementConvertors* convertors); - virtual ~MAMArchivedConvertor(); - - virtual boost::shared_ptr<MAMArchived> doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE; - virtual void doConvertToLua(lua_State*, boost::shared_ptr<MAMArchived>) SWIFTEN_OVERRIDE; - virtual boost::optional<Documentation> getDocumentation() const SWIFTEN_OVERRIDE; - - private: - LuaElementConvertors* convertors; - }; -} - diff --git a/Sluift/ElementConvertors/SConscript b/Sluift/ElementConvertors/SConscript index 5ff5379..33d40ab 100644 --- a/Sluift/ElementConvertors/SConscript +++ b/Sluift/ElementConvertors/SConscript @@ -47,7 +47,6 @@ convertors = [ env.File("ForwardedConvertor.cpp"), env.File("MAMResultConvertor.cpp"), env.File("MAMQueryConvertor.cpp"), - env.File("MAMArchivedConvertor.cpp"), env.File("SubjectConvertor.cpp"), env.File("IsodeIQDelegationConvertor.cpp") ] diff --git a/Sluift/LuaElementConvertors.cpp b/Sluift/LuaElementConvertors.cpp index 253a820..d2af664 100644 --- a/Sluift/LuaElementConvertors.cpp +++ b/Sluift/LuaElementConvertors.cpp @@ -33,7 +33,6 @@ #include <Sluift/ElementConvertors/ForwardedConvertor.h> #include <Sluift/ElementConvertors/MAMResultConvertor.h> #include <Sluift/ElementConvertors/MAMQueryConvertor.h> -#include <Sluift/ElementConvertors/MAMArchivedConvertor.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/Lua/Exception.h> @@ -61,7 +60,6 @@ LuaElementConvertors::LuaElementConvertors() { convertors.push_back(boost::make_shared<ForwardedConvertor>(this)); convertors.push_back(boost::make_shared<MAMResultConvertor>(this)); convertors.push_back(boost::make_shared<MAMQueryConvertor>(this)); - convertors.push_back(boost::make_shared<MAMArchivedConvertor>(this)); convertors.push_back(boost::make_shared<DOMElementConvertor>()); convertors.push_back(boost::make_shared<RawXMLElementConvertor>()); convertors.push_back(boost::make_shared<DefaultElementConvertor>()); diff --git a/Swiften/Elements/MAMArchived.h b/Swiften/Elements/MAMArchived.h deleted file mode 100644 index df83427..0000000 --- a/Swiften/Elements/MAMArchived.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#pragma once - -#include <boost/optional.hpp> -#include <Swiften/Base/API.h> -#include <Swiften/Elements/Payload.h> -#include <Swiften/JID/JID.h> - -namespace Swift { - class SWIFTEN_API MAMArchived : public Payload { - public: - virtual ~MAMArchived(); - - void setBy(const JID& by) { by_ = by; } - const JID& getBy() const { return by_; } - - void setID(const std::string& id) { id_ = id; } - const std::string& getID() const { return id_; } - - private: - JID by_; - std::string id_; - }; -} diff --git a/Swiften/Elements/MAMArchived.cpp b/Swiften/Elements/MAMFin.cpp index 4ec5750..163ee05 100644 --- a/Swiften/Elements/MAMArchived.cpp +++ b/Swiften/Elements/MAMFin.cpp @@ -4,9 +4,11 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#include <Swiften/Elements/MAMArchived.h> +#include <Swiften/Elements/MAMFin.h> -using namespace Swift; +namespace Swift { + +MAMFin::~MAMFin() { +} -MAMArchived::~MAMArchived() { } diff --git a/Swiften/Elements/MAMFin.h b/Swiften/Elements/MAMFin.h new file mode 100644 index 0000000..5393230 --- /dev/null +++ b/Swiften/Elements/MAMFin.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <string> + +#include <boost/shared_ptr.hpp> +#include <boost/optional.hpp> + +#include <Swiften/Base/API.h> +#include <Swiften/Elements/Payload.h> +#include <Swiften/Elements/ResultSet.h> + +namespace Swift { + class SWIFTEN_API MAMFin : public Payload { + public: + MAMFin() : isComplete_(false), isStable_(true) {} + virtual ~MAMFin(); + + void setComplete(const bool isComplete) { + isComplete_ = isComplete; + } + + bool isComplete() const { + return isComplete_; + } + + void setStable(const bool isStable) { + isStable_ = isStable; + } + + bool isStable() const { + return isStable_; + } + + void setResultSet(boost::shared_ptr<ResultSet> resultSet) { + resultSet_ = resultSet; + } + + boost::shared_ptr<ResultSet> getResultSet() const { + return resultSet_; + } + + void setQueryID(const std::string& queryID) { + queryID_ = queryID; + } + + const boost::optional<std::string>& getQueryID() const { + return queryID_; + } + + + private: + bool isComplete_; + bool isStable_; + boost::shared_ptr<ResultSet> resultSet_; + boost::optional<std::string> queryID_; + }; +} diff --git a/Swiften/Elements/MAMQuery.h b/Swiften/Elements/MAMQuery.h index 3f3724e..09b5af0 100644 --- a/Swiften/Elements/MAMQuery.h +++ b/Swiften/Elements/MAMQuery.h @@ -6,11 +6,13 @@ #pragma once +#include <string> + +#include <boost/shared_ptr.hpp> #include <boost/optional.hpp> + #include <Swiften/Base/API.h> #include <Swiften/Elements/Form.h> -#include <Swiften/Elements/Forwarded.h> -#include <Swiften/Elements/MAMResult.h> #include <Swiften/Elements/Payload.h> #include <Swiften/Elements/ResultSet.h> diff --git a/Swiften/Elements/MAMResult.h b/Swiften/Elements/MAMResult.h index 7d43902..17c7527 100644 --- a/Swiften/Elements/MAMResult.h +++ b/Swiften/Elements/MAMResult.h @@ -6,7 +6,10 @@ #pragma once +#include <string> + #include <boost/optional.hpp> + #include <Swiften/Base/API.h> #include <Swiften/Elements/ContainerPayload.h> #include <Swiften/Elements/Forwarded.h> diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index 49dd1e3..de7386b 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -46,7 +46,7 @@ #include <Swiften/Parser/PayloadParsers/JingleReasonParser.h> #include <Swiften/Parser/PayloadParsers/JingleS5BTransportMethodPayloadParser.h> #include <Swiften/Parser/PayloadParsers/LastParser.h> -#include <Swiften/Parser/PayloadParsers/MAMArchivedParser.h> +#include <Swiften/Parser/PayloadParsers/MAMFinParser.h> #include <Swiften/Parser/PayloadParsers/MAMQueryParser.h> #include <Swiften/Parser/PayloadParsers/MAMResultParser.h> #include <Swiften/Parser/PayloadParsers/MUCAdminPayloadParser.h> @@ -157,7 +157,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(boost::make_shared<GenericPayloadParserFactory2<ForwardedParser> >("forwarded", "urn:xmpp:forward:0", this)); factories_.push_back(boost::make_shared<GenericPayloadParserFactory2<MAMResultParser> >("result", "urn:xmpp:mam:0", this)); factories_.push_back(boost::make_shared<GenericPayloadParserFactory<MAMQueryParser> >("query", "urn:xmpp:mam:0")); - factories_.push_back(boost::make_shared<GenericPayloadParserFactory<MAMArchivedParser> >("archived", "urn:xmpp:mam:0")); + factories_.push_back(boost::make_shared<GenericPayloadParserFactory<MAMFinParser> >("fin", "urn:xmpp:mam:0")); factories_.push_back(boost::make_shared<GenericPayloadParserFactory2<IsodeIQDelegationParser> >("delegate", "http://isode.com/iq_delegation", this)); foreach(shared_ptr<PayloadParserFactory> factory, factories_) { diff --git a/Swiften/Parser/PayloadParsers/MAMArchivedParser.cpp b/Swiften/Parser/PayloadParsers/MAMArchivedParser.cpp deleted file mode 100644 index 616d41a..0000000 --- a/Swiften/Parser/PayloadParsers/MAMArchivedParser.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include <boost/lexical_cast.hpp> -#include <boost/optional.hpp> -#include <Swiften/Base/DateTime.h> -#include <Swiften/Parser/PayloadParserFactory.h> -#include <Swiften/Parser/PayloadParserFactoryCollection.h> -#include <Swiften/Parser/PayloadParsers/MAMArchivedParser.h> - -using namespace Swift; - -MAMArchivedParser::MAMArchivedParser() : level_(TopLevel) { -} - -void MAMArchivedParser::handleStartElement(const std::string&, const std::string&, const AttributeMap& attributes) { - if (level_ == TopLevel) { - boost::optional<std::string> attributeValue; - if ((attributeValue = attributes.getAttributeValue("by"))) { - getPayloadInternal()->setBy(*attributeValue); - } - if ((attributeValue = attributes.getAttributeValue("id"))) { - getPayloadInternal()->setID(*attributeValue); - } - } - - ++level_; -} - -void MAMArchivedParser::handleEndElement(const std::string&, const std::string&) { - --level_; -} - -void MAMArchivedParser::handleCharacterData(const std::string&) { -} diff --git a/Swiften/Parser/PayloadParsers/MAMFinParser.cpp b/Swiften/Parser/PayloadParsers/MAMFinParser.cpp new file mode 100644 index 0000000..cab493b --- /dev/null +++ b/Swiften/Parser/PayloadParsers/MAMFinParser.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <boost/lexical_cast.hpp> +#include <boost/optional.hpp> +#include <Swiften/Base/DateTime.h> +#include <Swiften/Parser/PayloadParserFactory.h> +#include <Swiften/Parser/PayloadParserFactoryCollection.h> +#include <Swiften/Parser/PayloadParsers/ResultSetParser.h> +#include <Swiften/Parser/PayloadParsers/MAMFinParser.h> + +using namespace Swift; + +MAMFinParser::MAMFinParser() : level_(TopLevel) { +} + +void MAMFinParser::handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) { + if (level_ == TopLevel) { + getPayloadInternal()->setComplete(attributes.getBoolAttribute("complete", false)); + getPayloadInternal()->setStable(attributes.getBoolAttribute("stable", true)); + boost::optional<std::string> attributeValue; + if ((attributeValue = attributes.getAttributeValue("queryid"))) { + getPayloadInternal()->setQueryID(*attributeValue); + } + } + else if (level_ == PayloadLevel) { + if (element == "set" && ns == "http://jabber.org/protocol/rsm") { + resultSetParser_ = boost::make_shared<ResultSetParser>(); + } + } + + if (resultSetParser_) { /* parsing a nested ResultSet */ + resultSetParser_->handleStartElement(element, ns, attributes); + } + + ++level_; +} + +void MAMFinParser::handleEndElement(const std::string& element, const std::string& ns) { + --level_; + + if (resultSetParser_ && level_ >= PayloadLevel) { + resultSetParser_->handleEndElement(element, ns); + } + if (resultSetParser_ && level_ == PayloadLevel) { + /* done parsing nested ResultSet */ + getPayloadInternal()->setResultSet(boost::dynamic_pointer_cast<ResultSet>(resultSetParser_->getPayload())); + resultSetParser_.reset(); + } +} + +void MAMFinParser::handleCharacterData(const std::string& data) { + if (resultSetParser_) { + resultSetParser_->handleCharacterData(data); + } +} diff --git a/Swiften/Parser/PayloadParsers/MAMArchivedParser.h b/Swiften/Parser/PayloadParsers/MAMFinParser.h index b92b41a..08a7f16 100644 --- a/Swiften/Parser/PayloadParsers/MAMArchivedParser.h +++ b/Swiften/Parser/PayloadParsers/MAMFinParser.h @@ -10,25 +10,27 @@ #include <Swiften/Base/Override.h> #include <Swiften/Base/API.h> -#include <Swiften/Elements/MAMArchived.h> #include <Swiften/Parser/GenericPayloadParser.h> +#include <Swiften/Elements/MAMFin.h> namespace Swift { - class PayloadParserFactoryCollection; + class ResultSetParser; - class SWIFTEN_API MAMArchivedParser : public GenericPayloadParser<MAMArchived> { + class SWIFTEN_API MAMFinParser : public GenericPayloadParser<MAMFin> { public: - MAMArchivedParser(); + MAMFinParser(); virtual void handleStartElement(const std::string& element, const std::string& ns, const AttributeMap& attributes) SWIFTEN_OVERRIDE; virtual void handleEndElement(const std::string& element, const std::string&) SWIFTEN_OVERRIDE; virtual void handleCharacterData(const std::string& data) SWIFTEN_OVERRIDE; enum Level { - TopLevel = 0 + TopLevel = 0, + PayloadLevel = 1 }; private: + boost::shared_ptr<ResultSetParser> resultSetParser_; int level_; }; } diff --git a/Swiften/Parser/PayloadParsers/MAMQueryParser.cpp b/Swiften/Parser/PayloadParsers/MAMQueryParser.cpp index 9ae8e41..6b3dc65 100644 --- a/Swiften/Parser/PayloadParsers/MAMQueryParser.cpp +++ b/Swiften/Parser/PayloadParsers/MAMQueryParser.cpp @@ -7,8 +7,6 @@ #include <boost/lexical_cast.hpp> #include <boost/optional.hpp> #include <Swiften/Base/DateTime.h> -#include <Swiften/Parser/PayloadParserFactory.h> -#include <Swiften/Parser/PayloadParserFactoryCollection.h> #include <Swiften/Parser/PayloadParsers/FormParser.h> #include <Swiften/Parser/PayloadParsers/ResultSetParser.h> #include <Swiften/Parser/PayloadParsers/MAMQueryParser.h> diff --git a/Swiften/Parser/PayloadParsers/MAMQueryParser.h b/Swiften/Parser/PayloadParsers/MAMQueryParser.h index 7bbdacb..d160926 100644 --- a/Swiften/Parser/PayloadParsers/MAMQueryParser.h +++ b/Swiften/Parser/PayloadParsers/MAMQueryParser.h @@ -6,6 +6,8 @@ #pragma once +#include <string> + #include <boost/shared_ptr.hpp> #include <Swiften/Base/Override.h> @@ -14,7 +16,6 @@ #include <Swiften/Parser/GenericPayloadParser.h> namespace Swift { - class PayloadParserFactoryCollection; class ResultSetParser; class FormParser; diff --git a/Swiften/Parser/PayloadParsers/MAMResultParser.cpp b/Swiften/Parser/PayloadParsers/MAMResultParser.cpp index e4eec3b..55db8ad 100644 --- a/Swiften/Parser/PayloadParsers/MAMResultParser.cpp +++ b/Swiften/Parser/PayloadParsers/MAMResultParser.cpp @@ -6,9 +6,8 @@ #include <boost/lexical_cast.hpp> #include <boost/optional.hpp> + #include <Swiften/Base/DateTime.h> -#include <Swiften/Parser/PayloadParserFactory.h> -#include <Swiften/Parser/PayloadParserFactoryCollection.h> #include <Swiften/Parser/PayloadParsers/ForwardedParser.h> #include <Swiften/Parser/PayloadParsers/MAMResultParser.h> diff --git a/Swiften/Parser/PayloadParsers/MAMResultParser.h b/Swiften/Parser/PayloadParsers/MAMResultParser.h index 39ff20a..cb58732 100644 --- a/Swiften/Parser/PayloadParsers/MAMResultParser.h +++ b/Swiften/Parser/PayloadParsers/MAMResultParser.h @@ -6,6 +6,8 @@ #pragma once +#include <string> + #include <boost/shared_ptr.hpp> #include <Swiften/Base/Override.h> diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MAMArchivedParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MAMArchivedParserTest.cpp deleted file mode 100644 index 3e65cc6..0000000 --- a/Swiften/Parser/PayloadParsers/UnitTest/MAMArchivedParserTest.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * 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/DateTime.h> -#include <Swiften/Parser/PayloadParsers/MAMArchivedParser.h> -#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> - -using namespace Swift; - -class MAMArchivedParserTest : public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE(MAMArchivedParserTest); - CPPUNIT_TEST(testParse); - CPPUNIT_TEST_SUITE_END(); - - public: - void testParse() { - PayloadsParserTester parser; - CPPUNIT_ASSERT(parser.parse( - "<archived by=\"juliet@capulet.lit\" id=\"28482-98726-73623\" xmlns=\"urn:xmpp:mam:0\"/>")); - - boost::shared_ptr<MAMArchived> payload = parser.getPayload<MAMArchived>(); - CPPUNIT_ASSERT(!!payload); - CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit"), payload->getBy()); - CPPUNIT_ASSERT_EQUAL(std::string("28482-98726-73623"), payload->getID()); - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(MAMArchivedParserTest); diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MAMFinParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MAMFinParserTest.cpp new file mode 100644 index 0000000..3f7cdc9 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/MAMFinParserTest.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * 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/Elements/MAMFin.h> +#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> + +using namespace Swift; + +class MAMFinParserTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(MAMFinParserTest); + CPPUNIT_TEST(testParse_XEP0313_Exmaple1); + CPPUNIT_TEST(testParse_XEP0313_Exmaple9); + CPPUNIT_TEST_SUITE_END(); + + public: + void testParse_XEP0313_Exmaple1() { + PayloadsParserTester parser; + CPPUNIT_ASSERT(parser.parse( + "<fin xmlns='urn:xmpp:mam:0' queryid='f27' />")); + + boost::shared_ptr<MAMFin> payload = parser.getPayload<MAMFin>(); + CPPUNIT_ASSERT(!!payload); + CPPUNIT_ASSERT_EQUAL(false, payload->isComplete()); + CPPUNIT_ASSERT_EQUAL(true, payload->isStable()); + + boost::optional<std::string> queryID = payload->getQueryID(); + CPPUNIT_ASSERT(queryID); + CPPUNIT_ASSERT_EQUAL(std::string("f27"), queryID.get()); + } + + void testParse_XEP0313_Exmaple9() { + PayloadsParserTester parser; + CPPUNIT_ASSERT(parser.parse( + "<fin xmlns='urn:xmpp:mam:0' complete='true'>" + "<set xmlns='http://jabber.org/protocol/rsm'>" + "<first index='0'>23452-4534-1</first>" + "<last>390-2342-22</last>" + "<count>16</count>" + "</set>" + "</fin>")); + + boost::shared_ptr<MAMFin> payload = parser.getPayload<MAMFin>(); + CPPUNIT_ASSERT(!!payload); + CPPUNIT_ASSERT_EQUAL(true, payload->isComplete()); + CPPUNIT_ASSERT_EQUAL(true, payload->isStable()); + + CPPUNIT_ASSERT(!!payload->getResultSet()); + boost::shared_ptr<ResultSet> resultSet = payload->getResultSet(); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(MAMFinParserTest); diff --git a/Swiften/Parser/PayloadParsers/UnitTest/MAMQueryParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/MAMQueryParserTest.cpp index ddcd7c4..e1c8e3e 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/MAMQueryParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/MAMQueryParserTest.cpp @@ -17,6 +17,8 @@ class MAMQueryParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(MAMQueryParserTest); CPPUNIT_TEST(testParse); + CPPUNIT_TEST(testParse_XEP0313_Example3); + CPPUNIT_TEST(testParse_XEP0313_Example4); CPPUNIT_TEST(testParseEmpty); CPPUNIT_TEST_SUITE_END(); @@ -58,6 +60,49 @@ class MAMQueryParserTest : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(*resultSet->getMaxItems(), 10); } + void testParse_XEP0313_Example3() { + PayloadsParserTester parser; + CPPUNIT_ASSERT(parser.parse( + "<query xmlns='urn:xmpp:mam:0'>" + "<x xmlns='jabber:x:data'>" + "<field var='FORM_TYPE'>" + "<value>urn:xmpp:mam:0</value>" + "</field>" + "<field var='with'>" + "<value>juliet@capulet.lit</value>" + "</field>" + "</x>" + "</query>")); + + boost::shared_ptr<MAMQuery> payload = parser.getPayload<MAMQuery>(); + CPPUNIT_ASSERT(!!payload && !!payload->getForm() && !!payload->getForm()->getField("FORM_TYPE") && !!payload->getForm()->getField("with")); + CPPUNIT_ASSERT_EQUAL(std::string("urn:xmpp:mam:0"), payload->getForm()->getField("FORM_TYPE")->getTextSingleValue()); + CPPUNIT_ASSERT_EQUAL(std::string("juliet@capulet.lit"), payload->getForm()->getField("with")->getTextSingleValue()); + } + + void testParse_XEP0313_Example4() { + PayloadsParserTester parser; + CPPUNIT_ASSERT(parser.parse( + "<query xmlns='urn:xmpp:mam:0'>" + "<x xmlns='jabber:x:data'>" + "<field var='FORM_TYPE'>" + "<value>urn:xmpp:mam:0</value>" + "</field>" + "<field var='start'>" + "<value>2010-06-07T00:00:00Z</value>" + "</field>" + "<field var='end'>" + "<value>2010-07-07T13:23:54Z</value>" + "</field>" + "</x>" + "</query>")); + boost::shared_ptr<MAMQuery> payload = parser.getPayload<MAMQuery>(); + CPPUNIT_ASSERT(!!payload && !!payload->getForm() && !!payload->getForm()->getField("FORM_TYPE") && !!payload->getForm()->getField("start") && !!payload->getForm()->getField("start")); + CPPUNIT_ASSERT_EQUAL(std::string("urn:xmpp:mam:0"), payload->getForm()->getField("FORM_TYPE")->getTextSingleValue()); + CPPUNIT_ASSERT_EQUAL(std::string("2010-06-07T00:00:00Z"), payload->getForm()->getField("start")->getTextSingleValue()); + CPPUNIT_ASSERT_EQUAL(std::string("2010-07-07T13:23:54Z"), payload->getForm()->getField("end")->getTextSingleValue()); + } + void testParseEmpty() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( @@ -71,6 +116,8 @@ class MAMQueryParserTest : public CppUnit::TestFixture CPPUNIT_ASSERT(!payload->getForm()); CPPUNIT_ASSERT(!payload->getResultSet()); } + + }; CPPUNIT_TEST_SUITE_REGISTRATION(MAMQueryParserTest); diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index e748320..94ea6d3 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -79,9 +79,9 @@ sources = [ "PayloadParsers/PubSubErrorParserFactory.cpp", "PayloadParsers/ResultSetParser.cpp", "PayloadParsers/ForwardedParser.cpp", + "PayloadParsers/MAMFinParser.cpp", "PayloadParsers/MAMResultParser.cpp", "PayloadParsers/MAMQueryParser.cpp", - "PayloadParsers/MAMArchivedParser.cpp", "PayloadParsers/IsodeIQDelegationParser.cpp", "PlatformXMLParserFactory.cpp", "PresenceParser.cpp", diff --git a/Swiften/SConscript b/Swiften/SConscript index 29298ca..877b084 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -149,7 +149,7 @@ if env["SCONS_STAGE"] == "build" : "Elements/Forwarded.cpp", "Elements/MAMResult.cpp", "Elements/MAMQuery.cpp", - "Elements/MAMArchived.cpp", + "Elements/MAMFin.cpp", "Elements/IsodeIQDelegation.cpp", "Entity/Entity.cpp", "Entity/PayloadPersister.cpp", @@ -231,9 +231,9 @@ if env["SCONS_STAGE"] == "build" : "Serializer/PayloadSerializers/WhiteboardSerializer.cpp", "Serializer/PayloadSerializers/ResultSetSerializer.cpp", "Serializer/PayloadSerializers/ForwardedSerializer.cpp", + "Serializer/PayloadSerializers/MAMFinSerializer.cpp", "Serializer/PayloadSerializers/MAMResultSerializer.cpp", "Serializer/PayloadSerializers/MAMQuerySerializer.cpp", - "Serializer/PayloadSerializers/MAMArchivedSerializer.cpp", "Serializer/PayloadSerializers/IsodeIQDelegationSerializer.cpp", "Serializer/PresenceSerializer.cpp", "Serializer/StanzaSerializer.cpp", @@ -426,9 +426,9 @@ if env["SCONS_STAGE"] == "build" : File("Parser/PayloadParsers/UnitTest/IdleParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/ResultSetParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/ForwardedParserTest.cpp"), + File("Parser/PayloadParsers/UnitTest/MAMFinParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MAMResultParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/MAMQueryParserTest.cpp"), - File("Parser/PayloadParsers/UnitTest/MAMArchivedParserTest.cpp"), File("Parser/UnitTest/BOSHBodyExtractorTest.cpp"), File("Parser/UnitTest/AttributeMapTest.cpp"), File("Parser/UnitTest/EnumParserTest.cpp"), @@ -483,9 +483,9 @@ if env["SCONS_STAGE"] == "build" : File("Serializer/PayloadSerializers/UnitTest/IdleSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/ResultSetSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/ForwardedSerializerTest.cpp"), + File("Serializer/PayloadSerializers/UnitTest/MAMFinSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MAMResultSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/MAMQuerySerializerTest.cpp"), - File("Serializer/PayloadSerializers/UnitTest/MAMArchivedSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/PubSubItemSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/PubSubItemsSerializerTest.cpp"), File("Serializer/UnitTest/StreamFeaturesSerializerTest.cpp"), diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index dcfb4ed..33f288d 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -36,7 +36,6 @@ #include <Swiften/Serializer/PayloadSerializers/JinglePayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/JingleS5BTransportPayloadSerializer.h> #include <Swiften/Serializer/PayloadSerializers/LastSerializer.h> -#include <Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MAMQuerySerializer.h> #include <Swiften/Serializer/PayloadSerializers/MAMResultSerializer.h> #include <Swiften/Serializer/PayloadSerializers/MUCAdminPayloadSerializer.h> @@ -147,7 +146,6 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new ForwardedSerializer(this)); serializers_.push_back(new MAMResultSerializer(this)); serializers_.push_back(new MAMQuerySerializer()); - serializers_.push_back(new MAMArchivedSerializer()); serializers_.push_back(new IsodeIQDelegationSerializer(this)); foreach(PayloadSerializer* serializer, serializers_) { diff --git a/Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.cpp deleted file mode 100644 index 0a49a4b..0000000 --- a/Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include <boost/lexical_cast.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <Swiften/Serializer/XML/XMLElement.h> -#include <Swiften/Serializer/XML/XMLRawTextNode.h> -#include <Swiften/Serializer/PayloadSerializerCollection.h> -#include <Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.h> - -using namespace Swift; - -MAMArchivedSerializer::MAMArchivedSerializer() { -} - -MAMArchivedSerializer::~MAMArchivedSerializer() { -} - -std::string MAMArchivedSerializer::serializePayload(boost::shared_ptr<MAMArchived> payload) const { - if (!payload) { - return ""; - } - - XMLElement element("archived", "urn:xmpp:mam:0"); - element.setAttribute("by", payload->getBy()); - element.setAttribute("id", payload->getID()); - - return element.serialize(); -} diff --git a/Swiften/Serializer/PayloadSerializers/MAMFinSerializer.cpp b/Swiften/Serializer/PayloadSerializers/MAMFinSerializer.cpp new file mode 100644 index 0000000..8e4c2eb --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/MAMFinSerializer.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <boost/lexical_cast.hpp> +#include <boost/smart_ptr/make_shared.hpp> +#include <Swiften/Serializer/XML/XMLElement.h> +#include <Swiften/Serializer/XML/XMLRawTextNode.h> +#include <Swiften/Serializer/PayloadSerializerCollection.h> +#include <Swiften/Serializer/PayloadSerializers/MAMFinSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/ResultSetSerializer.h> + +using namespace Swift; + +MAMFinSerializer::MAMFinSerializer() { +} + +MAMFinSerializer::~MAMFinSerializer() { +} + +std::string MAMFinSerializer::serializePayload(boost::shared_ptr<MAMFin> payload) const { + if (!payload) { + return ""; + } + + XMLElement element("fin", "urn:xmpp:mam:0"); + + if (payload->isComplete()) { + element.setAttribute("complete", "true"); + } + + if (!payload->isStable()) { + element.setAttribute("stable", "false"); + } + + if (payload->getQueryID()) { + element.setAttribute("queryid", *payload->getQueryID()); + } + + if (payload->getResultSet()) { + element.addNode(boost::make_shared<XMLRawTextNode>(ResultSetSerializer().serialize(payload->getResultSet()))); + } + + return element.serialize(); +} diff --git a/Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.h b/Swiften/Serializer/PayloadSerializers/MAMFinSerializer.h index 67fffcb..e07bf46 100644 --- a/Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.h +++ b/Swiften/Serializer/PayloadSerializers/MAMFinSerializer.h @@ -9,17 +9,17 @@ #include <boost/shared_ptr.hpp> #include <Swiften/Base/Override.h> #include <Swiften/Base/API.h> -#include <Swiften/Elements/MAMArchived.h> +#include <Swiften/Elements/MAMFin.h> #include <Swiften/Serializer/GenericPayloadSerializer.h> namespace Swift { class PayloadSerializerCollection; - class SWIFTEN_API MAMArchivedSerializer : public GenericPayloadSerializer<MAMArchived> { + class SWIFTEN_API MAMFinSerializer : public GenericPayloadSerializer<MAMFin> { public: - MAMArchivedSerializer(); - virtual ~MAMArchivedSerializer(); + MAMFinSerializer(); + virtual ~MAMFinSerializer(); - virtual std::string serializePayload(boost::shared_ptr<MAMArchived>) const SWIFTEN_OVERRIDE; + virtual std::string serializePayload(boost::shared_ptr<MAMFin>) const SWIFTEN_OVERRIDE; }; } diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/MAMArchivedSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/MAMArchivedSerializerTest.cpp deleted file mode 100644 index 2bd5c6a..0000000 --- a/Swiften/Serializer/PayloadSerializers/UnitTest/MAMArchivedSerializerTest.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2014 Kevin Smith and Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. - */ - -#include <boost/smart_ptr/make_shared.hpp> - -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> - -#include <Swiften/Base/DateTime.h> -#include <Swiften/Serializer/PayloadSerializers/MAMArchivedSerializer.h> -#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> - -using namespace Swift; - -class MAMArchivedSerializerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(MAMArchivedSerializerTest); - CPPUNIT_TEST(testSerialize); - CPPUNIT_TEST_SUITE_END(); - - public: - void testSerialize() { - MAMArchivedSerializer serializer; - - boost::shared_ptr<MAMArchived> archived(boost::make_shared<MAMArchived>()); - archived->setBy("juliet@capulet.lit"); - archived->setID("28482-98726-73623"); - - std::string expectedResult = - "<archived by=\"juliet@capulet.lit\" id=\"28482-98726-73623\" xmlns=\"urn:xmpp:mam:0\"/>"; - - CPPUNIT_ASSERT_EQUAL(expectedResult, serializer.serialize(archived)); - } - - private: - FullPayloadSerializerCollection serializers; -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(MAMArchivedSerializerTest); diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/MAMFinSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/MAMFinSerializerTest.cpp new file mode 100644 index 0000000..372af97 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/MAMFinSerializerTest.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Kevin Smith and Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <boost/smart_ptr/make_shared.hpp> + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include <Swiften/Serializer/PayloadSerializers/MAMFinSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> + +using namespace Swift; + +class MAMFinSerializerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(MAMFinSerializerTest); + CPPUNIT_TEST(testSerialize_XEP0313_Exmaple1); + CPPUNIT_TEST(testSerialize_XEP0313_Exmaple9); + CPPUNIT_TEST_SUITE_END(); + + public: + void testSerialize_XEP0313_Exmaple1() { + MAMFinSerializer serializer; + + boost::shared_ptr<MAMFin> fin = boost::make_shared<MAMFin>(); + fin->setQueryID("f27"); + + std::string expectedResult = + "<fin queryid=\"f27\" xmlns=\"urn:xmpp:mam:0\"/>"; + CPPUNIT_ASSERT_EQUAL(expectedResult, serializer.serialize(fin)); + } + + void testSerialize_XEP0313_Exmaple9() { + MAMFinSerializer serializer; + + boost::shared_ptr<MAMFin> fin = boost::make_shared<MAMFin>(); + fin->setComplete(true); + + boost::shared_ptr<ResultSet> set = boost::make_shared<ResultSet>(); + set->setFirstID(std::string("23452-4534-1")); + set->setFirstIDIndex(0); + set->setLastID(std::string("390-2342-22")); + set->setCount(16); + + fin->setResultSet(set); + + std::string expectedResult = + "<fin complete=\"true\" xmlns=\"urn:xmpp:mam:0\">" + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + "<count>16</count>" + "<first index=\"0\">23452-4534-1</first>" + "<last>390-2342-22</last>" + "</set>" + "</fin>"; + CPPUNIT_ASSERT_EQUAL(expectedResult, serializer.serialize(fin)); + } + + private: + FullPayloadSerializerCollection serializers; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(MAMFinSerializerTest); |