/*

  CheckUser
  =========

*/

#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>
#include <HBBS/release.h>
char *versionstr="$VER: CheckUser "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;
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);
}

void DoorMain( void )
{
  int loop;
  BOOL Done;
  BOOL LoginOK=TRUE;

  DOOR_DisplayScreen("HBBS:System/Data/Private");

  DOOR_WriteText(ANSI_RESET ANSI_FG_YELLOW "Checking your account"ANSI_FG_CYAN"... "ANSI_FG_YELLOW"Hold 1 sec!\r\n");

  strcpy(outstr,"HBBS:System/Data/ConfAcs");
  AddPart(outstr,N_ND->User->CallData->ConfAcsDataFile,1024);
  strcat(outstr,".CFG");
  if (!PathOK(outstr))
  {
    sprintf(outstr,"User: %s, conf acs file %s is missing!!\r\n",N_ND->User->CallData->Handle,N_ND->User->CallData->ConfAcsDataFile);
    HBBS_LogError(BBSGlobal->ErrorLogFile,ERR_GENERAL,outstr,TYPE_IMPORTANT);
    DOOR_WriteText("Your conference access settings could not be found, unable to join any\r\n"
                   "conferences.  The sysop has been notified of this problem.\r\n");
    DOOR_PausePrompt("Press A Key To Confinue");
    LoginOK=FALSE;
  }

  if (!HBBS_AllowConfAccess(N_ND->User->CallData->LastConf,NULL))
  {
    // Users conference is invalid, let's join the first conference we're allowed access to
    Done=FALSE;
    for (loop=1; loop <=BBSGlobal->Conferences && !Done ;loop++)
    {
      if (HBBS_AllowConfAccess(loop,NULL))
      {
        N_ND->User->CallData->LastConf=loop;
        N_ND->User->NormalData->LastConf=loop;
        Done=TRUE;
      }
    }
    if (!Done)
    {

      sprintf(outstr,"User: %s cannot join a conference",N_ND->User->CallData->Handle);
      HBBS_LogError(BBSGlobal->ErrorLogFile,ERR_GENERAL,outstr,TYPE_WARNING);
      DOOR_WriteText("Sorry, there are no conferences you can access\r\n"
                     "The sysop has been notified\r\n");
      DOOR_PausePrompt("Press [Enter] to leave a comment");
      DOOR_UserDoor("C",NULL);
      LoginOK=FALSE;
    }
  }

  if (!(N_ND->User->CallData->Language>=1 && N_ND->User->CallData->Language<=BBSGlobal->Languages))
  {
    // Invalid Language, call language select door to fix.
    
//    N_ND->User->CallData->Language=1; //default to first available language
//    N_ND->User->NormalData->Language=1;
    
    sprintf(outstr,"User: %s had an invalid language/style",N_ND->User->CallData->Handle);
    HBBS_LogError(BBSGlobal->ErrorLogFile,ERR_GENERAL,outstr,TYPE_WARNING);
    DOOR_WriteText("You have an invalid language/style selected, please pick another\r\n");
    DOOR_PausePrompt("Press [Enter] to choose a new language/style");
    DOOR_SystemDoor("SelectLanguage",NULL);
  }

  if ((HBBS_TimeLeft()<0) && (N_ND->User->Acs.Data[ACS_UNLIMTIME]=='N'))
  {
    if (!DOOR_DisplaySpecialScreen("NOTIME"))
    {
      DOOR_WriteText(N_ND->BBSStrings->OutOfTime);
      DOOR_WriteText("\r\n");
    }
    LoginOK=FALSE;
  }

  if (!LoginOK)
  {
    DOOR_Continue(FALSE);
    DOOR_Return("LOGIN_DENIED");
  }

}

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("Checking Account");

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