/*
    ษอออออออหอออออออออออออออออออออออออออออหอออออออออออออออออออหอออออออออออออออป
    บ LoGoN บ HanDLe / LoCATioN           บ ACTiViTY          บ  FiLE Status  บ
ษอออบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออน
บ 1 บ 19:44 บ The Skeleton                บ DownLoading       บ  12345678.dms บ
ศอออน  2400 บ Crystal And Indepdanant     บ CPS Rate: 1623    บ 4588383 Bytes บ
ษอออบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออน
บ 2 บ 19:33 บ The Skeleton                บ DownLoading       บ  12345678.dms บ
ศอออน 19200 บ Crystal / Indepdanant       บ CPS Rate: 1222    บ 4588383 Bytes บ
ษอออบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออน
บ 3 บ 11:33 บ The Skeleton                บ DownLoading       บ  12345678.dms บ
ศอออน 14400 บ Crystal / Indepdanant       บ CPS Rate: 1993    บ 4588383 Bytes บ
ษอออบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออน
บ 4 บ   :   บ [ Hiding from display ]     บ Logging On        บ ------------- บ
ศอออน       บ                             บ                   บ               บ
ษอออบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออน
บ15 บ   :   บ                             บ Waiting for call  บ               บ
ศอออน       บ                             บ                   บ               บ
    บอออออออสอออออออออออออออออออออออออออออสอออออออออออออออออออสอออออออออออออออบ
    บออออออออออออออออออหออออออออออออออออออหอออออออออออออออออหอออออออออออออออออน
    บ Active Lines: 11 บ Users Online: 11 บ Free Lines: 22  บ                 บ
    ศออออออออออออออออออสออออออออออออออออออสอออออออออออออออออสอออออออออออออออออผ

    .-------.-----------------------------.-------------------.---------------.
    | LoGoN | HanDLe / LoCATioN           | ACTiViTY          |  FiLE Status  |
.---|-------|-----------------------------|-------------------|---------------|
| 1 | 19:44 | The Skeleton                | DownLoading       |  12345678.dms |
`---|  2400 | Crystal And Indepdanant     | CPS Rate: 1623    | 4588383 Bytes |
.---|-------|-----------------------------|-------------------|---------------|
| 2 | 19:33 | The Skeleton                | DownLoading       |  12345678.dms |
`---| 19200 | Crystal / Indepdanant       | CPS Rate: 1222    | 4588383 Bytes |
.---|-------|-----------------------------|-------------------|---------------|
| 3 | 11:33 | The Skeleton                | DownLoading       |  12345678.dms |
`---| 14400 | Crystal / Indepdanant       | CPS Rate: 1993    | 4588383 Bytes |
.---|-------|-----------------------------|-------------------|---------------|
| 4 |   :   | [ Hiding from display ]     | Logging On        | ------------- |
`---|       |                             |                   |               |
.---|-------|-----------------------------|-------------------|---------------|
|15 |   :   |                             | Waiting for call  |               |
`---|       |                             |                   |               |
    |-------^-----------------------------^-------------------^---------------|
    |------------------.------------------.-----------------.-----------------|
    | Active Lines: 11 | Users Online: 11 | Free Lines: 22  |                 |
    `------------------^------------------^-----------------^-----------------'

*/
//**********************
//*****  Includes  *****
//**********************

#include <proto/all.h>
#include <stdio.h>
#include <exec/memory.h>

#include <string.h>
#include <tempest/headers.h>
#include <tempest/defines.h>

//********************************
//*****  Structures/Defines  *****
//********************************


struct MyMessage
 {
  struct Message Msg;
  struct User User;
  char text[255],
       text1[255];
  int car,Value,Data;
  long LongValue;
  int  IntValue;
 };

struct User User;
struct MsgPort *MyPort = NULL;
struct MyMessage *msg;
struct Down_Load Down_Load;
struct DownLoad_Status DownLoad_Status;

struct node_info nody;

