Skip to content

Commit 3bbd8e5

Browse files
author
V V
committed
fixed json
1 parent 70b0aed commit 3bbd8e5

File tree

3 files changed

+83
-50
lines changed

3 files changed

+83
-50
lines changed

‎json-tg.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "json-tg.h"
66
#include <tgl/tgl.h>
77
#include <tgl/tgl-layout.h>
8+
#include "interface.h"
89
#include <assert.h>
910
//format time:
1011
#include <time.h>
@@ -27,6 +28,9 @@ void json_pack_peer_type (json_t *res, tgl_peer_id_t id) {
2728
case TGL_PEER_ENCR_CHAT:
2829
assert (json_object_set (res, "type", json_string ("encr_chat")) >= 0);
2930
break;
31+
case TGL_PEER_CHANNEL:
32+
assert (json_object_set (res, "type", json_string ("channel")) >= 0);
33+
break;
3034
default:
3135
assert (0);
3236
}
@@ -77,6 +81,14 @@ void json_pack_chat (json_t *res, tgl_peer_t *P) {
7781
}
7882
}
7983

84+
void json_pack_channel (json_t *res, tgl_peer_t *P) {
85+
assert (P->channel.title);
86+
assert (json_object_set (res, "title", json_string (P->channel.title)) >= 0);
87+
assert (json_object_set (res, "participants_count", json_integer (P->channel.participants_count)) >= 0);
88+
assert (json_object_set (res, "admins_count", json_integer (P->channel.admins_count)) >= 0);
89+
assert (json_object_set (res, "kicked_count", json_integer (P->channel.kicked_count)) >= 0);
90+
}
91+
8092

