From 5b44c6e80b8009961f7029797023ca6715743718 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sun, 27 Feb 2011 09:33:54 +0100
Subject: Added 'get_next_event' sluift command.


diff --git a/Sluift/README b/Sluift/README
index 2d9c2e2..65fb648 100644
--- a/Sluift/README
+++ b/Sluift/README
@@ -1 +1,2 @@
-For example scripts, see Swiften/QA/ScriptedTests
+For example scripts, see the 'login.lua' script and the scripts in
+Swiften/QA/ScriptedTests.
diff --git a/Sluift/SConscript b/Sluift/SConscript
index 1e36b6e..44fabdf 100644
--- a/Sluift/SConscript
+++ b/Sluift/SConscript
@@ -16,7 +16,7 @@ if env["SCONS_STAGE"] == "build" :
 		f = open(source[0].abspath, "r")
 		contents = f.read()
 		f.close()
-		contents = contents.replace("LUA_RELEASE", "\"Sluift XMPP Console\"")
+		contents = contents.replace("LUA_RELEASE", "\"== Sluift XMPP Console ==\"")
 		contents = contents.replace("LUA_COPYRIGHT", "")
 		f = open(target[0].abspath, "w")
 		f.write(contents)
diff --git a/Sluift/login.lua b/Sluift/login.lua
new file mode 100644
index 0000000..52c1521
--- /dev/null
+++ b/Sluift/login.lua
@@ -0,0 +1,24 @@
+--
+-- Copyright (c) 2010 Remko Tronçon
+-- Licensed under the GNU General Public License v3.
+-- See Documentation/Licenses/GPLv3.txt for more information.
+--
+
+-- This script logs into an XMPP server, and sends initial presence
+-- Useful as initialization script for an interactive session ('-i')
+-- 
+-- The following environment variables are used:
+-- * SLUIFT_JID, SWIFT_PASS: JID and password to log in with
+-- * SLUIFT_OPTIONS: Client options to use (e.g. "{compress = false}")
+-- * SLUIFT_DEBUG: Sets whether debugging should be turned on
+
+require "sluift"
+sluift.debug = os.getenv("SLUIFT_DEBUG") or false
+
+print("Connecting " .. os.getenv("SLUIFT_JID") .. " ...")
+c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS"))
+c:set_options(os.getenv("SLUIFT_OPTIONS") or {})
+c:connect()
+c:send_presence("")
+
+print("Connected ...")
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp
index fd7b695..1ce9642 100644
--- a/Sluift/sluift.cpp
+++ b/Sluift/sluift.cpp
@@ -411,6 +411,30 @@ static int sluift_client_set_options(lua_State* L) {
 	return 0;
 }
 
+static void pushEvent(lua_State* L, Stanza::ref event) {
+	if (Message::ref message = boost::dynamic_pointer_cast<Message>(event)) {
+		lua_createtable(L, 0, 3);
+		lua_pushliteral(L, "message");
+		lua_setfield(L, -2, "type");
+		lua_pushstring(L, message->getFrom().toString().c_str());
+		lua_setfield(L, -2, "from");
+		lua_pushstring(L, message->getBody().c_str());
+		lua_setfield(L, -2, "body");
+	}
+	else if (Presence::ref presence = boost::dynamic_pointer_cast<Presence>(event)) {
+		lua_createtable(L, 0, 3);
+		lua_pushliteral(L, "presence");
+		lua_setfield(L, -2, "type");
+		lua_pushstring(L, presence->getFrom().toString().c_str());
+		lua_setfield(L, -2, "from");
+		lua_pushstring(L, presence->getStatus().c_str());
+		lua_setfield(L, -2, "status");
+	}
+	else {
+		lua_pushnil(L);
+	}
+}
+
 static int sluift_client_for_event(lua_State *L) {
 	try {
 		SluiftClient* client = getClient(L);
@@ -430,28 +454,7 @@ static int sluift_client_for_event(lua_State *L) {
 			else {
 				// Push the function and event on the stack
 				lua_pushvalue(L, 2);
-				if (Message::ref message = boost::dynamic_pointer_cast<Message>(event)) {
-					lua_createtable(L, 0, 3);
-					lua_pushliteral(L, "message");
-					lua_setfield(L, -2, "type");
-					lua_pushstring(L, message->getFrom().toString().c_str());
-					lua_setfield(L, -2, "from");
-					lua_pushstring(L, message->getBody().c_str());
-					lua_setfield(L, -2, "body");
-				}
-				else if (Presence::ref presence = boost::dynamic_pointer_cast<Presence>(event)) {
-					lua_createtable(L, 0, 3);
-					lua_pushliteral(L, "presence");
-					lua_setfield(L, -2, "type");
-					lua_pushstring(L, presence->getFrom().toString().c_str());
-					lua_setfield(L, -2, "from");
-					lua_pushstring(L, presence->getStatus().c_str());
-					lua_setfield(L, -2, "status");
-				}
-				else {
-					assert(false);
-					lua_pushnil(L);
-				}
+				pushEvent(L, event);
 				int oldTop = lua_gettop(L) - 2;
 				lua_call(L, 1, LUA_MULTRET);
 				int returnValues = lua_gettop(L) - oldTop;
@@ -467,6 +470,21 @@ static int sluift_client_for_event(lua_State *L) {
 	}
 }
 
+static int sluift_client_get_next_event(lua_State *L) {
+	try {
+		SluiftClient* client = getClient(L);
+		int timeout = -1;
+		if (lua_type(L, 2) != LUA_TNONE) {
+			timeout = lua_tonumber(L, 2);
+		}
+		pushEvent(L, client->getNextEvent(timeout));
+		return 1;
+	}
+	catch (const SluiftException& e) {
+		return luaL_error(L, e.getReason().c_str());
+	}
+}
+
 static int sluift_client_gc (lua_State *L) {
 	SluiftClient* client = getClient(L);
 	delete client;
@@ -487,6 +505,7 @@ static const luaL_reg sluift_client_functions[] = {
 	{"get_version", sluift_client_get_version},
 	{"set_options", sluift_client_set_options},
 	{"for_event", sluift_client_for_event},
+	{"get_next_event", sluift_client_get_next_event},
 	{"__gc", sluift_client_gc},
 	{NULL, NULL}
 };
-- 
cgit v0.10.2-6-g49f6