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

don't send null terminated strings, and null terminate all incoming strings

This commit is contained in:
Jfreegman 2014-03-31 22:00:17 -04:00
parent 15815bf4bb
commit 3b7e161149
8 changed files with 152 additions and 88 deletions

View File

@ -119,11 +119,15 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t *msg, u
if (self->num != num) if (self->num != num)
return; return;
msg[len] = '\0';
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH];
tox_get_name(m, num, nick); uint16_t n_len = tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH);
nick[n_len] = '\0';
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt); get_time_str(timefrmt);
@ -163,11 +167,16 @@ static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, uint8_t *action,
if (self->num != num) if (self->num != num)
return; return;
action[len] = '\0';
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH];
tox_get_name(m, num, nick); int n_len = tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH);;
nick[n_len] = '\0';
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt); get_time_str(timefrmt);
@ -182,10 +191,9 @@ static void chat_onNickChange(ToxWindow *self, Tox *m, int32_t num, uint8_t *nic
if (self->num != num) if (self->num != num)
return; return;
nick[TOXIC_MAX_NAME_LENGTH] = '\0'; len = MIN(len, TOXIC_MAX_NAME_LENGTH);
len = strlen(nick) + 1; nick[len] = '\0';
strcpy(self->name, nick); strcpy(self->name, nick);
self->name[len-1] = '\0';
} }
static void chat_onStatusChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status) static void chat_onStatusChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status)
@ -205,7 +213,7 @@ static void chat_onStatusMessageChange(ToxWindow *self, int32_t num, uint8_t *st
StatusBar *statusbar = self->stb; StatusBar *statusbar = self->stb;
statusbar->statusmsg_len = len; statusbar->statusmsg_len = len;
strcpy(statusbar->statusmsg, status); strcpy(statusbar->statusmsg, status);
statusbar->statusmsg[len-1] = '\0'; statusbar->statusmsg[len] = '\0';
} }
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum, static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum,
@ -485,14 +493,15 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti
return; return;
uint8_t selfname[TOX_MAX_NAME_LENGTH]; uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname); uint16_t len = tox_get_self_name(m, selfname);
selfname[len] = '\0';
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt); get_time_str(timefrmt);
line_info_add(self, timefrmt, selfname, NULL, action, ACTION, 0, 0); line_info_add(self, timefrmt, selfname, NULL, action, ACTION, 0, 0);
if (tox_send_action(m, self->num, action, strlen(action) + 1) == 0) { if (tox_send_action(m, self->num, action, strlen(action)) == 0) {
uint8_t *errmsg = " * Failed to send action."; uint8_t *errmsg = " * Failed to send action.";
line_info_add(self, NULL, selfname, NULL, errmsg, SYS_MSG, 0, RED); line_info_add(self, NULL, selfname, NULL, errmsg, SYS_MSG, 0, RED);
} else { } else {
@ -675,14 +684,15 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
} }
} else if (!string_is_empty(line)) { } else if (!string_is_empty(line)) {
uint8_t selfname[TOX_MAX_NAME_LENGTH]; uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname); uint16_t len = tox_get_self_name(m, selfname);
selfname[len] = '\0';
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt); get_time_str(timefrmt);
line_info_add(self, timefrmt, selfname, NULL, line, OUT_MSG, 0, 0); line_info_add(self, timefrmt, selfname, NULL, line, OUT_MSG, 0, 0);
if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) { if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line)) == 0) {
uint8_t *errmsg = " * Failed to send message."; uint8_t *errmsg = " * Failed to send message.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
} else { } else {
@ -782,10 +792,12 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); uint16_t s_len = tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
statusmsg[s_len] = '\0';
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = tox_get_status_message_size(m, self->num); statusbar->statusmsg_len = s_len;
} }
self->x = x2; self->x = x2;
@ -827,9 +839,10 @@ static void chat_onInit(ToxWindow *self, Tox *m)
statusbar->is_online = tox_get_friend_connection_status(m, self->num) == 1; statusbar->is_online = tox_get_friend_connection_status(m, self->num) == 1;
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); uint16_t s_len = tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
statusmsg[s_len] = '\0';
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = tox_get_status_message_size(m, self->num); statusbar->statusmsg_len = s_len;
/* Init subwindows */ /* Init subwindows */
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
@ -901,8 +914,11 @@ ToxWindow new_chat(Tox *m, int32_t friendnum)
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
uint16_t len = tox_get_name(m, friendnum, name); uint16_t len = tox_get_name(m, friendnum, name);
memcpy(ret.name, name, len);
ret.name[TOXIC_MAX_NAME_LENGTH] = '\0'; len = MIN(len, TOXIC_MAX_NAME_LENGTH);
name[len] = '\0';
strcpy(ret.name, name);
ChatContext *chatwin = calloc(1, sizeof(ChatContext)); ChatContext *chatwin = calloc(1, sizeof(ChatContext));
memset(chatwin, 0, sizeof(ChatContext)); memset(chatwin, 0, sizeof(ChatContext));

