mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 05:53:02 +01:00
Refactor peer-specific commands
This takes into account the possibility of multiple peers in the group using the same nick. The /whois command now lists all peers associated with the input name, and all commands that target a peer with a non-unique nick will require the public key to be used.
This commit is contained in:
parent
493dfeff22
commit
7f29705966
@ -88,12 +88,9 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
|
|||||||
const char *nick = argv[1];
|
const char *nick = argv[1];
|
||||||
uint32_t peer_id;
|
uint32_t peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TOX_ERR_GROUP_TOGGLE_IGNORE err;
|
TOX_ERR_GROUP_TOGGLE_IGNORE err;
|
||||||
tox_group_toggle_ignore(m, self->num, peer_id, true, &err);
|
tox_group_toggle_ignore(m, self->num, peer_id, true, &err);
|
||||||
@ -128,12 +125,9 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
|
|||||||
|
|
||||||
uint32_t target_peer_id;
|
uint32_t target_peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TOX_ERR_GROUP_MOD_KICK_PEER err;
|
TOX_ERR_GROUP_MOD_KICK_PEER err;
|
||||||
tox_group_mod_kick_peer(m, self->num, target_peer_id, &err);
|
tox_group_mod_kick_peer(m, self->num, target_peer_id, &err);
|
||||||
@ -204,12 +198,9 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
|
|||||||
const char *nick = argv[1];
|
const char *nick = argv[1];
|
||||||
uint32_t target_peer_id;
|
uint32_t target_peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TOX_ERR_GROUP_SELF_QUERY s_err;
|
TOX_ERR_GROUP_SELF_QUERY s_err;
|
||||||
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
|
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
|
||||||
@ -260,12 +251,9 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
|
|||||||
const char *nick = argv[1];
|
const char *nick = argv[1];
|
||||||
uint32_t target_peer_id;
|
uint32_t target_peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TOX_ERR_GROUP_SELF_QUERY s_err;
|
TOX_ERR_GROUP_SELF_QUERY s_err;
|
||||||
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
|
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
|
||||||
@ -504,12 +492,9 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
|
|||||||
const char *nick = argv[1];
|
const char *nick = argv[1];
|
||||||
uint32_t target_peer_id;
|
uint32_t target_peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TOX_ERR_GROUP_SELF_QUERY s_err;
|
TOX_ERR_GROUP_SELF_QUERY s_err;
|
||||||
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
|
uint32_t self_peer_id = tox_group_self_get_peer_id(m, self->num, &s_err);
|
||||||
@ -560,12 +545,9 @@ void cmd_unsilence(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg
|
|||||||
const char *nick = argv[1];
|
const char *nick = argv[1];
|
||||||
uint32_t target_peer_id;
|
uint32_t target_peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &target_peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (tox_group_peer_get_role(m, self->num, target_peer_id, NULL) != TOX_GROUP_ROLE_OBSERVER) {
|
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);
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not silenced.", nick);
|
||||||
@ -700,12 +682,9 @@ void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
|||||||
const char *nick = argv[1];
|
const char *nick = argv[1];
|
||||||
uint32_t peer_id;
|
uint32_t peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, nick, &peer_id) == -1) {
|
if (group_get_peer_id_of_identifier(self, nick, &peer_id) != 0) {
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TOX_ERR_GROUP_TOGGLE_IGNORE err;
|
TOX_ERR_GROUP_TOGGLE_IGNORE err;
|
||||||
tox_group_toggle_ignore(m, self->num, peer_id, false, &err);
|
tox_group_toggle_ignore(m, self->num, peer_id, false, &err);
|
||||||
@ -744,20 +723,31 @@ void cmd_whois(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *identifier = argv[1];
|
const char *identifier = argv[1];
|
||||||
|
bool is_public_key = false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < chat->max_idx && !is_public_key; ++i) {
|
||||||
uint32_t peer_id;
|
uint32_t peer_id;
|
||||||
|
|
||||||
if (group_get_nick_peer_id(self->num, identifier, &peer_id) == -1) {
|
if (group_get_public_key_peer_id(self->num, identifier, &peer_id) == 0) {
|
||||||
if (group_get_public_key_peer_id(self->num, identifier, &peer_id) == -1) {
|
is_public_key = true;
|
||||||
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
|
} else {
|
||||||
return;
|
GroupPeer *peer = &chat->peer_list[i];
|
||||||
|
|
||||||
|
if (!peer->active) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strcmp(identifier, peer->name) != 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
peer_id = peer->peer_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
int peer_index = get_peer_index(self->num, peer_id);
|
int peer_index = get_peer_index(self->num, peer_id);
|
||||||
|
|
||||||
if (peer_index < 0) {
|
if (peer_index < 0) {
|
||||||
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch peer index.");
|
continue;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GroupPeer *peer = &chat->peer_list[peer_index];
|
GroupPeer *peer = &chat->peer_list[peer_index];
|
||||||
@ -808,4 +798,5 @@ void cmd_whois(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
|
|||||||
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Connection: %s", connection_type_str);
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Connection: %s", connection_type_str);
|
||||||
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Last active: %s", last_seen_str);
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Last active: %s", last_seen_str);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -465,12 +465,13 @@ static void sort_peerlist(uint32_t groupnumber)
|
|||||||
qsort(chat->peer_list, chat->max_idx, sizeof(struct GroupPeer), peer_sort_cmp);
|
qsort(chat->peer_list, chat->max_idx, sizeof(struct GroupPeer), peer_sort_cmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Gets the peer_id associated with nick.
|
/* Puts the peer_id associated with nick in `peer_id`.
|
||||||
*
|
*
|
||||||
* Returns 0 on success.
|
* Returns 0 on success.
|
||||||
* Returns -1 on failure or if nick is not assigned to anyone in the group.
|
* Returns -1 if peer is not found.
|
||||||
|
* Returns -2 if there are more than one peers with nick.
|
||||||
*/
|
*/
|
||||||
int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id)
|
static int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id)
|
||||||
{
|
{
|
||||||
GroupChat *chat = get_groupchat(groupnumber);
|
GroupChat *chat = get_groupchat(groupnumber);
|
||||||
|
|
||||||
@ -478,6 +479,8 @@ int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *pee
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t count = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < chat->max_idx; ++i) {
|
for (size_t i = 0; i < chat->max_idx; ++i) {
|
||||||
GroupPeer *peer = &chat->peer_list[i];
|
GroupPeer *peer = &chat->peer_list[i];
|
||||||
|
|
||||||
@ -486,12 +489,15 @@ int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *pee
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(nick, peer->name) == 0) {
|
if (strcmp(nick, peer->name) == 0) {
|
||||||
|
if (++count > 1) {
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
*peer_id = peer->peer_id;
|
*peer_id = peer->peer_id;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Gets the peer_id associated with `public_key`.
|
/* Gets the peer_id associated with `public_key`.
|
||||||
@ -529,6 +535,50 @@ int group_get_public_key_peer_id(uint32_t groupnumber, const char *public_key, u
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Puts the peer_id associated with `identifier` in `peer_id`. The string may be
|
||||||
|
* either a nick or a public key.
|
||||||
|
*
|
||||||
|
* On failure, `peer_id` is set to (uint32_t)-1.
|
||||||
|
*
|
||||||
|
* This function is intended to be a helper for groupchat_commands.c and will print
|
||||||
|
* error messages to `self`.
|
||||||
|
* Return 0 on success.
|
||||||
|
* Return -1 if the identifier does not correspond with a peer in the group, or if
|
||||||
|
* the identifier is a nick which is being used by multiple peers.
|
||||||
|
*/
|
||||||
|
int group_get_peer_id_of_identifier(ToxWindow *self, const char *identifier, uint32_t *peer_id)
|
||||||
|
{
|
||||||
|
*peer_id = (uint32_t) -1;
|
||||||
|
|
||||||
|
if (group_get_public_key_peer_id(self->num, identifier, peer_id) == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = group_get_nick_peer_id(self->num, identifier, peer_id);
|
||||||
|
|
||||||
|
switch (ret) {
|
||||||
|
case 0: {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
case -1: {
|
||||||
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
case -2: {
|
||||||
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0,
|
||||||
|
"More than one peer is using this name. Specify the target's public key.");
|
||||||
|
*peer_id = (uint32_t) -1;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Unspecified error.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void groupchat_update_last_seen(uint32_t groupnumber, uint32_t peer_id)
|
static void groupchat_update_last_seen(uint32_t groupnumber, uint32_t peer_id)
|
||||||
{
|
{
|
||||||
GroupChat *chat = get_groupchat(groupnumber);
|
GroupChat *chat = get_groupchat(groupnumber);
|
||||||
@ -1076,10 +1126,6 @@ static void groupchat_onGroupRejected(ToxWindow *self, Tox *m, uint32_t groupnum
|
|||||||
const char *msg = NULL;
|
const char *msg = NULL;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TOX_GROUP_JOIN_FAIL_NAME_TAKEN:
|
|
||||||
msg = "Nick already in use. Change your nick and use the '/rejoin' command.";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case TOX_GROUP_JOIN_FAIL_PEER_LIMIT:
|
case TOX_GROUP_JOIN_FAIL_PEER_LIMIT:
|
||||||
msg = "Group is full. Try again with the '/rejoin' command.";
|
msg = "Group is full. Try again with the '/rejoin' command.";
|
||||||
break;
|
break;
|
||||||
|
@ -78,12 +78,18 @@ void groupchat_onGroupModeration(ToxWindow *self, Tox *m, uint32_t groupnumber,
|
|||||||
|
|
||||||
void groupchat_rejoin(ToxWindow *self, Tox *m);
|
void groupchat_rejoin(ToxWindow *self, Tox *m);
|
||||||
|
|
||||||
/* Gets the peer_id associated with nick.
|
/* Puts the peer_id associated with `identifier` in `peer_id`. The string may be
|
||||||
|
* either a nick or a public key.
|
||||||
*
|
*
|
||||||
* Returns 0 on success.
|
* On failure, `peer_id` is set to (uint32_t)-1.
|
||||||
* Returns -1 on failure or if nick is not assigned to anyone in the group.
|
*
|
||||||
|
* This function is intended to be a helper for groupchat_commands.c and will print
|
||||||
|
* error messages to `self`.
|
||||||
|
* Return 0 on success.
|
||||||
|
* Return -1 if the identifier does not correspond with a peer in the group.
|
||||||
|
* Return -2 if the identifier is a nick and the nick is in use by multiple peers.
|
||||||
*/
|
*/
|
||||||
int group_get_nick_peer_id(uint32_t groupnumber, const char *nick, uint32_t *peer_id);
|
int group_get_peer_id_of_identifier(ToxWindow *self, const char *identifier, uint32_t *peer_id);
|
||||||
|
|
||||||
/* Gets the peer_id associated with `public_key`.
|
/* Gets the peer_id associated with `public_key`.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user