js_msgbase.c 72.1 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 86
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
87
	private_t* p;
deuce's avatar
deuce committed
88
	jsrefcount	rc;
89 90
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
91
		JS_ReportError(cx,getprivate_failure,WHERE);
92 93 94
		return(JS_FALSE);
	}

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

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

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

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


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

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 147 148 149 150 151 152
		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";
	}
deuce's avatar
deuce committed
153

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

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

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

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

183
	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
rswindell's avatar
rswindell committed
184 185
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
186 187
		if((p->status=smb_hfield_netaddr(msg, RECIPIENTNETADDR, cp, &nettype))!=SMB_SUCCESS)
			return(FALSE);
188 189 190 191 192
	}

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

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

	return(TRUE);
}

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

221 222 223
	if(hdr==NULL)
		return(FALSE);

224 225 226
	if(recipient && !parse_recipient_object(cx,p,hdr,msg))
		return(FALSE);

227
	/* Required Header Fields */
228
	if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
229
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
230 231 232
			return(FALSE);
	} else
		cp="";
233 234
	if((p->status=smb_hfield_str(msg, SUBJECT, cp))!=SMB_SUCCESS)
		return(FALSE);
235
	msg->idx.subj=smb_subject_crc(cp);
236

237
	if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
238
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
239 240 241
			return(FALSE);
	} else
		return(FALSE);	/* "from" property required */
242 243
	if((p->status=smb_hfield_str(msg, SENDER, cp))!=SMB_SUCCESS)
		return(FALSE);
244
	if(!(p->smb.status.attr&SMB_EMAIL)) {
245 246
		SAFECOPY(from,cp);
		strlwr(from);
247
		msg->idx.from=crc16(from,0);
248
	}
249 250

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

260
	if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
261
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
262
			return(FALSE);
263 264
		if((p->status=smb_hfield_str(msg, SENDERORG, cp))!=SMB_SUCCESS)
			return(FALSE);
265 266
	}

267
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
268 269
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
270 271
	}

272
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
273
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
274
			return(FALSE);
275 276
		if((p->status=smb_hfield_netaddr(msg, SENDERNETADDR, cp, &nettype))!=SMB_SUCCESS)
			return(FALSE);
277 278 279 280 281
	}
	
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.from=0;
282 283
		if((p->status=smb_hfield_bin(msg, SENDERNETTYPE, nettype))!=SMB_SUCCESS)
			return(FALSE);
284 285
	}

286
	if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
287 288
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
289 290
		if((p->status=smb_hfield_bin(msg, SENDERAGENT, agent))!=SMB_SUCCESS)
			return(FALSE);
291 292
	}

293
	if(JS_GetProperty(cx, hdr, "from_ip_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
294 295
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
296 297
		if((p->status=smb_hfield_str(msg, SENDERIPADDR, cp))!=SMB_SUCCESS)
			return(FALSE);
298 299
	}

300
	if(JS_GetProperty(cx, hdr, "from_host_name", &val) && !JSVAL_NULL_OR_VOID(val)) {
301 302
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
303 304
		if((p->status=smb_hfield_str(msg, SENDERHOSTNAME, cp))!=SMB_SUCCESS)
			return(FALSE);
305 306
	}

307
	if(JS_GetProperty(cx, hdr, "from_protocol", &val) && !JSVAL_NULL_OR_VOID(val)) {
308 309
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
310 311
		if((p->status=smb_hfield_str(msg, SENDERPROTOCOL, cp))!=SMB_SUCCESS)
			return(FALSE);
312 313
	}

314
	if(JS_GetProperty(cx, hdr, "from_port", &val) && !JSVAL_NULL_OR_VOID(val)) {
315 316 317
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		if((p->status=smb_hfield_str(msg, SENDERPORT, cp))!=SMB_SUCCESS)
318
			return(FALSE);
319 320
	}

321
	if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
322
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
323
			return(FALSE);
324 325
		if((p->status=smb_hfield_str(msg, REPLYTO, cp))!=SMB_SUCCESS)
			return(FALSE);
326 327
	}

328
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
329
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
330
			return(FALSE);
331 332
		if((p->status=smb_hfield_str(msg, REPLYTOEXT, cp))!=SMB_SUCCESS)
			return(FALSE);
333 334
	}

335
	if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
336 337
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
338 339
		if((p->status=smb_hfield_str(msg, REPLYTOORG, cp))!=SMB_SUCCESS)
			return(FALSE);
