From 84a3d7348d9443e64edf7c019bc6cc71ee267e7c Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 1 Dec 2021 23:29:53 +0000 Subject: [PATCH] Add a way to set arbitrary data accessible by all tasks. Use it to allow autologin for testing multiple instances more easily. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3689 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/auth.js | 5 +++-- src/main.c | 3 +++ src/task.c | 35 +++++++++++++++++++++++++++++++++++ src/task.h | 1 + 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/core/auth.js b/core/auth.js index 7e66a8f4..f5d4b1c6 100644 --- a/core/auth.js +++ b/core/auth.js @@ -196,8 +196,9 @@ function getPermissionsForUser(userName) { function query(headers) { var session = getCookies(headers).session; var entry; - if (entry = readSession(session)) { - return {session: entry, permissions: getPermissions(session)}; + var autologin = tildefriends.args.autologin; + if (entry = autologin ? {name: autologin} : readSession(session)) { + return {session: entry, permissions: autologin ? getPermissionsForUser(autologin) : getPermissions(session)}; } } diff --git a/src/main.c b/src/main.c index 6d10b562..da387fec 100644 --- a/src/main.c +++ b/src/main.c @@ -306,6 +306,7 @@ typedef struct tf_run_args_t { const char* db_path; const char* secrets_path; int count; + const char* args; bool help; } tf_run_args_t; @@ -324,6 +325,7 @@ static int _tf_run_task(const tf_run_args_t* args, int index) tf_task_set_ssb_port(task, args->ssb_port ? args->ssb_port + index : 0); tf_task_set_http_port(task, args->http_port ? args->http_port + index : 0); tf_task_set_https_port(task, args->https_port ? args->https_port + index : 0); + tf_task_set_args(task, args->args); const char* db_path = args->db_path; const char* secrets_path = args->secrets_path; char db_path_buffer[256]; @@ -363,6 +365,7 @@ static int _tf_command_run(const char* file, int argc, char* argv[]) { "db-path", 'd', offsetof(tf_run_args_t, db_path), NULL, XOPT_TYPE_STRING, NULL, "Sqlite database path (default: db.sqlite)." }, { "secrets-path", 'i', offsetof(tf_run_args_t, secrets_path), NULL, XOPT_TYPE_STRING, NULL, "Secrets/identity path." }, { "count", 'n', offsetof(tf_run_args_t, count), NULL, XOPT_TYPE_INT, NULL, "Number of instances to run." }, + { "args", 'a', offsetof(tf_run_args_t, args), NULL, XOPT_TYPE_STRING, NULL, "Arguments of the form key=value,foo=bar,verbose=true." }, { "help", 'h', offsetof(tf_run_args_t, help), NULL, XOPT_TYPE_BOOL, NULL, "Shows this help message." }, XOPT_NULLOPTION, }; diff --git a/src/task.c b/src/task.c index 97107d2b..8773bcc2 100644 --- a/src/task.c +++ b/src/task.c @@ -107,6 +107,7 @@ typedef struct _tf_task_t { int _https_port; char _db_path[256]; char _secrets_path[256]; + const char* _args; } tf_task_t; typedef struct _export_record_t { @@ -1260,6 +1261,35 @@ void tf_task_activate(tf_task_t* task) JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, task->_ssb_port)); JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port)); JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port)); + JSValue args = JS_NewObject(context); + JS_SetPropertyStr(context, tildefriends, "args", args); + if (task->_args) + { + char* saveptr = NULL; + char* copy = strdup(task->_args); + char* start = copy; + while (true) + { + char* assignment = strtok_r(start, ",", &saveptr); + start = NULL; + if (!assignment) + { + break; + } + char* equals = strchr(assignment, '='); + if (equals) + { + *equals = '\0'; + JS_SetPropertyStr(context, args, assignment, JS_NewString(context, equals + 1)); + } + else + { + printf("Assignment missing '=': %s.\n", assignment); + exit(1); + } + } + free(copy); + } JS_SetPropertyStr(context, global, "tildefriends", tildefriends); if (task->_trusted) @@ -1463,6 +1493,11 @@ void tf_task_set_secrets_path(tf_task_t* task, const char* secrets_path) snprintf(task->_secrets_path, sizeof(task->_secrets_path), "%s", secrets_path); } +void tf_task_set_args(tf_task_t* task, const char* args) +{ + task->_args = args; +} + const char* tf_task_get_name(tf_task_t* task) { return task->_scriptName; diff --git a/src/task.h b/src/task.h index 3c30aca5..ee186c51 100644 --- a/src/task.h +++ b/src/task.h @@ -40,6 +40,7 @@ void tf_task_set_http_port(tf_task_t* task, int port); void tf_task_set_https_port(tf_task_t* task, int port); void tf_task_set_db_path(tf_task_t* task, const char* path); void tf_task_set_secrets_path(tf_task_t* task, const char* path); +void tf_task_set_args(tf_task_t* task, const char* args); void tf_task_activate(tf_task_t* task); void tf_task_run(tf_task_t* task); int tf_task_execute(tf_task_t* task, const char* file);