/* **** 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/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[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 )
{
  BOOL SysopMode=FALSE;
  BOOL Done=FALSE;
  BOOL CallDataOnly=TRUE;

  ULONG GLFlags=GL_EDIT + GL_DISPLAY;

  struct UserData UserDat;
  struct Node *node;
  V_BIGNUM BigNum,OptionNum;
  V_SMALLNUM SmallNum;
  struct UserData *User=NULL,*User2=NULL;

  // User is always modified, user2 is always modified unless sysop pressed SHIFT+F6
  // to activiate this door.

  if ((gargc>=3) && (iposition("SYSOPONLY",gargv[2])>=0))
  {
    SysopMode=TRUE;
    CallDataOnly=FALSE;
    DOOR_WriteText(ANSI_RESET ANSI_CLS "Wait while the sysop changes your account settings!\r\n");
    N_ND->NodeFlags+=NFLG_BLOCKSERIAL;


    GLFlags+=GL_SYSOP;


    if ((gargc>=4) && (iposition("CALLONLY",gargv[3])>=0))
    {
      CallDataOnly=TRUE;
    }

    if (N_ND->User.Valid)
    {
      User=&N_ND->User.CallData;
      User2=&N_ND->User.NormalData;
    }
    else
    {
      DOOR_WriteText("No User is Logged On Yet!\r\n");
    }

  }
  else
  {
    // *C* Add option to scan for new users..

    DOOR_WriteText("Enter Handle :");
    DOOR_GetLine(GLFlags,0,LEN_HANDLE,0,NULL);
    RemoveSpaces(N_ND->CurrentLine);
    if (N_ND->CurrentLine[0])
    {
      if (HBBS_ValidUserHandle(N_ND->CurrentLine,&UserDat))
      {
        User=&UserDat;
        User2=&UserDat;
      }
      else
      {
        DOOR_WriteText("Could Not Find That User!\r\n");
      }
    }
  }

  if (User && User2)
  {
    do
    {
      DOOR_WriteText(ANSI_RESET ANSI_CLS);

      strNcpy(outstr,gargv[0],(PathPart(gargv[0])-gargv[0])+1);
      strcat(outstr,"AccountEdit.SCR");
      DOOR_DisplayScreen(outstr);

      sprintf(outstr," 1) Handle         :\"%s\"\r\n",User->Handle);
      DOOR_WriteText(outstr);
      sprintf(outstr," 2) Password       :\"%s\"\r\n",User->Password);
      DOOR_WriteText(outstr);
      sprintf(outstr," 3) Status         :\"%c\"\r\n",User->Status);
      DOOR_WriteText(outstr);
      sprintf(outstr," 4) Group          :\"%s\"\r\n",User->Group);
      DOOR_WriteText(outstr);
      sprintf(outstr," 5) Access Level   :\"%d\"\r\n",User->Access);
      DOOR_WriteText(outstr);
      sprintf(outstr," 6) Bytes Ratio    :\"%d\"\r\n",User->BytesRatio);
      DOOR_WriteText(outstr);
      sprintf(outstr," 7) Files Ratio    :\"%d\"\r\n",User->FilesRatio);
      DOOR_WriteText(outstr);
      sprintf(outstr," 8) Bytes Upload   :\"%d\"\r\n",User->UploadBytes);
      DOOR_WriteText(outstr);
      sprintf(outstr," 9) Files Upload   :\"%d\"\r\n",User->UploadFiles);
      DOOR_WriteText(outstr);
      sprintf(outstr,"10) ConfAcs File   :\"%s\"\r\n",User->ConfAcsDataFile);
      DOOR_WriteText(outstr);
      sprintf(outstr,"11) SentBy         :\"%s\"\r\n",User->SentBy);
      DOOR_WriteText(outstr);
      sprintf(outstr,"12) Time Allowed   :\"%d\"\r\n",User->TimeAllowed);
      DOOR_WriteText(outstr);
      sprintf(outstr,"13) Time Used      :\"%d\"\r\n",User->TimeUsed);
      DOOR_WriteText(outstr);
      sprintf(outstr,"14) Extra Time     :\"%d\"\r\n",User->ExtraTimeLimit);
      DOOR_WriteText(outstr);
      sprintf(outstr,"15) Preferred Conf :\"%d\"\r\n",User->PreferedConf);
      DOOR_WriteText(outstr);
      sprintf(outstr,"16) Last Conf      :\"%d\"\r\n",User->LastConf);
      DOOR_WriteText(outstr);

      DOOR_MenuPrompt("\r\n[Q]uit, [H]elp, [V]alidate or # of option >",0);
      if (DOOR_GetLine(GLFlags,'\0',0,0,NULL)==IN_GOTLINE)
      {
        RemoveSpaces(N_ND->CurrentLine);
        if (stricmp("Q",N_ND->CurrentLine)==0)
        {
          DOOR_WriteText("Quit..\r\n");
          Done=TRUE;
        }
        else
        if (stricmp("H",N_ND->CurrentLine)==0)
        {
          DOOR_WriteText("To use the Validate option, just set the access level for the user\r\n"
                         "Then use the V option and the rest of the users settings will be changed\r\n");
          DOOR_PausePrompt(NULL);
        }
        else
        if (stricmp("V",N_ND->CurrentLine)==0)
        {
          // Set VALIDATED status
          User->Status=USER_VALIDATED;
          if (!CallDataOnly) User2->Status=USER_VALIDATED;

          sprintf(outstr,"%d",User->Access);
          if ((BigNum=HBBS_FindNodeNum(BBSGlobal->AcsLevelList,outstr))>=0)
          {
            strcpy(User->ConfAcsDataFile,HBBS_ListName(BBSGlobal->AcsLevelConfAcsDef,BigNum));
            if (!CallDataOnly) strcpy(User2->ConfAcsDataFile,HBBS_ListName(BBSGlobal->AcsLevelConfAcsDef,BigNum));
          }
        }
        else
        {
          OptionNum=0;
          if (sscanf(N_ND->CurrentLine,"%d",&OptionNum))
          {
            switch (OptionNum)
            {
              case 1: // handle
                DOOR_WriteText("New Handle >");
                DOOR_GetLine(GLFlags,0,LEN_HANDLE,0,User->Handle);
                if (N_ND->CurrentLine[0])
                {
                  strcpy(User->Handle,N_ND->CurrentLine);
                  if (!CallDataOnly) strcpy(User2->Handle,N_ND->CurrentLine);
                }
                break;
              case 2: // Password
                DOOR_WriteText("New Password >");
                DOOR_GetLine(GLFlags,0,0,0,User->Password);
                if (N_ND->CurrentLine[0])
                {
                  strcpy(User->Password,N_ND->CurrentLine);
                  if (!CallDataOnly) strcpy(User2->Password,N_ND->CurrentLine);
                }
                break;
              case 3: //status
                sprintf(outstr,"%c",User->Status);
                DOOR_WriteText("New Status >");
                DOOR_GetLine(GLFlags,0,1,0,outstr);
                if (N_ND->CurrentLine[0])
                {
                  User->Status=N_ND->CurrentLine[0];
                  if (!CallDataOnly) User2->Status=N_ND->CurrentLine[0];
                }
                break;
              case 4: // Password
                DOOR_WriteText("New Group >");
                DOOR_GetLine(GLFlags,0,LEN_GROUP,0,User->Group);
                if (N_ND->CurrentLine[0])
                {
                  strcpy(User->Group,N_ND->CurrentLine);
                  if (!CallDataOnly) strcpy(User2->Group,N_ND->CurrentLine);
                }
                break;
              case 5: //access level
                sprintf(outstr,"%ld",User->Access);
                DOOR_WriteText("New Access >");
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  // validate access level!
                  if (HBBS_FindNodeNum(BBSGlobal->AcsLevelList,N_ND->CurrentLine)>=0)
                  {
                    User->Access=SmallNum;
                    if (!CallDataOnly) User2->Access=SmallNum;
                  }
                  else
                  {
                    DOOR_PausePrompt("Invalid Access Level, check HBBS:Access/Levels/Level_List for valid ones");
                  }
                }
                break;
              case 6: // bytes ratio
                sprintf(outstr,"%ld",User->BytesRatio);
                DOOR_WriteText("New Bytes Ratio >");
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->BytesRatio=SmallNum;
                  if (!CallDataOnly) User2->BytesRatio=SmallNum;
                }
                break;
              case 7: // files ratio
                DOOR_WriteText("New Files Ratio >");
                sprintf(outstr,"%ld",User->FilesRatio);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->FilesRatio=SmallNum;
                  if (!CallDataOnly) User2->FilesRatio=SmallNum;
                }
                break;
              case 8: //bytes uploaded
                DOOR_WriteText("New Bytes Uploaded >");
                sprintf(outstr,"%ld",User->UploadBytes);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&BigNum))
                {
                  User->UploadBytes=BigNum;
                  if (!CallDataOnly) User2->UploadBytes=BigNum;
                }
                break;
              case 9: //files uploaded
                DOOR_WriteText("New Files Uploaded >");
                sprintf(outstr,"%ld",User->UploadFiles);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&BigNum))
                {
                  User->UploadFiles=BigNum;
                  if (!CallDataOnly) User2->UploadFiles=BigNum;
                }
                break;
              case 10: // ConfAcs File
                DOOR_WriteText("Access File >");
                DOOR_GetLine(GLFlags,0,LEN_CONFACCESSFILE,0,User->ConfAcsDataFile);
                if (N_ND->CurrentLine[0])
                {
                  strcpy(outstr,"HBBS:System/Data/ConfAcs");
                  AddPart(outstr,N_ND->CurrentLine,1024);
                  strcat(outstr,".CFG");
                  if (PathOK(outstr))
                  {
                    strcpy(User->ConfAcsDataFile,N_ND->CurrentLine);
                    if (!CallDataOnly) strcpy(User2->ConfAcsDataFile,N_ND->CurrentLine);
                  }
                }
                break;
              case 11: // Sentby File
                DOOR_WriteText("SentBy >");
                DOOR_GetLine(GLFlags,0,LEN_SENTBY,0,User->SentBy);
                strcpy(User->SentBy,N_ND->CurrentLine);
                if (!CallDataOnly) strcpy(User2->SentBy,N_ND->CurrentLine);
                break;
              case 12: // Time Allowed
                DOOR_WriteText("Time Allowed >");
                sprintf(outstr,"%ld",User->TimeAllowed);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->TimeAllowed=SmallNum;
                  if (!CallDataOnly) User2->TimeAllowed=SmallNum;
                }
                break;
              case 13: // Time Used
                DOOR_WriteText("Time Used >");
                sprintf(outstr,"%ld",User->TimeUsed);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->TimeUsed=SmallNum;
                  if (!CallDataOnly) User2->TimeUsed=SmallNum;
                }
                break;
              case 14: // Extra Time Limit
                DOOR_WriteText("Extra Time Limit >");
                sprintf(outstr,"%ld",User->ExtraTimeLimit);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->ExtraTimeLimit=SmallNum;
                  if (!CallDataOnly) User2->ExtraTimeLimit=SmallNum;
                }
                break;
              case 15: // Preferred Conf
                DOOR_WriteText("Preferred Conf >");
                sprintf(outstr,"%ld",User->PreferedConf);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->PreferedConf=SmallNum;
                  if (!CallDataOnly) User2->PreferedConf=SmallNum;
                }
                break;
              case 16: // Last Conf
                DOOR_WriteText("Last Conf >");
                sprintf(outstr,"%ld",User->LastConf);
                DOOR_GetLine(GLFlags,0,0,0,outstr);
                if (sscanf(N_ND->CurrentLine,"%d",&SmallNum))
                {
                  User->LastConf=SmallNum;
                  if (!CallDataOnly) User2->LastConf=SmallNum;
                }
                break;

              Default :
                DOOR_WriteText("Unknown Option!\r\n");
                break;
            }
          }
        }
      }
      else Done=TRUE;
    } while (!Done);

    if (SysopMode && CallDataOnly) DOOR_WriteText("Changes only last for this call!\r\n");

    HBBS_SetAccess();
  }
  if (!SysopMode) //
  {
    HBBS_SaveUserData(User);
  }
  else
  {
    N_ND->NodeFlags-=NFLG_BLOCKSERIAL;
  }
  DOOR_WriteText("Done.\r\n");
}

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

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

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