/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "Wizard_Callbacks_Add_FingerPrinting.h"
#include "Wizard_Interface_Add_FingerPrinting.h"
#include "Wizard_Interface.h"
#include "Wizard_Callbacks.h"
#include "../Com/Commun.h"
#include "../Equipement.h"
#include "../Debug.h"
#include "../Contribution.h"
#include "../Thread.h"
#include "../Macro.h"

void Initialisation_Variable_Wizard_Add_FingerPrinting ()
{
	New_System_Name = g_strdup ("");
	New_System_Type_Int = -1;
	New_System_Type_Char = g_strdup ("");
	New_System_Icon_Int = -1;
	New_System_Icon_Char = g_strdup ("");
}

void Wizard_Bouton_Annuler_Add_FingerPrinting_Standard (gpointer * pointer)
{
	gtk_widget_hide (window_wizard);
}


void Wizard_Bouton_Add_FingerPrinting_Page1 (gpointer * pointer)
{
	Wizard_Interface_Add_Fingerprinting_Page1 ();
}

void Wizard_Bouton_Add_FingerPrinting_Page2 (gpointer * pointer)
{
	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Fingerprinting_Page2 ();
	gtk_entry_set_text (GTK_ENTRY (entry_Information_MD5), Equipement_Selectionner->Md5_Signature_W);
}

void Wizard_Bouton_Post_Add_FingerPrinting_Page3 (gpointer * pointer)
{
	DEBUG_ACTION_USER ();
	MYFREE (New_System_Name);
	New_System_Name = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry_Information_OS_System_Name)));
	New_System_Type_Int = gtk_combo_box_get_active (GTK_COMBO_BOX (combobox_Information_OS_Type));
	New_System_Icon_Int = gtk_combo_box_get_active (GTK_COMBO_BOX (combobox_Information_Icon));

	if (strlen (New_System_Name) == 0) {
		Post_Boolean = FALSE;
		return;
	}

	if (New_System_Type_Int == -1) {
		Post_Boolean = FALSE;
		return;
	}

	if (New_System_Icon_Int == -1) {
		Post_Boolean = FALSE;
		return;
	}
	MYFREE (New_System_Type_Char);
	New_System_Type_Char = g_strdup (Recuperation_Texte_Combo (combobox_Information_OS_Type));
	MYFREE (New_System_Icon_Char);
	New_System_Icon_Char = g_strdup (Recuperation_Chaine_Attacher_Combo (combobox_Information_Icon));
	Post_Boolean = TRUE;
}


void Wizard_Bouton_Add_FingerPrinting_Page3 (gpointer * pointer)
{
#define TEXTE "\n\n\nConfirm the following information: \n\nSystem Name : %s\nType : %s\n\n\n\n\nThis data allows you to contribute new equipment fingerprint \nto the AutoScan database. By submitting fingerprints you are\ntransfering any copyright interest in the data to tlagarde@users.sourceforge.net\nThese data can be to modify,relicense or incorporate into programs like AutoScan."
	gchar    *text;

	DEBUG_ACTION_USER ();
	Wizard_Interface_Add_Fingerprinting_Page3 ();
	text = g_strdup_printf (TEXTE, New_System_Name, New_System_Type_Char);
	gtk_label_set_text (label_wizard_Final_Unknown_Equipement, text);
	MYFREE (text);
}

void Wizard_Bouton_Add_FingerPrinting_Fin (gpointer * pointer)
{
	struct struct_Demande_Log_Equipement Message;
		
	DEBUG_ACTION_USER ();
	gtk_widget_hide (window_wizard);
#ifdef __LOG_ENABLE

	if (Log_Equipement!=NULL)
		MYFREE(Log_Equipement);
	Log_Equipement = NULL;
	bzero (&Message, sizeof (struct struct_Demande_Log_Equipement));
	Message.ID_Reseau = ID_Reseau_En_Cours;
	memcpy (Message.ID_Equipement, Equipement_Selectionner->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
	Communication_Envoie_Donnee (Reseau_Selectionner->Pid_Agent, DEMANDE_LOG_EQUIPEMENT, (gpointer *) & Message, sizeof (Message), Reseau_Selectionner);
#endif
	
	Etat_Contribution = CONTRIBUTION_DEBUT;
	demmarrage_Thread (Thread_Contribution_Fingerprinting, NULL);
	while (Etat_Contribution == CONTRIBUTION_DEBUT)
		sleep (1);

	Rajouter_Nouvelle_Signature (New_System_Name, Equipement_Selectionner->Md5_Signature_W, New_System_Type_Int, New_System_Icon_Char, Equipement_Selectionner->Entete_FTP, Equipement_Selectionner->Entete_HTTP,Equipement_Selectionner->Entete_HTTPS, Equipement_Selectionner->Entete_IMAP,Equipement_Selectionner->Entete_SMTP,Equipement_Selectionner->Entete_SSH,Equipement_Selectionner->Entete_TELNET,Equipement_Selectionner->GateWay,Equipement_Selectionner->Routage);
	Parcours_Equipement_Inconnu (Equipement_Selectionner->Md5_Signature_W);
}
