js_msgbase.c 72.7 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 2011 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
 *																			*
 * 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"
39
#include "js_request.h"
40 41 42

#ifdef JAVASCRIPT

43
static scfg_t* 		scfg=NULL;
44 45 46 47

typedef struct
{
	smb_t	smb;
48
	int		status;
49 50 51 52
	BOOL	debug;

} private_t;

53 54 55 56 57 58 59 60
typedef struct
{
	private_t	*p;
	BOOL		expand_fields;
	smbmsg_t	msg;

} privatemsg_t;

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

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/* 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 */

82
static JSBool
83
js_open(JSContext *cx, uintN argc, jsval *arglist)
84
{
85
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
86
	private_t* p;
deuce's avatar
deuce committed
87
	jsrefcount	rc;
88 89
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
90
		JS_ReportError(cx,getprivate_failure,WHERE);
91 92 93
		return(JS_FALSE);
	}

94
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
95 96 97 98 99 100 101 102

	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);
	}

103
	rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
104
	if((p->status=smb_open(&(p->smb)))!=SMB_SUCCESS) {
105
		JS_RESUMEREQUEST(cx, rc);
106
		return(JS_TRUE);
deuce's avatar
deuce committed
107
	}
108
	JS_RESUMEREQUEST(cx, rc);
109

110
	JS_SET_RVAL(cx, arglist, JSVAL_TRUE);
111 112 113 114
	return(JS_TRUE);
}


115
static JSBool
116
js_close(JSContext *cx, uintN argc, jsval *arglist)
117
{
118
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
119
	private_t* p;
deuce's avatar
deuce committed
120
	jsrefcount	rc;
121
	
122
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
123
		JS_ReportError(cx,getprivate_failure,WHERE);
124
		return(JS_FALSE);
125
	}
126

127 128
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

129
	rc=JS_SUSPENDREQUEST(cx);
130
	smb_close(&(p->smb));
131
	JS_RESUMEREQUEST(cx, rc);
132 133 134 135

	return(JS_TRUE);
}

