Skip to content

Commit

Permalink
feat: Add the ability to pass a password to the groupchat /rejoin com…
Browse files Browse the repository at this point in the history
…mand

This replaces the deprecated tox_group_reconnect command with tox_group_join
which now supports the ability to reconnect to groups with a password.
  • Loading branch information
JFreegman committed Dec 20, 2024
1 parent 306c3fd commit 204a0e9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ static const char special_commands[][MAX_CMDNAME_SIZE] = {
"/nick",
"/note",
"/passwd",
"/rejoin",
"/silence",
"/topic",
"/unignore",
Expand Down
9 changes: 7 additions & 2 deletions src/global_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char
self_nick[nick_length] = '\0';

Tox_Err_Group_New err;
uint32_t groupnumber = tox_group_new(tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *) name, len,
(const uint8_t *) self_nick, nick_length, &err);
const uint32_t groupnumber = tox_group_new(tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *) name, len,
(const uint8_t *) self_nick, nick_length, &err);

if (err != TOX_ERR_GROUP_NEW_OK) {
switch (err) {
Expand Down Expand Up @@ -625,6 +625,11 @@ void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar
return;
}

if (get_groupnumber_by_public_key_string(chat_id) != -1) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You are already in this group.");
return;
}

char id_bin[TOX_GROUP_CHAT_ID_SIZE] = {0};

size_t i;
Expand Down
36 changes: 33 additions & 3 deletions src/groupchat_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,12 +867,42 @@ void cmd_rejoin(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*
return;
}

Tox *tox = toxic->tox;
const Client_Config *c_config = toxic->c_config;

Tox_Err_Group_Reconnect err;
char chat_id[TOX_GROUP_CHAT_ID_SIZE];
Tox_Err_Group_State_Query chatid_err;

if (!tox_group_get_chat_id(tox, self->num, (uint8_t *) chat_id, &chatid_err)) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Failed to retrieve the Chat ID (error %d).", chatid_err);
return;
}

char nick[TOX_MAX_NAME_LENGTH + 1];
const size_t nick_length = get_group_self_nick_truncate(tox, nick, self->num);

const char *password = NULL;
uint16_t password_length = 0;

if (argc == 1) {
password_length = (uint16_t) strlen(argv[1]);

if (password_length <= TOX_GROUP_MAX_PASSWORD_SIZE) {
password = argv[1];
} else {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Password length cannot exceed %d.",
TOX_GROUP_MAX_PASSWORD_SIZE);
password_length = 0;
}
}

Tox_Err_Group_Join join_err;
tox_group_join(tox, (uint8_t *) chat_id, (const uint8_t *) nick, nick_length, (const uint8_t *) password,
(uint16_t) password_length, &join_err);

if (!tox_group_reconnect(toxic->tox, self->num, &err)) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to rejoin group (error %d).", err);
if (join_err != TOX_ERR_GROUP_JOIN_OK) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to rejoin group (error %d).", join_err);
return;
}

Expand Down
8 changes: 1 addition & 7 deletions src/groupchats.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,7 @@ GroupChat *get_groupchat(uint32_t groupnumber)
return NULL;
}

/*
* Return the groupnumber associated with `public_key`.
* Return -1 if public_key does not designate a valid group.
*
* `public_key` must be a string of at least TOX_PUBLIC_KEY_SIZE * 2 chars in length.
*/
static int get_groupnumber_by_public_key_string(const char *public_key)
int get_groupnumber_by_public_key_string(const char *public_key)
{
char pk_bin[TOX_PUBLIC_KEY_SIZE];

Expand Down
8 changes: 8 additions & 0 deletions src/groupchats.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ void groupchat_onGroupModeration(ToxWindow *self, Toxic *toxic, uint32_t groupnu

void groupchat_rejoin(ToxWindow *self, Toxic *toxic);

/*
* Return the groupnumber associated with `public_key`.
* Return -1 if public_key does not designate a valid group.
*
* `public_key` must be a string of at least TOX_PUBLIC_KEY_SIZE * 2 chars in length.
*/
int get_groupnumber_by_public_key_string(const char *public_key);

/* Updates the groupchat topic in the top statusbar. */
void groupchat_update_statusbar_topic(ToxWindow *self, const Tox *tox);

Expand Down
4 changes: 2 additions & 2 deletions src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ static void help_draw_groupchats(ToxWindow *self)
wprintw(win, " /locktopic : Set the topic lock: on | off\n");
wprintw(win, " /mod <name>|<key> : Promote a peer to moderator\n");
wprintw(win, " /nick <name> : Set your name (for this group only)\n");
wprintw(win, " /passwd <s> : Set a password to join the group\n");
wprintw(win, " /passwd <password> : Set a password to join the group\n");
wprintw(win, " /peerlimit <n> : Set the maximum number of peers that can join\n");
wprintw(win, " /privacy <state> : Set the privacy state: private | public\n");
wprintw(win, " /rejoin : Reconnect to the group\n");
wprintw(win, " /rejoin <password> : Reconnect to the group (password is optional)\n");
wprintw(win, " /silence <name>|<key> : Silence a peer for the entire group\n");
wprintw(win, " /unsilence <name>|<key> : Unsilence a silenced peer\n");
wprintw(win, " /status <type> : Set your status (client-wide)\n");
Expand Down

0 comments on commit 204a0e9

Please sign in to comment.