mailu: full nox module — client, tools and events (ADR 0044/0046)
Email server. Users/aliases/domains over the admin REST API, mail read via doveadm in the imap container; ten tools. Emits user/alias created/deleted by polling and diffing the admin API, so a change in the web admin is announced as readily as one via a tool. Consumes nothing — deliberately, since mutating mail accounts off another module's event could silently lose mail. Typechecks against the sdk; manifest parses. Claude-Session: https://claude.ai/code/session_01LrgweAeERJYBg88c5cKDzF
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// mailu's events. The tool runtime imports this once the broker is bound. It watches the mail
|
||||
// server's accounts and announces what changed, so the rest of the mesh can react to a mailbox
|
||||
// appearing or an alias being pointed somewhere new.
|
||||
//
|
||||
// Emits (novox/hq ADR 0046/0047):
|
||||
// module.mailu.user.created / .deleted — a mailbox appeared or was removed
|
||||
// module.mailu.alias.created / .deleted — an alias was added or removed
|
||||
//
|
||||
// Consumes: nothing. Mail accounts are not something the mesh should mutate in the background off
|
||||
// another module's event — a wrong reaction here silently loses mail. mailu observes and announces;
|
||||
// it does not act on what others do. If a real consumer is ever wanted, it is a deliberate addition.
|
||||
//
|
||||
// Detection is by polling the admin API and diffing, exactly as plex diffs its sessions: the change
|
||||
// may have come from the web admin as easily as from a tool, and a diff catches both. The first
|
||||
// look primes silently, or a restart would re-announce every existing account as freshly created.
|
||||
|
||||
import { emit } from "@novox/mesh-sdk/events";
|
||||
import { MailuClient } from "./client.js";
|
||||
|
||||
const mailu = MailuClient.fromEnv();
|
||||
|
||||
// A generic diff over a keyed set: emit `created` for keys that appeared, `deleted` for keys that
|
||||
// went away, and stay silent until primed. Users and aliases are the same shape of watch.
|
||||
function watcher(created: string, deleted: string): (keys: string[]) => Promise<void> {
|
||||
const known = new Set<string>();
|
||||
let primed = false;
|
||||
return async (keys: string[]) => {
|
||||
const now = new Set(keys);
|
||||
if (primed) {
|
||||
for (const key of now) if (!known.has(key)) await emit(created, { email: key });
|
||||
for (const key of known) if (!now.has(key)) await emit(deleted, { email: key });
|
||||
}
|
||||
known.clear();
|
||||
for (const key of now) known.add(key);
|
||||
primed = true;
|
||||
};
|
||||
}
|
||||
|
||||
const watchUsers = watcher("module.mailu.user.created", "module.mailu.user.deleted");
|
||||
const watchAliases = watcher("module.mailu.alias.created", "module.mailu.alias.deleted");
|
||||
|
||||
async function pollUsers(): Promise<void> {
|
||||
await watchUsers((await mailu.listUsers()).map((u) => u.email));
|
||||
}
|
||||
|
||||
async function pollAliases(): Promise<void> {
|
||||
await watchAliases((await mailu.listAliases()).map((a) => a.email));
|
||||
}
|
||||
|
||||
const tick = (fn: () => Promise<void>, everyMs: number): void => {
|
||||
const runOnce = (): void => void fn().catch((err) => console.error(`[mailu] ${err}`));
|
||||
setInterval(runOnce, everyMs);
|
||||
runOnce();
|
||||
};
|
||||
|
||||
// Accounts and aliases change on human time, not machine time — a minute's latency is fine, and
|
||||
// polling the admin API harder buys immediacy nobody asked for.
|
||||
tick(pollUsers, 60_000);
|
||||
tick(pollAliases, 60_000);
|
||||
|
||||
console.log("[mailu] watching users and aliases");
|
||||
Reference in New Issue
Block a user