
//                            SKEL-BAL v1.0

//                         (BBS ADD/LIST DOOR)

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

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

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

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

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

struct BBSLIST
 {
  char Name[21],
       Number[21],
       Location[21],
       Sysop[21],
       Groups[21],
       Support[21],
       Nodes[3],
       Megs[5],
       Baud[6];
 };

struct BBSLIST BL;
struct User User;
struct MsgPort *MyPort = NULL;
struct MyMessage *msg;

#define DROP if(EXIT_FLAG) CloseStuff();

void CloseStuff();
void input(char mstring[],int len);
void LineInput(char ostring[],char mstring[],int len);
int  DOORIO();

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

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

void pl(fmt,a1,a2,a3)
char *fmt;
 {
  char s[255];
  sprintf(s,fmt,a1,a2,a3);
  DOOR_DATA=1; strcpy(DOOR_MSG,s); DOORIO();
 }

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

void main(int argc,char *argv[])
{
 int Test = 1;
 if(!DoorStart(argv[1])) { puts("Tempest Door!\n"); exit(0); }
 while(Test)
  {
   pl("\f[33m[[0mDSKEL-BAL v0.4[33m] [36mBBS Add/List Door     By The SkeletoN [33m[[0m07-20-93[33m]\r\n");
   pl("[32m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
   pl("[[33mA[32m] -- [36mAdd a number to database.\r\n");
   pl("[32m[[33mD[32m] -- [36mDelete a number from database.\r\n");
   pl("[32m[[33mE[32m] -- [36mEdit a number in database.\r\n");
   pl("[32m[[33mL[32m] -- [36mList Numbers in the database.\r\n");
   pl("[32m[[33mS[32m] -- [36mSearch for a number in database.\r\n");
   pl("[32m[[33mQ[32m] -- [31mQuit back to the BBS.\r\n\r\n");
   pl("[36mSelect [32m[[33mA,D,E,L,S,Q[32m] : [0m");
   input(string1,1);
   DROP;
   if((string1[0] == 'q') || (string1[0] == 'Q')) Test = 0;
   if((string1[0] == 'a') || (string1[0] == 'A')) New();
   if((string1[0] == 'l') || (string1[0] == 'L')) List();
   if((string1[0] == 'e') || (string1[0] == 'E')) Edit();
  }
 CloseStuff();
}

//*************************
//*****  List Entrys  *****
//*************************

List()
{
 int fd,stat,lines;
 lines=0;
 fd=Open("DOORS:DSKEL-BAL.Data",MODE_OLDFILE);
 if(fd==0) { pl("Error reading Doors:DSKEL-BAL.Data\n\r"); return(0); }

 pl("\f");
 ReadRecord:
 stat=Read(fd,(char *)&BL,sizeof(struct BBSLIST));
 if(stat<=0)
  {
   pl("[36mHit any key:");
   hotkey(string1);
   Close(fd);
   return(0);
  }
 pl("   [34m[[33mName[34m] [0m%-20s      [34m[ [0m%s [33mNodes[34m]      [[33mSysop[34m] [0m%-20s\r\n",
     BL.Name,BL.Nodes,BL.Sysop);
 pl("  [34m[[33mPhone[34m] [0m%-20s     [34m[[0m%4s [33mMegs[34m]     [[33mGroups[34m] [0m%-20s\r\n",
     BL.Number,BL.Megs,BL.Groups);
 pl("[34m[[33mLocated[34m] [0m%-20s    [34m[[0m%5s [33mBaud[34m]    [[33mSupport[34m] [0m%-20s\r\n\r\n",
                    BL.Location,BL.Baud,BL.Support);
 lines+=4;
 if(lines+4>User.Length)
  {
   pl("[36mHit any key:");
   hotkey(string1);
   lines=0;
   pl("\r\n\r\n");
  }
 goto ReadRecord;
}

//***************************
//*****  Add New Entry  *****
//***************************

New()
{
 int fd;
 pl("\f                    [34m[--------------------][0m\r\n");
 pl("           BBS Name: ");
 input(BL.Name,20);
 DROP;
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("       Phone Number: ");
 input(BL.Number,20);
 DROP;
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("             Groups: ");
 input(BL.Groups,20);
 DROP;
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("           Location: ");
 input(BL.Location,20);
 DROP;
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("              Sysop: ");
 input(BL.Sysop,20);
 DROP;
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("  Formats Supported: ");
 input(BL.Support,20);
 DROP;
 pl("\r\n                    [34m[-][0m\r\n");
 pl("            # Nodes: ");
 input(BL.Nodes,1);
 DROP;
 pl("\r\n                    [34m[----][0m\r\n");
 pl("    Megs of Storage: ");
 input(BL.Megs,4);
 DROP;
 pl("\r\n                    [34m[-----][0m\r\n");
 pl("      Max Buad Rate: ");
 input(BL.Baud,5);
 DROP;
 pl("\r\n[36mSave to database [34m[[33my/n[34m] [0m");
 input(string1,1);
 DROP;
 if((string1[0] =='n') || (string1[0] == 'N')) return(0);

//***********************************
//*****  Write DSKEL-BAL.Data  ******
//***********************************

 fd=Open("DOORS:DSKEL-BAL.Data",MODE_OLDFILE);
 if(fd==0)
  {
   fd=Open("DOORS:DSKEL-BAL.Data",MODE_NEWFILE);
   if(fd==0)
    {
     pl("Error writing Doors:DSKEL-BAL.Data!\r\n");
     CloseStuff();
   }
  }
 else
   Seek(fd,0,OFFSET_END);
 Write(fd,(char *)&BL ,sizeof(struct BBSLIST));
 Close(fd);
 return(0);
}

//*************************
//*****  Edit Entrys  *****
//*************************

Edit()
{
 char string[255];
 int fd,Slot;
 long Position;
 pl("Edit which Entry?: ");
 input(string,3);
 DROP;
 Slot = atoi(string);
 fd=Open("DOORS:DSKEL-BAL.Data",MODE_OLDFILE);
 if(fd==0)
  {
   pl("Error reading Doors:DSKEL-BAL.Data\n\r"); return(0);
  }
 Slot--;
 Position = Slot * sizeof(struct BBSLIST);
 Seek(fd,Position,0);
 Read(fd,(char *)&BL,sizeof(struct BBSLIST));
 Close(fd);
 pl("\f                    [34m[--------------------][0m\r\n");
 pl("           BBS Name: ");
 LineInput(BL.Name,string,20);
 DROP;
 strcpy(BL.Name,string);
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("       Phone Number: ");
 LineInput(BL.Number,string,20);
 DROP;
 strcpy(BL.Number,string);
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("             Groups: ");
 LineInput(BL.Groups,string,20);
 DROP;
 strcpy(BL.Groups,string);
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("           Location: ");
 LineInput(BL.Location,string,20);
 DROP;
 strcpy(BL.Location,string);
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("              Sysop: ");
 LineInput(BL.Sysop,string,20);
 DROP;
 strcpy(BL.Sysop,string);
 pl("\r\n                    [34m[--------------------][0m\r\n");
 pl("  Formats Supported: ");
 LineInput(BL.Support,string,20);
 DROP;
 strcpy(BL.Support,string);
 pl("\r\n                    [34m[-][0m\r\n");
 pl("            # Nodes: ");
 LineInput(BL.Nodes,string,1);
 DROP;
 strcpy(BL.Nodes,string);
 pl("\r\n                    [34m[----][0m\r\n");
 pl("    Megs of Storage: ");
 LineInput(BL.Megs,string,4);
 DROP;
 strcpy(BL.Megs,string);
 pl("\r\n                    [34m[-----][0m\r\n");
 pl("      Max Buad Rate: ");
 LineInput(BL.Baud,string,5);
 DROP;
 strcpy(BL.Baud,string);
 pl("\r\n[36mSave changes to database [34m[[33my/n[34m] [0m");
 input(string1,1);
 DROP;
 if((string1[0] =='n') || (string1[0] == 'N')) return(0);

//***********************************
//*****  Write DSKEL-BAL.Data  ******
//***********************************

 fd=Open("DOORS:DSKEL-BAL.Data",MODE_OLDFILE);
 if(fd==0)
    pl("Error writing Doors:DSKEL-BAL.Data!\r\n");
 else
 Seek(fd,Position,0);
 Write(fd,(char *)&BL ,sizeof(struct BBSLIST));
 Close(fd);
 return(0);
}










//*************************
//*****  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)
  {
   puts("Cant open port");
   return(int)FALSE;
  }
 DOORIO();
 return (int)TRUE;
}

//*********************
//*****  Hot Key  *****
//*********************

int hotkey(char character[])
{ strcpy(DOOR_MSG,'\0'); DOOR_DATA=20; DOORIO(); strcpy(character,DOOR_MSG);
 return((int)character[0]); }

//*******************
//*****  Input  *****
//*******************

void input(char mstring[],int len)
{ DOOR_DATA=40; DOOR_VALUE=len; strcpy(DOOR_MSG,mstring); DOORIO();
 strcpy(mstring,DOOR_MSG); strcpy(DOOR_MSG,'\0'); strcpy(DOOR_MSG1,'\0'); }

//************************
//*****  Line Input  *****
//************************

void LineInput(char ostring[],char mstring[],int len)
{ DOOR_DATA=41; DOOR_VALUE=len; strcpy(DOOR_MSG,mstring); strcpy(DOOR_MSG1,ostring);
 DOORIO(); strcpy(mstring,DOOR_MSG); strcpy(DOOR_MSG,'\0'); strcpy(DOOR_MSG1,'\0'); }
