/*************************************************************************************************************
* This file contains loggin create and loggin packet sending functions
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "Loggining.h"

// Variables for getting informations
// This can't be deallocated
static const gchar *entry_text_user;

// For crypting password
static char *cryped_password;

// Send loggin packet
void signal_send_loggin()
{
	// Get user and pass texts or report
	if (log_username && log_password)
	{
		entry_text_user = gtk_entry_get_text (GTK_ENTRY (log_username));
		entry_text = gtk_entry_get_text (GTK_ENTRY (log_password));
	}
	else function_show_warning(LANG_XLOGGIN_WIDGETS);

	// Check loggin informations
	if((strlen(entry_text_user) == 0) || (strlen(entry_text) == 0))
		function_show_warning(LANG_XLOGIN_INFO);

	// Save my name for future use
	memset(my_name, 0, DATA_ONE_SIZE);
	memcpy(my_name, entry_text_user, DATA_ONE_SIZE);

	// Crypt password
	cryped_password = function_crypt_sha512(entry_text);

	// Count lenght of whole packet
	sending_text_len = LENGHT_SIZE + DATA_ONE_SIZE + OPCODE_SIZE + strlen(cryped_password);
	memset(sending_text_len_char, 0, LENGHT_SIZE);
	sprintf_s(sending_text_len_char, LENGHT_SIZE, "%d", sending_text_len);

	// Update text for server with "DX_OPCODE_AUC" containing name and password datas
	memset(sending_text,0,BUF_SIZE);
	memcpy(sending_text, sending_text_len_char, LENGHT_SIZE);
	memcpy(&sending_text[LENGHT_SIZE], DX_OPCODE_AUC, OPCODE_SIZE);
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE], entry_text_user, strlen(entry_text_user));
	memcpy(&sending_text[LENGHT_SIZE+OPCODE_SIZE+DATA_ONE_SIZE], cryped_password, strlen(cryped_password));

	// Send packet to socket
	function_send_data(sending_text, sending_text_len);
}

