Commit 9cfd2fd5 authored by rswindell's avatar rswindell

Create sbbs_t::random_menu() (JS bbs.random_menu()) - to make the display of a...

Create sbbs_t::random_menu() (JS bbs.random_menu()) - to make the display of a randomly-chosen menu file from text/menu, very easy.
parent ba7b4579
......@@ -1208,6 +1208,53 @@ js_menu(JSContext *cx, uintN argc, jsval *arglist)
return(JS_TRUE);
}
static JSBool
js_random_menu(JSContext *cx, uintN argc, jsval *arglist)
{
jsval *argv=JS_ARGV(cx, arglist);
JSString* str;
sbbs_t* sbbs;
jsrefcount rc;
char *menu;
int32 mode = P_NONE;
JSObject* obj = JS_GetScopeChain(cx);
if(!js_argc(cx, argc, 1))
return(JS_FALSE);
if((sbbs=js_GetPrivate(cx, JS_THIS_OBJECT(cx, arglist)))==NULL)
return(JS_FALSE);
str = JS_ValueToString(cx, argv[0]);
if (!str)
return(JS_FALSE);
uintN argn = 1;
if(argc > argn && JSVAL_IS_NUMBER(argv[argn])) {
if(!JS_ValueToInt32(cx,argv[argn], &mode))
return JS_FALSE;
argn++;
}
if(argc > argn && JSVAL_IS_OBJECT(argv[argn])) {
if((obj = JSVAL_TO_OBJECT(argv[argn])) == NULL)
return JS_FALSE;
argn++;
}
JSSTRING_TO_MSTRING(cx, str, menu, NULL);
if(!menu)
return JS_FALSE;
rc=JS_SUSPENDREQUEST(cx);
bool result = sbbs->random_menu(menu, mode, obj);
free(menu);
JS_RESUMEREQUEST(cx, rc);
JS_SET_RVAL(cx, arglist, result ? JSVAL_TRUE : JSVAL_FALSE);
return(JS_TRUE);
}
static JSBool
js_menu_exists(JSContext *cx, uintN argc, jsval *arglist)
{
......@@ -4444,6 +4491,12 @@ static jsSyncMethodSpec js_bbs_functions[] = {
"When <i>scope</i> is specified, <tt>@JS:property@</tt> codes will expand the referenced property names.")
,310
},
{"random_menu", js_random_menu, 1, JSTYPE_BOOLEAN, JSDOCSTR("base_filepattern [,mode=<tt>P_NONE</tt>] [,object scope]")
,JSDOCSTR("display a randomly-chosen menu file from the text/menu directory.<br>"
"See <tt>P_*</tt> in <tt>sbbsdefs.js</tt> for <i>mode</i> flags.<br>"
"When <i>scope</i> is specified, <tt>@JS:property@</tt> codes will expand the referenced property names.")
,31800
},
{"menu_exists", js_menu_exists, 1, JSTYPE_BOOLEAN, JSDOCSTR("base_filename")
,JSDOCSTR("returns true if the referenced menu file exists (i.e. in the text/menu directory)")
,31700
......
......@@ -301,3 +301,34 @@ bool sbbs_t::menu_exists(const char *code, const char* ext, char* path)
safe_snprintf(path, MAX_PATH, "%s.%s", prefix, ext);
return fexistcase(path) ? true : false;
}
/****************************************************************************/
/* Displays a random menu file (e.g. from the text/menu directory) */
/****************************************************************************/
bool sbbs_t::random_menu(const char *name, long mode, JSObject* obj)
{
char path[MAX_PATH + 1];
glob_t g = {0};
str_list_t names = NULL;
SAFEPRINTF2(path, "%smenu/%s", cfg.text_dir, name);
if(glob(path, GLOB_NOESCAPE|GLOB_MARK, NULL, &g) != 0) {
return false;
}
for(size_t i = 0; i < g.gl_pathc; i++) {
char* ext = getfext(g.gl_pathv[i]);
if(ext == NULL)
continue;
*ext = 0;
strListPush(&names, g.gl_pathv[i]);
}
globfree(&g);
strListDedupe(&names, /* case_sensitive: */true);
bool result = false;
size_t i = sbbs_random(strListCount(names));
if(menu_exists(names[i], NULL, path)) {
result = menu(names[i], mode, obj);
}
strListFree(&names);
return result;
}
......@@ -845,6 +845,7 @@ public:
bool printfile(const char* fname, long mode, long org_cols = 0, JSObject* obj = NULL);
bool printtail(const char* fname, int lines, long mode, long org_cols = 0, JSObject* obj = NULL);
bool menu(const char *code, long mode = 0, JSObject* obj = NULL);
bool random_menu(const char *code, long mode = 0, JSObject* obj = NULL);
bool menu_exists(const char *code, const char* ext=NULL, char* realpath=NULL);
int uselect(int add, uint n, const char *title, const char *item, const uchar *ar);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment