#include "bbs.h"
#include <dos/dos.h>
#include <dos/dosextens.h>
ULONG ConfBaseLine;
extern int NotRelative;
#define USERDATA 0
#define CONFTOTAL 1
#define CONF      2
#define Tell(x)  Seek(x,0L,OFFSET_CURRENT)
extern struct DiskObject *ConfObj;
extern UBYTE NewSinceFlag;
extern struct List *ConfDBList;
struct MyConfBase
{
  struct MinNode l;
  struct ConfBase CB;
};
void GetConfName(char *n,char *l,int Conf)
{
  char **oldtooltypes;
  char *s;
  char temp[100];
   
  oldtooltypes=ConfObj->do_ToolTypes;
  sprintf(temp,"NAME.%d",Conf+1);

  s=FindToolType(oldtooltypes,temp);
  if(s!=NULL) strcpy(n,s);
  sprintf(temp,"LOCATION.%d",Conf+1);
  s=FindToolType(oldtooltypes,temp);

  if(s!=NULL) { strcpy(l,s);  sprintf(MsgBase_Location,"%sMsgBase/",l); 
                              sprintf(Upload_Location,"%sUpload/",l);
              }
}

int IsTempConf(struct User *p,int Conf)
{
   char temp[100];
   char temp1[30];
   sprintf(temp,"%sACCESS/AREA.%s",Cmds->BBSLoc,p->Conference_Access);
   sprintf(temp1,"CONF.%d",Conf+1);
   if(IsFromIcon(temp,temp1)) return(1); else return(0);
}
int IsValidConf(int Conf)
{
   if(GetAreaBit(Conf)) return(1); else return(0);
}


