/* Wall in the Hood V2.0 (C) 1993 Subhumanz/LSD*/

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>
#include <stdio.h>
#include <string.h>

#ifdef LATTICE
int CXBRK(void) {return(0);} /* Disable Lattice CTRL-C handling */
int chkabort(void) {return(0);}
#endif
#define CR '\n'

/* Function prototypes: */
int CheckMessage(void);
int PortStart(void);

void TakeOffEh(int code);
void ShutDown(void);
void Createtext(void);
void Writetext(char withtxt[20][80], char post[80]);
void Printtext(char withtxt[20][80]);
void GetString(char text[], int len);
void PutString(char text[], int lf);
void GetUserName(char text[], int len);
void GetUserLines(char text[], int len);
void Getprompt(char text[], int len);

struct XIM   /* This has been taken directly from JH_Message */
{            /* structure in the doordocs file.  */

  struct Message Msg;	/* msg structure */
  char String[200];	/* info buffer */
  int Data;		/* Read/Write & result indicator */
  int Command;		/* Command sent from door. */
  int NodeID;		/* reserved */
  int LineNum;		/* reserved */
  unsigned long signal;	/* reserved */
  struct Process *task;	/* see BB_GETTASK in doordocs */
  APTR Semi;		/* See MULTICOM in doordocs */
};

struct MsgPort *DoorControlPort, *DoorReplyPort;
struct XIM *XIM_Msg, *reply;
ULONG usersig, portsig, signals;
STRPTR doorport[30]; /* Portname for AEDoorPort(n) */
int EXIT_FLAG=0; /* This is the global shutdown flag */
char instring[256];
char finstring[80];
int line_num;
int showname = 0;
int length = 0;
char user_lines[] = "23";
char cuser_lines[2];
char answer[1];
char username[50];
char post[80];
char withtxt[20][80];

void main(int argc, char *argv[])
{

	if(argc<2) /* AmiExpress will supply line_num on command line */
	{
                printf("\\/\\/all in the |-|ood V2.0 (C) 1993 SuBhUmAnZ/LSD\n");
		printf("This is a XIM door and must be called from /X.\n");
		Exit(0);
	}

	line_num=atoi(argv[1]);

	/* Don't know what Jon Radoff's problem was.. This works fine.. */
	sprintf((STRPTR)doorport,"AEDoorPort%d",line_num);

	if(PortStart()!=0) TakeOffEh(40); /* You must use this before you start! */

/* ....................................................................... */
/*                      ..Place your own code in here..                    */
/* ....................................................................... */

GetUserLines(instring,1);
strcpy(cuser_lines,instring);
GetUserLines(user_lines,0);

Printtext(withtxt);
  
PutString(" ",1);
GetUserName(NULL,1);
strcpy(username,instring);
PutString(username,0);
PutString(" wanna write on the wall? (y/N) ",0);
Getprompt(answer,1);
strcpy(answer,instring);

if ((answer[0] == 'n') || (answer[0] == 'N') || (answer[0] == NULL)) 
{goto noway;}

answer[0] = '\0';

PutString("Wanna show your username in your post? (y/N) ",0);
Getprompt(answer,1);
strcpy(answer,instring);

if ((answer[0] == 'y') || (answer[0] == 'Y')) 
{showname = 1;}

PutString (" ",1);
PutString ("         1         2         3         4         5         6         7         ",1);
PutString ("1234567890123456789012345678901234567890123456789012345678901234567890123456789",1);
GetUserName(NULL,1);
strcpy(username,instring);


if (showname == 1)
{ length = 79 - strlen(username); 
  Getprompt(post,length-4); }
else
{Getprompt(post,79); }

if (instring[0] == NULL)
  { goto noway; }

if (showname == 1)
{ strcpy(post,instring);
  strcat(post," -[");
  strcat(post,username);
  strcat(post,"]"); }
else
{ strcpy(post,instring); }
  
  length = 0;
  
  while (length < 79)    
    {strcat(post," ");
     length = strlen(post);}
     
Writetext(withtxt, post);

noway:
GetUserLines(cuser_lines,0);

ShutDown(); /* NEVER quit with exit() as it will cause AmiExpress
                       to wait around forever on you. */
}

/**********************************************************************/
/* This function does all of the message passing, and signal checking */
/**********************************************************************/
int CheckMessage(void)
{
int ret=0;

	if(DoorControlPort)
	{
		Forbid();
		PutMsg(DoorControlPort, (struct Message *)XIM_Msg);
		Permit();

		signals = Wait(usersig|portsig);
		if(signals & portsig)
		{
			if(reply = (struct XIM_Msg *) GetMsg(DoorReplyPort))
			{
				strcpy(instring,XIM_Msg->String);
				if(XIM_Msg->Data == -1) EXIT_FLAG=1;
				ret=XIM_Msg->Data;
			}
		}
		if(signals & usersig) EXIT_FLAG=1; /* CTRL-C was received */
	}
	else
	{
		printf("Error! Can no longer locate doorport!!\n");
		EXIT_FLAG=1;
	}
	if(EXIT_FLAG) ShutDown();
	return(ret);
}

/*****************************************************************************/

