/* ***********************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 <gtk/gtk.h>
#include "Log.h"
#include "Smtp.h"
#include "Debug.h"
#include "Gconf.h"
#include "Thread.h"
#include "Com/Communication.h"
#include "Macro.h"

gboolean Envoie_Donnee (int Socket, char *Donnee, char *Reponse)
{
	int       NbOctet;
	char      Temp[255];

	if ((NbOctet = send (Socket, Donnee, strlen (Donnee), 0)) == 0) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Mail send");
		close (Socket);
		return FALSE;
	}

	if ((NbOctet = recv (Socket, Temp, sizeof (Temp), 0)) == 0) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Mail recv");
		close (Socket);
		return FALSE;
	}
	Temp[NbOctet] = 0;
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Command : %s#  ---> [%s]", Donnee, Temp);
	if (strncmp (Temp, Reponse, strlen (Reponse)) != 0) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error SMTP");
		return FALSE;
	}
	return TRUE;
}


void Envoie_Email (gpointer * Donnee)
{
	struct struct_Smtp *Donnee_SMTP = (struct struct_Smtp *) Donnee;
	struct hostent *host = NULL;
	int       Socket;
	int       NbOctet, Tentative;
	char      Temp[255];
	gboolean  Connection_Accepter;

	Create_Log(CATEG_THREAD_ENVOIE_EMAIL,NULL,NULL,TRUE);
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Email to %s", Lecture_Gconf_Char (CHAR_SERVEUR_SMTP));
	if ((host = gethostbyname (Lecture_Gconf_Char (CHAR_SERVEUR_SMTP))) == NULL) {
		Stop_Thread (0);
	}
	Socket = socket (AF_INET, SOCK_STREAM, 0);
	Tentative = 0;
	Connection_Accepter = FALSE;

	do {
	
		if (connection_Serveur (Lecture_Gconf_Char (CHAR_SERVEUR_SMTP), host, 25, Socket) == FALSE) {
			sleep (1);
		}
		else
			Connection_Accepter = TRUE;
		Tentative++;
	} while ((!Connection_Accepter) && (Tentative <= 5));

	if (!Connection_Accepter) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Impossible de se connecter");
		close (Socket);
		Stop_Thread (0);
	}

	//printf("Recupération de l'en-tete\n");
	if ((NbOctet = recv (Socket, Temp, sizeof (Temp), 0)) == 0) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Reception En-tete");
		close (Socket);
		Stop_Thread (0);
	}
	Temp[NbOctet] = 0;

	sprintf (Temp, "HELO %s\r\n", Lecture_Gconf_Char (CHAR_SERVEUR_SMTP));
	if (!Envoie_Donnee (Socket, Temp, "250"))
		Stop_Thread (0);

	sprintf (Temp, "MAIL FROM:<%s>\r\n",Donnee_SMTP->utilisateur);
	if (!Envoie_Donnee (Socket, Temp, "250"))
		Stop_Thread (0);
	sprintf (Temp, "RCPT TO:<%s>\r\n", Donnee_SMTP->utilisateur);
	if (!Envoie_Donnee (Socket, Temp, "250"))
		Stop_Thread (0);
	if (!Envoie_Donnee (Socket, "DATA\r\n", "354"))
		Stop_Thread (0);

	if ((NbOctet = send (Socket, Donnee_SMTP->Message, strlen (Donnee_SMTP->Message), 0)) == 0) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Envoie Mail From Erreur\n");
		close (Socket);
		Stop_Thread (0);
	}
	if (!Envoie_Donnee (Socket, "\r\n.\r\n", "250"))
		Stop_Thread (0);
	if (!Envoie_Donnee (Socket, "QUIT\r\n", "221"))
		Stop_Thread (0);
	close (Socket);
	printf ("Email send\n");
	MYFREE (Donnee_SMTP->utilisateur);
	MYFREE (Donnee_SMTP->Message);
	MYFREE (Donnee_SMTP);
	Stop_Thread (0);
}



void Envoie_Email_Notification (gchar * Message, gchar * utilisateur)
{
	struct struct_Smtp *SMTP = NULL;

	MALLOC (SMTP, sizeof (struct struct_Smtp));
	SMTP->utilisateur = g_strdup (utilisateur);
	SMTP->Message = g_strdup_printf ("Subject: Notification AutoScan\n%s", Message);
	demmarrage_Thread ((gpointer) & Envoie_Email, SMTP); // Thread Loggé
}