340 341
	}

342
	nettype=NET_UNKNOWN;
343
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
344 345
		JS_ValueToInt32(cx,val,&i32);
		nettype=(ushort)i32;
346
	}
347
	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
348
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
349
			return(FALSE);
350 351 352 353 354 355
		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);
356 357
	}

358
	if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
359 360
		JS_ValueToInt32(cx,val,&i32);
		agent=(ushort)i32;
361 362
		if((p->status=smb_hfield_bin(msg, REPLYTOAGENT, agent))!=SMB_SUCCESS)
			return(FALSE);
363 364
	}

365
	/* RFC822 headers */
366
	if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
367
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
368
			return(FALSE);
369 370
		if((p->status=smb_hfield_str(msg, RFC822MSGID, cp))!=SMB_SUCCESS)
			return(FALSE);
371 372
	}

373
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
374
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
375
			return(FALSE);
376 377
		if((p->status=smb_hfield_str(msg, RFC822REPLYID, cp))!=SMB_SUCCESS)
			return(FALSE);
378 379
	}

380
	/* SMTP headers */
381
	if(JS_GetProperty(cx, hdr, "reverse_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
382
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
383
			return(FALSE);
384 385
		if((p->status=smb_hfield_str(msg, SMTPREVERSEPATH, cp))!=SMB_SUCCESS)
			return(FALSE);
386 387
	}

388 389 390 391 392 393 394
	if(JS_GetProperty(cx, hdr, "forward_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
			return(FALSE);
		if((p->status=smb_hfield_str(msg, SMTPFORWARDPATH, cp))!=SMB_SUCCESS)
			return(FALSE);
	}

395
	/* USENET headers */
396
	if(JS_GetProperty(cx, hdr, "path", &val) && !JSVAL_NULL_OR_VOID(val)) {
397
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
398
			return(FALSE);
399 400
		if((p->status=smb_hfield_str(msg, USENETPATH, cp))!=SMB_SUCCESS)
			return(FALSE);
401 402
	}

403
	if(JS_GetProperty(cx, hdr, "newsgroups", &val) && !JSVAL_NULL_OR_VOID(val)) {
404
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
405
			return(FALSE);
406 407
		if((p->status=smb_hfield_str(msg, USENETNEWSGROUPS, cp))!=SMB_SUCCESS)
			return(FALSE);
408 409 410
	}

	/* FTN headers */
411
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && !JSVAL_NULL_OR_VOID(val)) {
412
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
413
			return(FALSE);
414 415
		if((p->status=smb_hfield_str(msg, FIDOMSGID, cp))!=SMB_SUCCESS)
			return(FALSE);
416 417
	}

418
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && !JSVAL_NULL_OR_VOID(val)) {
419
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
420
			return(FALSE);
421 422
		if((p->status=smb_hfield_str(msg, FIDOREPLYID, cp))!=SMB_SUCCESS)
			return(FALSE);
423 424
	}

425
	if(JS_GetProperty(cx, hdr, "ftn_area", &val) && !JSVAL_NULL_OR_VOID(val)) {
426
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
427
			return(FALSE);
428 429
		if((p->status=smb_hfield_str(msg, FIDOAREA, cp))!=SMB_SUCCESS)
			return(FALSE);
430 431
	}

432
	if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && !JSVAL_NULL_OR_VOID(val)) {
433
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
434
			return(FALSE);
435 436
		if((p->status=smb_hfield_str(msg, FIDOFLAGS, cp))!=SMB_SUCCESS)
			return(FALSE);
437 438
	}

439
	if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && !JSVAL_NULL_OR_VOID(val)) {
440
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
441
			return(FALSE);
442 443
		if((p->status=smb_hfield_str(msg, FIDOPID, cp))!=SMB_SUCCESS)
			return(FALSE);
444 445
	}

446
	if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && !JSVAL_NULL_OR_VOID(val)) {
447
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
448
			return(FALSE);
449 450
		if((p->status=smb_hfield_str(msg, FIDOTID, cp))!=SMB_SUCCESS)
			return(FALSE);
451 452
	}

453
	if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
454
		if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
455 456 457
			return(FALSE);
		msg->hdr.when_written=rfc822date(cp);
	}
458 459
	
	/* Numeric Header Fields */
460
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
461 462
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.attr=(ushort)i32;
463 464
		msg->idx.attr=msg->hdr.attr;
	}
