#parsecommandline TRUE
#resize TRUE

#include "netware.h--"
#include "sprintf.h--"

char servername[0x30];	//имя контролируемого сервера
char buf[256];
int logh;

_ServerInfo_ serverinfo;
CONNECT_INFO_ connectinfo={sizeof(CONNECT_INFO_)-2};
CONNECT_ADR_ connectadr;

char Help=
"KILL_NL - уничтожитель NOT-LOGGED-IN.\n"
"Использование: KILL_NL SERVER AGE DELTA\n"
"где: SERVER - имя сервера, на котором будет побоище\n"
"     AGE    - максимальный возраст в минутах NOT-LOGGED-IN\n"
"     DELTA  - интервал проверок в минутах\n";

void HELP()
inline
{

	@WRITESTR(#Help);
	@EXIT(byte 1);
}

void main()
{
int i;
int hour,min,year,day,mon;
int maxtimelive;	//время жизни NOT-LOGIN
int deltacheck;	//интервал проверок
int my_connect;
int curtime,lasttime=0;
	IF(@PARAMCOUNT()!=3)HELP();
	strcpy(#servername,PARAMSTR(0));
	@STR_UP(#servername);
	maxtimelive=STRTOINT(PARAMSTR(1));
	deltacheck=STRTOINT(PARAMSTR(2));
	IF(@ACCESS("kill.log",0)==0){
		@FOPEN(1,,,"kill.log");
	}
	ELSE{
		@FCREATE(,,0,"kill.log");
	}
	logh=AX;
	@FSEEK(2,logh,0,0);
	for(;;){
		@DOSGETTIME();
		hour=CH;
		min=CL;
		AX=CH*60+CL;
		IF(int AX<lasttime)lasttime=AX;
		curtime=AX;
		if(deltacheck+lasttime<=curtime){	//активизировать проверку
			@DOSGETDATE();
			year=CX;
			mon=DH;
			day=DL;
			sprintf(#buf,"%^02d.%^02d.%u %^02d:%^02d Check NOT-LOGGED-IN\n",day,mon,year,hour,min);
			WRITEBUF();
			DWRITESTR(#buf);
			lasttime=curtime;
			GetServerInfo(#serverinfo);
			IF(byte strcmp(#serverinfo.Name,#servername)!=0){
				FOR(i=0;i<8;i++){
					GetFileServerName(i,#buf);
					IF(byte strcmp(#buf,#servername)==0)BREAK;
				}
				IF(i==8){
					sprintf(#buf,"Сервер %s не найден\n",#servername);
					DWRITESTR(#buf);
					continue;
				}
				SetPreferedConnect(i+1);
				GetServerInfo(#serverinfo);
				IF(byte strcmp(#serverinfo.Name,#servername)!=0){
					sprintf(#buf,"Сервер %s не установлен текущим\n",#servername);
					DWRITESTR(#buf);
					continue;
				}
			}
			serverinfo.MaxConnect=IntSwap(serverinfo.MaxConnect);
			my_connect=@GETCONNECT();
			for(i=1;i<=serverinfo.MaxConnect;i++){
				if(i!=my_connect){
					if(GetConnectInfo(i,#connectinfo)==0){
						if(GetConnectAddress(i,#connectadr)==0){
//							sprintf(#buf,"%^ 3d %^08lx:%^04x%^04x%^04x %s\n",i,long LongSwap(connectadr.netwAdr),
//								IntSwap(connectadr.nodeAdr[0]),IntSwap(connectadr.nodeAdr[2]),
//								IntSwap(connectadr.nodeAdr[4]),#connectinfo.name);
							if(byte strcmp("NOT-LOGGED-IN",#connectinfo.name)==0){
								AX=connectinfo.hour*60+connectinfo.min;
								if(AX+maxtimelive<curtime){
									sprintf(#buf,"%^ 3d %^08lx:%^04x%^04x%^04x",i,long LongSwap(connectadr.netwAdr),
										IntSwap(connectadr.nodeAdr[0]),IntSwap(connectadr.nodeAdr[2]),
										IntSwap(connectadr.nodeAdr[4]));
									DWRITESTR(#buf);
									WRITEBUF();
									sprintf(#buf," connected %^02d.%^02d.%u %^02d:%^02d",
										connectinfo.day,connectinfo.mon,connectinfo.
										year+1900,connectinfo.hour,connectinfo.min);
									DWRITESTR(#buf);
									WRITEBUF();
									IF(ClearConnect(i)==0)STRCPY(,,,,#buf," - killed\n");
									ELSE STRCPY(,,,,#buf," - no access\n");
									WRITEBUF();
									DWRITESTR(#buf);
								}
							}
//							DWRITESTR(#buf);
						}
/*						ELSE{
							err=AX;
							sprintf(#buf,"GetConnectAddress error code %u\n",err);
							DWRITESTR(#buf);
						}*/
					}
/*					ELSE{
						err=AX;
						sprintf(#buf,"GetConnectInfo error code %u\n",err);
						DWRITESTR(#buf);
					}*/
				}
				IF(CHECKEXIT()!=0)BREAK 1;
			}
		}
		IF(CHECKEXIT()!=0)BREAK;
	}
	FCLOSE(,logh);

}

int CHECKEXIT()
{
	KVANT();
	IF(@BIOSKEYCHECK()){
		@BIOSREADKEY();
		IF(AX!=0x011b)AX=0;
	}
}

void WRITEBUF()
{
	@STRLEN(#buf);
	@FWRITE(,logh,AX,#buf);
}
