js_msgbase.c 31.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/* 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)		*
 *																			*
 * Copyright 2001 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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"

#ifdef JAVASCRIPT

static scfg_t* scfg=NULL;

typedef struct
{
	smb_t	smb;
	BOOL	debug;

} private_t;

51 52
static const char* getprivate_failure = "line %d %s JS_GetPrivate failed";

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/* 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 */

72 73 74 75 76 77
static JSBool
js_open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t* p;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
78
		JS_ReportError(cx,getprivate_failure,WHERE);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		return(JS_FALSE);
	}

	*rval = JSVAL_FALSE;

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

	if(smb_open(&(p->smb))!=0)
		return(JS_TRUE);

	*rval = JSVAL_TRUE;
	return(JS_TRUE);
}


99 100 101 102 103
static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t* p;
	
104
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
105
		JS_ReportError(cx,getprivate_failure,WHERE);
106
		return(JS_FALSE);
107
	}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

	smb_close(&(p->smb));

	*rval = JSVAL_VOID;
	return(JS_TRUE);
}

static JSClass js_msghdr_class = {
     "MsgHeader"				/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,JS_PropertyStub		/* getProperty	*/
	,JS_PropertyStub		/* setProperty	*/
	,JS_EnumerateStub		/* enumerate	*/
	,JS_ResolveStub			/* resolve		*/
	,JS_ConvertStub			/* convert		*/
125
	,JS_FinalizeStub		/* finalize		*/
126 127
};

