|
|
@ -388,12 +388,23 @@ 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("");
|
|
|
|
|
|
|
|
struct json_object *json = json_tokener_parse("{}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (text != NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
size_t length_buffer = strlen(text);
|
|
|
|
size_t length_buffer = strlen(text);
|
|
|
|
char *text_copy = why2_calloc(length_buffer + 2, sizeof(char));
|
|
|
|
char *text_copy = why2_calloc(length_buffer + 2, sizeof(char));
|
|
|
|
struct json_object *json = json_tokener_parse("{}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
|
|
|
|
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
|
|
|
|
if (text[length_buffer - 1] == '\n') length_buffer--;
|
|
|
|
if (text[length_buffer - 1] == '\n') length_buffer--;
|
|
|
@ -407,6 +418,11 @@ void send_socket(char *text, char *username, int socket, why2_bool welcome)
|
|
|
|
|
|
|
|
|
|
|
|
//ADD OBJECTS
|
|
|
|
//ADD OBJECTS
|
|
|
|
json_object_object_add(json, "message", json_object_new_string(text_copy));
|
|
|
|
json_object_object_add(json, "message", json_object_new_string(text_copy));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
|
|
|
|
why2_deallocate(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,18 +701,17 @@ 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;
|
|
|
|
exiting = 1;
|
|
|
|
} else if (strcmp(decoded_buffer, WHY2_CHAT_CODE_LIST) == 0) //USER REQUESTED LIST OF USERS
|
|
|
|
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_LIST) == 0) //USER REQUESTED LIST OF USERS
|
|
|
|
{
|
|
|
|
{
|
|
|
|
why2_node_t *head = connection_list.head;
|
|
|
|
why2_node_t *head = connection_list.head;
|
|
|
|
if (head == NULL) goto deallocation; //TODO: Send no users code
|
|
|
|
if (head == NULL) goto deallocation; //TODO: Send no users code
|
|
|
@ -699,7 +720,7 @@ void *why2_communicate_thread(void *arg)
|
|
|
|
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
|
|
|
@ -710,59 +731,51 @@ void *why2_communicate_thread(void *arg)
|
|
|
|
buffer = buffer -> next; //ITER
|
|
|
|
buffer = buffer -> next; //ITER
|
|
|
|
} while (buffer != NULL);
|
|
|
|
} while (buffer != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
char *message = why2_calloc(strlen(WHY2_CHAT_CODE_LIST_SERVER) + alloc_size + 2, sizeof(char));
|
|
|
|
char *message = why2_calloc(alloc_size + 1, sizeof(char));
|
|
|
|
buffer = head; //RESET
|
|
|
|
buffer = head; //RESET
|
|
|
|
|
|
|
|
|
|
|
|
sprintf(message, WHY2_CHAT_CODE_LIST_SERVER); //SET CODE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//FILL THE message
|
|
|
|
//FILL THE message
|
|
|
|
do
|
|
|
|
do
|
|
|
|
{
|
|
|
|
{
|
|
|
|
value_buffer = *(connection_node_t*) buffer -> value;
|
|
|
|
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(message + last_size, "%s;%ld;", value_buffer.username, value_buffer.id); //APPEND
|
|
|
|
sprintf(append_buffer, ";%s;%ld", value_buffer.username, value_buffer.id); //FILL THE BUFFER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
strcat(message, append_buffer); //JOIN THE BUFFER TO OUTPUT message
|
|
|
|
last_size = strlen(message);
|
|
|
|
|
|
|
|
|
|
|
|
why2_deallocate(append_buffer); //DEALLOCATION
|
|
|
|
|
|
|
|
buffer = buffer -> next; //ITER
|
|
|
|
buffer = buffer -> next; //ITER
|
|
|
|
} while (buffer != NULL);
|
|
|
|
} while (buffer != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
strcat(message, ";");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//SEND
|
|
|
|
//SEND
|
|
|
|
send_socket_deallocate(message, why2_chat_server_config("server_username"), connection);
|
|
|
|
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_LIST_SERVER);
|
|
|
|
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
//DEALLOCATION
|
|
|
|
why2_deallocate(message);
|
|
|
|
why2_deallocate(message);
|
|
|
|
} else if (strcmp(decoded_buffer, WHY2_CHAT_CODE_VERSION) == 0)
|
|
|
|
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_VERSION) == 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//GET VERSION STRING FROM THE VERSIONS JSON
|
|
|
|
//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
|
|
|
|
char *message = why2_malloc(strlen(WHY2_VERSION) + 1); //ALLOCATE MESSAGE FOR CLIENT
|
|
|
|
|
|
|
|
|
|
|
|
sprintf(message, WHY2_CHAT_CODE_VERSION_SERVER ";%s%c", WHY2_VERSION, '\0'); //CREATE THE MESSAGE
|
|
|
|
sprintf(message, "%s%c", WHY2_VERSION, '\0'); //CREATE THE MESSAGE
|
|
|
|
|
|
|
|
|
|
|
|
//SEND
|
|
|
|
//SEND
|
|
|
|
send_socket_deallocate(message, why2_chat_server_config("server_username"), connection);
|
|
|
|
send_socket_code_deallocate(message, why2_chat_server_config("server_username"), connection, WHY2_CHAT_CODE_VERSION_SERVER);
|
|
|
|
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
//DEALLOCATION
|
|
|
|
why2_deallocate(message);
|
|
|
|
why2_deallocate(message);
|
|
|
|
} else if (strncmp(decoded_buffer, WHY2_CHAT_CODE_PM, strlen(WHY2_CHAT_CODE_PM)) == 0) //PM
|
|
|
|
} else if (strcmp(decoded_code_buffer, WHY2_CHAT_CODE_PM) == 0 && decoded_buffer != NULL && strlen(decoded_buffer) != 0) //PM
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char *input = decoded_buffer + strlen(WHY2_CHAT_CODE_PM) + 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *id = NULL; //RECEIVER
|
|
|
|
char *id = NULL; //RECEIVER
|
|
|
|
char *msg;
|
|
|
|
char *msg;
|
|
|
|
|
|
|
|
|
|
|
|
//CHECK ARGS VALIDITY
|
|
|
|
//CHECK ARGS VALIDITY
|
|
|
|
why2_bool valid_param = strlen(input) >= 3;
|
|
|
|
why2_bool valid_param = strlen(decoded_buffer) >= 3;
|
|
|
|
if (valid_param)
|
|
|
|
if (valid_param)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
valid_param = 0;
|
|
|
|
valid_param = 0;
|
|
|
|
for (unsigned long i = 1; i < strlen(input); i++)
|
|
|
|
for (unsigned long i = 1; i < strlen(decoded_buffer); i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (input[i] == ';')
|
|
|
|
if (decoded_buffer[i] == ';')
|
|
|
|
{
|
|
|
|
{
|
|
|
|
valid_param = 1;
|
|
|
|
valid_param = 1;
|
|
|
|
|
|
|
|
|
|
|
@ -770,12 +783,12 @@ void *why2_communicate_thread(void *arg)
|
|
|
|
id = why2_malloc(i + 1);
|
|
|
|
id = why2_malloc(i + 1);
|
|
|
|
for (unsigned long j = 0; j < i; j++)
|
|
|
|
for (unsigned long j = 0; j < i; j++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
id[j] = input[j];
|
|
|
|
id[j] = decoded_buffer[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id[i] = '\0';
|
|
|
|
id[i] = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
//EXTRACT MESSAGE
|
|
|
|
//EXTRACT MESSAGE
|
|
|
|
msg = input + i + 1;
|
|
|
|
msg = decoded_buffer + i + 1;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -790,17 +803,17 @@ void *why2_communicate_thread(void *arg)
|
|
|
|
connection_node_t pm_connection_node = *(connection_node_t*) pm_connection -> value;
|
|
|
|
connection_node_t pm_connection_node = *(connection_node_t*) pm_connection -> value;
|
|
|
|
|
|
|
|
|
|
|
|
//ALLOCATE MESSAGE TO SEND TO RECEIVER
|
|
|
|
//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);
|
|
|
|
char *private_msg = why2_malloc(strlen(node.username) + strlen(pm_connection_node.username) + strlen(msg) + 5);
|
|
|
|
|
|
|
|
|
|
|
|
//CONSTRUCT DA MESSAGE
|
|
|
|
//CONSTRUCT DA MESSAGE
|
|
|
|
sprintf(private_msg, WHY2_CHAT_CODE_PM_SERVER ";%s;%s;%s;%c", node.username, pm_connection_node.username, msg, '\0');
|
|
|
|
sprintf(private_msg, "%s;%s;%s;%c", node.username, pm_connection_node.username, msg, '\0');
|
|
|
|
|
|
|
|
|
|
|
|
//USER IS SENDING THE MESSAGE TO HIMSELF
|
|
|
|
//USER IS SENDING THE MESSAGE TO HIMSELF
|
|
|
|
why2_bool self_pm = pm_connection_node.connection == connection;
|
|
|
|
why2_bool self_pm = pm_connection_node.connection == connection;
|
|
|
|
|
|
|
|
|
|
|
|
//SEND YOU DUMB FUCK
|
|
|
|
//SEND YOU DUMB FUCK
|
|
|
|
send_socket_deallocate(private_msg, why2_chat_server_config("server_username"), pm_connection_node.connection); //RECIPIENT
|
|
|
|
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_deallocate(private_msg, why2_chat_server_config("server_username"), connection); //AUTHOR
|
|
|
|
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);
|
|
|
|
why2_deallocate(private_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -810,7 +823,7 @@ void *why2_communicate_thread(void *arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//IGNORE INVALID CODES, THE USER JUST GOT THEIR LOBOTOMY DONE
|
|
|
|
//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 '!'
|
|
|
|
} 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
|
|
|
|
//REBUILD MESSAGE WITH USERNAME
|
|
|
|
json_object_object_add(json, "message", json_object_new_string(decoded_buffer));
|
|
|
|
json_object_object_add(json, "message", json_object_new_string(decoded_buffer));
|
|
|
@ -824,7 +837,6 @@ void *why2_communicate_thread(void *arg)
|
|
|
|
|
|
|
|
|
|
|
|
send_to_all(raw_output); //e
|
|
|
|
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);
|
|
|
|