Commit e8ffbc85 authored by Rob Swindell's avatar Rob Swindell 💬
Browse files

Add system.text() method to return text.dat strings

Like bbs.text(), except the "system" object is more widely available (e.g. in JSexec, mail server, web server, services) - in case any text.dat strings are useful in those execution environments tool. Requested by mlong.

Also cleaned up the argument validation in some of these other system methods (throw useful error exceptions rather than just returning false).
parent f7b0ba7b
/* Synchronet JavaScript "system" Object */
// vi: tabstop=4
/* $Id: js_system.c,v 1.179 2020/03/31 18:32:34 rswindell Exp $ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
......@@ -16,21 +13,9 @@
* 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. *
****************************************************************************/
......@@ -1148,9 +1133,13 @@ js_secondstr(JSContext *cx, uintN argc, jsval *arglist)
JS_SET_RVAL(cx, arglist, JSVAL_NULL);
if(argc<1)
return(JS_TRUE);
if(!js_argc(cx, argc, 1))
return JS_FALSE;
if(JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx, "Invalid argument");
return JS_FALSE;
}
JS_ValueToInt32(cx,argv[0],&t);
sectostr(t,str);
if((js_str = JS_NewStringCopyZ(cx, str))==NULL)
......@@ -1677,11 +1666,13 @@ js_new_user(JSContext *cx, uintN argc, jsval *arglist)
return JS_FALSE;
scfg_t* cfg = sys->cfg;
if(argc<1 || JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx,"Missing or invalid argument");
if(!js_argc(cx, argc, 1))
return JS_FALSE;
}
if(JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx, "Invalid argument");
return JS_FALSE;
}
JSVALUE_TO_ASTRING(cx, argv[0], alias, LEN_ALIAS+2, NULL);
rc=JS_SUSPENDREQUEST(cx);
......@@ -1839,9 +1830,13 @@ js_popen(JSContext *cx, uintN argc, jsval *arglist)
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
if(argc<1)
return(JS_TRUE);
if(!js_argc(cx, argc, 1))
return JS_FALSE;
if(JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx, "Invalid argument");
return JS_FALSE;
}
if((array=JS_NewArrayObject(cx,0,NULL))==NULL)
return(JS_FALSE);
......@@ -1934,9 +1929,13 @@ js_chkpid(JSContext *cx, uintN argc, jsval *arglist)
JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
if(argc<1)
return(JS_TRUE);
if(!js_argc(cx, argc, 1))
return JS_FALSE;
if(JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx, "Invalid argument");
return JS_FALSE;
}
JS_ValueToInt32(cx,argv[0],&pid);
rc=JS_SUSPENDREQUEST(cx);
......@@ -1955,9 +1954,13 @@ js_killpid(JSContext *cx, uintN argc, jsval *arglist)
JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
if(argc<1)
return(JS_TRUE);
if(!js_argc(cx, argc, 1))
return JS_FALSE;
if(JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx, "Invalid argument");
return JS_FALSE;
}
JS_ValueToInt32(cx,argv[0],&pid);
rc=JS_SUSPENDREQUEST(cx);
......@@ -1967,6 +1970,39 @@ js_killpid(JSContext *cx, uintN argc, jsval *arglist)
return(JS_TRUE);
}
static JSBool
js_text(JSContext *cx, uintN argc, jsval *arglist)
{
JSObject* obj=JS_THIS_OBJECT(cx, arglist);
jsval* argv=JS_ARGV(cx, arglist);
int32 i=0;
JS_SET_RVAL(cx, arglist, JSVAL_NULL);
if(!js_argc(cx, argc, 1))
return JS_FALSE;
if(JSVAL_NULL_OR_VOID(argv[0])) {
JS_ReportError(cx, "Invalid argument");
return JS_FALSE;
}
js_system_private_t* sys;
if((sys = (js_system_private_t*)js_GetClassPrivate(cx,obj,&js_system_class)) == NULL)
return JS_FALSE;
if(sys->cfg == NULL || sys->cfg->text == NULL)
return JS_TRUE;
if(!JS_ValueToECMAUint32(cx, argv[0], &i))
return JS_FALSE;
if(i > 0 && i <= TOTAL_TEXT) {
JSString* js_str = JS_NewStringCopyZ(cx, sys->cfg->text[i - 1]);
if(js_str==NULL)
return JS_FALSE;
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
}
return JS_TRUE;
}
static jsSyncMethodSpec js_system_functions[] = {
#ifndef JSDOOR
......@@ -2097,6 +2133,10 @@ static jsSyncMethodSpec js_system_functions[] = {
"returns <i>true</i> on success")
,315
},
{"text", js_text, 1, JSTYPE_STRING, JSDOCSTR("number")
,JSDOCSTR("returns specified text string from text.dat (like <tt>bbs.text()</tt>) or returns <i>null</i> upon error")
,31802
},
{0}
};
......
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