/* js_user.c */

/* Synchronet JavaScript "User" Object */

/* $Id: js_user.c,v 1.80 2010/03/01 07:02:58 rswindell Exp $ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
#include "js_request.h"

#ifdef JAVASCRIPT

static scfg_t* scfg=NULL;

static const char* getprivate_failure = "line %d %s JS_GetPrivate failed";

typedef struct
{
	user_t*		user;
	user_t		storage;
	BOOL		cached;
	scfg_t*		cfg;
	client_t*	client;

} private_t;

/* User Object Properites */
enum {
	 USER_PROP_NUMBER
	,USER_PROP_ALIAS 	
	,USER_PROP_NAME		
	,USER_PROP_HANDLE	
	,USER_PROP_NOTE		
	,USER_PROP_COMP		
	,USER_PROP_COMMENT	
	,USER_PROP_NETMAIL	
	,USER_PROP_EMAIL	/* READ ONLY */
	,USER_PROP_ADDRESS	
	,USER_PROP_LOCATION	
	,USER_PROP_ZIPCODE
	,USER_PROP_PASS
	,USER_PROP_PHONE  	
	,USER_PROP_BIRTH  
	,USER_PROP_AGE		/* READ ONLY */
	,USER_PROP_MODEM     
	,USER_PROP_LASTON	
	,USER_PROP_FIRSTON	
	,USER_PROP_EXPIRE    
	,USER_PROP_PWMOD     
	,USER_PROP_LOGONS    
	,USER_PROP_LTODAY    
	,USER_PROP_TIMEON    
	,USER_PROP_TEXTRA  	
	,USER_PROP_TTODAY    
	,USER_PROP_TLAST     
	,USER_PROP_POSTS     
	,USER_PROP_EMAILS    
	,USER_PROP_FBACKS    
	,USER_PROP_ETODAY	
	,USER_PROP_PTODAY
	,USER_PROP_MAIL_WAITING
	,USER_PROP_MAIL_PENDING
	,USER_PROP_ULB       
	,USER_PROP_ULS       
	,USER_PROP_DLB       
	,USER_PROP_DLS       
	,USER_PROP_CDT		
	,USER_PROP_MIN		
	,USER_PROP_LEVEL 	
	,USER_PROP_FLAGS1	
	,USER_PROP_FLAGS2	
	,USER_PROP_FLAGS3	
	,USER_PROP_FLAGS4	
	,USER_PROP_EXEMPT	
	,USER_PROP_REST		
	,USER_PROP_ROWS		
	,USER_PROP_SEX		
	,USER_PROP_MISC		
	,USER_PROP_LEECH 	
	,USER_PROP_CURSUB	
	,USER_PROP_CURDIR	
	,USER_PROP_CURXTRN
	,USER_PROP_FREECDT	
	,USER_PROP_XEDIT 	
	,USER_PROP_SHELL 	
	,USER_PROP_QWK		
	,USER_PROP_TMPEXT	
	,USER_PROP_CHAT		
	,USER_PROP_NS_TIME	
	,USER_PROP_PROT		
	,USER_PROP_LOGONTIME
	,USER_PROP_TIMEPERCALL
	,USER_PROP_TIMEPERDAY
	,USER_PROP_CALLSPERDAY
	,USER_PROP_LINESPERMSG
	,USER_PROP_EMAILPERDAY
	,USER_PROP_POSTSPERDAY
	,USER_PROP_FREECDTPERDAY
	,USER_PROP_CACHED
	,USER_PROP_IS_SYSOP
};

static void js_getuserdat(private_t* p)
{
	if(!p->cached) {
		if(getuserdat(p->cfg,p->user)==0)
			p->cached=TRUE;
	}
}