136
static BOOL parse_recipient_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg)
137 138
{
	char*		cp;
139
	char		to[256];
140
	ushort		nettype=NET_UNKNOWN;
141 142 143 144
	ushort		agent;
	int32		i32;
	jsval		val;

145
	if(JS_GetProperty(cx, hdr, "to", &val) && !JSVAL_NULL_OR_VOID(val)) {
146
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
147
		if(cp==NULL)
148 149 150 151 152 153
			return(FALSE);
	} else {
		if(p->smb.status.attr&SMB_EMAIL)	/* e-mail */
			return(FALSE);					/* "to" property required */
		cp="All";
	}
deuce's avatar
deuce committed
154

155 156
	if((p->status=smb_hfield_str(msg, RECIPIENT, cp))!=SMB_SUCCESS)
		return(FALSE);
157 158 159 160 161 162
	if(!(p->smb.status.attr&SMB_EMAIL)) {
		SAFECOPY(to,cp);
		strlwr(to);
		msg->idx.to=crc16(to,0);
	}

163
	if(JS_GetProperty(cx, hdr, "to_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
164
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
165
		if(cp==NULL)
rswindell's avatar
rswindell committed
166
			return(FALSE);
167 168
		if((p->status=smb_hfield_str(msg, RECIPIENTEXT, cp))!=SMB_SUCCESS)
			return(FALSE);
rswindell's avatar
rswindell committed
169 170 171 172
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.to=atoi(cp);
	}

173
	if(JS_GetProperty(cx, hdr, "to_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
174
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
175
		if(cp==NULL)
rswindell's avatar
rswindell committed
176
			return(FALSE);
177 178
		if((p->status=smb_hfield_str(msg, RECIPIENTORG, cp))!=SMB_SUCCESS)
			return(FALSE);
rswindell's avatar
rswindell committed
179 180
	}

181
	if(JS_GetProperty(cx, hdr, "to_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
182 183 184 185
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
	}

186
	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
187
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
188
		if(cp==NULL)
rswindell's avatar
rswindell committed
189
			return(FALSE);
190 191
		if((p->status=smb_hfield_netaddr(msg, RECIPIENTNETADDR, cp, &nettype))!=SMB_SUCCESS)
			return(FALSE);
192 193 194 195 196
	}

	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.to=0;
197 198
		if((p->status=smb_hfield_bin(msg, RECIPIENTNETTYPE, nettype))!=SMB_SUCCESS)
			return(FALSE);
rswindell's avatar
rswindell committed
199 200
	}

201
	if(JS_GetProperty(cx, hdr, "to_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
202 203
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
204 205
		if((p->status=smb_hfield_bin(msg, RECIPIENTAGENT, agent))!=SMB_SUCCESS)
			return(FALSE);
rswindell's avatar
rswindell committed
206
	}
207 208 209 210 211 212 213 214

	return(TRUE);
}

static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg
								,BOOL recipient)
{
	char*		cp;
215
	char		from[256];
216
	ushort		nettype=NET_UNKNOWN;
217
	ushort		type;
218 219
	ushort		agent;
	int32		i32;
220
	jsval		val;
221 222 223
	JSObject*	array;
	JSObject*	field;
	jsuint		i,len;
224

225 226 227
	if(hdr==NULL)
		return(FALSE);

228 229 230
	if(recipient && !parse_recipient_object(cx,p,hdr,msg))
		return(FALSE);

231
	/* Required Header Fields */
232
	if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
233
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
234
		if(cp==NULL)
235 236 237
			return(FALSE);
	} else
		cp="";
238 239
	if((p->status=smb_hfield_str(msg, SUBJECT, cp))!=SMB_SUCCESS)
		return(FALSE);
240
	msg->idx.subj=smb_subject_crc(cp);
241

242
	if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
243
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
244
		if(cp==NULL)
245 246 247
			return(FALSE);
	} else
		return(FALSE);	/* "from" property required */
248 249
	if((p->status=smb_hfield_str(msg, SENDER, cp))!=SMB_SUCCESS)
		return(FALSE);
250
	if(!(p->smb.status.attr&SMB_EMAIL)) {
251 252
		SAFECOPY(from,cp);
		strlwr(from);
253
		msg->idx.from=crc16(from,0);
254
	}
255 256

	/* Optional Header Fields */
257
	if(JS_GetProperty(cx, hdr, "from_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
258
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
259
		if(cp==NULL)
260
			return(FALSE);
261 262
		if((p->status=smb_hfield_str(msg, SENDEREXT, cp))!=SMB_SUCCESS)
			return(FALSE);
263
		if(p->smb.status.attr&SMB_EMAIL)
264 265 266
			msg->idx.from=atoi(cp);
	}

267
	if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
268
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
269
		if(cp==NULL)
270
			return(FALSE);
271 272
		if((p->status=smb_hfield_str(msg, SENDERORG, cp))!=SMB_SUCCESS)
			return(FALSE);
273 274
	}

275
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
276 277
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
278 279
	}

280
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
281
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
282
		if(cp==NULL)
283
			return(FALSE);
284 285
		if((p->status=smb_hfield_netaddr(msg, SENDERNETADDR, cp, &nettype))!=SMB_SUCCESS)
			return(FALSE);
286 287 288 289 290
	}
	
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.from=0;
291 292
		if((p->status=smb_hfield_bin(msg, SENDERNETTYPE, nettype))!=SMB_SUCCESS)
			return(FALSE);
293 294
	}

295
	if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
296 297
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
298 299
		if((p->status=smb_hfield_bin(msg, SENDERAGENT, agent))!=SMB_SUCCESS)
			return(FALSE);
300 301
	}

