diff --git a/core/ssb.js b/core/ssb.js index 67e919f7..7e70f717 100644 --- a/core/ssb.js +++ b/core/ssb.js @@ -4,7 +4,6 @@ var g_database = new Database('core'); let g_attendants = {}; const k_use_create_history_stream = false; const k_blobs_concurrent_target = 8; -const k_settings = JSON.parse(g_database.get('settings') ?? '{"room": true}'); function following(db, id) { var o = db.get(id + ":following"); @@ -195,14 +194,6 @@ ssb.addRpc(['blobs', 'createWants'], function(request) { requestMoreBlobs(request); }); -ssb.addRpc(['tunnel', 'isRoom'], function(request) { - if (k_settings.room) { - request.send_json({"name": "tilde friends tunnel", "membership": false, "features": ["tunnel", "room1"]}); - } else { - request.send_json(false); - } -}); - function notify_attendant_changed(id, type) { if (!id) { print(`notify_attendant_changed called with id=${id}`); diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index d60e1f22..72556dd9 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -5,6 +5,8 @@ #include "ssb.db.h" #include "util.js.h" +#include + #include #include #include @@ -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); }