static JSBool js_user_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
	char*		s=NULL;
	char		tmp[128];
	ulong		val=0;
    jsint       tiny;
	JSString*	js_str;
	private_t*	p;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_TRUE);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

    tiny = JSVAL_TO_INT(id);

	switch(tiny) {
		case USER_PROP_NUMBER:
			val=p->user->number;
			break;
		case USER_PROP_ALIAS: 
			s=p->user->alias;
			break;
		case USER_PROP_NAME:
			s=p->user->name;
			break;
		case USER_PROP_HANDLE:
			s=p->user->handle;
			break;
		case USER_PROP_NOTE:
			s=p->user->note;
			break;
		case USER_PROP_COMP:
			s=p->user->comp;
			break;
		case USER_PROP_COMMENT:
			s=p->user->comment;
			break;
		case USER_PROP_NETMAIL:
			s=p->user->netmail;
			break;
		case USER_PROP_EMAIL:
			s=usermailaddr(p->cfg, tmp
				,p->cfg->inetmail_misc&NMAIL_ALIAS ? p->user->alias : p->user->name);
			break;
		case USER_PROP_ADDRESS:
			s=p->user->address;
			break;
		case USER_PROP_LOCATION:
			s=p->user->location;
			break;
		case USER_PROP_ZIPCODE:
			s=p->user->zipcode;
			break;
		case USER_PROP_PASS:
			s=p->user->pass;
			break;
		case USER_PROP_PHONE:
			s=p->user->phone;
			break;
		case USER_PROP_BIRTH:
			s=p->user->birth;
			break;
		case USER_PROP_AGE:
			val=getage(p->cfg,p->user->birth);
			break;
		case USER_PROP_MODEM:
			s=p->user->modem;
			break;
		case USER_PROP_LASTON:
			val=p->user->laston;
			break;
		case USER_PROP_FIRSTON:
			val=p->user->firston;
			break;
		case USER_PROP_EXPIRE:
			val=p->user->expire;
			break;
		case USER_PROP_PWMOD: 
			val=p->user->pwmod;
			break;
		case USER_PROP_LOGONS:
			val=p->user->logons;
			break;
		case USER_PROP_LTODAY:
			val=p->user->ltoday;
			break;
		case USER_PROP_TIMEON:
			val=p->user->timeon;
			break;
		case USER_PROP_TEXTRA:
			val=p->user->textra;
			break;
		case USER_PROP_TTODAY:
			val=p->user->ttoday;
			break;
		case USER_PROP_TLAST: 
			val=p->user->tlast;
			break;
		case USER_PROP_POSTS: 
			val=p->user->posts;
			break;
		case USER_PROP_EMAILS: 
			val=p->user->emails;
			break;
		case USER_PROP_FBACKS: 
			val=p->user->fbacks;
			break;
		case USER_PROP_ETODAY:	
			val=p->user->etoday;
			break;
		case USER_PROP_PTODAY:
			val=p->user->ptoday;
			break;
		case USER_PROP_ULB:
			val=p->user->ulb;
			break;
		case USER_PROP_ULS:
			val=p->user->uls;
			break;
		case USER_PROP_DLB:
			val=p->user->dlb;
			break;
		case USER_PROP_DLS:
			val=p->user->dls;
			break;
		case USER_PROP_CDT:
			val=p->user->cdt;
			break;
		case USER_PROP_MIN:
			val=p->user->min;
			break;
		case USER_PROP_LEVEL:
			val=p->user->level;
			break;
		case USER_PROP_FLAGS1:
			val=p->user->flags1;
			break;
		case USER_PROP_FLAGS2:
			val=p->user->flags2;
			break;
		case USER_PROP_FLAGS3:
			val=p->user->flags3;
			break;
		case USER_PROP_FLAGS4:
			val=p->user->flags4;
			break;
		case USER_PROP_EXEMPT:
			val=p->user->exempt;
			break;
		case USER_PROP_REST:
			val=p->user->rest;
			break;
		case USER_PROP_ROWS:
			val=p->user->rows;
			break;
		case USER_PROP_SEX:
			sprintf(tmp,"%c",p->user->sex);
			s=tmp;
			break;
		case USER_PROP_MISC:
			val=p->user->misc;
			break;
		case USER_PROP_LEECH:
			val=p->user->leech;
			break;
		case USER_PROP_CURSUB:
			s=p->user->cursub;
			break;
		case USER_PROP_CURDIR:
			s=p->user->curdir;
			break;
		case USER_PROP_CURXTRN:
			s=p->user->curxtrn;
			break;

		case USER_PROP_FREECDT:
			val=p->user->freecdt;
			break;
		case USER_PROP_XEDIT:
			if(p->user->xedit>0 && p->user->xedit<=p->cfg->total_xedits)
				s=p->cfg->xedit[p->user->xedit-1]->code;
			else
				s=""; /* internal editor */
			break;
		case USER_PROP_SHELL:
			s=p->cfg->shell[p->user->shell]->code;
			break;
		case USER_PROP_QWK:
			val=p->user->qwk;
			break;
		case USER_PROP_TMPEXT:
			s=p->user->tmpext;
			break;
		case USER_PROP_CHAT:
			val=p->user->chat;
			break;
		case USER_PROP_NS_TIME:
			val=p->user->ns_time;
			break;
		case USER_PROP_PROT:
			sprintf(tmp,"%c",p->user->prot);
			s=tmp;
			break;
		case USER_PROP_LOGONTIME:
			val=p->user->logontime;
			break;
		case USER_PROP_TIMEPERCALL:
			val=p->cfg->level_timepercall[p->user->level];
			break;
		case USER_PROP_TIMEPERDAY:
			val=p->cfg->level_timeperday[p->user->level];
			break;
		case USER_PROP_CALLSPERDAY:
			val=p->cfg->level_callsperday[p->user->level];
			break;
		case USER_PROP_LINESPERMSG:
			val=p->cfg->level_linespermsg[p->user->level];
			break;
		case USER_PROP_POSTSPERDAY:
			val=p->cfg->level_postsperday[p->user->level];
			break;
		case USER_PROP_EMAILPERDAY:
			val=p->cfg->level_emailperday[p->user->level];
			break;
		case USER_PROP_FREECDTPERDAY:
			val=p->cfg->level_freecdtperday[p->user->level];
			break;
		case USER_PROP_MAIL_WAITING:
			val=getmail(p->cfg,p->user->number,/* sent? */FALSE);
			break;
		case USER_PROP_MAIL_PENDING:
			val=getmail(p->cfg,p->user->number,/* sent? */TRUE);
			break;

		case USER_PROP_CACHED:
			*vp = BOOLEAN_TO_JSVAL(p->cached);
			JS_RESUMEREQUEST(cx, rc);
			return(JS_TRUE);	/* intentional early return */

		case USER_PROP_IS_SYSOP:
			*vp = BOOLEAN_TO_JSVAL(p->user->level >= SYSOP_LEVEL);
			JS_RESUMEREQUEST(cx, rc);
			return(JS_TRUE);	/* intentional early return */

		default:	
			/* This must not set vp in order for child objects to work (stats and security) */
			JS_RESUMEREQUEST(cx, rc);
			return(JS_TRUE);
	}
	JS_RESUMEREQUEST(cx, rc);
	if(s!=NULL) {
		if((js_str=JS_NewStringCopyZ(cx, s))==NULL)
			return(JS_FALSE);
		*vp = STRING_TO_JSVAL(js_str);
	} else
		JS_NewNumberValue(cx,val,vp);

	return(JS_TRUE);
}

