js_msgbase.c 23 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/* 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;



/* MsgBase Constructor (open message base) */

static JSBool
js_msgbase_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		code;
	JSString*	js_str;
	private_t*	p;

	*rval = JSVAL_VOID;

	if((p=(private_t*)malloc(sizeof(private_t)))==NULL) 
		return(JS_FALSE);

	memset(&(p->smb),0,sizeof(smb_t));
	js_str = JS_ValueToString(cx, argv[0]);
	code = JS_GetStringBytes(js_str);
rswindell's avatar
rswindell committed
70
	if(stricmp(code,"mail")==0) {
71
		p->smb.subnum=INVALID_SUB;
72
		snprintf(p->smb.file,sizeof(p->smb.file),"%s%s",scfg->data_dir,code);
rswindell's avatar
rswindell committed
73
	} else {
74 75
		for(p->smb.subnum=0;p->smb.subnum<scfg->total_subs;p->smb.subnum++)
			if(!stricmp(scfg->sub[p->smb.subnum]->code,code))
rswindell's avatar
rswindell committed
76
				break;
77
		if(p->smb.subnum>=scfg->total_subs)	/* unknown code */
rswindell's avatar
rswindell committed
78
			return(JS_TRUE);
79
		snprintf(p->smb.file,sizeof(p->smb.file),"%s%s"
80
			,scfg->sub[p->smb.subnum]->data_dir,code);
rswindell's avatar
rswindell committed
81
	}
82 83 84 85 86 87

	if(argc>1)
		p->smb.retry_time=JSVAL_TO_INT(argv[0]);
	else
		p->smb.retry_time=scfg->smb_retry_time;

88
	if(smb_open(&(p->smb))!=0) 
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
		return(JS_FALSE);

	p->debug=JS_FALSE;

	if(!JS_SetPrivate(cx, obj, p)) 
		return(JS_FALSE);

	return(JS_TRUE);
}

/* 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 */

static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	private_t* p;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

	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		*/
	,js_finalize_msgbase	/* finalize		*/
};

145
static BOOL parse_header_object(JSContext* cx, JSObject* hdr, uint subnum, smbmsg_t* msg)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
{
	char*		cp;
	ushort		nettype;
	JSString*	js_str;
	jsval		val;

	/* Required Header Fields */
	if(JS_GetProperty(cx, hdr, "subject", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
	} else
		cp="";
	smb_hfield(msg, SUBJECT, (ushort)strlen(cp), cp);
	msg->idx.subj=crc16(cp);

	if(JS_GetProperty(cx, hdr, "to", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
	} else {
		if(subnum==INVALID_SUB)	/* e-mail */
			return(FALSE);		/* "to" property required */
		cp="All";
	}
	smb_hfield(msg, RECIPIENT, (ushort)strlen(cp), cp);
	if(subnum!=INVALID_SUB)
		msg->idx.to=crc16(cp);

	if(JS_GetProperty(cx, hdr, "from", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
	} else
		return(FALSE);	/* "from" property required */
	smb_hfield(msg, SENDER, (ushort)strlen(cp), cp);
	if(subnum!=INVALID_SUB)
		msg->idx.from=crc16(cp);

	/* Optional Header Fields */
	if(JS_GetProperty(cx, hdr, "from_ext", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		smb_hfield(msg, SENDEREXT, (ushort)strlen(cp), cp);
		if(subnum==INVALID_SUB)
			msg->idx.from=atoi(cp);
	}

	if(JS_GetProperty(cx, hdr, "from_net_type", &val) && val!=JSVAL_VOID) {
		nettype=(ushort)JSVAL_TO_INT(val);
		smb_hfield(msg, SENDERNETTYPE, sizeof(nettype), &nettype);
		if(subnum==INVALID_SUB && nettype!=NET_NONE)
			msg->idx.from=0;
	}

	if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		smb_hfield(msg, SENDERNETADDR, (ushort)strlen(cp), cp);
	}

	if(JS_GetProperty(cx, hdr, "to_ext", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		smb_hfield(msg, RECIPIENTEXT, (ushort)strlen(cp), cp);
		if(subnum==INVALID_SUB)
			msg->idx.to=atoi(cp);
	}

	if(JS_GetProperty(cx, hdr, "to_net_type", &val) && val!=JSVAL_VOID) {
		nettype=(ushort)JSVAL_TO_INT(val);
		smb_hfield(msg, RECIPIENTNETTYPE, sizeof(nettype), &nettype);
		if(subnum==INVALID_SUB && nettype!=NET_NONE)
			msg->idx.to=0;
	}

	if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		smb_hfield(msg, RECIPIENTNETADDR, (ushort)strlen(cp), cp);
	}
238 239 240 241 242 243 244 245

	if(JS_GetProperty(cx, hdr, "date", &val) && val!=JSVAL_VOID) {
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		msg->hdr.when_written=rfc822date(cp);
	}
246 247 248 249 250 251 252 253 254 255
	
	/* Numeric Header Fields */
	if(JS_GetProperty(cx, hdr, "attr", &val) && val!=JSVAL_VOID) 
		msg->hdr.attr=(ushort)JSVAL_TO_INT(val);
	if(JS_GetProperty(cx, hdr, "auxattr", &val) && val!=JSVAL_VOID) 
		msg->hdr.auxattr=JSVAL_TO_INT(val);
	if(JS_GetProperty(cx, hdr, "netattr", &val) && val!=JSVAL_VOID) 
		msg->hdr.netattr=JSVAL_TO_INT(val);
	if(JS_GetProperty(cx, hdr, "when_written_time", &val) && val!=JSVAL_VOID) 
		msg->hdr.when_written.time=JSVAL_TO_INT(val);
256
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && val!=JSVAL_VOID) 
257 258 259
		msg->hdr.when_written.zone=(short)JSVAL_TO_INT(val);
	if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && val!=JSVAL_VOID) 
		msg->hdr.when_imported.time=JSVAL_TO_INT(val);
260
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && val!=JSVAL_VOID) 
261 262 263 264 265
		msg->hdr.when_imported.zone=(short)JSVAL_TO_INT(val);

	return(TRUE);
}

