Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

js_xtrn_area.c 18.7 KB
Newer Older
rswindell's avatar
rswindell committed
1 2 3 4 5 6
/* Synchronet JavaScript "External Program Area" Object */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
rswindell's avatar
rswindell committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

#ifdef JAVASCRIPT

26
#ifdef BUILD_JSDOCS
27 28 29

static char* xtrn_sec_prop_desc[] = {

30
	 "index into sec_list array (or -1 if not in index) <i>(introduced in v3.12)</i>"
31
	,"unique number for this external program section"
32 33
	,"external program section internal code"
	,"external program section name"
34
	,"external program section access requirements"
35
	,"user has sufficient access to enter this section <i>(introduced in v3.15)</i>"
36
	,NULL
rswindell's avatar
rswindell committed
37 38 39 40
};

static char* xtrn_prog_prop_desc[] = {

41
	 "index into prog_list array (or -1 if not in index) <i>(introduced in v3.12)</i>"
42
	,"program number"
rswindell's avatar
rswindell committed
43
	,"program section index <i>(introduced in v3.12)</i>"
44
	,"program section number"
45
	,"program section internal code <i>(introduced in v3.12)</i>"
46
	,"internal code"
rswindell's avatar
rswindell committed
47 48 49 50
	,"name"
	,"command-line"
	,"clean-up command-line"
	,"startup directory"
51 52
	,"access requirements"
	,"execution requirements"
rswindell's avatar
rswindell committed
53 54 55 56 57
	,"toggle options (bitfield)"
	,"drop file type"
	,"event type (0=none)"
	,"extra time given to users running this program"
	,"maximum time allowed in program"
58
	,"execution cost (credits to run this program)"
59
	/* Insert here */
60
	,"user has sufficient access to see this program"
61 62 63 64 65 66 67 68 69
	,"user has sufficient access to run this program"
	,NULL
};

static char* event_prop_desc[] = {

	 "command-line"
	,"startup directory"
	,"node number"
70
	,"time to execute (minutes since midnight)"
71 72 73
	,"frequency to execute"
	,"days of week to execute (bitfield)"
	,"days of month to execute (bitfield)"
74
	,"months of year to execute (bitfield)"
75 76 77
	,"date/time of last run (in time_t format)"
	,"date/time of next run (in time_t format)"
	,"error log level"
78 79 80 81 82 83 84 85
	,"toggle options (bitfield)"
	,NULL
};

static char* xedit_prop_desc[] = {

	 "name"
	,"command-line"
86
	,"access requirements"
87 88
	,"toggle options (bitfield)"
	,"drop file type"
rswindell's avatar
rswindell committed
89 90
	,NULL
};
91

rswindell's avatar
rswindell committed
92 93
#endif

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
/* Event Object Properties */
enum {
	 EVENT_PROP_CMD,
	 EVENT_PROP_STARTUP_DIR,
	 EVENT_PROP_NODE_NUM,
	 EVENT_PROP_TIME,
	 EVENT_PROP_FREQ,
	 EVENT_PROP_DAYS,
	 EVENT_PROP_MDAYS,
	 EVENT_PROP_MONTHS,
	 EVENT_PROP_LAST_RUN,
	 EVENT_PROP_NEXT_RUN,
	 EVENT_PROP_ERRLEVEL,
	 EVENT_PROP_MISC
};