128
static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg)
129 130
{
	char*		cp;
131 132
	char		to[256];
	char		from[256];
133 134 135 136
	ushort		nettype;
	jsval		val;

	/* Required Header Fields */
137 138
	if(JS_GetProperty(cx, hdr, "subject", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
139 140 141 142
			return(FALSE);
	} else
		cp="";
	smb_hfield(msg, SUBJECT, (ushort)strlen(cp), cp);
143
	msg->idx.subj=subject_crc(cp);
144

145 146
	if(JS_GetProperty(cx, hdr, "to", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
147 148
			return(FALSE);
	} else {
149 150
		if(p->smb.status.attr&SMB_EMAIL)	/* e-mail */
			return(FALSE);					/* "to" property required */
151 152 153
		cp="All";
	}
	smb_hfield(msg, RECIPIENT, (ushort)strlen(cp), cp);
154
	if(!(p->smb.status.attr&SMB_EMAIL)) {
155 156 157
		SAFECOPY(to,cp);
		strlwr(to);
		msg->idx.to=crc16(to);
158
	}
159

160 161
	if(JS_GetProperty(cx, hdr, "from", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
162 163 164 165
			return(FALSE);
	} else
		return(FALSE);	/* "from" property required */
	smb_hfield(msg, SENDER, (ushort)strlen(cp), cp);
166
	if(!(p->smb.status.attr&SMB_EMAIL)) {
167 168 169
		SAFECOPY(from,cp);
		strlwr(from);
		msg->idx.from=crc16(from);
170
	}
171 172

	/* Optional Header Fields */
173 174
	if(JS_GetProperty(cx, hdr, "from_ext", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
175 176
			return(FALSE);
		smb_hfield(msg, SENDEREXT, (ushort)strlen(cp), cp);
177
		if(p->smb.status.attr&SMB_EMAIL)
178 179 180
			msg->idx.from=atoi(cp);
	}

181 182
	if(JS_GetProperty(cx, hdr, "from_org", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
183 184 185 186
			return(FALSE);
		smb_hfield(msg, SENDERORG, (ushort)strlen(cp), cp);
	}

rswindell's avatar
rswindell committed
187
	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && JSVAL_IS_INT(val)) {
188 189
		nettype=(ushort)JSVAL_TO_INT(val);
		smb_hfield(msg, SENDERNETTYPE, sizeof(nettype), &nettype);
190
		if(p->smb.status.attr&SMB_EMAIL && nettype!=NET_NONE)
191 192 193
			msg->idx.from=0;
	}

194 195
	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
196 197 198 199
			return(FALSE);
		smb_hfield(msg, SENDERNETADDR, (ushort)strlen(cp), cp);
	}

200 201
	if(JS_GetProperty(cx, hdr, "to_ext", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
202 203
			return(FALSE);
		smb_hfield(msg, RECIPIENTEXT, (ushort)strlen(cp), cp);
204
		if(p->smb.status.attr&SMB_EMAIL)
205 206 207
			msg->idx.to=atoi(cp);
	}

rswindell's avatar
rswindell committed
208
	if(JS_GetProperty(cx, hdr, "to_net_type", &val) && JSVAL_IS_INT(val)) {
209 210
		nettype=(ushort)JSVAL_TO_INT(val);
		smb_hfield(msg, RECIPIENTNETTYPE, sizeof(nettype), &nettype);
211
		if(p->smb.status.attr&SMB_EMAIL && nettype!=NET_NONE)
212 213 214
			msg->idx.to=0;
	}

215 216
	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
217 218 219
			return(FALSE);
		smb_hfield(msg, RECIPIENTNETADDR, (ushort)strlen(cp), cp);
	}
220

221 222
	if(JS_GetProperty(cx, hdr, "replyto", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
223 224 225 226
			return(FALSE);
		smb_hfield(msg, REPLYTO, (ushort)strlen(cp), cp);
	}

227 228
	if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
229 230 231 232
			return(FALSE);
		smb_hfield(msg, REPLYTOEXT, (ushort)strlen(cp), cp);
	}

rswindell's avatar
rswindell committed
233
	if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && JSVAL_IS_INT(val)) {
234 235 236 237
		nettype=(ushort)JSVAL_TO_INT(val);
		smb_hfield(msg, REPLYTONETTYPE, sizeof(nettype), &nettype);
	}

238 239
	if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
240 241 242 243
			return(FALSE);
		smb_hfield(msg, REPLYTONETADDR, (ushort)strlen(cp), cp);
	}

244
	/* RFC822 headers */
245 246
	if(JS_GetProperty(cx, hdr, "id", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
247 248 249 250
			return(FALSE);
		smb_hfield(msg, RFC822MSGID, (ushort)strlen(cp), cp);
	}

251 252
	if(JS_GetProperty(cx, hdr, "reply_id", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
253 254 255 256
			return(FALSE);
		smb_hfield(msg, RFC822REPLYID, (ushort)strlen(cp), cp);
	}

257
	/* USENET headers */
258 259
	if(JS_GetProperty(cx, hdr, "path", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
260 261 262 263
			return(FALSE);
		smb_hfield(msg, USENETPATH, (ushort)strlen(cp), cp);
	}

264 265
	if(JS_GetProperty(cx, hdr, "newsgroups", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
266 267 268 269 270
			return(FALSE);
		smb_hfield(msg, USENETNEWSGROUPS, (ushort)strlen(cp), cp);
	}

	/* FTN headers */
271 272
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
273 274 275 276
			return(FALSE);
		smb_hfield(msg, FIDOMSGID, (ushort)strlen(cp), cp);
	}

277 278
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
279 280 281 282
			return(FALSE);
		smb_hfield(msg, FIDOREPLYID, (ushort)strlen(cp), cp);
	}

283 284
	if(JS_GetProperty(cx, hdr, "ftn_area", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
285 286 287 288
			return(FALSE);
		smb_hfield(msg, FIDOAREA, (ushort)strlen(cp), cp);
	}

289 290
	if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
291 292 293 294
			return(FALSE);
		smb_hfield(msg, FIDOFLAGS, (ushort)strlen(cp), cp);
	}

295 296
	if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
297 298 299 300
			return(FALSE);
		smb_hfield(msg, FIDOPID, (ushort)strlen(cp), cp);
	}

301 302
	if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
303 304 305 306
			return(FALSE);
		smb_hfield(msg, FIDOTID, (ushort)strlen(cp), cp);
	}

307 308
	if(JS_GetProperty(cx, hdr, "date", &val) && JSVAL_IS_STRING(val)) {
		if((cp=JS_GetStringBytes(JSVAL_TO_STRING(val)))==NULL)
309 310 311
			return(FALSE);
		msg->hdr.when_written=rfc822date(cp);
	}
312 313
	
	/* Numeric Header Fields */
314
	if(JS_GetProperty(cx, hdr, "attr", &val) && JSVAL_IS_INT(val)) {
315
		msg->hdr.attr=(ushort)JSVAL_TO_INT(val);
316 317
		msg->idx.attr=msg->hdr.attr;
	}
rswindell's avatar
rswindell committed
318
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && JSVAL_IS_INT(val)) 
319
		msg->hdr.auxattr=JSVAL_TO_INT(val);
rswindell's avatar
rswindell committed
320
	if(JS_GetProperty(cx, hdr, "netattr", &val) && JSVAL_IS_INT(val)) 
321
		msg->hdr.netattr=JSVAL_TO_INT(val);
rswindell's avatar
rswindell committed
322
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && JSVAL_IS_INT(val)) 
323
		msg->hdr.when_written.time=JSVAL_TO_INT(val);
