Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add address search in Received headers #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/EmailProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];

Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface IRoomConfig {
useCcAsTarget: boolean;
useBccAsTarget: boolean;
useToAsTarget: boolean;
useEnvelopeToAsTarget: boolean;
plaintextOnly: boolean;
attachments: {
post: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down