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

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

// Buddy number for SNT
static int buddy_number;

// Textview variables
static GtkTextIter end_iter;
static GtkTextIter start_iter;
static GtkTextMark *endmark;
static gint line_count;
static gchar *message;

// Buffer for sending packets
static char sending_text[BUF_SIZE];
static int sending_text_len;
static char sending_text_len_char[LENGHT_SIZE+1];

// For avatar file name
static gchar *file_avatar;

// For event message
static gchar *event_message;

// Static event counter
static int current_event;

// Event images variables
static GtkImage *px_event_00;
static GtkImage *px_event_01;
static GtkImage *px_event_02;
static GtkImage *px_event_03;
static GtkImage *px_event_04;
static GtkImage *px_event_05;
static GtkImage *px_event_06;
static GtkImage *px_event_07;
static GtkImage *px_event_08;
static GtkImage *px_event_09;

// Counter
int small_counter;

// Packet size
int packet_size;

// For convering event id
int event_id_int;
char *event_id_char;
bool event_id_found;

// Testing avatar
FILE *test_avatar;

/****** Do requered actions *****/
void function_todo_packet()
{
	/******************************* If incomming socket is NFF type ********************************/
	if(cleaned_packet_nolen[0] == 'N' && cleaned_packet_nolen[1] == 'F' && cleaned_packet_nolen[2] == 'F')
	{
		// Clean and get friend data
		memset(data1, 0, DATA_ONE_SIZE+1);
		function_midcopy(cleaned_packet_nolen, data1, (OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE));

		// Clean and id data
		memset(data2, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data2, (OPCODE_SIZE+DATA_ONE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE+100));

		// Set to null
		event_id_int = 0;

		// Convert and get informations
		if (data2)
		{
			event_id_char = data2;
			event_id_int = atoi( event_id_char );
		}
		else
		{
			event_id_int = 0;
			function_show_warning("Can't covert event id to integer!");
			return;
		}

		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("Max count of events reached");
			return;
		}

		// Set current event
		current_event = small_counter;

		// Destroy previous button
		if (buddy_requests)
			gtk_widget_destroy(buddy_requests);
		else
			function_show_warning("Can't find requests button!");

		// Show requests
		buddy_requests = gtk_button_new();
		if (!buddy_requests)
		{
			function_show_warning("Can't recreate buddy requests button!");
			return;
		}
		gtk_widget_set_size_request(buddy_requests, 35, 26);

		// Load event images - this is protected by default image
		px_event_00 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_00.png");
		px_event_01 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_01.png");
		px_event_02 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_02.png");
		px_event_03 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_03.png");
		px_event_04 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_04.png");
		px_event_05 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_05.png");
		px_event_06 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_06.png");
		px_event_07 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_07.png");
		px_event_08 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_08.png");
		px_event_09 = (GtkImage *) gtk_image_new_from_file("images/core/px_event_09.png");

		// Get image of button
		if (activated_events == 0)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_01));
		else if (activated_events == 1)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_02));
		else if (activated_events == 2)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_03));
		else if (activated_events == 3)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_04));
		else if (activated_events == 4)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_05));
		else if (activated_events == 5)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_06));
		else if (activated_events == 6)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_07));
		else if (activated_events == 7)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_08));
		else if (activated_events == 8)
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_09));
		else
			gtk_button_set_image(GTK_BUTTON(buddy_requests), GTK_WIDGET(px_event_00));

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

		// Pack button into footer
		gtk_box_pack_start(GTK_BOX(buddy_hfooter), buddy_requests, FALSE, FALSE, 0);
		gtk_widget_show(buddy_requests);

		// Save new client name for event
		if (strlen(data1) > 0)
			strcpy_s(req_events[current_event].nick_name, DATA_ONE_SIZE, data1);
		else
		{
			function_show_warning("Can't copy requester name!");
			return;
		}

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

		// Create message for label
		event_message = g_strconcat (data1, " wants to add you in buddy list ", NULL);

		// Create label
		req_events[current_event].label = gtk_label_new(event_message);
		if (!req_events[current_event].label) function_show_warning("Can't create buddy requester label!");
		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), TRUE);
		gtk_widget_set_size_request(req_events[current_event].label, 220, 25);

		// Create buttons with sizes
		req_events[current_event].ok_button = gtk_button_new_with_label("OK");
		if (!req_events[current_event].ok_button) function_show_warning("Can't create buddy requester OK button!");
		gtk_widget_set_size_request(req_events[current_event].ok_button, 30, 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("SHUT");
		if (!req_events[current_event].shut_button) function_show_warning("Can't create buddy requester SHUT button!");
		gtk_widget_set_size_request(req_events[current_event].shut_button, 30, 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);
		if (!req_events[current_event].vbox) function_show_warning("Can't create buddy requester vbox!");
		gtk_box_pack_start(GTK_BOX(requests_vbox), req_events[current_event].vbox, FALSE, FALSE, 0);

		req_events[current_event].hbox = gtk_hbox_new(FALSE, 0);
		if (!req_events[current_event].hbox) function_show_warning("Can't create buddy requester hbox!");
		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, 0);
		gtk_box_pack_start(GTK_BOX(req_events[current_event].hbox), req_events[current_event].ok_button, FALSE, FALSE, 0);
		gtk_box_pack_start(GTK_BOX(req_events[current_event].hbox), req_events[current_event].shut_button, FALSE, FALSE, 0);

		// 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, 100);
		++activated_events;

		// Return true
		return;
	}

	/******************************* If incomming socket is NFR type ********************************/
	else if(cleaned_packet_nolen[0] == 'N' && cleaned_packet_nolen[1] == 'F' && cleaned_packet_nolen[2] == 'R')
	{
		// If everything is OK just close window
		if(cleaned_packet_nolen[3] == 'A' && cleaned_packet_nolen[4] == 'O' && cleaned_packet_nolen[5] == 'K')
		{
			if(new_friend_window)
				gtk_widget_destroy(new_friend_window);
			else
				function_show_warning("Can't destroy new friend window");

			return;
		}
		// If client wasn't found
		else if (cleaned_packet_nolen[3] == 'A' && cleaned_packet_nolen[4] == 'W' && cleaned_packet_nolen[5] == 'R')
		{
			if (new_friend_label)
				gtk_label_set_text (GTK_LABEL (new_friend_label), "Not found");
			else
				function_show_warning("Can't set label!");

			return;
		}
		// If client is already added
		else if (cleaned_packet_nolen[3] == 'A' && cleaned_packet_nolen[4] == 'E' && cleaned_packet_nolen[5] == 'X')
		{
			if (new_friend_label)
				gtk_label_set_text (GTK_LABEL (new_friend_label), "Already added");
			else
				function_show_warning("Can't set label!");

			return;
		}
		// Else is unexpected
		else
		{
			if (new_friend_label)
				gtk_label_set_text (GTK_LABEL (new_friend_label), "Unexpected error!");
			else
				function_show_warning("Can't set label!");

			return;
		}
	}

	/******************************* If incomming socket is REG type ********************************/
	else if(cleaned_packet_nolen[0] == 'R' && cleaned_packet_nolen[1] == 'E' && cleaned_packet_nolen[2] == 'G')
	{
		// On success update program UI
		if(cleaned_packet_nolen[3] == 'R' && cleaned_packet_nolen[4] == 'O' && cleaned_packet_nolen[5] == 'K')
		{
			// Resize window
			if(log_window)
				gtk_window_resize (GTK_WINDOW (log_window), 300, 80);
			else
				function_show_warning("Can't resize registration window");

			// Hide registration widgets
			if (reg_entry1 && reg_entry4 && reg_entry7 && reg_button && reg_entry2 && reg_entry5 && reg_entry8 && reg_label  &&
				reg_entry3 && reg_entry6 && reg_separator && reg_image )
			{
				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);
			}
			else function_show_warning("Can't hide some of registration widgets!");

			// Show loggin
			if(log_entry && log_entry2 && log_label)
			{
				gtk_widget_show(log_entry);
				gtk_widget_show(log_entry2);
				gtk_widget_show(log_label);
			}
			else function_show_warning("Can't show some of loggining widgets!");

			return;
		}
		// Write wrong loggin info
		else if (cleaned_packet_nolen[3] == 'R' && cleaned_packet_nolen[4] == 'W' && cleaned_packet_nolen[5] == 'R')
		{
			if(reg_label)
				gtk_label_set_text (GTK_LABEL (reg_label), "Already exists");
			else
				function_show_warning("Can't modify label");

			return;
		}
		// If something wrong with DB
		else if (cleaned_packet_nolen[3] == 'R' && cleaned_packet_nolen[4] == 'U' && cleaned_packet_nolen[5] == 'K')
		{
			if(reg_label)
				gtk_label_set_text (GTK_LABEL (reg_label), "Unkown error in DB");
			else
				function_show_warning("Can't modify label");

			return;
		}
		// Wrong socket content
		else
		{
			if(reg_label)
				gtk_label_set_text (GTK_LABEL (reg_label), "Wrong code received");
			else
				function_show_warning("Can't modify label");

			return;
		}
	}

	/********************************* If incomming socket is SNT type ***********************************/
	else if(cleaned_packet_nolen[0] == 'S' && cleaned_packet_nolen[1] == 'N' && cleaned_packet_nolen[2] == 'T')
	{
		// Clean and get friend data
		memset(data1, 0, DATA_ONE_SIZE);
		function_midcopy(cleaned_packet_nolen, data1, (OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE));
		if(strlen(data1) < 1)
		{
			function_show_warning("Sent packet doesn't contain friend data ?");
			return;
		}

		// Clean and get chat number data
		memset(data2, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data2, (DATA_ONE_SIZE+OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE));
		if(strlen(data2) < 1)
		{
			function_show_warning("Sent packet doesn't contain chat number 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
		if (buddy_number != (MAX_BUDDIES + 1))
			gtk_text_buffer_insert_pixbuf(buddy[buddy_number].buffer, &messages[value].msg_iter, px_received);

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

		return;
	}

	/******************************** If incomming socket is AUC type ***********************************/
	else if(cleaned_packet_nolen[0] == 'A' && cleaned_packet_nolen[1] == 'U' && cleaned_packet_nolen[2] == 'C')
	{
		// Check for accepted code
		if(cleaned_packet_nolen[3] == 'A' && cleaned_packet_nolen[4] == 'O' && cleaned_packet_nolen[5] == 'K')
		{
			// Update windows and make client online
			if(log_window)
				gtk_widget_destroy(log_window);
			else
				function_show_warning("Can't delete log_window");

			if(buddy_window)
				gtk_widget_show_all(buddy_window);
			else
				function_show_warning("Can't create buddy window");

			return;
		}
		// Write wrong loggin
		else
		{
			if (log_label)
				gtk_label_set_text (GTK_LABEL (log_label), "Wrong password?");
			else
				function_show_warning("Can't modify text in log label");

			return;
		}
	}

	/******************************** If incomming socket is IMS type ***********************************/
	else if(cleaned_packet_nolen[0] == 'I' && cleaned_packet_nolen[1] == 'M' && cleaned_packet_nolen[2] == 'S')
	{
		// Clean and get friend data
		memset(data1, 0, DATA_ONE_SIZE);
		function_midcopy(cleaned_packet_nolen, data1, (OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE));
		if(strlen(data1) < 1)
		{
			function_show_warning("Incomming message packet doesn't contain friend data?");
			return;
		}

		// Clean and get chat number data
		memset(data2, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data2, (DATA_ONE_SIZE+OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE));
		if(strlen(data2) < 1)
		{
			function_show_warning("Incomming message packet doesn't contain chat number data?");
			return;
		}

		// Clean and get message data
		memset(data3, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data3, (DATA_ONE_SIZE+OPCODE_SIZE+MSG_NUMB_SIZE), (packet_size-LENGHT_SIZE));
		if(strlen(data3) < 1)
		{
			function_show_warning("Incomming message packet doesn't contain message data?");
			return;
		}

		// 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("Can't get buddy chat window but market as created!");
		else
		{
			if(!create_buddy_chat(buddy_number))
				function_show_warning("Failed to create buddy chat!");
		}

		// 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());

		// Look for time
		if(strlen(timemsg) < 1)
			function_show_warning("Wrong time included");

		// 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
		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
		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);

		// 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("Wrong SNT packet!");

		return;
	}

	/******************************** If incomming socket is IMG type ***********************************/
	else if(cleaned_packet_nolen[0] == 'I' && cleaned_packet_nolen[1] == 'M' && cleaned_packet_nolen[2] == 'G')
	{
		// Clean and get friend data
		memset(data1, 0, DATA_ONE_SIZE);
		function_midcopy(cleaned_packet_nolen, data1, (OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE));
		if(strlen(data1) < 1)
		{
			function_show_warning("Incomming  offline message packet doesn't contain friend data?");
			return;
		}

		// Clean and get message data
		memset(data3, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data3, (DATA_ONE_SIZE+OPCODE_SIZE), (packet_size-LENGHT_SIZE));
		if(strlen(data3) < 1)
		{
			function_show_warning("Incomming offline message packet doesn't contain message data?");
			return;
		}

		// 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("Can't get buddy chat window but market as created!");
		else
		{
			if(!create_buddy_chat(buddy_number))
				function_show_warning("Error while creating buddy chat!");
		}

		// 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());

		// Look for time
		if(strlen(timemsg) < 1)
			function_show_warning("Wrong time included");

		// 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
		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
		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;
	}

	/******************************** If incomming socket is BDL type ***********************************/
	else if(cleaned_packet_nolen[0] == 'B' && cleaned_packet_nolen[1] == 'D' && cleaned_packet_nolen[2] == 'L')
	{
		// Clean and get type data
		memset(data1, 0, DATA_ONE_SIZE+1);
		function_midcopy(cleaned_packet_nolen, data1, (CODE_SIZE+OPCODE_SIZE), (CODE_SIZE+OPCODE_SIZE+DATA_ONE_SIZE));
		if(strlen(data1) < 1)
		{
			function_show_warning("Incomming status packet doesn't contain friend data?");
			return;
		}

		// Clean and get type data
		memset(data2, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data2, (CODE_SIZE+OPCODE_SIZE+DATA_ONE_SIZE), (CODE_SIZE+OPCODE_SIZE+DATA_ONE_SIZE+100));
		if(strlen(data2) > 0)		
		{
			// Download avatar
			file_avatar = g_strconcat("avatars/", data1, ".png", NULL);

			//Look for one downloaded
			if(fopen_s(&test_avatar, file_avatar, "r") != 0)
			{
				HRESULT hr = URLDownloadToFile ( NULL, data2, file_avatar, 0, NULL );
				if(hr == E_OUTOFMEMORY)
					function_show_warning("Can't download avatar - out of memory!");
				else if(hr == INET_E_DOWNLOAD_FAILURE)
					function_show_warning("Can't download avatar - connection failed!");
				else if(hr != S_OK)
					function_show_warning("Can't download avatar - unexpected error!");
			}
		}

		// If online signal create buddy
		if(cleaned_packet_nolen[3] == 'O' && cleaned_packet_nolen[4] == 'N' && cleaned_packet_nolen[5] == 'L')
		{
			if (function_make_online(data1))
				return;
		}
		// If offline signal delete buddy
		else if(cleaned_packet_nolen[3] == 'O' && cleaned_packet_nolen[4] == 'F' && cleaned_packet_nolen[5] == 'F')
		{
			if (function_make_offline(data1))
				return;
		}

		return;
	}

	/******************************** If incomming socket is FLT type ***********************************/
	else if(cleaned_packet_nolen[0] == 'F' && cleaned_packet_nolen[1] == 'L' && cleaned_packet_nolen[2] == 'T')
	{
		// Clean and get friend data
		memset(data1, 0, DATA_ONE_SIZE);
		function_midcopy(cleaned_packet_nolen, data1, (OPCODE_SIZE), (DATA_ONE_SIZE+OPCODE_SIZE));
		if(strlen(data1) < 1)
		{
			function_show_warning("Incomming  file packet doesn't contain friend data?");
			return;
		}

		// Clean and get file name data
		memset(data2, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data2, (DATA_ONE_SIZE+OPCODE_SIZE), (FILE_NAME_SIZE+DATA_ONE_SIZE+OPCODE_SIZE));
		if(strlen(data2) < 1)
		{
			function_show_warning("Incomming  file packet doesn't contain file name data?");
			return;
		}

		// Clean and get url data
		memset(data3, 0, BUF_SIZE);
		function_midcopy(cleaned_packet_nolen, data3, (FILE_NAME_SIZE+DATA_ONE_SIZE+OPCODE_SIZE), (FILE_NAME_SIZE+DATA_ONE_SIZE+OPCODE_SIZE+FILE_PATH_SIZE));
		if(strlen(data3) < 1)
		{
			function_show_warning("Incomming file packet doesn't contain url data?");
			return;
		}

		// Get buddy number
		buddy_number = function_get_buddy_number(data1);

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

		// if only hidden just show
		if(buddy[buddy_number].buddy_chat == true)
			if (buddy[buddy_number].window)
				gtk_widget_show(buddy[buddy_number].window);
			else
				function_show_warning("Can't get buddy chat window but market as created!");
		else
		{
			if(!create_buddy_chat(buddy_number))
				function_show_warning("Problem with creating buddy chat!");
		}

		// 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());

		// Look for time
		if(strlen(timemsg) < 1)
			function_show_warning("Wrong time included");

		// 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
		message = g_strconcat ("\n", "File receiving:", data2, " ", 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;

		// Add mark of sended text
		gtk_text_buffer_get_end_iter (buddy[buddy_number].buffer, &end_iter);
		gtk_text_buffer_insert_pixbuf(buddy[buddy_number].buffer, &end_iter, px_receiving);

		ftp_download_action(data3, data2, buddy_number, end_iter);

		// 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;
	}

	/****************************** If incomming socket is UNKNOWN type *********************************/
	else
	{
		function_show_warning("Unknown opcode packet!");
		return;
	}
}

/****************************** PROCESS ALL DATA *********************************/
bool function_process_packet(char fx_cleaned_packet[BUF_SIZE], int fx_packet_size)
{
	// Save packet size
	packet_size = fx_packet_size;

	// Check if packet is not empty
	if (fx_packet_size == 0 || strlen(fx_cleaned_packet) < 1)
	{
		function_show_warning("Trying to process empty packet with len?");
		return false;
	}

	// Delete lenght data
	memset(cleaned_packet_nolen, 0, BUF_SIZE);
	function_midcopy(fx_cleaned_packet, cleaned_packet_nolen, LENGHT_SIZE, fx_packet_size);

	// Check if cleaned data aren't empty
	if (cleaned_packet_nolen == 0 || strlen(cleaned_packet_nolen) < 1)
	{
		function_show_warning("Trying to process empty packet without len?");
		return false;
	}

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

	// Todo packet
	function_todo_packet();

	return true;
}