rswindell's avatar
rswindell committed
324
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && JSVAL_IS_INT(val)) 
325
		msg->hdr.when_written.zone=(short)JSVAL_TO_INT(val);
rswindell's avatar
rswindell committed
326
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && JSVAL_IS_INT(val)) 
327
		msg->hdr.when_imported.time=JSVAL_TO_INT(val);
rswindell's avatar
rswindell committed
328
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && JSVAL_IS_INT(val)) 
329 330 331 332 333
		msg->hdr.when_imported.zone=(short)JSVAL_TO_INT(val);

	return(TRUE);
}

334 335 336
static JSBool
js_get_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
337
	char		date[128];
338 339
	char		msg_id[256];
	char		reply_id[256];
340
	char*		val;
341 342
	ulong		l;
	smbmsg_t	msg;
343
	smbmsg_t	orig_msg;
344 345 346 347 348
	JSObject*	hdrobj;
	private_t*	p;

	*rval = JSVAL_NULL;
	
349
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
350
		JS_ReportError(cx,getprivate_failure,WHERE);
351
		return(JS_FALSE);
352
	}
353

354 355 356
	if(!SMB_IS_OPEN(&(p->smb)))
		return(JS_TRUE);

357 358 359 360 361 362 363 364 365 366
	memset(&msg,0,sizeof(msg));

	if(JSVAL_TO_BOOLEAN(argv[0])==JS_TRUE)	/* Get by offset */
		msg.offset=JSVAL_TO_INT(argv[1]);
	else									/* Get by number */
		msg.hdr.number=JSVAL_TO_INT(argv[1]);

	if(smb_getmsgidx(&(p->smb), &msg)!=0)
		return(JS_TRUE);

367
	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
368 369
		return(JS_TRUE);

370 371 372 373 374 375 376
	if(smb_getmsghdr(&(p->smb), &msg)!=0) {
		smb_unlockmsghdr(&(p->smb),&msg); 
		return(JS_TRUE);
	}

	smb_unlockmsghdr(&(p->smb),&msg); 

rswindell's avatar
rswindell committed
377 378
	if((hdrobj=JS_NewObject(cx,&js_msghdr_class,NULL,obj))==NULL) {
		smb_freemsgmem(&msg);
379
		return(JS_TRUE);
rswindell's avatar
rswindell committed
380
	}
381 382

	JS_DefineProperty(cx, hdrobj, "number", INT_TO_JSVAL(msg.hdr.number)
383
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
384 385
	JS_DefineProperty(cx, hdrobj, "to"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.to)))
386
		,NULL,NULL,JSPROP_ENUMERATE);
387 388
	JS_DefineProperty(cx, hdrobj, "from"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.from)))
389
		,NULL,NULL,JSPROP_ENUMERATE);
390 391
	JS_DefineProperty(cx, hdrobj, "subject"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.subj)))
392
		,NULL,NULL,JSPROP_ENUMERATE);
393
	if(msg.summary!=NULL)
394 395
		JS_DefineProperty(cx, hdrobj, "summary"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.summary)))
396
			,NULL,NULL,JSPROP_ENUMERATE);
397
	if(msg.to_ext!=NULL)
398 399
		JS_DefineProperty(cx, hdrobj, "to_ext"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.to_ext)))
400 401
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.from_ext!=NULL)
402 403
		JS_DefineProperty(cx, hdrobj, "from_ext"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.from_ext)))
