From 5a001c63a5cda5946777facf6758056d995580f2 Mon Sep 17 00:00:00 2001 From: AO55 Date: Tue, 17 Nov 2026 12:58:23 -0300 Subject: [PATCH 1/7] [rei] A Tracer report's number is no issue: "report #74", "Tracer #12" ReiWimsy linked ScarletNX's issue 74 for "Tracer report #74", and issue 12 for "@Tracer delete #12", a command of Tracer's: the number Fuddles feared the two bots would fight over. A number after report, reports or Tracer is left to Tracer. Signed-off-by: AO55 --- docs/REIWIMSY.md | 9 +++++---- docs/TESTING.md | 12 ++++++------ rei/src/refs.c | 15 ++++++++++++++- rei/src/refs.h | 3 ++- rei/tests/test_rei.c | 5 +++++ 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/docs/REIWIMSY.md b/docs/REIWIMSY.md index 68bc195..78e5e65 100644 --- a/docs/REIWIMSY.md +++ b/docs/REIWIMSY.md @@ -21,10 +21,11 @@ request, each open issue and each push to main, edited as GitHub changes. Instal ## Replies A message that names `#160` (`issue #160`, `PR #160` too; a bare number is talk) gets one reply: each linked, with its -title, issue or PR, its state and labels, in GitHub's colours; 5 numbers a message at most. `!nightly` links the newest -nightly and its downloads, `!release` the newest version (the nightly while there is none), `!release #355` a pull -request's builds. With a GitHub token it edits a reply of the last week when what it links changes, looking every 10 -minutes. +title, issue or PR, its state and labels, in GitHub's colours; 5 numbers a message at most. A number after `report`, +`reports` or `Tracer` is a Tracer report's (`Tracer report #74`, `Tracer #12`), not an issue, and gets none. `!nightly` +links the newest nightly and its downloads, `!release` the newest version (the nightly while there is none), `!release +#355` a pull request's builds. With a GitHub token it edits a reply of the last week when what it links changes, looking +every 10 minutes. It reads the text of messages only to find those numbers, and sends GitHub's API only the numbers. It keeps no message and logs nothing said, and pings no one. diff --git a/docs/TESTING.md b/docs/TESTING.md index c1e6c27..12e95c1 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -26,12 +26,12 @@ On FreeBSD, `make -C tracer test CXXRT=-lc++` ([Build](./BUILD.md#tracer)). The ## ReiWimsy -`rei/tests/test_rei.c`, no Concord: the parsing, the gateway's events (read from their JSON: Concord decodes none of -them, as its decoder crashes on messages of 2 to 3 KB) and the messages waiting for an answer, the replies and their -edits, GitHub's ETags and pages, Discord's waits and answers, and the state file. The cards: drawn, posted, edited, -moved to the end at their activity, deleted a day after they close, posted again when deleted by hand, a nightly's for -each push, the newest kept; and the fix of the cards left saying "Building" (a dry run, then each kind of card, then a -run with nothing to do). +`rei/tests/test_rei.c`, no Concord: the parsing (a Tracer report's number no issue), the gateway's events (read from +their JSON: Concord decodes none of them, as its decoder crashes on messages of 2 to 3 KB) and the messages waiting for +an answer, the replies and their edits, GitHub's ETags and pages, Discord's waits and answers, and the state file. The +cards: drawn, posted, edited, moved to the end at their activity, deleted a day after they close, posted again when +deleted by hand, a nightly's for each push, the newest kept; and the fix of the cards left saying "Building" (a dry run, +then each kind of card, then a run with nothing to do). ## AccordWimsy diff --git a/rei/src/refs.c b/rei/src/refs.c index 7da3409..b9c7424 100644 --- a/rei/src/refs.c +++ b/rei/src/refs.c @@ -5,6 +5,7 @@ #include #include #include +#include /* a letter, a digit or _; any byte of a UTF-8 letter counts, as Python's \w takes the letter */ static int word(unsigned char c) { return isalnum(c) || c == '_' || c >= 0x80; } @@ -54,6 +55,18 @@ static char* prose(const char* text) { return s; } +/* whether the word that ends at s[at - 1] (spaces before at passed over) is one that names Tracer's numbers: "report", + "reports" or "tracer", in any case */ +static int tracer_word_before(const char* s, size_t at) { + static const char* const words[] = {"report", "reports", "tracer"}; + size_t end = at, start, k; + while (end && space((unsigned char)s[end - 1])) --end; + for (start = end; start && word((unsigned char)s[start - 1]); --start); + for (k = 0; k < sizeof words / sizeof *words; ++k) + if (end - start == strlen(words[k]) && !strncasecmp(s + start, words[k], end - start)) return 1; + return 0; +} + int rei_references(const char* text, int numbers[REI_MOST]) { char* s = prose(text ? text : ""); int found = 0; @@ -64,7 +77,7 @@ int rei_references(const char* text, int numbers[REI_MOST]) { unsigned char before = i ? (unsigned char)s[i - 1] : ' '; if (s[i] != '#' || word(before) || before == '/' || before == '#' || before == '&') continue; while (isdigit((unsigned char)s[k]) && k - i <= 7) number = number * 10 + (s[k++] - '0'); - if (k == i + 1 || k - i > 7 || word((unsigned char)s[k]) || !number) continue; + if (k == i + 1 || k - i > 7 || word((unsigned char)s[k]) || !number || tracer_word_before(s, i)) continue; for (seen = 0, j = 0; j < found; ++j) seen |= numbers[j] == number; if (!seen) numbers[found++] = number; } diff --git a/rei/src/refs.h b/rei/src/refs.h index c278387..10925e3 100644 --- a/rei/src/refs.h +++ b/rei/src/refs.h @@ -7,7 +7,8 @@ #define REI_MOST 5 /* the numbers a reply links, at most */ /* the numbers text names as #160 (issue #160, PR #160 too), in order, each once, at most REI_MOST: not inside a word, a - path or a link, nor in code (`...` or ```...```); a bare 160 is talk. Their count */ + path or a link, nor in code (`...` or ```...```), nor after "report", "reports" or "Tracer" (Tracer's report + numbers: "report #74", "Tracer #12"); a bare 160 is talk. Their count */ int rei_references(const char* text, int numbers[REI_MOST]); /* the whole message as a command: !nightly, !release, or !release #N (*number); any case, spaces around */ diff --git a/rei/tests/test_rei.c b/rei/tests/test_rei.c index 3ba02d6..4030e18 100644 --- a/rei/tests/test_rei.c +++ b/rei/tests/test_rei.c @@ -132,6 +132,11 @@ static void refs(void) { CHECK(rei_references("issue 12, PR 305, pull request 7, pull 8 and pr 2 broke", n) == 0); CHECK(rei_references("#1234567 #123456 #7.", n) == 2 && n[0] == 123456 && n[1] == 7); CHECK(rei_references("\xc3\xa9#4 #4", n) == 1 && n[0] == 4); /* after a letter of UTF-8: in a word */ + /* Tracer's numbers are its reports', never an issue: "report #74", "Tracer #12", "reports #3 and #4" (the second + stands alone, an issue) */ + CHECK(rei_references("Tracer report #74 crashes, as tracer #12 and Reports #3 did", n) == 0); + c = rei_references("reports #3 and #4; the report: #5; reported #6", n); + CHECK(c == 3 && n[0] == 4 && n[1] == 5 && n[2] == 6); CHECK(rei_command("!nightly", &number) == REI_NIGHTLY); CHECK(rei_command(" !Release ", &number) == REI_RELEASE); CHECK(rei_command("!release #355", &number) == REI_RELEASE_PR && number == 355); From 9232ab5e1a7074fad31e61e599586c300a28cd60 Mon Sep 17 00:00:00 2001 From: AO55 Date: Tue, 17 Nov 2026 12:59:50 -0300 Subject: [PATCH 2/7] [rei] Each log line names its bot, whole, and each gateway is watched apart The shared pieces for one program that runs both bots: rei_log names the bot its file is built for (REI_NAME) and keeps a line whole whatever the threads, Discord's refusals name the bot whose Dc asked, and the gateway's watch follows each Concord client of the program, its line naming which. Signed-off-by: AO55 --- rei/src/main.c | 2 +- rei/src/rei.h | 7 ++-- rei/src/rest.c | 29 ++++++++-------- rei/src/rest.h | 4 ++- rei/src/util.c | 7 ++-- rei/src/watch.c | 88 ++++++++++++++++++++++++++++++++--------------- rei/src/watch.h | 8 +++-- tracer/src/main.c | 2 +- 8 files changed, 96 insertions(+), 51 deletions(-) diff --git a/rei/src/main.c b/rei/src/main.c index e5e2371..ed905f1 100644 --- a/rei/src/main.c +++ b/rei/src/main.c @@ -313,7 +313,7 @@ int main(int argc, char** argv) { discord_add_intents(client, DISCORD_GATEWAY_GUILDS | DISCORD_GATEWAY_GUILD_MESSAGES | DISCORD_GATEWAY_DIRECT_MESSAGES | DISCORD_GATEWAY_MESSAGE_CONTENT); discord_set_event_scheduler(client, scheduler); - wd_start(client, end); + wd_start(client, REI_NAME, end); code = discord_run(client); if (code == CCORD_DISCORD_BAD_AUTH) { /* what Concord's discord_run returns for any failure of GET gateway/bot */ char* now = dc_said(&dc, "gateway/bot"); diff --git a/rei/src/rei.h b/rei/src/rei.h index b52a9bf..659930a 100644 --- a/rei/src/rei.h +++ b/rei/src/rei.h @@ -41,8 +41,11 @@ const cJSON* jpath(const cJSON* o, const char* path); /* a string member added to an object, or skipped when s is NULL */ void jset(cJSON* o, const char* key, const char* s); -/* a line in the log (stderr, which daemon(8) sends to syslog): "reiwimsy: ..." (REI_NAME) */ -void rei_log(const char* fmt, ...); +/* a line in the log (stderr, which daemon(8) sends to syslog), whole whatever the threads: "name: ..."; rei_log names + the program its file is built for (REI_NAME: "reiwimsy", "tracer"), so that one program that runs both bots says + whose each line is */ +void rei_log_as(const char* name, const char* fmt, ...); +#define rei_log(...) rei_log_as(REI_NAME, __VA_ARGS__) /* seconds since the epoch, settable for the tests */ extern double (*rei_now)(void); diff --git a/rei/src/rest.c b/rei/src/rest.c index 689ab78..56721eb 100644 --- a/rei/src/rest.c +++ b/rei/src/rest.c @@ -13,8 +13,9 @@ static void nap(double seconds) { t.tv_nsec = (long)((seconds - (double)t.tv_sec) * 1e9); nanosleep(&t, NULL); } -void dc_init(Dc* d, RgFetch fetch, void* ctx) { +void dc_init_as(Dc* d, const char* name, RgFetch fetch, void* ctx) { memset(d, 0, sizeof *d); + d->name = name; d->fetch = fetch; d->ctx = ctx; d->sleep = nap; @@ -50,11 +51,11 @@ static int ask(Dc* d, const char* method, const char* path, const char* body, Rg free(url); return out->status; } -static void said_wrong(const char* what, const char* path, const RgAnswer* a) { - rei_log("Discord: %s %s answered %d: %.200s", what, path, a->status, - a->error ? a->error - : a->body ? a->body - : "nothing"); +static void said_wrong(const Dc* d, const char* what, const char* path, const RgAnswer* a) { + rei_log_as(d->name ? d->name : REI_NAME, "Discord: %s %s answered %d: %.200s", what, path, a->status, + a->error ? a->error + : a->body ? a->body + : "nothing"); } /* the body of a message carrying embed: a reply to reply_to when not 0, which pings its author when ping is 1, and no other ping; owned */ @@ -88,7 +89,7 @@ u64 dc_self(Dc* d) { d->self = id_of(me, "id"); cJSON_Delete(me); } else if (!d->self) - said_wrong("GET", "users/@me", &a); + said_wrong(d, "GET", "users/@me", &a); answer_free(&a); return d->self; } @@ -100,7 +101,7 @@ cJSON* dc_emojis(Dc* d) { if (app && ask(d, "GET", path, NULL, &a) == 200 && (got = cJSON_Parse(a.body))) items = cJSON_DetachItemFromObjectCaseSensitive(got, "items"); else if (app) - said_wrong("GET", path, &a); + said_wrong(d, "GET", path, &a); cJSON_Delete(got); answer_free(&a); free(path); @@ -117,14 +118,14 @@ u64 dc_reply(Dc* d, u64 channel, const cJSON* embed, u64 reply_to, int ping) { id = id_of(sent, "id"); cJSON_Delete(sent); } - if (!id) said_wrong("POST", path, &a); + if (!id) said_wrong(d, "POST", path, &a); answer_free(&a); free(body); free(path); return id; } /* 0 for a 2xx, 404 for gone, else the status, -1 for no answer (said in the log) */ -static int done_or_gone(const char* what, const char* path, RgAnswer* a, int status) { +static int done_or_gone(const Dc* d, const char* what, const char* path, RgAnswer* a, int status) { int said; if (status >= 200 && status < 300) said = 0; @@ -132,7 +133,7 @@ static int done_or_gone(const char* what, const char* path, RgAnswer* a, int sta said = 404; else { said = status ? status : -1; - said_wrong(what, path, a); + said_wrong(d, what, path, a); } answer_free(a); return said; @@ -141,7 +142,7 @@ int dc_edit(Dc* d, u64 channel, u64 message, const cJSON* embed) { char *path = rei_format("channels/%llu/messages/%llu", (unsigned long long)channel, (unsigned long long)message), *body = message_of(embed, 0, 0); RgAnswer a = {0}; - int said = done_or_gone("PATCH", path, &a, ask(d, "PATCH", path, body, &a)); + int said = done_or_gone(d, "PATCH", path, &a, ask(d, "PATCH", path, body, &a)); free(body); free(path); return said; @@ -149,7 +150,7 @@ int dc_edit(Dc* d, u64 channel, u64 message, const cJSON* embed) { int dc_remove(Dc* d, u64 channel, u64 message) { char* path = rei_format("channels/%llu/messages/%llu", (unsigned long long)channel, (unsigned long long)message); RgAnswer a = {0}; - int said = done_or_gone("DELETE", path, &a, ask(d, "DELETE", path, NULL, &a)); + int said = done_or_gone(d, "DELETE", path, &a, ask(d, "DELETE", path, NULL, &a)); free(path); return said; } @@ -162,7 +163,7 @@ cJSON* dc_messages(Dc* d, u64 channel, u64 before, int max) { cJSON* list = NULL; if (ask(d, "GET", path, NULL, &a) == 200) list = cJSON_Parse(a.body); if (!cJSON_IsArray(list)) { - said_wrong("GET", path, &a); + said_wrong(d, "GET", path, &a); cJSON_Delete(list); list = NULL; } diff --git a/rei/src/rest.h b/rei/src/rest.h index 930f4f0..93f4fe8 100644 --- a/rei/src/rest.h +++ b/rei/src/rest.h @@ -16,9 +16,11 @@ typedef struct { void* ctx; void (*sleep)(double seconds); /* the wait a 429 asks for */ u64 self; /* the bot's own user id, once dc_self found it */ + const char* name; /* the bot its log lines name: REI_NAME of the file that called dc_init */ } Dc; -void dc_init(Dc* d, RgFetch fetch, void* ctx); +void dc_init_as(Dc* d, const char* name, RgFetch fetch, void* ctx); +#define dc_init(d, fetch, ctx) dc_init_as(d, REI_NAME, fetch, ctx) /* the bot's user id, asked once; 0 when Discord did not say */ u64 dc_self(Dc* d); /* the application's emojis (its bot's id is the application's), Discord's list of them; owned, or NULL, said */ diff --git a/rei/src/util.c b/rei/src/util.c index 425f3d6..53bd766 100644 --- a/rei/src/util.c +++ b/rei/src/util.c @@ -102,14 +102,17 @@ void jset(cJSON* o, const char* key, const char* s) { if (s) cJSON_AddStringToObject(o, key, s); } -void rei_log(const char* fmt, ...) { +void rei_log_as(const char* name, const char* fmt, ...) { va_list ap; - fputs(REI_NAME ": ", stderr); + flockfile(stderr); /* another thread's line never lands inside this one */ + fputs(name, stderr); + fputs(": ", stderr); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fputc('\n', stderr); fflush(stderr); + funlockfile(stderr); } static double wall(void) { return (double)time(NULL); } diff --git a/rei/src/watch.c b/rei/src/watch.c index e2dc1ee..d032394 100644 --- a/rei/src/watch.c +++ b/rei/src/watch.c @@ -13,13 +13,21 @@ #include "rei.h" #include "watch.h" -static struct { - pthread_mutex_t lock; +/* each watched client's: the session's state and its heartbeat interval, which no call of Concord's gives */ +typedef struct { + struct discord* client; + const char* name; long long ready; /* when the session was last seen ready, or the watch started: ms of the monotonic clock */ int seen; /* whether it has been seen ready */ long long interval; /* the heartbeat interval the last HELLO gave, in ms */ +} Watched; + +static struct { + pthread_mutex_t lock; + Watched each[WD_CLIENTS]; + int n; void (*end)(void); -} watch = {PTHREAD_MUTEX_INITIALIZER, 0, 0, WD_INTERVAL, NULL}; +} watch = {PTHREAD_MUTEX_INITIALIZER, {{0}}, 0, NULL}; static long long now_ms(void) { struct timespec t; @@ -27,54 +35,78 @@ static long long now_ms(void) { return (long long)t.tv_sec * 1000 + t.tv_nsec / 1000000; } -/* each second, on the thread of discord_run, which alone changes the session */ +/* each second, on the thread of client's discord_run, which alone changes its session */ static void look(struct discord* client, struct discord_timer* timer) { + int i; (void)timer; pthread_mutex_lock(&watch.lock); - if (client->gw.session->is_ready) { - watch.ready = now_ms(); - watch.seen = 1; + for (i = 0; i < watch.n; ++i) { + Watched* w = &watch.each[i]; + if (w->client != client) continue; + if (client->gw.session->is_ready) { + w->ready = now_ms(); + w->seen = 1; + } + if (client->gw.timer->hbeat_interval > 0) w->interval = client->gw.timer->hbeat_interval; } - if (client->gw.timer->hbeat_interval > 0) watch.interval = client->gw.timer->hbeat_interval; pthread_mutex_unlock(&watch.lock); } -/* a thread of its own: discord_run's may be the one waiting */ +/* a thread of its own: a discord_run's may be the one waiting */ static void* guard(void* unused) { (void)unused; for (;;) { - long long since, interval; - int seen; + int i; sleep(1); - pthread_mutex_lock(&watch.lock); - since = now_ms() - watch.ready; - interval = watch.interval; - seen = watch.seen; - pthread_mutex_unlock(&watch.lock); - if (since > WD_INTERVALS * interval) { - rei_log( - "the gateway stopped: its session was %s %.1f s ago, longer than the %.1f s (%d heartbeat intervals " - "of %.2f s) a connection may take to bring READY or RESUMED", - seen ? "last seen ready" : "never ready since the bot started,", (double)since / 1000, - (double)(WD_INTERVALS * interval) / 1000, WD_INTERVALS, (double)interval / 1000); - watch.end(); + for (i = 0; i < WD_CLIENTS; ++i) { + long long since, interval; + int seen, watched; + const char* name; + pthread_mutex_lock(&watch.lock); + watched = i < watch.n; + since = watched ? now_ms() - watch.each[i].ready : 0; + interval = watched ? watch.each[i].interval : WD_INTERVAL; + seen = watched && watch.each[i].seen; + name = watched ? watch.each[i].name : REI_NAME; + pthread_mutex_unlock(&watch.lock); + if (watched && since > WD_INTERVALS * interval) { + rei_log_as(name, + "the gateway stopped: its session was %s %.1f s ago, longer than the %.1f s (%d heartbeat " + "intervals of %.2f s) a connection may take to bring READY or RESUMED", + seen ? "last seen ready" : "never ready since the bot started,", (double)since / 1000, + (double)(WD_INTERVALS * interval) / 1000, WD_INTERVALS, (double)interval / 1000); + watch.end(); + } } } return NULL; } -void wd_start(struct discord* client, void (*end)(void)) { +void wd_start(struct discord* client, const char* name, void (*end)(void)) { pthread_t thread; - int code; + int code, first; + pthread_mutex_lock(&watch.lock); + if (watch.n == WD_CLIENTS) { + pthread_mutex_unlock(&watch.lock); + rei_log_as(name, "the gateway's watch did not start: %d clients are watched already", WD_CLIENTS); + end(); + return; + } + watch.each[watch.n].client = client; + watch.each[watch.n].name = name; + watch.each[watch.n].ready = now_ms(); + watch.each[watch.n].interval = WD_INTERVAL; + first = !watch.n++; watch.end = end; - watch.ready = now_ms(); + pthread_mutex_unlock(&watch.lock); if (!discord_timer_interval(client, look, NULL, NULL, 1000, 1000, -1)) { - rei_log("the gateway's watch did not start: Concord made no timer for it"); + rei_log_as(name, "the gateway's watch did not start: Concord made no timer for it"); end(); return; } + if (!first) return; /* one guard watches them all */ if ((code = pthread_create(&thread, NULL, guard, NULL))) { - rei_log("the gateway's watch did not start: no thread for it: %s", strerror(code)); + rei_log_as(name, "the gateway's watch did not start: no thread for it: %s", strerror(code)); end(); return; } diff --git a/rei/src/watch.h b/rei/src/watch.h index 8623f07..d546e40 100644 --- a/rei/src/watch.h +++ b/rei/src/watch.h @@ -15,6 +15,10 @@ struct discord; /* Discord's heartbeat interval, in ms, until a HELLO gives one */ #define WD_INTERVAL 41250 -/* watches client's gateway from now on: before discord_run. end ends the bot */ -void wd_start(struct discord* client, void (*end)(void)); +/* the clients one program may watch: ReiWimsy's and Tracer's */ +#define WD_CLIENTS 2 + +/* watches client's gateway from now on: before discord_run. end ends the program; name is the bot's, in the log line + that says why (a program running two bots watches each) */ +void wd_start(struct discord* client, const char* name, void (*end)(void)); #endif diff --git a/tracer/src/main.c b/tracer/src/main.c index d4dd78c..3369552 100644 --- a/tracer/src/main.c +++ b/tracer/src/main.c @@ -527,7 +527,7 @@ int main(int argc, char** argv) { discord_add_intents(client, DISCORD_GATEWAY_GUILDS | DISCORD_GATEWAY_GUILD_MESSAGES | (issues.repo ? DISCORD_GATEWAY_GUILD_MESSAGE_REACTIONS : 0)); discord_set_event_scheduler(client, scheduler); - wd_start(client, end); + wd_start(client, REI_NAME, end); code = discord_run(client); if (code == CCORD_DISCORD_BAD_AUTH) { /* what Concord's discord_run returns for any failure of GET gateway/bot */ char* now = dc_said(&dc, "gateway/bot"); From be91fd857bd0bda59eb596a74459622a9e08bf7f Mon Sep 17 00:00:00 2001 From: AO55 Date: Tue, 17 Nov 2026 13:01:26 -0300 Subject: [PATCH 3/7] [tracer] A worker's environment holds no bot's settings or tokens A worker lost the TRACER_ keys alone: in a program that runs ReiWimsy too, REIWIMSY_TOKEN and GITHUB_TOKEN would reach the code that reads strangers' files. It loses every TRACER_, REIWIMSY_ and GITHUB_ key, and any name that holds TOKEN, KEY or SECRET. Signed-off-by: AO55 --- docs/SECURITY.md | 3 ++- docs/TESTING.md | 15 ++++++++------- scripts/test-mutants.py | 15 ++++++++++++++- tracer/src/worker.c | 15 ++++++++++++++- tracer/src/worker.h | 4 ++-- tracer/tests/test_tracer.c | 18 +++++++++++++++--- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/docs/SECURITY.md b/docs/SECURITY.md index e32d345..c1c2b2c 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -53,7 +53,8 @@ learned ([Forge](./FORGE.md#the-guard)). Tracer reads the files strangers send. Each message's files are read by a worker, the bot run again as `tracer --report` ([Tracer](./TRACER.md#the-worker)): -- its environment without the bot's settings and tokens (every `TRACER_` key taken out); +- its environment without the settings and tokens of either bot the program runs (every `TRACER_`, `REIWIMSY_` and + `GITHUB_` key, and any name that holds `TOKEN`, `KEY` or `SECRET`, taken out); - within 1 GiB of memory, 120 s of processor, 600 s in all and 128 MiB a file, with no core file (a crash's core would hold what a stranger sent), and every descriptor it inherited but the standard three closed (the bot's gateway, its database); diff --git a/docs/TESTING.md b/docs/TESTING.md index 12e95c1..0c7842a 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -95,12 +95,12 @@ temporary folder: anyone else's reaction or another emoji nothing, a refusal said and tried again, the settings and their refusals; - how old a report's build is, through a fake GitHub: behind main, the plurals, a commit asked once an hour, `-dirty` and a commit the repository lacks, no line at main or the newest nightly, GitHub silent or refusing, no token, no git; -- the worker, the test program run as one: a report through it, its environment without the token, one that crashes, - one that never answers, one gone before it reads, one that names a host file, one shut in its folder that tries the - site, the database and /etc/passwd, one that answers without having shut itself in, one that reads the bot's - environment before the bot keeps it from its user's debuggers and not after, the bot's descriptors closed in it, no - worker for a message without files, a Share Log zip whose names are flagged UTF-8 and have accents, read once shut - in; +- the worker, the test program run as one: a report through it, its environment without either bot's settings and + tokens, one that crashes, one that never answers, one gone before it reads, one that names a host file, one shut in + its folder that tries the site, the database and /etc/passwd, one that answers without having shut itself in, one that + reads the bot's environment before the bot keeps it from its user's debuggers and not after, the bot's descriptors + closed in it, no worker for a message without files, a Share Log zip whose names are flagged UTF-8 and have accents, + read once shut in; - the MCP server over that site: its handshake and protocol versions, notifications, broken and unknown requests, each tool's filters, limits and errors, and a query's 16 words at most; - runs of Amberlith (edenprobe before its 1.0.0): its families and which lose their detail, the drivers by the ban @@ -135,7 +135,8 @@ zip's screenshots, a picture never read as a log, the paths outside a home, the an issue may hold of the game: 33 mutants, `test_tracer --files`); what a log holds of a run, in `probe.c` (Amberlith's starting note, a name that only begins like it, libc's detail kept, a worker's answer naming another program: 4 mutants, `test_tracer --probe`); and the worker's locale for a zip's names, made before it shuts itself in, who links a report to an issue, and when a -report's sender is told its fix is out, and an approval of a failure an issue has (5 mutants, the whole suite). A mutant they pass, +report's sender is told its fix is out, an approval of a failure an issue has, and a worker's environment (7 +mutants, the whole suite). A mutant they pass, or one whose text its file no longer has once, fails it. About 30 s. `make -C tracer bench [RUNS=1000] [FOLDER=/tmp/tracer-bench]` (`tracer/tests/bench_probe.c`, built by `make test`, diff --git a/scripts/test-mutants.py b/scripts/test-mutants.py index c4065c6..6bc0e53 100755 --- a/scripts/test-mutants.py +++ b/scripts/test-mutants.py @@ -313,7 +313,8 @@ ), ) -# what only the whole suite holds: the worker, a process of its own shut in its folder (test_worker); who links a +# what only the whole suite holds: the worker, a process of its own shut in its folder, and its environment +# (test_worker); who links a # report to an issue (test_links); when a report's sender is told its fix is out (test_follow); an approval of a # failure an issue has (test_same_failure) WORKER = ( @@ -323,6 +324,18 @@ ' if (i < n) {\n e = same_linked(', ' if (0 && i < n) {\n e = same_linked(', ), + ( + "a worker's environment keeps ReiWimsy's settings", + 'worker.c', + 'static const char* const prefixes[] = {"TRACER_", "REIWIMSY_", "GITHUB_"};', + 'static const char* const prefixes[] = {"TRACER_", "TRACER_", "GITHUB_"};', + ), + ( + "a worker's environment keeps a name that holds a secret", + 'worker.c', + ' for (k = 0; k < sizeof words / sizeof *words; ++k)', + ' for (k = 0; k < 0; ++k)', + ), ( "a sender is told before a nightly has the fix", 'follow.c', diff --git a/tracer/src/worker.c b/tracer/src/worker.c index d5ff4c1..0de2135 100644 --- a/tracer/src/worker.c +++ b/tracer/src/worker.c @@ -34,6 +34,19 @@ #define NAME_MAX_KEPT 200 /* the bytes of a file's name kept from an answer */ #define SKIPPED_MAX 4000 /* the bytes of the lines of the files not taken */ +/* whether a variable, "NAME=value", is a bot's setting or names a secret: never a worker's */ +static int secret(const char* variable) { + static const char* const prefixes[] = {"TRACER_", "REIWIMSY_", "GITHUB_"}; + static const char* const words[] = {"TOKEN", "KEY", "SECRET"}; + size_t n = strcspn(variable, "="), k, at; + for (k = 0; k < sizeof prefixes / sizeof *prefixes; ++k) + if (!strncmp(variable, prefixes[k], strlen(prefixes[k]))) return 1; + for (k = 0; k < sizeof words / sizeof *words; ++k) + for (at = 0; at + strlen(words[k]) <= n; ++at) + if (!strncmp(variable + at, words[k], strlen(words[k]))) return 1; + return 0; +} + char** wk_env(char* const* environ_now) { size_t n = 0, kept = 0, i; char** env; @@ -44,7 +57,7 @@ char** wk_env(char* const* environ_now) { abort(); } for (i = 0; i < n; ++i) - if (strncmp(environ_now[i], "TRACER_", 7)) env[kept++] = environ_now[i]; + if (!secret(environ_now[i])) env[kept++] = environ_now[i]; env[kept] = NULL; return env; } diff --git a/tracer/src/worker.h b/tracer/src/worker.h index caa8f47..787ceff 100644 --- a/tracer/src/worker.h +++ b/tracer/src/worker.h @@ -16,8 +16,8 @@ #define WK_MEMORY (1u << 30) /* its address space at most, but under AddressSanitizer, which maps far more */ #define WK_ANSWER (64 << 20) /* the bytes of its answer at most: the logs' summaries */ -/* the environment a worker gets: environ without the bot's settings and its token (TRACER_*); owned, its strings - environ's */ +/* the environment a worker gets: environ without the settings and tokens of any bot the program runs (TRACER_*, + REIWIMSY_*, GITHUB_*, and any name that holds TOKEN, KEY or SECRET); owned, its strings environ's */ char** wk_env(char* const* environ_now); /* m's files taken into k (its dir made, its base and counts set) by the worker argv names (argv[0] the program, run without a shell), which may take seconds at most: 0, or -1 with why (owned): it could not start, it stopped on a diff --git a/tracer/tests/test_tracer.c b/tracer/tests/test_tracer.c index 9e97e54..fb6012e 100644 --- a/tracer/tests/test_tracer.c +++ b/tracer/tests/test_tracer.c @@ -2517,10 +2517,22 @@ static void test_worker(const char* self_path) { char *t, *p, *why, **env; Taken k; long id; - char* const fake_environ[] = {"HOME=/home/x", "TRACER_TOKEN=secret", "TRACER_URL=https://a", "PATH=/bin", NULL}; - /* its environment: no setting or token of the bot's */ + char* const fake_environ[] = {"HOME=/home/x", + "TRACER_TOKEN=secret", + "TRACER_URL=https://a", + "REIWIMSY_TOKEN=rei", + "REIWIMSY_STATE=/var/db/reiwimsy/state.json", + "GITHUB_TOKEN=gh", + "PATH=/bin", + "AWS_SECRET_ACCESS_KEY=k", + "SSH_AUTH_SOCK_KEYS=x", + "LANG=C.UTF-8", + "TOKENLESS=y", + NULL}; + /* its environment: no setting or token of either bot the program may run, nothing that names a secret */ env = wk_env(fake_environ); - CHECK(env[0] && !strcmp(env[0], "HOME=/home/x") && env[1] && !strcmp(env[1], "PATH=/bin") && !env[2]); + CHECK(env[0] && !strcmp(env[0], "HOME=/home/x") && env[1] && !strcmp(env[1], "PATH=/bin") && env[2] && + !strcmp(env[2], "LANG=C.UTF-8") && !env[3]); free(env); worker_argv[0] = (char*)self_path; worker_argv[2] = NULL; From ae0f73e179b3b4cfdedaeebedf40dc6aa3513d4c Mon Sep 17 00:00:00 2001 From: AO55 Date: Tue, 17 Nov 2026 13:03:30 -0300 Subject: [PATCH 4/7] [rei] One program, two bots: reiwimsy runs Tracer beside ReiWimsy DraVee asked for ReiWimsy and Tracer in one program that answers as two bots. With TRACER_TOKEN in its env file, reiwimsy runs Tracer in its own process: two Concord clients, each with its own token, gateway and name on Discord, Tracer's on a thread of its own, the first gateway that stops ending the program as before. ReiWimsy leaves a message that mentions Tracer to it. Each bot is a bot.c of its own (rb_, tb_); tracer alone stays, its main.c a few lines, and reiwimsy links libtracer.a. Signed-off-by: AO55 --- .github/workflows/test.yml | 1 + rei/Makefile | 30 ++- rei/src/bot.c | 333 +++++++++++++++++++++++ rei/src/bot.h | 23 ++ rei/src/event.c | 9 + rei/src/event.h | 3 + rei/src/main.c | 338 ++++------------------- rei/tests/fake_discord.py | 91 ++++++- rei/tests/test_gateway.sh | 32 ++- rei/tests/test_rei.c | 10 + tracer/.gitignore | 1 + tracer/Makefile | 24 +- tracer/src/bot.c | 539 +++++++++++++++++++++++++++++++++++++ tracer/src/bot.h | 27 ++ tracer/src/main.c | 532 ++---------------------------------- 15 files changed, 1164 insertions(+), 829 deletions(-) create mode 100644 rei/src/bot.c create mode 100644 rei/src/bot.h create mode 100644 tracer/src/bot.c create mode 100644 tracer/src/bot.h diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9515d7e..3a282c8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -59,6 +59,7 @@ jobs: - run: sudo sh rei/tests/test_gateway.sh unready rei/reiwimsy - run: sudo sh rei/tests/test_gateway.sh stalled rei/reiwimsy - run: sudo sh rei/tests/test_gateway.sh fix-cards rei/reiwimsy + - run: sudo sh rei/tests/test_gateway.sh both rei/reiwimsy - run: sudo sh rei/tests/test_gateway.sh tracer tracer/tracer - run: sudo sh rei/tests/test_gateway.sh tracer-all tracer/tracer - run: sudo sh rei/tests/test_gateway.sh tracer-zombie tracer/tracer diff --git a/rei/Makefile b/rei/Makefile index ba9f8b2..ced5c73 100644 --- a/rei/Makefile +++ b/rei/Makefile @@ -1,7 +1,9 @@ # SPDX-License-Identifier: GPL-3.0-or-later # Copyright 2026 the ScarletNX contributors # -# make [CONCORD=prefix] reiwimsy, linked with Concord v3.0.1 as make install PREFIX=prefix put it, and libcurl +# make [CONCORD=prefix] reiwimsy, linked with Concord v3.0.1 as make install PREFIX=prefix put it, libcurl, and +# Tracer (../tracer/libtracer.a, which it runs beside ReiWimsy with TRACER_TOKEN) with its +# SQLite, libarchive, zlib and C++ runtime (CXXRT=-lc++ on FreeBSD) # make test the tests: no Discord, no network, no Concord # make test-gateway reiwimsy (CONCORD as above) against a fake Discord whose gateway dies under it: Linux, as # root or with user namespaces (tests/test_gateway.sh) @@ -13,6 +15,10 @@ CFLAGS ?= -O2 -g CONCORD ?= /usr/local CURL_CFLAGS ?= `pkg-config --cflags libcurl` CURL_LIBS ?= `pkg-config --libs libcurl` +SQLITE_LIBS ?= `pkg-config --libs sqlite3` +ARCHIVE_LIBS ?= -larchive +ZLIB_LIBS ?= -lz +CXXRT ?= -lstdc++ WARN = -std=c99 -Wall -Wextra -Wpedantic -Wshadow -Wstrict-prototypes -Werror DEFS = -D_POSIX_C_SOURCE=200809L OURS = $(WARN) $(DEFS) -Isrc -Ithird_party/cjson @@ -22,9 +28,13 @@ LOGIC = src/util.o src/refs.o src/github.o src/look.o src/link.o src/cards.o src all: reiwimsy -reiwimsy: $(LOGIC) src/http.o src/watch.o src/main.o - $(CC) $(CFLAGS) -o $@ $(LOGIC) src/http.o src/watch.o src/main.o $(CONCORD)/lib/libdiscord.a $(CURL_LIBS) \ - -lpthread -lm +reiwimsy: $(LOGIC) src/http.o src/watch.o src/bot.o src/main.o tracer + $(CC) $(CFLAGS) -o $@ src/main.o src/bot.o ../tracer/libtracer.a $(LOGIC) src/http.o src/watch.o \ + $(CONCORD)/lib/libdiscord.a $(CURL_LIBS) $(SQLITE_LIBS) $(ARCHIVE_LIBS) $(ZLIB_LIBS) $(CXXRT) -lpthread -lm + +# Tracer's bot, as its Makefile builds it +tracer: + cd ../tracer && $(MAKE) libtracer.a CONCORD="$(CONCORD)" CC="$(CC)" CFLAGS="$(CFLAGS)" test: tests/test_rei ./tests/test_rei @@ -35,6 +45,7 @@ test-gateway: reiwimsy sh tests/test_gateway.sh unready ./reiwimsy sh tests/test_gateway.sh stalled ./reiwimsy sh tests/test_gateway.sh fix-cards ./reiwimsy + sh tests/test_gateway.sh both ./reiwimsy tests/test_rei: $(LOGIC) tests/test_rei.c $(CC) $(CFLAGS) $(OURS) -o $@ tests/test_rei.c $(LOGIC) -lpthread -lm @@ -60,8 +71,11 @@ src/event.o: src/event.c src/event.h src/rei.h src/http.o: src/http.c src/http.h src/github.h src/rei.h $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -c -o $@ src/http.c # Concord's headers are not ours: their warnings are theirs -src/main.o: src/main.c src/cards.h src/event.h src/http.h src/link.h src/rest.h src/state.h src/watch.h src/rei.h - $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -isystem $(CONCORD)/include/concord -c -o $@ src/main.c +src/bot.o: src/bot.c src/bot.h src/cards.h src/event.h src/http.h src/link.h src/rest.h src/state.h src/watch.h \ + src/rei.h + $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -isystem $(CONCORD)/include/concord -c -o $@ src/bot.c +src/main.o: src/main.c src/bot.h ../tracer/src/bot.h src/rei.h + $(CC) $(CFLAGS) $(OURS) -I.. $(CURL_CFLAGS) -isystem $(CONCORD)/include/concord -c -o $@ src/main.c src/watch.o: src/watch.c src/watch.h src/rei.h $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -isystem $(CONCORD)/include/concord -c -o $@ src/watch.c # cJSON as its project builds it @@ -69,6 +83,6 @@ third_party/cjson/cJSON.o: third_party/cjson/cJSON.c third_party/cjson/cJSON.h $(CC) $(CFLAGS) -std=c89 -c -o $@ third_party/cjson/cJSON.c clean: - rm -f $(LOGIC) src/http.o src/watch.o src/main.o reiwimsy tests/test_rei + rm -f $(LOGIC) src/http.o src/watch.o src/bot.o src/main.o reiwimsy tests/test_rei -.PHONY: all test test-gateway clean +.PHONY: all test test-gateway clean tracer diff --git a/rei/src/bot.c b/rei/src/bot.c new file mode 100644 index 0000000..6cac319 --- /dev/null +++ b/rei/src/bot.c @@ -0,0 +1,333 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + Copyright 2026 the ScarletNX contributors */ +/* ReiWimsy's bot (bot.h): the replies to messages (link.h) and the cards (cards.h) of a GitHub repository on Discord. + +Concord's gateway brings the events, read here from their JSON (event.h), never decoded by Concord; a person's message +waits for the thread that answers them, one at a time. A thread of its own draws the cards every ROUND seconds and +edits the replies whose items changed every LK_REFRESH. The cards need GITHUB_TOKEN (GitHub allows +60 requests an hour without one), and so does the replies' editing. */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include + +#include /* Concord's */ + +#include "bot.h" +#include "cards.h" +#include "event.h" +#include "http.h" +#include "link.h" +#include "rest.h" +#include "state.h" +#include "watch.h" + +#define ROUND 30 +#define CHANNELS 64 + +static struct { + const char *token, *repo, *nightly, *github, *state; + u64 channels[CHANNELS]; + int nchannels; + u64 prs, issues, nightlies; +} env; +static RhAuth github_auth, discord_auth; +static Rg link_gh, cards_gh; +static Link replies; +static Cards cards; +static Dc dc; +static cJSON* cards_saved; /* the cards as the last round left them, for rei_save */ +static pthread_mutex_t link_lock = PTHREAD_MUTEX_INITIALIZER, save_lock = PTHREAD_MUTEX_INITIALIZER; +static EvQueue waiting; + +static const char* const KNOWN[] = {"REIWIMSY_TOKEN", "REIWIMSY_REPO", "REIWIMSY_NIGHTLY", + "REIWIMSY_CHANNELS", "REIWIMSY_STATE", "REIWIMSY_CARDS_PRS", + "REIWIMSY_CARDS_ISSUES", "REIWIMSY_CARDS_NIGHTLY"}; + +static void (*end)(void); /* the program's end, which rb_settings is given */ +static u64 leave_to; /* the user of another bot the program runs (Tracer), whose messages are its own */ +static void stop(const char* fmt, const char* value) { + fputs("reiwimsy: ", stderr); + fprintf(stderr, fmt, value); + fputc('\n', stderr); + end(); +} +/* name's value; NULL when it is unset or empty */ +static const char* setting(const char* name) { + const char* v = getenv(name); + return v && *v ? v : NULL; +} +static int repo_name(const char* s) { + const char* slash = strchr(s, '/'); + size_t i; + if (!slash || slash == s || !slash[1] || strchr(slash + 1, '/')) return 0; + for (i = 0; s[i]; ++i) + if (!(isalnum((unsigned char)s[i]) || strchr("._-/", s[i]))) return 0; + return 1; +} +static u64 channel_id(const char* name) { + const char* v = getenv(name); + size_t i; + if (!v || !*v) return 0; + for (i = 0; v[i]; ++i) + if (!isdigit((unsigned char)v[i])) { + fprintf(stderr, "reiwimsy: %s=%s is not a channel id (Developer Mode, Copy Channel ID: its digits)\n", name, + v); + exit(1); + } + return strtoull(v, NULL, 10); +} +static void read_env(void) { + extern char** environ; + char** e; + const char* list; + size_t k; + for (e = environ; *e; ++e) { /* a key of ours misspelt would be a setting quietly not taken */ + int known = 0; + if (strncmp(*e, "REIWIMSY_", 9)) continue; + for (k = 0; k < sizeof KNOWN / sizeof *KNOWN; ++k) + known |= !strncmp(*e, KNOWN[k], strlen(KNOWN[k])) && (*e)[strlen(KNOWN[k])] == '='; + if (!known) + stop("%s: no such setting (REIWIMSY_TOKEN, _REPO, _NIGHTLY, _CHANNELS, _STATE, _CARDS_PRS, " + "_CARDS_ISSUES, _CARDS_NIGHTLY)", + *e); + } + env.token = setting("REIWIMSY_TOKEN"); + env.repo = setting("REIWIMSY_REPO"); + if (!env.token || !env.repo) stop("REIWIMSY_TOKEN and REIWIMSY_REPO (OWNER/NAME) are needed%s", ""); + if (!repo_name(env.repo)) stop("REIWIMSY_REPO=%s is not OWNER/NAME", env.repo); + env.nightly = setting("REIWIMSY_NIGHTLY"); + if (env.nightly && !repo_name(env.nightly)) stop("REIWIMSY_NIGHTLY=%s is not OWNER/NAME", env.nightly); + env.state = setting("REIWIMSY_STATE"); + env.github = getenv("GITHUB_TOKEN"); + if (env.github && !*env.github) env.github = NULL; + for (list = setting("REIWIMSY_CHANNELS"); list && *list;) { + size_t n = strcspn(list, ","); + char id[32]; + if (n && n < sizeof id) { + memcpy(id, list, n); + id[n] = 0; + for (k = 0; k < n; ++k) + if (!isdigit((unsigned char)id[k])) stop("REIWIMSY_CHANNELS has %s, which is not a channel id", id); + if (env.nchannels == CHANNELS) stop("REIWIMSY_CHANNELS names more than %s channels", "64"); + env.channels[env.nchannels++] = strtoull(id, NULL, 10); + } + list += n + (list[n] == ','); + } + env.prs = channel_id("REIWIMSY_CARDS_PRS"); + env.issues = channel_id("REIWIMSY_CARDS_ISSUES"); + env.nightlies = channel_id("REIWIMSY_CARDS_NIGHTLY"); + if ((env.prs || env.issues || env.nightlies) && !env.github) + stop("REIWIMSY_CARDS_* need GITHUB_TOKEN: GitHub allows 60 requests an hour without one%s", ""); +} + +static void save_all(void) { + cJSON* state = cJSON_CreateObject(); + pthread_mutex_lock(&save_lock); + pthread_mutex_lock(&link_lock); + cJSON_AddItemToObject(state, "link", lk_save(&replies)); + pthread_mutex_unlock(&link_lock); + if (cards_saved) cJSON_AddItemToObject(state, "cards", cJSON_Duplicate(cards_saved, 1)); + rei_save(env.state, state); + pthread_mutex_unlock(&save_lock); + cJSON_Delete(state); +} + +/* the cards' Discord, through dc */ +static u64 card_post(void* ctx, u64 channel, const cJSON* embed) { return dc_post(ctx, channel, embed, 0); } +static int card_edit(void* ctx, u64 channel, u64 message, const cJSON* embed) { + return dc_edit(ctx, channel, message, embed); +} +static int card_remove(void* ctx, u64 channel, u64 message) { return dc_remove(ctx, channel, message); } +static int card_history(void* ctx, u64 channel, u64 before, u64* ids, int* webhook, int* mine, int max) { + return dc_history(ctx, channel, before, ids, webhook, mine, max); +} +static cJSON* card_messages(void* ctx, u64 channel, u64 before, int max) { + return dc_messages(ctx, channel, before, max); +} +static int reply_edit(void* ctx, u64 channel, u64 message, const cJSON* embed) { + (void)ctx; + return dc_edit(&dc, channel, message, embed); +} + +static void* rounds(void* unused) { + double refreshed = rei_now(); + (void)unused; + for (;;) { + double start = rei_now(); + if (env.prs || env.issues || env.nightlies) { + cJSON* now; + cd_round(&cards); + now = cd_save(&cards); + pthread_mutex_lock(&save_lock); + cJSON_Delete(cards_saved); + cards_saved = now; + pthread_mutex_unlock(&save_lock); + } + if (env.github && rei_now() - refreshed >= LK_REFRESH) { + pthread_mutex_lock(&link_lock); + lk_refresh(&replies, reply_edit, NULL); + pthread_mutex_unlock(&link_lock); + refreshed = rei_now(); + } + save_all(); + if (rei_now() - start < ROUND) sleep((unsigned)(ROUND - (rei_now() - start))); + } + return NULL; +} + +/* each waiting message answered, one at a time: an answer asks GitHub, which takes a while */ +static void* answers(void* unused) { + (void)unused; + for (;;) { + EvMessage m; + int numbers[REI_MOST], n = 0; + cJSON* e; + u64 id; + ev_pop(&waiting, &m); + pthread_mutex_lock(&link_lock); + e = lk_answer(&replies, m.content, numbers, &n); + pthread_mutex_unlock(&link_lock); + if (e && (id = dc_post(&dc, m.channel, e, m.id)) && n) { + pthread_mutex_lock(&link_lock); + lk_keep(&replies, m.channel, id, numbers, n, e); + pthread_mutex_unlock(&link_lock); + save_all(); + } + cJSON_Delete(e); + ev_message_free(&m); + } + return NULL; +} +static void start(void* (*run)(void*), const char* what) { + pthread_t thread; + int code = pthread_create(&thread, NULL, run, NULL); + if (code) stop("the thread of %s", rei_format("%s did not start: %s", what, strerror(code))); + pthread_detach(thread); +} + +/* READY, at each new session: the threads started at the first */ +static void ready(const char* json, size_t size) { + static int started; + char* user = ev_ready_user(json, size); + if (user) + rei_log("up as %s for %s; cards: pull requests %s, issues %s, nightlies %s", user, env.repo, + env.prs ? "on" : "off", env.issues ? "on" : "off", env.nightlies ? "on" : "off"); + else + rei_log("up for %s, its READY naming no user; cards: pull requests %s, issues %s, nightlies %s", env.repo, + env.prs ? "on" : "off", env.issues ? "on" : "off", env.nightlies ? "on" : "off"); + free(user); + if (!env.github) rei_log("no GITHUB_TOKEN: its replies stay as sent"); + if (!started) { + start(rounds, "the cards and the replies' edits"); + start(answers, "the answers"); + started = 1; + } +} +/* MESSAGE_CREATE: a person's message in a channel the replies answer in waits for its answer; a bot's or a webhook's + (a card) never gets one */ +static void take(const char* json, size_t size) { + EvMessage m; + int i, here = !env.nchannels; + if (!ev_message(json, size, &m)) return; + if (leave_to && ev_mentions(json, size, leave_to)) { /* the other bot's: its commands may hold a #N */ + ev_message_free(&m); + return; + } + for (i = 0; i < env.nchannels; ++i) here |= env.channels[i] == m.channel; + if (here && !ev_push(&waiting, &m)) + rei_log("%d messages wait for an answer already: message %llu in channel %llu gets none", EV_WAITING, + (unsigned long long)m.id, (unsigned long long)m.channel); + ev_message_free(&m); +} +/* Concord's gateway thread, before Concord decodes anything: each event the bot reads is read here, from its JSON, and + none is left to Concord, whose decoder of MESSAGE_CREATE crashed the bot (SIGSEGV, SIGBUS) on messages of 2 to 3 + KB. READY's session is Concord's own before this */ +static enum discord_event_scheduler scheduler(struct discord* client, const char data[], size_t size, + enum discord_gateway_events event) { + (void)client; + if (event == DISCORD_EV_READY) ready(data, size); + if (event == DISCORD_EV_MESSAGE_CREATE) take(data, size); + return DISCORD_EVENT_IGNORE; +} + +void rb_settings(void (*program_end)(void)) { + end = program_end; + read_env(); +} + +/* the GitHub and Discord clients, the replies and the cards, made from the settings */ +static void clients_made(void) { + if (env.github) snprintf(github_auth.authorization, sizeof github_auth.authorization, "Bearer %s", env.github); + github_auth.accept = "application/vnd.github+json"; + snprintf(discord_auth.authorization, sizeof discord_auth.authorization, "Bot %s", env.token); + rg_init(&link_gh, env.repo, env.nightly, rh_fetch, &github_auth); + rg_init(&cards_gh, env.repo, env.nightly, rh_fetch, &github_auth); + lk_init(&replies, &link_gh); + dc_init(&dc, rh_fetch, &discord_auth); + { /* the icon beside each download, the application's emojis: a card or a reply drawn without them says so */ + cJSON* emojis = dc_emojis(&dc); + cd_icons(emojis); + cJSON_Delete(emojis); + } + { + CdDiscord cdd; + cdd.ctx = &dc; + cdd.post = card_post; + cdd.edit = card_edit; + cdd.remove = card_remove; + cdd.history = card_history; + cdd.messages = card_messages; + cd_init(&cards, &cards_gh, cdd, env.prs, env.issues, env.nightlies); + } +} + +int rb_fix_cards(int apply) { + u64 self; + clients_made(); /* drawn as the rounds draw, with the icons, and none of the rest: no state, no gateway */ + self = dc_self(&dc); + if (!self) stop("--fix-cards: Discord did not give the bot's own user id (GET users/@me)%s", ""); + return cd_fix(&cards, self, apply) ? 1 : 0; +} + +void rb_leave_to(u64 user) { leave_to = user; } + +struct discord* rb_start(void) { + struct discord* client; + struct discord_config config; + cJSON* saved; + clients_made(); + saved = rei_load(env.state); + lk_load(&replies, cJSON_GetObjectItemCaseSensitive(saved, "link")); + cd_load(&cards, cJSON_GetObjectItemCaseSensitive(saved, "cards")); + cards_saved = cd_save(&cards); + cJSON_Delete(saved); + ev_queue_init(&waiting); + memset(&config, 0, sizeof config); + /* a copy for Concord to own: discord_from_config keeps the pointer it is given, and discord_cleanup frees it */ + config.token = rei_strdup(env.token); + config.log.level = LOGMOD_LEVEL_WARN; /* Concord's traces name every event: only what goes wrong is logged */ + client = discord_from_config(&config); + if (!client) stop("Concord could not start a client with REIWIMSY_TOKEN%s", ""); + discord_add_intents(client, DISCORD_GATEWAY_GUILDS | DISCORD_GATEWAY_GUILD_MESSAGES | + DISCORD_GATEWAY_DIRECT_MESSAGES | DISCORD_GATEWAY_MESSAGE_CONTENT); + discord_set_event_scheduler(client, scheduler); + wd_start(client, REI_NAME, end); + return client; +} + +void rb_stopped(struct discord* client, CCORDcode code) { + if (code == CCORD_DISCORD_BAD_AUTH) { /* what Concord's discord_run returns for any failure of GET gateway/bot */ + char* now = dc_said(&dc, "gateway/bot"); + rei_log("the gateway stopped: Concord's GET gateway/bot failed, which it calls \"%s\" whatever the failure, a " + "timeout too; asked again, Discord answers %s", + discord_strerror(code, client), now); + free(now); + } else + rei_log("the gateway stopped: %s", discord_strerror(code, client)); +} diff --git a/rei/src/bot.h b/rei/src/bot.h new file mode 100644 index 0000000..99db678 --- /dev/null +++ b/rei/src/bot.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + Copyright 2026 the ScarletNX contributors */ +/* ReiWimsy's bot, for the program that runs it (main.c): its settings (the REIWIMSY_ keys and GITHUB_TOKEN of the + environment, the env file), its replies and cards, its Concord client and its threads */ +#ifndef REI_BOT_H +#define REI_BOT_H +#include /* Concord's */ + +#include "rei.h" + +/* the settings read, the program stopped through end (with why on stderr) on one the bot refuses */ +void rb_settings(void (*end)(void)); +/* --fix-cards (cards.h's cd_fix) with these settings, applied or only said: the program's exit status */ +int rb_fix_cards(int apply); +/* the bot made: its clients, what it kept (REIWIMSY_STATE) and its Concord client, which the caller runs + (discord_run); end on what fails */ +struct discord* rb_start(void); +/* user, another bot this program runs (Tracer): a message that mentions it is that bot's, and gets no reply here, as + its commands may hold a #N that is no issue. Before discord_run */ +void rb_leave_to(u64 user); +/* the line that says why its gateway stopped: discord_run's code */ +void rb_stopped(struct discord* client, CCORDcode code); +#endif diff --git a/rei/src/event.c b/rei/src/event.c index 64584cf..21bcc40 100644 --- a/rei/src/event.c +++ b/rei/src/event.c @@ -36,6 +36,15 @@ void ev_message_free(EvMessage* m) { memset(m, 0, sizeof *m); } +int ev_mentions(const char* json, size_t len, u64 user) { + cJSON* d = user ? cJSON_ParseWithLength(json, len) : NULL; + const cJSON* each; + int yes = 0; + cJSON_ArrayForEach(each, cJSON_GetObjectItemCaseSensitive(d, "mentions")) yes |= id_of(each, "id") == user; + cJSON_Delete(d); + return yes; +} + char* ev_ready_user(const char* json, size_t len) { cJSON* d = cJSON_ParseWithLength(json, len); const cJSON* name = jpath(d, "user.username"); diff --git a/rei/src/event.h b/rei/src/event.h index 3f4d194..9c806d6 100644 --- a/rei/src/event.h +++ b/rei/src/event.h @@ -20,6 +20,9 @@ typedef struct { card), and for one that is no JSON object with an author, an id and a channel id */ int ev_message(const char* json, size_t len, EvMessage* m); void ev_message_free(EvMessage* m); +/* whether MESSAGE_CREATE's message mentions user, as its mentions list the users it names (a reply that pings its + author too); 0 for no user */ +int ev_mentions(const char* json, size_t len, u64 user); /* READY's user name, owned, or NULL */ char* ev_ready_user(const char* json, size_t len); diff --git a/rei/src/main.c b/rei/src/main.c index ed905f1..cc28c82 100644 --- a/rei/src/main.c +++ b/rei/src/main.c @@ -1,63 +1,31 @@ /* SPDX-License-Identifier: GPL-3.0-or-later Copyright 2026 the ScarletNX contributors */ -/* ReiWimsy: the replies to messages (link.h) and the cards (cards.h) of a GitHub repository on Discord. +/* reiwimsy: ReiWimsy (bot.h), and Tracer beside it (tracer/src/bot.h) when its env file has TRACER_TOKEN. One process, + two bots: each its own token, gateway and Concord client, so each answers on Discord as itself; a message that + mentions Tracer is Tracer's alone. REIWIMSY_TOKEN=... REIWIMSY_REPO=OWNER/NAME [REIWIMSY_NIGHTLY=OWNER/NAME] [REIWIMSY_CHANNELS=id,id] [GITHUB_TOKEN=...] [REIWIMSY_CARDS_PRS=id] [REIWIMSY_CARDS_ISSUES=id] [REIWIMSY_CARDS_NIGHTLY=id] - [REIWIMSY_STATE=file] reiwimsy [--fix-cards [--apply]] + [REIWIMSY_STATE=file] [TRACER_TOKEN=... TRACER_URL=https://... and Tracer's other keys] reiwimsy + [--fix-cards [--apply]] --fix-cards, with the same settings, looks through the nightly channel for the cards the rounds made before a push had one card of its own, those still saying "Building" (cards.h, cd_fix), says what it would do of each, and exits; ---apply does it. No gateway and no state file: it may run beside the bot. - -Concord's gateway brings the events, read here from their JSON (event.h), never decoded by Concord; a person's message -waits for the thread that answers them, one at a time. A thread of its own draws the cards every ROUND seconds and -edits the replies whose items changed every LK_REFRESH. The cards need GITHUB_TOKEN (GitHub allows -60 requests an hour without one), and so does the replies' editing. */ +--apply does it. No gateway, no state file and no Tracer: it may run beside the bot. "--report" is a Tracer's worker +(the program runs itself again so, for each message with files) */ #define _POSIX_C_SOURCE 200809L -#include #include #include #include #include #include -#include #include -#include /* Concord's */ - -#include "cards.h" -#include "event.h" -#include "http.h" -#include "link.h" -#include "rest.h" -#include "state.h" -#include "watch.h" - -#define ROUND 30 -#define CHANNELS 64 - -static struct { - const char *token, *repo, *nightly, *github, *state; - u64 channels[CHANNELS]; - int nchannels; - u64 prs, issues, nightlies; -} env; -static RhAuth github_auth, discord_auth; -static Rg link_gh, cards_gh; -static Link replies; -static Cards cards; -static Dc dc; -static cJSON* cards_saved; /* the cards as the last round left them, for rei_save */ -static pthread_mutex_t link_lock = PTHREAD_MUTEX_INITIALIZER, save_lock = PTHREAD_MUTEX_INITIALIZER; -static EvQueue waiting; - -static const char* const KNOWN[] = {"REIWIMSY_TOKEN", "REIWIMSY_REPO", "REIWIMSY_NIGHTLY", - "REIWIMSY_CHANNELS", "REIWIMSY_STATE", "REIWIMSY_CARDS_PRS", - "REIWIMSY_CARDS_ISSUES", "REIWIMSY_CARDS_NIGHTLY"}; +#include "bot.h" +#include "tracer/src/bot.h" -/* the bot's end, with 1, from which daemon(8) starts it again. _exit, not exit: the threads of the rounds and the - answers may be inside their requests, which libcurl's cleanup and exit's handlers (OpenSSL's) must not run under */ +/* the program's end, with 1, from which daemon(8) starts it again. _exit, not exit: the bots' threads may be inside + their requests, which libcurl's cleanup and exit's handlers (OpenSSL's) must not run under */ static void end(void) { fflush(NULL); _exit(1); @@ -68,261 +36,61 @@ static void stop(const char* fmt, const char* value) { fputc('\n', stderr); end(); } -/* name's value; NULL when it is unset or empty */ -static const char* setting(const char* name) { - const char* v = getenv(name); - return v && *v ? v : NULL; -} -static int repo_name(const char* s) { - const char* slash = strchr(s, '/'); - size_t i; - if (!slash || slash == s || !slash[1] || strchr(slash + 1, '/')) return 0; - for (i = 0; s[i]; ++i) - if (!(isalnum((unsigned char)s[i]) || strchr("._-/", s[i]))) return 0; - return 1; -} -static u64 channel_id(const char* name) { - const char* v = getenv(name); - size_t i; - if (!v || !*v) return 0; - for (i = 0; v[i]; ++i) - if (!isdigit((unsigned char)v[i])) { - fprintf(stderr, "reiwimsy: %s=%s is not a channel id (Developer Mode, Copy Channel ID: its digits)\n", name, - v); - exit(1); - } - return strtoull(v, NULL, 10); -} -static void read_env(void) { - extern char** environ; - char** e; - const char* list; - size_t k; - for (e = environ; *e; ++e) { /* a key of ours misspelt would be a setting quietly not taken */ - int known = 0; - if (strncmp(*e, "REIWIMSY_", 9)) continue; - for (k = 0; k < sizeof KNOWN / sizeof *KNOWN; ++k) - known |= !strncmp(*e, KNOWN[k], strlen(KNOWN[k])) && (*e)[strlen(KNOWN[k])] == '='; - if (!known) - stop("%s: no such setting (REIWIMSY_TOKEN, _REPO, _NIGHTLY, _CHANNELS, _STATE, _CARDS_PRS, " - "_CARDS_ISSUES, _CARDS_NIGHTLY)", - *e); - } - env.token = setting("REIWIMSY_TOKEN"); - env.repo = setting("REIWIMSY_REPO"); - if (!env.token || !env.repo) stop("REIWIMSY_TOKEN and REIWIMSY_REPO (OWNER/NAME) are needed%s", ""); - if (!repo_name(env.repo)) stop("REIWIMSY_REPO=%s is not OWNER/NAME", env.repo); - env.nightly = setting("REIWIMSY_NIGHTLY"); - if (env.nightly && !repo_name(env.nightly)) stop("REIWIMSY_NIGHTLY=%s is not OWNER/NAME", env.nightly); - env.state = setting("REIWIMSY_STATE"); - env.github = getenv("GITHUB_TOKEN"); - if (env.github && !*env.github) env.github = NULL; - for (list = setting("REIWIMSY_CHANNELS"); list && *list;) { - size_t n = strcspn(list, ","); - char id[32]; - if (n && n < sizeof id) { - memcpy(id, list, n); - id[n] = 0; - for (k = 0; k < n; ++k) - if (!isdigit((unsigned char)id[k])) stop("REIWIMSY_CHANNELS has %s, which is not a channel id", id); - if (env.nchannels == CHANNELS) stop("REIWIMSY_CHANNELS names more than %s channels", "64"); - env.channels[env.nchannels++] = strtoull(id, NULL, 10); - } - list += n + (list[n] == ','); - } - env.prs = channel_id("REIWIMSY_CARDS_PRS"); - env.issues = channel_id("REIWIMSY_CARDS_ISSUES"); - env.nightlies = channel_id("REIWIMSY_CARDS_NIGHTLY"); - if ((env.prs || env.issues || env.nightlies) && !env.github) - stop("REIWIMSY_CARDS_* need GITHUB_TOKEN: GitHub allows 60 requests an hour without one%s", ""); -} - -static void save_all(void) { - cJSON* state = cJSON_CreateObject(); - pthread_mutex_lock(&save_lock); - pthread_mutex_lock(&link_lock); - cJSON_AddItemToObject(state, "link", lk_save(&replies)); - pthread_mutex_unlock(&link_lock); - if (cards_saved) cJSON_AddItemToObject(state, "cards", cJSON_Duplicate(cards_saved, 1)); - rei_save(env.state, state); - pthread_mutex_unlock(&save_lock); - cJSON_Delete(state); -} - -/* the cards' Discord, through dc */ -static u64 card_post(void* ctx, u64 channel, const cJSON* embed) { return dc_post(ctx, channel, embed, 0); } -static int card_edit(void* ctx, u64 channel, u64 message, const cJSON* embed) { - return dc_edit(ctx, channel, message, embed); -} -static int card_remove(void* ctx, u64 channel, u64 message) { return dc_remove(ctx, channel, message); } -static int card_history(void* ctx, u64 channel, u64 before, u64* ids, int* webhook, int* mine, int max) { - return dc_history(ctx, channel, before, ids, webhook, mine, max); -} -static cJSON* card_messages(void* ctx, u64 channel, u64 before, int max) { - return dc_messages(ctx, channel, before, max); -} -static int reply_edit(void* ctx, u64 channel, u64 message, const cJSON* embed) { - (void)ctx; - return dc_edit(&dc, channel, message, embed); -} -static void* rounds(void* unused) { - double refreshed = rei_now(); - (void)unused; - for (;;) { - double start = rei_now(); - if (env.prs || env.issues || env.nightlies) { - cJSON* now; - cd_round(&cards); - now = cd_save(&cards); - pthread_mutex_lock(&save_lock); - cJSON_Delete(cards_saved); - cards_saved = now; - pthread_mutex_unlock(&save_lock); - } - if (env.github && rei_now() - refreshed >= LK_REFRESH) { - pthread_mutex_lock(&link_lock); - lk_refresh(&replies, reply_edit, NULL); - pthread_mutex_unlock(&link_lock); - refreshed = rei_now(); - } - save_all(); - if (rei_now() - start < ROUND) sleep((unsigned)(ROUND - (rei_now() - start))); - } +/* Tracer's gateway, on a thread of its own: the program ends when it stops, as when ReiWimsy's does */ +static void* tracer_gateway(void* client) { + tb_stopped(client, discord_run(client)); + end(); return NULL; } -/* each waiting message answered, one at a time: an answer asks GitHub, which takes a while */ -static void* answers(void* unused) { - (void)unused; - for (;;) { - EvMessage m; - int numbers[REI_MOST], n = 0; - cJSON* e; - u64 id; - ev_pop(&waiting, &m); - pthread_mutex_lock(&link_lock); - e = lk_answer(&replies, m.content, numbers, &n); - pthread_mutex_unlock(&link_lock); - if (e && (id = dc_post(&dc, m.channel, e, m.id)) && n) { - pthread_mutex_lock(&link_lock); - lk_keep(&replies, m.channel, id, numbers, n, e); - pthread_mutex_unlock(&link_lock); - save_all(); - } - cJSON_Delete(e); - ev_message_free(&m); - } +/* the first TRACER_ key of the environment, which Tracer reads only with TRACER_TOKEN: NULL for none */ +static const char* tracer_key(void) { + extern char** environ; + char** e; + for (e = environ; *e; ++e) + if (!strncmp(*e, "TRACER_", 7)) return *e; return NULL; } -static void start(void* (*run)(void*), const char* what) { - pthread_t thread; - int code = pthread_create(&thread, NULL, run, NULL); - if (code) stop("the thread of %s", rei_format("%s did not start: %s", what, strerror(code))); - pthread_detach(thread); -} - -/* READY, at each new session: the threads started at the first */ -static void ready(const char* json, size_t size) { - static int started; - char* user = ev_ready_user(json, size); - if (user) - rei_log("up as %s for %s; cards: pull requests %s, issues %s, nightlies %s", user, env.repo, - env.prs ? "on" : "off", env.issues ? "on" : "off", env.nightlies ? "on" : "off"); - else - rei_log("up for %s, its READY naming no user; cards: pull requests %s, issues %s, nightlies %s", env.repo, - env.prs ? "on" : "off", env.issues ? "on" : "off", env.nightlies ? "on" : "off"); - free(user); - if (!env.github) rei_log("no GITHUB_TOKEN: its replies stay as sent"); - if (!started) { - start(rounds, "the cards and the replies' edits"); - start(answers, "the answers"); - started = 1; - } -} -/* MESSAGE_CREATE: a person's message in a channel the replies answer in waits for its answer; a bot's or a webhook's - (a card) never gets one */ -static void take(const char* json, size_t size) { - EvMessage m; - int i, here = !env.nchannels; - if (!ev_message(json, size, &m)) return; - for (i = 0; i < env.nchannels; ++i) here |= env.channels[i] == m.channel; - if (here && !ev_push(&waiting, &m)) - rei_log("%d messages wait for an answer already: message %llu in channel %llu gets none", EV_WAITING, - (unsigned long long)m.id, (unsigned long long)m.channel); - ev_message_free(&m); -} -/* Concord's gateway thread, before Concord decodes anything: each event the bot reads is read here, from its JSON, and - none is left to Concord, whose decoder of MESSAGE_CREATE crashed the bot (SIGSEGV, SIGBUS) on messages of 2 to 3 - KB. READY's session is Concord's own before this */ -static enum discord_event_scheduler scheduler(struct discord* client, const char data[], size_t size, - enum discord_gateway_events event) { - (void)client; - if (event == DISCORD_EV_READY) ready(data, size); - if (event == DISCORD_EV_MESSAGE_CREATE) take(data, size); - return DISCORD_EVENT_IGNORE; -} int main(int argc, char** argv) { - struct discord* client; - struct discord_config config; - CdDiscord cdd; - cJSON* saved; - CCORDcode code; int fix = argc > 1 && !strcmp(argv[1], "--fix-cards"), apply = fix && argc > 2 && !strcmp(argv[2], "--apply"); + const char* token = getenv("TRACER_TOKEN"); + int tracer = token && *token && !fix; + struct discord *rei, *tr = NULL; + if (argc == 2 && !strcmp(argv[1], "--report")) return tb_worker(); /* a worker: no settings, no token */ if (1 + fix + apply < argc) stop("%s: not an option of reiwimsy, which takes none, or --fix-cards [--apply]", argv[1 + fix + apply]); - read_env(); - curl_global_init(CURL_GLOBAL_DEFAULT); - if (env.github) snprintf(github_auth.authorization, sizeof github_auth.authorization, "Bearer %s", env.github); - github_auth.accept = "application/vnd.github+json"; - snprintf(discord_auth.authorization, sizeof discord_auth.authorization, "Bot %s", env.token); - rg_init(&link_gh, env.repo, env.nightly, rh_fetch, &github_auth); - rg_init(&cards_gh, env.repo, env.nightly, rh_fetch, &github_auth); - lk_init(&replies, &link_gh); - dc_init(&dc, rh_fetch, &discord_auth); - { /* the icon beside each download, the application's emojis: a card or a reply drawn without them says so */ - cJSON* emojis = dc_emojis(&dc); - cd_icons(emojis); - cJSON_Delete(emojis); + if (!tracer && !fix && tracer_key()) /* a Tracer's key with no Tracer would be a setting quietly not taken */ + stop("%s: Tracer's keys are read only with TRACER_TOKEN, and it is not set", tracer_key()); + if (tracer) { /* before anything else: its workers, which read what strangers send, never read the tokens */ + char* why; + if (tb_untraceable(&why)) stop("the tokens could not be kept from Tracer's workers: %s", why); } - cdd.ctx = &dc; - cdd.post = card_post; - cdd.edit = card_edit; - cdd.remove = card_remove; - cdd.history = card_history; - cdd.messages = card_messages; - cd_init(&cards, &cards_gh, cdd, env.prs, env.issues, env.nightlies); - if (fix) { /* drawn as the rounds draw, with the icons above, and none of the rest: no state, no gateway */ - u64 self = dc_self(&dc); - if (!self) stop("--fix-cards: Discord did not give the bot's own user id (GET users/@me)%s", ""); - return cd_fix(&cards, self, apply) ? 1 : 0; + rb_settings(end); + if (tracer) tb_settings(end); + curl_global_init(CURL_GLOBAL_DEFAULT); + if (fix) return rb_fix_cards(apply); + rei = rb_start(); + if (!tracer) + rei_log("Tracer: none, as TRACER_TOKEN is not set"); + else { + pthread_t thread; + u64 user; + int code; + tr = tb_start(argv[0]); + if (!(user = tb_user())) + stop("Tracer's user is not known (GET users/@me with TRACER_TOKEN did not say it), so ReiWimsy could not " + "leave the messages that mention it to it%s", + ""); + rb_leave_to(user); + rei_log("Tracer: beside ReiWimsy, with its own token; the messages that mention it (user %llu) are its own", + (unsigned long long)user); + if ((code = pthread_create(&thread, NULL, tracer_gateway, tr))) + stop("Tracer's gateway did not start: no thread for it: %s", strerror(code)); + pthread_detach(thread); } - saved = rei_load(env.state); - lk_load(&replies, cJSON_GetObjectItemCaseSensitive(saved, "link")); - cd_load(&cards, cJSON_GetObjectItemCaseSensitive(saved, "cards")); - cards_saved = cd_save(&cards); - cJSON_Delete(saved); - ev_queue_init(&waiting); - memset(&config, 0, sizeof config); - /* a copy for Concord to own: discord_from_config keeps the pointer it is given, and discord_cleanup frees it */ - config.token = rei_strdup(env.token); - config.log.level = LOGMOD_LEVEL_WARN; /* Concord's traces name every event: only what goes wrong is logged */ - client = discord_from_config(&config); - if (!client) stop("Concord could not start a client with REIWIMSY_TOKEN%s", ""); - discord_add_intents(client, DISCORD_GATEWAY_GUILDS | DISCORD_GATEWAY_GUILD_MESSAGES | - DISCORD_GATEWAY_DIRECT_MESSAGES | DISCORD_GATEWAY_MESSAGE_CONTENT); - discord_set_event_scheduler(client, scheduler); - wd_start(client, REI_NAME, end); - code = discord_run(client); - if (code == CCORD_DISCORD_BAD_AUTH) { /* what Concord's discord_run returns for any failure of GET gateway/bot */ - char* now = dc_said(&dc, "gateway/bot"); - rei_log("the gateway stopped: Concord's GET gateway/bot failed, which it calls \"%s\" whatever the failure, a " - "timeout too; asked again, Discord answers %s", - discord_strerror(code, client), now); - free(now); - } else - rei_log("the gateway stopped: %s", discord_strerror(code, client)); + rb_stopped(rei, discord_run(rei)); end(); return 1; } diff --git a/rei/tests/fake_discord.py b/rei/tests/fake_discord.py index 280ebd3..f0ea669 100644 --- a/rei/tests/fake_discord.py +++ b/rei/tests/fake_discord.py @@ -26,6 +26,12 @@ has a ScarletNX log and no mention (the REST API gives it; the CDN, its log); once the bot has answered it, another person's reply to the same message. +both, for reiwimsy running Tracer beside ReiWimsy: two tokens, each its own user (ReiWimsy's 100, Tracer's 200) in +users/@me and in its gateway's READY. Once both are ready, each gateway brings the same two messages, as Discord sends +a message to every bot that reads the channel: 80, "<@200> delete #12", which mentions Tracer, and 81, "see #12". Each +answer is a line naming the bot whose token posted it. GitHub answers issue 12. Two seconds after Tracer has answered +80 and ReiWimsy 81 (or 30 s), the drop. + cards, for reiwimsy --fix-cards: the nightly channel 5 holds two of the bot's cards saying "Building" from five hours ago, a commit inside a push (B) and that push's head (C), whose build failed; one of its cards saying something else, and someone else's. GitHub answers what drawing C asks; Discord's channel listing, PATCH and DELETE work on that @@ -42,11 +48,16 @@ import time CERT, KEY, DROP_AFTER = sys.argv[1], sys.argv[2], sys.argv[3] -if DROP_AFTER not in ("github", "ready", "replies", "cards", "zombie", "unready", "stalled"): - sys.exit( - f"fake_discord.py: DROP_AFTER is {DROP_AFTER!r}, not github, ready, replies, cards, zombie, unready or stalled" - ) +MODES = ("github", "ready", "replies", "cards", "zombie", "unready", "stalled", "both") +if DROP_AFTER not in MODES: + sys.exit(f"fake_discord.py: DROP_AFTER is {DROP_AFTER!r}, not one of {', '.join(MODES)}") ME = {"id": "100", "username": "FakeBot", "discriminator": "0", "bot": True} +# both: Tracer's token (test_gateway.sh's) and user, beside ReiWimsy's (any other token, ME) +TRACER_TOKEN = "TracerTracerTracerTracer.Trace0.TracerTracerTracerTracerTr" +TRACER_ME = {"id": "200", "username": "Tracer", "discriminator": "0", "bot": True} +ready_bots = {} # both: the send of each bot's gateway connection, by its user id, once READY went +both_started = False + cue = asyncio.Event() # what the drop waits for posted = asyncio.Queue() # each message a bot posts, its JSON broken = False @@ -216,7 +227,7 @@ async def frame_of(reader): async def gateway(reader, writer, headers): - global connections + global connections, both_started connections += 1 n = connections acks = 0 @@ -251,7 +262,7 @@ async def replies(): say(f"gateway: MESSAGE_CREATE {message}, a reply by {author} to message 50") try: # the bot answers in order: an answer to 70, when it gives one, comes first while True: - said = await asyncio.wait_for(posted.get(), 30) + said = (await asyncio.wait_for(posted.get(), 30))["body"] say(f"discord: answered {json.dumps(said)}") if said.get("message_reference", {}).get("message_id") == message: break @@ -260,6 +271,41 @@ async def replies(): break cue.set() + async def both(): + """the two messages through each bot's gateway, then each answer, named by the bot that posted it""" + person = {"id": "8", "username": "someone8"} + mention = { + "id": "80", + "channel_id": "6", + "guild_id": "5", + "type": 0, + "author": person, + "member": {"roles": []}, + "content": "<@200> delete #12", + "mentions": [TRACER_ME], + "attachments": [], + } + plain = dict(mention, id="81", content="see #12", mentions=[]) + for user, bot_send in ready_bots.items(): + for seq, message in enumerate((mention, plain), start=2): + bot_send({"op": 0, "t": "MESSAGE_CREATE", "s": seq, "d": message}) + say(f"gateway: MESSAGE_CREATE 80 and 81 to user {user}") + wanted = {("Tracer", "80"), ("ReiWimsy", "81")} + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + try: + got = await asyncio.wait_for(posted.get(), max(0.1, deadline - time.monotonic())) + except TimeoutError: + break + to = got["body"].get("message_reference", {}).get("message_id") + say(f"discord: answered as {got['by']} to {to}: {json.dumps(got['body'])}") + wanted.discard((got["by"], to)) + if not wanted: + deadline = min(deadline, time.monotonic() + 2) # what else comes within 2 s + if wanted: + say(f"discord: no answer in 30 s from {sorted(wanted)}") + cue.set() + async def ack(): nonlocal acks await asyncio.sleep(0.3) @@ -293,11 +339,17 @@ async def ack(): say(f"gateway: connection {n}: IDENTIFY, closed with no CLOSE frame") break if op == 2: - ready = {"v": 10, "user": ME, "guilds": [], "session_id": "fake", "shard": [0, 1]} + user = TRACER_ME if json.loads(data).get("d", {}).get("token") == TRACER_TOKEN else ME + ready = {"v": 10, "user": user, "guilds": [], "session_id": "fake", "shard": [0, 1]} ready["resume_gateway_url"] = "wss://gateway.discord.gg" - ready["application"] = {"id": ME["id"], "flags": 0} + ready["application"] = {"id": user["id"], "flags": 0} send({"op": 0, "t": "READY", "s": 1, "d": ready}) - say(f"gateway: connection {n}: READY" if beating else "gateway: READY") + say(f"gateway: connection {n}: READY" if beating else f"gateway: READY as user {user['id']}") + if DROP_AFTER == "both": + ready_bots[user["id"]] = send + if len(ready_bots) == 2 and not both_started: + both_started = True + replying = asyncio.ensure_future(both()) if DROP_AFTER == "zombie" and n == 1: start = json.dumps({"op": 0, "t": "MESSAGE_CREATE", "s": 2, "d": {"content": "x" * 60000}}) writer.write(frame(1, start[:1000].encode(), 60000)) @@ -336,6 +388,22 @@ async def serve(reader, writer): answer(writer, {"message": "Not Found"}, "404 Not Found") elif DROP_AFTER == "cards" and cards_discord(writer, method, target, body): pass + elif DROP_AFTER == "both" and host == "api.github.com": + if method == "GET" and target == "/repos/owner/repo/issues/12": + say(f"github: {method} {target}") + answer( + writer, + { + "number": 12, + "title": "An issue", + "state": "open", + "labels": [], + "html_url": "https://github.com/owner/repo/issues/12", + }, + ) + else: + say(f"github: {method} {target}: no answer here, 404") + answer(writer, {"message": "Not Found"}, "404 Not Found") elif host == "api.github.com": say(f"github: {method} {target} held") if DROP_AFTER == "github": @@ -368,14 +436,15 @@ async def serve(reader, writer): say(f"cdn: {method} {target}") answer(writer, LOG if target == LOG_PATH else "", kind="text/plain") elif target.startswith("/api/v10/users/@me"): - answer(writer, ME) + answer(writer, TRACER_ME if headers.get("authorization") == "Bot " + TRACER_TOKEN else ME) elif target.startswith("/api/v10/applications/@me"): answer(writer, {"id": ME["id"], "flags": 1 << 19}) # the Message Content intent, limited elif method == "GET" and target == "/api/v10/channels/6/messages/50": say(f"discord: {method} {target}") answer(writer, LOGGED) elif method == "POST" and target in ("/api/v10/channels/6/messages", "/api/v10/channels/7/messages"): - posted.put_nowait(json.loads(body)) + by = "Tracer" if headers.get("authorization") == "Bot " + TRACER_TOKEN else "ReiWimsy" + posted.put_nowait({"by": by, "body": json.loads(body)}) answer(writer, {"id": str(900 + posted.qsize())}) elif "/emojis" in target: answer(writer, {"items": []}) diff --git a/rei/tests/test_gateway.sh b/rei/tests/test_gateway.sh index 9d6992a..629aa03 100644 --- a/rei/tests/test_gateway.sh +++ b/rei/tests/test_gateway.sh @@ -16,11 +16,13 @@ # (fake_discord.py's cards): the dry run changes nothing, --apply deletes the one of a commit inside a push and redraws # the push's head with its failed build, and a second --apply finds nothing to do. Tracer answers only in its channel 6 # there, and a mention in channel 7 goes unanswered; tracer-all is the same with TRACER_CHANNELS=all, which answers -# that one too. +# that one too. both: reiwimsy with TRACER_TOKEN, two bots in one process (fake_discord.py's both): each answers with +# its own token, Tracer "@Tracer delete #12" and ReiWimsy "see #12", and ReiWimsy leaves the first to Tracer; the +# gateways then close under both, and the program ends with 1. # # sh rei/tests/test_gateway.sh PROGRAM BINARY # -# PROGRAM: reiwimsy, tracer, tracer-all, zombie, tracer-zombie, unready, tracer-unready, stalled or fix-cards. +# PROGRAM: reiwimsy, tracer, tracer-all, zombie, tracer-zombie, unready, tracer-unready, stalled, fix-cards or both. # # Linux, no network: the bot runs in a mount and network namespace of its own, and in a user namespace too when not run # by root (a machine that keeps those from users runs the test as root), whose /etc/hosts sends Discord and GitHub to @@ -37,9 +39,10 @@ unready) drop_after=unready bot=reiwimsy ;; stalled) drop_after=stalled bot=reiwimsy ;; tracer-unready) drop_after=unready bot=tracer ;; fix-cards) drop_after=cards bot=reiwimsy ;; +both) drop_after=both bot='\(reiwimsy\|tracer\)' ;; *) echo "usage: sh test_gateway.sh PROGRAM BINARY, PROGRAM one of reiwimsy, tracer, tracer-all, zombie," \ - "tracer-zombie, unready, tracer-unready, stalled, fix-cards (program \"$program\")" >&2 + "tracer-zombie, unready, tracer-unready, stalled, fix-cards, both (program \"$program\")" >&2 exit 2 ;; esac @@ -64,8 +67,9 @@ printf 'subjectAltName=DNS:discord.com,DNS:gateway.discord.gg,DNS:cdn.discordapp openssl x509 -req -in "$tmp/leaf.csr" -CA "$tmp/ca.pem" -CAkey "$tmp/ca.key" -CAcreateserial -days 2 \ -extfile "$tmp/san.ext" -out "$tmp/leaf.pem" 2> /dev/null printf '127.0.0.1 localhost discord.com gateway.discord.gg cdn.discordapp.com api.github.com\n' > "$tmp/hosts" -# a token of Discord's form, which Concord checks before anything +# a token of Discord's form, which Concord checks before anything; Tracer's beside ReiWimsy's (fake_discord.py's) token=FakeFakeFakeFakeFakeFake.Fake00.FakeFakeFakeFakeFakeFakeFake +tracer_token=TracerTracerTracerTracer.Trace0.TracerTracerTracerTracerTr mkdir "$tmp/site" if [ "$program" = reiwimsy ]; then set -- REIWIMSY_TOKEN=$token REIWIMSY_REPO=owner/repo GITHUB_TOKEN=fake REIWIMSY_CARDS_NIGHTLY=1 \ @@ -74,6 +78,10 @@ elif [ "$program" = zombie ] || [ "$program" = unready ] || [ "$program" = stall set -- REIWIMSY_TOKEN=$token REIWIMSY_REPO=owner/repo REIWIMSY_STATE="$tmp/state.json" elif [ "$program" = fix-cards ]; then set -- REIWIMSY_TOKEN=$token REIWIMSY_REPO=owner/repo GITHUB_TOKEN=fake REIWIMSY_CARDS_NIGHTLY=5 +elif [ "$program" = both ]; then + set -- REIWIMSY_TOKEN=$token REIWIMSY_REPO=owner/repo REIWIMSY_STATE="$tmp/state.json" \ + TRACER_TOKEN=$tracer_token TRACER_URL=http://127.0.0.1:8 TRACER_DB="$tmp/tracer.db" TRACER_SITE="$tmp/site" \ + TRACER_ASSETS="$top/tracer/site" TRACER_CHANNELS=6 else set -- TRACER_TOKEN=$token TRACER_URL=http://127.0.0.1:8 TRACER_DB="$tmp/tracer.db" TRACER_SITE="$tmp/site" \ TRACER_ASSETS="$top/tracer/site" TRACER_CHANNELS=$([ "$program" = tracer-all ] && echo all || echo 6) @@ -194,6 +202,21 @@ elif ! grep -q 'gateway: closed with 1001' "$tmp/fake.log"; then failed="the fake gateway never closed under it" elif ! grep -q "$said" "$tmp/bot.log"; then failed="it never said its GET gateway/bot failed and what Discord answers it now" +elif [ "$drop_after" = both ]; then + if ! grep -q '^reiwimsy: Tracer: beside ReiWimsy, with its own token; .* (user 200)' "$tmp/bot.log"; then + failed="it never said Tracer runs beside ReiWimsy as user 200" + elif ! grep -q '^gateway: READY as user 100$' "$tmp/fake.log" || + ! grep -q '^gateway: READY as user 200$' "$tmp/fake.log"; then + failed="the two gateways did not identify with the two tokens" + elif ! grep -q '^discord: answered as Tracer to 80: .*There is no report #12' "$tmp/fake.log"; then + failed="Tracer did not answer \"@Tracer delete #12\" with its own token" + elif ! grep -q '^discord: answered as ReiWimsy to 81: .*An issue' "$tmp/fake.log"; then + failed="ReiWimsy did not answer \"see #12\" with its own token" + elif grep -q '^discord: answered as ReiWimsy to 80' "$tmp/fake.log"; then + failed="ReiWimsy answered the message that mentions Tracer" + elif grep -q '^discord: answered as Tracer to 81' "$tmp/fake.log"; then + failed="Tracer answered the message that does not mention it" + fi elif [ "$drop_after" = replies ]; then # the first reply: a report of message 50, its log downloaded and kept with its home folder as ~; the second: told # that message is that report already. The mention in channel 7: its help, with all; nothing, with channel 6 alone @@ -224,5 +247,6 @@ fi [ "$program" = tracer ] && echo "tracer: the mention in channel 7, which TRACER_CHANNELS=6 leaves out, went unanswered" [ "$program" = tracer-all ] && echo "tracer-all: TRACER_CHANNELS=all answered the mention in channel 7 with its help" [ "$drop_after" = zombie ] && echo "$program: left the connection stalled mid-frame, identified on the next, kept it" +[ "$program" = both ] && echo "both: Tracer answered \"@Tracer delete #12\" and ReiWimsy \"see #12\", each as itself" { [ "$drop_after" = unready ] || [ "$drop_after" = stalled ]; } && said=$watched echo "$program: ended with 1 when its gateway stopped: $(grep "$said" "$tmp/bot.log")" diff --git a/rei/tests/test_rei.c b/rei/tests/test_rei.c index 4030e18..cbebc34 100644 --- a/rei/tests/test_rei.c +++ b/rei/tests/test_rei.c @@ -1413,6 +1413,16 @@ static void event_tests(void) { SAME(m.content, "he said \"#160\" \\ caf\xc3\xa9 \xf0\x9f\x98\x80\n"); ev_message_free(&m); CHECK(!m.content && !m.id); + /* whom it mentions: the users its mentions list, not a name in its text nor a role */ + { + static const char mention[] = + "{\"id\":\"7\",\"channel_id\":\"8\",\"content\":\"<@200> delete #12 <@&300>\",\"author\":{\"id\":\"1\"}," + "\"mentions\":[{\"id\":\"200\",\"username\":\"Tracer\",\"bot\":true}],\"mention_roles\":[\"300\"]}"; + CHECK(ev_mentions(mention, strlen(mention), 200)); + CHECK(!ev_mentions(mention, strlen(mention), 300) && !ev_mentions(mention, strlen(mention), 1)); + CHECK(!ev_mentions(mention, strlen(mention), 0)); + CHECK(!ev_mentions("{\"id\":\"7\",\"content\":\"<@200>\"}", 29, 200)); /* no list: none */ + } /* a reply: its own author counts, not the one it answers */ CHECK(message( "{\"id\":\"2\",\"channel_id\":\"3\",\"content\":\"#5\",\"author\":{\"id\":\"1\"},\"referenced_message\":" diff --git a/tracer/.gitignore b/tracer/.gitignore index 96f2c23..3eda584 100644 --- a/tracer/.gitignore +++ b/tracer/.gitignore @@ -3,3 +3,4 @@ /tests/test_tracer /tests/bench_probe /tracer-mcp +/libtracer.a diff --git a/tracer/Makefile b/tracer/Makefile index b0249de..a20483a 100644 --- a/tracer/Makefile +++ b/tracer/Makefile @@ -3,6 +3,8 @@ # # make [CONCORD=prefix] tracer, linked with Concord v3.0.1 as make install PREFIX=prefix put it, libcurl, SQLite # and libarchive; and tracer-mcp, its MCP server, with libmicrohttpd +# make libtracer.a [CONCORD=prefix] +# the bot alone, without ReiWimsy's pieces, which reiwimsy links to run it beside its own # make test the tests: no Discord, no network, no Concord, no libcurl # make bench [RUNS=1000] [FOLDER=/tmp/tracer-bench] # what RUNS runs of edenprobe cost: the database's size, the Probe pages', and the time they @@ -40,10 +42,18 @@ LOGIC = src/message.o src/files.o src/probe.o src/rules.o src/store.o src/site.o all: tracer tracer-mcp -tracer: $(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/main.o - $(CC) $(CFLAGS) -o $@ $(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/main.o \ +tracer: $(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/bot.o src/main.o + $(CC) $(CFLAGS) -o $@ $(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/bot.o src/main.o \ $(CONCORD)/lib/libdiscord.a $(CURL_LIBS) $(LIBS) +# the bot without ReiWimsy's pieces and cJSON, for reiwimsy, which runs it beside its own (../rei/Makefile) +TRACER_OBJS = src/message.o src/files.o src/probe.o src/rules.o src/store.o src/site.o src/report.o src/titles.o \ + src/channels.o src/worker.o src/issues.o src/age.o src/follow.o src/fetch.o src/bot.o third_party/logscan.o \ + third_party/demangle.o +libtracer.a: $(TRACER_OBJS) + rm -f $@ + ar rcs $@ $(TRACER_OBJS) + tracer-mcp: src/mcp.o src/mcp_main.o src/rei_util.o third_party/cjson.o $(CC) $(CFLAGS) -o $@ src/mcp.o src/mcp_main.o src/rei_util.o third_party/cjson.o $(MHD_LIBS) -lm @@ -102,8 +112,10 @@ src/mcp_main.o: src/mcp_main.c src/mcp.h src/tracer.h src/fetch.o: src/fetch.c src/fetch.h src/tracer.h $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -c -o $@ src/fetch.c # Concord's headers are not ours: their warnings are theirs -src/main.o: src/main.c $(LOGSCAN_FILES) src/age.h src/channels.h src/follow.h src/issues.h src/worker.h src/fetch.h \ - src/report.h src/site.h src/store.h src/probe.h src/message.h src/tracer.h $(REI)/src/watch.h +src/bot.o: src/bot.c src/bot.h $(LOGSCAN_FILES) src/age.h src/channels.h src/follow.h src/issues.h src/worker.h \ + src/fetch.h src/report.h src/site.h src/store.h src/probe.h src/message.h src/tracer.h $(REI)/src/watch.h + $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -isystem $(CONCORD)/include/concord -c -o $@ src/bot.c +src/main.o: src/main.c src/bot.h src/tracer.h $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -isystem $(CONCORD)/include/concord -c -o $@ src/main.c src/rei_util.o: $(REI)/src/util.c $(REI)/src/rei.h $(CC) $(CFLAGS) $(OURS) -c -o $@ $(REI)/src/util.c @@ -122,7 +134,7 @@ third_party/demangle.o: third_party/logscan/common/utilities/demangle.c $(CC) $(CFLAGS) -std=c99 -Ithird_party/logscan -c -o $@ third_party/logscan/common/utilities/demangle.c clean: - rm -f $(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/main.o src/mcp_main.o tracer tracer-mcp \ - tests/test_tracer tests/bench_probe + rm -f $(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/bot.o src/main.o src/mcp_main.o \ + tracer tracer-mcp libtracer.a tests/test_tracer tests/bench_probe .PHONY: all test bench test-gateway clean diff --git a/tracer/src/bot.c b/tracer/src/bot.c new file mode 100644 index 0000000..80d1e94 --- /dev/null +++ b/tracer/src/bot.c @@ -0,0 +1,539 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + Copyright 2026 the ScarletNX contributors */ +/* Tracer's bot (bot.h): the game reports that people send it on Discord, kept in a database and drawn as a static site. + +A report is deleted by the person who sent it, by the guild's owner and administrators, and by TRACER_ADMINS. + +Concord's gateway brings the events, read here from their JSON, never decoded by Concord. A message that mentions the +bot in a channel it answers in, and each message deleted, wait for the thread that handles them, one at a time: a +report's files are downloaded and read by a worker (worker.h, the program run again as "PROGRAM --report", without +the tokens), and the database is that thread's alone. The bot asks for no privileged intent: Discord sends the text +and the attachments of a message that mentions it */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include /* Concord's */ + +#include "age.h" +#include "bot.h" +#include "channels.h" +#include "fetch.h" +#include "follow.h" +#include "issues.h" +#include "http.h" +#include "report.h" +#include "rest.h" +#include "site.h" +#include "titles.h" +#include "watch.h" +#include "worker.h" + +#define LISTED 64 /* the channels and the admins named, at most */ +#define WAITING 1024 /* the events that wait for the thread, at most */ + +static struct { + const char *token, *url, *db, *site, *assets, *titles; + double min_free; /* bytes */ + TrNotify notify; + u64 channels[LISTED], admins[LISTED]; + int nchannels, nadmins; +} env; +static RhAuth discord_auth, github_auth; +static Dc dc; +static TrIssues issues; /* issues.repo NULL: no report is filed as an issue */ +static TrAge age; /* age.fetch NULL: no token, nothing asked of GitHub */ +static Tracer tracer; +static Titles titles; +static u64 self; + +static const char* const KNOWN[] = {TR_KEYS}; + +static void (*end)(void); /* the program's end, which tb_settings is given */ +static void stop(const char* fmt, const char* value) { + fputs("tracer: ", stderr); + fprintf(stderr, fmt, value); + fputc('\n', stderr); + end(); +} +static const char* setting(const char* name, const char* otherwise) { + const char* v = getenv(name); + return v && *v ? v : otherwise; +} +/* name's ids, "id,id", into ids */ +static int ids(const char* name, u64* to) { + const char* list = setting(name, NULL); + int n = 0; + while (list && *list) { + size_t len = strcspn(list, ","), k; + char id[32]; + if (len && len < sizeof id) { + memcpy(id, list, len); + id[len] = 0; + for (k = 0; k < len; ++k) + if (!isdigit((unsigned char)id[k])) stop("%s is not an id (Developer Mode, Copy ID: its digits)", id); + if (n == LISTED) stop("%s names more than 64 ids", name); + to[n++] = strtoull(id, NULL, 10); + } + list += len + (list[len] == ','); + } + return n; +} +static const char* environment(const char* name) { return getenv(name); } +static void read_env(void) { + extern char** environ; + char** e; + size_t k, n; + for (e = environ; *e; ++e) { /* a key of ours misspelt would be a setting quietly not taken */ + int known = 0; + if (strncmp(*e, "TRACER_", 7)) continue; + for (k = 0; k < sizeof KNOWN / sizeof *KNOWN; ++k) + known |= !strncmp(*e, KNOWN[k], strlen(KNOWN[k])) && (*e)[strlen(KNOWN[k])] == '='; + if (!known) + stop("%s: no such setting (TRACER_TOKEN, _URL, _CHANNELS, _ADMINS, _DB, _SITE, _ASSETS, _TITLES, " + "_ISSUES_REPO, _ISSUES_TOKEN, _ISSUES_EMOJI, _ISSUES_LABELS, _UPSTREAM_REPO, _MIN_FREE, _NOTIFY)", + *e); + } + env.token = setting("TRACER_TOKEN", NULL); + env.url = setting("TRACER_URL", NULL); + if (!env.token || !env.url) stop("TRACER_TOKEN and TRACER_URL (the site's address) are needed%s", ""); + if (strncmp(env.url, "https://", 8) && strncmp(env.url, "http://", 7)) /* http:// for a site on this machine */ + stop("TRACER_URL=%s is not an https:// or http:// address", env.url); + env.db = setting("TRACER_DB", "/var/db/tracer/tracer.db"); + env.site = setting("TRACER_SITE", "/var/db/tracer-site"); + env.assets = setting("TRACER_ASSETS", "/usr/local/share/tracer"); + env.titles = setting("TRACER_TITLES", NULL); + { /* "all", or the channels' ids */ + char* why; + if ((env.nchannels = tr_channels_of(getenv("TRACER_CHANNELS"), env.channels, LISTED, &why)) == -2) + stop("%s", why); + } + env.nadmins = ids("TRACER_ADMINS", env.admins); + { /* the MiB the site's file system keeps free */ + const char* v = setting("TRACER_MIN_FREE", NULL); + char* end_of; + long mib = v ? strtol(v, &end_of, 10) : TR_MIN_FREE; + if (v && (*end_of || !isdigit((unsigned char)*v) || mib < 0 || mib > 1048576)) + stop("TRACER_MIN_FREE=%s is not a whole number of MiB from 0 (no floor) to 1048576", v); + env.min_free = (double)mib * 1048576; + } + n = strlen(env.url); + while (n && env.url[n - 1] == '/') --n; + env.url = rei_format("%.*s", (int)n, env.url); + { /* the issues: their token goes into the GitHub requests' header alone, never into the log */ + const char* token; + char* why; + if (tr_issues_settings(&issues, environment, &token, &why)) stop("%s", why); + if (token) snprintf(github_auth.authorization, sizeof github_auth.authorization, "Bearer %s", token); + github_auth.accept = "application/vnd.github+json"; + issues.fetch = rh_fetch; + issues.ctx = &github_auth; + /* a build's age: asked with the same token, once a commit */ + if (tr_age_settings(&age, environment, &why)) stop("%s", why); + /* a report's sender told its fix is out */ + if (tr_notify_of(getenv("TRACER_NOTIFY"), issues.repo != NULL, &env.notify, &why)) stop("%s", why); + age.fetch = token ? rh_fetch : NULL; + age.ctx = &github_auth; + } +} + +/* ---- the events waiting for the thread ---- */ + +enum { EV_CREATE, EV_DELETE, EV_REACTION, EV_FOLLOW }; +static struct { + int kind[WAITING]; + char* json[WAITING]; /* owned: a MESSAGE_CREATE's or MESSAGE_REACTION_ADD's data, or NULL with id for a deletion */ + u64 id[WAITING]; + int first, n; + pthread_mutex_t lock; + pthread_cond_t more; +} waiting = {{0}, {0}, {0}, 0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER}; + +static void push(int kind, const char* json, size_t len, u64 id) { + pthread_mutex_lock(&waiting.lock); + if (waiting.n < WAITING) { + int at = (waiting.first + waiting.n++) % WAITING; + waiting.kind[at] = kind; + waiting.json[at] = json ? rei_format("%.*s", (int)len, json) : NULL; + waiting.id[at] = id; + pthread_cond_signal(&waiting.more); + } else + rei_log("%d events wait already: one is dropped", WAITING); + pthread_mutex_unlock(&waiting.lock); +} + +/* ---- the channels it answers in ---- */ + +/* channel's parent (a thread's channel, a forum post's forum), asked of Discord; 0 for none or no answer (said) */ +static u64 parent_of(void* ctx, u64 channel) { + char* url = rei_format(DC_API "channels/%llu", (unsigned long long)channel); + RgAnswer a; + cJSON* c; + u64 parent = 0; + (void)ctx; + rh_fetch(&discord_auth, "GET", url, NULL, NULL, &a); + if (a.status == 200 && (c = cJSON_Parse(a.body))) { + const char* id = jstr(c, "parent_id"); + parent = id ? strtoull(id, NULL, 10) : 0; + cJSON_Delete(c); + } else + rei_log("channel %llu could not be read: %s", (unsigned long long)channel, a.error ? a.error : "no access"); + free(url); + free(a.body); + free(a.etag); + free(a.error); + return parent; +} +static TrParents parents; +static int answers_in(u64 channel) { + return tr_answers_in(env.channels, env.nchannels, &parents, channel, parent_of, NULL); +} +#define ADMINISTRATOR 0x8ULL /* the permission's bit */ +/* guild's owner, or a member whose roles (and @everyone, the role whose id is the guild's) give Administrator: the + guild's roles asked of Discord, as a message names its author's roles and not their permissions */ +static int administers(void* ctx, u64 guild, u64 user, const u64* roles, int nroles) { + char* url = rei_format(DC_API "guilds/%llu", (unsigned long long)guild); + RgAnswer a; + cJSON* g; + const cJSON* role; + unsigned long long permissions = 0; + int i, yes = 0; + (void)ctx; + rh_fetch(&discord_auth, "GET", url, NULL, NULL, &a); + if (a.status == 200 && (g = cJSON_Parse(a.body))) { + const char* owner = jstr(g, "owner_id"); + yes = owner && strtoull(owner, NULL, 10) == user; + cJSON_ArrayForEach(role, cJSON_GetObjectItemCaseSensitive(g, "roles")) { + const char *id = jstr(role, "id"), *bits = jstr(role, "permissions"); + u64 rid = id ? strtoull(id, NULL, 10) : 0; + int has = rid == guild; + for (i = 0; i < nroles && !has; ++i) has = roles[i] == rid; + if (has && bits) permissions |= strtoull(bits, NULL, 10); + } + yes |= (permissions & ADMINISTRATOR) != 0; + cJSON_Delete(g); + } else + rei_log("guild %llu could not be read: %s", (unsigned long long)guild, a.error ? a.error : "no access"); + free(url); + free(a.body); + free(a.etag); + free(a.error); + return yes; +} + +/* GET channels/C/messages/ID, for a reply that asks for a report of the message it replies to: the message's JSON + (owned) and Discord's status; NULL, 0 and why when Discord did not answer */ +static char* message_got(void* ctx, u64 channel, u64 id, int* status, char** why) { + char* url = rei_format(DC_API "channels/%llu/messages/%llu", (unsigned long long)channel, (unsigned long long)id); + RgAnswer a; + (void)ctx; + rh_fetch(&discord_auth, "GET", url, NULL, NULL, &a); + free(url); + *status = a.status; + *why = a.error ? rei_strdup(a.error) : a.status != 200 && a.body ? rei_format("%.200s", a.body) : NULL; + free(a.etag); + free(a.error); + return a.body; +} +#define CONTENT_INTENT 0xc0000UL /* the application's flags of the Message Content intent: approved, or limited */ +/* whether Discord shows the bot the files of the messages that do not mention it, by its application's flags: 1, 0, + or -1 with why when Discord did not say */ +static int content_intent(void* ctx, char** why) { + RgAnswer a; + cJSON* app; + int yes = -1; + (void)ctx; + *why = NULL; + rh_fetch(&discord_auth, "GET", DC_API "applications/@me", NULL, NULL, &a); + if (a.status == 200 && (app = cJSON_Parse(a.body))) { + const cJSON* flags = cJSON_GetObjectItemCaseSensitive(app, "flags"); + if (cJSON_IsNumber(flags)) + yes = ((unsigned long)flags->valuedouble & CONTENT_INTENT) != 0; + else + *why = rei_strdup("GET applications/@me answered no flags"); + cJSON_Delete(app); + } else + *why = a.status ? rei_format("GET applications/@me answered %d: %.200s", a.status, a.body ? a.body : "nothing") + : rei_format("GET applications/@me: %s", a.error ? a.error : "no answer"); + free(a.body); + free(a.etag); + free(a.error); + return yes; +} + +static void typing(u64 channel) { + char* url = rei_format(DC_API "channels/%llu/typing", (unsigned long long)channel); + RgAnswer a; + rh_fetch(&discord_auth, "POST", url, "{}", NULL, &a); + free(url); + free(a.body); + free(a.etag); + free(a.error); +} + +static void* handle(void* unused) { + (void)unused; + for (;;) { + int kind; + char* json; + u64 id; + pthread_mutex_lock(&waiting.lock); + while (!waiting.n) pthread_cond_wait(&waiting.more, &waiting.lock); + kind = waiting.kind[waiting.first]; + json = waiting.json[waiting.first]; + id = waiting.id[waiting.first]; + waiting.first = (waiting.first + 1) % WAITING; + --waiting.n; + pthread_mutex_unlock(&waiting.lock); + if (kind == EV_CREATE) { + TrMessage m; + if (tr_message(json, strlen(json), self, &m)) { + if (answers_in(m.channel)) { + cJSON* e; + typing(m.channel); + long report; + if ((e = tr_answer(&tracer, &m, &report))) + tr_replied(&tracer, report, dc_post(&dc, m.channel, e, m.id)); + cJSON_Delete(e); + } + tr_message_free(&m); + } + } else if (kind == EV_REACTION) { + TrReaction r; + if (tr_reaction(json, strlen(json), self, &r)) { + cJSON* e = tr_approved(&tracer, &r); + if (e) dc_post(&dc, r.channel, e, r.message); + cJSON_Delete(e); + tr_reaction_free(&r); + } + } else if (kind == EV_FOLLOW) + tr_follow(&tracer); + else + tr_forget(&tracer, id); + free(json); + } + return NULL; +} + +/* a round of the issues' following (follow.h) every TR_FOLLOW seconds, on the thread of the events, which alone holds + the database */ +static void* follow_rounds(void* unused) { + (void)unused; + for (;;) { + sleep(TR_FOLLOW); + push(EV_FOLLOW, NULL, 0, 0); + } + return NULL; +} +/* a reply that tells a report's sender its fix is out */ +static u64 tell(void* ctx, u64 channel, u64 message, const cJSON* embed, int ping) { + (void)ctx; + return dc_reply(&dc, channel, embed, message, ping); +} + +/* ---- the gateway ---- */ + +static void ready(const char* json, size_t size) { + static int started; + char* name; + self = tr_ready(json, size, &name); + { + char* channels = tr_channels_said(env.channels, env.nchannels); + rei_log("up as %s, the site at %s; channels: %s", name ? name : "a user READY did not name", env.url, channels); + free(channels); + } + free(name); + if (!started) { + pthread_t thread; + int code = pthread_create(&thread, NULL, handle, NULL); + if (code) stop("the thread of the reports did not start: %s", strerror(code)); + pthread_detach(thread); + if (issues.repo && age.fetch) { /* the issues followed: with their repository and the token */ + if ((code = pthread_create(&thread, NULL, follow_rounds, NULL))) + stop("the thread of the issues' rounds did not start: %s", strerror(code)); + pthread_detach(thread); + } + started = 1; + } +} +/* the gateway's thread: a message that mentions the bot in a server waits for the thread, the others are dropped here; + each deletion waits, the thread knows which made a report */ +static enum discord_event_scheduler scheduler(struct discord* client, const char data[], size_t size, + enum discord_gateway_events event) { + (void)client; + if (event == DISCORD_EV_READY) ready(data, size); + if (event == DISCORD_EV_MESSAGE_CREATE) { + TrMessage m; + if (tr_message(data, size, self, &m)) { + if (m.mentioned && m.guild) push(EV_CREATE, data, size, 0); + tr_message_free(&m); + } + } + if (event == DISCORD_EV_MESSAGE_REACTION_ADD && issues.repo) { /* the approval's emoji alone waits */ + TrReaction r; + if (tr_reaction(data, size, self, &r)) { + if (r.guild && !strcmp(r.emoji, issues.emoji)) push(EV_REACTION, data, size, 0); + tr_reaction_free(&r); + } + } + if (event == DISCORD_EV_MESSAGE_DELETE) { + u64 id = tr_deleted(data, size); + if (id) push(EV_DELETE, NULL, 0, id); + } + if (event == DISCORD_EV_MESSAGE_DELETE_BULK) { + cJSON *d = cJSON_ParseWithLength(data, size), *each; + cJSON_ArrayForEach(each, cJSON_GetObjectItemCaseSensitive(d, "ids")) if (cJSON_IsString(each)) + push(EV_DELETE, NULL, 0, strtoull(each->valuestring, NULL, 10)); + cJSON_Delete(d); + } + return DISCORD_EVENT_IGNORE; +} + +/* a report's files taken by this program run again as "--report" (ctx its argv) */ +static int worker(void* ctx, Tracer* t, const TrMessage* m, Taken* k, char** why) { + (void)t; + return wk_take(ctx, WK_SECONDS, m, k, why); +} + +int tb_untraceable(char** why) { return wk_untraceable(why); } + +int tb_worker(void) { + int failed; + wk_limit(); + curl_global_init(CURL_GLOBAL_DEFAULT); + failed = wk_serve(stdin, stdout, tr_fetch, NULL); + /* _exit: shut in its folder, exit's handlers (libcurl's and OpenSSL's cleanup, a sanitizer's leak check, which + reads /proc) could open nothing they need */ + fflush(NULL); + _exit(failed); + return failed; +} + +void tb_settings(void (*program_end)(void)) { + end = program_end; + signal(SIGPIPE, SIG_IGN); /* a worker gone before it read its question is an error said, not the program's end */ + read_env(); +} + +u64 tb_user(void) { return dc_self(&dc); } + +struct discord* tb_start(const char* self_path) { + static char* again[3] = {NULL, "--report", NULL}; + struct discord* client; + struct discord_config config; + if (self_path[0] != '/') stop("%s is not run by its full path, which its workers need", self_path); + again[0] = (char*)self_path; + snprintf(discord_auth.authorization, sizeof discord_auth.authorization, "Bot %s", env.token); + dc_init(&dc, rh_fetch, &discord_auth); + tracer.store = st_open(env.db); + if (!tracer.store) stop("the database %s could not be opened", env.db); + tracer.root = env.site; + tracer.url = env.url; + tracer.get = tr_fetch; + { /* the files wait beside the database, never in the site */ + char* db = rei_strdup(env.db); + /* (dirname): FreeBSD's libgen.h makes dirname a C11 _Generic macro, which -std=c99 -Wpedantic refuses; the + function itself, which may write into its argument, gets a copy */ + tracer.work = rei_format("%s/work", (dirname)(db)); + free(db); + if (mkdir(tracer.work, 0700) && errno != EEXIST) + stop("%s", rei_format("the work folder %s could not be made: %s", tracer.work, strerror(errno))); + } + tracer.take = worker; + tracer.take_ctx = again; + tracer.admins = env.admins; + tracer.nadmins = env.nadmins; + tracer.administers = administers; + tracer.issues = &issues; + tracer.age = &age; + tracer.message = message_got; + tracer.content_intent = content_intent; + tracer.min_free = env.min_free; + tracer.notify = env.notify; + tracer.tell = tell; + { /* the disk the reports land on, said once */ + char* why; + double now = tr_free_bytes(env.site, &why); + if (!env.min_free) + rei_log("disk: TRACER_MIN_FREE=0 keeps none of the site's file system free, so a report may fill it"); + else if (now < 0) + rei_log("disk: the free space of the site's file system could not be read (%s): each report is refused " + "until it can", + why); + else + rei_log("disk: the site's file system has %.0f MiB free; a report that would leave less than %.0f MiB " + "(TRACER_MIN_FREE) is refused%s", + now / 1048576, env.min_free / 1048576, now < env.min_free ? ", as every one is now" : ""); + free(why); + } + { /* what a reply alone to a message with a log gets, said once at the start; asked again at each such reply */ + char* why; + int sees = content_intent(NULL, &why); + rei_log("replies: a reply that mentions me with no file makes a report of the message it replies to; %s", + sees == 1 ? "Discord shows me that message's files (the Message Content intent is on)" + : !sees ? "Discord does not show me that message's files, as the Message Content intent is off: " + "such a reply is told to send the log with a mention" + : why ? why + : "Discord did not say whether it shows me that message's files"); + free(why); + } + { + char* said = tr_issues_said(&issues); + rei_log("%s", said); + free(said); + said = tr_age_said(&age); + rei_log("%s", said); + free(said); + said = age.fetch || !issues.repo ? tr_follow_said(&tracer) + : rei_strdup("follow: no issue followed, as TRACER_ISSUES_TOKEN is not set"); + rei_log("%s", said); + free(said); + } + rei_log("workers: %s --report reads each report's files, without the settings, the tokens and this process's " + "memory", + self_path); + if (site_assets(env.assets, env.site)) stop("the site's files could not be copied from %s", env.assets); + if (!env.titles) + rei_log("TRACER_TITLES is not set: no list of games' names, a game its logs don't name keeps its title ID"); + else if (!tr_titles_load(&titles, env.titles)) { + tracer.titles = &titles; + rei_log("%d games' names from %s", titles.n, env.titles); + } + { /* a new log summary or list of names: the reports summarized and named again with them */ + char* version = rei_format("%s %s %s", TR_LOGSCAN, titles.version, TR_FACTS); + tr_rescan(&tracer, version); + free(version); + } + if (site_rebuild(tracer.store, env.site)) rei_log("some pages could not be drawn again"); + memset(&config, 0, sizeof config); + /* a copy for Concord to own: discord_from_config keeps the pointer it is given, and discord_cleanup frees it */ + config.token = rei_strdup(env.token); + config.log.level = LOGMOD_LEVEL_WARN; /* Concord's traces name every event: only what goes wrong is logged */ + client = discord_from_config(&config); + if (!client) stop("Concord could not start a client with TRACER_TOKEN%s", ""); + discord_add_intents(client, DISCORD_GATEWAY_GUILDS | DISCORD_GATEWAY_GUILD_MESSAGES | + (issues.repo ? DISCORD_GATEWAY_GUILD_MESSAGE_REACTIONS : 0)); + discord_set_event_scheduler(client, scheduler); + wd_start(client, REI_NAME, end); + return client; +} + +void tb_stopped(struct discord* client, CCORDcode code) { + if (code == CCORD_DISCORD_BAD_AUTH) { /* what Concord's discord_run returns for any failure of GET gateway/bot */ + char* now = dc_said(&dc, "gateway/bot"); + rei_log("the gateway stopped: Concord's GET gateway/bot failed, which it calls \"%s\" whatever the failure, a " + "timeout too; asked again, Discord answers %s", + discord_strerror(code, client), now); + free(now); + } else + rei_log("the gateway stopped: %s", discord_strerror(code, client)); +} diff --git a/tracer/src/bot.h b/tracer/src/bot.h new file mode 100644 index 0000000..7c449d8 --- /dev/null +++ b/tracer/src/bot.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + Copyright 2026 the ScarletNX contributors */ +/* Tracer's bot, for a program that runs it: its settings (the TRACER_ keys of the environment, the env file), its + database and site, its Concord client and the thread that answers its events. tracer (main.c) runs it alone; + reiwimsy runs it beside ReiWimsy in one process, each bot with its own token, gateway and name on Discord */ +#ifndef TR_BOT_H +#define TR_BOT_H +#include /* Concord's */ + +#include "rei.h" + +/* the program's memory and environment, where the tokens are, kept from its workers' debuggers (worker.h's + wk_untraceable): 0, or -1 with why (owned). Before the settings are read */ +int tb_untraceable(char** why); +/* a worker, "PROGRAM --report": one message's files read from stdin's question, answered on stdout (worker.h), within + its limits and without the program's settings; never returns */ +int tb_worker(void); +/* the settings read, the program stopped through end (with why on stderr) on one the bot refuses */ +void tb_settings(void (*end)(void)); +/* the bot made: its database, site, workers (self, the program's full path, which they run) and Concord client, which + the caller runs (discord_run); end on what fails */ +struct discord* tb_start(const char* self); +/* the bot's user id, asked of Discord with its token after tb_start: 0 when Discord did not say */ +u64 tb_user(void); +/* the line that says why its gateway stopped: discord_run's code */ +void tb_stopped(struct discord* client, CCORDcode code); +#endif diff --git a/tracer/src/main.c b/tracer/src/main.c index 3369552..ef45918 100644 --- a/tracer/src/main.c +++ b/tracer/src/main.c @@ -1,542 +1,44 @@ /* SPDX-License-Identifier: GPL-3.0-or-later Copyright 2026 the ScarletNX contributors */ -/* Tracer: the game reports that people send it on Discord, kept in a database and drawn as a static site. +/* Tracer alone (bot.h): the game reports that people send it on Discord, kept in a database and drawn as a static site. TRACER_TOKEN=... TRACER_URL=https://... [TRACER_CHANNELS=id,id|all] [TRACER_ADMINS=id,id] [TRACER_DB=file] [TRACER_SITE=folder] [TRACER_ASSETS=folder] [TRACER_TITLES=file] [TRACER_UPSTREAM_REPO=owner/name] tracer -A report is deleted by the person who sent it, by the guild's owner and administrators, and by TRACER_ADMINS. - -Concord's gateway brings the events, read here from their JSON, never decoded by Concord. A message that mentions the -bot in a channel it answers in, and each message deleted, wait for the thread that handles them, one at a time: a -report's files are downloaded and read by a worker (worker.h, this program run again as "tracer --report", without the -token), and the database is that thread's alone. The bot asks for no privileged intent: Discord -sends the text and the attachments of a message that mentions it */ +reiwimsy runs the same bot beside ReiWimsy, in one process */ #define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include #include -#include #include -#include #include #include -#include /* Concord's */ - -#include "age.h" -#include "channels.h" -#include "fetch.h" -#include "follow.h" -#include "issues.h" -#include "http.h" -#include "report.h" -#include "rest.h" -#include "site.h" -#include "titles.h" -#include "watch.h" -#include "worker.h" - -#define LISTED 64 /* the channels and the admins named, at most */ -#define WAITING 1024 /* the events that wait for the thread, at most */ - -static struct { - const char *token, *url, *db, *site, *assets, *titles; - double min_free; /* bytes */ - TrNotify notify; - u64 channels[LISTED], admins[LISTED]; - int nchannels, nadmins; -} env; -static RhAuth discord_auth, github_auth; -static Dc dc; -static TrIssues issues; /* issues.repo NULL: no report is filed as an issue */ -static TrAge age; /* age.fetch NULL: no token, nothing asked of GitHub */ -static Tracer tracer; -static Titles titles; -static u64 self; - -static const char* const KNOWN[] = {TR_KEYS}; +#include "bot.h" -/* the bot's end, with 1, from which daemon(8) starts it again. _exit, not exit: the thread of the events may be inside - a request, which libcurl's cleanup and exit's handlers (OpenSSL's) must not run under */ +/* the program's end, with 1, from which daemon(8) starts it again. _exit, not exit: the thread of the events may be + inside a request, which libcurl's cleanup and exit's handlers (OpenSSL's) must not run under */ static void end(void) { fflush(NULL); _exit(1); } -static void stop(const char* fmt, const char* value) { - fputs("tracer: ", stderr); - fprintf(stderr, fmt, value); - fputc('\n', stderr); - end(); -} -static const char* setting(const char* name, const char* otherwise) { - const char* v = getenv(name); - return v && *v ? v : otherwise; -} -/* name's ids, "id,id", into ids */ -static int ids(const char* name, u64* to) { - const char* list = setting(name, NULL); - int n = 0; - while (list && *list) { - size_t len = strcspn(list, ","), k; - char id[32]; - if (len && len < sizeof id) { - memcpy(id, list, len); - id[len] = 0; - for (k = 0; k < len; ++k) - if (!isdigit((unsigned char)id[k])) stop("%s is not an id (Developer Mode, Copy ID: its digits)", id); - if (n == LISTED) stop("%s names more than 64 ids", name); - to[n++] = strtoull(id, NULL, 10); - } - list += len + (list[len] == ','); - } - return n; -} -static const char* environment(const char* name) { return getenv(name); } -static void read_env(void) { - extern char** environ; - char** e; - size_t k, n; - for (e = environ; *e; ++e) { /* a key of ours misspelt would be a setting quietly not taken */ - int known = 0; - if (strncmp(*e, "TRACER_", 7)) continue; - for (k = 0; k < sizeof KNOWN / sizeof *KNOWN; ++k) - known |= !strncmp(*e, KNOWN[k], strlen(KNOWN[k])) && (*e)[strlen(KNOWN[k])] == '='; - if (!known) - stop("%s: no such setting (TRACER_TOKEN, _URL, _CHANNELS, _ADMINS, _DB, _SITE, _ASSETS, _TITLES, " - "_ISSUES_REPO, _ISSUES_TOKEN, _ISSUES_EMOJI, _ISSUES_LABELS, _UPSTREAM_REPO, _MIN_FREE, _NOTIFY)", - *e); - } - env.token = setting("TRACER_TOKEN", NULL); - env.url = setting("TRACER_URL", NULL); - if (!env.token || !env.url) stop("TRACER_TOKEN and TRACER_URL (the site's address) are needed%s", ""); - if (strncmp(env.url, "https://", 8) && strncmp(env.url, "http://", 7)) /* http:// for a site on this machine */ - stop("TRACER_URL=%s is not an https:// or http:// address", env.url); - env.db = setting("TRACER_DB", "/var/db/tracer/tracer.db"); - env.site = setting("TRACER_SITE", "/var/db/tracer-site"); - env.assets = setting("TRACER_ASSETS", "/usr/local/share/tracer"); - env.titles = setting("TRACER_TITLES", NULL); - { /* "all", or the channels' ids */ - char* why; - if ((env.nchannels = tr_channels_of(getenv("TRACER_CHANNELS"), env.channels, LISTED, &why)) == -2) - stop("%s", why); - } - env.nadmins = ids("TRACER_ADMINS", env.admins); - { /* the MiB the site's file system keeps free */ - const char* v = setting("TRACER_MIN_FREE", NULL); - char* end_of; - long mib = v ? strtol(v, &end_of, 10) : TR_MIN_FREE; - if (v && (*end_of || !isdigit((unsigned char)*v) || mib < 0 || mib > 1048576)) - stop("TRACER_MIN_FREE=%s is not a whole number of MiB from 0 (no floor) to 1048576", v); - env.min_free = (double)mib * 1048576; - } - n = strlen(env.url); - while (n && env.url[n - 1] == '/') --n; - env.url = rei_format("%.*s", (int)n, env.url); - { /* the issues: their token goes into the GitHub requests' header alone, never into the log */ - const char* token; - char* why; - if (tr_issues_settings(&issues, environment, &token, &why)) stop("%s", why); - if (token) snprintf(github_auth.authorization, sizeof github_auth.authorization, "Bearer %s", token); - github_auth.accept = "application/vnd.github+json"; - issues.fetch = rh_fetch; - issues.ctx = &github_auth; - /* a build's age: asked with the same token, once a commit */ - if (tr_age_settings(&age, environment, &why)) stop("%s", why); - /* a report's sender told its fix is out */ - if (tr_notify_of(getenv("TRACER_NOTIFY"), issues.repo != NULL, &env.notify, &why)) stop("%s", why); - age.fetch = token ? rh_fetch : NULL; - age.ctx = &github_auth; - } -} - -/* ---- the events waiting for the thread ---- */ - -enum { EV_CREATE, EV_DELETE, EV_REACTION, EV_FOLLOW }; -static struct { - int kind[WAITING]; - char* json[WAITING]; /* owned: a MESSAGE_CREATE's or MESSAGE_REACTION_ADD's data, or NULL with id for a deletion */ - u64 id[WAITING]; - int first, n; - pthread_mutex_t lock; - pthread_cond_t more; -} waiting = {{0}, {0}, {0}, 0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER}; - -static void push(int kind, const char* json, size_t len, u64 id) { - pthread_mutex_lock(&waiting.lock); - if (waiting.n < WAITING) { - int at = (waiting.first + waiting.n++) % WAITING; - waiting.kind[at] = kind; - waiting.json[at] = json ? rei_format("%.*s", (int)len, json) : NULL; - waiting.id[at] = id; - pthread_cond_signal(&waiting.more); - } else - rei_log("%d events wait already: one is dropped", WAITING); - pthread_mutex_unlock(&waiting.lock); -} - -/* ---- the channels it answers in ---- */ - -/* channel's parent (a thread's channel, a forum post's forum), asked of Discord; 0 for none or no answer (said) */ -static u64 parent_of(void* ctx, u64 channel) { - char* url = rei_format(DC_API "channels/%llu", (unsigned long long)channel); - RgAnswer a; - cJSON* c; - u64 parent = 0; - (void)ctx; - rh_fetch(&discord_auth, "GET", url, NULL, NULL, &a); - if (a.status == 200 && (c = cJSON_Parse(a.body))) { - const char* id = jstr(c, "parent_id"); - parent = id ? strtoull(id, NULL, 10) : 0; - cJSON_Delete(c); - } else - rei_log("channel %llu could not be read: %s", (unsigned long long)channel, a.error ? a.error : "no access"); - free(url); - free(a.body); - free(a.etag); - free(a.error); - return parent; -} -static TrParents parents; -static int answers_in(u64 channel) { - return tr_answers_in(env.channels, env.nchannels, &parents, channel, parent_of, NULL); -} -#define ADMINISTRATOR 0x8ULL /* the permission's bit */ -/* guild's owner, or a member whose roles (and @everyone, the role whose id is the guild's) give Administrator: the - guild's roles asked of Discord, as a message names its author's roles and not their permissions */ -static int administers(void* ctx, u64 guild, u64 user, const u64* roles, int nroles) { - char* url = rei_format(DC_API "guilds/%llu", (unsigned long long)guild); - RgAnswer a; - cJSON* g; - const cJSON* role; - unsigned long long permissions = 0; - int i, yes = 0; - (void)ctx; - rh_fetch(&discord_auth, "GET", url, NULL, NULL, &a); - if (a.status == 200 && (g = cJSON_Parse(a.body))) { - const char* owner = jstr(g, "owner_id"); - yes = owner && strtoull(owner, NULL, 10) == user; - cJSON_ArrayForEach(role, cJSON_GetObjectItemCaseSensitive(g, "roles")) { - const char *id = jstr(role, "id"), *bits = jstr(role, "permissions"); - u64 rid = id ? strtoull(id, NULL, 10) : 0; - int has = rid == guild; - for (i = 0; i < nroles && !has; ++i) has = roles[i] == rid; - if (has && bits) permissions |= strtoull(bits, NULL, 10); - } - yes |= (permissions & ADMINISTRATOR) != 0; - cJSON_Delete(g); - } else - rei_log("guild %llu could not be read: %s", (unsigned long long)guild, a.error ? a.error : "no access"); - free(url); - free(a.body); - free(a.etag); - free(a.error); - return yes; -} - -/* GET channels/C/messages/ID, for a reply that asks for a report of the message it replies to: the message's JSON - (owned) and Discord's status; NULL, 0 and why when Discord did not answer */ -static char* message_got(void* ctx, u64 channel, u64 id, int* status, char** why) { - char* url = rei_format(DC_API "channels/%llu/messages/%llu", (unsigned long long)channel, (unsigned long long)id); - RgAnswer a; - (void)ctx; - rh_fetch(&discord_auth, "GET", url, NULL, NULL, &a); - free(url); - *status = a.status; - *why = a.error ? rei_strdup(a.error) : a.status != 200 && a.body ? rei_format("%.200s", a.body) : NULL; - free(a.etag); - free(a.error); - return a.body; -} -#define CONTENT_INTENT 0xc0000UL /* the application's flags of the Message Content intent: approved, or limited */ -/* whether Discord shows the bot the files of the messages that do not mention it, by its application's flags: 1, 0, - or -1 with why when Discord did not say */ -static int content_intent(void* ctx, char** why) { - RgAnswer a; - cJSON* app; - int yes = -1; - (void)ctx; - *why = NULL; - rh_fetch(&discord_auth, "GET", DC_API "applications/@me", NULL, NULL, &a); - if (a.status == 200 && (app = cJSON_Parse(a.body))) { - const cJSON* flags = cJSON_GetObjectItemCaseSensitive(app, "flags"); - if (cJSON_IsNumber(flags)) - yes = ((unsigned long)flags->valuedouble & CONTENT_INTENT) != 0; - else - *why = rei_strdup("GET applications/@me answered no flags"); - cJSON_Delete(app); - } else - *why = a.status ? rei_format("GET applications/@me answered %d: %.200s", a.status, a.body ? a.body : "nothing") - : rei_format("GET applications/@me: %s", a.error ? a.error : "no answer"); - free(a.body); - free(a.etag); - free(a.error); - return yes; -} - -static void typing(u64 channel) { - char* url = rei_format(DC_API "channels/%llu/typing", (unsigned long long)channel); - RgAnswer a; - rh_fetch(&discord_auth, "POST", url, "{}", NULL, &a); - free(url); - free(a.body); - free(a.etag); - free(a.error); -} - -static void* handle(void* unused) { - (void)unused; - for (;;) { - int kind; - char* json; - u64 id; - pthread_mutex_lock(&waiting.lock); - while (!waiting.n) pthread_cond_wait(&waiting.more, &waiting.lock); - kind = waiting.kind[waiting.first]; - json = waiting.json[waiting.first]; - id = waiting.id[waiting.first]; - waiting.first = (waiting.first + 1) % WAITING; - --waiting.n; - pthread_mutex_unlock(&waiting.lock); - if (kind == EV_CREATE) { - TrMessage m; - if (tr_message(json, strlen(json), self, &m)) { - if (answers_in(m.channel)) { - cJSON* e; - typing(m.channel); - long report; - if ((e = tr_answer(&tracer, &m, &report))) - tr_replied(&tracer, report, dc_post(&dc, m.channel, e, m.id)); - cJSON_Delete(e); - } - tr_message_free(&m); - } - } else if (kind == EV_REACTION) { - TrReaction r; - if (tr_reaction(json, strlen(json), self, &r)) { - cJSON* e = tr_approved(&tracer, &r); - if (e) dc_post(&dc, r.channel, e, r.message); - cJSON_Delete(e); - tr_reaction_free(&r); - } - } else if (kind == EV_FOLLOW) - tr_follow(&tracer); - else - tr_forget(&tracer, id); - free(json); - } - return NULL; -} - -/* a round of the issues' following (follow.h) every TR_FOLLOW seconds, on the thread of the events, which alone holds - the database */ -static void* follow_rounds(void* unused) { - (void)unused; - for (;;) { - sleep(TR_FOLLOW); - push(EV_FOLLOW, NULL, 0, 0); - } - return NULL; -} -/* a reply that tells a report's sender its fix is out */ -static u64 tell(void* ctx, u64 channel, u64 message, const cJSON* embed, int ping) { - (void)ctx; - return dc_reply(&dc, channel, embed, message, ping); -} - -/* ---- the gateway ---- */ - -static void ready(const char* json, size_t size) { - static int started; - char* name; - self = tr_ready(json, size, &name); - { - char* channels = tr_channels_said(env.channels, env.nchannels); - rei_log("up as %s, the site at %s; channels: %s", name ? name : "a user READY did not name", env.url, channels); - free(channels); - } - free(name); - if (!started) { - pthread_t thread; - int code = pthread_create(&thread, NULL, handle, NULL); - if (code) stop("the thread of the reports did not start: %s", strerror(code)); - pthread_detach(thread); - if (issues.repo && age.fetch) { /* the issues followed: with their repository and the token */ - if ((code = pthread_create(&thread, NULL, follow_rounds, NULL))) - stop("the thread of the issues' rounds did not start: %s", strerror(code)); - pthread_detach(thread); - } - started = 1; - } -} -/* the gateway's thread: a message that mentions the bot in a server waits for the thread, the others are dropped here; - each deletion waits, the thread knows which made a report */ -static enum discord_event_scheduler scheduler(struct discord* client, const char data[], size_t size, - enum discord_gateway_events event) { - (void)client; - if (event == DISCORD_EV_READY) ready(data, size); - if (event == DISCORD_EV_MESSAGE_CREATE) { - TrMessage m; - if (tr_message(data, size, self, &m)) { - if (m.mentioned && m.guild) push(EV_CREATE, data, size, 0); - tr_message_free(&m); - } - } - if (event == DISCORD_EV_MESSAGE_REACTION_ADD && issues.repo) { /* the approval's emoji alone waits */ - TrReaction r; - if (tr_reaction(data, size, self, &r)) { - if (r.guild && !strcmp(r.emoji, issues.emoji)) push(EV_REACTION, data, size, 0); - tr_reaction_free(&r); - } - } - if (event == DISCORD_EV_MESSAGE_DELETE) { - u64 id = tr_deleted(data, size); - if (id) push(EV_DELETE, NULL, 0, id); - } - if (event == DISCORD_EV_MESSAGE_DELETE_BULK) { - cJSON *d = cJSON_ParseWithLength(data, size), *each; - cJSON_ArrayForEach(each, cJSON_GetObjectItemCaseSensitive(d, "ids")) if (cJSON_IsString(each)) - push(EV_DELETE, NULL, 0, strtoull(each->valuestring, NULL, 10)); - cJSON_Delete(d); - } - return DISCORD_EVENT_IGNORE; -} - -/* a report's files taken by this program run again as "--report" (ctx its argv) */ -static int worker(void* ctx, Tracer* t, const TrMessage* m, Taken* k, char** why) { - (void)t; - return wk_take(ctx, WK_SECONDS, m, k, why); -} int main(int argc, char** argv) { - static char* again[3] = {NULL, "--report", NULL}; struct discord* client; - struct discord_config config; - CCORDcode code; - if (argc == 2 && !strcmp(argv[1], "--report")) { /* a worker: no settings, no token, its own limits */ - int failed; - wk_limit(); - curl_global_init(CURL_GLOBAL_DEFAULT); - failed = wk_serve(stdin, stdout, tr_fetch, NULL); - /* _exit: shut in its folder, exit's handlers (libcurl's and OpenSSL's cleanup, a sanitizer's leak check, which - reads /proc) could open nothing they need */ - fflush(NULL); - _exit(failed); - } + if (argc == 2 && !strcmp(argv[1], "--report")) return tb_worker(); /* a worker: no settings, no token */ { /* before anything else: its workers, which read what strangers send, never read its tokens */ char* why; - if (wk_untraceable(&why)) stop("its tokens could not be kept from its workers: %s", why); - } - if (argc != 1) stop("%s takes no argument: its settings are in its environment (the env file)", argv[0]); - if (argv[0][0] != '/') stop("%s is not run by its full path, which its workers need", argv[0]); - again[0] = argv[0]; - signal(SIGPIPE, SIG_IGN); /* a worker gone before it read its question is an error said, not the bot's end */ - read_env(); - curl_global_init(CURL_GLOBAL_DEFAULT); - snprintf(discord_auth.authorization, sizeof discord_auth.authorization, "Bot %s", env.token); - dc_init(&dc, rh_fetch, &discord_auth); - tracer.store = st_open(env.db); - if (!tracer.store) stop("the database %s could not be opened", env.db); - tracer.root = env.site; - tracer.url = env.url; - tracer.get = tr_fetch; - { /* the files wait beside the database, never in the site */ - char* db = rei_strdup(env.db); - /* (dirname): FreeBSD's libgen.h makes dirname a C11 _Generic macro, which -std=c99 -Wpedantic refuses; the - function itself, which may write into its argument, gets a copy */ - tracer.work = rei_format("%s/work", (dirname)(db)); - free(db); - if (mkdir(tracer.work, 0700) && errno != EEXIST) - stop("%s", rei_format("the work folder %s could not be made: %s", tracer.work, strerror(errno))); - } - tracer.take = worker; - tracer.take_ctx = again; - tracer.admins = env.admins; - tracer.nadmins = env.nadmins; - tracer.administers = administers; - tracer.issues = &issues; - tracer.age = &age; - tracer.message = message_got; - tracer.content_intent = content_intent; - tracer.min_free = env.min_free; - tracer.notify = env.notify; - tracer.tell = tell; - { /* the disk the reports land on, said once */ - char* why; - double now = tr_free_bytes(env.site, &why); - if (!env.min_free) - rei_log("disk: TRACER_MIN_FREE=0 keeps none of the site's file system free, so a report may fill it"); - else if (now < 0) - rei_log("disk: the free space of the site's file system could not be read (%s): each report is refused " - "until it can", - why); - else - rei_log("disk: the site's file system has %.0f MiB free; a report that would leave less than %.0f MiB " - "(TRACER_MIN_FREE) is refused%s", - now / 1048576, env.min_free / 1048576, now < env.min_free ? ", as every one is now" : ""); - free(why); - } - { /* what a reply alone to a message with a log gets, said once at the start; asked again at each such reply */ - char* why; - int sees = content_intent(NULL, &why); - rei_log("replies: a reply that mentions me with no file makes a report of the message it replies to; %s", - sees == 1 ? "Discord shows me that message's files (the Message Content intent is on)" - : !sees ? "Discord does not show me that message's files, as the Message Content intent is off: " - "such a reply is told to send the log with a mention" - : why ? why - : "Discord did not say whether it shows me that message's files"); - free(why); - } - { - char* said = tr_issues_said(&issues); - rei_log("%s", said); - free(said); - said = tr_age_said(&age); - rei_log("%s", said); - free(said); - said = age.fetch || !issues.repo ? tr_follow_said(&tracer) - : rei_strdup("follow: no issue followed, as TRACER_ISSUES_TOKEN is not set"); - rei_log("%s", said); - free(said); - } - rei_log("workers: %s --report reads each report's files, without the settings, the tokens and this process's " - "memory", - argv[0]); - if (site_assets(env.assets, env.site)) stop("the site's files could not be copied from %s", env.assets); - if (!env.titles) - rei_log("TRACER_TITLES is not set: no list of games' names, a game its logs don't name keeps its title ID"); - else if (!tr_titles_load(&titles, env.titles)) { - tracer.titles = &titles; - rei_log("%d games' names from %s", titles.n, env.titles); + if (tb_untraceable(&why)) { + fprintf(stderr, "tracer: its tokens could not be kept from its workers: %s\n", why); + end(); + } } - { /* a new log summary or list of names: the reports summarized and named again with them */ - char* version = rei_format("%s %s %s", TR_LOGSCAN, titles.version, TR_FACTS); - tr_rescan(&tracer, version); - free(version); + if (argc != 1) { + fprintf(stderr, "tracer: %s takes no argument: its settings are in its environment (the env file)\n", argv[0]); + end(); } - if (site_rebuild(tracer.store, env.site)) rei_log("some pages could not be drawn again"); - memset(&config, 0, sizeof config); - /* a copy for Concord to own: discord_from_config keeps the pointer it is given, and discord_cleanup frees it */ - config.token = rei_strdup(env.token); - config.log.level = LOGMOD_LEVEL_WARN; /* Concord's traces name every event: only what goes wrong is logged */ - client = discord_from_config(&config); - if (!client) stop("Concord could not start a client with TRACER_TOKEN%s", ""); - discord_add_intents(client, DISCORD_GATEWAY_GUILDS | DISCORD_GATEWAY_GUILD_MESSAGES | - (issues.repo ? DISCORD_GATEWAY_GUILD_MESSAGE_REACTIONS : 0)); - discord_set_event_scheduler(client, scheduler); - wd_start(client, REI_NAME, end); - code = discord_run(client); - if (code == CCORD_DISCORD_BAD_AUTH) { /* what Concord's discord_run returns for any failure of GET gateway/bot */ - char* now = dc_said(&dc, "gateway/bot"); - rei_log("the gateway stopped: Concord's GET gateway/bot failed, which it calls \"%s\" whatever the failure, a " - "timeout too; asked again, Discord answers %s", - discord_strerror(code, client), now); - free(now); - } else - rei_log("the gateway stopped: %s", discord_strerror(code, client)); + tb_settings(end); + curl_global_init(CURL_GLOBAL_DEFAULT); + client = tb_start(argv[0]); + tb_stopped(client, discord_run(client)); end(); return 1; } From 230103cb4f9ab7d1dbc8ed8476d7fd8d6d1b76b7 Mon Sep 17 00:00:00 2001 From: AO55 Date: Tue, 17 Nov 2026 13:07:32 -0300 Subject: [PATCH 5/7] [dist] reiwimsy's package runs Tracer, and the docs say how to move it there The package holds the site's files under share/reiwimsy/tracer and depends on sqlite3; its service makes Tracer's folders and gives it TRACER_DB, TRACER_SITE and TRACER_ASSETS when the env file has TRACER_TOKEN. Install says how a Tracer that runs alone in its jail moves into discordbots, and the pages say what one process with two bots does. Signed-off-by: AO55 --- dist/freebsd/build.sh | 27 +++++++++++++-------- dist/freebsd/reiwimsy.desc | 3 +++ dist/freebsd/reiwimsy.env.sample | 5 ++++ dist/freebsd/reiwimsy.plist | 17 ++++++++++++++ dist/freebsd/reiwimsy.rc | 14 ++++++++++- docs/BUILD.md | 6 ++++- docs/DEVELOPMENT.md | 8 ++++--- docs/INSTALL.md | 40 ++++++++++++++++++++++++++------ docs/REIWIMSY.md | 22 +++++++++++++++++- docs/SECURITY.md | 8 ++++--- docs/TESTING.md | 18 ++++++++------ docs/TRACER.md | 4 +++- 12 files changed, 138 insertions(+), 34 deletions(-) diff --git a/dist/freebsd/build.sh b/dist/freebsd/build.sh index e9428a8..76c9144 100755 --- a/dist/freebsd/build.sh +++ b/dist/freebsd/build.sh @@ -111,19 +111,30 @@ done # The programs, built and tested from a copy, so the checkout keeps no objects cp -R "$top/rei" "$top/accordwimsy" "$top/tracer" "$work/" -(cd "$work/rei" && make -s clean && make -s test && make -s CONCORD="$work/concord-prefix") (cd "$work/accordwimsy" && make -s clean && make -s test) -# its log summary demangles through the C++ runtime, libc++'s in base +# Tracer's log summary demangles through the C++ runtime, libc++'s in base; reiwimsy runs Tracer too (cd "$work/tracer" && make -s clean && make -s test CXXRT=-lc++ && make -s CONCORD="$work/concord-prefix" CXXRT=-lc++) +(cd "$work/rei" && make -s clean && make -s test && make -s CONCORD="$work/concord-prefix" CXXRT=-lc++) + +# Tracer's site files (the pieces each start copies into its site, and the themes), as a package installs them under +# share/: the tracer package's, and reiwimsy's own, so that each works alone in a jail +site_files() { + install -d "$1" + install -m 644 "$work/tracer/site/style.css" "$work/tracer/site/site.js" "$work/tracer/site/tracer.svg" "$1/" + sh "$top/scripts/tracer-themes.sh" "$1/sws" "$top" + chmod 644 "$1/sws/"* +} s=$work/stage/reiwimsy/usr/local install -d "$s/libexec" "$s/etc/rc.d" install -m 755 "$work/rei/reiwimsy" "$s/libexec/reiwimsy" install -m 555 "$here/reiwimsy.rc" "$s/etc/rc.d/reiwimsy" install -m 644 "$here/reiwimsy.env.sample" "$s/etc/reiwimsy.env.sample" -# an assignment: a dependency missing stops the script, which an argument's $(...) would not -rdeps=$(deps curl ca_root_nss) -package reiwimsy "ScarletNX's Discord bot: links issues and pull requests, keeps cards" net-im/reiwimsy "$rdeps" +site_files "$s/share/reiwimsy/tracer" +# an assignment: a dependency missing stops the script, which an argument's $(...) would not; libarchive is base's +rdeps=$(deps curl ca_root_nss sqlite3) +package reiwimsy "ScarletNX's Discord bots: ReiWimsy's links and cards, and Tracer's game reports" net-im/reiwimsy \ + "$rdeps" s=$work/stage/accordwimsy/usr/local install -d "$s/bin" "$s/etc/accordwimsy" "$s/share/examples/accordwimsy" @@ -143,11 +154,7 @@ install -m 755 "$work/tracer/tracer" "$work/tracer/tracer-mcp" "$s/libexec/" install -m 555 "$here/tracer.rc" "$s/etc/rc.d/tracer" install -m 555 "$here/tracer-mcp.rc" "$s/etc/rc.d/tracer_mcp" install -m 644 "$here/tracer.env.sample" "$s/etc/tracer.env.sample" -install -m 644 "$work/tracer/site/style.css" "$work/tracer/site/site.js" "$work/tracer/site/tracer.svg" \ - "$s/share/tracer/" -# the stylesheet in each theme and each theme's Wimsy, which the site's nginx gives by the reader's pick -sh "$top/scripts/tracer-themes.sh" "$s/share/tracer/sws" "$top" -chmod 644 "$s/share/tracer/sws/"* +site_files "$s/share/tracer" install -m 644 "$here/tracer-nginx.conf" "$s/share/examples/tracer/nginx.conf" # libarchive is base's tdeps=$(deps curl ca_root_nss sqlite3 libmicrohttpd) diff --git a/dist/freebsd/reiwimsy.desc b/dist/freebsd/reiwimsy.desc index 69b4238..267bc73 100644 --- a/dist/freebsd/reiwimsy.desc +++ b/dist/freebsd/reiwimsy.desc @@ -2,3 +2,6 @@ ReiWimsy answers a Discord message that names an issue or a pull request of a Gi repository with a link to each, its title, state and labels, and keeps a card on Discord for each open pull request, each open issue and each nightly build, edited as GitHub changes. It runs as the service reiwimsy, its settings in /usr/local/etc/reiwimsy.env. +With TRACER_TOKEN in its env file it runs Tracer beside it, in one process and as a bot +of its own: the ScarletNX game reports people send on Discord, kept and drawn as a static +site (Tracer's package holds tracer-mcp, and Tracer alone). diff --git a/dist/freebsd/reiwimsy.env.sample b/dist/freebsd/reiwimsy.env.sample index 501cd8d..e629711 100644 --- a/dist/freebsd/reiwimsy.env.sample +++ b/dist/freebsd/reiwimsy.env.sample @@ -8,3 +8,8 @@ REIWIMSY_REPO=scarletnx/ScarletNX # REIWIMSY_CARDS_NIGHTLY=id the channel of the nightlies' cards # REIWIMSY_CHANNELS=id,id the channels its replies answer in (all it sees without it) # REIWIMSY_NIGHTLY=OWNER/NAME where the nightlies are when not OWNER/nightly +# Tracer beside ReiWimsy, in this process and as a bot of its own: its keys (Tracer's page, Settings), read only with +# TRACER_TOKEN; the service gives it TRACER_DB, TRACER_SITE and TRACER_ASSETS from rc.conf (reiwimsy_tracer_db, +# reiwimsy_tracer_site, reiwimsy_tracer_assets) +# TRACER_TOKEN=... Tracer's Discord bot's token, another application's than ReiWimsy's; then +# TRACER_URL and its other keys, as tracer.env.sample lists them diff --git a/dist/freebsd/reiwimsy.plist b/dist/freebsd/reiwimsy.plist index 5b3babb..2de1a06 100644 --- a/dist/freebsd/reiwimsy.plist +++ b/dist/freebsd/reiwimsy.plist @@ -6,3 +6,20 @@ libexec/reiwimsy etc/rc.d/reiwimsy @mode 644 etc/reiwimsy.env.sample +share/reiwimsy/tracer/style.css +share/reiwimsy/tracer/site.js +share/reiwimsy/tracer/tracer.svg +share/reiwimsy/tracer/sws/sepia-auto.css +share/reiwimsy/tracer/sws/sepia-light.css +share/reiwimsy/tracer/sws/sepia-dark.css +share/reiwimsy/tracer/sws/wimsy-auto.css +share/reiwimsy/tracer/sws/wimsy-light.css +share/reiwimsy/tracer/sws/wimsy-dark.css +share/reiwimsy/tracer/sws/fuddles-auto.css +share/reiwimsy/tracer/sws/fuddles-light.css +share/reiwimsy/tracer/sws/fuddles-dark.css +share/reiwimsy/tracer/sws/default.css +share/reiwimsy/tracer/sws/logo-sepia.svg +share/reiwimsy/tracer/sws/logo-wimsy.svg +share/reiwimsy/tracer/sws/logo-fuddles.svg +share/reiwimsy/tracer/sws/logo-default.svg diff --git a/dist/freebsd/reiwimsy.rc b/dist/freebsd/reiwimsy.rc index 36aa338..cbd7107 100644 --- a/dist/freebsd/reiwimsy.rc +++ b/dist/freebsd/reiwimsy.rc @@ -5,7 +5,10 @@ # # The ReiWimsy Discord bot, run by daemon(8) as the reiwimsy user, started again when it stops; its output in syslog. # Its tokens and channels are in reiwimsy_env_file (root's, mode 600), which rc.subr exports to it; what it keeps of -# its replies and its cards, in reiwimsy_state (its own folder, which only it reads). +# its replies and its cards, in reiwimsy_state (its own folder, which only it reads). With TRACER_TOKEN in that file it +# runs Tracer beside it: Tracer's database in reiwimsy_tracer_db's folder, which only it reads, the site it draws in +# reiwimsy_tracer_site, which anyone may read (a web server's jail mounts it read-only), its files from +# reiwimsy_tracer_assets. # # service reiwimsy fixcards says what reiwimsy --fix-cards would do of the nightly channel's cards that still # say "Building", as the reiwimsy user, with the env file; changes nothing @@ -20,6 +23,9 @@ load_rc_config $name : ${reiwimsy_enable:=NO} : ${reiwimsy_env_file:=/usr/local/etc/reiwimsy.env} : ${reiwimsy_state:=/var/db/reiwimsy/state.json} +: ${reiwimsy_tracer_db:=/var/db/tracer/tracer.db} +: ${reiwimsy_tracer_site:=/var/db/tracer-site} +: ${reiwimsy_tracer_assets:=/usr/local/share/reiwimsy/tracer} pidfile=/var/run/${name}.pid command=/usr/sbin/daemon @@ -37,6 +43,12 @@ reiwimsy_precmd() fi install -d -o reiwimsy -g reiwimsy -m 700 "$(dirname "${reiwimsy_state}")" export REIWIMSY_STATE="${reiwimsy_state}" + if grep -q '^TRACER_TOKEN=.' "${reiwimsy_env_file}"; then + install -d -o reiwimsy -g reiwimsy -m 700 "$(dirname "${reiwimsy_tracer_db}")" + install -d -o reiwimsy -g reiwimsy -m 755 "${reiwimsy_tracer_site}" + export TRACER_DB="${reiwimsy_tracer_db}" TRACER_SITE="${reiwimsy_tracer_site}" \ + TRACER_ASSETS="${reiwimsy_tracer_assets}" + fi } reiwimsy_fixcards() diff --git a/docs/BUILD.md b/docs/BUILD.md index 3e2012b..6693670 100644 --- a/docs/BUILD.md +++ b/docs/BUILD.md @@ -37,10 +37,14 @@ installed into a folder of its own. GNU make builds it (`gmake`, which is `make` ## ReiWimsy - make -C rei CONCORD="$HOME/concord" # rei/reiwimsy + make -C rei CONCORD="$HOME/concord" # rei/reiwimsy, with Tracer (tracer/libtracer.a) beside it make -C rei test # its tests: no Discord, no network, no Concord make -C rei test-gateway CONCORD="$HOME/concord" # reiwimsy against a fake Discord: Linux (Testing) +`reiwimsy` runs [Tracer](./REIWIMSY.md#tracer-beside-it) too: its make builds `make -C tracer libtracer.a`, the bot +without ReiWimsy's pieces, and links it, with SQLite, libarchive, zlib and the C++ runtime (`CXXRT=-lc++` on FreeBSD, +as for Tracer). Its tests need none of them. + ## Tracer Concord as above, then: diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index 9a3221f..0db9958 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -9,10 +9,10 @@ CI runs. Building: [Build](./BUILD.md); the tests: [Testing](./TESTING.md). | Folder | What it holds | |---|---| -| `rei/src/` | ReiWimsy: `main.c` (its settings, the gateway and its threads), `event.c` (the gateway's events, from their JSON), `watch.c` (the gateway's watch), `refs.c` (what a message asks for), `link.c` (the replies), `cards.c` (the cards), `look.c` (how an issue or a pull request reads), `github.c` (GitHub's REST and GraphQL APIs), `rest.c` (Discord's REST API), `http.c` (libcurl), `state.c` (the state file), `util.c` (strings, cJSON's lookups, the log, the clock) | +| `rei/src/` | ReiWimsy: `main.c` (the program: ReiWimsy, and Tracer beside it with `TRACER_TOKEN`), `bot.c` (ReiWimsy's settings, its gateway and its threads), `event.c` (the gateway's events, from their JSON), `watch.c` (the gateway's watch), `refs.c` (what a message asks for), `link.c` (the replies), `cards.c` (the cards), `look.c` (how an issue or a pull request reads), `github.c` (GitHub's REST and GraphQL APIs), `rest.c` (Discord's REST API), `http.c` (libcurl), `state.c` (the state file), `util.c` (strings, cJSON's lookups, the log, the clock) | | `rei/tests/` | `test_rei.c`; `test_gateway.sh` and `fake_discord.py`, the bots against a fake Discord | | `rei/third_party/cjson/` | cJSON 1.7.19, as its release has it, and the files' `SHA256SUMS` | -| `tracer/src/` | Tracer: `main.c` (its settings, the gateway), `message.c` (what a message asks), `report.c` (what it does with one), `files.c` (logs, pictures and videos), `worker.c` (the worker that reads them), `fetch.c` (the downloads), `store.c` (the SQLite database), `site.c` (the pages), `issues.c`, `age.c`, `titles.c`, `channels.c`; `mcp.c` and `mcp_main.c`, tracer-mcp | +| `tracer/src/` | Tracer: `main.c` (Tracer alone), `bot.c` (its settings, the gateway, the thread of its events), `follow.c` (the issues followed), `message.c` (what a message asks), `report.c` (what it does with one), `files.c` (logs, pictures and videos), `worker.c` (the worker that reads them), `fetch.c` (the downloads), `store.c` (the SQLite database), `site.c` (the pages), `issues.c`, `age.c`, `titles.c`, `channels.c`; `mcp.c` and `mcp_main.c`, tracer-mcp | | `tracer/site/` | the site's `style.css`, `site.js` and `tracer.svg` | | `tracer/third_party/logscan/` | ScarletNX's log summary, its commit in `COMMIT` | | `tracer/tests/` | `test_tracer.c` | @@ -23,7 +23,9 @@ CI runs. Building: [Build](./BUILD.md); the tests: [Testing](./TESTING.md). | `scripts/` | `install.sh`, `make-titles.py`, `sync-logscan.sh`, `check-docs.py`, and the tests of the first and the last | Tracer builds ReiWimsy's `util.c`, `http.c`, `rest.c` and `watch.c` again as its own, and AccordWimsy builds -ReiWimsy's cJSON: a change there is a change to all of them, and every program's tests run it. +ReiWimsy's cJSON: a change there is a change to all of them, and every program's tests run it. `reiwimsy` links +Tracer's objects (`tracer/libtracer.a`, which leaves those out) and runs both bots: a log line names the bot its file +is built for (`REI_NAME`), and the gateway's watch follows each Concord client of the program. ## Rules diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 96c2c4f..241f55b 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -11,6 +11,7 @@ * [ReiWimsy's Discord application](#reiwimsys-discord-application) * [Tracer](#tracer) * [Tracer's Discord application](#tracers-discord-application) + * [Tracer beside ReiWimsy, in one process](#tracer-beside-reiwimsy-in-one-process) * [AccordWimsy](#accordwimsy) * [Upgrade and removal](#upgrade-and-removal) * [Another machine: what changes](#another-machine-what-changes) @@ -116,13 +117,12 @@ A channel id for `REIWIMSY_CHANNELS` and the card channels: Developer Mode, righ 3. Its Discord application ([below](#tracers-discord-application)) and its token. 4. The env file the package made in `tracer`, `/usr/local/etc/tracer.env` (root's, 600): `TRACER_TOKEN`, `TRACER_URL` - (the site's address), `TRACER_CHANNELS` (channels' ids, or `all` for every channel it can read; without it no - report is taken), and the other keys - [Tracer](./TRACER.md#settings) lists. For the issues, `TRACER_ISSUES_REPO` and `TRACER_ISSUES_TOKEN`: on GitHub, - Settings, Developer settings, Fine-grained tokens, the repository alone, the permissions Issues: Read and write, - Pull requests: Read and Contents: Read (the issues followed, a fix compared with the nightlies), nothing else; and - `TRACER_NOTIFY=ping` for a report's sender to be told when its fix is out. For the games' names, `TRACER_TITLES`, a file `python3 scripts/make-titles.py --out FILE` makes in - the jail. + (the site's address), `TRACER_CHANNELS` (channels' ids, or `all` for every channel it can read; without it no report + is taken), and the other keys [Tracer](./TRACER.md#settings) lists. For the issues, `TRACER_ISSUES_REPO` and + `TRACER_ISSUES_TOKEN`: on GitHub, Settings, Developer settings, Fine-grained tokens, the repository alone, the + permissions Issues: Read and write, Pull requests: Read and Contents: Read (the issues followed, a fix compared with + the nightlies), nothing else; and `TRACER_NOTIFY=ping` for a report's sender to be told when its fix is out. For the + games' names, `TRACER_TITLES`, a file `python3 scripts/make-titles.py --out FILE` makes in the jail. 5. On, and started: sysrc -j tracer tracer_enable=YES @@ -161,6 +161,32 @@ An id for `TRACER_CHANNELS` or `TRACER_ADMINS`: Developer Mode, right click, Cop channel's id takes its posts too. `TRACER_CHANNELS=all` takes reports in every channel and thread the bot can read, as its server's roles and channels' permissions let it. +### Tracer beside ReiWimsy, in one process + +`reiwimsy` runs Tracer too when its env file has `TRACER_TOKEN` ([ReiWimsy](./REIWIMSY.md#tracer-beside-it)): one +service in `discordbots`, two bots, each with its own application and token. Its package holds the site's files +(`/usr/local/share/reiwimsy/tracer`); the `tracer` package stays in `tracerweb`, for `tracer-mcp`. Moving a Tracer that +runs alone in its jail to it: + +1. Stopped, and kept: `service -j tracer tracer stop`, `sysrc -j tracer tracer_enable=NO`. +2. Its database and site into `discordbots`, the bot's user their owner: + + cp -a /usr/local/bastille/jails/tracer/root/var/db/tracer /usr/local/bastille/jails/discordbots/root/var/db/ + cp -a /usr/local/bastille/jails/tracer/root/var/db/tracer-site /usr/local/bastille/jails/discordbots/root/var/db/ + bastille cmd discordbots chown -R reiwimsy:reiwimsy /var/db/tracer /var/db/tracer-site + +3. Tracer's keys from `tracer.env` into `discordbots`' `/usr/local/etc/reiwimsy.env` (`TRACER_TOKEN`, `TRACER_URL`, + `TRACER_CHANNELS` and the others; `TRACER_TITLES` with its file copied too); the service sets `TRACER_DB`, + `TRACER_SITE` and `TRACER_ASSETS` (`reiwimsy_tracer_db`, `reiwimsy_tracer_site`, `reiwimsy_tracer_assets`). +4. The new package: `sh scripts/install.sh discordbots packages/reiwimsy-$(cat VERSION).pkg` (it brings sqlite3). +5. `tracerweb`'s mount on the site in `discordbots`, read-only as before: + + bastille umount tracerweb /usr/local/www/tracer + bastille mount tracerweb /usr/local/bastille/jails/discordbots/root/var/db/tracer-site /usr/local/www/tracer nullfs ro 0 0 + +6. `service -j discordbots reiwimsy restart`; its log says `Tracer: beside ReiWimsy`, both bots' `up as` lines and + Tracer's `disk:` and `follow:` lines; a report in a test channel, then the real one. + ## AccordWimsy It runs in the forge's `gitsync` jail, made with its package in [Forge](./FORGE.md#accordwimsys-jail); the steps below diff --git a/docs/REIWIMSY.md b/docs/REIWIMSY.md index 78e5e65..3eaea41 100644 --- a/docs/REIWIMSY.md +++ b/docs/REIWIMSY.md @@ -7,13 +7,15 @@ * [Replies](#replies) * [Cards](#cards) * [Cards left saying "Building"](#cards-left-saying-building) +* [Tracer beside it](#tracer-beside-it) * [Settings](#settings) * [What it keeps](#what-it-keeps) * [The service](#the-service) * [When it stops](#when-it-stops) `rei/`, in C99 with [Concord](https://github.com/Cogmasters/concord) 3.0.1, libcurl and -[cJSON](https://github.com/DaveGamble/cJSON) 1.7.19 (`rei/third_party/cjson`): ScarletNX's Discord bot. It answers a +[cJSON](https://github.com/DaveGamble/cJSON) 1.7.19 (`rei/third_party/cjson`), and Tracer's code beside it: +ScarletNX's Discord bot. It answers a message that names an issue or a pull request with a link to each, and keeps a card on Discord for each open pull request, each open issue and each push to main, edited as GitHub changes. Installing it: [Install](./INSTALL.md#reiwimsy). @@ -61,6 +63,20 @@ build published, failed or cancelled). Main's head, a card under an hour old, on older than the builds GitHub lists keep saying "Building", each said with why, and the last line counts each kind. A second run finds nothing to do. +## Tracer beside it + +With `TRACER_TOKEN` in its env file, `reiwimsy` runs [Tracer](./TRACER.md) in its own process: two bots, each with its +own Discord application, token, gateway and name, so each answers as itself, and one service, one env file and one +jail. Each bot's gateway is a Concord client of its own, Tracer's on a thread of its own; the first gateway that stops +ends the program with status 1, its line saying which bot's stopped, and daemon(8) starts both again. A message that +mentions Tracer is Tracer's alone: ReiWimsy gives it no reply, as Tracer's commands may hold a `#12` that is a report's +number (`@Tracer delete #12`), and it reads no number after `report`, `reports` or `Tracer` either. Tracer's keys, +`TRACER_*`, are [Tracer's](./TRACER.md#settings), in the same file; one without `TRACER_TOKEN` stops the program, +naming it, as a setting quietly not taken. Tracer's workers run `reiwimsy --report`, and their environment holds +neither bot's settings nor tokens ([Security](./SECURITY.md#tracers-workers)). The start's lines say whether Tracer +runs (`Tracer: beside ReiWimsy, with its own token; the messages that mention it (user N) are its own`, or `Tracer: +none, as TRACER_TOKEN is not set`); each bot's lines open with its name, `reiwimsy:` or `tracer:`. + ## Settings A line each of its env file, `/usr/local/etc/reiwimsy.env` (the package puts `dist/freebsd/reiwimsy.env.sample` there @@ -76,6 +92,7 @@ to fill in; an empty value stops the service): | `REIWIMSY_CARDS_PRS` | none: no pull request cards | the channel id of the pull requests' cards | | `REIWIMSY_CARDS_ISSUES` | none: no issue cards | the channel id of the issues' cards | | `REIWIMSY_CARDS_NIGHTLY` | none: no nightly cards | the channel id of the nightlies' cards | +| `TRACER_TOKEN` | none: no Tracer | Tracer's Discord bot's token, another application's: with it the program runs Tracer beside ReiWimsy, and reads Tracer's other keys ([Tracer beside it](#tracer-beside-it)) | | `REIWIMSY_STATE` | none: nothing kept between runs | the file its replies' and cards' ids are kept in; the service sets it from `reiwimsy_state` | A `REIWIMSY_` key it does not know stops it, naming the key; so does a channel id that is not one, and a card channel @@ -101,6 +118,9 @@ user `reiwimsy` and starts it again 30 s after it stops, its output in syslog (t | `reiwimsy_enable` | `NO` | `YES` starts it with the jail | | `reiwimsy_env_file` | `/usr/local/etc/reiwimsy.env` | its settings; root's, 600 | | `reiwimsy_state` | `/var/db/reiwimsy/state.json` | what it keeps, exported as `REIWIMSY_STATE` | +| `reiwimsy_tracer_db` | `/var/db/tracer/tracer.db` | Tracer's database, exported as `TRACER_DB` when the env file has `TRACER_TOKEN`; its folder (the user's, 700) holds the workers' `work/` | +| `reiwimsy_tracer_site` | `/var/db/tracer-site` | Tracer's site (the user's, 755: the web server's jail mounts it read-only), exported as `TRACER_SITE` | +| `reiwimsy_tracer_assets` | `/usr/local/share/reiwimsy/tracer` | the site's files the package holds, exported as `TRACER_ASSETS` | service -j discordbots reiwimsy start|stop|restart|status service -j discordbots reiwimsy fixcards [apply] diff --git a/docs/SECURITY.md b/docs/SECURITY.md index c1c2b2c..5c9b156 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -13,7 +13,7 @@ None is in this repository; each is made or put in place on the host. `.gitignor | Secret | Where | |---|---| | ReiWimsy's Discord token, its GitHub token | `discordbots`: `/usr/local/etc/reiwimsy.env` (root's, 600) | -| Tracer's Discord token, its issues token | `tracer`: `/usr/local/etc/tracer.env` (root's, 600) | +| Tracer's Discord token, its issues token | `tracer`: `/usr/local/etc/tracer.env` (root's, 600); or, Tracer beside ReiWimsy, `discordbots`: `/usr/local/etc/reiwimsy.env` | | AccordWimsy's Forgejo token, its GitHub token | `gitsync`: `/usr/local/etc/accordwimsy/accordwimsy.env` (its user's, 600) | | Forgejo's `SECRET_KEY`, `INTERNAL_TOKEN`, `JWT_SECRET` | `gitscarlet`: `app.ini`, made in place, never leaving the jail | | Each tunnel's connector token | the file `--token-file` names, in `gitedge`, `tracerweb` and `status` | @@ -41,8 +41,10 @@ asks for no privileged intent, and the reactions only when it files issues ## What each jail reaches -Each program runs in a jail of its own, as its own user ([Install](./INSTALL.md#the-jails)). The bots listen on no -port. Tracer's bot writes its site into a folder that the web jail, `tracerweb`, mounts read-only: the jail that +Each program runs in a jail of its own, as its own user ([Install](./INSTALL.md#the-jails)); Tracer beside ReiWimsy +runs in ReiWimsy's process and jail, its workers keeping both bots' tokens away +([ReiWimsy](./REIWIMSY.md#tracer-beside-it)). The bots listen on no port. Tracer's bot writes its site into a folder +that the web jail, `tracerweb`, mounts read-only: the jail that answers the internet holds no token and cannot change the site. The forge's three jails are isolated: the one that holds the GitHub token (`gitsync`) answers no one, the one that answers the internet (`gitedge`) holds no token, and Forgejo (`gitscarlet`) reaches nothing but DNS; a guard stops the three at the first process, socket or file it never diff --git a/docs/TESTING.md b/docs/TESTING.md index 0c7842a..b7d6af1 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -131,13 +131,13 @@ temporary folder: `python3 scripts/test-mutants.py` then changes Tracer's code one way at a time in a copy of the tree, and expects its tests to fail on each: `tracer/src/rules.c` (a threshold one off, a comparison that lets one more in, a condition left out: 19 mutants, `test_tracer --rules`); what a report keeps of its files, in `files.c`, `report.c` and `issues.c` (a -zip's screenshots, a picture never read as a log, the paths outside a home, the limits said with their values, what -an issue may hold of the game: 33 mutants, `test_tracer --files`); what a log holds of a run, in `probe.c` (Amberlith's starting note, a name that only -begins like it, libc's detail kept, a worker's answer naming another program: 4 mutants, `test_tracer --probe`); and -the worker's locale for a zip's names, made before it shuts itself in, who links a report to an issue, and when a -report's sender is told its fix is out, an approval of a failure an issue has, and a worker's environment (7 -mutants, the whole suite). A mutant they pass, -or one whose text its file no longer has once, fails it. About 30 s. +zip's screenshots, a picture never read as a log, the paths outside a home, the limits said with their values, what an +issue may hold of the game: 33 mutants, `test_tracer --files`); what a log holds of a run, in `probe.c` (Amberlith's +starting note, a name that only begins like it, libc's detail kept, a worker's answer naming another program: 4 mutants, +`test_tracer --probe`); and the worker's locale for a zip's names, made before it shuts itself in, who links a report to +an issue, and when a report's sender is told its fix is out, an approval of a failure an issue has, and a worker's +environment (7 mutants, the whole suite). A mutant they pass, or one whose text its file no longer has once, fails it. +About 30 s. `make -C tracer bench [RUNS=1000] [FOLDER=/tmp/tracer-bench]` (`tracer/tests/bench_probe.c`, built by `make test`, run by hand) keeps RUNS runs like edenprobe 0.9.0's graphics run (1069 cases) on fifteen machines in a new database in @@ -194,6 +194,10 @@ In a job of its own, on each pull request and each push to main, the bots built forever: the bot has to end with status 1 on its own, saying its session was last seen ready longer ago than three heartbeat intervals. `stalled` (ReiWimsy) the same, once the first connection is left each `GET /gateway/bot` held with no answer, which Concord also waits on forever. +- `both`: `reiwimsy` with `TRACER_TOKEN`, Tracer beside ReiWimsy in one process. The fake knows two tokens, each its + own user in `users/@me` and in its gateway's READY; once both are ready, each gateway brings `<@200> delete #12`, + which mentions Tracer, and `see #12`. Tracer has to answer the first and ReiWimsy the second, each with its own token, + and ReiWimsy never the first; then the gateways close under both and the program ends with status 1. - `reiwimsy --fix-cards` on a nightly channel holding two cards that say "Building": its dry run changes nothing, `--apply` deletes the one of a commit inside a push and redraws the push's head with its failed build, and a second `--apply` finds nothing to do. diff --git a/docs/TRACER.md b/docs/TRACER.md index 8caf1e1..20337d8 100644 --- a/docs/TRACER.md +++ b/docs/TRACER.md @@ -24,7 +24,9 @@ `tracer/`, in C99 with [Concord](https://github.com/Cogmasters/concord) 3.0.1, libcurl, SQLite, libarchive, cJSON (ReiWimsy's copy) and ScarletNX's own log summary (`tracer/third_party/logscan`, the runtime's `--scan-log`). It keeps -the game reports that people send it on Discord and draws them as a static site, grouped by game. Its MCP server, +the game reports that people send it on Discord and draws them as a static site, grouped by game. It runs alone, as +`tracer`, or beside ReiWimsy in `reiwimsy`'s process, as a bot of its own ([ReiWimsy](./REIWIMSY.md#tracer-beside-it)), +where its settings are the same keys in `reiwimsy.env`. Its MCP server, which gives the same reports to agents, has a page of its own: [tracer-mcp](./TRACER-MCP.md). Installing both: [Install](./INSTALL.md#tracer). From aa13311548a99d85ccba3a0bdcba28f2b73188e8 Mon Sep 17 00:00:00 2001 From: AO55 Date: Tue, 17 Nov 2026 13:11:04 -0300 Subject: [PATCH 6/7] [tracer] Its objects are built again when rei.h changes rei_log became a macro of rei.h, and Tracer's Makefile named rei.h for rei_util.o alone: an object built before linked against a function that is no more. Every object of Tracer's now follows rei.h. Signed-off-by: AO55 --- tracer/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tracer/Makefile b/tracer/Makefile index a20483a..d024fd9 100644 --- a/tracer/Makefile +++ b/tracer/Makefile @@ -111,6 +111,9 @@ src/mcp_main.o: src/mcp_main.c src/mcp.h src/tracer.h $(CC) $(CFLAGS) $(OURS) $(MHD_CFLAGS) -c -o $@ src/mcp_main.c src/fetch.o: src/fetch.c src/fetch.h src/tracer.h $(CC) $(CFLAGS) $(OURS) $(CURL_CFLAGS) -c -o $@ src/fetch.c +# every object of Tracer's reads ReiWimsy's rei.h, through tracer.h or itself: a change there (rei_log is a macro) has +# them built again +$(LOGIC) src/rei_http.o src/rei_rest.o src/rei_watch.o src/fetch.o src/bot.o src/main.o src/mcp_main.o: $(REI)/src/rei.h # Concord's headers are not ours: their warnings are theirs src/bot.o: src/bot.c src/bot.h $(LOGSCAN_FILES) src/age.h src/channels.h src/follow.h src/issues.h src/worker.h \ src/fetch.h src/report.h src/site.h src/store.h src/probe.h src/message.h src/tracer.h $(REI)/src/watch.h From 57b8190df368fcee94f545c0b2cc3b0cdae9e24f Mon Sep 17 00:00:00 2001 From: AO55 Date: Wed, 18 Nov 2026 12:51:12 -0300 Subject: [PATCH 7/7] [status] Tracer is up as ReiWimsy is, once its token is in ReiWimsy's env Tracer runs inside reiwimsy now; the tracer jail's own service stays off, so the page showed it down. Signed-off-by: AO55 --- dist/status/check.sh | 2 +- dist/status/push.sh | 18 +++++++++++++++--- dist/status/push.sh.in | 18 +++++++++++++++--- docs/STATUS.md | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/dist/status/check.sh b/dist/status/check.sh index c134d8a..53f5b31 100755 --- a/dist/status/check.sh +++ b/dist/status/check.sh @@ -41,7 +41,7 @@ for e in c["external-endpoints"]: if e["group"] == "Bots": assert f'span[title="{e["name"]}"]::before{{background-image:url(data:image/png;base64,' in css, e["name"] key = e["group"].lower() + "_" + re.sub(r"[ /_,.#+&]", "-", e["name"]).lower() - assert re.search(rf"^\s*(bot|push|site) {key} ", push, re.M), ("push.sh never pushes", key) + assert re.search(rf"^\s*(bot|push|site|tracer) {key}(\s|$)", push, re.M), ("push.sh never pushes", key) print(len(bots), "bots:", ", ".join(bots)) assert set(c["ui"]["favicon"]) == {"default", "size16x16", "size32x32"}, c["ui"]["favicon"] for e in c["endpoints"]: diff --git a/dist/status/push.sh b/dist/status/push.sh index 34b6bf2..4b42f97 100755 --- a/dist/status/push.sh +++ b/dist/status/push.sh @@ -2,8 +2,9 @@ # Every minute, from /usr/local/etc/cron.d/status-scarletnx on the host (as root): whether each # bot is up, pushed to status.scarletnx.dev's Gatus at http://192.168.24.1:8080 (its external endpoints, group # Bots). Gatus marks one down when no push came for 5 minutes, so this script stopping shows there too. -# ReiWimsy, Tracer: up when its rc variable in its jail (discordbots, tracer) says YES and its program -# has run there for a minute or more (a bot daemon(8) restarts every 30 s reads as down). +# ReiWimsy: up when reiwimsy_enable says YES in the jail discordbots and its program has run there for a +# minute or more (a bot daemon(8) restarts every 30 s reads as down). +# Tracer: it runs inside ReiWimsy when ReiWimsy's env file holds TRACER_TOKEN: up when it does and ReiWimsy is up. # AccordWimsy: up when `noirjail.sh jail fresh` passes (each mirror had a good pass within its time). # git.scarletnx.dev: up when forgejo_enable says YES in the jail gitscarlet and its forgejo has run a minute or # more, read from the host (its guard keeps everything else off that jail and its tunnel's). @@ -52,6 +53,17 @@ bot() { fi } +# Pushes Tracer : it runs inside ReiWimsy, so down when ReiWimsy's env file in the jail discordbots holds no +# TRACER_TOKEN, else as ReiWimsy is. +# Usage: tracer +tracer() { + if ! grep -q '^TRACER_TOKEN=.' "$jails/discordbots/root/usr/local/etc/reiwimsy.env" 2> /dev/null; then + push "$1" false "turned off: no TRACER_TOKEN in discordbots' reiwimsy.env" + return + fi + bot "$1" discordbots reiwimsy_enable /usr/local/libexec/reiwimsy +} + # Pushes site : up when the web jail tracerweb's nginx answers from inside the jail, else down with # fetch's words. # Usage: site @@ -67,7 +79,7 @@ if [ ! -r "$header" ]; then exit 78 fi bot bots_reiwimsy discordbots reiwimsy_enable /usr/local/libexec/reiwimsy -bot bots_tracer tracer tracer_enable /usr/local/libexec/tracer +tracer bots_tracer bot web_git-scarletnx-dev gitscarlet forgejo_enable "/usr/local/sbin/forgejo web" site web_issues-scarletnx-dev http://127.0.0.1:8080/ esc=$(printf '\033') diff --git a/dist/status/push.sh.in b/dist/status/push.sh.in index 37624b1..f60098d 100644 --- a/dist/status/push.sh.in +++ b/dist/status/push.sh.in @@ -2,8 +2,9 @@ # Every minute, from /usr/local/etc/cron.d/status-scarletnx on the host (as root): whether each # bot is up, pushed to status.scarletnx.dev's Gatus at @GATUS_PUSH_URL@ (its external endpoints, group # Bots). Gatus marks one down when no push came for 5 minutes, so this script stopping shows there too. -# ReiWimsy, Tracer: up when its rc variable in its jail (discordbots, tracer) says YES and its program -# has run there for a minute or more (a bot daemon(8) restarts every 30 s reads as down). +# ReiWimsy: up when reiwimsy_enable says YES in the jail discordbots and its program has run there for a +# minute or more (a bot daemon(8) restarts every 30 s reads as down). +# Tracer: it runs inside ReiWimsy when ReiWimsy's env file holds TRACER_TOKEN: up when it does and ReiWimsy is up. # AccordWimsy: up when `noirjail.sh jail fresh` passes (each mirror had a good pass within its time). # git.scarletnx.dev: up when forgejo_enable says YES in the jail gitscarlet and its forgejo has run a minute or # more, read from the host (its guard keeps everything else off that jail and its tunnel's). @@ -52,6 +53,17 @@ bot() { fi } +# Pushes Tracer : it runs inside ReiWimsy, so down when ReiWimsy's env file in the jail discordbots holds no +# TRACER_TOKEN, else as ReiWimsy is. +# Usage: tracer +tracer() { + if ! grep -q '^TRACER_TOKEN=.' "$jails/discordbots/root/usr/local/etc/reiwimsy.env" 2> /dev/null; then + push "$1" false "turned off: no TRACER_TOKEN in discordbots' reiwimsy.env" + return + fi + bot "$1" discordbots reiwimsy_enable /usr/local/libexec/reiwimsy +} + # Pushes site : up when the web jail tracerweb's nginx answers from inside the jail, else down with # fetch's words. # Usage: site @@ -67,7 +79,7 @@ if [ ! -r "$header" ]; then exit 78 fi bot bots_reiwimsy discordbots reiwimsy_enable /usr/local/libexec/reiwimsy -bot bots_tracer tracer tracer_enable /usr/local/libexec/tracer +tracer bots_tracer bot web_git-scarletnx-dev gitscarlet forgejo_enable "/usr/local/sbin/forgejo web" site web_issues-scarletnx-dev http://127.0.0.1:8080/ esc=$(printf '\033') diff --git a/docs/STATUS.md b/docs/STATUS.md index 6d00d05..98c8747 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -18,7 +18,7 @@ off its jails but a read of their files and processes from the host. | Discord invite | 30 min | Discord's API knows the invite the site links to | | ReiWimsy | pushed | `reiwimsy_enable` is YES in `discordbots` and `/usr/local/libexec/reiwimsy` has run a minute or more | | AccordWimsy | pushed | `noirjail.sh jail fresh` passes: each mirror had a good pass within its time | -| Tracer | pushed | `tracer_enable` is YES in `tracer` and `/usr/local/libexec/tracer` has run a minute or more | +| Tracer | pushed | ReiWimsy's env file in `discordbots` holds `TRACER_TOKEN` (Tracer runs inside ReiWimsy) and ReiWimsy is up | | git.scarletnx.dev | pushed | `forgejo_enable` is YES in `gitscarlet` and its `forgejo web` has run a minute or more, read from the host | | issues.scarletnx.dev | pushed | the web jail's nginx gives Tracer's first page (`jexec tracerweb fetch`) |