-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
luaIF.cpp
318 lines (298 loc) · 8.11 KB
/
luaIF.cpp
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
/**
* @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 <iostream>
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)
{
return (lua_tonumber(L, x) == lua_tointeger(L, x));
}
using json = nlohmann::json;
static int tdclient_new(lua_State *L)
{
luaL_newmetatable(L, "tdclient");
lua_pushstring(L, "__index");
luaL_newlib(L, mt);
lua_newtable(L);
lua_pushstring(L, "__index");
lua_pushcfunction(L, tdclient_call);
lua_settable(L, -3);
lua_setmetatable(L, -2);
lua_settable(L, -3);
lua_pushstring(L, "__gc");
lua_pushcfunction(L, tdclient_unload);
lua_settable(L, -3);
TDLua **client = (TDLua**)(lua_newuserdata(L, sizeof(void*)));
*client = new TDLua();
luaL_setmetatable(L, "tdclient");
return 1;
}
static int tdclient_receive(lua_State *L)
{
TDLua* td = getTD(L);
if (!td->empty()) {
lua_pushjson(L, td->pop());
return 1;
}
lua_Number timeout = 10;
if (lua_type(L, -1) == LUA_TNUMBER) {
timeout = lua_tonumber(L, -1);
lua_pop(L, 1);
}
json result = td->receive(timeout);
if (result.empty()) {
lua_pushnil(L);
} else {
td->checkAuthState(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);
json j;
if (lua_type(L, 2) == LUA_TSTRING) {
try {
j = json::parse(lua_tostring(L, 2));
} catch (json::parse_error &e) {
std::cerr << "[TDLUA SEND] JSON Parse error " << e.what() << "\n";
return luaL_error(L, "Malformed JSON");
}
} else if (lua_type(L, 2) == LUA_TTABLE) {
lua_getjson(L, j);
}
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*CLOCKS_PER_SEC;
if (lua_type(L, -1) == LUA_TNUMBER) {
timeout = lua_tonumber(L, -1) * CLOCKS_PER_SEC;
lua_pop(L, 1);
}
if (lua_type(L, -1) == LUA_TSTRING) {
try {
j = json::parse(lua_tostring(L, -1));
} catch (json::parse_error &e) {
std::cerr << "[TDLUA EXECUTE] JSON Parse error " << e.what() << "\n";
return luaL_error(L, "Malformed JSON");
}
} else if (lua_type(L, -1) == LUA_TTABLE) {
lua_getjson(L, j);
} else {
return 0;
}
lua_pop(L, 1);
if (!j.is_object()) {
return 0;
}
int nonce = rand();
json extra = j["@extra"];
j["@extra"] = nonce;
TDLua *td = getTD(L);
if(!td->ready() && j["@type"] == "setTdlibParameters" && j["database_directory"].is_string()) {
td->setDB(j["database_directory"]);
}
td->send(j);
auto t = clock();
while (td->ready() && (clock() - t < timeout)) {
json res = td->receive(1);
if (!res.is_object()) {
continue;
}
td->checkAuthState(res);
if (res["@extra"].is_number() && nonce == res["@extra"].get<int>()) {
res["@extra"] = extra;
lua_pushjson(L, res);
return 1;
} else {
td->push(res);
}
}
return 0;
}
static int call(lua_State *L)
{
auto f = tdclient_execute;
if (lua_type(L, -1) == LUA_TBOOLEAN) {
if (lua_toboolean(L, -1))
f = tdclient_send;
lua_pop(L, 1);
}
if (lua_type(L, -1) != LUA_TTABLE) {
lua_newtable(L);
}
lua_pushstring(L, "@type");
lua_pushstring(L, lua_tostring(L, lua_upvalueindex(1)));
lua_settable(L, -3);
return f(L);
}
static int tdclient_call(lua_State *L)
{
lua_pushcclosure(L, call, 1);
return 1;
}
static int tdclient_rawexecute(lua_State *L)
{
json j;
if (lua_type(L, -1) == LUA_TSTRING) {
try {
j = json::parse(lua_tostring(L, -1));
} catch (json::parse_error &e) {
std::cerr << "[TDLUA RAWEXECUTE] JSON Parse error " << e.what() << "\n";
return luaL_error(L, "Malformed JSON");
}
} else if (lua_type(L, -1) == LUA_TTABLE) {
lua_getjson(L, j);
}
TDLua *td = getTD(L);
auto result = td->execute(j);
if (result.empty()) {
lua_pushnil(L);
} else {
lua_pushjson(L, result);
}
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);
#ifdef TDLUA_CALLS
td->deinitAllCalls();
while (td->runningCalls()) {
tdclient_receive(L);
}
#endif
if (td->ready()) {
td->send({{"@type", "close"}});
}
while (td->ready()) {
json res = td->receive();
td->checkAuthState(res);
td->push(res);
}
delete td;
return 0;
}
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}
};
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);
luaL_setmetatable(L, "tdlua");
td_set_log_fatal_error_callback(tdclient_fatalerrorcb);
return 1;
}
}