/* Tempest Door Code! */

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

#include <proto/all.h>
#include <exec/types.h>
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <devices/timer.h>
#include <devices/serial.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <exec/memory.h>

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

#define DROP if(EXIT_FLAG) CloseStuff();

//********************
//*****  Void's  *****
//********************

void CheckDoor();
int  CheckKey();
void CloseStuff();
void DOORIO();
void getkey(char character[]);
void GetStr(char s[],int opt);
void GetUserTime(char s[]);
int  GetValue(int opt);
void GetWorkDir(char s[]);
int  hotkey(char character[]);
void input(char mstring[],int len);
void LineInput(char ostring[],char mstring[],int len);
void LoadSystemData();
void pl(char outstring[]);
int  Random(int x);
void SetValue(int opt1,int opt);
void showfile(char mstring[]);
void UserTime(int x);
void WriteFile(char ostring[],char mstring[]);
void XmodemDownload(char mstring[]);
void ZmodemDownload(char mstring[]);
void LOG(char outstring[]);
void SLOG(char outstring[]);
void DEBUGLOG(char outstring[]);

//*********************
//*****  Structs  *****
//*********************

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

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

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];

//************************
//*****  Check Door  *****
//************************

void CheckDoor()
{ DOOR_DATA=1; strcpy(DOOR_MSG,'\0'); DOORIO(); strcpy(DOOR_MSG,'\0'); }

//***********************
//*****  Check Key  *****
//***********************

int CheckKey()
{ strcpy(DOOR_MSG,'\0'); DOOR_DATA=54; DOOR_VALUE1=0; DOORIO(); return(DOOR_VALUE1); }

//*************************
//*****  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(0L); }

//********************
//*****  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;
  }
}

//************************
//*****  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;
}

//********************
//*****  Editor  *****
//********************

void Editor(char Filename[])
{ DOOR_DATA=29; strcpy(DOOR_MSG,Filename); DOORIO(); }

//*********************
//*****  Get Key  *****
//*********************

void getkey(char character[])
{ strcpy(DOOR_MSG,'\0'); DOOR_DATA=31; DOORIO(); strcpy(character,DOOR_MSG); }

//********************
//*****  GetStr  *****
//********************

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

//***************************
//*****  Get User Time  *****
//***************************

void GetUserTime(char s[])
{ DOOR_DATA=14; DOORIO(); strcpy(s,DOOR_MSG); }

//***********************
//*****  Get Value  *****
//***********************

int GetValue(int opt)
{ DOOR_DATA=18; DOOR_VALUE=opt; DOORIO(); return(DOOR_VALUE1); }

//********************************
//*****  Get Work Directory  *****
//********************************

void GetWorkDir(char s[])
{ DOOR_DATA=13; strcpy(s,'\0'); DOORIO(); strcpy(s,DOOR_MSG); }

//*********************
//*****  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'); }

//******************************
//*****  Load System Data  *****
//******************************

void LoadSystemData()
{ DOOR_DATA=50; DOORIO(); }

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

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

//******************************
//*****  SPL (Print Line)  *****
//******************************

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

//********************
//*****  Random  *****
//********************

int Random(int x)
{ DOOR_DATA=26; DOOR_VALUE=x; DOORIO(); return(DOOR_VALUE); }

//***********************
//*****  Set Value  *****
//***********************

void SetValue(int opt1,int opt)
{ DOOR_DATA=17; DOOR_VALUE1 = opt1; DOOR_VALUE=opt; DOORIO(); }

//***********************
//*****  Show File  *****
//***********************

void showfile(char mstring[])
{ DOOR_DATA=30; strcpy(DOOR_MSG,mstring); DOORIO(); }

//***********************
//*****  User Time  *****
//***********************

void UserTime(int x)
{ DOOR_DATA=25; DOOR_VALUE=x; DOORIO(); }

//************************
//*****  Write File  *****
//************************

void WriteFile(char ostring[],char mstring[])
{ DOOR_DATA=16; strcpy(DOOR_MSG,ostring); strcpy(DOOR_MSG1,mstring); DOORIO();
  strcpy(DOOR_MSG,'\0'); strcpy(DOOR_MSG1,'\0'); }

