Project Files
accounts.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfiguredAccounts = getConfiguredAccounts;
exports.resolveAccount = resolveAccount;
function slot(cfg, n) {
const label = cfg.get(`account${n}Label`).trim();
const host = cfg.get(`account${n}ImapHost`).trim();
const user = cfg.get(`account${n}User`).trim();
const password = cfg.get(`account${n}Password`);
if (!host || !user || !password)
return null;
const smtpHost = cfg.get(`account${n}SmtpHost`).trim();
return {
slot: n,
label: label || `Account ${n}`,
imap: { host, port: cfg.get(`account${n}ImapPort`), user, password },
smtp: smtpHost
? { host: smtpHost, port: cfg.get(`account${n}SmtpPort`), user, password }
: null,
};
}
function getConfiguredAccounts(cfg) {
return [1, 2, 3].map(n => slot(cfg, n)).filter((a) => a !== null);
}
function resolveAccount(cfg, accountRef) {
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}`);
}