/*
 *
 *  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: newmail:"AMIGAMAIL_VERSION);

/* check for new mail */

/* usage: newmail [<username>] */

char Folder[MAIL_FIELDLENGTH];

char Username[160]="";
char Message[160];

struct Window *Window=0; /* dummy for os20stuff */

struct IntuitionBase *IntuitionBase;
struct Library *GadToolsBase; /* dummy, library not used */
struct Library *AslBase;      /* dummy, library not used */
struct Library *IconBase=0;

struct List Mailbox;

/* Check if "Username" has new mail */
void CheckUser(void)
{
	struct Mail *Mail;
	int found = FALSE;
	int foundunread = FALSE;
	int foundnew = FALSE;

	sprintf(Folder,"uumail:%s.mail",Username);
	LockContents(Folder);

	GetContents(&Mailbox,Folder);

	/* check for new mail */
	for (Mail=(struct Mail *)Mailbox.lh_Head;
		Mail->m_Node.ln_Succ;
		Mail=(struct Mail *)Mail->m_Node.ln_Succ)
		if (Mail->read & MAIL_NEW)
		{
			foundnew = TRUE;
			break;
		}
		else if (!(Mail->read & MAIL_READ))
			foundunread=TRUE;
		else
			found = TRUE;

	FreeMailbox(&Mailbox);

	UnLockContents(Folder);

	if (foundnew)
	{
		sprintf(Message,"User %s has new mail",Username);
		SimpleRequest(Window,Message);
	}
	else if (foundunread)
	{
		sprintf(Message,"User %s has unread mail",Username);
		SimpleRequest(Window,Message);
	}
	else if (found)
	{
		sprintf(Message,"User %s has mail",Username);
		SimpleRequest(Window,Message);
	}

}

int main(int argc, char *argv[])
{
	struct WBStartup *WBenchMsg;
	char *s;
	struct DiskObject *dop;
	FILE *kusers;
	int i;
	char InputBuffer[MAIL_FIELDLENGTH];

	if (argc==0)
	{
		if (!(IconBase = 
			OpenLibrary((UBYTE *)"icon.library",AM_LIBRARY_VERSION)))
			exit(5);

		WBenchMsg = (struct WBStartup *)argv;

		/* try to get Username from ToolTypes */
		if ((dop = GetDiskObject((UBYTE *)WBenchMsg->sm_ArgList->wa_Name)))
			if (s = (char *) FindToolType((UBYTE **)dop->do_ToolTypes ,(UBYTE *)"USERNAME"))
				strcpy(Username,s);

		CloseLibrary(IconBase);
	}
	else if (argc==2)
		strcpy(Username,argv[1]);
	else if (argc!=1)
	{
		printf("Usage: %s <username>\n",argv[0]);
		exit(5);
	}

	if (!(IntuitionBase = (struct IntuitionBase *)
		OpenLibrary((UBYTE *)"intuition.library",AM_LIBRARY_VERSION)))
	{
		puts("fatal error: can't open intuition.library!!!");
		exit(20);
	}

	/* init the mailbox-list */
	Mailbox.lh_Head = (struct Node *)&Mailbox.lh_Tail;
	Mailbox.lh_TailPred = (struct Node *)&Mailbox.lh_Head;
	Mailbox.lh_Tail = 0;
	Mailbox.lh_Type = AMNT_MAILBOX;

	/* when started from sys:wbstartup, possibly the contentsserver isn't already running */
	for (i=0;i<10;++i) 
		if (ServerPort = FindPort(SERVERNAME)) 
			break;
		else
			Delay(50);

	if (!ServerPort)
		exit(21);

	/* check version of server */
	if (!CmpVersion())
	{
		SimpleRequest(Window,"Wrong version of server!");
		CloseLibrary((struct Library *)IntuitionBase);
		exit(20);
	}
	
	/* username given? then check just that user */
	if (*Username)
		CheckUser();
	else /* check all users */
	{
		if (!(kusers = fopen("GETTY:passwd","r")))
		{
			SimpleRequest(Window,"Can't open GETTY:passwd");
			CloseLibrary((struct Library *)IntuitionBase);
			exit(20);
		}

		while (fgets(InputBuffer,MAIL_FIELDLENGTH,kusers))
		{
			if (InputBuffer[0]=='#') continue;
			if (InputBuffer[0]==' ') continue;
			if (InputBuffer[0]=='\t') continue;
			if (InputBuffer[0]=='\n') continue;
	
			i=0;
			s=Username;
	
			while (i<159 && *InputBuffer && InputBuffer[i]!=',')
				*s++=InputBuffer[i++];
	
			*s=0;

			CheckUser();
		}

		fclose(kusers);
	}

	CloseLibrary((struct Library *)IntuitionBase);

	return(0);
}
