js_msgbase.c 52.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* 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)		*
 *																			*
11
 * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html		*
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 *																			*
 * 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;

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

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/* 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 */

72 73 74 75 76 77
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) {
78
		JS_ReportError(cx,getprivate_failure,WHERE);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		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);
}


99 100 101 102 103
static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t* p;
	
104
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
105
		JS_ReportError(cx,getprivate_failure,WHERE);
106
		return(JS_FALSE);
107
	}
108 109 110 111 112 113 114

	smb_close(&(p->smb));

	*rval = JSVAL_VOID;
	return(JS_TRUE);
}

115
static BOOL parse_recipient_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg)
116 117
{
	char*		cp;
118
	char		to[256];
119
	ushort		nettype=NET_UNKNOWN;
120 121 122 123
	ushort		agent;
	int32		i32;
	jsval		val;

124
	if(JS_GetProperty(cx, hdr, "to", &val) && !JSVAL_NULL_OR_VOID(val)) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138
		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);
	}

139
	if(JS_GetProperty(cx, hdr, "to_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
140 141 142 143 144 145 146
		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);
	}

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

153
	if(JS_GetProperty(cx, hdr, "to_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
154 155 156 157
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
	}

158
	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
159 160
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
161
		smb_hfield_netaddr(msg, RECIPIENTNETADDR, cp, &nettype);
162 163 164 165 166
	}

	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.to=0;
167
		smb_hfield_bin(msg, RECIPIENTNETTYPE, nettype);
rswindell's avatar
rswindell committed
168 169
	}

170
	if(JS_GetProperty(cx, hdr, "to_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
171 172
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
173
		smb_hfield_bin(msg, RECIPIENTAGENT, agent);
rswindell's avatar
rswindell committed
174
	}
175 176 177 178 179 180 181 182

	return(TRUE);
}

static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg
								,BOOL recipient)
{
	char*		cp;
183
	char		from[256];
184
	ushort		nettype=NET_UNKNOWN;
185
	ushort		type;
186
	ushort		agent;
187
	ushort		port;
188
	int32		i32;
189
	jsval		val;
190 191 192
	JSObject*	array;
	JSObject*	field;
	jsuint		i,len;
193

194 195 196
	if(hdr==NULL)
		return(FALSE);

197 198 199
	if(recipient && !parse_recipient_object(cx,p,hdr,msg))
		return(FALSE);

200
	/* Required Header Fields */
201
	if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
202
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
203 204 205
			return(FALSE);
	} else
		cp="";
206
	smb_hfield_str(msg, SUBJECT, cp);
207
	msg->idx.subj=smb_subject_crc(cp);
208

209
	if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
210
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
211 212 213
			return(FALSE);
	} else
		return(FALSE);	/* "from" property required */
214
	smb_hfield_str(msg, SENDER, cp);
215
	if(!(p->smb.status.attr&SMB_EMAIL)) {
216 217
		SAFECOPY(from,cp);
		strlwr(from);
218
		msg->idx.from=crc16(from,0);
219
	}
220 221

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

230
	if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
231
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
232
			return(FALSE);
233
		smb_hfield_str(msg, SENDERORG, cp);
234 235
	}

236
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
237 238
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
239 240
	}

241
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
242
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
243
			return(FALSE);
244
		smb_hfield_netaddr(msg, SENDERNETADDR, cp, &nettype);
245 246 247 248 249
	}
	
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.from=0;
250
		smb_hfield_bin(msg, SENDERNETTYPE, nettype);
251 252
	}

253
	if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
254 255
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
256
		smb_hfield_bin(msg, SENDERAGENT, agent);
257 258
	}

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

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

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

277
	if(JS_GetProperty(cx, hdr, "from_port", &val) && !JSVAL_NULL_OR_VOID(val)) {
278 279
		JS_ValueToInt32(cx,val,&i32);
		port=(ushort)i32;
280
		smb_hfield_bin(msg, SENDERPORT, port);
281 282
	}

283
	if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
284
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
285
			return(FALSE);
286
		smb_hfield_str(msg, REPLYTO, cp);
287 288
	}

289
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
290
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
291
			return(FALSE);
292
		smb_hfield_str(msg, REPLYTOEXT, cp);
293 294
	}

295
	if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
296 297
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
298
		smb_hfield_str(msg, REPLYTOORG, cp);
299 300
	}

301
	nettype=NET_UNKNOWN;
302
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
303 304
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
305
	}
306
	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
307
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
308
			return(FALSE);
309
		smb_hfield_netaddr(msg, REPLYTONETADDR, cp, &nettype);
310
	}