266 267 268
static JSBool
js_get_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
269
	char		date[128];
270 271 272
	char		msg_id[128];
	char		reply_id[128];
	char*		val;
273 274 275 276 277 278 279 280 281 282
	ulong		l;
	smbmsg_t	msg;
	JSObject*	hdrobj;
	private_t*	p;

	*rval = JSVAL_NULL;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

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

286 287 288 289 290 291 292 293 294 295
	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);

296
	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
297 298
		return(JS_TRUE);

299 300 301 302 303 304 305
	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
306 307
	if((hdrobj=JS_NewObject(cx,&js_msghdr_class,NULL,obj))==NULL) {
		smb_freemsgmem(&msg);
308
		return(JS_TRUE);
rswindell's avatar
rswindell committed
309
	}
310 311

	JS_DefineProperty(cx, hdrobj, "number", INT_TO_JSVAL(msg.hdr.number)
312
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
313
	JS_DefineProperty(cx, hdrobj, "to",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.to))
314
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
315
	JS_DefineProperty(cx, hdrobj, "from",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.from))
316
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
317
	JS_DefineProperty(cx, hdrobj, "subject",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.subj))
318
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
319
	JS_DefineProperty(cx, hdrobj, "to_ext",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.to_ext))
320
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
321
	JS_DefineProperty(cx, hdrobj, "from_ext",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.from_ext))
322
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
323
	JS_DefineProperty(cx, hdrobj, "replyto",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.replyto))
324
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
325
	JS_DefineProperty(cx, hdrobj, "replyto_ext",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.replyto_ext))
