js_msgbase.c 79.6 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 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
#include "userdat.h"
41
42
43
44
45
46

#ifdef JAVASCRIPT

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

} private_t;

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

} privatemsg_t;

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

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

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

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

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

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

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


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

126
127
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

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

	return(JS_TRUE);
}

135
static BOOL parse_recipient_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg)
136
{
137
138
	char*		cp=NULL;
	size_t		cp_sz=0;
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
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
148
149
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"to\" string in recipient object");
150
			return(FALSE);
151
		}
152
	} else {
153
154
		if(p->smb.status.attr&SMB_EMAIL) {	/* e-mail */
			JS_ReportError(cx, "\"to\" property not included in email recipient object");
155
			return(FALSE);					/* "to" property required */
156
		}
157
		cp=strdup("All");
158
	}
deuce's avatar
deuce committed
159

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

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

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

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

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

	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.to=0;
225
		if((p->status=smb_hfield_bin(msg, RECIPIENTNETTYPE, nettype))!=SMB_SUCCESS) {
226
			JS_ReportError(cx, "Error %d adding RECIPIENTNETTYPE field to message header", p->status);
227
			return(FALSE);
228
		}
rswindell's avatar
rswindell committed
229
230
	}

231
	if(JS_GetProperty(cx, hdr, "to_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
232
233
		if(!JS_ValueToInt32(cx,val,&i32))
			return FALSE;
rswindell's avatar
rswindell committed
234
		agent=(ushort)i32;
235
		if((p->status=smb_hfield_bin(msg, RECIPIENTAGENT, agent))!=SMB_SUCCESS) {
236
			JS_ReportError(cx, "Error %d adding RECIPIENTAGENT field to message header", p->status);
237
			return(FALSE);
238
		}
rswindell's avatar
rswindell committed
239
	}
240
241
242
243
244
245
246

	return(TRUE);
}

static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg
								,BOOL recipient)
{
247
248
	char*		cp=NULL;
	size_t		cp_sz=0;
249
	char		from[256];
250
	ushort		nettype=NET_UNKNOWN;
251
	ushort		type;
252
253
	ushort		agent;
	int32		i32;
254
	jsval		val;
255
256
257
	JSObject*	array;
	JSObject*	field;
	jsuint		i,len;
258

259
260
	if(hdr==NULL) {
		JS_ReportError(cx, "NULL header object");
261
		goto err;
262
	}
263

264
	if(recipient && !parse_recipient_object(cx,p,hdr,msg))
265
		goto err;
266

267
	/* Required Header Fields */
268
	if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
269
270
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
271
272
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"subject\" string in header object");
273
			goto err;
274
		}
275
	} else
276
		cp=strdup("");
277
278

	if((p->status=smb_hfield_str(msg, SUBJECT, cp))!=SMB_SUCCESS) {
279
		JS_ReportError(cx, "Error %d adding SUBJECT field to message header", p->status);
280
		goto err;
281
	}
282
	msg->idx.subj=smb_subject_crc(cp);
283

284
	if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
285
286
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
287
288
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from\" string in header object");
289
			goto err;
290
291
292
		}
	} else {
		JS_ReportError(cx, "\"from\" property required in header");
293
		goto err;	/* "from" property required */
294
295
	}
	if((p->status=smb_hfield_str(msg, SENDER, cp))!=SMB_SUCCESS) {
296
		JS_ReportError(cx, "Error %d adding SENDER field to message header", p->status);
297
		goto err;
298
	}
299
	if(!(p->smb.status.attr&SMB_EMAIL)) {
300
301
		SAFECOPY(from,cp);
		strlwr(from);
302
		msg->idx.from=crc16(from,0);
303
	}
304
305

	/* Optional Header Fields */
306
	if(JS_GetProperty(cx, hdr, "from_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
307
308
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
309
310
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_ext\" string in header object");
311
			goto err;
312
313
		}
		if((p->status=smb_hfield_str(msg, SENDEREXT, cp))!=SMB_SUCCESS) {
314
			JS_ReportError(cx, "Error %d adding SENDEREXT field to message header", p->status);
315
			goto err;
316
		}
