-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_labels.gs
339 lines (296 loc) · 7.86 KB
/
apply_labels.gs
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
var Log = function(out) {
if (DEBUG)
{
Logger.log(out);
}
};
var Labeler = function() {
this.labelCache = {};
}
Labeler.prototype = {
test: function() {
return this.labelCache;
},
findOrCreateLabel: function(name) {
if (this.labelCache[name] === undefined) {
var labelObject = GmailApp.getUserLabelByName(name);
if( labelObject ){
this.labelCache[name] = labelObject;
} else {
this.labelCache[name] = GmailApp.createLabel(name);
Log("Created new label: [" + name + "]");
}
}
return this.labelCache[name];
},
getLabel: function(name) {
// create nested labels by parsing "/"
var label;
var labelName = "";
var self = this;
name.split('/').forEach(function(labelPart, i) {
labelName = labelName + (i===0 ? "" : "/") + labelPart.trim();
label = self.findOrCreateLabel(labelName);
});
return label;
},
applyLabel: function(name, thread){
var label = this.getLabel(name);
thread.addLabel(label);
},
removeLabel: function(name, thread) {
var label = this.getLabel(name);
thread.removeLabel(label);
},
};
var LabelMessage = function(message, filter) {
this.message = message;
this.filter = filter;
};
var getFilters = function() {
return {
to: function(filter, message) {
var str = message.getTo();
var match = filter.exec(str);
Log("Match to attempt=" + (match != null ? 'true' : 'false') + " " + str);
return match;
},
from: function(filter, message) {
var str = message.getFrom()
var match = filter.exec(str);
Log("Match from attempt=" + (match != null ? 'true' : 'false') + " " + str);
return match;
},
subject: function(filter, message)
{
var str = message.getSubject()
var match = filter.exec(str);
Log("Match subject attempt=" + (match != null ? 'true' : 'false') + " " + str);
return match;
},
body: function(filter, message) {
var str = message.getPlainBody()
var match = filter.exec(str);
Log("Match body attempt=" + (match != null ? 'true' : 'false') + " " + message.getSubject());
return match;
},
cc: function(filter, message) {
var str = message.getCc()
var match = filter.exec(str);
Log("Match body attempt=" + (match != null ? 'true' : 'false') + " " + message.getCc());
return match;
}
}
}
LabelMessage.prototype = {
doesMatch: function() {
var message=this.message;
var filter=this.filter;
var filterer = getFilters(message);
var filters = filter.filters;
if (!filters)
{
filters = []
for (key in filterer)
{
if (filter.hasOwnProperty(key))
{
filters.push([key, filter[key]]);
}
}
}
var matches = undefined;
matches = undefined;
Log("Current filter: (" + filter.id + ") " + message.getSubject());
for (v in filters) {
v = filters[v];
if (DEBUG_FILTER && DEBUG_FILTER != filter.id)
{
Log("Skipping filter " + filter.id + " looking for " + DEBUG_FILTER);
continue;
}
var filter_name = v[0];
var filter_match = v[1];
Log("filter=" + filter_name)
var negate_result = false;
if (filter_name[0] == "!")
{
negate_result = true;
filter_name = filter_name.substr(1);
}
Log("filter loop=" + filter_name + " matches=" + matches);
if (!filterer.hasOwnProperty(filter_name))
{
throw "Unknwon filter type " + filter_name;
}
var current_match = filterer[filter_name](filter_match, message);
if (negate_result)
{
if (current_match == null)
{
matches = ["negate"];
}
else
{
matches = null;
}
}
else
{
matches = current_match;
}
Log("negate=" + (negate_result ? 'true' : 'false') + ' result=' + (matches !== null ? 'true' : 'false'))
if (matches === null)
{
break;
}
}
return matches;
},
getLabelsAndMark: function(matches, archive) {
var message=this.message;
var filter=this.filter;
Log("matches=" + matches)
if (matches !== undefined && matches !== null) {
var label;
if (filter.name)
{
label = filter.name;
}
else if (matches[1])
{
label = matches[1];
if (filter.prefix_name)
label = filter.prefix_name + label;
}
// toggle flags
if (filter.star)
message.star();
if (filter.markRead)
message.markRead();
// prevent archive if filter explicitly sets "archive" to false (if "archive" is not defined, continue)
if (filter.archive !== undefined && !filter.archive)
archive = false;
if (filter.archive == true && archive == undefined)
archive = true;
return [label, archive];
}
else
{
Log("Filter not matched: " + message.getFrom());
return false;
}
},
}
var LabelThread = function(thread, filters, post, label) {
this.thread = thread;
this.filters = filters;
this.post = post;
this.label = label;
}
LabelThread.prototype = {
applyLabels: function() {
var messages = this.thread.getMessages();
if (messages == null)
return; // nothing to do
var labels = {}
var archive;
for (var message in messages) {
message = messages[message];
for (var filter in this.filters)
{
filter = this.filters[filter];
var applyLabel = function(filter) {
var labeler = new LabelMessage(message, filter);
var match = labeler.doesMatch();
if (match)
{
var label = labeler.getLabelsAndMark(match, archive);
if (label)
{
labels[label[0]] = true;
archive = label[1];
}
}
return match;
}
if (Array.isArray(filter))
{
for (var f in filter)
{
f = filter[f];
if (applyLabel(f))
{
break;
}
}
}
else
{
applyLabel(filter);
}
}
}
Log("Apply labels")
Log(labels)
labels = Object.keys(labels);
for (var label in labels) {
label = labels[label];
this.label.applyLabel(label, this.thread);
};
if (archive) {
this.post_process(this.post.archive);
}
else {
this.post_process(this.post.not_archive);
}
},
post_process: function(post) {
if (post.archive) {
this.thread.moveToArchive();
}
var process = function(labeler, funcname, thread, value)
{
if (value instanceof Function) {
value = value(thread);
}
if (!Array.isArray(value))
{
value = [value]
}
for (var label in value) {
label = value[label];
labeler[funcname](label, thread);
}
}
if (post.removeLabel) {
process(this.label, "removeLabel", this.thread, post.removeLabel);
}
if (post.addLabel) {
process(this.label, "applyLabel", this.thread, post.addLabel);
}
if (post.inbox) {
this.thread.moveToInbox();
}
}
}
var labeler = function(filters, post_process, selection) {
var threads = GmailApp.search(selection, 0, BATCH_SIZE);
var labeler = new Labeler();
if (filters) {
GmailApp.getMessagesForThreads(threads);
}
Log("Starting up");
for (var thread in threads)
{
thread = threads[thread];
var thread_labeler = new LabelThread(thread, filters, post_process, labeler);
if (filters) {
thread_labeler.applyLabels();
}
else {
//TODO: maybe we can do in bulk?
thread_labeler.post_process(post_process.not_archive);
}
}
}