Approval was a prompt on stdin: the terminal could answer, a phone could not, and in the TUI the whole path was dead code that failed closed. Now a request is two files: ~/.splinter/ack/pending/<id> the request (tool, args, why, expiry) ~/.splinter/ack/<id>.answer the answer (yes/no plus who) The blocked process waits on the file and nothing else. Channels are notifiers - a command per channel pushes the request out - and whoever reads the reply drops the answer: the gateway, a phone, a script, another terminal. No second poller, no lost Telegram updates, no special case per channel. SPLINTER_ACK_CHANNELS=... comma separated commands, each gets the request SPLINTER_ACK_TTL=120 seconds; a timeout is a no splinter --ack <id> yes|no answer from anywhere splinter --ack-list what is waiting The terminal is one more channel, not a special case: a thread reads stdin and drops the answer, while the main path waits on the file like everybody else. Kid mode uses the same mechanism instead of its own command. Verified: tests/test_ack.c, 7 checks - request, yes, no, who answered, timeout is a no, answering a request that does not exist fails, and the pending list shows the arguments. make check runs it with the other 21.
253 lines
6.1 KiB
C
253 lines
6.1 KiB
C
/*
|
|
* splinter -- approvals that do not care who answers.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* A tool call that changes something needs a yes from a human. The human may be
|
|
* at the terminal, on Telegram, or behind a command that a script answers; there
|
|
* may be several of them and the first answer wins.
|
|
*
|
|
* The shape that makes that work without two processes fighting over the same
|
|
* Telegram updates:
|
|
*
|
|
* ~/.splinter/ack/pending/<id> the request, written by whoever is blocked
|
|
* ~/.splinter/ack/<id>.answer the answer, written by whoever knows it
|
|
*
|
|
* So the asking process only waits on a file. Channels are notifiers: they push
|
|
* the pending request out (a command per channel, e.g. a Telegram sendMessage) and
|
|
* whoever reads the reply - the gateway, a pager, a script - drops the answer file.
|
|
* No polling loop in two places, no lost updates, and the same mechanism works
|
|
* for the terminal, a phone and an ack button.
|
|
*
|
|
* A timeout is a no. Silence is not consent.
|
|
*/
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "ack.h"
|
|
|
|
static char ack_root[512];
|
|
|
|
static int
|
|
ensure_dir(const char *path)
|
|
{
|
|
if (mkdir(path, 0700) == 0)
|
|
return 0;
|
|
return errno == EEXIST ? 0 : -1;
|
|
}
|
|
|
|
const char *
|
|
sp_ack_dir(void)
|
|
{
|
|
const char *env = getenv("SPLINTER_ACK_DIR");
|
|
const char *home = getenv("HOME");
|
|
|
|
if (env != NULL && *env != '\0') {
|
|
snprintf(ack_root, sizeof(ack_root), "%s", env);
|
|
return ack_root;
|
|
}
|
|
if (home == NULL || *home == '\0')
|
|
return NULL;
|
|
snprintf(ack_root, sizeof(ack_root), "%s/.splinter/ack", home);
|
|
return ack_root;
|
|
}
|
|
|
|
static int
|
|
ack_id_path(char *buf, size_t len, int id, const char *suffix)
|
|
{
|
|
const char *root = sp_ack_dir();
|
|
char dir[600];
|
|
|
|
if (root == NULL)
|
|
return -1;
|
|
snprintf(dir, sizeof(dir), "%s/pending", root);
|
|
(void)ensure_dir(root);
|
|
(void)ensure_dir(dir);
|
|
snprintf(buf, len, "%s/%d%s", dir, id, suffix);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Write the request. The id is the pid and a counter, so two processes asking at
|
|
* the same time cannot take each other's answer.
|
|
*/
|
|
int
|
|
sp_ack_request(const char *tool, const char *args, const char *why, int ttl)
|
|
{
|
|
char path[700];
|
|
FILE *f;
|
|
int id = (int)getpid();
|
|
time_t now = time(NULL);
|
|
|
|
if (ack_id_path(path, sizeof(path), id, "") != 0)
|
|
return -1;
|
|
f = fopen(path, "w");
|
|
if (f == NULL)
|
|
return -1;
|
|
fprintf(f, "id=%d\n", id);
|
|
fprintf(f, "tool=%s\n", tool != NULL ? tool : "");
|
|
fprintf(f, "args=%s\n", args != NULL ? args : "");
|
|
fprintf(f, "why=%s\n", why != NULL ? why : "");
|
|
fprintf(f, "pid=%d\n", (int)getpid());
|
|
fprintf(f, "expires=%ld\n", (long)now + (ttl > 0 ? ttl : 120));
|
|
fclose(f);
|
|
return id;
|
|
}
|
|
|
|
/*
|
|
* Fire every configured channel. Each entry is a command; the request is appended
|
|
* as one argument. A channel that fails is not fatal - another one may reach a
|
|
* human - but the failure is named.
|
|
*/
|
|
int
|
|
sp_ack_notify(int id, struct sp_buf *log)
|
|
{
|
|
const char *chans = getenv("SPLINTER_ACK_CHANNELS");
|
|
char list[512], *save = NULL, *tok;
|
|
int fired = 0;
|
|
|
|
if (chans == NULL || *chans == '\0')
|
|
return 0;
|
|
snprintf(list, sizeof(list), "%s", chans);
|
|
for (tok = strtok_r(list, ",", &save); tok != NULL;
|
|
tok = strtok_r(NULL, ",", &save)) {
|
|
char cmd[1400];
|
|
int rc;
|
|
|
|
while (*tok == ' ')
|
|
tok++;
|
|
if (*tok == '\0')
|
|
continue;
|
|
snprintf(cmd, sizeof(cmd), "%s 'Zustimmung %d'", tok, id);
|
|
rc = system(cmd);
|
|
if (rc == 0)
|
|
fired++;
|
|
else if (log != NULL)
|
|
sp_buf_printf(log, "ack: channel '%s' failed (%d)\n", tok,
|
|
rc);
|
|
}
|
|
return fired;
|
|
}
|
|
|
|
/*
|
|
* Wait for the answer. Returns 1 yes, 0 no, -1 nothing yet. 'who' says where the
|
|
* answer came from, which is worth recording when a child asks and an adult
|
|
* answers from a phone.
|
|
*/
|
|
int
|
|
sp_ack_wait(int id, int ttl, char *who, size_t who_len)
|
|
{
|
|
char path[700];
|
|
time_t end = time(NULL) + (ttl > 0 ? ttl : 120);
|
|
|
|
for (;;) {
|
|
FILE *f;
|
|
char line[256];
|
|
|
|
if (ack_id_path(path, sizeof(path), id, ".answer") == 0 &&
|
|
(f = fopen(path, "r")) != NULL) {
|
|
int ok = 0;
|
|
|
|
if (fgets(line, sizeof(line), f) != NULL) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
ok = strncmp(line, "yes", 3) == 0;
|
|
if (who != NULL) {
|
|
char *sp = strchr(line, ' ');
|
|
|
|
snprintf(who, who_len, "%s",
|
|
sp != NULL ? sp + 1 : "unknown");
|
|
}
|
|
}
|
|
fclose(f);
|
|
(void)unlink(path);
|
|
return ok;
|
|
}
|
|
if (time(NULL) >= end)
|
|
return 0; /* silence is not consent */
|
|
{
|
|
struct timespec ts = { 0, 200 * 1000 * 1000 };
|
|
|
|
(void)nanosleep(&ts, NULL);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Any process may answer: the gateway, a helper, a script. */
|
|
int
|
|
sp_ack_answer(int id, int yes, const char *who)
|
|
{
|
|
char path[700];
|
|
FILE *f;
|
|
|
|
if (ack_id_path(path, sizeof(path), id, "") != 0)
|
|
return -1;
|
|
{
|
|
FILE *req = fopen(path, "r");
|
|
|
|
if (req == NULL)
|
|
return -1; /* no such request */
|
|
fclose(req);
|
|
}
|
|
if (ack_id_path(path, sizeof(path), id, ".answer") != 0)
|
|
return -1;
|
|
f = fopen(path, "w");
|
|
if (f == NULL)
|
|
return -1;
|
|
fprintf(f, "%s %s\n", yes ? "yes" : "no",
|
|
who != NULL && *who != '\0' ? who : "unknown");
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
sp_ack_list(struct sp_buf *out)
|
|
{
|
|
const char *root = sp_ack_dir();
|
|
char dir[600];
|
|
struct dirent **ents;
|
|
int n, i;
|
|
|
|
if (root == NULL)
|
|
return -1;
|
|
snprintf(dir, sizeof(dir), "%s/pending", root);
|
|
n = scandir(dir, &ents, NULL, alphasort);
|
|
if (n < 0) {
|
|
sp_buf_puts(out, "keine offenen Anfragen\n");
|
|
return 0;
|
|
}
|
|
for (i = 0; i < n; i++) {
|
|
char path[1024], line[256];
|
|
FILE *f;
|
|
|
|
if (ents[i]->d_name[0] == '.') {
|
|
free(ents[i]);
|
|
continue;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/%s", dir, ents[i]->d_name);
|
|
sp_buf_printf(out, "%s ", ents[i]->d_name);
|
|
if ((f = fopen(path, "r")) != NULL) {
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
|
if (strncmp(line, "args=", 5) == 0 ||
|
|
strncmp(line, "expires=", 8) == 0) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
sp_buf_printf(out, "%s ", line);
|
|
}
|
|
}
|
|
fclose(f);
|
|
}
|
|
sp_buf_puts(out, "\n");
|
|
free(ents[i]);
|
|
}
|
|
free(ents);
|
|
return 0;
|
|
}
|