summaryrefslogtreecommitdiffstats
blob: 895d90d9871273291c898fcfc5093b9714492af8 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2011 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

extern "C" {
	#include "sluift.h"
	#include <lauxlib.h>
}

#include <iostream>
#include <string>
#include <deque>

#include <Swiften/Client/Client.h>
#include <Swiften/Client/ClientXMLTracer.h>
#include <Swiften/JID/JID.h>
#include <Swiften/EventLoop/SimpleEventLoop.h>
#include <Swiften/Network/BoostNetworkFactories.h>
#include <Swiften/Base/sleep.h>
#include <Swiften/Elements/SoftwareVersion.h>
#include <Swiften/Queries/Requests/GetSoftwareVersionRequest.h>

using namespace Swift;

#define SLUIFT_CLIENT "SluiftClient*"

/*******************************************************************************
 * Forward declarations
 ******************************************************************************/

static int debugRef = 0;

static bool debug(lua_State* L) {
	lua_rawgeti(L, LUA_REGISTRYINDEX, debugRef);
	lua_getfield(L, -1, "debug");
	bool b = lua_toboolean(L, -1);
	lua_pop(L, 2);
	return b;
}

/*******************************************************************************
 * Helper classes
 ******************************************************************************/

SimpleEventLoop eventLoop;
BoostNetworkFactories networkFactories(&eventLoop);

class SluiftClient {
	public:
		SluiftClient(const JID& jid, const std::string& password, bool debug = false) : tracer(NULL) {
			client = new Client(jid, password, &networkFactories);
			client->setAlwaysTrustCertificates();
			client->onMessageReceived.connect(boost::bind(&SluiftClient::handleIncomingEvent, this, _1));
			client->onPresenceReceived.connect(boost::bind(&SluiftClient::handleIncomingEvent, this, _1));
			if (debug) {
				tracer = new ClientXMLTracer(client);
			}
		}

		~SluiftClient() {
			delete tracer;
			delete client;
		}

		void connect() {
			client->connect();
			while (client->isActive() && !client->isAvailable()) {
				processEvents();
			}
		}

		bool isConnected() const {
			return client->isAvailable();
		}

		void sendMessage(const JID& to, const std::string& body) {
			Message::ref message = boost::make_shared<Message>();
			message->setTo(to);
			message->setBody(body);
			client->sendMessage(message);
		}

		void sendPresence(const std::string& status) {
			client->sendPresence(boost::make_shared<Presence>(status));
		}

		void disconnect() {
			client->disconnect();
			while (client->isActive()) {
				processEvents();
			}
		}

		void setSoftwareVersion(const std::string& name, const std::string& version, const std::string& os) {
			client->setSoftwareVersion(name, version, os);
		}

		boost::optional<SoftwareVersion> getSoftwareVersion(const JID& jid) {
			GetSoftwareVersionRequest::ref request = GetSoftwareVersionRequest::create(jid, client->getIQRouter());
			request->onResponse.connect(boost::bind(&SluiftClient::handleSoftwareVersionResponse, this, _1, _2));
			softwareVersion.reset();
			error.reset();
			request->send();
			while (!softwareVersion && !error) {
				processEvents();
			}
			return softwareVersion;
		}

		Stanza::ref getNextEvent() {
			if (client->isActive() && !pendingEvents.empty()) {
				Stanza::ref event = pendingEvents.front();
				pendingEvents.pop_front();
				return event;
			}
			while (client->isActive() && pendingEvents.empty()) {
				processEvents();
			}
			if (client->isActive()) {
				assert(!pendingEvents.empty());
				Stanza::ref event = pendingEvents.front();
				pendingEvents.pop_front();
				return event;
			}
			else {
				return Stanza::ref();
			}
		}

	private:
		void handleIncomingEvent(Stanza::ref stanza) {
			pendingEvents.push_back(stanza);
		}

		void processEvents() {
			eventLoop.runUntilEvents();
		}

		void handleSoftwareVersionResponse(boost::shared_ptr<SoftwareVersion> version, ErrorPayload::ref error) {
			if (error) {
				this->error = error;
			}
			else if (version) {
				this->softwareVersion = *version;
			}
			else {
				this->softwareVersion = SoftwareVersion("", "", "");
			}
		}
	
