From b6bf0eb0a0cb542aa65e8899b44d26e51369cd3f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 16 Sep 2013 00:28:28 -0400 Subject: [PATCH] fixes --- src/groupchat.c | 13 ++++----- src/groupchat.h | 2 +- src/prompt.c | 69 ++++++++++++++++++++++++++++++++------------- src/toxic_windows.h | 2 +- 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/groupchat.c b/src/groupchat.c index d1db257..40c77b3 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -26,15 +26,14 @@ extern int store_data(Tox *m, char *path); int get_num_groupchats(void) { - int count = 0; int i; - for (i = 0; i < group_chat_index; ++i) { - if (groupchats[i].active) - ++count; + for (i = 0; i <= group_chat_index; ++i) { + if (!groupchats[i].active) + return i; } - return count; + return -1; } int init_groupchat_win(ToxWindow *prompt, Tox *m) @@ -45,7 +44,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m) if (!groupchats[i].active) { groupchats[i].active = true; groupchats[i].chatwin = add_window(m, new_groupchat(m, prompt, i)); - set_active_window(groupchats[i].chatwin); + //set_active_window(groupchats[i].chatwin); if (i == group_chat_index) ++group_chat_index; @@ -140,7 +139,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) if (line[0] == '/') { if (close_win = !strncmp(line, "/close", strlen("/close"))) { set_active_window(0); - int group_num = groupchats[self->num].chatwin; + int group_num = self->num; delwin(ctx->linewin); del_window(self); close_groupchatwin(m, group_num); diff --git a/src/groupchat.h b/src/groupchat.h index 851e4e4..fe2f42c 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -3,4 +3,4 @@ */ int init_groupchat_win(ToxWindow *prompt, Tox *m); -int get_num_groupchats(void); \ No newline at end of file +int get_num_groupchats(void); diff --git a/src/prompt.c b/src/prompt.c index 15d286f..13c0b82 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -18,7 +18,7 @@ extern char *DATA_FILE; uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; uint8_t num_frnd_requests = 0; -uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; +uint8_t pending_grp_requests[MAX_GROUPCHAT_NUM][TOX_CLIENT_ID_SIZE]; uint8_t num_grp_requests = 0; static char prompt_buf[MAX_STR_SIZE] = {'\0'}; @@ -92,18 +92,28 @@ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) statusbar->is_online = is_connected; } -/* Adds friend request to pending friend requests. */ +/* 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) { - memcpy(pending_frnd_requests[num_frnd_requests++], public_key, TOX_CLIENT_ID_SIZE); - return num_frnd_requests - 1; + 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; + } + + return -1; } -/* Adds group chat invite to pending group chat requests */ +/* Adds group chat invite to pending group chat requests. + Returns group number on success, -1 if queue is full or other error. */ int add_group_req(uint8_t *group_pub_key) { - memcpy(pending_grp_requests[num_grp_requests++], group_pub_key, TOX_CLIENT_ID_SIZE); - return num_grp_requests - 1; + if (num_grp_requests < MAX_GROUPCHAT_NUM) { + memcpy(pending_grp_requests[num_grp_requests++], group_pub_key, TOX_CLIENT_ID_SIZE); + return num_grp_requests - 1; + } + + return -1; } // XXX: FIX @@ -143,13 +153,13 @@ void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv) return; } - num = tox_addfriend_norequest(m, pending_frnd_requests[num]); + int friendnum = tox_addfriend_norequest(m, pending_frnd_requests[num]); - if (num == -1) + if (friendnum == -1) wprintw(self->window, "Failed to add friend.\n"); else { - wprintw(self->window, "Friend accepted.\n"); - on_friendadded(m, num); + wprintw(self->window, "Friend request accepted.\n"); + on_friendadded(m, friendnum); } } @@ -296,7 +306,7 @@ void cmd_groupchat(ToxWindow *self, Tox *m, int argc, char **argv) { int ngc = get_num_groupchats(); - if (ngc < 0 || ngc >= MAX_GROUPCHAT_NUM) { + if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) { wprintw(self->window, "\nMaximum number of group chats has been reached.\n"); return; } @@ -357,9 +367,8 @@ void cmd_invite(ToxWindow *self, Tox *m, int argc, char **argv) int friendnum = atoi(argv[1]); int groupnum = atoi(argv[2]); - int n = tox_invite_friend(m, friendnum, groupnum); - if (n == -1) { + if (tox_invite_friend(m, friendnum, groupnum) == -1) { wprintw(self->window, "Failed to invite friend.\n"); return; } @@ -386,10 +395,18 @@ void cmd_join(ToxWindow *self, Tox *m, int argc, char **argv) return; } - num = tox_join_groupchat(m, num, pending_grp_requests[num]); + int groupnum = tox_join_groupchat(m, num, pending_grp_requests[num]); - if (num == -1 || init_groupchat_win(self, m) == -1) + if (groupnum == -1) { wprintw(self->window, "Group chat failed to initialize.\n"); + return; + } + + if (init_groupchat_win(self, m) == -1) { + wprintw(self->window, "Group chat failed to initialize.\n"); + tox_del_groupchat(m, groupnum); + return; + } } void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) @@ -484,7 +501,7 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv) wprintw(self->window, "Messages must be enclosed in quotes.\n"); return; } - } else if (argc < 1 || argc > 2) { + } else if (argc != 1) { wprintw(self->window, "Wrong number of arguments.\n"); return; } @@ -736,6 +753,12 @@ static void prompt_onInit(ToxWindow *self, Tox *m) void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length) { int n = add_friend_req(key); + + if (n == -1) { + wprintw(self->window, "Friend request queue is full. Discarding request.\n"); + return; + } + wprintw(self->window, "\nFriend request from:\n"); int i; @@ -753,16 +776,22 @@ void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16 void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key) { + wprintw(self->window, "\nGroup chat invite from: %d\n", friendnumber); + int ngc = get_num_groupchats(); - if (ngc < 0 || ngc >= MAX_GROUPCHAT_NUM) { - wprintw(self->window, "\nGroup chat invite from: %d\n", friendnumber); + + if (ngc < 0 || ngc > MAX_GROUPCHAT_NUM) { wprintw(self->window, "\nMaximum number of group chats has been reached. Discarding invite.\n"); return; } int n = add_group_req(group_pub_key); - wprintw(self->window, "\nGroup chat invite from: %d\n", friendnumber); + if (n == -1) { + wprintw(self->window, "\nGroup chat queue is full. Discarding invite.\n"); + return; + } + wprintw(self->window, "Type \"join %d\" to join the chat.\n", n); self->blink = true; diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 5768e67..e0c8e4e 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -19,7 +19,7 @@ #define MAX_WINDOWS_NUM 32 #define MAX_FRIENDS_NUM 100 -#define MAX_GROUPCHAT_NUM 32 +#define MAX_GROUPCHAT_NUM 30 #define MAX_STR_SIZE 256 #define KEY_SIZE_BYTES 32 #define TOXIC_MAX_NAME_LENGTH 30 /* Not to be confused with TOX_MAX_NAME_LENGTH */