/
umud/DOC/
umud/DOC/U/
umud/DOC/U/U-examples/
umud/DOC/internals/
umud/DOC/wizard/
umud/MISC/
umud/MISC/dbchk/
umud/RWHO/rwhod/
/*
	Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/

#ifndef	lint
static	char	RCSid[] = "$Header: /home/mjr/hacks/umud/RCS/login.c,v 1.2 92/05/17 23:32:43 mjr Exp $";
#endif

/* configure all options BEFORE including system stuff. */
#include	"config.h"


#include	"mud.h"
#include	"vars.h"
#include	"look.h"

/*
This routine builds a new player, assuming that the request was generated
by the system.
Code mostly stolen from CMD/build.c:buildplayer()
*/
static int
newplayer(name, pass, nuf)
char *name;
char *pass;
char nuf[MAXOID];
{
	char	hm[MAXOID];
	char	*limbo;

	if(ut_objnew("1",nuf))
		return(UERR_FATAL);
#ifdef	U_EXTENSIONS
	eval_cmd_returnstr(nuf);
#endif

	/* add the new player to limbo */
	if(ut_listadd("1","0",var_ply,nuf))
		return(UERR_FATAL);

	/* set owner - player owns itself ! */
	if(ut_set("1",nuf,typ_list,var_owner,nuf))
		return(UERR_FATAL);

	/* set password */
	if(pass == (char *)0) {
		/* set an empty password */
		if(ut_set("1",nuf,typ_str,var_pass,""))
			return(UERR_FATAL);
	} else {
		if(ut_setpass(nuf,pass))
			return(UERR_FATAL);
	}

	/* make it a player */
	if(ut_set("1",nuf,typ_flag,var_isplay,""))
		return(UERR_FATAL);

	/* set location of the new player (in limbo) */
	if(ut_set("1",nuf,typ_obj,var_loc,"0"))
		return(UERR_FATAL);

	/* set home for the player (limbo) */
	limbo = ut_getatt(system_object,0,typ_obj,var_syslimbo,(char *)0);
	if(limbo == (char *)0) {
		plogf("build player %s: no limbo; home is room %s\n",
		      nuf,"0");
		limbo = "0";
	}
	(void)strcpy(hm,limbo);
	ut_delocaliz(hm);
	if(ut_set("1",nuf,typ_obj,var_home,hm))
		return(UERR_FATAL);

	/* set name if one given */
	if(name != (char *)0 && ut_set("1",nuf,typ_str,var_nam,name))
		return(UERR_FATAL);

	plogf("SYSCREATE:  %s\n", nuf);
	return(UERR_NONE);
}

/*
newlogin creates and logs in a player.
*/
int
newlogin(line, whobuf)
char *line;
char *whobuf;
{
  int	ac;
  char	*av[25];
  char	ab[MUDBUF];

  ac = run_tokenize(line,av,25,ab,sizeof(ab));

  if(ac < 2 || ac > 3 || strlen(av[1]) > MAXOID - 2)
    return(0);

  /* Create the new character in limbo */
  if (newplayer(av[1], av[2], whobuf) != UERR_NONE)
    return(0);

  /* Say we created someone */
  return(2);
}

/*
login is expected to do something intelligent with the line of
user information passed to it. the first word is assumed to be "connect"
or something like that. the second is the object-id, the third is
maybe a password.
*/
login(lp,whobuf)
char	*lp;
char	*whobuf;
{
	char	*opw;
	int	ac;
	char	*av[25];
	char	ab[MUDBUF];
	char	pbuf[MAXOID];

	ac = run_tokenize(lp,av,25,ab,sizeof(ab));

	if(ac == 1 && !strcmp(av[0],"QUIT"))
		return(-1);

	if(ac < 2 || ac > 3 || strlen(av[1]) > MAXOID - 2)
		return(0);

	(void)strcpy(whobuf,av[1]);

	if((opw = ut_getatt(av[1],0,typ_str,var_pass,(char *)0)) == (char *)0)
		return(0);

	if(*opw == '\0')
		return(1);

	/* no password given */
	if(ac != 3)
		return(0);

	/* password encryption is seeded with the object # as key */
	rot_init(av[1]);

	/* then the password plaintext is encrypted with it. */
	rot_decode(opw,pbuf);

	/* so it should match the plaintext password given us by the player */
	if(strcmp(av[2],pbuf))
		return(0);
	return(1);
}



void
welcome(who)
char	*who;
{
	char	*w;
	char	*linkmsg;

	say(who,"Welcome to UnterMUD ",version," (",mud_getname(),")\n",(char *)0);
	(void)say_file(who,"NEWS/welcome.txt");

	w = ut_loc(who);
	if(!strcmp(w,"nowhere") && (linkmsg = ut_getatt(who,0,typ_str,var_linkmsg,(char *)0)) != (char *)0) {
		say(who,"You shouldn't be here. Go away.\n",(char *)0);
		say(who,linkmsg,"\n",(char *)0);
	} else {
		run(who,who,"look",0,(char **)0,1);
		if(!ut_flagged(who,var_isdark))
		  ut_roombcast(w,who,ut_name(who)," has arrived.\n",(char *)0);
	}
}




/* WARNING!!! the calls to goodbye() actually may make more
Iob writes as a result of a player hangup WHILE RUNNING INSIDE io_sync()!
Do NOT do much in goodbye!() or madness may result.
*/
void
goodbye(w)
char	*w;
{
	if(!ut_flagged(w,var_isdark))
	  ut_roombcast(ut_loc(w),w,ut_name(w)," has disconnected.\n",(char *)0);
}