/*************************************************************************************************************
* This file contains packet process functions
* 2012/8 by Dlxko
* Version 3.0
**************************************************************************************************************/
#include "FxPacket.h"

// For info
static char info_buffer1[DATA_ONE_SIZE+1];
static char info_buffer2[DATA_ONE_SIZE+1];
static char info_buffer3[DATA_ONE_SIZE+1];
static char info_buffer4[DATA_ONE_SIZE+1];
static char info_buffer5[DATA_ONE_SIZE+1];
static char info_buffer6[DATA_ONE_SIZE+1];
static char info_buffer7[DATA_ONE_SIZE+1];

// Datas for incomming
static char data4[BUF_SIZE];
static char data3[BUF_SIZE];
static char data2[BUF_SIZE];
static char data1[DATA_ONE_SIZE+1];
static char data_packet[BUF_SIZE];

// Buddy number
static int buddy_number;

// GTK Buffer's variables
static GtkTextIter end_iter;
static GtkTextIter start_iter;
static GtkTextMark *endmark;
static gint line_count;

// Message buffer
static gchar *message;

// Counters
static int small_counter;
static int current_event;

// Packet size
static int packet_size;

// Event id convert
static int event_id_int;
static char *event_id_char;
static bool event_id_found;

// Testing avatar
static FILE *test_avatar;

// Additional function to process incomming message (file trans, incomming, incomming history)
bool additional_function_fxpacket_process_incomming_message(int function_type)
{
	// Get buddy number
	buddy_number = function_get_buddy_number(data1);

	// If empty buddy slot
	if (buddy_number == (MAX_BUDDIES + 1))
	{
		// Create new contact
		buddy_number = function_make_online(data1);
	}

	// If only hidden just show, else create buddy
	if(buddy[buddy_number].buddy_chat == true)
		if(buddy[buddy_number].window)
			gtk_widget_show(buddy[buddy_number].window);
		else
		{
			function_show_warning(LANG_XGET_BUDDY_CHAT_BUT_LIKE_CREATED);
			return false;
		}
	else
		create_buddy_chat(buddy_number);

	// Get time and inset into string, create char, copy from string to char
	std::string time = function_get_std_time();
	gchar *timemsg = new char[time.size()+1];
	timemsg[time.size()]=0;
	memcpy(timemsg,time.c_str(),time.size());

	// Create message with time if its necessary
	if (buddy[buddy_number].who_sender == 1 || buddy[buddy_number].who_sender == 3)
	{
		// Print message of time
		message = g_strconcat ("\n",timemsg, " - ", data1, NULL);
		gtk_text_buffer_get_end_iter (buddy[buddy_number].buffer, &end_iter);
		gtk_text_buffer_insert (buddy[buddy_number].buffer, &end_iter, message, -1);
		g_free (message);

		// Make it bold
		line_count = gtk_text_buffer_get_line_count (buddy[buddy_number].buffer);
		gtk_text_buffer_get_iter_at_line(buddy[buddy_number].buffer, &start_iter, line_count);
		gtk_text_buffer_get_end_iter (buddy[buddy_number].buffer, &end_iter);
		gtk_text_buffer_apply_tag_by_name (buddy[buddy_number].buffer, "bold", &start_iter, &end_iter);
	}

	// Print other datas depend if file transfer
	if (function_type == 1)
		message = g_strconcat ("\n", LANG_FILE_RECEIVING, data2, " ", NULL);
	else if (function_type == 0)
		message = g_strconcat ("\n", data3, " ", NULL);
	gtk_text_buffer_get_end_iter (buddy[buddy_number].buffer, &end_iter);
	gtk_text_buffer_insert (buddy[buddy_number].buffer, &end_iter, message, -1);
	g_free (message);

	// Set sender as you
	buddy[buddy_number].who_sender = false;

	//Convert smileys if no file transfer
	if(function_type == 0)
		function_get_smile(buddy[buddy_number].buffer);

	// Scroll to end
	gtk_text_buffer_get_end_iter (buddy[buddy_number].buffer, &end_iter);
	endmark = gtk_text_mark_new ("E2", 0);
	gtk_text_buffer_add_mark (buddy[buddy_number].buffer, endmark, &end_iter);
	gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (buddy[buddy_number].text_view), endmark, 0.0, FALSE, 0, 0);
	gtk_text_buffer_delete_mark (buddy[buddy_number].buffer, endmark);

	// Return true
	return true;
}