404 405
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.from_org!=NULL)
406 407
		JS_DefineProperty(cx, hdrobj, "from_org"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.from_org)))
408 409
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.replyto!=NULL)
410 411
		JS_DefineProperty(cx, hdrobj, "replyto"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.replyto)))
412 413
			,NULL,NULL,JSPROP_ENUMERATE);
	if(msg.replyto_ext!=NULL)
414 415
		JS_DefineProperty(cx, hdrobj, "replyto_ext"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.replyto_ext)))
416
			,NULL,NULL,JSPROP_ENUMERATE);
417

418
	JS_DefineProperty(cx, hdrobj, "to_agent",INT_TO_JSVAL(msg.to_agent)
419
		,NULL,NULL,JSPROP_ENUMERATE);
420
	JS_DefineProperty(cx, hdrobj, "from_agent",INT_TO_JSVAL(msg.from_agent)
421
		,NULL,NULL,JSPROP_ENUMERATE);
422
	JS_DefineProperty(cx, hdrobj, "replyto_agent",INT_TO_JSVAL(msg.replyto_agent)
423
		,NULL,NULL,JSPROP_ENUMERATE);
424 425

	JS_DefineProperty(cx, hdrobj, "to_net_type",INT_TO_JSVAL(msg.to_net.type)
426
		,NULL,NULL,JSPROP_ENUMERATE);
427 428 429 430
	if(msg.to_net.type)
		JS_DefineProperty(cx, hdrobj, "to_net_addr"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.to_net)))
			,NULL,NULL,JSPROP_ENUMERATE);
431 432

	JS_DefineProperty(cx, hdrobj, "from_net_type",INT_TO_JSVAL(msg.from_net.type)
433
		,NULL,NULL,JSPROP_ENUMERATE);
434 435 436 437
	if(msg.from_net.type)
		JS_DefineProperty(cx, hdrobj, "from_net_addr"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.from_net)))
			,NULL,NULL,JSPROP_ENUMERATE);
438 439

	JS_DefineProperty(cx, hdrobj, "replyto_net_type",INT_TO_JSVAL(msg.replyto_net.type)
440
		,NULL,NULL,JSPROP_ENUMERATE);
441 442 443 444
	if(msg.replyto_net.type)
		JS_DefineProperty(cx, hdrobj, "replyto_net_addr"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.replyto_net)))
			,NULL,NULL,JSPROP_ENUMERATE);
445 446

	JS_DefineProperty(cx, hdrobj, "forwarded",INT_TO_JSVAL(msg.forwarded)
447
		,NULL,NULL,JSPROP_ENUMERATE);
448
	JS_DefineProperty(cx, hdrobj, "expiration",INT_TO_JSVAL(msg.expiration)
449
		,NULL,NULL,JSPROP_ENUMERATE);
450 451 452 453 454
	JS_DefineProperty(cx, hdrobj, "priority",INT_TO_JSVAL(msg.priority)
		,NULL,NULL,JSPROP_ENUMERATE);
	JS_DefineProperty(cx, hdrobj, "cost",INT_TO_JSVAL(msg.cost)
		,NULL,NULL,JSPROP_ENUMERATE);

455 456

	JS_DefineProperty(cx, hdrobj, "type", INT_TO_JSVAL(msg.hdr.type)
457
		,NULL,NULL,JSPROP_ENUMERATE);
458
	JS_DefineProperty(cx, hdrobj, "version", INT_TO_JSVAL(msg.hdr.version)
459
		,NULL,NULL,JSPROP_ENUMERATE);
460
	JS_DefineProperty(cx, hdrobj, "attr", INT_TO_JSVAL(msg.hdr.attr)
461
		,NULL,NULL,JSPROP_ENUMERATE);
462
	JS_DefineProperty(cx, hdrobj, "auxattr", INT_TO_JSVAL(msg.hdr.auxattr)
463
		,NULL,NULL,JSPROP_ENUMERATE);
464
	JS_DefineProperty(cx, hdrobj, "netattr", INT_TO_JSVAL(msg.hdr.netattr)
465
		,NULL,NULL,JSPROP_ENUMERATE);