	private:
		Client* client;
		ClientXMLTracer* tracer;
		boost::optional<SoftwareVersion> softwareVersion;
		ErrorPayload::ref error;
		std::deque<Stanza::ref> pendingEvents;
};


/*******************************************************************************
 * Client functions.
 ******************************************************************************/

static inline SluiftClient* getClient(lua_State* L) {
	return *reinterpret_cast<SluiftClient**>(luaL_checkudata(L, 1, SLUIFT_CLIENT));
}

static int sluift_client_is_connected(lua_State *L) {
	lua_pushboolean(L, getClient(L)->isConnected());
	return 1;
}

static int sluift_client_disconnect(lua_State *L) {
	getClient(L)->disconnect();
	return 0;
}

static int sluift_client_set_version(lua_State *L) {
	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);
	return 0;
}

static int sluift_client_get_version(lua_State *L) {
	SluiftClient* client = getClient(L);
	JID jid(std::string(luaL_checkstring(L, 2)));

	boost::optional<SoftwareVersion> version = client->getSoftwareVersion(jid);
	if (version) {
		lua_createtable(L, 0, 3);
		lua_pushstring(L, version->getName().c_str());
		lua_setfield(L, -2, "name");
		lua_pushstring(L, version->getVersion().c_str());
		lua_setfield(L, -2, "version");
		lua_pushstring(L, version->getOS().c_str());
		lua_setfield(L, -2, "os");
	}
	else {
		lua_pushnil(L);
	}
	return 1;
}

static int sluift_client_send_message(lua_State *L) {
	getClient(L)->sendMessage(std::string(luaL_checkstring(L, 2)), luaL_checkstring(L, 3));
	return 0;
}

static int sluift_client_send_presence(lua_State *L) {
	getClient(L)->sendPresence(std::string(luaL_checkstring(L, 2)));
	return 0;
}

static int sluift_client_for_event (lua_State *L) {
	SluiftClient* client = getClient(L);
	luaL_checktype(L, 2, LUA_TFUNCTION);
	while (true) {
		Stanza::ref event = client->getNextEvent();
		if (!event) {
			// We got disconnected
			lua_pushnil(L);
			lua_pushliteral(L, "disconnected");
			return 2;
		}
		else {
			// Push the function on the stack
			lua_pushvalue(L, 2);

			bool emitEvent = false;
			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");
				emitEvent = true;
			}
			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");
				emitEvent = true;
			}
			else {
				assert(false);
			}
			if (emitEvent) {
				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;
				}
			}
			else {
				// Remove the function from the stack again, since
				// we're not calling the function
				lua_pop(L, 1);
			}
		}
	}
}

static int sluift_client_gc (lua_State *L) {
	SluiftClient* client = getClient(L);
	delete client;
	return 0;
}


static const luaL_reg sluift_client_functions[] = {
	{"is_connected", sluift_client_is_connected},
	{"disconnect",  sluift_client_disconnect},
	{"send_message", sluift_client_send_message},
	{"send_presence", sluift_client_send_presence},
	{"set_version", sluift_client_set_version},
	{"get_version", sluift_client_get_version},
	{"for_event", sluift_client_for_event},
	{"__gc", sluift_client_gc},
	{NULL, NULL}
};

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

static int sluift_connect(lua_State *L) {
	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);

	*client = new SluiftClient(jid, password, debug(L));
	(*client)->connect();
	if (!(*client)->isConnected()) {
		lua_pushnil(L);
	}
	return 1;
}

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


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

SLUIFT_API int luaopen_sluift(lua_State *L) {
	// Register functions
	luaL_register(L, "sluift", sluift_functions);
	lua_pushboolean(L, false);
	lua_setfield(L, -2, "debug");
	
	lua_pushvalue(L, -1);
	debugRef = luaL_ref(L, LUA_REGISTRYINDEX);


	// Register the client metatable
	luaL_newmetatable(L, SLUIFT_CLIENT);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	luaL_register(L, NULL, sluift_client_functions);

	return 1;
}