-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
609 lines (489 loc) · 13.3 KB
/
index.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
let AWS = require('aws-sdk');
let moment = require('moment');
let parser = require("mailparser").simpleParser;
//
// Initialize S3.
//
let s3 = new AWS.S3({
apiVersion: '2006-03-01'
});
//
// Initialize SES.
//
let ses = new AWS.SES({
apiVersion: '2010-12-01'
});
//
// This variable will hold all the domain available in SES so we don't
// have to query SES each time the Lambda runs, we query SES only when
// the lambda starts from scratch to circumvent the 1 sec request limit.
//
let domains = [];
//
// This Lambda will filter all the incoming emails based on their From and To
// field.
//
exports.handler = (event) => {
//
// 1. This JS object will contain all the data within the chain.
//
let container = {
bucket: event.Records[0].s3.bucket.name,
unescaped_key: '',
escaped_key: event.Records[0].s3.object.key,
domains: domains,
folder: "Sent"
};
//
// -> Start the chain.
//
get_email_domains(container)
.then(function(container) {
return unescape_key(container);
}).then(function(container) {
return load_the_email(container);
}).then(function(container) {
return parse_the_email(container);
}).then(function(container) {
return format_time(container);
}).then(function(container) {
return extract_data(container);
}).then(function(container) {
return where_to_save(container);
}).then(function(container) {
return create_s3_boject_key(container);
}).then(function(container) {
return copy_email_to_inbox(container);
}).then(function(container) {
return copy_email_to_today(container);
}).then(function(container) {
return delete_the_email(container);
}).then(function(container) {
return true;
}).catch(function(error) {
console.error(error);
return false;
});
};
// _____ _____ ____ __ __ _____ _____ ______ _____
// | __ \ | __ \ / __ \ | \/ | |_ _| / ____| | ____| / ____|
// | |__) | | |__) | | | | | | \ / | | | | (___ | |__ | (___
// | ___/ | _ / | | | | | |\/| | | | \___ \ | __| \___ \
// | | | | \ \ | |__| | | | | | _| |_ ____) | | |____ ____) |
// |_| |_| \_\ \____/ |_| |_| |_____| |_____/ |______| |_____/
//
//
// List all the emails added to SES, so we can use them to decided where to
// store the emails. If in the Inbox, or sent. This is important when you
// upload emails by hand to back them up.
//
// Without doing this everything goes to the Inbox folder.
//
function get_email_domains(container)
{
return new Promise(function(resolve, reject) {
//
// 1. If we have already the domains we don't query SES anymore
// since you can only query SES once a second. To solve this
// limitation we grab the data once, and save it in to memory.
//
if(container.domains.length)
{
//
// -> Move to the next chain.
//
return resolve(container);
}
console.info("get_email_domains");
//
// 2. Create the query.
//
let params = {
IdentityType: "Domain",
MaxItems: 100
};
//
// -> Execute the query.
//
ses.listIdentities(params, function(error, data) {
//
// 1. Check for internal errors.
//
if(error)
{
console.error(params);
return reject(error);
}
container.domains = data.Identities;
//
// -> Move to the next chain.
//
return resolve(container);
});
});
}
//
// We need to process the path received by S3 since AWS dose escape
// the string in a special way. They escape the string in a HTML style
// but for whatever reason they convert spaces in to +ses.
//
function unescape_key(container)
{
return new Promise(function(resolve, reject) {
console.info("unescape_key");
//
// 1. First we convert the + in to spaces.
//
let plus_to_space = container.escaped_key.replace(/\+/g, ' ');
//
// 2. And then we unescape the string, other wise we lose
// real + characters.
//
let unescaped_key = decodeURIComponent(plus_to_space);
//
// 3. Save the result for the next promise.
//
container.unescaped_key = unescaped_key;
//
// -> Move to the next chain.
//
return resolve(container);
});
}
//
// Load the email that we received from SES.
//
function load_the_email(container)
{
return new Promise(function(resolve, reject) {
console.info("load_the_email");
//
// 1. Set the query.
//
let params = {
Bucket: container.bucket,
Key: container.unescaped_key
};
//
// -> Execute the query.
//
s3.getObject(params, function(error, data) {
//
// 1. Check for internal errors.
//
if(error)
{
console.error(params);
return reject(error);
}
//
// 2. Save the email for the next promise
//
container.raw_email = data.Body
//
// -> Move to the next chain.
//
return resolve(container);
});
});
}
//
// Once the raw email is loaded we parse it with one goal in mind, get
// the date the of the email. This way we don't rely on the SES date, but
// on the real date the email was created.
//
// This way we can even load in to the system old emails as long as they
// are in the standard raw email format, and not some proprietary solution.
//
// That why will be organized with the time the emails were created, and not
// received in to the system.
//
function parse_the_email(container)
{
return new Promise(function(resolve, reject) {
console.info("parse_the_email");
//
// 1. Parse the email and extract all the it necessary.
//
parser(container.raw_email, function(error, data) {
//
// 1. Check for internal errors.
//
if(error)
{
console.error(data);
return reject(error);
}
//
// 2. Save the parsed email for the next promise.
//
container.date = data.date;
container.from = data.from.value[0].address,
container.to = data.to.value[0].address,
container.subject = data.subject || "No Subject",
container.message_id = data.messageId
//
// -> Move to the next chain.
//
return resolve(container);
});
});
}
//
// We format the date and time to make sure that when the folder is saved
// in S3, the object will sort one after the other which makes it much
// easier to see the latest emails.
//
function format_time(container)
{
return new Promise(function(resolve, reject) {
console.info('format_time');
//
// 1. Format the date found in the email message itself.
//
container.date = moment(container.date).format("YYYY-MM-DD HH:mm:ss Z");
//
// -> Move to the next chain.
//
return resolve(container);
});
}
//
// Extract all the data necessary to organize the incoming emails.
//
function extract_data(container)
{
return new Promise(function(resolve, reject) {
console.info("extract_data");
//
// 1. Since the email string can come in a form of:
//
// Name Last <name@example.com>
//
// We have to extract just the email address, and discard
// the rest.
//
let tmp_to = container
.to
.match(/(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gm)[0]
.split('@');
let tmp_from = container
.from
.match(/(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gm)[0]
.split('@');
//
// 2. Get the domain name of the receiving end, so we can group
// emails by all the domain that were added to SES.
//
container.to_domain = tmp_to[1];
//
// 3. Based on the email name, we replace all the + characters, that
// can be used to organize ones on-line accounts in to /, this way
// we can build a S3 patch which will automatically organize
// all the email in structured folder.
//
container.to_account = tmp_to[0].replace(/\+/g, "/");
//
// 4. Get the domain name of the email which in our case will
// become the company name.
//
container.from_domain = tmp_from[1];
//
// 5. Get the name of who sent us the email.
//
container.from_account = tmp_from[0];
//
// 6. Set all the strings to lower case, because some times different
// on-line services will have your email in all caps. For example
// Priceline will do this.
//
// Since in the next step we compare the domain that is in SES
// with the data from the email, wee need to have it all in the
// same format for the if() statement to work correctly and
// generate the right path where to save the email.
//
container.to_domain = container.to_domain.toLowerCase();
container.to_account = container.to_account.toLowerCase();
container.from_domain = container.from_domain.toLowerCase();
container.from_account = container.from_account.toLowerCase();
//
// 7. S3 objects have a limit of how they they can be named
// so we remove everything but...
//
container.subject = container.subject.replace(/[^a-zA-Z0-9 &@:,$=+?;]/g, "_");
//
// -> Move to the next chain.
//
return resolve(container);
});
}
//
// Once we have the domains from SES, and we know the value of the To
// field, we can decide where the emails should be saved.
//
function where_to_save(container)
{
return new Promise(function(resolve, reject) {
console.info("where_to_save");
//
// 1. Since by default we assume that the email should be saved
// in the Sent folder, we need to loop over the SES domains
// and check if there is a match with the To: domain found in
// the email.
//
container.domains.forEach(function(domain) {
if(domain == container.to_domain)
{
container.folder = "Inbox";
}
});
//
// -> Move to the next chain.
//
return resolve(container);
});
}
//
// Create Key path for the S3 object to be saved to.
//
function create_s3_boject_key(container)
{
return new Promise(function(resolve, reject) {
console.info("create_s3_boject_key");
//
// 1. Create the path where the email needs to be moved
// so it is properly organized.
//
container.path = container.to_domain
+ "/"
+ container.to_account
+ "/"
+ container.from_domain
+ "/"
+ container.from_account
+ "/"
+ container.date
+ " - "
+ container.subject
+ "/"
+ "email.eml";
//
// -> Move to the next chain.
//
return resolve(container);
});
}
//
// Copy the email to a new location - we don't put the email that we
// already have in memory since the system requires a COPY action and not
// a PUT action.
//
// WARNING: We are using the escaped_key value, because there is a
// know bug in the AWS JS SDK which won't unescape the
// string, so you have to do it - AWS is aware of this issue.
//
function copy_email_to_inbox(container)
{
return new Promise(function(resolve, reject) {
console.info("copy_email_to_inbox");
//
// 1. Set the query.
//
let params = {
Bucket: container.bucket,
CopySource: container.bucket + '/' + container.escaped_key,
Key: container.folder + "/" + container.path
};
//
// -> Execute the query.
//
s3.copyObject(params, function(error, data) {
//
// 1. Check for internal errors.
//
if(error)
{
console.error(params);
return reject(error);
}
//
// -> Move to the next chain.
//
return resolve(container);
});
});
}
//
// Similarly to the previous promise, but in this case we COPY the email to
// the Today folder to showcase all the latest emails that were received
// within 24h.
//
// There is a Lifecycle rule set on the bucket to delete any object inside
// the Today folder if it is older then 24h.
//
// This way it is easier to see all the latest emails.
//
function copy_email_to_today(container)
{
return new Promise(function(resolve, reject) {
console.info("copy_email_to_today");
//
// 1. Set the query.
//
let params = {
Bucket: container.bucket,
CopySource: container.bucket + '/' + container.escaped_key,
Key: "Today/" + container.path
};
//
// -> Execute the query.
//
s3.copyObject(params, function(error, data) {
//
// 1. Check for internal errors.
//
if(error)
{
console.error(params);
return reject(error);
}
//
// -> Move to the next chain.
//
return resolve(container);
});
});
}
//
// Delete the original message.
//
function delete_the_email(container)
{
return new Promise(function(resolve, reject) {
console.info("delete_the_email");
//
// 1. Set the query.
//
let params = {
Bucket: container.bucket,
Key: container.unescaped_key
};
//
// -> Execute the query.
//
s3.deleteObject(params, function(error, data) {
//
// 1. Check for internal errors.
//
if(error)
{
console.error(params);
return reject(error);
}
//
// -> Move to the next chain.
//
return resolve(container);
});
});
}