-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathluaIF.cpp
More file actions
651 lines (603 loc) · 17.6 KB
/
Copy pathluaIF.cpp
File metadata and controls
651 lines (603 loc) · 17.6 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/**
* @author Giuseppe Marino
* ©Giuseppe Marino 2018 - 2018
* This file is under GPLv3 license see LICENCE
*/
#include "luaIF.h"
#include "tdlua.h"
#include "luajson.h"
#include <td/telegram/td_log.h>
#include <chrono>
#include <exception>
#include <cctype>
#include <iostream>
#include <string>
static TDLua * getTD(lua_State *L)
{
if (lua_type(L, 1) == LUA_TUSERDATA) {
return (TDLua*) *((void**)lua_touserdata(L,1));
}
return nullptr;
}
bool my_lua_isinteger(lua_State *L, int x)
{
lua_Integer value = 0;
return tdlua_lua_integer_value(L, x, value);
}
using json = nlohmann::json;
static bool lua_to_json(lua_State *L, int index, json &result, std::string &error)
{
if (lua_type(L, index) == LUA_TSTRING) {
try {
result = json::parse(lua_tostring(L, index));
} catch (const json::parse_error &parse_error) {
error = std::string("Malformed JSON: ") + parse_error.what();
return false;
}
return true;
}
if (lua_type(L, index) == LUA_TTABLE) {
try {
lua_pushvalue(L, index);
lua_getjson(L, result);
lua_pop(L, 1);
} catch (const std::exception &codec_error) {
error = codec_error.what();
return false;
}
return true;
}
error = "request must be a JSON string or table";
return false;
}
static int tdclient_new(lua_State *L)
{
luaL_newmetatable(L, "tdclient");
luaL_newlib(L, mt);
lua_setfield(L, -2, "__methods");
lua_pushcfunction(L, tdclient_index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, tdclient_newindex);
lua_setfield(L, -2, "__newindex");
lua_pushcfunction(L, tdclient_unload);
lua_setfield(L, -2, "__gc");
TDLua **client = (TDLua**)(lua_newuserdata(L, sizeof(void*)));
*client = new TDLua(L);
luaL_setmetatable(L, "tdclient");
return 1;
}
static int tdclient_receive(lua_State *L)
{
TDLua* td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
if (!td->empty()) {
json queued = td->pop();
td->checkAuthState(queued);
td->dispatcher().dispatch(queued);
lua_pushjson(L, queued);
return 1;
}
if (td->closed()) {
lua_pushnil(L);
return 1;
}
lua_Number timeout = 10.0;
if (lua_type(L, 2) == LUA_TNUMBER) {
timeout = lua_tonumber(L, 2);
}
json result = td->receive(timeout);
if (result.empty()) {
lua_pushnil(L);
} else {
td->checkAuthState(result);
td->dispatcher().dispatch(result);
lua_pushjson(L, result);
#ifdef TDLUA_CALLS
if (result["@type"] == "updateCall") {
std::string callState = result["call"]["state"]["@type"];
if (callState == "callStateReady") {
lua_getfield(L, -1, "call");
Call* call = Call::NewLua(L, result["call"], td);
lua_remove(L, -2);
td->setCall(result["call"]["id"], call);
lua_setfield(L, -2, "call");
} else if (callState == "callStateDiscarded") {
std::string reason = result["call"]["state"]["reason"]["@type"];
if (reason == "callDiscardReasonHungUp" || reason == "callDiscardReasonDisconnected") {
Call* call = td->getCall(result["call"]["id"]);
delete call;
td->delCall(result["call"]["id"]);
}
}
}
#endif
}
return 1;
}
static int tdclient_send(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
json j;
std::string error;
if (!lua_to_json(L, 2, j, error)) {
return luaL_error(L, "%s", error.c_str());
}
if (td->closed()) {
return luaL_error(L, "tdlua client is closed");
}
if(!td->ready() && j["@type"] == "setTdlibParameters" && j["database_directory"].is_string()) {
td->setDB(j["database_directory"]);
}
td->send(j);
return 0;
}
static int tdclient_execute(lua_State *L)
{
json j;
lua_Number timeout = 10.0;
if (lua_type(L, -1) == LUA_TNUMBER) {
timeout = lua_tonumber(L, -1);
lua_pop(L, 1);
}
if (lua_type(L, -1) == LUA_TSTRING || lua_type(L, -1) == LUA_TTABLE) {
std::string error;
if (!lua_to_json(L, -1, j, error)) {
return luaL_error(L, "%s", error.c_str());
}
} else {
return 0;
}
lua_pop(L, 1);
if (!j.is_object()) {
return 0;
}
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
if (td->closed()) {
return luaL_error(L, "tdlua client is closed");
}
const std::uint64_t nonce = td->nextRequestId();
json extra = j["@extra"];
j["@extra"] = nonce;
if(!td->ready() && j["@type"] == "setTdlibParameters" && j["database_directory"].is_string()) {
td->setDB(j["database_directory"]);
}
td->send(j);
const auto started = std::chrono::steady_clock::now();
while (!td->closed()) {
const std::chrono::duration<double> elapsed =
std::chrono::steady_clock::now() - started;
const double remaining = timeout - elapsed.count();
if (remaining <= 0.0) {
break;
}
json res = td->receive(remaining);
if (!res.is_object()) {
continue;
}
td->checkAuthState(res);
if (res["@extra"].is_number_integer() &&
nonce == res["@extra"].get<std::uint64_t>()) {
res["@extra"] = extra;
lua_pushjson(L, res);
return 1;
} else {
td->push(res);
}
}
return 0;
}
static int call(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
if (td->closed()) {
return luaL_error(L, "tdlua client is closed");
}
const int top = lua_gettop(L);
int params_index = 0;
int callback_index = 0;
int context_index = 0;
bool fire_and_forget = false;
if (top >= 2) {
const int first_type = lua_type(L, 2);
if (first_type == LUA_TFUNCTION) {
callback_index = 2;
if (top >= 3) {
context_index = 3;
}
if (top > 3) {
return luaL_error(L, "too many arguments for asynchronous helper");
}
} else if (first_type == LUA_TBOOLEAN) {
if (top > 2) {
return luaL_error(L, "legacy send flag must be the last argument");
}
fire_and_forget = lua_toboolean(L, 2) != 0;
} else if (first_type == LUA_TTABLE || first_type == LUA_TSTRING) {
params_index = 2;
if (top >= 3 && lua_type(L, 3) == LUA_TFUNCTION) {
callback_index = 3;
if (top >= 4) {
context_index = 4;
}
if (top > 4) {
return luaL_error(L, "too many arguments for asynchronous helper");
}
} else if (top >= 3 && lua_type(L, 3) == LUA_TBOOLEAN) {
if (top > 3) {
return luaL_error(L, "legacy send flag must be the last argument");
}
fire_and_forget = lua_toboolean(L, 3) != 0;
} else if (top > 2) {
return luaL_error(L, "expected callback function or legacy boolean");
}
} else if (first_type == LUA_TNIL) {
if (top >= 3 && lua_type(L, 3) == LUA_TFUNCTION) {
callback_index = 3;
if (top >= 4) {
context_index = 4;
}
if (top > 4) {
return luaL_error(L, "too many arguments for asynchronous helper");
}
} else if (top > 2) {
return luaL_error(L, "nil parameters must be followed by a callback");
}
} else {
return luaL_error(L, "expected params table, callback function, or legacy boolean");
}
}
json request = json::object();
if (params_index != 0) {
std::string error;
if (!lua_to_json(L, params_index, request, error)) {
return luaL_error(L, "%s", error.c_str());
}
if (!request.is_object()) {
return luaL_error(L, "helper params must be a JSON object");
}
}
request["@type"] = lua_tostring(L, lua_upvalueindex(1));
if (fire_and_forget) {
if (!td->ready() && request["@type"] == "setTdlibParameters" &&
request["database_directory"].is_string()) {
td->setDB(request["database_directory"]);
}
td->send(request);
return 0;
}
if (callback_index != 0) {
const std::uint64_t id = td->dispatcher().request(
L, request, callback_index, context_index);
if (!td->ready() && request["@type"] == "setTdlibParameters" &&
request["database_directory"].is_string()) {
td->setDB(request["database_directory"]);
}
td->send(request);
lua_pushinteger(L, static_cast<lua_Integer>(id));
return 1;
}
const int is_main = lua_pushthread(L);
lua_pop(L, 1);
if (!is_main) {
td->dispatcher().await(L, request);
if (!td->ready() && request["@type"] == "setTdlibParameters" &&
request["database_directory"].is_string()) {
td->setDB(request["database_directory"]);
}
td->send(request);
return lua_yield(L, 0);
}
lua_pushjson(L, request);
return tdclient_execute(L);
}
static int tdclient_call(lua_State *L)
{
lua_pushcclosure(L, call, 1);
return 1;
}
static bool handler_property(const char *name, std::string &type)
{
if (!name || name[0] != 'o' || name[1] != 'n' || name[2] == '\0' ||
!std::isupper(static_cast<unsigned char>(name[2]))) {
return false;
}
type.assign(name + 2);
type[0] = static_cast<char>(std::tolower(
static_cast<unsigned char>(type[0])));
return true;
}
static int tdclient_index(lua_State *L)
{
TDLua *td = getTD(L);
const char *name = luaL_checkstring(L, 2);
std::string type;
if (td && handler_property(name, type)) {
if (td->dispatcher().pushHandler(L, type)) {
return 1;
}
lua_pushnil(L);
return 1;
}
luaL_getmetatable(L, "tdclient");
lua_getfield(L, -1, "__methods");
lua_getfield(L, -1, name);
if (!lua_isnil(L, -1)) {
lua_remove(L, -2);
lua_remove(L, -2);
return 1;
}
lua_pop(L, 3);
lua_pushstring(L, name);
lua_pushcclosure(L, call, 1);
return 1;
}
static int tdclient_newindex(lua_State *L)
{
TDLua *td = getTD(L);
const char *name = luaL_checkstring(L, 2);
std::string type;
if (!td || !handler_property(name, type)) {
return luaL_error(L, "tdlua: unsupported client property '%s'", name);
}
if (lua_isnil(L, 3)) {
td->dispatcher().off(type);
} else {
luaL_checktype(L, 3, LUA_TFUNCTION);
td->dispatcher().on(L, type, 3);
}
return 0;
}
static int tdclient_rawexecute(lua_State *L)
{
json j;
if (lua_type(L, -1) == LUA_TSTRING || lua_type(L, -1) == LUA_TTABLE) {
std::string error;
if (!lua_to_json(L, -1, j, error)) {
return luaL_error(L, "%s", error.c_str());
}
} else {
return luaL_error(L, "request must be a JSON string or table");
}
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
if (td->closed()) {
return luaL_error(L, "tdlua client is closed");
}
auto result = td->execute(j);
if (result.empty()) {
lua_pushnil(L);
} else {
lua_pushjson(L, result);
}
return 1;
}
static int tdclient_request(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
if (td->closed()) {
return luaL_error(L, "tdlua client is closed");
}
json request;
std::string error;
if (!lua_to_json(L, 2, request, error)) {
return luaL_error(L, "%s", error.c_str());
}
if (!request.is_object()) {
return luaL_error(L, "request must be a JSON object");
}
int callback_index = 0;
int context_index = 0;
if (lua_gettop(L) >= 3 && !lua_isnil(L, 3)) {
callback_index = 3;
context_index = lua_gettop(L) >= 4 ? 4 : 0;
}
if (callback_index != 0 && lua_type(L, callback_index) != LUA_TFUNCTION) {
return luaL_error(L, "request callback must be a function");
}
const std::uint64_t id = td->dispatcher().request(
L, request, callback_index, context_index);
if (!td->ready() && request["@type"] == "setTdlibParameters" &&
request["database_directory"].is_string()) {
td->setDB(request["database_directory"]);
}
td->send(request);
lua_pushinteger(L, static_cast<lua_Integer>(id));
return 1;
}
static int tdclient_await(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
if (td->closed()) {
return luaL_error(L, "tdlua client is closed");
}
json request;
std::string error;
if (!lua_to_json(L, 2, request, error)) {
return luaL_error(L, "%s", error.c_str());
}
if (!request.is_object()) {
return luaL_error(L, "request must be a JSON object");
}
td->dispatcher().await(L, request);
if (!td->ready() && request["@type"] == "setTdlibParameters" &&
request["database_directory"].is_string()) {
td->setDB(request["database_directory"]);
}
td->send(request);
return lua_yield(L, 0);
}
static int tdclient_on(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
const char *type = luaL_checkstring(L, 2);
luaL_checktype(L, 3, LUA_TFUNCTION);
td->dispatcher().on(L, type, 3);
return 0;
}
static int tdclient_off(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
const char *type = luaL_checkstring(L, 2);
td->dispatcher().off(type);
return 0;
}
static int tdclient_save(lua_State *L)
{
TDLua *td = getTD(L);
td->saveUpdatesBuffer();
return 0;
}
static int tdclient_clear(lua_State *L)
{
TDLua *td = getTD(L);
td->emptyUpdatesBuffer();
return 0;
}
static int tdclient_unload(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return 0;
}
#ifdef TDLUA_CALLS
td->deinitAllCalls();
while (td->runningCalls()) {
tdclient_receive(L);
}
#endif
td->close();
delete td;
return 0;
}
static int tdclient_close(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
td->close();
td->dispatcher().clear();
return 0;
}
static int tdclient_isclosed(lua_State *L)
{
TDLua *td = getTD(L);
if (!td) {
return luaL_error(L, "invalid tdlua client");
}
lua_pushboolean(L, td->closed());
return 1;
}
static int tdclient_getcall(lua_State *L)
{
#ifdef TDLUA_CALLS
TDLua *td = getTD(L);
if (my_lua_isinteger(L, 2)) {
int32_t callID = lua_tointeger(L, 2);
Call* call = td->getCall(callID);
if (call) {
json j = call->getTDCall();
lua_pushjson(L, j);
*((Call**) lua_newuserdata(L, sizeof(void**))) = call;
Call::setMeta(L);
return 1;
}
}
return 0;
#else
return luaL_error(L, "TDLUA was not compiled with libtgvoip");
#endif
}
static void tdclient_fatalerrorcb(const char *error)
{
std::cerr << "[TDLUA FATAL ERROR] " << error << std::endl;
}
static int tdclient_setlogpath(lua_State *L)
{
if (lua_type(L, 1) == LUA_TSTRING) {
lua_pushboolean(L, td_set_log_file_path(lua_tostring(L, 1)));
} else {
lua_pushboolean(L, 0);
}
return 1;
}
static int tdclient_setlogmaxsize(lua_State *L)
{
if (lua_type(L, 1) == LUA_TNUMBER) {
td_set_log_max_file_size(lua_tointeger(L, 1));
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
return 1;
}
static int tdclient_setlogverbosity(lua_State *L)
{
if (my_lua_isinteger(L, 1)) {
td_set_log_verbosity_level(static_cast<int>(lua_tointeger(L, 1)));
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
return 1;
}
//Open Lib
static luaL_Reg tdlua[] = {
{"new", tdclient_new},
{"setLogPath", tdclient_setlogpath},
{"setLogMaxSize", tdclient_setlogmaxsize},
{"setLogLevel", tdclient_setlogverbosity},
{nullptr, nullptr}
};
#ifndef TDLUA_VERSION_STRING
#define TDLUA_VERSION_STRING "unknown"
#endif
#ifndef TDLUA_BASE_VERSION
#define TDLUA_BASE_VERSION "unknown"
#endif
#ifndef TDLUA_TDLIB_VERSION
#define TDLUA_TDLIB_VERSION "unknown"
#endif
extern "C" {
LUALIB_API int luaopen_tdlua(lua_State *L) {
luaL_newmetatable(L, "tdlua");
lua_pushstring(L, "__call");
lua_pushcfunction(L, tdclient_new);
lua_settable(L, -3);
luaL_newlib(L, tdlua);
lua_pushstring(L, TDLUA_VERSION_STRING);
lua_setfield(L, -2, "version");
lua_pushstring(L, TDLUA_BASE_VERSION);
lua_setfield(L, -2, "api_version");
lua_pushstring(L, TDLUA_TDLIB_VERSION);
lua_setfield(L, -2, "tdlib_version");
luaL_setmetatable(L, "tdlua");
td_set_log_fatal_error_callback(tdclient_fatalerrorcb);
return 1;
}
}