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

struct Protocol *GetProtocol(LONG);

VOID NetCall()
{
	struct List PointDataList;
	struct Point *point;
	struct Line *line;
	struct Protocol *protocol;
	char file[256];

	ResetLog();

	NewList(&PointDataList);
	SPrintf(file, "%lsPointData", Config.mail);
	GetPointDataList(file, &PointDataList);

	MyPrintf("\r\nSystemname:");
	In(80, 0);
	if(!connected && !login)	goto exit;
	Return();

	if(!(point = FindName(&PointDataList, wait_buffer)))	goto exit;

	SPrintf(Log.user, "Netcall von %ls", point->pointname);
	SPrintf(out_buffer, "Netcall von %ls", point->systemname);
	Location(out_buffer);

	MyPrintf("Passwort:");
	In(80, 0);
	if(!connected && !login)	goto exit;
	Return();

	if(!Compare(wait_buffer, point->passwort))
	{
		Location("Falsches Passwort");
		Log.pw_hacking = TRUE;
		goto exit;
	}

	MyPrintf("\r\nrunning ARC\r\nrunning ARC\r\nrunning ARC\r\nrunning ARC\r\n");

	DeleteAllFiles(Config.receive);

	SPrintf(file, "%lsPoints/%ls.PRE", Config.mail, point->systemname);
	if(!Exists(file))
	{
		BPTR FH;

		if(FH = Open(file, MODE_NEWFILE))
		{
			FPrintf(FH, "\r\n");
			Close(FH);
		}
		else
		{
			SendError("Netcall", "Konnte leeres Netcall-File nicht generieren !");
			HangUp();
			goto exit;
		}
	}

	Char(NAK);
	Location("Seriennummern-Abgleich");

/* muss 1 sein, da ASCII(0) sonst gefiltert wird */
	In(5, 1);
	if(!connected && !login)	goto exit;

	ser_out = TRUE;
	Char(ACK);

	ser_out = FALSE;
	protocol = GetProtocol(point->protocol);
	RemList(&FileList);
	if(line = GetLine())
	{
		strcpy(line->text, "CALLER.LZH");
		AddTail(&FileList, line);
	}
	if(DoTransfer(Config.receive, protocol, FALSE))
	{
		if(line = GetLine())
		{
			SPrintf(line->text, "%ls.PRE", point->systemname);
			AddTail(&FileList, line);
		}
		ser_out = FALSE;
		SPrintf(file, "%lsPoints/", Config.mail);
		if(DoTransfer(file, protocol, TRUE))
		{
			HangUp();
			Log.bad_logoff = FALSE;
			RemList(&FileList);

			SPrintf(file, "%lsPoints/%ls.PRE", Config.mail, point->systemname);
			Log.k_dloads += GetFileSize(file) / 1024;
			Log.dloads++;
			DeleteFile(file);

			FindFile(Config.receive, "CALLER.PRE");
			SPrintf(file, "%lsCALLER.PRE", Config.receive);
			Log.k_uloads += GetFileSize(file) / 1024;
			Log.uloads++;

			ATCmd(Config.busy);
			ShowPrg("ZSort C=%ls F=%lsCALLER.PRE N=%ls PRI=%ld S=%ld %ls", configfile, Config.receive, (point->pointname[0] ? point->pointname : point->systemname), Config.priority, (LONG)Scr, (point->pointname[0] ? " POINT" : ""));
			HangUp();

			Location("Netcall beendet");
		}
		else
		{
			HangUp();
			Log.bad_transfers++;
			Location("Senden gescheitert");
		}
	}
	else
	{
		HangUp();
		Log.bad_transfers++;
		Location("Empfangen gescheitert");
	}

	exit:
	RemList(&PointDataList);
	UserLog();
}