View File

@ -102,9 +102,13 @@ static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t *
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[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else { } else {
str[len] = '\0';
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, num, nick); uint16_t n_len = tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH);
nick[n_len] = '\0';
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt); get_time_str(timefrmt);
@ -134,10 +138,10 @@ static void friendlist_onNickChange(ToxWindow *self, Tox *m, int32_t num, uint8_
if (len > TOX_MAX_NAME_LENGTH || num >= max_friends_index) if (len > TOX_MAX_NAME_LENGTH || num >= max_friends_index)
return; return;
str[TOXIC_MAX_NAME_LENGTH] = '\0'; len = MIN(len, TOXIC_MAX_NAME_LENGTH);
str[len] = '\0';
strcpy(friends[num].name, str); strcpy(friends[num].name, str);
len = strlen(str) + 1;
friends[num].name[len-1] = '\0';
friends[num].namelength = len; friends[num].namelength = len;
sort_friendlist_index(); sort_friendlist_index();
} }
@ -156,7 +160,7 @@ static void friendlist_onStatusMessageChange(ToxWindow *self, int32_t num, uint8
return; return;
strcpy(friends[num].statusmsg, str); strcpy(friends[num].statusmsg, str);
friends[num].statusmsg[len-1] = '\0'; friends[num].statusmsg[len] = '\0';
friends[num].statusmsg_len = len; friends[num].statusmsg_len = len;
} }
@ -180,10 +184,10 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort)
if (friends[i].namelength == -1 || friends[i].name[0] == '\0') { if (friends[i].namelength == -1 || friends[i].name[0] == '\0') {
strcpy(friends[i].name, (uint8_t *) UNKNOWN_NAME); strcpy(friends[i].name, (uint8_t *) UNKNOWN_NAME);
friends[i].namelength = strlen(UNKNOWN_NAME) + 1; friends[i].namelength = strlen(UNKNOWN_NAME);
} else { /* Enforce toxic's maximum name length */ } else { /* Enforce toxic's maximum name length */
friends[i].name[TOXIC_MAX_NAME_LENGTH] = '\0'; friends[i].namelength = MIN(friends[i].namelength, TOXIC_MAX_NAME_LENGTH);
friends[i].namelength = strlen(friends[i].name) + 1; friends[i].name[friends[i].namelength] = '\0';
} }
num_friends = tox_count_friendlist(m); num_friends = tox_count_friendlist(m);
@ -211,9 +215,9 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u
} 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);
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH];
tox_get_name(m, num, nick); uint16_t n_len = tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0'; nick[n_len] = '\0';
uint8_t msg[MAX_STR_SIZE]; uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "* File transfer from %s failed: too many windows are open.", nick); snprintf(msg, sizeof(msg), "* File transfer from %s failed: too many windows are open.", nick);
@ -233,9 +237,9 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, uint8
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[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else { } else {
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH];
tox_get_name(m, num, nick); uint16_t n_len = tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0'; nick[n_len] = '\0';
uint8_t msg[MAX_STR_SIZE]; uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "* Group chat invite from %s failed: too many windows are open.", nick); snprintf(msg, sizeof(msg), "* Group chat invite from %s failed: too many windows are open.", nick);
@ -456,10 +460,12 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
tox_get_status_message(m, friends[f].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); uint16_t s_len = tox_get_status_message(m, friends[f].num, statusmsg,
friends[f].statusmsg_len = tox_get_status_message_size(m, f); TOX_MAX_STATUSMESSAGE_LENGTH);
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
friends[f].statusmsg_len = s_len;
friends[f].statusmsg[s_len] = '\0';
snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg); snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg);
} }
@ -538,8 +544,10 @@ static void friendlist_onAv(ToxWindow *self, ToxAv *av)
friends[id].chatwin = add_window(m, new_chat(m, friends[id].num)); friends[id].chatwin = add_window(m, new_chat(m, friends[id].num));
} else { } else {
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, id, nick); uint16_t n_len = tox_get_name(m, id, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH);
nick[n_len] = '\0';
uint8_t msg[MAX_STR_SIZE]; uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "Audio action from: %s!", nick); snprintf(msg, sizeof(msg), "Audio action from: %s!", nick);

