/* **** HBBS Door Code****************************************************** */

/*
  DoorName
  ========

    what the door does

  Version
  =======

    x.xx, release xx, dd/mm/yyyy

  Options
  =======

    N_ND->ActiveDoor->SystemOptions
    -------------------------------



    Command Line Arguments
    ----------------------

    2  <option>

    3  ...

    4  .

  ToDo
  ====

    what you still have to do



*/

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

#include <exec/types.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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/Access.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>
#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas.h>

/* **** Variables ********************************************************** */


struct Library *HBBSCommonBase  = NULL;
struct Library *HBBSNodeBase    = NULL;

struct BBSGlobalData *BBSGlobal = NULL;
struct NodeData *N_ND           = NULL;
int    N_NodeNum                = -1;
char   outstr[2048];

long __stack=16*1024; // increse this in 4k incrments if you suffer from
                      // random/suprious crashings after or during the running
                      // of your door.

int    gargc;         // these are just copies of main()'s argc and argv..
char   **gargv;

/* **** Functions ********************************************************** */


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif

static VOID cleanup(ULONG num)
{
  if (HBBSNodeBase)
  {
    HBBS_CleanUpDoor();
    CloseLibrary (HBBSNodeBase);
  }

  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

  if (num) printf("Door Error = %d\n",num);

  exit(0);
}

static VOID init(char *name)
{
  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    cleanup(1);
  }

  if (!(HBBS_InitCommon()))
  {
    cleanup(2);
  }

  if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  {
    cleanup(3);
  }

  if (!(HBBS_InitDoor(N_NodeNum,name)))
  {
    cleanup(4);
  }
  SetProgramName(name);
}

/* **** DoorMain *********************************************************** */

void DoorMain( void )
{
  BOOL error=FALSE;
  struct OLMNode *OLM;

  char oldcharsallowed[BIG_STR];

  strcpy(oldcharsallowed,N_ND->CharsAllowed);
  N_ND->NodeFlags+=NFLG_HANDLINGOLM; // *C* check it's not already set!

  while (N_ND->OLMCount && N_ND->OnlineStatus==OS_ONLINE && !error)
  {
    if (OLM = (struct OLMNode*)N_ND->OLMList->lh_Head)
    {
      sprintf(outstr,ANSI_RESET ANSI_FG_WHITE "\r\nOLM Received! %s %s%s%s\r\n",OLM->Time,OLM->Date,OLM->FromPRG[0] ? " Created By: " : "",OLM->FromPRG);

      if (OLM->Handle[0])
      {
        sprintf(outstr+strlen(outstr),ANSI_FG_CYAN "From "ANSI_FG_BLUE": "ANSI_FG_WHITE "%s, ",OLM->Handle);
      }

      sprintf(outstr+strlen(outstr),ANSI_FG_CYAN "Message "ANSI_FG_BLUE": "ANSI_FG_WHITE "%s\r\n",OLM->node.ln_Name);
      DOOR_WriteText(outstr);

      if (OLM->FromNode)
      {
        if (DOOR_ContinuePrompt("Reply to the OLM ? ",DEFAULT_NO|DCP_ADDYN))
        {
          DOOR_WriteText(ANSI_FG_YELLOW "Enter a reply "ANSI_FG_BLUE "> "ANSI_FG_WHITE);
          DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_NOOLM,'\0',0,0,NULL);
          RemoveSpaces(N_ND->CurrentLine);
          if (N_ND->OnlineStatus==OS_ONLINE)
          {
            if (N_ND->CurrentLine[0])
            {
              if (HBBS_SendOLM(N_ND->NodeNum+1,"ReplyOLM",OLM->FromNode-1,N_ND->CurrentLine,0))
              {
                DOOR_WriteText("Reply Sent!\r\n");
              }
              else
              {
                DOOR_WriteText("Reply Failed, node may be busy, offline or user has logged off!\r\n");
              }
            }
          } else error=TRUE; //lost carrier!
        }
      }
      else
      {
        DOOR_PausePrompt(NULL);
      }

      HBBS_FreeNode((struct Node*)OLM,TRUE); //free and remove()
      N_ND->OLMCount--;
    }
    else error=TRUE;
  }
  if (!N_ND->OLMCount)
  {
    if (N_ND->NodeFlags & NFLG_OLMSWAITING) N_ND->NodeFlags-=NFLG_OLMSWAITING;
  }

  N_ND->NodeFlags-=NFLG_HANDLINGOLM; // *C* check it's set!

  strcpy(N_ND->CharsAllowed,oldcharsallowed);

}

int main(int argc,char **argv)
{
  gargc=argc;
  gargv=argv;

  if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  {
    printf("Invalid/No Paramaters for door!\n");
    exit (20);
  }
  init("ReadingOLMs");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
    {
      DoorMain();
    }
  }
  cleanup(0);
}

/* **** End Of File ******************************************************** */