326
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
327
	JS_DefineProperty(cx, hdrobj, "to_agent",INT_TO_JSVAL(msg.to_agent)
328
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
329
	JS_DefineProperty(cx, hdrobj, "from_agent",INT_TO_JSVAL(msg.from_agent)
330
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
331
	JS_DefineProperty(cx, hdrobj, "replyto_agent",INT_TO_JSVAL(msg.replyto_agent)
332
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
333 334

	JS_DefineProperty(cx, hdrobj, "to_net_type",INT_TO_JSVAL(msg.to_net.type)
335
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
336
	JS_DefineProperty(cx, hdrobj, "to_net_addr"
337
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.to_net)))
338
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
339 340

	JS_DefineProperty(cx, hdrobj, "from_net_type",INT_TO_JSVAL(msg.from_net.type)
341
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
342
	JS_DefineProperty(cx, hdrobj, "from_net_addr"
343
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.from_net)))
344
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
345 346

	JS_DefineProperty(cx, hdrobj, "replyto_net_type",INT_TO_JSVAL(msg.replyto_net.type)
347
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
348
	JS_DefineProperty(cx, hdrobj, "replyto_net_addr"
349
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.replyto_net)))
350
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
351 352

	JS_DefineProperty(cx, hdrobj, "forwarded",INT_TO_JSVAL(msg.forwarded)
353
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
354
	JS_DefineProperty(cx, hdrobj, "expiration_time",INT_TO_JSVAL(msg.expiration.time)
355
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
356
	JS_DefineProperty(cx, hdrobj, "expiration_zone",INT_TO_JSVAL(msg.expiration.zone)
357
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
358 359

	JS_DefineProperty(cx, hdrobj, "type", INT_TO_JSVAL(msg.hdr.type)
360
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
361
	JS_DefineProperty(cx, hdrobj, "version", INT_TO_JSVAL(msg.hdr.version)
362
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
363
	JS_DefineProperty(cx, hdrobj, "attr", INT_TO_JSVAL(msg.hdr.attr)
364
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
365
	JS_DefineProperty(cx, hdrobj, "auxattr", INT_TO_JSVAL(msg.hdr.auxattr)
366
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
367
	JS_DefineProperty(cx, hdrobj, "netattr", INT_TO_JSVAL(msg.hdr.netattr)
368
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
369 370

	JS_DefineProperty(cx, hdrobj, "when_written_time", INT_TO_JSVAL(msg.hdr.when_written.time)
371
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
372
	JS_DefineProperty(cx, hdrobj, "when_written_zone", INT_TO_JSVAL(msg.hdr.when_written.zone)
373
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
374
	JS_DefineProperty(cx, hdrobj, "when_imported_time", INT_TO_JSVAL(msg.hdr.when_imported.time)
375
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
376
	JS_DefineProperty(cx, hdrobj, "when_imported_zone", INT_TO_JSVAL(msg.hdr.when_imported.zone)
377
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
378 379

	JS_DefineProperty(cx, hdrobj, "thread_orig", INT_TO_JSVAL(msg.hdr.thread_orig)
380
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
381
	JS_DefineProperty(cx, hdrobj, "thread_next", INT_TO_JSVAL(msg.hdr.thread_next)
382
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
383
	JS_DefineProperty(cx, hdrobj, "thread_first", INT_TO_JSVAL(msg.hdr.thread_first)
384
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
385 386

	JS_DefineProperty(cx, hdrobj, "delivery_attempts", INT_TO_JSVAL(msg.hdr.delivery_attempts)
387
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
388 389 390

	l=smb_getmsgdatlen(&msg);
	JS_DefineProperty(cx, hdrobj, "data_length", INT_TO_JSVAL(l)
391
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
392

393 394 395 396
	JS_DefineProperty(cx, hdrobj, "date"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msgdate(msg.hdr.when_written,date)))
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

397 398 399 400 401 402 403 404 405 406 407
	/* Reply-ID (References) */
	if(msg.reply_id!=NULL)
		val=msg.reply_id;
	else {
		reply_id[0]=0;
		if(p->smb.subnum!=INVALID_SUB && msg.hdr.thread_orig)
			sprintf(reply_id,"<%lu.%s@%s>"
				,msg.hdr.thread_orig,scfg->sub[p->smb.subnum]->code,scfg->sys_inetaddr);
		val=reply_id;
	}
	JS_DefineProperty(cx, hdrobj, "reply_id", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
408 409 410
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

	/* Message-ID */
411 412 413 414 415
	if(msg.id!=NULL)
		val=msg.id;
	else {
		if(p->smb.subnum==INVALID_SUB)
			sprintf(msg_id,"<%08lX.%lu@%s>"
416 417
				,msg.hdr.when_written.time
				,msg.idx.number,scfg->sys_inetaddr);
418
		else
419 420
			sprintf(msg_id,"<%08lX.%lu.%s@%s>"
				,msg.hdr.when_written.time
421 422 423 424
				,msg.idx.number,scfg->sub[p->smb.subnum]->code,scfg->sys_inetaddr);
		val=msg_id;
	}
	JS_DefineProperty(cx, hdrobj, "id", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
425 426
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

rswindell's avatar
rswindell committed
427 428
	smb_freemsgmem(&msg);

429 430 431 432 433
	*rval = OBJECT_TO_JSVAL(hdrobj);

	return(JS_TRUE);
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
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);

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

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

463
	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
464 465
		return(JS_TRUE);

466 467 468
	do {
		if(smb_getmsghdr(&(p->smb), &msg)!=0)
			break;
469

470
		if(!parse_header_object(cx, hdr, p->smb.subnum, &msg))
471 472 473 474 475 476 477 478 479
			break;

		if(smb_putmsghdr(&(p->smb), &msg)!=0)
			break;

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

	smb_unlockmsghdr(&(p->smb),&msg); 
rswindell's avatar
rswindell committed
480
	smb_freemsgmem(&msg);
481 482 483 484

	return(JS_TRUE);
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
static char* get_msg_text(smb_t* smb, smbmsg_t* msg, BOOL strip_ctrl_a, ulong mode)
{
	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
502
		smb_freemsgmem(msg);
503 504 505 506
		return(NULL);
	}

	smb_unlockmsghdr(smb, msg); 
rswindell's avatar
rswindell committed
507
	smb_freemsgmem(msg);
508 509 510 511 512 513

	if(strip_ctrl_a) {
		char* newbuf;
		if((newbuf=malloc(strlen(buf)+1))!=NULL) {
			int i,j;
			for(i=j=0;buf[i];i++) {
514
				if(buf[i]==CTRL_A && buf[i+1]!=0)
515 516 517 518 519 520 521 522 523 524 525
					i++;
				else newbuf[j++]=buf[i]; 
			}
			newbuf[j]=0;
			strcpy(buf,newbuf);
			free(newbuf);
		}
	}
	return(buf);
}

526 527 528 529 530 531
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;
532
	JSBool		tails=JS_TRUE;
533 534 535 536 537 538 539
	private_t*	p;

	*rval = JSVAL_NULL;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

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

543 544 545 546 547 548 549 550 551 552
	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]);

553 554 555 556
	if(argc>3)
		tails=JSVAL_TO_BOOLEAN(argv[3]);


557 558
	buf = get_msg_text(&(p->smb), &msg, strip_ctrl_a, tails ? GETMSGTXT_TAILS : 0);
	if(buf==NULL)
559 560
		return(JS_TRUE);

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

563
	smb_freemsgtxt(buf);
564

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	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;
	private_t*	p;

	*rval = JSVAL_NULL;
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

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

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

586 587 588 589 590 591 592 593 594 595 596
	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]);

	buf = get_msg_text(&(p->smb), &msg, strip_ctrl_a, GETMSGTXT_TAILS|GETMSGTXT_NO_BODY);
	if(buf==NULL)
		return(JS_TRUE);
597 598 599 600 601 602 603 604

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

	smb_freemsgtxt(buf);

	return(JS_TRUE);
}

rswindell's avatar
rswindell committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
static JSBool
js_save_msg(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	char*		body;
	JSString*	js_str;
	JSObject*	hdr;
	smbmsg_t	msg;
	private_t*	p;

	*rval = BOOLEAN_TO_JSVAL(JS_FALSE);

	if(argc<2)
		return(JS_TRUE);
	
	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

	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);
	if((js_str=JS_ValueToString(cx,argv[1]))==NULL)
		return(JS_FALSE);
	if((body=JS_GetStringBytes(js_str))==NULL)
		return(JS_FALSE);

635
	if(!parse_header_object(cx, hdr, p->smb.subnum, &msg))
636
		return(JS_TRUE);
rswindell's avatar
rswindell committed
637

638 639 640 641
	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
642

643
	truncsp(body);
644
	if(savemsg(scfg, &(p->smb), &msg, body)==0)
rswindell's avatar
rswindell committed
645 646 647 648 649 650
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);

	return(JS_TRUE);
}


651 652 653 654 655 656
/* MsgBase Object Properites */
enum {
	 SMB_PROP_LAST_ERROR
	,SMB_PROP_FILE		
	,SMB_PROP_DEBUG		
	,SMB_PROP_RETRY_TIME
rswindell's avatar
rswindell committed
657
	,SMB_PROP_FIRST_MSG		// first message number
658 659 660 661 662 663
	,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
664
	,SMB_PROP_SUBNUM		// sub-board number
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
};

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

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

    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
692
	idxrec_t	idx;
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	private_t*	p;

	if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
		return(JS_FALSE);

    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
713 714 715 716 717
		case SMB_PROP_FIRST_MSG:
			memset(&idx,0,sizeof(idx));
			smb_getfirstidx(&(p->smb),&idx);
			*vp = INT_TO_JSVAL(idx.number);
			break;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
		case SMB_PROP_LAST_MSG:
			smb_getstatus(&(p->smb));
			*vp = INT_TO_JSVAL(p->smb.status.last_msg);
			break;
		case SMB_PROP_TOTAL_MSGS:
			smb_getstatus(&(p->smb));
			*vp = INT_TO_JSVAL(p->smb.status.total_msgs);
			break;
		case SMB_PROP_MAX_CRCS:
			*vp = INT_TO_JSVAL(p->smb.status.max_crcs);
			break;
		case SMB_PROP_MAX_MSGS:
			*vp = INT_TO_JSVAL(p->smb.status.max_msgs);
			break;
		case SMB_PROP_MAX_AGE:
			*vp = INT_TO_JSVAL(p->smb.status.max_age);
			break;
		case SMB_PROP_ATTR:
			*vp = INT_TO_JSVAL(p->smb.status.attr);
			break;
rswindell's avatar
rswindell committed
738 739 740 741
		case SMB_PROP_SUBNUM:
			*vp = INT_TO_JSVAL(p->smb.subnum);
			break;

742 743 744 745 746 747 748 749 750 751 752 753 754 755
	}

	return(JS_TRUE);
}

#define SMB_PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY

static struct JSPropertySpec js_msgbase_properties[] = {
/*		 name				,tinyid					,flags,				getter,	setter	*/

	{	"last_error"		,SMB_PROP_LAST_ERROR	,SMB_PROP_FLAGS,	NULL,NULL},
	{	"file"				,SMB_PROP_FILE			,SMB_PROP_FLAGS,	NULL,NULL},
	{	"debug"				,SMB_PROP_DEBUG			,JSPROP_ENUMERATE,	NULL,NULL},
	{	"retry_time"		,SMB_PROP_RETRY_TIME	,JSPROP_ENUMERATE,	NULL,NULL},
rswindell's avatar
rswindell committed
756 757 758 759 760 761 762
	{	"first_msg"			,SMB_PROP_FIRST_MSG		,SMB_PROP_FLAGS,	NULL,NULL},
	{	"last_msg"			,SMB_PROP_LAST_MSG		,SMB_PROP_FLAGS,	NULL,NULL},
	{	"total_msgs"		,SMB_PROP_TOTAL_MSGS	,SMB_PROP_FLAGS,	NULL,NULL},
	{	"max_crcs"			,SMB_PROP_MAX_CRCS		,SMB_PROP_FLAGS,	NULL,NULL},
	{	"max_msgs"			,SMB_PROP_MAX_MSGS  	,SMB_PROP_FLAGS,	NULL,NULL},
	{	"max_age"			,SMB_PROP_MAX_AGE   	,SMB_PROP_FLAGS,	NULL,NULL},
	{	"attributes"		,SMB_PROP_ATTR			,SMB_PROP_FLAGS,	NULL,NULL},
rswindell's avatar
rswindell committed
763
	{	"subnum"			,SMB_PROP_SUBNUM		,SMB_PROP_FLAGS,	NULL,NULL},
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	{0}
};

static JSClass js_msgbase_class = {
     "MsgBase"				/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_msgbase_get			/* getProperty	*/
	,js_msgbase_set			/* setProperty	*/
	,JS_EnumerateStub		/* enumerate	*/
	,JS_ResolveStub			/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,js_finalize_msgbase	/* finalize		*/
};

static JSFunctionSpec js_msgbase_functions[] = {
	{"close",			js_close,			0},		/* close msgbase */
782 783 784
	{"get_msg_header",	js_get_msg_header,	2},		/* get_msg_header(by_offset, number) */
	{"put_msg_header",	js_put_msg_header,	2},		/* put_msg_header(by_offset, number, hdrObj) */
	{"get_msg_body",	js_get_msg_body,	2},		/* get_msg_body(by_offset, number, [strip_ctrl_a]) */
785
	{"get_msg_tail",	js_get_msg_tail,	2},		/* get_msg_body(by_offset, number, [strip_ctrl_a]) */
rswindell's avatar
rswindell committed
786
	{"save_msg",		js_save_msg,		2},		/* save_msg(code, hdr, body) */
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	{0}
};


JSObject* DLLCALL js_CreateMsgBaseClass(JSContext* cx, JSObject* parent, scfg_t* cfg)
{
	JSObject*	obj;

	scfg = cfg;
	obj = JS_InitClass(cx, parent, NULL
		,&js_msgbase_class
		,js_msgbase_constructor
		,1	/* number of constructor args */
		,js_msgbase_properties
		,js_msgbase_functions
		,NULL,NULL);

	return(obj);
}


808
#endif	/* JAVSCRIPT */