From 611338ee260e1646d63c847719c28e6d21e2dcdf Mon Sep 17 00:00:00 2001 From: Morten Date: Thu, 2 Oct 2014 18:39:49 +0200 Subject: [PATCH 1/9] typo fix --- mtproto-client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mtproto-client.c b/mtproto-client.c index 5c06148..1dab523 100644 --- a/mtproto-client.c +++ b/mtproto-client.c @@ -1301,7 +1301,7 @@ void tglmp_on_start (void) { } if (!ok) { - vlogprintf (E_ERROR, "No pubic keys found\n"); + vlogprintf (E_ERROR, "No public keys found\n"); exit (1); } From ecdec4620ddc97f779e0be37c6caf33688688d1b Mon Sep 17 00:00:00 2001 From: vvaltman Date: Thu, 2 Oct 2014 22:54:06 +0400 Subject: [PATCH 2/9] Added '-e' option --- loop.c | 16 ++++++++++++++++ main.c | 7 ++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/loop.c b/loop.c index cab2815..ee45225 100644 --- a/loop.c +++ b/loop.c @@ -91,6 +91,8 @@ static int delete_stdin_event; extern volatile int sigterm_cnt; +extern char *start_command; + static void stdin_read_callback_all (int arg, short what, struct event *self) { if (!readline_disabled) { if (((long)arg) & 1) { @@ -852,6 +854,20 @@ int loop (void) { lua_diff_end (); #endif + if (start_command) { + safe_quit = 1; + while (*start_command) { + char *start = start_command; + while (*start_command && *start_command != '\n') { + start_command ++; + } + if (*start_command) { + *start_command = 0; + start_command ++; + } + interpreter_ex (start, 0); + } + } /*tgl_do_get_dialog_list (get_dialogs_callback, 0); if (wait_dialog_list) { diff --git a/main.c b/main.c index d298d27..bac19f6 100644 --- a/main.c +++ b/main.c @@ -111,6 +111,7 @@ int readline_disabled; int disable_output; int reset_authorization; int port; +char *start_command; void set_default_username (const char *s) { if (default_username) { @@ -436,6 +437,7 @@ void usage (void) { printf (" -D disable output\n"); printf (" -P port to listen for input commands\n"); printf (" -S unix socket to create\n"); + printf (" -e make commands end exit\n"); exit (1); } @@ -538,7 +540,7 @@ char *unix_socket; void args_parse (int argc, char **argv) { int opt = 0; - while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:" + while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:" #ifdef HAVE_LIBCONFIG "c:p:" #else @@ -627,6 +629,9 @@ void args_parse (int argc, char **argv) { case 'S': unix_socket = optarg; break; + case 'e': + start_command = optarg; + break; case 'h': default: usage (); From e5419d2da6a40bf50459e7cfe20830b49dcd2a49 Mon Sep 17 00:00:00 2001 From: vvaltman Date: Thu, 2 Oct 2014 23:20:03 +0400 Subject: [PATCH 3/9] Check return code when downloading files --- main.c | 5 +++++ queries.c | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/main.c b/main.c index bac19f6..4ac1aec 100644 --- a/main.c +++ b/main.c @@ -401,6 +401,11 @@ void parse_config (void) { tasprintf (&secret_chat_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, SECRET_CHAT_FILE); } tgl_set_download_directory (downloads_directory); + if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) { + if (!disable_output) { + printf ("[%s] created\n", downloads_directory); + } + } } #endif diff --git a/queries.c b/queries.c index ec210a0..eb2779e 100644 --- a/queries.c +++ b/queries.c @@ -2415,6 +2415,10 @@ static int download_on_answer (struct query *q) { struct download *D = q->extra; if (D->fd == -1) { D->fd = open (D->name, O_CREAT | O_WRONLY, 0640); + if (D->fd < 0) { + vlogprintf (E_ERROR, "Can not open for writing: %m\n"); + assert (D->fd >= 0); + } } fetch_int (); // mtime int len = prefetch_strlen (); From 6087ad4097a0265631759357245c179ca28b7b53 Mon Sep 17 00:00:00 2001 From: Steve Illichevskiy Date: Fri, 3 Oct 2014 06:19:38 +0400 Subject: [PATCH 4/9] Add to ignore trash from debianisation --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index edd64af..5a05f71 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ objs/ dep/ auto/ libs/ +debian/telegram-cl* From 804bf0b82ebe0f8a9a1e3c01f280ce27dd98c5e3 Mon Sep 17 00:00:00 2001 From: Steve Illichevskiy Date: Fri, 3 Oct 2014 06:55:04 +0400 Subject: [PATCH 5/9] Try to use git-buildpakage --- .gitignore | 1 + debian/files | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 debian/files diff --git a/.gitignore b/.gitignore index 5a05f71..671dce1 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ dep/ auto/ libs/ debian/telegram-cl* +debian/files diff --git a/debian/files b/debian/files deleted file mode 100644 index cc7e0ea..0000000 --- a/debian/files +++ /dev/null @@ -1 +0,0 @@ -telegram-cli_1.0.5.1-1_i386.deb net extra From f1d2ecc66ee09e9f5795479c142f1d7c175d5959 Mon Sep 17 00:00:00 2001 From: Steve Illichevsky Date: Fri, 3 Oct 2014 12:15:16 +0400 Subject: [PATCH 6/9] Update control --- debian/control | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/debian/control b/debian/control index 06be8de..875ef16 100644 --- a/debian/control +++ b/debian/control @@ -3,14 +3,15 @@ Section: net Priority: extra Maintainer: Steve Illichevsky Build-Depends: debhelper (>= 8.0.0), - autotools-dev, - autoconf-archive, - libreadline-dev, - libconfig-dev, - libssl-dev, - lua5.1, - liblua5.1-dev -Standards-Version: 3.9.2 + autotools-dev, + autoconf-archive, + libreadline-dev, + libconfig-dev, + libssl-dev, + lua5.1, + liblua5.1-dev, + lua-lgi +Standards-Version: 3.9.5 Homepage: https://github.com/vysheng/tg Vcs-Git: git://github.com/vysheng/tg.git Vcs-Browser: https://github.com/vysheng/tg @@ -33,4 +34,4 @@ Description: Command-line interface for Telegram messenger * Group chat: create and manage groups. * Secret chat: secured and encrypted conversations. * Contact management: add/edit/remove contacts. - * Multimedia support: send/load photos and videos. \ No newline at end of file + * Multimedia support: send/load photos and videos. From 52470ce20780359c5dc20facae5933c3ca822f2a Mon Sep 17 00:00:00 2001 From: Steve Illichevsky Date: Fri, 3 Oct 2014 12:15:31 +0400 Subject: [PATCH 7/9] Update compat --- debian/compat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/compat b/debian/compat index 45a4fb7..ec63514 100644 --- a/debian/compat +++ b/debian/compat @@ -1 +1 @@ -8 +9 From 8fb606a7aa552b332d1346dfe6af6d3efcb76866 Mon Sep 17 00:00:00 2001 From: Vysheng Date: Fri, 3 Oct 2014 13:35:02 +0400 Subject: [PATCH 8/9] Added send_location query --- README-LUA | 2 ++ interface.c | 54 ++++++++++++++++++++++++++++++++++++------ lua-tg.c | 25 +++++++++++++++++++- queries.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++---- structures.c | 4 ++-- tgl.h | 1 + 6 files changed, 139 insertions(+), 14 deletions(-) diff --git a/README-LUA b/README-LUA index 7c2853a..6553d79 100644 --- a/README-LUA +++ b/README-LUA @@ -74,5 +74,7 @@ Function_list (arguments are listed aside from cb_function and cb_extra, : status_online () status_offline () + send_location (peer, latitude, longitude) + Also, you have function postpone (cb_function, cb_extra, timeout). It will call your cb_function in specified number of seconds (number of seconds may be double). diff --git a/interface.c b/interface.c index af13752..458fe65 100644 --- a/interface.c +++ b/interface.c @@ -292,6 +292,23 @@ long long cur_token_int (void) { } } +double cur_token_double (void) { + if (cur_token_len <= 0) { + return NOT_FOUND; + } else { + char c = cur_token[cur_token_len]; + cur_token[cur_token_len] = 0; + char *end = 0; + double x = strtod (cur_token, &end); + cur_token[cur_token_len] = c; + if (end != cur_token + cur_token_len) { + return NOT_FOUND; + } else { + return x; + } + } +} + tgl_peer_id_t cur_token_user (void) { if (cur_token_len <= 0) { return TGL_PEER_NOT_FOUND; } int l = cur_token_len; @@ -519,6 +536,7 @@ enum command_argument { ca_file_name_end, ca_period, ca_number, + ca_double, ca_string_end, ca_string, ca_modifier, @@ -536,6 +554,7 @@ struct arg { struct tgl_message *M; char *str; long long num; + double dval; }; }; @@ -996,6 +1015,11 @@ void do_clear (int arg_num, struct arg args[], struct in_ev *ev) { 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); +} + struct command commands[] = { {"help", {ca_none}, do_help, "help\tPrints this help"}, @@ -1054,6 +1078,7 @@ struct command commands[] = { {"send_contact", {ca_peer, ca_string, ca_string, ca_string, ca_none}, do_send_contact, "send_contact \tSends contact (not necessary telegram user)"}, {"main_session", {ca_none}, do_main_session, "main_session\tSends updates to this connection (or terminal). Useful only with listening socket"}, {"clear", {ca_none}, do_clear, "clear\tClears all data and exits. For debug."}, + {"send_location", {ca_peer, ca_double, ca_double, ca_none}, do_send_location, "send_location \tSends geo location"}, {0, {ca_none}, 0, ""} }; @@ -1116,7 +1141,7 @@ enum command_argument get_complete_mode (void) { char *save = line_ptr; next_token (); - if (op == ca_user || op == ca_chat || op == ca_secret_chat || op == ca_peer || op == ca_number) { + if (op == ca_user || op == ca_chat || op == ca_secret_chat || op == ca_peer || op == ca_number || op == ca_double) { if (cur_token_quoted) { if (opt) { line_ptr = save; @@ -1145,6 +1170,9 @@ enum command_argument get_complete_mode (void) { case ca_number: ok = (cur_token_int () != NOT_FOUND); break; + case ca_double: + ok = (cur_token_double () != NOT_FOUND); + break; default: assert (0); } @@ -1230,7 +1258,7 @@ char *command_generator (const char *text, int state) { if (index == -1) { return 0; } } - if (mode == ca_none || mode == ca_string || mode == ca_string_end || mode == ca_number) { + if (mode == ca_none || mode == ca_string || mode == ca_string_end || mode == ca_number || mode == ca_double) { if (c) { rl_line_buffer[rl_point] = c; } return 0; } @@ -1999,13 +2027,17 @@ void interpreter_ex (char *line UU, void *ex) { break; } - if (op == ca_user || op == ca_chat || op == ca_secret_chat || op == ca_peer || op == ca_number) { + if (op == ca_user || op == ca_chat || op == ca_secret_chat || op == ca_peer || op == ca_number || op == ca_double) { if (cur_token_quoted) { if (opt) { - if (op != ca_number) { + if (op != ca_number && op != ca_double) { args[args_num ++].P = 0; } else { - args[args_num ++].num = NOT_FOUND; + if (op == ca_number) { + args[args_num ++].num = NOT_FOUND; + } else { + args[args_num ++].dval = NOT_FOUND; + } } line_ptr = save; flags ++; @@ -2016,10 +2048,14 @@ void interpreter_ex (char *line UU, void *ex) { } else { if (cur_token_end_str) { if (opt) { - if (op != ca_number) { + if (op != ca_number && op != ca_double) { args[args_num ++].P = 0; } else { - args[args_num ++].num = NOT_FOUND; + if (op == ca_number) { + args[args_num ++].num = NOT_FOUND; + } else { + args[args_num ++].dval = NOT_FOUND; + } } line_ptr = save; flags ++; @@ -2050,6 +2086,10 @@ void interpreter_ex (char *line UU, void *ex) { args[args_num ++].num = cur_token_int (); ok = (args[args_num - 1].num != NOT_FOUND); break; + case ca_double: + args[args_num ++].dval = cur_token_double (); + ok = (args[args_num - 1].dval != NOT_FOUND); + break; default: assert (0); } diff --git a/lua-tg.c b/lua-tg.c index 7a63824..7d15c31 100644 --- a/lua-tg.c +++ b/lua-tg.c @@ -495,6 +495,7 @@ enum lua_query_type { lq_send_contact, lq_status_online, lq_status_offline, + lq_send_location, lq_extf }; @@ -1086,6 +1087,14 @@ void lua_do_all (void) { 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]); + } 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]); + } + p += 4; + break; /* lq_delete_msg, lq_restore_msg, @@ -1120,7 +1129,8 @@ enum lua_function_param { lfp_number, lfp_positive_number, lfp_nonnegative_number, - lfp_msg + lfp_msg, + lfp_double }; struct lua_function { @@ -1168,6 +1178,7 @@ struct lua_function functions[] = { {"send_contact", lq_send_contact, { lfp_peer, lfp_string, lfp_string, lfp_string, lfp_none }}, {"status_online", lq_status_online, { lfp_none }}, {"status_offline", lq_status_offline, { lfp_none }}, + {"send_location", lq_send_location, { lfp_peer, lfp_double, lfp_double, lfp_none }}, {"ext_function", lq_extf, { lfp_string, lfp_none }}, { 0, 0, { lfp_none}} }; @@ -1203,6 +1214,7 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) { const char *s; tgl_peer_t *P; long long num; + double dval; struct tgl_message *M; switch (F->params[p]) { case lfp_none: @@ -1256,6 +1268,17 @@ static int parse_lua_function (lua_State *L, struct lua_function *F) { lua_ptr[pos + p] = (void *)(long)num; break; + case lfp_double: + dval = lua_tonumber (L, -cc); + + if (sizeof (void *) == 4) { + *(float *)(lua_ptr + pos + p) = dval; + } else { + assert (sizeof (void *) >= 8); + *(double *)(lua_ptr + pos + p) = dval; + } + break; + case lfp_positive_number: num = lua_tonumber (L, -cc); if (num <= 0) { diff --git a/queries.c b/queries.c index eb2779e..f1ea070 100644 --- a/queries.c +++ b/queries.c @@ -1837,9 +1837,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_cstring ((void *)f->key, 32); out_cstring ((void *)f->init_iv, 32); - long long msg_id; - tglt_secure_random (&msg_id, 8); - bl_do_create_message_media_encr_pending (msg_id, tgl_state.our_id, tgl_get_peer_type (f->to_id), tgl_get_peer_id (f->to_id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); + bl_do_create_message_media_encr_pending (r, tgl_state.our_id, tgl_get_peer_type (f->to_id), tgl_get_peer_id (f->to_id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); encr_finish (&P->encr_chat); if (f->size < (16 << 20)) { @@ -1861,7 +1859,7 @@ static void send_part (struct send_file *f, void *callback, void *callback_extra out_int ((*(int *)md5) ^ (*(int *)(md5 + 4))); tfree_secure (f->iv, 32); - struct tgl_message *M = tgl_message_get (msg_id); + struct tgl_message *M = tgl_message_get (r); assert (M); //M->media.encr_photo.key = f->key; @@ -1997,6 +1995,7 @@ void tgl_do_set_profile_photo (char *file_name, void (*callback)(void *callback_ } /* }}} */ + /* {{{ Forward */ static int fwd_msg_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); @@ -2147,6 +2146,65 @@ void tgl_do_forward_media (tgl_peer_id_t id, int n, void (*callback)(void *callb } /* }}} */ +/* {{{ Send location */ + +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) { + if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) { + clear_packet (); + out_int (CODE_messages_send_encrypted); + out_int (CODE_input_encrypted_chat); + out_int (tgl_get_peer_id (id)); + tgl_peer_t *P = tgl_peer_get (id); + assert (P); + out_long (P->encr_chat.access_hash); + + long long r; + tglt_secure_random (&r, 8); + out_long (r); + encr_start (); + if (P->encr_chat.layer <= 16) { + out_int (CODE_decrypted_message_l16); + } else { + out_int (CODE_decrypted_message); + out_int (2 * P->encr_chat.in_seq_no + (P->encr_chat.admin_id != tgl_state.our_id)); + out_int (2 * P->encr_chat.out_seq_no + (P->encr_chat.admin_id == tgl_state.our_id) + 2); + out_int (0); + } + out_long (r); + out_random (15 + 4 * (lrand48 () % 3)); + out_string (""); + int *save_ptr = packet_ptr; + out_int (CODE_decrypted_message_media_geo_point); + out_double (latitude); + out_double (longitude); + + bl_do_create_message_media_encr_pending (r, tgl_state.our_id, tgl_get_peer_type (id), tgl_get_peer_id (id), time (0), 0, 0, save_ptr, packet_ptr - save_ptr); + + encr_finish (&P->encr_chat); + + struct tgl_message *M = tgl_message_get (r); + assert (M); + + tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_encr_methods, M, callback, callback_extra); + } else { + long long t; + tglt_secure_random (&t, 8); + vlogprintf (E_DEBUG, "t = %lld\n", t); + + clear_packet (); + out_int (CODE_messages_send_media); + out_peer_id (id); + out_int (CODE_input_media_geo_point); + out_int (CODE_input_geo_point); + out_double (latitude); + out_double (longitude); + out_long (t); + + tglq_send_query (tgl_state.DC_working, packet_ptr - packet_buffer, packet_buffer, &fwd_msg_methods, 0, callback, callback_extra); + } +} +/* }}} */ + /* {{{ Rename chat */ static int rename_chat_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_stated_message); @@ -3245,6 +3303,7 @@ void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback } 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) { + assert (0); clear_packet (); out_int (CODE_messages_get_dh_config); out_int (tgl_state.encr_param_version); diff --git a/structures.c b/structures.c index b39d92c..148ad3e 100644 --- a/structures.c +++ b/structures.c @@ -540,8 +540,8 @@ void tglf_fetch_photo_size (struct tgl_photo_size *S) { void tglf_fetch_geo (struct tgl_geo *G) { unsigned x = fetch_int (); if (x == CODE_geo_point) { - G->longitude = fetch_double (); G->latitude = fetch_double (); + G->longitude = fetch_double (); } else { assert (x == CODE_geo_point_empty); G->longitude = 0; @@ -951,8 +951,8 @@ void tglf_fetch_message_media_encrypted (struct tgl_message_media *M) { */ case CODE_decrypted_message_media_geo_point: M->type = tgl_message_media_geo; - M->geo.longitude = fetch_double (); M->geo.latitude = fetch_double (); + M->geo.longitude = fetch_double (); break; case CODE_decrypted_message_media_contact: M->type = tgl_message_media_contact; diff --git a/tgl.h b/tgl.h index af9f73b..617b2f4 100644 --- a/tgl.h +++ b/tgl.h @@ -295,6 +295,7 @@ void tgl_do_send_contact (tgl_peer_id_t id, const char *phone, int phone_len, co 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_visualize_key (tgl_peer_id_t id, unsigned char buf[16]); From 7a9764f24ac745eb03a72e2ddb69c8574f6a8c2f Mon Sep 17 00:00:00 2001 From: Vysheng Date: Fri, 3 Oct 2014 15:01:19 +0400 Subject: [PATCH 9/9] Deleted bad assert --- queries.c | 1 - 1 file changed, 1 deletion(-) diff --git a/queries.c b/queries.c index f1ea070..a72aa46 100644 --- a/queries.c +++ b/queries.c @@ -3303,7 +3303,6 @@ void tgl_do_accept_encr_chat_request (struct tgl_secret_chat *E, void (*callback } 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) { - assert (0); clear_packet (); out_int (CODE_messages_get_dh_config); out_int (tgl_state.encr_param_version);