js_msgbase.c 81.6 KB
Newer Older
1
2
3
4
5
6
7
8
/* 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)		*
 *																			*
9
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
10
11
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
 *																			*
 * 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"
37
#include "js_request.h"
38
#include "userdat.h"
39
40
41
42
43
44

#ifdef JAVASCRIPT

typedef struct
{
	smb_t	smb;
45
	int		status;
46
47
48
49
	BOOL	debug;

} private_t;

50
51
52
53
54
55
56
57
typedef struct
{
	private_t	*p;
	BOOL		expand_fields;
	smbmsg_t	msg;

} privatemsg_t;

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

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

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

91
	JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
92
93
94
95
96

	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);
97
		return JS_TRUE;
98
99
	}

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

107
	JS_SET_RVAL(cx, arglist, JSVAL_TRUE);
108
	return JS_TRUE;
109
110
111
}


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

124
125
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

126
	rc=JS_SUSPENDREQUEST(cx);
127
	smb_close(&(p->smb));
128
	JS_RESUMEREQUEST(cx, rc);
129

130
	return JS_TRUE;
131
132
}

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

144
145
	scfg = JS_GetRuntimePrivate(JS_GetRuntime(cx));
	
146
	if(JS_GetProperty(cx, hdr, "to", &val) && !JSVAL_NULL_OR_VOID(val)) {
147
148
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
149
150
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"to\" string in recipient object");
151
			return(FALSE);
152
		}
153
	} else {
154
155
		if(p->smb.status.attr&SMB_EMAIL) {	/* e-mail */
			JS_ReportError(cx, "\"to\" property not included in email recipient object");
156
			return(FALSE);					/* "to" property required */
157
		}
158
		cp=strdup("All");
159
	}
deuce's avatar
deuce committed
160

161
	if((p->status=smb_hfield_str(msg, RECIPIENT, cp))!=SMB_SUCCESS) {
162
		JS_ReportError(cx, "Error %d adding RECIPIENT field to message header", p->status);
163
		free(cp);
164
		return(FALSE);
165
	}
166
167
168
169
170
171
	if(!(p->smb.status.attr&SMB_EMAIL)) {
		SAFECOPY(to,cp);
		strlwr(to);
		msg->idx.to=crc16(to,0);
	}

172
	if(JS_GetProperty(cx, hdr, "to_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
173
174
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
175
176
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"to_ext\" string in recipient object");
rswindell's avatar
rswindell committed
177
			return(FALSE);
178
179
		}
		if((p->status=smb_hfield_str(msg, RECIPIENTEXT, cp))!=SMB_SUCCESS) {
180
			free(cp);
181
			JS_ReportError(cx, "Error %d adding RECIPIENTEXT field to message header", p->status);
182
			return(FALSE);
183
		}
rswindell's avatar
rswindell committed
184
185
186
187
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.to=atoi(cp);
	}

188
	if(JS_GetProperty(cx, hdr, "to_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
189
190
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
191
192
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"to_org\" string in recipient object");
rswindell's avatar
rswindell committed
193
			return(FALSE);
194
195
		}
		if((p->status=smb_hfield_str(msg, RECIPIENTORG, cp))!=SMB_SUCCESS) {
196
			free(cp);
197
			JS_ReportError(cx, "Error %d adding RECIPIENTORG field to message header", p->status);
198
			return(FALSE);
199
		}
rswindell's avatar
rswindell committed
200
201
	}

202
	if(JS_GetProperty(cx, hdr, "to_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
203
204
		if(!JS_ValueToInt32(cx,val,&i32))
			return(FALSE);
rswindell's avatar
rswindell committed
205
206
207
		nettype=(ushort)i32;
	}

208
	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
209
210
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
211
212
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"to_net_addr\" string in recipient object");
rswindell's avatar
rswindell committed
213
			return(FALSE);
214
		}
215
		if((p->status=smb_hfield_netaddr(msg, RECIPIENTNETADDR, cp, &nettype))!=SMB_SUCCESS) {
216
			free(cp);
217
			JS_ReportError(cx, "Error %d adding RECIPIENTADDR field to message header", p->status);
218
			return(FALSE);
219
		}
220
	}
221
	free(cp);
