Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(mail-ingest): read transfer bookings instead of filing them as forwarding confirmations

Maurice3b65da76262e
diff --git a/server/src/nest/mail-ingest/mail-classify.ts b/server/src/nest/mail-ingest/mail-classify.ts
index 9eabab939..32be0b2c8 100644
--- a/server/src/nest/mail-ingest/mail-classify.ts
+++ b/server/src/nest/mail-ingest/mail-classify.ts
@@ -12,8 +12,8 @@ type MailClass =
| { kind: 'drop'; reason: 'newsletter' | 'auto_reply' | 'empty_mail' };
const FORWARDING_SENDERS = /forwarding-noreply@google\.com|@accountprotection\.microsoft\.com|@microsoft\.com/i;
-const FORWARDING_SUBJECT =
- /(forwarding|weiterleitung|transfert|reenv[ií]o|inoltro|doorsturen).*(confirm|bestätig|verif|conferma|bevestig)/i;
+const FORWARDING_WORD = /forwarding|weiterleitung|transfert|reenv[ií]o|inoltro|doorsturen/i;
+const CONFIRM_WORD = /confirm|bestätig|verif|conferma|bevestig/i;
const CODE_RE = /(?:code|código|codice|kód)\D{0,40}?(\d{6,10})\b/i;
const AUTO_REPLY_SUBJECT =
/^(automatic reply|auto(matische)? ?(reply|antwort)|out of (the )?office|abwesenheit|r[ée]ponse automatique|risposta automatica|respuesta autom[áa]tica)/i;
@@ -30,9 +30,12 @@ const EMPTY_BODY_CHARS = 20;
export function classifyMail(mail: WalkedMail): MailClass {
const { envelope } = mail;
const subject = mail.subject ?? '';
- const fromProvider = FORWARDING_SENDERS.test(envelope.from ?? '') && CODE_RE.test(mail.body);
- if (FORWARDING_SUBJECT.test(subject) || fromProvider) {
- return { kind: 'forwarding_confirmation', code: CODE_RE.exec(mail.body)?.[1] ?? null };
+ const code = CODE_RE.exec(mail.body)?.[1] ?? null;
+ // A subject alone is no proof: "Votre transfert aéroport est confirmé" is a booking.
+ const fromProvider = FORWARDING_SENDERS.test(envelope.from ?? '');
+ const subjectSays = asksToConfirmForwarding(subject);
+ if (fromProvider ? subjectSays || code !== null : subjectSays && code !== null) {
+ return { kind: 'forwarding_confirmation', code };
}
const autoSubmitted = (envelope.autoSubmitted ?? '').trim().toLowerCase();
const precedence = (envelope.precedence ?? '').trim().toLowerCase();
@@ -49,6 +52,12 @@ export function classifyMail(mail: WalkedMail): MailClass {
return { kind: 'read' };
}
+/** A forwarding word with a confirm word after it, found without rescanning the subject from every forwarding word. */
+function asksToConfirmForwarding(subject: string): boolean {
+ const word = FORWARDING_WORD.exec(subject);
+ return word !== null && CONFIRM_WORD.test(subject.slice(word.index + word[0].length));
+}
+
/**
* Whether the mail came from the person themselves, signed: the From address
* is their account's, and the receiving server's verdict holds a DKIM pass
@@ -82,6 +91,6 @@ function passesFor(result: string, domain: string): boolean {
/** The bare address of a From header: `Mia <[email protected]>` is `[email protected]`. */
export function addressOf(header: string | null): string | null {
- const match = /<([^>]+)>/.exec(header ?? '') ?? /([^\s<>"]+@[^\s<>"]+)/.exec(header ?? '');
+ const match = /<([^<>]+)>/.exec(header ?? '') ?? /(?<![^\s<>"])([^\s<>"]+@[^\s<>"]+)/.exec(header ?? '');
return match ? match[1].trim().toLowerCase() : null;
}
diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts
index e7ddf4396..89e059a56 100644
--- a/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts
+++ b/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts
@@ -34,6 +34,34 @@ describe('classifyMail', () => {
'Confirmation code: 612345678. To allow forwarding, click the link.',
);
expect(classifyMail(gmail)).toEqual({ kind: 'forwarding_confirmation', code: '612345678' });
+ const relayed = mail({ Subject: 'Mail forwarding verification' }, 'Your code: 612345678, valid for 24 hours.');
+ expect(classifyMail(relayed)).toEqual({ kind: 'forwarding_confirmation', code: '612345678' });
+ const linkOnly = mail(
+ { From: '[email protected]', Subject: 'Gmail Forwarding Confirmation' },
+ 'Click the link below to allow forwarding to this address.',
+ );
+ expect(classifyMail(linkOnly)).toEqual({ kind: 'forwarding_confirmation', code: null });
+ });
+
+ it('reads a French transfer booking whose subject only sounds like a forwarding confirmation', () => {
+ const body = 'Votre transfert Orly vers Paris le 2030-03-02 à 10:00, réservation TR-4821.';
+ for (const subject of [
+ 'Votre transfert aéroport est confirmé',
+ 'Transfert Orly Paris : confirmation de réservation',
+ ]) {
+ expect(classifyMail(mail({ Subject: subject }, body)), subject).toEqual({ kind: 'read' });
+ }
+ });
+
+ it('reads a long subject and From header in linear time', () => {
+ const started = Date.now();
+ const long = mail(
+ { From: `${'<'.repeat(60_000)} ${'a'.repeat(60_000)}`, Subject: 'forwarding '.repeat(20_000) },
+ BOOKING_BODY,
+ );
+ expect(classifyMail(long)).toEqual({ kind: 'read' });
+ expect(addressOf(long.envelope.from)).toBeNull();
+ expect(Date.now() - started).toBeLessThan(1000);
});
it('drops an automatic reply, but not a confirmation a booking system generated', () => {
@@ -96,6 +124,7 @@ describe('senderVerified', () => {
it('reads a bare address out of a From header', () => {
expect(addressOf('Mia <[email protected]>')).toBe('[email protected]');
expect(addressOf('[email protected]')).toBe('[email protected]');
+ expect(addressOf('"Mia, Travel" [email protected]')).toBe('[email protected]');
expect(addressOf(null)).toBeNull();
});
});