static jsSyncPropertySpec js_event_properties[] = {
/*		 name				,tinyid					,flags								,ver	*/

	{	"cmd"				,EVENT_PROP_CMD			,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"startup_dir"		,EVENT_PROP_STARTUP_DIR	,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"node_num"			,EVENT_PROP_NODE_NUM	,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"time"				,EVENT_PROP_TIME		,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"freq"				,EVENT_PROP_FREQ		,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"days"				,EVENT_PROP_DAYS		,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"mdays"				,EVENT_PROP_MDAYS		,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"months"			,EVENT_PROP_MONTHS		,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"last_run"			,EVENT_PROP_LAST_RUN	,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{	"next_run"			,EVENT_PROP_NEXT_RUN	,JSPROP_ENUMERATE|JSPROP_READONLY	,31802},
	{	"error_level"		,EVENT_PROP_ERRLEVEL	,JSPROP_ENUMERATE|JSPROP_READONLY	,31802},
	{	"settings"			,EVENT_PROP_MISC		,JSPROP_ENUMERATE|JSPROP_READONLY	,311},
	{ NULL }
};

Rob Swindell's avatar
Rob Swindell committed
128
BOOL js_CreateXtrnProgProperties(JSContext* cx, JSObject* obj, xtrn_t* xtrn)
rswindell's avatar
rswindell committed
129
{
130
	JSString* js_str;
rswindell's avatar
rswindell committed
131

132 133
	if((js_str=JS_NewStringCopyZ(cx, xtrn->code))==NULL)
		return(FALSE);
134 135 136
	if(!JS_DefineProperty(cx, obj, "code", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
137

138 139
	if((js_str=JS_NewStringCopyZ(cx, xtrn->name))==NULL)
		return(FALSE);
140 141 142
	if(!JS_DefineProperty(cx, obj, "name", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
143

144 145
	if((js_str=JS_NewStringCopyZ(cx, xtrn->cmd))==NULL)
		return(FALSE);
146 147 148
	if(!JS_DefineProperty(cx, obj, "cmd", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
149

150 151
	if((js_str=JS_NewStringCopyZ(cx, xtrn->clean))==NULL)
		return(FALSE);
152 153 154
	if(!JS_DefineProperty(cx, obj, "clean_cmd", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
155

156 157
	if((js_str=JS_NewStringCopyZ(cx, xtrn->path))==NULL)
		return(FALSE);
158 159 160
	if(!JS_DefineProperty(cx, obj, "startup_dir", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
161

162 163 164 165 166
	if((js_str=JS_NewStringCopyZ(cx, xtrn->arstr))==NULL)
		return(FALSE);
	if(!JS_DefineProperty(cx, obj, "ars", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
167

168 169 170 171 172
	if((js_str=JS_NewStringCopyZ(cx, xtrn->run_arstr))==NULL)
		return(FALSE);
	if(!JS_DefineProperty(cx, obj, "execution_ars", STRING_TO_JSVAL(js_str)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
173

174 175 176
	if(!JS_DefineProperty(cx, obj, "settings", INT_TO_JSVAL(xtrn->misc)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
177

178 179 180
	if(!JS_DefineProperty(cx, obj, "type", INT_TO_JSVAL(xtrn->type)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
181

182 183 184
	if(!JS_DefineProperty(cx, obj, "event", INT_TO_JSVAL(xtrn->event)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
185

186 187 188 189 190 191 192 193 194 195 196
	if(!JS_DefineProperty(cx, obj, "textra", INT_TO_JSVAL(xtrn->textra)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);

	if(!JS_DefineProperty(cx, obj, "max_time", INT_TO_JSVAL(xtrn->maxtime)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);

	if(!JS_DefineProperty(cx, obj, "cost", INT_TO_JSVAL(xtrn->cost)
		,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
		return(FALSE);
rswindell's avatar
rswindell committed
197

198
#ifdef BUILD_JSDOCS
rswindell's avatar
rswindell committed
199 200 201 202 203 204
	js_CreateArrayOfStrings(cx, obj, "_property_desc_list", xtrn_prog_prop_desc, JSPROP_READONLY);
#endif

	return(TRUE);
}

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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static JSBool js_event_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
	const char* p = NULL;
	JSString*	js_str;
	jsval		idval;
    jsint       tiny;
	event_t*	event;

	if((event = JS_GetPrivate(cx, obj)) == NULL)
		return JS_FALSE;

    JS_IdToValue(cx, id, &idval);
    tiny = JSVAL_TO_INT(idval);

	switch(tiny) {
		case EVENT_PROP_CMD:
			p = event->cmd;
			break;
		case EVENT_PROP_STARTUP_DIR:
			p = event->dir;
			break;
		case EVENT_PROP_NODE_NUM:
			*vp = UINT_TO_JSVAL(event->node);
			break;
		case EVENT_PROP_TIME:
			*vp = UINT_TO_JSVAL(event->time);
			break;
		case EVENT_PROP_FREQ:
			*vp = UINT_TO_JSVAL(event->freq);
			break;
		case EVENT_PROP_DAYS:
			*vp = UINT_TO_JSVAL(event->days);
			break;
		case EVENT_PROP_MDAYS:
			*vp = UINT_TO_JSVAL(event->mdays);
			break;
		case EVENT_PROP_MONTHS:
			*vp = UINT_TO_JSVAL(event->months);
			break;
		case EVENT_PROP_LAST_RUN:
			*vp = UINT_TO_JSVAL(event->last);
			break;
		case EVENT_PROP_NEXT_RUN:
			*vp = UINT_TO_JSVAL((uint32)getnexteventtime(event));
			break;
		case EVENT_PROP_ERRLEVEL:
			*vp = UINT_TO_JSVAL(event->errlevel);
			break;
		case EVENT_PROP_MISC:
			*vp = UINT_TO_JSVAL(event->misc);
			break;
	}

	if(p != NULL) {	/* string property */
		if((js_str = JS_NewStringCopyZ(cx, p)) == NULL)
			return JS_FALSE;
		*vp = STRING_TO_JSVAL(js_str);
	}
	return JS_TRUE;
}

static JSClass js_event_class = {
     "Event"				/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,js_event_get			/* getProperty	*/
	,JS_StrictPropertyStub	/* setProperty	*/
	,JS_EnumerateStub		/* enumerate	*/
	,JS_ResolveStub			/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,JS_FinalizeStub		/* finalize		*/
};
rswindell's avatar
rswindell committed
278

deuce's avatar
deuce committed
279 280 281 282 283 284
struct js_xtrn_area_priv {
	scfg_t		*cfg;
	user_t		*user;
	client_t	*client;
};

Rob Swindell's avatar
Rob Swindell committed
285
JSBool js_xtrn_area_resolve(JSContext* cx, JSObject* areaobj, jsid id)
rswindell's avatar
rswindell committed
286
{
287
	JSObject*	allsec;
288
	JSObject*	allprog;
rswindell's avatar
rswindell committed
289 290
	JSObject*	secobj;
	JSObject*	progobj;
291
	JSObject*	eventobj;
292
	JSObject*	event_array;
293
	JSObject*	xeditobj;
294
	JSObject*	xedit_array;
rswindell's avatar
rswindell committed
295 296
	JSObject*	sec_list;
	JSObject*	prog_list;
297
	JSString*	js_str;
rswindell's avatar
rswindell committed
298
	jsval		val;
299 300
	jsuint		sec_index;
	jsuint		prog_index;
rswindell's avatar
rswindell committed
301
	uint		l,d;
deuce's avatar
deuce committed
302 303 304 305 306 307 308 309 310 311 312 313 314
	char*		name=NULL;
	struct js_xtrn_area_priv *p;

	if((p=(struct js_xtrn_area_priv*)JS_GetPrivate(cx,areaobj))==NULL)
		return JS_FALSE;

	if(id != JSID_VOID && id != JSID_EMPTY) {
		jsval idval;
		
		JS_IdToValue(cx, id, &idval);
		if(JSVAL_IS_STRING(idval))
			JSSTRING_TO_MSTRING(cx, JSVAL_TO_STRING(idval), name, NULL);
	}
rswindell's avatar
rswindell committed
315

deuce's avatar
deuce committed
316 317
	if (name == NULL || strcmp(name, "sec")==0 || strcmp(name, "prog")==0 || strcmp(name, "sec_list")==0 || strcmp(name, "event")==0  || strcmp(name, "editor")==0) {
		FREE_AND_NULL(name);
318
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
319
		js_DescribeSyncObject(cx,areaobj,"External Program Areas",310);
rswindell's avatar
rswindell committed
320 321
#endif

deuce's avatar
deuce committed
322 323 324
		/* xtrn_area.sec[] */
		if((allsec=JS_NewObject(cx,NULL,NULL,areaobj))==NULL)
			return JS_FALSE;
325

deuce's avatar
deuce committed
326 327 328
		val=OBJECT_TO_JSVAL(allsec);
		if(!JS_SetProperty(cx, areaobj, "sec", &val))
			return JS_FALSE;
329

deuce's avatar
deuce committed
330 331 332
		/* xtrn_area.prog[] */
		if((allprog=JS_NewObject(cx,NULL,NULL,areaobj))==NULL)
			return JS_FALSE;
333

deuce's avatar
deuce committed
334 335 336
		val=OBJECT_TO_JSVAL(allprog);
		if(!JS_SetProperty(cx, areaobj, "prog", &val))
			return JS_FALSE;
337

deuce's avatar
deuce committed
338 339 340
		/* xtrn_area.sec_list[] */
		if((sec_list=JS_NewArrayObject(cx, 0, NULL))==NULL) 
			return JS_FALSE;
rswindell's avatar
rswindell committed
341

deuce's avatar
deuce committed
342 343 344
		val=OBJECT_TO_JSVAL(sec_list);
		if(!JS_SetProperty(cx, areaobj, "sec_list", &val)) 
			return JS_FALSE;
rswindell's avatar
rswindell committed
345

deuce's avatar
deuce committed
346
		for(l=0;l<p->cfg->total_xtrnsecs;l++) {
347

deuce's avatar
deuce committed
348 349
			if((secobj=JS_NewObject(cx, NULL, NULL, NULL))==NULL)
				return JS_FALSE;
rswindell's avatar
rswindell committed
350

351
			val=OBJECT_TO_JSVAL(secobj);
deuce's avatar
deuce committed
352 353
			sec_index=-1;
			if(p->user==NULL || chk_ar(p->cfg,p->cfg->xtrnsec[l]->ar,p->user,p->client)) {
354

deuce's avatar
deuce committed
355 356
				if(!JS_GetArrayLength(cx, sec_list, &sec_index))
					return JS_FALSE;
357

deuce's avatar
deuce committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
				if(!JS_SetElement(cx, sec_list, sec_index, &val))
					return JS_FALSE;
			}

			/* Add as property (associative array element) */
			if(!JS_DefineProperty(cx, allsec, p->cfg->xtrnsec[l]->code, val
				,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE))
				return JS_FALSE;

			val=INT_TO_JSVAL(sec_index);
			if(!JS_SetProperty(cx, secobj, "index", &val))
				return JS_FALSE;

			val=INT_TO_JSVAL(l);
			if(!JS_SetProperty(cx, secobj, "number", &val))
				return JS_FALSE;

			if((js_str=JS_NewStringCopyZ(cx, p->cfg->xtrnsec[l]->code))==NULL)
				return JS_FALSE;
			val=STRING_TO_JSVAL(js_str);
			if(!JS_SetProperty(cx, secobj, "code", &val))
				return JS_FALSE;

			if((js_str=JS_NewStringCopyZ(cx, p->cfg->xtrnsec[l]->name))==NULL)
				return JS_FALSE;
			val=STRING_TO_JSVAL(js_str);
			if(!JS_SetProperty(cx, secobj, "name", &val))
				return JS_FALSE;

			if((js_str=JS_NewStringCopyZ(cx, p->cfg->xtrnsec[l]->arstr))==NULL)
				return JS_FALSE;
			val=STRING_TO_JSVAL(js_str);
			if(!JS_SetProperty(cx, secobj, "ars", &val))
				return JS_FALSE;

			if(p->user==NULL || chk_ar(p->cfg,p->cfg->xtrnsec[l]->ar,p->user,p->client))
				val=JSVAL_TRUE;
			else
				val=JSVAL_FALSE;
			if(!JS_SetProperty(cx, secobj, "can_access", &val))
				return JS_FALSE;

			/* prog_list[] */
			if((prog_list=JS_NewArrayObject(cx, 0, NULL))==NULL) 
				return JS_FALSE;
403

deuce's avatar
deuce committed
404 405 406
			val=OBJECT_TO_JSVAL(prog_list);
			if(!JS_SetProperty(cx, secobj, "prog_list", &val)) 
				return JS_FALSE;
rswindell's avatar
rswindell committed
407

408
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
409
			js_DescribeSyncObject(cx,secobj,"Online Program (door) Sections (current user has access to)",310);
410 411
#endif

deuce's avatar
deuce committed
412 413 414
			for(d=0;d<p->cfg->total_xtrns;d++) {
				if(p->cfg->xtrn[d]->sec!=l)
					continue;
415

deuce's avatar
deuce committed
416 417
				if((progobj=JS_NewObject(cx, NULL, NULL, NULL))==NULL)
					return JS_FALSE;
rswindell's avatar
rswindell committed
418

419
				val=OBJECT_TO_JSVAL(progobj);
deuce's avatar
deuce committed
420 421 422
				prog_index=-1;
				if((p->user==NULL || chk_ar(p->cfg,p->cfg->xtrn[d]->ar,p->user,p->client))
					&& !(p->cfg->xtrn[d]->event && p->cfg->xtrn[d]->misc&EVENTONLY)) {
423

deuce's avatar
deuce committed
424 425
					if(!JS_GetArrayLength(cx, prog_list, &prog_index))
						return JS_FALSE;
426

deuce's avatar
deuce committed
427 428 429
					if(!JS_SetElement(cx, prog_list, prog_index, &val))
						return JS_FALSE;
				}
430

deuce's avatar
deuce committed
431 432 433 434
				/* Add as property (associative array element) */
				if(!JS_DefineProperty(cx, allprog, p->cfg->xtrn[d]->code, val
					,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE))
					return JS_FALSE;
435

deuce's avatar
deuce committed
436 437 438
				val=INT_TO_JSVAL(prog_index);
				if(!JS_SetProperty(cx, progobj, "index", &val))
					return JS_FALSE;
rswindell's avatar
rswindell committed
439

deuce's avatar
deuce committed
440 441 442
				val=INT_TO_JSVAL(d);
				if(!JS_SetProperty(cx, progobj, "number", &val))
					return JS_FALSE;
rswindell's avatar
rswindell committed
443

deuce's avatar
deuce committed
444 445 446
				val=INT_TO_JSVAL(sec_index);
				if(!JS_SetProperty(cx, progobj, "sec_index", &val))
					return JS_FALSE;
447

deuce's avatar
deuce committed
448 449 450
				val=INT_TO_JSVAL(l);
				if(!JS_SetProperty(cx, progobj, "sec_number", &val))
					return JS_FALSE;
451

deuce's avatar
deuce committed
452 453 454
				val=STRING_TO_JSVAL(JS_NewStringCopyZ(cx,p->cfg->xtrnsec[l]->code));
				if(!JS_SetProperty(cx, progobj, "sec_code", &val))
					return JS_FALSE;
455

deuce's avatar
deuce committed
456 457
				if(!js_CreateXtrnProgProperties(cx, progobj, p->cfg->xtrn[d]))
					return JS_FALSE;
458

deuce's avatar
deuce committed
459 460 461 462 463 464
				if(p->user==NULL || chk_ar(p->cfg,p->cfg->xtrn[d]->ar,p->user,p->client))
					val=JSVAL_TRUE;
				else
					val=JSVAL_FALSE;
				if(!JS_SetProperty(cx, progobj, "can_access", &val))
					return JS_FALSE;
465

deuce's avatar
deuce committed
466 467 468 469 470 471
				if(p->user==NULL || chk_ar(p->cfg,p->cfg->xtrn[d]->run_ar,p->user,p->client))
					val=JSVAL_TRUE;
				else
					val=JSVAL_FALSE;
				if(!JS_SetProperty(cx, progobj, "can_run", &val))
					return JS_FALSE;
rswindell's avatar
rswindell committed
472

473
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
474
				js_DescribeSyncObject(cx,progobj,"Online External Programs (doors) (current user has access to)",310);
rswindell's avatar
rswindell committed
475
#endif
deuce's avatar
deuce committed
476
			}
rswindell's avatar
rswindell committed
477

478
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
479
			js_CreateArrayOfStrings(cx, secobj, "_property_desc_list", xtrn_sec_prop_desc, JSPROP_READONLY);
480 481
#endif

deuce's avatar
deuce committed
482
		}
rswindell's avatar
rswindell committed
483

484
#ifdef BUILD_JSDOCS
485

deuce's avatar
deuce committed
486 487
		js_DescribeSyncObject(cx,allsec,"Associative array of all external program sections (use internal code as index)",312);
		JS_DefineProperty(cx,allsec,"_dont_document",JSVAL_TRUE,NULL,NULL,JSPROP_READONLY);
488

deuce's avatar
deuce committed
489 490
		js_DescribeSyncObject(cx,allprog,"Associative array of all external programs (use internal code as index)",311);
		JS_DefineProperty(cx,allprog,"_dont_document",JSVAL_TRUE,NULL,NULL,JSPROP_READONLY);
491 492
#endif

deuce's avatar
deuce committed
493 494 495
		/* Create event property */
		if((event_array=JS_NewObject(cx,NULL,NULL,areaobj))==NULL)
			return JS_FALSE;
496

deuce's avatar
deuce committed
497 498 499
		val=OBJECT_TO_JSVAL(event_array);
		if(!JS_SetProperty(cx, areaobj, "event", &val))
			return JS_FALSE;
500

deuce's avatar
deuce committed
501
		for(l=0;l<p->cfg->total_events;l++) {
502

503
			if((eventobj=JS_NewObject(cx, &js_event_class, NULL, NULL))==NULL)
deuce's avatar
deuce committed
504
				return JS_FALSE;
505

506
			JS_SetPrivate(cx, eventobj, p->cfg->event[l]);
507

508
			if(!js_DefineSyncProperties(cx, eventobj, js_event_properties))
deuce's avatar
deuce committed
509
				return JS_FALSE;
510

511 512
			if(!JS_DefineProperty(cx, event_array, p->cfg->event[l]->code, OBJECT_TO_JSVAL(eventobj)
				,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE))
deuce's avatar
deuce committed
513
				return JS_FALSE;
514

515
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
516
			js_CreateArrayOfStrings(cx, eventobj, "_property_desc_list", event_prop_desc, JSPROP_READONLY);
517
#endif
deuce's avatar
deuce committed
518
		}
519

520
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
521 522
		js_DescribeSyncObject(cx,event_array,"Associative array of all timed events (use internal code as index)",311);
		JS_DefineProperty(cx,event_array,"_assoc_array",JSVAL_TRUE,NULL,NULL,JSPROP_READONLY);
523 524
#endif

deuce's avatar
deuce committed
525 526 527
		/* Create editor property */
		if((xedit_array=JS_NewObject(cx,NULL,NULL,areaobj))==NULL)
			return JS_FALSE;
528

deuce's avatar
deuce committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
		val=OBJECT_TO_JSVAL(xedit_array);
		if(!JS_SetProperty(cx, areaobj, "editor", &val))
			return JS_FALSE;

		for(l=0;l<p->cfg->total_xedits;l++) {

			if(p->user!=NULL && !chk_ar(p->cfg,p->cfg->xedit[l]->ar,p->user,p->client))
				continue;

			if((xeditobj=JS_NewObject(cx, NULL, NULL, NULL))==NULL)
				return JS_FALSE;

			if(!JS_DefineProperty(cx, xedit_array, p->cfg->xedit[l]->code, OBJECT_TO_JSVAL(xeditobj)
				,NULL,NULL,JSPROP_READONLY|JSPROP_ENUMERATE))
				return JS_FALSE;

			if((js_str=JS_NewStringCopyZ(cx, p->cfg->xedit[l]->name))==NULL)
				return JS_FALSE;
			if(!JS_DefineProperty(cx, xeditobj, "name", STRING_TO_JSVAL(js_str)
				,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
				return JS_FALSE;

			if((js_str=JS_NewStringCopyZ(cx, p->cfg->xedit[l]->rcmd))==NULL)
				return JS_FALSE;
			if(!JS_DefineProperty(cx, xeditobj, "cmd", STRING_TO_JSVAL(js_str)
				,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
				return JS_FALSE;

			if((js_str=JS_NewStringCopyZ(cx, p->cfg->xedit[l]->arstr))==NULL)
				return JS_FALSE;
			if(!JS_DefineProperty(cx, xeditobj, "ars", STRING_TO_JSVAL(js_str)
				,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
				return JS_FALSE;

			if(!JS_DefineProperty(cx, xeditobj, "settings", INT_TO_JSVAL(p->cfg->xedit[l]->misc)
				,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
				return JS_FALSE;

			if(!JS_DefineProperty(cx, xeditobj, "type", INT_TO_JSVAL(p->cfg->xedit[l]->type)
				,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY))
				return JS_FALSE;

#ifdef BUILD_JSDOCS
			js_CreateArrayOfStrings(cx, xeditobj, "_property_desc_list", xedit_prop_desc, JSPROP_READONLY);
#endif
		}
575

deuce's avatar
deuce committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
#ifdef BUILD_JSDOCS
		js_DescribeSyncObject(cx,xedit_array,"Associative array of all external editors (use internal code as index)",311);
		JS_DefineProperty(cx,xedit_array,"_assoc_array",JSVAL_TRUE,NULL,NULL,JSPROP_READONLY);
#endif
	}
	if(name)
		free(name);

	return JS_TRUE;
}

static JSBool js_xtrn_area_enumerate(JSContext *cx, JSObject *obj)
{
	return(js_xtrn_area_resolve(cx, obj, JSID_VOID));
}

static void 
js_xtrn_area_finalize(JSContext *cx, JSObject *obj)
{
	struct js_xtrn_area_priv *p;
596

deuce's avatar
deuce committed
597 598 599 600 601 602
	if((p=(struct js_xtrn_area_priv*)JS_GetPrivate(cx,obj))==NULL)
		return;

	free(p);
	JS_SetPrivate(cx,obj,NULL);
}
603 604


deuce's avatar
deuce committed
605 606 607 608 609 610 611 612 613 614 615 616
static JSClass js_xtrn_area_class = {
     "XtrnArea"				/* name			*/
    ,JSCLASS_HAS_PRIVATE	/* flags		*/
	,JS_PropertyStub		/* addProperty	*/
	,JS_PropertyStub		/* delProperty	*/
	,JS_PropertyStub		/* getProperty	*/
	,JS_StrictPropertyStub		/* setProperty	*/
	,js_xtrn_area_enumerate	/* enumerate	*/
	,js_xtrn_area_resolve	/* resolve		*/
	,JS_ConvertStub			/* convert		*/
	,js_xtrn_area_finalize	/* finalize		*/
};
617

Rob Swindell's avatar
Rob Swindell committed
618
JSObject* js_CreateXtrnAreaObject(JSContext* cx, JSObject* parent, scfg_t* cfg
deuce's avatar
deuce committed
619 620 621 622
										  ,user_t* user, client_t* client)
{
	JSObject* obj;
	struct js_xtrn_area_priv *p;
623

deuce's avatar
deuce committed
624 625
	obj = JS_DefineObject(cx, parent, "xtrn_area", &js_xtrn_area_class, NULL
		,JSPROP_ENUMERATE|JSPROP_READONLY);
626

deuce's avatar
deuce committed
627 628
	if(obj==NULL)
		return(NULL);
629

deuce's avatar
deuce committed
630 631 632
	p = (struct js_xtrn_area_priv *)malloc(sizeof(struct js_xtrn_area_priv));
	if (p == NULL)
		return NULL;
633

deuce's avatar
deuce committed
634 635 636 637
	memset(p,0,sizeof(*p));
	p->cfg = cfg;
	p->user = user;
	p->client = client;
638

deuce's avatar
deuce committed
639 640 641
	if(!JS_SetPrivate(cx, obj, p)) {
		free(p);
		return(NULL);
642 643
	}

644
#ifdef BUILD_JSDOCS
deuce's avatar
deuce committed
645 646
	// Ensure they're all created for JSDOCS
	js_xtrn_area_enumerate(cx, obj);
647 648
#endif

deuce's avatar
deuce committed
649
	return(obj);
rswindell's avatar
rswindell committed
650 651 652
}

#endif	/* JAVSCRIPT */