8193
void json_pack_encr_chat (json_t *res, tgl_peer_t *P) {
8294
assert (json_object_set (res, "user", json_pack_peer (TGL_MK_USER (P->encr_chat.user_id))) >= 0);
@@ -86,7 +98,7 @@ json_t *json_pack_peer (tgl_peer_id_t id) {
8698
tgl_peer_t *P = tgl_peer_get (TLS, id);
8799
//assert (P);
88100
json_t *res = json_object ();
89-
assert (json_object_set (res, "id", json_integer (tgl_get_peer_id (id))) >= 0);
101+
assert (json_object_set (res, "id", json_string (print_permanent_peer_id (id))) >= 0);
90102

91103
json_pack_peer_type (res, id);
92104

@@ -101,6 +113,9 @@ json_t *json_pack_peer (tgl_peer_id_t id) {
101113
case TGL_PEER_CHAT:
102114
sprintf (s, "chat#%d", tgl_get_peer_id (id));
103115
break;
116+
case TGL_PEER_CHANNEL:
117+
sprintf (s, "channel#%d", tgl_get_peer_id (id));
118+
break;
104119
case TGL_PEER_ENCR_CHAT:
105120
sprintf (s, "encr_chat#%d", tgl_get_peer_id (id));
106121
break;
@@ -128,6 +143,9 @@ json_t *json_pack_peer (tgl_peer_id_t id) {
128143
case TGL_PEER_ENCR_CHAT:
129144
json_pack_encr_chat (res, P);
130145
break;
146+
case TGL_PEER_CHANNEL:
147+
json_pack_channel (res, P);
148+
break;
131149
default:
132150
assert (0);
133151
}
@@ -377,6 +395,10 @@ json_t *json_pack_service (struct tgl_message *M) {
377395
case tgl_message_action_abort_key:
378396
assert (json_object_set (res, "type", json_string ("abort_key")) >= 0);
379397
break;
398+
case tgl_message_action_channel_create:
399+
assert (json_object_set (res, "type", json_string ("channel_created")) >= 0);
400+
assert (json_object_set (res, "title", json_string (M->action.title)) >= 0);
401+
break;
380402
default:
381403
assert (json_object_set (res, "type", json_string ("???")) >= 0);
382404
break;
@@ -389,7 +411,7 @@ json_t *json_pack_message (struct tgl_message *M) {
389411
assert (json_object_set (res, "event", json_string ("message")) >= 0);
390412
//will overwriten to service, if service.
391413

392-
assert (json_object_set (res, "id", json_integer (M->id)) >= 0);
414+
assert (json_object_set (res, "id", json_string (print_permanent_msg_id (M->permanent_id))) >= 0);
393415
if (!(M->flags & TGLMF_CREATED)) { return res; }
394416

395417
assert (json_object_set (res, "flags", json_integer (M->flags)) >= 0);

‎lua-tg.c

+58-47
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ lua_State *luaState;
4242
//#include "interface.h"
4343
//#include "auto/constants.h"
4444
#include <tgl/tgl.h>
45+
#include <tgl/tgl-queries.h>
4546
#include "interface.h"
4647

4748
#include <assert.h>
@@ -78,7 +79,7 @@ void lua_add_lstring_field (const char *name, const char *value, int len) {
7879
if (!value || !len) { return; }
7980
my_lua_checkstack (luaState, 3);
8081
lua_pushstring (luaState, name);
81-
lua_pushlstring (luaState, len, value);
82+
lua_pushlstring (luaState, value, len);
8283
lua_settable (luaState, -3);
8384
}
8485

@@ -158,11 +159,11 @@ void push_encr_chat (tgl_peer_t *P) {
158159

159160
void push_channel (tgl_peer_t *P) {
160161
my_lua_checkstack (luaState, 4);
161-
lua_add_string_field (luaState, "title", P->channel.title);
162-
lua_add_string_field (luaState, "about", P->channel.about);
163-
lua_add_num_field (luaState, "participants_count", P->channel.participants_count);
164-
lua_add_num_field (luaState, "admins_count", P->channel.admins_count);
165-
lua_add_num_field (luaState, "kicked_count", P->channel.kicked_count);
162+
lua_add_string_field ("title", P->channel.title);
163+
lua_add_string_field ("about", P->channel.about);
164+
lua_add_num_field ("participants_count", P->channel.participants_count);
165+
lua_add_num_field ("admins_count", P->channel.admins_count);
166+
lua_add_num_field ("kicked_count", P->channel.kicked_count);
166167
}
167168

168169
void push_update_types (unsigned flags) {
@@ -225,8 +226,10 @@ void push_update_types (unsigned flags) {
225226
void push_peer (tgl_peer_id_t id, tgl_peer_t *P) {
226227
lua_newtable (luaState);
227228

228-
lua_add_lstring_field ("id", print_permanent_peer_id (P->id));
229-
lua_add_string_field (luaState, "type", tgl_get_peer_type (id));
229+
lua_add_string_field ("id", print_permanent_peer_id (P->id));
230+
lua_pushstring (luaState, "type");
231+
push_tgl_peer_type (tgl_get_peer_type (P->id));
232+
lua_settable (luaState, -3);
230233

231234
if (!P || !(P->flags & TGLPF_CREATED)) {
232235
lua_pushstring (luaState, "print_name");
@@ -441,9 +444,9 @@ void push_service (struct tgl_message *M) {
441444
lua_add_string_field ("type", "channel_created");
442445
lua_add_string_field ("title", M->action.title);
443446
break;
444-
/*default:
447+
default:
445448
lua_pushstring (luaState, "???");
446-
break;*/
449+
break;
447450
}
448451
}
449452

@@ -544,13 +547,13 @@ void lua_diff_end (void) {
544547
}
545548
}
546549

547-
void lua_our_id (int id) {
550+
void lua_our_id (tgl_peer_id_t id) {
548551
if (!have_file) { return; }
549552
lua_settop (luaState, 0);
550553
//lua_checkstack (luaState, 20);
551554
my_lua_checkstack (luaState, 20);
552555
lua_getglobal (luaState, "on_our_id");
553-
lua_pushnumber (luaState, id);
556+
lua_pushnumber (luaState, tgl_get_peer_id (id));
554557
assert (lua_gettop (luaState) == 2);
555558

556559
int r = ps_lua_pcall (luaState, 1, 0, 0);
@@ -626,7 +629,19 @@ void lua_chat_update (struct tgl_chat *C, unsigned flags) {
626629
//extern int peer_num;
627630

628631
#define MAX_LUA_COMMANDS 1000
629-
void *lua_ptr[MAX_LUA_COMMANDS];
632+
633+
struct lua_arg {
634+
int flags;
635+
union {
636+
tgl_message_id_t msg_id;
637+
tgl_peer_id_t peer_id;
638+
char *str;
639+
long long num;
640+
double dval;
641+
void *ptr;
642+
};
643+
};
644+
struct lua_arg lua_ptr[MAX_LUA_COMMANDS];
630645
static int pos;
631646

632647
static inline tgl_peer_t *get_peer (const char *s) {
@@ -750,7 +765,7 @@ void lua_contact_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, i
750765
free (cb);
751766
}
752767

753-
void lua_dialog_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, tgl_peer_id_t peers[], int msgs[], int unread[]) {
768+
void lua_dialog_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, tgl_peer_id_t peers[], tgl_message_id_t *msgs[], int unread[]) {
754769
assert (TLSR == TLS);
755770
struct lua_query_extra *cb = cb_extra;
756771
lua_settop (luaState, 0);
@@ -1034,106 +1049,102 @@ void lua_str_cb (struct tgl_state *TLSR, void *cb_extra, int success, const char
10341049
free (cb);
10351050
}
10361051

1052+
#define LUA_STR_ARG(n) lua_ptr[n].str, strlen (lua_ptr[n].str)
1053+
10371054
void lua_do_all (void) {
10381055
int p = 0;
10391056
while (p < pos) {
1040-
int l = (long)lua_ptr[p ++];
1057+
int l = lua_ptr[p ++].num;
10411058
assert (p + l + 1 <= pos);
1042-
enum lua_query_type f = (long)lua_ptr[p ++];
1059+
enum lua_query_type f = lua_ptr[p ++].num;
10431060
struct tgl_message *M;
10441061
char *s, *s1, *s2, *s3;
1062+
int q = p;
1063+
tgl_message_id_t *tmp_msg_id;
10451064
switch (f) {
10461065
case lq_contact_list:
1047-
tgl_do_update_contact_list (TLS, lua_contact_list_cb, lua_ptr[p ++]);
1066+
tgl_do_update_contact_list (TLS, lua_contact_list_cb, lua_ptr[p ++].ptr);
10481067
break;
10491068
case lq_dialog_list:
1050-
tgl_do_get_dialog_list (TLS, 100, 0, lua_dialog_list_cb, lua_ptr[p ++]);
1069+
tgl_do_get_dialog_list (TLS, 100, 0, lua_dialog_list_cb, lua_ptr[p ++].ptr);
10511070
break;
10521071
case lq_msg:
1053-
tgl_do_send_message (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], strlen (lua_ptr[p + 2]), 0, NULL, lua_msg_cb, lua_ptr[p]);
1054-
free (lua_ptr[p + 2]);
1072+
tgl_do_send_message (TLS, lua_ptr[p + 1].peer_id, LUA_STR_ARG (p + 2), 0, NULL, lua_msg_cb, lua_ptr[p].ptr);
10551073
p += 3;
10561074
break;
10571075
case lq_send_typing:
1058-
tgl_do_send_typing (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, tgl_typing_typing, lua_empty_cb, lua_ptr[p]);
1076+
tgl_do_send_typing (TLS, lua_ptr[p + 1].peer_id, tgl_typing_typing, lua_empty_cb, lua_ptr[p].ptr);
10591077
p += 2;
10601078
break;
10611079
case lq_send_typing_abort:
1062-
tgl_do_send_typing (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, tgl_typing_cancel, lua_empty_cb, lua_ptr[p]);
1080+
tgl_do_send_typing (TLS, lua_ptr[p + 1].peer_id, tgl_typing_cancel, lua_empty_cb, lua_ptr[p].ptr);
10631081
p += 2;
10641082
break;
10651083
case lq_rename_chat:
1066-
tgl_do_rename_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], strlen (lua_ptr[p + 2]), lua_empty_cb, lua_ptr[p]);
1067-
free (lua_ptr[p + 2]);
1084+
tgl_do_rename_chat (TLS, lua_ptr[p + 1].peer_id, LUA_STR_ARG (p + 2), lua_empty_cb, lua_ptr[p].ptr);
10681085
p += 3;
10691086
break;
10701087
case lq_send_photo:
1071-
tgl_do_send_document (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_PHOTO, lua_msg_cb, lua_ptr[p]);
1072-
free (lua_ptr[p + 2]);
1088+
tgl_do_send_document (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_PHOTO, lua_msg_cb, lua_ptr[p].ptr);
10731089
p += 3;
10741090
break;
10751091
case lq_send_video:
1076-
tgl_do_send_document (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_VIDEO, lua_msg_cb, lua_ptr[p]);
1077-
free (lua_ptr[p + 2]);
1092+
tgl_do_send_document (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_VIDEO, lua_msg_cb, lua_ptr[p].ptr);
10781093
p += 3;
10791094
break;
10801095
case lq_send_audio:
1081-
tgl_do_send_document (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUDIO, lua_msg_cb, lua_ptr[p]);
1082-
free (lua_ptr[p + 2]);
1096+
tgl_do_send_document (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUDIO, lua_msg_cb, lua_ptr[p].ptr);
10831097
p += 3;
10841098
break;
10851099
case lq_send_document:
1086-
tgl_do_send_document (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], NULL, 0, 0, lua_msg_cb, lua_ptr[p]);
1087-
free (lua_ptr[p + 2]);
1100+
tgl_do_send_document (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, NULL, 0, 0, lua_msg_cb, lua_ptr[p].ptr);
10881101
p += 3;
10891102
break;
10901103
case lq_send_file:
1091-
tgl_do_send_document (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUTO, lua_msg_cb, lua_ptr[p]);
1092-
free (lua_ptr[p + 2]);
1104+
tgl_do_send_document (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUTO, lua_msg_cb, lua_ptr[p].ptr);
10931105
p += 3;
10941106
break;
10951107
case lq_send_text:
1096-
tgl_do_send_text (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], 0, lua_msg_cb, lua_ptr[p]);
1097-
free (lua_ptr[p + 2]);
1108+
tgl_do_send_text (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, 0, lua_msg_cb, lua_ptr[p].ptr);
10981109
p += 3;
10991110
break;
11001111
case lq_chat_set_photo:
1101-
tgl_do_set_chat_photo (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_empty_cb, lua_ptr[p]);
1102-
free (lua_ptr[p + 2]);
1112+
tgl_do_set_chat_photo (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, lua_empty_cb, lua_ptr[p].ptr);
11031113
p += 3;
11041114
break;
11051115
case lq_load_photo:
11061116
case lq_load_video:
11071117
case lq_load_audio:
11081118
case lq_load_document:
1109-
M = lua_ptr[p + 1];
1119+
M = tgl_message_get (TLS, &lua_ptr[p + 1].msg_id);
11101120
if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) {
1111-
lua_file_cb (TLS, lua_ptr[p], 0, 0);
1121+
lua_file_cb (TLS, lua_ptr[p].ptr, 0, 0);
11121122
} else {
11131123
if (M->media.type == tgl_message_media_photo) {
11141124
assert (M->media.photo);
1115-
tgl_do_load_photo (TLS, M->media.photo, lua_file_cb, lua_ptr[p]);
1125+
tgl_do_load_photo (TLS, M->media.photo, lua_file_cb, lua_ptr[p].ptr);
11161126
} else if (M->media.type == tgl_message_media_document) {
11171127
assert (M->media.document);
1118-
tgl_do_load_document (TLS, M->media.document, lua_file_cb, lua_ptr[p]);
1128+
tgl_do_load_document (TLS, M->media.document, lua_file_cb, lua_ptr[p].ptr);
11191129
} else {
1120-
tgl_do_load_encr_document (TLS, M->media.encr_document, lua_file_cb, lua_ptr[p]);
1130+
tgl_do_load_encr_document (TLS, M->media.encr_document, lua_file_cb, lua_ptr[p].ptr);
11211131
}
11221132
}
11231133
p += 2;
11241134
break;
11251135
case lq_load_video_thumb:
11261136
case lq_load_document_thumb:
1127-
M = lua_ptr[p + 1];
1137+
M = tgl_message_get (TLS, &lua_ptr[p + 1].msg_id);
11281138
if (!M || (M->media.type != tgl_message_media_document)) {
1129-
lua_file_cb (TLS, lua_ptr[p], 0, 0);
1139+
lua_file_cb (TLS, lua_ptr[p].ptr, 0, 0);
11301140
} else {
1131-
tgl_do_load_document_thumb (TLS, M->media.document, lua_file_cb, lua_ptr[p]);
1141+
tgl_do_load_document_thumb (TLS, M->media.document, lua_file_cb, lua_ptr[p].ptr);
11321142
}
11331143
p += 2;
11341144
break;
11351145
case lq_fwd:
1136-
tgl_do_forward_message (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, 0, lua_msg_cb, lua_ptr[p]);
1146+
tmp_msg_id = &lua_ptr[p + 2].msg_id;
1147+
tgl_do_forward_messages (TLS, lua_ptr[p + 1].peer_id, 1, &tmp_msg_id, 0, lua_msg_cb, lua_ptr[p].ptr);
11371148
p += 3;
11381149
break;
11391150
case lq_fwd_media:

‎lua-tg.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
void lua_init (const char *file);
2626
void lua_new_msg (struct tgl_message *M);
27-
void lua_our_id (int id);
27+
void lua_our_id (tgl_peer_id_t id);
2828
void lua_secret_chat_update (struct tgl_secret_chat *U, unsigned flags);
2929
void lua_user_update (struct tgl_user *U, unsigned flags);
3030
void lua_chat_update (struct tgl_chat *C, unsigned flags);

0 commit comments

Comments
 (0)