55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { ImapInstance } from "./imap/ImapInstance";
|
|
import { SmtpInstance } from "./smtp/SmtpInstance";
|
|
import logger from "../system/Logger";
|
|
import { getAllAccounts } from "../db/imap/imap-db";
|
|
|
|
export interface Account {
|
|
id: number;
|
|
user: string;
|
|
password?: string;
|
|
}
|
|
|
|
class EmailManager {
|
|
imapInstances: ImapInstance[];
|
|
smtpInstances: SmtpInstance[];
|
|
|
|
constructor() {
|
|
this.imapInstances = [];
|
|
this.smtpInstances = [];
|
|
}
|
|
|
|
init() {
|
|
getAllAccounts()
|
|
.then((accounts: Account[]) => {
|
|
for (let i = 0; i < accounts.length; i++) {
|
|
accounts[i].password = accounts[i]?.password?.toString().replace(/[\u{0080}-\u{FFFF}]/gu, "");
|
|
if (accounts[i].id == 2) continue; //debug_todo
|
|
this.addImapInstance(accounts[i]);
|
|
this.addSmtpInstance(accounts[i]);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
logger.err(err);
|
|
});
|
|
}
|
|
|
|
addImapInstance(config) {
|
|
this.imapInstances.push(new ImapInstance(config));
|
|
}
|
|
|
|
addSmtpInstance(config) {
|
|
this.smtpInstances.push(new SmtpInstance(config));
|
|
}
|
|
|
|
getSmtp(email: string): SmtpInstance | undefined {
|
|
return this.smtpInstances.find((instance) => instance.user === email);
|
|
}
|
|
|
|
getImap(email: string): ImapInstance | undefined {
|
|
return this.imapInstances.find((instance) => instance.account.user === email);
|
|
}
|
|
}
|
|
|
|
const emailManager = new EmailManager();
|
|
export default emailManager;
|