From 7f38c3c6e7e7d0cd6091a4f152a15932995f2cd4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 1 Mar 2014 18:06:35 -0500 Subject: [PATCH] add prompt logging support --- src/chat.c | 2 +- src/global_commands.c | 43 ++++++++++++++++++++++++++----------------- src/log.c | 4 ++-- src/log.h | 4 ++-- src/main.c | 2 ++ src/prompt.c | 38 +++++++++++++++++++++++++++++++++----- src/toxic_windows.h | 2 ++ 7 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/chat.c b/src/chat.c index 3b4d3a9..5c8ef1b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -681,7 +681,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) memset(ctx->log, 0, sizeof(struct chatlog)); if (friends[self->num].logging_on) - log_enable(ctx->log, self->name, friends[self->num].pub_key); + log_enable(self->name, friends[self->num].pub_key, ctx->log); wprintw(ctx->history, "\n\n"); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); diff --git a/src/global_commands.c b/src/global_commands.c index 8072157..f15cc9e 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -222,22 +222,22 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - if (!self->is_chat && !self->is_groupchat) { /* remove if prompt logging gets implemented */ - wprintw(window, "Invalid command.\n"); - return; - } - - ChatContext *ctx = self->chatwin; - if (argc == 0) { - if (ctx->log->log_on) { - wprintw(window, "Logging for this chat is "); + bool on; + + if (self->is_chat || self->is_groupchat) + on = self->chatwin->log->log_on; + else if (self->is_prompt) + on = self->promptbuf->log->log_on; + + if (on) { + wprintw(window, "Logging for this window is "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]"); wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, ". Type \"/log off\" to disable.\n"); } else { - wprintw(window, "Logging for this chat is "); + wprintw(window, "Logging for this window is "); wattron(window, COLOR_PAIR(RED) | A_BOLD); wprintw(window, "[off]"); wattroff(window, COLOR_PAIR(RED) | A_BOLD); @@ -248,26 +248,34 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX } uint8_t *swch = argv[1]; - uint8_t *ident = NULL; if (!strcmp(swch, "1") || !strcmp(swch, "on")) { + if (self->is_chat) { friends[self->num].logging_on = true; - ident = friends[self->num].pub_key; + log_enable(self->name, friends[self->num].pub_key, self->chatwin->log); + } else if (self->is_prompt) { + uint8_t myid[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, myid); + log_enable(self->name, &myid, self->promptbuf->log); + } else if (self->is_groupchat) { + log_enable(self->name, NULL, self->chatwin->log); } - log_enable(ctx->log, self->name, ident); - wprintw(window, "Logging "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]\n"); wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { - if (self->is_chat) + if (self->is_chat) { friends[self->num].logging_on = false; - - log_disable(ctx->log); + log_disable(self->chatwin->log); + } else if (self->is_prompt) { + log_disable(self->promptbuf->log); + } else if (self->is_groupchat) { + log_disable(self->chatwin->log); + } wprintw(window, "Logging "); wattron(window, COLOR_PAIR(RED) | A_BOLD); @@ -363,6 +371,7 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wprintw(window, " /status : Set status with optional note\n"); wprintw(window, " /note : Set a personal note\n"); wprintw(window, " /nick : Set your nickname\n"); + wprintw(window, " /log or : Enable/disable logging\n"); wprintw(window, " /groupchat : Create a group chat\n"); wprintw(window, " /myid : Print your ID\n"); wprintw(window, " /help : Print this message again\n"); diff --git a/src/log.c b/src/log.c index 14cb648..2a83db6 100644 --- a/src/log.c +++ b/src/log.c @@ -1,4 +1,4 @@ -/* log->c +/* log.c * * * Copyright (C) 2014 Toxic All Rights Reserved. @@ -121,7 +121,7 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event write_to_log(log); } -void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key) +void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log) { log->log_on = true; diff --git a/src/log.h b/src/log.h index 3854f00..e4ab01c 100644 --- a/src/log.h +++ b/src/log.h @@ -31,5 +31,5 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event This is triggered automatically when the log buffer is full, but may be forced. */ void write_to_log(struct chatlog *log); -void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key); -void log_disable(struct chatlog *log); \ No newline at end of file +void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log); +void log_disable(struct chatlog *log); diff --git a/src/main.c b/src/main.c index 21ce5a7..fd4679d 100644 --- a/src/main.c +++ b/src/main.c @@ -479,6 +479,8 @@ void exit_toxic(Tox *m) free(DATA_FILE); free(SRVLIST_FILE); free(prompt->stb); + log_disable(prompt->promptbuf->log); + free(prompt->promptbuf->log); free(prompt->promptbuf); tox_kill(m); endwin(); diff --git a/src/prompt.c b/src/prompt.c index fb143ce..9116123 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -360,6 +360,18 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) static void prompt_onInit(ToxWindow *self, Tox *m) { scrollok(self->window, true); + PromptBuf *prt = self->promptbuf; + + prt->log = malloc(sizeof(struct chatlog)); + + if (prt->log == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + memset(prt->log, 0, sizeof(struct chatlog)); + execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE); wclrtoeol(self->window); } @@ -369,6 +381,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u if (friendnum < 0) return; + PromptBuf *prt = self->promptbuf; prep_prompt_win(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; @@ -382,22 +395,29 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u wprintw(self->window, "\n"); print_time(self->window); + uint8_t *msg; + if (status == 1) { + msg = "has come online\n"; wattron(self->window, COLOR_PAIR(GREEN)); wattron(self->window, A_BOLD); wprintw(self->window, "* %s ", nick); wattroff(self->window, A_BOLD); - wprintw(self->window, "has come online\n"); + wprintw(self->window, "%s", msg); wattroff(self->window, COLOR_PAIR(GREEN)); + add_to_log_buf(msg, nick, prt->log, true); alert_window(self, WINDOW_ALERT_2, false); } else { + msg = "has gone offline\n"; wattron(self->window, COLOR_PAIR(RED)); wattron(self->window, A_BOLD); wprintw(self->window, "* %s ", nick); wattroff(self->window, A_BOLD); - wprintw(self->window, "has gone offline\n"); + wprintw(self->window, "%s", msg); wattroff(self->window, COLOR_PAIR(RED)); + + add_to_log_buf(msg, nick, prt->log, true); } } @@ -405,16 +425,23 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, { // make sure message data is null-terminated data[length - 1] = 0; - + PromptBuf *prt = self->promptbuf; prep_prompt_win(); + wprintw(self->window, "\n"); print_time(self->window); - wprintw(self->window, "Friend request with the message: '%s'\n", data); + + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data); + wprintw(self->window, "%s", msg); + add_to_log_buf(msg, "", prt->log, true); int n = add_friend_request(key); if (n == -1) { - wprintw(self->window, "Friend request queue is full. Discarding request.\n"); + uint8_t *errmsg = "Friend request queue is full. Discarding request.\n"; + wprintw(self->window, "%s", errmsg); + add_to_log_buf(errmsg, "", prt->log, true); return; } @@ -460,6 +487,7 @@ ToxWindow new_prompt(void) memset(&ret, 0, sizeof(ret)); ret.active = true; + ret.is_prompt = true; ret.onKey = &prompt_onKey; ret.onDraw = &prompt_onDraw; diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 7466acf..d242d19 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -114,6 +114,7 @@ struct ToxWindow { /* window type identifiers */ bool is_chat; bool is_groupchat; + bool is_prompt; bool alert0; bool alert1; @@ -182,6 +183,7 @@ struct PromptBuf { int hst_pos; int hst_tot; + struct chatlog *log; WINDOW *linewin; };