// Additional function to process friend datas
bool additional_function_fxpacket_get_friend_data()
{
	// Clean and get friend data
	memset(data1, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, data1, (OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE));
	if(strlen(data1) < 1)
	{
		function_show_warning(LANG_NO_FRIEND_DATA);
		return false;
	}
	return true;
}

// Function to process NFF packet
void packet_new_friend_request()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and id data
	memset(data2, 0, BUF_SIZE);
	function_midcopy(data_packet, data2, (OPCODE_SIZE+DATA_ONE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE+REQUESTS_ID_CHAR_SIZE));
	if(strlen(data2) < 1)
	{
		function_show_warning(LANG_NEW_FRIEND_REQ_PACKET_NO_IDDATA);
		return;
	}

	// Set to null
	event_id_int = 0;

	// Convert and get informations
	event_id_char = data2;
	event_id_int = atoi( event_id_char );

	// Look if already exist
	small_counter = 0;
	event_id_found = false;
	do
	{
		if(req_events[small_counter].id_integer == event_id_int)
		{
			event_id_found = true;
			break;
		}
		else ++small_counter;
	}
	while ( small_counter < MAX_REQUESTS);

	// If already created exit
	if (event_id_found == true)
		return;

	// Get any empty event slot
	small_counter = 0;
	do
	{
		if(req_events[small_counter].active == false)
			break;
		else ++small_counter;
	}
	while ( small_counter < MAX_REQUESTS);

	// Check if something was found
	if (small_counter == MAX_REQUESTS)
	{
		function_show_warning(LANG_MAX_COUNT_OF_EVENTS);
		return;
	}

	// Set current event
	current_event = small_counter;

	// Destroy previous button
	gtk_container_remove (GTK_CONTAINER(buddy_toolbar), GTK_WIDGET(buddy_events));

	// Create new button
	buddy_events = gtk_tool_button_new_from_stock(GTK_STOCK_YES);

	// Attach signal
	g_signal_connect (G_OBJECT(buddy_events), "clicked", G_CALLBACK (signal_show_requests), NULL);

	// Pack button toolbar
	gtk_toolbar_insert(GTK_TOOLBAR(buddy_toolbar), buddy_events, -1);
	gtk_widget_show(GTK_WIDGET(buddy_events));

	// Save new client name for event
	strcpy_s(req_events[current_event].nick_name, DATA_ONE_SIZE, data1);

	// Save event id integer
	req_events[current_event].id_integer = event_id_int;

	// Create message for label"<span foreground=\"#FF0066\"><b>"
	message = g_strconcat ("<b>", LANG_WANTS_TO_ADD_U_AS_FRIEND, "<span foreground=\"#FF0066\">", data1, "</span>", LANG_WANTS_TO_ADD_U_AS_FRIEND2, "</b>", NULL);

	// Create label
	req_events[current_event].label = gtk_label_new("Can't load message!");
	gtk_label_set_justify (GTK_LABEL (req_events[current_event].label), GTK_JUSTIFY_FILL);
	gtk_label_set_line_wrap (GTK_LABEL (req_events[current_event].label), FALSE);
	gtk_widget_set_size_request(req_events[current_event].label, 170, 25);
	gtk_label_set_markup(GTK_LABEL(req_events[current_event].label), message);

	// Create buttons with sizes
	req_events[current_event].ok_button = gtk_button_new_with_label(LANG_ACCEPT);
	gtk_widget_set_size_request(req_events[current_event].ok_button, 50, 25);
	g_signal_connect (G_OBJECT(req_events[current_event].ok_button), "clicked", G_CALLBACK (signal_requests_ok), (gint*) current_event);

	req_events[current_event].shut_button = gtk_button_new_with_label(LANG_REJECT);
	gtk_widget_set_size_request(req_events[current_event].shut_button, 50, 25);
	g_signal_connect (G_OBJECT(req_events[current_event].shut_button), "clicked", G_CALLBACK (signal_requests_shut), (gint*) current_event);

	// Pack everything into vbox and hbox
	req_events[current_event].vbox = gtk_vbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(requests_vbox), req_events[current_event].vbox, FALSE, FALSE, 10);

	req_events[current_event].hbox = gtk_hbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(req_events[current_event].vbox), req_events[current_event].hbox);

	// Pack all of objects
	gtk_box_pack_start(GTK_BOX(req_events[current_event].hbox), req_events[current_event].label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(req_events[current_event].hbox), req_events[current_event].ok_button, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(req_events[current_event].hbox), req_events[current_event].shut_button, FALSE, FALSE, 5);

	// Show everything
	gtk_widget_show(req_events[current_event].label);
	gtk_widget_show(req_events[current_event].vbox);
	gtk_widget_show(req_events[current_event].hbox);
	gtk_widget_show(req_events[current_event].shut_button);
	gtk_widget_show(req_events[current_event].ok_button);

	// Set as active
	req_events[current_event].active = true;
	strncpy(req_events[current_event].id, data2, REQUESTS_ID_CHAR_SIZE);
	++activated_events;
}

