/* **** 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 <intuition/intuition.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>
#include <clib/intuition_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>
#include <HBBS/release.h>
char *versionstr="$VER: Pager "RELEASE_STR;

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


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


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

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;

struct IntuitionBase *IntuitionBase = NULL;

/* **** 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 (IntuitionBase)
  {
    CloseLibrary( ( struct Library * )IntuitionBase );
  }

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

  exit(0);
}

static VOID init(char *name)
{
  if (!(IntuitionBase = (struct IntuitionBase * )OpenLibrary((UBYTE *)"intuition.library" , 37)))
  {
    cleanup(5);
  }
  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 )
{
  LONG Beeps=0,MaxBeeps=15,glret;

  DOOR_Add_Last_Pager(N_ND->User->NormalData->Handle);

  if (!(N_ND->NodeFlags & NFLG_PAGED)) N_ND->NodeFlags += NFLG_PAGED;
  DOOR_UpdateNodeStatus(UPD_NAME);

  N_ND->Actions[ACTN_PAGED]=ACTC_PAGED;
  // *C* check if user is allowed to page (name NOT is PageDisabled.CFG file)

  DOOR_DisplaySpecialScreen("Page");
  DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "Enter Page Reason" ANSI_FG_BLUE " : " ANSI_FG_WHITE);
  DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY,'\0',45,0,NULL);
  if (N_ND->OnlineStatus==OS_ONLINE)
  {
    RemoveSpaces(N_ND->CurrentLine);
    if (N_ND->CurrentLine[0])
    {
      N_ND->User->PagesMade++;
      sprintf(outstr,"Paged, Reason: %s",N_ND->CurrentLine);
      HBBS_AddToCallersLog(outstr);

      if (N_ND->NodeSettings.ChatFlag)
      {
        DOOR_MenuPrompt("Paging Sysop, any key to cancel",' ');

        do
        {
          DOOR_WriteText(" . ");
          Beeps++;
          DisplayBeep(NULL);
          if (DOOR_CheckRaw(CR_CHECKFUNC)) // auto handle the page..
          {
            glret=N_ND->CurrentLine[3];
          }
          else
          {
            Delay(60);
            glret=IN_TIMEOUT;
          }
        } while (N_ND->OnlineStatus==OS_ONLINE && Beeps<MaxBeeps && glret==IN_TIMEOUT);

        if (N_ND->OnlineStatus==OS_ONLINE)
        {
          DOOR_WriteText("\r\n");
          switch(glret)
          {
            case IN_GOTLINE:
              DOOR_DisplaySpecialScreen("PageCancel");
              break;
            case IN_PAGEANSWERED:
              DOOR_DisplaySpecialScreen("PageAnswered");
              break;
            default:
              DOOR_DisplaySpecialScreen("Paged");
              break;
          }
        }
      }
      else
      {
        DOOR_DisplaySpecialScreen("PageNoSysop");
      }
    }
  }
}

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("Paging Sysop");

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

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