/*

  CheckUser
  =========

    This door is called when a user has logged on to the system, just after frontend
    It's function is to check all the user's settings are OK, (e.g. LastConf still
    exsists, mail scan pointers are in valid ranges, etc etc)

    You can also use it for sentby checking and the like...

*/

#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/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>

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

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

int    gargc;
char   **gargv;

#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... 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 A Key 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.
    sprintf(outstr,"User: %s had an invalid language",N_ND->User.CallData.Handle);
    HBBS_LogError(BBSGlobal->ErrorLogFile,ERR_GENERAL,outstr,TYPE_WARNING);
    DOOR_WriteText("You have an invalid language/ansi-type selected, please pick another\r\n");
    DOOR_PausePrompt("Press A Key To Choose A Language");
    DOOR_SystemDoor("SelectLanguage",NULL);
  }
  if (!LoginOK)
  {
    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);
}
