Refactored code a bit to better separation of library. Library can now be compiled without libevent.

This commit is contained in:
Vysheng 2014-10-24 21:43:08 +04:00
parent c01eb11296
commit 56fe7bed54
28 changed files with 2634 additions and 2430 deletions

View File

@ -21,7 +21,7 @@ EXE_LIST=${EXE}/generate ${EXE}/tlc ${EXE}/telegram-cli
LIB_LIST=${LIB}/libtgl.a
TG_OBJECTS=${OBJ}/main.o ${OBJ}/loop.o ${OBJ}/interface.o ${OBJ}/lua-tg.o
TGL_OBJECTS=${OBJ}/net.o ${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/auto/auto.o ${OBJ}/tgl.o ${OBJ}/updates.o
TGL_OBJECTS=${OBJ}/net.o ${OBJ}/mtproto-common.o ${OBJ}/mtproto-client.o ${OBJ}/queries.o ${OBJ}/structures.o ${OBJ}/binlog.o ${OBJ}/auto/auto.o ${OBJ}/tgl.o ${OBJ}/updates.o ${OBJ}/tgl-timers.o
TLC_OBJECTS=${OBJ}/tlc.o ${OBJ}/tl-parser.o ${OBJ}/crc32.o
TLD_OBJECTS=${OBJ}/dump-tl-file.o
GENERATE_OBJECTS=${OBJ}/generate.o

View File

@ -303,7 +303,7 @@ static void free_vars_to_be_freed (void) {
fvars_pos = 0;
}
int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, char *data, int data_len) {
int tglf_extf_autocomplete (struct tgl_state *TLS, const char *text, int text_len, int index, char **R, char *data, int data_len) {
if (index == -1) {
buffer_pos = data;
buffer_end = data + data_len;
@ -330,7 +330,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R,
}
}
struct paramed_type *tglf_extf_store (const char *data, int data_len) {
struct paramed_type *tglf_extf_store (struct tgl_state *TLS, const char *data, int data_len) {
buffer_pos = (char *)data;
buffer_end = (char *)(data + data_len);
local_next_token ();
@ -432,7 +432,7 @@ static void print_offset (void) {
}
}
char *tglf_extf_fetch (struct paramed_type *T) {
char *tglf_extf_fetch (struct tgl_state *TLS, struct paramed_type *T) {
out_buf_pos = 0;
if (fetch_type_any (T) < 0) { return 0; }
return out_buf;

978
binlog.c

File diff suppressed because it is too large Load Diff

130
binlog.h
View File

@ -23,80 +23,80 @@
//#include "structures.h"
#include "tgl.h"
void bl_do_set_auth_key_id (int num, unsigned char *buf);
void bl_do_set_auth_key_id (struct tgl_state *TLS, int num, unsigned char *buf);
void bl_do_dc_option (int id, int l1, const char *name, int l2, const char *ip, int port);
void bl_do_dc_option (struct tgl_state *TLS, int id, int l1, const char *name, int l2, const char *ip, int port);
void bl_do_set_our_id (int id);
void bl_do_user_add (int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact);
void bl_do_user_delete (struct tgl_user *U);
void bl_do_set_user_profile_photo (struct tgl_user *U, long long photo_id, struct tgl_file_location *big, struct tgl_file_location *small);
void bl_do_user_set_name (struct tgl_user *U, const char *f, int fl, const char *l, int ll);
void bl_do_user_set_username (struct tgl_user *U, const char *f, int l);
void bl_do_user_set_access_hash (struct tgl_user *U, long long access_token);
void bl_do_user_set_phone (struct tgl_user *U, const char *p, int pl);
void bl_do_user_set_friend (struct tgl_user *U, int friend);
void bl_do_user_set_full_photo (struct tgl_user *U, const int *start, int len);
void bl_do_user_set_blocked (struct tgl_user *U, int blocked);
void bl_do_user_set_real_name (struct tgl_user *U, const char *f, int fl, const char *l, int ll);
void bl_do_set_our_id (struct tgl_state *TLS, int id);
void bl_do_user_add (struct tgl_state *TLS, int id, const char *f, int fl, const char *l, int ll, long long access_token, const char *p, int pl, int contact);
void bl_do_user_delete (struct tgl_state *TLS, struct tgl_user *U);
void bl_do_set_user_profile_photo (struct tgl_state *TLS, struct tgl_user *U, long long photo_id, struct tgl_file_location *big, struct tgl_file_location *small);
void bl_do_user_set_name (struct tgl_state *TLS, struct tgl_user *U, const char *f, int fl, const char *l, int ll);
void bl_do_user_set_username (struct tgl_state *TLS, struct tgl_user *U, const char *f, int l);
void bl_do_user_set_access_hash (struct tgl_state *TLS, struct tgl_user *U, long long access_token);
void bl_do_user_set_phone (struct tgl_state *TLS, struct tgl_user *U, const char *p, int pl);
void bl_do_user_set_friend (struct tgl_state *TLS, struct tgl_user *U, int friend);
void bl_do_user_set_full_photo (struct tgl_state *TLS, struct tgl_user *U, const int *start, int len);
void bl_do_user_set_blocked (struct tgl_state *TLS, struct tgl_user *U, int blocked);
void bl_do_user_set_real_name (struct tgl_state *TLS, struct tgl_user *U, const char *f, int fl, const char *l, int ll);
void bl_do_encr_chat_delete (struct tgl_secret_chat *U);
void bl_do_encr_chat_requested (struct tgl_secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]);
void bl_do_encr_chat_create (int id, int user_id, int admin_id, char *name, int name_len);
void bl_do_encr_chat_set_access_hash (struct tgl_secret_chat *U, long long access_hash);
void bl_do_encr_chat_set_date (struct tgl_secret_chat *U, int date);
void bl_do_encr_chat_set_state (struct tgl_secret_chat *U, enum tgl_secret_chat_state state);
void bl_do_encr_chat_set_ttl (struct tgl_secret_chat *U, int ttl);
void bl_do_encr_chat_set_layer (struct tgl_secret_chat *U, int layer);
void bl_do_encr_chat_accepted (struct tgl_secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint);
void bl_do_encr_chat_set_key (struct tgl_secret_chat *E, unsigned char key[], long long key_fingerprint);
void bl_do_encr_chat_init (int id, int user_id, unsigned char random[], unsigned char g_a[]);
void bl_do_encr_chat_update_seq (struct tgl_secret_chat *E, int in_seq_no, int out_seq_no);
void bl_do_encr_chat_set_seq (struct tgl_secret_chat *E, int in_seq_no, int last_in_seq_no, int out_seq_no);
void bl_do_encr_chat_delete (struct tgl_state *TLS, struct tgl_secret_chat *U);
void bl_do_encr_chat_requested (struct tgl_state *TLS, struct tgl_secret_chat *U, long long access_hash, int date, int admin_id, int user_id, unsigned char g_key[], unsigned char nonce[]);
void bl_do_encr_chat_create (struct tgl_state *TLS, int id, int user_id, int admin_id, char *name, int name_len);
void bl_do_encr_chat_set_access_hash (struct tgl_state *TLS, struct tgl_secret_chat *U, long long access_hash);
void bl_do_encr_chat_set_date (struct tgl_state *TLS, struct tgl_secret_chat *U, int date);
void bl_do_encr_chat_set_state (struct tgl_state *TLS, struct tgl_secret_chat *U, enum tgl_secret_chat_state state);
void bl_do_encr_chat_set_ttl (struct tgl_state *TLS, struct tgl_secret_chat *U, int ttl);
void bl_do_encr_chat_set_layer (struct tgl_state *TLS, struct tgl_secret_chat *U, int layer);
void bl_do_encr_chat_accepted (struct tgl_state *TLS, struct tgl_secret_chat *U, const unsigned char g_key[], const unsigned char nonce[], long long key_fingerprint);
void bl_do_encr_chat_set_key (struct tgl_state *TLS, struct tgl_secret_chat *E, unsigned char key[], long long key_fingerprint);
void bl_do_encr_chat_init (struct tgl_state *TLS, int id, int user_id, unsigned char random[], unsigned char g_a[]);
void bl_do_encr_chat_update_seq (struct tgl_state *TLS, struct tgl_secret_chat *E, int in_seq_no, int out_seq_no);
void bl_do_encr_chat_set_seq (struct tgl_state *TLS, struct tgl_secret_chat *E, int in_seq_no, int last_in_seq_no, int out_seq_no);
void bl_do_dc_signed (int id);
void bl_do_set_working_dc (int num);
void bl_do_set_dh_params (int root, unsigned char prime[], int version);
void bl_do_dc_signed (struct tgl_state *TLS, int id);
void bl_do_set_working_dc (struct tgl_state *TLS, int num);
void bl_do_set_dh_params (struct tgl_state *TLS, int root, unsigned char prime[], int version);
void bl_do_set_pts (int pts);
void bl_do_set_qts (int qts);
void bl_do_set_seq (int seq);
void bl_do_set_date (int date);
void bl_do_set_pts (struct tgl_state *TLS, int pts);
void bl_do_set_qts (struct tgl_state *TLS, int qts);
void bl_do_set_seq (struct tgl_state *TLS, int seq);
void bl_do_set_date (struct tgl_state *TLS, int date);
void bl_do_create_chat (struct tgl_chat *C, int y, const char *s, int l, int users_num, int date, int version, struct tgl_file_location *big, struct tgl_file_location *small);
void bl_do_chat_forbid (struct tgl_chat *C, int on);
void bl_do_chat_set_title (struct tgl_chat *C, const char *s, int l);
void bl_do_chat_set_photo (struct tgl_chat *C, struct tgl_file_location *big, struct tgl_file_location *small);
void bl_do_chat_set_date (struct tgl_chat *C, int date);
void bl_do_chat_set_set_in_chat (struct tgl_chat *C, int on);
void bl_do_chat_set_version (struct tgl_chat *C, int version, int user_num);
void bl_do_chat_set_admin (struct tgl_chat *C, int admin);
void bl_do_chat_set_participants (struct tgl_chat *C, int version, int user_num, struct tgl_chat_user *users);
void bl_do_chat_set_full_photo (struct tgl_chat *U, const int *start, int len);
void bl_do_chat_add_user (struct tgl_chat *C, int version, int user, int inviter, int date);
void bl_do_chat_del_user (struct tgl_chat *C, int version, int user);
void bl_do_create_chat (struct tgl_state *TLS, struct tgl_chat *C, int y, const char *s, int l, int users_num, int date, int version, struct tgl_file_location *big, struct tgl_file_location *small);
void bl_do_chat_forbid (struct tgl_state *TLS, struct tgl_chat *C, int on);
void bl_do_chat_set_title (struct tgl_state *TLS, struct tgl_chat *C, const char *s, int l);
void bl_do_chat_set_photo (struct tgl_state *TLS, struct tgl_chat *C, struct tgl_file_location *big, struct tgl_file_location *small);
void bl_do_chat_set_date (struct tgl_state *TLS, struct tgl_chat *C, int date);
void bl_do_chat_set_set_in_chat (struct tgl_state *TLS, struct tgl_chat *C, int on);
void bl_do_chat_set_version (struct tgl_state *TLS, struct tgl_chat *C, int version, int user_num);
void bl_do_chat_set_admin (struct tgl_state *TLS, struct tgl_chat *C, int admin);
void bl_do_chat_set_participants (struct tgl_state *TLS, struct tgl_chat *C, int version, int user_num, struct tgl_chat_user *users);
void bl_do_chat_set_full_photo (struct tgl_state *TLS, struct tgl_chat *U, const int *start, int len);
void bl_do_chat_add_user (struct tgl_state *TLS, struct tgl_chat *C, int version, int user, int inviter, int date);
void bl_do_chat_del_user (struct tgl_state *TLS, struct tgl_chat *C, int version, int user);
void bl_do_create_message_text (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s);
void bl_do_create_message_text_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s);
void bl_do_create_message_service (int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len);
void bl_do_create_message_service_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len);
void bl_do_create_message_media (int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr_pending (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr_sent (long long msg_id, const int *data, int len);
void bl_do_create_message_media_fwd (int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2);
void bl_do_create_message_service_encr (long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len);
void bl_do_send_message_text (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s);
void bl_do_send_message_action_encr (long long msg_id, int from_id, int to_type, int to_id, int date, int l, const int *s);
void bl_do_set_unread (struct tgl_message *M, int unread);
void bl_do_set_message_sent (struct tgl_message *M);
void bl_do_set_msg_id (struct tgl_message *M, int id);
void bl_do_delete_msg (struct tgl_message *M);
void bl_do_create_message_text (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s);
void bl_do_create_message_text_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s);
void bl_do_create_message_service (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, const int *data, int len);
void bl_do_create_message_service_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, const int *data, int len);
void bl_do_create_message_media (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int unread, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr_pending (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr_sent (struct tgl_state *TLS, long long msg_id, const int *data, int len);
void bl_do_create_message_media_fwd (struct tgl_state *TLS, int msg_id, int from_id, int to_type, int to_id, int date, int fwd, int fwd_date, int unread, int l, const char *s, const int *data, int len);
void bl_do_create_message_media_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s, const int *data, int len, const int *data2, int len2);
void bl_do_create_message_service_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, const int *data, int len);
void bl_do_send_message_text (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const char *s);
void bl_do_send_message_action_encr (struct tgl_state *TLS, long long msg_id, int from_id, int to_type, int to_id, int date, int l, const int *s);
void bl_do_set_unread (struct tgl_state *TLS, struct tgl_message *M, int unread);
void bl_do_set_message_sent (struct tgl_state *TLS, struct tgl_message *M);
void bl_do_set_msg_id (struct tgl_state *TLS, struct tgl_message *M, int id);
void bl_do_delete_msg (struct tgl_state *TLS, struct tgl_message *M);
void bl_do_msg_seq_update (long long id);
void bl_do_msg_update (long long id);
void bl_do_msg_seq_update (struct tgl_state *TLS, long long id);
void bl_do_msg_update (struct tgl_state *TLS, long long id);
void bl_do_reset_authorization (void);
void bl_do_reset_authorization (struct tgl_state *TLS);
//void bl_do_add_dc (int id, const char *ip, int l, int port, long long auth_key_id, const char *auth_key);
#endif

View File

@ -105,6 +105,8 @@ extern int usfd;
extern int sfd;
extern int use_ids;
extern struct tgl_state *TLS;
int is_same_word (const char *s, size_t l, const char *word) {
return s && word && strlen (word) == l && !memcmp (s, word, l);
}
@ -335,7 +337,7 @@ tgl_peer_id_t cur_token_user (void) {
else { return TGL_PEER_NOT_FOUND; }
}
tgl_peer_t *P = tgl_peer_get_by_name (s);
tgl_peer_t *P = tgl_peer_get_by_name (TLS, s);
cur_token[cur_token_len] = c;
if (P && tgl_get_peer_type (P->id) == TGL_PEER_USER) {
@ -370,7 +372,7 @@ tgl_peer_id_t cur_token_chat (void) {
else { return TGL_PEER_NOT_FOUND; }
}
tgl_peer_t *P = tgl_peer_get_by_name (s);
tgl_peer_t *P = tgl_peer_get_by_name (TLS, s);
cur_token[cur_token_len] = c;
if (P && tgl_get_peer_type (P->id) == TGL_PEER_CHAT) {
@ -386,7 +388,7 @@ tgl_peer_id_t cur_token_encr_chat (void) {
char c = cur_token[cur_token_len];
cur_token[cur_token_len] = 0;
tgl_peer_t *P = tgl_peer_get_by_name (s);
tgl_peer_t *P = tgl_peer_get_by_name (TLS, s);
cur_token[cur_token_len] = c;
if (P && tgl_get_peer_type (P->id) == TGL_PEER_ENCR_CHAT) {
@ -436,7 +438,7 @@ tgl_peer_id_t cur_token_peer (void) {
else { return TGL_PEER_NOT_FOUND; }
}
tgl_peer_t *P = tgl_peer_get_by_name (s);
tgl_peer_t *P = tgl_peer_get_by_name (TLS, s);
cur_token[cur_token_len] = c;
if (P) {
@ -448,15 +450,15 @@ tgl_peer_id_t cur_token_peer (void) {
static tgl_peer_t *mk_peer (tgl_peer_id_t id) {
if (tgl_get_peer_type (id) == NOT_FOUND) { return 0; }
tgl_peer_t *P = tgl_peer_get (id);
tgl_peer_t *P = tgl_peer_get (TLS, id);
if (!P) {
if (tgl_get_peer_type (id) == TGL_PEER_USER) {
tgl_insert_empty_user (tgl_get_peer_id (id));
tgl_insert_empty_user (TLS, tgl_get_peer_id (id));
}
if (tgl_get_peer_type (id) == TGL_PEER_CHAT) {
tgl_insert_empty_chat (tgl_get_peer_id (id));
tgl_insert_empty_chat (TLS, tgl_get_peer_id (id));
}
P = tgl_peer_get (id);
P = tgl_peer_get (TLS, id);
}
return P;
}
@ -465,26 +467,26 @@ char *get_default_prompt (void) {
static char buf[1000];
int l = 0;
if (in_chat_mode) {
tgl_peer_t *U = tgl_peer_get (chat_mode_id);
tgl_peer_t *U = tgl_peer_get (TLS, chat_mode_id);
assert (U && U->print_name);
l += snprintf (buf + l, 999 - l, COLOR_RED "%.*s " COLOR_NORMAL, 100, U->print_name);
}
if (tgl_state.unread_messages || tgl_state.cur_uploading_bytes || tgl_state.cur_downloading_bytes) {
if (TLS->unread_messages || TLS->cur_uploading_bytes || TLS->cur_downloading_bytes) {
l += snprintf (buf + l, 999 - l, COLOR_RED "[");
int ok = 0;
if (tgl_state.unread_messages) {
l += snprintf (buf + l, 999 - l, "%d unread", tgl_state.unread_messages);
if (TLS->unread_messages) {
l += snprintf (buf + l, 999 - l, "%d unread", TLS->unread_messages);
ok = 1;
}
if (tgl_state.cur_uploading_bytes) {
if (TLS->cur_uploading_bytes) {
if (ok) { *(buf + l) = ' '; l ++; }
ok = 1;
l += snprintf (buf + l, 999 - l, "%lld%%Up", 100 * tgl_state.cur_uploaded_bytes / tgl_state.cur_uploading_bytes);
l += snprintf (buf + l, 999 - l, "%lld%%Up", 100 * TLS->cur_uploaded_bytes / TLS->cur_uploading_bytes);
}
if (tgl_state.cur_downloading_bytes) {
if (TLS->cur_downloading_bytes) {
if (ok) { *(buf + l) = ' '; l ++; }
ok = 1;
l += snprintf (buf + l, 999 - l, "%lld%%Down", 100 * tgl_state.cur_downloaded_bytes / tgl_state.cur_downloading_bytes);
l += snprintf (buf + l, 999 - l, "%lld%%Down", 100 * TLS->cur_downloaded_bytes / TLS->cur_downloading_bytes);
}
l += snprintf (buf + l, 999 - l, "]" COLOR_NORMAL);
l += snprintf (buf + l, 999 - l, "%s", default_prompt);
@ -568,17 +570,20 @@ struct command {
int offline_mode;
void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[]);
void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *ML[]);
void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]);
void print_chat_info_gw (void *extra, int success, struct tgl_chat *C);
void print_user_info_gw (void *extra, int success, struct tgl_user *C);
void print_filename_gw (void *extra, int success, char *name);
void open_filename_gw (void *extra, int success, char *name);
void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E);
void print_card_gw (void *extra, int success, int size, int *card);
void print_user_gw (void *extra, int success, struct tgl_user *U);
void print_msg_gw (void *extra, int success, struct tgl_message *M);
void print_user_list_gw (struct tgl_state *TLS, void *extra, int success, int num, struct tgl_user *UL[]);
void print_msg_list_gw (struct tgl_state *TLS, void *extra, int success, int num, struct tgl_message *ML[]);
void print_dialog_list_gw (struct tgl_state *TLS, void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]);
void print_chat_info_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_chat *C);
void print_user_info_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_user *C);
void print_filename_gw (struct tgl_state *TLS, void *extra, int success, char *name);
void open_filename_gw (struct tgl_state *TLS, void *extra, int success, char *name);
void print_secret_chat_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E);
void print_card_gw (struct tgl_state *TLS, void *extra, int success, int size, int *card);
void print_user_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_user *U);
void print_msg_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M);
void print_msg_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M);
void print_encr_chat_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E);;
void print_success_gw (struct tgl_state *TLS, void *extra, int success);
struct command commands[];
void do_help (int arg_num, struct arg args[], struct in_ev *ev) {
@ -600,13 +605,13 @@ void do_help (int arg_num, struct arg args[], struct in_ev *ev) {
void do_contact_list (int arg_num, struct arg args[], struct in_ev *ev) {
assert (!arg_num);
if (ev) { ev->refcnt ++; }
tgl_do_update_contact_list (print_user_list_gw, ev);
tgl_do_update_contact_list (TLS, print_user_list_gw, ev);
}
void do_stats (int arg_num, struct arg args[], struct in_ev *ev) {
assert (!arg_num);
static char stat_buf[1 << 15];
tgl_print_stat (stat_buf, (1 << 15) - 1);
tgl_print_stat (TLS, stat_buf, (1 << 15) - 1);
if (ev) { mprint_start (ev); }
mprintf (ev, "%s\n", stat_buf);
if (ev) { mprint_end (ev); }
@ -618,92 +623,101 @@ void do_stats (int arg_num, struct arg args[], struct in_ev *ev) {
void do_history (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 3);
if (ev) { ev->refcnt ++; }
tgl_do_get_history_ext (args[0].P->id, args[2].num != NOT_FOUND ? args[2].num : 0, args[1].num != NOT_FOUND ? args[1].num : 40, offline_mode, print_msg_list_gw, ev);
tgl_do_get_history_ext (TLS, args[0].P->id, args[2].num != NOT_FOUND ? args[2].num : 0, args[1].num != NOT_FOUND ? args[1].num : 40, offline_mode, print_msg_list_gw, ev);
}
void do_dialog_list (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 0);
if (ev) { ev->refcnt ++; }
tgl_do_get_dialog_list (print_dialog_list_gw, ev);
tgl_do_get_dialog_list (TLS, print_dialog_list_gw, ev);
}
void do_send_photo (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_send_photo (tgl_message_media_photo, args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_photo (TLS, tgl_message_media_photo, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
void do_send_audio (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_send_photo (tgl_message_media_audio, args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_photo (TLS, tgl_message_media_audio, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
void do_send_video (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_send_photo (tgl_message_media_video, args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_photo (TLS, tgl_message_media_video, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
void do_send_document (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_send_photo (tgl_message_media_document, args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_photo (TLS, tgl_message_media_document, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
void do_send_text (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_send_text (args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_text (TLS, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
void do_chat_info (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
if (ev) { ev->refcnt ++; }
tgl_do_get_chat_info (args[0].P->id, offline_mode, print_chat_info_gw, ev);
tgl_do_get_chat_info (TLS, args[0].P->id, offline_mode, print_chat_info_gw, ev);
}
void do_user_info (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
if (ev) { ev->refcnt ++; }
tgl_do_get_user_info (args[0].P->id, offline_mode, print_user_info_gw, ev);
tgl_do_get_user_info (TLS, args[0].P->id, offline_mode, print_user_info_gw, ev);
}
void do_fwd (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_forward_message (args[0].P->id, args[1].num, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_forward_message (TLS, args[0].P->id, args[1].num, print_msg_success_gw, ev);
}
void do_fwd_media (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_forward_message (args[0].P->id, args[1].num, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_forward_message (TLS, args[0].P->id, args[1].num, print_msg_success_gw, ev);
}
void do_msg (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_send_message (args[0].P->id, args[1].str, strlen (args[1].str), 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_message (TLS, args[0].P->id, args[1].str, strlen (args[1].str), print_msg_success_gw, ev);
}
void do_rename_chat (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_rename_chat (args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_rename_chat (TLS, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
#define DO_LOAD_PHOTO(tp,act,actf) \
void do_ ## act ## _ ## tp (int arg_num, struct arg args[], struct in_ev *ev) { \
assert (arg_num == 1);\
struct tgl_message *M = tgl_message_get (args[0].num);\
struct tgl_message *M = tgl_message_get (TLS, args[0].num);\
if (M && !M->service && M->media.type == tgl_message_media_ ## tp) {\
if (ev) { ev->refcnt ++; } \
tgl_do_load_ ## tp (&M->media.tp, actf, ev); \
tgl_do_load_ ## tp (TLS, &M->media.tp, actf, ev); \
} else if (M && !M->service && M->media.type == tgl_message_media_ ## tp ## _encr) { \
if (ev) { ev->refcnt ++; } \
tgl_do_load_encr_video (&M->media.encr_video, actf, ev); \
tgl_do_load_encr_video (TLS, &M->media.encr_video, actf, ev); \
} \
}
#define DO_LOAD_PHOTO_THUMB(tp,act,actf) \
void do_ ## act ## _ ## tp ## _thumb (int arg_num, struct arg args[], struct in_ev *ev) { \
assert (arg_num == 1);\
struct tgl_message *M = tgl_message_get (args[0].num);\
struct tgl_message *M = tgl_message_get (TLS, args[0].num);\
if (M && !M->service && M->media.type == tgl_message_media_ ## tp) { \
if (ev) { ev->refcnt ++; } \
tgl_do_load_ ## tp ## _thumb (&M->media.tp, actf, ev); \
tgl_do_load_ ## tp ## _thumb (TLS, &M->media.tp, actf, ev); \
}\
}
@ -723,19 +737,23 @@ DO_LOAD_PHOTO_THUMB(document, open, open_filename_gw)
void do_add_contact (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 3);
if (ev) { ev->refcnt ++; }
tgl_do_add_contact (args[0].str, strlen (args[0].str), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev);
tgl_do_add_contact (TLS, args[0].str, strlen (args[0].str), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev);
}
void do_del_contact (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
tgl_do_del_contact (args[0].P->id, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_del_contact (TLS, args[0].P->id, print_success_gw, ev);
}
void do_rename_contact (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 3);
if (args[0].P->user.phone) {
if (ev) { ev->refcnt ++; }
tgl_do_add_contact (args[0].P->user.phone, strlen (args[0].P->user.phone), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev);
tgl_do_add_contact (TLS, args[0].P->user.phone, strlen (args[0].P->user.phone), args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), 0, print_user_list_gw, ev);
} else {
if (ev) { ev->refcnt ++; }
print_success_gw (TLS, ev, 0);
}
}
@ -782,12 +800,13 @@ void do_search (int arg_num, struct arg args[], struct in_ev *ev) {
offset = 0;
}
if (ev) { ev->refcnt ++; }
tgl_do_msg_search (id, from, to, limit, offset, args[5].str, print_msg_list_gw, ev);
tgl_do_msg_search (TLS, id, from, to, limit, offset, args[5].str, print_msg_list_gw, ev);
}
void do_mark_read (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
tgl_do_mark_read (args[0].P->id, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_mark_read (TLS, args[0].P->id, print_success_gw, ev);
}
void do_visualize_key (int arg_num, struct arg args[], struct in_ev *ev) {
@ -796,7 +815,7 @@ void do_visualize_key (int arg_num, struct arg args[], struct in_ev *ev) {
static unsigned char buf[16];
memset (buf, 0, sizeof (buf));
tgl_peer_id_t id = args[0].P->id;
tgl_do_visualize_key (id, buf);
tgl_do_visualize_key (TLS, id, buf);
mprint_start (ev);
int i;
for (i = 0; i < 16; i++) {
@ -841,27 +860,31 @@ void do_visualize_key (int arg_num, struct arg args[], struct in_ev *ev) {
void do_create_secret_chat (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
if (ev) { ev->refcnt ++; }
tgl_do_create_secret_chat (args[0].P->id, print_secret_chat_gw, ev);
tgl_do_create_secret_chat (TLS, args[0].P->id, print_secret_chat_gw, ev);
}
void do_chat_add_user (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 3);
tgl_do_add_user_to_chat (args[0].P->id, args[1].P->id, args[2].num != NOT_FOUND ? args[2].num : 100, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_add_user_to_chat (TLS, args[0].P->id, args[1].P->id, args[2].num != NOT_FOUND ? args[2].num : 100, print_msg_success_gw, ev);
}
void do_chat_del_user (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_del_user_from_chat (args[0].P->id, args[1].P->id, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_del_user_from_chat (TLS, args[0].P->id, args[1].P->id, print_msg_success_gw, ev);
}
void do_status_online (int arg_num, struct arg args[], struct in_ev *ev) {
assert (!arg_num);
tgl_do_update_status (1, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_update_status (TLS, 1, print_success_gw, ev);
}
void do_status_offline (int arg_num, struct arg args[], struct in_ev *ev) {
assert (!arg_num);
tgl_do_update_status (0, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_update_status (TLS, 0, print_success_gw, ev);
}
void do_quit (int arg_num, struct arg args[], struct in_ev *ev) {
@ -875,7 +898,7 @@ void do_safe_quit (int arg_num, struct arg args[], struct in_ev *ev) {
void do_set (int arg_num, struct arg args[], struct in_ev *ev) {
int num = args[1].num;
if (!strcmp (args[0].str, "debug_verbosity")) {
tgl_set_verbosity (num);
tgl_set_verbosity (TLS, num);
} else if (!strcmp (args[0].str, "log_level")) {
log_level = num;
} else if (!strcmp (args[0].str, "msg_num")) {
@ -893,11 +916,13 @@ void do_chat_with_peer (int arg_num, struct arg args[], struct in_ev *ev) {
}
void do_delete_msg (int arg_num, struct arg args[], struct in_ev *ev) {
tgl_do_delete_msg (args[0].num, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_delete_msg (TLS, args[0].num, print_success_gw, ev);
}
void do_restore_msg (int arg_num, struct arg args[], struct in_ev *ev) {
tgl_do_restore_msg (args[0].num, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_restore_msg (TLS, args[0].num, print_success_gw, ev);
}
void do_create_group_chat (int arg_num, struct arg args[], struct in_ev *ev) {
@ -908,50 +933,61 @@ void do_create_group_chat (int arg_num, struct arg args[], struct in_ev *ev) {
ids[i] = args[i + 1].P->id;
}
tgl_do_create_group_chat_ex (arg_num - 1, ids, args[0].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_create_group_chat_ex (TLS, arg_num - 1, ids, args[0].str, print_msg_success_gw, ev);
}
void do_chat_set_photo (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_set_chat_photo (args[0].P->id, args[1].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_set_chat_photo (TLS, args[0].P->id, args[1].str, print_msg_success_gw, ev);
}
void do_set_profile_photo (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
tgl_do_set_profile_photo (args[0].str, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_set_profile_photo (TLS, args[0].str, print_success_gw, ev);
}
void do_set_profile_name (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_set_profile_name (args[0].str, args[1].str, print_user_gw, ev);
if (ev) { ev->refcnt ++; }
tgl_do_set_profile_name (TLS, args[0].str, args[1].str, print_user_gw, ev);
}
void do_set_username (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
tgl_do_set_username (args[0].str, print_user_gw, ev);
if (ev) { ev->refcnt ++; }
tgl_do_set_username (TLS, args[0].str, print_user_gw, ev);
}
void do_contact_search (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
tgl_do_contact_search (args[0].str, args[1].num == NOT_FOUND ? args[1].num : 10, print_user_list_gw, ev);
if (ev) { ev->refcnt ++; }
tgl_do_contact_search (TLS, args[0].str, args[1].num == NOT_FOUND ? args[1].num : 10, print_user_list_gw, ev);
}
void do_accept_secret_chat (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 1);
tgl_do_accept_encr_chat_request (&args[0].P->encr_chat, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_accept_encr_chat_request (TLS, &args[0].P->encr_chat, print_encr_chat_success_gw, ev);
}
void do_set_ttl (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 2);
if (args[0].P->encr_chat.state == sc_ok) {
tgl_do_set_encr_chat_ttl (&args[0].P->encr_chat, args[1].num, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_set_encr_chat_ttl (TLS, &args[0].P->encr_chat, args[1].num, print_msg_success_gw, ev);
} else {
if (ev) { ev->refcnt ++; }
print_success_gw (TLS, ev, 0);
}
}
void do_export_card (int arg_num, struct arg args[], struct in_ev *ev) {
assert (!arg_num);
if (ev) { ev->refcnt ++; }
tgl_do_export_card (print_card_gw, ev);
tgl_do_export_card (TLS, print_card_gw, ev);
}
void do_import_card (int arg_num, struct arg args[], struct in_ev *ev) {
@ -981,7 +1017,7 @@ void do_import_card (int arg_num, struct arg args[], struct in_ev *ev) {
if (ok) {
p[pp ++] = cur;
if (ev) { ev->refcnt ++; }
tgl_do_import_card (pp, p, print_user_gw, ev);
tgl_do_import_card (TLS, pp, p, print_user_gw, ev);
}
}
}
@ -989,7 +1025,7 @@ void do_import_card (int arg_num, struct arg args[], struct in_ev *ev) {
void do_send_contact (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 4);
if (ev) { ev->refcnt ++; }
tgl_do_send_contact (args[0].P->id, args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), args[3].str, strlen (args[3].str), print_msg_gw, ev);
tgl_do_send_contact (TLS, args[0].P->id, args[1].str, strlen (args[1].str), args[2].str, strlen (args[2].str), args[3].str, strlen (args[3].str), print_msg_gw, ev);
}
void do_main_session (int arg_num, struct arg args[], struct in_ev *ev) {
@ -1014,7 +1050,7 @@ extern struct event *term_ev;
void do_clear (int arg_num, struct arg args[], struct in_ev *ev) {
logprintf ("Do_clear\n");
tgl_free_all ();
tgl_free_all (TLS);
free (default_username);
free (config_filename);
free (prefix);
@ -1027,13 +1063,14 @@ void do_clear (int arg_num, struct arg args[], struct in_ev *ev) {
free (lua_file);
clear_history ();
event_free (term_ev);
event_base_free (tgl_state.ev_base);
event_base_free (TLS->ev_base);
do_halt (0);
}
void do_send_location (int arg_num, struct arg args[], struct in_ev *ev) {
assert (arg_num == 3);
tgl_do_send_location (args[0].P->id, args[1].dval, args[2].dval, 0, 0);
if (ev) { ev->refcnt ++; }
tgl_do_send_location (TLS, args[0].P->id, args[1].dval, args[2].dval, print_msg_success_gw, ev);
}
@ -1242,6 +1279,9 @@ int complete_command_list (int index, const char *text, int len, char **R) {
index ++;
}
if (commands[index].name) {
void print_msg_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M);
void print_encr_chat_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E);;
void print_success_gw (struct tgl_state *TLS, void *extra, int success);
*R = strdup (commands[index].name);
assert (*R);
return index;
@ -1295,11 +1335,11 @@ char *command_generator (const char *text, int state) {
if (c) { rl_line_buffer[rl_point] = c; }
return R;
case ca_user:
index = tgl_complete_user_list (index, command_pos, command_len, &R);
index = tgl_complete_user_list (TLS, index, command_pos, command_len, &R);
if (c) { rl_line_buffer[rl_point] = c; }
return R;
case ca_peer:
index = tgl_complete_peer_list (index, command_pos, command_len, &R);
index = tgl_complete_peer_list (TLS, index, command_pos, command_len, &R);
if (c) { rl_line_buffer[rl_point] = c; }
return R;
case ca_file_name:
@ -1308,11 +1348,11 @@ char *command_generator (const char *text, int state) {
R = rl_filename_completion_function (command_pos, state);
return R;
case ca_chat:
index = tgl_complete_chat_list (index, command_pos, command_len, &R);
index = tgl_complete_chat_list (TLS, index, command_pos, command_len, &R);
if (c) { rl_line_buffer[rl_point] = c; }
return R;
case ca_secret_chat:
index = tgl_complete_encr_chat_list (index, command_pos, command_len, &R);
index = tgl_complete_encr_chat_list (TLS, index, command_pos, command_len, &R);
if (c) { rl_line_buffer[rl_point] = c; }
return R;
case ca_modifier:
@ -1321,7 +1361,7 @@ char *command_generator (const char *text, int state) {
return R;
#ifndef DISABLE_EXTF
case ca_extf:
index = tglf_extf_autocomplete (text, len, index, &R, rl_line_buffer, rl_point);
index = tglf_extf_autocomplete (TLS, text, len, index, &R, rl_line_buffer, rl_point);
if (c) { rl_line_buffer[rl_point] = c; }
return R;
#endif
@ -1346,13 +1386,48 @@ void work_modifier (const char *s, int l) {
#endif
}
void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *ML[]) {
void print_fail (struct in_ev *ev) {
if (ev) {
mprint_start (ev);
mprintf (ev, "FAIL\n");
mprint_end (ev);
}
}
void print_success (struct in_ev *ev) {
if (ev) {
mprint_start (ev);
mprintf (ev, "SUCCESS\n");
mprint_end (ev);
}
}
void print_success_gw (struct tgl_state *TLSR, void *extra, int success) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
else { print_success (ev); return; }
}
void print_msg_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_message *M) {
print_success_gw (TLS, extra, success);
}
void print_encr_chat_success_gw (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E) {
print_success_gw (TLS, extra, success);
}
void print_msg_list_gw (struct tgl_state *TLSR, void *extra, int success, int num, struct tgl_message *ML[]) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { print_fail (ev); return; }
mprint_start (ev);
int i;
for (i = num - 1; i >= 0; i--) {
@ -1361,25 +1436,27 @@ void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *M
mprint_end (ev);
}
void print_msg_gw (void *extra, int success, struct tgl_message *M) {
void print_msg_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_message *M) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
print_message (ev, M);
mprint_end (ev);
}
void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[]) {
void print_user_list_gw (struct tgl_state *TLSR, void *extra, int success, int num, struct tgl_user *UL[]) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
int i;
for (i = num - 1; i >= 0; i--) {
@ -1389,39 +1466,42 @@ void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[
mprint_end (ev);
}
void print_user_gw (void *extra, int success, struct tgl_user *U) {
void print_user_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
print_user_name (ev, U->id, (void *)U);
mprintf (ev, "\n");
mprint_end (ev);
}
void print_filename_gw (void *extra, int success, char *name) {
void print_filename_gw (struct tgl_state *TLSR, void *extra, int success, char *name) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
mprintf (ev, "Saved to %s\n", name);
mprint_end (ev);
}
void open_filename_gw (void *extra, int success, char *name) {
void open_filename_gw (struct tgl_state *TLSR, void *extra, int success, char *name) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (ev) { return; }
if (!success) { return; }
if (!success) { print_fail (ev); return; }
static char buf[PATH_MAX];
if (snprintf (buf, sizeof (buf), OPEN_BIN, name) >= (int) sizeof (buf)) {
logprintf ("Open image command buffer overflow\n");
@ -1434,16 +1514,14 @@ void open_filename_gw (void *extra, int success, char *name) {
}
}
void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) {
void print_chat_info_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_chat *C) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) {
vlogprintf (E_NOTICE, "Failed to get chat info\n");
return;
}
if (!success) { print_fail (ev); return; }
mprint_start (ev);
tgl_peer_t *U = (void *)C;
@ -1454,9 +1532,9 @@ void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) {
int i;
for (i = 0; i < C->user_list_size; i++) {
mprintf (ev, "\t\t");
print_user_name (ev, TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].user_id)));
print_user_name (ev, TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TLS, TGL_MK_USER (C->user_list[i].user_id)));
mprintf (ev, " invited by ");
print_user_name (ev, TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].inviter_id)));
print_user_name (ev, TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TLS, TGL_MK_USER (C->user_list[i].inviter_id)));
mprintf (ev, " at ");
print_date_full (ev, C->user_list[i].date);
if (C->user_list[i].user_id == C->admin_id) {
@ -1468,13 +1546,14 @@ void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) {
mprint_end (ev);
}
void print_user_info_gw (void *extra, int success, struct tgl_user *U) {
void print_user_info_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
tgl_peer_t *C = (void *)U;
mprint_start (ev);
mpush_color (ev, COLOR_YELLOW);
@ -1497,13 +1576,14 @@ void print_user_info_gw (void *extra, int success, struct tgl_user *U) {
mprint_end (ev);
}
void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E) {
void print_secret_chat_gw (struct tgl_state *TLSR, void *extra, int success, struct tgl_secret_chat *E) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
mpush_color (ev, COLOR_YELLOW);
mprintf (ev, " Encrypted chat ");
@ -1513,13 +1593,14 @@ void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E)
mprint_end (ev);
}
void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) {
void print_dialog_list_gw (struct tgl_state *TLSR, void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) {
assert (TLS == TLSR);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
mpush_color (ev, COLOR_YELLOW);
int i;
@ -1527,13 +1608,13 @@ void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t pee
tgl_peer_t *UC;
switch (tgl_get_peer_type (peers[i])) {
case TGL_PEER_USER:
UC = tgl_peer_get (peers[i]);
UC = tgl_peer_get (TLS, peers[i]);
mprintf (ev, "User ");
print_user_name (ev, peers[i], UC);
mprintf (ev, ": %d unread\n", unread_count[i]);
break;
case TGL_PEER_CHAT:
UC = tgl_peer_get (peers[i]);
UC = tgl_peer_get (TLS, peers[i]);
mprintf (ev, "Chat ");
print_chat_name (ev, peers[i], UC);
mprintf (ev, ": %d unread\n", unread_count[i]);
@ -1555,15 +1636,15 @@ void interpreter_chat_mode (char *line) {
int limit = 40;
sscanf (line, "/history %99d", &limit);
if (limit < 0 || limit > 1000) { limit = 40; }
tgl_do_get_history (chat_mode_id, limit, offline_mode, print_msg_list_gw, 0);
tgl_do_get_history (TLS, chat_mode_id, limit, offline_mode, print_msg_list_gw, 0);
return;
}
if (!strncmp (line, "/read", 5)) {
tgl_do_mark_read (chat_mode_id, 0, 0);
tgl_do_mark_read (TLS, chat_mode_id, 0, 0);
return;
}
if (strlen (line) > 0) {
tgl_do_send_message (chat_mode_id, line, strlen (line), 0, 0);
tgl_do_send_message (TLS, chat_mode_id, line, strlen (line), 0, 0);
}
}
@ -1579,7 +1660,7 @@ void print_read_list (int num, struct tgl_message *list[]) {
mprint_start (ev);
for (i = 0; i < num; i++) if (list[i]) {
tgl_peer_id_t to_id;
if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == tgl_state.our_id) {
if (tgl_get_peer_type (list[i]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[i]->to_id) == TLS->our_id) {
to_id = list[i]->from_id;
} else {
to_id = list[i]->to_id;
@ -1589,7 +1670,7 @@ void print_read_list (int num, struct tgl_message *list[]) {
int c2 = 0;
for (j = i; j < num; j++) if (list[j]) {
tgl_peer_id_t end_id;
if (tgl_get_peer_type (list[j]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[j]->to_id) == tgl_state.our_id) {
if (tgl_get_peer_type (list[j]->to_id) == TGL_PEER_USER && tgl_get_peer_id (list[j]->to_id) == TLS->our_id) {
end_id = list[j]->from_id;
} else {
end_id = list[j]->to_id;
@ -1609,15 +1690,15 @@ void print_read_list (int num, struct tgl_message *list[]) {
switch (tgl_get_peer_type (to_id)) {
case TGL_PEER_USER:
mprintf (ev, "User ");
print_user_name (ev, to_id, tgl_peer_get (to_id));
print_user_name (ev, to_id, tgl_peer_get (TLS, to_id));
break;
case TGL_PEER_CHAT:
mprintf (ev, "Chat ");
print_chat_name (ev, to_id, tgl_peer_get (to_id));
print_chat_name (ev, to_id, tgl_peer_get (TLS, to_id));
break;
case TGL_PEER_ENCR_CHAT:
mprintf (ev, "Secret chat ");
print_encr_chat_name (ev, to_id, tgl_peer_get (to_id));
print_encr_chat_name (ev, to_id, tgl_peer_get (TLS, to_id));
break;
default:
assert (0);
@ -1635,7 +1716,8 @@ void unread_message_alarm (evutil_socket_t fd, short what, void *arg) {
unread_message_event = 0;
}
void mark_read_upd (int num, struct tgl_message *list[]) {
void mark_read_upd (struct tgl_state *TLSR, int num, struct tgl_message *list[]) {
assert (TLSR == TLS);
if (!binlog_read) { return; }
if (log_level < 1) { return; }
@ -1644,7 +1726,7 @@ void mark_read_upd (int num, struct tgl_message *list[]) {
unread_message_count += num;
if (!unread_message_event) {
unread_message_event = evtimer_new (tgl_state.ev_base, unread_message_alarm, 0);
unread_message_event = evtimer_new (TLS->ev_base, unread_message_alarm, 0);
static struct timeval ptimeout = { 1, 0};
event_add (unread_message_event, &ptimeout);
}
@ -1697,7 +1779,8 @@ void print_typing (struct in_ev *ev, enum tgl_typing_status status) {
}
}
void type_notification_upd (struct tgl_user *U, enum tgl_typing_status status) {
void type_notification_upd (struct tgl_state *TLSR, struct tgl_user *U, enum tgl_typing_status status) {
assert (TLSR == TLS);
if (log_level < 2 || (disable_output && !notify_ev)) { return; }
struct in_ev *ev = notify_ev;
mprint_start (ev);
@ -1711,7 +1794,8 @@ void type_notification_upd (struct tgl_user *U, enum tgl_typing_status status) {
mprint_end (ev);
}
void type_in_chat_notification_upd (struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status) {
void type_in_chat_notification_upd (struct tgl_state *TLSR, struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status) {
assert (TLSR == TLS);
if (log_level < 2 || (disable_output && !notify_ev)) { return; }
struct in_ev *ev = notify_ev;
mprint_start (ev);
@ -1728,7 +1812,8 @@ void type_in_chat_notification_upd (struct tgl_user *U, struct tgl_chat *C, enum
}
void print_message_gw (struct tgl_message *M) {
void print_message_gw (struct tgl_state *TLSR, struct tgl_message *M) {
assert (TLSR == TLS);
#ifdef USE_LUA
lua_new_msg (M);
#endif
@ -1746,7 +1831,8 @@ void print_message_gw (struct tgl_message *M) {
mprint_end (ev);
}
void our_id_gw (int id) {
void our_id_gw (struct tgl_state *TLSR, int id) {
assert (TLSR == TLS);
#ifdef USE_LUA
lua_our_id (id);
#endif
@ -1797,7 +1883,8 @@ void print_peer_updates (struct in_ev *ev, int flags) {
}
}
void user_update_gw (struct tgl_user *U, unsigned flags) {
void user_update_gw (struct tgl_state *TLSR, struct tgl_user *U, unsigned flags) {
assert (TLSR == TLS);
#ifdef USE_LUA
lua_user_update (U, flags);
#endif
@ -1823,7 +1910,8 @@ void user_update_gw (struct tgl_user *U, unsigned flags) {
}
}
void chat_update_gw (struct tgl_chat *U, unsigned flags) {
void chat_update_gw (struct tgl_state *TLSR, struct tgl_chat *U, unsigned flags) {
assert (TLSR == TLS);
#ifdef USE_LUA
lua_chat_update (U, flags);
#endif
@ -1849,7 +1937,8 @@ void chat_update_gw (struct tgl_chat *U, unsigned flags) {
}
}
void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) {
void secret_chat_update_gw (struct tgl_state *TLSR, struct tgl_secret_chat *U, unsigned flags) {
assert (TLSR == TLS);
#ifdef USE_LUA
lua_secret_chat_update (U, flags);
#endif
@ -1861,7 +1950,7 @@ void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) {
if (!binlog_read) { return; }
if ((flags & TGL_UPDATE_REQUESTED) && !disable_auto_accept) {
tgl_do_accept_encr_chat_request (U, 0, 0);
tgl_do_accept_encr_chat_request (TLS, U, 0, 0);
}
if (disable_output && !notify_ev) { return; }
@ -1885,13 +1974,14 @@ void secret_chat_update_gw (struct tgl_secret_chat *U, unsigned flags) {
}
}
void print_card_gw (void *extra, int success, int size, int *card) {
void print_card_gw (struct tgl_state *TLSR, void *extra, int success, int size, int *card) {
assert (TLSR == TLS);
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
mprintf (ev, "Card: ");
int i;
@ -1901,13 +1991,13 @@ void print_card_gw (void *extra, int success, int size, int *card) {
mprint_end (ev);
}
void callback_extf (void *extra, int success, char *buf) {
void callback_extf (struct tgl_state *TLS, void *extra, int success, char *buf) {
struct in_ev *ev = extra;
if (ev && !--ev->refcnt) {
free (ev);
return;
}
if (!success) { return; }
if (!success) { print_fail (ev); return; }
mprint_start (ev);
mprintf (ev, "%s\n", buf);
mprint_end (ev);
@ -1957,7 +2047,7 @@ void interpreter_ex (char *line UU, void *ex) {
if (*line == '(') {
struct in_ev *ev = ex;
if (ev) { ev->refcnt ++; }
tgl_do_send_extf (line, strlen (line), callback_extf, ev);
tgl_do_send_extf (TLS, line, strlen (line), callback_extf, ev);
in_readline = 0;
return;
}
@ -2463,13 +2553,13 @@ void print_service_message (struct in_ev *ev, struct tgl_message *M) {
mpop_color (ev);
mprintf (ev, " ");
if (tgl_get_peer_type (M->to_id) == TGL_PEER_CHAT) {
print_chat_name (ev, M->to_id, tgl_peer_get (M->to_id));
print_chat_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id));
} else {
assert (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT);
print_encr_chat_name (ev, M->to_id, tgl_peer_get (M->to_id));
print_encr_chat_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id));
}
mprintf (ev, " ");
print_user_name (ev, M->from_id, tgl_peer_get (M->from_id));
print_user_name (ev, M->from_id, tgl_peer_get (TLS, M->from_id));
switch (M->action.type) {
case tgl_message_action_none:
@ -2496,12 +2586,12 @@ void print_service_message (struct in_ev *ev, struct tgl_message *M) {
break;
case tgl_message_action_chat_add_user:
mprintf (ev, " added user ");
print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (tgl_set_peer_id (TGL_PEER_USER, M->action.user)));
print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (TLS, tgl_set_peer_id (TGL_PEER_USER, M->action.user)));
mprintf (ev, "\n");
break;
case tgl_message_action_chat_delete_user:
mprintf (ev, " deleted user ");
print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (tgl_set_peer_id (TGL_PEER_USER, M->action.user)));
print_user_name (ev, tgl_set_peer_id (TGL_PEER_USER, M->action.user), tgl_peer_get (TLS, tgl_set_peer_id (TGL_PEER_USER, M->action.user)));
mprintf (ev, "\n");
break;
case tgl_message_action_set_message_ttl:
@ -2564,7 +2654,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) {
print_date (ev, M->date);
mpop_color (ev);
mprintf (ev, " ");
print_user_name (ev, M->to_id, tgl_peer_get (M->to_id));
print_user_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id));
mpush_color (ev, COLOR_GREEN);
if (M->unread) {
mprintf (ev, " <<< ");
@ -2579,7 +2669,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) {
print_date (ev, M->date);
mpop_color (ev);
mprintf (ev, " ");
print_user_name (ev, M->from_id, tgl_peer_get (M->from_id));
print_user_name (ev, M->from_id, tgl_peer_get (TLS, M->from_id));
mpush_color (ev, COLOR_BLUE);
if (M->unread) {
mprintf (ev, " >>> ");
@ -2588,7 +2678,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) {
}
}
} else if (tgl_get_peer_type (M->to_id) == TGL_PEER_ENCR_CHAT) {
tgl_peer_t *P = tgl_peer_get (M->to_id);
tgl_peer_t *P = tgl_peer_get (TLS, M->to_id);
assert (P);
if (M->out) {
mpush_color (ev, COLOR_GREEN);
@ -2629,10 +2719,10 @@ void print_message (struct in_ev *ev, struct tgl_message *M) {
print_date (ev, M->date);
mpop_color (ev);
mprintf (ev, " ");
print_chat_name (ev, M->to_id, tgl_peer_get (M->to_id));
print_chat_name (ev, M->to_id, tgl_peer_get (TLS, M->to_id));
mprintf (ev, " ");
print_user_name (ev, M->from_id, tgl_peer_get (M->from_id));
if ((tgl_get_peer_type (M->from_id) == TGL_PEER_USER) && (tgl_get_peer_id (M->from_id) == tgl_state.our_id)) {
print_user_name (ev, M->from_id, tgl_peer_get (TLS, M->from_id));
if ((tgl_get_peer_type (M->from_id) == TGL_PEER_USER) && (tgl_get_peer_id (M->from_id) == TLS->our_id)) {
mpush_color (ev, COLOR_GREEN);
} else {
mpush_color (ev, COLOR_BLUE);
@ -2645,7 +2735,7 @@ void print_message (struct in_ev *ev, struct tgl_message *M) {
}
if (tgl_get_peer_type (M->fwd_from_id) == TGL_PEER_USER) {
mprintf (ev, "[fwd from ");
print_user_name (ev, M->fwd_from_id, tgl_peer_get (M->fwd_from_id));
print_user_name (ev, M->fwd_from_id, tgl_peer_get (TLS, M->fwd_from_id));
mprintf (ev, "] ");
}
if (M->message && strlen (M->message)) {

View File

@ -45,7 +45,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2)))
#define vlogprintf(v,...) \
do { \
if (tgl_state.verbosity >= (v)) {\
if (TLS->verbosity >= (v)) {\
logprintf (__VA_ARGS__);\
}\
} while (0);\

170
loop.c
View File

@ -59,6 +59,8 @@
#include "lua-tg.h"
#include "tgl.h"
#include "binlog.h"
#include "net.h"
#include "tgl-timers.h"
int verbosity;
extern int readline_disabled;
@ -92,6 +94,7 @@ static int delete_stdin_event;
extern volatile int sigterm_cnt;
extern char *start_command;
extern struct tgl_state *TLS;
static void stdin_read_callback_all (int arg, short what, struct event *self) {
if (!readline_disabled) {
@ -119,7 +122,7 @@ static void stdin_read_callback_all (int arg, short what, struct event *self) {
break;
}
if (r == 0) {
//struct event *ev = event_base_get_running_event (tgl_state.ev_base);
//struct event *ev = event_base_get_running_event (TLS->ev_base);
//event_del (ev);
//event_del (self);
@ -165,16 +168,16 @@ void net_loop (int flags, int (*is_end)(void)) {
}
if (flags & 3) {
if (flags & 1) {
term_ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_char, 0);
term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_char, 0);
} else {
term_ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_line, 0);
term_ev = event_new (TLS->ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback_line, 0);
}
event_add (term_ev, 0);
}
int last_get_state = time (0);
while (!is_end || !is_end ()) {
event_base_loop (tgl_state.ev_base, EVLOOP_ONCE);
event_base_loop (TLS->ev_base, EVLOOP_ONCE);
if (term_ev && delete_stdin_event) {
event_free (term_ev);
@ -184,7 +187,7 @@ void net_loop (int flags, int (*is_end)(void)) {
#ifdef USE_LUA
lua_do_all ();
#endif
if (safe_quit && !tgl_state.active_queries) {
if (safe_quit && !TLS->active_queries) {
printf ("All done. Exit\n");
do_halt (0);
}
@ -192,7 +195,7 @@ void net_loop (int flags, int (*is_end)(void)) {
do_halt (0);
}
if (time (0) - last_get_state > 3600) {
tgl_do_lookup_state ();
tgl_do_lookup_state (TLS);
last_get_state = time (0);
}
write_state_file ();
@ -200,7 +203,7 @@ void net_loop (int flags, int (*is_end)(void)) {
if (unknown_user_list_pos) {
int i;
for (i = 0; i < unknown_user_list_pos; i++) {
tgl_do_get_user_info (TGL_MK_USER (unknown_user_list[i]), 0, 0, 0);
tgl_do_get_user_info (TLS, TGL_MK_USER (unknown_user_list[i]), 0, 0, 0);
}
unknown_user_list_pos = 0;
}
@ -250,13 +253,13 @@ int main_loop (void) {
struct tgl_dc *cur_a_dc;
int is_authorized (void) {
return tgl_authorized_dc (cur_a_dc);
return tgl_authorized_dc (TLS, cur_a_dc);
}
int all_authorized (void) {
int i;
for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i]) {
if (!tgl_authorized_dc (tgl_state.DC_list[i])) {
for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i]) {
if (!tgl_authorized_dc (TLS, TLS->DC_list[i])) {
return 0;
}
}
@ -280,7 +283,8 @@ void on_get_config (void *extra, int success) {
int should_register;
char *hash;
void sign_in_callback (void *extra, int success, int registered, const char *mhash) {
void sign_in_callback (struct tgl_state *TLSR, void *extra, int success, int registered, const char *mhash) {
assert (TLSR == TLS);
if (!success) {
logprintf ("Can not send code\n");
do_halt (1);
@ -293,7 +297,8 @@ void sign_in_callback (void *extra, int success, int registered, const char *mha
int signed_in_ok;
void sign_in_result (void *extra, int success, struct tgl_user *U) {
void sign_in_result (struct tgl_state *TLSR, void *extra, int success, struct tgl_user *U) {
assert (TLSR == TLS);
if (!success) {
logprintf ("Can not login\n");
do_halt (1);
@ -310,10 +315,11 @@ int sent_code (void) {
}
int dc_signed_in (void) {
return tgl_signed_dc (cur_a_dc);
return tgl_signed_dc (TLS, cur_a_dc);
}
void export_auth_callback (void *DC, int success) {
void export_auth_callback (struct tgl_state *TLSR, void *DC, int success) {
assert (TLSR == TLS);
if (!success) {
logprintf ("Can not export auth\n");
do_halt (1);
@ -321,7 +327,8 @@ void export_auth_callback (void *DC, int success) {
}
int d_got_ok;
void get_difference_callback (void *extra, int success) {
void get_difference_callback (struct tgl_state *TLSR, void *extra, int success) {
assert (TLSR == TLS);
assert (success);
d_got_ok = 1;
}
@ -368,10 +375,10 @@ void read_state_file (void) {
int seq = x[2];
int date = x[3];
close (state_file_fd);
bl_do_set_seq (seq);
bl_do_set_pts (pts);
bl_do_set_qts (qts);
bl_do_set_date (date);
bl_do_set_seq (TLS, seq);
bl_do_set_pts (TLS, pts);
bl_do_set_qts (TLS, qts);
bl_do_set_date (TLS, date);
}
@ -381,8 +388,8 @@ void write_state_file (void) {
static int wpts;
static int wqts;
static int wdate;
if (wseq >= tgl_state.seq && wpts >= tgl_state.pts && wqts >= tgl_state.qts && wdate >= tgl_state.date) { return; }
wseq = tgl_state.seq; wpts = tgl_state.pts; wqts = tgl_state.qts; wdate = tgl_state.date;
if (wseq >= TLS->seq && wpts >= TLS->pts && wqts >= TLS->qts && wdate >= TLS->date) { return; }
wseq = TLS->seq; wpts = TLS->pts; wqts = TLS->qts; wdate = TLS->date;
int state_file_fd = open (get_state_filename (), O_CREAT | O_RDWR, 0600);
if (state_file_fd < 0) {
logprintf ("Can not write state file '%s': %m\n", get_state_filename ());
@ -426,12 +433,12 @@ void write_auth_file (void) {
assert (auth_file_fd >= 0);
int x = DC_SERIALIZED_MAGIC;
assert (write (auth_file_fd, &x, 4) == 4);
assert (write (auth_file_fd, &tgl_state.max_dc_num, 4) == 4);
assert (write (auth_file_fd, &tgl_state.dc_working_num, 4) == 4);
assert (write (auth_file_fd, &TLS->max_dc_num, 4) == 4);
assert (write (auth_file_fd, &TLS->dc_working_num, 4) == 4);
tgl_dc_iterator_ex (write_dc, &auth_file_fd);
tgl_dc_iterator_ex (TLS, write_dc, &auth_file_fd);
assert (write (auth_file_fd, &tgl_state.our_id, 4) == 4);
assert (write (auth_file_fd, &TLS->our_id, 4) == 4);
close (auth_file_fd);
}
@ -477,7 +484,7 @@ void write_secret_chat_file (void) {
y[0] = secret_chat_fd;
y[1] = 0;
tgl_peer_iterator_ex (write_secret_chat, y);
tgl_peer_iterator_ex (TLS, write_secret_chat, y);
lseek (secret_chat_fd, 8, SEEK_SET);
assert (write (secret_chat_fd, &y[1], 4) == 4);
@ -500,24 +507,24 @@ void read_dc (int auth_file_fd, int id, unsigned ver) {
assert (read (auth_file_fd, auth_key, 256) == 256);
//bl_do_add_dc (id, ip, l, port, auth_key_id, auth_key);
bl_do_dc_option (id, 2, "DC", l, ip, port);
bl_do_set_auth_key_id (id, auth_key);
bl_do_dc_signed (id);
bl_do_dc_option (TLS, id, 2, "DC", l, ip, port);
bl_do_set_auth_key_id (TLS, id, auth_key);
bl_do_dc_signed (TLS, id);
}
void empty_auth_file (void) {
if (tgl_state.test_mode) {
bl_do_dc_option (1, 0, "", strlen (TG_SERVER_TEST_1), TG_SERVER_TEST_1, 443);
bl_do_dc_option (2, 0, "", strlen (TG_SERVER_TEST_2), TG_SERVER_TEST_2, 443);
bl_do_dc_option (3, 0, "", strlen (TG_SERVER_TEST_3), TG_SERVER_TEST_3, 443);
bl_do_set_working_dc (2);
if (TLS->test_mode) {
bl_do_dc_option (TLS, 1, 0, "", strlen (TG_SERVER_TEST_1), TG_SERVER_TEST_1, 443);
bl_do_dc_option (TLS, 2, 0, "", strlen (TG_SERVER_TEST_2), TG_SERVER_TEST_2, 443);
bl_do_dc_option (TLS, 3, 0, "", strlen (TG_SERVER_TEST_3), TG_SERVER_TEST_3, 443);
bl_do_set_working_dc (TLS, 2);
} else {
bl_do_dc_option (1, 0, "", strlen (TG_SERVER_1), TG_SERVER_1, 443);
bl_do_dc_option (2, 0, "", strlen (TG_SERVER_2), TG_SERVER_2, 443);
bl_do_dc_option (3, 0, "", strlen (TG_SERVER_3), TG_SERVER_3, 443);
bl_do_dc_option (4, 0, "", strlen (TG_SERVER_4), TG_SERVER_4, 443);
bl_do_dc_option (5, 0, "", strlen (TG_SERVER_5), TG_SERVER_5, 443);
bl_do_set_working_dc (2);
bl_do_dc_option (TLS, 1, 0, "", strlen (TG_SERVER_1), TG_SERVER_1, 443);
bl_do_dc_option (TLS, 2, 0, "", strlen (TG_SERVER_2), TG_SERVER_2, 443);
bl_do_dc_option (TLS, 3, 0, "", strlen (TG_SERVER_3), TG_SERVER_3, 443);
bl_do_dc_option (TLS, 4, 0, "", strlen (TG_SERVER_4), TG_SERVER_4, 443);
bl_do_dc_option (TLS, 5, 0, "", strlen (TG_SERVER_5), TG_SERVER_5, 443);
bl_do_set_working_dc (TLS, 4);
}
}
@ -550,14 +557,14 @@ void read_auth_file (void) {
read_dc (auth_file_fd, i, m);
}
}
bl_do_set_working_dc (dc_working_num);
bl_do_set_working_dc (TLS, dc_working_num);
int our_id;
int l = read (auth_file_fd, &our_id, 4);
if (l < 4) {
assert (!l);
}
if (our_id) {
bl_do_set_our_id (our_id);
bl_do_set_our_id (TLS, our_id);
}
close (auth_file_fd);
}
@ -588,17 +595,17 @@ void read_secret_chat (int fd, int v) {
assert (read (fd, &out_seq_no, 4) == 4);
}
bl_do_encr_chat_create (id, user_id, admin_id, s, l);
struct tgl_secret_chat *P = (void *)tgl_peer_get (TGL_MK_ENCR_CHAT (id));
bl_do_encr_chat_create (TLS, id, user_id, admin_id, s, l);
struct tgl_secret_chat *P = (void *)tgl_peer_get (TLS, TGL_MK_ENCR_CHAT (id));
assert (P && (P->flags & FLAG_CREATED));
bl_do_encr_chat_set_date (P, date);
bl_do_encr_chat_set_ttl (P, ttl);
bl_do_encr_chat_set_layer (P, layer);
bl_do_encr_chat_set_access_hash (P, access_hash);
bl_do_encr_chat_set_state (P, state);
bl_do_encr_chat_set_key (P, key, key_fingerprint);
bl_do_encr_chat_set_date (TLS, P, date);
bl_do_encr_chat_set_ttl (TLS, P, ttl);
bl_do_encr_chat_set_layer (TLS ,P, layer);
bl_do_encr_chat_set_access_hash (TLS, P, access_hash);
bl_do_encr_chat_set_state (TLS, P, state);
bl_do_encr_chat_set_key (TLS, P, key, key_fingerprint);
if (v >= 1) {
bl_do_encr_chat_set_seq (P, in_seq_no, last_in_seq_no, out_seq_no);
bl_do_encr_chat_set_seq (TLS, P, in_seq_no, last_in_seq_no, out_seq_no);
}
}
@ -621,7 +628,8 @@ void read_secret_chat_file (void) {
close (secret_chat_fd);
}
void dlist_cb (void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) {
void dlist_cb (struct tgl_state *TLSR, void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) {
assert (TLSR == TLS);
d_got_ok = 1;
}
@ -680,7 +688,7 @@ static void accept_incoming (evutil_socket_t efd, short what, void *arg) {
int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen);
assert (fd >= 0);
struct bufferevent *bev = bufferevent_socket_new (tgl_state.ev_base, fd, 0);
struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, 0);
struct in_ev *e = malloc (sizeof (*e));
e->bev = bev;
e->refcnt = 1;
@ -693,20 +701,24 @@ static void accept_incoming (evutil_socket_t efd, short what, void *arg) {
int loop (void) {
//on_start ();
tgl_set_callback (&upd_cb);
//tgl_state.temp_key_expire_time = 60;
tgl_init ();
tgl_set_callback (TLS, &upd_cb);
//TLS->temp_key_expire_time = 60;
struct event_base *ev = event_base_new ();
tgl_set_ev_base (TLS, ev);
tgl_set_net_methods (TLS, &tgl_conn_methods);
tgl_set_timer_methods (TLS, &tgl_libevent_timers);
tgl_init (TLS);
if (binlog_enabled) {
double t = tglt_get_double_time ();
if (verbosity >= E_DEBUG) {
logprintf ("replay log start\n");
}
tgl_replay_log ();
tgl_replay_log (TLS);
if (verbosity >= E_DEBUG) {
logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t);
}
tgl_reopen_binlog_for_writing ();
tgl_reopen_binlog_for_writing (TLS);
} else {
read_auth_file ();
read_state_file ();
@ -719,31 +731,31 @@ int loop (void) {
#endif
if (sfd >= 0) {
struct event *ev = event_new (tgl_state.ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0);
struct event *ev = event_new (TLS->ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0);
event_add (ev, 0);
}
if (usfd >= 0) {
struct event *ev = event_new (tgl_state.ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0);
struct event *ev = event_new (TLS->ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0);
event_add (ev, 0);
}
update_prompt ();
if (reset_authorization) {
tgl_peer_t *P = tgl_peer_get (TGL_MK_USER (tgl_state.our_id));
tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (TLS->our_id));
if (P && P->user.phone && reset_authorization == 1) {
set_default_username (P->user.phone);
}
bl_do_reset_authorization ();
bl_do_reset_authorization (TLS);
}
net_loop (0, all_authorized);
int i;
for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_authorized_dc (tgl_state.DC_list[i])) {
for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_authorized_dc (TLS, TLS->DC_list[i])) {
assert (0);
}
if (!tgl_signed_dc (tgl_state.DC_working)) {
if (!tgl_signed_dc (TLS, TLS->DC_working)) {
if (disable_output) {
fprintf (stderr, "Can not login without output\n");
do_halt (1);
@ -761,7 +773,7 @@ int loop (void) {
set_default_username (user);
}
}
tgl_do_send_code (default_username, sign_in_callback, 0);
tgl_do_send_code (TLS, default_username, sign_in_callback, 0);
net_loop (0, sent_code);
if (verbosity >= E_DEBUG) {
@ -778,11 +790,11 @@ int loop (void) {
}
if (!strcmp (code, "call")) {
printf ("You typed \"call\", switching to phone system.\n");
tgl_do_phone_call (default_username, hash, 0, 0);
tgl_do_phone_call (TLS, default_username, hash, 0, 0);
printf ("Calling you! Code: ");
continue;
}
if (tgl_do_send_code_result (default_username, hash, code, sign_in_result, 0) >= 0) {
if (tgl_do_send_code_result (TLS, default_username, hash, code, sign_in_result, 0) >= 0) {
break;
}
printf ("Invalid code. Try again: ");
@ -822,11 +834,11 @@ int loop (void) {
}
if (!strcmp (code, "call")) {
printf ("You typed \"call\", switching to phone system.\n");
tgl_do_phone_call (default_username, hash, 0, 0);
tgl_do_phone_call (TLS, default_username, hash, 0, 0);
printf ("Calling you! Code: ");
continue;
}
if (tgl_do_send_code_result_auth (default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) {
if (tgl_do_send_code_result_auth (TLS, default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) {
break;
}
printf ("Invalid code. Try again: ");
@ -835,14 +847,14 @@ int loop (void) {
}
net_loop (0, signed_in);
//bl_do_dc_signed (tgl_state.DC_working);
//bl_do_dc_signed (TLS->DC_working);
}
for (i = 0; i <= tgl_state.max_dc_num; i++) if (tgl_state.DC_list[i] && !tgl_signed_dc (tgl_state.DC_list[i])) {
tgl_do_export_auth (i, export_auth_callback, (void*)(long)tgl_state.DC_list[i]);
cur_a_dc = tgl_state.DC_list[i];
for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_signed_dc (TLS, TLS->DC_list[i])) {
tgl_do_export_auth (TLS, i, export_auth_callback, (void*)(long)TLS->DC_list[i]);
cur_a_dc = TLS->DC_list[i];
net_loop (0, dc_signed_in);
assert (tgl_signed_dc (tgl_state.DC_list[i]));
assert (tgl_signed_dc (TLS, TLS->DC_list[i]));
}
write_auth_file ();
@ -854,14 +866,14 @@ int loop (void) {
set_interface_callbacks ();
tglm_send_all_unsent ();
tgl_do_get_difference (sync_from_start, get_difference_callback, 0);
tglm_send_all_unsent (TLS);
tgl_do_get_difference (TLS, sync_from_start, get_difference_callback, 0);
net_loop (0, dgot);
assert (!(tgl_state.locks & TGL_LOCK_DIFF));
tgl_state.started = 1;
assert (!(TLS->locks & TGL_LOCK_DIFF));
TLS->started = 1;
if (wait_dialog_list) {
d_got_ok = 0;
tgl_do_get_dialog_list (dlist_cb, 0);
tgl_do_get_dialog_list (TLS, dlist_cb, 0);
net_loop (0, dgot);
}
#ifdef USE_LUA

163
lua-tg.c
View File

@ -47,6 +47,7 @@ lua_State *luaState;
#include <assert.h>
extern int verbosity;
extern struct tgl_state *TLS;
static int have_file;
@ -119,7 +120,7 @@ void push_chat (tgl_peer_t *P) {
for (i = 0; i < P->chat.users_num; i++) {
lua_pushnumber (luaState, i);
tgl_peer_id_t id = TGL_MK_USER (P->chat.user_list[i].user_id);
push_peer (id, tgl_peer_get (id));
push_peer (id, tgl_peer_get (TLS, id));
lua_settable (luaState, -3);
}
lua_settable (luaState, -3);
@ -129,7 +130,7 @@ void push_chat (tgl_peer_t *P) {
void push_encr_chat (tgl_peer_t *P) {
my_lua_checkstack (luaState, 4);
lua_pushstring (luaState, "user");
push_peer (TGL_MK_USER (P->encr_chat.user_id), tgl_peer_get (TGL_MK_USER (P->encr_chat.user_id)));
push_peer (TGL_MK_USER (P->encr_chat.user_id), tgl_peer_get (TLS, TGL_MK_USER (P->encr_chat.user_id)));
lua_settable (luaState, -3);
}
@ -298,18 +299,18 @@ void push_message (struct tgl_message *M) {
if (tgl_get_peer_type (M->fwd_from_id)) {
lua_pushstring (luaState, "fwd_from");
push_peer (M->fwd_from_id, tgl_peer_get (M->fwd_from_id));
push_peer (M->fwd_from_id, tgl_peer_get (TLS, M->fwd_from_id));
lua_settable (luaState, -3); // fwd_from
lua_add_num_field ("fwd_date", M->fwd_date);
}
lua_pushstring (luaState, "from");
push_peer (M->from_id, tgl_peer_get (M->from_id));
push_peer (M->from_id, tgl_peer_get (TLS, M->from_id));
lua_settable (luaState, -3);
lua_pushstring (luaState, "to");
push_peer (M->to_id, tgl_peer_get (M->to_id));
push_peer (M->to_id, tgl_peer_get (TLS, M->to_id));
lua_settable (luaState, -3);
lua_pushstring (luaState, "out");
@ -456,7 +457,7 @@ void *lua_ptr[MAX_LUA_COMMANDS];
static int pos;
static inline tgl_peer_t *get_peer (const char *s) {
return tgl_peer_get_by_name (s);
return tgl_peer_get_by_name (TLS, s);
}
enum lua_query_type {
@ -508,7 +509,8 @@ struct lua_query_extra {
int param;
};
void lua_empty_cb (void *cb_extra, int success) {
void lua_empty_cb (struct tgl_state *TLSR, void *cb_extra, int success) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -533,7 +535,8 @@ void lua_empty_cb (void *cb_extra, int success) {
free (cb);
}
void lua_contact_list_cb (void *cb_extra, int success, int num, struct tgl_user **UL) {
void lua_contact_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, struct tgl_user **UL) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -570,7 +573,8 @@ void lua_contact_list_cb (void *cb_extra, int success, int num, struct tgl_user
free (cb);
}
void lua_dialog_list_cb (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[], int msgs[], int unread[]) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -589,10 +593,10 @@ void lua_dialog_list_cb (void *cb_extra, int success, int num, tgl_peer_id_t pee
lua_newtable (luaState);
lua_pushstring (luaState, "peer");
push_peer (peers[i], tgl_peer_get (peers[i]));
push_peer (peers[i], tgl_peer_get (TLS, peers[i]));
lua_settable (luaState, -3);
struct tgl_message *M = tgl_message_get (msgs[i]);
struct tgl_message *M = tgl_message_get (TLS, msgs[i]);
if (M && (M->flags & FLAG_CREATED)) {
lua_pushstring (luaState, "message");
push_message (M);
@ -623,7 +627,8 @@ void lua_dialog_list_cb (void *cb_extra, int success, int num, tgl_peer_id_t pee
free (cb);
}
void lua_msg_cb (void *cb_extra, int success, struct tgl_message *M) {
void lua_msg_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_message *M) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -654,7 +659,8 @@ void lua_msg_cb (void *cb_extra, int success, struct tgl_message *M) {
free (cb);
}
void lua_msg_list_cb (void *cb_extra, int success, int num, struct tgl_message *M[]) {
void lua_msg_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, struct tgl_message *M[]) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -691,7 +697,8 @@ void lua_msg_list_cb (void *cb_extra, int success, int num, struct tgl_message *
free (cb);
}
void lua_file_cb (void *cb_extra, int success, char *file_name) {
void lua_file_cb (struct tgl_state *TLSR, void *cb_extra, int success, char *file_name) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -722,7 +729,8 @@ void lua_file_cb (void *cb_extra, int success, char *file_name) {
free (cb);
}
void lua_chat_cb (void *cb_extra, int success, struct tgl_chat *C) {
void lua_chat_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_chat *C) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -753,7 +761,8 @@ void lua_chat_cb (void *cb_extra, int success, struct tgl_chat *C) {
free (cb);
}
void lua_secret_chat_cb (void *cb_extra, int success, struct tgl_secret_chat *C) {
void lua_secret_chat_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_secret_chat *C) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -784,7 +793,8 @@ void lua_secret_chat_cb (void *cb_extra, int success, struct tgl_secret_chat *C)
free (cb);
}
void lua_user_cb (void *cb_extra, int success, struct tgl_user *C) {
void lua_user_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_user *C) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -815,7 +825,8 @@ void lua_user_cb (void *cb_extra, int success, struct tgl_user *C) {
free (cb);
}
void lua_str_cb (void *cb_extra, int success, char *data) {
void lua_str_cb (struct tgl_state *TLSR, void *cb_extra, int success, char *data) {
assert (TLSR == TLS);
struct lua_query_extra *cb = cb_extra;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -856,60 +867,60 @@ void lua_do_all (void) {
char *s, *s1, *s2, *s3;
switch (f) {
case lq_contact_list:
tgl_do_update_contact_list (lua_contact_list_cb, lua_ptr[p ++]);
tgl_do_update_contact_list (TLS, lua_contact_list_cb, lua_ptr[p ++]);
break;
case lq_dialog_list:
tgl_do_get_dialog_list (lua_dialog_list_cb, lua_ptr[p ++]);
tgl_do_get_dialog_list (TLS, lua_dialog_list_cb, lua_ptr[p ++]);
break;
case lq_msg:
tgl_do_send_message (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], strlen (lua_ptr[p + 2]), lua_msg_cb, lua_ptr[p]);
tgl_do_send_message (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], strlen (lua_ptr[p + 2]), lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_rename_chat:
tgl_do_rename_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_rename_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_send_photo:
tgl_do_send_photo (tgl_message_media_photo, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_send_photo (TLS, tgl_message_media_photo, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_send_video:
tgl_do_send_photo (tgl_message_media_video, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_send_photo (TLS, tgl_message_media_video, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_send_audio:
tgl_do_send_photo (tgl_message_media_audio, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_send_photo (TLS, tgl_message_media_audio, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_send_document:
tgl_do_send_photo (tgl_message_media_document, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_send_photo (TLS, tgl_message_media_document, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_send_text:
tgl_do_send_text (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_send_text (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_chat_set_photo:
tgl_do_set_chat_photo (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
tgl_do_set_chat_photo (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_ptr[p + 2], lua_msg_cb, lua_ptr[p]);
free (lua_ptr[p + 2]);
p += 3;
break;
case lq_load_photo:
M = lua_ptr[p + 1];
if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_photo_encr)) {
lua_file_cb (lua_ptr[p], 0, 0);
lua_file_cb (TLS, lua_ptr[p], 0, 0);
} else {
if (M->media.type == tgl_message_media_photo) {
tgl_do_load_photo (&M->media.photo, lua_file_cb, lua_ptr[p]);
tgl_do_load_photo (TLS, &M->media.photo, lua_file_cb, lua_ptr[p]);
} else {
tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]);
tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]);
}
}
p += 2;
@ -917,12 +928,12 @@ void lua_do_all (void) {
case lq_load_video:
M = lua_ptr[p + 1];
if (!M || (M->media.type != tgl_message_media_video && M->media.type != tgl_message_media_video_encr)) {
lua_file_cb (lua_ptr[p], 0, 0);
lua_file_cb (TLS, lua_ptr[p], 0, 0);
} else {
if (M->media.type == tgl_message_media_video) {
tgl_do_load_video (&M->media.video, lua_file_cb, lua_ptr[p]);
tgl_do_load_video (TLS, &M->media.video, lua_file_cb, lua_ptr[p]);
} else {
tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]);
tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]);
}
}
p += 2;
@ -930,21 +941,21 @@ void lua_do_all (void) {
case lq_load_video_thumb:
M = lua_ptr[p + 1];
if (!M || (M->media.type != tgl_message_media_video)) {
lua_file_cb (lua_ptr[p], 0, 0);
lua_file_cb (TLS, lua_ptr[p], 0, 0);
} else {
tgl_do_load_video_thumb (&M->media.video, lua_file_cb, lua_ptr[p]);
tgl_do_load_video_thumb (TLS, &M->media.video, lua_file_cb, lua_ptr[p]);
}
p += 2;
break;
case lq_load_audio:
M = lua_ptr[p + 1];
if (!M || (M->media.type != tgl_message_media_audio && M->media.type != tgl_message_media_audio_encr)) {
lua_file_cb (lua_ptr[p], 0, 0);
lua_file_cb (TLS, lua_ptr[p], 0, 0);
} else {
if (M->media.type == tgl_message_media_audio) {
tgl_do_load_audio (&M->media.audio, lua_file_cb, lua_ptr[p]);
tgl_do_load_audio (TLS, &M->media.audio, lua_file_cb, lua_ptr[p]);
} else {
tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]);
tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]);
}
}
p += 2;
@ -952,12 +963,12 @@ void lua_do_all (void) {
case lq_load_document:
M = lua_ptr[p + 1];
if (!M || (M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) {
lua_file_cb (lua_ptr[p], 0, 0);
lua_file_cb (TLS, lua_ptr[p], 0, 0);
} else {
if (M->media.type == tgl_message_media_document) {
tgl_do_load_document (&M->media.document, lua_file_cb, lua_ptr[p]);
tgl_do_load_document (TLS, &M->media.document, lua_file_cb, lua_ptr[p]);
} else {
tgl_do_load_encr_video (&M->media.encr_video, lua_file_cb, lua_ptr[p]);
tgl_do_load_encr_video (TLS, &M->media.encr_video, lua_file_cb, lua_ptr[p]);
}
}
p += 2;
@ -965,59 +976,59 @@ void lua_do_all (void) {
case lq_load_document_thumb:
M = lua_ptr[p + 1];
if (!M || (M->media.type != tgl_message_media_document)) {
lua_file_cb (lua_ptr[p], 0, 0);
lua_file_cb (TLS, lua_ptr[p], 0, 0);
} else {
tgl_do_load_document_thumb (&M->media.document, lua_file_cb, lua_ptr[p]);
tgl_do_load_document_thumb (TLS, &M->media.document, lua_file_cb, lua_ptr[p]);
}
p += 2;
break;
case lq_fwd:
tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]);
tgl_do_forward_message (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]);
p += 3;
break;
case lq_fwd_media:
tgl_do_forward_media (((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]);
tgl_do_forward_media (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((struct tgl_message *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]);
p += 3;
break;
case lq_chat_info:
tgl_do_get_chat_info (((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_chat_cb, lua_ptr[p]);
tgl_do_get_chat_info (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_chat_cb, lua_ptr[p]);
p += 2;
break;
case lq_user_info:
tgl_do_get_user_info (((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_user_cb, lua_ptr[p]);
tgl_do_get_user_info (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, 0, lua_user_cb, lua_ptr[p]);
p += 2;
break;
case lq_history:
tgl_do_get_history (((tgl_peer_t *)lua_ptr[p + 1])->id, (long)lua_ptr[p + 2], 0, lua_msg_list_cb, lua_ptr[p]);
tgl_do_get_history (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, (long)lua_ptr[p + 2], 0, lua_msg_list_cb, lua_ptr[p]);
p += 3;
break;
case lq_chat_add_user:
tgl_do_add_user_to_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, 10, lua_msg_cb, lua_ptr[p]);
tgl_do_add_user_to_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, 10, lua_msg_cb, lua_ptr[p]);
p += 3;
break;
case lq_chat_del_user:
tgl_do_del_user_from_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]);
tgl_do_del_user_from_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, ((tgl_peer_t *)lua_ptr[p + 2])->id, lua_msg_cb, lua_ptr[p]);
p += 3;
break;
case lq_add_contact:
s1 = lua_ptr[p + 1];
s2 = lua_ptr[p + 2];
s3 = lua_ptr[p + 3];
tgl_do_add_contact (s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 0, lua_contact_list_cb, lua_ptr[p]);
tgl_do_add_contact (TLS, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 0, lua_contact_list_cb, lua_ptr[p]);
free (s1);
free (s2);
free (s3);
p += 4;
break;
case lq_del_contact:
tgl_do_del_contact (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]);
tgl_do_del_contact (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]);
p += 2;
break;
case lq_rename_contact:
s1 = lua_ptr[p + 1];
s2 = lua_ptr[p + 2];
s3 = lua_ptr[p + 3];
tgl_do_add_contact (s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 1, lua_contact_list_cb, lua_ptr[p]);
tgl_do_add_contact (TLS, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), 1, lua_contact_list_cb, lua_ptr[p]);
free (s1);
free (s2);
free (s3);
@ -1025,85 +1036,85 @@ void lua_do_all (void) {
break;
case lq_search:
s = lua_ptr[p + 2];
tgl_do_msg_search (((tgl_peer_t *)lua_ptr[p + 1])->id, 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]);
tgl_do_msg_search (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]);
free (s);
p += 3;
break;
case lq_global_search:
s = lua_ptr[p + 1];
tgl_do_msg_search (tgl_set_peer_id (TGL_PEER_UNKNOWN, 0), 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]);
tgl_do_msg_search (TLS, tgl_set_peer_id (TGL_PEER_UNKNOWN, 0), 0, 0, 40, 0, s, lua_msg_list_cb, lua_ptr[p]);
free (s);
p += 2;
break;
case lq_mark_read:
tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]);
tgl_do_mark_read (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]);
p += 2;
break;
case lq_set_profile_photo:
s = lua_ptr[p + 1];
tgl_do_set_profile_photo (s, lua_empty_cb, lua_ptr[p]);
tgl_do_set_profile_photo (TLS, s, lua_empty_cb, lua_ptr[p]);
free (s);
p += 2;
break;
case lq_set_profile_name:
s1 = lua_ptr[p + 1];
s2 = lua_ptr[p + 1];
tgl_do_set_profile_name (s1, s2, lua_user_cb, lua_ptr[p]);
tgl_do_set_profile_name (TLS, s1, s2, lua_user_cb, lua_ptr[p]);
free (s1);
free (s2);
p += 3;
break;
case lq_create_secret_chat:
tgl_do_create_secret_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, lua_secret_chat_cb, lua_ptr[p]);
tgl_do_create_secret_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, lua_secret_chat_cb, lua_ptr[p]);
p += 2;
break;
case lq_create_group_chat:
s = lua_ptr[p + 2];
tgl_do_create_group_chat (((tgl_peer_t *)lua_ptr[p + 1])->id, s, lua_msg_cb, lua_ptr[p]);
tgl_do_create_group_chat (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, s, lua_msg_cb, lua_ptr[p]);
free (s);
p += 3;
break;
case lq_delete_msg:
tgl_do_delete_msg (((struct tgl_message *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]);
tgl_do_delete_msg (TLS, ((struct tgl_message *)lua_ptr[p + 1])->id, lua_empty_cb, lua_ptr[p]);
p += 2;
break;
case lq_restore_msg:
tgl_do_delete_msg ((long)lua_ptr[p + 1], lua_empty_cb, lua_ptr[p]);
tgl_do_delete_msg (TLS, (long)lua_ptr[p + 1], lua_empty_cb, lua_ptr[p]);
p += 2;
break;
case lq_accept_secret_chat:
tgl_do_accept_encr_chat_request (lua_ptr[p + 1], lua_secret_chat_cb, lua_ptr[p]);
tgl_do_accept_encr_chat_request (TLS, lua_ptr[p + 1], lua_secret_chat_cb, lua_ptr[p]);
p += 2;
break;
case lq_send_contact:
s1 = lua_ptr[p + 2];
s2 = lua_ptr[p + 3];
s3 = lua_ptr[p + 4];
tgl_do_send_contact (((tgl_peer_t *)lua_ptr[p + 1])->id, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), lua_msg_cb, lua_ptr[p]);
tgl_do_send_contact (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id, s1, strlen (s1), s2, strlen (s2), s3, strlen (s3), lua_msg_cb, lua_ptr[p]);
free (s1);
free (s2);
free (s3);
p += 5;
break;
case lq_status_online:
tgl_do_update_status (1, lua_empty_cb, lua_ptr[p]);
tgl_do_update_status (TLS, 1, lua_empty_cb, lua_ptr[p]);
p ++;
break;
case lq_status_offline:
tgl_do_update_status (0, lua_empty_cb, lua_ptr[p]);
tgl_do_update_status (TLS, 0, lua_empty_cb, lua_ptr[p]);
p ++;
break;
case lq_extf:
s = lua_ptr[p + 1];
tgl_do_send_extf (s, strlen (s), lua_str_cb, lua_ptr[p]);
tgl_do_send_extf (TLS, s, strlen (s), lua_str_cb, lua_ptr[p]);
free (s);
p += 2;
break;
case lq_send_location:
if (sizeof (void *) == 4) {
tgl_do_send_location (((tgl_peer_t *)lua_ptr[p + 1])->id , *(float *)(lua_ptr + p + 2), *(float *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]);
tgl_do_send_location (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id , *(float *)(lua_ptr + p + 2), *(float *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]);
} else {
tgl_do_send_location (((tgl_peer_t *)lua_ptr[p + 1])->id , *(double *)(lua_ptr + p + 2), *(double *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]);
tgl_do_send_location (TLS, ((tgl_peer_t *)lua_ptr[p + 1])->id , *(double *)(lua_ptr + p + 2), *(double *)(lua_ptr + p + 3), lua_msg_cb, lua_ptr[p]);
}
p += 4;
break;
@ -1243,11 +1254,11 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) {
break;
}
if (sscanf (s, "user#id%lld", &num) == 1 && num > 0) {
tgl_insert_empty_user (num);
P = tgl_peer_get (TGL_MK_USER (num));
tgl_insert_empty_user (TLS, num);
P = tgl_peer_get (TLS, TGL_MK_USER (num));
} else if (sscanf (s, "chat#id%lld", &num) == 1 && num > 0) {
tgl_insert_empty_chat (num);
P = tgl_peer_get (TGL_MK_CHAT (num));
tgl_insert_empty_chat (TLS, num);
P = tgl_peer_get (TLS, TGL_MK_CHAT (num));
} else {
P = get_peer (s);
}
@ -1321,7 +1332,7 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) {
num = atoll (s);
M = tgl_message_get (num);
M = tgl_message_get (TLS, num);
if (!M || !(M->flags & FLAG_CREATED)) {
ok = 0;
@ -1398,7 +1409,7 @@ static int postpone_from_lua (lua_State *L) {
int *t = malloc (16);
assert (t);
struct event *ev = evtimer_new (tgl_state.ev_base, lua_postpone_alarm, t);
struct event *ev = evtimer_new (TLS->ev_base, lua_postpone_alarm, t);
t[0] = a1;
t[1] = a2;
*(void **)(t + 2) = ev;

35
main.c
View File

@ -114,6 +114,8 @@ int port;
int use_ids;
char *start_command;
struct tgl_state *TLS;
void set_default_username (const char *s) {
if (default_username) {
tfree_str (default_username);
@ -348,7 +350,7 @@ void parse_config (void) {
strcpy (buf + l, "test");
config_lookup_bool (&conf, buf, &test_mode);
if (test_mode) {
tgl_set_test_mode ();
tgl_set_test_mode (TLS);
}
strcpy (buf + l, "log_level");
@ -378,20 +380,20 @@ void parse_config (void) {
strcpy (buf + l, "pfs_enabled");
config_lookup_bool (&conf, buf, &pfs_enabled);
if (pfs_enabled) {
tgl_enable_pfs ();
tgl_enable_pfs (TLS);
}
if (binlog_enabled) {
parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory);
tgl_set_binlog_mode (1);
tgl_set_binlog_path (binlog_file_name);
tgl_set_binlog_mode (TLS, 1);
tgl_set_binlog_path (TLS, binlog_file_name);
} else {
tgl_set_binlog_mode (0);
tgl_set_binlog_mode (TLS, 0);
parse_config_val (&conf, &state_file_name, "state_file", STATE_FILE, config_directory);
parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_FILE, config_directory);
//tgl_set_auth_file_path (auth_file_name);
}
tgl_set_download_directory (downloads_directory);
tgl_set_download_directory (TLS, downloads_directory);
if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
if (!disable_output) {
@ -414,16 +416,16 @@ void parse_config (void) {
if (binlog_enabled) {
tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE);
tgl_set_binlog_mode (1);
tgl_set_binlog_path (binlog_file_name);
tgl_set_binlog_mode (TLS, 1);
tgl_set_binlog_path (TLS, binlog_file_name);
} else {
tgl_set_binlog_mode (0);
tgl_set_binlog_mode (TLS, 0);
//tgl_set_auth_file_path (auth_file_name;
tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE);
tasprintf (&state_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, STATE_FILE);
tasprintf (&secret_chat_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, SECRET_CHAT_FILE);
}
tgl_set_download_directory (downloads_directory);
tgl_set_download_directory (TLS, downloads_directory);
if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) {
if (!disable_output) {
printf ("[%s] created\n", downloads_directory);
@ -568,6 +570,7 @@ int change_user_group () {
char *unix_socket;
void args_parse (int argc, char **argv) {
TLS = tgl_state_alloc ();
int opt = 0;
while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:I"
#ifdef HAVE_LIBCONFIG
@ -586,10 +589,10 @@ void args_parse (int argc, char **argv) {
break;
case 'k':
//rsa_public_key_name = tstrdup (optarg);
tgl_set_rsa_key (optarg);
tgl_set_rsa_key (TLS, optarg);
break;
case 'v':
tgl_incr_verbosity ();
tgl_incr_verbosity (TLS);
verbosity ++;
break;
case 'N':
@ -722,8 +725,8 @@ void sig_term_handler (int signum __attribute__ ((unused))) {
if (write (1, "SIGTERM/SIGINT received\n", 25) < 0) {
// Sad thing
}
if (tgl_state.ev_base) {
event_base_loopbreak(tgl_state.ev_base);
if (TLS && TLS->ev_base) {
event_base_loopbreak (TLS->ev_base);
}
sigterm_cnt ++;
}
@ -841,8 +844,8 @@ int main (int argc, char **argv) {
running_for_first_time ();
parse_config ();
tgl_set_rsa_key ("/etc/" PROG_NAME "/server.pub");
tgl_set_rsa_key ("tg-server.pub");
tgl_set_rsa_key (TLS, "/etc/" PROG_NAME "/server.pub");
tgl_set_rsa_key (TLS, "tg-server.pub");
get_terminal_attributes ();

File diff suppressed because it is too large Load Diff

View File

@ -45,16 +45,16 @@ struct tgl_dc;
struct connection;
long long tglmp_encrypt_send_message (struct connection *c, int *msg, int msg_ints, int flags);
void tglmp_dc_create_session (struct tgl_dc *DC);
int tglmp_check_g (unsigned char p[256], BIGNUM *g);
int tglmp_check_DH_params (BIGNUM *p, int g);
struct tgl_dc *tglmp_alloc_dc (int id, char *ip, int port);
void tglmp_regenerate_temp_auth_key (struct tgl_dc *D);
long long tglmp_encrypt_send_message (struct tgl_state *TLS, struct connection *c, int *msg, int msg_ints, int flags);
void tglmp_dc_create_session (struct tgl_state *TLS, struct tgl_dc *DC);
int tglmp_check_g (struct tgl_state *TLS, unsigned char p[256], BIGNUM *g);
int tglmp_check_DH_params (struct tgl_state *TLS, BIGNUM *p, int g);
struct tgl_dc *tglmp_alloc_dc (struct tgl_state *TLS, int id, char *ip, int port);
void tglmp_regenerate_temp_auth_key (struct tgl_state *TLS, struct tgl_dc *D);
void tgln_insert_msg_id (struct tgl_session *S, long long id);
void tglmp_on_start (void);
void tgl_dc_authorize (struct tgl_dc *DC);
void tgls_free_dc (struct tgl_dc *DC);
void tgls_free_pubkey (void);
void tgln_insert_msg_id (struct tgl_state *TLS, struct tgl_session *S, long long id);
void tglmp_on_start (struct tgl_state *TLS);
void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC);
void tgls_free_dc (struct tgl_state *TLS, struct tgl_dc *DC);
void tgls_free_pubkey (struct tgl_state *TLS);
#endif

View File

@ -57,7 +57,7 @@ static long long rsa_encrypted_chunks, rsa_decrypted_chunks;
//int verbosity;
static int get_random_bytes (unsigned char *buf, int n) {
static int get_random_bytes (struct tgl_state *TLS, unsigned char *buf, int n) {
int r = 0, h = open ("/dev/random", O_RDONLY | O_NONBLOCK);
if (h >= 0) {
r = read (h, buf, n);
@ -107,7 +107,7 @@ static __inline__ unsigned long long rdtsc (void) {
}
#endif
void tgl_prng_seed (const char *password_filename, int password_length) {
void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int password_length) {
struct timespec T;
tgl_my_clock_gettime (CLOCK_REALTIME, &T);
RAND_add (&T, sizeof (T), 4.0);
@ -120,7 +120,7 @@ void tgl_prng_seed (const char *password_filename, int password_length) {
p = getppid ();
RAND_add (&p, sizeof (p), 0.0);
unsigned char rb[32];
int s = get_random_bytes (rb, 32);
int s = get_random_bytes (TLS, rb, 32);
if (s > 0) {
RAND_add (rb, s, s);
}
@ -142,8 +142,8 @@ void tgl_prng_seed (const char *password_filename, int password_length) {
tfree_secure (a, password_length);
}
}
tgl_state.BN_ctx = BN_CTX_new ();
ensure_ptr (tgl_state.BN_ctx);
TLS->BN_ctx = BN_CTX_new ();
ensure_ptr (TLS->BN_ctx);
}
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
@ -250,7 +250,7 @@ int tgl_fetch_bignum (BIGNUM *x) {
return l;
}
int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) {
int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E) {
int pad = (255000 - from_len - 32) % 255 + 32;
int chunks = (from_len + pad) / 255;
int bits = BN_num_bits (N);
@ -265,7 +265,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N
rsa_encrypted_chunks += chunks;
for (i = 0; i < chunks; i++) {
BN_bin2bn ((unsigned char *) from, 255, &x);
assert (BN_mod_exp (&y, &x, E, N, tgl_state.BN_ctx) == 1);
assert (BN_mod_exp (&y, &x, E, N, TLS->BN_ctx) == 1);
unsigned l = 256 - BN_num_bytes (&y);
assert (l <= 256);
memset (to, 0, l);
@ -277,7 +277,7 @@ int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N
return chunks * 256;
}
int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) {
int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D) {
if (from_len < 0 || from_len > 0x1000 || (from_len & 0xff)) {
return -1;
}
@ -292,7 +292,7 @@ int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N
for (i = 0; i < chunks; i++) {
++rsa_decrypted_chunks;
BN_bin2bn ((unsigned char *) from, 256, &x);
assert (BN_mod_exp (&y, &x, D, N, tgl_state.BN_ctx) == 1);
assert (BN_mod_exp (&y, &x, D, N, TLS->BN_ctx) == 1);
int l = BN_num_bytes (&y);
if (l > 255) {
BN_free (&x);

View File

@ -117,7 +117,7 @@ struct encrypted_message {
//BN_CTX *BN_ctx;
void tgl_prng_seed (const char *password_filename, int password_length);
void tgl_prng_seed (struct tgl_state *TLS, const char *password_filename, int password_length);
int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen);
long long tgl_do_compute_rsa_key_fingerprint (RSA *key);
@ -356,8 +356,8 @@ static inline int in_remaining (void) {
//int get_random_bytes (unsigned char *buf, int n);
int tgl_pad_rsa_encrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E);
int tgl_pad_rsa_decrypt (char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D);
int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *E);
int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *to, int size, BIGNUM *N, BIGNUM *D);
//extern long long rsa_encrypted_chunks, rsa_decrypted_chunks;

34
net.c
View File

@ -71,6 +71,7 @@ static void fail_connection (struct connection *c);
static void start_ping_timer (struct connection *c);
static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG + 2,"ping alarm\n");
assert (c->state == conn_ready || c->state == conn_connecting);
if (tglt_get_double_time () - c->last_receive_time > 6 * PING_TIMEOUT) {
@ -78,7 +79,7 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
c->state = conn_failed;
fail_connection (c);
} else if (tglt_get_double_time () - c->last_receive_time > 3 * PING_TIMEOUT && c->state == conn_ready) {
tgl_do_send_ping (c);
tgl_do_send_ping (c->TLS, c);
start_ping_timer (c);
} else {
start_ping_timer (c);
@ -124,6 +125,7 @@ static void delete_connection_buffer (struct connection_buffer *b) {
}
int tgln_write_out (struct connection *c, const void *_data, int len) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "write_out: %d bytes\n", len);
const unsigned char *data = _data;
if (!len) { return 0; }
@ -242,14 +244,16 @@ static void try_write (struct connection *c);
static void conn_try_read (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG + 1, "Try read. Fd = %d\n", c->fd);
try_read (c);
}
static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
struct tgl_state *TLS = c->TLS;
if (c->state == conn_connecting) {
c->state = conn_ready;
c->methods->ready (c);
c->methods->ready (TLS, c);
}
try_write (c);
if (c->out_bytes) {
@ -257,8 +261,9 @@ static void conn_try_write (evutil_socket_t fd, short what, void *arg) {
}
}
struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) {
struct connection *tgln_create_connection (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods) {
struct connection *c = talloc0 (sizeof (*c));
c->TLS = TLS;
int fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
vlogprintf (E_ERROR, "Can not create socket: %m\n");
@ -300,12 +305,12 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg
assert (!Connections[fd]);
Connections[fd] = c;
c->ping_ev = evtimer_new (tgl_state.ev_base, ping_alarm, c);
c->fail_ev = evtimer_new (tgl_state.ev_base, fail_alarm, c);
c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
c->ping_ev = evtimer_new (TLS->ev_base, ping_alarm, c);
c->fail_ev = evtimer_new (TLS->ev_base, fail_alarm, c);
c->write_ev = event_new (TLS->ev_base, c->fd, EV_WRITE, conn_try_write, c);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
c->read_ev = event_new (TLS->ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
start_ping_timer (c);
@ -322,6 +327,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct tg
}
static void restart_connection (struct connection *c) {
struct tgl_state *TLS = c->TLS;
if (c->last_connect_time == time (0)) {
start_fail_timer (c);
return;
@ -369,10 +375,10 @@ static void restart_connection (struct connection *c) {
start_ping_timer (c);
Connections[fd] = c;
c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
c->write_ev = event_new (TLS->ev_base, c->fd, EV_WRITE, conn_try_write, c);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
c->read_ev = event_new (TLS->ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
char byte = 0xef;
@ -381,6 +387,7 @@ static void restart_connection (struct connection *c) {
}
static void fail_connection (struct connection *c) {
struct tgl_state *TLS = c->TLS;
if (c->state == conn_ready || c->state == conn_connecting) {
stop_ping_timer (c);
}
@ -411,6 +418,7 @@ static void fail_connection (struct connection *c) {
//extern FILE *log_net_f;
static void try_write (struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "try write: fd = %d\n", c->fd);
int x = 0;
while (c->out_head) {
@ -443,6 +451,7 @@ static void try_write (struct connection *c) {
static void try_rpc_read (struct connection *c) {
assert (c->in_head);
struct tgl_state *TLS = c->TLS;
while (1) {
if (c->in_bytes < 1) { return; }
@ -472,11 +481,12 @@ static void try_rpc_read (struct connection *c) {
len *= 4;
int op;
assert (tgln_read_in_lookup (c, &op, 4) == 4);
c->methods->execute (c, op, len);
c->methods->execute (TLS, c, op, len);
}
}
static void try_read (struct connection *c) {
struct tgl_state *TLS = c->TLS;
vlogprintf (E_DEBUG, "try read: fd = %d\n", c->fd);
if (!c->in_tail) {
c->in_head = c->in_tail = new_connection_buffer (1 << 20);
@ -518,7 +528,7 @@ static void try_read (struct connection *c) {
try_rpc_read (c);
}
}
/*
int tgl_connections_make_poll_array (struct pollfd *fds, int max) {
int _max = max;
int i;
@ -562,7 +572,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) {
}
}
}
}
}*/
static void incr_out_packet_num (struct connection *c) {
c->out_packet_num ++;

26
net.h
View File

@ -20,23 +20,6 @@
#ifndef __NET_H__
#define __NET_H__
#include <poll.h>
#define DC_SERIALIZED_MAGIC 0x64582faa
#define DC_SERIALIZED_MAGIC_V2 0x94032abb
#define STATE_FILE_MAGIC 0x84217a0d
#define SECRET_CHAT_FILE_MAGIC 0xa9840add
struct tgl_dc_serialized {
int magic;
int port;
char ip[64];
char user[64];
char auth_key[256];
long long auth_key_id, server_salt;
int authorized;
};
struct connection_buffer {
unsigned char *start;
unsigned char *end;
@ -71,6 +54,7 @@ struct connection {
int last_connect_time;
int in_fail_timer;
struct mtproto_methods *methods;
struct tgl_state *TLS;
struct tgl_session *session;
struct tgl_dc *dc;
void *extra;
@ -88,16 +72,16 @@ void tgln_flush_out (struct connection *c);
int tgln_read_in (struct connection *c, void *data, int len);
int tgln_read_in_lookup (struct connection *c, void *data, int len);
void tgln_insert_msg_id (struct tgl_session *S, long long id);
//void tgln_insert_msg_id (struct tgl_session *S, long long id);
extern struct tgl_net_methods tgl_conn_methods;
//void create_all_outbound_connections (void);
//struct connection *create_connection (const char *host, int port, struct tgl_session *session, struct connection_methods *methods);
struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port);
void tgln_dc_create_session (struct tgl_dc *DC, struct mtproto_methods *methods);
struct connection *tgln_create_connection (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
//struct tgl_dc *tgln_alloc_dc (int id, char *ip, int port);
//void tgln_dc_create_session (struct tgl_dc *DC, struct mtproto_methods *methods);
struct connection *tgln_create_connection (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
#define GET_DC(c) (c->session->dc)
#endif

1152
queries.c

File diff suppressed because it is too large Load Diff

View File

@ -24,16 +24,14 @@
#include "auto.h"
#include "tgl-layout.h"
struct event;
#define QUERY_ACK_RECEIVED 1
#define QUERY_FORCE_SEND 2
struct query;
struct query_methods {
int (*on_answer)(struct query *q);
int (*on_error)(struct query *q, int error_code, int len, char *error);
int (*on_timeout)(struct query *q);
int (*on_answer)(struct tgl_state *TLS, struct query *q);
int (*on_error)(struct tgl_state *TLS, struct query *q, int error_code, int len, char *error);
int (*on_timeout)(struct tgl_state *TLS, struct query *q);
struct paramed_type *type;
};
@ -45,7 +43,7 @@ struct query {
long long session_id;
void *data;
struct query_methods *methods;
struct event *ev;
struct tgl_timer *ev;
struct tgl_dc *DC;
struct tgl_session *session;
struct paramed_type *type;
@ -55,20 +53,20 @@ struct query {
};
struct query *tglq_send_query (struct tgl_dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra);
void tglq_query_ack (long long id);
void tglq_query_error (long long id);
void tglq_query_result (long long id);
void tglq_query_restart (long long id);
struct query *tglq_send_query (struct tgl_state *TLS, struct tgl_dc *DC, int len, void *data, struct query_methods *methods, void *extra, void *callback, void *callback_extra);
void tglq_query_ack (struct tgl_state *TLS, long long id);
void tglq_query_error (struct tgl_state *TLS, long long id);
void tglq_query_result (struct tgl_state *TLS, long long id);
void tglq_query_restart (struct tgl_state *TLS, long long id);
double next_timer_in (void);
void work_timers (void);
//double next_timer_in (void);
//void work_timers (void);
//extern struct query_methods help_get_config_methods;
double get_double_time (void);
void tgl_do_send_bind_temp_key (struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id);
void tgl_do_send_bind_temp_key (struct tgl_state *TLS, struct tgl_dc *D, long long nonce, int expires_at, void *data, int len, long long msg_id);
// For binlog

File diff suppressed because it is too large Load Diff

View File

@ -23,34 +23,35 @@
#include <assert.h>
#include "tgl-layout.h"
#include "tgl-fetch.h"
#include "tgl.h"
char *tgls_default_create_print_name (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
char *tgls_default_create_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
void tgls_free_user (struct tgl_user *U);
void tgls_free_chat (struct tgl_chat *U);
void tgls_free_photo (struct tgl_photo *P);
void tgls_free_message (struct tgl_message *M);
void tgls_clear_message (struct tgl_message *M);
void tgls_free_user (struct tgl_state *TLS, struct tgl_user *U);
void tgls_free_chat (struct tgl_state *TLS, struct tgl_chat *U);
void tgls_free_photo (struct tgl_state *TLS, struct tgl_photo *P);
void tgls_free_message (struct tgl_state *TLS, struct tgl_message *M);
void tgls_clear_message (struct tgl_state *TLS, struct tgl_message *M);
struct tgl_message *tglm_message_alloc (long long id);
void tglm_message_insert_tree (struct tgl_message *M);
void tglm_update_message_id (struct tgl_message *M, long long id);
void tglm_message_insert (struct tgl_message *M);
void tglm_message_insert_unsent (struct tgl_message *M);
void tglm_message_remove_unsent (struct tgl_message *M);
void tglm_send_all_unsent (void);
void tglm_message_remove_tree (struct tgl_message *M);
void tglm_message_add_peer (struct tgl_message *M);
void tglm_message_del_peer (struct tgl_message *M);
void tglm_message_del_use (struct tgl_message *M);
void tglm_message_add_use (struct tgl_message *M);
struct tgl_message *tglm_message_alloc (struct tgl_state *TLS, long long id);
void tglm_message_insert_tree (struct tgl_state *TLS, struct tgl_message *M);
void tglm_update_message_id (struct tgl_state *TLS, struct tgl_message *M, long long id);
void tglm_message_insert (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_insert_unsent (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_remove_unsent (struct tgl_state *TLS, struct tgl_message *M);
void tglm_send_all_unsent (struct tgl_state *TLS);
void tglm_message_remove_tree (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_add_peer (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_del_peer (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_del_use (struct tgl_state *TLS, struct tgl_message *M);
void tglm_message_add_use (struct tgl_state *TLS, struct tgl_message *M);
void tglp_peer_insert_name (tgl_peer_t *P);
void tglp_peer_delete_name (tgl_peer_t *P);
void tglp_insert_encrypted_chat (tgl_peer_t *P);
void tglp_insert_user (tgl_peer_t *P);
void tglp_insert_chat (tgl_peer_t *P);
void tglp_peer_insert_name (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_peer_delete_name (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_encrypted_chat (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_user (struct tgl_state *TLS, tgl_peer_t *P);
void tglp_insert_chat (struct tgl_state *TLS, tgl_peer_t *P);
enum tgl_typing_status tglf_fetch_typing (void);
#endif

View File

@ -19,35 +19,36 @@
*/
#ifndef __TGL_FETCH_H__
#define __TGL_FETCH_H__
#include "tgl.h"
int tglf_fetch_file_location (struct tgl_file_location *loc);
int tglf_fetch_user_status (struct tgl_user_status *S);
int tglf_fetch_user (struct tgl_user *U);
struct tgl_user *tglf_fetch_alloc_user (void);
struct tgl_user *tglf_fetch_alloc_user_full (void);
struct tgl_chat *tglf_fetch_alloc_chat (void);
struct tgl_chat *tglf_fetch_alloc_chat_full (void);
struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (void);
struct tgl_message *tglf_fetch_alloc_message (void);
struct tgl_message *tglf_fetch_alloc_geo_message (void);
struct tgl_message *tglf_fetch_alloc_message_short (void);
struct tgl_message *tglf_fetch_alloc_message_short_chat (void);
struct tgl_message *tglf_fetch_alloc_encrypted_message (void);
void tglf_fetch_encrypted_message_file (struct tgl_message_media *M);
tgl_peer_id_t tglf_fetch_peer_id (void);
int tglf_fetch_file_location (struct tgl_state *TLS, struct tgl_file_location *loc);
int tglf_fetch_user_status (struct tgl_state *TLS, struct tgl_user_status *S);
int tglf_fetch_user (struct tgl_state *TLS, struct tgl_user *U);
struct tgl_user *tglf_fetch_alloc_user (struct tgl_state *TLS);
struct tgl_user *tglf_fetch_alloc_user_full (struct tgl_state *TLS);
struct tgl_chat *tglf_fetch_alloc_chat (struct tgl_state *TLS);
struct tgl_chat *tglf_fetch_alloc_chat_full (struct tgl_state *TLS);
struct tgl_secret_chat *tglf_fetch_alloc_encrypted_chat (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_message (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_geo_message (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_message_short (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_message_short_chat (struct tgl_state *TLS);
struct tgl_message *tglf_fetch_alloc_encrypted_message (struct tgl_state *TLS);
void tglf_fetch_encrypted_message_file (struct tgl_state *TLS, struct tgl_message_media *M);
tgl_peer_id_t tglf_fetch_peer_id (struct tgl_state *TLS);
void tglf_fetch_message_media (struct tgl_message_media *M);
void tglf_fetch_message_media_encrypted (struct tgl_message_media *M);
void tglf_fetch_message_action (struct tgl_message_action *M);
void tglf_fetch_message_action_encrypted (struct tgl_message_action *M);
void tglf_fetch_photo (struct tgl_photo *P);
void tglf_fetch_message_media (struct tgl_state *TLS, struct tgl_message_media *M);
void tglf_fetch_message_media_encrypted (struct tgl_state *TLS, struct tgl_message_media *M);
void tglf_fetch_message_action (struct tgl_state *TLS, struct tgl_message_action *M);
void tglf_fetch_message_action_encrypted (struct tgl_state *TLS, struct tgl_message_action *M);
void tglf_fetch_photo (struct tgl_state *TLS, struct tgl_photo *P);
void tglf_fetch_chat (struct tgl_chat *C);
void tglf_fetch_chat_full (struct tgl_chat *C);
void tglf_fetch_chat (struct tgl_state *TLS, struct tgl_chat *C);
void tglf_fetch_chat_full (struct tgl_state *TLS, struct tgl_chat *C);
void tglf_fetch_audio (struct tgl_audio *V);
void tglf_fetch_video (struct tgl_video *V);
void tglf_fetch_document (struct tgl_document *V);
void tglf_fetch_message (struct tgl_message *M);
void tglf_fetch_geo_message (struct tgl_message *M);
void tglf_fetch_audio (struct tgl_state *TLS, struct tgl_audio *V);
void tglf_fetch_video (struct tgl_state *TLS, struct tgl_video *V);
void tglf_fetch_document (struct tgl_state *TLS, struct tgl_document *V);
void tglf_fetch_message (struct tgl_state *TLS, struct tgl_message *M);
void tglf_fetch_geo_message (struct tgl_state *TLS, struct tgl_message *M);
#endif

View File

@ -23,8 +23,8 @@
#define vlogprintf(verbosity_level,...) \
do { \
if (tgl_state.verbosity >= verbosity_level) { \
tgl_state.callback.logprintf (__VA_ARGS__); \
if (TLS->verbosity >= verbosity_level) { \
TLS->callback.logprintf (__VA_ARGS__); \
} \
} while (0)

View File

@ -62,8 +62,7 @@ struct tgl_session {
int seq_no;
struct connection *c;
struct tree_long *ack_tree;
struct event *ev;
//struct event_timer ev;
struct tgl_timer *ev;
};
struct tgl_dc {
@ -83,7 +82,7 @@ struct tgl_dc {
long long temp_auth_key_id;
long long server_salt;
struct event *ev;
struct tgl_timer *ev;
int server_time_delta;
double server_time_udelta;

69
tgl-timers.c Normal file
View File

@ -0,0 +1,69 @@
/*
This file is part of tgl-library
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Copyright Vitaly Valtman 2013-2014
*/
#include "config.h"
#ifdef EVENT_V2
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#else
#include <event.h>
#include "event-old.h"
#endif
#include "tgl.h"
#include <stdlib.h>
static void timer_alarm (evutil_socket_t fd, short what, void *arg) {
void **p = arg;
((void (*)(struct tgl_state *, void *))p[1]) (p[0], p[2]);
}
struct tgl_timer *tgl_timer_alloc (struct tgl_state *TLS, void (*cb)(struct tgl_state *TLS, void *arg), void *arg) {
void **p = malloc (sizeof (void *) * 3);
p[0] = TLS;
p[1] = cb;
p[2] = arg;
return (void *)evtimer_new (TLS->ev_base, timer_alarm, p);
}
void tgl_timer_insert (struct tgl_timer *t, double p) {
if (p < 0) { p = 0; }
double e = p - (int)p;
if (e < 0) { e = 0; }
struct timeval pv = { (int)p, (int)(e * 1e6)};
event_add ((void *)t, &pv);
}
void tgl_timer_delete (struct tgl_timer *t) {
event_del ((void *)t);
}
void tgl_timer_free (struct tgl_timer *t) {
void *arg = event_get_callback_arg ((void *)t);
free (arg);
event_free ((void *)t);
}
struct tgl_timer_methods tgl_libevent_timers = {
.alloc = tgl_timer_alloc,
.insert = tgl_timer_insert,
.delete = tgl_timer_delete,
.free = tgl_timer_free
};

27
tgl-timers.h Normal file
View File

@ -0,0 +1,27 @@
/*
This file is part of tgl-library
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Copyright Vitaly Valtman 2013-2014
*/
#ifndef __TGL_TIMERS_H__
#define __TGL_TIMERS_H__
#include "tgl.h"
extern struct tgl_timer_methods tgl_libevent_timers;
#endif

62
tgl.c
View File

@ -26,70 +26,60 @@
#include "tools.h"
#include "mtproto-client.h"
#include "structures.h"
#include "net.h"
#ifdef EVENT_V2
#include <event2/event.h>
#else
#include <event.h>
#include "event-old.h"
#endif
//#include "net.h"
#include <assert.h>
struct tgl_state tgl_state;
void tgl_set_binlog_mode (int mode) {
tgl_state.binlog_enabled = mode;
void tgl_set_binlog_mode (struct tgl_state *TLS, int mode) {
TLS->binlog_enabled = mode;
}
void tgl_set_binlog_path (const char *path) {
tgl_state.binlog_name = tstrdup (path);
void tgl_set_binlog_path (struct tgl_state *TLS, const char *path) {
TLS->binlog_name = tstrdup (path);
}
void tgl_set_auth_file_path (const char *path) {
tgl_state.auth_file = tstrdup (path);
void tgl_set_auth_file_path (struct tgl_state *TLS, const char *path) {
TLS->auth_file = tstrdup (path);
}
void tgl_set_download_directory (const char *path) {
tgl_state.downloads_directory = tstrdup (path);
void tgl_set_download_directory (struct tgl_state *TLS, const char *path) {
TLS->downloads_directory = tstrdup (path);
}
void tgl_set_callback (struct tgl_update_callback *cb) {
tgl_state.callback = *cb;
void tgl_set_callback (struct tgl_state *TLS, struct tgl_update_callback *cb) {
TLS->callback = *cb;
}
void tgl_set_rsa_key (const char *key) {
assert (tgl_state.rsa_key_num < TGL_MAX_RSA_KEYS_NUM);
tgl_state.rsa_key_list[tgl_state.rsa_key_num ++] = tstrdup (key);
void tgl_set_rsa_key (struct tgl_state *TLS, const char *key) {
assert (TLS->rsa_key_num < TGL_MAX_RSA_KEYS_NUM);
TLS->rsa_key_list[TLS->rsa_key_num ++] = tstrdup (key);
}
void tgl_init (void) {
tgl_state.ev_base = event_base_new ();
if (!tgl_state.net_methods) {
tgl_state.net_methods = &tgl_conn_methods;
void tgl_init (struct tgl_state *TLS) {
assert (TLS->timer_methods);
assert (TLS->net_methods);
if (!TLS->callback.create_print_name) {
TLS->callback.create_print_name = tgls_default_create_print_name;
}
if (!tgl_state.callback.create_print_name) {
tgl_state.callback.create_print_name = tgls_default_create_print_name;
}
if (!tgl_state.temp_key_expire_time) {
tgl_state.temp_key_expire_time = 100000;
if (!TLS->temp_key_expire_time) {
TLS->temp_key_expire_time = 100000;
}
tgl_state.message_list.next_use = &tgl_state.message_list;
tgl_state.message_list.prev_use = &tgl_state.message_list;
TLS->message_list.next_use = &TLS->message_list;
TLS->message_list.prev_use = &TLS->message_list;
tglmp_on_start ();
tglmp_on_start (TLS);
}
int tgl_authorized_dc (struct tgl_dc *DC) {
int tgl_authorized_dc (struct tgl_state *TLS, struct tgl_dc *DC) {
assert (DC);
return (DC->flags & 4) != 0;//DC->auth_key_id;
}
int tgl_signed_dc (struct tgl_dc *DC) {
int tgl_signed_dc (struct tgl_state *TLS, struct tgl_dc *DC) {
assert (DC);
return DC->has_auth;
}

300
tgl.h
View File

@ -36,8 +36,8 @@
#define TG_SERVER_TEST_3 "174.140.142.5"
// JUST RANDOM STRING
#define TGL_BUILD "2234"
#define TGL_VERSION "1.0.6"
#define TGL_BUILD "2590"
#define TGL_VERSION "1.1.0"
#define TGL_ENCRYPTED_LAYER 18
@ -72,36 +72,26 @@ struct tgl_allocator {
};
extern struct tgl_allocator tgl_allocator_release;
extern struct tgl_allocator tgl_allocator_debug;
struct tgl_state;
struct tgl_update_callback {
void (*new_msg)(struct tgl_message *M);
void (*marked_read)(int num, struct tgl_message *list[]);
void (*new_msg)(struct tgl_state *TLS, struct tgl_message *M);
void (*marked_read)(struct tgl_state *TLS, int num, struct tgl_message *list[]);
void (*logprintf)(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
void (*type_notification)(struct tgl_user *U, enum tgl_typing_status status);
void (*type_in_chat_notification)(struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status);
void (*type_in_secret_chat_notification)(struct tgl_secret_chat *E);
void (*status_notification)(struct tgl_user *U);
void (*user_registered)(struct tgl_user *U);
void (*user_activated)(struct tgl_user *U);
void (*new_authorization)(const char *device, const char *location);
//void (*secret_chat_created)(struct tgl_secret_chat *E);
//void (*secret_chat_request)(struct tgl_secret_chat *E);
//void (*secret_chat_established)(struct tgl_secret_chat *E);
//void (*secret_chat_deleted)(struct tgl_secret_chat *E);
//void (*secret_chat_accepted)(struct tgl_secret_chat *E);
//void (*new_user)(struct tgl_user *U);
//void (*delete_user)(struct tgl_user *U);
//void (*update_user_info)(struct tgl_user *U);
//void (*secret_chat_delete)(struct tgl_secret_chat *U);
//void (*secret_chat_requested)(struct tgl_secret_chat *U);
//void (*secret_chat_accepted)(struct tgl_secret_chat *U);
//void (*secret_chat_created)(struct tgl_secret_chat *U);
void (*chat_update)(struct tgl_chat *C, unsigned flags);
void (*user_update)(struct tgl_user *C, unsigned flags);
void (*secret_chat_update)(struct tgl_secret_chat *C, unsigned flags);
void (*msg_receive)(struct tgl_message *M);
void (*our_id)(int id);
char *(*create_print_name) (tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
void (*type_notification)(struct tgl_state *TLS, struct tgl_user *U, enum tgl_typing_status status);
void (*type_in_chat_notification)(struct tgl_state *TLS, struct tgl_user *U, struct tgl_chat *C, enum tgl_typing_status status);
void (*type_in_secret_chat_notification)(struct tgl_state *TLS, struct tgl_secret_chat *E);
void (*status_notification)(struct tgl_state *TLS, struct tgl_user *U);
void (*user_registered)(struct tgl_state *TLS, struct tgl_user *U);
void (*user_activated)(struct tgl_state *TLS, struct tgl_user *U);
void (*new_authorization)(struct tgl_state *TLS, const char *device, const char *location);
void (*chat_update)(struct tgl_state *TLS, struct tgl_chat *C, unsigned flags);
void (*user_update)(struct tgl_state *TLS, struct tgl_user *C, unsigned flags);
void (*secret_chat_update)(struct tgl_state *TLS, struct tgl_secret_chat *C, unsigned flags);
void (*msg_receive)(struct tgl_state *TLS, struct tgl_message *M);
void (*our_id)(struct tgl_state *TLS, int id);
void (*notification)(struct tgl_state *TLS, char *type, char *message);
char *(*create_print_name) (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4);
};
struct tgl_net_methods {
@ -114,9 +104,23 @@ struct tgl_net_methods {
struct tgl_dc *(*get_dc) (struct connection *c);
struct tgl_session *(*get_session) (struct connection *c);
struct connection *(*create_connection) (const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
struct connection *(*create_connection) (struct tgl_state *TLS, const char *host, int port, struct tgl_session *session, struct tgl_dc *dc, struct mtproto_methods *methods);
};
struct mtproto_methods {
int (*ready) (struct tgl_state *TLS, struct connection *c);
int (*close) (struct tgl_state *TLS, struct connection *c);
int (*execute) (struct tgl_state *TLS, struct connection *c, int op, int len);
};
struct tgl_timer;
struct tgl_timer_methods {
struct tgl_timer *(*alloc) (struct tgl_state *TLS, void (*cb)(struct tgl_state *TLS, void *arg), void *arg);
void (*insert) (struct tgl_timer *t, double timeout);
void (*delete) (struct tgl_timer *t);
void (*free) (struct tgl_timer *t);
};
#define E_ERROR 0
#define E_WARNING 1
@ -128,6 +132,7 @@ struct tgl_net_methods {
#define TGL_MAX_RSA_KEYS_NUM 10
// Do not modify this structure, unless you know what you do
#pragma pack(push,4)
struct tgl_state {
int our_id; // ID of logged in user
int encr_root;
@ -164,7 +169,7 @@ struct tgl_state {
struct tgl_update_callback callback;
struct tgl_net_methods *net_methods;
struct event_base *ev_base;
void *ev_base;
char *rsa_key_list[TGL_MAX_RSA_KEYS_NUM];
int rsa_key_num;
@ -189,23 +194,30 @@ struct tgl_state {
struct tgl_message message_list;
int binlog_fd;
struct tgl_timer_methods *timer_methods;
void *pubKey;
struct tree_query *queries_tree;
};
extern struct tgl_state tgl_state;
#pragma pack(pop)
//extern struct tgl_state tgl_state;
void tgl_reopen_binlog_for_writing (void);
void tgl_replay_log (void);
void tgl_reopen_binlog_for_writing (struct tgl_state *TLS);
void tgl_replay_log (struct tgl_state *TLS);
int tgl_print_stat (char *s, int len);
tgl_peer_t *tgl_peer_get (tgl_peer_id_t id);
tgl_peer_t *tgl_peer_get_by_name (const char *s);
int tgl_print_stat (struct tgl_state *TLS, char *s, int len);
tgl_peer_t *tgl_peer_get (struct tgl_state *TLS, tgl_peer_id_t id);
tgl_peer_t *tgl_peer_get_by_name (struct tgl_state *TLS, const char *s);
struct tgl_message *tgl_message_get (long long id);
void tgl_peer_iterator_ex (void (*it)(tgl_peer_t *P, void *extra), void *extra);
struct tgl_message *tgl_message_get (struct tgl_state *TLS, long long id);
void tgl_peer_iterator_ex (struct tgl_state *TLS, void (*it)(tgl_peer_t *P, void *extra), void *extra);
int tgl_complete_user_list (int index, const char *text, int len, char **R);
int tgl_complete_chat_list (int index, const char *text, int len, char **R);
int tgl_complete_encr_chat_list (int index, const char *text, int len, char **R);
int tgl_complete_peer_list (int index, const char *text, int len, char **R);
int tgl_complete_user_list (struct tgl_state *TLS, int index, const char *text, int len, char **R);
int tgl_complete_chat_list (struct tgl_state *TLS, int index, const char *text, int len, char **R);
int tgl_complete_encr_chat_list (struct tgl_state *TLS, int index, const char *text, int len, char **R);
int tgl_complete_peer_list (struct tgl_state *TLS, int index, const char *text, int len, char **R);
#define TGL_PEER_USER 1
#define TGL_PEER_CHAT 2
@ -218,12 +230,12 @@ int tgl_complete_peer_list (int index, const char *text, int len, char **R);
#define TGL_MK_GEO_CHAT(id) tgl_set_peer_id (TGL_PEER_GEO_CHAT,id)
#define TGL_MK_ENCR_CHAT(id) tgl_set_peer_id (TGL_PEER_ENCR_CHAT,id)
void tgl_set_binlog_mode (int mode);
void tgl_set_binlog_path (const char *path);
void tgl_set_auth_file_path (const char *path);
void tgl_set_download_directory (const char *path);
void tgl_set_callback (struct tgl_update_callback *cb);
void tgl_set_rsa_key (const char *key);
void tgl_set_binlog_mode (struct tgl_state *TLS, int mode);
void tgl_set_binlog_path (struct tgl_state *TLS, const char *path);
void tgl_set_auth_file_path (struct tgl_state *TLS, const char *path);
void tgl_set_download_directory (struct tgl_state *TLS, const char *path);
void tgl_set_callback (struct tgl_state *TLS, struct tgl_update_callback *cb);
void tgl_set_rsa_key (struct tgl_state *TLS, const char *key);
static inline int tgl_get_peer_type (tgl_peer_id_t id) {
@ -245,116 +257,130 @@ static inline int tgl_cmp_peer_id (tgl_peer_id_t a, tgl_peer_id_t b) {
return memcmp (&a, &b, sizeof (a));
}
static inline void tgl_incr_verbosity (void) {
tgl_state.verbosity ++;
static inline void tgl_incr_verbosity (struct tgl_state *TLS) {
TLS->verbosity ++;
}
static inline void tgl_set_verbosity (int val) {
tgl_state.verbosity = val;
static inline void tgl_set_verbosity (struct tgl_state *TLS, int val) {
TLS->verbosity = val;
}
static inline void tgl_enable_pfs (void) {
tgl_state.enable_pfs = 1;
static inline void tgl_enable_pfs (struct tgl_state *TLS) {
TLS->enable_pfs = 1;
}
static inline void tgl_set_test_mode (void) {
tgl_state.test_mode ++;
static inline void tgl_set_test_mode (struct tgl_state *TLS) {
TLS->test_mode ++;
}
struct pollfd;
int tgl_connections_make_poll_array (struct pollfd *fds, int max);
void tgl_connections_poll_result (struct pollfd *fds, int max);
static inline void tgl_set_net_methods (struct tgl_state *TLS, struct tgl_net_methods *methods) {
TLS->net_methods = methods;
}
void tgl_do_help_get_config (void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra, int success, int registered, const char *hash), void *callback_extra);
void tgl_do_phone_call (const char *user, const char *hash, void (*callback)(void *callback_extra, int success), void *callback_extra);
int tgl_do_send_code_result (const char *user, const char *hash, const char *code, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) ;
int tgl_do_send_code_result_auth (const char *user, const char *hash, const char *code, const char *first_name, const char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *Self), void *callback_extra);
void tgl_do_update_contact_list (void (*callback) (void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra);
void tgl_do_send_message (tgl_peer_id_t id, const char *msg, int len, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_msg (struct tgl_message *M, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_text (tgl_peer_id_t id, char *file, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_mark_read (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_get_history (tgl_peer_id_t id, int limit, int offline_mode, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
void tgl_do_get_history_ext (tgl_peer_id_t id, int offset, int limit, int offline_mode, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
void tgl_do_get_dialog_list (void (*callback)(void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]), void *callback_extra);
void tgl_do_send_photo (enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_set_chat_photo (tgl_peer_id_t chat_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_set_profile_photo (char *file_name, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_set_profile_name (char *first_name, char *last_name, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_set_username (char *name, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_forward_message (tgl_peer_id_t id, int n, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_rename_chat (tgl_peer_id_t id, char *name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_get_chat_info (tgl_peer_id_t id, int offline_mode, void (*callback)(void *callback_extra, int success, struct tgl_chat *C), void *callback_extra);
void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_load_photo (struct tgl_photo *photo, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_video_thumb (struct tgl_video *video, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_audio (struct tgl_audio *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_video (struct tgl_video *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_document (struct tgl_document *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_document_thumb (struct tgl_document *video, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_encr_video (struct tgl_encr_video *V, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_export_auth (int num, void (*callback) (void *callback_extra, int success), void *callback_extra);
void tgl_do_add_contact (const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force, void (*callback)(void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra);
void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, const char *s, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
//void tgl_do_contacts_search (int limit, const char *s, void (*callback) (void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra);
void tgl_do_create_encr_chat_request (int user_id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra);
void tgl_do_create_secret_chat (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra);
void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback)(void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra);
void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_lookup_state (void);
void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_create_group_chat_ex (int users_num, tgl_peer_id_t ids[], char *chat_topic, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_delete_msg (long long id, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_restore_msg (long long id, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_update_status (int online, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_help_get_config_dc (struct tgl_dc *D, void (*callback)(void *, int), void *callback_extra);
void tgl_do_export_card (void (*callback)(void *callback_extra, int success, int size, int *card), void *callback_extra);
void tgl_do_import_card (int size, int *card, void (*callback)(void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_send_contact (tgl_peer_id_t id, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_forward_media (tgl_peer_id_t id, int n, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_del_contact (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra);
void tgl_do_set_encr_chat_ttl (struct tgl_secret_chat *E, int ttl, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_location(tgl_peer_id_t id, double latitude, double longitude, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_contact_search (char *name, int limit, void (*callback)(void *callback_extra, int success, int cnt, struct tgl_user *U[]), void *callback_extra);
static inline void tgl_set_timer_methods (struct tgl_state *TLS, struct tgl_timer_methods *methods) {
TLS->timer_methods = methods;
}
static inline void tgl_set_ev_base (struct tgl_state *TLS, void *ev_base) {
TLS->ev_base = ev_base;
}
//struct pollfd;
//int tgl_connections_make_poll_array (struct tgl_state *TLS, struct pollfd *fds, int max);
//void tgl_connections_poll_result (struct tgl_state *TLS, struct pollfd *fds, int max);
void tgl_do_help_get_config (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_send_code (struct tgl_state *TLS, const char *user, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int registered, const char *hash), void *callback_extra);
void tgl_do_phone_call (struct tgl_state *TLS, const char *user, const char *hash, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
int tgl_do_send_code_result (struct tgl_state *TLS, const char *user, const char *hash, const char *code, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra) ;
int tgl_do_send_code_result_auth (struct tgl_state *TLS, const char *user, const char *hash, const char *code, const char *first_name, const char *last_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *Self), void *callback_extra);
void tgl_do_update_contact_list (struct tgl_state *TLS, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *contacts[]), void *callback_extra);
void tgl_do_send_message (struct tgl_state *TLS, tgl_peer_id_t id, const char *msg, int len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_msg (struct tgl_state *TLS, struct tgl_message *M, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_text (struct tgl_state *TLS, tgl_peer_id_t id, char *file, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_mark_read (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_get_history (struct tgl_state *TLS, tgl_peer_id_t id, int limit, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
void tgl_do_get_history_ext (struct tgl_state *TLS, tgl_peer_id_t id, int offset, int limit, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
void tgl_do_get_dialog_list (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]), void *callback_extra);
void tgl_do_send_photo (struct tgl_state *TLS, enum tgl_message_media_type type, tgl_peer_id_t to_id, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_set_chat_photo (struct tgl_state *TLS, tgl_peer_id_t chat_id, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_set_profile_photo (struct tgl_state *TLS, char *file_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_set_profile_name (struct tgl_state *TLS, char *first_name, char *last_name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_set_username (struct tgl_state *TLS, char *name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_forward_message (struct tgl_state *TLS, tgl_peer_id_t id, int n, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_rename_chat (struct tgl_state *TLS, tgl_peer_id_t id, char *name, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_get_chat_info (struct tgl_state *TLS, tgl_peer_id_t id, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_chat *C), void *callback_extra);
void tgl_do_get_user_info (struct tgl_state *TLS, tgl_peer_id_t id, int offline_mode, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_load_photo (struct tgl_state *TLS, struct tgl_photo *photo, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_video_thumb (struct tgl_state *TLS, struct tgl_video *video, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_audio (struct tgl_state *TLS, struct tgl_audio *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_video (struct tgl_state *TLS, struct tgl_video *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_document (struct tgl_state *TLS, struct tgl_document *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_document_thumb (struct tgl_state *TLS, struct tgl_document *video, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_load_encr_video (struct tgl_state *TLS, struct tgl_encr_video *V, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *filename), void *callback_extra);
void tgl_do_export_auth (struct tgl_state *TLS, int num, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_add_contact (struct tgl_state *TLS, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, int force, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra);
void tgl_do_msg_search (struct tgl_state *TLS, tgl_peer_id_t id, int from, int to, int limit, int offset, const char *s, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
//void tgl_do_contacts_search (int limit, const char *s, void (*callback) (struct tgl_state *TLS, void *callback_extra, int success, int size, struct tgl_user *users[]), void *callback_extra);
void tgl_do_create_encr_chat_request (struct tgl_state *TLS, int user_id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra);
void tgl_do_create_secret_chat (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra);
void tgl_do_accept_encr_chat_request (struct tgl_state *TLS, struct tgl_secret_chat *E, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_secret_chat *E), void *callback_extra);
void tgl_do_get_difference (struct tgl_state *TLS, int sync_from_start, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_lookup_state (struct tgl_state *TLS);
void tgl_do_add_user_to_chat (struct tgl_state *TLS, tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_del_user_from_chat (struct tgl_state *TLS, tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_create_group_chat (struct tgl_state *TLS, tgl_peer_id_t id, char *chat_topic, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_create_group_chat_ex (struct tgl_state *TLS, int users_num, tgl_peer_id_t ids[], char *chat_topic, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_delete_msg (struct tgl_state *TLS, long long id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_restore_msg (struct tgl_state *TLS, long long id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_update_status (struct tgl_state *TLS, int online, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_help_get_config_dc (struct tgl_state *TLS, struct tgl_dc *D, void (*callback)(struct tgl_state *TLS, void *, int), void *callback_extra);
void tgl_do_export_card (struct tgl_state *TLS, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int size, int *card), void *callback_extra);
void tgl_do_import_card (struct tgl_state *TLS, int size, int *card, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_user *U), void *callback_extra);
void tgl_do_send_contact (struct tgl_state *TLS, tgl_peer_id_t id, const char *phone, int phone_len, const char *first_name, int first_name_len, const char *last_name, int last_name_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_forward_media (struct tgl_state *TLS, tgl_peer_id_t id, int n, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_del_contact (struct tgl_state *TLS, tgl_peer_id_t id, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success), void *callback_extra);
void tgl_do_set_encr_chat_ttl (struct tgl_state *TLS, struct tgl_secret_chat *E, int ttl, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_send_location (struct tgl_state *TLS, tgl_peer_id_t id, double latitude, double longitude, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, struct tgl_message *M), void *callback_extra);
void tgl_do_contact_search (struct tgl_state *TLS, char *name, int limit, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, int cnt, struct tgl_user *U[]), void *callback_extra);
void tgl_do_visualize_key (tgl_peer_id_t id, unsigned char buf[16]);
void tgl_do_visualize_key (struct tgl_state *TLS, tgl_peer_id_t id, unsigned char buf[16]);
void tgl_do_send_ping (struct connection *c);
void tgl_do_send_ping (struct tgl_state *TLS, struct connection *c);
int tgl_authorized_dc (struct tgl_dc *DC);
int tgl_signed_dc (struct tgl_dc *DC);
void tgl_do_send_extf (struct tgl_state *TLS, char *data, int data_len, void (*callback)(struct tgl_state *TLS, void *callback_extra, int success, char *data), void *callback_extra);
int tgl_authorized_dc (struct tgl_state *TLS, struct tgl_dc *DC);
int tgl_signed_dc (struct tgl_state *TLS, struct tgl_dc *DC);
//void tgl_do_get_suggested (void);
void tgl_do_create_keys_end (struct tgl_secret_chat *U);
void tgl_do_send_encr_chat_layer (struct tgl_secret_chat *E);
void tgl_do_create_keys_end (struct tgl_state *TLS, struct tgl_secret_chat *U);
void tgl_do_send_encr_chat_layer (struct tgl_state *TLS, struct tgl_secret_chat *E);
struct mtproto_methods {
int (*ready) (struct connection *c);
int (*close) (struct connection *c);
int (*execute) (struct connection *c, int op, int len);
};
void tgl_init (struct tgl_state *TLS);
void tgl_dc_authorize (struct tgl_state *TLS, struct tgl_dc *DC);
void tgl_init (void);
void tgl_dc_authorize (struct tgl_dc *DC);
void tgl_dc_iterator (void (*iterator)(struct tgl_dc *DC));
void tgl_dc_iterator_ex (void (*iterator)(struct tgl_dc *DC, void *extra), void *extra);
void tgl_dc_iterator (struct tgl_state *TLS, void (*iterator)(struct tgl_dc *DC));
void tgl_dc_iterator_ex (struct tgl_state *TLS, void (*iterator)(struct tgl_dc *DC, void *extra), void *extra);
double tglt_get_double_time (void);
void tgl_insert_empty_user (int id);
void tgl_insert_empty_chat (int id);
void tgl_insert_empty_user (struct tgl_state *TLS, int id);
void tgl_insert_empty_chat (struct tgl_state *TLS, int id);
int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, char *data, int data_len);
struct paramed_type *tglf_extf_store (const char *data, int data_len);
void tgl_do_send_extf (char *data, int data_len, void (*callback)(void *callback_extra, int success, char *data), void *callback_extra);
char *tglf_extf_fetch (struct paramed_type *T);
int tglf_extf_autocomplete (struct tgl_state *TLS, const char *text, int text_len, int index, char **R, char *data, int data_len);
struct paramed_type *tglf_extf_store (struct tgl_state *TLS, const char *data, int data_len);
char *tglf_extf_fetch (struct tgl_state *TLS, struct paramed_type *T);
void tgl_free_all (void);
void tgl_free_all (struct tgl_state *TLS);
static inline struct tgl_state *tgl_state_alloc (void) {
struct tgl_state *TLS = malloc (sizeof (*TLS));
if (!TLS) { return NULL; }
memset (TLS, 0, sizeof (*TLS));
return TLS;
}
#endif

221
updates.c
View File

@ -26,27 +26,27 @@
#include <assert.h>
void tglu_fetch_pts (void) {
void tglu_fetch_pts (struct tgl_state *TLS) {
int p = fetch_int ();
if (p <= tgl_state.pts) { return; }
bl_do_set_pts (p);
if (p <= TLS->pts) { return; }
bl_do_set_pts (TLS, p);
}
void tglu_fetch_qts (void) {
void tglu_fetch_qts (struct tgl_state *TLS) {
int p = fetch_int ();
if (p <= tgl_state.qts) { return; }
bl_do_set_qts (p);
if (p <= TLS->qts) { return; }
bl_do_set_qts (TLS, p);
}
void tglu_fetch_date (void) {
void tglu_fetch_date (struct tgl_state *TLS) {
int p = fetch_int ();
if (p > tgl_state.date) {
//tgl_state.date = p;
bl_do_set_date (tgl_state.date);
if (p > TLS->date) {
//TLS->date = p;
bl_do_set_date (TLS, TLS->date);
}
}
static void fetch_dc_option (void) {
static void fetch_dc_option (struct tgl_state *TLS) {
assert (fetch_int () == CODE_dc_option);
int id = fetch_int ();
int l1 = prefetch_strlen ();
@ -56,27 +56,27 @@ static void fetch_dc_option (void) {
int port = fetch_int ();
vlogprintf (E_DEBUG, "id = %d, name = %.*s ip = %.*s port = %d\n", id, l1, name, l2, ip, port);
bl_do_dc_option (id, l1, name, l2, ip, port);
bl_do_dc_option (TLS, id, l1, name, l2, ip, port);
}
void tglu_work_update (struct connection *c, long long msg_id) {
void tglu_work_update (struct tgl_state *TLS, struct connection *c, long long msg_id) {
unsigned op = fetch_int ();
switch (op) {
case CODE_update_new_message:
{
struct tgl_message *M = tglf_fetch_alloc_message ();
struct tgl_message *M = tglf_fetch_alloc_message (TLS);
assert (M);
tglu_fetch_pts ();
bl_do_msg_update (M->id);
tglu_fetch_pts (TLS);
bl_do_msg_update (TLS, M->id);
break;
};
case CODE_update_message_i_d:
{
int id = fetch_int (); // id
int new = fetch_long (); // random_id
struct tgl_message *M = tgl_message_get (new);
struct tgl_message *M = tgl_message_get (TLS, new);
if (M) {
bl_do_set_msg_id (M, id);
bl_do_set_msg_id (TLS, M, id);
}
}
break;
@ -89,23 +89,23 @@ void tglu_work_update (struct connection *c, long long msg_id) {
int i;
for (i = 0; i < n; i++) {
int id = fetch_int ();
struct tgl_message *M = tgl_message_get (id);
struct tgl_message *M = tgl_message_get (TLS, id);
if (M) {
bl_do_set_unread (M, 0);
bl_do_set_unread (TLS, M, 0);
}
}
tglu_fetch_pts ();
tglu_fetch_pts (TLS);
}
break;
case CODE_update_user_typing:
{
//vlogprintf (E_ERROR, "user typing\n");
tgl_peer_id_t id = TGL_MK_USER (fetch_int ());
tgl_peer_t *U = tgl_peer_get (id);
tgl_peer_t *U = tgl_peer_get (TLS, id);
enum tgl_typing_status status = tglf_fetch_typing ();
if (tgl_state.callback.type_notification && U) {
tgl_state.callback.type_notification ((void *)U, status);
if (TLS->callback.type_notification && U) {
TLS->callback.type_notification (TLS, (void *)U, status);
}
}
break;
@ -114,13 +114,13 @@ void tglu_work_update (struct connection *c, long long msg_id) {
//vlogprintf (E_ERROR, "chat typing\n");
tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ());
tgl_peer_id_t id = TGL_MK_USER (fetch_int ());
tgl_peer_t *C = tgl_peer_get (chat_id);
tgl_peer_t *U = tgl_peer_get (id);
tgl_peer_t *C = tgl_peer_get (TLS, chat_id);
tgl_peer_t *U = tgl_peer_get (TLS, id);
enum tgl_typing_status status = tglf_fetch_typing ();
if (U && C) {
if (tgl_state.callback.type_in_chat_notification) {
tgl_state.callback.type_in_chat_notification ((void *)U, (void *)C, status);
if (TLS->callback.type_in_chat_notification) {
TLS->callback.type_in_chat_notification (TLS, (void *)U, (void *)C, status);
}
}
}
@ -128,33 +128,33 @@ void tglu_work_update (struct connection *c, long long msg_id) {
case CODE_update_user_status:
{
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
tgl_peer_t *U = tgl_peer_get (user_id);
tgl_peer_t *U = tgl_peer_get (TLS, user_id);
if (U) {
tglf_fetch_user_status (&U->user.status);
tglf_fetch_user_status (TLS, &U->user.status);
if (tgl_state.callback.status_notification) {
tgl_state.callback.status_notification ((void *)U);
if (TLS->callback.status_notification) {
TLS->callback.status_notification (TLS, (void *)U);
}
} else {
struct tgl_user_status t;
tglf_fetch_user_status (&t);
tglf_fetch_user_status (TLS, &t);
}
}
break;
case CODE_update_user_name:
{
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
tgl_peer_t *UC = tgl_peer_get (user_id);
tgl_peer_t *UC = tgl_peer_get (TLS, user_id);
if (UC && (UC->flags & FLAG_CREATED)) {
int l1 = prefetch_strlen ();
char *f = fetch_str (l1);
int l2 = prefetch_strlen ();
char *l = fetch_str (l2);
struct tgl_user *U = &UC->user;
bl_do_user_set_real_name (U, f, l1, l, l2);
bl_do_user_set_real_name (TLS, U, f, l1, l, l2);
int l3 = prefetch_strlen ();
f = fetch_str (l3);
bl_do_user_set_username (U, f, l3);
bl_do_user_set_username (TLS, U, f, l3);
} else {
fetch_skip_str ();
fetch_skip_str ();
@ -165,8 +165,8 @@ void tglu_work_update (struct connection *c, long long msg_id) {
case CODE_update_user_photo:
{
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
tgl_peer_t *UC = tgl_peer_get (user_id);
tglu_fetch_date ();
tgl_peer_t *UC = tgl_peer_get (TLS, user_id);
tglu_fetch_date (TLS);
if (UC && (UC->flags & FLAG_CREATED)) {
struct tgl_user *U = &UC->user;
unsigned y = fetch_int ();
@ -182,10 +182,10 @@ void tglu_work_update (struct connection *c, long long msg_id) {
} else {
assert (y == CODE_user_profile_photo);
photo_id = fetch_long ();
tglf_fetch_file_location (&small);
tglf_fetch_file_location (&big);
tglf_fetch_file_location (TLS, &small);
tglf_fetch_file_location (TLS, &big);
}
bl_do_set_user_profile_photo (U, photo_id, &big, &small);
bl_do_set_user_profile_photo (TLS, U, photo_id, &big, &small);
} else {
struct tgl_file_location t;
unsigned y = fetch_int ();
@ -193,8 +193,8 @@ void tglu_work_update (struct connection *c, long long msg_id) {
} else {
assert (y == CODE_user_profile_photo);
fetch_long (); // photo_id
tglf_fetch_file_location (&t);
tglf_fetch_file_location (&t);
tglf_fetch_file_location (TLS, &t);
tglf_fetch_file_location (TLS, &t);
}
}
fetch_bool ();
@ -205,7 +205,7 @@ void tglu_work_update (struct connection *c, long long msg_id) {
assert (fetch_int () == CODE_vector);
int n = fetch_int ();
fetch_skip (n);
tglu_fetch_pts ();
tglu_fetch_pts (TLS);
}
break;
case CODE_update_delete_messages:
@ -213,7 +213,7 @@ void tglu_work_update (struct connection *c, long long msg_id) {
assert (fetch_int () == CODE_vector);
int n = fetch_int ();
fetch_skip (n);
tglu_fetch_pts ();
tglu_fetch_pts (TLS);
}
break;
case CODE_update_chat_participants:
@ -222,10 +222,10 @@ void tglu_work_update (struct connection *c, long long msg_id) {
assert (x == CODE_chat_participants || x == CODE_chat_participants_forbidden);
tgl_peer_id_t chat_id = TGL_MK_CHAT (fetch_int ());
int n = 0;
tgl_peer_t *C = tgl_peer_get (chat_id);
tgl_peer_t *C = tgl_peer_get (TLS, chat_id);
if (C && (C->flags & FLAG_CREATED)) {
if (x == CODE_chat_participants) {
bl_do_chat_set_admin (&C->chat, fetch_int ());
bl_do_chat_set_admin (TLS, &C->chat, fetch_int ());
assert (fetch_int () == CODE_vector);
n = fetch_int ();
struct tgl_chat_user *users = talloc (12 * n);
@ -237,7 +237,7 @@ void tglu_work_update (struct connection *c, long long msg_id) {
users[i].date = fetch_int ();
}
int version = fetch_int ();
bl_do_chat_set_participants (&C->chat, version, n, users);
bl_do_chat_set_participants (TLS, &C->chat, version, n, users);
}
} else {
if (x == CODE_chat_participants) {
@ -253,17 +253,17 @@ void tglu_work_update (struct connection *c, long long msg_id) {
case CODE_update_contact_registered:
{
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
tgl_peer_t *U = tgl_peer_get (user_id);
tgl_peer_t *U = tgl_peer_get (TLS, user_id);
fetch_int (); // date
if (tgl_state.callback.user_registered && U) {
tgl_state.callback.user_registered ((void *)U);
if (TLS->callback.user_registered && U) {
TLS->callback.user_registered (TLS, (void *)U);
}
}
break;
case CODE_update_contact_link:
{
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
tgl_peer_t *U = tgl_peer_get (user_id);
tgl_peer_t *U = tgl_peer_get (TLS, user_id);
unsigned t = fetch_int ();
assert (t == CODE_contacts_my_link_empty || t == CODE_contacts_my_link_requested || t == CODE_contacts_my_link_contact);
if (t == CODE_contacts_my_link_requested) {
@ -280,10 +280,10 @@ void tglu_work_update (struct connection *c, long long msg_id) {
case CODE_update_activation:
{
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
tgl_peer_t *U = tgl_peer_get (user_id);
tgl_peer_t *U = tgl_peer_get (TLS, user_id);
if (tgl_state.callback.user_activated && U) {
tgl_state.callback.user_activated ((void *)U);
if (TLS->callback.user_activated && U) {
TLS->callback.user_activated (TLS, (void *)U);
}
}
break;
@ -293,8 +293,8 @@ void tglu_work_update (struct connection *c, long long msg_id) {
fetch_int (); // date
char *s = fetch_str_dup ();
char *location = fetch_str_dup ();
if (tgl_state.callback.new_authorization) {
tgl_state.callback.new_authorization (s, location);
if (TLS->callback.new_authorization) {
TLS->callback.new_authorization (TLS, s, location);
}
tfree_str (s);
tfree_str (location);
@ -302,25 +302,25 @@ void tglu_work_update (struct connection *c, long long msg_id) {
break;
case CODE_update_new_geo_chat_message:
{
struct tgl_message *M = tglf_fetch_alloc_geo_message ();
struct tgl_message *M = tglf_fetch_alloc_geo_message (TLS);
assert (M);
bl_do_msg_update (M->id);
bl_do_msg_update (TLS, M->id);
}
break;
case CODE_update_new_encrypted_message:
{
struct tgl_message *M = tglf_fetch_alloc_encrypted_message ();
struct tgl_message *M = tglf_fetch_alloc_encrypted_message (TLS);
assert (M);
tglu_fetch_qts ();
bl_do_msg_update (M->id);
tglu_fetch_qts (TLS);
bl_do_msg_update (TLS, M->id);
}
break;
case CODE_update_encryption:
{
struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat ();
struct tgl_secret_chat *E = tglf_fetch_alloc_encrypted_chat (TLS);
vlogprintf (E_DEBUG, "Secret chat state = %d\n", E->state);
if (E->state == sc_ok) {
tgl_do_send_encr_chat_layer (E);
tgl_do_send_encr_chat_layer (TLS, E);
}
fetch_int (); // date
}
@ -328,11 +328,11 @@ void tglu_work_update (struct connection *c, long long msg_id) {
case CODE_update_encrypted_chat_typing:
{
tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ());
tgl_peer_t *P = tgl_peer_get (id);
tgl_peer_t *P = tgl_peer_get (TLS, id);
if (P) {
if (tgl_state.callback.type_in_secret_chat_notification) {
tgl_state.callback.type_in_secret_chat_notification ((void *)P);
if (TLS->callback.type_in_secret_chat_notification) {
TLS->callback.type_in_secret_chat_notification (TLS, (void *)P);
}
}
}
@ -342,14 +342,14 @@ void tglu_work_update (struct connection *c, long long msg_id) {
tgl_peer_id_t id = TGL_MK_ENCR_CHAT (fetch_int ()); // chat_id
fetch_int (); // max_date
fetch_int (); // date
tgl_peer_t *P = tgl_peer_get (id);
tgl_peer_t *P = tgl_peer_get (TLS, id);
//int x = -1;
if (P && P->last) {
//x = 0;
struct tgl_message *M = P->last;
while (M && (!M->out || M->unread)) {
if (M->out) {
bl_do_set_unread (M, 0);
bl_do_set_unread (TLS, M, 0);
}
M = M->next;
}
@ -363,9 +363,9 @@ void tglu_work_update (struct connection *c, long long msg_id) {
tgl_peer_id_t inviter_id = TGL_MK_USER (fetch_int ());
int version = fetch_int ();
tgl_peer_t *C = tgl_peer_get (chat_id);
tgl_peer_t *C = tgl_peer_get (TLS, chat_id);
if (C && (C->flags & FLAG_CREATED)) {
bl_do_chat_add_user (&C->chat, version, tgl_get_peer_id (user_id), tgl_get_peer_id (inviter_id), time (0));
bl_do_chat_add_user (TLS, &C->chat, version, tgl_get_peer_id (user_id), tgl_get_peer_id (inviter_id), time (0));
}
}
break;
@ -375,9 +375,9 @@ void tglu_work_update (struct connection *c, long long msg_id) {
tgl_peer_id_t user_id = TGL_MK_USER (fetch_int ());
int version = fetch_int ();
tgl_peer_t *C = tgl_peer_get (chat_id);
tgl_peer_t *C = tgl_peer_get (TLS, chat_id);
if (C && (C->flags & FLAG_CREATED)) {
bl_do_chat_del_user (&C->chat, version, tgl_get_peer_id (user_id));
bl_do_chat_del_user (TLS, &C->chat, version, tgl_get_peer_id (user_id));
}
}
break;
@ -388,7 +388,7 @@ void tglu_work_update (struct connection *c, long long msg_id) {
assert (n >= 0);
int i;
for (i = 0; i < n; i++) {
fetch_dc_option ();
fetch_dc_option (TLS);
}
}
break;
@ -396,9 +396,9 @@ void tglu_work_update (struct connection *c, long long msg_id) {
{
int id = fetch_int ();
int blocked = fetch_bool ();
tgl_peer_t *P = tgl_peer_get (TGL_MK_USER (id));
tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (id));
if (P && (P->flags & FLAG_CREATED)) {
bl_do_user_set_blocked (&P->user, blocked);
bl_do_user_set_blocked (TLS, &P->user, blocked);
}
}
break;
@ -417,6 +417,13 @@ void tglu_work_update (struct connection *c, long long msg_id) {
skip_type_message_media (TYPE_TO_PARAM(message_media));
fetch_bool ();
vlogprintf (E_ERROR, "Notification %.*s: %.*s\n", l1, type, l2, message);
if (TLS->callback.notification) {
char *t = tstrndup (type, l1);
char *m = tstrndup (message, l2);
TLS->callback.notification (TLS, t, m);
tfree_str (t);
tfree_str (m);
}
}
break;
default:
@ -425,32 +432,32 @@ void tglu_work_update (struct connection *c, long long msg_id) {
}
}
void tglu_work_update_short (struct connection *c, long long msg_id) {
void tglu_work_update_short (struct tgl_state *TLS, struct connection *c, long long msg_id) {
int *save = in_ptr;
assert (!skip_type_any (TYPE_TO_PARAM (updates)));
int *save_end = in_ptr;
in_ptr = save;
assert (fetch_int () == CODE_update_short);
tglu_work_update (c, msg_id);
tglu_fetch_date ();
tglu_work_update (TLS, c, msg_id);
tglu_fetch_date (TLS);
assert (save_end == in_ptr);
}
static int do_skip_seq (int seq) {
if (tgl_state.seq) {
if (seq <= tgl_state.seq) {
static int do_skip_seq (struct tgl_state *TLS, int seq) {
if (TLS->seq) {
if (seq <= TLS->seq) {
vlogprintf (E_NOTICE, "Duplicate message with seq=%d\n", seq);
return -1;
}
if (seq > tgl_state.seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, tgl_state.seq);
//vlogprintf (E_NOTICE, "lock_diff = %s\n", (tgl_state.locks & TGL_LOCK_DIFF) ? "true" : "false");
tgl_do_get_difference (0, 0, 0);
if (seq > TLS->seq + 1) {
vlogprintf (E_NOTICE, "Hole in seq (seq = %d, cur_seq = %d)\n", seq, TLS->seq);
//vlogprintf (E_NOTICE, "lock_diff = %s\n", (TLS->locks & TGL_LOCK_DIFF) ? "true" : "false");
tgl_do_get_difference (TLS, 0, 0, 0);
return -1;
}
if (tgl_state.locks & TGL_LOCK_DIFF) {
if (TLS->locks & TGL_LOCK_DIFF) {
vlogprintf (E_DEBUG, "Update during get_difference. seq = %d\n", seq);
return -1;
}
@ -461,10 +468,10 @@ static int do_skip_seq (int seq) {
}
}
void tglu_work_updates (struct connection *c, long long msg_id) {
void tglu_work_updates (struct tgl_state *TLS, struct connection *c, long long msg_id) {
int *save = in_ptr;
assert (!skip_type_any (TYPE_TO_PARAM (updates)));
if (do_skip_seq (*(in_ptr - 1)) < 0) {
if (do_skip_seq (TLS, *(in_ptr - 1)) < 0) {
return;
}
int *save_end = in_ptr;
@ -474,66 +481,66 @@ void tglu_work_updates (struct connection *c, long long msg_id) {
int n = fetch_int ();
int i;
for (i = 0; i < n; i++) {
tglu_work_update (c, msg_id);
tglu_work_update (TLS, c, msg_id);
}
assert (fetch_int () == CODE_vector);
n = fetch_int ();
for (i = 0; i < n; i++) {
tglf_fetch_alloc_user ();
tglf_fetch_alloc_user (TLS);
}
assert (fetch_int () == CODE_vector);
n = fetch_int ();
for (i = 0; i < n; i++) {
tglf_fetch_alloc_chat ();
tglf_fetch_alloc_chat (TLS);
}
bl_do_set_date (fetch_int ());
bl_do_set_date (TLS, fetch_int ());
//bl_do_set_seq (fetch_int ());
int seq = fetch_int ();
assert (seq == tgl_state.seq + 1);
bl_do_set_seq (seq);
assert (seq == TLS->seq + 1);
bl_do_set_seq (TLS, seq);
assert (save_end == in_ptr);
}
void tglu_work_update_short_message (struct connection *c, long long msg_id) {
void tglu_work_update_short_message (struct tgl_state *TLS, struct connection *c, long long msg_id) {
int *save = in_ptr;
assert (!skip_type_any (TYPE_TO_PARAM (updates)));
if (do_skip_seq (*(in_ptr - 1)) < 0) {
if (do_skip_seq (TLS, *(in_ptr - 1)) < 0) {
return;
}
int *save_end = in_ptr;
in_ptr = save;
assert (fetch_int () == (int)CODE_update_short_message);
struct tgl_message *M = tglf_fetch_alloc_message_short ();
struct tgl_message *M = tglf_fetch_alloc_message_short (TLS);
assert (M);
assert (save_end == in_ptr);
if (!(tgl_state.locks & TGL_LOCK_DIFF)) {
bl_do_msg_seq_update (M->id);
if (!(TLS->locks & TGL_LOCK_DIFF)) {
bl_do_msg_seq_update (TLS, M->id);
}
}
void tglu_work_update_short_chat_message (struct connection *c, long long msg_id) {
void tglu_work_update_short_chat_message (struct tgl_state *TLS, struct connection *c, long long msg_id) {
int *save = in_ptr;
assert (!skip_type_any (TYPE_TO_PARAM (updates)));
if (do_skip_seq (*(in_ptr - 1)) < 0) {
if (do_skip_seq (TLS, *(in_ptr - 1)) < 0) {
return;
}
int *save_end = in_ptr;
in_ptr = save;
assert (fetch_int () == CODE_update_short_chat_message);
struct tgl_message *M = tglf_fetch_alloc_message_short_chat ();
struct tgl_message *M = tglf_fetch_alloc_message_short_chat (TLS);
assert (M);
assert (save_end == in_ptr);
if (!(tgl_state.locks & TGL_LOCK_DIFF)) {
bl_do_msg_seq_update (M->id);
if (!(TLS->locks & TGL_LOCK_DIFF)) {
bl_do_msg_seq_update (TLS, M->id);
}
}
void tglu_work_updates_to_long (struct connection *c, long long msg_id) {
void tglu_work_updates_to_long (struct tgl_state *TLS, struct connection *c, long long msg_id) {
assert (fetch_int () == (int)CODE_updates_too_long);
vlogprintf (E_NOTICE, "updates too long... Getting difference\n");
tgl_do_get_difference (0, 0, 0);
tgl_do_get_difference (TLS, 0, 0, 0);
}

View File

@ -21,15 +21,15 @@
#ifndef __UPDATES_H__
#define __UPDATES_H__
struct connection;
void tglu_work_update (struct connection *c, long long msg_id);
void tglu_work_updates_to_long (struct connection *c, long long msg_id);
void tglu_work_update_short_chat_message (struct connection *c, long long msg_id);
void tglu_work_update_short_message (struct connection *c, long long msg_id);
void tglu_work_update_short (struct connection *c, long long msg_id);
void tglu_work_updates (struct connection *c, long long msg_id);
void tglu_work_update (struct tgl_state *TLS, struct connection *c, long long msg_id);
void tglu_work_updates_to_long (struct tgl_state *TLS, struct connection *c, long long msg_id);
void tglu_work_update_short_chat_message (struct tgl_state *TLS, struct connection *c, long long msg_id);
void tglu_work_update_short_message (struct tgl_state *TLS, struct connection *c, long long msg_id);
void tglu_work_update_short (struct tgl_state *TLS, struct connection *c, long long msg_id);
void tglu_work_updates (struct tgl_state *TLS, struct connection *c, long long msg_id);
void tglu_fetch_pts (void);
void tglu_fetch_qts (void);
void tglu_fetch_seq (void);
void tglu_fetch_date (void);
void tglu_fetch_pts (struct tgl_state *TLS);
void tglu_fetch_qts (struct tgl_state *TLS);
void tglu_fetch_seq (struct tgl_state *TLS);
void tglu_fetch_date (struct tgl_state *TLS);
#endif