Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make invite and cinvite commands global #387

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/conference.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static const char *const conference_cmd_list[] = {
#endif
"/avatar",
"/chatid",
"/cinvite",
"/clear",
"/close",
"/color",
Expand All @@ -79,6 +80,7 @@ static const char *const conference_cmd_list[] = {
"/game",
#endif
"/help",
"/invite",
"/join",
"/log",
#ifdef AUDIO
Expand Down
2 changes: 2 additions & 0 deletions src/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static struct cmd_func global_commands[] = {
{ "/game", cmd_game },
#endif
{ "/help", cmd_prompt_help },
{ "/invite", cmd_group_invite_g},
{ "/cinvite", cmd_conference_invite_g },
{ "/join", cmd_join },
{ "/log", cmd_log },
{ "/myid", cmd_myid },
Expand Down
19 changes: 19 additions & 0 deletions src/friendlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,25 @@ Tox_Connection get_friend_connection_status(uint32_t friendnumber)
return Friends.list[friendnumber].connection_status;
}

int64_t get_friend_number(const char *name)
{
int64_t num = -1;
size_t count = 0;

for (size_t i = 0; i < Friends.max_idx; ++i) {
if (memcmp(name, Friends.list[i].name, Friends.list[i].namelength) == 0) {
num = Friends.list[i].num;
++count;
}
}

if (count > 1) {
return -2;
}

return num;
}

/*
* Returns true if friend associated with `public_key` is in the block list.
*
Expand Down
7 changes: 7 additions & 0 deletions src/friendlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ bool friend_get_auto_accept_files(uint32_t friendnumber);
*/
uint16_t get_friend_name(char *buf, size_t buf_size, uint32_t friendnumber);

/*
* Returns the friend number associated with `nick`.
* Returns -1 if `nick` does not designate a friend in the friend list.
* Returns -2 if `nick` matches more than one friend in the friend list.
*/
int64_t get_friend_number(const char *nick);

/*
* Puts a friend's public key in `pk`, which must have room for at least
* TOX_PUBLIC_KEY_SIZE bytes.
Expand Down
97 changes: 97 additions & 0 deletions src/global_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,56 @@ void cmd_color(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a
self->colour = colour_val;
}

void cmd_conference_invite_g(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE])
{
UNUSED_VAR(window);

if (toxic == NULL || self == NULL) {
return;
}

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

if (argc < 2) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Conference number and name required.");
return;
}

const long int conferencenum = strtol(argv[1], NULL, 10);

if ((conferencenum == 0 && strcmp(argv[1], "0")) || conferencenum < 0 || conferencenum == LONG_MAX) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid conference number.");
return;
}

const char *nick = argv[2];
int64_t friend_number = get_friend_number(nick);

if (friend_number == -1) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Friend '%s' not found (this command is case-sensitive)", nick);
return;
}

if (friend_number == -2) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"There are multiple friends in your friend list with this name. To invite this friend, navigate to their chat window and try again");
return;
}

Tox_Err_Conference_Invite err;

if (!tox_conference_invite(tox, (uint32_t)friend_number, conferencenum, &err)) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Failed to invite contact to conference (error %d)", err);
return;
}

line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Conference %ld.",
conferencenum);
}

void cmd_connect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE])
{
UNUSED_VAR(window);
Expand Down Expand Up @@ -604,6 +654,53 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char
}
}

void cmd_group_invite_g(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE])
{
if (toxic == NULL || self == NULL) {
return;
}

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

if (argc < 2) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group number and name required.");
return;
}

const int groupnumber = atoi(argv[1]);

if (groupnumber == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid group number.");
return;
}

const char *nick = argv[2];
int64_t friend_number = get_friend_number(nick);

if (friend_number == -1) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"Friend '%s' not found (this command is case-sensitive)", nick);
return;
}

if (friend_number == -2) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0,
"There are multiple friends in your friend list with this name. To invite this friend, navigate to their chat window and try again");
return;
}

Tox_Err_Group_Invite_Friend err;

if (!tox_group_invite_friend(tox, groupnumber, (uint32_t)friend_number, &err)) {
line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group (error %d).",
err);
return;
}

line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %d.", groupnumber);
}

void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE])
{
if (toxic == NULL || self == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions src/global_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ void cmd_avatar(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX
void cmd_clear(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_color(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_conference(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_conference_invite_g(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_connect(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_decline(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_groupchat(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_group_invite_g(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_join(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_log(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_myid(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]);
Expand Down
2 changes: 2 additions & 0 deletions src/groupchats.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static const char *const group_cmd_list[] = {
"/add",
"/avatar",
"/chatid",
"/cinvite",
"/clear",
"/close",
"/color",
Expand All @@ -73,6 +74,7 @@ static const char *const group_cmd_list[] = {
"/group",
"/help",
"/ignore",
"/invite",
"/join",
"/kick",
"/list",
Expand Down
2 changes: 2 additions & 0 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const char *const glob_cmd_list[] = {
"/accept",
"/add",
"/avatar",
"/cinvite",
"/clear",
"/color",
"/connect",
Expand All @@ -50,6 +51,7 @@ static const char *const glob_cmd_list[] = {
"/game",
#endif
"/help",
"/invite",
"/join",
"/log",
"/myid",
Expand Down
Loading