void CloseStuff();
void pl(char outstring[]);
void GetStr(char s[],int opt);
int hotkey(char character[]);
void RIPIT(char *,char *);
int DOORIO();
int KEY(void);
int Loadnody(int node);

//******************************
//*****  Global Variables  *****
//******************************

int  EXIT_FLAG, DOOR_DATA, DOOR_CAR, DOOR_VALUE, DOOR_VALUE1 = 0,
     Result,Error,first = 0;
char MyName[255],st[100],DOOR_MSG[255],DOOR_MSG1[255];

//******************
//*****  Main  *****
//******************

void main(int argc,char *argv[])
{
 struct FileInfoBlock *FBlock;
 struct FileLock *FLock;
 char string[255],string2[255];
 char Handle[40],Location[40],Activity[30],Time[15],Filename[15],Filesize[15];
 int  file,Position,stat,count=0,NODES,NODE,line;
 int online=0;
 register int i;
 if(!DoorStart(argv[1])) { PutStr("[os2] Tempest Door\n"); exit(0); }

 if(!User.NFlags1 & NF1_ViewNodeLists) CloseStuff();

//********************************
//*****  Load Internal.Data  *****
//********************************

 NODE = atoi(argv[1]);
 stat=Loadnody(NODE);
 if(stat==0)
  {
   pl("NodeInfo File Not Found!\r\n");
   CloseStuff();
  }
 NODES = nody.SystemDataInfo->NumberOfLines + 1;
 line=0; // Line Counter

//***************************************
//*****  Beginning oF Massive Loop  *****
//***************************************

 pl("\f    [34mษอออออออหอออออออออออออออออออออออออออออหอออออออออออออออออออหอออออออออออออออป\r\n");
 pl("    บ [33mLoGoN [34mบ [33mHanDLe / LoCATioN           [34mบ [33mACTiViTY          [34mบ  [33mFiLE STATuS  [34mบ\r\n");

 i=1;
 do
  {
   if(CTRLC())
    {
     pl("[31mAborting Who Display![0m\r\n");
     CloseStuff();
    }
   stat=Loadnody(i);
   if(stat==0) setmem(&nody,sizeof(struct node_info),NULL);

   strcpy(Filename," ");
   strcpy(Filesize," ");

   //***********************
   //*****  Uploading  *****
   //***********************
   if(*nody.AN== 58) // Only If they are uploading!
    {
     sprintf(string,"%s",nody.NodeDataInfo->TempULWorkPath);
     if((FLock =(struct FileLock *)Lock(string,ACCESS_READ))==NULL) CloseStuff();
     if((FBlock=(struct FileInfoBlock *)AllocMem((long)sizeof(struct FileInfoBlock),MEMF_CHIP)) == NULL)
      {
       UnLock((BPTR)FLock);
       CloseStuff();
      }
     Examine((BPTR)FLock,FBlock);
     while ((ExNext((BPTR)FLock,FBlock))!=0)
      {
       if((FBlock->fib_DirEntryType) <0)
        {
         strcpy(Filename,FBlock->fib_FileName);
         sprintf(Filesize,"%ld",FBlock->fib_Size);
         break;
        }
      }
     UnLock((BPTR)FLock);
     FreeMem(FBlock,sizeof(struct FileInfoBlock));
    }

   //********************************
   //*****  Downloading Checks  *****
   //********************************

   if(*nody.AN==18) // Only If they are Downloading!
    {
     sprintf(string, "NODE:Download_File_List_Node#%d",i);
     sprintf(string2,"NODE:Download_File_Status_Node#%d",i);
     file=Open(string2,MODE_OLDFILE);
     if(file==0)
      {
       file=Open(string,MODE_OLDFILE);   // Single Downloading
       if(file!=0) Read(file,(char *)&Down_Load, sizeof(struct Down_Load));
      }
     else
      {
       do
        {
         count++;
         stat=Read(file,(char *)&DownLoad_Status, sizeof(struct DownLoad_Status));
        }
       while(stat > 0);
       Close(file);
       file=Open(string,MODE_OLDFILE);
       count--;
       Position=sizeof(struct Down_Load) * count;
       Seek(file,Position,OFFSET_BEGINNING);
       Read(file,(char *)&Down_Load, sizeof(struct Down_Load));
      }
     Close(file);
     strcpy(string,Down_Load.File);
     RIPIT(string,string2);
     strcpy(Filename,string2);
     sprintf(Filesize,"%ld",Down_Load.Bytes);
     count=0;
    }

   //**************************
   //*****  Final Output  *****
   //**************************

   strcpy(Handle,nody.UserInfo->Name);
   if((*nody.hide==1)&&(User.Security!=255)&&(User.Name[0]!=NULL))
    {
     if(nody.UserInfo->Slot_Number==1) Handle[0]=NULL;
     else strcpy(Handle,"[ Hiding From Display ]");
     strcpy(Location," ");
     strcpy(Filename," ");
    }
   if(Handle[0]!=NULL)
    {
     if((i!=(NODES-1))) online++;
     sprintf(string,"%s",ctime(&nody.UserInfo->Time_Last_Connect));
     strmid(string,Time,12,5);
     strcpy(Location,nody.UserInfo->City);
     strcpy(Activity,nody.activity);
     line+=3;
     pl("[34mษอออบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออน\r\n");
     if(nody.NodeDataInfo->LocalMode==0) sprintf(string2,"%2d",i);
     else                                strcpy(string2," L");

     sprintf(string,"[34mบ[33m%2s [34mบ [0m%5s [34mบ [36m%-27.27s [34mบ [31m%-18.18s[34mบ [35m %-12.12s [34mบ\r\n",
                    string2,Time,Handle,Activity,Filename);
     pl(string);
     sprintf(string,"[34mศอออน [32m%5.5d [34mบ [35m%-27.27s [34mบ ",*nody.baudrate,Location);
     pl(string);
     if((*nody.AN==58)||(*nody.AN==18))
      {
       sprintf(string,"[32mCPS Rate[0m: %-4.4ld    [34mบ [0m%7.7s [36mBytes [34mบ\r\n",*nody.cps,Filesize);
      }
     else sprintf(string,"                  บ               บ\r\n");
     pl(string);
     if( ((line+3)>User.Length)&&((i+1)<NODES))
      {
       pl("    [34mบอออออออบอออออออออออออออออออออออออออออบอออออออออออออออออออบอออออออออออออออบ[0m\r\n");
       KEY();
       line=0;
      }
    }
   i++;
  }
 while(i<NODES);

 pl("    [34mบอออออออสอออออออออออออออออออออออออออออสอออออออออออออออออออสอออออออออออออออบ\r\n");
 pl("    [34mบออออออออออออออออออหออออออออออออออออออหอออออออออออออออออหอออออออออออออออออน\r\n");
 sprintf(string,"    [34mบ [36mActive Lines[0m: [31m%2d [34mบ [36mUsers Online[0m: [31m%2d [34mบ [36mFree Lines[0m: [31m%2d  [34mบ                 บ\r\n",(NODES-2),online,(NODES-2-online));
 pl(string);
 pl("    [34mศออออออออออออออออออสออออออออออออออออออสอออออออออออออออออสอออออออออออออออออผ[0m\r\n");
 CloseStuff();
}