465
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
466 467 468
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.auxattr=i32;
	}
469
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
470 471 472
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.netattr=i32;
	}
473
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val))  {
474 475 476
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.time=i32;
	}
477
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
478 479 480
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_written.zone=(short)i32;
	}
481
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
482 483 484
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.time=i32;
	}
485
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
486 487 488 489
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.when_imported.zone=(short)i32;
	}

490 491
	if((JS_GetProperty(cx, hdr, "thread_orig", &val) 
		|| JS_GetProperty(cx, hdr, "thread_back", &val)) && !JSVAL_NULL_OR_VOID(val)) {
492
		JS_ValueToInt32(cx,val,&i32);
493
		msg->hdr.thread_back=i32;
494
	}
495
	if(JS_GetProperty(cx, hdr, "thread_next", &val) && !JSVAL_NULL_OR_VOID(val)) {
496 497 498
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_next=i32;
	}
499
	if(JS_GetProperty(cx, hdr, "thread_first", &val) && !JSVAL_NULL_OR_VOID(val)) {
500 501 502
		JS_ValueToInt32(cx,val,&i32);
		msg->hdr.thread_first=i32;
	}
503

504 505 506
	if(JS_GetProperty(cx, hdr, "field_list", &val) && JSVAL_IS_OBJECT(val)) {
		array=JSVAL_TO_OBJECT(val);
		len=0;
507 508
		if(!JS_GetArrayLength(cx, array, &len))
			return(FALSE);
509 510 511 512 513 514 515

		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);
516
			if(!JS_GetProperty(cx, field, "type", &val))
517
				continue;
518 519 520 521 522 523
			if(JSVAL_IS_STRING(val))
				type=smb_hfieldtypelookup(JS_GetStringBytes(JS_ValueToString(cx,val)));
			else {
				JS_ValueToInt32(cx,val,&i32);
				type=(ushort)i32;
			}
524
			if(!JS_GetProperty(cx, field, "data", &val))
525
				continue;
526
			if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
527
				return(FALSE);
528 529
			if((p->status=smb_hfield_str(msg, type, cp))!=SMB_SUCCESS)
				return(FALSE);
530 531 532
		}
	}

533 534 535 536 537 538 539 540 541
	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() */
542
BOOL DLLCALL js_ParseMsgHeaderObject(JSContext* cx, JSObject* obj, smbmsg_t* msg)
543 544 545 546 547 548 549 550 551 552 553 554 555
{
	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);
	}

556 557 558
	return(TRUE);
}

deuce's avatar
deuce committed
559
static BOOL msg_offset_by_id(private_t* p, char* id, int32_t* offset)
560 561 562
{
	smbmsg_t msg;

563
	if((p->status=smb_getmsgidx_by_msgid(&(p->smb),&msg,id))!=SMB_SUCCESS)
564 565 566 567 568 569
		return(FALSE);

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

570
static JSBool
571
js_get_msg_index(JSContext *cx, uintN argc, jsval *arglist)
572
{
573 574
	JSObject *obj=JS_THIS_OBJECT(cx, arglist);
	jsval *argv=JS_ARGV(cx, arglist);
575
	uintN		n;
576
	jsval		val;
577 578 579 580
	smbmsg_t	msg;
	JSObject*	idxobj;
	JSBool		by_offset=JS_FALSE;
	private_t*	p;
deuce's avatar
deuce committed
581
	jsrefcount	rc;
582
	JSObject*	proto;
583

584
	JS_SET_RVAL(cx, arglist, JSVAL_NULL);
585 586 587 588 589 590 591 592 593 594 595 596
	
	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++) {
597
		if(JSVAL_IS_BOOLEAN(argv[n])) {
598
			by_offset=JSVAL_TO_BOOLEAN(argv[n]);
599
		}
600
		else if(JSVAL_IS_NUM(argv[n])) {
601
			if(by_offset)							/* Get by offset */
602
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
603
			else									/* Get by number */
604
				JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);
605

606
			rc=JS_SUSPENDREQUEST(cx);
deuce's avatar
deuce committed
607
			if((p->status=smb_getmsgidx(&(p->smb), &msg))!=SMB_SUCCESS) {
608
				JS_RESUMEREQUEST(cx, rc);
609
				return(JS_TRUE);
deuce's avatar
deuce committed
610
			}
611
			JS_RESUMEREQUEST(cx, rc);
612 613 614 615 616

			break;
		}
	}

