177 lines
5.1 KiB
C
177 lines
5.1 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "tools_internal.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "cjson/cJSON.h"
|
|
#include "../nostr_handler.h"
|
|
|
|
static char* json_error_local(const char* msg) {
|
|
cJSON* root = cJSON_CreateObject();
|
|
if (!root) return NULL;
|
|
cJSON_AddBoolToObject(root, "success", 0);
|
|
cJSON_AddStringToObject(root, "error", msg ? msg : "unknown error");
|
|
char* out = cJSON_PrintUnformatted(root);
|
|
cJSON_Delete(root);
|
|
return out;
|
|
}
|
|
|
|
static cJSON* parse_args_local(const char* args_json) {
|
|
const char* raw = args_json ? args_json : "{}";
|
|
cJSON* args = cJSON_Parse(raw);
|
|
if (!args || !cJSON_IsObject(args)) {
|
|
cJSON_Delete(args);
|
|
return NULL;
|
|
}
|
|
return args;
|
|
}
|
|
|
|
char* execute_admin_identity(tools_context_t* ctx, const char* args_json) {
|
|
if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");
|
|
|
|
cJSON* args = parse_args_local(args_json);
|
|
if (!args) return json_error_local("invalid arguments JSON");
|
|
cJSON_Delete(args);
|
|
|
|
cJSON* out = cJSON_CreateObject();
|
|
if (!out) return NULL;
|
|
|
|
const char* tier_text = "Sender tier unknown.";
|
|
if (ctx->template_sender_tier == 1) {
|
|
tier_text = "This message has been cryptographically verified as coming from your administrator.";
|
|
} else if (ctx->template_sender_tier == 2) {
|
|
tier_text = "This message is from a web-of-trust contact (not the administrator).";
|
|
}
|
|
|
|
cJSON_AddBoolToObject(out, "success", 1);
|
|
|
|
cJSON* identity = cJSON_CreateObject();
|
|
if (!identity) {
|
|
cJSON_Delete(out);
|
|
return NULL;
|
|
}
|
|
cJSON_AddStringToObject(identity,
|
|
"admin_pubkey",
|
|
ctx->cfg->admin.pubkey[0] ? ctx->cfg->admin.pubkey : "unknown");
|
|
cJSON_AddStringToObject(identity, "sender_tier", tier_text);
|
|
char* identity_json = cJSON_PrintUnformatted(identity);
|
|
cJSON_Delete(identity);
|
|
if (!identity_json) {
|
|
cJSON_Delete(out);
|
|
return NULL;
|
|
}
|
|
cJSON_AddStringToObject(out, "content", identity_json);
|
|
free(identity_json);
|
|
|
|
char* json = cJSON_PrintUnformatted(out);
|
|
cJSON_Delete(out);
|
|
return json;
|
|
}
|
|
|
|
char* execute_nostr_admin_profile(tools_context_t* ctx, const char* args_json) {
|
|
if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");
|
|
|
|
cJSON* args = parse_args_local(args_json);
|
|
if (!args) return json_error_local("invalid arguments JSON");
|
|
cJSON_Delete(args);
|
|
|
|
char* kind0 = nostr_handler_get_admin_kind0_context();
|
|
const char* profile_json = (kind0 && kind0[0]) ? kind0 : "{}";
|
|
|
|
cJSON* out = cJSON_CreateObject();
|
|
if (!out) {
|
|
free(kind0);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddBoolToObject(out, "success", 1);
|
|
|
|
cJSON_AddStringToObject(out, "content", profile_json);
|
|
|
|
char* json = cJSON_PrintUnformatted(out);
|
|
cJSON_Delete(out);
|
|
free(kind0);
|
|
return json;
|
|
}
|
|
|
|
char* execute_nostr_admin_contacts(tools_context_t* ctx, const char* args_json) {
|
|
if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");
|
|
|
|
cJSON* args = parse_args_local(args_json);
|
|
if (!args) return json_error_local("invalid arguments JSON");
|
|
cJSON_Delete(args);
|
|
|
|
char* kind3 = nostr_handler_get_admin_kind3_context();
|
|
const char* contacts_json = (kind3 && kind3[0]) ? kind3 : "[]";
|
|
|
|
cJSON* out = cJSON_CreateObject();
|
|
if (!out) {
|
|
free(kind3);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddBoolToObject(out, "success", 1);
|
|
|
|
cJSON_AddStringToObject(out, "content", contacts_json);
|
|
|
|
char* json = cJSON_PrintUnformatted(out);
|
|
cJSON_Delete(out);
|
|
free(kind3);
|
|
return json;
|
|
}
|
|
|
|
char* execute_nostr_admin_relays(tools_context_t* ctx, const char* args_json) {
|
|
if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");
|
|
|
|
cJSON* args = parse_args_local(args_json);
|
|
if (!args) return json_error_local("invalid arguments JSON");
|
|
cJSON_Delete(args);
|
|
|
|
char* kind10002 = nostr_handler_get_admin_kind10002_context();
|
|
const char* relays_json = (kind10002 && kind10002[0]) ? kind10002 : "[]";
|
|
|
|
cJSON* out = cJSON_CreateObject();
|
|
if (!out) {
|
|
free(kind10002);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddBoolToObject(out, "success", 1);
|
|
|
|
cJSON_AddStringToObject(out, "content", relays_json);
|
|
|
|
char* json = cJSON_PrintUnformatted(out);
|
|
cJSON_Delete(out);
|
|
free(kind10002);
|
|
return json;
|
|
}
|
|
|
|
char* execute_nostr_admin_notes(tools_context_t* ctx, const char* args_json) {
|
|
if (!ctx || !ctx->cfg) return json_error_local("tool context unavailable");
|
|
|
|
cJSON* args = parse_args_local(args_json);
|
|
if (!args) return json_error_local("invalid arguments JSON");
|
|
cJSON_Delete(args);
|
|
|
|
char* notes = nostr_handler_get_admin_kind1_notes_context();
|
|
const char* notes_text = (notes && notes[0]) ? notes : "";
|
|
|
|
cJSON* out = cJSON_CreateObject();
|
|
if (!out) {
|
|
free(notes);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddBoolToObject(out, "success", 1);
|
|
|
|
cJSON_AddStringToObject(out, "content", notes_text);
|
|
|
|
char* json = cJSON_PrintUnformatted(out);
|
|
cJSON_Delete(out);
|
|
free(notes);
|
|
return json;
|
|
}
|