//************************
//*****  Load Nody  ******
//************************

int Loadnody(int node)
{
 char string[50];
 register int y;
 int file;
 sprintf(string,"Node:NodeInfo_%d",node);
 for(y=1;y<3;y++)
  {
   file=Open(string,MODE_OLDFILE);
   if(file!=0)
    {
     Read(file,(char *)&nody,sizeof(struct node_info));
     Close(file);
     return(1);
    }
  }
 return(0);
}

//*******************
//*****  Ripit  *****
//*******************

void RIPIT(char path[100],char fname[100])
{
 int x,y,len;
 char string[255],string1[255],string3[255],string4[255];
/* to get the reverse read of the file name */
 len=strlen(path);
 x=len-1;
 y=0;
 while(x>=0)
  {
   if(path[x]==':' || path[x]=='/') goto got;
   string[y] = path[x];
   x--;
   y++;
  }
 if(y==0) return;     /* Unknown (0)? */
 got:
 string[y]='\0';
 /* reverse read to finish the line to get the path */
 y=0;
 while(x>=0)
  {
   string3[y] = path[x];
   x--;
   y++;
  }
 string3[y]='\0';
 /* flip the path around */
 len=strlen(string3);
 x=len-1;
 y=0;
 while(x>=0)
  {
   string4[y] = string3[x];
   x--;
   y++;
  }
 string4[y]='\0';
 len=strlen(string);
 /* flip the filename around */
 x=len-1;
 y=0;
 while(x>=0)
  {
   string1[y] = string[x];
   x--;
   y++;
  }
 string1[y]='\0';
 strcpy(fname,string1);
 strcpy(path,string4);
}



