Fixed search

This commit is contained in:
Vysheng 2014-09-18 19:14:24 +04:00
parent efa3267528
commit db3bd6f970
4 changed files with 101 additions and 16 deletions

View File

@ -649,7 +649,7 @@ void do_show_license (int arg_num, struct arg args[]) {
}
void do_search (int arg_num, struct arg args[]) {
assert (arg_num == 5);
assert (arg_num == 6);
tgl_peer_id_t id;
if (args[0].P) {
id = args[0].P->id;
@ -674,7 +674,13 @@ void do_search (int arg_num, struct arg args[]) {
} else {
to = 0;
}
tgl_do_msg_search (id, from, to, limit, args[4].str, print_msg_list_gw, 0);
int offset;
if (args[4].num != NOT_FOUND) {
offset = args[4].num;
} else {
offset = 0;
}
tgl_do_msg_search (id, from, to, limit, offset, args[5].str, print_msg_list_gw, 0);
}
void do_mark_read (int arg_num, struct arg args[]) {
@ -890,7 +896,7 @@ struct command commands[] = {
{"del_contact", {ca_user, ca_none}, do_del_contact, "del_contact <user>\tDeletes contact from contact list"},
{"rename_contact", {ca_user, ca_string, ca_string, ca_none}, do_rename_contact, "rename_contact <user> <first name> <last name>\tRenames contact"},
{"show_license", {ca_none}, do_show_license, "show_license\tPrints contents of GPL license"},
{"search", {ca_peer | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_string_end}, do_search, "search [peer] [from] [to] pattern\tSearch for pattern in messages from date from to date to (unixtime) in messages with peer (if peer not present, in all messages)"},
{"search", {ca_peer | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_string_end}, do_search, "search [peer] [limit] [from] [to] [offset] pattern\tSearch for pattern in messages from date from to date to (unixtime) in messages with peer (if peer not present, in all messages)"},
{"mark_read", {ca_peer, ca_none}, do_mark_read, "mark_read <peer>\tMarks messages with peer as read"},
{"visualize_key", {ca_secret_chat, ca_none}, do_visualize_key, "visualize_key <secret chat>\tPrints visualization of encryption key (first 16 bytes sha1 of it in fact}"},
{"create_secret_chat", {ca_user, ca_none}, do_create_secret_chat, "create_secret_chat <user>\tStarts creation of secret chat"},
@ -1817,11 +1823,6 @@ void interpreter (char *line UU) {
}
if (opt && !ok) {
if (op != ca_number) {
args[args_num ++].P = 0;
} else {
args[args_num ++].num = NOT_FOUND;
}
line_ptr = save;
flags ++;
continue;

View File

@ -1020,13 +1020,13 @@ 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, s, lua_msg_list_cb, lua_ptr[p]);
tgl_do_msg_search (((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, s, lua_msg_list_cb, lua_ptr[p]);
tgl_do_msg_search (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;

View File

@ -2715,8 +2715,76 @@ void tgl_do_del_contact (tgl_peer_id_t id, void (*callback)(void *callback_extra
/* }}} */
/* {{{ Msg search */
void _tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, int max_id, char *s, int list_offset, int list_size, struct tgl_message **list, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra);
static int msg_search_on_answer (struct query *q UU) {
return get_history_on_answer (q);
int count = -1;
int i;
int x = fetch_int ();
assert (x == (int)CODE_messages_messages_slice || x == (int)CODE_messages_messages);
if (x == (int)CODE_messages_messages_slice) {
count = fetch_int ();
//fetch_int ();
}
assert (fetch_int () == CODE_vector);
void **T = q->extra;
struct tgl_message **ML = T[0];
int list_offset = (long)T[1];
int list_size = (long)T[2];
tgl_peer_id_t id = tgl_set_peer_id ((long)T[4], (long)T[3]);
int limit = (long)T[5];
int offset = (long)T[6];
int from = (long)T[7];
int to = (long)T[8];
char *s = T[9];
tfree (T, sizeof (void *) * 10);
int n = fetch_int ();
if (list_size - list_offset < n) {
int new_list_size = 2 * list_size;
if (new_list_size - list_offset < n) {
new_list_size = n + list_offset;
}
ML = trealloc (ML, list_size * sizeof (void *), new_list_size * sizeof (void *));
assert (ML);
list_size = new_list_size;
}
//struct tgl_message **ML = talloc (sizeof (void *) * n);
for (i = 0; i < n; i++) {
ML[i + list_offset] = tglf_fetch_alloc_message ();
}
list_offset += n;
offset += n;
limit -= n;
if (count >= 0 && limit + offset >= count) {
limit = count - offset;
if (limit < 0) { limit = 0; }
}
assert (limit >= 0);
assert (fetch_int () == CODE_vector);
n = fetch_int ();
for (i = 0; i < n; i++) {
tglf_fetch_alloc_chat ();
}
assert (fetch_int () == CODE_vector);
n = fetch_int ();
for (i = 0; i < n; i++) {
tglf_fetch_alloc_user ();
}
if (limit <= 0 || x == (int)CODE_messages_messages) {
if (q->callback) {
((void (*)(void *, int, int, struct tgl_message **))q->callback) (q->callback_extra, 1, list_offset, ML);
}
tfree_str (s);
tfree (ML, sizeof (void *) * list_size);
} else {
_tgl_do_msg_search (id, from, to, limit, 0, ML[list_offset - 1]->id, s, list_offset, list_size, ML, q->callback, q->callback_extra);
}
return 0;
}
static struct query_methods msg_search_methods = {
@ -2724,7 +2792,7 @@ static struct query_methods msg_search_methods = {
.type = TYPE_TO_PARAM(messages_messages)
};
void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, const char *s, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) {
void _tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, int offset, int max_id, char *s, int list_offset, int list_size, struct tgl_message **list, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), void *callback_extra) {
if (tgl_get_peer_type (id) == TGL_PEER_ENCR_CHAT) {
vlogprintf (E_WARNING, "Can not search in secure chat\n");
if (callback) {
@ -2739,14 +2807,30 @@ void tgl_do_msg_search (tgl_peer_id_t id, int from, int to, int limit, const cha
} else {
out_peer_id (id);
}
void **T = talloc (sizeof (void *) * 10);
T[0] = list;
T[1] = (void *)(long)list_offset;
T[2] = (void *)(long)list_size;
T[3] = (void *)(long)tgl_get_peer_id (id);
T[4] = (void *)(long)tgl_get_peer_type (id);
T[5] = (void *)(long)limit;
T[6] = (void *)(long)offset;
T[7] = (void *)(long)from;
T[8] = (void *)(long)to;
T[9] = s;
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 (offset); // offset
out_int (max_id); // max_id
out_int (limit);
tglq_send_query (tgl_state.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, T, callback, 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) {
_tgl_do_msg_search (id, from, to, limit, offset, 0, tstrdup (s), 0, 0, 0, callback, callback_extra);
}
/* }}} */

2
tgl.h
View File

@ -267,7 +267,7 @@ void tgl_do_load_document_thumb (struct tgl_document *video, void (*callback)(vo
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, const char *s, void (*callback)(void *callback_extra, int success, int size, struct tgl_message *list[]), 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);