222
223

	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
		if(p->smb.status.attr&SMB_EMAIL) {
			if(nettype==NET_QWK && msg->idx.to==0) {
				char fulladdr[128];
				msg->idx.to = qwk_route(scfg, msg->to_net.addr, fulladdr, sizeof(fulladdr)-1);
				if(fulladdr[0]==0) {
					JS_ReportError(cx, "Unrouteable QWKnet \"to_net_addr\" (%s) in recipient object"
						,msg->to_net.addr);
					return(FALSE);
				}
				if((p->status=smb_hfield_str(msg, RECIPIENTNETADDR, fulladdr))!=SMB_SUCCESS) {
					JS_ReportError(cx, "Error %d adding RECIPIENTADDR field to message header"
						,p->status);
					return(FALSE);
				}
				if(msg->idx.to != 0) {
					char ext[32];
					sprintf(ext,"%u",msg->idx.to);
					if((p->status=smb_hfield_str(msg, RECIPIENTEXT, ext))!=SMB_SUCCESS) {
						JS_ReportError(cx, "Error %d adding RECIPIENTEXT field to message header"
							,p->status);
						return(FALSE);
					}
				}
			} else
				msg->idx.to=0;
		}
250
		if((p->status=smb_hfield_bin(msg, RECIPIENTNETTYPE, nettype))!=SMB_SUCCESS) {
251
			JS_ReportError(cx, "Error %d adding RECIPIENTNETTYPE field to message header", p->status);
252
			return(FALSE);
253
		}
rswindell's avatar
rswindell committed
254
255
	}

256
	if(JS_GetProperty(cx, hdr, "to_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
257
258
		if(!JS_ValueToInt32(cx,val,&i32))
			return FALSE;
rswindell's avatar
rswindell committed
259
		agent=(ushort)i32;
260
		if((p->status=smb_hfield_bin(msg, RECIPIENTAGENT, agent))!=SMB_SUCCESS) {
261
			JS_ReportError(cx, "Error %d adding RECIPIENTAGENT field to message header", p->status);
262
			return(FALSE);
263
		}
rswindell's avatar
rswindell committed
264
	}
265
266
267
268
269
270
271

	return(TRUE);
}

static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg
								,BOOL recipient)
{
272
273
	char*		cp=NULL;
	size_t		cp_sz=0;
274
	char		from[256];
275
	ushort		nettype=NET_UNKNOWN;
276
	ushort		type;
277
278
	ushort		agent;
	int32		i32;
279
	jsval		val;
280
281
282
	JSObject*	array;
	JSObject*	field;
	jsuint		i,len;
283

284
285
	if(hdr==NULL) {
		JS_ReportError(cx, "NULL header object");
286
		goto err;
287
	}
288

289
	if(recipient && !parse_recipient_object(cx,p,hdr,msg))
290
		goto err;
291

292
	/* Required Header Fields */
293
	if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
294
295
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
296
297
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"subject\" string in header object");
298
			goto err;
299
		}
300
	} else
301
		cp=strdup("");
302
303

	if((p->status=smb_hfield_str(msg, SUBJECT, cp))!=SMB_SUCCESS) {
304
		JS_ReportError(cx, "Error %d adding SUBJECT field to message header", p->status);
305
		goto err;
306
	}
307
	msg->idx.subj=smb_subject_crc(cp);
308

309
	if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
310
311
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
312
313
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from\" string in header object");
314
			goto err;
315
316
317
		}
	} else {
		JS_ReportError(cx, "\"from\" property required in header");
318
		goto err;	/* "from" property required */
319
320
	}
	if((p->status=smb_hfield_str(msg, SENDER, cp))!=SMB_SUCCESS) {
321
		JS_ReportError(cx, "Error %d adding SENDER field to message header", p->status);
322
		goto err;
323
	}
324
	if(!(p->smb.status.attr&SMB_EMAIL)) {
325
326
		SAFECOPY(from,cp);
		strlwr(from);
327
		msg->idx.from=crc16(from,0);
328
	}
329
330

	/* Optional Header Fields */
331
	if(JS_GetProperty(cx, hdr, "from_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
332
333
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
334
335
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_ext\" string in header object");
336
			goto err;
337
338
		}
		if((p->status=smb_hfield_str(msg, SENDEREXT, cp))!=SMB_SUCCESS) {
339
			JS_ReportError(cx, "Error %d adding SENDEREXT field to message header", p->status);
340
			goto err;
341
		}
342
		if(p->smb.status.attr&SMB_EMAIL)
343
344
345
			msg->idx.from=atoi(cp);
	}

346
	if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
347
348
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
349
350
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_org\" string in header object");
351
			goto err;
352
353
		}
		if((p->status=smb_hfield_str(msg, SENDERORG, cp))!=SMB_SUCCESS) {
354
			JS_ReportError(cx, "Error %d adding SENDERORG field to message header", p->status);
355
			goto err;
356
		}