// Function to process NFR packet
void packet_new_friend_feedback()
{
	// If everything is OK just close window
	if(data_packet[3] == 'A' && data_packet[4] == 'O' && data_packet[5] == 'K')
		gtk_widget_destroy(new_friend_window);

	// If client wasn't found
	else if (data_packet[3] == 'A' && data_packet[4] == 'W' && data_packet[5] == 'R')
		gtk_label_set_text (GTK_LABEL (new_friend_label), LANG_CLIENT_WASNT_FOUND);

	// Else is unexpected
	else
		gtk_label_set_text (GTK_LABEL (new_friend_label), LANG_SOME_ERROR_OCCUPIED);
}

// Function to process INF packet
void packet_receive_info_datas()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Get info
	memset(info_buffer1, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer1, (OPCODE_SIZE), (OPCODE_SIZE+DATA_ONE_SIZE));
	memset(info_buffer2, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer2, (OPCODE_SIZE+DATA_ONE_SIZE), (OPCODE_SIZE+(DATA_ONE_SIZE*2)));
	memset(info_buffer3, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer3, (OPCODE_SIZE+(DATA_ONE_SIZE*2)), (OPCODE_SIZE+(DATA_ONE_SIZE*3)));
	memset(info_buffer4, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer4, (OPCODE_SIZE+(DATA_ONE_SIZE*3)), (OPCODE_SIZE+(DATA_ONE_SIZE*4)));
	memset(info_buffer5, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer5, (OPCODE_SIZE+(DATA_ONE_SIZE*4)), (OPCODE_SIZE+(DATA_ONE_SIZE*5)));
	memset(info_buffer6, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer6, (OPCODE_SIZE+(DATA_ONE_SIZE*5)), (OPCODE_SIZE+(DATA_ONE_SIZE*6)));
	memset(info_buffer7, 0, DATA_ONE_SIZE);
	function_midcopy(data_packet, info_buffer7, (OPCODE_SIZE+(DATA_ONE_SIZE*6)), (OPCODE_SIZE+(DATA_ONE_SIZE*7)));

	// Create label message (multiline) and show it
	message = g_strconcat (LANG_NAME_NAME, info_buffer1, LANG_SURNAME_NAME, info_buffer2, LANG_NICKNAME_NAME, info_buffer3, LANG_CITYNAME, info_buffer4, LANG_COUNTRYNAME, info_buffer5, LANG_AGENUMBER,info_buffer6, LANG_GENDERTYPE ,info_buffer7, NULL);
	gtk_label_set_markup(GTK_LABEL(info_label), message);

	// Show info window if hidden
	function_show_info_window();
}

// Function to process NDF packet
void packet_delete_old_friend()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Get buddy number for deleting process
	buddy_number = function_get_buddy_number(data1);

	// If found
	if (buddy_number == (MAX_BUDDIES + 1))
		return;

	// Delete this buddy
	gtk_list_store_remove(buddy_tree_view, buddy[buddy_number].tree_iter);

	// If chat shown hide if (for spammers)
	if (gtk_widget_get_visible(buddy[buddy_number].window))
		gtk_widget_hide(buddy[buddy_number].window);

	// Set as deleted
	buddy[buddy_number].deleted = true;
}

