1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 21:33:02 +01:00

refactor friend/blocklist, dynamically allocate memory

This commit is contained in:
Jfreegman 2014-08-07 13:05:42 -04:00
parent 6ab184e7ce
commit 396d08f0d2
11 changed files with 289 additions and 226 deletions

View File

@ -52,7 +52,7 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern FileSender file_senders[MAX_FILES]; extern FileSender file_senders[MAX_FILES];
extern ToxicFriend friends[MAX_FRIENDS_NUM]; extern _Friends Friends;
extern struct _Winthread Winthread; extern struct _Winthread Winthread;
extern struct user_settings *user_settings_; extern struct user_settings *user_settings_;
@ -185,12 +185,12 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, int32_t num, uint8_
if (status == 1) { /* Friend goes online */ if (status == 1) { /* Friend goes online */
statusbar->is_online = true; statusbar->is_online = true;
friends[num].is_typing = user_settings_->show_typing_other == SHOW_TYPING_ON Friends.list[num].is_typing = user_settings_->show_typing_other == SHOW_TYPING_ON
? tox_get_is_typing(m, num) : 0; ? tox_get_is_typing(m, num) : 0;
} else { /* Friend goes offline */ } else { /* Friend goes offline */
statusbar->is_online = false; statusbar->is_online = false;
friends[num].is_typing = 0; Friends.list[num].is_typing = 0;
if (self->chatwin->self_is_typing) if (self->chatwin->self_is_typing)
set_self_typingstatus(self, m, 0); set_self_typingstatus(self, m, 0);
@ -202,7 +202,7 @@ static void chat_onTypingChange(ToxWindow *self, Tox *m, int32_t num, uint8_t is
if (self->num != num) if (self->num != num)
return; return;
friends[num].is_typing = is_typing; Friends.list[num].is_typing = is_typing;
} }
static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, const char *action, uint16_t len) static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, const char *action, uint16_t len)
@ -296,7 +296,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
len += strlen(user_settings_->download_path); len += strlen(user_settings_->download_path);
} }
if (len >= sizeof(friends[num].file_receiver[filenum].filename)) { if (len >= sizeof(Friends.list[num].file_receiver[filenum].filename)) {
errmsg = "File name too long; discarding."; errmsg = "File name too long; discarding.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
return; return;
@ -336,9 +336,9 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %d' to accept the file transfer.", filenum); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %d' to accept the file transfer.", filenum);
friends[num].file_receiver[filenum].pending = true; Friends.list[num].file_receiver[filenum].pending = true;
friends[num].file_receiver[filenum].size = filesize; Friends.list[num].file_receiver[filenum].size = filesize;
strcpy(friends[num].file_receiver[filenum].filename, filename); strcpy(Friends.list[num].file_receiver[filenum].filename, filename);
if (self->active_box != -1) if (self->active_box != -1)
box_notify2(self, transfer_pending, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box, box_notify2(self, transfer_pending, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box,
@ -355,12 +355,12 @@ void chat_close_file_receiver(Tox *m, int filenum, int friendnum, int CTRL)
if (CTRL > 0) if (CTRL > 0)
tox_file_send_control(m, friendnum, 1, filenum, CTRL, 0, 0); tox_file_send_control(m, friendnum, 1, filenum, CTRL, 0, 0);
FILE *file = friends[friendnum].file_receiver[filenum].file; FILE *file = Friends.list[friendnum].file_receiver[filenum].file;
if (file != NULL) if (file != NULL)
fclose(file); fclose(file);
memset(&friends[friendnum].file_receiver[filenum], 0, sizeof(struct FileReceiver)); memset(&Friends.list[friendnum].file_receiver[filenum], 0, sizeof(struct FileReceiver));
} }
static void close_all_file_receivers(Tox *m, int friendnum) static void close_all_file_receivers(Tox *m, int friendnum)
@ -368,7 +368,7 @@ static void close_all_file_receivers(Tox *m, int friendnum)
int i; int i;
for (i = 0; i < MAX_FILES; ++i) { for (i = 0; i < MAX_FILES; ++i) {
if (friends[friendnum].file_receiver[i].active) if (Friends.list[friendnum].file_receiver[i].active)
chat_close_file_receiver(m, i, friendnum, TOX_FILECONTROL_KILL); chat_close_file_receiver(m, i, friendnum, TOX_FILECONTROL_KILL);
} }
} }
@ -384,7 +384,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec
int i = 0; /* file_sender index */ int i = 0; /* file_sender index */
if (receive_send == 0) { if (receive_send == 0) {
filename = friends[num].file_receiver[filenum].filename; filename = Friends.list[num].file_receiver[filenum].filename;
} else { } else {
for (i = 0; i < MAX_FILES; ++i) { for (i = 0; i < MAX_FILES; ++i) {
if (file_senders[i].active && file_senders[i].filenum == filenum) if (file_senders[i].active && file_senders[i].filenum == filenum)
@ -457,7 +457,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
if (self->num != num) if (self->num != num)
return; return;
FILE *fp = friends[num].file_receiver[filenum].file; FILE *fp = Friends.list[num].file_receiver[filenum].file;
if (fp) { if (fp) {
if (fwrite(data, length, 1, fp) != 1) { if (fwrite(data, length, 1, fp) != 1) {
@ -466,17 +466,17 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
} }
} }
friends[num].file_receiver[filenum].bps += length; Friends.list[num].file_receiver[filenum].bps += length;
double remain = (double) tox_file_data_remaining(m, num, filenum, 1); double remain = (double) tox_file_data_remaining(m, num, filenum, 1);
uint64_t curtime = get_unix_time(); uint64_t curtime = get_unix_time();
/* refresh line with percentage complete and transfer speed (must be called once per second) */ /* refresh line with percentage complete and transfer speed (must be called once per second) */
if (!remain || timed_out(friends[num].file_receiver[filenum].last_progress, curtime, 1)) { if (!remain || timed_out(Friends.list[num].file_receiver[filenum].last_progress, curtime, 1)) {
friends[num].file_receiver[filenum].last_progress = curtime; Friends.list[num].file_receiver[filenum].last_progress = curtime;
uint64_t size = friends[num].file_receiver[filenum].size; uint64_t size = Friends.list[num].file_receiver[filenum].size;
double pct_done = remain > 0 ? (1 - (remain / size)) * 100 : 100; double pct_done = remain > 0 ? (1 - (remain / size)) * 100 : 100;
print_progress_bar(self, filenum, num, pct_done); print_progress_bar(self, filenum, num, pct_done);
friends[num].file_receiver[filenum].bps = 0; Friends.list[num].file_receiver[filenum].bps = 0;
} }
} }
@ -491,9 +491,9 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, co
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a group chat.", name); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a group chat.", name);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type \"/join\" to join the chat."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type \"/join\" to join the chat.");
memcpy(friends[friendnumber].groupchat_key, group_pub_key, memcpy(Friends.list[friendnumber].groupchat_key, group_pub_key,
sizeof(friends[friendnumber].groupchat_key)); sizeof(Friends.list[friendnumber].groupchat_key));
friends[friendnumber].groupchat_pending = true; Friends.list[friendnumber].groupchat_pending = true;
sound_notify(self, generic_message, NT_WNDALERT_2, NULL); sound_notify(self, generic_message, NT_WNDALERT_2, NULL);
@ -910,14 +910,14 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wprintw(statusbar->topline, " %s", ONLINE_CHAR); wprintw(statusbar->topline, " %s", ONLINE_CHAR);
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
if (friends[self->num].is_typing) if (Friends.list[self->num].is_typing)
wattron(statusbar->topline, COLOR_PAIR(YELLOW)); wattron(statusbar->topline, COLOR_PAIR(YELLOW));
wattron(statusbar->topline, A_BOLD); wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " %s ", statusbar->nick); wprintw(statusbar->topline, " %s ", statusbar->nick);
wattroff(statusbar->topline, A_BOLD); wattroff(statusbar->topline, A_BOLD);
if (friends[self->num].is_typing) if (Friends.list[self->num].is_typing)
wattroff(statusbar->topline, COLOR_PAIR(YELLOW)); wattroff(statusbar->topline, COLOR_PAIR(YELLOW));
} else { } else {
wprintw(statusbar->topline, " %s", OFFLINE_CHAR); wprintw(statusbar->topline, " %s", OFFLINE_CHAR);
@ -958,7 +958,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
int i; int i;
for (i = 0; i < KEY_IDENT_DIGITS; ++i) for (i = 0; i < KEY_IDENT_DIGITS; ++i)
wprintw(statusbar->topline, "%02X", friends[self->num].pub_key[i] & 0xff); wprintw(statusbar->topline, "%02X", Friends.list[self->num].pub_key[i] & 0xff);
wprintw(statusbar->topline, "}\n"); wprintw(statusbar->topline, "}\n");
@ -1022,8 +1022,8 @@ static void chat_onInit(ToxWindow *self, Tox *m)
line_info_init(ctx->hst); line_info_init(ctx->hst);
if (friends[self->num].logging_on) if (Friends.list[self->num].logging_on)
log_enable(nick, friends[self->num].pub_key, ctx->log); log_enable(nick, Friends.list[self->num].pub_key, ctx->log);
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);

