[mw-devel] MW3 r927 - branches/jscript/src

arthur at sucs.org arthur at sucs.org
Wed Mar 21 16:25:44 GMT 2007


Author: arthur
Date: 2007-03-21 16:25:44 +0000 (Wed, 21 Mar 2007)
New Revision: 927

Modified:
   branches/jscript/src/js.c
Log:
Add who data to jscript, refs #13


Modified: branches/jscript/src/js.c
===================================================================
--- branches/jscript/src/js.c	2007-03-21 15:48:18 UTC (rev 926)
+++ branches/jscript/src/js.c	2007-03-21 16:25:44 UTC (rev 927)
@@ -2,6 +2,7 @@
 
 #define XP_UNIX
 
+#include <fcntl.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
@@ -16,6 +17,7 @@
 #include "sqlite.h"
 #include "chattable.h"
 #include "script.h"
+#include "talker_privs.h"
 
 extern struct person *user;
 extern long userposn;
@@ -42,6 +44,13 @@
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
+JSClass js_whoclass = {
+	"who", JSCLASS_HAS_PRIVATE,
+	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
+	JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
 char *
 utf16tolocal(char * utf16, size_t len) {
 	char * local;
@@ -243,6 +252,69 @@
 	return jsarray;
 }
 
+// Provides a javascript function to query the wholist
+static JSBool
+js_wholist(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) {
+	struct person u;
+	struct who w;
+	int ufile, wfile;
+	JSObject *res;
+	int n=0;
+
+	wfile=openwhofile(O_RDWR);
+	ufile=openuserfile(O_RDONLY);
+	if (wfile<0 || ufile<0) return JS_FALSE;
+
+	res = JS_NewArrayObject(cx, 0, NULL);
+	JS_AddRoot(cx, res);
+
+	while (read(wfile,&w,sizeof(w))) {
+		JSObject *line;
+		JSString *jsstr;
+		jsval jv;
+		int rown=0;
+
+		if (w.posn < 0) continue;
+		lseek(ufile,w.posn,SEEK_SET);
+		read(ufile,&u,sizeof(u));
+
+		/* make a new row and populate it */
+		line = JS_NewArrayObject(cx, 0, NULL);
+		/* user name */
+		jsstr = JS_NewStringCopyZ(cx, u.name);
+		jv = STRING_TO_JSVAL(jsstr);
+		JS_SetElement(cx, line, rown++, &jv);
+		/* room number */
+		jv = INT_TO_JSVAL(u.room);
+		JS_SetElement(cx, line, rown++, &jv);
+		/* idle time */
+		jv = INT_TO_JSVAL(time(0)-u.idletime);
+		JS_SetElement(cx, line, rown++, &jv);
+		/* chat modes */
+		jsstr = JS_NewStringCopyZ(cx, display_cmflags(u.chatmode));
+		jv = STRING_TO_JSVAL(jsstr);
+		JS_SetElement(cx, line, rown++, &jv);
+		/* protection level */
+		jv = INT_TO_JSVAL((u.chatmode & CM_PROTMASK) >> CM_PROTSHIFT);
+		JS_SetElement(cx, line, rown++, &jv);
+		jv = INT_TO_JSVAL((u.chatprivs & CP_PROTMASK) >> CP_PROTSHIFT);
+		JS_SetElement(cx, line, rown++, &jv);
+		/* chat privs */
+		jsstr = JS_NewStringCopyZ(cx, display_cpflags(u.chatprivs & user->chatprivs));
+		jv = STRING_TO_JSVAL(jsstr);
+		JS_SetElement(cx, line, rown++, &jv);
+
+		/* stick line into master array */
+		jv = OBJECT_TO_JSVAL(line);
+		JS_SetElement(cx, res, n++, &jv);
+	}
+
+	JS_RemoveRoot(cx, res);
+	*rval = OBJECT_TO_JSVAL(res);
+	return JS_TRUE;
+}
+
+
 // Provides a javascript function to query an sqlite3 database
 static JSBool
 js_doquery(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) {
@@ -572,7 +644,6 @@
 int setup_js(void)
 {
 	JSBool builtins;
-	JSFunction *ok;
 	struct passwd *pw;
 	int is_local=1;
 
@@ -606,15 +677,16 @@
 
 	/* initiate local stuff */
 
-	ok = JS_DefineFunction(jscx, jsroot, "print", js_print, 1, 0);
-	ok = JS_DefineFunction(jscx, jsroot, "exec", js_mwexec, 1, 0);
-	ok = JS_DefineFunction(jscx, jsroot, "say", js_say, 1, 0);
+	JS_DefineFunction(jscx, jsroot, "print", js_print, 1, 0);
+	JS_DefineFunction(jscx, jsroot, "exec", js_mwexec, 1, 0);
+	JS_DefineFunction(jscx, jsroot, "say", js_say, 1, 0);
+	JS_DefineFunction(jscx, jsroot, "wholist", js_wholist, 0, 1);
 
-	ok = JS_DefineProperty(jscx, jsroot, "whoami", STRING_TO_JSVAL(JS_NewStringCopyZ(jscx,user->name)), NULL, NULL, JSPROP_READONLY|JSPROP_PERMANENT);
+	JS_DefineProperty(jscx, jsroot, "whoami", STRING_TO_JSVAL(JS_NewStringCopyZ(jscx,user->name)), NULL, NULL, JSPROP_READONLY|JSPROP_PERMANENT);
 
 	/* not for bbs user */
 	if (is_local) {
-		ok = JS_DefineFunction(jscx, jsroot, "dbquery", js_doquery, 2, 1);
+		JS_DefineFunction(jscx, jsroot, "dbquery", js_doquery, 2, 1);
 	}
 
 	/* need additional functions : 





More information about the mw-devel mailing list