js_msgbase.c 25.7 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 246 247 248 249 250 251 252 253 254
	if(JS_GetProperty(cx, hdr, "id", &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, RFC822MSGID, (ushort)strlen(cp), cp);
	}

	if(JS_GetProperty(cx, hdr, "reply_id", &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, RFC822REPLYID, (ushort)strlen(cp), cp);
	}

255
	if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && val!=JSVAL_VOID) {
256 257 258 259 260 261 262
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		smb_hfield(msg, FIDOMSGID, (ushort)strlen(cp), cp);
	}

263
	if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && val!=JSVAL_VOID) {
264 265 266 267 268 269 270
		if((js_str=JS_ValueToString(cx,val))==NULL)
			return(FALSE);
		if((cp=JS_GetStringBytes(js_str))==NULL)
			return(FALSE);
		smb_hfield(msg, FIDOREPLYID, (ushort)strlen(cp), cp);
	}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	if(JS_GetProperty(cx, hdr, "ftn_area", &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, FIDOAREA, (ushort)strlen(cp), cp);
	}

	if(JS_GetProperty(cx, hdr, "ftn_flags", &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, FIDOFLAGS, (ushort)strlen(cp), cp);
	}

	if(JS_GetProperty(cx, hdr, "ftn_pid", &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, FIDOPID, (ushort)strlen(cp), cp);
	}

295 296 297 298 299 300 301
	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);
	}
