Skip to content
Snippets Groups Projects
js_msgbase.c 51.9 KiB
Newer Older
/* js_msgbase.c */

/* Synchronet JavaScript "MsgBase" Object */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2004 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"

#ifdef JAVASCRIPT

static scfg_t* scfg=NULL;

typedef struct
{
	smb_t	smb;
	BOOL	debug;

} private_t;

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

/* Destructor */

static void js_finalize_msgbase(JSContext *cx, JSObject *obj)
{
	private_t* p;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return;

	if(SMB_IS_OPEN(&(p->smb)))
		smb_close(&(p->smb));

	free(p);

	JS_SetPrivate(cx, obj, NULL);
}

/* Methods */

static JSBool
js_open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t* p;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

	*rval = JSVAL_FALSE;

	if(p->smb.subnum==INVALID_SUB 
		&& strchr(p->smb.file,'/')==NULL
		&& strchr(p->smb.file,'\\')==NULL) {
		JS_ReportError(cx,"Unrecognized msgbase code: %s",p->smb.file);
		return(JS_TRUE);
	}

	if(smb_open(&(p->smb))!=0)
		return(JS_TRUE);

	*rval = JSVAL_TRUE;
	return(JS_TRUE);
}


static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t* p;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);

	smb_close(&(p->smb));

	*rval = JSVAL_VOID;
	return(JS_TRUE);
}

