#include <libraries/zbbs.h>
#include "protos.h"
#include "globals.c"

VOID GetBrettList(struct List *brettdata_list, struct List *list, STRPTR brett)
{
	struct Brett *entry, *e;
	STRPTR ptr;
	char buffer[81];
	LONG len;

	RemList(list);

	len = strlen(brett);
	entry = GetHead(brettdata_list);
	e = AllocVec(sizeof(struct Brett), MEMF_ANY | MEMF_CLEAR);

	while(entry && e)
	{
		strcpy(buffer, entry->name);
		buffer[len] = 0;
		if(!strcmp(buffer, brett))
		{
			strcpy(buffer, &entry->name[len]);
			if(ptr = strchr(buffer, '/'))
			{
				*ptr = 0;
				if(!(FindName(list, buffer)))
				{
					strcpy(e->name, buffer);
					e->node.ln_Pri = 1;
				}
			}
			else
			{
				strcpy(e->name, buffer);
				e->node.ln_Pri = 0;
			}
		}
		if(e->name[0])
		{
			e->gruppe		= entry->gruppe;
			e->flags			= entry->flags;
			e->read_level	= entry->read_level;
			e->write_level	= entry->write_level;
			if(e->node.ln_Pri)	strcpy(e->comment, "(DIR)");
			else						strcpy(e->comment, entry->comment);

			e->node.ln_Name = e->name;
			e->node.ln_Type = NT_MEMORY;

			Enqueue(list, e);
			e = AllocVec(sizeof(struct Brett), MEMF_ANY | MEMF_CLEAR);
		}
		entry = GetSucc(entry);
	}
	if(e)	FreeVec(e);
	else	SendError("GetBrettList", "couldn't allocate memory");
}

VOID ShowBrettList(struct List *list)
{
	struct Brett *entry;
	LONG line, i;

	ClrScr();

	i		= 0;
	entry	= GetHead(list);
	line	= (User.lines ? 0 : -1);

	while(entry && wait_buffer[0] != 'Q' && (connected || login))
	{
		if(User.partial_scroll)	Csi(DEL_RO_LINE);

		color(CYAN);
		MyPrintf("%-2ld ", ++i);
		color(YELLOW);
		MyPrintf("%-30ls  ", entry->name);
		color(GREEN);
		MyPrintf("%.44ls\r\n", entry->comment);

		entry = GetSucc(entry);

		if(line > -1)	line++;
		if(entry)	CheckLines(&line);
		if(!line && User.partial_scroll)	gotoxy(1, 1);
	}
	if(User.partial_scroll && wait_buffer[0] != 'Q' && (connected | login))
	{
		while(line++ < User.lines - 1)
		{
			Csi(DEL_RO_LINE);
			Return();
		}
	}
}

VOID GetMailList(STRPTR path, struct List *list, BOOL pmail)
{
	struct Mail *entry;
	STRPTR ptr, buf, buffer;
	LONG size;
	char file[256];

	RemList(list);

	SPrintf(file, "%lsIndex", path);
	if(!(buffer = GetFile(file, &size)))	return;

	buf = buffer;
	SPrintf(file, "%ls %ls", User.first_name, User.name);
	entry = AllocVec(sizeof(struct Mail), MEMF_ANY | MEMF_CLEAR);
	while(buf < buffer + size && entry)
	{
		skip:
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Empfaenger, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Betreff, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Absender, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Datum, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Pfad, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->MsgId, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Typ, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Laenge, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(entry->Filename, buf);
			buf = ptr + 1;

			if(pmail)
			{
				if(!Compare(entry->Empfaenger, User.alias) &&
					 !Compare(entry->Empfaenger, file) &&
					 !Compare(entry->Empfaenger, User.number))
					 goto skip;
			}

			entry->node.ln_Name = entry->Filename;
			entry->node.ln_Type = NT_MEMORY;

			AddTail(list, entry);
			entry = AllocVec(sizeof(struct Mail), MEMF_ANY | MEMF_CLEAR);
		}
		else	buf = buffer + size;
	}

	if(entry)	FreeVec(entry);
	else	SendError("GetMailList", "couldn't allocate memmory");

	FreeVec(buffer);
}

