tunnel.isRoom => C.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4089 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		@@ -5,6 +5,8 @@
 | 
			
		||||
#include "ssb.db.h"
 | 
			
		||||
#include "util.js.h"
 | 
			
		||||
 | 
			
		||||
#include <sqlite3.h>
 | 
			
		||||
 | 
			
		||||
#include <inttypes.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
@@ -206,10 +208,68 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
 | 
			
		||||
	JS_FreeValue(context, arg_array);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool _get_global_setting_bool(tf_ssb_t* ssb, const char* name, bool default_value)
 | 
			
		||||
{
 | 
			
		||||
	bool result = default_value;
 | 
			
		||||
	JSContext* context = tf_ssb_get_context(ssb);
 | 
			
		||||
	sqlite3* db = tf_ssb_get_db(ssb);
 | 
			
		||||
	sqlite3_stmt* statement;
 | 
			
		||||
	if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = 'core' AND key = 'settings'", -1, &statement, NULL) == SQLITE_OK)
 | 
			
		||||
	{
 | 
			
		||||
		if (sqlite3_step(statement) == SQLITE_ROW)
 | 
			
		||||
		{
 | 
			
		||||
			JSValue value = JS_ParseJSON(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0), NULL);
 | 
			
		||||
			JSValue property = JS_GetPropertyStr(context, value,  name);
 | 
			
		||||
			if (JS_IsBool(property))
 | 
			
		||||
			{
 | 
			
		||||
				result = JS_ToBool(context, property);
 | 
			
		||||
			}
 | 
			
		||||
			JS_FreeValue(context, property);
 | 
			
		||||
			JS_FreeValue(context, value);
 | 
			
		||||
		}
 | 
			
		||||
		sqlite3_finalize(statement);
 | 
			
		||||
	}
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void _tf_ssb_rpc_tunnel_is_room(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
 | 
			
		||||
{
 | 
			
		||||
	tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
 | 
			
		||||
	JSContext* context = tf_ssb_get_context(ssb);
 | 
			
		||||
	JSValue response = JS_FALSE;
 | 
			
		||||
	if (_get_global_setting_bool(ssb, "room", true))
 | 
			
		||||
	{
 | 
			
		||||
		response = JS_NewObject(context);
 | 
			
		||||
		JS_SetPropertyStr(context, response, "name", JS_NewString(context, "tilde friends tunnel"));
 | 
			
		||||
		JS_SetPropertyStr(context, response, "membership", JS_FALSE);
 | 
			
		||||
		JSValue features = JS_NewArray(context);
 | 
			
		||||
		JS_SetPropertyUint32(context, features, 0, JS_NewString(context, "tunnel"));
 | 
			
		||||
		JS_SetPropertyUint32(context, features, 1, JS_NewString(context, "room1"));
 | 
			
		||||
		JS_SetPropertyStr(context, response, "features", features);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	JSValue message_val = JS_JSONStringify(context, response, JS_NULL, JS_NULL);
 | 
			
		||||
	size_t json_size = 0;
 | 
			
		||||
	const char* message_str = JS_ToCStringLen(context, &json_size, message_val);
 | 
			
		||||
	tf_ssb_connection_rpc_send(
 | 
			
		||||
		connection,
 | 
			
		||||
		flags | k_ssb_rpc_flag_end_error,
 | 
			
		||||
		-request_number,
 | 
			
		||||
		(const uint8_t*)message_str,
 | 
			
		||||
		json_size,
 | 
			
		||||
		NULL,
 | 
			
		||||
		NULL,
 | 
			
		||||
		NULL);
 | 
			
		||||
	JS_FreeCString(context, message_str);
 | 
			
		||||
	JS_FreeValue(context, message_val);
 | 
			
		||||
	JS_FreeValue(context, response);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tf_ssb_rpc_register(tf_ssb_t* ssb)
 | 
			
		||||
{
 | 
			
		||||
	tf_ssb_add_rpc_callback(ssb, (const char*[]) { "gossip", "ping", NULL }, _tf_ssb_rpc_gossip_ping, NULL, NULL);
 | 
			
		||||
	tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "get", NULL }, _tf_ssb_rpc_blobs_get, NULL, NULL);
 | 
			
		||||
	tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "has", NULL }, _tf_ssb_rpc_blobs_has, NULL, NULL);
 | 
			
		||||
	tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "connect", NULL }, _tf_ssb_rpc_tunnel_connect, NULL, NULL);
 | 
			
		||||
	tf_ssb_add_rpc_callback(ssb, (const char*[]) { "tunnel", "isRoom", NULL }, _tf_ssb_rpc_tunnel_is_room, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user