// Function to process REG packet
void packet_registration_feedback()
{
	// On success update program UI
	if(data_packet[3] == 'R' && data_packet[4] == 'O' && data_packet[5] == 'K')
	{
		// Update interface
		gtk_window_resize (GTK_WINDOW (log_window), 300, 80);
		gtk_widget_hide(reg_entry1);	gtk_widget_hide(reg_entry2);
		gtk_widget_hide(reg_entry3);	gtk_widget_hide(reg_entry4);
		gtk_widget_hide(reg_entry5);	gtk_widget_hide(reg_entry6);
		gtk_widget_hide(reg_entry7);	gtk_widget_hide(reg_entry8);
		gtk_widget_hide(reg_separator);	gtk_widget_hide(reg_button);
		gtk_widget_hide(reg_label); 	gtk_widget_hide(reg_image);
		gtk_widget_show(log_username);	gtk_widget_show(log_password);
		gtk_widget_show(log_label);
	}

	// Write wrong loggin info
	else if (data_packet[3] == 'R' && data_packet[4] == 'W' && data_packet[5] == 'R')
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_ACCOUNT_ALREADY_EXIST);

	// If something wrong with DB
	else if (data_packet[3] == 'R' && data_packet[4] == 'U' && data_packet[5] == 'K')
		gtk_label_set_text (GTK_LABEL (reg_label), LANG_REGISTRATION_ERROR);
}

// Function to process SNT packet
void packet_message_sent_feedback()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and get chat number data
	memset(data2, 0, BUF_SIZE);
	function_midcopy(data_packet, data2, (DATA_ONE_SIZE+OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE));
	if(strlen(data2) < 1)
	{
		function_show_warning(LANG_SENT_PACKET_NO_MSG_ID_DATA);
		return;
	}

	// Convert and get informations
	char    *chat_num = data2;
	int     value = 0;
	value = atoi( chat_num );
	buddy_number = function_get_buddy_number(data1);

	// Add image of sended text if buddy found
	gtk_text_buffer_get_iter_at_offset(buddy[buddy_number].buffer, &end_iter, messages[value].char_count);
	gtk_text_buffer_insert_pixbuf(buddy[buddy_number].buffer, &end_iter, px_received);

	// Free message structure
	messages[value].waiting = false;
	messages[value].chat = 0;
}

// Function to process AUC packet
void packet_authorize_feedback()
{
	// Check for accepted code
	if(data_packet[3] == 'A' && data_packet[4] == 'O' && data_packet[5] == 'K')
	{
		// Update windows and make client online
		if(log_window && buddy_window)
		{
			gtk_widget_destroy(log_window);
			gtk_widget_show_all(buddy_window);
		}
		else
		{
			function_show_warning(LANG_CANT_UPDATE_INTERFACE_AFTER_AUTH);
			return;
		}
	}
	// Write wrong loggin
	else
	{
		if (log_label)
			gtk_label_set_text (GTK_LABEL (log_label), LANG_LOGGIN_UNSSUCESSFUL);
		else
			function_show_warning(LANG_XMODIFY_TEXT_IN_LOGGIN_LABEL);
	}
}

// Function to process IMS packet
void packet_incomming_message()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and get chat number data
	memset(data2, 0, BUF_SIZE);
	function_midcopy(data_packet, data2, (DATA_ONE_SIZE+OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE));
	if(strlen(data2) < 1)
	{
		function_show_warning(LANG_INCOMMING_MSG_XCONTAIN_CHATNUMB);
		return;
	}

	// Clean and get message data
	memset(data3, 0, BUF_SIZE);
	function_midcopy(data_packet, data3, (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE), packet_size);
	if(strlen(data3) < 1)
	{
		function_show_warning(LANG_INCOMMING_MSG_XCONTAIN_MESSAGE);
		return;
	}

	// Use additional function to process incomming message
	if(!additional_function_fxpacket_process_incomming_message(0))
		return;

	// Count lenght
	sending_text_len = LENGHT_SIZE+DATA_ONE_SIZE+ OPCODE_SIZE+MSG_NUMB_SIZE;
	sprintf_s(sending_text_len_char, LENGHT_SIZE,  "%d", sending_text_len);

	// Check packet integrity
	if((strlen(buddy[buddy_number].nick_name) > 0) && (strlen(data2) > 0))
	{
		// Update text for server with "DX_OPCODE_SNT" and name datas
		memset(sending_text,0,BUF_SIZE);
		memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
		memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_SNT, OPCODE_SIZE);
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], buddy[buddy_number].nick_name, strlen(buddy[buddy_number].nick_name));
		memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], data2, strlen(data2));

		// Send socket
		function_send_data(sending_text, sending_text_len);
	}
	else function_show_warning(LANG_TRYING_SEND_WRONG_PACKET_SNT);
}

