From ff669be8d1e9221f605ddd773c3ffca4885a792f Mon Sep 17 00:00:00 2001 From: jfreegman Date: Tue, 4 Apr 2023 16:16:57 -0400 Subject: [PATCH] detach group names from global name Group names are now set on a per-group basis and are not affected by the global /nick command. --- src/execute.c | 1 + src/global_commands.c | 1 - src/groupchat_commands.c | 26 ++++++++++++ src/groupchat_commands.h | 1 + src/groupchats.c | 87 ++++++++++++++++++++++++++++------------ src/groupchats.h | 2 +- src/help.c | 5 ++- 7 files changed, 93 insertions(+), 30 deletions(-) diff --git a/src/execute.c b/src/execute.c index f7dfd3c..3a2a003 100644 --- a/src/execute.c +++ b/src/execute.c @@ -130,6 +130,7 @@ static struct cmd_func groupchat_commands[] = { { "/list", cmd_list }, { "/locktopic", cmd_set_topic_lock }, { "/mod", cmd_mod }, + { "/nick", cmd_group_nick }, { "/passwd", cmd_set_passwd }, { "/peerlimit", cmd_set_peerlimit }, { "/privacy", cmd_set_privacy }, diff --git a/src/global_commands.c b/src/global_commands.c index 57ec0b4..1652e70 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -777,7 +777,6 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA tox_self_set_name(m, (uint8_t *) nick, len, NULL); prompt_update_nick(prompt, nick); - set_nick_all_groups(m, nick, len); store_data(m, DATA_FILE); } diff --git a/src/groupchat_commands.c b/src/groupchat_commands.c index 571c26a..d2fbff3 100644 --- a/src/groupchat_commands.c +++ b/src/groupchat_commands.c @@ -76,6 +76,32 @@ void cmd_disconnect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar } } +void cmd_group_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + UNUSED_VAR(window); + + if (argc < 1) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); + return; + } + + char nick[MAX_STR_SIZE]; + snprintf(nick, sizeof(nick), "%s", argv[1]); + size_t len = strlen(nick); + + if (!valid_nick(nick)) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); + return; + } + + len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); + nick[len] = '\0'; + + set_nick_this_group(self, m, nick, len); + + store_data(m, DATA_FILE); +} + void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { diff --git a/src/groupchat_commands.h b/src/groupchat_commands.h index e308a0d..df65545 100644 --- a/src/groupchat_commands.h +++ b/src/groupchat_commands.h @@ -28,6 +28,7 @@ void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_disconnect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_group_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_list(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/groupchats.c b/src/groupchats.c index 4f480e1..df08ab3 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -363,40 +363,75 @@ int init_groupchat_win(Tox *m, uint32_t groupnumber, const char *groupname, size return -1; } -void set_nick_all_groups(Tox *m, const char *new_nick, size_t length) +void set_nick_this_group(ToxWindow *self, Tox *m, const char *new_nick, size_t length) { - for (int i = 0; i < max_groupchat_index; ++i) { - if (groupchats[i].active) { - ToxWindow *self = get_window_ptr(groupchats[i].chatwin); + if (self == NULL) { + return; + } - if (!self) { - continue; + char old_nick[TOX_MAX_NAME_LENGTH + 1]; + size_t old_length = get_group_self_nick_truncate(m, old_nick, self->num); + + Tox_Err_Group_Self_Name_Set err; + tox_group_self_set_name(m, self->num, (uint8_t *) new_nick, length, &err); + + GroupChat *chat = get_groupchat(self->num); + + if (chat == NULL) { + line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick: invalid groupnumber"); + return; + } + + switch (err) { + case TOX_ERR_GROUP_SELF_NAME_SET_OK: { + groupchat_onGroupSelfNickChange(self, m, self->num, old_nick, old_length, new_nick, length); + break; + } + + default: { + if (chat->time_connected > 0) { + line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); } - char old_nick[TOX_MAX_NAME_LENGTH + 1]; - size_t old_length = get_group_self_nick_truncate(m, old_nick, self->num); - - Tox_Err_Group_Self_Name_Set err; - tox_group_self_set_name(m, groupchats[i].groupnumber, (uint8_t *) new_nick, length, &err); - - switch (err) { - case TOX_ERR_GROUP_SELF_NAME_SET_OK: { - groupchat_onGroupSelfNickChange(self, m, self->num, old_nick, old_length, new_nick, length); - break; - } - - default: { - if (groupchats[i].time_connected > 0) { - line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); - } - - break; - } - } + break; } } } +/* void set_nick_all_groups(Tox *m, const char *new_nick, size_t length) */ +/* { */ +/* for (int i = 0; i < max_groupchat_index; ++i) { */ +/* if (groupchats[i].active) { */ +/* ToxWindow *self = get_window_ptr(groupchats[i].chatwin); */ + +/* if (!self) { */ +/* continue; */ +/* } */ + +/* char old_nick[TOX_MAX_NAME_LENGTH + 1]; */ +/* size_t old_length = get_group_self_nick_truncate(m, old_nick, self->num); */ + +/* Tox_Err_Group_Self_Name_Set err; */ +/* tox_group_self_set_name(m, groupchats[i].groupnumber, (uint8_t *) new_nick, length, &err); */ + +/* switch (err) { */ +/* case TOX_ERR_GROUP_SELF_NAME_SET_OK: { */ +/* groupchat_onGroupSelfNickChange(self, m, self->num, old_nick, old_length, new_nick, length); */ +/* break; */ +/* } */ + +/* default: { */ +/* if (groupchats[i].time_connected > 0) { */ +/* line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); */ +/* } */ + +/* break; */ +/* } */ +/* } */ +/* } */ +/* } */ +/* } */ + void set_status_all_groups(Tox *m, uint8_t status) { for (int i = 0; i < max_groupchat_index; ++i) { diff --git a/src/groupchats.h b/src/groupchats.h index 559fb91..0df7073 100644 --- a/src/groupchats.h +++ b/src/groupchats.h @@ -68,7 +68,7 @@ typedef struct { 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, Group_Join_Type join_type); -void set_nick_all_groups(Tox *m, const char *new_nick, size_t length); +void set_nick_this_group(ToxWindow *self, Tox *m, const char *new_nick, size_t length); void set_status_all_groups(Tox *m, uint8_t status); int get_peer_index(uint32_t groupnumber, uint32_t peer_id); void groupchat_onGroupPeerExit(ToxWindow *self, Tox *m, uint32_t groupnumber, uint32_t peer_id, diff --git a/src/help.c b/src/help.c index ea2e41f..dec24ed 100644 --- a/src/help.c +++ b/src/help.c @@ -183,7 +183,7 @@ static void help_draw_global(ToxWindow *self) wprintw(win, " /requests : List pending friend requests\n"); wprintw(win, " /status : Set status (Online, Busy, Away)\n"); wprintw(win, " /note : Set a personal note\n"); - wprintw(win, " /nick : Set your nickname\n"); + wprintw(win, " /nick : Set your global name (doesn't affect groups)\n"); wprintw(win, " /nospam : Change part of your Tox ID to stop spam\n"); wprintw(win, " /log or : Enable/disable logging\n"); wprintw(win, " /myid : Print your Tox ID\n"); @@ -307,6 +307,7 @@ static void help_draw_groupchats(ToxWindow *self) wprintw(win, " /list : Print a list of peers currently in the group\n"); wprintw(win, " /locktopic : Set the topic lock: on | off\n"); wprintw(win, " /mod : Promote a peer to moderator\n"); + wprintw(win, " /nick : Set your name for this group only\n"); wprintw(win, " /passwd : Set a password needed to join the group\n"); wprintw(win, " /peerlimit : Set the maximum number of peers that can join\n"); wprintw(win, " /privacy : Set the privacy state: private | public\n"); @@ -496,7 +497,7 @@ void help_onKey(ToxWindow *self, wint_t key) break; case L'r': - help_init_window(self, 26, 80); + help_init_window(self, 27, 80); self->help->type = HELP_GROUP; break; }