317
		if(p->smb.status.attr&SMB_EMAIL)
318
319
320
			msg->idx.from=atoi(cp);
	}

321
	if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
322
323
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
324
325
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_org\" string in header object");
326
			goto err;
327
328
		}
		if((p->status=smb_hfield_str(msg, SENDERORG, cp))!=SMB_SUCCESS) {
329
			JS_ReportError(cx, "Error %d adding SENDERORG field to message header", p->status);
330
			goto err;
331
		}
332
333
	}

334
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
335
336
337
		if(!JS_ValueToInt32(cx,val,&i32)) {
			goto err;
		}
338
		nettype=(ushort)i32;
339
340
	}

341
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
342
343
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
344
345
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_net_addr\" string in header object");
346
			goto err;
347
		}
348
		if((p->status=smb_hfield_netaddr(msg, SENDERNETADDR, cp, &nettype))!=SMB_SUCCESS) {
349
			JS_ReportError(cx, "Error %d adding SENDERNETADDR field to message header", p->status);
350
			goto err;
351
		}
352
353
354
355
356
	}
	
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
		if(p->smb.status.attr&SMB_EMAIL)
			msg->idx.from=0;
357
		if((p->status=smb_hfield_bin(msg, SENDERNETTYPE, nettype))!=SMB_SUCCESS) {
358
			JS_ReportError(cx, "Error %d adding SENDERNETTYPE field to message header", p->status);
359
			goto err;
360
		}
361
362
	}

363
	if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
364
		if(!JS_ValueToInt32(cx,val,&i32))
365
			goto err;
366
		agent=(ushort)i32;
367
		if((p->status=smb_hfield_bin(msg, SENDERAGENT, agent))!=SMB_SUCCESS) {
368
			JS_ReportError(cx, "Error %d adding SENDERAGENT field to message header", p->status);
369
			goto err;
370
		}
371
372
	}

373
	if(JS_GetProperty(cx, hdr, "from_ip_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
374
375
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
376
377
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_ip_addr\" string in header object");
378
			goto err;
379
380
		}
		if((p->status=smb_hfield_str(msg, SENDERIPADDR, cp))!=SMB_SUCCESS) {
381
			JS_ReportError(cx, "Error %d adding SENDERIPADDR field to message header", p->status);
382
			goto err;
383
		}
384
385
	}

386
	if(JS_GetProperty(cx, hdr, "from_host_name", &val) && !JSVAL_NULL_OR_VOID(val)) {
387
388
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
389
390
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"from_host_name\" string in header object");
391
			goto err;
392
393
		}
		if((p->status=smb_hfield_str(msg, SENDERHOSTNAME, cp))!=SMB_SUCCESS) {
394
			JS_ReportError(cx, "Error %d adding SENDERHOSTNAME field to message header", p->status);
395
			goto err;
396
		}
397
398
	}

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

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

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

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

	if(JS_GetProperty(cx, hdr, "sender_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
452
453
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
454
455
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"sender_time\" string in header object");
456
			goto err;
457
458
459
		}
		if((p->status=smb_hfield_str(msg, SENDERTIME, cp))!=SMB_SUCCESS) {
			JS_ReportError(cx, "Error %d adding SENDERTIME field to message header", p->status);
460
			goto err;
461
462
463
		}
	}
	
464
	if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
465
466
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
467
468
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto\" string in header object");
469
			goto err;
470
471
		}
		if((p->status=smb_hfield_str(msg, REPLYTO, cp))!=SMB_SUCCESS) {
472
			JS_ReportError(cx, "Error %d adding REPLYTO field to message header", p->status);
473
			goto err;
474
		}
475
476
	}

477
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
478
479
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
480
481
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto_ext\" string in header object");
482
			goto err;
483
484
		}
		if((p->status=smb_hfield_str(msg, REPLYTOEXT, cp))!=SMB_SUCCESS) {
485
			JS_ReportError(cx, "Error %d adding REPLYTOEXT field to message header", p->status);
486
			goto err;
487
		}
488
489
	}