357
358
	}

359
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
360
361
362
		if(!JS_ValueToInt32(cx,val,&i32)) {
			goto err;
		}
363
		nettype=(ushort)i32;
364
365
	}

366
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
367
368
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
369
370
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_net_addr\" string in header object");
371
			goto err;
372
		}
373
		if((p->status=smb_hfield_netaddr(msg, SENDERNETADDR, cp, &nettype))!=SMB_SUCCESS) {
374
			JS_ReportError(cx, "Error %d adding SENDERNETADDR field to message header", p->status);
375
			goto err;
376
		}
377
378
379
380
381
	}
	
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.from=0;
382
		if((p->status=smb_hfield_bin(msg, SENDERNETTYPE, nettype))!=SMB_SUCCESS) {
383
			JS_ReportError(cx, "Error %d adding SENDERNETTYPE field to message header", p->status);
384
			goto err;
385
		}
386
387
	}

388
	if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
389
		if(!JS_ValueToInt32(cx,val,&i32))
390
			goto err;
391
		agent=(ushort)i32;
392
		if((p->status=smb_hfield_bin(msg, SENDERAGENT, agent))!=SMB_SUCCESS) {
393
			JS_ReportError(cx, "Error %d adding SENDERAGENT field to message header", p->status);
394
			goto err;
395
		}
396
397
	}

398
	if(JS_GetProperty(cx, hdr, "from_ip_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
399
400
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
401
402
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_ip_addr\" string in header object");
403
			goto err;
404
405
		}
		if((p->status=smb_hfield_str(msg, SENDERIPADDR, cp))!=SMB_SUCCESS) {
406
			JS_ReportError(cx, "Error %d adding SENDERIPADDR field to message header", p->status);
407
			goto err;
408
		}
409
410
	}

411
	if(JS_GetProperty(cx, hdr, "from_host_name", &val) && !JSVAL_NULL_OR_VOID(val)) {
412
413
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
414
415
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_host_name\" string in header object");
416
			goto err;
417
418
		}
		if((p->status=smb_hfield_str(msg, SENDERHOSTNAME, cp))!=SMB_SUCCESS) {
419
			JS_ReportError(cx, "Error %d adding SENDERHOSTNAME field to message header", p->status);
420
			goto err;
421
		}
422
423
	}

424
	if(JS_GetProperty(cx, hdr, "from_protocol", &val) && !JSVAL_NULL_OR_VOID(val)) {
425
426
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
427
428
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_protocol\" string in header object");
429
			goto err;
430
431
		}
		if((p->status=smb_hfield_str(msg, SENDERPROTOCOL, cp))!=SMB_SUCCESS) {
432
			JS_ReportError(cx, "Error %d adding SENDERPROTOCOL field to message header", p->status);
433
			goto err;
434
		}
435
436
	}

437
	if(JS_GetProperty(cx, hdr, "from_port", &val) && !JSVAL_NULL_OR_VOID(val)) {
438
439
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
440
441
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_port\" string in header object");
442
			goto err;
443
444
		}
		if((p->status=smb_hfield_str(msg, SENDERPORT, cp))!=SMB_SUCCESS) {
445
			JS_ReportError(cx, "Error %d adding SENDERPORT field to message header", p->status);
446
			goto err;
447
		}
448
449
	}

450
	if(JS_GetProperty(cx, hdr, "sender_userid", &val) && !JSVAL_NULL_OR_VOID(val)) {
451
452
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
453
454
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"sender_userid\" string in header object");
455
			goto err;
456
457
458
		}
		if((p->status=smb_hfield_str(msg, SENDERUSERID, cp))!=SMB_SUCCESS) {
			JS_ReportError(cx, "Error %d adding SENDERUSERID field to message header", p->status);
459
			goto err;
460
461
462
463
		}
	}

	if(JS_GetProperty(cx, hdr, "sender_server", &val) && !JSVAL_NULL_OR_VOID(val)) {
464
465
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
466
467
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"sender_server\" string in header object");
468
			goto err;
469
470
471
		}
		if((p->status=smb_hfield_str(msg, SENDERSERVER, cp))!=SMB_SUCCESS) {
			JS_ReportError(cx, "Error %d adding SENDERSERVER field to message header", p->status);
472
			goto err;
473
474
475
476
		}
	}

	if(JS_GetProperty(cx, hdr, "sender_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
477
478
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
479
480
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"sender_time\" string in header object");
481
			goto err;
482
483
484
		}
		if((p->status=smb_hfield_str(msg, SENDERTIME, cp))!=SMB_SUCCESS) {
			JS_ReportError(cx, "Error %d adding SENDERTIME field to message header", p->status);
485
			goto err;
486
487
488
		}
	}
	
