diff --git a/package-lock.json b/package-lock.json index 6b43cdd..bcf98f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "catchallbird", - "version": "1.0.8", + "version": "1.0.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "catchallbird", - "version": "1.0.8", + "version": "1.0.9", "license": "MPL-2.0", "devDependencies": { "chai": "^4.3.7", diff --git a/package.json b/package.json index 77dad2f..c14d3ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "catchallbird", - "version": "1.0.8", + "version": "1.0.9", "description": "Enable Thunderbird to handle CatchAll email addresses.", "main": "index.js", "type": "module", diff --git a/src/background/background.js b/src/background/background.js index fb827d9..45c74a1 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -87,11 +87,10 @@ async function processMessages(folder, messages) { catchAllBirdOptions: { ...DEFAULT_OPTIONS } }); - const { accountId, path } = folder; + const { accountId } = folder; - // Decide if this message is relevant for the tool by accountId and folder - // only use /INBOX - if (!accounts.has(accountId) || path !== GLOBAL_INBOX_PATH) + // Decide if this message should be processed by accountId and folder + if (!accounts.has(accountId)) return; const account = await messenger.accounts.get(accountId, true); // Include mail folders as well @@ -105,7 +104,7 @@ async function processMessages(folder, messages) { const domain = identities[0].split("@").pop(); // Mapping from to mail ids to move there - // In theory this could use pagination. But we never handle that many messages I guess. + // In theory this could use pagination. But we never handle that many messages yet. const mailMapping = new Map(); for await (let message of iterateMessagePages(messages)) { @@ -115,7 +114,7 @@ async function processMessages(folder, messages) { const prefix = getPrefixFromMessage(domain, recipients, ccList, bccList); if (prefix === null) { - console.warn(`Message ${id} does not have a recipient associated with domain ${domain}. Thus it is not moved. Check if this is a mistake.`, message); + console.warn(`Message ${id} does not have a recipient associated with domain ${domain}. Thus it is not processed. Check if this is a mistake.`, message); } else { if (!mailMapping.has(prefix)) { mailMapping.set(prefix, []); @@ -128,7 +127,8 @@ async function processMessages(folder, messages) { if (mailMapping.size > 0) { // Move emails to respective subfolder if (options.isAutomaticFolderCreationEnabled) { - await moveMessages(folder, mailMapping); + const inboxFolder = await getInboxForAccount(accountId); + await moveMessages(inboxFolder, mailMapping); } // Create identities associated with subdomains if (options.isAutomaticIdentityCreationEnabled) { @@ -137,22 +137,42 @@ async function processMessages(folder, messages) { } } +async function getInboxForAccount(accountId) { + const account = await messenger.accounts.get(accountId, true); + const inboxFolder = account.folders.filter(folder => folder.path == GLOBAL_INBOX_PATH)[0] || null; + return inboxFolder; +} + async function processInbox() { // Get inbox folder of all accounts to listen for const { catchAllBirdAccounts: accounts } = await messenger.storage.local.get({ catchAllBirdAccounts: new Set() }); for (const accountId of accounts) { - const account = await messenger.accounts.get(accountId, true); - const inboxFolder = account.folders.filter(folder => folder.path == GLOBAL_INBOX_PATH)[0] || null; + + const inboxFolder = await getInboxForAccount(accountId); if (inboxFolder === null) { console.warn(`Account ${account} does not have inbox folder with path ${GLOBAL_INBOX_PATH}`); } else { - const messages = await messenger.messages.list(inboxFolder); - await processMessages(inboxFolder, messages); + await processMessagesInFolder(inboxFolder); } } } +async function processMessagesInFolder(folder) { + const allMessages = await messenger.messages.list(folder); + await processMessages(folder, allMessages); +} + +async function onNewMailReceived(folder, messages) { + + // Only use new mail in the inbox folder + if (folder.path !== GLOBAL_INBOX_PATH) { + return; + } + + await processMessages(folder, messages); +} + async function welcomeTab() { await messenger.tabs.create({ url: "../popup/popup.html", @@ -160,17 +180,8 @@ async function processInbox() { }); } -async function load() { - const { - catchAllBirdHideWelcomeMessage: hideWelcomeMessage - } = await messenger.storage.local.get({ - catchAllBirdHideWelcomeMessage: false - }); - - if (!hideWelcomeMessage) { - await welcomeTab(); - } +async function addMenuItemProcessInbox() { // Setup menu button for reprocessing inbox const menu_id = await messenger.menus.create({ title: "CatchAll Bird: Process INBOX", @@ -184,11 +195,48 @@ async function load() { await processInbox(); } }); +} + + +async function addMenuItemProcessFolder() { + // Setup menu button for processing a specific folder + const menu_id = await messenger.menus.create({ + title: "CatchAll Bird: Process this folder", + contexts: [ + "folder_pane" + ], + }); + + await messenger.menus.onClicked.addListener(async (info, tab) => { + if (info.menuItemId == menu_id) { + const { selectedFolder } = info; + + if (!!selectedFolder) { + processMessagesInFolder(selectedFolder); + } + } + }); +} + + +async function load() { + const { + catchAllBirdHideWelcomeMessage: hideWelcomeMessage + } = await messenger.storage.local.get({ + catchAllBirdHideWelcomeMessage: false + }); + + if (!hideWelcomeMessage) { + await welcomeTab(); + } + + await addMenuItemProcessInbox(); + await addMenuItemProcessFolder(); // Add a listener for the onNewMailReceived events. // On each new message decide what to do - // Messages are through junk classification and message filters - await messenger.messages.onNewMailReceived.addListener(processMessages); + // Messages are already filtered by junk classification and message filters + await messenger.messages.onNewMailReceived.addListener(onNewMailReceived); } document.addEventListener("DOMContentLoaded", load); \ No newline at end of file diff --git a/src/manifest.json b/src/manifest.json index c171087..eb56867 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "CatchAll Bird", "description": "Enable Thunderbird to handle CatchAll email addresses.", - "version": "1.0.8", + "version": "1.0.9", "author": "Jabb0", "browser_specific_settings": { "gecko": {