490
	if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
491
492
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
493
494
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto_org\" string in header object");
495
			goto err;
496
497
		}
		if((p->status=smb_hfield_str(msg, REPLYTOORG, cp))!=SMB_SUCCESS) {
498
			JS_ReportError(cx, "Error %d adding REPLYTOORG field to message header", p->status);
499
			goto err;
500
		}
501
502
	}

503
	nettype=NET_UNKNOWN;
504
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
505
		if(!JS_ValueToInt32(cx,val,&i32))
506
			goto err;
507
		nettype=(ushort)i32;
508
	}
509
	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
510
511
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
512
513
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"replyto_net_addr\" string in header object");
514
			goto err;
515
		}
516
		if((p->status=smb_hfield_netaddr(msg, REPLYTONETADDR, cp, &nettype))!=SMB_SUCCESS) {
517
			JS_ReportError(cx, "Error %d adding REPLYTONETADDR field to message header", p->status);
518
			goto err;
519
		}
520
521
	}
	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
522
		if((p->status=smb_hfield_bin(msg, REPLYTONETTYPE, nettype))!=SMB_SUCCESS) {
523
			JS_ReportError(cx, "Error %d adding REPLYTONETTYPE field to message header", p->status);
524
			goto err;
525
		}
526
527
	}

528
	if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
529
		if(!JS_ValueToInt32(cx,val,&i32))
530
			goto err;
531
		agent=(ushort)i32;
532
		if((p->status=smb_hfield_bin(msg, REPLYTOAGENT, agent))!=SMB_SUCCESS) {
533
			JS_ReportError(cx, "Error %d adding REPLYTOAGENT field to message header", p->status);
534
			goto err;
535
		}
536
537
	}

538
	/* RFC822 headers */
539
	if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
540
541
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
542
543
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"id\" string in header object");
544
			goto err;
545
546
		}
		if((p->status=smb_hfield_str(msg, RFC822MSGID, cp))!=SMB_SUCCESS) {
547
			JS_ReportError(cx, "Error %d adding RFC822MSGID field to message header", p->status);
548
			goto err;
549
		}
550
551
	}

552
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
553
554
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
555
556
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"reply_id\" string in header object");
557
			goto err;
558
559
		}
		if((p->status=smb_hfield_str(msg, RFC822REPLYID, cp))!=SMB_SUCCESS) {
560
			JS_ReportError(cx, "Error %d adding RFC822REPLYID field to message header", p->status);
561
			goto err;
562
		}
563
564
	}

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

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

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

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

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

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

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

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

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

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

698
	if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
699
700
		JSVALUE_TO_RASTRING(cx, val, cp, &cp_sz, NULL);
		HANDLE_PENDING(cx);
701
702
		if(cp==NULL) {
			JS_ReportError(cx, "Invalid \"date\" string in header object");
703
			goto err;
704
		}
705
706
		msg->hdr.when_written=rfc822date(cp);
	}
707

708
	/* Numeric Header Fields */
709
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
710
		if(!JS_ValueToInt32(cx,val,&i32))
711
			goto err;
712
		msg->hdr.attr=(ushort)i32;
713
714
		msg->idx.attr=msg->hdr.attr;
	}
715
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
716
		if(!JS_ValueToInt32(cx,val,&i32))
717
			goto err;
718
719
		msg->hdr.auxattr=i32;
	}
720
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
721
		if(!JS_ValueToInt32(cx,val,&i32))
722
			goto err;
723
724
		msg->hdr.netattr=i32;
	}
725
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val))  {
726
		if(!JS_ValueToInt32(cx,val,&i32))
727
			goto err;
728
729
		msg->hdr.when_written.time=i32;
	}
730
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
731
		if(!JS_ValueToInt32(cx,val,&i32))
732
			goto err;
733
734
		msg->hdr.when_written.zone=(short)i32;
	}
735
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
736
		if(!JS_ValueToInt32(cx,val,&i32))
737
			goto err;
738
739
		msg->hdr.when_imported.time=i32;
	}
740
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
741
		if(!JS_ValueToInt32(cx,val,