#include <exec/exec.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <proto/dos.h>
#include "work:romconf/doorheader.h"
#include "source:aztec/glue.h"
#define Tell(x)  Seek(x,0L,OFFSET_CURRENT)
ULONG GetConfBaseLine(int user);
#define USERDATA 0
#define CONFTOTAL 1
#define CONF      2

struct ConfBase
{
  ULONG NewSinceDate, ConfRead,ConfYM1;
  ULONG Bytes_Download,Bytes_Upload;
  USHORT Upload,Downloads,RatioType,Ratio;
  UWORD Access;
};


struct MyData
{
  int Type,Size,misc1,Version;
} ;
struct MasterData
{
   struct MyData   Code;
   struct ConfBase Conf;
};
#define sm sendmessage
#define pm prompt
#define hk hotkey
#define gu getuserstring
int user;
int conf;
int conftotal=0;
ULONG ConfBaseLine;
void LastCommand(void);
void ChangeAccess(int user,int Conf);
void end(void);
char BBSLoc[200];
main(int argc,char *argv[])
{
   char temp[100];
   if(argc!=2)
   {
     printf("Access version 1.0, written by Joseph Hodge\n");
     printf("(XIM) for /X 2.30+\n");
     printf("\n");
     exit(0);
   }
   Register(argv[1][0]-'0');
   sm("",1);
   sm("[36mAccess version 1.0[33m, [32mwritten by Joseph Hodge",1);
   pm("[32mEnter UserNum [33m>:[0m",temp,5);
   user=atoi(temp);
   pm("[32mEnter Conference to Toggle Access [33m>:[0m",temp,3);
   conf=atoi(temp)-1;
   gu(BBSLoc,BB_LOCAL);
   ConfBaseLine=GetConfBaseLine(user);
   if(ConfBaseLine==0L || conf>conftotal || conf <0)
   {
     sm("",1);
     sm("Sorry, Access impossible[0m",1);
     sprintf(temp,"Address %ld",ConfBaseLine);
     sm(temp,1);
     sprintf(temp,"Conference to change %d",conf);
     sm(temp,1);
     sprintf(temp,"TotalConf = %d",conftotal);
     sm(temp,1);
     ShutDown();
     end();
   }
   ChangeAccess(user,conf);
   ShutDown();
   end();
}
void LastCommand(void)
{
  sm("",1);
}
void end(void)
{
  exit(0);
}
void ChangeAccess(int user,int Conf)
{
   BPTR bi;
   struct MasterData t;
   char temp[100];
   sprintf(temp,"%sUDBase/user.%04d",BBSLoc,user);
   bi=Open(temp,MODE_OLDFILE);
   if(bi==NULL)
   {
     return;
   }

   Seek(bi,ConfBaseLine+(ULONG)(sizeof(struct MasterData)*Conf),OFFSET_BEGINNING);
   FRead(bi,(APTR)&t,sizeof(struct MasterData),1);
   if(t.Conf.Access) t.Conf.Access=0; else t.Conf.Access=1;
   Seek(bi,ConfBaseLine+(ULONG)(sizeof(struct MasterData)*Conf),OFFSET_BEGINNING);
   Write(bi,(APTR)&t,sizeof(struct MasterData));
   Close(bi);
}
ULONG GetConfBaseLine(int user)
{
   BPTR bi;
   char temp[100];
   struct MyData t;
   ULONG Address;
   sprintf(temp,"%sUDBase/user.%04d",BBSLoc,user);
   bi=Open(temp,MODE_OLDFILE);
   if(bi==NULL) return(0L);
   FRead(bi,(APTR)&t,sizeof(struct MyData),1);
   while(t.Type!=CONFTOTAL)
   {
     Seek(bi,(ULONG)t.Size,OFFSET_CURRENT);
     FRead(bi,(APTR)&t,sizeof(struct MyData),1);
   }
   conftotal=t.misc1;
   Address=Tell(bi);
   Close(bi);
   return(Address);
}
   