302
	if(JS_GetProperty(cx, hdr, "from_ip_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
303
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
304
		if(cp==NULL)
305
			return(FALSE);
306 307
		if((p->status=smb_hfield_str(msg, SENDERIPADDR, cp))!=SMB_SUCCESS)
			return(FALSE);
308 309
	}

310
	if(JS_GetProperty(cx, hdr, "from_host_name", &val) && !JSVAL_NULL_OR_VOID(val)) {
311
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
312
		if(cp==NULL)
313
			return(FALSE);
314 315
		if((p->status=smb_hfield_str(msg, SENDERHOSTNAME, cp))!=SMB_SUCCESS)
			return(FALSE);
316 317
	}

318
	if(JS_GetProperty(cx, hdr, "from_protocol", &val) && !JSVAL_NULL_OR_VOID(val)) {
319
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
320
		if(cp==NULL)
321
			return(FALSE);
322 323
		if((p->status=smb_hfield_str(msg, SENDERPROTOCOL, cp))!=SMB_SUCCESS)
			return(FALSE);
324 325
	}

326
	if(JS_GetProperty(cx, hdr, "from_port", &val) && !JSVAL_NULL_OR_VOID(val)) {
327
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
328
		if(cp==NULL)
329 330
			return(FALSE);
		if((p->status=smb_hfield_str(msg, SENDERPORT, cp))!=SMB_SUCCESS)
331
			return(FALSE);
332 333
	}

334
	if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
335
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
336
		if(cp==NULL)
337
			return(FALSE);
338 339
		if((p->status=smb_hfield_str(msg, REPLYTO, cp))!=SMB_SUCCESS)
			return(FALSE);
340 341
	}

342
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
343
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
344
		if(cp==NULL)
345
			return(FALSE);
346 347
		if((p->status=smb_hfield_str(msg, REPLYTOEXT, cp))!=SMB_SUCCESS)
			return(FALSE);
348 349
	}

350
	if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
351
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
352
		if(cp==NULL)
353
			return(FALSE);
354 355
		if((p->status=smb_hfield_str(msg, REPLYTOORG, cp))!=SMB_SUCCESS)
			return(FALSE);
356 357
	}

358
	nettype=NET_UNKNOWN;
359
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
360 361
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
362
	}
363
	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
364
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
365
		if(cp==NULL)
366
			return(FALSE);
367 368 369 370 371 372
		if((p->status=smb_hfield_netaddr(msg, REPLYTONETADDR, cp, &nettype))!=SMB_SUCCESS)
			return(FALSE);
	}
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if((p->status=smb_hfield_bin(msg, REPLYTONETTYPE, nettype))!=SMB_SUCCESS)
			return(FALSE);
373 374
	}

375
	if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
376 377
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
378 379
		if((p->status=smb_hfield_bin(msg, REPLYTOAGENT, agent))!=SMB_SUCCESS)
			return(FALSE);
380 381
	}

382
	/* RFC822 headers */
383
	if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
384
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
385
		if(cp==NULL)
386
			return(FALSE);
387 388
		if((p->status=smb_hfield_str(msg, RFC822MSGID, cp))!=SMB_SUCCESS)
			return(FALSE);
389 390
	}

391
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
392
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
393
		if(cp==NULL)
394
			return(FALSE);
395 396
		if((p->status=smb_hfield_str(msg, RFC822REPLYID, cp))!=SMB_SUCCESS)
			return(FALSE);
397 398
	}

399
	/* SMTP headers */
