From 24baaf8ad66354c17a6d6ba4438e95d6798564a8 Mon Sep 17 00:00:00 2001
From: Thanos Doukoudakis <thanos.doukoudakis@isode.com>
Date: Thu, 11 May 2017 16:41:20 +0100
Subject: Add pointer checks in some CoreClient members.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds some checks in the CoreClient class to avoid accessing
stanza and session channels to send data when they are not available.
The Sluift lua wrapper functions will throw an exception in these cases.

Test-Information
All unit test pass.
Timlx:
Test Suite ‘basic’: All tests Pass.
Test Suite ‘fmuc’: All tests except FmucChain Pass (Not related with the
changes)

Change-Id: I3d5894b3cfdafd0ea28c0fb33b6db8588f2a5c8f

diff --git a/Sluift/client.cpp b/Sluift/client.cpp
index 0dc7014..75f675d 100644
--- a/Sluift/client.cpp
+++ b/Sluift/client.cpp
@@ -358,6 +358,9 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
     }
     message->addPayloads(payloads.begin(), payloads.end());
     message->setType(type);
+    if (!getClient(L)->getClient()->isAvailable()) {
+        throw Lua::Exception("Trying to send message while client is offline.");
+    }
     getClient(L)->getClient()->sendMessage(message);
     return 0;
 }
@@ -403,7 +406,9 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
         std::vector< std::shared_ptr<Payload> > payloads = getPayloadsFromTable(L, index);
         presence->addPayloads(payloads.begin(), payloads.end());
     }
-
+    if (!getClient(L)->getClient()->getPresenceSender()->isAvailable()) {
+        throw Lua::Exception("Trying to send presence while client is offline.");
+    }
     getClient(L)->getClient()->getPresenceSender()->sendPresence(presence);
     lua_pushvalue(L, 1);
     return 0;
@@ -489,7 +494,9 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
         ""
 ) {
     Sluift::globals.eventLoop.runOnce();
-
+    if (!getClient(L)->getClient()->isAvailable()) {
+        throw Lua::Exception("Trying to send data while client is offline.");
+    }
     getClient(L)->getClient()->sendData(std::string(Lua::checkString(L, 2)));
     lua_pushvalue(L, 1);
     return 0;
diff --git a/Swiften/Client/CoreClient.cpp b/Swiften/Client/CoreClient.cpp
index 3c7902e..1de1d61 100644
--- a/Swiften/Client/CoreClient.cpp
+++ b/Swiften/Client/CoreClient.cpp
@@ -390,6 +390,10 @@ void CoreClient::sendPresence(std::shared_ptr<Presence> presence) {
 }
 
 void CoreClient::sendData(const std::string& data) {
+    if (!sessionStream_) {
+        SWIFT_LOG(warning) << "Client: Trying to send data while disconnected." << std::endl;
+        return;
+    }
     sessionStream_->writeData(data);
 }
 
-- 
cgit v0.10.2-6-g49f6