This repository was archived by the owner on Nov 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·78 lines (56 loc) · 2.04 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
'use strict';
// Load modules
const Hoek = require('hoek');
const Joi = require('joi');
const MailParser = require('mailparser');
const SmtpServer = require('smtp-server');
const Teamwork = require('teamwork');
const Wreck = require('wreck');
// Declare internals
const internals = {};
internals.schema = Joi.object({
host: Joi.string().default('0.0.0.0'),
port: Joi.number().integer().min(0).default(0),
onMessage: Joi.func().required(),
smtp: Joi.object({
onData: Joi.forbidden()
})
.unknown()
});
exports.Server = internals.Server = function (options) {
options = Joi.attempt(options, internals.schema, 'Invalid server options');
this._settings = Hoek.clone(options);
this._settings.smtp = this._settings.smtp || {};
this._settings.smtp.disabledCommands = this._settings.smtp.disabledCommands || ['AUTH'];
this._settings.smtp.logger = (this._settings.smtp.logger !== undefined ? this._settings.smtp.logger : false);
this._settings.smtp.onData = (stream, session, callback) => this._onMessage(stream, session, callback);
this._server = new SmtpServer.SMTPServer(this._settings.smtp);
};
internals.Server.prototype.start = async function () {
const team = new Teamwork();
this._server.listen(this._settings.port, this._settings.host, () => team.attend());
await team.work;
const address = this._server.server.address();
this.info = {
address: address.address,
port: address.port
};
};
internals.Server.prototype.stop = async function () {
const team = new Teamwork();
this._server.close(() => team.attend());
await team.work;
};
internals.Server.prototype._onMessage = async function (stream, session, callback) {
try {
const message = await Wreck.read(stream);
const mail = await MailParser.simpleParser(message);
mail.from = mail.from.value;
mail.to = mail.to.value;
this._settings.onMessage(null, mail);
}
catch (err) {
this._settings.onMessage(err);
}
return callback();
};