Compare commits
No commits in common. "b3924514861ab51f51117d93fd2e4e520f71f4ff" and "e34f92b0d804fdc3d5d14b157f27f55e31cd7c84" have entirely different histories.
b392451486
...
e34f92b0d8
@ -58,7 +58,6 @@ extern "C" {
|
||||
#define WHY2_CHAT_COMMAND_LIST "list" //LIST ALL CONNECTIONS
|
||||
#define WHY2_CHAT_COMMAND_DM "dm" //DIRECT (UNENCRYPTED) MESSAGES
|
||||
#define WHY2_CHAT_COMMAND_VERSION "version" //COMPARE CLIENT VERSION AND SERVER VERSION
|
||||
#define WHY2_CHAT_COMMAND_PM "pm" //PRIVATE (ENCRYPTED) MESSAGES
|
||||
|
||||
//SHORTCUTS CAUSE I'M LAZY BITCH
|
||||
#define WHY2_CHAT_CODE_SSQC WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION
|
||||
|
@ -26,7 +26,6 @@ extern "C" {
|
||||
#include <why2/flags.h> //TODO: fuck this
|
||||
|
||||
void why2_send_socket(char *text, char *username, int socket); //send socket.... wtf did you expect
|
||||
void why2_send_socket_code(char *params, char *username, int socket, char *code); //SEND SOCKET BUT WITH CODE
|
||||
void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD
|
||||
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
|
||||
void why2_clean_connections(void); //CLOSE EVERY CONNECTION
|
||||
|
@ -43,7 +43,7 @@ void exit_client(WHY2_UNUSED int i) //guess what
|
||||
if (exited) return;
|
||||
exited = 1;
|
||||
|
||||
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_EXIT);
|
||||
why2_send_socket(WHY2_CHAT_CODE_EXIT, NULL, listen_socket);
|
||||
}
|
||||
|
||||
why2_bool command(char *input, char *command, char **arg)
|
||||
@ -189,7 +189,6 @@ int main(void)
|
||||
|
||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_HELP "\t\tPrints out all the commands. :)\n"
|
||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_DM " <ID> <MSG>\tSends direct message to user.\n"
|
||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_PM " <ID> <MSG>\tSends encrypted message to user.\n" //TODO: Implement
|
||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_LIST "\t\tLists all users and their IDs.\n"
|
||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_VERSION "\tCheck server version.\n"
|
||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_EXIT "\t\tExits the program."
|
||||
@ -241,20 +240,20 @@ int main(void)
|
||||
}
|
||||
|
||||
//BUILD MESSAGE TO SEND TO SERVER
|
||||
char *final_message = why2_malloc(strlen(id) + strlen(msg) + 2);
|
||||
sprintf(final_message, "%s;%s%c", id, msg, '\0');
|
||||
char *final_message = why2_malloc(strlen(WHY2_CHAT_CODE_PM) + strlen(id) + strlen(msg) + 3);
|
||||
sprintf(final_message, WHY2_CHAT_CODE_PM ";%s;%s%c", id, msg, '\0');
|
||||
|
||||
why2_send_socket_code(final_message, NULL, listen_socket, WHY2_CHAT_CODE_PM); //SEND
|
||||
why2_send_socket(final_message, NULL, listen_socket); //SEND
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(id);
|
||||
why2_deallocate(final_message);
|
||||
} else if (command(line, WHY2_CHAT_COMMAND_LIST, &cmd_arg)) //LIST CMD
|
||||
{
|
||||
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_LIST);
|
||||
why2_send_socket(WHY2_CHAT_CODE_LIST, NULL, listen_socket);
|
||||
} else if (command(line, WHY2_CHAT_COMMAND_VERSION, &cmd_arg)) //VERSION CMD
|
||||
{
|
||||
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_VERSION);
|
||||
why2_send_socket(WHY2_CHAT_CODE_VERSION, NULL, listen_socket);
|
||||
} else
|
||||
{
|
||||
invalid("command");
|
||||
|
350
src/chat/misc.c
350
src/chat/misc.c
@ -388,41 +388,25 @@ void send_socket_deallocate(char *text, char *username, int socket) //SAME AS wh
|
||||
why2_toml_read_free(username);
|
||||
}
|
||||
|
||||
void send_socket_code_deallocate(char *params, char *username, int socket, char *code) //SAME AS send_socket_deallocate BUT WITH CODE FIELD
|
||||
void send_socket(char *text, char *username, int socket, why2_bool welcome)
|
||||
{
|
||||
why2_send_socket_code(params, username, socket, code);
|
||||
|
||||
why2_toml_read_free(username);
|
||||
}
|
||||
|
||||
void send_socket(char *text, char *username, int socket, why2_bool welcome, char *code)
|
||||
{
|
||||
//VARIABLES
|
||||
char *output = why2_strdup("");
|
||||
size_t length_buffer = strlen(text);
|
||||
char *text_copy = why2_calloc(length_buffer + 2, sizeof(char));
|
||||
struct json_object *json = json_tokener_parse("{}");
|
||||
|
||||
if (text != NULL)
|
||||
{
|
||||
size_t length_buffer = strlen(text);
|
||||
char *text_copy = why2_calloc(length_buffer + 2, sizeof(char));
|
||||
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
|
||||
if (text[length_buffer - 1] == '\n') length_buffer--;
|
||||
strncpy(text_copy, text, length_buffer);
|
||||
|
||||
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
|
||||
if (text[length_buffer - 1] == '\n') length_buffer--;
|
||||
strncpy(text_copy, text, length_buffer);
|
||||
//UNFUCK QUOTES FROM text_copy
|
||||
remove_json_syntax_characters(text_copy);
|
||||
|
||||
//UNFUCK QUOTES FROM text_copy
|
||||
remove_json_syntax_characters(text_copy);
|
||||
|
||||
//REMOVE NON ASCII CHARS
|
||||
remove_non_ascii(&text_copy);
|
||||
|
||||
//ADD OBJECTS
|
||||
json_object_object_add(json, "message", json_object_new_string(text_copy));
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(text_copy);
|
||||
}
|
||||
//REMOVE NON ASCII CHARS
|
||||
remove_non_ascii(&text_copy);
|
||||
|
||||
//ADD OBJECTS
|
||||
json_object_object_add(json, "message", json_object_new_string(text_copy));
|
||||
if (username != NULL) json_object_object_add(json, "username", json_object_new_string(username)); //WAS SENT FROM SERVER
|
||||
|
||||
if (welcome) //SENDING WELCOME MESSAGE TO USER
|
||||
@ -446,8 +430,6 @@ void send_socket(char *text, char *username, int socket, why2_bool welcome, char
|
||||
why2_toml_read_free(server_name);
|
||||
}
|
||||
|
||||
if (code != NULL) json_object_object_add(json, "code", json_object_new_string(code));
|
||||
|
||||
//GENERATE JSON STRING
|
||||
json_object_object_foreach(json, key, value)
|
||||
{
|
||||
@ -455,17 +437,19 @@ void send_socket(char *text, char *username, int socket, why2_bool welcome, char
|
||||
}
|
||||
add_brackets(&output);
|
||||
|
||||
why2_deallocate(text_copy);
|
||||
json_object_put(json);
|
||||
|
||||
//SEND
|
||||
send(socket, output, strlen(output), 0);
|
||||
|
||||
//DEALLOCATION
|
||||
json_object_put(json);
|
||||
why2_deallocate(output);
|
||||
}
|
||||
|
||||
void send_welcome_socket_deallocate(char *text, char *username, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
||||
{
|
||||
send_socket(NULL, username, socket, 1, text);
|
||||
send_socket(text, username, socket, 1);
|
||||
|
||||
why2_toml_read_free(username);
|
||||
}
|
||||
@ -501,12 +485,7 @@ unsigned long get_latest_id()
|
||||
//GLOBAL
|
||||
void why2_send_socket(char *text, char *username, int socket)
|
||||
{
|
||||
send_socket(text, username, socket, 0, NULL);
|
||||
}
|
||||
|
||||
void why2_send_socket_code(char *params, char *username, int socket, char *code)
|
||||
{
|
||||
send_socket(params, username, socket, 0, code);
|
||||
send_socket(text, username, socket, 0);
|
||||
}
|
||||
|
||||
void *why2_communicate_thread(void *arg)
|
||||
@ -527,7 +506,6 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_bool invalid_username = 1;
|
||||
why2_bool exiting = 0;
|
||||
char *decoded_buffer = NULL;
|
||||
char *decoded_code_buffer = NULL;
|
||||
char *username;
|
||||
int usernames_n = 0;
|
||||
struct json_object *json = json_tokener_parse("{}");
|
||||
@ -541,7 +519,7 @@ 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");
|
||||
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_PICK_USERNAME); //ASK USER FOR USERNAME
|
||||
send_socket_deallocate(WHY2_CHAT_CODE_PICK_USERNAME, why2_chat_server_config("server_username"), connection); //ASK USER FOR USERNAME
|
||||
|
||||
while (invalid_username)
|
||||
{
|
||||
@ -590,7 +568,7 @@ void *why2_communicate_thread(void *arg)
|
||||
|
||||
if (invalid_username)
|
||||
{
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_INVALID_USERNAME); //TELL THE USER THEY ARE DUMB AS FUCK
|
||||
send_socket_deallocate(WHY2_CHAT_CODE_INVALID_USERNAME, why2_chat_server_config("server_username"), connection); //TELL THE USER THEY ARE DUMB AS FUCK
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -600,7 +578,7 @@ void *why2_communicate_thread(void *arg)
|
||||
char *user_config_path = why2_get_server_users_path();
|
||||
if (!why2_toml_contains(user_config_path, decoded_buffer)) //REGISTRATION
|
||||
{
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||
send_socket_deallocate(WHY2_CHAT_CODE_ENTER_PASSWORD, why2_chat_server_config("server_username"), connection);
|
||||
|
||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
|
||||
{
|
||||
@ -613,7 +591,7 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_toml_write(user_config_path, username, password); //SAVE PASSWORD
|
||||
} else //LOGIN
|
||||
{
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||
send_socket_deallocate(WHY2_CHAT_CODE_ENTER_PASSWORD, why2_chat_server_config("server_username"), connection);
|
||||
|
||||
unsigned char max_tries = (unsigned char) server_config_int("max_password_tries");
|
||||
|
||||
@ -637,7 +615,7 @@ void *why2_communicate_thread(void *arg)
|
||||
goto deallocation;
|
||||
}
|
||||
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_INVALID_PASSWORD);
|
||||
send_socket_deallocate(WHY2_CHAT_CODE_INVALID_PASSWORD, why2_chat_server_config("server_username"), connection);
|
||||
}
|
||||
}
|
||||
|
||||
@ -701,141 +679,151 @@ void *why2_communicate_thread(void *arg)
|
||||
}
|
||||
|
||||
decoded_buffer = get_string_from_json_string(raw, "message"); //DECODE
|
||||
decoded_code_buffer = get_string_from_json_string(raw, "code"); //DECODE
|
||||
|
||||
//TRIM MESSAGE
|
||||
if (decoded_buffer != NULL) why2_trim_string(&decoded_buffer);
|
||||
why2_trim_string(&decoded_buffer);
|
||||
|
||||
if (decoded_code_buffer != NULL) //CODES FROM CLIENT
|
||||
if (decoded_buffer != NULL && strlen(decoded_buffer) != 0)
|
||||
{
|
||||
if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_EXIT) == 0) //USER REQUESTED EXIT
|
||||
if (strncmp(decoded_buffer, "code", 4) == 0) //CODES FROM CLIENT
|
||||
{
|
||||
exiting = 1;
|
||||
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_LIST) == 0) //USER REQUESTED LIST OF USERS
|
||||
{
|
||||
why2_node_t *head = connection_list.head;
|
||||
if (head == NULL) goto deallocation; //TODO: Send no users code
|
||||
|
||||
//BUFFERS
|
||||
why2_node_t *buffer = head;
|
||||
connection_node_t value_buffer;
|
||||
unsigned long alloc_size = 0;
|
||||
size_t last_size = 0;
|
||||
|
||||
//COUNT REQUIRED MESSAGE ALLOCATION SIZE
|
||||
do
|
||||
if (strcmp(decoded_buffer, WHY2_CHAT_CODE_EXIT) == 0) //USER REQUESTED EXIT
|
||||
{
|
||||
value_buffer = *(connection_node_t*) buffer -> value;
|
||||
alloc_size += strlen(value_buffer.username) + why2_count_int_length(value_buffer.id) + 2;
|
||||
|
||||
buffer = buffer -> next; //ITER
|
||||
} while (buffer != NULL);
|
||||
|
||||
char *message = why2_calloc(alloc_size + 1, sizeof(char));
|
||||
buffer = head; //RESET
|
||||
|
||||
//FILL THE message
|
||||
do
|
||||
exiting = 1;
|
||||
} else if (strcmp(decoded_buffer, WHY2_CHAT_CODE_LIST) == 0) //USER REQUESTED LIST OF USERS
|
||||
{
|
||||
value_buffer = *(connection_node_t*) buffer -> value;
|
||||
why2_node_t *head = connection_list.head;
|
||||
if (head == NULL) goto deallocation; //TODO: Send no users code
|
||||
|
||||
sprintf(message + last_size, "%s;%ld;", value_buffer.username, value_buffer.id); //APPEND
|
||||
//BUFFERS
|
||||
why2_node_t *buffer = head;
|
||||
connection_node_t value_buffer;
|
||||
unsigned long alloc_size = 0;
|
||||
char *append_buffer;
|
||||
|
||||
last_size = strlen(message);
|
||||
|
||||
buffer = buffer -> next; //ITER
|
||||
} while (buffer != NULL);
|
||||
|
||||
//SEND
|
||||
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_LIST_SERVER);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message);
|
||||
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_VERSION) == 0)
|
||||
{
|
||||
//GET VERSION STRING FROM THE VERSIONS JSON
|
||||
char *message = why2_malloc(strlen(WHY2_VERSION) + 1); //ALLOCATE MESSAGE FOR CLIENT
|
||||
|
||||
sprintf(message, "%s%c", WHY2_VERSION, '\0'); //CREATE THE MESSAGE
|
||||
|
||||
//SEND
|
||||
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_VERSION_SERVER);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message);
|
||||
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_PM) == 0 && decoded_buffer != NULL && strlen(decoded_buffer) != 0) //PM
|
||||
{
|
||||
char *id = NULL; //RECEIVER
|
||||
char *msg;
|
||||
|
||||
//CHECK ARGS VALIDITY
|
||||
why2_bool valid_param = strlen(decoded_buffer) >= 3;
|
||||
if (valid_param)
|
||||
{
|
||||
valid_param = 0;
|
||||
for (unsigned long i = 1; i < strlen(decoded_buffer); i++)
|
||||
//COUNT REQUIRED MESSAGE ALLOCATION SIZE
|
||||
do
|
||||
{
|
||||
if (decoded_buffer[i] == ';')
|
||||
value_buffer = *(connection_node_t*) buffer -> value;
|
||||
alloc_size += strlen(value_buffer.username) + why2_count_int_length(value_buffer.id) + 2;
|
||||
|
||||
buffer = buffer -> next; //ITER
|
||||
} while (buffer != NULL);
|
||||
|
||||
char *message = why2_calloc(strlen(WHY2_CHAT_CODE_LIST_SERVER) + alloc_size + 2, sizeof(char));
|
||||
buffer = head; //RESET
|
||||
|
||||
sprintf(message, WHY2_CHAT_CODE_LIST_SERVER); //SET CODE
|
||||
|
||||
//FILL THE message
|
||||
do
|
||||
{
|
||||
value_buffer = *(connection_node_t*) buffer -> value;
|
||||
|
||||
append_buffer = why2_malloc(strlen(value_buffer.username) + why2_count_int_length(value_buffer.id) + 3); //ALLOCATE THE BUFFER
|
||||
sprintf(append_buffer, ";%s;%ld", value_buffer.username, value_buffer.id); //FILL THE BUFFER
|
||||
|
||||
strcat(message, append_buffer); //JOIN THE BUFFER TO OUTPUT message
|
||||
|
||||
why2_deallocate(append_buffer); //DEALLOCATION
|
||||
buffer = buffer -> next; //ITER
|
||||
} while (buffer != NULL);
|
||||
|
||||
strcat(message, ";");
|
||||
|
||||
//SEND
|
||||
send_socket_deallocate(message, why2_chat_server_config("server_username"), connection);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message);
|
||||
} else if (strcmp(decoded_buffer, WHY2_CHAT_CODE_VERSION) == 0)
|
||||
{
|
||||
//GET VERSION STRING FROM THE VERSIONS JSON
|
||||
char *message = why2_malloc(strlen(WHY2_CHAT_CODE_VERSION_SERVER) + strlen(WHY2_VERSION) + 2); //ALLOCATE MESSAGE FOR CLIENT
|
||||
|
||||
sprintf(message, WHY2_CHAT_CODE_VERSION_SERVER ";%s%c", WHY2_VERSION, '\0'); //CREATE THE MESSAGE
|
||||
|
||||
//SEND
|
||||
send_socket_deallocate(message, why2_chat_server_config("server_username"), connection);
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(message);
|
||||
} else if (strncmp(decoded_buffer, WHY2_CHAT_CODE_PM, strlen(WHY2_CHAT_CODE_PM)) == 0) //PM
|
||||
{
|
||||
char *input = decoded_buffer + strlen(WHY2_CHAT_CODE_PM) + 1;
|
||||
|
||||
char *id = NULL; //RECEIVER
|
||||
char *msg;
|
||||
|
||||
//CHECK ARGS VALIDITY
|
||||
why2_bool valid_param = strlen(input) >= 3;
|
||||
if (valid_param)
|
||||
{
|
||||
valid_param = 0;
|
||||
for (unsigned long i = 1; i < strlen(input); i++)
|
||||
{
|
||||
valid_param = 1;
|
||||
|
||||
//EXTRACT FIRST ARG (ID)
|
||||
id = why2_malloc(i + 1);
|
||||
for (unsigned long j = 0; j < i; j++)
|
||||
if (input[i] == ';')
|
||||
{
|
||||
id[j] = decoded_buffer[j];
|
||||
}
|
||||
id[i] = '\0';
|
||||
valid_param = 1;
|
||||
|
||||
//EXTRACT MESSAGE
|
||||
msg = decoded_buffer + i + 1;
|
||||
break;
|
||||
//EXTRACT FIRST ARG (ID)
|
||||
id = why2_malloc(i + 1);
|
||||
for (unsigned long j = 0; j < i; j++)
|
||||
{
|
||||
id[j] = input[j];
|
||||
}
|
||||
id[i] = '\0';
|
||||
|
||||
//EXTRACT MESSAGE
|
||||
msg = input + i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//FIND CONNECTION
|
||||
why2_node_t *pm_connection = id == NULL ? NULL : find_connection_by_id(atoi(id));
|
||||
if (pm_connection == NULL) valid_param = 0;
|
||||
|
||||
if (valid_param) //IGNORE INVALID ARGS
|
||||
{
|
||||
connection_node_t pm_connection_node = *(connection_node_t*) pm_connection -> value;
|
||||
|
||||
//ALLOCATE MESSAGE TO SEND TO RECEIVER
|
||||
char *private_msg = why2_malloc(strlen(WHY2_CHAT_CODE_PM_SERVER) + strlen(node.username) + strlen(pm_connection_node.username) + strlen(msg) + 6);
|
||||
|
||||
//CONSTRUCT DA MESSAGE
|
||||
sprintf(private_msg, WHY2_CHAT_CODE_PM_SERVER ";%s;%s;%s;%c", node.username, pm_connection_node.username, msg, '\0');
|
||||
|
||||
//USER IS SENDING THE MESSAGE TO HIMSELF
|
||||
why2_bool self_pm = pm_connection_node.connection == connection;
|
||||
|
||||
//SEND YOU DUMB FUCK
|
||||
send_socket_deallocate(private_msg, why2_chat_server_config("server_username"), pm_connection_node.connection); //RECIPIENT
|
||||
if (!self_pm) send_socket_deallocate(private_msg, why2_chat_server_config("server_username"), connection); //AUTHOR
|
||||
|
||||
why2_deallocate(private_msg);
|
||||
}
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(id);
|
||||
}
|
||||
|
||||
//FIND CONNECTION
|
||||
why2_node_t *pm_connection = id == NULL ? NULL : find_connection_by_id(atoi(id));
|
||||
if (pm_connection == NULL) valid_param = 0;
|
||||
|
||||
if (valid_param) //IGNORE INVALID ARGS
|
||||
{
|
||||
connection_node_t pm_connection_node = *(connection_node_t*) pm_connection -> value;
|
||||
|
||||
//ALLOCATE MESSAGE TO SEND TO RECEIVER
|
||||
char *private_msg = why2_malloc(strlen(node.username) + strlen(pm_connection_node.username) + strlen(msg) + 5);
|
||||
|
||||
//CONSTRUCT DA MESSAGE
|
||||
sprintf(private_msg, "%s;%s;%s;%c", node.username, pm_connection_node.username, msg, '\0');
|
||||
|
||||
//USER IS SENDING THE MESSAGE TO HIMSELF
|
||||
why2_bool self_pm = pm_connection_node.connection == connection;
|
||||
|
||||
//SEND YOU DUMB FUCK
|
||||
send_socket_code_deallocate(private_msg, why2_chat_server_config("server_username"), pm_connection_node.connection, WHY2_CHAT_CODE_PM_SERVER); //RECIPIENT
|
||||
if (!self_pm) send_socket_code_deallocate(private_msg, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_PM_SERVER); //AUTHOR
|
||||
|
||||
why2_deallocate(private_msg);
|
||||
}
|
||||
|
||||
//DEALLOCATION
|
||||
why2_deallocate(id);
|
||||
}
|
||||
|
||||
//IGNORE INVALID CODES, THE USER JUST GOT THEIR LOBOTOMY DONE
|
||||
} else if (decoded_buffer != NULL && strlen(decoded_buffer) != 0 && strncmp(decoded_buffer, WHY2_CHAT_COMMAND_PREFIX, strlen(WHY2_CHAT_COMMAND_PREFIX)) != 0) //IGNORE MESSAGES BEGINNING WITH '!'
|
||||
{
|
||||
//REBUILD MESSAGE WITH USERNAME
|
||||
json_object_object_add(json, "message", json_object_new_string(decoded_buffer));
|
||||
json_object_object_add(json, "username", json_object_new_string(get_username(connection)));
|
||||
|
||||
json_object_object_foreach(json, key, value) //GENERATE JSON STRING
|
||||
//IGNORE INVALID CODES, THE USER JUST GOT THEIR LOBOTOMY DONE
|
||||
} else if (strncmp(decoded_buffer, WHY2_CHAT_COMMAND_PREFIX, strlen(WHY2_CHAT_COMMAND_PREFIX)) != 0) //IGNORE MESSAGES BEGINNING WITH '!'
|
||||
{
|
||||
append(&raw_output, key, (char*) json_object_get_string(value));
|
||||
}
|
||||
add_brackets(&raw_output);
|
||||
//REBUILD MESSAGE WITH USERNAME
|
||||
json_object_object_add(json, "message", json_object_new_string(decoded_buffer));
|
||||
json_object_object_add(json, "username", json_object_new_string(get_username(connection)));
|
||||
|
||||
send_to_all(raw_output); //e
|
||||
json_object_object_foreach(json, key, value) //GENERATE JSON STRING
|
||||
{
|
||||
append(&raw_output, key, (char*) json_object_get_string(value));
|
||||
}
|
||||
add_brackets(&raw_output);
|
||||
|
||||
send_to_all(raw_output); //e
|
||||
}
|
||||
}
|
||||
|
||||
deallocation:
|
||||
@ -845,11 +833,10 @@ void *why2_communicate_thread(void *arg)
|
||||
why2_deallocate(raw_ptr);
|
||||
why2_deallocate(raw_output);
|
||||
why2_deallocate(decoded_buffer);
|
||||
why2_deallocate(decoded_code_buffer);
|
||||
json_object_put(json);
|
||||
}
|
||||
|
||||
if (exiting) send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_SSQC);
|
||||
if (exiting) send_socket_deallocate(WHY2_CHAT_CODE_SSQC, why2_chat_server_config("server_username"), connection);
|
||||
|
||||
printf("User disconnected.\t%d\n", connection);
|
||||
|
||||
@ -896,7 +883,7 @@ void why2_clean_connections(void)
|
||||
|
||||
connection_buffer = *(connection_node_t*) node_buffer_2 -> value;
|
||||
|
||||
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection_buffer.connection, WHY2_CHAT_CODE_SSQC);
|
||||
send_socket_deallocate(WHY2_CHAT_CODE_SSQC, why2_chat_server_config("server_username"), connection_buffer.connection);
|
||||
|
||||
close(connection_buffer.connection);
|
||||
why2_list_remove(&connection_list, node_buffer_2); //REMOVE
|
||||
@ -934,7 +921,6 @@ void *why2_listen_server(void *socket)
|
||||
//CONTENT
|
||||
char *username = NULL;
|
||||
char *message = NULL;
|
||||
char *code = NULL;
|
||||
|
||||
//SERVER SETTINGS
|
||||
int max_uname = -1;
|
||||
@ -955,7 +941,6 @@ void *why2_listen_server(void *socket)
|
||||
//GET CONTENT
|
||||
username = get_string_from_json_string(read, "username");
|
||||
message = get_string_from_json_string(read, "message");
|
||||
code = get_string_from_json_string(read, "code");
|
||||
|
||||
if (server_uname == NULL) //GET SERVER USERNAME
|
||||
{
|
||||
@ -973,18 +958,18 @@ void *why2_listen_server(void *socket)
|
||||
continuing = 1;
|
||||
}
|
||||
|
||||
if ((strcmp(username, server_uname) == 0 && code != NULL) && !continuing) //CODE WAS SENT
|
||||
if ((strcmp(username, server_uname) == 0 && strncmp(message, "code", 4) == 0) && !continuing) //CODE WAS SENT
|
||||
{
|
||||
if (strcmp(code, WHY2_CHAT_CODE_SSQC) == 0) //SERVER BROKE UP WITH YOU
|
||||
if (strcmp(message, WHY2_CHAT_CODE_SSQC) == 0) //SERVER BROKE UP WITH YOU
|
||||
{
|
||||
printf("%s%s%s\nServer closed the connection.\n", asking_username > max_tries ? WHY2_CLEAR_AND_GO_UP : "", WHY2_CLEAR_AND_GO_UP WHY2_CLEAR_AND_GO_UP, (asking_username == 0 ? "\n": ""));
|
||||
fflush(stdout);
|
||||
|
||||
pthread_cancel(getline_thread); //CANCEL CLIENT getline
|
||||
exiting = 1; //EXIT THIS THREAD
|
||||
} else if (strcmp(code, WHY2_CHAT_CODE_PICK_USERNAME) == 0 || strcmp(code, WHY2_CHAT_CODE_INVALID_USERNAME) == 0) //PICK USERNAME (COULD BE CAUSE BY INVALID USERNAME)
|
||||
} else if (strcmp(message, WHY2_CHAT_CODE_PICK_USERNAME) == 0 || strcmp(message, WHY2_CHAT_CODE_INVALID_USERNAME) == 0) //PICK USERNAME (COULD BE CAUSE BY INVALID USERNAME)
|
||||
{
|
||||
if (strcmp(code, WHY2_CHAT_CODE_INVALID_USERNAME) == 0) //INVALID USERNAME
|
||||
if (strcmp(message, WHY2_CHAT_CODE_INVALID_USERNAME) == 0) //INVALID USERNAME
|
||||
{
|
||||
printf(WHY2_CLEAR_AND_GO_UP WHY2_CLEAR_AND_GO_UP "%s\nInvalid username!\n\n\n", asking_username > 1 ? WHY2_CLEAR_AND_GO_UP : "");
|
||||
fflush(stdout);
|
||||
@ -992,14 +977,14 @@ void *why2_listen_server(void *socket)
|
||||
|
||||
printf("%s%sEnter username (a-Z, 0-9; %d-%d characters):\n", asking_username++ > 0 ? WHY2_CLEAR_AND_GO_UP : "", WHY2_CLEAR_AND_GO_UP, min_uname, max_uname);
|
||||
fflush(stdout);
|
||||
} else if (strcmp(code, WHY2_CHAT_CODE_LIST_SERVER) == 0) //LIST USERS
|
||||
} else if (strncmp(message, WHY2_CHAT_CODE_LIST_SERVER, strlen(WHY2_CHAT_CODE_LIST_SERVER)) == 0) //LIST USERS
|
||||
{
|
||||
why2_bool printing_id = 0;
|
||||
|
||||
printf("\nList:\n-----\n");
|
||||
|
||||
//ITER TROUGH LIST OF USERS FROM SERVER
|
||||
for (unsigned long i = 0; i < strlen(message); i++)
|
||||
for (unsigned long i = strlen(WHY2_CHAT_CODE_LIST_SERVER) + 1; i < strlen(message); i++)
|
||||
{
|
||||
if (message[i] == ';')
|
||||
{
|
||||
@ -1012,20 +997,24 @@ void *why2_listen_server(void *socket)
|
||||
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
} else if (strcmp(code, WHY2_CHAT_CODE_VERSION_SERVER) == 0)
|
||||
} else if (strncmp(message, WHY2_CHAT_CODE_VERSION_SERVER, strlen(WHY2_CHAT_CODE_VERSION_SERVER)) == 0)
|
||||
{
|
||||
char *server_version = message + strlen(WHY2_CHAT_CODE_VERSION_SERVER) + 1;
|
||||
|
||||
//INFO
|
||||
printf("\nServer Version: %s\nClient Version: %s\n\n", message, WHY2_VERSION);
|
||||
printf("\nServer Version: %s\nClient Version: %s\n\n", server_version, WHY2_VERSION);
|
||||
|
||||
//SERVER IS OUTDATED
|
||||
if (atoi(message + 1) < atoi(WHY2_VERSION + 1))
|
||||
if (atoi(server_version + 1) < atoi(WHY2_VERSION + 1))
|
||||
{
|
||||
printf("Server is outdated. Some new features may not work correctly.\n\n");
|
||||
}
|
||||
} else if (strcmp(code, WHY2_CHAT_CODE_PM_SERVER) == 0)
|
||||
} else if (strncmp(message, WHY2_CHAT_CODE_PM_SERVER, strlen(WHY2_CHAT_CODE_PM_SERVER)) == 0)
|
||||
{
|
||||
printf(WHY2_CLEAR_AND_GO_UP WHY2_CLEAR_AND_GO_UP); //do not fucking ask me how the fucking formatting fucking works, i dont fucking know
|
||||
|
||||
char *received_pm = message + strlen(WHY2_CHAT_CODE_PM_SERVER) + 1;
|
||||
|
||||
//DECODED MESSAGE, AUTHOR AND RECIPIENT; 0 = AUTHOR, 1 = RECIPIENT, 2 = MESSAGE
|
||||
char **pm_info = why2_calloc(3, sizeof(char*));
|
||||
|
||||
@ -1033,9 +1022,9 @@ void *why2_listen_server(void *socket)
|
||||
unsigned long n_buffer = 0;
|
||||
|
||||
//DECODE
|
||||
for (unsigned long i = 0; i < strlen(message); i++)
|
||||
for (unsigned long i = 0; i < strlen(received_pm); i++)
|
||||
{
|
||||
if (message[i] == ';') //FUTURE ME, THIS IS PRETTY MUCH split FUNCTION IMPLEMENTATION
|
||||
if (received_pm[i] == ';') //FUTURE ME, THIS IS PRETTY MUCH split FUNCTION IMPLEMENTATION
|
||||
{
|
||||
//ALLOCATE INFO
|
||||
pm_info[n_buffer] = why2_malloc((i - i_buffer) + 1);
|
||||
@ -1043,7 +1032,7 @@ void *why2_listen_server(void *socket)
|
||||
//COPY INFO
|
||||
for (unsigned long j = i_buffer; j < i; j++)
|
||||
{
|
||||
pm_info[n_buffer][j - i_buffer] = message[j];
|
||||
pm_info[n_buffer][j - i_buffer] = received_pm[j];
|
||||
}
|
||||
pm_info[n_buffer][i - i_buffer] = '\0';
|
||||
|
||||
@ -1067,11 +1056,11 @@ void *why2_listen_server(void *socket)
|
||||
why2_deallocate(pm_info[i]);
|
||||
}
|
||||
why2_deallocate(pm_info);
|
||||
} else if (strcmp(code, WHY2_CHAT_CODE_ENTER_PASSWORD) == 0 || strcmp(code, WHY2_CHAT_CODE_INVALID_PASSWORD) == 0) //PICK USERNAME (COULD BE CAUSE BY INVALID USERNAME)
|
||||
} else if (strcmp(message, WHY2_CHAT_CODE_ENTER_PASSWORD) == 0 || strcmp(message, WHY2_CHAT_CODE_INVALID_PASSWORD) == 0) //PICK USERNAME (COULD BE CAUSE BY INVALID USERNAME)
|
||||
{
|
||||
__why2_set_asking_password(1);
|
||||
|
||||
if (strcmp(code, WHY2_CHAT_CODE_INVALID_PASSWORD) == 0) //INVALID USERNAME
|
||||
if (strcmp(message, WHY2_CHAT_CODE_INVALID_PASSWORD) == 0) //INVALID USERNAME
|
||||
{
|
||||
printf(WHY2_CLEAR_AND_GO_UP WHY2_CLEAR_AND_GO_UP "%s\nInvalid password!\n\n\n", asking_password > 1 ? WHY2_CLEAR_AND_GO_UP : "");
|
||||
fflush(stdout);
|
||||
@ -1105,7 +1094,6 @@ void *why2_listen_server(void *socket)
|
||||
why2_deallocate(read);
|
||||
why2_deallocate(username);
|
||||
why2_deallocate(message);
|
||||
why2_deallocate(code);
|
||||
}
|
||||
|
||||
why2_deallocate(server_uname);
|
||||
|
@ -166,8 +166,6 @@ void *why2_recalloc(void *pointer, unsigned long size, unsigned long block_size)
|
||||
|
||||
char *why2_strdup(char *string)
|
||||
{
|
||||
if (string == NULL) return NULL;
|
||||
|
||||
char *allocated = strdup(string);
|
||||
|
||||
push_to_list(allocated, ALLOCATION);
|
||||
|
Loading…
x
Reference in New Issue
Block a user