View File

@ -114,7 +114,8 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
snprintf(msg, sizeof(msg), "%s", temp); snprintf(msg, sizeof(msg), "%s", temp);
} else { } else {
uint8_t selfname[TOX_MAX_NAME_LENGTH]; uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname); uint16_t n_len = tox_get_self_name(m, selfname);
selfname[n_len] = '\0';
snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname); snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname);
} }
@ -147,7 +148,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
id[i] = toupper(id[i]); id[i] = toupper(id[i]);
} }
int32_t f_num = tox_add_friend(m, id_bin, msg, strlen(msg) + 1); int32_t f_num = tox_add_friend(m, id_bin, msg, strlen(msg));
switch (f_num) { switch (f_num) {
case TOX_FAERR_TOOLONG: case TOX_FAERR_TOOLONG:
@ -334,13 +335,13 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
return; return;
} }
if (len >= TOXIC_MAX_NAME_LENGTH) { if (len >= TOXIC_MAX_NAME_LENGTH)
len = TOXIC_MAX_NAME_LENGTH; len = TOXIC_MAX_NAME_LENGTH;
nick[len-1] = L'\0';
}
tox_set_name(m, nick, len+1); nick[len] = L'\0';
prompt_update_nick(prompt, nick, len+1);
tox_set_name(m, nick, len);
prompt_update_nick(prompt, nick, len);
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
} }
@ -364,9 +365,8 @@ void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
} }
msg[strlen(++msg)-1] = L'\0'; msg[strlen(++msg)-1] = L'\0';
uint16_t len = strlen(msg) + 1; uint16_t len = strlen(msg);
tox_set_status_message(m, msg, len); tox_set_status_message(m, msg, len);
prompt_update_statusmessage(prompt, msg, len); prompt_update_statusmessage(prompt, msg, len);
} }
@ -468,7 +468,7 @@ void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
if (msg != NULL) { if (msg != NULL) {
msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */ msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */
uint16_t len = strlen(msg) + 1; uint16_t len = strlen(msg);
tox_set_status_message(m, msg, len); tox_set_status_message(m, msg, len);
prompt_update_statusmessage(prompt, msg, len); prompt_update_statusmessage(prompt, msg, len);
} }

View File