311
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE)
312
		smb_hfield_bin(msg, REPLYTONETTYPE, nettype);
313

314
	if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
315 316
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
317
		smb_hfield_bin(msg, REPLYTOAGENT, agent);
318 319
	}

320
	/* RFC822 headers */
321
	if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
322
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
323
			return(FALSE);
324
		smb_hfield_str(msg, RFC822MSGID, cp);
325 326
	}

327
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
328
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
329
			return(FALSE);
330
		smb_hfield_str(msg, RFC822REPLYID, cp);
331 332
	}

333
	if(JS_GetProperty(cx, hdr, "reverse_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
334
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
335
			return(FALSE);
336
		smb_hfield_str(msg, SMTPREVERSEPATH, cp);
337 338
	}

339
	/* USENET headers */
340
	if(JS_GetProperty(cx, hdr, "path", &val) && !JSVAL_NULL_OR_VOID(val)) {
341
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
342
			return(FALSE);
343
		smb_hfield_str(msg, USENETPATH, cp);
344 345
	}

346
	if(JS_GetProperty(cx, hdr, "newsgroups", &val) && !JSVAL_NULL_OR_VOID(val)) {
347
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
348
			return(FALSE);
349
		smb_hfield_str(msg, USENETNEWSGROUPS, cp);
350 351 352
	}

	/* FTN headers */
353
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && !JSVAL_NULL_OR_VOID(val)) {
354
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
355
			return(FALSE);
356
		smb_hfield_str(msg, FIDOMSGID, cp);
357 358
	}

359
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && !JSVAL_NULL_OR_VOID(val)) {
360
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
361
			return(FALSE);
362
		smb_hfield_str(msg, FIDOREPLYID, cp);
363 364
	}

365
	if(JS_GetProperty(cx, hdr, "ftn_area", &val) && !JSVAL_NULL_OR_VOID(val)) {
366
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
367
			return(FALSE);
368
		smb_hfield_str(msg, FIDOAREA, cp);
369 370
	}

371
	if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && !JSVAL_NULL_OR_VOID(val)) {
372
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
373
			return(FALSE);
374
		smb_hfield_str(msg, FIDOFLAGS, cp);
375 376
	}

377
	if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && !JSVAL_NULL_OR_VOID(val)) {
378
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
379
			return(FALSE);
380
		smb_hfield_str(msg, FIDOPID, cp);
381 382
	}

383
	if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && !JSVAL_NULL_OR_VOID(val)) {
384
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
385
			return(FALSE);
386
		smb_hfield_str(msg, FIDOTID, cp);
387 388
	}

389
	if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
390
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
391 392 393
			return(FALSE);
		msg->hdr.when_written=rfc822date(cp);
	}
394 395
	
	/* Numeric Header Fields */
396
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
397 398
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.attr=(ushort)i32;
399 400
		msg->idx.attr=msg->hdr.attr;
	}
401
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
402 403 404
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.auxattr=i32;
	}
405
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
406 407 408
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.netattr=i32;
	}
409
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val))  {
410 411 412
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.time=i32;
	}
413
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
414 415 416
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.zone=(short)i32;
	}
417
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
418 419 420
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.time=i32;
	}
421
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
422 423 424 425
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.zone=(short)i32;
	}

426 427
	if((JS_GetProperty(cx, hdr, "thread_orig", &val) 
		|| JS_GetProperty(cx, hdr, "thread_back", &val)) && !JSVAL_NULL_OR_VOID(val)) {
428
		JS_ValueToInt32(cx,val,&i32);
429
		msg->hdr.thread_back=i32;
430
	}
431
	if(JS_GetProperty(cx, hdr, "thread_next", &val) && !JSVAL_NULL_OR_VOID(val)) {
432 433 434
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_next=i32;
	}
435
	if(JS_GetProperty(cx, hdr, "thread_first", &val) && !JSVAL_NULL_OR_VOID(val)) {
436 437 438
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_first=i32;
	}
439

440 441 442
	if(JS_GetProperty(cx, hdr, "field_list", &val) && JSVAL_IS_OBJECT(val)) {
		array=JSVAL_TO_OBJECT(val);
		len=0;
443 444
		if(!JS_GetArrayLength(cx, array, &len))
			return(FALSE);
445 446 447 448 449 450 451

		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);
452
			if(!JS_GetProperty(cx, field, "type", &val))
453
				continue;
454 455 456 457 458 459
			if(JSVAL_IS_STRING(val))
				type=smb_hfieldtypelookup(JS_GetStringBytes(JS_ValueToString(cx,val)));
			else {
				JS_ValueToInt32(cx,val,&i32);
				type=(ushort)i32;
			}
