/*
    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"

time_t cut_time ;

do_user_check(uptr)
struct user *uptr ;
{
	if(*(uptr->userid) == '\0')
	  return ;
	if(cut_time > uptr->lastlogin && uptr->userlevel == 0)
	  AddNameList(uptr->userid) ;
}

d_u_list(s)
char *s ;
{
	int id ;
	extern int delusernum ;
	extern int duentbds() ;

	if(*s == '*')
	  return ;
	if(!(id = getuser(s)))
	  return ;
	delusernum = id ;
	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) ;
	prints(".") ;
	refresh() ;
}

m_uclean()
{
    int daysold ;
    char buf[STRLEN], *killsp() ;
    
    getdata(1,0,"Enter Inactive Age (30 days default):",buf,5,DOECHO,NULL) ;
    if(isdigit(*killsp(buf)))
      daysold = atoi(killsp(buf)) ;
    else
      daysold = 30 ;
	cut_time = time(NULL) ;
	cut_time -= (daysold * 24 * 60 * 60) ;
	CreateNameList() ;
	AddNameList("*") ;
	if(apply_record(PASSFILE, do_user_check, sizeof(struct user))==-1)
	  return 0 ;
	move(2,0) ;
	namecomplete("Delete Inactive User (* is All): ",genbuf) ;
	if(*genbuf == '*') {
		move(2,0) ;
		prints("Delete ALL inactive Users?  ") ;
        getdata(3,0,"(Yes, or No): ",genbuf,4,DOECHO,NULL) ;
		if(*genbuf != 'y' && *genbuf != 'Y') {
			move(2,0) ;
			prints("Delete Inactive Users Aborted\n") ;
			pressreturn() ;
			clear() ;
			return 0 ;
		}
		move(3,0) ;
        fastdelete() ;
    } else if(*genbuf != '\0') {
		char tbuf[10] ;

		move(2,0) ;
		prints("Delete inactive user '%s'?  ",genbuf) ;
		Getyn(tbuf) ;
		if(tbuf[0] != 'y' && tbuf[0] != 'Y') {
			move(2,0) ;
			prints("Delete Inactive Users Aborted\n") ;
			pressreturn() ;
			clear() ;
			return 0 ;
		}
		d_u_list(genbuf) ;
	}
	clear() ;
	return 0 ;
}
		

m_info()
{
    int id ;
    struct user changes ;
    struct user initial ;
    extern char *genpasswd() ;
    u_namelist() ;
    clear() ;
    standout() ;
    prints("Get and Change User Info") ;
    standend() ;
    move(1,0) ;
    namecomplete("Enter userid to be accessed: ", genbuf) ;
    if(*genbuf == '\0') {
        clear() ;
        return 0 ;
    }
    if(!(id = getuser(genbuf))) {
        move(3,0) ;
        prints("Invalid User Id") ;
        clrtoeol() ;
        pressreturn() ;
        clear() ;
        return 0 ;
    }
    bcopy(&lookupuser,&changes, sizeof(changes)) ;
    bcopy(&lookupuser,&initial, sizeof(initial)) ;
    user_display(&initial) ;
    getdata(10,0,"Change User Info?  (Yes or No): ",genbuf,4,DOECHO,NULL) ;
    if(*genbuf == 'y' || *genbuf == 'Y') {
        move(12,0) ;
        prints("Press <Return> to ignore field\n") ;
      enteruid:
        getdata(13,0,"User ID:    ",genbuf,20,DOECHO,NULL) ;
        if(*genbuf != '\0') {
            if(getuser(genbuf)) {
                move(3,0) ;
                prints("Error!  User id already exists\n") ;
                bell() ;
                move(13,0) ;
                clrtobot() ;
                goto enteruid ;
            }
            strncpy(changes.userid, genbuf,STRLEN-6) ;
        }
      dopasswd:
        getdata(14,0,"Passwd:     ",genbuf,20,NOECHO,NULL) ;
        if(genbuf[0] != '\0') {
            char temp[20] ;
            getdata(15,0,"ReEnter Passwd: ",temp,20,NOECHO,NULL) ;
            if(strcmp(temp,genbuf)) {
                move(3,0) ;
                prints("Error!  Passwds don't match\n") ;
                move(14,0) ;
                clrtobot() ;
                goto dopasswd ;
            }
            strncpy(changes.passwd,genpasswd(genbuf),PASSLEN) ;
        }
        getdata(15,0,"User Name:  ",genbuf,STRLEN,DOECHO,NULL) ;
        if(*genbuf != '\0')
          strncpy(changes.username, genbuf, STRLEN) ;
        getdata(16,0,"Term Type:  ",genbuf,STRLEN,DOECHO,NULL) ;
        if(*genbuf != '\0')
          strncpy(changes.termtype, genbuf, STRLEN) ;
        getdata(17,0,"User Level: ",genbuf,5,DOECHO,NULL) ;
        if(*genbuf != '\0')
          changes.userlevel = atoi(genbuf) ;
        getdata(18,0,"Apply these changes?  (Yes or No): ",
                genbuf,4,DOECHO,NULL) ;
        if(*genbuf == 'y' || *genbuf == 'Y') {
            if(strcmp(changes.userid, initial.userid)) {
                char from[512], to[512] ;
                
                sprintf(from,"mail/%s",initial.userid) ;
                sprintf(to,"mail/%s",changes.userid) ;
                rename(from,to) ;
            }
            substitute_record(PASSFILE, &changes, sizeof(changes), id) ;
        }
    }
    return 0 ;
}

user_display(u)
struct user *u ;
{
    move(4,0) ;
    prints("User Id:      %s\n", u->userid) ;
    prints("User Name:    %s\n", u->username) ;
    prints("Term Type:    %s\n", u->termtype) ;
    prints("Level:        %d\n", u->userlevel) ;
}

auto_clean(oldness)
int oldness ;
{
    if(oldness <= 4)
      oldness = 4 ;
    cut_time = time(NULL) ;
    cut_time -= (oldness * 24 * 60 * 60) ;
    CreateNameList() ;
    if(apply_record(PASSFILE,do_user_check,sizeof(struct user)) == -1)
      return 0 ;
    fastdelete() ;
}
