Skip to content

Commit

Permalink
Merge pull request #15 from Jabb0/manual-per-folder-mode
Browse files Browse the repository at this point in the history
Addded manual per folder processing option
  • Loading branch information
Jabb0 authored Jan 7, 2024
2 parents 493b358 + 4615a35 commit 1a27af4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
94 changes: 71 additions & 23 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,7 +104,7 @@ async function processMessages(folder, messages) {
const domain = identities[0].split("@").pop();

// Mapping from <prefix> 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)) {
Expand All @@ -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, []);
Expand All @@ -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) {
Expand All @@ -137,40 +137,51 @@ 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",
index: 1
});
}

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",
Expand All @@ -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);
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 1a27af4

Please sign in to comment.