From 41c771ec02682c2b344263d29f68eb6452c42dbe Mon Sep 17 00:00:00 2001 From: Roger Planas Date: Thu, 27 Oct 2016 13:54:40 +0100 Subject: Sluift: Added Carbons element convertors Sluift was showing payloads coming from a carbon copied message as a series of dom payloads, which was not really usable. With these ElementConvertors, the payload is translated into a much more sensible table Also a minor update to both presence/message events, which now will report the 'to' as well. Seemed handy to have. Test-information: Carbons sent/received messages are formated into a meaningful table now. Change-Id: I5b8943636e09e5377bde76d16970c01df29164d6 diff --git a/Sluift/ElementConvertors/CarbonsReceivedConvertor.cpp b/Sluift/ElementConvertors/CarbonsReceivedConvertor.cpp new file mode 100644 index 0000000..cfd55f3 --- /dev/null +++ b/Sluift/ElementConvertors/CarbonsReceivedConvertor.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include + +#include +#include + +#include + +#include + +using namespace Swift; + +CarbonsReceivedConvertor::CarbonsReceivedConvertor(LuaElementConvertors* convertors) : + GenericLuaElementConvertor("carbons_received"), + convertors(convertors) { +} + +CarbonsReceivedConvertor::~CarbonsReceivedConvertor() { +} + +std::shared_ptr CarbonsReceivedConvertor::doConvertFromLua(lua_State* L) { + std::shared_ptr result = std::make_shared(); + lua_getfield(L, -1, "payload"); + if (!lua_isnil(L, -1)) { + std::shared_ptr payload = std::dynamic_pointer_cast(convertors->convertFromLuaUntyped(L, -1, "payload")); + if (!!payload) { + result->setForwarded(payload); + } + } + lua_pop(L, 1); + return result; +} + +void CarbonsReceivedConvertor::doConvertToLua(lua_State* L, std::shared_ptr payload) { + lua_createtable(L, 0, 0); + if (convertors->convertToLuaUntyped(L, payload->getForwarded()) > 0) { + lua_setfield(L, -2, "payload"); + + } +} + +boost::optional CarbonsReceivedConvertor::getDocumentation() const { + return Documentation( + "CarbonsReceived", + "This table has the following fields:\n\n" + "- `payload`: @{Forwarded}\n" + ); +} diff --git a/Sluift/ElementConvertors/CarbonsReceivedConvertor.h b/Sluift/ElementConvertors/CarbonsReceivedConvertor.h new file mode 100644 index 0000000..f18f699 --- /dev/null +++ b/Sluift/ElementConvertors/CarbonsReceivedConvertor.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include +#include + +#include + +namespace Swift { + class LuaElementConvertors; + + class CarbonsReceivedConvertor : public GenericLuaElementConvertor { + public: + CarbonsReceivedConvertor(LuaElementConvertors* convertors); + virtual ~CarbonsReceivedConvertor(); + + virtual std::shared_ptr doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE; + virtual void doConvertToLua(lua_State*, std::shared_ptr) SWIFTEN_OVERRIDE; + virtual boost::optional getDocumentation() const SWIFTEN_OVERRIDE; + + private: + LuaElementConvertors* convertors; + }; +} + diff --git a/Sluift/ElementConvertors/CarbonsSentConvertor.cpp b/Sluift/ElementConvertors/CarbonsSentConvertor.cpp new file mode 100644 index 0000000..45851d2 --- /dev/null +++ b/Sluift/ElementConvertors/CarbonsSentConvertor.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include + +#include +#include + +#include + +#include + +using namespace Swift; + +CarbonsSentConvertor::CarbonsSentConvertor(LuaElementConvertors* convertors) : + GenericLuaElementConvertor("carbons_sent"), + convertors(convertors) { +} + +CarbonsSentConvertor::~CarbonsSentConvertor() { +} + +std::shared_ptr CarbonsSentConvertor::doConvertFromLua(lua_State* L) { + std::shared_ptr result = std::make_shared(); + lua_getfield(L, -1, "payload"); + if (!lua_isnil(L, -1)) { + std::shared_ptr payload = std::dynamic_pointer_cast(convertors->convertFromLuaUntyped(L, -1, "payload")); + if (!!payload) { + result->setForwarded(payload); + } + } + lua_pop(L, 1); + return result; +} + +void CarbonsSentConvertor::doConvertToLua(lua_State* L, std::shared_ptr payload) { + lua_createtable(L, 0, 0); + if (convertors->convertToLuaUntyped(L, payload->getForwarded()) > 0) { + lua_setfield(L, -2, "payload"); + } +} + +boost::optional CarbonsSentConvertor::getDocumentation() const { + return Documentation( + "CarbonsSent", + "This table has the following fields:\n\n" + "- `payload`: @{Forwarded}\n" + ); +} diff --git a/Sluift/ElementConvertors/CarbonsSentConvertor.h b/Sluift/ElementConvertors/CarbonsSentConvertor.h new file mode 100644 index 0000000..a36a6a8 --- /dev/null +++ b/Sluift/ElementConvertors/CarbonsSentConvertor.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include +#include + +#include + +namespace Swift { + class LuaElementConvertors; + + class CarbonsSentConvertor : public GenericLuaElementConvertor { + public: + CarbonsSentConvertor(LuaElementConvertors* convertors); + virtual ~CarbonsSentConvertor(); + + virtual std::shared_ptr doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE; + virtual void doConvertToLua(lua_State*, std::shared_ptr) SWIFTEN_OVERRIDE; + virtual boost::optional getDocumentation() const SWIFTEN_OVERRIDE; + + private: + LuaElementConvertors* convertors; + }; +} + diff --git a/Sluift/ElementConvertors/SConscript b/Sluift/ElementConvertors/SConscript index b67c65b..0bf8022 100644 --- a/Sluift/ElementConvertors/SConscript +++ b/Sluift/ElementConvertors/SConscript @@ -48,6 +48,8 @@ convertors = [ env.File("MAMResultConvertor.cpp"), env.File("MAMQueryConvertor.cpp"), env.File("MAMFinConvertor.cpp"), + env.File("CarbonsReceivedConvertor.cpp"), + env.File("CarbonsSentConvertor.cpp"), env.File("SubjectConvertor.cpp"), env.File("IsodeIQDelegationConvertor.cpp") ] diff --git a/Sluift/LuaElementConvertors.cpp b/Sluift/LuaElementConvertors.cpp index cfc90d8..38926e9 100644 --- a/Sluift/LuaElementConvertors.cpp +++ b/Sluift/LuaElementConvertors.cpp @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include #include @@ -63,6 +65,8 @@ LuaElementConvertors::LuaElementConvertors() { convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared(this)); + convertors.push_back(std::make_shared(this)); + convertors.push_back(std::make_shared(this)); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); convertors.push_back(std::make_shared()); diff --git a/Sluift/client.cpp b/Sluift/client.cpp index 7d0924f..6376e9d 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -487,6 +486,7 @@ static void pushEvent(lua_State* L, const SluiftClient::Event& event) { Lua::Table result = boost::assign::map_list_of ("type", std::make_shared(std::string("message"))) ("from", std::make_shared(message->getFrom().toString())) + ("to", std::make_shared(message->getTo().toString())) ("body", std::make_shared(message->getBody().get_value_or(""))) ("message_type", std::make_shared(MessageConvertor::convertMessageTypeToString(message->getType()))); Lua::pushValue(L, result); @@ -499,6 +499,7 @@ static void pushEvent(lua_State* L, const SluiftClient::Event& event) { Lua::Table result = boost::assign::map_list_of ("type", std::make_shared(std::string("presence"))) ("from", std::make_shared(presence->getFrom().toString())) + ("to", std::make_shared(presence->getTo().toString())) ("status", std::make_shared(presence->getStatus())) ("presence_type", std::make_shared(PresenceConvertor::convertPresenceTypeToString(presence->getType()))); Lua::pushValue(L, result); -- cgit v0.10.2-6-g49f6