static BOOL parse_recipient_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg)
	if(JS_GetProperty(cx, hdr, "to", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
	} else {
		if(p->smb.status.attr&SMB_EMAIL)	/* e-mail */
			return(FALSE);					/* "to" property required */
		cp="All";
	}
	smb_hfield_str(msg, RECIPIENT, cp);
	if(!(p->smb.status.attr&SMB_EMAIL)) {
		SAFECOPY(to,cp);
		strlwr(to);
		msg->idx.to=crc16(to,0);
	}

	if(JS_GetProperty(cx, hdr, "to_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, RECIPIENTEXT, cp);
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.to=atoi(cp);
	}

	if(JS_GetProperty(cx, hdr, "to_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, RECIPIENTORG, cp);
	}

	if(JS_GetProperty(cx, hdr, "to_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
		smb_hfield(msg, RECIPIENTNETTYPE, sizeof(nettype), &nettype);
		if(p->smb.status.attr&SMB_EMAIL && nettype!=NET_NONE)
			msg->idx.to=0;
	}

	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, RECIPIENTNETADDR, cp);
	}

	if(JS_GetProperty(cx, hdr, "to_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
		smb_hfield(msg, RECIPIENTAGENT, sizeof(agent), &agent);
	}

	return(TRUE);
}

static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg
								,BOOL recipient)
{
	char*		cp;
	ushort		nettype;
	jsval		val;
	JSObject*	array;
	JSObject*	field;
	jsuint		i,len;
	if(recipient && !parse_recipient_object(cx,p,hdr,msg))
		return(FALSE);

	/* Required Header Fields */
	if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
	} else
		cp="";
	smb_hfield_str(msg, SUBJECT, cp);
	if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
	} else
		return(FALSE);	/* "from" property required */
	smb_hfield_str(msg, SENDER, cp);
	if(!(p->smb.status.attr&SMB_EMAIL)) {
		msg->idx.from=crc16(from,0);

	/* Optional Header Fields */
	if(JS_GetProperty(cx, hdr, "from_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, SENDEREXT, cp);
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.from=atoi(cp);
	}

	if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, SENDERORG, cp);
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
		smb_hfield(msg, SENDERNETTYPE, sizeof(nettype), &nettype);
		if(p->smb.status.attr&SMB_EMAIL && nettype!=NET_NONE)
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, SENDERNETADDR, cp);
	if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
		smb_hfield(msg, SENDERAGENT, sizeof(agent), &agent);
	}

	if(JS_GetProperty(cx, hdr, "from_ip_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, SENDERIPADDR, cp);
	}

	if(JS_GetProperty(cx, hdr, "from_host_name", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, SENDERHOSTNAME, cp);
	}

	if(JS_GetProperty(cx, hdr, "from_protocol", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, SENDERPROTOCOL, cp);
	}

	if(JS_GetProperty(cx, hdr, "from_port", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		port=(ushort)i32;
		smb_hfield(msg, SENDERPORT, sizeof(port), &port);
	}

	if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, REPLYTO, cp);
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, REPLYTOEXT, cp);
	if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		smb_hfield_str(msg, REPLYTOORG, cp);
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
		smb_hfield(msg, REPLYTONETTYPE, sizeof(nettype), &nettype);
	}

	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, REPLYTONETADDR, cp);
	if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
		smb_hfield(msg, REPLYTOAGENT, sizeof(agent), &agent);
	}

	if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, RFC822MSGID, cp);
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, RFC822REPLYID, cp);
	if(JS_GetProperty(cx, hdr, "reverse_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, SMTPREVERSEPATH, cp);
	if(JS_GetProperty(cx, hdr, "path", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, USENETPATH, cp);
	if(JS_GetProperty(cx, hdr, "newsgroups", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, USENETNEWSGROUPS, cp);
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, FIDOMSGID, cp);
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, FIDOREPLYID, cp);
	if(JS_GetProperty(cx, hdr, "ftn_area", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, FIDOAREA, cp);
	if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, FIDOFLAGS, cp);
	if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, FIDOPID, cp);
	if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
		smb_hfield_str(msg, FIDOTID, cp);
	if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		msg->hdr.when_written=rfc822date(cp);
	}
	
	/* Numeric Header Fields */
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.attr=(ushort)i32;
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.auxattr=i32;
	}
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.netattr=i32;
	}
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val))  {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.time=i32;
	}
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.zone=(short)i32;
	}
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.time=i32;
	}
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.zone=(short)i32;
	}

	if(JS_GetProperty(cx, hdr, "thread_orig", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_orig=i32;
	}
	if(JS_GetProperty(cx, hdr, "thread_next", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_next=i32;
	}
	if(JS_GetProperty(cx, hdr, "thread_first", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_first=i32;
	}
	if(JS_GetProperty(cx, hdr, "field_list", &val) && JSVAL_IS_OBJECT(val)) {
		array=JSVAL_TO_OBJECT(val);
		len=0;
		if(!JS_GetArrayLength(cx, array, &len))
			return(FALSE);

		for(i=0;i<len;i++) {
			if(!JS_GetElement(cx, array, i, &val))
				continue;
			if(!JSVAL_IS_OBJECT(val))
				continue;
			field=JSVAL_TO_OBJECT(val);
			if(!JS_GetProperty(cx, field, "type", &val))
			if(JSVAL_IS_STRING(val))
				type=smb_hfieldtypelookup(JS_GetStringBytes(JS_ValueToString(cx,val)));
			else {
				JS_ValueToInt32(cx,val,&i32);
				type=(ushort)i32;
			}
			if(!JS_GetProperty(cx, field, "data", &val))
			if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			smb_hfield_str(msg, type, cp);
BOOL msg_offset_by_id(scfg_t* scfg, smb_t* smb, char* id, ulong* offset)
	if(!get_msg_by_id(scfg,smb,id,&msg))
		return(FALSE);

	smb_freemsgmem(&msg);

	*offset = msg.offset;
	return(TRUE);
}

static JSBool
js_get_msg_index(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	uintN		n;
	smbmsg_t	msg;
	JSObject*	idxobj;
	JSBool		by_offset=JS_FALSE;
	private_t*	p;

	*rval = JSVAL_NULL;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);
	}

	if(!SMB_IS_OPEN(&(p->smb)))
		return(JS_TRUE);

	memset(&msg,0,sizeof(msg));

	for(n=0;n<argc;n++) {
		if(JSVAL_IS_BOOLEAN(argv[n]))
			by_offset=JSVAL_TO_BOOLEAN(argv[n]);
		else if(JSVAL_IS_NUMBER(argv[n])) {
			if(by_offset)							/* Get by offset */
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);

			if(smb_getmsgidx(&(p->smb), &msg)!=0)
				return(JS_TRUE);

			break;
		}
	}

	if((idxobj=JS_NewObject(cx,NULL,NULL,obj))==NULL)
		return(JS_TRUE);

	JS_NewNumberValue(cx, msg.idx.number	,&val);
	JS_DefineProperty(cx, idxobj, "number"	,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	JS_NewNumberValue(cx, msg.idx.to		,&val);
	JS_DefineProperty(cx, idxobj, "to"		,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	JS_NewNumberValue(cx, msg.idx.from		,&val);
	JS_DefineProperty(cx, idxobj, "from"	,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	JS_NewNumberValue(cx, msg.idx.subj		,&val);
	JS_DefineProperty(cx, idxobj, "subject"	,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	JS_NewNumberValue(cx, msg.idx.attr		,&val);
	JS_DefineProperty(cx, idxobj, "attr"	,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	JS_NewNumberValue(cx, msg.idx.offset	,&val);
	JS_DefineProperty(cx, idxobj, "offset"	,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	JS_NewNumberValue(cx, msg.idx.time		,&val);
	JS_DefineProperty(cx, idxobj, "time"	,val
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	*rval = OBJECT_TO_JSVAL(idxobj);

	return(JS_TRUE);
}

static JSBool
js_get_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char		msg_id[256];
	char		reply_id[256];
	JSString*	js_str;
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
	if(!SMB_IS_OPEN(&(p->smb)))
		return(JS_TRUE);

	memset(&msg,0,sizeof(msg));

	/* Parse boolean arguments first */
		if(!JSVAL_IS_BOOLEAN(argv[n]))
			continue;
		if(n)
			expand_fields=JSVAL_TO_BOOLEAN(argv[n]);
		else
			by_offset=JSVAL_TO_BOOLEAN(argv[n]);
	}

	/* Now parse message offset/id and get message */
	for(n=0;n<argc;n++) {
		if(JSVAL_IS_NUMBER(argv[n])) {
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);

			if(smb_getmsgidx(&(p->smb), &msg)!=0)
				return(JS_TRUE);

			if(smb_lockmsghdr(&(p->smb),&msg)!=0)
				return(JS_TRUE);

			if(smb_getmsghdr(&(p->smb), &msg)!=0) {
				smb_unlockmsghdr(&(p->smb),&msg); 
				return(JS_TRUE);
			}

			smb_unlockmsghdr(&(p->smb),&msg); 
			break;
		} else if(JSVAL_IS_STRING(argv[n]))	{		/* Get by ID */
				,JS_GetStringBytes(JSVAL_TO_STRING(argv[n]))
				return(JS_TRUE);	/* ID not found */
	if(msg.hdr.number==0) /* No valid message number/id/offset specified */
	if((hdrobj=JS_NewObject(cx,NULL,NULL,obj))==NULL) {
rswindell's avatar
rswindell committed
		smb_freemsgmem(&msg);
rswindell's avatar
rswindell committed
	}
	JS_NewNumberValue(cx,msg.hdr.number,&v);
	JS_DefineProperty(cx, hdrobj, "number", v, NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	if((js_str=JS_NewStringCopyZ(cx,truncsp(msg.to)))==NULL)
		return(JS_FALSE);
	JS_DefineProperty(cx, hdrobj, "to"
		,STRING_TO_JSVAL(js_str)

	if((js_str=JS_NewStringCopyZ(cx,truncsp(msg.from)))==NULL)
		return(JS_FALSE);
	JS_DefineProperty(cx, hdrobj, "from"
		,STRING_TO_JSVAL(js_str)

	if((js_str=JS_NewStringCopyZ(cx,truncsp(msg.subj)))==NULL)
		return(JS_FALSE);
	JS_DefineProperty(cx, hdrobj, "subject"
		,STRING_TO_JSVAL(js_str)
	if(msg.summary!=NULL 
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.summary)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "summary"
			,STRING_TO_JSVAL(js_str)
	if(msg.to_ext!=NULL 
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.to_ext)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "to_ext"
			,STRING_TO_JSVAL(js_str)
	if(msg.from_ext!=NULL 
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.from_ext)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_ext"
			,STRING_TO_JSVAL(js_str)
	if(msg.from_org!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.from_org)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_org"
			,STRING_TO_JSVAL(js_str)
	if(msg.replyto!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.replyto)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "replyto"
			,STRING_TO_JSVAL(js_str)
	if(msg.replyto_ext!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.replyto_ext)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "replyto_ext"
			,STRING_TO_JSVAL(js_str)
	if(msg.reverse_path!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.reverse_path)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "reverse_path"
			,STRING_TO_JSVAL(js_str)
	if(expand_fields || msg.to_agent)
		JS_DefineProperty(cx, hdrobj, "to_agent",INT_TO_JSVAL(msg.to_agent)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(expand_fields || msg.from_agent)
		JS_DefineProperty(cx, hdrobj, "from_agent",INT_TO_JSVAL(msg.from_agent)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(expand_fields || msg.replyto_agent)
		JS_DefineProperty(cx, hdrobj, "replyto_agent",INT_TO_JSVAL(msg.replyto_agent)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(expand_fields || msg.to_net.type)
		JS_DefineProperty(cx, hdrobj, "to_net_type",INT_TO_JSVAL(msg.to_net.type)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.to_net.type
		&& (js_str=JS_NewStringCopyZ(cx,net_addr(&msg.to_net)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "to_net_addr"
			,STRING_TO_JSVAL(js_str)
	if(expand_fields || msg.from_net.type)
		JS_DefineProperty(cx, hdrobj, "from_net_type",INT_TO_JSVAL(msg.from_net.type)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.from_net.type
		&& (js_str=JS_NewStringCopyZ(cx,net_addr(&msg.from_net)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_net_addr"
			,STRING_TO_JSVAL(js_str)

	if(expand_fields || msg.replyto_net.type)
		JS_DefineProperty(cx, hdrobj, "replyto_net_type",INT_TO_JSVAL(msg.replyto_net.type)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.replyto_net.type
		&& (js_str=JS_NewStringCopyZ(cx,net_addr(&msg.replyto_net)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "replyto_net_addr"
			,STRING_TO_JSVAL(js_str)
	if((val=smb_get_hfield(&msg,SENDERIPADDR,NULL))!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,val))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_ip_addr"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_ENUMERATE);

	if((val=smb_get_hfield(&msg,SENDERHOSTNAME,NULL))!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,val))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_host_name"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_ENUMERATE);

	if((val=smb_get_hfield(&msg,SENDERPROTOCOL,NULL))!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,val))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_protocol"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_ENUMERATE);

	if((port=smb_get_hfield(&msg,SENDERPORT,NULL))!=NULL)
		JS_DefineProperty(cx, hdrobj, "from_port"
			,INT_TO_JSVAL(*port)
			,NULL,NULL,JSPROP_ENUMERATE);
	
	if(expand_fields || msg.forwarded)
		JS_DefineProperty(cx, hdrobj, "forwarded",INT_TO_JSVAL(msg.forwarded)
			,NULL,NULL,JSPROP_ENUMERATE);
	if(expand_fields || msg.expiration) {
		JS_NewNumberValue(cx,msg.expiration,&v);
		JS_DefineProperty(cx, hdrobj, "expiration",v,NULL,NULL,JSPROP_ENUMERATE);
	}
	if(expand_fields || msg.priority) {
		JS_NewNumberValue(cx,msg.priority,&v);
		JS_DefineProperty(cx, hdrobj, "priority",v,NULL,NULL,JSPROP_ENUMERATE);
	}
	if(expand_fields || msg.cost) {
		JS_NewNumberValue(cx,msg.cost,&v);
		JS_DefineProperty(cx, hdrobj, "cost",v,NULL,NULL,JSPROP_ENUMERATE);
	}
	/* Fixed length portion of msg header */
	JS_DefineProperty(cx, hdrobj, "type", INT_TO_JSVAL(msg.hdr.type)
	JS_DefineProperty(cx, hdrobj, "version", INT_TO_JSVAL(msg.hdr.version)
	JS_DefineProperty(cx, hdrobj, "attr", INT_TO_JSVAL(msg.hdr.attr)
	JS_NewNumberValue(cx,msg.hdr.auxattr,&v);
	JS_DefineProperty(cx, hdrobj, "auxattr", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.netattr,&v);
	JS_DefineProperty(cx, hdrobj, "netattr", v, NULL,NULL,JSPROP_ENUMERATE);

	JS_NewNumberValue(cx,msg.hdr.when_written.time,&v);
	JS_DefineProperty(cx, hdrobj, "when_written_time", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.when_written.zone,&v);
	JS_DefineProperty(cx, hdrobj, "when_written_zone", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.when_imported.time,&v);
	JS_DefineProperty(cx, hdrobj, "when_imported_time", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.when_imported.zone,&v);
	JS_DefineProperty(cx, hdrobj, "when_imported_zone", v, NULL,NULL,JSPROP_ENUMERATE);

	JS_NewNumberValue(cx,msg.hdr.thread_orig,&v);
	JS_DefineProperty(cx, hdrobj, "thread_orig", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.thread_next,&v);
	JS_DefineProperty(cx, hdrobj, "thread_next", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.thread_first,&v);
	JS_DefineProperty(cx, hdrobj, "thread_first", v, NULL,NULL,JSPROP_ENUMERATE);

	JS_NewNumberValue(cx,msg.hdr.delivery_attempts,&v);
	JS_DefineProperty(cx, hdrobj, "delivery_attempts", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.last_downloaded,&v);
	JS_DefineProperty(cx, hdrobj, "last_downloaded", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_NewNumberValue(cx,msg.hdr.times_downloaded,&v);
	JS_DefineProperty(cx, hdrobj, "times_downloaded", v, NULL,NULL,JSPROP_ENUMERATE);

	JS_NewNumberValue(cx,smb_getmsgdatlen(&msg),&v);
	JS_DefineProperty(cx, hdrobj, "data_length", v, NULL,NULL,JSPROP_ENUMERATE);
	if((js_str=JS_NewStringCopyZ(cx,msgdate(msg.hdr.when_written,date)))==NULL)
		return(JS_FALSE);
	JS_DefineProperty(cx, hdrobj, "date"
		,STRING_TO_JSVAL(js_str)
	/* Reply-ID (References) */
		val=msg.reply_id;
	else {
		reply_id[0]=0;
		if(expand_fields && msg.hdr.thread_orig) {
			memset(&orig_msg,0,sizeof(orig_msg));
			orig_msg.hdr.number=msg.hdr.thread_orig;
			if(smb_getmsgidx(&(p->smb), &orig_msg))
				sprintf(reply_id,"<%s>",p->smb.last_error);
			else
				SAFECOPY(reply_id,get_msgid(scfg,p->smb.subnum,&orig_msg));
		val=reply_id;
	}
	if(val[0] && (js_str=JS_NewStringCopyZ(cx,truncsp(val)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "reply_id"
			, STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(expand_fields || msg.id!=NULL) {
		SAFECOPY(msg_id,get_msgid(scfg,p->smb.subnum,&msg));
		val=msg_id;
		if((js_str=JS_NewStringCopyZ(cx,truncsp(val)))==NULL)
			return(JS_FALSE);
		JS_DefineProperty(cx, hdrobj, "id"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	}
	if(msg.path!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.path)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "path"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.newsgroups!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.newsgroups)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "newsgroups"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	/* FidoNet Header Fields */
	if(msg.ftn_msgid!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.ftn_msgid)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_msgid"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_reply!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.ftn_reply)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_reply"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_pid!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.ftn_pid)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_pid"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_tid!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.ftn_tid)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_tid"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_area!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.ftn_area)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_area"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_flags!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.ftn_flags)))!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_flags"
			,STRING_TO_JSVAL(js_str)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	/* Create hdr.field_list[] with repeating header fields (including type and data) */
	if((array=JS_NewArrayObject(cx,0,NULL))!=NULL) {
		items=0;
		for(i=0;i<msg.total_hfields;i++) {
			switch(msg.hfield[i].type) {
				case SMB_COMMENT:
				case SMB_CARBONCOPY:
				case SMB_GROUP:
				case FILEATTACH:
				case DESTFILE:
				case FILEATTACHLIST:
				case DESTFILELIST:
				case FILEREQUEST:
				case FILEPASSWORD:
				case FILEREQUESTLIST:
				case FILEPASSWORDLIST:
				case FIDOCTRL:
				case FIDOSEENBY:
				case FIDOPATH:
				case RFC822HEADER:
				case UNKNOWNASCII:
					/* only support these header field types */
					break;
				default:
					/* dupe or possibly binary header field */
					continue;
			}
			if((field=JS_NewObject(cx,NULL,NULL,array))==NULL)
				continue;
			JS_DefineProperty(cx,field,"type"
				,INT_TO_JSVAL(msg.hfield[i].type)
				,NULL,NULL,JSPROP_ENUMERATE);
			if((js_str=JS_NewStringCopyN(cx,msg.hfield_dat[i],msg.hfield[i].length))==NULL)
				break;
			JS_DefineProperty(cx,field,"data"
				,STRING_TO_JSVAL(js_str)
				,NULL,NULL,JSPROP_ENUMERATE);
			JS_DefineElement(cx,array,items,OBJECT_TO_JSVAL(field)
				,NULL,NULL,JSPROP_ENUMERATE);
			items++;
		}
		JS_DefineProperty(cx,hdrobj,"field_list",OBJECT_TO_JSVAL(array)
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	}

rswindell's avatar
rswindell committed
	smb_freemsgmem(&msg);

	*rval = OBJECT_TO_JSVAL(hdrobj);

	return(JS_TRUE);
}

static JSBool
js_put_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	JSBool		msg_specified=JS_FALSE;
	smbmsg_t	msg;
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
		JS_ReportError(cx,getprivate_failure,WHERE);
		return(JS_FALSE);

	if(!SMB_IS_OPEN(&(p->smb)))
		return(JS_TRUE);

	memset(&msg,0,sizeof(msg));

	for(n=0;n<argc;n++) {
		if(JSVAL_IS_BOOLEAN(argv[n]))
			by_offset=JSVAL_TO_BOOLEAN(argv[n]);
		else if(JSVAL_IS_NUMBER(argv[n])) {
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);
			break;
		} else if(JSVAL_IS_STRING(argv[n]))	{		/* Get by ID */
			if(!msg_offset_by_id(scfg,&(p->smb)
				,JS_GetStringBytes(JSVAL_TO_STRING(argv[n]))
				,&msg.offset))
				return(JS_TRUE);	/* ID not found */
	if(!msg_specified)
		return(JS_TRUE);

	if(n==argc || !JSVAL_IS_OBJECT(argv[n])) /* no header supplied? */
		return(JS_TRUE);

	hdr = JSVAL_TO_OBJECT(argv[n++]);

	if(smb_getmsgidx(&(p->smb), &msg)!=0)
		return(JS_TRUE);

	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
		return(JS_TRUE);

	do {
		if(smb_getmsghdr(&(p->smb), &msg)!=0)
			break;
		smb_freemsghdrmem(&msg);	/* prevent duplicate header fields */