400
	if(JS_GetProperty(cx, hdr, "reverse_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
401
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
402
		if(cp==NULL)
403
			return(FALSE);
404 405
		if((p->status=smb_hfield_str(msg, SMTPREVERSEPATH, cp))!=SMB_SUCCESS)
			return(FALSE);
406 407
	}

408
	if(JS_GetProperty(cx, hdr, "forward_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
409
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
410
		if(cp==NULL)
411 412 413 414 415
			return(FALSE);
		if((p->status=smb_hfield_str(msg, SMTPFORWARDPATH, cp))!=SMB_SUCCESS)
			return(FALSE);
	}

416
	/* USENET headers */
417
	if(JS_GetProperty(cx, hdr, "path", &val) && !JSVAL_NULL_OR_VOID(val)) {
418
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
419
		if(cp==NULL)
420
			return(FALSE);
421 422
		if((p->status=smb_hfield_str(msg, USENETPATH, cp))!=SMB_SUCCESS)
			return(FALSE);
423 424
	}

425
	if(JS_GetProperty(cx, hdr, "newsgroups", &val) && !JSVAL_NULL_OR_VOID(val)) {
426
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
427
		if(cp==NULL)
428
			return(FALSE);
429 430
		if((p->status=smb_hfield_str(msg, USENETNEWSGROUPS, cp))!=SMB_SUCCESS)
			return(FALSE);
431 432 433
	}

	/* FTN headers */
434
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && !JSVAL_NULL_OR_VOID(val)) {
435
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
436
		if(cp==NULL)
437
			return(FALSE);
438 439
		if((p->status=smb_hfield_str(msg, FIDOMSGID, cp))!=SMB_SUCCESS)
			return(FALSE);
440 441
	}

442
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && !JSVAL_NULL_OR_VOID(val)) {
443
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
444
		if(cp==NULL)
445
			return(FALSE);
446 447
		if((p->status=smb_hfield_str(msg, FIDOREPLYID, cp))!=SMB_SUCCESS)
			return(FALSE);
448 449
	}

450
	if(JS_GetProperty(cx, hdr, "ftn_area", &val) && !JSVAL_NULL_OR_VOID(val)) {
451
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
452
		if(cp==NULL)
453
			return(FALSE);
454 455
		if((p->status=smb_hfield_str(msg, FIDOAREA, cp))!=SMB_SUCCESS)
			return(FALSE);
456 457
	}

458
	if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && !JSVAL_NULL_OR_VOID(val)) {
459
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
460
		if(cp==NULL)
461
			return(FALSE);
462 463
		if((p->status=smb_hfield_str(msg, FIDOFLAGS, cp))!=SMB_SUCCESS)
			return(FALSE);
464 465
	}

466
	if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && !JSVAL_NULL_OR_VOID(val)) {
467
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
468
		if(cp==NULL)
469
			return(FALSE);
470 471
		if((p->status=smb_hfield_str(msg, FIDOPID, cp))!=SMB_SUCCESS)
			return(FALSE);
472 473
	}

474
	if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && !JSVAL_NULL_OR_VOID(val)) {
475
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
476
		if(cp==NULL)
477
			return(FALSE);
478 479
		if((p->status=smb_hfield_str(msg, FIDOTID, cp))!=SMB_SUCCESS)
			return(FALSE);
480 481
	}

482
	if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
483
		JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
484
		if(cp==NULL)
485 486 487
			return(FALSE);
		msg->hdr.when_written=rfc822date(cp);
	}
488 489
	
	/* Numeric Header Fields */
490
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
491 492
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.attr=(ushort)i32;
493 494
		msg->idx.attr=msg->hdr.attr;
	}
495
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
496 497 498
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.auxattr=i32;
	}
499
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
500 501 502
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.netattr=i32;
	}
503
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val))  {
504 505 506
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.time=i32;
	}
507
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
508 509 510
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.zone=(short)i32;
	}
511
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
512 513 514
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.time=i32;
	}
515
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
516 517 518 519
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.zone=(short)i32;
	}

520 521
	if((JS_GetProperty(cx, hdr, "thread_orig", &val) 
		|| JS_GetProperty(cx, hdr, "thread_back", &val)) && !JSVAL_NULL_OR_VOID(val)) {
522
		JS_ValueToInt32(cx,val,&i32);
523
		msg->hdr.thread_back=i32;
524
	}
525
	if(JS_GetProperty(cx, hdr, "thread_next", &val) && !JSVAL_NULL_OR_VOID(val)) {
526 527 528
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_next=i32;
	}
529
	if(JS_GetProperty(cx, hdr, "thread_first", &val) && !JSVAL_NULL_OR_VOID(val)) {
530 531 532
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_first=i32;
	}
