/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */

#include "am.h"


/* delete mail */
/* doit = TRUE: don't ask for confirmation */
int DeleteMail(struct Mail *Mail,int doit)
{
	char dummy[160];

	if (!Mail) return(FALSE); /* hurz! */

	/* confirm delete */
	if (!doit) doit = TwoGadRequest(Window,"Really delete ?");

	if (doit)
	{
		/* build filename */
		sprintf(dummy,"%s/%ld",Folder,Mail->Number);

		/* unprotect file */
		SetProtection((UBYTE *)dummy,0L);

		/* delete file */
		if (!DeleteFile((UBYTE *)dummy))
		{
			SimpleRequest(Window,"Can't delete file");

			/* protect file again (if I can't delete the file, this might not work, too) */
			SetProtection((UBYTE *)dummy,7L);

			return(FALSE);
		}

		/* delete item from selected list */
		Remove(&(Mail->m_SelectNode));
		
		/* remove it from mailbox */
		Remove(&(Mail->m_Node));

		/* tell server */
		if (!(DeleteItem(Mail->Number,Folder)))
		{
			SimpleRequest(Window,"Internal Error: Can't detete item from database");
			FreeMail(Mail);
			return(FALSE);
		}

		/* free it's memory */
		FreeMail(Mail);

	}

	return(TRUE);
}