int PortStart(void)
{
int error=0;

	if(DoorReplyPort = CreatePort(0,0))
	{
		portsig = 1<<DoorReplyPort->mp_SigBit; /* setup signal bits */
		usersig = SIGBREAKF_CTRL_C;

		if(XIM_Msg = (struct XIM *) AllocMem(sizeof(struct XIM), MEMF_PUBLIC |MEMF_CLEAR))
		{
			/* Setup stuff for exec.. */
			XIM_Msg->Msg.mn_Node.ln_Type = NT_MESSAGE;
			XIM_Msg->Msg.mn_Length = sizeof(struct XIM);
			XIM_Msg->Msg.mn_ReplyPort = DoorReplyPort;

			XIM_Msg->Command = 1; /* Register Door with AmiExpress */

			Forbid();
			DoorControlPort = FindPort(doorport);
			Permit();

			if(DoorControlPort==NULL)
			{
				printf("Can't find doorport \"%s\"\n",doorport);
				error=3;
			}
			else CheckMessage();
		}
		else
		{
			printf("Can't allocate memory for XIM Message Structure!\n");
			error=2;
		}
	}
	else
	{
		printf("Can't setup reply port!\n");
		error=1;
	}

return(error);
}

void TakeOffEh(int code) /* Notify AmiExpress of shutdown, and then exit safely */
{
	XIM_Msg->Command=2;
	if(DoorControlPort) CheckMessage();

	if(XIM_Msg) FreeMem(XIM_Msg, sizeof(struct XIM));
	if(DoorReplyPort) DeletePort(DoorReplyPort);
	exit(code);
}

/*****************************************************************************\
 This is the start of the general routines that you will probably want to use.
 Only two other functions are provided in addition to the ShutDown() funciton,
 for the purpose of examples:

 To use the other XIM commands available, look up the Msg->Command/Data fields
 from the doordocs file, set them up for the XIM_Msg struct, and then
 CheckMessage() it.  The contents of XIM_Msg->String will be copied into the
 global instring[] for you when the command returns, and CheckMessage() will
 return the contents of XIM_Msg->Data.  (Note that 'XIM_Msg' is simply called
 'Msg' in the doordocs file.

\*****************************************************************************/

void ShutDown(void)
{
	/* This routine will be call automatically by CheckMessage if
           AmiExpress says to shut down, or if it receives a CTRL-C signal.
           This allows you to save any data before shutdown.

           IF YOU HAVE ANYTHING TO SAVE, DO IT HERE!!  THIS NEVER RETURNS!
	*/
	TakeOffEh(0);
}

void GetString(char text[], int len)
{
	XIM_Msg->Command=0;
	XIM_Msg->Data=len;
	strcpy(XIM_Msg->String,text);
	CheckMessage(); /* the returned string will be found in instring[] */
}

void PutString(char text[], int lf)
{
	XIM_Msg->Command=4;
	XIM_Msg->Data=lf;
	strcpy(XIM_Msg->String,text);
	CheckMessage();
}

void GetUserName(char text[], int len)
{
	XIM_Msg->Command=100;
	XIM_Msg->Data=len;
 	strcpy(XIM_Msg->String,text);
	CheckMessage();
}

void GetUserLines(char text[], int len)
{
	XIM_Msg->Command=122;
	XIM_Msg->Data=len;
	strcpy(XIM_Msg->String,text);
	CheckMessage();
}

void Getprompt(char text[], int len)
{
	XIM_Msg->Command=5;
	XIM_Msg->Data=len;
	strcpy(XIM_Msg->String,text);
	CheckMessage(); /* the returned string will be found in instring[] */
}

void Printtext(char Withtxt[20][80])

{
int ctr = 0;
FILE *fp;
char *ht[] = {"[36m-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",
              "[35m               \\/\\/all in the |-|ood V2.0 (C) 1993 SuBhUmAnZ/LSD               ",  
              "[36m-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-[37m",
              "[36m-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",
              "[32m                               A LSD PRODUCTION!                               ",
              "[36m-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-[31m",
             };
reader:

 if ((fp = fopen("doors:with.txt", "r")) == NULL)
   { Createtext();
     goto reader;
   }
   else
       {
        while(fgets(finstring,80,fp)!=NULL)
        { 
          strcpy(withtxt[ctr],finstring);
          ctr++;
       }
        }
        fclose(fp);

for (ctr=0;ctr<3; ctr++)
    {PutString(ht[ctr],1);}

for (ctr=0; ctr<14; ctr++)
   {PutString(withtxt[ctr],1);}

for (ctr=3;ctr<6; ctr++)
    {PutString(ht[ctr],1);}

return;
}

void Createtext(void)

{
FILE *fw;
char blank[80] =   "                                                                               ";
int ctr2;

if ((fw = fopen("doors:with.txt", "w")) == NULL)
   { PutString("Error openin file!",1); 
     ShutDown();}

else
   { 
   for (ctr2=0; ctr2<14; ctr2++)
       {fprintf(fw, "%s",blank);}
   }
fclose(fw);
return;
}

void Writetext(char withtxt[20][80], char post[80])

{
FILE *fo;
int ctr2;

if ((fo = fopen("doors:with.txt", "w")) == NULL)
   { PutString("Error openin file!",1); 
     ShutDown();}
else
   {
   for (ctr2=1; ctr2<14; ctr2++)
       {fprintf(fo, "%s",withtxt[ctr2]);}
        fprintf(fo, "%s",post);
       }
fclose(fo);
return;
}

