Add creating IMAP folders from the app
Bump version to 0.2.0 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+67
-1
@@ -404,6 +404,28 @@ async function findSentMailbox(client, createIfMissing = false) {
|
||||
return findSpecialMailbox(client, '\\Sent', ['sent', 'sent items', 'sent mail', 'sent messages'], createIfMissing ? 'Sent' : null);
|
||||
}
|
||||
|
||||
const CUSTOM_FOLDER_PREFIX = 'Folder:';
|
||||
const RESERVED_FOLDER_NAMES = new Set(['inbox', 'sent', 'drafts', 'archive', 'trash', 'starred', 'snoozed', 'junk', 'spam']);
|
||||
|
||||
async function listCustomFolders(client) {
|
||||
return (await client.list())
|
||||
.filter(mailbox => !mailbox.flags?.has('\\Noselect') && !mailbox.specialUse &&
|
||||
mailbox.path.toUpperCase() !== 'INBOX' &&
|
||||
!RESERVED_FOLDER_NAMES.has(mailbox.path.toLowerCase().split(/[./]/).pop()) &&
|
||||
!['sent items', 'sent mail', 'sent messages', 'archives', 'all mail', 'deleted items', 'deleted messages']
|
||||
.includes(mailbox.path.toLowerCase().split(/[./]/).pop()))
|
||||
.map(mailbox => ({ name: mailbox.name, path: mailbox.path, id: CUSTOM_FOLDER_PREFIX + mailbox.path }))
|
||||
.sort((a, b) => a.path.localeCompare(b.path));
|
||||
}
|
||||
|
||||
function validFolderName(input) {
|
||||
const name = String(input || '').trim();
|
||||
if (!name || name.length > 100) throw new Error('Enter a folder name of up to 100 characters.');
|
||||
if (/[\\/.*%#&\u0000-\u001f]/.test(name)) throw new Error('Folder names cannot contain / \\ . * % # & or control characters.');
|
||||
if (RESERVED_FOLDER_NAMES.has(name.toLowerCase())) throw new Error('That folder name is reserved.');
|
||||
return name;
|
||||
}
|
||||
|
||||
async function folderPath(client, folder, createIfMissing = false) {
|
||||
if (folder === 'Inbox' || folder === 'Starred' || folder === 'Snoozed') return 'INBOX';
|
||||
const definitions = {
|
||||
@@ -412,6 +434,12 @@ async function folderPath(client, folder, createIfMissing = false) {
|
||||
Trash: ['\\Trash', ['trash', 'deleted items', 'deleted messages'], 'Trash']
|
||||
};
|
||||
const definition = definitions[folder];
|
||||
if (!definition && typeof folder === 'string' && folder.startsWith(CUSTOM_FOLDER_PREFIX)) {
|
||||
const path = folder.slice(CUSTOM_FOLDER_PREFIX.length);
|
||||
const match = (await client.list()).find(mailbox => mailbox.path === path);
|
||||
if (!match || match.flags?.has('\\Noselect')) throw new Error('This folder is not available on the mail server.');
|
||||
return match.path;
|
||||
}
|
||||
if (!definition) throw new Error('This folder is not available on the mail server.');
|
||||
return findSpecialMailbox(client, ...definition.slice(0, 2), createIfMissing ? definition[2] : null);
|
||||
}
|
||||
@@ -869,7 +897,8 @@ ipcMain.handle('mail:sync-folder', async (_event, requestedFolder, offset = 0, f
|
||||
if (!account) throw new Error('Add a mail account before syncing.');
|
||||
const allowedFolders = new Set(['Inbox', 'Sent', 'Archive', 'Trash', 'Starred', 'Snoozed']);
|
||||
const selectedFolder = allowedFolders.has(filters.folder) ? filters.folder : requestedFolder;
|
||||
const folder = allowedFolders.has(selectedFolder) ? selectedFolder : 'Inbox';
|
||||
const folder = allowedFolders.has(selectedFolder) || String(selectedFolder).startsWith(CUSTOM_FOLDER_PREFIX)
|
||||
? selectedFolder : 'Inbox';
|
||||
const { folder: _filterFolder, ...criteria } = filters;
|
||||
try {
|
||||
await flushMailOutbox(account);
|
||||
@@ -882,6 +911,43 @@ ipcMain.handle('mail:sync-folder', async (_event, requestedFolder, offset = 0, f
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('mail:list-folders', async () => {
|
||||
const account = await readAccount();
|
||||
if (!account) throw new Error('Add a mail account before listing folders.');
|
||||
const client = imapClient(account);
|
||||
try {
|
||||
await client.connect();
|
||||
return { folders: await listCustomFolders(client) };
|
||||
} catch (error) {
|
||||
if (isConnectivityFailure(error)) return { folders: [], offline: true };
|
||||
throw error;
|
||||
} finally {
|
||||
if (client.usable) await client.logout().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('mail:create-folder', async (_event, input) => {
|
||||
const account = await readAccount();
|
||||
if (!account) throw new Error('Add a mail account before creating folders.');
|
||||
const name = validFolderName(input?.name);
|
||||
const client = imapClient(account);
|
||||
try {
|
||||
await client.connect();
|
||||
const existing = await client.list();
|
||||
if (existing.some(mailbox => mailbox.path.toLowerCase() === name.toLowerCase())) {
|
||||
throw new Error('A folder with that name already exists.');
|
||||
}
|
||||
const created = await client.mailboxCreate(name);
|
||||
await client.mailboxSubscribe(created.path).catch(() => {});
|
||||
return { ok: true, folder: { name, path: created.path, id: CUSTOM_FOLDER_PREFIX + created.path }, folders: await listCustomFolders(client) };
|
||||
} catch (error) {
|
||||
if (error.message === 'A folder with that name already exists.') throw error;
|
||||
throw new Error(`Could not create the folder: ${error.message}`);
|
||||
} finally {
|
||||
if (client.usable) await client.logout().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('mail:list-drafts', async () => {
|
||||
const account = await readAccount();
|
||||
if (!account) throw new Error('Add a mail account before opening drafts.');
|
||||
|
||||
Reference in New Issue
Block a user