summaryrefslogtreecommitdiffstats
blob: 95e7101afdc57e43fb7006213378da61446d5563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (c) 2011-2013 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Sluift/sluift.h>

#include <lua.hpp>

#include <string>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/assign/list_of.hpp>

#include "Watchdog.h"
#include <Sluift/Lua/Check.h>
#include <Sluift/SluiftClient.h>
#include <Sluift/globals.h>
#include <Sluift/Lua/Exception.h>
#include <Sluift/Lua/FunctionRegistration.h>
#include <Swiften/Base/sleep.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Base/IDGenerator.h>
#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>
#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h>
#include <Swiften/Serializer/PayloadSerializer.h>
#include <Sluift/Lua/Debug.h>
#include <Swiften/StringCodecs/Base64.h>
#include <Swiften/StringCodecs/Hexify.h>
#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>

using namespace Swift;

namespace Swift {
	namespace Sluift {
		SluiftGlobals globals;
	}
}

extern "C" const char boot_lua[];

/*******************************************************************************
 * Module functions
 ******************************************************************************/

SLUIFT_LUA_FUNCTION(Sluift, new_client) {
	Lua::checkString(L, 1);
	JID jid(std::string(Lua::checkString(L, 1)));
	std::string password(Lua::checkString(L, 2));

	SluiftClient** client = reinterpret_cast<SluiftClient**>(lua_newuserdata(L, sizeof(SluiftClient*)));
	luaL_getmetatable(L, Lua::FunctionRegistry::getMetaTableNameForType("Client").c_str());
	lua_setmetatable(L, -2);

	*client = new SluiftClient(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop, &Sluift::globals);
	return 1;
}

SLUIFT_LUA_FUNCTION(Sluift, sha1) {
	static boost::shared_ptr<CryptoProvider> crypto(PlatformCryptoProvider::create());
	if (!lua_isstring(L, 1)) {
		throw Lua::Exception("Expected string");
	}
	size_t len;
	const char* data = lua_tolstring(L, 1, &len);
	ByteArray result = crypto->getSHA1Hash(createByteArray(data, len));
	lua_pushlstring(L, reinterpret_cast<char*>(vecptr(result)), result.size());
	return 1;
}

SLUIFT_LUA_FUNCTION(Sluift, sleep) {
	Sluift::globals.eventLoop.runOnce();
	int timeout = Lua::checkIntNumber(L, 1);
	Watchdog watchdog(timeout, Sluift::globals.networkFactories.getTimerFactory());
	while (!watchdog.getTimedOut()) {
		Swift::sleep(boost::numeric_cast<unsigned int>(std::min(100, timeout)));
		Sluift::globals.eventLoop.runOnce();
	}
	return 0;
}

SLUIFT_LUA_FUNCTION(Sluift, new_uuid) {
	lua_pushstring(L, IDGenerator().generateID().c_str());
	return 1;
}

static int sluift_index(lua_State* L) {
	try {
		std::string key(Lua::checkString(L, 2));
		if (key == "debug") {
			lua_pushboolean(L, Sluift::globals.debug);
			return 1;
		}
		else if (key == "timeout") {
			lua_pushnumber(L, Sluift::globals.timeout);
			return 1;
		}
		throw Lua::Exception("Unknown property");
	}
	catch (const std::exception& e) {
		return luaL_error(L, e.what());
	}
}


static int sluift_newindex(lua_State* L) {
	try {
		std::string key(Lua::checkString(L, 2));
		if (key == "debug") {
			Sluift::globals.debug = lua_toboolean(L, 3);
		}
		else if (key == "timeout") {
			Sluift::globals.timeout = Lua::checkIntNumber(L, 3);
		}
		return 0;
	}
	catch (const std::exception& e) {
		return luaL_error(L, e.what());
	}
}

SLUIFT_LUA_FUNCTION(Sluift, from_xml) {
	PayloadsParserTester parser;
	if (!parser.parse(Lua::checkString(L, 1))) {
		throw Lua::Exception("Error in XML");
	}
	return Sluift::globals.elementConvertor.convertToLua(L, parser.getPayload());
}

