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

/*
  DoorName
  ========

    Sets DoorLogOverride when a current users handle matches the a handle in a list
    of suspect users specified in Progdir:Suspect.CFG in the Users_XX settings

  Version
  =======

    1.0, release 1, 18/06/1996

  Options
  =======

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

    None

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

    None

  ToDo
  ====

    Nothing

  Config Files
  ============

    Progdir:Suspect.CFG
    -------------------

      Users_XX=<Handle>    List of handles
      Message=<Message>    A Message to display to the user if they are Suspect
      LogNewUsers=YES|NO   Log All New Users Actions ?

*/

/* **** 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 <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: SetSuspect "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;

/* **** 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 )
{
  struct CfgFileData *CfgFile;
  struct List *Users=NULL;
  struct Node *node;
  V_STRING MsgStr=NULL;
  BOOL Found=FALSE,LogNewUsers=FALSE;

  DOOR_WriteText(ANSI_RESET ANSI_FG_BLUE"[ "ANSI_FG_WHITE"SetSuspect"ANSI_FG_BLUE" ]"ANSI_FG_CYAN"  by "ANSI_FG_YELLOW"Hydra^cP!"ANSI_FG_WHITE"\r\n");

  if (CfgFile=HBBS_LoadConfig("Progdir:Suspect.CFG",LCFG_NONE))
  {
    if (HBBS_GetSetting(CfgFile,(void *)&LogNewUsers,VTYPE_BOOL,"LogNewUsers",OPT_SINGLE) && LogNewUsers && N_ND->User->CallData->Status==USER_NEW)
    {
      Found=TRUE;
    }
    else
    if (HBBS_GetSetting(CfgFile,(void *)&Users,VTYPE_STRINGLIST,"User",OPT_MULTI))
    {
      for (node=Users->lh_Head;!Found && node->ln_Succ;node=node->ln_Succ)
      {
        if (stricmp(node->ln_Name,N_ND->User->CallData->Handle)==0)
        {
          Found=TRUE;
        }
      }
      FreeStrList(Users);
    } else DOOR_PausePrompt("Config File Error, Missing Users Item");

    if (Found)
    {
      if (HBBS_GetSetting(CfgFile,(void *)&MsgStr,VTYPE_STRING,"Message",OPT_SINGLE))
      {
        N_ND->DoorLogOverride=TRUE;
        DOOR_WriteText(MsgStr);
        DOOR_WriteText("\r\n");
        FreeStr(MsgStr);
      }
    }
    HBBS_FlushConfig(CfgFile);
  }
  else DOOR_PausePrompt("ProgDir:Suspect.CFG Not Found!");

}

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

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

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