-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
90 lines (75 loc) · 2.65 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function escapeRegExp(string) {
// Source: https://stackoverflow.com/a/6969486
// $& means the whole matched string
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function buildNewFrom(address, name) {
if (!name) {
return address;
}
return name + " <" + address + ">";
}
async function extractAddressPartsFromHeader(header) {
const addresses = await emailAddresses.parseAddressList(header);
if (!addresses.length) {
return null;
}
return addresses[0];
}
async function extractRecipientMailAddresses(message) {
const allRecipients = [
...message.recipients,
...message.ccList,
...message.bccList
];
const allMailAddresses = []
for (let recipient of allRecipients) {
const addressParts = await extractAddressPartsFromHeader(recipient);
if (!addressParts || !addressParts.address) {
return;
}
allMailAddresses.push(addressParts.address);
}
return allMailAddresses;
}
function detectSubaddress(identityMail, relatedRecipients) {
const identityMailParts = identityMail.split('@');
const identityMailUser = escapeRegExp(identityMailParts[0]);
const identityMailDomain = escapeRegExp(identityMailParts[1]);
const subaddressRegex = new RegExp(identityMailUser + "[\-+].+@" + identityMailDomain, "i");
return relatedRecipients.find((possibleSubaddress) => {
return possibleSubaddress.match(subaddressRegex);
});
}
async function handleTabCreation(tab) {
if (tab.type !== 'messageCompose') {
return;
}
const composeDetails = await messenger.compose.getComposeDetails(tab.id);
if (composeDetails.type !== "reply") {
return;
}
if (!composeDetails.relatedMessageId) {
return;
}
const originalFrom = await extractAddressPartsFromHeader(composeDetails.from);
if (!originalFrom || !originalFrom.address) {
return;
}
const relatedMessage = await messenger.messages.get(composeDetails.relatedMessageId);
if (!relatedMessage) {
return;
}
const relatedRecipients = await extractRecipientMailAddresses(relatedMessage);
// If message was sent to original mail address we do not need to look for subaddresses.
if (relatedRecipients.includes(originalFrom.address)) {
return;
}
const matchingSubaddress = detectSubaddress(originalFrom.address, relatedRecipients);
if (!matchingSubaddress) {
return;
}
const newFrom = buildNewFrom(matchingSubaddress, originalFrom.name);
messenger.compose.setComposeDetails(tab.id, {from: newFrom});
}
browser.tabs.onCreated.addListener(handleTabCreation);