/* * sx_connectors.h -- persistent state for plugin_ntfy. * * Everything the connector remembers lives in ONE directory, mode 0600 files: * * $SX_CONNECTORS_DIR/plugin_ntfy/ (explicit override, used by tests) * /workspace/.scorpiox/connectors/plugin_ntfy/ BotCloud container (survives restarts) * ~/.scorpiox/connectors/plugin_ntfy/ plain node * %LOCALAPPDATA%\scorpiox\connectors\plugin_ntfy\ Windows node * * Files: * config.json operator settings. The node password and the ntfy bearer * token are write-only: no action ever returns them, only a * boolean "set" flag (asserted by the test suite). * approved.json identities allowed to reach the agent. * pending.json identities held in the pairing queue, each with its code. * sent.json the ids of notifications THIS plugin published, so the * subscription can recognise its own replies when ntfy * echoes them back (the anti-self-loop ledger). * status.json written by the daemon: liveness, counters, last error. * daemon.pid pid of the running daemon (liveness is proven by probing it). * daemon.log rotating log (daemon.log -> daemon.log.1 at 1 MiB). * * Writes are always tmp-file + fsync + rename, so a crash mid-save cannot * leave a half-written config. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_NTFY_STATE_H #define SX_NTFY_STATE_H #include #define SX_NTFY_IDENTITY_LEN 64 /* hex sha256 (64 chars) of the sender fingerprint. * It was 80, which made valid_key() reject every * real identity (sx_sha256_hex returns exactly 64 * chars): approve/deny could never accept a key * copied straight out of ?action=pending. */ #define SX_NTFY_CODE_LEN 8 /* pairing code, e.g. "K7QF2M" */ typedef struct { /* transport */ char server_url[256]; /* default https://ntfy.sh */ char topic[160]; /* subscribe topic */ char publish_topic[160]; /* reply topic, defaults to topic */ char bearer_token[256]; /* sent verbatim as Authorization; never echoed */ char ca_file[512]; /* extra PEM anchors for a self-hosted relay */ char allowed_pins[1024]; /* comma/space separated SHA-256 SPKI pins */ /* bot bridge */ char bot_api_url[256]; /* default http://127.0.0.1:8150 */ char bot_password[256]; /* X-Bot-Password; never echoed */ char target_session[256]; /* session id to POST /inbox?id= */ /* publishing */ int priority; /* 1..5, default 3 */ char tags[128]; /* default tags for our own notifications */ int markdown; /* X-Markdown: true */ char reply_prefix[32]; /* marks our own outbound text (anti-echo) */ char title_prefix[48]; /* Title: on our replies */ /* pairing / trust */ int allow_all; /* dev only: forward every sender (default 0) */ int pairing_code_enabled; /* accept "PAIR " on the topic (default 1) */ /* loop tuning */ long long reply_poll_ms; /* /conversation poll interval */ long long idle_timeout_ms; /* subscription readline deadline */ long long reconnect_min_ms, reconnect_max_ms; /* provenance, for the UI */ long long updated_at; } sx_ntfy_config; typedef struct { char key[SX_NTFY_IDENTITY_LEN + 1]; /* identity = sha256hex(fingerprint) */ char label[160]; /* what the UI shows: user/tags sample */ char code[SX_NTFY_CODE_LEN + 1]; /* pairing code to present */ long long first_seen, last_seen; long long last_challenge_at; /* challenge rate limit */ int count; char sample[240]; /* first 200 chars of a sample message */ } sx_ntfy_pending; typedef struct { char key[SX_NTFY_IDENTITY_LEN + 1]; char label[160]; long long added_at, last_seen; int count; int via; /* 1 = pairing code, 0 = operator UI */ } sx_ntfy_approved; typedef struct { char id[40]; long long at; } sx_ntfy_sent_id; /* --------------------------------------------------------------- paths */ /* Resolves (and creates) the connector directory. Returns 0 on success. */ int sx_ntfy_dir(char *out, size_t capsz); int sx_ntfy_path(char *out, size_t capsz, const char *file); /* ---------------------------------------------------------------- config */ void sx_ntfy_config_defaults(sx_ntfy_config *c); /* Loads config.json (if any) then applies environment overrides on top: * NTFY_SERVER_URL, NTFY_TOPIC, NTFY_PUBLISH_TOPIC, NTFY_TOKEN, * NTFY_TARGET_SESSION, NTFY_BOT_API_URL, BOT_PASSWORD, NTFY_MARKDOWN, * NTFY_PRIORITY, NTFY_TAGS, NTFY_REPLY_PREFIX, NTFY_TITLE_PREFIX, * NTFY_ALLOW_ALL, NTFY_PAIRING_CODE, NTFY_CA_FILE, NTFY_TLS_PIN, * NTFY_TLS_INSECURE, NTFY_REPLY_POLL_MS, NTFY_IDLE_TIMEOUT_MS. */ int sx_ntfy_config_load(sx_ntfy_config *c, char *err, size_t errsz); int sx_ntfy_config_save(const sx_ntfy_config *c, char *err, size_t errsz); /* True when the environment/OS says verification may be skipped. Deliberately * reachable ONLY from the environment, never from config.json or a request. */ int sx_ntfy_tls_insecure(void); /* Sanity checks before publishing/starting a daemon. */ int sx_ntfy_config_validate(const sx_ntfy_config *c, char *err, size_t errsz); /* --------------------------------------------------------- pairing state */ int sx_ntfy_pending_load(sx_ntfy_pending **out, size_t *n_out); int sx_ntfy_pending_save(const sx_ntfy_pending *items, size_t n); /* Adds or refreshes a pending entry, returns the (possibly existing) entry. * NULL when the queue is full (cap 64) -- then nothing is stored. */ sx_ntfy_pending *sx_ntfy_pending_touch(sx_ntfy_pending **items, size_t *n, size_t *cap, const char *key, const char *label, const char *sample); int sx_ntfy_pending_remove(const char *key); int sx_ntfy_pending_find_code(const char *code_upper, char *key_out, size_t capsz); int sx_ntfy_approved_load(sx_ntfy_approved **out, size_t *n_out); int sx_ntfy_approved_save(const sx_ntfy_approved *items, size_t n); int sx_ntfy_approved_has(const char *key); int sx_ntfy_approved_add(const char *key, const char *label, int via); int sx_ntfy_approved_remove(const char *key); int sx_ntfy_approved_touch_seen(const char *key); /* ------------------------------------------------------- self-echo ledger */ int sx_ntfy_sent_note(const char *id); /* remember our own publish */ int sx_ntfy_sent_has(const char *id); /* ------------------------------------------------------- daemon plumbing */ int sx_ntfy_pidfile_read(long *pid_out); int sx_ntfy_pidfile_write(long pid); int sx_ntfy_pidfile_remove(void); int sx_ntfy_daemon_running(void); /* 1 when the pidfile pid is alive */ int sx_ntfy_log_open(int append); /* returns FILE* or NULL */ void sx_ntfy_logf(const char *fmt, ...); /* stderr + daemon.log when open */ /* status.json ---------------------------------------------------------- */ typedef struct { long long started_at, last_seen_at, last_inbound_at, last_outbound_at; long long inbound_count, outbound_count, blocked_count, pending_count, connect_count, error_count; int connected; /* subscription currently open */ char last_error[256]; char last_event[256]; long long last_message_time; /* ntfy time of the last record seen, for since= */ long long conv_cursor; /* /conversation sequence already delivered */ int configured; char topic[160]; char publish_topic[160]; char target_session[256]; char server_url[256]; int allow_all, pairing_code_enabled, markdown; int has_bearer, has_bot_password; int tls_insecure; int pinned; } sx_ntfy_status; void sx_ntfy_status_defaults(sx_ntfy_status *s); int sx_ntfy_status_load(sx_ntfy_status *s); int sx_ntfy_status_save(const sx_ntfy_status *s); /* small helpers shared by the plugin body */ long long sx_ntfy_now_epoch(void); void sx_ntfy_random_code(char out[SX_NTFY_CODE_LEN + 1]); void sx_ntfy_json_escape(const char *in, char *out, size_t capsz); int sx_ntfy_read_file(const char *path, char **buf, size_t *len); int sx_ntfy_write_file_atomic(const char *path, const void *data, size_t len, int mode600); #endif /* SX_NTFY_STATE_H */