466 467

	JS_DefineProperty(cx, hdrobj, "when_written_time", INT_TO_JSVAL(msg.hdr.when_written.time)
468
		,NULL,NULL,JSPROP_ENUMERATE);
469
	JS_DefineProperty(cx, hdrobj, "when_written_zone", INT_TO_JSVAL(msg.hdr.when_written.zone)
470
		,NULL,NULL,JSPROP_ENUMERATE);
471
	JS_DefineProperty(cx, hdrobj, "when_imported_time", INT_TO_JSVAL(msg.hdr.when_imported.time)
472
		,NULL,NULL,JSPROP_ENUMERATE);
473
	JS_DefineProperty(cx, hdrobj, "when_imported_zone", INT_TO_JSVAL(msg.hdr.when_imported.zone)
474
		,NULL,NULL,JSPROP_ENUMERATE);
475 476

	JS_DefineProperty(cx, hdrobj, "thread_orig", INT_TO_JSVAL(msg.hdr.thread_orig)
477
		,NULL,NULL,JSPROP_ENUMERATE);
478
	JS_DefineProperty(cx, hdrobj, "thread_next", INT_TO_JSVAL(msg.hdr.thread_next)
479
		,NULL,NULL,JSPROP_ENUMERATE);
480
	JS_DefineProperty(cx, hdrobj, "thread_first", INT_TO_JSVAL(msg.hdr.thread_first)
481
		,NULL,NULL,JSPROP_ENUMERATE);
482 483

	JS_DefineProperty(cx, hdrobj, "delivery_attempts", INT_TO_JSVAL(msg.hdr.delivery_attempts)
484
		,NULL,NULL,JSPROP_ENUMERATE);
485 486 487 488
	JS_DefineProperty(cx, hdrobj, "last_downloaded", INT_TO_JSVAL(msg.hdr.last_downloaded)
		,NULL,NULL,JSPROP_ENUMERATE);
	JS_DefineProperty(cx, hdrobj, "times_downloaded", INT_TO_JSVAL(msg.hdr.times_downloaded)
		,NULL,NULL,JSPROP_ENUMERATE);
489 490 491

	l=smb_getmsgdatlen(&msg);
	JS_DefineProperty(cx, hdrobj, "data_length", INT_TO_JSVAL(l)
492
		,NULL,NULL,JSPROP_ENUMERATE);
493

494 495
	JS_DefineProperty(cx, hdrobj, "date"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msgdate(msg.hdr.when_written,date)))
496
		,NULL,NULL,JSPROP_ENUMERATE);
497

498
	/* Reply-ID (References) */
499
	if(msg.reply_id!=NULL)
500 501 502
		val=msg.reply_id;
	else {
		reply_id[0]=0;
503
		if(msg.hdr.thread_orig) {
504 505 506 507 508
			memset(&orig_msg,0,sizeof(orig_msg));
			orig_msg.hdr.number=msg.hdr.thread_orig;
			if(smb_getmsgidx(&(p->smb), &orig_msg))
				sprintf(reply_id,"<%s>",p->smb.last_error);
			else
509
				SAFECOPY(reply_id,get_msgid(scfg,p->smb.subnum,&orig_msg));
510
		}
511 512
		val=reply_id;
	}
513
	if(val[0])
514 515
		JS_DefineProperty(cx, hdrobj, "reply_id"
			, STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(val)))
516
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
517 518

	/* Message-ID */
519 520
	SAFECOPY(msg_id,get_msgid(scfg,p->smb.subnum,&msg));
	val=msg_id;
521 522
	JS_DefineProperty(cx, hdrobj, "id"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(val)))
523 524
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

525 526
	/* USENET Fields */
	if(msg.path!=NULL)
527 528
		JS_DefineProperty(cx, hdrobj, "path"
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.path)))
529
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
530 531
	if(msg.newsgroups!=NULL)
		JS_DefineProperty(cx, hdrobj, "newsgroups"
532
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.newsgroups)))
533 534
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

535 536
	/* FidoNet Header Fields */
	if(msg.ftn_msgid!=NULL)
537
		JS_DefineProperty(cx, hdrobj, "ftn_msgid"
538
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.ftn_msgid)))
539
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
540
	if(msg.ftn_reply!=NULL)
