js_msgbase.c 90.1 KB
Newer Older
1
2
3
/* Synchronet JavaScript "MsgBase" Object */

/* $Id$ */
4
// vi: tabstop=4
5
6
7
8
9

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
10
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
37
 *																			*
 * 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"
38
#include "js_request.h"
39
#include "userdat.h"
40
41
42
43
44
45

#ifdef JAVASCRIPT

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

} private_t;

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

} privatemsg_t;

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

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

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

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

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

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

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


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

125
126
	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

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

131
	return JS_TRUE;
132
133
}

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

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

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

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

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

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

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

	if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
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
250
		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;
		}
251
		if((p->status=smb_hfield_bin(msg, RECIPIENTNETTYPE, nettype))!=SMB_SUCCESS) {
252
			JS_ReportError(cx, "Error %d adding RECIPIENTNETTYPE field to message header", p->status);
253
			return(FALSE);
254
		}
rswindell's avatar
rswindell committed
255
256
	}

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

	return(TRUE);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

565
	/* RFC822 headers */
566
	if(JS_GetProperty(cx, hdr, "id", &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 \"id\" string in header object");
571
			goto err;
572
573
		}
		if((p->status=smb_hfield_str(msg, RFC822MSGID, cp))!=SMB_SUCCESS) {
574
			JS_ReportError(cx, "Error %d adding RFC822MSGID field to message header", p->status);
575
			goto err;
576
		}
577
578
	}

579
	if(JS_GetProperty(cx, hdr, "reply_id", &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 \"reply_id\" string in header object");
584
			goto err;
585
586
		}
		if((p->status=smb_hfield_str(msg, RFC822REPLYID, cp))!=SMB_SUCCESS) {
587
			JS_ReportError(cx, "Error %d adding RFC822REPLYID field to message header", p->status);
588
			goto err;
589
		}
590
591
	}

592
	/* SMTP headers */
593
	if(JS_GetProperty(cx, hdr, "reverse_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 \"reverse_path\" string in header object");
598
			goto err;
599
600
		}
		if((p->status=smb_hfield_str(msg, SMTPREVERSEPATH, cp))!=SMB_SUCCESS) {
601
			JS_ReportError(cx, "Error %d adding SMTPREVERSEPATH field to message header", p->status);
602
			goto err;
603
		}
604
605
	}

606
	if(JS_GetProperty(cx, hdr, "forward_path", &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 \"forward_path\" string in header object");
611
			goto err;
612
613
		}
		if((p->status=smb_hfield_str(msg, SMTPFORWARDPATH, cp))!=SMB_SUCCESS) {
614
			JS_ReportError(cx, "Error %d adding SMTPFORWARDPATH field to message header", p->status);
615
			goto err;
616
		}
617
618
	}

619
	/* USENET headers */
620
	if(JS_GetProperty(cx, hdr, "path", &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 \"path\" string in header object");
625
			goto err;
626
627
		}
		if((p->status=smb_hfield_str(msg, USENETPATH, cp))!=SMB_SUCCESS) {
628
			JS_ReportError(cx, "Error %d adding USENETPATH field to message header", p->status);
629
			goto err;
630
		}
631
632
	}

633
	if(JS_GetProperty(cx, hdr, "newsgroups", &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 \"newsgroups\" string in header object");
638
			goto err;
639
640
		}
		if((p->status=smb_hfield_str(msg, USENETNEWSGROUPS, cp))!=SMB_SUCCESS) {
641
			JS_ReportError(cx, "Error %d adding USENETNEWSGROUPS field to message header", p->status);
642
			goto err;
643
		}
644
645
646
	}

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

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

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

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

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

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

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

735
	/* Numeric Header Fields */
736
	if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
737
		if(!JS_ValueToInt32(cx,val,&i32))
738
			goto err;
739
		msg->hdr.attr=(ushort)i32;
740
741
		msg->idx.attr=msg->hdr.attr;
	}
742
	if(JS_GetProperty(cx, hdr, "votes", &val) && !JSVAL_NULL_OR_VOID(val)) {
743
744
		if(!JS_ValueToInt32(cx,val,&i32))
			goto err;
745
746
747
		msg->hdr.votes=(ushort)i32;
		if(msg->hdr.type == SMB_MSG_TYPE_BALLOT)
			msg->idx.votes=msg->hdr.votes;
748
	}
749
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
750
		if(!JS_ValueToInt32(cx,val,&i32))
751
			goto err;
752
753
		msg->hdr.auxattr=i32;
	}
754
	if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
755
		if(!JS_ValueToInt32(cx,val,&i32))
756
			goto err;
757
758
		msg->hdr.netattr=i32;
	}