VOID ShowMail(STRPTR path, struct Mail *entry)
{
	if(entry->Typ[0] == 'T')
	{
		ClrScr();

		color(CYAN);
		MyPrintf("An   : ");
		color(GREEN);
		MyPrintf("%-41.41ls ", entry->Empfaenger);

		color(CYAN);
		MyPrintf("Pfad : ");
		color(GREEN);
		if(strlen(entry->Pfad) > 22)	MyPrintf("%.22ls\r\n", &entry->Pfad[strlen(entry->Pfad) - 22]);
		else MyPrintf("%ls\r\n", entry->Pfad);

		color(CYAN);
		MyPrintf("Von  : ");
		color(GREEN);
		MyPrintf("%-41.41ls ", entry->Absender);

		color(CYAN);
		MyPrintf("Datum: ");
		color(GREEN);
		MyPrintf("%.2ls-%.2ls-%.2ls  %.2ls:%.2ls\r\n", &entry->Datum[4], &entry->Datum[2], &entry->Datum[0], &entry->Datum[6], &entry->Datum[8]);

		color(CYAN);
		MyPrintf("Betr.: ");
		color(GREEN);
		MyPrintf("%ls\r\n", entry->Betreff);

		color(YELLOW);
		MyPrintf("------------------------------------------------------------------------------\r\n");

		color(WHITE);
		ShowFile("%ls%ls.i", path, entry->Filename);
		SPrintf(out_buffer, "las #%ls", entry->Filename);
		Location(out_buffer);

		User.mails_read++;
	}
	else	FileInfo(entry);
}

BOOL WriteMail(STRPTR path, STRPTR user, STRPTR betr)
{
	struct Mail entry;
	char file[256];
	BPTR FH;
	LONG size;

	Location("Mail schreiben");

	entry.Empfaenger[0] = 0;
	if(strstr(path, Config.mail))
	{
		SPrintf(entry.Empfaenger, "/%ls", &path[strlen(Config.mail)]);
		if(entry.Empfaenger[strlen(entry.Empfaenger) - 1] == '/')
			entry.Empfaenger[strlen(entry.Empfaenger) - 1] = 0;
	}
	if(user)	strcpy(entry.Empfaenger, user);

	if(!entry.Empfaenger[0])
	{
		Ask("Empfaenger : ", NULL, -1, TRUE);
		if(!connected && !login)	return(FALSE);
		if(!wait_buffer[0])
		{
			color(WHITE);
			MyPrintf("<Abbruch>\r\n");
			return(FALSE);
		}
		strcpy(entry.Empfaenger, wait_buffer);
		strupr(entry.Empfaenger);
	}
	else
	{
		color(CYAN);
		MyPrintf("Empfaenger : ");
		color(GREEN);
		MyPrintf("%ls\r\n", entry.Empfaenger);
	}

	Ask("Betreff : ", betr, 40, TRUE);
	if(!connected && !login)	return(FALSE);
	if(!wait_buffer[0])
	{
		MyPrintf("<Abbruch>\r\n");
		return(FALSE);
	}
	strcpy(entry.Betreff, wait_buffer);

	strcpy(entry.Absender, User.alias);
	SPrintf(entry.Datum, "%.2ls%.2ls%.2ls%.2ls%.2ls", &date[6], &date[3], &date[0], &time[0], &time[3]);
	entry.Pfad[0] = 0;
	SPrintf(entry.MsgId, "%ld%.02ld.%ld@%ls", RangeRand(9999), micros, Status.mail_counter++, Config.netz_name);
	entry.MsgId[19] = 0;
	strcpy(entry.Typ, "T");

	SPrintf(file, "%lsmail", Config.temp);
	if(Editor(file, entry.Empfaenger, entry.Betreff))
	{
		UBYTE *buf;

		SPrintf(entry.Laenge, "%ld", GetFileSize(file));

		SPrintf(file, "%lsPUFFER", Config.temp);
		if(!(FH = COpen(file)))
			SendError("WriteMail", "couldn't open/create PUFFER !");
		else
		{
			Seek(FH, 0L, OFFSET_END);
			SPrintf(out_buffer, "%ls\r\n", entry.Empfaenger);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.Betreff);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.Absender);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.Datum);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.Pfad);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.MsgId);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.Typ);
			Write(FH, out_buffer, strlen(out_buffer));
			SPrintf(out_buffer, "%ls\r\n", entry.Laenge);
			Write(FH, out_buffer, strlen(out_buffer));

			SPrintf(file, "%lsmail", Config.temp);
			if(buf = GetFile(file, &size))
			{
				Write(FH, buf, strlen(buf));
				FreeVec(buf);
				DeleteFile(file);
			}
			Close(FH);

			color(WHITE);
			MyPrintf("\r\nDie Mail wird nach dem Logoff einsortiert...\r\n\r\n");

			do_sort = TRUE;

			User.mails_sent++;
			Log.messages++;
			Status.mails_today++;

			SPrintf(out_buffer, "schrieb %ls", entry.MsgId);
			Location(out_buffer);

			return(TRUE);
		}
	}

	return(FALSE);
}