541
		JS_DefineProperty(cx, hdrobj, "ftn_reply"
542
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.ftn_reply)))
543 544
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_pid!=NULL)
545
		JS_DefineProperty(cx, hdrobj, "ftn_pid"
546
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.ftn_pid)))
547
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
548 549
	if(msg.ftn_tid!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_tid"
550
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.ftn_tid)))
551
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
552
	if(msg.ftn_area!=NULL)
553
		JS_DefineProperty(cx, hdrobj, "ftn_area"
554
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.ftn_area)))
555
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
556
	if(msg.ftn_flags!=NULL)
557
		JS_DefineProperty(cx, hdrobj, "ftn_flags"
558
			,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,truncsp(msg.ftn_flags)))
559 560
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

rswindell's avatar
rswindell committed
561 562
	smb_freemsgmem(&msg);

563 564 565 566 567
	*rval = OBJECT_TO_JSVAL(hdrobj);

	return(JS_TRUE);
}

568 569 570 571 572 573 574 575 576
static JSBool
js_put_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	smbmsg_t	msg;
	JSObject*	hdr;
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

577
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
578
		JS_ReportError(cx,getprivate_failure,WHERE);
579
		return(JS_FALSE);
580
	}
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

	if(!JSVAL_IS_OBJECT(argv[2]))
		return(JS_TRUE);
	hdr = JSVAL_TO_OBJECT(argv[2]);

	if(!SMB_IS_OPEN(&(p->smb)))
		return(JS_TRUE);

	memset(&msg,0,sizeof(msg));

	if(JSVAL_TO_BOOLEAN(argv[0])==JS_TRUE)	/* Get by offset */
		msg.offset=JSVAL_TO_INT(argv[1]);
	else									/* Get by number */
		msg.hdr.number=JSVAL_TO_INT(argv[1]);

	if(smb_getmsgidx(&(p->smb), &msg)!=0)
		return(JS_TRUE);

599
	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
600 601
		return(JS_TRUE);

602 603 604
	do {
		if(smb_getmsghdr(&(p->smb), &msg)!=0)
			break;
605

606
		if(!parse_header_object(cx, p, hdr, &msg))
607 608
			break;

609
		if(smb_putmsg(&(p->smb), &msg)!=0)
610 611 612 613 614 615
			break;

		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);
	} while(0);

	smb_unlockmsghdr(&(p->smb),&msg); 
rswindell's avatar
rswindell committed
616
	smb_freemsgmem(&msg);
617 618 619 620

	return(JS_TRUE);
}

621
static char* get_msg_text(smb_t* smb, smbmsg_t* msg, BOOL strip_ctrl_a, BOOL rfc822, ulong mode)
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
{
	char*		buf;

	if(smb_getmsgidx(smb, msg)!=0)
		return(NULL);

	if(smb_lockmsghdr(smb,msg)!=0)
		return(NULL);

	if(smb_getmsghdr(smb, msg)!=0) {
		smb_unlockmsghdr(smb, msg); 
		return(NULL);
	}

	if((buf=smb_getmsgtxt(smb, msg, mode))==NULL) {
		smb_unlockmsghdr(smb,msg); 
rswindell's avatar
rswindell committed
638
		smb_freemsgmem(msg);
639 640 641 642
		return(NULL);
	}

	smb_unlockmsghdr(smb, msg); 
rswindell's avatar
rswindell committed
643
	smb_freemsgmem(msg);
644 645 646 647 648 649

	if(strip_ctrl_a) {
		char* newbuf;
		if((newbuf=malloc(strlen(buf)+1))!=NULL) {
			int i,j;
			for(i=j=0;buf[i];i++) {
650
				if(buf[i]==CTRL_A && buf[i+1]!=0)
651 652 653 654 655 656 657 658
					i++;
				else newbuf[j++]=buf[i]; 
			}
			newbuf[j]=0;
			strcpy(buf,newbuf);
			free(newbuf);
		}
	}
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

	if(rfc822) {	/* must escape lines starting with dot ('.') */
		char* newbuf;
		if((newbuf=malloc((strlen(buf)*2)+1))!=NULL) {
			int i,j;
			for(i=j=0;buf[i];i++) {
				if((i==0 || buf[i-1]=='\n') && buf[i]=='.')
					newbuf[j++]='.';
				newbuf[j++]=buf[i]; 
			}
			newbuf[j]=0;
			free(buf);
			buf = newbuf;
		}
	}

675 676 677
	return(buf);
}