@ -57,6 +57,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
groupchats[i].num_peers = 0; groupchats[i].num_peers = 0;
groupchats[i].peer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); groupchats[i].peer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH);
groupchats[i].oldpeer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); groupchats[i].oldpeer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH);
groupchats[i].peer_name_lengths = malloc(sizeof(uint16_t));
/* temp fix */ /* temp fix */
memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME)); memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME));
@ -93,6 +94,7 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum)
free(groupchats[groupnum].peer_names); free(groupchats[groupnum].peer_names);
free(groupchats[groupnum].oldpeer_names); free(groupchats[groupnum].oldpeer_names);
free(groupchats[groupnum].peer_name_lengths);
memset(&groupchats[groupnum], 0, sizeof(GroupChat)); memset(&groupchats[groupnum], 0, sizeof(GroupChat));
int i; int i;
@ -152,18 +154,24 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int
if (self->num != groupnum) if (self->num != groupnum)
return; return;
msg[len] = '\0';
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_group_peername(m, groupnum, peernum, nick); uint16_t n_len = tox_group_peername(m, groupnum, peernum, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0'; /* enforce client max name length */
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH); /* enforce client max name length */
nick[n_len] = '\0';
/* check if message contains own name and alert appropriately */ /* check if message contains own name and alert appropriately */
int alert_type = WINDOW_ALERT_1; int alert_type = WINDOW_ALERT_1;
bool beep = false; bool beep = false;
uint8_t selfnick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t selfnick[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfnick); n_len = tox_get_self_name(m, selfnick);
selfnick[n_len] = '\0';
int nick_clr = strcmp(nick, selfnick) == 0 ? GREEN : CYAN; int nick_clr = strcmp(nick, selfnick) == 0 ? GREEN : CYAN;
bool nick_match = strcasestr(msg, selfnick) && strncmp(selfnick, nick, TOXIC_MAX_NAME_LENGTH); bool nick_match = strcasestr(msg, selfnick) && strncmp(selfnick, nick, TOXIC_MAX_NAME_LENGTH);
@ -189,14 +197,17 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p
if (self->num != groupnum) if (self->num != groupnum)
return; return;
action[len] = '\0';
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
/* check if message contains own name and alert appropriately */ /* check if message contains own name and alert appropriately */
int alert_type = WINDOW_ALERT_1; int alert_type = WINDOW_ALERT_1;
bool beep = false; bool beep = false;
uint8_t selfnick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t selfnick[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfnick); uint16_t n_len = tox_get_self_name(m, selfnick);
selfnick[n_len] = '\0';
bool nick_match = strcasestr(action, selfnick); bool nick_match = strcasestr(action, selfnick);
@ -208,8 +219,10 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p
alert_window(self, alert_type, beep); alert_window(self, alert_type, beep);
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_group_peername(m, groupnum, peernum, nick); n_len = tox_group_peername(m, groupnum, peernum, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0'; /* enforce client max name length */
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH);
nick[n_len] = '\0';
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt); get_time_str(timefrmt);
@ -218,36 +231,50 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p
write_to_log(action, nick, ctx->log, true); write_to_log(action, nick, ctx->log, true);
} }
/* Puts two copies of peerlist in chat instance */ /* Puts two copies of peerlist/lengths in chat instance */
static void copy_peernames(int gnum, int npeers, uint8_t tmp_peerlist[][TOX_MAX_NAME_LENGTH]) static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], int npeers)
{ {
/* Assumes these are initiated in init_groupchat_win */ /* Assumes these are initiated in init_groupchat_win */
free(groupchats[gnum].peer_names); free(groupchats[gnum].peer_names);
free(groupchats[gnum].oldpeer_names); free(groupchats[gnum].oldpeer_names);
free(groupchats[gnum].peer_name_lengths);
free(groupchats[gnum].oldpeer_name_lengths);
int N = TOX_MAX_NAME_LENGTH; int N = TOX_MAX_NAME_LENGTH;
groupchats[gnum].peer_names = malloc(sizeof(uint8_t) * npeers * N); groupchats[gnum].peer_names = malloc(sizeof(uint8_t) * npeers * N);
groupchats[gnum].oldpeer_names = malloc(sizeof(uint8_t) * npeers * N); groupchats[gnum].oldpeer_names = malloc(sizeof(uint8_t) * npeers * N);
groupchats[gnum].peer_name_lengths = malloc(sizeof(uint16_t) * npeers);
groupchats[gnum].oldpeer_name_lengths = malloc(sizeof(uint16_t) * npeers);
if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL) { if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL
|| groupchats[gnum].peer_name_lengths == NULL || groupchats[gnum].oldpeer_name_lengths == NULL) {
endwin(); endwin();
fprintf(stderr, "malloc() failed. Aborting...\n"); fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
uint16_t unknown_len = strlen(UNKNOWN_NAME);
int i; int i;
for (i = 0; i < npeers; ++i) { for (i = 0; i < npeers; ++i) {
if (string_is_empty(tmp_peerlist[i])) { if (string_is_empty(peerlist[i])) {
memcpy(&groupchats[gnum].peer_names[i*N], UNKNOWN_NAME, sizeof(UNKNOWN_NAME)); memcpy(&groupchats[gnum].peer_names[i*N], UNKNOWN_NAME, sizeof(UNKNOWN_NAME));
groupchats[gnum].peer_name_lengths[i] = unknown_len;
} else { } else {
memcpy(&groupchats[gnum].peer_names[i*N], tmp_peerlist[i], N); memcpy(&groupchats[gnum].peer_names[i*N], peerlist[i], N);
groupchats[gnum].peer_names[i*N+TOXIC_MAX_NAME_LENGTH] = '\0'; uint16_t n_len = lengths[i];
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH);
groupchats[gnum].peer_names[i*N+n_len] = '\0';
groupchats[gnum].peer_name_lengths[i] = n_len;
} }
} }
memcpy(groupchats[gnum].oldpeer_names, groupchats[gnum].peer_names, N*npeers); memcpy(groupchats[gnum].oldpeer_names, groupchats[gnum].peer_names, N*npeers);
memcpy(groupchats[gnum].oldpeer_name_lengths, groupchats[gnum].peer_name_lengths,
sizeof(uint16_t) * npeers);
} }
static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum, int peernum, static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum, int peernum,
@ -260,20 +287,26 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu
int num_peers = groupchats[groupnum].num_peers; int num_peers = groupchats[groupnum].num_peers;
/* get old peer name before updating name list */ /* get old peer name before updating name list */
uint8_t oldpeername[TOX_MAX_NAME_LENGTH] = {0}; uint8_t oldpeername[TOX_MAX_NAME_LENGTH];
if (change != TOX_CHAT_CHANGE_PEER_ADD) if (change != TOX_CHAT_CHANGE_PEER_ADD) {
memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum*TOX_MAX_NAME_LENGTH], memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum*TOX_MAX_NAME_LENGTH],
sizeof(oldpeername)); sizeof(oldpeername));
uint16_t old_n_len = groupchats[groupnum].oldpeer_name_lengths[peernum];
oldpeername[old_n_len] = '\0';
}
/* Update name lists */ /* Update name/len lists */
uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH]; uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH];
tox_group_get_names(m, groupnum, tmp_peerlist, num_peers); uint16_t tmp_peerlens[num_peers];
copy_peernames(groupnum, num_peers, tmp_peerlist); tox_group_get_names(m, groupnum, tmp_peerlist, tmp_peerlens, num_peers);
copy_peernames(groupnum, tmp_peerlist, tmp_peerlens, num_peers);
/* get current peername then sort namelist */ /* get current peername then sort namelist */
uint8_t peername[TOX_MAX_NAME_LENGTH] = {0}; uint8_t peername[TOX_MAX_NAME_LENGTH];
uint16_t n_len = groupchats[groupnum].peer_name_lengths[peernum];
memcpy(peername, &groupchats[groupnum].peer_names[peernum*TOX_MAX_NAME_LENGTH], sizeof(peername)); memcpy(peername, &groupchats[groupnum].peer_names[peernum*TOX_MAX_NAME_LENGTH], sizeof(peername));
peername[n_len] = '\0';
qsort(groupchats[groupnum].peer_names, groupchats[groupnum].num_peers, TOX_MAX_NAME_LENGTH, qsort_strcasecmp_hlpr); qsort(groupchats[groupnum].peer_names, groupchats[groupnum].num_peers, TOX_MAX_NAME_LENGTH, qsort_strcasecmp_hlpr);
@ -319,7 +352,7 @@ static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t
return; return;
} }
if (tox_group_action_send(m, self->num, action, strlen(action) + 1) == -1) { if (tox_group_action_send(m, self->num, action, strlen(action)) == -1) {
uint8_t *errmsg = " * Failed to send action."; uint8_t *errmsg = " * Failed to send action.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
} }
@ -517,7 +550,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE); execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE);
} }
} else if (!string_is_empty(line)) { } else if (!string_is_empty(line)) {
if (tox_group_message_send(m, self->num, line, strlen(line) + 1) == -1) { if (tox_group_message_send(m, self->num, line, strlen(line)) == -1) {
uint8_t *errmsg = " * Failed to send message."; uint8_t *errmsg = " * Failed to send message.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
} }

View File

@ -30,6 +30,8 @@ typedef struct {
int side_pos; /* current position of the sidebar - used for scrolling up and down */ int side_pos; /* current position of the sidebar - used for scrolling up and down */
uint8_t *peer_names; uint8_t *peer_names;
uint8_t *oldpeer_names; uint8_t *oldpeer_names;
uint16_t *peer_name_lengths;
uint16_t *oldpeer_name_lengths;
} GroupChat; } GroupChat;
void kill_groupchat_window(ToxWindow *self); void kill_groupchat_window(ToxWindow *self);