BOOL DeleteMail(STRPTR path, struct List *originalmail_list, struct List *del_list)
{
	struct List CompleteMailList, *mail_list;
	struct Line *line;
	struct Mail *mail;
	char file[256];
	BPTR FH;

	if(!(line = GetHead(del_list)) || !(mail = GetHead(originalmail_list)))	return(FALSE);

/************************************************************************************
 * bei PMails ist MailList nicht vollständig !! Deshalb ausweichen auf CompleteList *
 ************************************************************************************/

	NewList(&CompleteMailList);
	if(strstr(path, Config.pmail))
	{
		GetMailList(path, &CompleteMailList, FALSE);

		mail_list = &CompleteMailList;
	}
	else	mail_list = originalmail_list;

	line = GetHead(del_list);
	while(line)
	{
		if(mail = FindName(mail_list, line->text))
		{
			SPrintf(file, "%ls %ls", User.first_name, User.name);
			if(!strstr(path, Config.pmail) && !Compare(mail->Absender, User.alias) &&
				!Compare(mail->Absender, file) &&
				!Compare(mail->Absender, User.number) && User.level < 90)
			{
				color(YELLOW);
				MyPrintf("Mail mit dem Betreff: '%ls'\r\nwurde nicht gelöscht. Kein Zugriff.\r\n", mail->Betreff);
			}
			else
			{
				SPrintf(file, "%ls%ls", path, mail->Filename);
				DeleteFile(file);

				SPrintf(out_buffer, "%ls WURDE GELOESCHT !!", file);
				Location(out_buffer);
				color(WHITE);
				MyPrintf("Mail (%ls) wurde geloescht...\r\n", mail->Filename);

				Remove(mail);
				FreeVec(mail);

				if(strstr(path, Config.pmail))
				{
					if(mail = FindName(originalmail_list, line->text))
					{
						Remove(mail);
						FreeVec(mail);
					}
				}
			}
		}
		else	SendError("DeleteMail", "mail not found");

		line = GetSucc(line);
	}

	SPrintf(file, "%lsIndex", path);
	if(!(FH = Open(file, MODE_NEWFILE)))	return(FALSE);

	mail = GetHead(mail_list);
	while(mail)
	{
		FPrintf(FH, "%ls\n%ls\n%ls\n%ls\n%ls\n%ls\n%ls\n%ls\n%ls\n", mail->Empfaenger, mail->Betreff, mail->Absender, mail->Datum, mail->Pfad, mail->MsgId, mail->Typ, mail->Laenge, mail->Filename);
		mail = GetSucc(mail);
	}
	Close(FH);

	RemList(&CompleteMailList);

	return(TRUE);
}

BOOL ReplyMail(STRPTR path, struct Mail *entry)
{
	LONG i;
	BOOL ret;
	char file[256], betreff[81];

	if(entry->Filename[0])
	{
		char file2[256];
		BPTR FH;

		SPrintf(file, "%lsQuote", Config.temp);
		if(FH = Open(file, MODE_NEWFILE))
		{
			FPrintf(FH, "%ls schrieb am %.2ls.%.2ls.%.2ls um %.2ls:%.2ls Uhr unter dem Betreff \r\n'%ls' folgendes :\r\n\r\n", entry->Absender, &entry->Datum[4], &entry->Datum[2], entry->Datum, &entry->Datum[6], &entry->Datum[8], entry->Betreff);
			Close(FH);
		}
		SPrintf(file2, "%ls%ls", path, entry->Filename);
		Append(file, file2);
	}

	strcpy(betreff, entry->Betreff);
	betreff[3] = 0;
	if(Compare(betreff, "re:"))	SPrintf(betreff, "Re^2:%ls", &entry->Betreff[3]);
	else
	{
		if(Compare(betreff, "re^"))
		{
			strcpy(betreff, &entry->Betreff[3]);
			if(isdigit(betreff[1]))	betreff[2] = 0;
			else	betreff[1] = 0;
	
			i = atol(betreff);
			SPrintf(betreff, "Re^%ld:%ls", i + 1, &entry->Betreff[5 + (i > 9)]);
		}
		else	SPrintf(betreff, "Re: %ls", entry->Betreff);
	}
	betreff[41] = 0;

	strcpy(file, entry->Absender);
	if(!strstr(path, Config.pmail))
	{
		Ask("Private Antwort [y/N]", NULL, HOTKEY, TRUE);
		if(!connected && !login)	return(FALSE);

		if(toupper(wait_buffer[0]) != 'Y')
		{
			SPrintf(file, "/%ls", &path[strlen(Config.mail)]);
			file[strlen(file) - 1] = 0;
		}
	}

	ret = WriteMail((toupper(wait_buffer[0]) == 'Y' ? Config.pmail : path), file, betreff);

	SPrintf(file, "%lsQuote", Config.temp);
	DeleteFile(file);

	return(ret);
}

VOID ListMail(struct List *list)
{
	struct Mail *entry;
	LONG line, i;
	BOOL files;

	if(!GetHead(list))
	{
		color(WHITE);
		MyPrintf("Dieses Brett ist noch leer.\r\n");
		return;
	}

	files = TRUE;
	entry = GetHead(list);
	while(entry && files)
	{
		if(entry->Typ[0] == 'T')	files = FALSE;
		entry = GetSucc(entry);
	}

	ClrScr();

	color(WHITE);
	if(files)
		MyPrintf("#   Filename         Laenge Beschreibung\r\n");
	else
		MyPrintf("#   Absender       Betrifft\r\n");

	color(BLUE);
	MyPrintf("------------------------------------------------------------------------------\r\n");

	i		= 0;
	entry	= GetHead(list);
	line	= (User.lines ? 2 : -1);

	while(entry && wait_buffer[0] != 'Q' && (connected || login))
	{
		if(User.partial_scroll)	Csi(DEL_RO_LINE);

		color(CYAN);
		MyPrintf("%-3ld ", ++i);

		if(files)
		{
			color(CYAN);
			MyPrintf("%-16.16ls ", entry->Filename);
		}
		else
		{
			color(GREEN);
			MyPrintf("%-14.14ls ", entry->Absender);
		}

		if(files)
		{
			color(WHITE);
			MyPrintf("%6ls ", entry->Laenge);
		}

		if(!files && entry->Typ[0] != 'T')
		{
			color(CYAN);
			MyPrintf("%ls", entry->Filename);
			color(WHITE);
			MyPrintf(" (%ls Bytes)\r\n", entry->Laenge);
			if(strcmp(entry->Betreff, entry->Filename))
			{
				color(YELLOW);
				MyPrintf("                    %ls\r\n", entry->Betreff);
				if(line > -1)	line++;
			}
		}
		else
		{
			color(YELLOW);
			MyPrintf("%ls\r\n", entry->Betreff);
		}

		entry = GetSucc(entry);
		if(line > -1)	line++;
		if(entry)	CheckLines(&line);
		if(!line && User.partial_scroll)
		{
			gotoxy(1, 3);
			line = 2;
		}
	}
	if(User.partial_scroll && wait_buffer[0] != 'Q' && (connected | login))
	{
		while(line++ < User.lines - 1)
		{
			Csi(DEL_RO_LINE);
			Return();
		}
	}
}

BOOL ShowNewMail_Date(struct Brett *b_entry, struct DateStamp *user_st, LONG *line)
{
	struct List MailList;
	struct Mail *entry;
	BOOL found, err;
	BPTR lock;
	struct FileInfoBlock *finfo;
	char file[256];

	found = FALSE;

	NewList(&MailList);
	if(b_entry)	SPrintf(file, "%ls%ls/", Config.mail, b_entry->name);
	else	strcpy(file, Config.pmail);
	GetMailList(file, &MailList, (b_entry ? FALSE : TRUE));
	entry = GetHead(&MailList);

	if(lock = Lock(file, ACCESS_READ))
	{
		if(finfo = AllocVec(sizeof(struct FileInfoBlock), MEMF_ANY | MEMF_CLEAR))
		{
			if(Examine(lock, finfo) != DOSFALSE)
			{
				do
				{
					ExNext(lock, finfo);
					err = IoErr();
					if(!err && !Compare(finfo->fib_FileName, "Index") &&
						!Compare(finfo->fib_FileName, "UserRead") &&
						finfo->fib_Date.ds_Days >= user_st->ds_Days &&
						finfo->fib_DirEntryType < 0)
					{
						if(entry = FindName(&MailList, finfo->fib_FileName))
						{
							if(!found)
							{
								found = TRUE;
								Return();
							}

							color(GREEN);
							if(entry->Typ[0] != 'T')
							{
								MyPrintf("%-16.16ls ", entry->Filename);
								color(WHITE);
								MyPrintf("%6ls ", entry->Laenge);
							}
							else	MyPrintf("%-14.14ls ", entry->Absender);

							color(YELLOW);
							MyPrintf("%ls\r\n", entry->Betreff);

							if(*line > -1)	*line += 1;
							CheckLines(line);
						}
						else
						{
							if(b_entry)
							{
								SPrintf(file, "Mail (%ls) gefunden ohne Eintrag in %ls/Index", finfo->fib_FileName, b_entry->name);
								SendError("ShowNewMail_Date", file);
							}
						}
					}
				}	while(!err && toupper(wait_buffer[0]) != 'Q');
			}
			FreeVec(finfo);
		}
		UnLock(lock);
	}

	RemList(&MailList);
	return(found);
}

VOID NewMail(struct List *list, STRPTR where)
{
	struct Brett *entry;
	struct DateTime user_dt;
	char da[LEN_DATSTRING];
	LONG line;

	SPrintf(da, "%.8ls", User.time_last);
	Ask("Ab welchen Datum [DD-MM-YY] : ", da, 9, TRUE);
	if(!connected && !login)	return;
	SPrintf(da, "%.8ls", wait_buffer);

	user_dt.dat_StrDate	= da;
	user_dt.dat_StrTime	= "00:00:01";
	user_dt.dat_Format	= FORMAT_CDN;
	user_dt.dat_Flags		= NULL;

	if(!StrToDate(&user_dt))
	{
		color(YELLOW);
		MyPrintf("Invalid date\r\n");
		return;
	}

	line = (User.lines ? 0 : -1);
	entry = GetHead(list);

	while(entry && wait_buffer[0] != 'Q' && (connected || login))
	{
		if((strstr(entry->name, where) == entry->name || !*where) &&
			entry->read_level <= User.level)
		{
			color(YELLOW);
			MyPrintf("%-40ls  ", entry->name);

			if(!ShowNewMail_Date(entry, &user_dt.dat_Stamp, &line))
				Char(CARRIAGE_RETURN);
			else
			{
				if(line > -1)	line++;
				if(toupper(wait_buffer[0]) != 'Q')	CheckLines(&line);
			}
			if(toupper(wait_buffer[0]) == 'Q')
			{
				Ask("Abort global scan [y/N] : ", NULL, HOTKEY, FALSE);
				if((!connected && !login) || toupper(wait_buffer[0]) == 'Y')
				{
					Return();
					return;
				}
			}
		}
		entry = GetSucc(entry);
	}
}