678 679 680 681 682 683
static JSBool
js_get_msg_body(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		buf;
	smbmsg_t	msg;
	JSBool		strip_ctrl_a=JS_FALSE;
684
	JSBool		tails=JS_TRUE;
685
	JSBool		rfc822=JS_FALSE;
686 687 688 689
	private_t*	p;

	*rval = JSVAL_NULL;
	
690
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
691
		JS_ReportError(cx,getprivate_failure,WHERE);
692
		return(JS_FALSE);
693
	}
694

695 696 697
	if(!SMB_IS_OPEN(&(p->smb)))
		return(JS_TRUE);

698 699 700 701 702 703 704 705 706 707
	memset(&msg,0,sizeof(msg));

	if(JSVAL_TO_BOOLEAN(argv[0])==JS_TRUE)	/* Get by offset */
		msg.offset=JSVAL_TO_INT(argv[1]);
	else									/* Get by number */
		msg.hdr.number=JSVAL_TO_INT(argv[1]);

	if(argc>2)
		strip_ctrl_a=JSVAL_TO_BOOLEAN(argv[2]);

708
	if(argc>3)
709
		rfc822=JSVAL_TO_BOOLEAN(argv[3]);
710

711 712
	if(argc>4)
		tails=JSVAL_TO_BOOLEAN(argv[4]);
713

714
	buf = get_msg_text(&(p->smb), &msg, strip_ctrl_a, rfc822, tails ? GETMSGTXT_TAILS : 0);
715
	if(buf==NULL)
716 717
		return(JS_TRUE);

718
	*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,buf));
719

720
	smb_freemsgtxt(buf);
721

722 723 724 725 726 727 728 729 730
	return(JS_TRUE);
}

static JSBool
js_get_msg_tail(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		buf;
	smbmsg_t	msg;
	JSBool		strip_ctrl_a=JS_FALSE;
731
	JSBool		rfc822=JS_FALSE;
732 733 734 735
	private_t*	p;

	*rval = JSVAL_NULL;
	
736
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
737
		JS_ReportError(cx,getprivate_failure,WHERE);
738
		return(JS_FALSE);
739
	}
740 741

	if(!SMB_IS_OPEN(&(p->smb)))
742 743
		return(JS_TRUE);

744
	memset(&msg,0,sizeof(msg));
745

746 747 748 749 750 751 752 753
	if(JSVAL_TO_BOOLEAN(argv[0])==JS_TRUE)	/* Get by offset */
		msg.offset=JSVAL_TO_INT(argv[1]);
	else									/* Get by number */
		msg.hdr.number=JSVAL_TO_INT(argv[1]);

	if(argc>2)
		strip_ctrl_a=JSVAL_TO_BOOLEAN(argv[2]);

754 755 756 757
	if(argc>3)
		rfc822=JSVAL_TO_BOOLEAN(argv[3]);

	buf = get_msg_text(&(p->smb), &msg, strip_ctrl_a, rfc822, GETMSGTXT_TAILS|GETMSGTXT_NO_BODY);
758 759
	if(buf==NULL)
		return(JS_TRUE);
760 761 762 763 764 765 766 767

	*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,buf));

	smb_freemsgtxt(buf);

	return(JS_TRUE);
}

rswindell's avatar
rswindell committed
768 769 770 771 772 773
static JSBool
js_save_msg(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		body;
	JSObject*	hdr;
	smbmsg_t	msg;
774
	jsval		open_rval;
rswindell's avatar
rswindell committed
775 776 777 778 779 780 781
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

	if(argc<2)
		return(JS_TRUE);
	
782
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
783
		JS_ReportError(cx,getprivate_failure,WHERE);
rswindell's avatar
rswindell committed
784
		return(JS_FALSE);
785
	}
rswindell's avatar
rswindell committed
786

787 788 789 790 791 792
	if(!SMB_IS_OPEN(&(p->smb))) {
		if(!js_open(cx, obj, 0, NULL, &open_rval))
			return(JS_FALSE);
		if(open_rval == JSVAL_FALSE)
			return(JS_TRUE);
	}
