scorpiox-bot-plugin-ntfy
←sx_connectors.hc
191 lines · 8 KBHEADBlameDownload
1/*
2 * sx_connectors.h -- persistent state for plugin_ntfy.
3 *
4 * Everything the connector remembers lives in ONE directory, mode 0600 files:
5 *
6 * $SX_CONNECTORS_DIR/plugin_ntfy/ (explicit override, used by tests)
7 * /workspace/.scorpiox/connectors/plugin_ntfy/ BotCloud container (survives restarts)
8 * ~/.scorpiox/connectors/plugin_ntfy/ plain node
9 * %LOCALAPPDATA%\scorpiox\connectors\plugin_ntfy\ Windows node
10 *
11 * Files:
12 * config.json operator settings. The node password and the ntfy bearer
13 * token are write-only: no action ever returns them, only a
14 * boolean "set" flag (asserted by the test suite).
15 * approved.json identities allowed to reach the agent.
16 * pending.json identities held in the pairing queue, each with its code.
17 * sent.json the ids of notifications THIS plugin published, so the
18 * subscription can recognise its own replies when ntfy
19 * echoes them back (the anti-self-loop ledger).
20 * status.json written by the daemon: liveness, counters, last error.
21 * daemon.pid pid of the running daemon (liveness is proven by probing it).
22 * daemon.log rotating log (daemon.log -> daemon.log.1 at 1 MiB).
23 *
24 * Writes are always tmp-file + fsync + rename, so a crash mid-save cannot
25 * leave a half-written config.
26 *
27 * MIT License. Copyright (c) 2026 SCORPIOX INC.
28 */
29#ifndef SX_NTFY_STATE_H
30#define SX_NTFY_STATE_H
31
32#include <stddef.h>
33
34#define SX_NTFY_IDENTITY_LEN 64 /* hex sha256 (64 chars) of the sender fingerprint.
35 * It was 80, which made valid_key() reject every
36 * real identity (sx_sha256_hex returns exactly 64
37 * chars): approve/deny could never accept a key
38 * copied straight out of ?action=pending. */
39#define SX_NTFY_CODE_LEN 8 /* pairing code, e.g. "K7QF2M" */
40
41typedef struct {
42 /* transport */
43 char server_url[256]; /* default https://ntfy.sh */
44 char topic[160]; /* subscribe topic */
45 char publish_topic[160]; /* reply topic, defaults to topic */
46 char bearer_token[256]; /* sent verbatim as Authorization; never echoed */
47 char ca_file[512]; /* extra PEM anchors for a self-hosted relay */
48 char allowed_pins[1024]; /* comma/space separated SHA-256 SPKI pins */
49
50 /* bot bridge */
51 char bot_api_url[256]; /* default http://127.0.0.1:8150 */
52 char bot_password[256]; /* X-Bot-Password; never echoed */
53 char target_session[256]; /* session id to POST /inbox?id= */
54
55 /* publishing */
56 int priority; /* 1..5, default 3 */
57 char tags[128]; /* default tags for our own notifications */
58 int markdown; /* X-Markdown: true */
59 char reply_prefix[32]; /* marks our own outbound text (anti-echo) */
60 char title_prefix[48]; /* Title: on our replies */
61
62 /* pairing / trust */
63 int allow_all; /* dev only: forward every sender (default 0) */
64 int pairing_code_enabled; /* accept "PAIR <code>" on the topic (default 1) */
65
66 /* loop tuning */
67 long long reply_poll_ms; /* /conversation poll interval */
68 long long idle_timeout_ms; /* subscription readline deadline */
69 long long reconnect_min_ms, reconnect_max_ms;
70
71 /* provenance, for the UI */
72 long long updated_at;
73} sx_ntfy_config;
74
75typedef struct {
76 char key[SX_NTFY_IDENTITY_LEN + 1]; /* identity = sha256hex(fingerprint) */
77 char label[160]; /* what the UI shows: user/tags sample */
78 char code[SX_NTFY_CODE_LEN + 1]; /* pairing code to present */
79 long long first_seen, last_seen;
80 long long last_challenge_at; /* challenge rate limit */
81 int count;
82 char sample[240]; /* first 200 chars of a sample message */
83} sx_ntfy_pending;
84
85typedef struct {
86 char key[SX_NTFY_IDENTITY_LEN + 1];
87 char label[160];
88 long long added_at, last_seen;
89 int count;
90 int via; /* 1 = pairing code, 0 = operator UI */
91} sx_ntfy_approved;
92
93typedef struct {
94 char id[40];
95 long long at;
96} sx_ntfy_sent_id;
97
98/* --------------------------------------------------------------- paths */
99
100/* Resolves (and creates) the connector directory. Returns 0 on success. */
101int sx_ntfy_dir(char *out, size_t capsz);
102int sx_ntfy_path(char *out, size_t capsz, const char *file);
103
104/* ---------------------------------------------------------------- config */
105
106void sx_ntfy_config_defaults(sx_ntfy_config *c);
107
108/* Loads config.json (if any) then applies environment overrides on top:
109 * NTFY_SERVER_URL, NTFY_TOPIC, NTFY_PUBLISH_TOPIC, NTFY_TOKEN,
110 * NTFY_TARGET_SESSION, NTFY_BOT_API_URL, BOT_PASSWORD, NTFY_MARKDOWN,
111 * NTFY_PRIORITY, NTFY_TAGS, NTFY_REPLY_PREFIX, NTFY_TITLE_PREFIX,
112 * NTFY_ALLOW_ALL, NTFY_PAIRING_CODE, NTFY_CA_FILE, NTFY_TLS_PIN,
113 * NTFY_TLS_INSECURE, NTFY_REPLY_POLL_MS, NTFY_IDLE_TIMEOUT_MS. */
114int sx_ntfy_config_load(sx_ntfy_config *c, char *err, size_t errsz);
115int sx_ntfy_config_save(const sx_ntfy_config *c, char *err, size_t errsz);
116
117/* True when the environment/OS says verification may be skipped. Deliberately
118 * reachable ONLY from the environment, never from config.json or a request. */
119int sx_ntfy_tls_insecure(void);
120
121/* Sanity checks before publishing/starting a daemon. */
122int sx_ntfy_config_validate(const sx_ntfy_config *c, char *err, size_t errsz);
123
124/* --------------------------------------------------------- pairing state */
125
126int sx_ntfy_pending_load(sx_ntfy_pending **out, size_t *n_out);
127int sx_ntfy_pending_save(const sx_ntfy_pending *items, size_t n);
128/* Adds or refreshes a pending entry, returns the (possibly existing) entry.
129 * NULL when the queue is full (cap 64) -- then nothing is stored. */
130sx_ntfy_pending *sx_ntfy_pending_touch(sx_ntfy_pending **items, size_t *n, size_t *cap,
131 const char *key, const char *label,
132 const char *sample);
133int sx_ntfy_pending_remove(const char *key);
134int sx_ntfy_pending_find_code(const char *code_upper, char *key_out, size_t capsz);
135
136int sx_ntfy_approved_load(sx_ntfy_approved **out, size_t *n_out);
137int sx_ntfy_approved_save(const sx_ntfy_approved *items, size_t n);
138int sx_ntfy_approved_has(const char *key);
139int sx_ntfy_approved_add(const char *key, const char *label, int via);
140int sx_ntfy_approved_remove(const char *key);
141int sx_ntfy_approved_touch_seen(const char *key);
142
143/* ------------------------------------------------------- self-echo ledger */
144
145int sx_ntfy_sent_note(const char *id); /* remember our own publish */
146int sx_ntfy_sent_has(const char *id);
147
148/* ------------------------------------------------------- daemon plumbing */
149
150int sx_ntfy_pidfile_read(long *pid_out);
151int sx_ntfy_pidfile_write(long pid);
152int sx_ntfy_pidfile_remove(void);
153int sx_ntfy_daemon_running(void); /* 1 when the pidfile pid is alive */
154
155int sx_ntfy_log_open(int append); /* returns FILE* or NULL */
156void sx_ntfy_logf(const char *fmt, ...); /* stderr + daemon.log when open */
157
158/* status.json ---------------------------------------------------------- */
159typedef struct {
160 long long started_at, last_seen_at, last_inbound_at, last_outbound_at;
161 long long inbound_count, outbound_count, blocked_count, pending_count,
162 connect_count, error_count;
163 int connected; /* subscription currently open */
164 char last_error[256];
165 char last_event[256];
166 long long last_message_time; /* ntfy time of the last record seen, for since= */
167 long long conv_cursor; /* /conversation sequence already delivered */
168 int configured;
169 char topic[160];
170 char publish_topic[160];
171 char target_session[256];
172 char server_url[256];
173 int allow_all, pairing_code_enabled, markdown;
174 int has_bearer, has_bot_password;
175 int tls_insecure;
176 int pinned;
177} sx_ntfy_status;
178
179void sx_ntfy_status_defaults(sx_ntfy_status *s);
180int sx_ntfy_status_load(sx_ntfy_status *s);
181int sx_ntfy_status_save(const sx_ntfy_status *s);
182
183/* small helpers shared by the plugin body */
184long long sx_ntfy_now_epoch(void);
185void sx_ntfy_random_code(char out[SX_NTFY_CODE_LEN + 1]);
186void sx_ntfy_json_escape(const char *in, char *out, size_t capsz);
187int sx_ntfy_read_file(const char *path, char **buf, size_t *len);
188int sx_ntfy_write_file_atomic(const char *path, const void *data, size_t len,
189 int mode600);
190
191#endif /* SX_NTFY_STATE_H */