From 011aac836929c0cb005753ed2f649a2a068e223a Mon Sep 17 00:00:00 2001 From: jfreegman Date: Tue, 23 Jan 2024 12:11:24 -0500 Subject: [PATCH] feat: add command to get the conference unique ID --- src/conference.c | 4 ++++ src/conference_commands.c | 28 ++++++++++++++++++++++++++++ src/conference_commands.h | 1 + src/execute.c | 1 + src/groupchat_commands.c | 18 +++++++++--------- src/groupchats.c | 1 + src/groupchats.h | 2 -- src/help.c | 3 ++- 8 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/conference.c b/src/conference.c index f223bdddf..7900e9cef 100644 --- a/src/conference.c +++ b/src/conference.c @@ -24,6 +24,7 @@ #define _GNU_SOURCE /* needed for strcasestr() and wcswidth() */ #endif +#include #include #include #include @@ -70,6 +71,8 @@ static int max_conference_index = 0; extern struct Winthread Winthread; +static_assert(TOX_CONFERENCE_ID_SIZE == TOX_PUBLIC_KEY_SIZE, "TOX_CONFERENCE_ID_SIZE != TOX_PUBLIC_KEY_SIZE"); + /* Array of conference command names used for tab completion. */ static const char *conference_cmd_list[] = { "/accept", @@ -78,6 +81,7 @@ static const char *conference_cmd_list[] = { "/audio", #endif "/avatar", + "/chatid", "/clear", "/close", "/color", diff --git a/src/conference_commands.c b/src/conference_commands.c index b3b6043af..8d3daac45 100644 --- a/src/conference_commands.c +++ b/src/conference_commands.c @@ -35,6 +35,34 @@ static void print_err(ToxWindow *self, const Client_Config *c_config, const char line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } +void cmd_conference_chatid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) +{ + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; + + char id_string[TOX_GROUP_CHAT_ID_SIZE * 2 + 1] = {0}; + char id[TOX_CONFERENCE_ID_SIZE]; + + if (!tox_conference_get_id(toxic->tox, self->num, (uint8_t *) id)) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve the Chat ID."); + return; + } + + char tmp[3]; + + for (size_t i = 0; i < TOX_CONFERENCE_ID_SIZE; ++i) { + snprintf(tmp, sizeof(tmp), "%02X", id[i] & 0xff); + strcat(id_string, tmp); + } + + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", id_string); +} + void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); diff --git a/src/conference_commands.h b/src/conference_commands.h index 310eeea22..6704a167c 100644 --- a/src/conference_commands.h +++ b/src/conference_commands.h @@ -26,6 +26,7 @@ #include "toxic.h" #include "windows.h" +void cmd_conference_chatid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_conference_set_title(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_enable_audio(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_conference_mute(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/execute.c b/src/execute.c index 62619e652..406b5f09e 100644 --- a/src/execute.c +++ b/src/execute.c @@ -112,6 +112,7 @@ static struct cmd_func chat_commands[] = { }; static struct cmd_func conference_commands[] = { + { "/chatid", cmd_conference_chatid }, { "/title", cmd_conference_set_title }, #ifdef AUDIO diff --git a/src/groupchat_commands.c b/src/groupchat_commands.c index 9e59db1b2..0eb055545 100644 --- a/src/groupchat_commands.c +++ b/src/groupchat_commands.c @@ -42,24 +42,24 @@ void cmd_chatid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* const Client_Config *c_config = toxic->c_config; - char chatid[TOX_GROUP_CHAT_ID_SIZE * 2 + 1] = {0}; - char chat_public_key[TOX_GROUP_CHAT_ID_SIZE]; + char id_string[TOX_GROUP_CHAT_ID_SIZE * 2 + 1] = {0}; + char chat_id[TOX_GROUP_CHAT_ID_SIZE]; Tox_Err_Group_State_Queries err; - if (!tox_group_get_chat_id(toxic->tox, self->num, (uint8_t *) chat_public_key, &err)) { - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve the Chat ID (error %d).", - err); + if (!tox_group_get_chat_id(toxic->tox, self->num, (uint8_t *) chat_id, &err)) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve the Chat ID (error %d).", err); return; } + char tmp[3]; + for (size_t i = 0; i < TOX_GROUP_CHAT_ID_SIZE; ++i) { - char xx[3]; - snprintf(xx, sizeof(xx), "%02X", chat_public_key[i] & 0xff); - strcat(chatid, xx); + snprintf(tmp, sizeof(tmp), "%02X", chat_id[i] & 0xff); + strcat(id_string, tmp); } - line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", chatid); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", id_string); } void cmd_disconnect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) diff --git a/src/groupchats.c b/src/groupchats.c index 86b87795f..fa50b233f 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -24,6 +24,7 @@ #define _GNU_SOURCE /* needed for strcasestr() and wcswidth() */ #endif +#include #include #include #include diff --git a/src/groupchats.h b/src/groupchats.h index e13f61cb9..0dd9fa73f 100644 --- a/src/groupchats.h +++ b/src/groupchats.h @@ -23,8 +23,6 @@ #ifndef GROUPCHATS_H #define GROUPCHATS_H -#include - #include "toxic.h" #include "windows.h" diff --git a/src/help.c b/src/help.c index cc8b81923..8e905f750 100644 --- a/src/help.c +++ b/src/help.c @@ -365,6 +365,7 @@ static void help_draw_conference(ToxWindow *self) wprintw(win, "Conference commands:\n"); wattroff(win, A_BOLD | COLOR_PAIR(RED)); + wprintw(win, " /chatid : Print this conference's ID\n"); wprintw(win, " /title : Show/set conference title\n"); #ifdef AUDIO wattron(win, A_BOLD); @@ -467,7 +468,7 @@ void help_onKey(ToxWindow *self, wint_t key) break; case L'o': - height = 6; + height = 7; #ifdef AUDIO height += 7; #endif