617 618 619 620 621 622 623 624 625 626 627
	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)
628 629
		return(JS_TRUE);

630
	val=UINT_TO_JSVAL(msg.idx.number);
631
	JS_DefineProperty(cx, idxobj, "number"	,val
632
		,NULL,NULL,JSPROP_ENUMERATE);
633

634
	val=UINT_TO_JSVAL(msg.idx.to);
635
	JS_DefineProperty(cx, idxobj, "to"		,val
636
		,NULL,NULL,JSPROP_ENUMERATE);
637

638
	val=UINT_TO_JSVAL(msg.idx.from);
639
	JS_DefineProperty(cx, idxobj, "from"	,val
640
		,NULL,NULL,JSPROP_ENUMERATE);
641

642
	val=UINT_TO_JSVAL(msg.idx.subj);
643
	JS_DefineProperty(cx, idxobj, "subject"	,val
644
		,NULL,NULL,JSPROP_ENUMERATE);
645

646
	val=UINT_TO_JSVAL(msg.idx.attr);
647
	JS_DefineProperty(cx, idxobj, "attr"	,val
648
		,NULL,NULL,JSPROP_ENUMERATE);
649

650
	val=UINT_TO_JSVAL(msg.offset);
651
	JS_DefineProperty(cx, idxobj, "offset"	,val
652
		,NULL,NULL,JSPROP_ENUMERATE);
653

654
	val=UINT_TO_JSVAL(msg.idx.time);
655
	JS_DefineProperty(cx, idxobj, "time"	,val
656
		,NULL,NULL,JSPROP_ENUMERATE);
657

658
	JS_SET_RVAL(cx, arglist, OBJECT_TO_JSVAL(idxobj));
659 660 661 662

	return(JS_TRUE);
}

663
#define LAZY_INTEGER(PropName, PropValue, flags) \
deuce's avatar
deuce committed
664
	if(name==NULL || strcmp(name, (PropName))==0) { \
665
		v=UINT_TO_JSVAL((PropValue)); \
666
		JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
deuce's avatar
deuce committed
667 668 669
		if(name) return(JS_TRUE); \
	}

