-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
342 lines (279 loc) · 11 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Intercepts send and checks number of To + CC recipients against prefs
// If limit exceeded consults user
// v2.1.0 Process nested mailing lists
extensionName = browser.i18n.getMessage("extensionName");
// Retrieve the stored prefs, or initialise them
// Same code here and in prefs.js
var prefs = new Object() ;
let gettingItem = browser.storage.local.get('prefs');
gettingItem.then(onGot, onError);
// Prefs got
function onGot(item) {
if (item['prefs']!=null) {
prefs = item['prefs'] ;
// Add any prefs added since version 1 & set them to default or special values
prefsAdded = false ;
if (typeof prefs['strictLimit'] == "undefined") {
prefs['strictLimit'] = false ;
prefsAdded = true ;
}
if (prefsAdded) {
browser.storage.local.set({'prefs': prefs})
.then( null, onError);
}
}
else {
// Set up defaults if prefs absent
prefs['maxNonBCC'] = 10 ;
prefs['strictLimit'] = false ;
browser.storage.local.set({'prefs': prefs})
.then( null, onError);
}
// console.log("Limit non-BCC recipients: limit: " + prefs['maxNonBCC'])
// Listen for about-to-be-sent messsage
async function listenforsend(tabid, details) { //15
// console.log("Details: "+JSON.stringify(details))
// Check recipients of a message
// console.log("To: "+ details['to'] )
// console.log("CC: "+ details['cc'] )
// console.log("BCC: "+ details['bcc'] )
// Build an array of To and CC recipients and remove exact duplicates
nonbccrecip = details['to'].concat(details['cc'])
nonbccweeded = [];
nonbccrecip.forEach(element => {
dup = false;
for (let i = 0; i < nonbccweeded.length; i++) {
if (element == nonbccweeded[i]) {
dup = true;
break;
};
};
if ( !dup ) { nonbccweeded.push(element) };
});
// console.log( "Weeded: " + nonbccweeded );
// Separate email addresses and lists (or posssible lists)
validaddresses = [];
possiblelists = [];
nonbccweeded.forEach(element => {
// A mailing list will be in the address-book format: xxxx <xxxx> where x is anything non-blank
// Treat anything else as an email address
// None of this _validates_ an email address.
var re1 = /^[^ ].* <.*>$/ ;
if (!re1.test(element)) {
validaddresses.push(element);
} else {
// If it ends with <yyyy@yyyy> where y is not space or @ treat it as an email address
// Note that it could still be a mailing list; if so we won't expand and count it
// except for special case below
var re2 = /<[^@ ][^@ ]*@[^@ ][^@ ]*>$/ ;
isaddress = re2.test(element) ;
if (isaddress) {
validaddresses.push(element);
// If the address ends with yyyy@zzzz where y is as above and z is not space or @ or .
// then treat it as a possible list as well. e.g friends@club
// If there is such a list then we will overcount by one, which is better than undercounting.
// A list with a period such as friends@sailing.club will not be expanded.
var re3 = /<[^@ ][^@ ]*@[^@ \.][^@ \.]*>$/ ;
if (re3.test(element)) {
possiblelists.push(element);
}
} else {
possiblelists.push(element);
}
}
});
// console.log("Valid email addresses: " + validaddresses);
// console.log("Possible lists: " + possiblelists);
addresscount = validaddresses.length ;
// Match possible lists with mailing lists and count their contacts
let listcontactspromise = new Promise((resolve) => { //8
listcontactsarray = [];
if ( possiblelists.length == 0) { //7.5
resolve(listcontactsarray);
} else {
// This section executed when a list has been found
// This function find addresses and nested lists with possiblelists
function processLists(possiblelists) { //7.4
listlistsarray = [];
return new Promise(resolve => { //7.2
browser.addressBooks.list(true)
.then(function(books) { //7
books.forEach(book => { //6
if (book.mailingLists) { //5
// console.log("Mailing list node: " + JSON.stringify(book));
book.mailingLists.forEach(list => { //4
// console.log("Mailing list name: " + list.name);
// Check each possible list for match on name
possiblelists.forEach(posslist => { //3
// List recipients are of the form "name <name>" - extract the name
possname = posslist.replace(/ <.*>$/,"");
// console.log("Possible list name: " + possname );
// console.log("This list name: " + list.name);
// console.log("This list contacts: " + JSON.stringify(list.contacts) );
if (possname == list.name) { //2
// Matches a list
// console.log("Matched list: " + possname + " Contacts: " + list.contacts.length);
// Collect the contacts from the list for later weeding of duplicates
// console.log("Mailing list node: " + JSON.stringify(list));
list.contacts.forEach(contact => { //1
// A contact may be a mailing list
// If it is in form yyy@yyy where y is not space or @ then treat it as an email address
primaryEmail = contact.properties.PrimaryEmail ;
var re4 = /[^@ ][^@ ]*@[^@ ][^@ ]*/ ;
isaddress = re4.test(primaryEmail) ;
if (isaddress) {
listcontactsarray.push(primaryEmail);
// console.log("Added contact: " + primaryEmail );
} else {
listlistsarray.push(primaryEmail);
// console.log("Added list-list " + primaryEmail );
}
}) //1
}; //2
}); //3
}); //4
}; //5
}); //6
// console.log("listlistsarray: " + listlistsarray );
// console.log("listcontactsarray: " + listcontactsarray );
resolve(listlistsarray);
}); //7
}); //7.2
}; //7.4
// async function to call processLists and await result
async function asyncCall() {
// Loop until processLists returns empty listlists = any nested lists expanded
continueLoop = true ;
while (continueLoop){
// console.log('calling processLists');
const listlists = await processLists(possiblelists) ;
// console.log("listlists 2: " + listlists);
// console.log("listcontactsarray 2: " + listcontactsarray );
if (listlists.length == 0) {
continueLoop = false ;
} else {
possiblelists = listlists ;
}
}
// console.log("listcontactsarray 3: " + listcontactsarray );
resolve(listcontactsarray);
}
asyncCall();
// End of list processing section
}; //7.5
}); //8
// Wait for all that
let listcontacts = await listcontactspromise ;
// Weed out duplicates within the list contacts.
// Note: Could count all the contacts in one go.
// But might report counts separately in future.
// console.log("List contacts: " + listcontacts );
listcontactsweeded = [] ;
listcontacts.forEach(element => {
dup = false;
// Check within listcontacts
for (let i = 0; i < listcontactsweeded.length; i++) {
if (element == listcontactsweeded[i]) {
dup = true;
break;
};
};
// Check within validaddresses - just email address part so remove name
for (let i = 0; i < validaddresses.length; i++) {
emailpart = validaddresses[i].replace(/^.*</, "") ;
emailpart = emailpart.replace(/>.*$/, "") ;
// console.log("Email part: " + emailpart ) ;
if (element == emailpart) {
dup = true;
break;
};
};
if ( !dup ) { listcontactsweeded.push(element) };
});
// console.log( "Weeded list contacts: " + listcontactsweeded );
// count the number of non-duplicates
listcount = listcontactsweeded.length ;
// Check if non-BCC count exceeds pref
// console.log("Addresses: " + addresscount + " Lists: " + listcount + " Limit: " + prefs['maxNonBCC']) ;
nonbcc = addresscount + listcount ;
if ( nonbcc > prefs['maxNonBCC']) {
// Non-BCC count exceeded. Open a dialogue popup
if (prefs['strictLimit']) {SL = 1} else {SL = 0}
//console.log("dialogue.html?" + nonbcc + "&m" + prefs['maxNonBCC'] + "&s" + SL);
createData={allowScriptsToClose: true,
titlePreface: extensionName,
width : 600,
height : 300,
type : "popup",
url : "dialogue.html?" + nonbcc + "&m" + prefs['maxNonBCC'] + "&s" + SL
};
var wid=null;
browser.windows.create(createData).then((win)=>{
wid=win.id;
});
// Wait for response from popup
let response = new Promise((resolve) => {
async function gotmessage(msg, sender) {
// console.log("Message: "+JSON.stringify(msg));
// console.log("Sender: " +JSON.stringify(sender));
// Only resolve responses from the dialogue window which will have frameID zero
if (sender.frameId == 0 ) {
browser.windows.remove(wid);
browser.runtime.onMessage.removeListener(gotmessage);
resolve(msg);
}
}
// There is a second listener for pref changes
browser.runtime.onMessage.addListener(gotmessage);
});
msgobj = await response ;
mess = msgobj['msg']
// console.log("Response: "+mess) ;
// Action according to response
var returnobj = new Object()
returnobj['details'] = {} ;
if (mess == "bcc_cancel") {
returnobj['cancel'] = true ;
}
if (mess == "bcc_ok_false") {
//TESTING - Change to true to stop sending
returnobj['cancel'] = false ;
}
if (mess == "bcc_ok_true") {
// Move To and CC to BCC
returnobj['details']['bcc'] = details['bcc'].concat(details['to'], details['cc'])
returnobj['details']['to'] = []
returnobj['details']['cc'] = []
// console.log("Returned compose object:" + JSON.stringify(returnobj))
//TESTING - Change to true to stop sending
returnobj['cancel'] = false ;
}
return returnobj ;
} else {
// Non-BCC count not exceeded - continue with send
var returnobj = new Object();
//TESTING - Change to true to stop sending
returnobj['cancel'] = false ;
return returnobj ;
}
}; //15
// Register the onBeforeSend listener
browser.compose.onBeforeSend.addListener(listenforsend) ;
// Listener for changed prefs - 2nd listener for messages
function gotmessage2(msg, sender) {
// console.log("Message2: "+JSON.stringify(msg));
// console.log("Sender2: " +JSON.stringify(sender));
// Only resolve responses from the prefs window which will have frameID non-zero
if (sender.frameId != 0 ) {
if (msg.maxNonBCC !== "undefined"){
prefs = msg ;
// console.log("Limit non-BCC recipients: limit changed to: " + prefs['maxNonBCC'])
}
}
}
browser.runtime.onMessage.addListener(gotmessage2);
}
// Error getting prefs
function onError(error) {
console.log("Limit non-BCC recipients: "+ error)
}