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

wrap tox_get_name in function that truncates name if it's too long

This commit is contained in:
Jfreegman 2014-07-08 14:31:59 -04:00
parent e74212cb9e
commit 442f68cd31
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
5 changed files with 29 additions and 40 deletions

View File

@ -156,9 +156,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int32_t num, const char *msg
ChatContext *ctx = self->chatwin;
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, num, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
get_nick_truncate(m, nick, num);
char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt));
@ -200,9 +198,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, const char *acti
ChatContext *ctx = self->chatwin;
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, num, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
get_nick_truncate(m, nick, num);
char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt));
@ -430,11 +426,10 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, co
if (self->num != friendnumber)
return;
char name[TOX_MAX_NAME_LENGTH];
char msg[MAX_STR_SIZE + TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, friendnumber, (uint8_t *) name);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
name[n_len] = '\0';
char name[TOX_MAX_NAME_LENGTH];
get_nick_truncate(m, name, friendnumber);
snprintf(msg, sizeof(msg), "%s has invited you to a group chat.", name);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
@ -895,9 +890,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
statusbar->statusmsg_len = s_len;
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, self->num, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
int n_len = get_nick_truncate(m, nick, self->num);
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
statusbar->nick_len = n_len;
@ -966,9 +959,7 @@ ToxWindow new_chat(Tox *m, int32_t friendnum)
#endif /* _SUPPORT_AUDIO */
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, friendnum, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
int n_len = get_nick_truncate(m, nick, friendnum);
chat_set_window_name(&ret, nick, n_len);
ChatContext *chatwin = calloc(1, sizeof(ChatContext));

View File

@ -106,9 +106,7 @@ static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, const cha
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else {
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, num, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
get_nick_truncate(m, nick, num);
char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt));
@ -183,15 +181,13 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort)
update_friend_last_online(i, tox_get_last_online(m, i));
char tempname[TOX_MAX_NAME_LENGTH] = {0};
int len = tox_get_name(m, num, (uint8_t *) tempname);
int len = get_nick_truncate(m, tempname, num);
if (len == -1 || tempname[0] == '\0') {
strcpy(friends[i].name, UNKNOWN_NAME);
friends[i].namelength = strlen(UNKNOWN_NAME);
} else { /* Enforce toxic's maximum name length */
len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
friends[i].namelength = len;
tempname[len] = '\0';
snprintf(friends[i].name, sizeof(friends[i].name), "%s", tempname);
}
@ -221,9 +217,7 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, num, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
get_nick_truncate(m, nick, num);
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "* File transfer from %s failed: too many windows are open.", nick);
@ -244,9 +238,7 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, const
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else {
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, num, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
get_nick_truncate(m, nick, num);
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "* Group chat invite from %s failed: too many windows are open.", nick);
@ -570,10 +562,7 @@ static void friendlist_onAv(ToxWindow *self, ToxAv *av, int call_index)
friends[id].chatwin = add_window(m, new_chat(m, friends[id].num));
} else {
char nick[TOX_MAX_NAME_LENGTH];
int n_len = tox_get_name(m, id, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
nick[n_len] = '\0';
get_nick_truncate(m, nick, friends[id].num);
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "Audio action from: %s!", nick);

View File

@ -224,3 +224,13 @@ void str_to_lower(char *str)
for (i = 0; str[i]; ++i)
str[i] = tolower(str[i]);
}
/* puts friendnum's nick in buf, truncating at TOXIC_MAX_NAME_LENGTH if necessary.
Returns nick len on success, -1 on failure */
int get_nick_truncate(Tox *m, char *buf, int friendnum)
{
int len = tox_get_name(m, friendnum, (uint8_t *) buf);
len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1);
buf[len] = '\0';
return len;
}

View File

@ -85,4 +85,8 @@ void get_file_name(char *namebuf, const char *pathname);
/* converts str to all lowercase */
void str_to_lower(char *str);
/* puts friendnum's nick in buf, truncating at TOXIC_MAX_NAME_LENGTH if necessary.
Returns nick len on success, -1 on failure */
int get_nick_truncate(Tox *m, char *buf, int friendnum);
#endif /* #define _misc_tools_h */

View File

@ -270,16 +270,11 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum
ChatContext *ctx = self->chatwin;
char nick[TOX_MAX_NAME_LENGTH] = {0};
int n_len = tox_get_name(m, friendnum, (uint8_t *) nick);
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1);
char nick[TOX_MAX_NAME_LENGTH] = {0}; /* stop removing this initiation */
get_nick_truncate(m, nick, friendnum);
if (!nick[0]) {
if (!nick[0])
snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME);
n_len = strlen(UNKNOWN_NAME);
}
nick[n_len] = '\0';
char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt));