#include "/Mailbox.h"
#include "protos.h"

VOID EscapeString(UBYTE *);
STRPTR GetFile(STRPTR, LONG *);
BOOL Compare(STRPTR, STRPTR);
BPTR COpen(STRPTR);

VOID __saveds __asm SendError(register __a0 STRPTR routine, register __a1 STRPTR string)
{
	Printf("ERROR in %ls() :\n%ls\n", routine, string);
}

BOOL __saveds __asm GetConfig(register __a0 STRPTR file, register __a1 struct Config *config, register __a2 struct Status *status)
{
	BPTR FH;

	if(!config || !file)	return(FALSE);

	if(FH = Open(file, MODE_OLDFILE))
	{
		Seek(FH, 0, OFFSET_END);
		if(Seek(FH, 0, OFFSET_BEGINNING) == sizeof(struct Config))
		{
			Read(FH, config, sizeof(struct Config));
		}
		else
		{
			Close(FH);
			SendError("GetConfig", "Ungültiges Config-File");
			return(FALSE);
		}
		Close(FH);
	}
	else
	{
		SendError("GetConfig", "Konnte Config-File nicht öffnen");
		return(FALSE);
	}

	if(status)
	{
		char file[256];
		LONG i;

		SPrintf(file, "%lsStatus", config->system);
		if(FH = Open(file, MODE_OLDFILE))
		{
			Seek(FH, 0, OFFSET_END);
			if(Seek(FH, 0, OFFSET_BEGINNING) == sizeof(struct Status))
			{
				Read(FH, status, sizeof(struct Status));
			}
			else
			{
				Close(FH);
				SendError("GetConfig", "Ungültiges Status-File");
				return(FALSE);
			}
			Close(FH);
		}
		else
		{
			strcpy(status->wartung_date, "01-01-1979");
			status->sysop	= TRUE;
			status->window	= TRUE;
		}

		if(status->colors[0] == status->colors[4] == status->colors[6]	== status->colors[7])
		{
			status->colors[0] = 0x000;
			status->colors[4] = 0x00f;
			status->colors[6] = 0x0ff;
			status->colors[7] = 0xfff;
		}

		i = 12;
		while(i--)	EscapeString(config->ansi_colors[i]);
	}

	return(TRUE);
}

BOOL __saveds __asm GetProtocolList(register __a0 STRPTR file, register __a1 struct List *list)
{
	STRPTR buffer, buf, ptr;
	struct Protocol *protocol;
	LONG size;

	NewList(list);

	if(!(buffer = GetFile(file, &size)))
	{
		SendError("GetProtocolList", "Konnte Protokoll-Liste nicht öffnen");
		return(FALSE);
	}

	buf = buffer;
	protocol = AllocVec(sizeof(struct Protocol), MEMF_ANY | MEMF_CLEAR);
	while(protocol && buf < buffer + size)
	{
		protocol->batch = TRUE;
		if(*buf == '*')
		{
			protocol->batch = FALSE;
			buf++;
		}
		if(ptr = strchr(buf, ' '))
		{
			*ptr = 0;
			SPrintf(protocol->library, "%.40ls", buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, ' '))
		{
			*ptr = 0;
			SPrintf(protocol->options, "%.40ls", buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			SPrintf(protocol->name, "%.40ls", buf);
			buf = ptr + 1;
			protocol->node.ln_Name = protocol->name;

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

	if(protocol)	FreeVec(protocol);
	else
	{
		SendError("GetProtocolList", "Konnte Speicher für Protokoll-Liste nicht anfordern");
		return(FALSE);
	}
}

BOOL __saveds __asm RefreshStatus(register __a0 STRPTR file, register __a1 struct Status *status)
{
	BPTR FH;

	if(FH = Open(file, MODE_NEWFILE))
	{
		Write(FH, status, sizeof(struct Status));
		Close(FH);
	}
	else
	{
		SendError("RefreshStatus", "Konnte Status-File nicht speichern");
		return(FALSE);
	}

	return(TRUE);
}

BOOL __saveds __asm RefreshUser(register __a0 STRPTR file, register __a1 struct User *user)
{
	BPTR FH;

	if(Compare(user->alias, "GAST"))	return(TRUE);

	if(FH = Open(file, MODE_NEWFILE))
	{
		FPrintf(FH, "%ls\n", user->password);
		FPrintf(FH, "%ls\n", user->street);
		FPrintf(FH, "%ls\n", user->place);
		FPrintf(FH, "%ls\n", user->phone);
		FPrintf(FH, "%ls\n", user->birthday);
		FPrintf(FH, "%ls\n", user->terminal);
		FPrintf(FH, "%ls\n", user->time_last);
		FPrintf(FH, "%ld\n", user->level);
		FPrintf(FH, "%ld\n", user->uploads);
		FPrintf(FH, "%ld\n", user->downloads);
		FPrintf(FH, "%ld\n", user->time);
		FPrintf(FH, "%ld\n", user->time_used);
		FPrintf(FH, "%ld\n", user->time_on);
		FPrintf(FH, "%ld\n", user->calls);
		FPrintf(FH, "%ld\n", user->mails_sent);
		FPrintf(FH, "%ld\n", user->mail_count);
		FPrintf(FH, "%ld\n", user->pw_try);
		FPrintf(FH, "%ld\n", user->bytes_upload);
		FPrintf(FH, "%ld\n", user->bytes_download);
		FPrintf(FH, "%ld\n", user->hotkeys);
		FPrintf(FH, "%ld\n", user->zone);
		FPrintf(FH, "%ld\n", user->lines);
		FPrintf(FH, "%ld\n", user->ansi);
		FPrintf(FH, "%ld\n", user->file_count);
		FPrintf(FH, "%ld\n", user->protocol);
		FPrintf(FH, "%ld\n", user->editor);
		FPrintf(FH, "%ld\n", user->help_level);
		FPrintf(FH, "%ls\n", user->time_joined);
		FPrintf(FH, "%ls\n", user->ud_ratio);
		FPrintf(FH, "%ld\n", user->b_ratio);
		FPrintf(FH, "%ld\n", user->ul_first);
		FPrintf(FH, "%ld\n", user->ul_already);
		FPrintf(FH, "%ld\n", user->mails_read);
		FPrintf(FH, "%ld\n", user->chat_requests);
		FPrintf(FH, "%ld\n", user->screenblank);
		FPrintf(FH, "%ld\n", user->partial_scroll);
		FPrintf(FH, "%ls\n", user->vertreter);
		FPrintf(FH, "%ld\n", user->amiga);

		Close(FH);
	}
	else
	{
		SendError("RefreshUser", "Konnte User-File nicht speichern");
		return(FALSE);
	}

	return(TRUE);
}

BOOL __saveds __asm CheckUser(register __a0 STRPTR file, register __a1 STRPTR name, register __a2 struct User *user)
{
	STRPTR buf, ptr, buffer, alias, vorname, nachname, nummer;
	LONG size;
	BOOL found;
	char temp[82];

	if(user)
	{
		user->password[0] = user->street[0] = user->place[0] = user->phone[0] =
		user->birthday[0] = user->terminal[0] = user->time_last[0] = user->level =
		user->uploads = user->downloads = user->time = user->time_used =
		user->time_on = user->calls = user->mails_sent = user->mail_count =
		user->pw_try = user->bytes_upload = user->bytes_download = user->hotkeys =
		user->zone = user->lines = user->ansi = user->file_count = user->protocol =
		user->editor = user->help_level = user->time_joined[0] = user->ud_ratio[0] =
		user->b_ratio = user->ul_first = user->ul_already = user->mails_read =
		user->chat_requests = user->screenblank = user->partial_scroll =
		user->vertreter[0] = user->amiga = NULL;
	}
	if(Compare(name, "GAST") && user)
	{
		strcpy(user->alias, "GAST");
		strcpy(user->first_name, "unbekannt");
		strcpy(user->name, "unbekannt");
		user->number[0] = 0;
	}
	if(Compare(name, "GAST"))	return(TRUE);

	if(buffer = GetFile(file, &size))
	{
		found	= FALSE;
		buf = buffer;

		while((buf < buffer + size) && !found)
		{
			if(ptr = strchr(buf, '\n'))
			{
				*ptr = 0;
				alias = buf;
				buf = ptr + 1;
			}
			if(ptr = strchr(buf, '\n'))
			{
				*ptr = 0;
				vorname = buf;
				buf = ptr + 1;
			}
			if(ptr = strchr(buf, '\n'))
			{
				*ptr = 0;
				nachname = buf;
				buf = ptr + 1;
			}
			if(ptr = strchr(buf, '\n'))
			{
				*ptr = 0;
				nummer = buf;
				buf = ptr + 1;
			}
			else	buf = buffer + size;

			SPrintf(temp, "%ls %ls", vorname, nachname);
			if(Compare(name, alias) || Compare(name, temp) || Compare(name, nummer))
				found = TRUE;
		}
		if(found && user)
		{
			strcpy(user->alias, alias);
			strcpy(user->first_name, vorname);
			strcpy(user->name, nachname);
			strcpy(user->number, nummer);
		}
		FreeVec(buffer);

		return(found);
	}
	else
	{
		SendError("CheckUser", "Konnte UserListe nicht öffnen, generiere neues File");
		Close(COpen(file));
		return(FALSE);
	}
}

BOOL __saveds __asm LoadUser(register __a0 STRPTR file, register __a1 struct User *user)
{
	STRPTR buf, ptr, buffer;
	LONG size;

	user->ansi				= 2;
	user->screenblank		= user->partial_scroll	= TRUE;
	user->lines				= 23;

	if(Compare(user->alias, "GAST"))
	{
		strcpy(user->password,		"unbekannt");
		strcpy(user->street,			user->password);
		strcpy(user->place,			user->password);
		strcpy(user->phone,			user->password);
		strcpy(user->birthday,		user->password);
		strcpy(user->terminal,		user->password);
		strcpy(user->time_last,		"01-01-79 00:00:00");
		user->time	= 3600;
		user->level	= 10;

		return(TRUE);
	}

	if(buffer = GetFile(file, &size))
	{
		buf = buffer;

		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->password, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->street, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->place, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->phone, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->birthday, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->terminal, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->time_last, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->level = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->uploads = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->downloads = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->time = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->time_used = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->time_on = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->calls = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->mails_sent = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->mail_count = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->pw_try = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->bytes_upload = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->bytes_download = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->hotkeys = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->zone = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->lines  = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->ansi  = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->file_count = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->protocol = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->editor = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->help_level = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->time_joined, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->ud_ratio, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->b_ratio = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->ul_first = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->ul_already = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->mails_read = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->chat_requests = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->screenblank = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->partial_scroll = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(user->vertreter, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			user->amiga = atol(buf);
			buf = ptr + 1;
		}

		FreeVec(buffer);
	}
	else
	{
		SendError("LoadUser", "Konnte User-File nicht öffnen");
		return(FALSE);
	}

	user->time_already = user->time_used;

	return(TRUE);
}

BOOL __saveds __asm GetPointDataList(register __a0 STRPTR file, register __a1 struct List *list)
{
	struct Point *point;
	STRPTR ptr, buffer, buf;
	LONG size;

	if(!(buffer = GetFile(file, &size)))
	{
		SendError("GetPointDataList" ,"Konnte PointData nicht öffnen.\n");
		return(FALSE);
	}

	buf = buffer;
	point = AllocVec(sizeof(struct Point), MEMF_ANY | MEMF_CLEAR);
	while(buf < buffer + size && point)
	{
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(point->systemname, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(point->pointname, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(point->passwort, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			point->gruppe = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			point->level = atol(buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(point->packer, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			strcpy(point->entpacker, buf);
			buf = ptr + 1;
		}
		if(ptr = strchr(buf, '\n'))
		{
			*ptr = 0;
			point->protocol = atol(buf);
			buf = ptr + 1;

			point->node.ln_Name = point->systemname;
			point->node.ln_Type = NT_MEMORY;

			AddTail(list, point);
			point = AllocVec(sizeof(struct Point), MEMF_ANY | MEMF_CLEAR);
		}
		else	buf = buffer + size;
	}
	FreeVec(buffer);

	if(point)	FreeVec(point);
	else
	{
		SendError("GetPointDataList", "Konnte Speicher für Point nicht anfordern.\n");
		return(FALSE);
	}

	return(TRUE);
}