302 303 304 305 306 307 308 309 310 311
	
	/* 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);
312
	if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && val!=JSVAL_VOID) 
313 314 315
		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);
316
	if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && val!=JSVAL_VOID) 
317 318 319 320 321
		msg->hdr.when_imported.zone=(short)JSVAL_TO_INT(val);

	return(TRUE);
}

322 323 324
static JSBool
js_get_msg_header(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
325
	char		date[128];
326 327
	char		msg_id[256];
	char		reply_id[256];
328
	char*		val;
329 330
	ulong		l;
	smbmsg_t	msg;
331
	smbmsg_t	orig_msg;
332 333 334 335 336 337 338 339
	JSObject*	hdrobj;
	private_t*	p;

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

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

343 344 345 346 347 348 349 350 351 352
	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);

353
	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
354 355
		return(JS_TRUE);

356 357 358 359 360 361 362
	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
363 364
	if((hdrobj=JS_NewObject(cx,&js_msghdr_class,NULL,obj))==NULL) {
		smb_freemsgmem(&msg);
365
		return(JS_TRUE);
rswindell's avatar
rswindell committed
366
	}
367 368

	JS_DefineProperty(cx, hdrobj, "number", INT_TO_JSVAL(msg.hdr.number)
369
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
370
	JS_DefineProperty(cx, hdrobj, "to",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.to))
371
		,NULL,NULL,JSPROP_ENUMERATE);
372
	JS_DefineProperty(cx, hdrobj, "from",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.from))
373
		,NULL,NULL,JSPROP_ENUMERATE);
374
	JS_DefineProperty(cx, hdrobj, "subject",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.subj))
375
		,NULL,NULL,JSPROP_ENUMERATE);
376
	JS_DefineProperty(cx, hdrobj, "to_ext",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.to_ext))
377
		,NULL,NULL,JSPROP_ENUMERATE);
378
	JS_DefineProperty(cx, hdrobj, "from_ext",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.from_ext))
379
		,NULL,NULL,JSPROP_ENUMERATE);
380
	JS_DefineProperty(cx, hdrobj, "replyto",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.replyto))
381
		,NULL,NULL,JSPROP_ENUMERATE);
382
	JS_DefineProperty(cx, hdrobj, "replyto_ext",STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msg.replyto_ext))
383
		,NULL,NULL,JSPROP_ENUMERATE);
384
	JS_DefineProperty(cx, hdrobj, "to_agent",INT_TO_JSVAL(msg.to_agent)
385
		,NULL,NULL,JSPROP_ENUMERATE);
386
	JS_DefineProperty(cx, hdrobj, "from_agent",INT_TO_JSVAL(msg.from_agent)
387
		,NULL,NULL,JSPROP_ENUMERATE);
388
	JS_DefineProperty(cx, hdrobj, "replyto_agent",INT_TO_JSVAL(msg.replyto_agent)
389
		,NULL,NULL,JSPROP_ENUMERATE);
390 391

	JS_DefineProperty(cx, hdrobj, "to_net_type",INT_TO_JSVAL(msg.to_net.type)
392
		,NULL,NULL,JSPROP_ENUMERATE);
393
	JS_DefineProperty(cx, hdrobj, "to_net_addr"
394
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.to_net)))
395
		,NULL,NULL,JSPROP_ENUMERATE);
396 397

	JS_DefineProperty(cx, hdrobj, "from_net_type",INT_TO_JSVAL(msg.from_net.type)
398
		,NULL,NULL,JSPROP_ENUMERATE);
399
	JS_DefineProperty(cx, hdrobj, "from_net_addr"
400
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.from_net)))
401
		,NULL,NULL,JSPROP_ENUMERATE);
402 403

	JS_DefineProperty(cx, hdrobj, "replyto_net_type",INT_TO_JSVAL(msg.replyto_net.type)
404
		,NULL,NULL,JSPROP_ENUMERATE);
405
	JS_DefineProperty(cx, hdrobj, "replyto_net_addr"
406
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,net_addr(&msg.replyto_net)))
407
		,NULL,NULL,JSPROP_ENUMERATE);
408 409

	JS_DefineProperty(cx, hdrobj, "forwarded",INT_TO_JSVAL(msg.forwarded)
410
		,NULL,NULL,JSPROP_ENUMERATE);
411
	JS_DefineProperty(cx, hdrobj, "expiration_time",INT_TO_JSVAL(msg.expiration.time)
412
		,NULL,NULL,JSPROP_ENUMERATE);
413
	JS_DefineProperty(cx, hdrobj, "expiration_zone",INT_TO_JSVAL(msg.expiration.zone)
414
		,NULL,NULL,JSPROP_ENUMERATE);
415 416

	JS_DefineProperty(cx, hdrobj, "type", INT_TO_JSVAL(msg.hdr.type)
417
		,NULL,NULL,JSPROP_ENUMERATE);
418
	JS_DefineProperty(cx, hdrobj, "version", INT_TO_JSVAL(msg.hdr.version)
419
		,NULL,NULL,JSPROP_ENUMERATE);
420
	JS_DefineProperty(cx, hdrobj, "attr", INT_TO_JSVAL(msg.hdr.attr)
421
		,NULL,NULL,JSPROP_ENUMERATE);
422
	JS_DefineProperty(cx, hdrobj, "auxattr", INT_TO_JSVAL(msg.hdr.auxattr)
423
		,NULL,NULL,JSPROP_ENUMERATE);
424
	JS_DefineProperty(cx, hdrobj, "netattr", INT_TO_JSVAL(msg.hdr.netattr)
425
		,NULL,NULL,JSPROP_ENUMERATE);
426 427

	JS_DefineProperty(cx, hdrobj, "when_written_time", INT_TO_JSVAL(msg.hdr.when_written.time)
428
		,NULL,NULL,JSPROP_ENUMERATE);
429
	JS_DefineProperty(cx, hdrobj, "when_written_zone", INT_TO_JSVAL(msg.hdr.when_written.zone)
430
		,NULL,NULL,JSPROP_ENUMERATE);
431
	JS_DefineProperty(cx, hdrobj, "when_imported_time", INT_TO_JSVAL(msg.hdr.when_imported.time)
432
		,NULL,NULL,JSPROP_ENUMERATE);
433
	JS_DefineProperty(cx, hdrobj, "when_imported_zone", INT_TO_JSVAL(msg.hdr.when_imported.zone)
434
		,NULL,NULL,JSPROP_ENUMERATE);
435 436

	JS_DefineProperty(cx, hdrobj, "thread_orig", INT_TO_JSVAL(msg.hdr.thread_orig)
437
		,NULL,NULL,JSPROP_ENUMERATE);
438
	JS_DefineProperty(cx, hdrobj, "thread_next", INT_TO_JSVAL(msg.hdr.thread_next)
439
		,NULL,NULL,JSPROP_ENUMERATE);
440
	JS_DefineProperty(cx, hdrobj, "thread_first", INT_TO_JSVAL(msg.hdr.thread_first)
441
		,NULL,NULL,JSPROP_ENUMERATE);
442 443

	JS_DefineProperty(cx, hdrobj, "delivery_attempts", INT_TO_JSVAL(msg.hdr.delivery_attempts)
444
		,NULL,NULL,JSPROP_ENUMERATE);
445 446 447

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

450 451
	JS_DefineProperty(cx, hdrobj, "date"
		,STRING_TO_JSVAL(JS_NewStringCopyZ(cx,msgdate(msg.hdr.when_written,date)))
452
		,NULL,NULL,JSPROP_ENUMERATE);
453

454 455 456 457 458
	/* Reply-ID (References) */
	if(msg.reply_id!=NULL)
		val=msg.reply_id;
	else {
		reply_id[0]=0;
459 460 461 462 463 464 465 466 467 468 469 470
		if(p->smb.subnum!=INVALID_SUB && msg.hdr.thread_orig) {
			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
				sprintf(reply_id,"<%08lX.%lu.%s@%s>"
					,orig_msg.idx.time
					,msg.hdr.thread_orig
					,scfg->sub[p->smb.subnum]->code
					,scfg->sys_inetaddr);
		}
471 472
		val=reply_id;
	}
