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

x_passwd()
{
	char *pass, passbuf[PASSLEN],*getpass() ;
    char *genpasswd() ;

	move(3,0) ;
	clrtobot() ;
	getdata(3,0,"Enter Passwd: ",passbuf,PASSLEN,NOECHO,NULL) ;
    if(passbuf[0] == '\0') {
        prints("\nBad Passwd\n") ;
        pressreturn() ;
        return 0;
    }
	strncpy(genbuf,passbuf,PASSLEN) ;
	getdata(4,0,"Renter Passwd: ",genbuf,PASSLEN,NOECHO,NULL) ;
	if(strncmp(passbuf,genbuf,PASSLEN)) {
		prints("Error entering passwd\n") ;
		return -1 ;
	}
    genbuf[8] = '\0' ;
    pass = genpasswd(genbuf) ;
	strncpy(currentuser.passwd,pass,PASSLEN) ;
    substitute_record(PASSFILE,&currentuser,sizeof(currentuser),usernum) ;
	clear() ;
	return 0 ;
}

x_name()
{
	getdata(1,0,"Enter your name: ",genbuf,USERNAMESZ,DOECHO,NULL) ;
	strncpy(currentuser.username,genbuf,USERNAMESZ) ;
    bbssetenv("USERNAME",currentuser.username) ;
	substitute_record(PASSFILE,&currentuser,sizeof(currentuser),usernum) ;
	clear() ;
	return 0 ;
}


x_csh()
{
    clear() ;
    refresh() ;
    do_exec("csh",NULL) ;
	clear() ;
	return 0 ;
}


x_terminal()
{
	getdata(1,0,"Enter new terminal type: ",genbuf,TERMTYPESZ,DOECHO,NULL) ;
	if(genbuf[0] == '\0')
	  return 0 ;
    if(!term_init(genbuf)) {
        move(2,0) ;
        prints("Invalid terminal type.\n") ;
        return -1 ;
    }
	strncpy(currentuser.termtype,genbuf,TERMTYPESZ) ;
	substitute_record(PASSFILE,&currentuser,sizeof(currentuser),usernum) ;
    bbssetenv("TERM",currentuser.termtype) ;
    initscr() ;
	clear() ;
    pressreturn() ;
	return 0 ;
}

x_level()
{
   	int id ;

	u_namelist() ;
	move(0,0) ;
	prints("Change User Priority\n") ;
	clrtoeol() ;
	move(1,0) ;
	namecomplete("Enter userid to be changed: ",genbuf) ;
	if(genbuf[0] == '\0') {
		clear() ;
		return 0 ;
	}
	if(!(id = getuser(genbuf))) {
		move(3,0) ;
		prints("Invalid User Id") ;
		clrtoeol() ;
		pressreturn() ;
		clear() ;
		return 0 ;
	}
	getdata(1,0,"Enter priority: ",genbuf,5,DOECHO,NULL) ;
	lookupuser.userlevel = atoi(genbuf) ;
	substitute_record(PASSFILE,&lookupuser,sizeof(struct user),id) ;
	move(2,0) ;
	prints("User '%s' level changed\n",lookupuser.userid) ;
	pressreturn() ;
	clear() ;
	return 0 ;
}

x_cloak()
{
	extern struct user_info uinfo ;
	extern int usernum ;

	uinfo.invisible = (uinfo.invisible)?NA:YEA ;
	substitute_record(ULIST,&uinfo,sizeof(uinfo), utmpent) ;
	move(3,0) ;
	prints("Cloak has been %s.\n",(uinfo.invisible)?"activated":"deactivated") ;
	return 0 ;
}

