From ede7c8f99be8cc12ff868c0987af41ea5038a5ae Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Wed, 9 Jul 2003 04:31:10 +0000
Subject: [PATCH] Created global read and readln methods (standardized across
 all Synchronet servers) - needs work (timeout support, LF-termination, etc).

---
 src/sbbs3/services.c | 58 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/src/sbbs3/services.c b/src/sbbs3/services.c
index a9005bc19b..b2182e18ea 100644
--- a/src/sbbs3/services.c
+++ b/src/sbbs3/services.c
@@ -248,6 +248,62 @@ static time_t checktime(void)
 
 /* Global JavaScript Methods */
 
+static JSBool
+js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
+{
+	char*		buf;
+	int32		len=512;
+	service_client_t* client;
+
+	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
+		return(JS_FALSE);
+
+	*rval = JSVAL_VOID;
+
+	if(argc)
+		JS_ValueToInt32(cx,argv[0],&len);
+	
+	if((buf=malloc(len))==NULL)
+		return(JS_TRUE);
+
+	len=recv(client->socket,buf,len,0);
+
+	if(len>0)
+		*rval = STRING_TO_JSVAL(JS_NewStringCopyN(cx,buf,len));
+
+	free(buf);
+
+	return(JS_TRUE);
+}
+
+static JSBool
+js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
+{
+	char*		buf;
+	int32		len=512;
+	service_client_t* client;
+
+	if((client=(service_client_t*)JS_GetContextPrivate(cx))==NULL)
+		return(JS_FALSE);
+
+	*rval = JSVAL_VOID;
+
+	if(argc)
+		JS_ValueToInt32(cx,argv[0],&len);
+	
+	if((buf=malloc(len))==NULL)
+		return(JS_TRUE);
+
+	len=recv(client->socket,buf,len,0);	/* Need to switch to sockreadline */
+
+	if(len>0)
+		*rval = STRING_TO_JSVAL(JS_NewStringCopyN(cx,buf,len));
+
+	free(buf);
+
+	return(JS_TRUE);
+}
+
 static JSBool
 js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 {
@@ -446,6 +502,8 @@ js_logout(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 }
 
 static JSFunctionSpec js_global_functions[] = {
+	{"read",			js_read,			0},		/* read from client socket */
+	{"readln",			js_readln,			0},		/* read line from client socket */
 	{"write",			js_write,			0},		/* write to client socket */
 	{"writeln",			js_writeln,			0},		/* write line to client socket */
 	{"print",			js_writeln,			0},		/* write line to client socket */
-- 
GitLab