Skip to content

Commit 76d6b1e

Browse files
committed
feat: Add the ability to pass a password to the groupchat /rejoin command
This replaces the deprecated tox_group_reconnect command with tox_group_join which now supports the ability to reconnect to groups with a password.
1 parent 306c3fd commit 76d6b1e

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

src/execute.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static const char special_commands[][MAX_CMDNAME_SIZE] = {
145145
"/nick",
146146
"/note",
147147
"/passwd",
148+
"/rejoin",
148149
"/silence",
149150
"/topic",
150151
"/unignore",

src/global_commands.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char
566566
self_nick[nick_length] = '\0';
567567

568568
Tox_Err_Group_New err;
569-
uint32_t groupnumber = tox_group_new(tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *) name, len,
570-
(const uint8_t *) self_nick, nick_length, &err);
569+
const uint32_t groupnumber = tox_group_new(tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *) name, len,
570+
(const uint8_t *) self_nick, nick_length, &err);
571571

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

628+
if (get_groupnumber_by_public_key_string(chat_id) != -1) {
629+
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You are already in this group.");
630+
return;
631+
}
632+
628633
char id_bin[TOX_GROUP_CHAT_ID_SIZE] = {0};
629634

630635
size_t i;
@@ -860,6 +865,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar
860865

861866
free(dir);
862867
}
868+
863869
#endif /* QRCODE */
864870

865871
void cmd_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE])

src/groupchat_commands.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,12 +867,42 @@ void cmd_rejoin(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*
867867
return;
868868
}
869869

870+
Tox *tox = toxic->tox;
870871
const Client_Config *c_config = toxic->c_config;
871872

872-
Tox_Err_Group_Reconnect err;
873+
char chat_id[TOX_GROUP_CHAT_ID_SIZE];
874+
Tox_Err_Group_State_Query chatid_err;
875+
876+
if (!tox_group_get_chat_id(tox, self->num, (uint8_t *) chat_id, &chatid_err)) {
877+
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
878+
"Failed to retrieve the Chat ID (error %d).", chatid_err);
879+
return;
880+
}
881+
882+
char nick[TOX_MAX_NAME_LENGTH + 1];
883+
const size_t nick_length = get_group_self_nick_truncate(tox, nick, self->num);
884+
885+
const char *password = NULL;
886+
uint16_t password_length = 0;
887+
888+
if (argc == 1) {
889+
password_length = (uint16_t) strlen(argv[1]);
890+
891+
if (password_length <= TOX_GROUP_MAX_PASSWORD_SIZE) {
892+
password = argv[1];
893+
} else {
894+
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Password length cannot exceed %d.",
895+
TOX_GROUP_MAX_PASSWORD_SIZE);
896+
password_length = 0;
897+
}
898+
}
899+
900+
Tox_Err_Group_Join join_err;
901+
tox_group_join(tox, (uint8_t *) chat_id, (const uint8_t *) nick, nick_length, (const uint8_t *) password,
902+
(uint16_t) password_length, &join_err);
873903

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

src/groupchats.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,7 @@ GroupChat *get_groupchat(uint32_t groupnumber)
140140
return NULL;
141141
}
142142

143-
/*
144-
* Return the groupnumber associated with `public_key`.
145-
* Return -1 if public_key does not designate a valid group.
146-
*
147-
* `public_key` must be a string of at least TOX_PUBLIC_KEY_SIZE * 2 chars in length.
148-
*/
149-
static int get_groupnumber_by_public_key_string(const char *public_key)
143+
int get_groupnumber_by_public_key_string(const char *public_key)
150144
{
151145
char pk_bin[TOX_PUBLIC_KEY_SIZE];
152146

src/groupchats.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ void groupchat_onGroupModeration(ToxWindow *self, Toxic *toxic, uint32_t groupnu
7171

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

74+
/*
75+
* Return the groupnumber associated with `public_key`.
76+
* Return -1 if public_key does not designate a valid group.
77+
*
78+
* `public_key` must be a string of at least TOX_PUBLIC_KEY_SIZE * 2 chars in length.
79+
*/
80+
int get_groupnumber_by_public_key_string(const char *public_key);
81+
7482
/* Updates the groupchat topic in the top statusbar. */
7583
void groupchat_update_statusbar_topic(ToxWindow *self, const Tox *tox);
7684

src/help.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ static void help_draw_groupchats(ToxWindow *self)
296296
wprintw(win, " /locktopic : Set the topic lock: on | off\n");
297297
wprintw(win, " /mod <name>|<key> : Promote a peer to moderator\n");
298298
wprintw(win, " /nick <name> : Set your name (for this group only)\n");
299-
wprintw(win, " /passwd <s> : Set a password to join the group\n");
299+
wprintw(win, " /passwd <password> : Set a password to join the group\n");
300300
wprintw(win, " /peerlimit <n> : Set the maximum number of peers that can join\n");
301301
wprintw(win, " /privacy <state> : Set the privacy state: private | public\n");
302-
wprintw(win, " /rejoin : Reconnect to the group\n");
302+
wprintw(win, " /rejoin <password> : Reconnect to the group (password is optional)\n");
303303
wprintw(win, " /silence <name>|<key> : Silence a peer for the entire group\n");
304304
wprintw(win, " /unsilence <name>|<key> : Unsilence a silenced peer\n");
305305
wprintw(win, " /status <type> : Set your status (client-wide)\n");
@@ -389,6 +389,7 @@ static void help_draw_plugin(ToxWindow *self)
389389
box(win, ACS_VLINE, ACS_HLINE);
390390
wnoutrefresh(win);
391391
}
392+
392393
#endif /* PYTHON */
393394

394395
static void help_draw_contacts(ToxWindow *self)

0 commit comments

Comments
 (0)