/*
                            DSKEL-XS v1.1

                       (Xpress User Stats DOOR)

                       1         2         3
             (123456789012345678901234567890123456789)
Conf Access : XX_X____X___s__sX______X______________X
Caller Num. : 100888
1st Date On : 22 Apr 91
Lst Date On :  7 Mar 92
Security Lv : 255
# Times On  : 1123        Today : 4
Msgs Posted : 1500        Today : 0
File Ratio  : Disabled
Byte Ratio  : Disabled
# Downloads : 71          Today : 0
# Uploads   : 85          Today : 0
Bytes DL'd  : 137936061   Today : 39994
Bytes UL'd  : 8089329     Today : 4433
Online Baud : 14400
Screen Clear: On
Time Bar    : Off
Byte Credits:
Bytes Avail : 3000000


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

#include <proto/all.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <stdarg.h>
#include <string.h>
#include <tempest/headers.h>
#include <skeleton/commas.h>

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


int  DoorStart(TEXT *);
VOID GetStr(TEXT *,int);
VOID pl(TEXT *,...);
int  DOORIO(WORD);
VOID CloseStuff(VOID);
char *Myctime(long *);

 struct MyMessage
  {
   struct Message Msg; // for Exec message routines
   ULONG  Command;     // Command to be executed.

   char   *text1,
          *text2,
          *text3;
   int    Value1,
          Value2;
   ULONG  LongValue;
   LONGBITS Flags;
   int    carrier;
   struct User       *User;             // Pointers!
   struct SystemData *SystemData;
   struct NodeData   *NodeData;
   struct Today      *Today;
  };

struct MsgPort *MyPort = NULL;
struct MyMessage *msg;
struct MyMessage send;
struct User *User;
struct SystemData *SystemData;
struct NodeData *NodeData;
struct Today *Today;

struct MsgPort *TempestPort;

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

int main(int argc,char *argv[])
{
 char string[200],string1[200];
 char Day[15],Month[15],Year[15];
 long Bytes, BytesAvail;
 int x=1;
 if(!DoorStart(argv[1])) { PutStr("TempesT Door\n"); exit(0); }

 strncpy(string,User->MsgBase,40);
 string[40]=NULL;
 do
  {
   if(string[x] == 'N') string[x] = '_';
   else if(string[x] == 'S') string[x] = 's';
     else string[x] = 'X';
   x++;
  }
 while(x < 40);
 strmid(string,string1,2,39);
 pl("\f\r\n                       [35m1         2         3\r\n");
 pl("             [36m([35m123456789012345678901234567890123456789[36m)\r\n");
 pl("Conf Access : [0m%s\r\n",string1);
//********************************************************************
 GetStr(string1,95);
 pl("[36mCaller Num. : [0m%s\r\n",string1);
//********************************************************************
 strcpy(string,Myctime(&User->Time_First_Called));
 strmid(string,Month,5,3);
 strmid(string,Day,9,2);
 strmid(string,Year,23,2);
 pl("[36m1st Date On : [0m%2s %s %s\r\n",Day,Month,Year);
//********************************************************************
 strcpy(string,Myctime(&User->Time_Last_Logoff));
 strmid(string,Month,5,3);
 strmid(string,Day,9,2);
 strmid(string,Year,23,2);
 pl("[36mLst Date On : [0m%2s %s %s\r\n",Day,Month,Year);
//********************************************************************
 pl("[36mSecurity Lv : [0m%d\r\n",User->Security);
//********************************************************************
 pl("[36m# Times On  : [0m%-5s       [36mToday : [0m%u\r\n",commas(User->Total_Calls,string),User->Period_Calls);
//********************************************************************
 pl("[36mMsgs Posted : [0m%-5s       [36mToday : [0m%u\r\n",commas(User->Total_Posts,string),User->Period_Posts);
//********************************************************************
 if(User->F_Ratio==0) strcpy(string1,"Disabled");
 else                 sprintf(string1,"%d/1",User->F_Ratio);
 pl("[36mFile Ratio  : [0m%s\r\n",string1);
//********************************************************************
 if(User->B_Ratio==0) strcpy(string1,"Disabled");
 else                 sprintf(string1,"%d/1",User->B_Ratio);
 pl("[36mByte Ratio  : [0m%s\r\n",string1);
//********************************************************************
 pl("[36m# Downloads : [0m%-4u        [36mToday : [0m%u\r\n",User->Total_DLFiles,User->Period_DLFiles);
//********************************************************************
 pl("[36m# Uploads   : [0m%-4u        [36mToday : [0m%u\r\n",User->Total_ULFiles,User->Period_ULFiles);
//********************************************************************
 pl("[36mBytes DL'd  : [0m%-10lu  [36mToday : [0m%ld \r\n",User->Total_DLBytes,User->Period_DLBytes);
//********************************************************************
 pl("[36mBytes UL'd  : [0m%-10lu  [36mToday : [0m%ld\r\n",User->Total_ULBytes,User->Period_ULBytes);
//********************************************************************
 GetStr(string1,19);
 pl("[36mOnline Baud : [0m%s\r\n",string1);
//********************************************************************
 if(User->UserBitsA & 0x00000400L) strcpy(string1,"On");
 else                              strcpy(string1,"Off");
 pl("[36mScreen Clear: [0m%s\r\n",string1);
//********************************************************************
 if(User->UserBitsA&0x00000008L) strcpy(string1,"On");
 else                            strcpy(string1,"Off");
 pl("[36mTime Bar    : [0m%s\r\n",string1);
//********************************************************************

 if(User->ByteCredits>0)
    pl("[36mByte Credits: [0m%s\r\n",commas(User->ByteCredits,string));

//********************************************************************
 if(User->B_Ratio==0)
  {
   Bytes = User->Left_DLBytes - User->Period_DLBytes;
   BytesAvail = Bytes;
  }
 else
  {
   Bytes      = (User->B_Ratio * User->Total_ULBytes) - User->Total_DLBytes + User->ByteCredits;
   BytesAvail = User->Left_DLBytes - User->Period_DLBytes;
   if(!(Bytes < BytesAvail)) Bytes = BytesAvail;
   if(Bytes < 0)             Bytes = 0L;
  }
 pl("[36mBytes Avail : [0m%s\r\n\r\n",commas(Bytes,string));
//********************************************************************
 CloseStuff();
}
//===========================* Beginning of Module *===========================
char *Myctime(long *secs)
{
 struct tm *tm;
 tm=gmtime(secs);
 return(asctime(tm));
}





//************************
//*****  Door Start  ***** // MUST BE INCLUDED IN ALL DOORS!
//************************
int DoorStart(TEXT *node)
{
 char string[60];
 send.carrier=0;
 sprintf(string,"%s:TEMPEST_DOOR",node);
 TempestPort=FindPort(string);
 if(TempestPort==NULL) return(int)FALSE;
 MyPort=CreateMsgPort();
 if(MyPort==NULL) return(int)FALSE;
 DOORIO(0);
 User=*&send.User;
 SystemData=*&send.SystemData;
 NodeData=*&send.NodeData;
 Today=*&send.Today;
 return(int)TRUE;
}
//********************
//*****  DoorIO  ***** // MUST BE INCLUDED IN ALL DOORS!
//********************
int DOORIO(WORD Command)
{
 if(send.carrier) return(0);
 send.Command=Command;
 send.Msg.mn_Node.ln_Type = NT_MESSAGE;
 send.Msg.mn_Length = sizeof(send);
 send.Msg.mn_ReplyPort = MyPort;
 send.carrier=0;
 PutMsg((struct MsgPort *)TempestPort,(struct Message *)&send);
 Wait(1 << MyPort->mp_SigBit);
 GetMsg(MyPort);
 if((send.carrier)&&(send.Command!=999)) CloseStuff();
 return(1);
}
//*************************
//*****  Close Stuff  ***** // MUST BE INCLUDED IN ALL DOORS!
//*************************
VOID CloseStuff(VOID)
{
 DOORIO(999);
 while(msg=(struct MyMessage *)GetMsg(MyPort)) ReplyMsg((struct Message *)msg);
 if(MyPort) DeleteMsgPort(MyPort);
 exit(0);
}
//*********************************
//*****  GetStr (Get String)  ***** // Command 33
//*********************************
VOID GetStr(TEXT *s,int opt)
{
 send.Value1=opt;
 strcpy(s,"");
 send.text1=s;
 DOORIO(33);
}
//*****************************
//*****  PL (Print Line)  ***** // Command 1 with "%s %d",xxx,xxx) paramter support
//*****************************
VOID pl(TEXT *fmt,...)
{
 va_list args;
 TEXT s[255];
 va_start(args,fmt);
 vsprintf(s,fmt,args);
 va_end(args);
 send.text1=&s[0];
 DOORIO(1);
}
