Project Files
src / accounts.ts
import type { ImapConfig } from "./imap";
import type { SmtpConfig } from "./smtp";
export interface AccountInfo {
slot: 1 | 2 | 3;
label: string;
imap: ImapConfig;
smtp: SmtpConfig | null;
}
function slot(cfg: any, n: 1 | 2 | 3): AccountInfo | null {
const label: string = cfg.get(`account${n}Label`).trim();
const host: string = cfg.get(`account${n}ImapHost`).trim();
const user: string = cfg.get(`account${n}User`).trim();
const password: string = cfg.get(`account${n}Password`);
if (!host || !user || !password) return null;
const smtpHost: string = cfg.get(`account${n}SmtpHost`).trim();
return {
slot: n,
label: label || `Account ${n}`,
imap: { host, port: cfg.get(`account${n}ImapPort`) as number, user, password },
smtp: smtpHost
? { host: smtpHost, port: cfg.get(`account${n}SmtpPort`) as number, user, password }
: null,
};
}
export function getConfiguredAccounts(cfg: any): AccountInfo[] {
return ([1, 2, 3] as const).map(n => slot(cfg, n)).filter((a): a is AccountInfo => a !== null);
}
export function resolveAccount(cfg: any, accountRef: string): AccountInfo {
const accounts = getConfiguredAccounts(cfg);
if (accounts.length === 0) {
throw new Error("No email accounts configured. Add IMAP Host, Email Address, and Password for at least one account in plugin settings.");
}
if (!accountRef || accountRef === "1" || accountRef.toLowerCase() === "default") {
return accounts[0];
}
const n = parseInt(accountRef, 10);
if (!isNaN(n) && n >= 1 && n <= 3) {
const found = accounts.find(a => a.slot === n);
if (!found) throw new Error(`Account ${n} is not configured. Check plugin settings.`);
return found;
}
// Match by label (case-insensitive)
const byLabel = accounts.find(a => a.label.toLowerCase() === accountRef.toLowerCase());
if (byLabel) return byLabel;
const list = accounts.map(a => `${a.slot} (${a.label})`).join(", ");
throw new Error(`Account "${accountRef}" not found. Configured accounts: ${list}`);
}