From 0a0756ec6a03e3f84e927b97426f8bd5de02ecb8 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Thu, 19 Nov 2020 01:31:47 -0500 Subject: [PATCH] Fix/update group logging implementation --- src/chat_commands.c | 2 +- src/global_commands.c | 4 +- src/groupchats.c | 90 ++++++++++++++++++++++++++++++++----------- src/groupchats.h | 10 ++++- src/toxic.c | 2 +- 5 files changed, 80 insertions(+), 28 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index f054f56..c093931 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -219,7 +219,7 @@ void cmd_group_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (* return; } - if (init_groupchat_win(m, groupnumber, NULL, 0) == -1) { + if (init_groupchat_win(m, groupnumber, NULL, 0, Group_Join_Type_Join) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_group_leave(m, groupnumber, NULL, 0, NULL); return; diff --git a/src/global_commands.c b/src/global_commands.c index c13f3c3..bf610b0 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -521,7 +521,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg return; } - int init = init_groupchat_win(m, groupnumber, name, len); + int init = init_groupchat_win(m, groupnumber, name, len, Group_Join_Type_Create); if (init == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); @@ -598,7 +598,7 @@ void cmd_join(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA return; } - int init = init_groupchat_win(m, groupnumber, NULL, 0); + int init = init_groupchat_win(m, groupnumber, NULL, 0, Group_Join_Type_Join); if (init == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); diff --git a/src/groupchats.c b/src/groupchats.c index ce788aa..b97a8c1 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -265,7 +265,53 @@ void exit_groupchat(ToxWindow *self, Tox *m, uint32_t groupnumber, const char *p } tox_group_leave(m, groupnumber, (uint8_t *) partmessage, length, NULL); - close_groupchat(self, m, groupnumber); + + if (self != NULL) { + close_groupchat(self, m, groupnumber); + } +} + +/* + * Initializes groupchat log. This should only be called after we have the group name. + */ +static void init_groupchat_log(ToxWindow *self, Tox *m, uint32_t groupnumber) +{ + GroupChat *chat = get_groupchat(groupnumber); + + if (!chat) { + return; + } + + ChatContext *ctx = self->chatwin; + + char my_id[TOX_ADDRESS_SIZE]; + tox_self_get_address(m, (uint8_t *) my_id); + + char chat_id[TOX_GROUP_CHAT_ID_SIZE]; + + TOX_ERR_GROUP_STATE_QUERIES err; + + if (!tox_group_get_chat_id(m, groupnumber, (uint8_t *)chat_id, &err)) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch chat id. Logging disabled. (error: %d)", err); + return; + } + + if (log_init(ctx->log, chat->group_name, my_id, chat_id, LOG_TYPE_CHAT) != 0) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); + return; + } + + if (load_chat_history(self, ctx->log) != 0) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); + } + + if (user_settings->autolog == AUTOLOG_ON) { + if (log_enable(ctx->log) != 0) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); + } + } + + execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); // print log state to screen } /* Creates a new toxic groupchat window associated with groupnumber. @@ -273,17 +319,12 @@ void exit_groupchat(ToxWindow *self, Tox *m, uint32_t groupnumber, const char *p * Returns 0 on success. * Returns -1 on general failure. * Returns -2 if the groupnumber is already in use. This usually means that the client has - * been kicked and needs to close the chat window before opening a new one. + * been kicked and needs to close the chat window before opening a new one. */ -int init_groupchat_win(Tox *m, uint32_t groupnumber, const char *groupname, size_t length) +int init_groupchat_win(Tox *m, uint32_t groupnumber, const char *groupname, size_t length, Group_Join_Type join_type) { ToxWindow *self = new_group_chat(m, groupnumber, groupname, length); - /* In case we're loading a saved group */ - if (length == 0) { - groupchat_set_group_name(self, m, groupnumber); - } - for (int i = 0; i <= max_groupchat_index; ++i) { if (!groupchats[i].active) { groupchats[i].chatwin = add_window(m, self); @@ -307,6 +348,10 @@ int init_groupchat_win(Tox *m, uint32_t groupnumber, const char *groupname, size return -1; } + if (join_type == Group_Join_Type_Create || join_type == Group_Join_Type_Load) { + groupchat_set_group_name(self, m, groupnumber); + } + groupchat_onGroupPeerJoin(self, m, groupnumber, peer_id); return 0; @@ -901,6 +946,12 @@ void groupchat_onGroupPeerExit(ToxWindow *self, Tox *m, uint32_t groupnumber, ui static void groupchat_set_group_name(ToxWindow *self, Tox *m, uint32_t groupnumber) { + GroupChat *chat = get_groupchat(groupnumber); + + if (!chat) { + return; + } + TOX_ERR_GROUP_STATE_QUERIES err; size_t len = tox_group_get_name_size(m, groupnumber, &err); @@ -916,11 +967,12 @@ static void groupchat_set_group_name(ToxWindow *self, Tox *m, uint32_t groupnumb return; } - char groupname[TOX_GROUP_MAX_GROUP_NAME_LENGTH + 1]; - len = copy_tox_str(groupname, sizeof(groupname), tmp_groupname, len); + len = copy_tox_str(chat->group_name, sizeof(chat->group_name), tmp_groupname, len); + chat->group_name_length = len; if (len > 0) { - set_window_title(self, groupname, len); + set_window_title(self, chat->group_name, len); + init_groupchat_log(self, m, groupnumber); } } @@ -936,7 +988,6 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Tox *m, uint32_t groupnum return; } - groupchat_set_group_name(self, m, groupnumber); chat->time_connected = get_unix_time(); char topic[TOX_GROUP_MAX_TOPIC_LENGTH + 1]; @@ -962,6 +1013,10 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Tox *m, uint32_t groupnum line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- Topic set to: %s", topic); + if (chat->group_name_length == 0) { + groupchat_set_group_name(self, m, groupnumber); + } + /* Update own role since it may have changed while we were offline */ TOX_ERR_GROUP_SELF_QUERY s_err; TOX_GROUP_ROLE role = tox_group_self_get_role(m, groupnumber, &s_err); @@ -1583,17 +1638,6 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) line_info_init(ctx->hst); - if (user_settings->autolog == AUTOLOG_ON) { - char myid[TOX_ADDRESS_SIZE]; - tox_self_get_address(m, (uint8_t *) myid); - - if (log_enable(self->name, myid, NULL, ctx->log, LOG_CONFERENCE) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); - } - } - - execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); - scrollok(ctx->history, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0); } diff --git a/src/groupchats.h b/src/groupchats.h index 1e2ab38..488849c 100644 --- a/src/groupchats.h +++ b/src/groupchats.h @@ -32,6 +32,12 @@ #define MAX_GROUPCHAT_NUM (MAX_WINDOWS_NUM - 2) +typedef enum Group_Join_Type { + Group_Join_Type_Create, + Group_Join_Type_Join, + Group_Join_Type_Load, +} Group_Join_Type; + typedef struct GroupPeer { bool active; char name[TOX_MAX_NAME_LENGTH]; @@ -49,6 +55,8 @@ typedef struct { uint32_t num_peers; /* Number of peers in the chat/name_list array */ uint32_t max_idx; /* Maximum peer list index - 1 */ + char group_name[TOX_GROUP_MAX_GROUP_NAME_LENGTH + 1]; + size_t group_name_length; uint32_t groupnumber; bool active; uint64_t time_connected; /* The time we successfully connected to the group */ @@ -58,7 +66,7 @@ typedef struct { } GroupChat; void exit_groupchat(ToxWindow *self, Tox *m, uint32_t groupnumber, const char *partmessage, size_t length); -int init_groupchat_win(Tox *m, uint32_t groupnumber, const char *groupname, size_t length); +int init_groupchat_win(Tox *m, uint32_t groupnumber, const char *groupname, size_t length, Group_Join_Type join_type); void set_nick_all_groups(Tox *m, const char *new_nick, size_t length); void set_status_all_groups(Tox *m, uint8_t status); int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id); diff --git a/src/toxic.c b/src/toxic.c index 4436724..3bba1a3 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -478,7 +478,7 @@ static void load_groups(Tox *m) size_t numgroups = tox_group_get_number_groups(m); for (size_t i = 0; i < numgroups; ++i) { - if (init_groupchat_win(m, i, NULL, 0) != 0) { + if (init_groupchat_win(m, i, NULL, 0, Group_Join_Type_Load) != 0) { tox_group_leave(m, i, NULL, 0, NULL); } }