From 0a6ce62363616ce09eae26bcb82a61fe3f97dfc8 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 11 Sep 2014 01:36:33 -0400 Subject: [PATCH] don't write unsent messages to log --- src/chat.c | 5 +---- src/log.c | 2 +- src/message_queue.c | 20 ++++++++++++++------ src/message_queue.h | 4 ++-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/chat.c b/src/chat.c index 3db1eb7..fd7c63c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -290,8 +290,7 @@ static void chat_onStatusMessageChange(ToxWindow *self, int32_t num, const char static void chat_onReadReceipt(ToxWindow *self, Tox *m, int32_t num, uint32_t receipt) { - struct chat_queue *q = self->chatwin->cqueue; - cqueue_remove(self, q, receipt); + cqueue_remove(self, m, receipt); } static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum, @@ -838,7 +837,6 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, char *action) get_time_str(timefrmt, sizeof(timefrmt)); line_info_add(self, timefrmt, selfname, NULL, OUT_ACTION, 0, 0, "%s", action); - write_to_log(action, selfname, ctx->log, true); cqueue_add(ctx->cqueue, action, strlen(action), OUT_ACTION, ctx->hst->line_end->id + 1); } @@ -920,7 +918,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) get_time_str(timefrmt, sizeof(timefrmt)); line_info_add(self, timefrmt, selfname, NULL, OUT_MSG, 0, 0, "%s", line); - write_to_log(line, selfname, ctx->log, false); cqueue_add(ctx->cqueue, line, strlen(line), OUT_MSG, ctx->hst->line_end->id + 1); } diff --git a/src/log.c b/src/log.c index 2819071..c76edfa 100644 --- a/src/log.c +++ b/src/log.c @@ -86,7 +86,7 @@ void init_logging_session(char *name, const char *key, struct chatlog *log) fprintf(log->file, "\n*** NEW SESSION ***\n\n"); } -#define LOG_FLUSH_LIMIT 2 /* limits calls to fflush to a max of one per LOG_FLUSH_LIMIT seconds */ +#define LOG_FLUSH_LIMIT 1 /* limits calls to fflush to a max of one per LOG_FLUSH_LIMIT seconds */ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event) { diff --git a/src/message_queue.c b/src/message_queue.c index 6b18a6f..5e2c46e 100644 --- a/src/message_queue.c +++ b/src/message_queue.c @@ -27,6 +27,7 @@ #include "message_queue.h" #include "misc_tools.h" #include "line_info.h" +#include "log.h" void cqueue_cleanup(struct chat_queue *q) { @@ -68,17 +69,17 @@ void cqueue_add(struct chat_queue *q, const char *msg, int len, uint8_t type, ui } /* update line to show receipt was received after queue removal */ -static void cqueue_mark_read(ToxWindow *self, uint32_t id, uint8_t type) +static void cqueue_mark_read(ToxWindow *self, struct cqueue_msg *msg) { struct line_info *line = self->chatwin->hst->line_end; while (line) { - if (line->id != id) { + if (line->id != msg->line_id) { line = line->prev; continue; } - line->type = type == OUT_ACTION ? OUT_ACTION_READ : OUT_MSG_READ; + line->type = msg->type == OUT_ACTION ? OUT_ACTION_READ : OUT_MSG_READ; if (line->noread_flag == true) { line->len -= 2; @@ -89,9 +90,10 @@ static void cqueue_mark_read(ToxWindow *self, uint32_t id, uint8_t type) } } -/* removes message with matching receipt from queue and updates line to show the message was received. */ -void cqueue_remove(ToxWindow *self, struct chat_queue *q, uint32_t receipt) +/* removes message with matching receipt from queue, writes to log and updates line to show the message was received. */ +void cqueue_remove(ToxWindow *self, Tox *m, uint32_t receipt) { + struct chat_queue *q = self->chatwin->cqueue; struct cqueue_msg *msg = q->root; while (msg) { @@ -100,7 +102,13 @@ void cqueue_remove(ToxWindow *self, struct chat_queue *q, uint32_t receipt) continue; } - cqueue_mark_read(self, msg->line_id, msg->type); + char selfname[TOX_MAX_NAME_LENGTH]; + uint16_t len = tox_get_self_name(m, (uint8_t *) selfname); + selfname[len] = '\0'; + + write_to_log(msg->message, selfname, self->chatwin->log, msg->type == OUT_ACTION); + cqueue_mark_read(self, msg); + struct cqueue_msg *next = msg->next; if (msg->prev == NULL) { /* root */ diff --git a/src/message_queue.h b/src/message_queue.h index 9831629..3ceec47 100644 --- a/src/message_queue.h +++ b/src/message_queue.h @@ -42,5 +42,5 @@ void cqueue_add(struct chat_queue *q, const char *msg, int len, uint8_t type, ui /* Tries to send the oldest unsent message in queue. */ void cqueue_try_send(ToxWindow *self, Tox *m); -/* removes message with matching receipt from queue and updates line to show the message was received. */ -void cqueue_remove(ToxWindow *self, struct chat_queue *q, uint32_t receipt); +/* removes message with matching receipt from queue, writes to log and updates line to show the message was received. */ +void cqueue_remove(ToxWindow *self, Tox *m, uint32_t receipt);