Files

86 lines
4.3 KiB
PHP

<?php
// ═══════════════════════════════════════════════════════════════════════════════════════════════
// PURPOSE
// Accept one mesh message from a partner and store it. Invoked over SSH by the sending node's
// vv_nc_deliver(), with the record on stdin.
//
// OPERATIONAL MODEL
// Exit 0 means stored. The sender treats anything else as undelivered and spools for retry, so
// a non-zero exit here is a message that will arrive later rather than one that is lost.
//
// Two jobs in one file because they are two halves of the same contract: --channel takes what
// a partner is delivering now, --flush pushes what this host failed to deliver earlier. A node
// is both a receiver and a sender, and splitting them would mean two files that must agree on
// the same spool layout.
//
// DESIGN PRINCIPLES
// The exit code is the receipt, and it is the only one.
// There is no acknowledgement message and no reply body. The sender is an SSH command that
// already has an exit status, so inventing a second channel to say the same thing would
// give the two ways to disagree.
//
// Refusing is cheaper than being wrong.
// An unparseable record, an unknown channel or a failed write all exit non-zero and leave
// the message in the sender's spool. A retry costs one SSH round trip; a message accepted
// and dropped is gone with nothing recording that it existed.
//
// OPERATIONAL SAFEGUARDS
// Reached only over SSH with a key this mesh installed, so the caller already has root. This
// file therefore validates shape, not authority — there is no privilege here to protect that
// the transport has not already granted.
//
// The channel is resolved against this host's own membership. A name that is not a channel
// this machine belongs to is refused, so the argument cannot address a path outside the store.
//
// Storage is append-and-trim through vv_nc_append(), which is idempotent on message id — a
// retry of something that already landed is a no-op rather than a duplicate.
//
// RUNTIME MODES
// Not invoked by hand — the sending node's vv_nc_deliver() runs it over SSH.
//
// php node_chat_receive.php --channel=<id> store one JSON record, read from stdin
// php node_chat_receive.php --flush retry this host's own undelivered spool
//
// DEPENDS ON
// include/node_chat.php vv_nc_append(), vv_nc_channel(), vv_nc_flush_spool()
// ═══════════════════════════════════════════════════════════════════════════════════════════════
require_once dirname(__DIR__) . '/include/node_chat.php';
$args = $argv ?? [];
if (in_array('--flush', $args, true)) {
$n = vv_nc_flush_spool();
if ($n) echo "delivered $n queued message(s)\n";
exit(0);
}
$channel = '';
foreach ($args as $a) {
if (str_starts_with($a, '--channel=')) $channel = substr($a, 10);
}
if ($channel === '' || !vv_nc_channel($channel)) {
fwrite(STDERR, "unknown channel\n");
exit(2);
}
$raw = stream_get_contents(STDIN);
$msg = json_decode((string)$raw, true);
if (!is_array($msg) || empty($msg['id']) || !isset($msg['text'])) {
fwrite(STDERR, "malformed message\n");
exit(2);
}
// Rebuilt rather than stored as sent: a record is only ever the fields this store understands,
// so a sender running newer code cannot write keys this one will later hand to a page.
$clean = [
'id' => preg_replace('/[^a-f0-9]/i', '', (string)$msg['id']),
'ts' => (int)($msg['ts'] ?? time()),
'from' => preg_replace('/[^a-z0-9]/i', '', strtolower((string)($msg['from'] ?? ''))),
'kind' => in_array($msg['kind'] ?? 'msg', ['msg', 'question', 'notice'], true) ? $msg['kind'] : 'msg',
'text' => mb_substr((string)$msg['text'], 0, 4000),
'style' => vv_nc_clean_style(is_array($msg['style'] ?? null) ? $msg['style'] : []),
];
if ($clean['id'] === '' || $clean['from'] === '') { fwrite(STDERR, "malformed message\n"); exit(2); }
exit(vv_nc_append($channel, $clean) ? 0 : 1);