/*
    Pirate Bulletin Board System
    Copyright (C) 1990, Edward Luke

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    Email address:
    lush@Athena.EE.MsState.EDU
*/
#include "bbs.h"
#include "io.h"

d_exit()
{
	clear() ;
	return QUIT ;
}

/*ARGSUSED*/
boardcheck(fptr)
struct fileheader *fptr ;
{
/*	if(!strcmp(fptr->filename,genbuf))
	  return 1 ;
	return 0 ;*/
	return 1 ;
}

d_board()
{
	struct fileheader binfo ;
	int bid ;
    extern int numboards ;

	move(3,0) ;
	clrtobot() ;
	move(0,0) ;
	standout() ;
	prints("DELETE BOARD DELETE BOARD DELETE BOARD") ;
	standend() ;
	clrtoeol() ;
	move(1,0) ;
    make_blist() ;
	namecomplete("Enter Board Name: ",genbuf) ;
	bid = getbnum(genbuf) ;
	if(get_record(BOARDS,&binfo,sizeof(binfo),bid) == -1) {
		move(2,0) ;
		prints("Invalid Board Number\n") ;
		pressreturn() ;
		clear() ;
		return 0 ;
	}
	move(1,0) ;
	prints("Delete board '%s'.",binfo.filename) ;
	getdata(2,0,"(Yes, or No): ",genbuf,4,DOECHO,NULL) ;
	if(strcmp(genbuf,"Yes") && strcmp(genbuf,"yes")) { /* if not yes quit */
		move(2,0) ;
		prints("Quiting delete board\n") ;
		pressreturn() ;
		clear() ;
		return 0 ;
	}
	sprintf(genbuf,"rm -fr boards/%s",binfo.filename) ;
	do_exec(genbuf,NULL) ;
	strncpy(genbuf,binfo.filename,sizeof(genbuf)) ;
	delete_file(BOARDS,sizeof(binfo),bid,boardcheck) ;
	move(2,0) ;
    numboards = -1 ;
	prints("Board Deleted\n") ;
	pressreturn() ;
	clear() ;
	return 0 ;
}

int delusernum ;

duentposts(fptr)
struct fileheader *fptr ;
{
	char *bfile() ;
	struct fileheader update ;
	static int id ;

	if(fptr == NULL) {
		id = 0 ;
		return 0;
	}
	id++ ;
	if(get_record(strcat(bfile(currboard),DIR),&update,sizeof(update),id))
	  return -1 ;
    CLRFILEMODE(update.accessed,delusernum,FILE_READ) ;
    CLRFILEMODE(update.accessed,delusernum,FILE_OWND) ;
    CLRFILEMODE(update.accessed,delusernum,FILE_VISIT) ;
    CLRFILEMODE(update.accessed,delusernum,FILE_SPARE) ;
	if(substitute_record(strcat(bfile(currboard),DIR),&update,sizeof(update),id))
	  return -1 ;
	return 0 ;
}

duentbds(fptr,mode)
struct fileheader *fptr ;
int mode ;
{
	char *bfile() ;
	
	duentposts(NULL) ;
	strcpy(currboard,fptr->filename) ;
	apply_record(strcat(bfile(currboard),DIR),duentposts,sizeof(struct fileheader)) ;
}

d_user()
{
	int id ;

	u_namelist() ;
	move(0,0) ;
	standout() ;
	prints("DELETE USER DELETE USER DELETE USER") ;
	standend() ;
	clrtoeol() ;
	move(1,0) ;
	namecomplete("Enter userid to be deleted: ",genbuf) ;
	if(*genbuf == '\0') {
		clear() ;
		return 0 ;
	}
	if(!(id = getuser(genbuf))) {
		move(3,0) ;
		prints("Invalid User Id") ;
		clrtoeol() ;
		pressreturn() ;
		clear() ;
		return 0 ;
	}
	delusernum = id ;
	move(1,0) ;
	prints("Delete User '%s'.",genbuf) ;
	getdata(2,0,"(Yes, or No): ",genbuf,4,DOECHO,NULL) ;
	if(strcmp(genbuf,"Yes") && strcmp(genbuf,"yes")) { /* if not yes quit */
		move(2,0) ;
		prints("Aborting delete User\n") ;
		pressreturn() ;
		clear() ;
		return 0 ;
	}
	sprintf(genbuf,"rm -fr mail/%s",lookupuser.userid) ;
	do_exec(genbuf,NULL) ;
	lookupuser.userid[0] = '\0' ;
	substitute_record(PASSFILE,&lookupuser,sizeof(lookupuser),id) ;
	apply_boards(duentbds) ;
    clear_user_zaps(delusernum) ;
	move(2,0) ;
	prints("User Deleted\n") ;
	pressreturn() ;
	clear() ;
	return 0 ;
}

