/*
 *
 *  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"

UBYTE *vers = (UBYTE *)("\0$VER: lmail:"AMIGAMAIL_VERSION);

static struct RDArgs *RDArgs;

static LONG CommandLine[4];

char Username[USERLEN+1];

char MailDirName[MAIL_FIELDLENGTH];

char Folder[MAIL_FIELDLENGTH];

#define BUFLEN 1024
static char Buf[BUFLEN];

void Panic(void)
{
	FILE *MailFile;
	int i;

	writelog("Panic!!");

	/* try up to 20 times with 5s delay */
	i=0;
	while (!(MailFile = fopen("UUMAIL:undeliverable.mail","a")) && i++<20) 
		Delay(250L);

	if (MailFile)
	{
		fprintf(MailFile,"\n\n\nCould't deliver mail for user %s\n\n",Username);

		while (i=fread(Buf,1,BUFLEN,stdin))
			fwrite(Buf,1,i,MailFile);

		fclose(MailFile);
	}
	else
		writelog("Can't write to UUMAIL:undeliverable.mail");

	exit(20);
}

/* mail on stdin; username is first or, if present, second parameter */

int main(int argc,char *argv[])
{
	FILE *MailFile;
	int i,seq;
	BPTR MyLock;
	struct Mail *New;
	int markread=FALSE,markold=FALSE;
	int fixdosbug = FALSE; /* crude fix for a bug in dos.library/ReadArgs() */

	if (!(RDArgs = (struct RDArgs *)AllocDosObject(DOS_RDARGS, NULL)))
		Panic();

	/* get parameters */
	if (argc) /* started from CLI */
	{
		strcpy(Username,argv[1]);
#if 0
		/* can check argv[1] only when it's sure */
		/* that we have at least one argument  */
		if (argc>1)
			if (*argv[1]=='?')
				goto getfromstdin;

		RDArgs->RDA_Source.CS_Buffer = GetArgStr();
		RDArgs->RDA_Source.CS_Length = (RDArgs->RDA_Source.CS_Buffer)?
			(strlen((char *)RDArgs->RDA_Source.CS_Buffer)):(0);

getfromstdin:

		if (!(ReadArgs((UBYTE *)"USERNAME/A,FOLDER/K,MARKOLD/S,MARKREAD/S",
			CommandLine,RDArgs)))
		{
			/* due to a buf in dos.library ... :-( :-( we do the following */
			if (!(fixdosbug = ReadArgs((UBYTE *)
				"PROGNAME/A,USERNAME/A,FOLDER/K,MARKOLD/S,MARKREAD/S",
				CommandLine,RDArgs)))
			{
				printf("USAGE: %s USERNAME/A,FOLDER/K,MARKOLD/S,MARKREAD/S",argv[0]);
				puts("Appending to uulib:undeliverable.mail, please press ^\\");
				FreeArgs(RDArgs);
				FreeDosObject(DOS_RDARGS, RDArgs);
				Panic();
			}
		}

		if (fixdosbug) fixdosbug=1; /* Shift CommandLine */

		/* USERNAME/A ==> CommandLine[0+fixdosbug]!=0 */
		strcpy(Username,(char *)CommandLine[0+fixdosbug]);

		if (CommandLine[1+fixdosbug]) 
			strcpy(Folder,(char *)CommandLine[1+fixdosbug]);

		if (CommandLine[2+fixdosbug]) 
			markold = TRUE;

		if (CommandLine[3+fixdosbug]) 
			markread = TRUE;
#endif
	}
	else
		Panic();

	FreeArgs(RDArgs);
	FreeDosObject(DOS_RDARGS, RDArgs);

	/* server running? */
	if (!(ServerPort = FindPort(SERVERNAME))) 
		Panic();

	/* was a folder specified? */
	if (!*Folder)
		sprintf(Folder,"uumail:%s.mail",Username);

	if (!LockContents(Folder))
		Panic();

	/* get seq */
	if (!(seq = NewSeq(Folder)))
		Panic();

	/* check for directory */
	if (MyLock = Lock((UBYTE *)Folder,ACCESS_READ))
		UnLock(MyLock);
	else
	{
		/* create directory */
		if (!(MyLock = CreateDir((UBYTE *)Folder)))
			Panic();
		UnLock(MyLock);
	}

	sprintf(MailDirName,"%s/%ld",Folder,seq);
	
	/* open mailfile */
	if (!(MailFile = fopen(MailDirName,"w")))
		Panic();

	/* save the mail */
	while (i=fread(Buf,1,BUFLEN,stdin))
		fwrite(Buf,1,i,MailFile);

	/* close the mailfile */
	fclose(MailFile);

	/* protect file */
	SetProtection((UBYTE *)MailDirName,7L);

	/* inform server about new item */
	if (New = AddItem(seq,Folder))
	{	
		/* mark as old */
		if (markread) 
			MarkRead(New->Number,Folder);

		if (markold) 
			MarkOld(New->Number,Folder);

		FreeMail(New);
	}
	else
		writelog("contentsserver doesn't accept mail");

	UnLockContents(Folder);

	return(0);
}