View File

@ -20,7 +20,7 @@
* *
*/ */
#define MAX_HISTORY 1000 #define MAX_HISTORY 100
enum { enum {
SYS_MSG, SYS_MSG,

View File

@ -157,13 +157,13 @@ static Tox *init_tox(int ipv4)
tox_callback_file_data(m, on_file_data, NULL); tox_callback_file_data(m, on_file_data, NULL);
#ifdef __linux__ #ifdef __linux__
tox_set_name(m, (uint8_t *) "Cool guy", sizeof("Cool guy")); tox_set_name(m, (uint8_t *) "Cool guy", strlen("Cool guy"));
#elif defined(_WIN32) #elif defined(_WIN32)
tox_set_name(m, (uint8_t *) "I should install GNU/Linux", sizeof("I should install GNU/Linux")); tox_set_name(m, (uint8_t *) "I should install GNU/Linux", strlen("I should install GNU/Linux"));
#elif defined(__APPLE__) #elif defined(__APPLE__)
tox_set_name(m, (uint8_t *) "Hipster", sizeof("Hipster")); /* This used to users of other Unixes are hipsters */ tox_set_name(m, (uint8_t *) "Hipster", strlen("Hipster")); /* This used to users of other Unixes are hipsters */
#else #else
tox_set_name(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4")); tox_set_name(m, (uint8_t *) "Registered Minix user #4", strlen("Registered Minix user #4"));
#endif #endif
return m; return m;

View File

@ -395,8 +395,8 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum
static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_t *data, uint16_t length) static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_t *data, uint16_t length)
{ {
/* make sure message data is null-terminated */ data[length] = '\0';
data[length - 1] = 0;
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
uint8_t timefrmt[TIME_STR_SIZE]; uint8_t timefrmt[TIME_STR_SIZE];
@ -431,27 +431,32 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
statusbar->status = TOX_USERSTATUS_NONE; statusbar->status = TOX_USERSTATUS_NONE;
statusbar->is_online = false; statusbar->is_online = false;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; uint8_t nick[TOX_MAX_NAME_LENGTH];
uint8_t statusmsg[MAX_STR_SIZE]; uint8_t statusmsg[MAX_STR_SIZE];
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
tox_get_self_name(m, nick); uint16_t n_len = tox_get_self_name(m, nick);
tox_get_self_status_message(m, statusmsg, MAX_STR_SIZE); uint16_t s_len = tox_get_self_status_message(m, statusmsg, MAX_STR_SIZE);
uint8_t status = tox_get_self_user_status(m); uint8_t status = tox_get_self_user_status(m);
pthread_mutex_unlock(&Winthread.lock); pthread_mutex_unlock(&Winthread.lock);
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); nick[n_len] = '\0';
statusmsg[s_len] = '\0';
/* load prev status message or show toxic version if it has never been set */ /* load prev status message or show toxic version if it has never been set */
uint8_t ver[strlen(TOXICVER) + 1]; uint8_t ver[strlen(TOXICVER) + 1];
strcpy(ver, TOXICVER); strcpy(ver, TOXICVER);
const uint8_t *toxic_ver = strtok(ver, "_"); const uint8_t *toxic_ver = strtok(ver, "_");
if ( (!strcmp("Online", statusmsg) || !strncmp("Toxing on Toxic", statusmsg, 15)) && toxic_ver != NULL) if ( (!strcmp("Online", statusmsg) || !strncmp("Toxing on Toxic", statusmsg, 15)) && toxic_ver != NULL) {
snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver); snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver);
s_len = strlen(statusmsg);
statusmsg[s_len] = '\0';
}
prompt_update_statusmessage(prompt, statusmsg, strlen(statusmsg) + 1); prompt_update_statusmessage(prompt, statusmsg, s_len);
prompt_update_status(prompt, status); prompt_update_status(prompt, status);
prompt_update_nick(prompt, nick, n_len);
/* Init statusbar subwindow */ /* Init statusbar subwindow */
statusbar->topline = subwin(self->window, 2, x2, 0, 0); statusbar->topline = subwin(self->window, 2, x2, 0, 0);