diff --git a/src/chat.c b/src/chat.c index 0f6d318..5c14774 100644 --- a/src/chat.c +++ b/src/chat.c @@ -17,22 +17,6 @@ extern char *DATA_FILE; extern int store_data(Tox *m, char *path); -/* One group chat request slot for each friend; slot is - overwritten on subsequent requests by the same friend. */ -extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; - -/* Adds group chat invite to pending group chat requests. - Returns friend number on success, -1 if f_num is out of range. */ -static int add_group_request(uint8_t *group_pub_key, int f_num) -{ - if (f_num >= 0 && f_num < MAX_FRIENDS_NUM) { - memcpy(pending_grp_requests[f_num], group_pub_key, TOX_CLIENT_ID_SIZE); - return f_num; - } - - return -1; -} - static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -159,7 +143,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil strcat(filename, d); filename[len + strlen(d)] = '\0'; - if (count >= 999999) { + if (count > 999999) { wprintw(ctx->history, "Error saving file to disk.\n"); return; } @@ -229,7 +213,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key) { - if (friendnumber < 0) + if (self->num != friendnumber) return; ChatContext *ctx = (ChatContext *) self->chatwin; @@ -247,14 +231,9 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_ return; } - int n = add_group_request(group_pub_key, friendnumber); + memcpy(friends[friendnumber].pending_groupchat, group_pub_key, TOX_CLIENT_ID_SIZE); - if (n == -1) { - wprintw(ctx->history, "Something bad happened. Discarding invite.\n"); - return; - } - - wprintw(ctx->history, "Type \"/join %d\" to join the chat.\n", n); + wprintw(ctx->history, "Type \"/join\" to join the chat.\n"); self->blink = true; beep(); } diff --git a/src/chat_commands.c b/src/chat_commands.c index 2e6708c..f360653 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -12,8 +12,6 @@ #include "toxic_windows.h" #include "misc_tools.h" -extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; - void cmd_chat_help(WINDOW *window, ToxWindow *prompt, Tox *m, int num, int argc, char (*argv)[MAX_STR_SIZE]) { wattron(window, COLOR_PAIR(CYAN) | A_BOLD); @@ -23,10 +21,10 @@ void cmd_chat_help(WINDOW *window, ToxWindow *prompt, Tox *m, int num, int argc, wprintw(window, " /status : Set your status with optional note\n"); wprintw(window, " /note : Set a personal note\n"); wprintw(window, " /nick : Set your nickname\n"); - wprintw(window, " /invite : Invite friend to a groupchat\n"); + wprintw(window, " /invite : Invite friend to a group chat\n"); wprintw(window, " /me : Do an action\n"); wprintw(window, " /myid : Print your ID\n"); - wprintw(window, " /join : Join a group chat\n"); + wprintw(window, " /join : Join a pending group chat\n"); wprintw(window, " /clear : Clear the screen\n"); wprintw(window, " /close : Close the current chat window\n"); wprintw(window, " /sendfile : Send a file\n"); @@ -65,26 +63,14 @@ void cmd_groupinvite(WINDOW *window, ToxWindow *prompt, Tox *m, int num, int arg void cmd_join_group(WINDOW *window, ToxWindow *prompt, Tox *m, int num, int argc, char (*argv)[MAX_STR_SIZE]) { - if (argc != 1) { - wprintw(window, "Invalid syntax.\n"); - return; - } + uint8_t *groupkey = friends[num].pending_groupchat; - int g_num = atoi(argv[1]); - - if ((g_num == 0 && strcmp(argv[1], "0")) || g_num >= MAX_FRIENDS_NUM) { - wprintw(window, "No pending group chat invite with that number.\n"); + if (groupkey[0] == '\0') { + wprintw(window, "No pending group chat invite.\n"); return; } - uint8_t *groupkey = pending_grp_requests[g_num]; - - if (!strlen(groupkey)) { - wprintw(window, "No pending group chat invite with that number.\n"); - return; - } - - int groupnum = tox_join_groupchat(m, g_num, groupkey); + int groupnum = tox_join_groupchat(m, num, groupkey); if (groupnum == -1) { wprintw(window, "Group chat instance failed to initialize.\n"); diff --git a/src/friendlist.c b/src/friendlist.c index 89ef39b..d22ab6f 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -111,6 +111,7 @@ int friendlist_onFriendAdded(Tox *m, int num) friends[i].online = false; friends[i].status = TOX_USERSTATUS_NONE; friends[i].namelength = tox_getname(m, num, friends[i].name); + memset(friends[i].pending_groupchat, 0, TOX_CLIENT_ID_SIZE); if (friends[i].namelength == -1 || friends[i].name[0] == '\0') { strcpy((char *) friends[i].name, UNKNOWN_NAME); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 5386b43..24bda56 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -128,6 +128,7 @@ typedef struct { uint16_t namelength; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; uint16_t statusmsg_len; + uint8_t pending_groupchat[TOX_CLIENT_ID_SIZE]; int num; int chatwin; bool active; diff --git a/src/windows.c b/src/windows.c index 9fe326d..8f120fd 100644 --- a/src/windows.c +++ b/src/windows.c @@ -16,10 +16,6 @@ static ToxWindow *active_window; static ToxWindow *prompt; static Tox *m; -/* One group chat request slot for each friend; slot is - overwritten on subsequent requests by the same friend. */ -uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; - /* CALLBACKS START */ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) {