@wtfalch/email 0.19.0 → 0.19.1

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 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'`). `removeOrgRelay(client,
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 { FALLBACK_ROUTE, outboundRoute } from './org-mail.js';
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: { else: FALLBACK_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
- /** Stalwart's built-in route for direct delivery by MX lookup. */
181
- export declare const FALLBACK_ROUTE = "'mx'";
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. */
@@ -210,7 +210,9 @@ export async function parkOrgMail(client, org, options) {
210
210
  const RELAY_PREFIX = 'lettermint-';
211
211
  const RELAY_DESCRIPTION = 'Lettermint relay for ';
212
212
  /** Stalwart's built-in route for direct delivery by MX lookup. */
213
- export const FALLBACK_ROUTE = "'mx'";
213
+ const FALLBACK_ROUTE = "'mx'";
214
+ /** The `MtaRoute` that writes a message into this server's own mailboxes. */
215
+ export const LOCAL_ROUTE = 'local';
214
216
  function relayName(slug) {
215
217
  assertLabel('an org slug', slug);
216
218
  return `${RELAY_PREFIX}${slug}`;
@@ -243,6 +245,24 @@ function routeRule(domain, routeName) {
243
245
  ['then', `'${routeName}'`],
244
246
  ]);
245
247
  }
248
+ /** The first rule of every route expression: a recipient on a domain this
249
+ * server hosts is delivered here. Without it the message follows the sender's
250
+ * relay or the MX lookup, and never reaches the mailbox. */
251
+ function localRule() {
252
+ return Object.fromEntries([
253
+ ['if', 'is_local_domain(rcpt_domain)'],
254
+ ['then', `'${LOCAL_ROUTE}'`],
255
+ ]);
256
+ }
257
+ /** The route expression for a set of relays: local delivery first, then each
258
+ * relay by sender domain, then direct delivery. */
259
+ export function routeExpression(relays) {
260
+ const rules = [localRule(), ...relays.map((e) => routeRule(e.domain, e.name))];
261
+ return {
262
+ match: Object.fromEntries(rules.map((r, i) => [String(i), r])),
263
+ else: FALLBACK_ROUTE,
264
+ };
265
+ }
246
266
  /** The `MtaOutboundStrategy.route` expression for the relays that exist now.
247
267
  * `applyObjects` uses it too, so a re-apply of the instance plan keeps every
248
268
  * org's route instead of resetting the expression to the direct fallback. */
@@ -253,14 +273,15 @@ export async function outboundRoute(client) {
253
273
  return domain && r.name ? [{ domain, name: r.name }] : [];
254
274
  })
255
275
  .sort((a, b) => a.name.localeCompare(b.name));
256
- return entries.length === 0
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
- };
276
+ return routeExpression(entries);
262
277
  }
263
278
  async function rebuildRoute(client) {
279
+ // The expression names the local route. On a server without it Stalwart
280
+ // falls back to the MX lookup for its own recipients, without an error.
281
+ const routes = await client.list('MtaRoute');
282
+ if (!routes.some((r) => r.name === LOCAL_ROUTE)) {
283
+ await client.create('MtaRoute', { '@type': 'Local', name: LOCAL_ROUTE });
284
+ }
264
285
  const route = await outboundRoute(client);
265
286
  await client.update('MtaOutboundStrategy', 'singleton', { route });
266
287
  await client.create('Action', { '@type': 'ReloadSettings' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wtfalch/email",
3
- "version": "0.19.0",
3
+ "version": "0.19.1",
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",