From f033fc54d5cd549822a3462bf0dfcc852e7b4dee Mon Sep 17 00:00:00 2001
From: Roger Planas <roger.planas@isode.com>
Date: Thu, 4 May 2017 14:28:53 +0100
Subject: Sluift: Enable more options to be passed to connect()

There are many connection options that Sluift enables (bosh_url, tls,
etc..) but only host/port were understood by connect(), the rest
were meant to be set by using set_options().

This can be sometimes annoying, so to make things uniform, now
connect() should accept the same options settable in set_options()

Test-information:

Run test to check you can pass values to connect() rather than
set_options() and things worked.

Change-Id: Ic79aa5c6b232ddc2ffcfc3c14aa6f31233242314

diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp
index 4680d4b..e8c43c9 100644
--- a/Sluift/SluiftClient.cpp
+++ b/Sluift/SluiftClient.cpp
@@ -54,15 +54,6 @@ void SluiftClient::connect() {
     client->connect(options);
 }
 
-void SluiftClient::connect(const std::string& host, int port) {
-    rosterReceived = false;
-    blockListReceived = false;
-    options.manualHostname = host;
-    options.manualPort = port;
-    disconnectedError = boost::optional<ClientError>();
-    client->connect(options);
-}
-
 void SluiftClient::setTraceEnabled(bool b) {
     if (b && !tracer) {
         tracer = new ClientXMLTracer(client, options.boshURL.isEmpty()? false: true);
diff --git a/Sluift/client.cpp b/Sluift/client.cpp
index d2b50fa..0dc7014 100644
--- a/Sluift/client.cpp
+++ b/Sluift/client.cpp
@@ -96,20 +96,45 @@ static std::vector< std::shared_ptr<Payload> > getPayloadsFromTable(lua_State* L
     return result;
 }
 
+static void setOptions(lua_State* L, SluiftClient* client) {
+    Lua::checkType(L, 2, LUA_TTABLE);
+    lua_getfield(L, 2, "host");
+    if (!lua_isnil(L, -1)) {
+        client->getOptions().manualHostname = lua_tostring(L, -1);
+    }
+    lua_getfield(L, 2, "port");
+    if (!lua_isnil(L, -1)) {
+        client->getOptions().manualPort = boost::numeric_cast<int>(lua_tointeger(L, -1));
+    }
+    lua_getfield(L, 2, "ack");
+    if (!lua_isnil(L, -1)) {
+        client->getOptions().useAcks = lua_toboolean(L, -1);
+    }
+    lua_getfield(L, 2, "compress");
+    if (!lua_isnil(L, -1)) {
+        client->getOptions().useStreamCompression = lua_toboolean(L, -1);
+    }
+    lua_getfield(L, 2, "tls");
+    if (!lua_isnil(L, -1)) {
+        bool useTLS = lua_toboolean(L, -1);
+        client->getOptions().useTLS = (useTLS ? ClientOptions::UseTLSWhenAvailable : ClientOptions::NeverUseTLS);
+    }
+    lua_getfield(L, 2, "bosh_url");
+    if (!lua_isnil(L, -1)) {
+        client->getOptions().boshURL = URL::fromString(lua_tostring(L, -1));
+    }
+    lua_getfield(L, 2, "allow_plain_without_tls");
+    if (!lua_isnil(L, -1)) {
+        client->getOptions().allowPLAINWithoutTLS = lua_toboolean(L, -1);
+    }
+    lua_pushvalue(L, 1);
+}
+
+
 SLUIFT_LUA_FUNCTION(Client, async_connect) {
     SluiftClient* client = getClient(L);
-
-    std::string host = client->getOptions().manualHostname;
-    int port = client->getOptions().manualPort;
-    if (lua_istable(L, 2)) {
-        if (boost::optional<std::string> hostString = Lua::getStringField(L, 2, "host")) {
-            host = *hostString;
-        }
-        if (boost::optional<int> portInt = Lua::getIntField(L, 2, "port")) {
-            port = *portInt;
-        }
-    }
-    client->connect(host, port);
+    setOptions(L, client);
+    client->connect();
     return 0;
 }
 
@@ -486,37 +511,7 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
         "allow_plain_without_tls  Allow PLAIN authentication without a TLS encrypted connection\n"
 ) {
     SluiftClient* client = getClient(L);
-    Lua::checkType(L, 2, LUA_TTABLE);
-    lua_getfield(L, 2, "host");
-    if (!lua_isnil(L, -1)) {
-        client->getOptions().manualHostname = lua_tostring(L, -1);
-    }
-    lua_getfield(L, 2, "port");
-    if (!lua_isnil(L, -1)) {
-        client->getOptions().manualPort = boost::numeric_cast<int>(lua_tointeger(L, -1));
-    }
-    lua_getfield(L, 2, "ack");
-    if (!lua_isnil(L, -1)) {
-        client->getOptions().useAcks = lua_toboolean(L, -1);
-    }
-    lua_getfield(L, 2, "compress");
-    if (!lua_isnil(L, -1)) {
-        client->getOptions().useStreamCompression = lua_toboolean(L, -1);
-    }
-    lua_getfield(L, 2, "tls");
-    if (!lua_isnil(L, -1)) {
-        bool useTLS = lua_toboolean(L, -1);
-        client->getOptions().useTLS = (useTLS ? ClientOptions::UseTLSWhenAvailable : ClientOptions::NeverUseTLS);
-    }
-    lua_getfield(L, 2, "bosh_url");
-    if (!lua_isnil(L, -1)) {
-        client->getOptions().boshURL = URL::fromString(lua_tostring(L, -1));
-    }
-    lua_getfield(L, 2, "allow_plain_without_tls");
-    if (!lua_isnil(L, -1)) {
-        client->getOptions().allowPLAINWithoutTLS = lua_toboolean(L, -1);
-    }
-    lua_pushvalue(L, 1);
+    setOptions(L, client);
     return 0;
 }
 
-- 
cgit v0.10.2-6-g49f6