460
			if(!JS_GetProperty(cx, field, "data", &val))
461
				continue;
462
			if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
463
				return(FALSE);
464
			smb_hfield_str(msg, type, cp);
465 466 467
		}
	}

468 469 470
	return(TRUE);
}

471
BOOL msg_offset_by_id(scfg_t* scfg, smb_t* smb, char* id, ulong* offset)
472 473 474
{
	smbmsg_t msg;

475
	if(smb_getmsgidx_by_msgid(smb,&msg,id)!=SMB_SUCCESS)
476 477 478 479 480 481
		return(FALSE);

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

482 483 484 485
static JSBool
js_get_msg_index(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	uintN		n;
486
	jsval		val;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	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]);
507
		else if(JSVAL_IS_NUMBER(argv[n])) {
508
			if(by_offset)							/* Get by offset */
509
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
510
			else									/* Get by number */
511
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);
512 513 514 515 516 517 518 519 520 521 522

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

			break;
		}
	}

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

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

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

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

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

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

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

547 548
	JS_NewNumberValue(cx, msg.idx.time		,&val);
	JS_DefineProperty(cx, idxobj, "time"	,val
549 550 551 552 553 554 555
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	*rval = OBJECT_TO_JSVAL(idxobj);

	return(JS_TRUE);
}

556 557 558
static JSBool
js_get_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
559
	char		date[128];
560 561
	char		msg_id[256];
	char		reply_id[256];
562
	char*		val;
563
	ushort*		port;
564
	int			i;
565
	uintN		n;
566
	smbmsg_t	msg;
567
	smbmsg_t	remsg;
568
	JSObject*	hdrobj;
569 570
	JSObject*	array;
	JSObject*	field;
571
	JSString*	js_str;
572
	jsint		items;
573
	jsval		v;
574
	JSBool		by_offset=JS_FALSE;
575
	JSBool		expand_fields=JS_TRUE;
576 577 578 579
	private_t*	p;

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

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

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

590
	/* Parse boolean arguments first */
591
	for(n=0;n<argc;n++) {
592 593 594 595 596
		if(!JSVAL_IS_BOOLEAN(argv[n]))
			continue;
		if(n)
			expand_fields=JSVAL_TO_BOOLEAN(argv[n]);
		else
597
			by_offset=JSVAL_TO_BOOLEAN(argv[n]);
598 599 600 601 602
	}

	/* Now parse message offset/id and get message */
	for(n=0;n<argc;n++) {
		if(JSVAL_IS_NUMBER(argv[n])) {
603
			if(by_offset)							/* Get by offset */
604
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
605
			else									/* Get by number */
606
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);
607 608 609 610 611 612 613 614 615 616 617 618 619

			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); 
620 621
			break;
		} else if(JSVAL_IS_STRING(argv[n]))	{		/* Get by ID */
622 623
			if(smb_getmsghdr_by_msgid(&(p->smb),&msg
				,JS_GetStringBytes(JSVAL_TO_STRING(argv[n])))!=SMB_SUCCESS)
624 625 626 627
				return(JS_TRUE);	/* ID not found */
			break;
		}
	}
628

629
	if(msg.hdr.number==0) /* No valid message number/id/offset specified */
630 631
		return(JS_TRUE);

632
	if((hdrobj=JS_NewObject(cx,NULL,NULL,obj))==NULL) {
rswindell's avatar
rswindell committed
633
		smb_freemsgmem(&msg);
634
		return(JS_TRUE);
rswindell's avatar
rswindell committed
635
	}
636

637 638
	JS_NewNumberValue(cx,msg.hdr.number,&v);
	JS_DefineProperty(cx, hdrobj, "number", v, NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
639 640 641

	if((js_str=JS_NewStringCopyZ(cx,truncsp(msg.to)))==NULL)
		return(JS_FALSE);
642
	JS_DefineProperty(cx, hdrobj, "to"
643
		,STRING_TO_JSVAL(js_str)
644
		,NULL,NULL,JSPROP_ENUMERATE);
645 646 647

	if((js_str=JS_NewStringCopyZ(cx,truncsp(msg.from)))==NULL)
		return(JS_FALSE);
648
	JS_DefineProperty(cx, hdrobj, "from"
649
		,STRING_TO_JSVAL(js_str)
650
		,NULL,NULL,JSPROP_ENUMERATE);
651 652 653

	if((js_str=JS_NewStringCopyZ(cx,truncsp(msg.subj)))==NULL)
		return(JS_FALSE);
654
	JS_DefineProperty(cx, hdrobj, "subject"
655
		,STRING_TO_JSVAL(js_str)
656
		,NULL,NULL,JSPROP_ENUMERATE);
657 658
	if(msg.summary!=NULL 
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.summary)))!=NULL)
659
		JS_DefineProperty(cx, hdrobj, "summary"
660
			,STRING_TO_JSVAL(js_str)
661
			,NULL,NULL,JSPROP_ENUMERATE);