VOID DoNetCall(STRPTR hostname, STRPTR dialcommand, STRPTR entpacker, STRPTR passwort, LONG protocol_nummer)
{
	struct Protocol *protocol;
	struct Line *line;
	BOOL stop;
	LONG i;
	char file[256], wait_string[81];

	if(connected || login)	HangUp();

	ResetLog();
	SPrintf(Log.user, "Netcall: %ls", hostname);
	SPrintf(out_buffer, "Netcall: %ls", hostname);
	Location(out_buffer);

	User.time = 1800;
	User.time_used = 0;
	baud = Config.baud;
	ser_out = FALSE;

	SPrintf(file, "%lsPoints/%ls.PRE", Config.mail, hostname);
	if(!Exists(file))
	{
		BPTR FH;

		if(!(FH = Open(file, MODE_NEWFILE)))
		{
			SendError("DoNetcall", "Konnte leeres Netcall-File nicht generieren !");
			HangUp();
			return;
		}
		else
		{
			FPrintf(FH, "\r\n");
			Close(FH);
		}
	}
	DeleteAllFiles(Config.receive);

	ATCmd(dialcommand);

	i = 0;
	do
	{
		ser_out = FALSE;
		In(80, 3);
		if(!wait_buffer[0])	Return();

		if(strstr(wait_buffer, "BUSY") || strstr(wait_buffer, "NO CARRIER"))
		{
			if(i > 20)
			{
				Location("Es konnte keine Verbindung hergestellt werden.");
				return;
			}
			else
			{
				i++;
				Delay(1500);
				SPrintf(out_buffer, "%ld. Versuch", i + 1);
				Location(out_buffer);
				ATCmd(dialcommand);
			}
		}
	}	while(!strstr(wait_buffer, "CONNECT"));

	ser_out = TRUE;
	Char(CTRL_X);

	wait_string[0] = 0;
	connected = TRUE;
	Location("Verbindung hergestellt");

	stop = FALSE;
	do
	{
		ser_out = FALSE;
		ser_in = TRUE;
		In(1, 3);
		if(strlen(wait_string) < 79 && wait_buffer[0])
		{
			wait_string[strlen(wait_string) + 1] = 0;
			wait_string[strlen(wait_string)] = wait_buffer[0];
		}
		else
		{
			wait_string[0] = wait_buffer[0];
			wait_string[1] = 0;
			Return();
		}

		if(instr(wait_string, "Username:"))
		{
			ser_out = TRUE;
			MyPrintf("ZERBERUS\r");
			ser_out = FALSE;
			In(8, 1);
			if(!connected && !login)	stop = TRUE;
			wait_string[0] = 0;
		}

		if(instr(wait_string, "Systemname:"))
		{
			ser_out = TRUE;
			MyPrintf("%ls\r", Config.netz_name);
			ser_out = FALSE;
			In(strlen(Config.netz_name) + 1, 1);
			if(!connected && !login)	stop = TRUE;
			wait_string[0] = 0;
		}

		if(instr(wait_string, "Passwort:"))
		{
			ser_out = TRUE;
			MyPrintf("%ls\r", passwort);
			ser_out = FALSE;
			wait_string[0] = 0;
			stop = TRUE;
		}
	}	while(!stop);

	if(!connected && !login)	return;

	ser_out = FALSE;
	do
	{
/* muss 1,1 sein, da NAK sonst gefiltert wird */
		In(1, 1);
	}	while(wait_buffer[0] != NAK);

serial_number:
	ser_out = TRUE;
	Char(0);
	Char(0);
	Char(0);
	Char(0);
	Char(0);
	ser_out = FALSE;

	Location("Seriennummern-Abgleich");

	do
	{
/* muss 1,1 sein, da ACK sonst gefiltert wird */
		In(1, 1);
		if(wait_buffer[0] == NAK)	goto serial_number;
	}	while(wait_buffer[0] != ACK);

	protocol = GetProtocol(protocol_nummer);
	RemList(&FileList);
	if(line = GetLine())
	{
		SPrintf(line->text, "%ls.PRE", hostname);
		AddTail(&FileList, line);
	}
	ser_out = FALSE;
	SPrintf(file, "%lsPoints/", Config.mail);
	if(DoTransfer(file, protocol, TRUE))
	{
		RemList(&FileList);
		if(line = GetLine())
		{
			SPrintf(line->text, "CALLED.LZH", hostname);
			AddTail(&FileList, line);
		}
		ser_out = FALSE;
		if(DoTransfer(Config.receive, protocol, FALSE))
		{
			HangUp();
			Log.bad_logoff = FALSE;

			SPrintf(file,"%lsPoints/%ls.PRE", Config.mail, hostname);
			Log.k_uloads += GetFileSize(file) / 1024;
			Log.uloads++;
			DeleteFile(file);

			FindFile(Config.receive, "CALLED.PRE");
			SPrintf(file, "%lsCALLED.PRE", Config.receive);
			Log.k_dloads += GetFileSize(file) / 1024;
			Log.dloads++;

			ATCmd(Config.busy);
			ShowPrg("ZSort C=%ls F=%lsCALLED.PRE N=%ls PRI=%ld S=%ld", configfile, Config.receive, hostname, Config.priority, (LONG)Scr);
			HangUp();

			Location("Netcall beendet");
		}
		else
		{
			HangUp();
			Log.bad_transfers++;
			Location("Empfangen gescheitert");
		}
	}
	else
	{
		HangUp();
		Log.bad_transfers++;
		Location("Senden gescheitert");
	}
}