VOID ReadMail(STRPTR path, struct Brett *b_entry)
{
	struct List MailList;
	struct Mail *entry;
	struct DateTime user_dt;
	char da[LEN_DATSTRING], buffer[82];
	LONG anz, current, i;
	BOOL stop;

	Location(path);
	MakePath(path);

	NewList(&MailList);
	GetMailList(path, &MailList, (strstr(path, Config.pmail) ? TRUE : FALSE));

	current = 0;
	if(entry = GetHead(&MailList))
	{
		char file[256];

		anz = 1;
		while(entry = GetSucc(entry))	anz++;

		current = 0;
		SPrintf(file, "%lsUserRead", path);
		if(LoadLine(file, atol(User.number), buffer))
		{
			entry = GetHead(&MailList);
			while(entry && strcmp(entry->MsgId, buffer))
			{
				entry = GetSucc(entry);
				current++;
			}
			if(entry)	current++;
			else current = 0;
		}
		if(!entry)	entry = GetHead(&MailList);
	}
	else
	{
		anz = 0;
		color(WHITE);
		MyPrintf("\r\nDieses Brett ist noch leer...\r\n");
	}

	stop = FALSE;
	do
	{
		if(User.help_level == 1)
		{
			color(WHITE);
			MyPrintf("(Commands: #, +, -, S, R, U, D, N, I, L, A, E, V, C, G, Z, M, ?)\r\n");
		}
		if(strstr(path, Config.pmail))
			SPrintf(buffer, "PMail: [%ld/%ld] : ", current, anz);
		else
			SPrintf(buffer, "%ls:%ls [%ld/%ld] : ", Config.box_name, &path[strlen(Config.mail)], current, anz);
		Ask(buffer, NULL, -1, TRUE);

		switch(toupper(wait_buffer[0]))
		{
			case '+':
			case 0:
				if(!current)	entry = GetHead(&MailList);
				else				entry = GetSucc(entry);
				if(entry)
				{
 					current++;
					ShowMail(path, entry);
				}
				else
				{
					color(YELLOW);
					MyPrintf("Keine weiteren Mails/Files gefunden. (? fuer Hilfe)\r\n");

					entry = GetTail(&MailList);
				}
				break;

			case 'X':
				if(Access(256, 0))
				{
					STRPTR ptr;
					char file[256];

					strcpy(buffer, entry->Betreff);
					while(ptr = strchr(buffer, ' '))	*ptr = '_';
					while(ptr = strchr(buffer, ':'))	*ptr = '.';
					while(ptr = strchr(buffer, '/'))	*ptr = '-';
					buffer[20] = 0;

					SPrintf(file, "copy %ls%ls ram:%ls", path, entry->Filename, buffer);
					StartPrg(file);
				}
				break;

			case 'S':
				if(Access((b_entry ? b_entry->write_level : 0), 0))
				{
					WriteMail(path, NULL, NULL);
				}
				break;

			case 'R':
				if(Access((b_entry ? b_entry->write_level : 0), 0))
				{
					ReplyMail(path, entry);
				}
				break;

			case 'U':
				if(Access((b_entry ? b_entry->write_level : 0), 0))
				{
					Upload(path);
				}
				break;

			case 'D':
				Download(path, &MailList);
				break;

			case 'N':
				SPrintf(da, "%.8ls", User.time_last);
				Ask("Ab welchen Datum [DD-MM-YY] : ", da, 9, TRUE);
				if((!connected && !login) || !login)	goto exit;
				SPrintf(da, "%.8ls", wait_buffer);

				user_dt.dat_StrDate	= da;
				user_dt.dat_StrTime	= "00:00:01";
				user_dt.dat_Format	= FORMAT_CDN;
				user_dt.dat_Flags		= NULL;

				if(!StrToDate(&user_dt))
				{
					color(YELLOW);
					MyPrintf("Ungültigs Datum.\r\n");
				}
				else
				{
					LONG line;

					line = 0;
					ShowNewMail_Date(b_entry, &user_dt.dat_Stamp, &line);
				}
				break;

			case 'I':
				ListMail(&MailList);
				break;

			case 'L':
				if(entry)
				{
					struct List DelList;
					struct Line *line;
					struct Mail *e;

					NewList(&DelList);
					if(line = GetLine())
					{
						e = GetPred(entry);
						strcpy(line->text, entry->Filename);
						AddTail(&DelList, line);

						if(DeleteMail(path, &MailList, &DelList))
						{
							anz--;
							current--;
							entry = e;
						}
						RemList(&DelList);
					}
				}
				break;

			case 'A':
				ShowMail(path, entry);
				break;

			case 'V':
				ViewFile(path, entry);
				break;

			case '-':
				if(GetPred(entry))
				{
					current--;
					entry = GetPred(entry);
					ShowMail(path, entry);
				}
				else
				{
					color(WHITE);
					MyPrintf("Keine weiteren Mails/Files gefunden...\r\n");
				}
				break;

			case '?':
				ClrScr();
				ShowFile("%lsMailMenu.txt", Config.text);
				if(!connected && !login)	goto exit;
				break;

			case 'G':
				Goodbye();
				if(!connected && !login)	goto exit;
				break;

			case 'C':
				ChatRequest();
				break;

			case 'Z':
			case 'M':
				stop = TRUE;
				break;

			default:
				if(atol(wait_buffer))
				{
					if(entry = GetHead(&MailList))
					{
						i = 1;
						while(atol(wait_buffer) > i && GetSucc(entry))
						{
							entry = GetSucc(entry);
							i++;
						}
						if(i == atol(wait_buffer))
						{
							current = i;
							ShowMail(path, entry);
						}
						else
						{
							color(YELLOW);
							MyPrintf("Mail/File nicht gefunden.\r\n");
						}
					}
					else
					{
						color(YELLOW);
						MyPrintf("Dieses Brett ist noch leer.\r\n");
					}
				}
				else
				{
					color(WHITE);
					MyPrintf("Ungültige Eingabe. (? fuer Hilfe)\r\n");
				}
				break;
		}
	}	while((connected || login) && !stop);

	if(toupper(wait_buffer[0]) == 'M')	Location("Main Menu");

	exit:
	if(entry && !Compare(User.alias, "GAST") && *entry->MsgId)
	{
		char file[256];

		SPrintf(file, "%lsUserRead", path);
		ReplaceLine(file, atol(User.number), entry->MsgId);
	}

	RemList(&MailList);
}