void MasterLoadPointers(void)
{
   BPTR bi;
   struct ConfBase t;
   struct MyConfBase *it;
   struct MyConfBase *tt;
   char temp[100];
   register int i;
   if(User.Slot_Number<=0) {
       return;
     }
   it=(struct MyConfBase *)(ConfDBList->lh_Head);
   i=0;
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
     GetConfName(temp,Conference_Location,i);
     i++;
     sprintf(temp,"%sConf.DB",Conference_Location);
     bi=Open(temp,MODE_OLDFILE);
     if(bi==NULL)
     {
       CallersLog("\tError can't open >:"); CallersLog(temp); CallersLog("\n");
       return;
     }
    
     if(Seek(bi,(ULONG)(User.Slot_Number-1)*(ULONG)(sizeof(struct ConfBase)),OFFSET_BEGINNING)==-1)
     {
       CallersLog("\tError Reading MsgBase Pointer\n"); Close(bi); return;
     }
   
     FRead(bi,(APTR)&t,(ULONG)sizeof(struct ConfBase),1L);
     CopyMem(&t,(APTR)&(it->CB),sizeof(struct ConfBase));
     
   Close(bi);
     it=tt;
  }
}
void MasterSavePointers(void)
{
   BPTR bi;
   struct ConfBase t;
   struct MyConfBase *it;
   struct MyConfBase *tt;
   char temp[100];
   register int i;
 if(User.Slot_Number<=0) {
       return;
     }
   it=(struct MyConfBase *)(ConfDBList->lh_Head);
   i=0;
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
     GetConfName(temp,Conference_Location,i);
     i++;
     sprintf(temp,"%sConf.DB",Conference_Location);
     bi=Open(temp,MODE_OLDFILE);
     if(bi==NULL)
     {
       CallersLog("\tError can't open >:"); CallersLog(temp); CallersLog("\n");
       return;
     }
      
     if(Seek(bi,(ULONG)(User.Slot_Number-1)*(ULONG)(sizeof(struct ConfBase)),OFFSET_BEGINNING)==-1)
     {
       CallersLog("\tError Reading MsgBase Pointer\n"); Close(bi); return;
     }
     CopyMem((APTR)&(it->CB),(APTR)&t,sizeof(struct ConfBase));
     Write(bi,(APTR)&t,(ULONG)sizeof(struct ConfBase));
     
   Close(bi);
     it=tt;
  }
}
/***** Send Actual Conf + 1 to this routine *****/
void SaveMsgPointers(int CN)
{
   struct MyConfBase *it;
   struct MyConfBase *tt;
   register int i;
   if(User.Slot_Number<=0) {
       Last_Msg_Read_Conf=0;
       Last_New_Read_Conf=0;
       return;
   }
   it=(struct MyConfBase *)(ConfDBList->lh_Head);
   i=0;
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
      if(CN-1==i)
      {
if(GET_BIT(ACS_CONFERENCE_ACCOUNTING))
        {
         it->CB.Bytes_Download= User.Bytes_Download;
         it->CB.Bytes_Upload= User.Bytes_Upload;      
         //it->CB.Daily_Bytes_Limit= User.Daily_Bytes_Limit;
         //it->CB.Daily_Bytes_Dld= User.Daily_Bytes_Dld;   
         it->CB.Upload= User.Uploads;           
         it->CB.Downloads= User.Downloads;         
         it->CB.RatioType= User.Sec_Board;         
         it->CB.Ratio= User.Sec_Library;       
         it->CB.Messages_Posted= User.Messages_Posted;   

        }        if(NewSinceFlag)it->CB.NewSinceDate=time(NULL);
        it->CB.ConfYM=Last_Msg_Read_Conf;
        it->CB.ConfRead=Last_New_Read_Conf;
        return;
      }
      i++;
      it=tt;
   }
 
}
/***** Send Actual Conf + 1 to this routine *****/
void LoadMsgPointers(int CN)
{
   struct MyConfBase *it;
   struct MyConfBase *tt;
   register int i;
   if(User.Slot_Number<=0) {
       Last_Msg_Read_Conf=0;
       Last_New_Read_Conf=0;
       return;
   }
   it=(struct MyConfBase *)(ConfDBList->lh_Head);
   i=0;
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
      if(CN-1==i)
      {
        if(GET_BIT(ACS_CONFERENCE_ACCOUNTING))
        {
          User.Bytes_Download=    it->CB.Bytes_Download;
          User.Bytes_Upload=      it->CB.Bytes_Upload;
          //User.Daily_Bytes_Limit= it->CB.Daily_Bytes_Limit;
          //User.Daily_Bytes_Dld=   it->CB.Daily_Bytes_Dld;
          User.Uploads=           it->CB.Upload;
          User.Downloads=         it->CB.Downloads;
          User.Sec_Board=         it->CB.RatioType;
          User.Sec_Library=       it->CB.Ratio;
          User.Messages_Posted=   it->CB.Messages_Posted;

        }
        if(NewSinceFlag)it->CB.NewSinceDate=time(NULL);
        Last_Msg_Read_Conf=it->CB.ConfYM;
        Last_New_Read_Conf=it->CB.ConfRead;
        return;
      }
      i++;
      it=tt;
   }
 
}

void ClearMsgPointers(void)
{
   struct MyConfBase *it;
   struct MyConfBase *tt;
   if(User.Slot_Number<=0) {
       return;
   }
   it=(struct MyConfBase *)(ConfDBList->lh_Head);
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
          it->CB.Bytes_Download=0L;
          it->CB.Bytes_Upload=0L;
          it->CB.Upload=0;
          it->CB.Downloads=0;
          it->CB.RatioType=0;
          it->CB.Ratio=0;
          it->CB.Messages_Posted=0;
          it->CB.ConfYM=0;
          it->CB.ConfRead=0;

      it=tt;
   }
 
}

