Compare commits
22 Commits
e34f92b0d8
...
b392451486
Author | SHA1 | Date | |
---|---|---|---|
b392451486 | |||
c0c0ad6894 | |||
0781fc4caf | |||
a3c54207db | |||
c120d38804 | |||
9ab4f50fc6 | |||
cf70fb0367 | |||
771e0de333 | |||
2394006d76 | |||
b96852606e | |||
06f5d434c1 | |||
e66109c262 | |||
1978ed4cd8 | |||
3b603f25e8 | |||
fa8560d51a | |||
4ca411f32b | |||
db032d3066 | |||
1f9df8363a | |||
2e63191085 | |||
ea018644f2 | |||
4649b7ccf6 | |||
61fdb17018 |
@ -58,6 +58,7 @@ extern "C" {
|
|||||||
#define WHY2_CHAT_COMMAND_LIST "list" //LIST ALL CONNECTIONS
|
#define WHY2_CHAT_COMMAND_LIST "list" //LIST ALL CONNECTIONS
|
||||||
#define WHY2_CHAT_COMMAND_DM "dm" //DIRECT (UNENCRYPTED) MESSAGES
|
#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_VERSION "version" //COMPARE CLIENT VERSION AND SERVER VERSION
|
||||||
|
#define WHY2_CHAT_COMMAND_PM "pm" //PRIVATE (ENCRYPTED) MESSAGES
|
||||||
|
|
||||||
//SHORTCUTS CAUSE I'M LAZY BITCH
|
//SHORTCUTS CAUSE I'M LAZY BITCH
|
||||||
#define WHY2_CHAT_CODE_SSQC WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION
|
#define WHY2_CHAT_CODE_SSQC WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION
|
||||||
|
@ -26,6 +26,7 @@ extern "C" {
|
|||||||
#include <why2/flags.h> //TODO: fuck this
|
#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(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_communicate_thread(void *arg); //COMMUNICATION THREAD
|
||||||
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
|
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
|
||||||
void why2_clean_connections(void); //CLOSE EVERY CONNECTION
|
void why2_clean_connections(void); //CLOSE EVERY CONNECTION
|
||||||
|
@ -43,7 +43,7 @@ void exit_client(WHY2_UNUSED int i) //guess what
|
|||||||
if (exited) return;
|
if (exited) return;
|
||||||
exited = 1;
|
exited = 1;
|
||||||
|
|
||||||
why2_send_socket(WHY2_CHAT_CODE_EXIT, NULL, listen_socket);
|
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_EXIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
why2_bool command(char *input, char *command, char **arg)
|
why2_bool command(char *input, char *command, char **arg)
|
||||||
@ -189,6 +189,7 @@ 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_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_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_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_VERSION "\tCheck server version.\n"
|
||||||
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_EXIT "\t\tExits the program."
|
WHY2_CHAT_COMMAND_PREFIX WHY2_CHAT_COMMAND_EXIT "\t\tExits the program."
|
||||||
@ -240,20 +241,20 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//BUILD MESSAGE TO SEND TO SERVER
|
//BUILD MESSAGE TO SEND TO SERVER
|
||||||
char *final_message = why2_malloc(strlen(WHY2_CHAT_CODE_PM) + strlen(id) + strlen(msg) + 3);
|
char *final_message = why2_malloc(strlen(id) + strlen(msg) + 2);
|
||||||
sprintf(final_message, WHY2_CHAT_CODE_PM ";%s;%s%c", id, msg, '\0');
|
sprintf(final_message, "%s;%s%c", id, msg, '\0');
|
||||||
|
|
||||||
why2_send_socket(final_message, NULL, listen_socket); //SEND
|
why2_send_socket_code(final_message, NULL, listen_socket, WHY2_CHAT_CODE_PM); //SEND
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
why2_deallocate(id);
|
why2_deallocate(id);
|
||||||
why2_deallocate(final_message);
|
why2_deallocate(final_message);
|
||||||
} else if (command(line, WHY2_CHAT_COMMAND_LIST, &cmd_arg)) //LIST CMD
|
} else if (command(line, WHY2_CHAT_COMMAND_LIST, &cmd_arg)) //LIST CMD
|
||||||
{
|
{
|
||||||
why2_send_socket(WHY2_CHAT_CODE_LIST, NULL, listen_socket);
|
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_LIST);
|
||||||
} else if (command(line, WHY2_CHAT_COMMAND_VERSION, &cmd_arg)) //VERSION CMD
|
} else if (command(line, WHY2_CHAT_COMMAND_VERSION, &cmd_arg)) //VERSION CMD
|
||||||
{
|
{
|
||||||
why2_send_socket(WHY2_CHAT_CODE_VERSION, NULL, listen_socket);
|
why2_send_socket_code(NULL, NULL, listen_socket, WHY2_CHAT_CODE_VERSION);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
invalid("command");
|
invalid("command");
|
||||||
|
350
src/chat/misc.c
350
src/chat/misc.c
@ -388,25 +388,41 @@ void send_socket_deallocate(char *text, char *username, int socket) //SAME AS wh
|
|||||||
why2_toml_read_free(username);
|
why2_toml_read_free(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_socket(char *text, char *username, int socket, why2_bool welcome)
|
void send_socket_code_deallocate(char *params, char *username, int socket, char *code) //SAME AS send_socket_deallocate BUT WITH CODE FIELD
|
||||||
{
|
{
|
||||||
|
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("");
|
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("{}");
|
struct json_object *json = json_tokener_parse("{}");
|
||||||
|
|
||||||
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
|
if (text != NULL)
|
||||||
if (text[length_buffer - 1] == '\n') length_buffer--;
|
{
|
||||||
strncpy(text_copy, text, length_buffer);
|
size_t length_buffer = strlen(text);
|
||||||
|
char *text_copy = why2_calloc(length_buffer + 2, sizeof(char));
|
||||||
|
|
||||||
//UNFUCK QUOTES FROM text_copy
|
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
|
||||||
remove_json_syntax_characters(text_copy);
|
if (text[length_buffer - 1] == '\n') length_buffer--;
|
||||||
|
strncpy(text_copy, text, length_buffer);
|
||||||
|
|
||||||
//REMOVE NON ASCII CHARS
|
//UNFUCK QUOTES FROM text_copy
|
||||||
remove_non_ascii(&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);
|
||||||
|
}
|
||||||
|
|
||||||
//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 (username != NULL) json_object_object_add(json, "username", json_object_new_string(username)); //WAS SENT FROM SERVER
|
||||||
|
|
||||||
if (welcome) //SENDING WELCOME MESSAGE TO USER
|
if (welcome) //SENDING WELCOME MESSAGE TO USER
|
||||||
@ -430,6 +446,8 @@ void send_socket(char *text, char *username, int socket, why2_bool welcome)
|
|||||||
why2_toml_read_free(server_name);
|
why2_toml_read_free(server_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (code != NULL) json_object_object_add(json, "code", json_object_new_string(code));
|
||||||
|
|
||||||
//GENERATE JSON STRING
|
//GENERATE JSON STRING
|
||||||
json_object_object_foreach(json, key, value)
|
json_object_object_foreach(json, key, value)
|
||||||
{
|
{
|
||||||
@ -437,19 +455,17 @@ void send_socket(char *text, char *username, int socket, why2_bool welcome)
|
|||||||
}
|
}
|
||||||
add_brackets(&output);
|
add_brackets(&output);
|
||||||
|
|
||||||
why2_deallocate(text_copy);
|
|
||||||
json_object_put(json);
|
|
||||||
|
|
||||||
//SEND
|
//SEND
|
||||||
send(socket, output, strlen(output), 0);
|
send(socket, output, strlen(output), 0);
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
|
json_object_put(json);
|
||||||
why2_deallocate(output);
|
why2_deallocate(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_welcome_socket_deallocate(char *text, char *username, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
void send_welcome_socket_deallocate(char *text, char *username, int socket) //SAME AS why2_send_socket BUT IT DEALLOCATES username
|
||||||
{
|
{
|
||||||
send_socket(text, username, socket, 1);
|
send_socket(NULL, username, socket, 1, text);
|
||||||
|
|
||||||
why2_toml_read_free(username);
|
why2_toml_read_free(username);
|
||||||
}
|
}
|
||||||
@ -485,7 +501,12 @@ unsigned long get_latest_id()
|
|||||||
//GLOBAL
|
//GLOBAL
|
||||||
void why2_send_socket(char *text, char *username, int socket)
|
void why2_send_socket(char *text, char *username, int socket)
|
||||||
{
|
{
|
||||||
send_socket(text, username, socket, 0);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *why2_communicate_thread(void *arg)
|
void *why2_communicate_thread(void *arg)
|
||||||
@ -506,6 +527,7 @@ void *why2_communicate_thread(void *arg)
|
|||||||
why2_bool invalid_username = 1;
|
why2_bool invalid_username = 1;
|
||||||
why2_bool exiting = 0;
|
why2_bool exiting = 0;
|
||||||
char *decoded_buffer = NULL;
|
char *decoded_buffer = NULL;
|
||||||
|
char *decoded_code_buffer = NULL;
|
||||||
char *username;
|
char *username;
|
||||||
int usernames_n = 0;
|
int usernames_n = 0;
|
||||||
struct json_object *json = json_tokener_parse("{}");
|
struct json_object *json = json_tokener_parse("{}");
|
||||||
@ -519,7 +541,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");
|
if (config_username == NULL) fprintf(stderr, "Your config doesn't contain 'user_pick_username'. Please update your configuration.\n");
|
||||||
|
|
||||||
send_socket_deallocate(WHY2_CHAT_CODE_PICK_USERNAME, why2_chat_server_config("server_username"), connection); //ASK USER FOR USERNAME
|
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_PICK_USERNAME); //ASK USER FOR USERNAME
|
||||||
|
|
||||||
while (invalid_username)
|
while (invalid_username)
|
||||||
{
|
{
|
||||||
@ -568,7 +590,7 @@ void *why2_communicate_thread(void *arg)
|
|||||||
|
|
||||||
if (invalid_username)
|
if (invalid_username)
|
||||||
{
|
{
|
||||||
send_socket_deallocate(WHY2_CHAT_CODE_INVALID_USERNAME, why2_chat_server_config("server_username"), connection); //TELL THE USER THEY ARE DUMB AS FUCK
|
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
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -578,7 +600,7 @@ void *why2_communicate_thread(void *arg)
|
|||||||
char *user_config_path = why2_get_server_users_path();
|
char *user_config_path = why2_get_server_users_path();
|
||||||
if (!why2_toml_contains(user_config_path, decoded_buffer)) //REGISTRATION
|
if (!why2_toml_contains(user_config_path, decoded_buffer)) //REGISTRATION
|
||||||
{
|
{
|
||||||
send_socket_deallocate(WHY2_CHAT_CODE_ENTER_PASSWORD, why2_chat_server_config("server_username"), connection);
|
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||||
|
|
||||||
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
|
if ((raw = read_user(connection, &raw_ptr)) == NULL) //READ
|
||||||
{
|
{
|
||||||
@ -591,7 +613,7 @@ void *why2_communicate_thread(void *arg)
|
|||||||
why2_toml_write(user_config_path, username, password); //SAVE PASSWORD
|
why2_toml_write(user_config_path, username, password); //SAVE PASSWORD
|
||||||
} else //LOGIN
|
} else //LOGIN
|
||||||
{
|
{
|
||||||
send_socket_deallocate(WHY2_CHAT_CODE_ENTER_PASSWORD, why2_chat_server_config("server_username"), connection);
|
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_ENTER_PASSWORD);
|
||||||
|
|
||||||
unsigned char max_tries = (unsigned char) server_config_int("max_password_tries");
|
unsigned char max_tries = (unsigned char) server_config_int("max_password_tries");
|
||||||
|
|
||||||
@ -615,7 +637,7 @@ void *why2_communicate_thread(void *arg)
|
|||||||
goto deallocation;
|
goto deallocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
send_socket_deallocate(WHY2_CHAT_CODE_INVALID_PASSWORD, why2_chat_server_config("server_username"), connection);
|
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_INVALID_PASSWORD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -679,151 +701,141 @@ void *why2_communicate_thread(void *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
decoded_buffer = get_string_from_json_string(raw, "message"); //DECODE
|
decoded_buffer = get_string_from_json_string(raw, "message"); //DECODE
|
||||||
|
decoded_code_buffer = get_string_from_json_string(raw, "code"); //DECODE
|
||||||
|
|
||||||
//TRIM MESSAGE
|
//TRIM MESSAGE
|
||||||
why2_trim_string(&decoded_buffer);
|
if (decoded_buffer != NULL) why2_trim_string(&decoded_buffer);
|
||||||
|
|
||||||
if (decoded_buffer != NULL && strlen(decoded_buffer) != 0)
|
if (decoded_code_buffer != NULL) //CODES FROM CLIENT
|
||||||
{
|
{
|
||||||
if (strncmp(decoded_buffer, "code", 4) == 0) //CODES FROM CLIENT
|
if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_EXIT) == 0) //USER REQUESTED EXIT
|
||||||
{
|
{
|
||||||
if (strcmp(decoded_buffer, WHY2_CHAT_CODE_EXIT) == 0) //USER REQUESTED EXIT
|
exiting = 1;
|
||||||
{
|
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_LIST) == 0) //USER REQUESTED LIST OF USERS
|
||||||
exiting = 1;
|
{
|
||||||
} else if (strcmp(decoded_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
|
||||||
why2_node_t *head = connection_list.head;
|
|
||||||
if (head == NULL) goto deallocation; //TODO: Send no users code
|
|
||||||
|
|
||||||
//BUFFERS
|
//BUFFERS
|
||||||
why2_node_t *buffer = head;
|
why2_node_t *buffer = head;
|
||||||
connection_node_t value_buffer;
|
connection_node_t value_buffer;
|
||||||
unsigned long alloc_size = 0;
|
unsigned long alloc_size = 0;
|
||||||
char *append_buffer;
|
size_t last_size = 0;
|
||||||
|
|
||||||
//COUNT REQUIRED MESSAGE ALLOCATION SIZE
|
//COUNT REQUIRED MESSAGE ALLOCATION SIZE
|
||||||
do
|
do
|
||||||
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
|
value_buffer = *(connection_node_t*) buffer -> value;
|
||||||
|
|
||||||
|
sprintf(message + last_size, "%s;%ld;", value_buffer.username, value_buffer.id); //APPEND
|
||||||
|
|
||||||
|
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++)
|
||||||
{
|
{
|
||||||
value_buffer = *(connection_node_t*) buffer -> value;
|
if (decoded_buffer[i] == ';')
|
||||||
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++)
|
|
||||||
{
|
{
|
||||||
if (input[i] == ';')
|
valid_param = 1;
|
||||||
|
|
||||||
|
//EXTRACT FIRST ARG (ID)
|
||||||
|
id = why2_malloc(i + 1);
|
||||||
|
for (unsigned long j = 0; j < i; j++)
|
||||||
{
|
{
|
||||||
valid_param = 1;
|
id[j] = decoded_buffer[j];
|
||||||
|
|
||||||
//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;
|
|
||||||
}
|
}
|
||||||
|
id[i] = '\0';
|
||||||
|
|
||||||
|
//EXTRACT MESSAGE
|
||||||
|
msg = decoded_buffer + 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//IGNORE INVALID CODES, THE USER JUST GOT THEIR LOBOTOMY DONE
|
//FIND CONNECTION
|
||||||
} else if (strncmp(decoded_buffer, WHY2_CHAT_COMMAND_PREFIX, strlen(WHY2_CHAT_COMMAND_PREFIX)) != 0) //IGNORE MESSAGES BEGINNING WITH '!'
|
why2_node_t *pm_connection = id == NULL ? NULL : find_connection_by_id(atoi(id));
|
||||||
{
|
if (pm_connection == NULL) valid_param = 0;
|
||||||
//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
|
if (valid_param) //IGNORE INVALID ARGS
|
||||||
{
|
{
|
||||||
append(&raw_output, key, (char*) json_object_get_string(value));
|
connection_node_t pm_connection_node = *(connection_node_t*) pm_connection -> value;
|
||||||
}
|
|
||||||
add_brackets(&raw_output);
|
|
||||||
|
|
||||||
send_to_all(raw_output); //e
|
//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
|
||||||
|
{
|
||||||
|
append(&raw_output, key, (char*) json_object_get_string(value));
|
||||||
|
}
|
||||||
|
add_brackets(&raw_output);
|
||||||
|
|
||||||
|
send_to_all(raw_output); //e
|
||||||
}
|
}
|
||||||
|
|
||||||
deallocation:
|
deallocation:
|
||||||
@ -833,10 +845,11 @@ void *why2_communicate_thread(void *arg)
|
|||||||
why2_deallocate(raw_ptr);
|
why2_deallocate(raw_ptr);
|
||||||
why2_deallocate(raw_output);
|
why2_deallocate(raw_output);
|
||||||
why2_deallocate(decoded_buffer);
|
why2_deallocate(decoded_buffer);
|
||||||
|
why2_deallocate(decoded_code_buffer);
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exiting) send_socket_deallocate(WHY2_CHAT_CODE_SSQC, why2_chat_server_config("server_username"), connection);
|
if (exiting) send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_SSQC);
|
||||||
|
|
||||||
printf("User disconnected.\t%d\n", connection);
|
printf("User disconnected.\t%d\n", connection);
|
||||||
|
|
||||||
@ -883,7 +896,7 @@ void why2_clean_connections(void)
|
|||||||
|
|
||||||
connection_buffer = *(connection_node_t*) node_buffer_2 -> value;
|
connection_buffer = *(connection_node_t*) node_buffer_2 -> value;
|
||||||
|
|
||||||
send_socket_deallocate(WHY2_CHAT_CODE_SSQC, why2_chat_server_config("server_username"), connection_buffer.connection);
|
send_socket_code_deallocate(NULL, why2_chat_server_config("server_username"), connection_buffer.connection, WHY2_CHAT_CODE_SSQC);
|
||||||
|
|
||||||
close(connection_buffer.connection);
|
close(connection_buffer.connection);
|
||||||
why2_list_remove(&connection_list, node_buffer_2); //REMOVE
|
why2_list_remove(&connection_list, node_buffer_2); //REMOVE
|
||||||
@ -921,6 +934,7 @@ void *why2_listen_server(void *socket)
|
|||||||
//CONTENT
|
//CONTENT
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
char *message = NULL;
|
char *message = NULL;
|
||||||
|
char *code = NULL;
|
||||||
|
|
||||||
//SERVER SETTINGS
|
//SERVER SETTINGS
|
||||||
int max_uname = -1;
|
int max_uname = -1;
|
||||||
@ -941,6 +955,7 @@ void *why2_listen_server(void *socket)
|
|||||||
//GET CONTENT
|
//GET CONTENT
|
||||||
username = get_string_from_json_string(read, "username");
|
username = get_string_from_json_string(read, "username");
|
||||||
message = get_string_from_json_string(read, "message");
|
message = get_string_from_json_string(read, "message");
|
||||||
|
code = get_string_from_json_string(read, "code");
|
||||||
|
|
||||||
if (server_uname == NULL) //GET SERVER USERNAME
|
if (server_uname == NULL) //GET SERVER USERNAME
|
||||||
{
|
{
|
||||||
@ -958,18 +973,18 @@ void *why2_listen_server(void *socket)
|
|||||||
continuing = 1;
|
continuing = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((strcmp(username, server_uname) == 0 && strncmp(message, "code", 4) == 0) && !continuing) //CODE WAS SENT
|
if ((strcmp(username, server_uname) == 0 && code != NULL) && !continuing) //CODE WAS SENT
|
||||||
{
|
{
|
||||||
if (strcmp(message, WHY2_CHAT_CODE_SSQC) == 0) //SERVER BROKE UP WITH YOU
|
if (strcmp(code, 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": ""));
|
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);
|
fflush(stdout);
|
||||||
|
|
||||||
pthread_cancel(getline_thread); //CANCEL CLIENT getline
|
pthread_cancel(getline_thread); //CANCEL CLIENT getline
|
||||||
exiting = 1; //EXIT THIS THREAD
|
exiting = 1; //EXIT THIS THREAD
|
||||||
} 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)
|
} 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)
|
||||||
{
|
{
|
||||||
if (strcmp(message, WHY2_CHAT_CODE_INVALID_USERNAME) == 0) //INVALID USERNAME
|
if (strcmp(code, 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 : "");
|
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);
|
fflush(stdout);
|
||||||
@ -977,14 +992,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);
|
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);
|
fflush(stdout);
|
||||||
} else if (strncmp(message, WHY2_CHAT_CODE_LIST_SERVER, strlen(WHY2_CHAT_CODE_LIST_SERVER)) == 0) //LIST USERS
|
} else if (strcmp(code, WHY2_CHAT_CODE_LIST_SERVER) == 0) //LIST USERS
|
||||||
{
|
{
|
||||||
why2_bool printing_id = 0;
|
why2_bool printing_id = 0;
|
||||||
|
|
||||||
printf("\nList:\n-----\n");
|
printf("\nList:\n-----\n");
|
||||||
|
|
||||||
//ITER TROUGH LIST OF USERS FROM SERVER
|
//ITER TROUGH LIST OF USERS FROM SERVER
|
||||||
for (unsigned long i = strlen(WHY2_CHAT_CODE_LIST_SERVER) + 1; i < strlen(message); i++)
|
for (unsigned long i = 0; i < strlen(message); i++)
|
||||||
{
|
{
|
||||||
if (message[i] == ';')
|
if (message[i] == ';')
|
||||||
{
|
{
|
||||||
@ -997,24 +1012,20 @@ void *why2_listen_server(void *socket)
|
|||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
} else if (strncmp(message, WHY2_CHAT_CODE_VERSION_SERVER, strlen(WHY2_CHAT_CODE_VERSION_SERVER)) == 0)
|
} else if (strcmp(code, WHY2_CHAT_CODE_VERSION_SERVER) == 0)
|
||||||
{
|
{
|
||||||
char *server_version = message + strlen(WHY2_CHAT_CODE_VERSION_SERVER) + 1;
|
|
||||||
|
|
||||||
//INFO
|
//INFO
|
||||||
printf("\nServer Version: %s\nClient Version: %s\n\n", server_version, WHY2_VERSION);
|
printf("\nServer Version: %s\nClient Version: %s\n\n", message, WHY2_VERSION);
|
||||||
|
|
||||||
//SERVER IS OUTDATED
|
//SERVER IS OUTDATED
|
||||||
if (atoi(server_version + 1) < atoi(WHY2_VERSION + 1))
|
if (atoi(message + 1) < atoi(WHY2_VERSION + 1))
|
||||||
{
|
{
|
||||||
printf("Server is outdated. Some new features may not work correctly.\n\n");
|
printf("Server is outdated. Some new features may not work correctly.\n\n");
|
||||||
}
|
}
|
||||||
} else if (strncmp(message, WHY2_CHAT_CODE_PM_SERVER, strlen(WHY2_CHAT_CODE_PM_SERVER)) == 0)
|
} else if (strcmp(code, 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
|
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
|
//DECODED MESSAGE, AUTHOR AND RECIPIENT; 0 = AUTHOR, 1 = RECIPIENT, 2 = MESSAGE
|
||||||
char **pm_info = why2_calloc(3, sizeof(char*));
|
char **pm_info = why2_calloc(3, sizeof(char*));
|
||||||
|
|
||||||
@ -1022,9 +1033,9 @@ void *why2_listen_server(void *socket)
|
|||||||
unsigned long n_buffer = 0;
|
unsigned long n_buffer = 0;
|
||||||
|
|
||||||
//DECODE
|
//DECODE
|
||||||
for (unsigned long i = 0; i < strlen(received_pm); i++)
|
for (unsigned long i = 0; i < strlen(message); i++)
|
||||||
{
|
{
|
||||||
if (received_pm[i] == ';') //FUTURE ME, THIS IS PRETTY MUCH split FUNCTION IMPLEMENTATION
|
if (message[i] == ';') //FUTURE ME, THIS IS PRETTY MUCH split FUNCTION IMPLEMENTATION
|
||||||
{
|
{
|
||||||
//ALLOCATE INFO
|
//ALLOCATE INFO
|
||||||
pm_info[n_buffer] = why2_malloc((i - i_buffer) + 1);
|
pm_info[n_buffer] = why2_malloc((i - i_buffer) + 1);
|
||||||
@ -1032,7 +1043,7 @@ void *why2_listen_server(void *socket)
|
|||||||
//COPY INFO
|
//COPY INFO
|
||||||
for (unsigned long j = i_buffer; j < i; j++)
|
for (unsigned long j = i_buffer; j < i; j++)
|
||||||
{
|
{
|
||||||
pm_info[n_buffer][j - i_buffer] = received_pm[j];
|
pm_info[n_buffer][j - i_buffer] = message[j];
|
||||||
}
|
}
|
||||||
pm_info[n_buffer][i - i_buffer] = '\0';
|
pm_info[n_buffer][i - i_buffer] = '\0';
|
||||||
|
|
||||||
@ -1056,11 +1067,11 @@ void *why2_listen_server(void *socket)
|
|||||||
why2_deallocate(pm_info[i]);
|
why2_deallocate(pm_info[i]);
|
||||||
}
|
}
|
||||||
why2_deallocate(pm_info);
|
why2_deallocate(pm_info);
|
||||||
} 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)
|
} 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)
|
||||||
{
|
{
|
||||||
__why2_set_asking_password(1);
|
__why2_set_asking_password(1);
|
||||||
|
|
||||||
if (strcmp(message, WHY2_CHAT_CODE_INVALID_PASSWORD) == 0) //INVALID USERNAME
|
if (strcmp(code, 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 : "");
|
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);
|
fflush(stdout);
|
||||||
@ -1094,6 +1105,7 @@ void *why2_listen_server(void *socket)
|
|||||||
why2_deallocate(read);
|
why2_deallocate(read);
|
||||||
why2_deallocate(username);
|
why2_deallocate(username);
|
||||||
why2_deallocate(message);
|
why2_deallocate(message);
|
||||||
|
why2_deallocate(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
why2_deallocate(server_uname);
|
why2_deallocate(server_uname);
|
||||||
|
@ -166,6 +166,8 @@ void *why2_recalloc(void *pointer, unsigned long size, unsigned long block_size)
|
|||||||
|
|
||||||
char *why2_strdup(char *string)
|
char *why2_strdup(char *string)
|
||||||
{
|
{
|
||||||
|
if (string == NULL) return NULL;
|
||||||
|
|
||||||
char *allocated = strdup(string);
|
char *allocated = strdup(string);
|
||||||
|
|
||||||
push_to_list(allocated, ALLOCATION);
|
push_to_list(allocated, ALLOCATION);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user