-
Notifications
You must be signed in to change notification settings - Fork 20
Match arbitrary header #67
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
Open
mlsad3
wants to merge
1
commit into
ranmocy:main
Choose a base branch
from
mlsad3:match-arbitrary-header
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,14 @@ | |
| */ | ||
|
|
||
| import {MessageData} from './ThreadData'; | ||
| import {SessionData} from './SessionData'; | ||
| import Mocks from './Mocks'; | ||
| import Utils from './utils'; | ||
|
|
||
| const RE_FLAG_PATTERN = /^\/(.*)\/([gimuys]*)$/; | ||
|
|
||
| enum ConditionType { | ||
| AND, OR, NOT, SUBJECT, FROM, TO, CC, BCC, LIST, SENDER, RECEIVER, BODY, | ||
| AND, OR, NOT, SUBJECT, FROM, TO, CC, BCC, LIST, SENDER, RECEIVER, BODY, HEADER, | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -65,7 +67,7 @@ export default class Condition { | |
| return pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
| } | ||
|
|
||
| private static parseRegExp(pattern: string, condition_str: string, matching_address: boolean): RegExp { | ||
| public static parseRegExp(pattern: string, condition_str: string, matching_address: boolean): RegExp { | ||
| Utils.assert(pattern.length > 0, `Condition ${condition_str} should have value but not found`); | ||
| const match = pattern.match(RE_FLAG_PATTERN); | ||
| if (match !== null) { | ||
|
|
@@ -85,6 +87,7 @@ export default class Condition { | |
| } | ||
|
|
||
| private readonly type: ConditionType; | ||
| private readonly subtype: string; | ||
| private readonly regexp: RegExp; | ||
| private readonly sub_conditions: Condition[]; | ||
|
|
||
|
|
@@ -94,8 +97,9 @@ export default class Condition { | |
| `Condition ${condition_str} should be surrounded by ().`); | ||
| const first_space = condition_str.indexOf(" "); | ||
| const type_str = condition_str.substring(1, first_space).trim().toUpperCase(); | ||
| const rest_str = condition_str.substring(first_space + 1, condition_str.length - 1).trim(); | ||
| let rest_str = condition_str.substring(first_space + 1, condition_str.length - 1).trim(); | ||
| this.type = ConditionType[type_str as keyof typeof ConditionType]; | ||
| this.subtype = ""; | ||
| switch (this.type) { | ||
| case ConditionType.AND: | ||
| case ConditionType.OR: { | ||
|
|
@@ -119,6 +123,13 @@ export default class Condition { | |
| this.regexp = Condition.parseRegExp(rest_str, condition_str, true); | ||
| break; | ||
| } | ||
| case ConditionType.HEADER: { | ||
| const subtype_first_space = rest_str.indexOf(" "); | ||
| this.subtype = rest_str.substring(0, subtype_first_space).trim(); | ||
| rest_str = rest_str.substring(subtype_first_space + 1, rest_str.length - 1).trim(); | ||
| this.regexp = Condition.parseRegExp(rest_str, condition_str, false); | ||
| break; | ||
| } | ||
| case ConditionType.SUBJECT: | ||
| case ConditionType.BODY: { | ||
| this.regexp = Condition.parseRegExp(rest_str, condition_str, false); | ||
|
|
@@ -177,6 +188,13 @@ export default class Condition { | |
| case ConditionType.BODY: { | ||
| return this.regexp.test(message_data.body); | ||
| } | ||
| case ConditionType.HEADER: { | ||
| const headerData = message_data.headers.get(this.subtype); | ||
| if (headerData !== undefined) { | ||
| return this.regexp.test(headerData); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -191,6 +209,17 @@ export default class Condition { | |
| return `(${type_str} ${regexp_str} ${sub_str})`; | ||
| } | ||
|
|
||
| getConditionHeaders(): string[] { | ||
| const headers = []; | ||
| if (this.type === ConditionType.HEADER) { | ||
| headers.push(this.subtype); | ||
| } | ||
| this.sub_conditions?.forEach((sub_condition) => { | ||
| headers.push(...sub_condition.getConditionHeaders()); | ||
| }); | ||
| return headers; | ||
| } | ||
|
|
||
| public static testRegex(it: Function, expect: Function) { | ||
|
|
||
| function test_regexp(condition_str: string, target_str: string, is_address: boolean) { | ||
|
|
@@ -271,11 +300,18 @@ export default class Condition { | |
| getSubject: () => '', | ||
| getPlainBody: () => '', | ||
| getRawContent: () => '', | ||
| getHeader: (_name: string) => '', | ||
| } as GoogleAppsScript.Gmail.GmailMessage; | ||
|
|
||
| function test_cond(condition_str: string, message: Partial<GoogleAppsScript.Gmail.GmailMessage>): boolean { | ||
| function test_cond( | ||
| condition_str: string, | ||
| message: Partial<GoogleAppsScript.Gmail.GmailMessage>, | ||
| session_data: Partial<SessionData> = {}): boolean { | ||
| const condition = new Condition(condition_str); | ||
| const message_data = new MessageData(Object.assign({}, base_message, message)); | ||
| const mock_session_data = Mocks.getMockSessionData(session_data); | ||
| const message_data = new MessageData( | ||
| mock_session_data, | ||
| Object.assign({}, base_message, message)); | ||
| return condition.match(message_data); | ||
| } | ||
|
|
||
|
|
@@ -324,5 +360,56 @@ export default class Condition { | |
| getTo: () => '[email protected]', | ||
| })).toBe(true) | ||
| }) | ||
|
|
||
| it('Matches custom header with value', () => { | ||
| expect(test_cond(`(header Sender [email protected])`, | ||
| { | ||
| getHeader: (name: string) => { | ||
| if (name === 'Sender') { | ||
| return '[email protected]'; | ||
| } | ||
| return ''; | ||
| }, | ||
| }, | ||
| { | ||
| requested_headers: ['Sender', 'List-Post'], | ||
| })).toBe(true) | ||
| }) | ||
| it('Matches nested custom header with value', () => { | ||
| expect(test_cond(`(and | ||
| (from [email protected]) | ||
| (and | ||
| (header X-List mylist.gmail.com) | ||
| (header Precedence /list/i)))`, | ||
| { | ||
| getFrom: () => 'DDD EEE <[email protected]>', | ||
| getHeader: (name: string) => { | ||
| if (name === 'X-List') { | ||
| return 'mylist.gmail.com'; | ||
| } | ||
| if (name === 'Precedence') { | ||
| return 'bills list'; | ||
| } | ||
| return ''; | ||
| }, | ||
| }, | ||
| { | ||
| requested_headers: ['X-List', 'Precedence'], | ||
| })).toBe(true) | ||
| }) | ||
| it('Does not match custom header with incorrect data', () => { | ||
| expect(test_cond(`(header MyHeader abc)`, | ||
| { | ||
| getHeader: (name: string) => { | ||
| if (name === 'MyHeader') { | ||
| return 'xyz'; | ||
| } | ||
| return ''; | ||
| }, | ||
| }, | ||
| { | ||
| requested_headers: ['MyHeader'], | ||
| })).toBe(false) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,10 @@ export class Rule { | |
| return this.condition.toString(); | ||
| } | ||
|
|
||
| getConditionHeaders(): string[] { | ||
| return this.condition.getConditionHeaders(); | ||
| } | ||
|
|
||
| private static parseBooleanValue(str: string): boolean { | ||
| if (str.length === 0) { | ||
| return false; | ||
|
|
@@ -86,6 +90,15 @@ export class Rule { | |
| return result; | ||
| } | ||
|
|
||
| public static getConditionHeaders(rules: Rule[]): string[] { | ||
| const headers: Set<string> = new Set<string>(); | ||
| rules.forEach((rule) => { | ||
| const rule_headers = rule.getConditionHeaders(); | ||
| rule_headers.forEach(item => headers.add(item)) | ||
| }); | ||
| return Array.from(headers.values()) | ||
| } | ||
|
|
||
| public static parseRules(values: string[][]): Rule[] { | ||
| const row_num = values.length; | ||
| const column_num = values[0].length; | ||
|
|
@@ -206,10 +219,12 @@ export class Rule { | |
| }]); | ||
|
|
||
| const rules = Rule.parseRules(sheet); | ||
| const condition_headers = Rule.getConditionHeaders(rules); | ||
|
|
||
| expect(rules.length).toBe(1); | ||
| expect(rules[0].stage).toBe(5); | ||
| expect(rules[0].thread_action.label_names.size).toBe(2); | ||
| expect(condition_headers).toEqual([]); | ||
| }) | ||
|
|
||
| it('Loaded Rules are sorted by stage', () => { | ||
|
|
@@ -239,5 +254,34 @@ export class Rule { | |
| expect(rules[2].stage).toBe(15); | ||
| }) | ||
|
|
||
| it('Loads rules with Headers', () => { | ||
| const sheet = Mocks.getMockTestSheet([ | ||
| { | ||
| conditions: '(and (or (header Test1 /abc/i)' + | ||
| ' (header h2 [email protected]))' + | ||
| ' (and' + | ||
| ' (header X-List abcde)' + | ||
| ' (header h3 /abcde/)))', | ||
| add_labels: 'def, uvw', | ||
| stage: "10", | ||
| }, | ||
| { | ||
| conditions: '(header h5 /asdf/)', | ||
| add_labels: 'abc', | ||
| stage: "15", | ||
| } | ||
| ]); | ||
|
|
||
| const rules = Rule.parseRules(sheet); | ||
| const condition_headers = Rule.getConditionHeaders(rules); | ||
|
|
||
| expect(rules.length).toBe(2); | ||
| expect(rules[0].stage).toBe(10); | ||
| expect(rules[0].thread_action.label_names).toEqual(new Set(['def', 'uvw'])); | ||
| expect(rules[1].stage).toBe(15); | ||
| expect(rules[1].thread_action.label_names).toEqual(new Set(['abc'])); | ||
| expect(condition_headers).toEqual( | ||
| ['Test1', 'h2', 'X-List', 'h3', 'h5']); | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
.length - 1was clipping the last letter off the value I was using