Implement regexp-based thread ID parsing from recipient email
This commit is contained in:
22
src/threads/id.ts
Normal file
22
src/threads/id.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import {config} from "../config.ts";
|
||||
|
||||
function escapeRexString(rex: string): string {
|
||||
const specials = ['.', '+', '(', ')', '[', ']', '^', '$']
|
||||
for ( const char of specials ) {
|
||||
rex = rex.replaceAll(char, `\\${char}`)
|
||||
}
|
||||
return rex
|
||||
}
|
||||
|
||||
export function buildThreadAddressMatcher(): RegExp {
|
||||
// We want a regular expression that only matches the thread address
|
||||
// template specified by the config. We also want it to have a capture
|
||||
// group for the thread ID.
|
||||
// In the config, the thread ID is represented with the placeholder: %ID%
|
||||
|
||||
const idPrefix = escapeRexString(config.mail.threads.idPrefix)
|
||||
const idCapture = `(${idPrefix}[^@]+)` // first rule of email: don't be as strict as the RFC
|
||||
|
||||
const template = escapeRexString(config.mail.threads.template).replace('%ID%', idCapture)
|
||||
return new RegExp(`^${template}$`)
|
||||
}
|
||||
Reference in New Issue
Block a user