/*************************************************************************************************************
This file contains receiving datas function
2012/6 by Dlxko
**************************************************************************************************************/
#include "RecvData.h"

// Set buffer for next use
static char buffer[BUFFER_SIZE];

/********************* RECEIVING DATA **************************/

void function_receive_data()
{
	// Looks for every client
	for ( int j = 0; j < MAX_CLIENTS; j++ )
	{
		// Only if connected
		if ( client[j].connected )
		{
			// Try to get recived data
			if (function_receive_client ( &client[j], buffer, BUFFER_SIZE ) )
			{
				// Get lenght
				memset(data0, 0, LENGHT_SIZE+1);
				function_midcopy(buffer, data0, 0, LENGHT_SIZE);

				/***************************************** If packet type DEV ********************************************/
				if(buffer[5] == 'D' && buffer[6] == 'E' && buffer[7] == 'V')
				{
					// Friends name
					memset(data2, 0, BUFFER_SIZE);
					function_midcopy(buffer, data2, (OPCODE_SIZE+LENGHT_SIZE), (LENGHT_SIZE+OPCODE_SIZE+EVENT_ID_SIZE));

					// Mark as read
					memset(mysql_text, 0, MYSQL_TEXT_SIZE);
					sprintf_s(mysql_text, "DELETE FROM events WHERE id = %s", data2);
					mysql_return_value = function_mysql_returned(mysql_text);

					// Check returned value
					if(mysql_return_value != NO_RESULT_OR_SUCESS)
						function_show_warning("Can't remove event!");
				}

				/***************************************** If packet type NAR ********************************************/
				else if(buffer[5] == 'N' && buffer[6] == 'A' && buffer[7] == 'R')
				{
					// Friends name
					memset(data1, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, data1, (OPCODE_SIZE+LENGHT_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));

					// Insert into database new row with new friend state for this client
					memset(mysql_text, 0, MYSQL_TEXT_SIZE);
					sprintf_s(mysql_text, "INSERT INTO buddylist (client_name, friend_name) VALUES ('%s', '%s');", client[j].nick_name, data1);
					mysql_return_value = function_mysql_returned(mysql_text);

					// Check returned value
					if(mysql_return_value != NO_RESULT_OR_SUCESS)
						function_show_warning("Can't insert client into buddy list!");

					// Update avivable client for selected one
					int i = 0;
					do
					{
						if (client[i].nick_name )
						{
							if (strcmp(data1, client[i].nick_name) == 0)
							{
								if(client[i].connected && client[i].client_acess == 1)
								{
									function_budies_update(1, i);
									break;
								}
							}
						}
						i++;
					}
					while (i < MAX_CLIENTS);

					// Update avivable clients for this one
					function_budies_update(1, j);
				}

				/***************************************** If packet type NFR ********************************************/
				else if(buffer[5] == 'N' && buffer[6] == 'F' && buffer[7] == 'R')
				{
					// Friends name
					memset(data1, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, data1, (OPCODE_SIZE+LENGHT_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));

					// Make found falsed
					buddy_was_found = false;

					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|AFR|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << client[j].nick_name
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] wnt to add ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data1
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< std::endl;

					// Count lenght
					buffer_text_len = LENGHT_SIZE+CODE_SIZE+OPCODE_SIZE;
					memset(buffer_text_len_char, 0, LENGHT_SIZE);
					sprintf_s(buffer_text_len_char,LENGHT_SIZE, "%d", buffer_text_len);

					// Check if already exist in buddy list
					sprintf_s(mysql_text, "SELECT * FROM buddylist WHERE client_name = '%s' AND friend_name = '%s'", client[j].nick_name, data1);
					mysql_return_value = function_mysql_returned(mysql_text);

					if (mysql_return_value == MORE_ROWS_OR_SEARCHED)
					{
						memset(buffer_text, 0, BUFFER_SIZE);
						memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_NFR, OPCODE_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_AEX, CODE_SIZE);
						function_send_data (&client[j], buffer_text, buffer_text_len);
					}
					else
					{
						// Check if exists in accounts
						sprintf_s(mysql_text, "SELECT * FROM accounts WHERE nickname = '%s'", data1);
						mysql_return_value = function_mysql_returned(mysql_text);

						// If exist reply to client
						if(mysql_return_value == MORE_ROWS_OR_SEARCHED)
						{
							// Update text for server with "DX_OPCODE_NFR" and AOK code
							memset(buffer_text, 0, BUFFER_SIZE);
							memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_NFR, OPCODE_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_AOK, CODE_SIZE);
							function_send_data (&client[j], buffer_text, buffer_text_len);

							// Insert into database new row with new friend state for this client
							sprintf_s(mysql_text, "INSERT INTO buddylist (client_name, friend_name) VALUES ('%s', '%s');", client[j].nick_name, data1);
							mysql_return_value = function_mysql_returned(mysql_text);

							// Check returned value
							if(mysql_return_value != NO_RESULT_OR_SUCESS)
								function_show_warning("Can't insert buddy into buddy list case 2!");

							// Update avivable client for selected one
							int i = 0;
							do
							{
								if (client[i].nick_name )
								{
									if (strcmp(data1, client[i].nick_name) == 0)
									{
										if(client[i].connected && client[i].client_acess == 1)
										{
											function_budies_update(1, i);
											buddy_was_found = true;
											break;
										}
									}
								}
								i++;
							}
							while (i < MAX_CLIENTS);

							// Save into database
							memset(mysql_text, 0, MYSQL_TEXT_SIZE);
							sprintf_s(mysql_text, "INSERT INTO events (id, client_name, req_client, opcode) VALUES (NULL, '%s', '%s', 'NFR');", client[j].nick_name, data1);
							mysql_return_value = function_mysql_returned(mysql_text);

							// Check returned value
							if(mysql_return_value != NO_RESULT_OR_SUCESS)
								function_show_warning("Can't insert into events case 2!");

							// If found selected client update his undelivered events
							if (buddy_was_found == true)
								function_send_offline_messages(&client[i]);

							// Update avivable clients for this one
							function_budies_update(1, j);

						}
						// If client doesn't exist reply it
						else if (mysql_return_value == NO_ROWS_OR_NOTSEARCHED)
						{
							// Update text for server with "DX_OPCODE_NFR" and AWR code
							memset(buffer_text, 0, BUFFER_SIZE);
							memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_NFR, OPCODE_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_AWR, CODE_SIZE);
							function_send_data (&client[j], buffer_text, buffer_text_len);
						}
						// This is probably database error
						else
						{
							// Update text for server with "DX_OPCODE_REG" and ROK code
							memset(buffer_text, 0, BUFFER_SIZE);
							memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_NFR, OPCODE_SIZE);
							memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_AUK, CODE_SIZE);
							function_send_data (&client[j], buffer_text, buffer_text_len);
						}
					}
				}

				/***************************************** If packet type REG ********************************************/
				else if(buffer[5] == 'R' && buffer[6] == 'E' && buffer[7] == 'G')
				{
					// Get info
					memset(reg1, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg1, (LENGHT_SIZE+OPCODE_SIZE), (OPCODE_SIZE + DATA_ONE_SIZE+LENGHT_SIZE));
					memset(reg2, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg2, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*2)) );
					memset(reg3, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg3, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*2), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*3)) );
					memset(reg4, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg4, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*3), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*4)) );
					memset(reg5, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg5, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*4), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*5)) );
					memset(reg6, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg6, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*5), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*6)) );
					memset(reg7, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg7, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*6), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*7)) );
					memset(reg8, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, reg8, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*7), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*8)) );
					memset(img_reg, 0, 101);
					function_midcopy(buffer, img_reg, (LENGHT_SIZE+OPCODE_SIZE + DATA_ONE_SIZE*8), (LENGHT_SIZE+OPCODE_SIZE + (DATA_ONE_SIZE*8) + 100) );

					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|REG|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << reg3
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Wants to reg"
						<< std::endl;

					// Insert new client into database
					memset(mysql_text, 0, MYSQL_TEXT_SIZE);
					sprintf_s(mysql_text, "INSERT INTO accounts VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", reg1, reg2, reg3, reg4, reg5, reg6, reg7, reg8, img_reg);

					// Count lenght
					buffer_text_len = LENGHT_SIZE+CODE_SIZE+OPCODE_SIZE;
					memset(buffer_text_len_char, 0, LENGHT_SIZE);
					sprintf_s(buffer_text_len_char,LENGHT_SIZE, "%d", buffer_text_len);

					// Process mysql
					mysql_return_value = function_mysql_returned(mysql_text);

					// If everything ok
					if (mysql_return_value == NO_RESULT_OR_SUCESS)
					{
						// Update text for server with "DX_OPCODE_REG" and ROK code
						memset(buffer_text, 0, BUFFER_SIZE);
						memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_REG, OPCODE_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_ROK, CODE_SIZE);
						function_send_data (&client[j], buffer_text, buffer_text_len);
					}
					// If already exists
					else if (mysql_return_value == NOT_EXECUTET_QUERY)
					{
						// Update text for server with "DX_OPCODE_REG" and RWR code
						memset(buffer_text, 0, BUFFER_SIZE);
						memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_REG, OPCODE_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_RWR, CODE_SIZE);
						function_send_data (&client[j], buffer_text, buffer_text_len);
					}
					// If unknown error
					else
					{
						// Update text for server with "DX_OPCODE_REG" and RUK code
						memset(buffer_text, 0, BUFFER_SIZE);
						memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_REG, OPCODE_SIZE);
						memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_RUK, CODE_SIZE);
						function_send_data (&client[j], buffer_text, buffer_text_len);
					}
				}

				/***************************************** If packet type SNT ********************************************/
				else if(buffer[5] == 'S' && buffer[6] == 'N' && buffer[7] == 'T')
				{
					// Friends name
					memset(data1, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, data1, (OPCODE_SIZE+LENGHT_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));
					// Chat number
					memset(data2, 0, BUFFER_SIZE);
					function_midcopy(buffer, data2, (OPCODE_SIZE+LENGHT_SIZE+DATA_ONE_SIZE), (OPCODE_SIZE+LENGHT_SIZE+DATA_ONE_SIZE+MSG_NUMB_SIZE));

					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|SNT|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << client[j].nick_name
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Rcv4 ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data1
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Chat-Num ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data2
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< std::endl;

					// Count lenght
					buffer_text_len = LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE;
					memset(buffer_text_len_char, 0, LENGHT_SIZE);
					sprintf_s(buffer_text_len_char,LENGHT_SIZE, "%d", buffer_text_len);

					// Update text for server with "DX_OPCODE_SNT" and name datas
					memset(buffer_text,0,BUFFER_SIZE);
					memcpy(buffer_text, buffer_text_len_char, LENGHT_SIZE);
					memcpy(&buffer_text[LENGHT_SIZE], DX_OPCODE_SNT, OPCODE_SIZE);
					memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE], client[j].nick_name, strlen(client[j].nick_name));
					memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], data2, strlen(data2));

					// Get right buddy for sending
					int i = 0;
					do
					{
						if (client[i].nick_name )
						{
							if (strcmp(data1, client[i].nick_name) == 0)
							{
								if(client[i].connected && client[i].client_acess == 1)
								{
									// Send data
									function_send_data ( &client[i], buffer_text, buffer_text_len);
									break;
								}
							}
						}
						i++;
					}
					while (i < MAX_CLIENTS);
				}

				/***************************************** If packet type MSG ********************************************/
				else if(buffer[5] == 'M' && buffer[6] == 'S' && buffer[7] == 'G' && client[j].client_acess == 1)
				{
					// Friends name
					memset(data1, 0, DATA_ONE_SIZE);
					function_midcopy(buffer, data1, (OPCODE_SIZE+LENGHT_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));
					// Chat number
					memset(data2, 0, BUFFER_SIZE);
					function_midcopy(buffer, data2, (OPCODE_SIZE+LENGHT_SIZE+DATA_ONE_SIZE), (OPCODE_SIZE+LENGHT_SIZE+DATA_ONE_SIZE+MSG_NUMB_SIZE));
					// Message
					memset(data3, 0, BUFFER_SIZE);
					function_midcopy(buffer, data3, (OPCODE_SIZE+LENGHT_SIZE+DATA_ONE_SIZE+MSG_NUMB_SIZE), BUFFER_SIZE); //TODO BUFFER_SIZE to data0

					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|MSG|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] 4Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data1
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Chat-Num ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data2
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Msg ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data3
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< std::endl;

					// Message resending to other client
					// Compare only of client_name and is online and acessed
					// Also sets that buddy was found to default value
					int i = 0;
					buddy_was_found = false;

					do
					{
						if (client[i].nick_name )
						{
							if (strncmp(data1, client[i].nick_name, 9) == 0)
							{
								if(client[i].connected && client[i].client_acess == 1)
								{
									// Count lenght
									buffer_text_len = LENGHT_SIZE+DATA_ONE_SIZE+OPCODE_SIZE + strlen(data3) + MSG_NUMB_SIZE;
									memset(buffer_text_len_char, 0, LENGHT_SIZE);
									sprintf_s(buffer_text_len_char,LENGHT_SIZE, "%d", buffer_text_len);

									// Update text for server with "DX_OPCODE_IMS" and name datas
									memset(buffer_text,0,BUFFER_SIZE);
									memcpy(buffer_text,buffer_text_len_char,LENGHT_SIZE);
									memcpy(&buffer_text[LENGHT_SIZE],DX_OPCODE_IMS,OPCODE_SIZE);
									memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE],client[j].nick_name,strlen(client[j].nick_name));
									memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], data2, strlen(data2));
									memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+ MSG_NUMB_SIZE], data3, strlen(data3));

									// Send socket for sender and receiver and clean sock info
									function_send_data ( &client[i], buffer_text, buffer_text_len);

									// Get positions of clients in DB and then save the message to the Database
									memset(mysql_text, 0, MYSQL_TEXT_SIZE);
									sprintf_s(mysql_text, "SELECT * FROM  messages WHERE member_one = '%s'", client[j].nick_name);
									mysql_return_value = function_mysql_returned(mysql_text);

									// Check the result
									if(mysql_return_value == MORE_ROWS_OR_SEARCHED)
										sprintf_s(mysql_text, "INSERT INTO messages (member_one, member_two, message, time_stamp, member_one_received, member_two_received) VALUES ('%s', '%s', '%s', CURRENT_TIMESTAMP, 1, 1)", client[j].nick_name, client[i].nick_name, data3);
									else if (mysql_return_value == NO_ROWS_OR_NOTSEARCHED)
										sprintf_s(mysql_text, "INSERT INTO messages (member_one, member_two, message, time_stamp, member_one_received, member_two_received) VALUES ('%s', '%s', '%s', CURRENT_TIMESTAMP, 1, 1)", client[i].nick_name, client[j].nick_name, data3);
									else
									{
										std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|ERR|->>"
											<< color(FOREGROUND_RED|FOREGROUND_RED) << "Wrong ret.val after saving msg to DB ["
											<< color(FOREGROUND_GREEN|FOREGROUND_RED) << mysql_return_value
											<< color(FOREGROUND_RED|FOREGROUND_RED) << "]"
											<< std::endl;
									}

									// Execute mysql command
									mysql_return_value = function_mysql_returned(mysql_text);

									// Check returned value
									if(mysql_return_value != NO_RESULT_OR_SUCESS)
										function_show_warning("Can't save message");

									// Set that buddy was found
									buddy_was_found = true;
									break;
								}
							}
						}
						i++;
					}
					while (i < MAX_CLIENTS);

					// If buddy wasn't found insert to offline messages
					if (buddy_was_found == false)
					{
						memset(mysql_text, 0, MYSQL_TEXT_SIZE);
						sprintf_s(mysql_text, "INSERT INTO messages (member_one, member_two, message, time_stamp, member_one_received, member_two_received) VALUES ('%s', '%s', '%s', CURRENT_TIMESTAMP, 1, 0)", client[j].nick_name, data1, data3);
						mysql_return_value = function_mysql_returned(mysql_text);

						//Check returned value
						if(mysql_return_value != NO_RESULT_OR_SUCESS)
							function_show_warning("Can't save message case 2");
					}
				}

				/***************************************** If packet type OFL ********************************************/
				else if (buffer[5] == 'O' && buffer[6] == 'F' && buffer[7] == 'L')
				{
					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|OFL|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Req-OFF 4Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << client[j].nick_name
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< std::endl;

					// Disconnect client
					function_budies_update(3, j);
					function_disconnect_client(&client[j]);
				}

				/***************************************** If packet type FLT ********************************************/
				else if (buffer[5] == 'F' && buffer[6] == 'L' && buffer[7] == 'T')
				{
					// Get friend name
					memset(data1, 0, DATA_ONE_SIZE+1);
					function_midcopy(buffer, data1, (LENGHT_SIZE+OPCODE_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));
					// Get file name
					memset(data2, 0, BUFFER_SIZE);
					function_midcopy(buffer, data2, (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+FILE_NAME_SIZE));
					// Get url
					memset(data3, 0, BUFFER_SIZE);
					function_midcopy(buffer, data3, (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+FILE_NAME_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+FILE_NAME_SIZE+FILE_PATH_SIZE) );

					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|FLT|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] FilTrans 4Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data1
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] FilName ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data2
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< std::endl;

					int i = 0;
					do
					{
						if (client[i].nick_name )
						{
							if (strncmp(data1, client[i].nick_name, 9) == 0)
							{
								if(client[i].connected && client[i].client_acess == 1)
								{
									// Count lenght
									buffer_text_len = LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+FILE_NAME_SIZE + strlen(data3);
									memset(buffer_text_len_char, 0, LENGHT_SIZE);
									sprintf_s(buffer_text_len_char, LENGHT_SIZE, "%d", buffer_text_len);

									// Update text for server with "DX_OPCODE_IMS" and name datas
									memset(buffer_text,0,BUFFER_SIZE);
									memcpy(buffer_text,buffer_text_len_char,LENGHT_SIZE);
									memcpy(&buffer_text[LENGHT_SIZE],DX_OPCODE_FLT,OPCODE_SIZE);
									memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE],client[j].nick_name,strlen(client[j].nick_name));
									memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], data2, strlen(data2));
									memcpy(&buffer_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+FILE_NAME_SIZE], data3, strlen(data3));

									// Send socket for sender and receiver and clean sock info
									function_send_data ( &client[i], buffer_text, buffer_text_len);
								}
							}
						}
						i++;
					}
					while (i < MAX_CLIENTS);
				}

				/***************************************** If packet type AUC ********************************************/
				else if (buffer[5] == 'A' && buffer[6] == 'U' && buffer[7] == 'C' && client[j].client_acess != 1)
				{
					// Get name
					memset(data1, 0, DATA_ONE_SIZE+1);
					function_midcopy(buffer, data1, (LENGHT_SIZE+OPCODE_SIZE), (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));
					// Get password
					memset(data2, 0, BUFFER_SIZE);
					function_midcopy(buffer, data2, (LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE), (LENGHT_SIZE+OPCODE_SIZE+ DATA_ONE_SIZE+PASS_SIZE));

					// Check users in database
					memset(mysql_text, 0, MYSQL_TEXT_SIZE);
					sprintf_s(mysql_text, "SELECT * FROM  accounts WHERE nickname = '%s' AND password = '%s'", data1, data2);

					// Print message
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|AUC|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Want acs 4Cln ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data1
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "] Pass ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data2
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< std::endl;

					// If wrong dont acess and report
					if (function_mysql_returned(mysql_text) != MORE_ROWS_OR_SEARCHED)
					{
						// Count lenght and send data
						buffer2_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE;
						memset(buffer_text_len_char, 0, LENGHT_SIZE);
						sprintf_s(buffer2_text_len_char, LENGHT_SIZE, "%d", buffer2_text_len);

						// Update text for server with "DX_OPCODE_AUC" and name datas
						memset(buffer2_text, 0, BUFFER_SIZE);
						memcpy(buffer2_text, buffer2_text_len_char, LENGHT_SIZE);
						memcpy(&buffer2_text[LENGHT_SIZE], DX_OPCODE_AUC, OPCODE_SIZE);
						memcpy(&buffer2_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_AWR, CODE_SIZE);

						function_send_data ( &client[j], buffer2_text, buffer2_text_len );
					}
					else
					{
						// Count lenght and send data
						buffer2_text_len = LENGHT_SIZE + OPCODE_SIZE + CODE_SIZE;
						memset(buffer2_text_len_char, 0, LENGHT_SIZE);
						sprintf_s(buffer2_text_len_char, LENGHT_SIZE, "%d", buffer2_text_len);

						// Update text for server with "DX_OPCODE_AUC" and name datas
						memset(buffer2_text, 0, BUFFER_SIZE);
						memcpy(buffer2_text, buffer2_text_len_char, LENGHT_SIZE);
						memcpy(&buffer2_text[LENGHT_SIZE], DX_OPCODE_AUC, OPCODE_SIZE);
						memcpy(&buffer2_text[LENGHT_SIZE+OPCODE_SIZE], DX_CODE_AOK, CODE_SIZE);

						function_send_data ( &client[j], buffer2_text, buffer2_text_len );

						// Save it!
						client[j].client_acess = 1;
						strcpy_s(client[j].nick_name, 10, data1);

						// Update buddies
						function_budies_update(2, j);
						function_budies_update(1, j);
						function_send_offline_messages(&client[j]);
					}
				}

				/***************************************** If packet type unknown ********************************************/
				else
				{
					std::cout << color(FOREGROUND_RED|FOREGROUND_BLUE) << "|"
						<< buffer[5] << buffer[6] << buffer[7]  << "|->>"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "Pkg-Len ["
						<< color(FOREGROUND_GREEN|FOREGROUND_RED) << data0
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN) << "]"
						<< color(FOREGROUND_RED|FOREGROUND_RED) << " is unknown!!!"
						<< color(FOREGROUND_GREEN|FOREGROUND_GREEN)
						<< white << std::endl;
				}
			}
		}
	}
}