473 474 475
	if(val[0])
		JS_DefineProperty(cx, hdrobj, "reply_id", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
476 477

	/* Message-ID */
478 479 480 481 482
	if(msg.id!=NULL)
		val=msg.id;
	else {
		if(p->smb.subnum==INVALID_SUB)
			sprintf(msg_id,"<%08lX.%lu@%s>"
483
				,msg.idx.time
484
				,msg.idx.number,scfg->sys_inetaddr);
485
		else
486
			sprintf(msg_id,"<%08lX.%lu.%s@%s>"
487
				,msg.idx.time
488 489 490 491
				,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))
492 493
		,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

494 495 496
	/* FidoNet Header Fields */
	if(msg.ftn_msgid!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_msgid", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
497
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
498 499 500 501 502 503 504 505
	if(msg.ftn_reply!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_reply", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_pid!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_pid", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
	if(msg.ftn_area!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_area", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
506
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);
507 508 509 510
	if(msg.ftn_flags!=NULL)
		JS_DefineProperty(cx, hdrobj, "ftn_flags", STRING_TO_JSVAL(JS_NewStringCopyZ(cx,val))
			,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE);

511

rswindell's avatar
rswindell committed
512 513
	smb_freemsgmem(&msg);

514 515 516 517 518
	*rval = OBJECT_TO_JSVAL(hdrobj);

	return(JS_TRUE);
}

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
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);

548
	if(smb_lockmsghdr(&(p->smb),&msg)!=0)
549 550
		return(JS_TRUE);

551 552 553
	do {
		if(smb_getmsghdr(&(p->smb), &msg)!=0)
			break;
554

555
		if(!parse_header_object(cx, hdr, p->smb.subnum, &msg))
556 557 558 559 560 561 562 563 564
			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
565
	smb_freemsgmem(&msg);
566 567 568 569

	return(JS_TRUE);
}

570
static char* get_msg_text(smb_t* smb, smbmsg_t* msg, BOOL strip_ctrl_a, BOOL rfc822, ulong mode)
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
{
	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
587
		smb_freemsgmem(msg);
588 589 590 591
		return(NULL);
	}

	smb_unlockmsghdr(smb, msg); 
rswindell's avatar
rswindell committed
592
	smb_freemsgmem(msg);
593 594 595 596 597 598

	if(strip_ctrl_a) {
		char* newbuf;
		if((newbuf=malloc(strlen(buf)+1))!=NULL) {
			int i,j;
			for(i=j=0;buf[i];i++) {
599
				if(buf[i]==CTRL_A && buf[i+1]!=0)
600 601 602 603 604 605 606 607
					i++;
				else newbuf[j++]=buf[i]; 
			}
			newbuf[j]=0;
			strcpy(buf,newbuf);
			free(newbuf);
		}
	}
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

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

624 625 626
	return(buf);
}

