From 41c771ec02682c2b344263d29f68eb6452c42dbe Mon Sep 17 00:00:00 2001
From: Roger Planas <roger.planas@isode.com>
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 <Sluift/ElementConvertors/CarbonsReceivedConvertor.h>
+
+#include <memory>
+#include <lua.hpp>
+
+#include <Swiften/Elements/Forwarded.h>
+
+#include <Sluift/LuaElementConvertors.h>
+
+using namespace Swift;
+
+CarbonsReceivedConvertor::CarbonsReceivedConvertor(LuaElementConvertors* convertors) :
+        GenericLuaElementConvertor<CarbonsReceived>("carbons_received"),
+        convertors(convertors) {
+}
+
+CarbonsReceivedConvertor::~CarbonsReceivedConvertor() {
+}
+
+std::shared_ptr<CarbonsReceived> CarbonsReceivedConvertor::doConvertFromLua(lua_State* L) {
+    std::shared_ptr<CarbonsReceived> result = std::make_shared<CarbonsReceived>();
+    lua_getfield(L, -1, "payload");
+    if (!lua_isnil(L, -1)) {
+        std::shared_ptr<Forwarded> payload = std::dynamic_pointer_cast<Forwarded>(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<CarbonsReceived> payload) {
+    lua_createtable(L, 0, 0);
+    if (convertors->convertToLuaUntyped(L, payload->getForwarded()) > 0) {
+        lua_setfield(L, -2, "payload");
+
+    }
+}
+
+boost::optional<LuaElementConvertor::Documentation> 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 <Swiften/Base/Override.h>
+#include <Swiften/Elements/CarbonsReceived.h>
+
+#include <Sluift/GenericLuaElementConvertor.h>
+
+namespace Swift {
+    class LuaElementConvertors;
+
+    class CarbonsReceivedConvertor : public GenericLuaElementConvertor<CarbonsReceived> {
+        public:
+            CarbonsReceivedConvertor(LuaElementConvertors* convertors);
+            virtual ~CarbonsReceivedConvertor();
+
+            virtual std::shared_ptr<CarbonsReceived> doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE;
+            virtual void doConvertToLua(lua_State*, std::shared_ptr<CarbonsReceived>) SWIFTEN_OVERRIDE;
+            virtual boost::optional<Documentation> 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 <Sluift/ElementConvertors/CarbonsSentConvertor.h>
+
+#include <memory>
+#include <lua.hpp>
+
+#include <Swiften/Elements/Forwarded.h>
+
+#include <Sluift/LuaElementConvertors.h>
+
+using namespace Swift;
+
+CarbonsSentConvertor::CarbonsSentConvertor(LuaElementConvertors* convertors) :
+        GenericLuaElementConvertor<CarbonsSent>("carbons_sent"),
+        convertors(convertors) {
+}
+
+CarbonsSentConvertor::~CarbonsSentConvertor() {
+}
+
+std::shared_ptr<CarbonsSent> CarbonsSentConvertor::doConvertFromLua(lua_State* L) {
+    std::shared_ptr<CarbonsSent> result = std::make_shared<CarbonsSent>();
+    lua_getfield(L, -1, "payload");
+    if (!lua_isnil(L, -1)) {
+        std::shared_ptr<Forwarded> payload = std::dynamic_pointer_cast<Forwarded>(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<CarbonsSent> payload) {
+    lua_createtable(L, 0, 0);
+    if (convertors->convertToLuaUntyped(L, payload->getForwarded()) > 0) {
+        lua_setfield(L, -2, "payload");
+    }
+}
+
+boost::optional<LuaElementConvertor::Documentation> 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 <Swiften/Base/Override.h>
+#include <Swiften/Elements/CarbonsSent.h>
+
+#include <Sluift/GenericLuaElementConvertor.h>
+
+namespace Swift {
+    class LuaElementConvertors;
+
+    class CarbonsSentConvertor : public GenericLuaElementConvertor<CarbonsSent> {
+        public:
+            CarbonsSentConvertor(LuaElementConvertors* convertors);
+            virtual ~CarbonsSentConvertor();
+
+            virtual std::shared_ptr<CarbonsSent> doConvertFromLua(lua_State*) SWIFTEN_OVERRIDE;
+            virtual void doConvertToLua(lua_State*, std::shared_ptr<CarbonsSent>) SWIFTEN_OVERRIDE;
+            virtual boost::optional<Documentation> 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 <Swiften/Base/foreach.h>
 
 #include <Sluift/ElementConvertors/BodyConvertor.h>
+#include <Sluift/ElementConvertors/CarbonsReceivedConvertor.h>
+#include <Sluift/ElementConvertors/CarbonsSentConvertor.h>
 #include <Sluift/ElementConvertors/CommandConvertor.h>
 #include <Sluift/ElementConvertors/DOMElementConvertor.h>
 #include <Sluift/ElementConvertors/DefaultElementConvertor.h>
@@ -63,6 +65,8 @@ LuaElementConvertors::LuaElementConvertors() {
     convertors.push_back(std::make_shared<MAMResultConvertor>(this));
     convertors.push_back(std::make_shared<MAMQueryConvertor>(this));
     convertors.push_back(std::make_shared<MAMFinConvertor>(this));
+    convertors.push_back(std::make_shared<CarbonsReceivedConvertor>(this));
+    convertors.push_back(std::make_shared<CarbonsSentConvertor>(this));
     convertors.push_back(std::make_shared<DOMElementConvertor>());
     convertors.push_back(std::make_shared<RawXMLElementConvertor>());
     convertors.push_back(std::make_shared<DefaultElementConvertor>());
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 <Swiften/Base/foreach.h>
 #include <Swiften/Disco/ClientDiscoManager.h>
 #include <Swiften/Elements/DiscoInfo.h>
-#include <Swiften/Elements/MAMQuery.h>
 #include <Swiften/Elements/Message.h>
 #include <Swiften/Elements/Presence.h>
 #include <Swiften/Elements/RawXMLPayload.h>
@@ -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<Lua::Value>(std::string("message")))
                 ("from", std::make_shared<Lua::Value>(message->getFrom().toString()))
+                ("to", std::make_shared<Lua::Value>(message->getTo().toString()))
                 ("body", std::make_shared<Lua::Value>(message->getBody().get_value_or("")))
                 ("message_type", std::make_shared<Lua::Value>(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<Lua::Value>(std::string("presence")))
                 ("from", std::make_shared<Lua::Value>(presence->getFrom().toString()))
+                ("to", std::make_shared<Lua::Value>(presence->getTo().toString()))
                 ("status", std::make_shared<Lua::Value>(presence->getStatus()))
                 ("presence_type", std::make_shared<Lua::Value>(PresenceConvertor::convertPresenceTypeToString(presence->getType())));
             Lua::pushValue(L, result);
-- 
cgit v0.10.2-6-g49f6