//*****************************
//*****  Zmodem Download  *****
//*****************************

void ZmodemDownload(char mstring[])
{ DOOR_DATA=27; strcpy(DOOR_MSG,mstring); DOORIO(); }

//*****************************
//*****  Xmodem Download  *****
//*****************************

void XmodemDownload(char mstring[])
{ DOOR_DATA=28; strcpy(DOOR_MSG,mstring); DOORIO(); }






//********************************
//*****  LOG (Write to Log)  *****
//********************************

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

//***************************************
//*****  SLOG (Write to Sysop Log)  *****
//***************************************

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

//*****************************************
//*****  de_bug (Write to debug Log)  *****
//*****************************************

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






//****************************
//**********  Docs  **********
//****************************


//************************************************************************
       A run down of what the above lines of code actually do.

x=DoorStart(node)             - if (x==0) door was ran from cli so exit.
                              - This is your very first call in the door
                              - program, its mandatory or else the bbs
                              - will lock up!!
                              - The Node number is passed by the bbs
                              - so we know what port to open up so if
                              - you run 2 bbs's you don't collide.

CloseStuff()                  - Used to tell the BBS were done with the door.

x = CheckKey()                - This will not wait for a key to be hit,
                              - hit just simply tells me if there has been
                              - one hit, if so the character will be in
                              - integer form returned via (x) in this example

pl(string)                    - Print the following string.

GetWorkDir(string)            - The path where to store data files the
                              - sysop has defined.

GetUserTime(string)           - A string containg the number of minutes
                              - the user has online.

showfile("ram:TextFile")      - It will display a text file.


hotkey(string)                - The string[0] postion contains the character
                              - the user has typed.

getkey(string)                - The string[0] postion contains the character
                              - the user has typed. But also passes the ESC

input(string,20)              - Input a line intro string that contains
                              - up to 20 character (MAX 200)

LineInput(old,new,40)         - Used for RE-EDITING of a string, place the
                                old string where the string 'old' is located
                                and place your string of the new input in
                                the string is located at 'new' and up to
                                200 characters can be inputed.

GetStr(string,0)              - This would place the path of the BBS into
                                the string.

GetStr(string,1)              - This would place the path of the Accounts.data
                                file into the string variable.
                         OTHERS:
                        ~~~~~~~~~
         0 - The path the BBS was loaded with.
         1 - The path of the accounts data file.
         2 - The path where the catalog files are kept.
         3 - The path to the temporary directory.
         4 - The path to the Text directory.
         5 - The path to the Describe directory.
         6 - The path to the Voting Dir.
         7 - The path where the optional files are kept.
         8 - The path where the Modules are kept.
         9 - The path where the new user answers are kept.
        10 - The path where sysop uploads are kept.
        11 - The path where the aborted uploads are kept.
        12 - The path where uploads are kept when there being uploaded.
        13 - The path where doors may be kept.
        14 - The path where your log files are kept at.
        15 - Get a full date & time string
        16 - Get the current date
        17 - Get the current time
        18 - Get the system name of the bbs
        19 - The Baud of the online caller.

SetValue(1,0)                 - This would make the BBS have a priority of 1
                         OTHERS:
                        ~~~~~~~~~
         0 - BBS Priority
         1 - Ansi ON
         2 - Ansi OFF

WriteFile("ram:test","What string to write\n")
                              - This would 'append' or create a new file
                                called ram:text, and in that file write
                                a line of text. the '\n' indicates go to
                                the next line.

Random(range)                 - Get a Random number between 0-range;
Editor(Filename)              - Use the BBS editor to write a text file
                                and save it back as 'Filename'

ZmodemDownload("path/filename");
                              - Send a file with zmodem, just send the
                                complete path.
LoadSystemData()              - Reloads all the BBS data files.
UserTime(time)                - The Number of minutes to ADD
                                UserTime(15)   add 15 minutes
                                UserTime(-15)  removed 15 minutes
DROP;                         - Small macro to tell the Door when we have
                                a dropped carrier to jump to function
                                ShutDown() which then closes the door.

For the latest rundown on how to use the commands, look for my door program
called 'DEMO.c'  it will try to use all of the available commands.
*************************************************************************
