2024-07-15 14:07:09 +02:00
|
|
|
import accounts from '../config/account.json' with { type: 'json' }
|
|
|
|
import dkim from '../config/dkim.json' with { type: 'json' }
|
2024-03-28 22:32:46 +01:00
|
|
|
|
2024-07-15 14:23:17 +02:00
|
|
|
export type AddressString = `${string}@${string}.${string}`
|
|
|
|
|
2024-03-28 22:32:46 +01:00
|
|
|
export class Contact {
|
|
|
|
#name: string
|
2024-07-15 14:23:17 +02:00
|
|
|
#address: AddressString
|
2024-03-28 22:32:46 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
{ name, address }: {
|
|
|
|
name: string
|
2024-07-15 14:23:17 +02:00
|
|
|
address: AddressString
|
2024-03-28 22:32:46 +01:00
|
|
|
},
|
|
|
|
) {
|
|
|
|
this.#name = name
|
|
|
|
this.#address = address
|
|
|
|
}
|
|
|
|
|
|
|
|
static expand(shortName: string): Contact {
|
2024-07-15 14:07:09 +02:00
|
|
|
if (!(shortName in accounts)) {
|
2024-03-28 22:32:46 +01:00
|
|
|
throw new Error('unknown short name contact')
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:07:09 +02:00
|
|
|
const { name, address } = accounts[shortName as keyof typeof accounts]
|
2024-03-28 22:32:46 +01:00
|
|
|
|
2024-04-04 17:11:32 +02:00
|
|
|
if (typeof name !== 'string') {
|
2024-03-28 22:32:46 +01:00
|
|
|
throw new SyntaxError(
|
|
|
|
`missing key "name" in contact short name config for "${shortName}"`,
|
|
|
|
)
|
|
|
|
}
|
2024-04-04 17:11:32 +02:00
|
|
|
if (typeof address !== 'string') {
|
2024-03-28 22:32:46 +01:00
|
|
|
throw new SyntaxError(
|
|
|
|
`missing key "address" in contact short name config for "${shortName}"`,
|
|
|
|
)
|
|
|
|
}
|
2024-07-15 14:07:09 +02:00
|
|
|
|
|
|
|
const addressRegExp = new RegExp(String.raw`\w+@(\w+\.)?${dkim.domainName}`)
|
|
|
|
if (!(addressRegExp.test(address))) {
|
2024-03-28 22:32:46 +01:00
|
|
|
throw new SyntaxError(
|
|
|
|
`invalid "address" in contact short name config for "${shortName}"`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore checked against regex
|
|
|
|
return new Contact({ name, address })
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(raw: string): Contact {
|
|
|
|
const [_, name, address] =
|
|
|
|
raw.match(/(.*)<(\S+@\S+\.\S+)>/)?.map((match) => match.trim()) ?? []
|
|
|
|
|
|
|
|
if (typeof address !== 'string') {
|
|
|
|
throw new Error('address is empty')
|
|
|
|
}
|
|
|
|
|
|
|
|
//@ts-ignore checked against regex
|
|
|
|
return new Contact({ name, address })
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return `${this.#name} <${this.#address}>`
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:23:17 +02:00
|
|
|
toJSON(): { name: string; address: AddressString } {
|
2024-03-28 22:32:46 +01:00
|
|
|
return {
|
|
|
|
name: this.#name,
|
|
|
|
address: this.#address,
|
|
|
|
} as const
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:23:17 +02:00
|
|
|
get name(): string {
|
2024-03-28 22:32:46 +01:00
|
|
|
return this.#name
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:23:17 +02:00
|
|
|
get address(): AddressString {
|
2024-03-28 22:32:46 +01:00
|
|
|
return this.#address
|
|
|
|
}
|
|
|
|
}
|