533

534 535 536
	if(JS_GetProperty(cx, hdr, "field_list", &val) && JSVAL_IS_OBJECT(val)) {
		array=JSVAL_TO_OBJECT(val);
		len=0;
537 538
		if(!JS_GetArrayLength(cx, array, &len))
			return(FALSE);
539 540 541 542 543 544 545

		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);
546
			if(!JS_GetProperty(cx, field, "type", &val))
547
				continue;
deuce's avatar
deuce committed
548
			if(JSVAL_IS_STRING(val)) {
549
				JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
550 551
				type=smb_hfieldtypelookup(cp);
			}
552 553 554 555
			else {
				JS_ValueToInt32(cx,val,&i32);
				type=(ushort)i32;
			}
556
			if(!JS_GetProperty(cx, field, "data", &val))
557
				continue;
558
			JSVALUE_TO_STRING(cx, val, cp, NULL);
deuce's avatar
deuce committed
559
			if(cp==NULL)
560
				return(FALSE);
561 562
			if((p->status=smb_hfield_str(msg, type, cp))!=SMB_SUCCESS)
				return(FALSE);
563 564 565
		}
	}

566 567 568 569 570 571 572 573 574
	if(msg->hdr.number==0 && JS_GetProperty(cx, hdr, "number", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.number=i32;
	}

	return(TRUE);
}

/* obj must've been previously returned from get_msg_header() */
575
BOOL DLLCALL js_ParseMsgHeaderObject(JSContext* cx, JSObject* obj, smbmsg_t* msg)
576 577 578 579 580 581 582 583 584 585 586 587 588
{
	private_t*	p;

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

	if(!parse_header_object(cx, p, obj, msg, /* recipient */ TRUE)) {
		smb_freemsgmem(msg);
		return(FALSE);
	}

589 590 591
	return(TRUE);
}

deuce's avatar
deuce committed
592
static BOOL msg_offset_by_id(private_t* p, char* id, int32_t* offset)
593 594 595
{
	smbmsg_t msg;

596
	if((p->status=smb_getmsgidx_by_msgid(&(p->smb),&msg,id))!=SMB_SUCCESS)
597 598 599 600 601 602
		return(FALSE);

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

603
static JSBool
604
js_get_msg_index(JSContext *cx, uintN argc, jsval *arglist)
605
{
606 607
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
608
	uintN		n;
609
	jsval		val;
610 611 612 613
	smbmsg_t	msg;
	JSObject*	idxobj;
	JSBool		by_offset=JS_FALSE;
	private_t*	p;
deuce's avatar
deuce committed
614
	jsrefcount	rc;
615
	JSObject*	proto;
616

617
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);
618 619 620 621 622 623 624 625 626 627 628 629
	
	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++) {
630
		if(JSVAL_IS_BOOLEAN(argv[n])) {
631
			by_offset=JSVAL_TO_BOOLEAN(argv[n]);
632
		}
633 634 635 636 637 638 639 640 641
		else if(JSVAL_IS_NUMBER(argv[n])) {
			if(by_offset) {							/* Get by offset */
				if(!JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset))
					return JS_FALSE;
			}
			else {									/* Get by number */
				if(!JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number))
					return JS_FALSE;
			}
642

643
			rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
644
			if((p->status=smb_getmsgidx(&(p->smb), &msg))!=SMB_SUCCESS) {
645
				JS_RESUMEREQUEST(cx, rc);
646
				return(JS_TRUE);
deuce's avatar
deuce committed
647
			}
648
			JS_RESUMEREQUEST(cx, rc);
649 650 651 652 653

			break;
		}
	}

654 655 656 657 658 659 660 661 662 663 664
	if(JS_GetProperty(cx, JS_GetGlobalObject(cx), "MsgBase", &val) && !JSVAL_NULL_OR_VOID(val)) {
		JS_ValueToObject(cx,val,&proto);
		if(JS_GetProperty(cx, proto, "IndexPrototype", &val) && !JSVAL_NULL_OR_VOID(val))
			JS_ValueToObject(cx,val,&proto);
		else
			proto=NULL;
	}
	else
		proto=NULL;

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