/*
							if(filebox)
							{
								color(CYAN);
								SPrintf(out_buffer, "%-16.16ls ", entry->Filename);
								Send();
								color(WHITE);
								SPrintf(out_buffer, "%6ls ", entry->Laenge);
								Send();
								color(YELLOW);
								SPrintf(out_buffer, "%-50.50ls\r\n", entry->Betreff);
								Send();
								if(*line > -1)	*line += 1;
								CheckLines(line);
							}
							else
							{
								if(*line > 0)	*line = 0;
								ShowMail(path, entry);

								Ask("Reply, Next, Again, Quit [r/N/a/q] : ", NULL, HOTKEY, TRUE);
								switch(toupper(wait_buffer[0]))
								{
									case 'R':
										if(Access((b_entry ? b_entry->write_level : 0), 0))
										{
											ReplyMail(path, entry->Filename, entry->Absender, entry->Betreff, entry->Datum);
										}
										break;

									case 'A':
										ShowMail(path, entry);
										break;

									default:
										break;
								}
							}

	if(*entry->MsgId && entry && strlen(entry->MsgId) < 50 && !Compare(User.alias, "GAST"))
	{
		char file[256];

		SPrintf(file, "%lsUserRead", path);
		ReplaceLine(file, atol(User.number), entry->MsgId);
	}


VOID ShowNewMail_ID(STRPTR path)
{
	struct List MailList;
	struct Mail *entry;
	char file[256], buffer[81], msgid[81];
	BOOL stop;

	NewList(&MailList);
	GetMailList(path, &MailList, strstr(path, Config.pmail));
	SPrintf(file, "%lsUserRead", path);
	LoadLine(file, atol(User.number), buffer);
	entry = GetHead(&MailList);
	while(entry && strcmp(entry->MsgId, buffer))
	{
		entry = GetSucc(entry);
	}
	if(!strcmp(entry->MsgId, buffer))	entry = GetSucc(entry);
	while(entry && Compare(User.alias, entry->Absender))
		entry = GetSucc(entry);

	msgid[0] = '\0';

	stop = FALSE;
	while(entry && !stop && (connected || login))
	{
		ShowMail(path, entry);
		strcpy(msgid, entry->MsgId);

		Ask("Reply, Next, Last, Again, Quit [r/N/l/a/q] : ", NULL, HOTKEY, TRUE);
		switch(toupper(wait_buffer[0]))
		{
			case 'R':
if access
				ReplyMail(path, entry->Filename, entry->Absender, entry->Betreff, entry->Datum);
				break;

			case 'L':
				do
				{
					entry = GetPred(entry);
				}	while(entry && Compare(entry->Absender, User.alias));
				if(!entry)	entry = GetHead(&MailList);
				break;

			case 'A':
				break;

			case 'Q':
				stop = TRUE;
				break;

			default:
				do
				{
					entry = GetSucc(entry);
					if(entry)	strcpy(msgid, entry->MsgId);
				}	while(entry && Compare(entry->Absender, User.alias));
				break;
		}
	}

	if(msgid[0] && strlen(msgid) < 50)
	{
		SPrintf(file, "%lsUserRead", path);
		ReplaceLine(file, atol(User.number), msgid);
	}
	else
	{
		color(YELLOW);
		strcpy(out_buffer, "none found..\r\n");
		Send();
	}
	RemList(&MailList);
}
*/
