Skip to content

Commit ef5ad23

Browse files
author
Matt Diehl
committed
Sender match matches on Sender, from, reply_to.
Currently Sender only matches on 'from'. This resolves Issue #35 by making Sender match: - reply_to - from - sender - X-Original-Sender This is similar to the functionality we have in `receiver`, which matches 'to', 'cc', 'bcc', and 'list'. We also speed up analysis by switching from getRawContent to getHeader.
1 parent b3a33e2 commit ef5ad23

2 files changed

Lines changed: 41 additions & 30 deletions

File tree

Condition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export default class Condition {
166166
return this.matchAddress(message_data.list);
167167
}
168168
case ConditionType.SENDER: {
169-
return this.matchAddress(message_data.from);
169+
return this.matchAddress(...message_data.sender);
170170
}
171171
case ConditionType.RECEIVER: {
172172
return this.matchAddress(...message_data.receivers);
@@ -244,7 +244,7 @@ export default class Condition {
244244
getReplyTo: () => '',
245245
getSubject: () => '',
246246
getPlainBody: () => '',
247-
getRawContent: () => '',
247+
getHeader: (_name: string) => '',
248248
} as GoogleAppsScript.Gmail.GmailMessage;
249249

250250
function c(condition_str: string, message: Partial<GoogleAppsScript.Gmail.GmailMessage>, expected: boolean) {

ThreadData.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,47 @@ export class MessageData {
2727
return str.toLowerCase().split(',').map(address => address.trim());
2828
}
2929

30-
private static parseListId(raw: string): string {
31-
// Parsing would be limited to headers only
32-
const raw_header_str = raw.split('\r\n\r\n')[0];
33-
// const match = raw_header_str.match(/^\s*list-id:[^<]*<([^>]*)>\s*$/im);
34-
// if (match == null || !match[1]) {
35-
// return '';
36-
// }
37-
// const raw_list_id = match[1];
38-
// const raw_list_id_at_index = raw_list_id.lastIndexOf('.', raw_list_id.lastIndexOf('.') - 1);
39-
// const listId = raw_list_id.substr(0, raw_list_id_at_index) + '@' + raw_list_id.substr(raw_list_id_at_index + 1, raw_list_id.length);
40-
// return listId.toLowerCase().replace(/[^a-z0-9@\.\/+]+/g, '-');
41-
42-
// E.x. Mailing-list: list [email protected]; contact [email protected]
43-
const match = raw_header_str.match(/^\s*mailing-list:(.*)$/im);
44-
if (match == null || !match[1]) {
45-
return '';
46-
}
47-
const parts = match[1].trim().split(';');
48-
if (parts.length === 0) {
49-
return '';
30+
private static parseMailingList(message: GoogleAppsScript.Gmail.GmailMessage): string {
31+
const mailing_list = message.getHeader('Mailing-list').trim();
32+
if (!!mailing_list) {
33+
// E.x. "list [email protected]; contact [email protected]"
34+
const parts = mailing_list.split(';');
35+
for (const part of parts) {
36+
const [type, address] = part.trim().split(/\s+/);
37+
Utils.assert(typeof address !== 'undefined', `Unexpected mailing list: ${mailing_list}`);
38+
if (type.trim() === 'list') {
39+
return address;
40+
}
41+
}
5042
}
51-
for (const part of parts) {
52-
const [type, address] = part.trim().split(/\s+/);
53-
Utils.assert(typeof address !== 'undefined', `Unexpected mailing list: ${match[1].trim()}`);
54-
if (type.trim() === 'list') {
55-
return address;
43+
const list_id = message.getHeader('List-ID').trim();
44+
if (!!list_id) {
45+
// E.x. "<mygroup.gmail.com>"
46+
let address = list_id;
47+
if (address.length > 0 && address.charAt(0) == '<') {
48+
address = address.substring(1);
49+
}
50+
if (address.length > 0 && address.charAt(-1)) {
51+
address = address.slice(0, -1);
5652
}
53+
return address;
5754
}
5855
return '';
5956
}
6057

58+
private static parseSenders(message: GoogleAppsScript.Gmail.GmailMessage): string[] {
59+
const original_sender = message.getHeader('X-Original-Sender').trim();
60+
const sender = message.getHeader('Sender').trim();
61+
const senders: string[] = [];
62+
if (!!original_sender) {
63+
senders.push(original_sender);
64+
}
65+
if (!!sender) {
66+
senders.push(sender);
67+
}
68+
return senders;
69+
}
70+
6171
public readonly from: string;
6272
public readonly to: string[];
6373
public readonly cc: string[];
@@ -74,17 +84,18 @@ export class MessageData {
7484
this.to = MessageData.parseAddresses(message.getTo());
7585
this.cc = MessageData.parseAddresses(message.getCc());
7686
this.bcc = MessageData.parseAddresses(message.getBcc());
77-
this.list = MessageData.parseListId(message.getRawContent());
87+
this.list = MessageData.parseMailingList(message);
7888
this.reply_to = MessageData.parseAddresses(message.getReplyTo());
79-
this.sender = ([] as string[]).concat(this.from, this.reply_to);
89+
this.sender = ([] as string[]).concat(
90+
this.from, this.reply_to, ...MessageData.parseSenders(message));
8091
this.receivers = ([] as string[]).concat(this.to, this.cc, this.bcc, this.list);
8192
this.subject = message.getSubject();
8293
// Potentially could be HTML, Plain, or RAW. But doesn't seem very useful other than Plain.
8394
let body = message.getPlainBody();
8495
// Truncate and log long messages.
8596
if (body.length > MAX_BODY_PROCESSING_LENGTH) {
8697
Logger.log(`Ignoring the end of long message with subject "${this.subject}"`);
87-
body = body.substr(0, MAX_BODY_PROCESSING_LENGTH);
98+
body = body.substring(0, MAX_BODY_PROCESSING_LENGTH);
8899
}
89100
this.body = body;
90101
}

0 commit comments

Comments
 (0)