diff --git a/src/commands.c b/src/commands.c index 8f2b2c7..ecca0ba 100644 --- a/src/commands.c +++ b/src/commands.c @@ -31,7 +31,12 @@ void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv int num = atoi(argv[1]); - if (num < 0 || num >= num_frnd_requests) { + if (num < 0 || num > num_frnd_requests) { + wprintw(window, "No pending friend request with that number.\n"); + return; + } + + if (!strlen(pending_frnd_requests[num])) { wprintw(window, "No pending friend request with that number.\n"); return; } @@ -44,6 +49,17 @@ void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv wprintw(window, "Friend request accepted.\n"); on_friendadded(m, friendnum); } + + memset(&pending_frnd_requests[num], 0, sizeof(TOX_CLIENT_ID_SIZE)); + + int i; + + for (i = num_frnd_requests; i > 0; --i) { + if (!strlen(pending_frnd_requests[i-1])) + break; + } + + num_frnd_requests = i; } void cmd_add(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv) diff --git a/src/friendlist.c b/src/friendlist.c index 330ca01..9063549 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -34,7 +34,7 @@ static int num_friends = 0; static int num_selected = 0; -void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) +static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) { if (num < 0 || num >= num_friends) return; @@ -43,7 +43,7 @@ void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16 friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num)); } -void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) +static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) { if (num < 0 || num >= num_friends) return; @@ -51,7 +51,7 @@ void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t sta friends[num].online = status == 1 ? true : false; } -void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) +static void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends) return; @@ -60,7 +60,7 @@ void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t le friends[num].namelength = len; } -void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) +static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) { if (num < 0 || num >= num_friends) return; @@ -68,7 +68,7 @@ void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS friends[num].status = status; } -void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) +static void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; @@ -108,8 +108,8 @@ int friendlist_onFriendAdded(Tox *m, int num) return -1; } -void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint64_t filesize, - uint8_t *filename, uint16_t filename_len) +static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum, + uint64_t filesize, uint8_t *filename, uint16_t filename_len) { if (num < 0 || num >= num_friends) return; @@ -153,7 +153,7 @@ static void select_friend(Tox *m, wint_t key) static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) { tox_delfriend(m, f_num); - memset(&(friends[f_num]), 0, sizeof(friend_t)); + memset(&friends[f_num], 0, sizeof(friend_t)); int i; diff --git a/src/prompt.c b/src/prompt.c index d5fb7a5..33d1c54 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -14,7 +14,7 @@ #include "commands.h" #include "misc_tools.h" -uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; +uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; uint8_t num_frnd_requests = 0; /* One group chat request slot for each friend; slot is @@ -55,12 +55,22 @@ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) } /* Adds friend request to pending friend requests. - Returns friend number on success, -1 if queue is full or other error. */ -int add_friend_req(uint8_t *public_key) + Returns request number on success, -1 if queue is full or other error. */ +static int add_friend_request(uint8_t *public_key) { if (num_frnd_requests < MAX_FRIENDS_NUM) { - memcpy(pending_frnd_requests[num_frnd_requests++], public_key, TOX_CLIENT_ID_SIZE); - return num_frnd_requests - 1; + int i; + + for (i = 0; i <= num_frnd_requests; ++i) { + if (!strlen(pending_frnd_requests[i])) { + memcpy(pending_frnd_requests[i], public_key, TOX_CLIENT_ID_SIZE); + + if (i == num_frnd_requests) + ++num_frnd_requests; + + return i; + } + } } return -1; @@ -68,7 +78,7 @@ int add_friend_req(uint8_t *public_key) /* Adds group chat invite to pending group chat requests. Returns friend number on success, -1 if f_num is out of range. */ -int add_group_req(uint8_t *group_pub_key, int f_num) +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); @@ -261,7 +271,7 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, } wprintw(self->window, "\n\nWith the message: %s\n\n", data); - int n = add_friend_req(key); + int n = add_friend_request(key); if (n == -1) { wprintw(self->window, "Friend request queue is full. Discarding request.\n"); @@ -292,7 +302,7 @@ static void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint return; } - int n = add_group_req(group_pub_key, friendnumber); + int n = add_group_request(group_pub_key, friendnumber); if (n == -1) { wprintw(self->window, "\nSomething bad happened.\n");