670
#define LAZY_INTEGER_EXPAND(PropName, PropValue, flags) \
deuce's avatar
deuce committed
671 672
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if(p->expand_fields || (PropValue)) { \
673
			v=UINT_TO_JSVAL((PropValue)); \
674
			JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
deuce's avatar
deuce committed
675 676 677 678 679
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

680
#define LAZY_INTEGER_COND(PropName, Condition, PropValue, flags) \
deuce's avatar
deuce committed
681 682
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if(Condition) { \
683
			v=UINT_TO_JSVAL((PropValue)); \
684
			JS_DefineProperty(cx, obj, (PropName), v, NULL,NULL,flags); \
deuce's avatar
deuce committed
685 686 687 688 689
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

690
#define LAZY_STRING(PropName, PropValue, flags) \
deuce's avatar
deuce committed
691 692
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if((js_str=JS_NewStringCopyZ(cx, (PropValue)))!=NULL) { \
693
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
694 695 696 697 698
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

699
#define LAZY_STRING_TRUNCSP(PropName, PropValue, flags) \
deuce's avatar
deuce committed
700 701
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if((js_str=JS_NewStringCopyZ(cx, truncsp(PropValue)))!=NULL) { \
702
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
703 704 705 706 707
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

708
#define LAZY_STRING_COND(PropName, Condition, PropValue, flags) \
deuce's avatar
deuce committed
709
	if(name==NULL || strcmp(name, (PropName))==0) { \
710
		if((Condition) && (js_str=JS_NewStringCopyZ(cx, (PropValue)))!=NULL) { \
711
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
712 713 714 715 716
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

717
#define LAZY_STRING_TRUNCSP_NULL(PropName, PropValue, flags) \
deuce's avatar
deuce committed
718 719
	if(name==NULL || strcmp(name, (PropName))==0) { \
		if((PropValue) != NULL && (js_str=JS_NewStringCopyZ(cx, truncsp(PropValue)))!=NULL) { \
720
			JS_DefineProperty(cx, obj, PropName, STRING_TO_JSVAL(js_str), NULL, NULL, flags); \
deuce's avatar
deuce committed
721 722 723 724 725
			if(name) return(JS_TRUE); \
		} \
		else if(name) return(JS_TRUE); \
	}

726
static JSBool js_get_msg_header_resolve(JSContext *cx, JSObject *obj, jsid id)
727 728 729 730
{
	char			date[128];
	char			msg_id[256];
	char			reply_id[256];
731
	char			tmp[128];
732 733 734 735 736 737 738 739 740 741
	char*			val;
	int				i;
	smbmsg_t		remsg;
	JSObject*		array;
	JSObject*		field;
	JSString*		js_str;
	jsint			items;
	jsval			v;
	privatemsg_t*	p;
	char*			name=NULL;
deuce's avatar
deuce committed
742
	jsrefcount		rc;
743

deuce's avatar
deuce committed
744 745 746 747
	if(id != JSID_VOID && id != JSID_EMPTY) {
		jsval idval;
		
		JS_IdToValue(cx, id, &idval);
748
		name=JS_GetStringBytes(JSVAL_TO_STRING(idval));
deuce's avatar
deuce committed
749
	}
750 751 752 753 754 755 756 757

	/* If we have already enumerated, we're done here... */
	if((p=(privatemsg_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_TRUE);

	if((p->msg).hdr.number==0) /* No valid message number/id/offset specified */
		return(JS_TRUE);

758
	LAZY_INTEGER("number", p->msg.hdr.number, JSPROP_ENUMERATE);
deuce's avatar
deuce committed
759 760 761
	LAZY_INTEGER("offset", p->msg.offset, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP("to",p->msg.to, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP("from",p->msg.from, JSPROP_ENUMERATE);
762 763 764 765 766 767 768 769 770 771 772 773 774
	LAZY_STRING_TRUNCSP("subject",p->msg.subj, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("summary", p->msg.summary, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("to_ext", p->msg.to_ext, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("from_ext", p->msg.from_ext, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("from_org", p->msg.from_org, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("replyto", p->msg.replyto, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("replyto_ext", p->msg.replyto_ext, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("reverse_path", p->msg.reverse_path, JSPROP_ENUMERATE);
	LAZY_STRING_TRUNCSP_NULL("forward_path", p->msg.forward_path, JSPROP_ENUMERATE);
	LAZY_INTEGER_EXPAND("to_agent", p->msg.to_agent, JSPROP_ENUMERATE);
	LAZY_INTEGER_EXPAND("from_agent", p->msg.from_agent, JSPROP_ENUMERATE);
	LAZY_INTEGER_EXPAND("replyto_agent", p->msg.replyto_agent, JSPROP_ENUMERATE);
	LAZY_INTEGER_EXPAND("to_net_type", p->msg.to_net.type, JSPROP_ENUMERATE);
775
	LAZY_STRING_COND("to_net_addr", p->msg.to_net.type && p->msg.to_net.addr, smb_netaddrstr(&(p->msg).to_net,tmp), JSPROP_ENUMERATE);
776
	LAZY_INTEGER_EXPAND("from_net_type", p->msg.from_net.type, JSPROP_ENUMERATE);
777
	/* exception here because p->msg.from_net is NULL */
778
	LAZY_STRING_COND("from_net_addr", p->msg.from_net.type && p->msg.from_net.addr, smb_netaddrstr(&(p->msg).from_net,tmp), JSPROP_ENUMERATE);
779
	LAZY_INTEGER_EXPAND("replyto_net_type", p->msg.replyto_net.type, JSPROP_ENUMERATE);
780
	LAZY_STRING_COND("replyto_net_addr", p->msg.replyto_net.type && p->msg.replyto_net.addr, smb_netaddrstr(&(p->msg).replyto_net,tmp), JSPROP_ENUMERATE);
781 782 783 784 785 786 787 788
	LAZY_STRING_COND("from_ip_addr", (val=smb_get_hfield(&(p->msg),SENDERIPADDR,NULL))!=NULL, val, JSPROP_ENUMERATE);
	LAZY_STRING_COND("from_host_name", (val=smb_get_hfield(&(p->msg),SENDERHOSTNAME,NULL))!=NULL, val, JSPROP_ENUMERATE);
	LAZY_STRING_COND("from_protocol", (val=smb_get_hfield(&(p->msg),SENDERPROTOCOL,NULL))!=NULL, val, JSPROP_ENUMERATE);
	LAZY_STRING_COND("from_port", (val=smb_get_hfield(&(p->msg),SENDERPORT,NULL))!=NULL, val, JSPROP_ENUMERATE);
	LAZY_INTEGER_EXPAND("forwarded", p->msg.forwarded, JSPROP_ENUMERATE