489
	if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
490
491
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
492
493
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto\" string in header object");
494
			goto err;
495
496
		}
		if((p->status=smb_hfield_str(msg, REPLYTO, cp))!=SMB_SUCCESS) {
497
			JS_ReportError(cx, "Error %d adding REPLYTO field to message header", p->status);
498
			goto err;
499
		}
500
501
	}

502
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
503
504
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
505
506
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto_ext\" string in header object");
507
			goto err;
508
509
		}
		if((p->status=smb_hfield_str(msg, REPLYTOEXT, cp))!=SMB_SUCCESS) {
510
			JS_ReportError(cx, "Error %d adding REPLYTOEXT field to message header", p->status);
511
			goto err;
512
		}
513
514
	}

515
	if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
516
517
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
518
519
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto_org\" string in header object");
520
			goto err;
521
522
		}
		if((p->status=smb_hfield_str(msg, REPLYTOORG, cp))!=SMB_SUCCESS) {
523
			JS_ReportError(cx, "Error %d adding REPLYTOORG field to message header", p->status);
524
			goto err;
525
		}
526
527
	}

528
	nettype=NET_UNKNOWN;
529
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
530
		if(!JS_ValueToInt32(cx,val,&i32))
531
			goto err;
532
		nettype=(ushort)i32;
533
	}
534
	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
535
536
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
537
538
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto_net_addr\" string in header object");
539
			goto err;
540
		}
541
		if((p->status=smb_hfield_netaddr(msg, REPLYTONETADDR, cp, &nettype))!=SMB_SUCCESS) {
542
			JS_ReportError(cx, "Error %d adding REPLYTONETADDR field to message header", p->status);
543
			goto err;
544
		}
545
546
	}
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
547
		if((p->status=smb_hfield_bin(msg, REPLYTONETTYPE, nettype))!=SMB_SUCCESS) {
548
			JS_ReportError(cx, "Error %d adding REPLYTONETTYPE field to message header", p->status);
549
			goto err;
550
		}
551
552
	}

553
	if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
554
		if(!JS_ValueToInt32(cx,val,&i32))
555
			goto err;
556
		agent=(ushort)i32;
557
		if((p->status=smb_hfield_bin(msg, REPLYTOAGENT, agent))!=SMB_SUCCESS) {
558
			JS_ReportError(cx, "Error %d adding REPLYTOAGENT field to message header", p->status);
559
			goto err;
560
		}
561
562
	}

563
	/* RFC822 headers */
564
	if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
565
566
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
567
568
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"id\" string in header object");
569
			goto err;
570
571
		}
		if((p->status=smb_hfield_str(msg, RFC822MSGID, cp))!=SMB_SUCCESS) {
572
			JS_ReportError(cx, "Error %d adding RFC822MSGID field to message header", p->status);
573
			goto err;
574
		}
575
576
	}

577
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
578
579
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
580
581
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"reply_id\" string in header object");
582
			goto err;
583
584
		}
		if((p->status=smb_hfield_str(msg, RFC822REPLYID, cp))!=SMB_SUCCESS) {
585
			JS_ReportError(cx, "Error %d adding RFC822REPLYID field to message header", p->status);
586
			goto err;
587
		}
588
589
	}

590
	/* SMTP headers */
591
	if(JS_GetProperty(cx, hdr, "reverse_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
592
593
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
594
595
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"reverse_path\" string in header object");
596
			goto err;
597
598
		}
		if((p->status=smb_hfield_str(msg, SMTPREVERSEPATH, cp))!=SMB_SUCCESS) {
599
			JS_ReportError(cx, "Error %d adding SMTPREVERSEPATH field to message header", p->status);
600
			goto err;
601
		}
602
603
	}

604
	if(JS_GetProperty(cx, hdr, "forward_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
605
606
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
607
608
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"forward_path\" string in header object");
609
			goto err;
610
611
		}
		if((p->status=smb_hfield_str(msg, SMTPFORWARDPATH, cp))!=SMB_SUCCESS) {
612
			JS_ReportError(cx, "Error %d adding SMTPFORWARDPATH field to message header", p->status);
613
			goto err;
614
		}
615
616
	}

617
	/* USENET headers */
