From db8a7a774b366ae293e2517b1935798a93501a94 Mon Sep 17 00:00:00 2001 From: vysheng Date: Tue, 22 Oct 2013 19:28:41 +0400 Subject: [PATCH] Some fixes --- interface.c | 2 +- interface.h | 1 + queries.c | 17 ++++++++++++----- queries.h | 2 +- structures.h | 1 + 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/interface.c b/interface.c index 4ded901..634ab6a 100644 --- a/interface.c +++ b/interface.c @@ -223,7 +223,7 @@ void interpreter (char *line UU) { } while (*q && (*q == ' ' || *q == '\t')) { q ++; } if (*q && index < user_num + chat_num) { - do_send_message (Peers[index], q); + do_send_message (Peers[index], q, strlen (q)); } } else if (!memcmp (line, "send_photo", 10)) { char *q = line + 10; diff --git a/interface.h b/interface.h index ecb94f6..2f9fb9b 100644 --- a/interface.h +++ b/interface.h @@ -21,6 +21,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) void hexdump (int *in_ptr, int *in_end); struct message; +union user_chat; void print_message (struct message *M); void print_chat_name (int id, union user_chat *C); void print_user_name (int id, union user_chat *U); diff --git a/queries.c b/queries.c index dfc9085..92a5877 100644 --- a/queries.c +++ b/queries.c @@ -107,6 +107,8 @@ void query_error (long long id) { queries_tree = tree_delete_query (queries_tree, q); if (q->methods && q->methods->on_error) { q->methods->on_error (q, error_code, error_len, error); + } else { + logprintf ( "error for query #%lld: #%d :%.*s\n", id, error_code, error_len, error); } free (q->data); free (q); @@ -451,7 +453,7 @@ struct query_methods msg_send_methods = { int out_message_num; int our_id; -void do_send_message (union user_chat *U, const char *msg) { +void do_send_message (union user_chat *U, const char *msg, int len) { if (!out_message_num) { out_message_num = -lrand48 (); } @@ -475,12 +477,15 @@ void do_send_message (union user_chat *U, const char *msg) { out_int (U->id); } } - M->message = strdup (msg); + M->message = malloc (len + 1); + memcpy (M->message, msg, len); + M->message[len] = 0; + M->message_len = len; M->out = 1; M->media.type = CODE_message_media_empty; M->id = out_message_num; M->date = time (0); - out_string (msg); + out_cstring (msg, len); out_long ((--out_message_num) - (1ll << 32)); send_query (DC_working, packet_ptr - packet_buffer, packet_buffer, &msg_send_methods, M); print_message (M); @@ -495,12 +500,14 @@ void do_send_text (union user_chat *U, char *file_name) { } static char buf[(1 << 20) + 1]; int x = read (fd, buf, (1 << 20) + 1); + assert (x >= 0); if (x == (1 << 20) + 1) { rprintf ("Too big file '%s'\n", file_name); free (file_name); close (fd); } else { - do_send_message (U, buf); + buf[x] = 0; + do_send_message (U, buf, x); free (file_name); close (fd); } @@ -738,7 +745,7 @@ void do_send_photo (int type, int to_id, char *file_name) { f->size = size; f->offset = 0; f->part_num = 0; - f->part_size = (size / 1000 + 0x3ff) & ~0x3ff; + f->part_size = ((size + 999) / 1000 + 0x3ff) & ~0x3ff; f->id = lrand48 () * (1ll << 32) + lrand48 (); f->to_id = to_id; f->media_type = type; diff --git a/queries.h b/queries.h index bb2f5b3..0200438 100644 --- a/queries.h +++ b/queries.h @@ -47,7 +47,7 @@ double get_double_time (void); void do_update_contact_list (void); union user_chat; -void do_send_message (union user_chat *U, const char *msg); +void do_send_message (union user_chat *U, const char *msg, int len); void do_send_text (union user_chat *U, char *file); void do_get_history (union user_chat *U, int limit); void do_get_dialog_list (void); diff --git a/structures.h b/structures.h index 02eb976..b2a5fe2 100644 --- a/structures.h +++ b/structures.h @@ -136,6 +136,7 @@ struct message { struct message_action action; struct { char *message; + int message_len; struct message_media media; }; };