// Function to process IMG packet
void packet_incomming_offline_message()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and get message data
	memset(data3, 0, BUF_SIZE);
	function_midcopy(data_packet, data3, (DATA_ONE_SIZE+OPCODE_SIZE), packet_size);
	if(strlen(data3) < 1)
	{
		function_show_warning(LANG_INCOMMING_OFLMSG_XCONTAIN_MESSAGE);
		return;
	}

	// Use additional function to process incomming message
	if(!additional_function_fxpacket_process_incomming_message(0))
		return;
}

// Function to process BDL packet
void packet_friend_status_message()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and get avatar data
	memset(data2, 0, BUF_SIZE);
	function_midcopy(data_packet, data2, (CODE_SIZE+OPCODE_SIZE+DATA_ONE_SIZE), (CODE_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+AVATAR_SIZE));

	// Download avatar
	message = g_strconcat("avatars/", data1, ".png", NULL);

	//Look for one downloaded
	test_avatar = fopen(message, "r");

	//Download case
	if(!test_avatar)
	{
		HRESULT hr = URLDownloadToFile ( NULL, data2, message, 0, NULL );
		if(hr == E_OUTOFMEMORY)
		{
			function_show_warning(LANG_XDOWNLOAD_AVATAR_MEMORY);
		}
		else if(hr == INET_E_DOWNLOAD_FAILURE)
		{
			function_show_warning(LANG_XDOWNLOAD_AVATAR_CONNECTION);
		}
		else if(hr != S_OK)
		{
			function_show_warning(LANG_XDOWNLOAD_AVATAR_UNKNOWN);
		}
	}
	else fclose(test_avatar);

	// If online signal create buddy
	if(data_packet[DATA_ONE_SIZE+OPCODE_SIZE] == 'O' && data_packet[DATA_ONE_SIZE+OPCODE_SIZE+1] == 'N' && data_packet[DATA_ONE_SIZE+OPCODE_SIZE+2] == 'L')
		function_make_online(data1);

	// If offline signal delete buddy
	else if(data_packet[DATA_ONE_SIZE+OPCODE_SIZE] == 'O' && data_packet[DATA_ONE_SIZE+OPCODE_SIZE+1] == 'F' && data_packet[DATA_ONE_SIZE+OPCODE_SIZE+2] == 'F')
		function_make_offline(data1);
}

// Function to process IMH packet
void packet_incomming_history_message()
{
	// Update label
	if(data_packet[3] == 'N' && data_packet[4] == 'H' && data_packet[5] == 'I')
	{
		gtk_label_set_text(GTK_LABEL(history_label), LANG_NO_MESSAGES_FOUND);
		return;
	}

	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and get message data
	memset(data2, 0, BUF_SIZE);
	function_midcopy(data_packet, data2, (OPCODE_SIZE+DATA_ONE_SIZE), packet_size);
	if(strlen(data1) < 1)
	{
		function_show_warning(LANG_INCOMMING_HISMSG_XCONTAIN_MESSAGE);
		return;
	}

	// Update label
	gtk_label_set_text(GTK_LABEL(history_label), LANG_ENTER_HISTORY_DATA);

	// Hide enter date window
	if(gtk_widget_get_visible(history_window))
		gtk_widget_hide(history_window);

	// If history view hidden show it
	if(!gtk_widget_get_visible(history_window_view))
		gtk_widget_show_all(history_window_view);

	// End iter
	gtk_text_buffer_get_end_iter ( GTK_TEXT_BUFFER (history_buffer), &end_iter);

	// Get text of name
	message = g_strconcat ("\n", data1, NULL);

	// Insert text
	gtk_text_buffer_insert (GTK_TEXT_BUFFER(history_buffer),&end_iter,message,-1);

	// Check line count and make it bold
	line_count = gtk_text_buffer_get_line_count (history_buffer);
	if (line_count > 0)
	{
		gtk_text_buffer_get_iter_at_line(history_buffer, &start_iter, line_count);
		gtk_text_buffer_get_end_iter (history_buffer, &end_iter);
		gtk_text_buffer_apply_tag_by_name (history_buffer, "bold", &start_iter, &end_iter);
	}
	else
	{
		function_show_warning(LANG_CANT_GET_HISTORY_LINES_COUNT);
		return;
	}

	// Get text of msg
	message = g_strconcat ("\n", data2, NULL);

	// Insert text
	gtk_text_buffer_insert (GTK_TEXT_BUFFER(history_buffer),&end_iter,message,-1);

	// Convert smiles
	function_get_smile(history_buffer);

	// Scroll to end
	gtk_text_buffer_get_end_iter (history_buffer, &end_iter);
	endmark = gtk_text_mark_new ("E2", 0);
	gtk_text_buffer_add_mark (history_buffer, endmark, &end_iter);
	gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (history_view), endmark, 0.0, FALSE, 0, 0);
	gtk_text_buffer_delete_mark (history_buffer, endmark);
}

