From 4d38f3328f0a2e551fd7f19a1a1ead288cc27207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20M=C3=A4der?= Date: Wed, 30 Mar 2022 16:11:13 +0200 Subject: [PATCH] Add address search in Received headers --- config/default.yaml | 4 ++++ src/EmailProcessor.ts | 11 ++++++++++- src/config.ts | 1 + src/configUtils.ts | 3 ++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 7afa8a2..27b592f 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -75,6 +75,10 @@ defaultRoomConfig: # If true, the To header will be searched for valid targets (rooms). useToAsTarget: true + # If true, the Received header will be searched for valid targets (rooms). + # Useful for redirected emails (as used by sieve or mailman). + useEnvelopeToAsTarget: false + # If true, the bot won't post an HTML message to the Matrix room, instead posting a plain # text message. It is recommended to use $text_body in your formatting to avoid HTML tags # being posted in the rough to the room. diff --git a/src/EmailProcessor.ts b/src/EmailProcessor.ts index e191c16..9dc0258 100644 --- a/src/EmailProcessor.ts +++ b/src/EmailProcessor.ts @@ -9,7 +9,7 @@ import { MessageType } from "./MessageType"; interface IEmailTarget { address: string; name: string; - source: "to" | "cc" | "bcc"; + source: "to" | "cc" | "bcc" | "envelope"; } export class EmailProcessor { @@ -36,6 +36,15 @@ export class EmailProcessor { for (const email of (message.to || [])) targets.push({address: email.address, name: email.name, source: 'to'}); for (const email of (message.cc || [])) targets.push({address: email.address, name: email.name, source: 'cc'}); for (const email of (message.bcc || [])) targets.push({address: email.address, name: email.name, source: 'bcc'}); + for (const header of (message.headerLines || [])) { + if (header.key == 'received') { + const regex = /for <(.*)>/; + const email = header.line.match(regex); + if (email) { + targets.push({address: email[1], name: '', source: 'envelope'}); + } + } + } const primaryFrom = message.from[0]; diff --git a/src/config.ts b/src/config.ts index b961544..9efaab7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -8,6 +8,7 @@ export interface IRoomConfig { useCcAsTarget: boolean; useBccAsTarget: boolean; useToAsTarget: boolean; + useEnvelopeToAsTarget: boolean; plaintextOnly: boolean; attachments: { post: boolean; diff --git a/src/configUtils.ts b/src/configUtils.ts index 7fa5c5a..545fc93 100644 --- a/src/configUtils.ts +++ b/src/configUtils.ts @@ -13,7 +13,7 @@ export function getRoomConfig(roomId: string): IAnnotatedRoomConfig { return Object.assign({}, {roomId}, defaults, overrides) as IAnnotatedRoomConfig; } -export function getRoomConfigsForTarget(emailAddress: string, source: "cc" | "bcc" | "to"): IAnnotatedRoomConfig[] { +export function getRoomConfigsForTarget(emailAddress: string, source: "cc" | "bcc" | "to" | "envelope"): IAnnotatedRoomConfig[] { const configs: IAnnotatedRoomConfig[] = []; const customMapping = config.customMailTargets[emailAddress]; if (!customMapping) { @@ -41,6 +41,7 @@ export function getRoomConfigsForTarget(emailAddress: string, source: "cc" | "bc if (source === "cc" && !roomConfig.useCcAsTarget) continue; if (source === "bcc" && !roomConfig.useBccAsTarget) continue; if (source === "to" && !roomConfig.useToAsTarget) continue; + if (source === "envelope" && !roomConfig.useEnvelopeToAsTarget) continue; freshConfigs.push(roomConfig); }