627 628 629 630 631 632
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;
633
	JSBool		tails=JS_TRUE;
634
	JSBool		rfc822=JS_FALSE;
635 636 637 638 639 640 641
	private_t*	p;

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

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

645 646 647 648 649 650 651 652 653 654
	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]);

655
	if(argc>3)
656
		rfc822=JSVAL_TO_BOOLEAN(argv[3]);
657

658 659
	if(argc>4)
		tails=JSVAL_TO_BOOLEAN(argv[4]);
660

661
	buf = get_msg_text(&(p->smb), &msg, strip_ctrl_a, rfc822, tails ? GETMSGTXT_TAILS : 0);
662
	if(buf==NULL)
663 664
		return(JS_TRUE);

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

667
	smb_freemsgtxt(buf);
668

669 670 671 672 673 674 675 676 677
	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;
678
	JSBool		rfc822=JS_FALSE;
679 680 681 682 683 684 685 686
	private_t*	p;

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

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

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

691 692 693 694 695 696 697 698
	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]);

699 700 701 702
	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);
703 704
	if(buf==NULL)
		return(JS_TRUE);
705 706 707 708 709 710 711 712

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

	smb_freemsgtxt(buf);

	return(JS_TRUE);
}

rswindell's avatar
rswindell committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
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);

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

746 747 748 749
	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
750

751
	truncsp(body);
752
	if(savemsg(scfg, &(p->smb), &msg, body)==0)
rswindell's avatar
rswindell committed
753 754 755 756 757 758
		*rval = BOOLEAN_TO_JSVAL(JS_TRUE);

	return(JS_TRUE);
}


759 760 761 762 763 764
/* MsgBase Object Properites */
enum {
	 SMB_PROP_LAST_ERROR
	,SMB_PROP_FILE		
	,SMB_PROP_DEBUG		
	,SMB_PROP_RETRY_TIME
rswindell's avatar
rswindell committed
765
	,SMB_PROP_FIRST_MSG		// first message number
766 767 768 769 770 771
	,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
772
	,SMB_PROP_SUBNUM		// sub-board number
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
};

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
800
	idxrec_t	idx;
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	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
821 822 823 824 825
		case SMB_PROP_FIRST_MSG:
			memset(&idx,0,sizeof(idx));
			smb_getfirstidx(&(p->smb),&idx);
			*vp = INT_TO_JSVAL(idx.number);
			break;
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
		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
846 847 848 849
		case SMB_PROP_SUBNUM:
			*vp = INT_TO_JSVAL(p->smb.subnum);
			break;

850 851 852 853 854 855 856 857 858 859 860 861 862 863
	}

	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
864 865 866 867 868 869 870
	{	"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
871
	{	"subnum"			,SMB_PROP_SUBNUM		,SMB_PROP_FLAGS,	NULL,NULL},
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	{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 */
890 891 892
	{"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]) */
893
	{"get_msg_tail",	js_get_msg_tail,	2},		/* get_msg_body(by_offset, number, [strip_ctrl_a]) */
rswindell's avatar
rswindell committed
894
	{"save_msg",		js_save_msg,		2},		/* save_msg(code, hdr, body) */
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	{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);
}


916
#endif	/* JAVSCRIPT */