618
	if(JS_GetProperty(cx, hdr, "path", &val) && !JSVAL_NULL_OR_VOID(val)) {
619
620
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
621
622
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"path\" string in header object");
623
			goto err;
624
625
		}
		if((p->status=smb_hfield_str(msg, USENETPATH, cp))!=SMB_SUCCESS) {
626
			JS_ReportError(cx, "Error %d adding USENETPATH field to message header", p->status);
627
			goto err;
628
		}
629
630
	}

631
	if(JS_GetProperty(cx, hdr, "newsgroups", &val) && !JSVAL_NULL_OR_VOID(val)) {
632
633
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
634
635
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"newsgroups\" string in header object");
636
			goto err;
637
638
		}
		if((p->status=smb_hfield_str(msg, USENETNEWSGROUPS, cp))!=SMB_SUCCESS) {
639
			JS_ReportError(cx, "Error %d adding USENETNEWSGROUPS field to message header", p->status);
640
			goto err;
641
		}
642
643
644
	}

	/* FTN headers */
645
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && !JSVAL_NULL_OR_VOID(val)) {
646
647
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
648
649
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"ftn_msgid\" string in header object");
650
			goto err;
651
652
		}
		if((p->status=smb_hfield_str(msg, FIDOMSGID, cp))!=SMB_SUCCESS) {
653
			JS_ReportError(cx, "Error %d adding FIDOMSGID field to message header", p->status);
654
			goto err;
655
		}
656
657
	}

658
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && !JSVAL_NULL_OR_VOID(val)) {
659
660
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
661
662
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"ftn_reply\" string in header object");
663
			goto err;
664
665
		}
		if((p->status=smb_hfield_str(msg, FIDOREPLYID, cp))!=SMB_SUCCESS) {
666
			JS_ReportError(cx, "Error %d adding FIDOREPLYID field to message header", p->status);
667
			goto err;
668
		}
669
670
	}

671
	if(JS_GetProperty(cx, hdr, "ftn_area", &val) && !JSVAL_NULL_OR_VOID(val)) {
672
673
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
674
675
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"ftn_area\" string in header object");
676
			goto err;
677
678
		}
		if((p->status=smb_hfield_str(msg, FIDOAREA, cp))!=SMB_SUCCESS) {
679
			JS_ReportError(cx, "Error %d adding FIDOAREA field to message header", p->status);
680
			goto err;
681
		}
682
683
	}

684
	if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && !JSVAL_NULL_OR_VOID(val)) {
685
686
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
687
688
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"ftn_flags\" string in header object");
689
			goto err;
690
691
		}
		if((p->status=smb_hfield_str(msg, FIDOFLAGS, cp))!=SMB_SUCCESS) {
692
			JS_ReportError(cx, "Error %d adding FIDOFLAGS field to message header", p->status);
693
			goto err;
694
		}
695
696
	}

697
	if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && !JSVAL_NULL_OR_VOID(val)) {
698
699
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
700
701
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"ftn_pid\" string in header object");
702
			goto err;
703
704
		}
		if((p->status=smb_hfield_str(msg, FIDOPID, cp))!=SMB_SUCCESS) {
705
			JS_ReportError(cx, "Error %d adding FIDOPID field to message header", p->status);
706
			goto err;
707
		}
708
709
	}

710
	if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && !JSVAL_NULL_OR_VOID(val)) {
711
712
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
713
714
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"ftn_tid\" string in header object");
715
			goto err;
716
717
		}
		if((p->status=smb_hfield_str(msg, FIDOTID, cp))!=SMB_SUCCESS) {
718
			JS_ReportError(cx, "Error %d adding FIDOTID field to message header", p->status);
719
			goto err;
720
		}
721
722
	}

723
	if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
724
725
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
726
727
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"date\" string in header object");
728
			goto err;
729
		}
730
731
		msg->hdr.when_written=rfc822date(cp);
	}
732

733
	/* Numeric Header Fields */
734
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
735
		if(!JS_ValueToInt32(cx,val,&i32))
736
			goto err;
737
		msg->hdr.attr=(ushort)i32;
738
739
		msg->idx.attr=msg->hdr.attr;
	}
740
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
741
		if(!JS_ValueToInt32(cx,val,&i32))
742
			goto err;
743
744
		msg->hdr.auxattr=i32;
	}
745
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
746
		if(!JS_ValueToInt32(cx,val,&i32))
747
			goto err;
748
749
		msg->hdr.netattr=i32;
	}
750
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val))  {
751
		if(