-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tdlua.cpp
188 lines (165 loc) · 3.88 KB
/
tdlua.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
/**
* @author Giuseppe Marino
* ©Giuseppe Marino 2018 - 2018
* This file is under GPLv3 license see LICENCE
*/
#include "tdlua.h"
#include <iostream>
#include <fstream>
#include <td/telegram/td_json_client.h>
TDLua::TDLua()
{
tdjson = td_json_client_create();
_ready = false;
}
TDLua::~TDLua()
{
td_json_client_destroy(tdjson);
}
void TDLua::setTD(void* td)
{
tdjson = td;
}
void* TDLua::getTD() const
{
return tdjson;
}
nlohmann::json TDLua::pop()
{
nlohmann::json res = updates.front();
updates.pop();
return res;
}
void TDLua::setDB(const std::string path)
{
dbpath = path;
if (dbpath.back() != '/')
dbpath += "/";
dbpath += "tdlua.json";
}
void TDLua::send(const nlohmann::json json) const
{
td_json_client_send(tdjson, json.dump().c_str());
}
nlohmann::json TDLua::execute(const nlohmann::json json) const
{
const char *res = td_json_client_execute(tdjson, json.dump().c_str());
if (!res) {
return nullptr;
}
nlohmann::json jres = nullptr;
try {
jres = nlohmann::json::parse(res);
} catch (nlohmann::json::parse_error &e) {
std::cout << "[TDCLIENT EXECUTE] JSON Parse error " << e.what() << "\n";
}
return jres;
}
nlohmann::json TDLua::receive(const size_t timeout) const
{
const char *res = td_json_client_receive(tdjson, timeout);
if (!res) {
return nullptr;
}
nlohmann::json jres = nullptr;
try {
jres = nlohmann::json::parse(res);
} catch (nlohmann::json::parse_error &e) {
std::cout << "[TDCLIENT RECEIVE] JSON Parse error " << e.what() << "\n";
}
return jres;
}
void TDLua::push(const nlohmann::json update)
{
updates.push(update);
}
bool TDLua::empty() const
{
return updates.empty();
}
#ifdef TDLUA_CALLS
void TDLua::setCall(const int32_t id, const Call* call)
{
calls[id] = (Call*) call;
}
void TDLua::delCall(const int32_t id)
{
calls.erase(id);
}
Call* TDLua::getCall(const int32_t id) const
{
return calls.at(id);
}
void TDLua::deinitAllCalls()
{
for (auto call : calls)
{
call.second->closeCall();
}
}
uint64_t TDLua::runningCalls()
{
return calls.size();
}
#endif
void TDLua::saveUpdatesBuffer()
{
if (!_ready || dbpath.empty()) return;
nlohmann::json jupdates = nlohmann::json::array();
while(updates.size()) {
jupdates[updates.size()] = this->pop();
}
std::ofstream out(dbpath);
out << jupdates.dump();
out.close();
}
void TDLua::loadUpdatesBuffer()
{
if (dbpath.empty() || _ready || !updates.empty()) return;
std::ifstream in(dbpath);
if (in && in.is_open()) {
std::string buf;
in.seekg(0, std::ios::end);
buf.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&buf[0], buf.size());
in.close();
try {
nlohmann::json j = nlohmann::json::parse(buf);
if (j.is_array() && !j.empty()) {
for (auto &elem : j) {
updates.push(elem);
}
}
} catch (nlohmann::json::parse_error &e){
std::cerr << "[TDCLIENT LOAD BUFFER] JSON Parse error " << e.what() << "\n";
_ready = true;
emptyUpdatesBuffer();
saveUpdatesBuffer();
return;
}
}
_ready = true;
}
void TDLua::emptyUpdatesBuffer()
{
while (!updates.empty()) {
updates.pop();
}
}
void TDLua::checkAuthState(const nlohmann::json update)
{
if (update["@type"] == "updateAuthorizationState") {
if (!ready() && update["authorization_state"]["@type"] == "authorizationStateReady") {
loadUpdatesBuffer();
} else if (update["authorization_state"]["@type"] == "authorizationStateClosed") {
saveUpdatesBuffer();
emptyUpdatesBuffer();
_ready = false;
}
}
}
bool TDLua::ready() const
{
return _ready;
}