forked from Flolagale/mailin
/
mailin.js
552 lines (456 loc) · 18.8 KB
/
mailin.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
'use strict';
var LanguageDetect = require('languagedetect');
var MailParser = require('mailparser').MailParser;
var _ = require('lodash');
var Promise = require('bluebird');
var htmlToText = require('html-to-text');
var events = require('events');
var fs = Promise.promisifyAll(require('fs'));
var path = require('path');
var request = require('superagent');
var shell = require('shelljs');
var util = require('util');
var SMTPServer = require('smtp-server').SMTPServer;
var uuid = require('node-uuid');
var dns = require('dns');
var extend = require('extend');
var logger = require('./logger');
var mailUtilities = Promise.promisifyAll(require('./mailUtilities'));
var Mailin = function () {
events.EventEmitter.call(this);
/* Set up the default options. */
this.configuration = {
host: '0.0.0.0',
port: 2500,
tmp: '.tmp',
webhook: 'http://localhost:3000/webhook',
disableWebhook: false,
logFile: null,
disableDkim: false,
disableSpf: false,
disableSpamScore: false,
verbose: false,
debug: false,
logLevel: 'info',
profile: false,
disableDNSValidation: true,
smtpOptions: {
banner: 'Mailin Smtp Server',
logger: false,
disabledCommands: ['AUTH']
}
};
/* The simplesmtp server instance, 'exposed' as an undocuumented, private
* member. It is not meant for normal usage, but is can be uuseful for
* Mailin hacking.
* The instance will be initialized only after that mailin.start() has been called. */
this._smtp = null;
};
util.inherits(Mailin, events.EventEmitter);
Mailin.prototype.start = function (options, callback) {
var _this = this;
options = options || {};
if (_.isFunction(options)) {
callback = options;
options = {};
}
var configuration = this.configuration;
extend(true, configuration, options);
configuration.smtpOptions.secure = Boolean(configuration.smtpOptions.secure);
callback = callback || function () {};
/* Create tmp dir if necessary. */
if (!fs.existsSync(configuration.tmp)) {
shell.mkdir('-p', configuration.tmp);
}
/* Log to a file if necessary. */
if (configuration.logFile) {
logger.setLogFile(configuration.logFile);
}
/* Set log level if necessary. */
if (configuration.logLevel) {
logger.setLevel(configuration.logLevel);
}
if (configuration.verbose) {
logger.setLevel('verbose');
logger.info('Log level set to verbose.');
}
if (configuration.debug) {
logger.info('Debug option activated.');
logger.setLevel('debug');
/* Enable debug for the simplesmtp server as well. */
configuration.smtpOptions.debug = true;
}
/* Basic memory profiling. */
if (configuration.profile) {
logger.info('Enable memory profiling');
setInterval(function () {
var memoryUsage = process.memoryUsage();
var ram = memoryUsage.rss + memoryUsage.heapUsed;
var million = 1000000;
logger.info('Ram Usage: ' + ram / million + 'mb | rss: ' + memoryUsage.rss / million +
'mb | heapTotal: ' + memoryUsage.heapTotal / million +
'mb | heapUsed: ' + memoryUsage.heapUsed / million);
}, 500);
}
/* Check the webhook validity. */
if (!configuration.disableWebhook) {
var url = configuration.webhook;
request
.head(url)
.timeout(3000)
.end(function (err, resp) {
if (err || resp.statusCode !== 200) {
logger.warn('Webhook ' + configuration.webhook +
' seems invalid or down. You may want to double check the webhook url.');
} else {
logger.info('Webhook ' + configuration.webhook + ' is valid, up and running.');
}
});
}
function validateAddress(addressType, email, envelope) {
return new Promise(function (resolve, reject) {
if (configuration.disableDnsLookup) {
return resolve();
}
try {
var validateEvent,
validationFailedEvent,
dnsErrorMessage,
localErrorMessage;
if (addressType === 'sender') {
validateEvent = 'validateSender';
validationFailedEvent = 'senderValidationFailed';
dnsErrorMessage = '450 4.1.8 <' + email + '>: Sender address rejected: Domain not found';
localErrorMessage = '550 5.1.1 <' + email + '>: Sender address rejected: User unknown in local sender table';
} else if (addressType === 'recipient') {
validateEvent = 'validateRecipient';
validationFailedEvent = 'recipientValidationFailed';
dnsErrorMessage = '450 4.1.8 <' + email + '>: Recipient address rejected: Domain not found';
localErrorMessage = '550 5.1.1 <' + email + '>: Recipient address rejected: User unknown in local recipient table';
} else {
// How are internal errors handled?
return reject(new Error('Address type not supported'));
}
if (!email) {
return reject(new Error(localErrorMessage));
}
var domain = /@(.*)/.exec(email)[1];
var validateViaLocal = function () {
if (_this.listeners(validateEvent).length) {
_this.emit(validateEvent, envelope, email, (function (err) {
if (err) {
_this.emit(validationFailedEvent, email);
return reject(new Error(localErrorMessage));
} else {
return resolve();
}
}));
} else {
return resolve();
}
};
var validateViaDNS = function () {
try {
dns.resolveMx(domain, (function (err, addresses) {
if (err || !addresses || !addresses.length) {
_this.emit(validationFailedEvent, email);
return reject(new Error(dnsErrorMessage));
}
validateViaLocal();
}));
} catch (e) {
return reject(e);
}
};
if (configuration.disableDNSValidation) {
validateViaLocal();
} else {
validateViaDNS();
}
} catch (e) {
reject(e);
}
});
}
function dataReady(connection) {
logger.info(connection.id + ' Processing message from ' + connection.envelope.mailFrom.address);
return retrieveRawEmail(connection)
.then(function (rawEmail) {
return Promise.all([
rawEmail,
validateDkim(connection, rawEmail),
validateSpf(connection),
computeSpamScore(connection, rawEmail),
parseEmail(connection)
]);
})
.spread(function (rawEmail, isDkimValid, isSpfValid, spamScore, parsedEmail) {
return Promise.all([
connection,
rawEmail,
isDkimValid,
isSpfValid,
spamScore,
parsedEmail,
detectLanguage(connection, parsedEmail.text)
]);
})
.spread(finalizeMessage)
.then(postWebhook.bind(null, connection))
.then(unlinkFile.bind(null, connection))
.catch(function (error) {
logger.error(connection.id + ' Unable to finish processing message!!', error);
logger.error(error);
throw error;
});
}
function retrieveRawEmail(connection) {
return fs.readFileAsync(connection.mailPath)
.then(function (rawEmail) {
return rawEmail.toString();
});
}
function validateDkim(connection, rawEmail) {
if (configuration.disableDkim) {
return Promise.resolve(false);
}
logger.verbose(connection.id + ' Validating dkim.');
return mailUtilities.validateDkimAsync(rawEmail)
.catch(function (err) {
logger.error(connection.id + ' Unable to validate dkim. Consider dkim as failed.');
logger.error(err);
return false;
});
}
function validateSpf(connection) {
if (configuration.disableSpf) {
return Promise.resolve(false);
}
logger.verbose(connection.id + ' Validating spf.');
/* Get ip and host. */
return mailUtilities.validateSpfAsync(connection.remoteAddress, connection.from, connection.clientHostname)
.catch(function (err) {
logger.error(connection.id + ' Unable to validate spf. Consider spf as failed.');
logger.error(err);
return false;
});
}
function computeSpamScore(connection, rawEmail) {
if (configuration.disableSpamScore) {
return Promise.resolve(0.0);
}
return mailUtilities.computeSpamScoreAsync(rawEmail)
.catch(function (err) {
logger.error(connection.id + ' Unable to compute spam score. Set spam score to 0.');
logger.error(err);
return 0.0;
});
}
function parseEmail(connection) {
return new Promise(function (resolve) {
logger.verbose(connection.id + ' Parsing email.');
/* Prepare the mail parser. */
var mailParser = new MailParser();
mailParser.on('end', function (mail) {
// logger.verbose(util.inspect(mail, {
// depth: 5
// }));
/* Make sure that both text and html versions of the
* body are available. */
if (!mail.text && !mail.html) {
mail.text = '';
mail.html = '<div></div>';
} else if (!mail.html) {
mail.html = _this._convertTextToHtml(mail.text);
} else if (!mail.text) {
mail.text = _this._convertHtmlToText(mail.html);
}
return resolve(mail);
});
/* Stream the written email to the parser. */
fs.createReadStream(connection.mailPath).pipe(mailParser);
});
}
function detectLanguage(connection, text) {
logger.verbose(connection.id + ' Detecting language.');
var language = '';
var languageDetector = new LanguageDetect();
var potentialLanguages = languageDetector.detect(text, 2);
if (potentialLanguages.length !== 0) {
logger.verbose('Potential languages: ' + util.inspect(potentialLanguages, {
depth: 5
}));
/* Use the first detected language.
* potentialLanguages = [['english', 0.5969], ['hungarian', 0.40563]] */
language = potentialLanguages[0][0];
} else {
logger.info(connection.id + ' Unable to detect language for the current message.');
}
return language;
}
function finalizeMessage(connection, rawEmail, isDkimValid, isSpfValid, spamScore, parsedEmail, language) {
/* Finalize the parsed email object. */
parsedEmail.dkim = isDkimValid ? 'pass' : 'failed';
parsedEmail.spf = isSpfValid ? 'pass' : 'failed';
parsedEmail.spamScore = spamScore;
parsedEmail.language = language;
/* Make fields exist, even if empty. That will make
* json easier to use on the webhook receiver side. */
parsedEmail.cc = parsedEmail.cc || [];
parsedEmail.attachments = parsedEmail.attachments || [];
/* Add the connection authentication to the parsedEmail. */
parsedEmail.connection = connection;
/* Add envelope data to the parsedEmail. */
parsedEmail.envelopeFrom = connection.envelope.mailFrom;
parsedEmail.envelopeTo = connection.envelope.rcptTo;
_this.emit('message', connection, parsedEmail, rawEmail);
return parsedEmail;
}
function postWebhook(connection, finalizedMessage) {
return new Promise(function (resolve) {
if (configuration.disableWebhook) return resolve();
logger.info(connection.id + ' Sending request to webhook ' + configuration.webhook);
/* Convert the attachments contents from Buffer to
* base64 encoded strings and remove them from the
* message. They will be posted as multipart of a form
* as key values pairs (attachmentName, attachmentContent). */
logger.profile('Convert attachments to strings');
var attachmentNamesAndContent = {};
finalizedMessage.attachments.forEach(function (attachment) {
attachmentNamesAndContent[attachment.generatedFileName] = (attachment.content || Buffer('')).toString('base64');
delete attachment.content;
});
logger.profile('Convert attachments to strings');
logger.debug(finalizedMessage);
var req = request.post(configuration.webhook);
req.field('mailinMsg', JSON.stringify(finalizedMessage));
_.forEach(attachmentNamesAndContent, function (content, name) {
req.field(name, content);
});
req.end(function (err, resp) {
/* Avoid memory leak by hinting the gc. */
if (err || resp.statusCode !== 200) {
logger.error(connection.id + ' Error in posting to webhook ' + configuration.webhook);
if (resp) {
logger.error(connection.id + ' Response status code: ' + resp.statusCode);
}
return resolve();
}
logger.info(connection.id + ' Succesfully posted to webhook ' + configuration.webhook);
logger.debug(resp.text);
return resolve();
});
});
}
function unlinkFile(connection) {
/* Don't forget to unlink the tmp file. */
return fs.unlinkAsync(connection.mailPath)
.then(function () {
logger.info(connection.id + ' End processing message, deleted ' + connection.mailPath);
return;
});
}
var _session;
function onData(stream, session, callback) {
_session = session;
var connection = _.cloneDeep(session);
connection.id = uuid.v4();
var mailPath = path.join(configuration.tmp, connection.id);
connection.mailPath = mailPath;
_this.emit('startData', connection);
logger.verbose('Connection id ' + connection.id);
logger.info(connection.id + ' Receiving message from ' + connection.envelope.mailFrom.address);
_this.emit('startMessage', connection);
stream.pipe(fs.createWriteStream(mailPath));
stream.on('data', function (chunk) {
_this.emit('data', connection, chunk);
});
stream.on('end', function () {
dataReady(connection);
callback();
});
stream.on('close', function () {
_this.emit('close', connection);
});
stream.on('error', function (error) {
_this.emit('error', connection, error);
});
}
function onAuth(auth, session, streamCallback) {
if (_this.emit('authorizeUser', session, auth.username, auth.password, streamCallback)) {
streamCallback(new Error('Unauthorized user'));
}
}
function onMailFrom(address, session, streamCallback) {
_this.emit('validateSender', session, address.address, streamCallback);
var ack = function (err) {
streamCallback(err);
};
validateAddress('sender', address.address, session.envelope)
.then(ack)
.catch(ack);
}
function onRcptTo(address, session, streamCallback) {
var ack = function (err) {
streamCallback(err);
};
_this.emit('validateRecipient', session, address.address, callback);
validateAddress('recipient', address.address, session.envelope)
.then(ack)
.catch(ack);
}
var smtpOptions = _.extend({}, configuration.smtpOptions || {}, {
onData: onData,
onAuth: onAuth,
onMailFrom: onMailFrom,
onRcptTo: onRcptTo
});
var server = new SMTPServer(smtpOptions);
this._smtp = server;
server.listen(configuration.port, configuration.host, function (err) {
if (!err) {
logger.info('Mailin Smtp server listening on port ' + configuration.port);
} else {
callback(err);
logger.error('Could not start server on port ' + configuration.port + '.');
if (configuration.port < 1000) {
logger.error('Ports under 1000 require root privileges.');
}
if (configuration.logFile) {
logger.error('Do you have write access to log file ' +
configuration.logFile + '?');
}
logger.error(err.message);
}
});
server.on('close', function () {
logger.info('Closing smtp server');
_this.emit('close', _session);
});
server.on('error', function (error) {
logger.error(error);
_this.emit('error', _session, error);
});
callback();
};
Mailin.prototype.stop = function (callback) {
callback = callback || function () {};
logger.info('Stopping mailin.');
/* FIXME A bug in the RAI module prevents the callback to be called, so
* call end and call the callback directly. */
this._smtp.close(callback);
callback();
};
Mailin.prototype._convertTextToHtml = function (text) {
/* Replace newlines by <br>. */
text = text.replace(/(\n\r)|(\n)/g, '<br>');
/* Remove <br> at the begining. */
text = text.replace(/^\s*(<br>)*\s*/, '');
/* Remove <br> at the end. */
text = text.replace(/\s*(<br>)*\s*$/, '');
return text;
};
Mailin.prototype._convertHtmlToText = function (html) {
return htmlToText.fromString(html);
};
module.exports = new Mailin();