diff options
Diffstat (limited to 'Sluift/sluift.cpp')
-rw-r--r-- | Sluift/sluift.cpp | 612 |
1 files changed, 257 insertions, 355 deletions
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 0a653bb..397d7f3 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Remko Tronçon + * Copyright (c) 2011-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -17,9 +17,9 @@ #include <Swiften/Swiften.h> #include "Watchdog.h" -#include "SluiftException.h" #include "ResponseSink.h" #include "Lua/Value.h" +#include "ClientHelpers.h" using namespace Swift; @@ -42,7 +42,7 @@ static BoostNetworkFactories networkFactories(&eventLoop); class SluiftClient { public: - SluiftClient(const JID& jid, const std::string& password) : tracer(NULL) { + SluiftClient(const JID& jid, const std::string& password, lua_State* L) : L(L), tracer(NULL) { client = new Client(jid, password, &networkFactories); client->setAlwaysTrustCertificates(); client->onDisconnected.connect(boost::bind(&SluiftClient::handleDisconnected, this, _1)); @@ -85,7 +85,7 @@ class SluiftClient { } if (watchdog.getTimedOut()) { client->disconnect(); - throw SluiftException("Timeout while connecting"); + luaL_error(L, "Timeout while connecting"); } } @@ -183,11 +183,12 @@ class SluiftClient { void handleDisconnected(const boost::optional<ClientError>& error) { if (error) { - throw SluiftException(*error); + luaL_error(L, getClientErrorString(*error).c_str()); } } private: + lua_State* L; Client* client; ClientOptions options; ClientXMLTracer* tracer; @@ -205,44 +206,29 @@ static inline SluiftClient* getClient(lua_State* L) { } static int sluift_client_connect(lua_State *L) { - try { - SluiftClient* client = getClient(L); - std::string host; - if (lua_type(L, 2) != LUA_TNONE) { - host = luaL_checkstring(L, 2); - } - if (host.empty()) { - client->connect(); - } - else { - client->connect(host); - } - client->waitConnected(); - return 1; + SluiftClient* client = getClient(L); + std::string host; + if (lua_type(L, 2) != LUA_TNONE) { + host = luaL_checkstring(L, 2); } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + if (host.empty()) { + client->connect(); } + else { + client->connect(host); + } + client->waitConnected(); + return 1; } static int sluift_client_async_connect(lua_State *L) { - try { - getClient(L)->connect(); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + getClient(L)->connect(); + return 1; } static int sluift_client_wait_connected(lua_State *L) { - try { - getClient(L)->waitConnected(); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + getClient(L)->waitConnected(); + return 1; } static int sluift_client_is_connected(lua_State *L) { @@ -251,221 +237,176 @@ static int sluift_client_is_connected(lua_State *L) { } static int sluift_client_disconnect(lua_State *L) { - try { - getClient(L)->disconnect(); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + getClient(L)->disconnect(); + return 1; } static int sluift_client_set_version(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - SluiftClient* client = getClient(L); - luaL_checktype(L, 2, LUA_TTABLE); - lua_getfield(L, 2, "name"); - const char* rawName = lua_tostring(L, -1); - lua_getfield(L, 2, "version"); - const char* rawVersion = lua_tostring(L, -1); - lua_getfield(L, 2, "os"); - const char* rawOS = lua_tostring(L, -1); - client->setSoftwareVersion(rawName ? rawName : "", rawVersion ? rawVersion : "", rawOS ? rawOS : ""); - lua_pop(L, 3); - lua_pushvalue(L, 1); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + SluiftClient* client = getClient(L); + luaL_checktype(L, 2, LUA_TTABLE); + lua_getfield(L, 2, "name"); + const char* rawName = lua_tostring(L, -1); + lua_getfield(L, 2, "version"); + const char* rawVersion = lua_tostring(L, -1); + lua_getfield(L, 2, "os"); + const char* rawOS = lua_tostring(L, -1); + client->setSoftwareVersion(rawName ? rawName : "", rawVersion ? rawVersion : "", rawOS ? rawOS : ""); + lua_pop(L, 3); + lua_pushvalue(L, 1); + return 1; } static int sluift_client_get_contacts(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - SluiftClient* client = getClient(L); - Lua::Table contactsTable; - foreach(const XMPPRosterItem& item, client->getRoster()) { - std::string subscription; - switch(item.getSubscription()) { - case RosterItemPayload::None: subscription = "none"; break; - case RosterItemPayload::To: subscription = "to"; break; - case RosterItemPayload::From: subscription = "from"; break; - case RosterItemPayload::Both: subscription = "both"; break; - case RosterItemPayload::Remove: subscription = "remove"; break; - } - Lua::Value groups(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end())); - Lua::Table itemTable = boost::assign::map_list_of - ("jid", boost::make_shared<Lua::Value>(item.getJID().toString())) - ("name", boost::make_shared<Lua::Value>(item.getName())) - ("subscription", boost::make_shared<Lua::Value>(subscription)) - ("groups", boost::make_shared<Lua::Value>(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end()))); - contactsTable[item.getJID().toString()] = boost::make_shared<Lua::Value>(itemTable); - } - pushValue(L, contactsTable); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + SluiftClient* client = getClient(L); + Lua::Table contactsTable; + foreach(const XMPPRosterItem& item, client->getRoster()) { + std::string subscription; + switch(item.getSubscription()) { + case RosterItemPayload::None: subscription = "none"; break; + case RosterItemPayload::To: subscription = "to"; break; + case RosterItemPayload::From: subscription = "from"; break; + case RosterItemPayload::Both: subscription = "both"; break; + case RosterItemPayload::Remove: subscription = "remove"; break; + } + Lua::Value groups(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end())); + Lua::Table itemTable = boost::assign::map_list_of + ("jid", boost::make_shared<Lua::Value>(item.getJID().toString())) + ("name", boost::make_shared<Lua::Value>(item.getName())) + ("subscription", boost::make_shared<Lua::Value>(subscription)) + ("groups", boost::make_shared<Lua::Value>(std::vector<Lua::Value>(item.getGroups().begin(), item.getGroups().end()))); + contactsTable[item.getJID().toString()] = boost::make_shared<Lua::Value>(itemTable); + } + pushValue(L, contactsTable); + return 1; } static int sluift_client_get_version(lua_State *L) { - try { - SluiftClient* client = getClient(L); - int timeout = -1; - if (lua_type(L, 3) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(luaL_checknumber(L, 3)); - } + SluiftClient* client = getClient(L); + int timeout = -1; + if (lua_type(L, 3) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(luaL_checknumber(L, 3)); + } - ResponseSink<SoftwareVersion> sink; - GetSoftwareVersionRequest::ref request = GetSoftwareVersionRequest::create(std::string(luaL_checkstring(L, 2)), client->getClient()->getIQRouter()); - boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); - request->send(); + ResponseSink<SoftwareVersion> sink; + GetSoftwareVersionRequest::ref request = GetSoftwareVersionRequest::create(std::string(luaL_checkstring(L, 2)), client->getClient()->getIQRouter()); + boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); + request->send(); - Watchdog watchdog(timeout, networkFactories.getTimerFactory()); - while (!watchdog.getTimedOut() && !sink.hasResponse()) { - eventLoop.runUntilEvents(); - } + Watchdog watchdog(timeout, networkFactories.getTimerFactory()); + while (!watchdog.getTimedOut() && !sink.hasResponse()) { + eventLoop.runUntilEvents(); + } - ErrorPayload::ref error = sink.getResponseError(); - if (error || watchdog.getTimedOut()) { - lua_pushnil(L); - if (watchdog.getTimedOut()) { - lua_pushstring(L, "Timeout"); - } - else if (error->getCondition() == ErrorPayload::RemoteServerNotFound) { - lua_pushstring(L, "Remote server not found"); - } - // TODO - else { - lua_pushstring(L, "Error"); - } - return 2; + ErrorPayload::ref error = sink.getResponseError(); + if (error || watchdog.getTimedOut()) { + lua_pushnil(L); + if (watchdog.getTimedOut()) { + lua_pushstring(L, "Timeout"); } - else if (SoftwareVersion::ref version = sink.getResponsePayload()) { - Lua::Table result = boost::assign::map_list_of - ("name", boost::make_shared<Lua::Value>(version->getName())) - ("version", boost::make_shared<Lua::Value>(version->getVersion())) - ("os", boost::make_shared<Lua::Value>(version->getOS())); - Lua::pushValue(L, result); + else if (error->getCondition() == ErrorPayload::RemoteServerNotFound) { + lua_pushstring(L, "Remote server not found"); } + // TODO else { - lua_pushnil(L); + lua_pushstring(L, "Error"); } - return 1; + return 2; + } + else if (SoftwareVersion::ref version = sink.getResponsePayload()) { + Lua::Table result = boost::assign::map_list_of + ("name", boost::make_shared<Lua::Value>(version->getName())) + ("version", boost::make_shared<Lua::Value>(version->getVersion())) + ("os", boost::make_shared<Lua::Value>(version->getOS())); + Lua::pushValue(L, result); } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + else { + lua_pushnil(L); } + return 1; } static int sluift_client_send_message(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - getClient(L)->sendMessage(std::string(luaL_checkstring(L, 2)), luaL_checkstring(L, 3)); - lua_pushvalue(L, 1); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + getClient(L)->sendMessage(std::string(luaL_checkstring(L, 2)), luaL_checkstring(L, 3)); + lua_pushvalue(L, 1); + return 1; } static int sluift_client_send_presence(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - getClient(L)->sendPresence(std::string(luaL_checkstring(L, 2))); - lua_pushvalue(L, 1); - return 0; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + getClient(L)->sendPresence(std::string(luaL_checkstring(L, 2))); + lua_pushvalue(L, 1); + return 0; } static int sluift_client_get(lua_State *L) { - try { - SluiftClient* client = getClient(L); - JID jid; - std::string data; - int timeout = -1; - if (lua_type(L, 3) == LUA_TSTRING) { - jid = JID(std::string(luaL_checkstring(L, 2))); - data = std::string(luaL_checkstring(L, 3)); - if (lua_type(L, 4) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(luaL_checknumber(L, 4)); - } - } - else { - data = std::string(luaL_checkstring(L, 2)); - if (lua_type(L, 3) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(luaL_checknumber(L, 3)); - } - } - boost::optional<std::string> result = client->sendQuery(jid, IQ::Get, data, timeout); - if (result) { - lua_pushstring(L, result->c_str()); + SluiftClient* client = getClient(L); + JID jid; + std::string data; + int timeout = -1; + if (lua_type(L, 3) == LUA_TSTRING) { + jid = JID(std::string(luaL_checkstring(L, 2))); + data = std::string(luaL_checkstring(L, 3)); + if (lua_type(L, 4) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(luaL_checknumber(L, 4)); } - else { - lua_pushnil(L); + } + else { + data = std::string(luaL_checkstring(L, 2)); + if (lua_type(L, 3) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(luaL_checknumber(L, 3)); } - return 1; } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + boost::optional<std::string> result = client->sendQuery(jid, IQ::Get, data, timeout); + if (result) { + lua_pushstring(L, result->c_str()); } + else { + lua_pushnil(L); + } + return 1; } static int sluift_client_set(lua_State *L) { - try { - SluiftClient* client = getClient(L); - JID jid; - std::string data; - int timeout = -1; - if (lua_type(L, 3) == LUA_TSTRING) { - jid = JID(std::string(luaL_checkstring(L, 2))); - data = std::string(luaL_checkstring(L, 3)); - if (lua_type(L, 4) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(luaL_checknumber(L, 4)); - } - } - else { - data = std::string(luaL_checkstring(L, 2)); - if (lua_type(L, 3) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(luaL_checknumber(L, 3)); - } - } - boost::optional<std::string> result = client->sendQuery(jid, IQ::Set, data, timeout); - if (result) { - lua_pushstring(L, result->c_str()); + SluiftClient* client = getClient(L); + JID jid; + std::string data; + int timeout = -1; + if (lua_type(L, 3) == LUA_TSTRING) { + jid = JID(std::string(luaL_checkstring(L, 2))); + data = std::string(luaL_checkstring(L, 3)); + if (lua_type(L, 4) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(luaL_checknumber(L, 4)); } - else { - lua_pushnil(L); + } + else { + data = std::string(luaL_checkstring(L, 2)); + if (lua_type(L, 3) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(luaL_checknumber(L, 3)); } - return 1; } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + boost::optional<std::string> result = client->sendQuery(jid, IQ::Set, data, timeout); + if (result) { + lua_pushstring(L, result->c_str()); + } + else { + lua_pushnil(L); } + return 1; } static int sluift_client_send(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - getClient(L)->getClient()->sendData(std::string(luaL_checkstring(L, 2))); - lua_pushvalue(L, 1); - return 0; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + getClient(L)->getClient()->sendData(std::string(luaL_checkstring(L, 2))); + lua_pushvalue(L, 1); + return 0; } static int sluift_client_set_options(lua_State* L) { @@ -509,131 +450,91 @@ static void pushEvent(lua_State* L, Stanza::ref event) { } static int sluift_client_for_event(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - SluiftClient* client = getClient(L); - luaL_checktype(L, 2, LUA_TFUNCTION); - int timeout = -1; - if (lua_type(L, 3) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(lua_tonumber(L, 3)); - } + SluiftClient* client = getClient(L); + luaL_checktype(L, 2, LUA_TFUNCTION); + int timeout = -1; + if (lua_type(L, 3) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(lua_tonumber(L, 3)); + } - while (true) { - Stanza::ref event = client->getNextEvent(timeout); - if (!event) { - // We got a timeout - lua_pushnil(L); - return 1; - } - else { - // Push the function and event on the stack - lua_pushvalue(L, 2); - pushEvent(L, event); - int oldTop = lua_gettop(L) - 2; - lua_call(L, 1, LUA_MULTRET); - int returnValues = lua_gettop(L) - oldTop; - if (returnValues > 0) { - lua_remove(L, -1 - returnValues); - return returnValues; - } + while (true) { + Stanza::ref event = client->getNextEvent(timeout); + if (!event) { + // We got a timeout + lua_pushnil(L); + return 1; + } + else { + // Push the function and event on the stack + lua_pushvalue(L, 2); + pushEvent(L, event); + int oldTop = lua_gettop(L) - 2; + lua_call(L, 1, LUA_MULTRET); + int returnValues = lua_gettop(L) - oldTop; + if (returnValues > 0) { + lua_remove(L, -1 - returnValues); + return returnValues; } } } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } } static int sluift_client_get_next_event(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - SluiftClient* client = getClient(L); - int timeout = -1; - if (lua_type(L, 2) != LUA_TNONE) { - timeout = boost::numeric_cast<int>(lua_tonumber(L, 2)); - } - pushEvent(L, client->getNextEvent(timeout)); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + SluiftClient* client = getClient(L); + int timeout = -1; + if (lua_type(L, 2) != LUA_TNONE) { + timeout = boost::numeric_cast<int>(lua_tonumber(L, 2)); } + pushEvent(L, client->getNextEvent(timeout)); + return 1; } static int sluift_client_add_contact(lua_State* L) { - try { - eventLoop.runOnce(); - SluiftClient* client = getClient(L); - RosterItemPayload item; - if (lua_type(L, 2) == LUA_TTABLE) { - lua_getfield(L, 2, "jid"); - const char* rawJID = lua_tostring(L, -1); - if (rawJID) { - item.setJID(std::string(rawJID)); - } - lua_getfield(L, 2, "name"); - const char* rawName = lua_tostring(L, -1); - if (rawName) { - item.setName(rawName); - } - lua_getfield(L, 2, "groups"); - if (!lua_isnil(L, -1)) { - if (lua_type(L, -1) == LUA_TTABLE) { - for (size_t i = 1; i <= lua_objlen(L, -1); ++i) { - lua_rawgeti(L, -1, boost::numeric_cast<int>(i)); - const char* rawGroup = lua_tostring(L, -1); - if (rawGroup) { - item.addGroup(rawGroup); - } - lua_pop(L, 1); + eventLoop.runOnce(); + SluiftClient* client = getClient(L); + RosterItemPayload item; + if (lua_type(L, 2) == LUA_TTABLE) { + lua_getfield(L, 2, "jid"); + const char* rawJID = lua_tostring(L, -1); + if (rawJID) { + item.setJID(std::string(rawJID)); + } + lua_getfield(L, 2, "name"); + const char* rawName = lua_tostring(L, -1); + if (rawName) { + item.setName(rawName); + } + lua_getfield(L, 2, "groups"); + if (!lua_isnil(L, -1)) { + if (lua_type(L, -1) == LUA_TTABLE) { + for (size_t i = 1; i <= lua_objlen(L, -1); ++i) { + lua_rawgeti(L, -1, boost::numeric_cast<int>(i)); + const char* rawGroup = lua_tostring(L, -1); + if (rawGroup) { + item.addGroup(rawGroup); } - } - else { - return luaL_error(L, "Groups should be a table"); + lua_pop(L, 1); } } - } - else { - item.setJID(luaL_checkstring(L, 2)); - } - - client->getRoster(); - if (!client->getClient()->getRoster()->containsJID(item.getJID())) { - RosterPayload::ref roster = boost::make_shared<RosterPayload>(); - roster->addItem(item); - - ResponseSink<RosterPayload> sink; - SetRosterRequest::ref request = SetRosterRequest::create(roster, client->getClient()->getIQRouter()); - boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); - request->send(); - while (!sink.hasResponse()) { - eventLoop.runUntilEvents(); - } - if (sink.getResponseError()) { - lua_pushboolean(L, false); - return 1; + else { + return luaL_error(L, "Groups should be a table"); } } - client->getClient()->getSubscriptionManager()->requestSubscription(item.getJID()); - lua_pushboolean(L, true); - return 1; } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + else { + item.setJID(luaL_checkstring(L, 2)); } -} - -static int sluift_client_remove_contact(lua_State* L) { - try { - eventLoop.runOnce(); - SluiftClient* client = getClient(L); - JID jid(luaL_checkstring(L, 2)); + client->getRoster(); + if (!client->getClient()->getRoster()->containsJID(item.getJID())) { RosterPayload::ref roster = boost::make_shared<RosterPayload>(); - roster->addItem(RosterItemPayload(JID(luaL_checkstring(L, 2)), "", RosterItemPayload::Remove)); + roster->addItem(item); + ResponseSink<RosterPayload> sink; SetRosterRequest::ref request = SetRosterRequest::create(roster, client->getClient()->getIQRouter()); boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); @@ -641,38 +542,48 @@ static int sluift_client_remove_contact(lua_State* L) { while (!sink.hasResponse()) { eventLoop.runUntilEvents(); } - lua_pushboolean(L, !sink.getResponseError()); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + if (sink.getResponseError()) { + lua_pushboolean(L, false); + return 1; + } } + client->getClient()->getSubscriptionManager()->requestSubscription(item.getJID()); + lua_pushboolean(L, true); + return 1; +} + +static int sluift_client_remove_contact(lua_State* L) { + eventLoop.runOnce(); + SluiftClient* client = getClient(L); + JID jid(luaL_checkstring(L, 2)); + + RosterPayload::ref roster = boost::make_shared<RosterPayload>(); + roster->addItem(RosterItemPayload(JID(luaL_checkstring(L, 2)), "", RosterItemPayload::Remove)); + ResponseSink<RosterPayload> sink; + SetRosterRequest::ref request = SetRosterRequest::create(roster, client->getClient()->getIQRouter()); + boost::signals::scoped_connection c = request->onResponse.connect(boost::ref(sink)); + request->send(); + while (!sink.hasResponse()) { + eventLoop.runUntilEvents(); + } + lua_pushboolean(L, !sink.getResponseError()); + return 1; } static int sluift_client_confirm_subscription(lua_State* L) { - try { - eventLoop.runOnce(); - SluiftClient* client = getClient(L); - JID jid(luaL_checkstring(L, 2)); - client->getClient()->getSubscriptionManager()->confirmSubscription(jid); - return 0; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + eventLoop.runOnce(); + SluiftClient* client = getClient(L); + JID jid(luaL_checkstring(L, 2)); + client->getClient()->getSubscriptionManager()->confirmSubscription(jid); + return 0; } static int sluift_client_cancel_subscription(lua_State* L) { - try { - eventLoop.runOnce(); - SluiftClient* client = getClient(L); - JID jid(luaL_checkstring(L, 2)); - client->getClient()->getSubscriptionManager()->cancelSubscription(jid); - return 0; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + eventLoop.runOnce(); + SluiftClient* client = getClient(L); + JID jid(luaL_checkstring(L, 2)); + client->getClient()->getSubscriptionManager()->cancelSubscription(jid); + return 0; } static int sluift_client_gc (lua_State *L) { @@ -711,20 +622,16 @@ static const luaL_reg sluift_client_functions[] = { ******************************************************************************/ static int sluift_new_client(lua_State *L) { - try { - JID jid(std::string(luaL_checkstring(L, 1))); - std::string password(luaL_checkstring(L, 2)); + luaL_checkstring(L, 1); + JID jid(std::string(luaL_checkstring(L, 1))); + std::string password(luaL_checkstring(L, 2)); - SluiftClient** client = reinterpret_cast<SluiftClient**>(lua_newuserdata(L, sizeof(SluiftClient*))); - luaL_getmetatable(L, SLUIFT_CLIENT); - lua_setmetatable(L, -2); + SluiftClient** client = reinterpret_cast<SluiftClient**>(lua_newuserdata(L, sizeof(SluiftClient*))); + luaL_getmetatable(L, SLUIFT_CLIENT); + lua_setmetatable(L, -2); - *client = new SluiftClient(jid, password); - return 1; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); - } + *client = new SluiftClient(jid, password, L); + return 1; } static int sluift_jid_to_bare(lua_State *L) { @@ -752,20 +659,15 @@ static int sluift_jid_resource(lua_State *L) { } static int sluift_sleep(lua_State *L) { - try { - eventLoop.runOnce(); + eventLoop.runOnce(); - int timeout = boost::numeric_cast<int>(luaL_checknumber(L, 1)); - Watchdog watchdog(timeout, networkFactories.getTimerFactory()); - while (!watchdog.getTimedOut()) { - Swift::sleep(boost::numeric_cast<unsigned int>(std::min(100, timeout))); - eventLoop.runOnce(); - } - return 0; - } - catch (const SluiftException& e) { - return luaL_error(L, e.getReason().c_str()); + int timeout = boost::numeric_cast<int>(luaL_checknumber(L, 1)); + Watchdog watchdog(timeout, networkFactories.getTimerFactory()); + while (!watchdog.getTimedOut()) { + Swift::sleep(boost::numeric_cast<unsigned int>(std::min(100, timeout))); + eventLoop.runOnce(); } + return 0; } static int sluift_index(lua_State *L) { |