removed goto from username loop & implemented WHY2_MAX_USERNAME_TRIES

This commit is contained in:
Václav Šmejkal 2024-01-27 22:13:02 +01:00
parent 31db7c0622
commit b21d39a35e
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -379,10 +379,11 @@ void *why2_communicate_thread(void *arg)
char *raw; char *raw;
void *raw_ptr = NULL; void *raw_ptr = NULL;
why2_bool force_exiting = 0; why2_bool force_exiting = 0;
why2_bool invalid_username; why2_bool invalid_username = 1;
why2_bool exiting = 0; why2_bool exiting = 0;
char *decoded_buffer = NULL; char *decoded_buffer = NULL;
char *username = node.username; char *username = node.username;
int usernames_n = 0;
why2_deallocate(string_buffer); why2_deallocate(string_buffer);
@ -390,32 +391,37 @@ void *why2_communicate_thread(void *arg)
{ {
if (config_username == NULL) fprintf(stderr, "Your config doesn't contain 'user_pick_username'. Please update your configuration.\n"); if (config_username == NULL) fprintf(stderr, "Your config doesn't contain 'user_pick_username'. Please update your configuration.\n");
repeat_username_get: while (invalid_username)
invalid_username = 0;
why2_send_socket(WHY2_CHAT_CODE_PICK_USERNAME, WHY2_CHAT_SERVER_USERNAME, connection); //ASK USER FOR USERNAME
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
{ {
force_exiting = 1; //FAILURE why2_send_socket(WHY2_CHAT_CODE_PICK_USERNAME, WHY2_CHAT_SERVER_USERNAME, connection); //ASK USER FOR USERNAME
goto deallocation;
}
decoded_buffer = get_string_from_json_string(raw, "message"); //DECODE if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
{
force_exiting = 1; //FAILURE
goto deallocation;
}
invalid_username = (strlen(decoded_buffer) > WHY2_MAX_USERNAME_LENGTH) || !check_username(decoded_buffer); //CHECK FOR USERNAMES LONGER THAN 20 CHARACTERS decoded_buffer = get_string_from_json_string(raw, "message"); //DECODE
why2_deallocate(username); invalid_username = (strlen(decoded_buffer) > WHY2_MAX_USERNAME_LENGTH) || !check_username(decoded_buffer); //CHECK FOR USERNAMES LONGER THAN 20 CHARACTERS
username = decoded_buffer;
//DEALLOCATE STUFF HERE why2_deallocate(username);
why2_deallocate(raw); username = decoded_buffer;
why2_deallocate(raw_ptr);
if (invalid_username) //DEALLOCATE STUFF HERE
{ why2_deallocate(raw);
why2_send_socket(WHY2_CHAT_CODE_INVALID_USERNAME, WHY2_CHAT_SERVER_USERNAME, connection); //TELL THE USER HE IS DUMB AS FUCK why2_deallocate(raw_ptr);
goto repeat_username_get; //REPEAT
if (invalid_username)
{
why2_send_socket(WHY2_CHAT_CODE_INVALID_USERNAME, WHY2_CHAT_SERVER_USERNAME, connection); //TELL THE USER HE IS DUMB AS FUCK
}
if (++usernames_n == WHY2_MAX_USERNAME_TRIES) //ASKED CLIENT WAY TOO FUCKING MANY TIMES FOR USERNAME, KICK HIM
{
exiting = 1;
goto deallocation;
}
} }
} }