Implement regexp-based thread ID parsing from recipient email

This commit is contained in:
2025-01-03 23:58:58 -05:00
parent 9f750bc2eb
commit 0f596ab5f5
6 changed files with 81 additions and 7 deletions

22
src/threads/id.ts Normal file
View 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}$`)
}