View File

@ -34,8 +34,7 @@
#include "file_senders.h" #include "file_senders.h"
extern ToxWindow *prompt; extern ToxWindow *prompt;
extern _Friends Friends;
extern ToxicFriend friends[MAX_FRIENDS_NUM];
extern FileSender file_senders[MAX_FILES]; extern FileSender file_senders[MAX_FILES];
extern uint8_t max_file_senders_index; extern uint8_t max_file_senders_index;
@ -57,12 +56,12 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar
} }
if (strcasecmp(inoutstr, "in") == 0) { /* cancel an incoming file transfer */ if (strcasecmp(inoutstr, "in") == 0) { /* cancel an incoming file transfer */
if (!friends[self->num].file_receiver[filenum].active) { if (!Friends.list[self->num].file_receiver[filenum].active) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
return; return;
} }
const char *filepath = friends[self->num].file_receiver[filenum].filename; const char *filepath = Friends.list[self->num].file_receiver[filenum].filename;
char name[MAX_STR_SIZE]; char name[MAX_STR_SIZE];
get_file_name(name, sizeof(name), filepath); get_file_name(name, sizeof(name), filepath);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer for '%s' canceled.", name); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer for '%s' canceled.", name);
@ -124,9 +123,9 @@ void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar
return; return;
} }
const char *groupkey = friends[self->num].groupchat_key; const char *groupkey = Friends.list[self->num].groupchat_key;
if (!friends[self->num].groupchat_pending) { if (!Friends.list[self->num].groupchat_pending) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending group chat invite."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending group chat invite.");
return; return;
} }
@ -159,12 +158,12 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
return; return;
} }
if (!friends[self->num].file_receiver[filenum].pending) { if (!Friends.list[self->num].file_receiver[filenum].pending) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
return; return;
} }
const char *filename = friends[self->num].file_receiver[filenum].filename; const char *filename = Friends.list[self->num].file_receiver[filenum].filename;
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) { if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%d] as: '%s'", filenum, filename); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%d] as: '%s'", filenum, filename);
@ -173,9 +172,9 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
char progline[MAX_STR_SIZE]; char progline[MAX_STR_SIZE];
prep_prog_line(progline); prep_prog_line(progline);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline);
friends[self->num].file_receiver[filenum].line_id = self->chatwin->hst->line_end->id + 2; Friends.list[self->num].file_receiver[filenum].line_id = self->chatwin->hst->line_end->id + 2;
if ((friends[self->num].file_receiver[filenum].file = fopen(filename, "a")) == NULL) { if ((Friends.list[self->num].file_receiver[filenum].file = fopen(filename, "a")) == NULL) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Error writing to file."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Error writing to file.");
tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
} }
@ -183,8 +182,8 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed.");
} }
friends[self->num].file_receiver[filenum].pending = false; Friends.list[self->num].file_receiver[filenum].pending = false;
friends[self->num].file_receiver[filenum].active = true; Friends.list[self->num].file_receiver[filenum].active = true;
} }
void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])

View File

@ -36,7 +36,7 @@
FileSender file_senders[MAX_FILES]; FileSender file_senders[MAX_FILES];
uint8_t max_file_senders_index; uint8_t max_file_senders_index;
uint8_t num_active_file_senders; uint8_t num_active_file_senders;
extern ToxicFriend friends[MAX_FRIENDS_NUM]; extern _Friends Friends;
#define KiB 1024 #define KiB 1024
#define MiB 1048576 /* 1024 ^ 2 */ #define MiB 1048576 /* 1024 ^ 2 */
@ -66,8 +66,8 @@ void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_done
bps = file_senders[idx].bps; bps = file_senders[idx].bps;
line_id = file_senders[idx].line_id; line_id = file_senders[idx].line_id;
} else { } else {
bps = friends[friendnum].file_receiver[idx].bps; bps = Friends.list[friendnum].file_receiver[idx].bps;
line_id = friends[friendnum].file_receiver[idx].line_id; line_id = Friends.list[friendnum].file_receiver[idx].line_id;
} }
const char *unit; const char *unit;
@ -88,13 +88,11 @@ void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_done
char msg[MAX_STR_SIZE]; char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "%.1f %s [", bps, unit); snprintf(msg, sizeof(msg), "%.1f %s [", bps, unit);
int n = pct_done / (100 / NUM_PROG_MARKS); int n = pct_done / (100 / NUM_PROG_MARKS);
int i; int i, j;
for (i = 0; i < n; ++i) for (i = 0; i < n; ++i)
strcat(msg, "#"); strcat(msg, "#");
int j;
for (j = i; j < NUM_PROG_MARKS; ++j) for (j = i; j < NUM_PROG_MARKS; ++j)
strcat(msg, "-"); strcat(msg, "-");