// Function to process FLT packet
void packet_incomming_file_transfer()
{
	// Get friend data
	if(!additional_function_fxpacket_get_friend_data())
		return;

	// Clean and get msg number data
	memset(data4, 0, BUF_SIZE);
	function_midcopy(data_packet, data4, (DATA_ONE_SIZE+OPCODE_SIZE), (MSG_NUMB_SIZE+DATA_ONE_SIZE+OPCODE_SIZE));
	if(strlen(data2) < 1)
	{
		function_show_warning(LANG_INCOMMING_TRANSFER_XCONTAIN_FILENAME);
		return;
	}

	// Clean and get file name data
	memset(data2, 0, BUF_SIZE);
	function_midcopy(data_packet, data2, (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE), (FILE_NAME_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE));
	if(strlen(data2) < 1)
	{
		function_show_warning(LANG_INCOMMING_TRANSFER_XCONTAIN_FILENAME);
		return;
	}

	// Clean and get url data
	memset(data3, 0, BUF_SIZE);
	function_midcopy(data_packet, data3, (FILE_NAME_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE), (FILE_NAME_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+FILE_PATH_SIZE+MSG_NUMB_SIZE));
	if(strlen(data3) < 1)
	{
		function_show_warning(LANG_INCOMMING_TRANSFER_XCONTAIN_URLDATA);
		return;
	}

	// Use additional function to process incomming message
	if(!additional_function_fxpacket_process_incomming_message(1))
		return;

	// Start download action
	function_ftp_download(data3, data2, buddy_number, end_iter, data4);
}

// Look for packet types
void function_todo_packet()
{
	if(data_packet[0] == 'N' && data_packet[1] == 'F' && data_packet[2] == 'F')
		packet_new_friend_request();
	else if(data_packet[0] == 'N' && data_packet[1] == 'F' && data_packet[2] == 'R')
		packet_new_friend_feedback();
	else if(data_packet[0] == 'I' && data_packet[1] == 'N' && data_packet[2] == 'F')
		packet_receive_info_datas();
	else if(data_packet[0] == 'N' && data_packet[1] == 'F' && data_packet[2] == 'D')
		packet_delete_old_friend();
	else if(data_packet[0] == 'R' && data_packet[1] == 'E' && data_packet[2] == 'G')
		packet_registration_feedback();
	else if(data_packet[0] == 'S' && data_packet[1] == 'N' && data_packet[2] == 'T')
		packet_message_sent_feedback();
	else if(data_packet[0] == 'A' && data_packet[1] == 'U' && data_packet[2] == 'C')
		packet_authorize_feedback();
	else if(data_packet[0] == 'I' && data_packet[1] == 'M' && data_packet[2] == 'S')
		packet_incomming_message();
	else if(data_packet[0] == 'I' && data_packet[1] == 'M' && data_packet[2] == 'G')
		packet_incomming_offline_message();
	else if(data_packet[0] == 'I' && data_packet[1] == 'M' && data_packet[2] == 'H')
		packet_incomming_history_message();
	else if(data_packet[0] == 'B' && data_packet[1] == 'D' && data_packet[2] == 'L')
		packet_friend_status_message();
	else if(data_packet[0] == 'F' && data_packet[1] == 'L' && data_packet[2] == 'T')
		packet_incomming_file_transfer();
	else
		function_show_warning(LANG_UNKNOWN_PACKAGE_OPCODE);
}

// Function to process data called from channel
void function_process_packet(char fx_data_packet[BUF_SIZE], int fx_packet_size)
{
	// Check function inputs
	if(fx_packet_size < (OPCODE_SIZE+1))
		return;
	if(strlen(fx_data_packet) < (OPCODE_SIZE+1))
		return;

	// Save packet datas
	memset(data_packet, 0, BUF_SIZE);
	memcpy(data_packet, fx_data_packet, fx_packet_size);
	packet_size = fx_packet_size;

	// This is only for dubug
	small_counter = 0;
	do
	{
		std::cout << "[" << data_packet[small_counter] << "]";
		++small_counter;
	}
	while (small_counter < fx_packet_size);
	std::cout << std::endl;

	// Todo packet
	function_todo_packet();
}