static JSBool js_user_set(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
	char*		str;
	char		tmp[64];
	jsint		val;
	ulong		usermisc;
    jsint       tiny;
	JSString*	js_str;
	private_t*	p;
	int32		usernumber;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_TRUE);

	if((js_str=JS_ValueToString(cx,*vp))==NULL)
		return(JS_FALSE);

	if((str=JS_GetStringBytes(js_str))==NULL)
		return(JS_FALSE);

    tiny = JSVAL_TO_INT(id);

	rc=JS_SUSPENDREQUEST(cx);
	switch(tiny) {
		case USER_PROP_NUMBER:
			JS_RESUMEREQUEST(cx, rc);
			JS_ValueToInt32(cx, *vp, &usernumber);
			rc=JS_SUSPENDREQUEST(cx);
			if(usernumber!=p->user->number) {
				p->user->number=(ushort)usernumber;
				p->cached=FALSE;
			}
			break;
		case USER_PROP_ALIAS:
			SAFECOPY(p->user->alias,str);
			/* update USER.DAT */
			putuserrec(p->cfg,p->user->number,U_ALIAS,LEN_ALIAS,str);

			/* update NAME.DAT */
			getuserrec(p->cfg,p->user->number,U_MISC,8,tmp);
			usermisc=ahtoul(tmp);
			if(!(usermisc&DELETED))
				putusername(p->cfg,p->user->number,str);
			break;
		case USER_PROP_NAME:
			SAFECOPY(p->user->name,str);
			putuserrec(p->cfg,p->user->number,U_NAME,LEN_NAME,str);
			break;
		case USER_PROP_HANDLE:
			SAFECOPY(p->user->handle,str);
			putuserrec(p->cfg,p->user->number,U_HANDLE,LEN_HANDLE,str);
			break;
		case USER_PROP_NOTE:		 
			SAFECOPY(p->user->note,str);
			putuserrec(p->cfg,p->user->number,U_NOTE,LEN_NOTE,str);
			break;
		case USER_PROP_COMP:
			SAFECOPY(p->user->comp,str);
			putuserrec(p->cfg,p->user->number,U_COMP,LEN_COMP,str);
			break;
		case USER_PROP_COMMENT:	 
			SAFECOPY(p->user->comment,str);
			putuserrec(p->cfg,p->user->number,U_COMMENT,LEN_COMMENT,str);
			break;
		case USER_PROP_NETMAIL:	 
			SAFECOPY(p->user->netmail,str);
			putuserrec(p->cfg,p->user->number,U_NETMAIL,LEN_NETMAIL,str);
			break;
		case USER_PROP_ADDRESS:	 
			SAFECOPY(p->user->address,str);
			putuserrec(p->cfg,p->user->number,U_ADDRESS,LEN_ADDRESS,str);
			break;
		case USER_PROP_LOCATION:	 
			SAFECOPY(p->user->location,str);
			putuserrec(p->cfg,p->user->number,U_LOCATION,LEN_LOCATION,str);
			break;
		case USER_PROP_ZIPCODE:	 
			SAFECOPY(p->user->zipcode,str);
			putuserrec(p->cfg,p->user->number,U_ZIPCODE,LEN_ZIPCODE,str);
			break;
		case USER_PROP_PHONE:  	 
			SAFECOPY(p->user->phone,str);
			putuserrec(p->cfg,p->user->number,U_PHONE,LEN_PHONE,str);
			break;
		case USER_PROP_BIRTH:  	 
			SAFECOPY(p->user->birth,str);
			putuserrec(p->cfg,p->user->number,U_BIRTH,LEN_BIRTH,str);
			break;
		case USER_PROP_MODEM:     
			SAFECOPY(p->user->modem,str);
			putuserrec(p->cfg,p->user->number,U_MODEM,LEN_MODEM,str);
			break;
		case USER_PROP_ROWS:	
			p->user->rows=atoi(str);
			putuserrec(p->cfg,p->user->number,U_ROWS,0,str);	/* base 10 */
			break;
		case USER_PROP_SEX:		 
			p->user->sex=toupper(str[0]);
			putuserrec(p->cfg,p->user->number,U_SEX,0,strupr(str));	/* single char */
			break;
		case USER_PROP_CURSUB:	 
			SAFECOPY(p->user->cursub,str);
			putuserrec(p->cfg,p->user->number,U_CURSUB,0,str);
			break;
		case USER_PROP_CURDIR:	 
			SAFECOPY(p->user->curdir,str);
			putuserrec(p->cfg,p->user->number,U_CURDIR,0,str);
			break;
		case USER_PROP_CURXTRN:	 
			SAFECOPY(p->user->curxtrn,str);
			putuserrec(p->cfg,p->user->number,U_CURXTRN,0,str);
			break;
		case USER_PROP_XEDIT: 	 
			putuserrec(p->cfg,p->user->number,U_XEDIT,0,str);
			break;
		case USER_PROP_SHELL: 	 
			putuserrec(p->cfg,p->user->number,U_SHELL,0,str);
			break;
		case USER_PROP_MISC:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_MISC,0,ultoa(p->user->misc=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_QWK:		 
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_QWK,0,ultoa(p->user->qwk=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_CHAT:		 
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_CHAT,0,ultoa(p->user->chat=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_TMPEXT:	 
			SAFECOPY(p->user->tmpext,str);
			putuserrec(p->cfg,p->user->number,U_TMPEXT,0,str);
			break;
		case USER_PROP_NS_TIME:	 
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_NS_TIME,0,ultoa(p->user->ns_time=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_PROT:	
			p->user->prot=toupper(str[0]);
			putuserrec(p->cfg,p->user->number,U_PROT,0,strupr(str)); /* single char */
			break;
		case USER_PROP_LOGONTIME:	 
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_LOGONTIME,0,ultoa(p->user->logontime=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
			
		/* security properties*/
		case USER_PROP_PASS:	
			SAFECOPY(p->user->pass,str);
			putuserrec(p->cfg,p->user->number,U_PASS,LEN_PASS,strupr(str));
			break;
		case USER_PROP_PWMOD:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_PWMOD,0,ultoa(p->user->pwmod=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_LEVEL: 
			p->user->level=atoi(str);
			putuserrec(p->cfg,p->user->number,U_LEVEL,0,str);
			break;
		case USER_PROP_FLAGS1:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_FLAGS1,0,ultoa(p->user->flags1=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_FLAGS2:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_FLAGS2,0,ultoa(p->user->flags2=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_FLAGS3:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_FLAGS3,0,ultoa(p->user->flags3=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_FLAGS4:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_FLAGS4,0,ultoa(p->user->flags4=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_EXEMPT:
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_EXEMPT,0,ultoa(p->user->exempt=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_REST:	
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_REST,0,ultoa(p->user->rest=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;
		case USER_PROP_CDT:	
			p->user->cdt=strtoul(str,NULL,0);
			putuserrec(p->cfg,p->user->number,U_CDT,0,str);
			break;
		case USER_PROP_FREECDT:
			p->user->freecdt=strtoul(str,NULL,0);
			putuserrec(p->cfg,p->user->number,U_FREECDT,0,str);
			break;
		case USER_PROP_MIN:	
			p->user->min=strtoul(str,NULL,0);
			putuserrec(p->cfg,p->user->number,U_MIN,0,str);
			break;
		case USER_PROP_TEXTRA:  
			p->user->textra=(ushort)strtoul(str,NULL,0);
			putuserrec(p->cfg,p->user->number,U_TEXTRA,0,str);
			break;
		case USER_PROP_EXPIRE:  
			JS_RESUMEREQUEST(cx, rc);
			if(JS_ValueToInt32(cx,*vp,&val))
				putuserrec(p->cfg,p->user->number,U_EXPIRE,0,ultoa(p->user->expire=val,tmp,16));
			rc=JS_SUSPENDREQUEST(cx);
			break;

		case USER_PROP_CACHED:
			JS_ValueToBoolean(cx, *vp, &p->cached);
			JS_RESUMEREQUEST(cx, rc);
			return(JS_TRUE);	/* intentional early return */

	}
	if(!(p->user->rest&FLAG('G')))
		p->cached=FALSE;

	JS_RESUMEREQUEST(cx, rc);
	return(JS_TRUE);
}

#define USER_PROP_FLAGS JSPROP_ENUMERATE

static jsSyncPropertySpec js_user_properties[] = {
/*		 name				,tinyid					,flags,					ver	*/

	{	"number"			,USER_PROP_NUMBER		,USER_PROP_FLAGS,		310},
	{	"alias"				,USER_PROP_ALIAS 		,USER_PROP_FLAGS,		310},
	{	"name"				,USER_PROP_NAME		 	,USER_PROP_FLAGS,		310},
	{	"handle"			,USER_PROP_HANDLE	 	,USER_PROP_FLAGS,		310},
	{	"ip_address"		,USER_PROP_NOTE		 	,USER_PROP_FLAGS,		310},
	{	"note"				,USER_PROP_NOTE		 	,USER_PROP_FLAGS,		310},
	{	"host_name"			,USER_PROP_COMP		 	,USER_PROP_FLAGS,		310},
	{	"computer"			,USER_PROP_COMP		 	,USER_PROP_FLAGS,		310},
	{	"comment"			,USER_PROP_COMMENT	 	,USER_PROP_FLAGS,		310},
	{	"netmail"			,USER_PROP_NETMAIL	 	,USER_PROP_FLAGS,		310},
	{	"email"				,USER_PROP_EMAIL	 	,USER_PROP_FLAGS|JSPROP_READONLY,		310},
	{	"address"			,USER_PROP_ADDRESS	 	,USER_PROP_FLAGS,		310},
	{	"location"			,USER_PROP_LOCATION	 	,USER_PROP_FLAGS,		310},
	{	"zipcode"			,USER_PROP_ZIPCODE	 	,USER_PROP_FLAGS,		310},
	{	"phone"				,USER_PROP_PHONE  	 	,USER_PROP_FLAGS,		310},
	{	"birthdate"			,USER_PROP_BIRTH  	 	,USER_PROP_FLAGS,		310},
	{	"age"				,USER_PROP_AGE			,USER_PROP_FLAGS|JSPROP_READONLY,		310},
	{	"connection"		,USER_PROP_MODEM      	,USER_PROP_FLAGS,		310},
	{	"modem"				,USER_PROP_MODEM      	,USER_PROP_FLAGS,		310},
	{	"screen_rows"		,USER_PROP_ROWS		 	,USER_PROP_FLAGS,		310},
	{	"gender"			,USER_PROP_SEX		 	,USER_PROP_FLAGS,		310},
	{	"cursub"			,USER_PROP_CURSUB	 	,USER_PROP_FLAGS,		310},
	{	"curdir"			,USER_PROP_CURDIR	 	,USER_PROP_FLAGS,		310},
	{	"curxtrn"			,USER_PROP_CURXTRN	 	,USER_PROP_FLAGS,		310},
	{	"editor"			,USER_PROP_XEDIT 	 	,USER_PROP_FLAGS,		310},
	{	"command_shell"		,USER_PROP_SHELL 	 	,USER_PROP_FLAGS,		310},
	{	"settings"			,USER_PROP_MISC		 	,USER_PROP_FLAGS,		310},
	{	"qwk_settings"		,USER_PROP_QWK		 	,USER_PROP_FLAGS,		310},
	{	"chat_settings"		,USER_PROP_CHAT		 	,USER_PROP_FLAGS,		310},
	{	"temp_file_ext"		,USER_PROP_TMPEXT	 	,USER_PROP_FLAGS,		310},
	{	"new_file_time"		,USER_PROP_NS_TIME	 	,USER_PROP_FLAGS,		311},
	{	"newscan_date"		,USER_PROP_NS_TIME	 	,0, /* Alias */			310},
	{	"download_protocol"	,USER_PROP_PROT		 	,USER_PROP_FLAGS,		310},
	{	"logontime"			,USER_PROP_LOGONTIME 	,USER_PROP_FLAGS,		310},
	{	"cached"			,USER_PROP_CACHED		,USER_PROP_FLAGS,		314},
	{	"is_sysop"			,USER_PROP_IS_SYSOP		,JSPROP_ENUMERATE|JSPROP_READONLY,	315},
	{0}
};

#ifdef BUILD_JSDOCS
static char* user_prop_desc[] = {

	 "record number (1-based)"
	,"alias/name"
	,"real name"
	,"chat handle"
	,"IP address last logged on from"
	,"AKA ip_address"
	,"host name last logged on from"
	,"AKA host_name"
	,"sysop's comment"
	,"external e-mail address"
	,"local Internet e-mail address	- <small>READ ONLY</small>"
	,"street address"
	,"location (city, state)"
	,"zip/postal code"
	,"phone number"
	,"birth date"
	,"calculated age in years - <small>READ ONLY</small>"
	,"connection type"
	,"AKA connection"
	,"terminal rows (lines)"
	,"gender type"
	,"current/last message sub-board (internal code)"
	,"current/last file directory (internal code)"
	,"current/last external program (internal code) run"
	,"external message editor (internal code) or <i>blank</i> if none"
	,"command shell (internal code)"
	,"settings bitfield - see <tt>USER_*</tt> in <tt>sbbsdefs.js</tt> for bit definitions"
	,"QWK packet settings bitfield - see <tt>QWK_*</tt> in <tt>sbbsdefs.js</tt> for bit definitions"
	,"chat settings bitfield - see <tt>CHAT_*</tt> in <tt>sbbsdefs.js</tt> for bit definitions"
	,"temporary file type (extension)"
	,"new file scan date/time (time_t format)"
	,"file transfer protocol (command key)"
	,"logon time (time_t format)"
	,"record is currently cached in memory"
	,"user has a System Operator's security level"
	,NULL
};
#endif


/* user.security */
static jsSyncPropertySpec js_user_security_properties[] = {
/*		 name				,tinyid					,flags,				ver	*/

	{	"password"			,USER_PROP_PASS		 	,USER_PROP_FLAGS,	310 },
	{	"password_date"		,USER_PROP_PWMOD      	,USER_PROP_FLAGS,	310 },
	{	"level"				,USER_PROP_LEVEL 	 	,USER_PROP_FLAGS,	310 },
	{	"flags1"			,USER_PROP_FLAGS1	 	,USER_PROP_FLAGS,	310 },
	{	"flags2"			,USER_PROP_FLAGS2	 	,USER_PROP_FLAGS,	310 },
	{	"flags3"			,USER_PROP_FLAGS3	 	,USER_PROP_FLAGS,	310 },
	{	"flags4"			,USER_PROP_FLAGS4	 	,USER_PROP_FLAGS,	310 },
	{	"exemptions"		,USER_PROP_EXEMPT	 	,USER_PROP_FLAGS,	310 },
	{	"restrictions"		,USER_PROP_REST		 	,USER_PROP_FLAGS,	310 },
	{	"credits"			,USER_PROP_CDT		 	,USER_PROP_FLAGS,	310 },
	{	"free_credits"		,USER_PROP_FREECDT	 	,USER_PROP_FLAGS,	310 },
	{	"minutes"			,USER_PROP_MIN		 	,USER_PROP_FLAGS,	310 },
	{	"extra_time"		,USER_PROP_TEXTRA  	 	,USER_PROP_FLAGS,	310 },
	{	"expiration_date"	,USER_PROP_EXPIRE     	,USER_PROP_FLAGS,	310 },
	{0}
};

#ifdef BUILD_JSDOCS
static char* user_security_prop_desc[] = {

	 "password"
	,"date password last modified (time_t format)"
	,"security level (0-99)"
	,"flag set #1 (bitfield)"
	,"flag set #2 (bitfield)"
	,"flag set #3 (bitfield)"
	,"flag set #4 (bitfield)"
	,"exemption flags (bitfield)"
	,"restriction flags (bitfield)"
	,"credits"
	,"free credits (for today only)"
	,"extra minutes (time bank)"
	,"extra minutes (for today only)"
	,"expiration date/time (time_t format)"
	,NULL
};
#endif

#undef  USER_PROP_FLAGS
#define USER_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY

/* user.limits: These should be READ ONLY by nature */
static jsSyncPropertySpec js_user_limits_properties[] = {
/*		 name					,tinyid					,flags,				ver	*/

	{	"time_per_logon"		,USER_PROP_TIMEPERCALL	,USER_PROP_FLAGS,	311 },
	{	"time_per_day"			,USER_PROP_TIMEPERDAY	,USER_PROP_FLAGS,	311 },
	{	"logons_per_day"		,USER_PROP_CALLSPERDAY	,USER_PROP_FLAGS,	311 },
	{	"lines_per_message"		,USER_PROP_LINESPERMSG	,USER_PROP_FLAGS,	311 },
	{	"email_per_day"			,USER_PROP_EMAILPERDAY	,USER_PROP_FLAGS,	311 },
	{	"posts_per_day"			,USER_PROP_POSTSPERDAY	,USER_PROP_FLAGS,	311 },
	{	"free_credits_per_day"	,USER_PROP_FREECDTPERDAY,USER_PROP_FLAGS,	311 },
	{0}
};


#ifdef BUILD_JSDOCS
static char* user_limits_prop_desc[] = {

	 "time (in minutes) per logon"
	,"time (in minutes) per day"
	,"logons per day"
	,"lines per message (post or email)"
	,"email sent per day"
	,"messages posted per day"
	,"free credits given per day"
	,NULL
};
#endif


#undef  USER_PROP_FLAGS
#define USER_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY

/* user.stats: These should be READ ONLY by nature */
static jsSyncPropertySpec js_user_stats_properties[] = {
/*		 name				,tinyid					,flags,					ver	*/

	{	"laston_date"		,USER_PROP_LASTON	 	,USER_PROP_FLAGS,		310 },
	{	"firston_date"		,USER_PROP_FIRSTON	 	,USER_PROP_FLAGS,		310 },
	{	"total_logons"		,USER_PROP_LOGONS     	,USER_PROP_FLAGS,		310 },
	{	"logons_today"		,USER_PROP_LTODAY     	,USER_PROP_FLAGS,		310 },
	{	"total_timeon"		,USER_PROP_TIMEON     	,USER_PROP_FLAGS,		310 },
	{	"timeon_today"		,USER_PROP_TTODAY     	,USER_PROP_FLAGS,		310 },
	{	"timeon_last_logon"	,USER_PROP_TLAST      	,USER_PROP_FLAGS,		310 },
	{	"total_posts"		,USER_PROP_POSTS      	,USER_PROP_FLAGS,		310 },
	{	"total_emails"		,USER_PROP_EMAILS     	,USER_PROP_FLAGS,		310 },
	{	"total_feedbacks"	,USER_PROP_FBACKS     	,USER_PROP_FLAGS,		310 },
	{	"email_today"		,USER_PROP_ETODAY	 	,USER_PROP_FLAGS,		310 },
	{	"posts_today"		,USER_PROP_PTODAY	 	,USER_PROP_FLAGS,		310 },
	{	"bytes_uploaded"	,USER_PROP_ULB        	,USER_PROP_FLAGS,		310 },
	{	"files_uploaded"	,USER_PROP_ULS        	,USER_PROP_FLAGS,		310 },
	{	"bytes_downloaded"	,USER_PROP_DLB        	,USER_PROP_FLAGS,		310 },
	{	"files_downloaded"	,USER_PROP_DLS        	,USER_PROP_FLAGS,		310 },
	{	"leech_attempts"	,USER_PROP_LEECH 	 	,USER_PROP_FLAGS,		310 },
	{	"mail_waiting"		,USER_PROP_MAIL_WAITING	,USER_PROP_FLAGS,		312	},
	{	"mail_pending"		,USER_PROP_MAIL_PENDING	,USER_PROP_FLAGS,		312	},
	{0}
};

#ifdef BUILD_JSDOCS
static char* user_stats_prop_desc[] = {

	 "date of previous logon (time_t format)"
	,"date of first logon (time_t format)"
	,"total number of logons"
	,"total logons today"
	,"total time used (in minutes)"
	,"time used today"
	,"time used last session"
	,"total messages posted"
	,"total e-mails sent"
	,"total feedback messages sent"
	,"e-mail sent today"
	,"messages posted today"
	,"total bytes uploaded"
	,"total files uploaded"
	,"total bytes downloaded"
	,"total files downloaded"
	,"suspected leech downloads"
	,"number of e-mail messages currently waiting"
	,"number of e-mail messages sent, currently pending deletion"
	,NULL
};
#endif


static void js_user_finalize(JSContext *cx, JSObject *obj)
{
	private_t* p;

	p=(private_t*)JS_GetPrivate(cx,obj);

	if(p!=NULL && (scfg_t*)p!=scfg)
		free(p);

	p=NULL;
	JS_SetPrivate(cx,obj,p);
}

static JSBool
js_chk_ar(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	uchar*		ar;
	JSString*	js_str;
	private_t*	p;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if((js_str=JS_ValueToString(cx, argv[0]))==NULL)
		return JS_FALSE;

	rc=JS_SUSPENDREQUEST(cx);
	ar = arstr(NULL,JS_GetStringBytes(js_str),p->cfg);

	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(chk_ar(p->cfg,ar,p->user,p->client));

	if(ar!=NULL && ar!=nular)
		free(ar);
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_posted_msg(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	count=1;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &count);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(user_posted_msg(p->cfg, p->user, count));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_sent_email(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	count=1;
	BOOL	feedback=FALSE;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &count);
	if(argc>1)
		JS_ValueToBoolean(cx, argv[1], &feedback);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(user_sent_email(p->cfg, p->user, count, feedback));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_downloaded_file(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	files=1;
	int32	bytes=0;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &bytes);
	if(argc>1)
		JS_ValueToInt32(cx, argv[1], &files);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(user_downloaded(p->cfg, p->user, files, bytes));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_uploaded_file(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	files=1;
	int32	bytes=0;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &bytes);
	if(argc>1)
		JS_ValueToInt32(cx, argv[1], &files);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(user_uploaded(p->cfg, p->user, files, bytes));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_adjust_credits(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	count=0;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &count);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(user_adjust_credits(p->cfg, p->user, count));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_adjust_minutes(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	count=0;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &count);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = BOOLEAN_TO_JSVAL(user_adjust_minutes(p->cfg, p->user, count));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}

static JSBool
js_get_time_left(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t*	p;
	int32	start_time=0;
	jsrefcount	rc;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return JS_FALSE;

	if(argc)
		JS_ValueToInt32(cx, argv[0], &start_time);

	rc=JS_SUSPENDREQUEST(cx);
	js_getuserdat(p);

	*rval = INT_TO_JSVAL(gettimeleft(p->cfg, p->user, (time_t)start_time));
	JS_RESUMEREQUEST(cx, rc);

	return JS_TRUE;
}


static jsSyncMethodSpec js_user_functions[] = {
	{"compare_ars",		js_chk_ar,			1,	JSTYPE_BOOLEAN,	JSDOCSTR("string ars")
	,JSDOCSTR("Verify user meets access requirements string")
	,310
	},		
	{"adjust_credits",	js_adjust_credits,	1,	JSTYPE_BOOLEAN,	JSDOCSTR("count")
	,JSDOCSTR("Adjust user's credits by <i>count</i> (negative to subtract)")
	,314
	},		
	{"adjust_minutes",	js_adjust_minutes,	1,	JSTYPE_BOOLEAN,	JSDOCSTR("count")
	,JSDOCSTR("Adjust user's extra minutes <i>count</i> (negative to subtract)")
	,314
	},		
	{"posted_message",	js_posted_msg,		1,	JSTYPE_BOOLEAN,	JSDOCSTR("[count]")
	,JSDOCSTR("Adjust user's posted-messages statistics by <i>count</i> (default: 1) (negative to subtract)")
	,314
	},		
	{"sent_email",		js_sent_email,		1,	JSTYPE_BOOLEAN,	JSDOCSTR("[count] [,bool feedback]")
	,JSDOCSTR("Adjust user's email/feedback-sent statistics by <i>count</i> (default: 1) (negative to subtract)")
	,314
	},		
	{"uploaded_file",	js_uploaded_file,	1,	JSTYPE_BOOLEAN,	JSDOCSTR("[bytes] [,files]")
	,JSDOCSTR("Adjust user's files/bytes-uploaded statistics")
	,314
	},		
	{"downloaded_file",	js_downloaded_file,	1,	JSTYPE_BOOLEAN,	JSDOCSTR("[bytes] [,files]")
	,JSDOCSTR("Adjust user's files/bytes-downloaded statistics")
	,314
	},
	{"get_time_left",	js_get_time_left,	1,	JSTYPE_NUMBER,	JSDOCSTR("start_time")
	,JSDOCSTR("Returns the user's available remaining time online, in seconds,<br>"
	"based on the passed <i>start_time</i> value (in time_t format)<br>"
	"Note: this method does not account for pending forced timed events")
	,31401
	},
	{0}
};

static JSBool js_user_stats_resolve(JSContext *cx, JSObject *obj, jsval id)
{
	char*			name=NULL;

	if(id != JSVAL_NULL)
		name=JS_GetStringBytes(JSVAL_TO_STRING(id));

	return(js_SyncResolve(cx, obj, name, js_user_stats_properties, NULL, NULL, 0));
}

static JSBool js_user_stats_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_user_stats_resolve(cx, obj, JSVAL_NULL));
}

static JSBool js_user_security_resolve(JSContext *cx, JSObject *obj, jsval id)
{
	char*			name=NULL;

	if(id != JSVAL_NULL)
		name=JS_GetStringBytes(JSVAL_TO_STRING(id));

	return(js_SyncResolve(cx, obj, name, js_user_security_properties, NULL, NULL, 0));
}

static JSBool js_user_security_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_user_security_resolve(cx, obj, JSVAL_NULL));
}

static JSBool js_user_limits_resolve(JSContext *cx, JSObject *obj, jsval id)
{
	char*			name=NULL;

	if(id != JSVAL_NULL)
		name=JS_GetStringBytes(JSVAL_TO_STRING(id));

	return(js_SyncResolve(cx, obj, name, js_user_limits_properties, NULL, NULL, 0));
}

static JSBool js_user_limits_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_user_limits_resolve(cx, obj, JSVAL_NULL));
}

static JSClass js_user_stats_class = {
     "UserStats"			/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_user_get			/* getProperty	*/
	,js_user_set			/* setProperty	*/
	,js_user_stats_enumerate		/* enumerate	*/
	,js_user_stats_resolve	/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,JS_FinalizeStub        /* finalize		*/
};

static JSClass js_user_security_class = {
     "UserSecurity"			/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_user_get			/* getProperty	*/
	,js_user_set			/* setProperty	*/
	,js_user_security_enumerate		/* enumerate	*/
	,js_user_security_resolve		/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,JS_FinalizeStub        /* finalize		*/
};

static JSClass js_user_limits_class = {
     "UserLimits"			/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_user_get			/* getProperty	*/
	,js_user_set			/* setProperty	*/
	,js_user_limits_enumerate		/* enumerate	*/
	,js_user_limits_resolve			/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,JS_FinalizeStub        /* finalize		*/
};

static JSBool js_user_resolve(JSContext *cx, JSObject *obj, jsval id)
{
	char*			name=NULL;
	JSObject*		newobj;
	private_t*		p;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_TRUE);

	if(id != JSVAL_NULL)
		name=JS_GetStringBytes(JSVAL_TO_STRING(id));

	if(name==NULL || strcmp(name, "stats")==0) {
		/* user.stats */
		if((newobj=JS_DefineObject(cx, obj, "stats"
			,&js_user_stats_class, NULL, JSPROP_ENUMERATE|JSPROP_READONLY))==NULL) 
			return(JS_FALSE);
		JS_SetPrivate(cx, newobj, p);
#ifdef BUILD_JSDOCS
		js_DescribeSyncObject(cx,newobj,"User statistics (all <small>READ ONLY</small>)",310);
		js_CreateArrayOfStrings(cx, newobj, "_property_desc_list", user_stats_prop_desc, JSPROP_READONLY);
#endif
		if(name)
			return(JS_TRUE);

	}

	if(name==NULL || strcmp(name, "security")==0) {
		/* user.security */
		if((newobj=JS_DefineObject(cx, obj, "security"
			,&js_user_security_class, NULL, JSPROP_ENUMERATE|JSPROP_READONLY))==NULL) 
			return(JS_FALSE);
		JS_SetPrivate(cx, newobj, p);
#ifdef BUILD_JSDOCS
		js_DescribeSyncObject(cx,newobj,"User limitations based on security level (all <small>READ ONLY</small>)",311);
		js_CreateArrayOfStrings(cx, newobj, "_property_desc_list", user_limits_prop_desc, JSPROP_READONLY);
#endif
		if(name)
			return(JS_TRUE);
	}

	if(name==NULL || strcmp(name, "limits")==0) {
		/* user.limits */
		if((newobj=JS_DefineObject(cx, obj, "limits"
			,&js_user_limits_class, NULL, JSPROP_ENUMERATE|JSPROP_READONLY))==NULL) 
			return(JS_FALSE);
		JS_SetPrivate(cx, newobj, p);
#ifdef BUILD_JSDOCS
		js_DescribeSyncObject(cx,newobj,"User security settings",310);
		js_CreateArrayOfStrings(cx, newobj, "_property_desc_list", user_security_prop_desc, JSPROP_READONLY);
#endif
		if(name)
			return(JS_TRUE);
	}

	return(js_SyncResolve(cx, obj, name, js_user_properties, js_user_functions, NULL, 0));
}

static JSBool js_user_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_user_resolve(cx, obj, JSVAL_NULL));
}

static JSClass js_user_class = {
     "User"					/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_user_get			/* getProperty	*/
	,js_user_set			/* setProperty	*/
	,js_user_enumerate		/* enumerate	*/
	,js_user_resolve		/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,js_user_finalize		/* finalize		*/
};

/* User Constructor (creates instance of user class) */

static JSBool
js_user_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	int			i;
	int32		val=0;
	user_t		user;
	private_t*	p;

	JS_ValueToInt32(cx,argv[0],&val);
	user.number=(ushort)val;
	if(user.number!=0 && (i=getuserdat(scfg,&user))!=0) {
		JS_ReportError(cx,"Error %d reading user number %d",i,val);
		return(JS_FALSE);
	}

	if((p=(private_t*)malloc(sizeof(private_t)))==NULL)
		return(JS_FALSE);

	memset(p,0,sizeof(private_t));

	p->cfg = scfg;
	p->storage = user;
	p->user = &p->storage;
	p->cached = (user.number==0 ? FALSE : TRUE);

	JS_SetPrivate(cx, obj, p);

	return(JS_TRUE);
}

JSObject* DLLCALL js_CreateUserClass(JSContext* cx, JSObject* parent, scfg_t* cfg)
{
	JSObject*	userclass;

	scfg = cfg;
	userclass = JS_InitClass(cx, parent, NULL
		,&js_user_class
		,js_user_constructor
		,1	/* number of constructor args */
		,NULL /* props, defined in constructor */
		,NULL /* funcs, defined in constructor */
		,NULL,NULL);

	return(userclass);
}

JSObject* DLLCALL js_CreateUserObject(JSContext* cx, JSObject* parent, scfg_t* cfg, char* name
									  ,user_t* user, client_t* client, BOOL global_user)
{
	JSObject*	userobj;
	private_t*	p;
	jsval		val;

	if(name==NULL)
	    userobj = JS_NewObject(cx, &js_user_class, NULL, parent);
	else if(JS_GetProperty(cx,parent,name,&val) && val!=JSVAL_VOID)
		userobj = JSVAL_TO_OBJECT(val);	/* Return existing user object */
	else
		userobj = JS_DefineObject(cx, parent, name, &js_user_class
								, NULL, JSPROP_ENUMERATE|JSPROP_READONLY);
	if(userobj==NULL)
		return(NULL);

	if((p=JS_GetPrivate(cx, userobj)) == NULL)	/* Uses existing private pointer: Fix memory leak? */
		if((p=(private_t*)malloc(sizeof(private_t)))==NULL)
			return(NULL);

	memset(p,0,sizeof(private_t));

	p->cfg = cfg;
	p->client = client;
	p->cached = FALSE;
	p->user = &p->storage;
	if(user!=NULL) {
		p->storage = *user;
		if(global_user)
			p->user = user;
	}

	JS_SetPrivate(cx, userobj, p);	

#ifdef BUILD_JSDOCS
	js_DescribeSyncObject(cx,userobj
		,"Instance of <i>User</i> class, representing current user online"
		,310);
	js_DescribeSyncConstructor(cx,userobj
		,"To create a new user object: <tt>var u = new User(<i>number</i>)</tt>");
	js_CreateArrayOfStrings(cx, userobj
		,"_property_desc_list", user_prop_desc, JSPROP_READONLY);
#endif

	return(userobj);
}

/****************************************************************************/
/* Creates all the user-specific objects: user, msg_area, file_area			*/
/****************************************************************************/
JSBool DLLCALL
js_CreateUserObjects(JSContext* cx, JSObject* parent, scfg_t* cfg, user_t* user, client_t* client
					 ,char* html_index_file, subscan_t* subscan)
{
	if(js_CreateUserObject(cx,parent,cfg,"user",user,client,/* global_user */TRUE)==NULL)
		return(JS_FALSE);
	if(js_CreateFileAreaObject(cx,parent,cfg,user,client,html_index_file)==NULL) 
		return(JS_FALSE);
	if(js_CreateMsgAreaObject(cx,parent,cfg,user,client,subscan)==NULL) 
		return(JS_FALSE);
	if(js_CreateXtrnAreaObject(cx,parent,cfg,user,client)==NULL)
		return(JS_FALSE);

	return(JS_TRUE);
}

#endif	/* JAVSCRIPT */
