@wtfalch/email 0.19.0 → 0.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/postmaster/objects.js +10 -2
- package/dist/postmaster/org-mail.d.ts +8 -2
- package/dist/postmaster/org-mail.js +45 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -657,7 +657,9 @@ await parkOrgMail(client, { slug: 'acme', domain: 'acme.com', mode: 'hosted' },
|
|
|
657
657
|
upserts an `MtaRoute` named `lettermint-<slug>` (`smtp.lettermint.co`, port 465,
|
|
658
658
|
implicit TLS, user `lettermint`, the token as a `Value` secret) and rebuilds
|
|
659
659
|
`MtaOutboundStrategy.route` so mail from `domain` leaves through it and every
|
|
660
|
-
other sender is delivered direct (route `'mx'`).
|
|
660
|
+
other sender is delivered direct (route `'mx'`). The expression always starts
|
|
661
|
+
with the rule that delivers a recipient on a hosted domain to its mailbox
|
|
662
|
+
(route `'local'`), whoever the sender is. `removeOrgRelay(client,
|
|
661
663
|
{ slug })` deletes the route and rebuilds; a missing route sends nothing.
|
|
662
664
|
`hasOrgRelay(client, { slug })` is true when the route holds a secret, so a
|
|
663
665
|
caller knows whether it needs a fresh token. The domain is kept in the route's
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LOCAL_ROUTE, outboundRoute, routeExpression } from './org-mail.js';
|
|
2
2
|
/** A value that names an object created earlier in the same plan. */
|
|
3
3
|
export const ref = (key) => ({ $ref: key });
|
|
4
4
|
const env = (variableName) => ({ '@type': 'EnvironmentVariable', variableName });
|
|
@@ -176,11 +176,19 @@ export function mailObjects(i) {
|
|
|
176
176
|
...(i.oidc ? { directoryId: ref('oidc') } : {}),
|
|
177
177
|
},
|
|
178
178
|
},
|
|
179
|
+
{
|
|
180
|
+
// The route expression names this route for every local recipient. If
|
|
181
|
+
// it is missing, Stalwart falls back to the MX lookup without an error.
|
|
182
|
+
key: 'local',
|
|
183
|
+
object: 'MtaRoute',
|
|
184
|
+
match: { name: LOCAL_ROUTE },
|
|
185
|
+
value: { '@type': 'Local', name: LOCAL_ROUTE },
|
|
186
|
+
},
|
|
179
187
|
{
|
|
180
188
|
key: 'outbound',
|
|
181
189
|
object: 'MtaOutboundStrategy',
|
|
182
190
|
match: { id: 'singleton' },
|
|
183
|
-
value: { route:
|
|
191
|
+
value: { route: routeExpression([]) },
|
|
184
192
|
},
|
|
185
193
|
{
|
|
186
194
|
key: 'system',
|
|
@@ -177,8 +177,14 @@ export interface OrgRelayInput extends OrgRelayOrg {
|
|
|
177
177
|
/** The Lettermint project token. Sent as `@type: Value` and never logged or thrown. */
|
|
178
178
|
readonly secret: string;
|
|
179
179
|
}
|
|
180
|
-
/**
|
|
181
|
-
export declare const
|
|
180
|
+
/** The `MtaRoute` that writes a message into this server's own mailboxes. */
|
|
181
|
+
export declare const LOCAL_ROUTE = "local";
|
|
182
|
+
/** The route expression for a set of relays: local delivery first, then each
|
|
183
|
+
* relay by sender domain, then direct delivery. */
|
|
184
|
+
export declare function routeExpression(relays: readonly {
|
|
185
|
+
domain: string;
|
|
186
|
+
name: string;
|
|
187
|
+
}[]): Record<string, unknown>;
|
|
182
188
|
/** The `MtaOutboundStrategy.route` expression for the relays that exist now.
|
|
183
189
|
* `applyObjects` uses it too, so a re-apply of the instance plan keeps every
|
|
184
190
|
* org's route instead of resetting the expression to the direct fallback. */
|
|
@@ -185,6 +185,7 @@ export async function applyOrgMail(client, org, options) {
|
|
|
185
185
|
else {
|
|
186
186
|
id = await client.create('Domain', value);
|
|
187
187
|
}
|
|
188
|
+
await repairRoute(client);
|
|
188
189
|
const [wire] = await client.get('Domain', [id]);
|
|
189
190
|
const records = [
|
|
190
191
|
...mxAndSpf(domain, org.mode, options),
|
|
@@ -210,7 +211,9 @@ export async function parkOrgMail(client, org, options) {
|
|
|
210
211
|
const RELAY_PREFIX = 'lettermint-';
|
|
211
212
|
const RELAY_DESCRIPTION = 'Lettermint relay for ';
|
|
212
213
|
/** Stalwart's built-in route for direct delivery by MX lookup. */
|
|
213
|
-
|
|
214
|
+
const FALLBACK_ROUTE = "'mx'";
|
|
215
|
+
/** The `MtaRoute` that writes a message into this server's own mailboxes. */
|
|
216
|
+
export const LOCAL_ROUTE = 'local';
|
|
214
217
|
function relayName(slug) {
|
|
215
218
|
assertLabel('an org slug', slug);
|
|
216
219
|
return `${RELAY_PREFIX}${slug}`;
|
|
@@ -243,6 +246,24 @@ function routeRule(domain, routeName) {
|
|
|
243
246
|
['then', `'${routeName}'`],
|
|
244
247
|
]);
|
|
245
248
|
}
|
|
249
|
+
/** The first rule of every route expression: a recipient on a domain this
|
|
250
|
+
* server hosts is delivered here. Without it the message follows the sender's
|
|
251
|
+
* relay or the MX lookup, and never reaches the mailbox. */
|
|
252
|
+
function localRule() {
|
|
253
|
+
return Object.fromEntries([
|
|
254
|
+
['if', 'is_local_domain(rcpt_domain)'],
|
|
255
|
+
['then', `'${LOCAL_ROUTE}'`],
|
|
256
|
+
]);
|
|
257
|
+
}
|
|
258
|
+
/** The route expression for a set of relays: local delivery first, then each
|
|
259
|
+
* relay by sender domain, then direct delivery. */
|
|
260
|
+
export function routeExpression(relays) {
|
|
261
|
+
const rules = [localRule(), ...relays.map((e) => routeRule(e.domain, e.name))];
|
|
262
|
+
return {
|
|
263
|
+
match: Object.fromEntries(rules.map((r, i) => [String(i), r])),
|
|
264
|
+
else: FALLBACK_ROUTE,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
246
267
|
/** The `MtaOutboundStrategy.route` expression for the relays that exist now.
|
|
247
268
|
* `applyObjects` uses it too, so a re-apply of the instance plan keeps every
|
|
248
269
|
* org's route instead of resetting the expression to the direct fallback. */
|
|
@@ -253,18 +274,35 @@ export async function outboundRoute(client) {
|
|
|
253
274
|
return domain && r.name ? [{ domain, name: r.name }] : [];
|
|
254
275
|
})
|
|
255
276
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
256
|
-
return entries
|
|
257
|
-
? { else: FALLBACK_ROUTE }
|
|
258
|
-
: {
|
|
259
|
-
match: Object.fromEntries(entries.map((e, i) => [String(i), routeRule(e.domain, e.name)])),
|
|
260
|
-
else: FALLBACK_ROUTE,
|
|
261
|
-
};
|
|
277
|
+
return routeExpression(entries);
|
|
262
278
|
}
|
|
263
279
|
async function rebuildRoute(client) {
|
|
280
|
+
// The expression names the local route. On a server without it Stalwart
|
|
281
|
+
// falls back to the MX lookup for its own recipients, without an error.
|
|
282
|
+
const routes = await client.list('MtaRoute');
|
|
283
|
+
if (!routes.some((r) => r.name === LOCAL_ROUTE)) {
|
|
284
|
+
await client.create('MtaRoute', { '@type': 'Local', name: LOCAL_ROUTE });
|
|
285
|
+
}
|
|
264
286
|
const route = await outboundRoute(client);
|
|
265
287
|
await client.update('MtaOutboundStrategy', 'singleton', { route });
|
|
266
288
|
await client.create('Action', { '@type': 'ReloadSettings' });
|
|
267
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Rewrite the route expression when it is not the one this version writes. A
|
|
292
|
+
* server last written by a version before 0.19.1 has no local-delivery rule,
|
|
293
|
+
* and nothing else rewrites the expression while every relay is healthy.
|
|
294
|
+
*/
|
|
295
|
+
async function repairRoute(client) {
|
|
296
|
+
const [strategy] = await client.get('MtaOutboundStrategy', [
|
|
297
|
+
'singleton',
|
|
298
|
+
]);
|
|
299
|
+
const routes = await client.list('MtaRoute');
|
|
300
|
+
const wanted = await outboundRoute(client);
|
|
301
|
+
if (!routes.some((r) => r.name === LOCAL_ROUTE) ||
|
|
302
|
+
JSON.stringify(strategy?.route) !== JSON.stringify(wanted)) {
|
|
303
|
+
await rebuildRoute(client);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
268
306
|
/**
|
|
269
307
|
* Give an org its own Lettermint relay, or replace its token. Idempotent: the
|
|
270
308
|
* route is upserted by name, so calling it twice leaves one route and one
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtfalch/email",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.2",
|
|
4
4
|
"description": "The wtfalch estate: reading a mailbox over JMAP, administering the Stalwart server it lives on, and iTIP scheduling over iMIP. Three entries with no code in common.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|