-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
259 lines (223 loc) · 6.61 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
//
// changelog
// v1.0
// - base implementation, inbox of all accounts is monitored per default
// - other folders can be enabled/disabled via the options page
// - on newmail event, window is set to draw attention
// v1.2
// - settings are automatically saved as they are changed
// v1.4
// - add account wide checkbox to select and deselect all folders at once
// v1.5
// - major overhaul of the settings handling
// v1.6
// - unlimitedStorage permission and hidden debug
(function() {
var um_debug = false;
var log = console.log;
var logDebug;
var useraccounts = [];
setDebug(um_debug);
init()
.then(initSettings, onError)
.then(storeSettings, onError)
.then(function start() {
log("urgentMail initialized");
logDebug(useraccounts);
browser.storage.onChanged.addListener(onSettingsChanged);
browser.messages.onNewMailReceived.addListener((folder, msgList) => onNewMailReceived(folder, msgList));
}, onError);
// ---
/*
* Initialize urgentMail
* populate settings with currently existing accounts/folders
*/
function init() {
return new Promise((resolve) => {
browser.accounts.list()
.then(function(acc) {
for (a of acc) {
logDebug (a);
if (a.type == "none") continue;
let accountObj = {
id: {
accountId: null,
name: null
},
folders: [],
monitored: []
};
accountObj.id.accountId = a.id;
accountObj.id.name = a.name;
for (fol of a.folders) {
accountObj.folders.push(fol);
// set default monitoring of inbox
if (fol.type == "inbox") {
accountObj.monitored.push(fol.path);
}
}
useraccounts.push(accountObj);
}
resolve("success");
}, onError);
});
}
/*
* Initialize or update settings
*/
function initSettings() {
return new Promise((resolve) => {
browser.storage.local.get(["accounts","debug"])
.then(function(pref) {
if (pref.debug != undefined) {
setDebug(pref.debug);
}
logDebug("old settings:");
logDebug(JSON.stringify(pref.accounts));
var newPrefs = {
accounts: useraccounts,
debug: um_debug
};
logDebug("new accounts:");
logDebug(JSON.stringify(newPrefs.accounts));
if (pref.accounts == undefined) {
logDebug("no previous settings found");
} else {
let update = updateSettings(pref.accounts, useraccounts);
update.then(function(newaccs) {
newPrefs.accounts = newaccs;
resolve(newPrefs);
});
}
resolve(newPrefs);
}, onError);
});
}
/*
* Store new settings
*/
function storeSettings(prefs) {
return new Promise((resolve) => {
logDebug("active settings:");
logDebug(prefs.accounts);
useraccounts = prefs.accounts;
browser.storage.local.set(prefs)
.then( function() { resolve("yay"); });
});
}
/*
* Check if a folder with given path exists in an account.
* this traverses the folder structure from top to bottom
* and stops if a folder in the hierarchy does not exist.
*/
function folderExists(a, path) {
var chunks = path.split(/\//).filter(e => e); /* split at '/' and remove empty chunks */
logDebug(`folder path split into chunks: ${chunks}`);
var i = 0;
var path = `/${chunks[i]}`;
var folders = a.folders;
var found = false;
while (!found) {
logDebug(`testing ${path} in:`);
logDebug(folders);
let f = folders.find(el => el.path == path);
logDebug(f);
if (f == undefined) {
// this chunk does not exist, no need to probe further
logDebug("folder not found");
break;
}
// so far all chunks have been found
if (chunks[++i] == null) {
// no more chunks; folder found
logDebug("no more chunks");
found = true;
} else if (f.subFolders.length == 0) {
// no more subfolders, but apparently more chunks
logDebug("no more subfolders");
break;
} else {
// traverse deeper
path += `/${chunks[i]}`
folders = f.subFolders;
}
}
return found;
}
/*
* update settings:
* start with current account/folder structure,
* then check for each monitored path in the old set
* wether that path still exists in the new set: if yes -> add to new monitored
*/
function updateSettings(oldSet, newSet) {
logDebug("updating settings");
return new Promise((resolve) => {
var updated = newSet;
for (let acc of updated) {
logDebug(`Updating account ${acc.id.accountId}`);
var old = oldSet.find(el => el.id.accountId == acc.id.accountId);
if (old == undefined) {
// account is not in old set, no settings to transfer
} else {
// reset defaults since we only want to apply existing settings
acc.monitored = [];
// for each monitored path in old, check if this folder exists in account
for (let folder of old.monitored) {
if (folderExists(acc, folder)) {
logDebug(`monitored folder ${folder} exists. keep monitoring`);
acc.monitored.push(folder);
} else {
logDebug(`monitored folder ${folder} does not exist anymore`);
}
}
}
}
// resolve with the updated settings
resolve(updated);
});
}
/*
* Handler for changed settings
*/
function onSettingsChanged() {
logDebug("settings changed!");
browser.storage.local.get(["accounts"])
.then(function(pref) {
useraccounts = pref.accounts;
}, onError);
}
/*
* Handler for new mail
*/
function onNewMailReceived(folder, msglist) {
logDebug("mail received event");
for (acc of useraccounts) {
if (acc.id.accountId != folder.accountId) continue;
logDebug("for " + acc.id.name + " in folder " + folder.name);
if (acc.monitored.includes(folder.path)) {
log("Hey " + acc.id.name + "! You got mail!");
notify();
}
}
}
/*
* Set window to draw attention
*/
function notify() {
browser.windows.getLastFocused({ populate: false }).then(win => {
browser.windows.update(win.id, {drawAttention: true});
});
}
function onError(e) {
console.error(e);
}
function setDebug(debug) {
um_debug = debug;
if (um_debug == true) {
logDebug = console.debug;
} else {
logDebug = function () { };
}
}
})();