793

rswindell's avatar
rswindell committed
794 795 796 797 798 799 800 801
	memset(&msg,0,sizeof(msg));

	if(!JSVAL_IS_OBJECT(argv[0]))
		return(JS_TRUE);
	hdr = JSVAL_TO_OBJECT(argv[0]);

	if(!JSVAL_IS_STRING(argv[1]))
		return(JS_TRUE);
802
	if((body=JS_GetStringBytes(JSVAL_TO_STRING(argv[1])))==NULL) {
803
		JS_ReportError(cx,"JS_GetStringBytes failed");
rswindell's avatar
rswindell committed
804
		return(JS_FALSE);
805
	}
rswindell's avatar
rswindell committed
806

807
	if(!parse_header_object(cx, p, hdr, &msg))
808
		return(JS_TRUE);
rswindell's avatar
rswindell committed
809

810 811 812 813
	if(msg.hdr.when_written.time==0) {
		msg.hdr.when_written.time=time(NULL);
		msg.hdr.when_written.zone=scfg->sys_timezone;
	}
rswindell's avatar
rswindell committed
814

815
	truncsp(body);
816
	if(savemsg(scfg, &(p->smb), &msg, body)==0)
rswindell's avatar
rswindell committed
817 818 819 820 821
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);

	return(JS_TRUE);
}

822 823 824 825 826 827
/* MsgBase Object Properites */
enum {
	 SMB_PROP_LAST_ERROR
	,SMB_PROP_FILE		
	,SMB_PROP_DEBUG		
	,SMB_PROP_RETRY_TIME
rswindell's avatar
rswindell committed
828
	,SMB_PROP_FIRST_MSG		// first message number
829 830 831 832 833 834
	,SMB_PROP_LAST_MSG		// last message number
	,SMB_PROP_TOTAL_MSGS 	// total messages
	,SMB_PROP_MAX_CRCS		// Maximum number of CRCs to keep in history
    ,SMB_PROP_MAX_MSGS      // Maximum number of message to keep in sub
    ,SMB_PROP_MAX_AGE       // Maximum age of message to keep in sub (in days)
	,SMB_PROP_ATTR			// Attributes for this message base (SMB_HYPER,etc)
rswindell's avatar
rswindell committed
835
	,SMB_PROP_SUBNUM		// sub-board number
836
	,SMB_PROP_IS_OPEN
837 838 839 840 841 842 843
};

static JSBool js_msgbase_set(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
    jsint       tiny;
	private_t*	p;

844
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
845
		JS_ReportError(cx,getprivate_failure,WHERE);
846
		return(JS_FALSE);
847
	}
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

    tiny = JSVAL_TO_INT(id);

	switch(tiny) {
		case SMB_PROP_RETRY_TIME:
			p->smb.retry_time = JSVAL_TO_BOOLEAN(*vp);
			break;
		case SMB_PROP_DEBUG:
			p->debug = JSVAL_TO_BOOLEAN(*vp);
			break;
	}

	return(JS_TRUE);
}

static JSBool js_msgbase_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
    jsint       tiny;
rswindell's avatar
rswindell committed
866
	idxrec_t	idx;
867 868
	private_t*	p;

869
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
870
		JS_ReportError(cx,getprivate_failure,WHERE);
871
		return(JS_FALSE);
872
	}
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

    tiny = JSVAL_TO_INT(id);

	switch(tiny) {
		case SMB_PROP_FILE:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, p->smb.file));
			break;
		case SMB_PROP_LAST_ERROR:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, p->smb.last_error));
			break;
		case SMB_PROP_RETRY_TIME:
			*vp = INT_TO_JSVAL(p->smb.retry_time);
			break;
		case SMB_PROP_DEBUG:
			*vp = INT_TO_JSVAL(p->debug);
			break;
rswindell's avatar
rswindell committed
889 890 891 892 893
		case SMB_PROP_FIRST_MSG:
			memset(&idx,0,sizeof(idx));
			smb_getfirstidx(&(p->smb),&idx);
			*vp = INT_TO_JSVAL(idx.number);
			break;
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908