662 663
	if(msg.to_ext!=NULL 
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.to_ext)))!=NULL)
664
		JS_DefineProperty(cx, hdrobj, "to_ext"
665
			,STRING_TO_JSVAL(js_str)
666
			,NULL,NULL,JSPROP_ENUMERATE);
667 668
	if(msg.from_ext!=NULL 
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.from_ext)))!=NULL)
669
		JS_DefineProperty(cx, hdrobj, "from_ext"
670
			,STRING_TO_JSVAL(js_str)
671
			,NULL,NULL,JSPROP_ENUMERATE);
672 673
	if(msg.from_org!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.from_org)))!=NULL)
674
		JS_DefineProperty(cx, hdrobj, "from_org"
675
			,STRING_TO_JSVAL(js_str)
676
			,NULL,NULL,JSPROP_ENUMERATE);
677 678
	if(msg.replyto!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.replyto)))!=NULL)
679
		JS_DefineProperty(cx, hdrobj, "replyto"
680
			,STRING_TO_JSVAL(js_str)
681
			,NULL,NULL,JSPROP_ENUMERATE);
682 683
	if(msg.replyto_ext!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.replyto_ext)))!=NULL)
684
		JS_DefineProperty(cx, hdrobj, "replyto_ext"
685
			,STRING_TO_JSVAL(js_str)
686
			,NULL,NULL,JSPROP_ENUMERATE);
687 688
	if(msg.reverse_path!=NULL
		&& (js_str=JS_NewStringCopyZ(cx,truncsp(msg.reverse_path)))!=NULL)
689
		JS_DefineProperty(cx, hdrobj, "reverse_path"
690
			,STRING_TO_JSVAL(js_str)
691
			,NULL,NULL,JSPROP_ENUMERATE);
692

693 694 695 696 697 698 699 700 701
	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);
702

703 704 705
	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);
706
	if(msg.to_net.type
707
		&& (js_str=JS_NewStringCopyZ(cx,smb_netaddr(&msg.to_net)))!=NULL)
708
		JS_DefineProperty(cx, hdrobj, "to_net_addr"
709
			,STRING_TO_JSVAL(js_str)
710
			,NULL,NULL,JSPROP_ENUMERATE);
711

712 713 714
	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);
715
	if(msg.from_net.type
716
		&& (js_str=JS_NewStringCopyZ(cx,smb_netaddr(&msg.from_net)))!=NULL)
717
		JS_DefineProperty(cx, hdrobj, "from_net_addr"
718
			,STRING_TO_JSVAL(js_str)
719
			,NULL,NULL,JSPROP_ENUMERATE);
720

721 722 723 724

	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);
725
	if(msg.replyto_net.type
726
		&& (js_str=JS_NewStringCopyZ(cx,smb_netaddr(&msg.replyto_net)))!=NULL)
727
		JS_DefineProperty(cx, hdrobj, "replyto_net_addr"
728
			,STRING_TO_JSVAL(js_str)
729
			,NULL,NULL,JSPROP_ENUMERATE);
730

731 732 733 734 735 736 737 738 739 740 741
	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);
742 743 744 745 746 747 748 749 750 751 752

	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);
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	
	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);
	}
769

770
	/* Fixed length portion of msg header */
771
	JS_DefineProperty(cx, hdrobj, "type", INT_TO_JSVAL(msg.hdr.type)
772
		,NULL,NULL,JSPROP_ENUMERATE);
773
	JS_DefineProperty(cx, hdrobj, "version", INT_TO_JSVAL(msg.hdr.version)
774
		,NULL,NULL,JSPROP_ENUMERATE);
775
	JS_DefineProperty(cx, hdrobj, "attr", INT_TO_JSVAL(msg.hdr.attr)
776
		,NULL,NULL,JSPROP_ENUMERATE);
777 778 779 780 781 782 783 784 785 786 787 788 789 790
	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);

791 792 793
	JS_NewNumberValue(cx,msg.hdr.thread_back,&v);
	JS_DefineProperty(cx, hdrobj, "thread_back", v, NULL,NULL,JSPROP_ENUMERATE);
	JS_DefineProperty(cx, hdrobj, "thread_orig", v, NULL,NULL,0);
794 795 796 797 798 799 800 801 802 803 804 805 806 807
	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,