View File

@ -27,7 +27,7 @@
#include "windows.h" #include "windows.h"
#define FILE_PIECE_SIZE 2048 /* must be >= (MAX_CRYPTO_DATA_SIZE - 2) in toxcore/net_crypto.h */ #define FILE_PIECE_SIZE 2048 /* must be >= (MAX_CRYPTO_DATA_SIZE - 2) in toxcore/net_crypto.h */
#define MAX_FILES 255 #define MAX_FILES 32
#define TIMEOUT_FILESENDER 120 #define TIMEOUT_FILESENDER 120
#define NUM_PROG_MARKS 50 /* number of "#"'s in file transfer progress bar. Keep well below MAX_STR_SIZE */ #define NUM_PROG_MARKS 50 /* number of "#"'s in file transfer progress bar. Keep well below MAX_STR_SIZE */

View File

@ -51,19 +51,16 @@ extern struct user_settings *user_settings_;
extern struct arg_opts arg_opts; extern struct arg_opts arg_opts;
static uint8_t blocklist_view = 0; /* 0 if we're in friendlist view, 1 if we're in blocklist view */ static uint8_t blocklist_view = 0; /* 0 if we're in friendlist view, 1 if we're in blocklist view */
static int num_selected = 0;
static int max_friends_index = 0; /* 1 + the index of the last friend in friends array */
static int num_friends = 0;
ToxicFriend friends[MAX_FRIENDS_NUM]; _Friends Friends;
static int friendlist_index[MAX_FRIENDS_NUM] = {0};
static struct _Blocked_Contacts { static struct _Blocked_Contacts {
int num_selected; int num_selected;
int max_index; int max_idx;
int num_blocked; int num_blocked;
BlockedFriend list[MAX_FRIENDS_NUM];
int index[MAX_FRIENDS_NUM]; int *index;
BlockedFriend *list;
} Blocked_Contacts; } Blocked_Contacts;
static struct _pendingDel { static struct _pendingDel {
@ -72,6 +69,52 @@ static struct _pendingDel {
WINDOW *popup; WINDOW *popup;
} pendingdelete; } pendingdelete;
static void realloc_friends(int n)
{
if (n <= 0) {
free(Friends.list);
free(Friends.index);
Friends.list = NULL;
Friends.index = NULL;
return;
}
ToxicFriend *f = realloc(Friends.list, n * sizeof(ToxicFriend));
int *f_idx = realloc(Friends.index, n * sizeof(int));
if (f == NULL || f_idx == NULL)
exit_toxic_err("failed in realloc_friends", FATALERR_MEMORY);
Friends.list = f;
Friends.index = f_idx;
}
static void realloc_blocklist(int n)
{
if (n <= 0) {
free(Blocked_Contacts.list);
free(Blocked_Contacts.index);
Blocked_Contacts.list = NULL;
Blocked_Contacts.index = NULL;
return;
}
BlockedFriend *b = realloc(Blocked_Contacts.list, n * sizeof(BlockedFriend));
int *b_idx = realloc(Blocked_Contacts.index, n * sizeof(int));
if (b == NULL || b_idx == NULL)
exit_toxic_err("failed in realloc_blocklist", FATALERR_MEMORY);
Blocked_Contacts.list = b;
Blocked_Contacts.index = b_idx;
}
void kill_friendlist(void)
{
realloc_blocklist(0);
realloc_friends(0);
}
static int save_blocklist(char *path) static int save_blocklist(char *path)
{ {
if (arg_opts.ignore_data_file) if (arg_opts.ignore_data_file)
@ -89,7 +132,7 @@ static int save_blocklist(char *path)
int i; int i;
int count = 0; int count = 0;
for (i = 0; i < Blocked_Contacts.max_index; ++i) { for (i = 0; i < Blocked_Contacts.max_idx; ++i) {
if (count > Blocked_Contacts.num_blocked) if (count > Blocked_Contacts.num_blocked)
return -1; return -1;
@ -163,9 +206,14 @@ int load_blocklist(char *path)
} }
int num = len / sizeof(BlockedFriend); int num = len / sizeof(BlockedFriend);
Blocked_Contacts.max_idx = num;
realloc_blocklist(num);
int i; int i;
for (i = 0; i < num; ++i) { for (i = 0; i < num; ++i) {
memset(&Blocked_Contacts.list[i], 0, sizeof(BlockedFriend));
BlockedFriend tmp; BlockedFriend tmp;
memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend)); memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend));
Blocked_Contacts.list[i].active = true; Blocked_Contacts.list[i].active = true;
@ -182,8 +230,6 @@ int load_blocklist(char *path)
++Blocked_Contacts.num_blocked; ++Blocked_Contacts.num_blocked;
} }
Blocked_Contacts.max_index = i + 1;
free(data); free(data);
fclose(fp); fclose(fp);
sort_blocklist_index(); sort_blocklist_index();
@ -194,27 +240,27 @@ int load_blocklist(char *path)
#define S_WEIGHT 100000 #define S_WEIGHT 100000
static int index_name_cmp(const void *n1, const void *n2) static int index_name_cmp(const void *n1, const void *n2)
{ {
int res = qsort_strcasecmp_hlpr(friends[*(int *) n1].name, friends[*(int *) n2].name); int res = qsort_strcasecmp_hlpr(Friends.list[*(int *) n1].name, Friends.list[*(int *) n2].name);
/* Use weight to make qsort always put online friends before offline */ /* Use weight to make qsort always put online friends before offline */
res = friends[*(int *) n1].online ? (res - S_WEIGHT) : (res + S_WEIGHT); res = Friends.list[*(int *) n1].online ? (res - S_WEIGHT) : (res + S_WEIGHT);
res = friends[*(int *) n2].online ? (res + S_WEIGHT) : (res - S_WEIGHT); res = Friends.list[*(int *) n2].online ? (res + S_WEIGHT) : (res - S_WEIGHT);
return res; return res;
} }
/* sorts friendlist_index first by connection status then alphabetically */ /* sorts Friends.index first by connection status then alphabetically */
void sort_friendlist_index(void) void sort_friendlist_index(void)
{ {
int i; int i;
int n = 0; int n = 0;
for (i = 0; i < max_friends_index; ++i) { for (i = 0; i < Friends.max_idx; ++i) {
if (friends[i].active) if (Friends.list[i].active)
friendlist_index[n++] = friends[i].num; Friends.index[n++] = Friends.list[i].num;
} }
qsort(friendlist_index, num_friends, sizeof(int), index_name_cmp); qsort(Friends.index, Friends.num_friends, sizeof(int), index_name_cmp);
} }
static int index_name_cmp_block(const void *n1, const void *n2) static int index_name_cmp_block(const void *n1, const void *n2)
@ -227,7 +273,7 @@ static void sort_blocklist_index(void)
int i; int i;
int n = 0; int n = 0;
for (i = 0; i < Blocked_Contacts.max_index; ++i) { for (i = 0; i < Blocked_Contacts.max_idx; ++i) {
if (Blocked_Contacts.list[i].active) if (Blocked_Contacts.list[i].active)
Blocked_Contacts.index[n++] = Blocked_Contacts.list[i].num; Blocked_Contacts.index[n++] = Blocked_Contacts.list[i].num;
} }
@ -237,23 +283,23 @@ static void sort_blocklist_index(void)
static void update_friend_last_online(int32_t num, uint64_t timestamp) static void update_friend_last_online(int32_t num, uint64_t timestamp)
{ {
friends[num].last_online.last_on = timestamp; Friends.list[num].last_online.last_on = timestamp;
friends[num].last_online.tm = *localtime((const time_t*)&timestamp); Friends.list[num].last_online.tm = *localtime((const time_t*)&timestamp);
/* if the format changes make sure TIME_STR_SIZE is the correct size */ /* if the format changes make sure TIME_STR_SIZE is the correct size */
const char *t = user_settings_->time == TIME_12 ? "%I:%M %p" : "%H:%M"; const char *t = user_settings_->time == TIME_12 ? "%I:%M %p" : "%H:%M";
strftime(friends[num].last_online.hour_min_str, TIME_STR_SIZE, t, strftime(Friends.list[num].last_online.hour_min_str, TIME_STR_SIZE, t,
&friends[num].last_online.tm); &Friends.list[num].last_online.tm);
} }
static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, const char *str, uint16_t len) static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, const char *str, uint16_t len)
{ {
if (num >= max_friends_index) if (num >= Friends.max_idx)
return; return;
if (friends[num].chatwin == -1) { if (Friends.list[num].chatwin == -1) {
if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (get_num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); Friends.list[num].chatwin = add_window(m, new_chat(m, Friends.list[num].num));
} else { } else {
char nick[TOX_MAX_NAME_LENGTH]; char nick[TOX_MAX_NAME_LENGTH];
get_nick_truncate(m, nick, num); get_nick_truncate(m, nick, num);
@ -272,10 +318,10 @@ static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, const cha
static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status) static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status)
{ {
if (num >= max_friends_index) if (num >= Friends.max_idx)
return; return;
friends[num].online = status; Friends.list[num].online = status;
update_friend_last_online(num, get_unix_time()); update_friend_last_online(num, get_unix_time());
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
sort_friendlist_index(); sort_friendlist_index();
@ -283,106 +329,110 @@ static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int32_t num,
static void friendlist_onNickChange(ToxWindow *self, Tox *m, int32_t num, const char *nick, uint16_t len) static void friendlist_onNickChange(ToxWindow *self, Tox *m, int32_t num, const char *nick, uint16_t len)
{ {
if (len > TOX_MAX_NAME_LENGTH || num >= max_friends_index) if (len > TOX_MAX_NAME_LENGTH || num >= Friends.max_idx)
return; return;
char tempname[TOX_MAX_NAME_LENGTH]; char tempname[TOX_MAX_NAME_LENGTH];
strcpy(tempname, nick); strcpy(tempname, nick);
len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
tempname[len] = '\0'; tempname[len] = '\0';
snprintf(friends[num].name, sizeof(friends[num].name), "%s", tempname); snprintf(Friends.list[num].name, sizeof(Friends.list[num].name), "%s", tempname);
friends[num].namelength = len; Friends.list[num].namelength = len;
sort_friendlist_index(); sort_friendlist_index();
} }
static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status) static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status)
{ {
if (num >= max_friends_index) if (num >= Friends.max_idx)
return; return;
friends[num].status = status; Friends.list[num].status = status;
} }
static void friendlist_onStatusMessageChange(ToxWindow *self, int32_t num, const char *status, uint16_t len) static void friendlist_onStatusMessageChange(ToxWindow *self, int32_t num, const char *status, uint16_t len)
{ {
if (len > TOX_MAX_STATUSMESSAGE_LENGTH || num >= max_friends_index) if (len > TOX_MAX_STATUSMESSAGE_LENGTH || num >= Friends.max_idx)
return; return;
snprintf(friends[num].statusmsg, sizeof(friends[num].statusmsg), "%s", status); snprintf(Friends.list[num].statusmsg, sizeof(Friends.list[num].statusmsg), "%s", status);
friends[num].statusmsg_len = strlen(friends[num].statusmsg); Friends.list[num].statusmsg_len = strlen(Friends.list[num].statusmsg);
} }
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort) void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort)
{ {
if (max_friends_index < 0 || max_friends_index >= MAX_FRIENDS_NUM) if (Friends.max_idx < 0)
return; return;
Friends.num_friends = tox_count_friendlist(m);
realloc_friends(Friends.max_idx + 1);
memset(&Friends.list[Friends.max_idx], 0, sizeof(ToxicFriend));
int i; int i;
for (i = 0; i <= max_friends_index; ++i) { for (i = 0; i <= Friends.max_idx; ++i) {
if (!friends[i].active) { if (Friends.list[i].active)
friends[i].num = num; continue;
friends[i].active = true;
friends[i].chatwin = -1;
friends[i].online = false;
friends[i].status = TOX_USERSTATUS_NONE;
friends[i].logging_on = (bool) user_settings_->autolog == AUTOLOG_ON;
tox_get_client_id(m, num, (uint8_t *) friends[i].pub_key);
update_friend_last_online(i, tox_get_last_online(m, i));
char tempname[TOX_MAX_NAME_LENGTH] = {0}; Friends.list[i].num = num;
int len = get_nick_truncate(m, tempname, num); Friends.list[i].active = true;
Friends.list[i].chatwin = -1;
Friends.list[i].online = false;
Friends.list[i].status = TOX_USERSTATUS_NONE;
Friends.list[i].logging_on = (bool) user_settings_->autolog == AUTOLOG_ON;
tox_get_client_id(m, num, (uint8_t *) Friends.list[i].pub_key);
update_friend_last_online(i, tox_get_last_online(m, i));
if (len == -1 || tempname[0] == '\0') { char tempname[TOX_MAX_NAME_LENGTH] = {0};
strcpy(friends[i].name, UNKNOWN_NAME); int len = get_nick_truncate(m, tempname, num);
friends[i].namelength = strlen(UNKNOWN_NAME);
} else { /* Enforce toxic's maximum name length */
friends[i].namelength = len;
snprintf(friends[i].name, sizeof(friends[i].name), "%s", tempname);
}
num_friends = tox_count_friendlist(m); if (len == -1 || tempname[0] == '\0') {
strcpy(Friends.list[i].name, UNKNOWN_NAME);
if (i == max_friends_index) Friends.list[i].namelength = strlen(UNKNOWN_NAME);
++max_friends_index; } else { /* Enforce toxic's maximum name length */
Friends.list[i].namelength = len;
if (sort) snprintf(Friends.list[i].name, sizeof(Friends.list[i].name), "%s", tempname);
sort_friendlist_index();
return;
} }
if (i == Friends.max_idx)
++Friends.max_idx;
if (sort)
sort_friendlist_index();
return;
} }
} }
/* puts blocked friend back in friendlist. fnum is new friend number, bnum is blocked number */ /* puts blocked friend back in friendlist. fnum is new friend number, bnum is blocked number */
static void friendlist_add_blocked(Tox *m, int32_t fnum, int32_t bnum) static void friendlist_add_blocked(Tox *m, int32_t fnum, int32_t bnum)
{ {
if (max_friends_index >= MAX_FRIENDS_NUM) Friends.num_friends = tox_count_friendlist(m);
return; realloc_friends(Friends.max_idx + 1);
memset(&Friends.list[Friends.max_idx], 0, sizeof(ToxicFriend));
int i; int i;
for (i = 0; i <= max_friends_index; ++i) { for (i = 0; i <= Friends.max_idx; ++i) {
if (friends[i].active) if (Friends.list[i].active)
continue; continue;
friends[i].num = fnum; Friends.list[i].num = fnum;
friends[i].active = true; Friends.list[i].active = true;
friends[i].chatwin = -1; Friends.list[i].chatwin = -1;
friends[i].status = TOX_USERSTATUS_NONE; Friends.list[i].status = TOX_USERSTATUS_NONE;
friends[i].logging_on = (bool) user_settings_->autolog == AUTOLOG_ON; Friends.list[i].logging_on = (bool) user_settings_->autolog == AUTOLOG_ON;
friends[i].namelength = Blocked_Contacts.list[bnum].namelength; Friends.list[i].namelength = Blocked_Contacts.list[bnum].namelength;
update_friend_last_online(i, Blocked_Contacts.list[bnum].last_on); update_friend_last_online(i, Blocked_Contacts.list[bnum].last_on);
memcpy(friends[i].name, Blocked_Contacts.list[bnum].name, friends[i].namelength + 1); memcpy(Friends.list[i].name, Blocked_Contacts.list[bnum].name, Friends.list[i].namelength + 1);
memcpy(friends[i].pub_key, Blocked_Contacts.list[bnum].pub_key, TOX_CLIENT_ID_SIZE); memcpy(Friends.list[i].pub_key, Blocked_Contacts.list[bnum].pub_key, TOX_CLIENT_ID_SIZE);
num_friends = tox_count_friendlist(m); if (i == Friends.max_idx)
++Friends.max_idx;
if (i == max_friends_index)
++max_friends_index;
sort_blocklist_index(); sort_blocklist_index();
sort_friendlist_index(); sort_friendlist_index();
return; return;
} }
} }
@ -390,12 +440,12 @@ static void friendlist_add_blocked(Tox *m, int32_t fnum, int32_t bnum)
static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum, static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum,
uint64_t filesize, const char *filename, uint16_t filename_len) uint64_t filesize, const char *filename, uint16_t filename_len)
{ {
if (num >= max_friends_index) if (num >= Friends.max_idx)
return; return;
if (friends[num].chatwin == -1) { if (Friends.list[num].chatwin == -1) {
if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (get_num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); Friends.list[num].chatwin = add_window(m, new_chat(m, Friends.list[num].num));
} else { } else {
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
@ -412,12 +462,12 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u
static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, const char *group_pub_key) static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, const char *group_pub_key)
{ {
if (num >= max_friends_index) if (num >= Friends.max_idx)
return; return;
if (friends[num].chatwin == -1) { if (Friends.list[num].chatwin == -1) {
if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (get_num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); Friends.list[num].chatwin = add_window(m, new_chat(m, Friends.list[num].num));
if (self->active_box != -1) if (self->active_box != -1)
box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box, box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box,
@ -452,8 +502,8 @@ static void select_friend(ToxWindow *self, wint_t key, int *selected, int num)
static void delete_friend(Tox *m, int32_t f_num) static void delete_friend(Tox *m, int32_t f_num)
{ {
if (friends[f_num].chatwin >= 0) { if (Friends.list[f_num].chatwin >= 0) {
ToxWindow *toxwin = get_window_ptr(friends[f_num].chatwin); ToxWindow *toxwin = get_window_ptr(Friends.list[f_num].chatwin);
if (toxwin != NULL) { if (toxwin != NULL) {
kill_chat_window(toxwin, m); kill_chat_window(toxwin, m);
@ -462,21 +512,22 @@ static void delete_friend(Tox *m, int32_t f_num)
} }
tox_del_friend(m, f_num); tox_del_friend(m, f_num);
memset(&friends[f_num], 0, sizeof(ToxicFriend)); memset(&Friends.list[f_num], 0, sizeof(ToxicFriend));
int i; int i;
for (i = max_friends_index; i > 0; --i) { for (i = Friends.max_idx; i > 0; --i) {
if (friends[i - 1].active) if (Friends.list[i - 1].active)
break; break;
} }
max_friends_index = i; Friends.max_idx = i;
num_friends = tox_count_friendlist(m); Friends.num_friends = tox_count_friendlist(m);
realloc_friends(i);
/* make sure num_selected stays within num_friends range */ /* make sure num_selected stays within Friends.num_friends range */
if (num_friends && num_selected == num_friends) if (Friends.num_friends && Friends.num_selected == Friends.num_friends)
--num_selected; --Friends.num_selected;
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
} }
@ -524,7 +575,7 @@ static void draw_del_popup(void)
wattron(pendingdelete.popup, A_BOLD); wattron(pendingdelete.popup, A_BOLD);
if (blocklist_view == 0) if (blocklist_view == 0)
wprintw(pendingdelete.popup, "%s", friends[pendingdelete.num].name); wprintw(pendingdelete.popup, "%s", Friends.list[pendingdelete.num].name);
else else
wprintw(pendingdelete.popup, "%s", Blocked_Contacts.list[pendingdelete.num].name); wprintw(pendingdelete.popup, "%s", Blocked_Contacts.list[pendingdelete.num].name);
@ -541,13 +592,14 @@ static void delete_blocked_friend(int32_t bnum)
int i; int i;
for (i = Blocked_Contacts.max_index; i > 0; --i) { for (i = Blocked_Contacts.max_idx; i > 0; --i) {
if (Blocked_Contacts.list[i - 1].active) if (Blocked_Contacts.list[i - 1].active)
break; break;
} }
--Blocked_Contacts.num_blocked; --Blocked_Contacts.num_blocked;
Blocked_Contacts.max_index = i; Blocked_Contacts.max_idx = i;
realloc_blocklist(i);
save_blocklist(BLOCK_FILE); save_blocklist(BLOCK_FILE);
if (Blocked_Contacts.num_blocked && Blocked_Contacts.num_selected == Blocked_Contacts.num_blocked) if (Blocked_Contacts.num_blocked && Blocked_Contacts.num_selected == Blocked_Contacts.num_blocked)
@ -557,26 +609,29 @@ static void delete_blocked_friend(int32_t bnum)
/* deletes contact from friendlist and puts in blocklist */ /* deletes contact from friendlist and puts in blocklist */
void block_friend(Tox *m, int32_t fnum) void block_friend(Tox *m, int32_t fnum)
{ {
if (Blocked_Contacts.max_index >= MAX_FRIENDS_NUM || num_friends <= 0) if (Friends.num_friends <= 0)
return; return;
realloc_blocklist(Blocked_Contacts.max_idx + 1);
memset(&Blocked_Contacts.list[Blocked_Contacts.max_idx], 0, sizeof(BlockedFriend));
int i; int i;
for (i = 0; i <= Blocked_Contacts.max_index; ++i) { for (i = 0; i <= Blocked_Contacts.max_idx; ++i) {
if (Blocked_Contacts.list[i].active) if (Blocked_Contacts.list[i].active)
continue; continue;
Blocked_Contacts.list[i].active = true; Blocked_Contacts.list[i].active = true;
Blocked_Contacts.list[i].num = i; Blocked_Contacts.list[i].num = i;
Blocked_Contacts.list[i].namelength = friends[fnum].namelength; Blocked_Contacts.list[i].namelength = Friends.list[fnum].namelength;
Blocked_Contacts.list[i].last_on = friends[fnum].last_online.last_on; Blocked_Contacts.list[i].last_on = Friends.list[fnum].last_online.last_on;
memcpy(Blocked_Contacts.list[i].pub_key, friends[fnum].pub_key, TOX_CLIENT_ID_SIZE); memcpy(Blocked_Contacts.list[i].pub_key, Friends.list[fnum].pub_key, TOX_CLIENT_ID_SIZE);
memcpy(Blocked_Contacts.list[i].name, friends[fnum].name, friends[fnum].namelength + 1); memcpy(Blocked_Contacts.list[i].name, Friends.list[fnum].name, Friends.list[fnum].namelength + 1);
++Blocked_Contacts.num_blocked; ++Blocked_Contacts.num_blocked;
if (i == Blocked_Contacts.max_index) if (i == Blocked_Contacts.max_idx)
++Blocked_Contacts.max_index; ++Blocked_Contacts.max_idx;
delete_friend(m, fnum); delete_friend(m, fnum);
save_blocklist(BLOCK_FILE); save_blocklist(BLOCK_FILE);
@ -619,14 +674,18 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
return; return;
} }
if (!blocklist_view && !num_friends && (key != KEY_RIGHT && key != KEY_LEFT)) if (!blocklist_view && !Friends.num_friends && (key != KEY_RIGHT && key != KEY_LEFT))
return; return;
if (blocklist_view && !Blocked_Contacts.num_blocked && (key != KEY_RIGHT && key != KEY_LEFT)) if (blocklist_view && !Blocked_Contacts.num_blocked && (key != KEY_RIGHT && key != KEY_LEFT))
return; return;
int f = blocklist_view == 1 ? Blocked_Contacts.index[Blocked_Contacts.num_selected] int f = 0;
: friendlist_index[num_selected];
if (blocklist_view == 1 && Blocked_Contacts.num_blocked)
f = Blocked_Contacts.index[Blocked_Contacts.num_selected];
else if (Friends.num_friends)
f = Friends.index[Friends.num_selected];
/* lock screen and force decision on deletion popup */ /* lock screen and force decision on deletion popup */
if (pendingdelete.active) { if (pendingdelete.active) {
@ -645,11 +704,11 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
break; break;
/* Jump to chat window if already open */ /* Jump to chat window if already open */
if (friends[f].chatwin != -1) { if (Friends.list[f].chatwin != -1) {
set_active_window(friends[f].chatwin); set_active_window(Friends.list[f].chatwin);
} else if (get_num_active_windows() < MAX_WINDOWS_NUM) { } else if (get_num_active_windows() < MAX_WINDOWS_NUM) {
friends[f].chatwin = add_window(m, new_chat(m, friends[f].num)); Friends.list[f].chatwin = add_window(m, new_chat(m, Friends.list[f].num));
set_active_window(friends[f].chatwin); set_active_window(Friends.list[f].chatwin);
} else { } else {
const char *msg = "* Warning: Too many windows are open."; const char *msg = "* Warning: Too many windows are open.";
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, msg); line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, msg);
@ -676,7 +735,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
default: default:
if (blocklist_view == 0) if (blocklist_view == 0)
select_friend(self, key, &num_selected, num_friends); select_friend(self, key, &Friends.num_selected, Friends.num_friends);
else else
select_friend(self, key, &Blocked_Contacts.num_selected, Blocked_Contacts.num_blocked); select_friend(self, key, &Blocked_Contacts.num_selected, Blocked_Contacts.num_blocked);
break; break;
@ -788,7 +847,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
wprintw(self->window, " Online: "); wprintw(self->window, " Online: ");
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
wprintw(self->window, "%d/%d \n\n", nf, num_friends); wprintw(self->window, "%d/%d \n\n", nf, Friends.num_friends);
if ((y2 - FLIST_OFST) <= 0) if ((y2 - FLIST_OFST) <= 0)
return; return;
@ -796,18 +855,18 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
int selected_num = 0; int selected_num = 0;
/* Determine which portion of friendlist to draw based on current position */ /* Determine which portion of friendlist to draw based on current position */
int page = num_selected / (y2 - FLIST_OFST); int page = Friends.num_selected / (y2 - FLIST_OFST);
int start = (y2 - FLIST_OFST) * page; int start = (y2 - FLIST_OFST) * page;
int end = y2 - FLIST_OFST + start; int end = y2 - FLIST_OFST + start;
int i; int i;
for (i = start; i < num_friends && i < end; ++i) { for (i = start; i < Friends.num_friends && i < end; ++i) {
int f = friendlist_index[i]; int f = Friends.index[i];
bool f_selected = false; bool f_selected = false;
if (friends[f].active) { if (Friends.list[f].active) {
if (i == num_selected) { if (i == Friends.num_selected) {
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
wprintw(self->window, " > "); wprintw(self->window, " > ");
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
@ -817,8 +876,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wprintw(self->window, " "); wprintw(self->window, " ");
} }
if (friends[f].online) { if (Friends.list[f].online) {
uint8_t status = friends[f].status; uint8_t status = Friends.list[f].status;
int colour = WHITE; int colour = WHITE;
switch (status) { switch (status) {
@ -847,36 +906,36 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattron(self->window, COLOR_PAIR(BLUE)); wattron(self->window, COLOR_PAIR(BLUE));
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
wprintw(self->window, "%s", friends[f].name); wprintw(self->window, "%s", Friends.list[f].name);
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
if (f_selected) if (f_selected)
wattroff(self->window, COLOR_PAIR(BLUE)); wattroff(self->window, COLOR_PAIR(BLUE));
/* Reset friends[f].statusmsg on window resize */ /* Reset Friends.list[f].statusmsg on window resize */
if (fix_statuses) { if (fix_statuses) {
char statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; char statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
tox_get_status_message(m, friends[f].num, (uint8_t *) statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); tox_get_status_message(m, Friends.list[f].num, (uint8_t *) statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg); snprintf(Friends.list[f].statusmsg, sizeof(Friends.list[f].statusmsg), "%s", statusmsg);
friends[f].statusmsg_len = strlen(friends[f].statusmsg); Friends.list[f].statusmsg_len = strlen(Friends.list[f].statusmsg);
} }
/* Truncate note if it doesn't fit on one line */ /* Truncate note if it doesn't fit on one line */
uint16_t maxlen = x2 - getcurx(self->window) - 2; uint16_t maxlen = x2 - getcurx(self->window) - 2;
if (friends[f].statusmsg_len > maxlen) { if (Friends.list[f].statusmsg_len > maxlen) {
friends[f].statusmsg[maxlen - 3] = '\0'; Friends.list[f].statusmsg[maxlen - 3] = '\0';
strcat(friends[f].statusmsg, "..."); strcat(Friends.list[f].statusmsg, "...");
friends[f].statusmsg[maxlen] = '\0'; Friends.list[f].statusmsg[maxlen] = '\0';
friends[f].statusmsg_len = maxlen; Friends.list[f].statusmsg_len = maxlen;
} }
if (friends[f].statusmsg[0]) if (Friends.list[f].statusmsg[0])
wprintw(self->window, " %s", friends[f].statusmsg); wprintw(self->window, " %s", Friends.list[f].statusmsg);
wprintw(self->window, "\n"); wprintw(self->window, "\n");
} else { } else {
@ -886,17 +945,17 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattron(self->window, COLOR_PAIR(BLUE)); wattron(self->window, COLOR_PAIR(BLUE));
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
wprintw(self->window, "%s", friends[f].name); wprintw(self->window, "%s", Friends.list[f].name);
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
if (f_selected) if (f_selected)
wattroff(self->window, COLOR_PAIR(BLUE)); wattroff(self->window, COLOR_PAIR(BLUE));
uint64_t last_seen = friends[f].last_online.last_on; uint64_t last_seen = Friends.list[f].last_online.last_on;
if (last_seen != 0) { if (last_seen != 0) {
int day_dist = (cur_loc_tm.tm_yday - friends[f].last_online.tm.tm_yday) % 365; int day_dist = (cur_loc_tm.tm_yday - Friends.list[f].last_online.tm.tm_yday) % 365;
const char *hourmin = friends[f].last_online.hour_min_str; const char *hourmin = Friends.list[f].last_online.hour_min_str;
switch (day_dist) { switch (day_dist) {
case 0: case 0:
@ -920,7 +979,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
self->x = x2; self->x = x2;
if (num_friends) { if (Friends.num_friends) {
wmove(self->window, y2 - 1, 1); wmove(self->window, y2 - 1, 1);
wattron(self->window, A_BOLD); wattron(self->window, A_BOLD);
@ -930,7 +989,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
int i; int i;
for (i = 0; i < TOX_CLIENT_ID_SIZE; ++i) for (i = 0; i < TOX_CLIENT_ID_SIZE; ++i)
wprintw(self->window, "%02X", friends[selected_num].pub_key[i] & 0xff); wprintw(self->window, "%02X", Friends.list[selected_num].pub_key[i] & 0xff);
} }
wrefresh(self->window); wrefresh(self->window);
@ -942,7 +1001,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
void disable_chatwin(int32_t f_num) void disable_chatwin(int32_t f_num)
{ {
friends[f_num].chatwin = -1; Friends.list[f_num].chatwin = -1;
} }
#ifdef _AUDIO #ifdef _AUDIO
@ -951,19 +1010,19 @@ static void friendlist_onAv(ToxWindow *self, ToxAv *av, int call_index)
int id = toxav_get_peer_id(av, call_index, 0); int id = toxav_get_peer_id(av, call_index, 0);
/*id++;*/ /*id++;*/
if ( id != ErrorInternal && id >= max_friends_index) if ( id != ErrorInternal && id >= Friends.max_idx)
return; return;
Tox *m = toxav_get_tox(av); Tox *m = toxav_get_tox(av);
if (friends[id].chatwin == -1) { if (Friends.list[id].chatwin == -1) {
if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (get_num_active_windows() < MAX_WINDOWS_NUM) {
if (toxav_get_call_state(av, call_index) == av_CallStarting) { /* Only open windows when call is incoming */ if (toxav_get_call_state(av, call_index) == av_CallStarting) { /* Only open windows when call is incoming */
friends[id].chatwin = add_window(m, new_chat(m, friends[id].num)); Friends.list[id].chatwin = add_window(m, new_chat(m, Friends.list[id].num));
} }
} else { } else {
char nick[TOX_MAX_NAME_LENGTH]; char nick[TOX_MAX_NAME_LENGTH];
get_nick_truncate(m, nick, friends[id].num); get_nick_truncate(m, nick, Friends.list[id].num);
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick); line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick);
const char *errmsg = "* Warning: Too many windows are open."; const char *errmsg = "* Warning: Too many windows are open.";

View File

@ -60,7 +60,7 @@ typedef struct {
bool active; bool active;
bool online; bool online;
uint8_t is_typing; uint8_t is_typing;
bool logging_on; /* saves preference for friend irrespective of chat windows */ bool logging_on; /* saves preference for friend irrespective of global settings */
uint8_t status; uint8_t status;
struct LastOnline last_online; struct LastOnline last_online;
struct FileReceiver file_receiver[MAX_FILES]; struct FileReceiver file_receiver[MAX_FILES];
@ -75,11 +75,19 @@ typedef struct {
uint64_t last_on; uint64_t last_on;
} BlockedFriend; } BlockedFriend;
typedef struct {
int num_selected;
int max_idx; /* 1 + the index of the last friend in friends array */
int num_friends;
int *index;
ToxicFriend *list;
} _Friends;
ToxWindow new_friendlist(void); ToxWindow new_friendlist(void);
void disable_chatwin(int32_t f_num); void disable_chatwin(int32_t f_num);
int get_friendnum(uint8_t *name); int get_friendnum(uint8_t *name);
int load_blocklist(char *data); int load_blocklist(char *data);
void kill_friendlist(void);
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort); void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort);
/* sorts friendlist_index first by connection status then alphabetically */ /* sorts friendlist_index first by connection status then alphabetically */

View File

@ -37,11 +37,8 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern ToxWindow *prompt; extern ToxWindow *prompt;
extern _Friends Friends;
extern ToxicFriend friends[MAX_FRIENDS_NUM]; extern _FriendRequests FriendRequests;
extern char pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
extern uint8_t num_frnd_requests;
/* command functions */ /* command functions */
void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
@ -53,18 +50,18 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
int req = atoi(argv[1]); int req = atoi(argv[1]);
if ((req == 0 && strcmp(argv[1], "0")) || req >= MAX_FRIENDS_NUM) { if ((req == 0 && strcmp(argv[1], "0")) || req < 0 || req >= MAX_FRIEND_REQUESTS) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID.");
return; return;
} }
if (!strlen(pending_frnd_requests[req])) { if (FriendRequests.list[req][0] == '\0') {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID.");
return; return;
} }
const char *msg; const char *msg;
int32_t friendnum = tox_add_friend_norequest(m, (uint8_t *) pending_frnd_requests[req]); int32_t friendnum = tox_add_friend_norequest(m, FriendRequests.list[req]);
if (friendnum == -1) if (friendnum == -1)
msg = "Failed to add friend."; msg = "Failed to add friend.";
@ -73,16 +70,17 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
on_friendadded(m, friendnum, true); on_friendadded(m, friendnum, true);
} }
memset(&pending_frnd_requests[req], 0, TOX_CLIENT_ID_SIZE); memset(&FriendRequests.list[req], 0, TOX_CLIENT_ID_SIZE);
int i; int i;
for (i = num_frnd_requests; i > 0; --i) { for (i = FriendRequests.index; i > 0; --i) {
if (!strlen(pending_frnd_requests[i - 1])) if (FriendRequests.list[i - 1][0] == '\0')
break; break;
} }
num_frnd_requests = i; FriendRequests.index = i;
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg);
} }
@ -258,8 +256,8 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
if (!strcmp(swch, "1") || !strcmp(swch, "on")) { if (!strcmp(swch, "1") || !strcmp(swch, "on")) {
if (self->is_chat) { if (self->is_chat) {
friends[self->num].logging_on = true; Friends.list[self->num].logging_on = true;
log_enable(self->name, friends[self->num].pub_key, log); log_enable(self->name, Friends.list[self->num].pub_key, log);
} else if (self->is_prompt) { } else if (self->is_prompt) {
char myid[TOX_FRIEND_ADDRESS_SIZE]; char myid[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(m, (uint8_t *) myid); tox_get_address(m, (uint8_t *) myid);
@ -273,7 +271,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
return; return;
} else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) {
if (self->is_chat) if (self->is_chat)
friends[self->num].logging_on = false; Friends.list[self->num].logging_on = false;
log_disable(log); log_disable(log);

View File

@ -42,13 +42,11 @@
#include "notify.h" #include "notify.h"
#include "autocomplete.h" #include "autocomplete.h"
char pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
uint16_t num_frnd_requests = 0;
extern ToxWindow *prompt; extern ToxWindow *prompt;
struct _Winthread Winthread;
extern struct user_settings *user_settings_; extern struct user_settings *user_settings_;
_FriendRequests FriendRequests;
/* Array of global command names used for tab completion. */ /* Array of global command names used for tab completion. */
const char glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = { const char glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/accept" }, { "/accept" },
@ -126,20 +124,20 @@ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected)
} }
/* Adds friend request to pending friend requests. /* Adds friend request to pending friend requests.
Returns request number on success, -1 if queue is full or other error. */ Returns request number on success, -1 if queue is full. */
static int add_friend_request(const char *public_key) static int add_friend_request(const char *public_key)
{ {
if (num_frnd_requests >= MAX_FRIENDS_NUM) if (FriendRequests.index >= MAX_FRIEND_REQUESTS)
return -1; return -1;
int i; int i;
for (i = 0; i <= num_frnd_requests; ++i) { for (i = 0; i <= FriendRequests.index; ++i) {
if (!strlen(pending_frnd_requests[i])) { if (FriendRequests.list[i][0] == '\0') {
memcpy(pending_frnd_requests[i], public_key, TOX_CLIENT_ID_SIZE); memcpy(FriendRequests.list[i], public_key, TOX_CLIENT_ID_SIZE);
if (i == num_frnd_requests) if (i == FriendRequests.index)
++num_frnd_requests; ++FriendRequests.index;
return i; return i;
} }
@ -345,7 +343,6 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, const char *key, con
if (n == -1) { if (n == -1) {
const char *errmsg = "Friend request queue is full. Discarding request."; const char *errmsg = "Friend request queue is full. Discarding request.";
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
write_to_log(errmsg, "", ctx->log, true);
return; return;
} }

View File

@ -32,6 +32,13 @@
#define AC_NUM_GLOB_COMMANDS 14 #define AC_NUM_GLOB_COMMANDS 14
#endif /* _AUDIO */ #endif /* _AUDIO */
#define MAX_FRIEND_REQUESTS 32
typedef struct {
int index;
uint8_t list[MAX_FRIEND_REQUESTS][TOX_CLIENT_ID_SIZE];
} _FriendRequests;
ToxWindow new_prompt(void); ToxWindow new_prompt(void);
void prep_prompt_win(void); void prep_prompt_win(void);
void prompt_init_statusbar(ToxWindow *self, Tox *m); void prompt_init_statusbar(ToxWindow *self, Tox *m);

View File

@ -42,7 +42,6 @@
#define UNKNOWN_NAME "Anonymous" #define UNKNOWN_NAME "Anonymous"
#define MAX_FRIENDS_NUM 999
#define MAX_STR_SIZE TOX_MAX_MESSAGE_LENGTH #define MAX_STR_SIZE TOX_MAX_MESSAGE_LENGTH
#define MAX_CMDNAME_SIZE 64 #define MAX_CMDNAME_SIZE 64
#define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */ #define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */

View File

@ -100,9 +100,6 @@ void on_action(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t len
void on_nickchange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata) void on_nickchange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata)
{ {
if (friendnumber < 0 || friendnumber > MAX_FRIENDS_NUM)
return;
int i; int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
@ -500,6 +497,7 @@ int get_num_active_windows(void)
void kill_all_windows(Tox *m) void kill_all_windows(Tox *m)
{ {
kill_prompt_window(prompt); kill_prompt_window(prompt);
kill_friendlist();
int i; int i;