SLUIFT_LUA_FUNCTION(Sluift, to_xml) {
	static FullPayloadSerializerCollection serializers;
	boost::shared_ptr<Payload> payload = Sluift::globals.elementConvertor.convertFromLua(L, 1);
	if (!payload) {
		throw Lua::Exception("Unrecognized XML");
	}
	PayloadSerializer* serializer = serializers.getPayloadSerializer(payload);
	if (!payload) {
		throw Lua::Exception("Unrecognized XML");
	}
	lua_pushstring(L, serializer->serialize(payload).c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(Sluift, hexify) {
	if (!lua_isstring(L, 1)) {
		throw Lua::Exception("Expected string");
	}
	size_t len;
	const char* data = lua_tolstring(L, 1, &len);
	lua_pushstring(L, Hexify::hexify(createByteArray(data, len)).c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(Sluift, unhexify) {
	if (!lua_isstring(L, 1)) {
		throw Lua::Exception("Expected string");
	}
	ByteArray result = Hexify::unhexify(lua_tostring(L, 1));
	lua_pushlstring(L, reinterpret_cast<char*>(vecptr(result)), result.size());
	return 1;
}


/*******************************************************************************
 * JID Functions
 ******************************************************************************/

SLUIFT_LUA_FUNCTION(JID, to_bare) {
	JID jid(std::string(Lua::checkString(L, 1)));
	lua_pushstring(L, jid.toBare().toString().c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(JID, node) {
	JID jid(std::string(Lua::checkString(L, 1)));
	lua_pushstring(L, jid.getNode().c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(JID, domain) {
	JID jid(std::string(Lua::checkString(L, 1)));
	lua_pushstring(L, jid.getDomain().c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(JID, resource) {
	JID jid(std::string(Lua::checkString(L, 1)));
	lua_pushstring(L, jid.getResource().c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(JID, escape_node) {
	lua_pushstring(L, JID::getEscapedNode(Lua::checkString(L, 1)).c_str());
	return 1;
}

/*******************************************************************************
 * Base64 Functions
 ******************************************************************************/

SLUIFT_LUA_FUNCTION(Base64, encode) {
	if (!lua_isstring(L, 1)) {
		throw Lua::Exception("Expected string");
	}
	size_t len;
	const char* data = lua_tolstring(L, 1, &len);
	lua_pushstring(L, Base64::encode(createByteArray(data, len)).c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(Base64, decode) {
	if (!lua_isstring(L, 1)) {
		throw Lua::Exception("Expected string");
	}
	ByteArray result = Base64::decode(lua_tostring(L, 1));
	lua_pushlstring(L, reinterpret_cast<char*>(vecptr(result)), result.size());
	return 1;
}

/*******************************************************************************
 * IDN Functions
 ******************************************************************************/

SLUIFT_LUA_FUNCTION(IDN, encode) {
	IDNConverter* converter = Sluift::globals.networkFactories.getIDNConverter();
	lua_pushstring(L, converter->getIDNAEncoded(Lua::checkString(L, 1)).c_str());
	return 1;
}

SLUIFT_LUA_FUNCTION(IDN, stringprep) {
	IDNConverter* converter = Sluift::globals.networkFactories.getIDNConverter();
	IDNConverter::StringPrepProfile profile;
	std::string profileString = Lua::checkString(L, 2);
	if (profileString == "nameprep") {
		profile = IDNConverter::NamePrep;
	}
	else if (profileString == "xmpp_nodeprep") {
		profile = IDNConverter::XMPPNodePrep;
	}
	else if (profileString == "xmpp_resourceprep") {
		profile = IDNConverter::XMPPResourcePrep;
	}
	else if (profileString == "saslprep") {
		profile = IDNConverter::SASLPrep;
	}
	else {
		throw Lua::Exception("Invalid profile");
	}
	try {
		lua_pushstring(L, converter->getStringPrepared(Lua::checkString(L, 1), profile).c_str());
	}
	catch (const std::exception&) {
		throw Lua::Exception("Error");
	}
	return 1;
}


/*******************************************************************************
 * Module registration
 ******************************************************************************/

static const luaL_reg sluift_functions[] = { {NULL, NULL} };

SLUIFT_API int luaopen_sluift(lua_State* L) {
	// Initialize globals
	Sluift::globals.debug = false;
	Sluift::globals.timeout = -1;

	luaL_register(L, "sluift", sluift_functions);

	// Load bootstrap code
	if (luaL_loadbuffer(L, boot_lua, strlen(boot_lua), "boot.lua") != 0) {
		lua_error(L);
	}
	lua_call(L, 0, 1);
	Sluift::globals.bootIndex = luaL_ref(L, LUA_REGISTRYINDEX);

	// Register functions
	Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, "Sluift");
	Lua::FunctionRegistry::getInstance().createFunctionTable(L, "JID");
	lua_setfield(L, -2, "jid");
	Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Base64");
	lua_setfield(L, -2, "base64");
	Lua::FunctionRegistry::getInstance().createFunctionTable(L, "IDN");
	lua_setfield(L, -2, "idn");

	// Register convenience functions
	lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex);
	lua_getfield(L, -1, "tprint");
	lua_setfield(L, -3, "tprint");
	lua_pop(L, 1);

	lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex);
	lua_getfield(L, -1, "disco");
	lua_setfield(L, -3, "disco");
	lua_pop(L, 1);

	// Set read only
	lua_createtable(L, 0, 0);
	lua_pushcclosure(L, sluift_index, 0);
	lua_setfield(L, -2, "__index");
	lua_pushcclosure(L, sluift_newindex, 0);
	lua_setfield(L, -2, "__newindex");
	lua_setmetatable(L, -2);
	
	// Load client metatable
	std::vector<std::string> tables = boost::assign::list_of("Client");
	foreach (const std::string& table, tables) {
		Lua::FunctionRegistry::getInstance().registerTypeMetaTable(L, table);
		luaL_getmetatable(L, Lua::FunctionRegistry::getMetaTableNameForType(table).c_str());
		lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex);
		lua_getfield(L, -1, table.c_str());
		if (!lua_isnil(L, -1)) {
			for (lua_pushnil(L); lua_next(L, -2); ) {
				lua_pushvalue(L, -2);
				lua_pushvalue(L, -2);
				lua_settable(L, -7);
				lua_pop(L, 1);
			}
		}
		lua_pop(L, 2);
	}
	return 1;
}