diff --git a/interface.c b/interface.c index 174fdbc..4ee41d6 100644 --- a/interface.c +++ b/interface.c @@ -231,6 +231,7 @@ char *commands[] = { "add_contact", "rename_contact", "show_license", + "search", 0 }; int commands_flags[] = { @@ -256,6 +257,7 @@ int commands_flags[] = { 07, 071, 07, + 072, }; int get_complete_mode (void) { @@ -671,6 +673,18 @@ void interpreter (char *line UU) { #include "LICENSE.h" ; printf ("%s", b); + } else if (IS_WORD ("search")) { + GET_PEER; + int from = 0; + int to = 0; + int limit = 40; + int t; + char *s = next_token (&t); + if (!s) { + printf ("Empty message\n"); + RET; + } + do_msg_search (id, from, to, limit, s); } #undef IS_WORD #undef RET diff --git a/mtproto-common.h b/mtproto-common.h index ff4f20a..02a920e 100644 --- a/mtproto-common.h +++ b/mtproto-common.h @@ -261,6 +261,7 @@ static inline void out_bignum (BIGNUM *n) { extern int *in_ptr, *in_end; +void fetch_pts (void); static inline int prefetch_strlen (void) { if (in_ptr >= in_end) { return -1; diff --git a/queries.c b/queries.c index 0e157bd..5db981e 100644 --- a/queries.c +++ b/queries.c @@ -607,12 +607,12 @@ int msg_send_on_answer (struct query *q UU) { assert (fetch_int () == (int)CODE_messages_sent_message); int id = fetch_int (); // id int date = fetch_int (); // date - int ptr = fetch_int (); // ptr + fetch_pts (); int seq = fetch_int (); // seq struct message *M = q->extra; M->id = id; message_insert (M); - logprintf ("Sent: id = %d, date = %d, ptr = %d, seq = %d\n", id, date, ptr, seq); + logprintf ("Sent: id = %d, date = %d, seq = %d\n", id, date, seq); return 0; } @@ -725,7 +725,7 @@ int get_history_on_answer (struct query *q UU) { for (i = 0; i < n; i++) { fetch_alloc_user (); } - if (sn > 0) { + if (sn > 0 && q->extra) { do_messages_mark_read (*(peer_id_t *)&(q->extra), ML[0]->id); } return 0; @@ -1447,3 +1447,25 @@ void do_add_contact (const char *phone, int phone_len, const char *first_name, i out_int (force ? CODE_bool_true : CODE_bool_false); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &add_contact_methods, 0); } + +int msg_search_on_answer (struct query *q UU) { + return get_history_on_answer (q); +} + +struct query_methods msg_search_methods = { + .on_answer = msg_search_on_answer +}; + +void do_msg_search (peer_id_t id, int from, int to, int limit, const char *s) { + clear_packet (); + out_int (CODE_messages_search); + out_peer_id (id); + out_string (s); + out_int (CODE_input_messages_filter_empty); + out_int (from); + out_int (to); + out_int (0); // offset + out_int (0); // max_id + out_int (limit); + send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_search_methods, 0); +} diff --git a/queries.h b/queries.h index 34e3838..5c2abf7 100644 --- a/queries.h +++ b/queries.h @@ -92,5 +92,6 @@ int do_send_code_result_auth (const char *code, const char *first_name, const ch void do_import_auth (int num); void do_export_auth (int num); void 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 do_msg_search (peer_id_t id, int from, int to, int limit, const char *s); #endif