#include "bbs.h"

int numboards = -1 ;
struct shortfile bcache[MAXBOARD] ;
unsigned char bmode[MAXBOARD] ;

fillcache(fptr)
struct fileheader *fptr ;
{
    strcpy(bcache[numboards].filename,fptr->filename) ;
    strcpy(bcache[numboards].owner,fptr->owner) ;
    strcpy(bcache[numboards].title,fptr->title) ;
    bmode[numboards] = ((usernum<MAXUSERS)?fptr->accessed[usernum]:
                        fptr->accessed[usernum%MAXUSERS]>>4) & 0xf ;
    if(numboards < MAXBOARD - 1)
      numboards++ ;
    return 0 ;
}

apply_boards(func)
int (*func)() ;
{
    register int i ;

    if(numboards < 0) {
        numboards = 0 ;
        apply_record(BOARDS,fillcache,sizeof(struct fileheader)) ;
    }
    
    for(i=0;i<numboards;i++)
      if((*func)(&bcache[i],(int)bmode[i]) == QUIT)
        break ;
}

clear_board_zaps()
{
    register int i,j ;
    struct fileheader binfo ;
    extern char uarray[MAXUSERS*2] ;

    if(numboards < 0) {
        numboards = 0 ;
        apply_record(BOARDS,fillcache,sizeof(struct fileheader)) ;
    }
    
    for(i=0;i<numboards;i++) {
        if(get_record(BOARDS,&binfo,sizeof(binfo),i) == -1)
          continue ;
        for(j=0; j<MAXUSERS*2; j++) {
            if(uarray[j])
              binfo.accessed[j%MAXUSERS] &= ~SHIFTMODE(j,0xf) ;
        }
        substitute_record(BOARDS,&binfo,sizeof(binfo),i) ;
    }
}

clear_user_zaps(uid)
{
    register int i ;
    struct fileheader binfo ;

    if(numboards < 0) {
        numboards = 0 ;
        apply_record(BOARDS,fillcache,sizeof(struct fileheader)) ;
    }

    for(i=0;i<numboards;i++) {
        if(get_record(BOARDS,&binfo,sizeof(binfo),i) == -1)
          continue ;
        binfo.accessed[uid%MAXUSERS] &= ~SHIFTMODE(uid,0xf) ;
        substitute_record(BOARDS,&binfo,sizeof(binfo),i) ;
    }
}


getbnum(bname)
char *bname ;
{
    register int i ;

    if(numboards < 0) {
        numboards = 0 ;
        apply_record(BOARDS,fillcache,sizeof(struct fileheader)) ;
    }

    for(i=0;i<numboards;i++)
      if(!strcmp(bname,bcache[i].filename))
        return i+1 ;
    return 0 ;
}


long ucached = 0 ;
char ucache[MAXUSERS*2][USERIDSIZE] ;
int numuents = 0;

fillucache(uentp)
struct user *uentp ;
{
    if(numuents < MAXUSERS*2) {
        strncpy(ucache[numuents],uentp->userid,USERIDSIZE) ;
        ucache[numuents++][USERIDSIZE-1] = '\0' ;
    }
    return 0 ;
}

resolve_ucache()
{
    struct stat st ;
    if(stat(FLUSH,&st) < 0) {
        st.st_mtime++ ;
    }
    if(ucached < st.st_mtime) {
        numuents = 0 ;
        apply_record(PASSFILE,fillucache,sizeof(struct user)) ;
        ucached = st.st_mtime ;
    }
}

u_namelist()
{
    register int i ;

    resolve_ucache() ;
    CreateNameList() ;
    for(i=0; i < numuents; i++)
      if(ucache[i][0] != '\0')
        AddNameList(ucache[i]) ;
    return 0 ;
}


int
searchuser(userid)
char *userid ;
{
    register int i ;

    resolve_ucache() ;
    for(i=0; i < numuents; i++)
      if(!strncmp(userid,ucache[i],13))
        return i+1 ;
    return 0 ;
}

apply_users(func)
void (*func)() ;
{
    register int i ;
    resolve_ucache() ;
    for(i=0; i < numuents; i++)
      (*func)(ucache[i],i+1) ;
    return 0 ;
}

struct user lookupuser ;

int
getuser(userid)
char *userid ;
{
    int uid = searchuser(userid) ;
    if(uid == 0)
      return 0 ;
    get_record(PASSFILE,&lookupuser,sizeof(lookupuser),uid) ;
    return uid ;
}

