many fixes

This commit is contained in:
vvaltman 2014-08-15 02:16:01 +04:00
parent e2e7d0c906
commit cedc1d17d4
14 changed files with 416 additions and 176 deletions

View File

@ -7,7 +7,7 @@ DEFS=@DEFS@
COMPILE_FLAGS=${CFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter
EXTRA_LIBS=@LIBS@ @EXTRA_LIBS@
LOCAL_LDFLAGS=-rdynamic -ggdb ${EXTRA_LIBS}
LOCAL_LDFLAGS=-rdynamic -ggdb -levent ${EXTRA_LIBS}
LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS}
DEP=${srcdir}/dep

View File

@ -46,6 +46,8 @@
#include "tgl.h"
#include "auto.h"
#include "structures.h"
#include <openssl/sha.h>
#define BINLOG_BUFFER_SIZE (1 << 20)
@ -1327,7 +1329,7 @@ void bl_do_set_our_id (int id) {
ev[0] = CODE_binlog_our_id;
ev[1] = id;
add_log_event (ev, 8);
write_auth_file ();
//write_auth_file ();
}
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) {

View File

@ -20,6 +20,7 @@
#define __BINLOG_H__
//#include "structures.h"
#include "tgl.h"
void bl_do_set_auth_key_id (int num, unsigned char *buf);

View File

@ -43,13 +43,23 @@
#include "interface.h"
#include "telegram.h"
#include "auto/constants.h"
#include "tools.h"
//#include "tools.h"
//#include "structures.h"
//#include "mtproto-common.h"
#include "tgl.h"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#ifdef __APPLE__
#define OPEN_BIN "open %s"
#else
#define OPEN_BIN "xdg-open %s"
#endif
#define ALLOW_MULT 1
char *default_prompt = "> ";
@ -222,29 +232,29 @@ char *get_default_prompt (void) {
if (in_chat_mode) {
tgl_peer_t *U = tgl_peer_get (chat_mode_id);
assert (U && U->print_name);
l += tsnprintf (buf + l, 999 - l, COLOR_RED "%.*s " COLOR_NORMAL, 100, 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) {
l += tsnprintf (buf + l, 999 - l, COLOR_RED "[");
l += snprintf (buf + l, 999 - l, COLOR_RED "[");
int ok = 0;
if (tgl_state.unread_messages) {
l += tsnprintf (buf + l, 999 - l, "%d unread", tgl_state.unread_messages);
l += snprintf (buf + l, 999 - l, "%d unread", tgl_state.unread_messages);
ok = 1;
}
if (tgl_state.cur_uploading_bytes) {
if (ok) { *(buf + l) = ' '; l ++; }
ok = 1;
l += tsnprintf (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 * tgl_state.cur_uploaded_bytes / tgl_state.cur_uploading_bytes);
}
if (tgl_state.cur_downloading_bytes) {
if (ok) { *(buf + l) = ' '; l ++; }
ok = 1;
l += tsnprintf (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 * tgl_state.cur_downloaded_bytes / tgl_state.cur_downloading_bytes);
}
l += tsnprintf (buf + l, 999 - l, "]" COLOR_NORMAL);
l += snprintf (buf + l, 999 - l, "]" COLOR_NORMAL);
return buf;
}
l += tsnprintf (buf + l, 999 - l, "%s", default_prompt);
l += snprintf (buf + l, 999 - l, "%s", default_prompt);
return buf;
}
@ -512,7 +522,137 @@ void work_modifier (const char *s, int l) {
#endif
}
void print_msg_list_gw (void *extra, int success, int num, struct tgl_message *ML[]) {
if (!success) { return; }
print_start ();
int i;
for (i = num - 1; i >= 0; i--) {
print_message (ML[i]);
}
print_end ();
}
void print_msg_gw (void *extra, int success, struct tgl_message *M) {
if (!success) { return; }
print_start ();
print_message (M);
print_end ();
}
void print_user_list_gw (void *extra, int success, int num, struct tgl_user *UL[]) {
if (!success) { return; }
print_start ();
int i;
for (i = num - 1; i >= 0; i--) {
print_user_name (UL[i]->id, (void *)UL[i]);
}
print_end ();
}
void print_filename_gw (void *extra, int success, char *name) {
if (!success) { return; }
print_start ();
printf ("Saved to %s\n", name);
print_end ();
}
void open_filename_gw (void *extra, int success, char *name) {
if (!success) { return; }
static char buf[PATH_MAX];
if (snprintf (buf, sizeof (buf), OPEN_BIN, name) >= (int) sizeof (buf)) {
logprintf ("Open image command buffer overflow\n");
} else {
int x = system (buf);
if (x < 0) {
logprintf ("Can not open image viewer: %m\n");
logprintf ("Image is at %s\n", name);
}
}
}
void print_chat_info_gw (void *extra, int success, struct tgl_chat *C) {
if (!success) { return; }
print_start ();
tgl_peer_t *U = (void *)C;
push_color (COLOR_YELLOW);
printf ("Chat ");
print_chat_name (U->id, U);
printf (" members:\n");
int i;
for (i = 0; i < C->user_list_size; i++) {
printf ("\t\t");
print_user_name (TGL_MK_USER (C->user_list[i].user_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].user_id)));
printf (" invited by ");
print_user_name (TGL_MK_USER (C->user_list[i].inviter_id), tgl_peer_get (TGL_MK_USER (C->user_list[i].inviter_id)));
printf (" at ");
print_date_full (C->user_list[i].date);
if (C->user_list[i].user_id == C->admin_id) {
printf (" admin");
}
printf ("\n");
}
pop_color ();
print_end ();
}
void print_user_info_gw (void *extra, int success, struct tgl_user *U) {
if (!success) { return; }
tgl_peer_t *C = (void *)U;
print_start ();
push_color (COLOR_YELLOW);
printf ("User ");
print_user_name (U->id, C);
printf (":\n");
printf ("\treal name: %s %s\n", U->real_first_name, U->real_last_name);
printf ("\tphone: %s\n", U->phone);
if (U->status.online > 0) {
printf ("\tonline\n");
} else {
printf ("\toffline (was online ");
print_date_full (U->status.when);
printf (")\n");
}
pop_color ();
print_end ();
}
void print_secret_chat_gw (void *extra, int success, struct tgl_secret_chat *E) {
if (!success) { return; }
print_start ();
push_color (COLOR_YELLOW);
printf (" Encrypted chat ");
print_encr_chat_name (E->id, (void *)E);
printf (" is now in wait state\n");
pop_color ();
print_end ();
}
void print_dialog_list_gw (void *extra, int success, int size, tgl_peer_id_t peers[], int last_msg_id[], int unread_count[]) {
if (!success) { return; }
print_start ();
push_color (COLOR_YELLOW);
int i;
for (i = size - 1; i >= 0; i--) {
tgl_peer_t *UC;
switch (tgl_get_peer_type (peers[i])) {
case TGL_PEER_USER:
UC = tgl_peer_get (peers[i]);
printf ("User ");
print_user_name (peers[i], UC);
printf (": %d unread\n", unread_count[i]);
break;
case TGL_PEER_CHAT:
UC = tgl_peer_get (peers[i]);
printf ("Chat ");
print_chat_name (peers[i], UC);
printf (": %d unread\n", unread_count[i]);
break;
}
}
pop_color ();
print_end ();
}
void interpreter_chat_mode (char *line) {
if (line == NULL || /* EOF received */
@ -525,18 +665,69 @@ 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);
tgl_do_get_history (chat_mode_id, limit, offline_mode, print_msg_list_gw, 0);
return;
}
if (!strncmp (line, "/read", 5)) {
tgl_do_mark_read (chat_mode_id);
tgl_do_mark_read (chat_mode_id, 0, 0);
return;
}
if (strlen (line)>0) {
tgl_do_send_message (chat_mode_id, line, strlen (line));
tgl_do_send_message (chat_mode_id, line, strlen (line), print_msg_gw, 0);
}
}
void mark_read_upd (int num, struct tgl_message *list[]) {
if (log_level < 1) { return; }
print_start ();
push_color (COLOR_YELLOW);
printf ("%d messages mark read\n", num);
pop_color ();
print_end ();
}
void type_notification_upd (struct tgl_user *U) {
if (log_level < 2) { return; }
print_start ();
push_color (COLOR_YELLOW);
printf ("User ");
print_user_name (U->id, (void *)U);
printf (" is typing\n");
pop_color ();
print_end ();
}
void type_in_chat_notification_upd (struct tgl_user *U, struct tgl_chat *C) {
if (log_level < 2) { return; }
print_start ();
push_color (COLOR_YELLOW);
printf ("User ");
print_user_name (U->id, (void *)U);
printf (" is typing in chat ");
print_chat_name (C->id, (void *)C);
printf ("\n");
pop_color ();
print_end ();
}
struct tgl_update_callback upd_cb = {
.new_msg = print_message,
.marked_read = mark_read_upd,
.logprintf = logprintf,
.type_notification = type_notification_upd,
.type_in_chat_notification = type_in_chat_notification_upd,
.type_in_secret_chat_notification = 0,
.status_notification = 0,
.user_registered = 0,
.user_activated = 0,
.new_authorization = 0,
.secret_chat_request = 0,
.secret_chat_established = 0,
.secret_chat_deleted = 0
};
void interpreter (char *line UU) {
assert (!in_readline);
in_readline = 1;
@ -607,9 +798,9 @@ void interpreter (char *line UU) {
}
if (IS_WORD ("contact_list")) {
tgl_do_update_contact_list ();
tgl_do_update_contact_list (print_user_list_gw, 0);
} else if (IS_WORD ("dialog_list")) {
tgl_do_get_dialog_list ();
tgl_do_get_dialog_list (print_dialog_list_gw, 0);
} else if (IS_WORD ("stats")) {
static char stat_buf[1 << 15];
tgl_print_stat (stat_buf, (1 << 15) - 1);
@ -622,7 +813,7 @@ void interpreter (char *line UU) {
printf ("Empty message\n");
RET;
}
tgl_do_send_message (id, s, strlen (s));
tgl_do_send_message (id, s, strlen (s), print_msg_gw, 0);
} else if (IS_WORD ("rename_chat")) {
GET_PEER_CHAT;
int t;
@ -631,7 +822,7 @@ void interpreter (char *line UU) {
printf ("Empty new name\n");
RET;
}
tgl_do_rename_chat (id, s);
tgl_do_rename_chat (id, s, print_msg_gw, 0);
} else if (IS_WORD ("send_photo")) {
GET_PEER;
int t;
@ -640,7 +831,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n");
RET;
}
tgl_do_send_photo (CODE_input_media_uploaded_photo, id, tstrndup (s, t));
tgl_do_send_photo (CODE_input_media_uploaded_photo, id, strndup (s, t), print_msg_gw, 0);
} else if (IS_WORD("send_video")) {
GET_PEER;
int t;
@ -649,7 +840,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n");
RET;
}
tgl_do_send_photo (CODE_input_media_uploaded_video, id, tstrndup (s, t));
tgl_do_send_photo (CODE_input_media_uploaded_video, id, strndup (s, t), print_msg_gw, 0);
} else if (IS_WORD ("send_text")) {
GET_PEER;
int t;
@ -658,7 +849,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n");
RET;
}
tgl_do_send_text (id, tstrndup (s, t));
tgl_do_send_text (id, strndup (s, t), print_msg_gw, 0);
} else if (IS_WORD ("fwd")) {
GET_PEER;
int num = next_token_int ();
@ -666,7 +857,7 @@ void interpreter (char *line UU) {
printf ("Bad msg id\n");
RET;
}
tgl_do_forward_message (id, num);
tgl_do_forward_message (id, num, print_msg_gw, 0);
} else if (IS_WORD ("load_photo")) {
long long num = next_token_int ();
if (num == NOT_FOUND) {
@ -675,9 +866,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_photo) {
tgl_do_load_photo (&M->media.photo, 1);
tgl_do_load_photo (&M->media.photo, print_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_photo) {
tgl_do_load_encr_video (&M->media.encr_video, 1); // this is not a bug.
tgl_do_load_encr_video (&M->media.encr_video, print_filename_gw, 0); // this is not a bug.
} else {
printf ("Bad msg id\n");
RET;
@ -690,9 +881,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_photo) {
tgl_do_load_photo (&M->media.photo, 2);
tgl_do_load_photo (&M->media.photo, open_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_photo) {
tgl_do_load_encr_video (&M->media.encr_video, 2); // this is not a bug.
tgl_do_load_encr_video (&M->media.encr_video, open_filename_gw, 0); // this is not a bug.
} else {
printf ("Bad msg id\n");
RET;
@ -705,7 +896,7 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_video) {
tgl_do_load_video_thumb (&M->media.video, 1);
tgl_do_load_video_thumb (&M->media.video, print_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -718,7 +909,7 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_video) {
tgl_do_load_video_thumb (&M->media.video, 2);
tgl_do_load_video_thumb (&M->media.video, open_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -731,9 +922,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_video) {
tgl_do_load_video (&M->media.video, 1);
tgl_do_load_video (&M->media.video, print_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_video) {
tgl_do_load_encr_video (&M->media.encr_video, 1);
tgl_do_load_encr_video (&M->media.encr_video, print_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -746,33 +937,33 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_video) {
tgl_do_load_video (&M->media.video, 2);
tgl_do_load_video (&M->media.video, open_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_video) {
tgl_do_load_encr_video (&M->media.encr_video, 2);
tgl_do_load_encr_video (&M->media.encr_video, open_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
}
} else if (IS_WORD ("chat_info")) {
GET_PEER_CHAT;
tgl_do_get_chat_info (id);
tgl_do_get_chat_info (id, offline_mode, print_chat_info_gw, 0);
} else if (IS_WORD ("user_info")) {
GET_PEER_USER;
tgl_do_get_user_info (id);
tgl_do_get_user_info (id, offline_mode, print_user_info_gw, 0);
} else if (IS_WORD ("history")) {
GET_PEER;
int limit = next_token_int ();
tgl_do_get_history (id, limit > 0 ? limit : 40);
tgl_do_get_history (id, limit > 0 ? limit : 40, offline_mode, print_msg_list_gw, 0);
} else if (IS_WORD ("chat_add_user")) {
GET_PEER_CHAT;
tgl_peer_id_t chat_id = id;
GET_PEER_USER;
tgl_do_add_user_to_chat (chat_id, id, 100);
tgl_do_add_user_to_chat (chat_id, id, 100, print_msg_gw, 0);
} else if (IS_WORD ("chat_del_user")) {
GET_PEER_CHAT;
tgl_peer_id_t chat_id = id;
GET_PEER_USER;
tgl_do_del_user_from_chat (chat_id, id);
tgl_do_del_user_from_chat (chat_id, id, print_msg_gw, 0);
} else if (IS_WORD ("add_contact")) {
int phone_len, first_name_len, last_name_len;
char *phone, *first_name, *last_name;
@ -791,7 +982,7 @@ void interpreter (char *line UU) {
printf ("No last name found\n");
RET;
}
tgl_do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 0);
tgl_do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 0, print_user_list_gw, 0);
} else if (IS_WORD ("rename_contact")) {
GET_PEER_USER;
tgl_peer_t *U = tgl_peer_get (id);
@ -817,7 +1008,7 @@ void interpreter (char *line UU) {
printf ("No last name found\n");
RET;
}
tgl_do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 1);
tgl_do_add_contact (phone, phone_len, first_name, first_name_len, last_name, last_name_len, 1, print_user_list_gw, 0);
} else if (IS_WORD ("help")) {
//print_start ();
push_color (COLOR_YELLOW);
@ -874,7 +1065,7 @@ void interpreter (char *line UU) {
printf ("Empty message\n");
RET;
}
tgl_do_msg_search (id, from, to, limit, s);
tgl_do_msg_search (id, from, to, limit, s, print_msg_list_gw, 0);
} else if (IS_WORD ("global_search")) {
int from = 0;
int to = 0;
@ -885,16 +1076,35 @@ void interpreter (char *line UU) {
printf ("Empty message\n");
RET;
}
tgl_do_msg_search (TGL_PEER_NOT_FOUND, from, to, limit, s);
tgl_do_msg_search (TGL_PEER_NOT_FOUND, from, to, limit, s, print_msg_list_gw, 0);
} else if (IS_WORD ("mark_read")) {
GET_PEER;
tgl_do_mark_read (id);
tgl_do_mark_read (id, 0, 0);
} else if (IS_WORD ("visualize_key")) {
static char *colors[4] = {COLOR_GREY, COLOR_CYAN, COLOR_BLUE, COLOR_GREEN};
GET_PEER_ENCR_CHAT;
tgl_do_visualize_key (id);
static unsigned char buf[16];
memset (buf, 0, sizeof (buf));
tgl_do_visualize_key (id, buf);
print_start ();
int i;
for (i = 0; i < 16; i++) {
int x = buf[i];
int j;
for (j = 0; j < 4; j ++) {
push_color (colors[x & 3]);
push_color (COLOR_INVERSE);
printf (" ");
pop_color ();
pop_color ();
x = x >> 2;
}
if (i & 1) { printf ("\n"); }
}
print_end ();
} else if (IS_WORD ("create_secret_chat")) {
GET_PEER;
tgl_do_create_secret_chat (id);
tgl_do_create_secret_chat (id, print_secret_chat_gw, 0);
} else if (IS_WORD ("create_group_chat")) {
GET_PEER;
int t;
@ -903,13 +1113,13 @@ void interpreter (char *line UU) {
printf ("Empty chat topic\n");
RET;
}
tgl_do_create_group_chat (id, s);
} else if (IS_WORD ("suggested_contacts")) {
tgl_do_get_suggested ();
tgl_do_create_group_chat (id, s, print_msg_gw, 0);
//} else if (IS_WORD ("suggested_contacts")) {
// tgl_do_get_suggested ();
} else if (IS_WORD ("status_online")) {
tgl_do_update_status (1);
tgl_do_update_status (1, 0, 0);
} else if (IS_WORD ("status_offline")) {
tgl_do_update_status (0);
tgl_do_update_status (0, 0, 0);
} else if (IS_WORD ("contacts_search")) {
int t;
char *s = next_token (&t);
@ -917,7 +1127,7 @@ void interpreter (char *line UU) {
printf ("Empty search query\n");
RET;
}
tgl_do_contacts_search (100, s);
tgl_do_contacts_search (100, s, print_user_list_gw, 0);
} else if (IS_WORD("send_audio")) {
GET_PEER;
int t;
@ -926,7 +1136,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n");
RET;
}
tgl_do_send_photo (CODE_input_media_uploaded_audio, id, tstrndup (s, t));
tgl_do_send_photo (CODE_input_media_uploaded_audio, id, strndup (s, t), print_msg_gw, 0);
} else if (IS_WORD("send_document")) {
GET_PEER;
int t;
@ -935,7 +1145,7 @@ void interpreter (char *line UU) {
printf ("Empty file name\n");
RET;
}
tgl_do_send_photo (CODE_input_media_uploaded_document, id, tstrndup (s, t));
tgl_do_send_photo (CODE_input_media_uploaded_document, id, strndup (s, t), print_msg_gw, 0);
} else if (IS_WORD ("load_audio")) {
long long num = next_token_int ();
if (num == NOT_FOUND) {
@ -944,9 +1154,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_audio) {
tgl_do_load_audio (&M->media.video, 1);
tgl_do_load_audio (&M->media.video, print_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_audio) {
tgl_do_load_encr_video (&M->media.encr_video, 1);
tgl_do_load_encr_video (&M->media.encr_video, print_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -959,9 +1169,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_audio) {
tgl_do_load_audio (&M->media.video, 2);
tgl_do_load_audio (&M->media.video, open_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_audio) {
tgl_do_load_encr_video (&M->media.encr_video, 2);
tgl_do_load_encr_video (&M->media.encr_video, open_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -974,7 +1184,7 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == (int)CODE_message_media_document) {
tgl_do_load_document_thumb (&M->media.document, 1);
tgl_do_load_document_thumb (&M->media.document, print_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -987,7 +1197,7 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == (int)CODE_message_media_document) {
tgl_do_load_document_thumb (&M->media.document, 2);
tgl_do_load_document_thumb (&M->media.document, open_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -1000,9 +1210,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_document) {
tgl_do_load_document (&M->media.document, 1);
tgl_do_load_document (&M->media.document, print_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_document) {
tgl_do_load_encr_video (&M->media.encr_video, 1);
tgl_do_load_encr_video (&M->media.encr_video, print_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -1015,9 +1225,9 @@ void interpreter (char *line UU) {
}
struct tgl_message *M = tgl_message_get (num);
if (M && !M->service && M->media.type == CODE_message_media_document) {
tgl_do_load_document (&M->media.document, 2);
tgl_do_load_document (&M->media.document, open_filename_gw, 0);
} else if (M && !M->service && M->media.type == CODE_decrypted_message_media_document) {
tgl_do_load_encr_video (&M->media.encr_video, 2);
tgl_do_load_encr_video (&M->media.encr_video, open_filename_gw, 0);
} else {
printf ("Bad msg id\n");
RET;
@ -1048,22 +1258,22 @@ void interpreter (char *line UU) {
printf ("Bad msg id\n");
RET;
}
tgl_do_delete_msg (num);
tgl_do_delete_msg (num, 0, 0);
} else if (IS_WORD ("restore_msg")) {
long long num = next_token_int ();
if (num == NOT_FOUND) {
printf ("Bad msg id\n");
RET;
}
tgl_do_restore_msg (num);
tgl_do_restore_msg (num, 0, 0);
} else if (IS_WORD ("delete_restore_msg")) {
long long num = next_token_int ();
if (num == NOT_FOUND) {
printf ("Bad msg id\n");
RET;
}
tgl_do_delete_msg (num);
tgl_do_restore_msg (num);
tgl_do_delete_msg (num, 0, 0);
tgl_do_restore_msg (num, 0, 0);
} else if (IS_WORD ("quit")) {
exit (0);
} else if (IS_WORD ("safe_quit")) {
@ -1095,7 +1305,7 @@ void print_start (void) {
if (readline_active) {
saved_point = rl_point;
#ifdef READLINE_GNU
saved_line = talloc (rl_end + 1);
saved_line = malloc (rl_end + 1);
saved_line[rl_end] = 0;
memcpy (saved_line, rl_line_buffer, rl_end);
@ -1103,7 +1313,7 @@ void print_start (void) {
rl_replace_line("", 0);
#else
assert (rl_end >= 0);
saved_line = talloc (rl_end + 1);
saved_line = malloc (rl_end + 1);
memcpy (saved_line, rl_line_buffer, rl_end + 1);
rl_line_buffer[0] = 0;
set_prompt ("");
@ -1125,7 +1335,7 @@ void print_end (void) {
#endif
rl_point = saved_point;
rl_redisplay();
tfree_str (saved_line);
free (saved_line);
}
prompt_was = 0;
}

30
loop.c
View File

@ -62,7 +62,7 @@ extern int unknown_user_list_pos;
extern int unknown_user_list[];
int register_mode;
extern int safe_quit;
extern int queries_num;
int queries_num;
void got_it (char *line, int len);
@ -77,6 +77,9 @@ static void stdin_read_callback (evutil_socket_t fd, short what, void *arg) {
}
}
void net_loop (int flags, int (*is_end)(void)) {
if (verbosity) {
logprintf ("Starting netloop\n");
}
struct event *ev = 0;
if (flags & 3) {
ev = event_new (tgl_state.ev_base, 0, EV_READ | EV_PERSIST, stdin_read_callback, (void *)(long)flags);
@ -106,6 +109,10 @@ void net_loop (int flags, int (*is_end)(void)) {
if (ev) {
event_free (ev);
}
if (verbosity) {
logprintf ("End of netloop\n");
}
}
char **_s;
@ -201,6 +208,16 @@ void export_auth_callback (void *DC, int success) {
}
}
int d_got_ok;
void get_difference_callback (void *extra, int success) {
assert (success);
d_got_ok = 1;
}
int dgot (void) {
return d_got_ok;
}
int zero[512];
@ -208,8 +225,11 @@ int readline_active;
int new_dc_num;
int wait_dialog_list;
extern struct tgl_update_callback upd_cb;
int loop (void) {
//on_start ();
tgl_set_callback (&upd_cb);
tgl_init ();
double t = tglt_get_double_time ();
@ -328,7 +348,7 @@ int loop (void) {
}
net_loop (0, signed_in);
bl_do_dc_signed (tgl_state.DC_working);
//bl_do_dc_signed (tgl_state.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])) {
@ -346,7 +366,7 @@ int loop (void) {
set_interface_callbacks ();
tgl_do_get_difference (0, 0);
tgl_do_get_difference (0, get_difference_callback, 0);
net_loop (0, dgot);
#ifdef USE_LUA
lua_diff_end ();
@ -354,11 +374,11 @@ int loop (void) {
tglm_send_all_unsent ();
tgl_do_get_dialog_list ();
/*tgl_do_get_dialog_list (get_dialogs_callback, 0);
if (wait_dialog_list) {
dialog_list_got = 0;
net_loop (0, dlgot);
}
}*/
return main_loop ();
}

View File

@ -13,14 +13,11 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <event2/event.h>
lua_State *luaState;
#include "structures.h"
#include "interface.h"
#include "auto/constants.h"
#include "tools.h"
#include "queries.h"
#include "net.h"
#include "tgl.h"
extern int verbosity;
@ -183,7 +180,7 @@ void push_message (struct tgl_message *M) {
lua_newtable (luaState);
static char s[30];
tsnprintf (s, 30, "%lld", M->id);
snprintf (s, 30, "%lld", M->id);
lua_add_string_field ("id", s);
lua_add_num_field ("flags", M->flags);
@ -355,19 +352,19 @@ void lua_do_all (void) {
int f = (long)lua_ptr[p ++];
switch (f) {
case 0:
tgl_do_send_message (((tgl_peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1]));
tfree_str (lua_ptr[p + 1]);
tgl_do_send_message (((tgl_peer_t *)lua_ptr[p])->id, lua_ptr[p + 1], strlen (lua_ptr[p + 1]), 0, 0);
free (lua_ptr[p + 1]);
p += 2;
break;
case 1:
tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1]);
tgl_do_forward_message (((tgl_peer_t *)lua_ptr[p])->id, (long)lua_ptr[p + 1], 0, 0);
p += 2;
break;
case 2:
#ifdef DEBUG
texists (lua_ptr[p], sizeof (tgl_peer_t));
#endif
tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p])->id);
tgl_do_mark_read (((tgl_peer_t *)lua_ptr[p])->id, 0, 0);
p += 1;
break;
default:
@ -404,7 +401,7 @@ static int send_msg_from_lua (lua_State *L) {
lua_ptr[pos ++] = (void *)2l;
lua_ptr[pos ++] = (void *)0l;
lua_ptr[pos ++] = P;
lua_ptr[pos ++] = tstrdup (msg);
lua_ptr[pos ++] = strdup (msg);
logprintf ("msg = %s\n", msg);
lua_pushboolean (L, 1);
@ -469,8 +466,8 @@ static int mark_read_from_lua (lua_State *L) {
return 1;
}
int lua_postpone_alarm (void *self) {
int *t = self;
static void lua_postpone_alarm (evutil_socket_t fd, short what, void *arg) {
int *t = arg;
lua_settop (luaState, 0);
//lua_checkstack (luaState, 20);
@ -488,9 +485,7 @@ int lua_postpone_alarm (void *self) {
if (r) {
logprintf ("lua: %s\n", lua_tostring (luaState, -1));
}
tfree (*(void **)(t + 2), sizeof (struct event_timer));
tfree (t, 16);
return 0;
}
static int postpone_from_lua (lua_State *L) {
@ -510,16 +505,14 @@ static int postpone_from_lua (lua_State *L) {
int a1 = luaL_ref (L, LUA_REGISTRYINDEX);
int a2 = luaL_ref (L, LUA_REGISTRYINDEX);
struct event_timer *ev = talloc (sizeof (*ev));
int *t = talloc (16);
int *t = malloc (16);
struct event *ev = evtimer_new (tgl_state.ev_base, lua_postpone_alarm, t);
t[0] = a1;
t[1] = a2;
*(void **)(t + 2) = ev;
ev->timeout = get_double_time () + timeout;
ev->alarm = (void *)lua_postpone_alarm;
ev->self = t;
insert_event_timer (ev);
struct timeval ts= { timeout, 0};
event_add (ev, &ts);
lua_pushboolean (L, 1);
return 1;

2
main.c
View File

@ -75,6 +75,7 @@
"# This is an empty config file\n" \
"# Feel free to put something here\n"
int verbosity;
char *default_username;
char *auth_token;
int msg_num_mode;
@ -410,6 +411,7 @@ void args_parse (int argc, char **argv) {
break;
case 'v':
tgl_incr_verbosity ();
verbosity ++;
break;
case 'N':
msg_num_mode ++;

View File

@ -76,7 +76,7 @@
//int verbosity;
static int auth_success;
static enum dc_state c_state;
//static enum dc_state c_state;
static char nonce[256];
static char new_nonce[256];
static char server_nonce[256];
@ -197,6 +197,7 @@ static int rpc_send_packet (struct connection *c) {
int total_len = len + 20;
assert (total_len > 0 && !(total_len & 0xfc000003));
total_len >>= 2;
vlogprintf (E_DEBUG, "writing packet: total_len = %d, len = %d\n", total_len, len);
if (total_len < 0x7f) {
assert (tgl_state.net_methods->write_out (c, &total_len, 1) == 1);
} else {
@ -257,7 +258,7 @@ static int process_respq_answer (struct connection *c, char *packet, int len) {
unsigned long long what;
unsigned p1, p2;
int i;
vlogprintf (E_DEBUG, "process_respq_answer(), len=%d\n", len);
vlogprintf (E_DEBUG, "process_respq_answer(), len=%d, op=0x%08x\n", len, *(int *)(packet + 20));
assert (len >= 76);
assert (!*(long long *) packet);
assert (*(int *) (packet + 16) == len - 20);
@ -414,7 +415,7 @@ static int process_respq_answer (struct connection *c, char *packet, int len) {
out_long (pk_fingerprint);
out_cstring ((char *) encrypt_buffer, l);
c_state = st_reqdh_sent;
tgl_state.net_methods->get_dc (c)->state = st_reqdh_sent;
return rpc_send_packet (c);
}
@ -608,7 +609,7 @@ static int process_dh_answer (struct connection *c, char *packet, int len) {
out_ints ((int *) server_nonce, 4);
out_cstring ((char *) encrypt_buffer, l);
c_state = st_client_dh_sent;
tgl_state.net_methods->get_dc (c)->state = st_client_dh_sent;
return rpc_send_packet (c);
}
@ -642,7 +643,7 @@ static int process_auth_complete (struct connection *c UU, char *packet, int len
//c->status = conn_error;
//sleep (1);
c_state = st_authorized;
tgl_state.net_methods->get_dc (c)->state = st_authorized;
//return 1;
vlogprintf (E_DEBUG, "Auth success\n");
auth_success ++;
@ -1038,7 +1039,7 @@ static int rpc_execute (struct connection *c, int op, int len) {
#endif
return 0;
default:
vlogprintf (E_ERROR, "fatal: cannot receive answer in state %d\n", c_state);
vlogprintf (E_ERROR, "fatal: cannot receive answer in state %d\n", D->state);
exit (2);
}
@ -1053,9 +1054,9 @@ static int tc_close (struct connection *c, int who) {
static int tc_becomes_ready (struct connection *c) {
vlogprintf (E_DEBUG, "outbound rpc connection from dc #%d becomed ready\n", tgl_state.net_methods->get_dc(c)->id);
char byte = 0xef;
assert (tgl_state.net_methods->write_out (c, &byte, 1) == 1);
tgl_state.net_methods->flush_out (c);
//char byte = 0xef;
//assert (tgl_state.net_methods->write_out (c, &byte, 1) == 1);
//tgl_state.net_methods->flush_out (c);
#if !defined(__MACH__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined (__CYGWIN__)
// setsockopt (c->fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){0}, 4);
@ -1067,8 +1068,10 @@ static int tc_becomes_ready (struct connection *c) {
case st_init:
send_req_pq_packet (c);
break;
case st_authorized:
break;
default:
vlogprintf (E_DEBUG, "c_state = %d\n", c_state);
vlogprintf (E_DEBUG, "c_state = %d\n", D->state);
assert (0);
}
return 0;

26
net.c
View File

@ -48,6 +48,7 @@
//#include "mtproto-client.h"
//#include "mtproto-common.h"
#include "tree.h"
#include "tools.h"
#ifndef POLLRDHUP
#define POLLRDHUP 0
@ -66,11 +67,11 @@ static void ping_alarm (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
vlogprintf (E_DEBUG + 2,"ping alarm\n");
assert (c->state == conn_ready || c->state == conn_connecting);
if (get_double_time () - c->last_receive_time > 20 * PING_TIMEOUT) {
if (tglt_get_double_time () - c->last_receive_time > 20 * PING_TIMEOUT) {
vlogprintf (E_WARNING, "fail connection: reason: ping timeout\n");
c->state = conn_failed;
fail_connection (c);
} else if (get_double_time () - c->last_receive_time > 5 * PING_TIMEOUT && c->state == conn_ready) {
} else if (tglt_get_double_time () - c->last_receive_time > 5 * PING_TIMEOUT && c->state == conn_ready) {
tgl_do_send_ping (c);
start_ping_timer (c);
} else {
@ -117,6 +118,7 @@ static void delete_connection_buffer (struct connection_buffer *b) {
}
int tgln_write_out (struct connection *c, const void *_data, int len) {
vlogprintf (E_DEBUG, "write_out: %d bytes\n", len);
const unsigned char *data = _data;
if (!len) { return 0; }
assert (len > 0);
@ -234,10 +236,15 @@ static void try_write (struct connection *c);
static void conn_try_read (evutil_socket_t fd, short what, void *arg) {
struct connection *c = arg;
vlogprintf (2, "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;
if (c->state == conn_connecting) {
c->state = conn_ready;
c->methods->ready (c);
}
try_write (c);
if (c->out_bytes) {
event_add (c->write_ev, 0);
@ -269,6 +276,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
fcntl (fd, F_SETFL, O_NONBLOCK);
if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) == -1) {
//vlogprintf (E_ERROR, "Can not connect to %s:%d %m\n", host, port);
if (errno != EINPROGRESS) {
vlogprintf (E_ERROR, "Can not connect to %s:%d %m\n", host, port);
close (fd);
@ -279,7 +287,7 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
c->fd = fd;
c->state = conn_connecting;
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
c->ip = tstrdup (host);
c->flags = 0;
c->port = port;
@ -288,9 +296,11 @@ struct connection *tgln_create_connection (const char *host, int port, struct se
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->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
c->write_ev = event_new (tgl_state.ev_base, c->fd, EV_WRITE, conn_try_write, c);
event_add (c->read_ev, 0);
struct timeval tv = {5, 0};
c->read_ev = event_new (tgl_state.ev_base, c->fd, EV_READ | EV_PERSIST, conn_try_read, c);
event_add (c->read_ev, &tv);
start_ping_timer (c);
@ -345,7 +355,7 @@ static void restart_connection (struct connection *c) {
c->fd = fd;
c->state = conn_connecting;
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
start_ping_timer (c);
Connections[fd] = c;
@ -499,7 +509,7 @@ static void try_read (struct connection *c) {
fflush (log_net_f);
}*/
if (r > 0) {
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
stop_ping_timer (c);
start_ping_timer (c);
}
@ -565,7 +575,7 @@ void tgl_connections_poll_result (struct pollfd *fds, int max) {
if (c->state == conn_connecting) {
vlogprintf (E_DEBUG, "connection ready\n");
c->state = conn_ready;
c->last_receive_time = get_double_time ();
c->last_receive_time = tglt_get_double_time ();
}
if (c->out_bytes) {
try_write (c);

113
queries.c
View File

@ -31,9 +31,6 @@
#include <fcntl.h>
#include <sys/utsname.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#include "include.h"
#include "mtproto-client.h"
@ -60,10 +57,8 @@
#define sha1 SHA1
#ifdef __APPLE__
#define OPEN_BIN "open %s"
#else
#define OPEN_BIN "xdg-open %s"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
int want_dc_num;
@ -278,8 +273,8 @@ int max_chat_size;
int max_bcast_size;
//int want_dc_num;
//int new_dc_num;
extern struct dc *DC_list[];
extern struct dc *DC_working;
//extern struct dc *DC_list[];
//extern struct dc *tgl_state.DC_working;
static void out_random (int n) {
assert (n <= 32);
@ -363,7 +358,7 @@ void tgl_do_help_get_config (void (*callback)(void *, int), void *callback_extra
clear_packet ();
tgl_do_insert_header ();
out_int (CODE_help_get_config);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &help_get_config_methods, 0, callback, callback_extra);
}
/* }}} */
@ -406,7 +401,7 @@ static int send_code_on_error (struct query *q UU, int error_code, int l, char *
//if (q->callback) {
// ((void (*)(void *, int, int, const char *))(q->callback)) (q->callback_extra, 0, 0, 0);
//}
assert (DC_working->id == want_dc_num);
assert (tgl_state.DC_working->id == want_dc_num);
tgl_do_send_code (q->extra, q->callback, q->callback_extra);
tfree_str (q->extra);
return 0;
@ -432,7 +427,7 @@ void tgl_do_send_code (const char *user, void (*callback)(void *callback_extra,
out_string (TG_APP_HASH);
out_string ("en");
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, tstrdup (user), callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_code_methods, tstrdup (user), callback, callback_extra);
}
@ -459,7 +454,7 @@ void tgl_do_phone_call (const char *user, const char *hash,void (*callback)(void
out_string (user);
out_string (hash);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &phone_call_methods, 0, callback, callback_extra);
}
/* }}} */
@ -484,7 +479,7 @@ int check_phone_on_error (struct query *q UU, int error_code, int l, char *error
assert (DC_list[i]);
dc_working_num = i;
DC_working = DC_list[i];
tgl_state.DC_working = DC_list[i];
write_auth_file ();
bl_do_set_working_dc (i);
@ -497,7 +492,7 @@ int check_phone_on_error (struct query *q UU, int error_code, int l, char *error
bl_do_set_working_dc (i);
DC_working = DC_list[i];
tgl_state.DC_working = DC_list[i];
write_auth_file ();
check_phone_result = 1;
} else {
@ -519,10 +514,10 @@ int tgl_do_auth_check_phone (const char *user) {
out_int (CODE_auth_check_phone);
out_string (user);
check_phone_result = -1;
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &check_phone_methods, 0);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &check_phone_methods, 0);
net_loop (0, cr_f);
check_phone_result = -1;
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &check_phone_methods, 0);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &check_phone_methods, 0);
net_loop (0, cr_f);
return check_phone_result;
}*/
@ -556,7 +551,7 @@ int tgl_do_get_nearest_dc (void) {
clear_packet ();
out_int (CODE_help_get_nearest_dc);
nearest_dc_num = -1;
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &nearest_dc_methods, 0);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &nearest_dc_methods, 0);
net_loop (0, nr_f);
return nearest_dc_num;
}*/
@ -570,9 +565,9 @@ static int sign_in_on_answer (struct query *q UU) {
struct tgl_user *U = tglf_fetch_alloc_user ();
DC_working->has_auth = 1;
tgl_state.DC_working->has_auth = 1;
bl_do_dc_signed (DC_working->id);
bl_do_dc_signed (tgl_state.DC_working->id);
if (q->callback) {
((void (*)(void *, int, struct tgl_user *))q->callback) (q->callback_extra, 1, U);
@ -592,7 +587,7 @@ int tgl_do_send_code_result (const char *user, const char *hash, const char *cod
out_string (user);
out_string (hash);
out_string (code);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}
@ -604,7 +599,7 @@ int tgl_do_send_code_result_auth (const char *user, const char *hash, const char
out_string (code);
out_string (first_name);
out_string (last_name);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &sign_in_methods, 0, callback, callback_extra);
return 0;
}
/* }}} */
@ -673,7 +668,7 @@ void tgl_do_update_contact_list (void (*callback) (void *callback_extra, int suc
clear_packet ();
out_int (CODE_contacts_get_contacts);
out_string ("");
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_contacts_methods, 0, callback, callback_extra);
}
/* }}} */
@ -900,7 +895,7 @@ void tgl_do_send_encr_msg_action (struct tgl_message *M, void (*callback)(void *
}
encr_finish (&P->encr_chat);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra);
}
void tgl_do_send_encr_msg (struct tgl_message *M, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) {
@ -933,7 +928,7 @@ void tgl_do_send_encr_msg (struct tgl_message *M, void (*callback)(void *callbac
out_int (CODE_decrypted_message_media_empty);
encr_finish (&P->encr_chat);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, 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) {
@ -946,7 +941,7 @@ void tgl_do_send_msg (struct tgl_message *M, void (*callback)(void *callback_ext
out_peer_id (M->to_id);
out_cstring (M->message, M->message_len);
out_long (M->id);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M, callback, 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) {
@ -1042,7 +1037,7 @@ void tgl_do_messages_mark_read (tgl_peer_id_t id, int max_id, void (*callback)(v
out_peer_id (id);
out_int (max_id);
out_int (0);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_methods, 0, callback, callback_extra);
}
void tgl_do_messages_mark_read_encr (tgl_peer_id_t id, long long access_hash, int last_time, void (*callback)(void *callback_extra, int), void *callback_extra) {
@ -1052,7 +1047,7 @@ void tgl_do_messages_mark_read_encr (tgl_peer_id_t id, long long access_hash, in
out_int (tgl_get_peer_id (id));
out_long (access_hash);
out_int (last_time);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_encr_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &mark_read_encr_methods, 0, callback, callback_extra);
}
void tgl_do_mark_read (tgl_peer_id_t id, void (*callback)(void *callback_extra, int success), void *callback_extra) {
@ -1162,7 +1157,7 @@ void tgl_do_get_history (tgl_peer_id_t id, int limit, int offline_mode, void (*c
out_int (0);
out_int (0);
out_int (limit);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods, (void *)*(long *)&id, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_history_methods, (void *)*(long *)&id, callback, callback_extra);
}
/* }}} */
@ -1249,7 +1244,7 @@ void tgl_do_get_dialog_list (void (*callback)(void *callback_extra, int success,
out_int (0);
out_int (0);
out_int (1000);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dialogs_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dialogs_methods, 0, callback, callback_extra);
}
/* }}} */
@ -1405,7 +1400,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra
assert (f->part_size == x);
}
//update_prompt ();
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra);
} else {
tgl_state.cur_uploaded_bytes -= f->size;
tgl_state.cur_uploading_bytes -= f->size;
@ -1452,7 +1447,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra
}
out_long (-lrand48 () * (1ll << 32) - lrand48 ());
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_methods, 0, callback, callback_extra);
} else {
struct tgl_message *M = talloc0 (sizeof (*M));
@ -1543,7 +1538,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra
M->id = r;
M->date = time (0);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_file_methods, M, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_file_methods, M, callback, callback_extra);
}
tfree_str (f->file_name);
tfree (f, sizeof (*f));
@ -1557,7 +1552,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra
out_long (f->thumb_id);
out_int (0);
out_cstring ((void *)thumb_file, thumb_file_size);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_file_part_methods, f, callback, callback_extra);
}*/
void tgl_do_send_photo (int type, tgl_peer_id_t to_id, char *file_name, void (*callback)(void *callback_extra, int success, struct tgl_message *M), void *callback_extra) {
@ -1661,7 +1656,7 @@ void tgl_do_forward_message (tgl_peer_id_t id, int n, void (*callback)(void *cal
out_peer_id (id);
out_int (n);
out_long (lrand48 () * (1ll << 32) + lrand48 ());
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra);
}
/* }}} */
@ -1700,7 +1695,7 @@ void tgl_do_rename_chat (tgl_peer_id_t id, char *name UU, void (*callback)(void
assert (tgl_get_peer_type (id) == TGL_PEER_CHAT);
out_int (tgl_get_peer_id (id));
out_string (name);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &rename_chat_methods, 0, callback, callback_extra);
}
/* }}} */
@ -1759,7 +1754,7 @@ void tgl_do_get_chat_info (tgl_peer_id_t id, int offline_mode, void (*callback)(
out_int (CODE_messages_get_full_chat);
assert (tgl_get_peer_type (id) == TGL_PEER_CHAT);
out_int (tgl_get_peer_id (id));
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &chat_info_methods, 0, callback, callback_extra);
}
/* }}} */
@ -1821,7 +1816,7 @@ void tgl_do_get_user_info (tgl_peer_id_t id, int offline_mode, void (*callback)(
out_int (CODE_input_user_contact);
out_int (tgl_get_peer_id (id));
}
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &user_info_methods, 0, callback, callback_extra);
}
/* }}} */
@ -1852,7 +1847,7 @@ void tgl_do_get_user_list_info_silent (int num, int *list) {
out_int (list[i]);
//out_long (0);
}
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &user_list_info_silent_methods, 0);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &user_list_info_silent_methods, 0);
}*/
/* }}} */
@ -1997,8 +1992,8 @@ static void load_next_part (struct download *D, void *callback, void *callback_e
}
out_int (D->offset);
out_int (1 << 14);
tglq_send_query (DC_list[D->dc], packet_ptr - packet_buffer, packet_buffer, &download_methods, D, callback, callback_extra);
//tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &download_methods, D);
tglq_send_query (tgl_state.DC_list[D->dc], packet_ptr - packet_buffer, packet_buffer, &download_methods, D, callback, callback_extra);
//tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &download_methods, D);
}
void tgl_do_load_photo_size (struct tgl_photo_size *P, void (*callback)(void *callback_extra, int success, char *filename), void *callback_extra) {
@ -2162,7 +2157,7 @@ void tgl_do_export_auth (int num, void (*callback) (void *callback_extra, int su
clear_packet ();
out_int (CODE_auth_export_authorization);
out_int (num);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &export_auth_methods, DC_list[num], callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &export_auth_methods, tgl_state.DC_list[num], callback, callback_extra);
}
/* }}} */
@ -2249,7 +2244,7 @@ void tgl_do_add_contact (const char *phone, int phone_len, const char *first_nam
out_cstring (first_name, first_name_len);
out_cstring (last_name, last_name_len);
out_int (force ? CODE_bool_true : CODE_bool_false);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &add_contact_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &add_contact_methods, 0, callback, callback_extra);
}
/* }}} */
@ -2285,7 +2280,7 @@ void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, const cha
out_int (0); // offset
out_int (0); // max_id
out_int (limit);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_search_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_search_methods, 0, callback, callback_extra);
}
/* }}} */
@ -2335,7 +2330,7 @@ void tgl_do_contacts_search (int limit, const char *s, void (*callback) (void *c
out_int (CODE_contacts_search);
out_string (s);
out_int (limit);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &contacts_search_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &contacts_search_methods, 0, callback, callback_extra);
}
/* }}} */
@ -2472,7 +2467,7 @@ void tgl_do_send_accept_encr_chat (struct tgl_secret_chat *E, unsigned char *ran
BN_clear_free (p);
BN_clear_free (r);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_accept_methods, E, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_accept_methods, E, callback, callback_extra);
}
void tgl_do_create_keys_end (struct tgl_secret_chat *U) {
@ -2576,13 +2571,13 @@ void tgl_do_send_create_encr_chat (void *x, unsigned char *random, void (*callba
}
out_int (tgl_get_peer_id (E->id));
out_cstring (g_a, 256);
write_secret_chat_file ();
//write_secret_chat_file ();
BN_clear_free (g);
BN_clear_free (p);
BN_clear_free (r);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_request_methods, E, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &send_encr_request_methods, E, callback, callback_extra);
}
static int get_dh_config_on_answer (struct query *q UU) {
@ -2631,7 +2626,7 @@ void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback
void **x = talloc (2 * sizeof (void *));
x[0] = tgl_do_send_accept_encr_chat;
x[1] = E;
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, 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) {
@ -2642,7 +2637,7 @@ void tgl_do_create_encr_chat_request (int user_id, void (*callback)(void *callba
void **x = talloc (2 * sizeof (void *));
x[0] = tgl_do_send_create_encr_chat;
x[1] = (void *)(long)(user_id);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_dh_config_methods, x, callback, callback_extra);
}
/* }}} */
@ -2767,10 +2762,10 @@ void tgl_do_get_difference (int sync_from_start, void (*callback)(void *callback
out_int (tgl_state.pts);
out_int (tgl_state.date);
out_int (tgl_state.qts);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_difference_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_difference_methods, 0, callback, callback_extra);
} else {
out_int (CODE_updates_get_state);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_state_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_state_methods, 0, callback, callback_extra);
}
}
/* }}} */
@ -2859,7 +2854,7 @@ void tgl_do_get_suggested (void) {
clear_packet ();
out_int (CODE_contacts_get_suggested);
out_int (100);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &get_suggested_methods, 0);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &get_suggested_methods, 0);
}*/
/* }}} */
@ -2886,7 +2881,7 @@ void tgl_do_add_user_to_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, int limit
out_int (tgl_get_peer_id (id));
}
out_int (limit);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, 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) {
@ -2904,7 +2899,7 @@ void tgl_do_del_user_from_chat (tgl_peer_id_t chat_id, tgl_peer_id_t id, void (*
out_int (CODE_input_user_contact);
out_int (tgl_get_peer_id (id));
}
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &add_user_to_chat_methods, 0, callback, callback_extra);
}
/* }}} */
@ -2949,7 +2944,7 @@ void tgl_do_create_group_chat (tgl_peer_id_t id, char *chat_topic, void (*callba
out_int (tgl_get_peer_id (id));
}
out_string (chat_topic);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &create_group_chat_methods, 0, callback, callback_extra);
}
/* }}} */
@ -2978,7 +2973,7 @@ void tgl_do_delete_msg (long long id, void (*callback)(void *callback_extra, int
out_int (CODE_vector);
out_int (1);
out_int (id);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &delete_msg_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &delete_msg_methods, 0, callback, callback_extra);
}
/* }}} */
@ -3007,7 +3002,7 @@ void tgl_do_restore_msg (long long id, void (*callback)(void *callback_extra, in
out_int (CODE_vector);
out_int (1);
out_int (id);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &restore_msg_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &restore_msg_methods, 0, callback, callback_extra);
}
/* }}} */
@ -3029,5 +3024,5 @@ void tgl_do_update_status (int online UU, void (*callback)(void *callback_extra,
clear_packet ();
out_int (CODE_account_update_status);
out_int (online ? CODE_bool_false : CODE_bool_true);
tglq_send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &update_status_methods, 0, callback, callback_extra);
tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &update_status_methods, 0, callback, callback_extra);
}

View File

@ -279,7 +279,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
return;
}
bl_do_encr_chat_delete (U);
write_secret_chat_file ();
//write_secret_chat_file ();
return;
}
@ -311,7 +311,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
}
bl_do_encr_chat_requested (U, access_hash, date, admin_id, user_id, (void *)g_key, (void *)nonce);
write_secret_chat_file ();
//write_secret_chat_file ();
} else {
bl_do_encr_chat_set_access_hash (U, fetch_long ());
bl_do_encr_chat_set_date (U, fetch_int ());
@ -325,7 +325,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
}
if (x == CODE_encrypted_chat_waiting) {
bl_do_encr_chat_set_state (U, sc_waiting);
write_secret_chat_file ();
//write_secret_chat_file ();
return; // We needed only access hash from here
}
@ -339,7 +339,7 @@ void tglf_fetch_encrypted_chat (struct tgl_secret_chat *U) {
return; // Duplicate?
}
bl_do_encr_chat_accepted (U, (void *)g_key, (void *)nonce, fetch_long ());
write_secret_chat_file ();
//write_secret_chat_file ();
}
}

3
tgl.c
View File

@ -8,6 +8,9 @@
#include "net.h"
#include <event2/event.h>
#include <assert.h>
struct tgl_state tgl_state;

View File

@ -297,7 +297,7 @@ void my_clock_gettime (int clock_id, struct timespec *T) {
#endif
}
double get_double_time (void) {
double tglt_get_double_time (void) {
struct timespec tv;
my_clock_gettime (CLOCK_REALTIME, &tv);
return tv.tv_sec + 1e-9 * tv.tv_nsec;

View File

@ -3,6 +3,7 @@
#include "mtproto-common.h"
#include "binlog.h"
#include "auto.h"
#include "structures.h"
#include <assert.h>