667
	val=UINT_TO_JSVAL(msg.idx.number);
668
	JS_DefineProperty(cx, idxobj, "number"	,val
669
		,NULL,NULL,JSPROP_ENUMERATE);
670

671
	val=UINT_TO_JSVAL(msg.idx.to);
672
	JS_DefineProperty(cx, idxobj, "to"		,val
673
		,NULL,NULL,JSPROP_ENUMERATE);
674

675
	val=UINT_TO_JSVAL(msg.idx.from);
676
	JS_DefineProperty(cx, idxobj, "from"	,val
677
		,NULL,NULL,JSPROP_ENUMERATE);
678

679
	val=UINT_TO_JSVAL(msg.idx.subj);
680
	JS_DefineProperty(cx, idxobj, "subject"	,val
681
		,NULL,NULL,JSPROP_ENUMERATE);
682

683
	val=UINT_TO_JSVAL(msg.idx.attr);
684
	JS_DefineProperty(cx, idxobj, "attr"	,val
685
		,NULL,NULL,JSPROP_ENUMERATE);
686

687
	val=UINT_TO_JSVAL(msg.offset);
688
	JS_DefineProperty(cx, idxobj, "offset"	,val
689
		,NULL,NULL,JSPROP_ENUMERATE);
690

691
	val=UINT_TO_JSVAL(msg.idx.time);
692
	JS_DefineProperty(cx, idxobj, "time"	,val
693
		,NULL,NULL,JSPROP_ENUMERATE);
694

695
	JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(idxobj));
696 697 698 699

	return(JS_TRUE);
}

700
#define LAZY_INTEGER(PropName, PropValue, flags) \
701 702 703 704 705 706 707
	if(name==NULL || strcmp(name, (PropName))==0) { \
		v=INT_TO_JSVAL((PropValue)); \
		JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
		if(name) return(JS_TRUE); \
	}

#define LAZY_UINTEGER(PropName, PropValue, flags) \
deuce's avatar
deuce committed
708
	if(name==NULL || strcmp(name, (PropName))==0) { \
709
		v=UINT_TO_JSVAL((PropValue)); \
710
		JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
deuce's avatar
deuce committed
711 712 713
		if(name) return(JS_TRUE); \
	}

714
#define LAZY_UINTEGER_EXPAND(PropName, PropValue, flags) \
deuce's avatar
deuce committed
715 716
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if(p->expand_fields || (PropValue)) { \
717
			v=UINT_TO_JSVAL((PropValue)); \
718
			JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
deuce's avatar
deuce committed
719 720 721 722 723
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

724
#define LAZY_UINTEGER_COND(PropName, Condition, PropValue, flags) \
deuce's avatar
deuce committed
725 726
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if(Condition) { \
727
			v=UINT_TO_JSVAL((uint32_t)(PropValue)); \
728
			JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
deuce's avatar
deuce committed
729 730 731 732 733
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

734
#define LAZY_STRING(PropName, PropValue, flags) \
deuce's avatar
deuce committed
735 736
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if((js_str=JS_NewStringCopyZ(cx, (PropValue)))!=NULL) { \
737
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
738 739 740 741 742
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

743
#define LAZY_STRING_TRUNCSP(PropName, PropValue, flags) \
deuce's avatar
deuce committed
744 745
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if((js_str=JS_NewStringCopyZ(cx, truncsp(PropValue)))!=NULL) { \
746
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
747 748 749 750 751
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

752
#define LAZY_STRING_COND(PropName, Condition, PropValue, flags) \
deuce's avatar
deuce committed
753
	if(name==NULL || strcmp(name, (PropName))==0) { \
754
		if((Condition) && (js_str=JS_NewStringCopyZ(cx, (PropValue)))!=NULL) { \
755
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
756 757 758 759 760
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}