void AddMsgPointers(void)
{
   struct MyConfBase *it;
   struct MyConfBase *tt;
   if(User.Slot_Number<=0) { return;
   }
   it=(struct MyConfBase *)(ConfDBList->lh_Head);
if(!GET_BIT(ACS_CONFERENCE_ACCOUNTING)) return;
          User.Bytes_Download=    0L;
          User.Bytes_Upload=      0L;
          User.Uploads=           0;
          User.Downloads=         0;
          User.Messages_Posted=   0;

   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
          User.Bytes_Download +=    it->CB.Bytes_Download;
          User.Bytes_Upload +=      it->CB.Bytes_Upload;
          User.Uploads +=           it->CB.Upload;
          User.Downloads +=         it->CB.Downloads;
          User.Messages_Posted +=   it->CB.Messages_Posted;

      it=tt;
   }
 
}

int RelConf(int CN)
{
  register int i=0;
  register int count=0;
    while(i<CN)
    {
       if(GetAreaBit(i) || NotRelative) count++;
       i++;
    }
  return(count);
}
int GetInverse(int CN)
{
  register int i=0;
  register int j=0;
  if(CN -1< 0) return(0);
  if(NotRelative) return(CN);
    while(i<CN)
    {
      if(j<Boards_Are_Active)
      {
          if(GetAreaBit(j)) i++;
      } else { return(0); }
      j++;
    }
  return(j);

}

void Save_ConfDB(int account,int conf,APTR address)
{
   BPTR bi;
   register int i;
   struct ConfBase *t;
   struct MyConfBase *tt;
   struct MyConfBase *it;
   char temp[100];
   char temp1[100];
   t=address;
 if(account==User.Slot_Number)
   {
      it=(struct MyConfBase *)(ConfDBList->lh_Head);
   i=0;
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
      if(conf-1==i)
      {
        CopyMem((APTR)t,(APTR)&(it->CB),sizeof(struct ConfBase));
        return;
      }
      i++;
      it=tt;
   }
     return;
   }
   GetConfName(temp,temp1,conf-1);
   sprintf(temp,"%sConf.DB",temp1);
   bi=Open(temp,MODE_OLDFILE);
   if(bi==NULL)
   {
   CallersLog("\tError can't open >:"); CallersLog(temp); CallersLog("\n");
  
     return;
   }

   if(Seek(bi,(ULONG)(account-1)*(ULONG)(sizeof(struct ConfBase)),OFFSET_BEGINNING)==-1)
   {
      CallersLog("\tError Saving MsgBase Pointer\n"); Close(bi); return;
   }
   Write(bi,(APTR)t,sizeof(struct ConfBase));
   Close(bi);
   GetConfName(temp,temp1,CN-1);
}
void Load_ConfDB(int account,int conf,APTR address)
{
   BPTR bi;
   register int i;
   struct ConfBase *t;
   struct MyConfBase *it;
   struct MyConfBase *tt;
   char temp[100];
   char temp1[100];
   t=address;
   if(account==User.Slot_Number)
   {
      it=(struct MyConfBase *)(ConfDBList->lh_Head);
   i=0;
   while(tt=(struct MyConfBase *)it->l.mln_Succ)
   {
      if(conf-1==i)
      {
        CopyMem((APTR)&(it->CB),(APTR)t,sizeof(struct ConfBase));
        return;
      }
      i++;
      it=tt;
   }
      return;
   }
   GetConfName(temp,temp1,conf-1);
   sprintf(temp,"%sConf.DB",temp1);
   bi=Open(temp,MODE_OLDFILE);
   if(bi==NULL)
   {
   CallersLog("\tError can't open >:"); CallersLog(temp); CallersLog("\n");
  
     return;
   }

   if(Seek(bi,(ULONG)(account-1)*(ULONG)(sizeof(struct ConfBase)),OFFSET_BEGINNING)==-1)
   {
      CallersLog("\tError Saving MsgBase Pointer\n"); Close(bi); return;
   }
   FRead(bi,(APTR)t,sizeof(struct ConfBase),1L);
   Close(bi);
   GetConfName(temp,temp1,CN-1);
}
