fixed json

This commit is contained in:
V V 2015-10-01 18:28:30 +03:00
parent 70b0aed3b3
commit 3bbd8e5255
3 changed files with 83 additions and 50 deletions

View File

@ -5,6 +5,7 @@
#include "json-tg.h" #include "json-tg.h"
#include <tgl/tgl.h> #include <tgl/tgl.h>
#include <tgl/tgl-layout.h> #include <tgl/tgl-layout.h>
#include "interface.h"
#include <assert.h> #include <assert.h>
//format time: //format time:
#include <time.h> #include <time.h>
@ -27,6 +28,9 @@ void json_pack_peer_type (json_t *res, tgl_peer_id_t id) {
case TGL_PEER_ENCR_CHAT: case TGL_PEER_ENCR_CHAT:
assert (json_object_set (res, "type", json_string ("encr_chat")) >= 0); assert (json_object_set (res, "type", json_string ("encr_chat")) >= 0);
break; break;
case TGL_PEER_CHANNEL:
assert (json_object_set (res, "type", json_string ("channel")) >= 0);
break;
default: default:
assert (0); assert (0);
} }
@ -77,6 +81,14 @@ void json_pack_chat (json_t *res, tgl_peer_t *P) {
} }
} }
void json_pack_channel (json_t *res, tgl_peer_t *P) {
assert (P->channel.title);
assert (json_object_set (res, "title", json_string (P->channel.title)) >= 0);
assert (json_object_set (res, "participants_count", json_integer (P->channel.participants_count)) >= 0);
assert (json_object_set (res, "admins_count", json_integer (P->channel.admins_count)) >= 0);
assert (json_object_set (res, "kicked_count", json_integer (P->channel.kicked_count)) >= 0);
}
void json_pack_encr_chat (json_t *res, tgl_peer_t *P) { void json_pack_encr_chat (json_t *res, tgl_peer_t *P) {
assert (json_object_set (res, "user", json_pack_peer (TGL_MK_USER (P->encr_chat.user_id))) >= 0); 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) {
tgl_peer_t *P = tgl_peer_get (TLS, id); tgl_peer_t *P = tgl_peer_get (TLS, id);
//assert (P); //assert (P);
json_t *res = json_object (); json_t *res = json_object ();
assert (json_object_set (res, "id", json_integer (tgl_get_peer_id (id))) >= 0); assert (json_object_set (res, "id", json_string (print_permanent_peer_id (id))) >= 0);
json_pack_peer_type (res, id); json_pack_peer_type (res, id);
@ -101,6 +113,9 @@ json_t *json_pack_peer (tgl_peer_id_t id) {
case TGL_PEER_CHAT: case TGL_PEER_CHAT:
sprintf (s, "chat#%d", tgl_get_peer_id (id)); sprintf (s, "chat#%d", tgl_get_peer_id (id));
break; break;
case TGL_PEER_CHANNEL:
sprintf (s, "channel#%d", tgl_get_peer_id (id));
break;
case TGL_PEER_ENCR_CHAT: case TGL_PEER_ENCR_CHAT:
sprintf (s, "encr_chat#%d", tgl_get_peer_id (id)); sprintf (s, "encr_chat#%d", tgl_get_peer_id (id));
break; break;
@ -128,6 +143,9 @@ json_t *json_pack_peer (tgl_peer_id_t id) {
case TGL_PEER_ENCR_CHAT: case TGL_PEER_ENCR_CHAT:
json_pack_encr_chat (res, P); json_pack_encr_chat (res, P);
break; break;
case TGL_PEER_CHANNEL:
json_pack_channel (res, P);
break;
default: default:
assert (0); assert (0);
} }
@ -377,6 +395,10 @@ json_t *json_pack_service (struct tgl_message *M) {
case tgl_message_action_abort_key: case tgl_message_action_abort_key:
assert (json_object_set (res, "type", json_string ("abort_key")) >= 0); assert (json_object_set (res, "type", json_string ("abort_key")) >= 0);
break; break;
case tgl_message_action_channel_create:
assert (json_object_set (res, "type", json_string ("channel_created")) >= 0);
assert (json_object_set (res, "title", json_string (M->action.title)) >= 0);
break;
default: default:
assert (json_object_set (res, "type", json_string ("???")) >= 0); assert (json_object_set (res, "type", json_string ("???")) >= 0);
break; break;
@ -389,7 +411,7 @@ json_t *json_pack_message (struct tgl_message *M) {
assert (json_object_set (res, "event", json_string ("message")) >= 0); assert (json_object_set (res, "event", json_string ("message")) >= 0);
//will overwriten to service, if service. //will overwriten to service, if service.
assert (json_object_set (res, "id", json_integer (M->id)) >= 0); assert (json_object_set (res, "id", json_string (print_permanent_msg_id (M->permanent_id))) >= 0);
if (!(M->flags & TGLMF_CREATED)) { return res; } if (!(M->flags & TGLMF_CREATED)) { return res; }
assert (json_object_set (res, "flags", json_integer (M->flags)) >= 0); assert (json_object_set (res, "flags", json_integer (M->flags)) >= 0);

105
lua-tg.c
View File

@ -42,6 +42,7 @@ lua_State *luaState;
//#include "interface.h" //#include "interface.h"
//#include "auto/constants.h" //#include "auto/constants.h"
#include <tgl/tgl.h> #include <tgl/tgl.h>
#include <tgl/tgl-queries.h>
#include "interface.h" #include "interface.h"
#include <assert.h> #include <assert.h>
@ -78,7 +79,7 @@ void lua_add_lstring_field (const char *name, const char *value, int len) {
if (!value || !len) { return; } if (!value || !len) { return; }
my_lua_checkstack (luaState, 3); my_lua_checkstack (luaState, 3);
lua_pushstring (luaState, name); lua_pushstring (luaState, name);
lua_pushlstring (luaState, len, value); lua_pushlstring (luaState, value, len);
lua_settable (luaState, -3); lua_settable (luaState, -3);
} }
@ -158,11 +159,11 @@ void push_encr_chat (tgl_peer_t *P) {
void push_channel (tgl_peer_t *P) { void push_channel (tgl_peer_t *P) {
my_lua_checkstack (luaState, 4); my_lua_checkstack (luaState, 4);
lua_add_string_field (luaState, "title", P->channel.title); lua_add_string_field ("title", P->channel.title);
lua_add_string_field (luaState, "about", P->channel.about); lua_add_string_field ("about", P->channel.about);
lua_add_num_field (luaState, "participants_count", P->channel.participants_count); lua_add_num_field ("participants_count", P->channel.participants_count);
lua_add_num_field (luaState, "admins_count", P->channel.admins_count); lua_add_num_field ("admins_count", P->channel.admins_count);
lua_add_num_field (luaState, "kicked_count", P->channel.kicked_count); lua_add_num_field ("kicked_count", P->channel.kicked_count);
} }
void push_update_types (unsigned flags) { void push_update_types (unsigned flags) {
@ -225,8 +226,10 @@ void push_update_types (unsigned flags) {
void push_peer (tgl_peer_id_t id, tgl_peer_t *P) { void push_peer (tgl_peer_id_t id, tgl_peer_t *P) {
lua_newtable (luaState); lua_newtable (luaState);
lua_add_lstring_field ("id", print_permanent_peer_id (P->id)); lua_add_string_field ("id", print_permanent_peer_id (P->id));
lua_add_string_field (luaState, "type", tgl_get_peer_type (id)); lua_pushstring (luaState, "type");
push_tgl_peer_type (tgl_get_peer_type (P->id));
lua_settable (luaState, -3);
if (!P || !(P->flags & TGLPF_CREATED)) { if (!P || !(P->flags & TGLPF_CREATED)) {
lua_pushstring (luaState, "print_name"); lua_pushstring (luaState, "print_name");
@ -441,9 +444,9 @@ void push_service (struct tgl_message *M) {
lua_add_string_field ("type", "channel_created"); lua_add_string_field ("type", "channel_created");
lua_add_string_field ("title", M->action.title); lua_add_string_field ("title", M->action.title);
break; break;
/*default: default:
lua_pushstring (luaState, "???"); lua_pushstring (luaState, "???");
break;*/ break;
} }
} }
@ -544,13 +547,13 @@ void lua_diff_end (void) {
} }
} }
void lua_our_id (int id) { void lua_our_id (tgl_peer_id_t id) {
if (!have_file) { return; } if (!have_file) { return; }
lua_settop (luaState, 0); lua_settop (luaState, 0);
//lua_checkstack (luaState, 20); //lua_checkstack (luaState, 20);
my_lua_checkstack (luaState, 20); my_lua_checkstack (luaState, 20);
lua_getglobal (luaState, "on_our_id"); lua_getglobal (luaState, "on_our_id");
lua_pushnumber (luaState, id); lua_pushnumber (luaState, tgl_get_peer_id (id));
assert (lua_gettop (luaState) == 2); assert (lua_gettop (luaState) == 2);
int r = ps_lua_pcall (luaState, 1, 0, 0); int r = ps_lua_pcall (luaState, 1, 0, 0);
@ -626,7 +629,19 @@ void lua_chat_update (struct tgl_chat *C, unsigned flags) {
//extern int peer_num; //extern int peer_num;
#define MAX_LUA_COMMANDS 1000 #define MAX_LUA_COMMANDS 1000
void *lua_ptr[MAX_LUA_COMMANDS];
struct lua_arg {
int flags;
union {
tgl_message_id_t msg_id;
tgl_peer_id_t peer_id;
char *str;
long long num;
double dval;
void *ptr;
};
};
struct lua_arg lua_ptr[MAX_LUA_COMMANDS];
static int pos; static int pos;
static inline tgl_peer_t *get_peer (const char *s) { 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
free (cb); free (cb);
} }
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[]) { 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[]) {
assert (TLSR == TLS); assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra; struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0); lua_settop (luaState, 0);
@ -1034,106 +1049,102 @@ void lua_str_cb (struct tgl_state *TLSR, void *cb_extra, int success, const char
free (cb); free (cb);
} }
#define LUA_STR_ARG(n) lua_ptr[n].str, strlen (lua_ptr[n].str)
void lua_do_all (void) { void lua_do_all (void) {
int p = 0; int p = 0;
while (p < pos) { while (p < pos) {
int l = (long)lua_ptr[p ++]; int l = lua_ptr[p ++].num;
assert (p + l + 1 <= pos); assert (p + l + 1 <= pos);
enum lua_query_type f = (long)lua_ptr[p ++]; enum lua_query_type f = lua_ptr[p ++].num;
struct tgl_message *M; struct tgl_message *M;
char *s, *s1, *s2, *s3; char *s, *s1, *s2, *s3;
int q = p;
tgl_message_id_t *tmp_msg_id;
switch (f) { switch (f) {
case lq_contact_list: case lq_contact_list:
tgl_do_update_contact_list (TLS, lua_contact_list_cb, lua_ptr[p ++]); tgl_do_update_contact_list (TLS, lua_contact_list_cb, lua_ptr[p ++].ptr);
break; break;
case lq_dialog_list: case lq_dialog_list:
tgl_do_get_dialog_list (TLS, 100, 0, lua_dialog_list_cb, lua_ptr[p ++]); tgl_do_get_dialog_list (TLS, 100, 0, lua_dialog_list_cb, lua_ptr[p ++].ptr);
break; break;
case lq_msg: case lq_msg:
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]); 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);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_typing: case lq_send_typing:
tgl_do_send_typing (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, tgl_typing_typing, lua_empty_cb, lua_ptr[p]); tgl_do_send_typing (TLS, lua_ptr[p + 1].peer_id, tgl_typing_typing, lua_empty_cb, lua_ptr[p].ptr);
p += 2; p += 2;
break; break;
case lq_send_typing_abort: case lq_send_typing_abort:
tgl_do_send_typing (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, tgl_typing_cancel, lua_empty_cb, lua_ptr[p]); tgl_do_send_typing (TLS, lua_ptr[p + 1].peer_id, tgl_typing_cancel, lua_empty_cb, lua_ptr[p].ptr);
p += 2; p += 2;
break; break;
case lq_rename_chat: case lq_rename_chat:
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]); tgl_do_rename_chat (TLS, lua_ptr[p + 1].peer_id, LUA_STR_ARG (p + 2), lua_empty_cb, lua_ptr[p].ptr);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_photo: case lq_send_photo:
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]); 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);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_video: case lq_send_video:
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]); 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);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_audio: case lq_send_audio:
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]); 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);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_document: case lq_send_document:
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]); 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);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_file: case lq_send_file:
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]); 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);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_send_text: case lq_send_text:
tgl_do_send_text (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], 0, lua_msg_cb, lua_ptr[p]); tgl_do_send_text (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, 0, lua_msg_cb, lua_ptr[p].ptr);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_chat_set_photo: case lq_chat_set_photo:
tgl_do_set_chat_photo (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_empty_cb, lua_ptr[p]); tgl_do_set_chat_photo (TLS, lua_ptr[p + 1].peer_id, lua_ptr[p + 2].str, lua_empty_cb, lua_ptr[p].ptr);
free (lua_ptr[p + 2]);
p += 3; p += 3;
break; break;
case lq_load_photo: case lq_load_photo:
case lq_load_video: case lq_load_video:
case lq_load_audio: case lq_load_audio:
case lq_load_document: case lq_load_document:
M = lua_ptr[p + 1]; M = tgl_message_get (TLS, &lua_ptr[p + 1].msg_id);
if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) { if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) {
lua_file_cb (TLS, lua_ptr[p], 0, 0); lua_file_cb (TLS, lua_ptr[p].ptr, 0, 0);
} else { } else {
if (M->media.type == tgl_message_media_photo) { if (M->media.type == tgl_message_media_photo) {
assert (M->media.photo); assert (M->media.photo);
tgl_do_load_photo (TLS, M->media.photo, lua_file_cb, lua_ptr[p]); tgl_do_load_photo (TLS, M->media.photo, lua_file_cb, lua_ptr[p].ptr);
} else if (M->media.type == tgl_message_media_document) { } else if (M->media.type == tgl_message_media_document) {
assert (M->media.document); assert (M->media.document);
tgl_do_load_document (TLS, M->media.document, lua_file_cb, lua_ptr[p]); tgl_do_load_document (TLS, M->media.document, lua_file_cb, lua_ptr[p].ptr);
} else { } else {
tgl_do_load_encr_document (TLS, M->media.encr_document, lua_file_cb, lua_ptr[p]); tgl_do_load_encr_document (TLS, M->media.encr_document, lua_file_cb, lua_ptr[p].ptr);
} }
} }
p += 2; p += 2;
break; break;
case lq_load_video_thumb: case lq_load_video_thumb:
case lq_load_document_thumb: case lq_load_document_thumb:
M = lua_ptr[p + 1]; M = tgl_message_get (TLS, &lua_ptr[p + 1].msg_id);
if (!M || (M->media.type != tgl_message_media_document)) { if (!M || (M->media.type != tgl_message_media_document)) {
lua_file_cb (TLS, lua_ptr[p], 0, 0); lua_file_cb (TLS, lua_ptr[p].ptr, 0, 0);
} else { } else {
tgl_do_load_document_thumb (TLS, M->media.document, lua_file_cb, lua_ptr[p]); tgl_do_load_document_thumb (TLS, M->media.document, lua_file_cb, lua_ptr[p].ptr);
} }
p += 2; p += 2;
break; break;
case lq_fwd: case lq_fwd:
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]); tmp_msg_id = &lua_ptr[p + 2].msg_id;
tgl_do_forward_messages (TLS, lua_ptr[p + 1].peer_id, 1, &tmp_msg_id, 0, lua_msg_cb, lua_ptr[p].ptr);
p += 3; p += 3;
break; break;
case lq_fwd_media: case lq_fwd_media:

View File

@ -24,7 +24,7 @@
void lua_init (const char *file); void lua_init (const char *file);
void lua_new_msg (struct tgl_message *M); void lua_new_msg (struct tgl_message *M);
void lua_our_id (int id); void lua_our_id (tgl_peer_id_t id);
void lua_secret_chat_update (struct tgl_secret_chat *U, unsigned flags); void lua_secret_chat_update (struct tgl_secret_chat *U, unsigned flags);
void lua_user_update (struct tgl_user *U, unsigned flags); void lua_user_update (struct tgl_user *U, unsigned flags);
void lua_chat_update (struct tgl_chat *C, unsigned flags); void lua_chat_update (struct tgl_chat *C, unsigned flags);