//*************************
//*****  Close Stuff  *****
//*************************

void CloseStuff()
{ DOOR_DATA=99; strcpy(DOOR_MSG,'\0'); DOORIO();
 while(msg=(struct MyMessage *)GetMsg(MyPort)) ReplyMsg((struct Message *)msg);
 if(MyPort) DeletePort(MyPort);
 exit(0); }

//********************
//*****  DoorIO  *****
//********************

int DOORIO()
{
 struct MsgPort *HisPort;
 struct MyMessage message;
 int UPDATE;
 if(EXIT_FLAG) return 0;
 UPDATE=0;
 HisPort = FindPort(st);
 if(HisPort!=NULL)
  {
   message.Msg.mn_Node.ln_Type = NT_MESSAGE;
   message.Msg.mn_Length = sizeof(message);
   message.Msg.mn_ReplyPort = MyPort;
   if(first)CopyMem(&User,&message.User,sizeof(struct User));
   message.car   = 0;
   message.Data  = DOOR_DATA;
   message.Value = DOOR_VALUE;
   message.IntValue = DOOR_VALUE1;
   strcpy(message.text,DOOR_MSG);
   strcpy(message.text1,DOOR_MSG1);
   PutMsg((struct MsgPort *)HisPort,(struct Message *)&message);
   Wait(1 << MyPort->mp_SigBit);
   GetMsg(MyPort);
   DOOR_DATA  = message.Data;
   DOOR_VALUE = message.Value;
   DOOR_VALUE1 = message.IntValue;
   strcpy(DOOR_MSG,message.text);
   strcpy(DOOR_MSG1,message.text1);
   CopyMem(&message.User,&User,sizeof(struct User));
   first=1;
   UPDATE=0;
   if(message.car) EXIT_FLAG=1;
  }
 return 0;
}

//************************
//*****  Door Start  *****
//************************

DoorStart(char node[])
{
 struct MsgPort *HisPort;
 EXIT_FLAG = 0;
 sprintf(st,"%s:TEMPEST_DOOR",node);
 HisPort = FindPort(st);
 if(HisPort==NULL) return (FALSE);
 sprintf(MyName,"%s:DOOR_PORT",node);
 MyPort = CreatePort(MyName,0L);
 if(MyPort==NULL)
  {
   PutStr("Cant open port");
   return(int)FALSE;
  }
 DOORIO();
 return (int)TRUE;
}

//*********************************
//*****  GetStr (Get String)  *****
//*********************************

void GetStr(char s[],int opt)
{ DOOR_DATA=15; DOOR_VALUE=opt; strcpy(s,'\0'); DOORIO(); strcpy(s,DOOR_MSG); }

//*****************
//*****  KEY  *****
//*****************

int KEY(void)
{ DOOR_DATA=70; DOORIO(); return(DOOR_VALUE); }

//*****************************
//*****  PL (Print Line)  *****
//*****************************

void pl(char outstring[])
{ DOOR_DATA=1; strcpy(DOOR_MSG,outstring); DOORIO(); }


//*************************
//*****  CTRLC CHECK  *****
//*************************

int CTRLC(void)
 {
  DOOR_DATA=73;
  DOORIO();
  return(DOOR_VALUE);
 }
