-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
Copy pathkumar_asshole.js
executable file
·113 lines (90 loc) · 3.65 KB
/
kumar_asshole.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
#!/usr/bin/env node
/* Before running:
npm install nodemailer
npm install imap
I realize this is long. IMAP can only fetch emails and nodemailer can only
send. Could try implementing with Gmail Node API later.
*/
var GMAIL_USERNAME = process.env['GMAIL_USERNAME'];
var GMAIL_PASSWORD = process.env['GMAIL_PASSWORD'];
var KUMAR_EMAIL = process.env['KUMAR_EMAIL'];
var EMAIL = 'No worries mate, be careful next time';
// Scan for unread email from Kumar
var Imap = require('imap');
var imap = new Imap({
user: GMAIL_USERNAME,
password: GMAIL_PASSWORD,
host: 'imap.gmail.com',
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false }
});
function openInbox(cb) {
imap.openBox('INBOX', false, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
if (err)
process.exit(1);
imap.search(['UNSEEN', ['FROM', KUMAR_EMAIL]],
function(err, results) {
if (err)
process.exit(1);
// RegEx search for keywords; ignore case
var kumarPattern = new RegExp(/sorry|help|wrong/i);
// IMAP dumps all headers, so need to parse and get email body
var MailParser = require("mailparser").MailParser;
var f = imap.fetch(results, {bodies: ''});
f.on('message', function(msg, seqno) {
msg.on('body', function(stream, info) {
var kumarEmail = "";
stream.on('data', function(chunk) {
kumarEmail += chunk.toString('utf8');
});
stream.once('end', function() {
var mailparser = new MailParser();
mailparser.on("end", function(mail_object){
// If the RegEx matches
if(mail_object.text.match(kumarPattern)) {
// Shoot email to Kumar!
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: GMAIL_USERNAME,
pass: GMAIL_PASSWORD
}
});
// setup e-mail data
var mailOptions = {
from: GMAIL_USERNAME,
to: KUMAR_EMAIL,
subject: 'Database Fixes',
text: EMAIL
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info) {
if(error)
process.exit(1)
});
}
});
mailparser.write(kumarEmail);
mailparser.end();
});
});
msg.once('end', function() {
// Fetched all unread from kumar
});
});
f.once('error', function(err) {
process.exit(1);
});
f.once('end', function() {
imap.end();
});
});
});
});
imap.connect();