-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (67 loc) · 2.2 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
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var Newsletter = require('./newsletter')
var request = require('request')
exports.handler = (event, context, callback) => {
console.log('Process tl;dr email')
var sesNotification = event.Records[0].ses
console.log('SES Notification:\n', JSON.stringify(sesNotification, null, 2))
if (sesNotification.receipt.virusVerdict.status === 'FAIL'
|| sesNotification.receipt.spfVerdict.status === 'FAIL'
|| sesNotification.receipt.dkimVerdict.status === 'FAIL'
|| sesNotification.receipt.dmarcVerdict.status === 'FAIL') {
console.log('Dropping spam')
callback(null, {'disposition':'STOP_RULE'})
} else {
for (var index in sesNotification.mail.headers) {
var header = sesNotification.mail.headers[index]
if (header.name === 'From') {
if (/\@mozilla\.com>?\s*$/.test(header.value)) {
processMail(sesNotification, callback)
} else {
console.log('Email not from @mozilla.com address')
callback(null, {'disposition':'STOP_RULE'})
}
}
}
}
}
function processMail (sesNotification, callback) {
s3.getObject({
Bucket: process.env.DISCOURSE_TLDR_BUCKET,
Key: sesNotification.mail.messageId
}, (err, data) => {
if (err) {
console.log(err, err.stack)
callback(err)
} else {
console.log('Raw email fetched from S3')
new Newsletter().from_mail(data.Body).then(self => {
console.log('Posting: ' + self.title)
data = {
title: self.title,
raw: self.markdown,
category: process.env.DISCOURSE_TLDR_CATEGORY
}
request.post({
url: process.env.DISCOURSE_TLDR_URL + '/posts',
form: data,
headers: {
'Api-Key': process.env.DISCOURSE_TLDR_API_KEY,
'Api-Username': process.env.DISCOURSE_TLDR_API_USERNAME,
}
}, (err, res, body) => {
if (res.statusCode != 200) {
console.log('Posting to Discourse failed: ' + body)
callback(body)
} else {
callback(null, 'Posted to Discourse')
}
})
}).catch(err => {
console.log(err)
callback(err)
})
}
})
}