1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:03:27 +01:00

Add the ability to make peer-specific commands using public keys

This allows us to use commands on peers who may be using nicks with strange
or unsupported unicode characters
This commit is contained in:
jfreegman 2021-11-12 12:28:54 -05:00
parent 408c8f7c10
commit a5b44dffd2
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
4 changed files with 99 additions and 28 deletions

View File

@ -81,7 +81,7 @@ void cmd_disconnect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar
void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified.");
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified.");
return;
}
@ -89,9 +89,11 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
uint32_t peer_id;
if (group_get_nick_peer_id(self->num, nick, &peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
TOX_ERR_GROUP_TOGGLE_IGNORE err;
tox_group_toggle_ignore(m, self->num, peer_id, true, &err);
@ -118,7 +120,7 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified.");
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified.");
return;
}
@ -127,9 +129,11 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
uint32_t target_peer_id;
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
TOX_ERR_GROUP_MOD_KICK_PEER err;
tox_group_mod_kick_peer(m, self->num, target_peer_id, &err);
@ -193,7 +197,7 @@ void cmd_list(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
void cmd_mod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified.");
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified.");
return;
}
@ -201,9 +205,11 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
uint32_t target_peer_id;
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
TOX_ERR_GROUP_SELF_QUERY s_err;
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
@ -247,7 +253,7 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified.");
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified.");
return;
}
@ -255,9 +261,11 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
uint32_t target_peer_id;
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
TOX_ERR_GROUP_SELF_QUERY s_err;
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
@ -439,7 +447,7 @@ void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a
void cmd_silence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified.");
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified.");
return;
}
@ -447,9 +455,11 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
uint32_t target_peer_id;
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
TOX_ERR_GROUP_SELF_QUERY s_err;
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
@ -493,7 +503,7 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
void cmd_unsilence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name must be specified.");
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified.");
return;
}
@ -501,9 +511,11 @@ void cmd_unsilence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
uint32_t target_peer_id;
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &target_peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
if (tox_group_peer_get_role(m, self->num, target_peer_id, NULL) != TOX_GROUP_ROLE_OBSERVER) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not silenced.", nick);
@ -639,9 +651,11 @@ void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
uint32_t peer_id;
if (group_get_nick_peer_id(self->num, nick, &peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
TOX_ERR_GROUP_TOGGLE_IGNORE err;
tox_group_toggle_ignore(m, self->num, peer_id, false, &err);
@ -683,9 +697,11 @@ void cmd_whois(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
uint32_t peer_id;
if (group_get_nick_peer_id(self->num, nick, &peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name '%s'.", nick);
if (group_get_public_key_peer_id(self->num, nick, &peer_id) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
return;
}
}
int peer_index = get_peer_index(self->num, peer_id);

View File

@ -45,3 +45,4 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
void cmd_whois(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
#endif /* GROUPCHAT_COMMANDS_H */

View File

@ -465,6 +465,8 @@ static void sort_peerlist(uint32_t groupnumber)
}
/* Gets the peer_id associated with nick.
*
* Returns 0 on success.
* Returns -1 on failure or if nick is not assigned to anyone in the group.
*/
int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id)
@ -476,12 +478,51 @@ int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *pee
}
for (size_t i = 0; i < chat->max_idx; ++i) {
if (chat->peer_list[i].active) {
if (strcmp(nick, chat->peer_list[i].name) == 0) {
*peer_id = chat->peer_list[i].peer_id;
GroupPeer *peer = &chat->peer_list[i];
if (!peer->active) {
continue;
}
if (strcmp(nick, peer->name) == 0) {
*peer_id = peer->peer_id;
return 0;
}
}
return -1;
}
/* Gets the peer_id associated with `public_key`.
*
* Returns 0 on success.
* Returns -1 on failure or if `public_key` is invalid.
*/
int group_get_public_key_peer_id(uint32_t groupnumber, const char *public_key, uint32_t *peer_id)
{
GroupChat *chat = get_groupchat(groupnumber);
if (!chat) {
return -1;
}
char key_bin[TOX_GROUP_PEER_PUBLIC_KEY_SIZE];
if (hex_string_to_bin(public_key, strlen(public_key), key_bin, sizeof(key_bin)) == -1) {
return -1;
}
for (size_t i = 0; i < chat->max_idx; ++i) {
GroupPeer *peer = &chat->peer_list[i];
if (!peer->active) {
continue;
}
if (memcmp(key_bin, peer->public_key, TOX_GROUP_PEER_PUBLIC_KEY_SIZE) == 0) {
*peer_id = peer->peer_id;
return 0;
}
}
return -1;

View File

@ -69,7 +69,6 @@ void exit_groupchat(ToxWindow *self, Tox *m, uint32_t groupnumber, const char *p
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);
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,
Tox_Group_Exit_Type exit_type,
@ -79,6 +78,20 @@ void groupchat_onGroupModeration(ToxWindow *self, Tox *m, uint32_t groupnumber,
void groupchat_rejoin(ToxWindow *self, Tox *m);
/* Gets the peer_id associated with nick.
*
* Returns 0 on success.
* Returns -1 on failure or if nick is not assigned to anyone in the group.
*/
int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id);
/* Gets the peer_id associated with `public_key`.
*
* Returns 0 on success.
* Returns -1 on failure or if `public_key` is invalid.
*/
int group_get_public_key_peer_id(uint32_t groupnumber, const char *public_key, uint32_t *peer_id);
/* destroys and re-creates groupchat window */
void redraw_groupchat_win(ToxWindow *self);