-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (65 loc) · 1.98 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
"use strict";
const debug = require("debug")("smtp-server");
const simpleParser = require("mailparser").simpleParser;
require("./src/db/mongo");
const Message = require("./src/db/models/message");
const messageHelper = require("./src/messageHelper");
const SmtpServer = require("./src/smtpServer");
const port = process.env.SMTP_PORT || 8025;
let socket = null;
const socketServer = require("./src/socket")();
socketServer.on("connection", (connection) => {
debug(`Socket client connected.`);
connection.on("close", function connectionCloseListener(socket) {
debug(`Socket client connection closed.`);
});
socket = connection;
});
socketServer.on("error", function errorListener(err) {
debug(`Socket Server error: ${err.toString()}`);
console.error(err);
});
const onData = async (stream, session, callback) => {
debug(`Message received.`);
simpleParser(stream)
.then((parsed) => {
const message = new Message({
text: parsed.text,
subject: parsed.subject,
date: parsed.date,
to: parsed.to.text || "",
from: parsed.from.text || "",
messageId: parsed.messageId,
});
for (const attachment of parsed.attachments) {
message.attachments.push({
content: attachment.content,
contentType: attachment.contentType,
contentDisposition: attachment.contentDisposition,
filename: attachment.filename,
size: attachment.size,
});
}
return message.save();
})
.then((savedDoc) => {
socket.write(messageHelper(savedDoc));
callback();
})
.catch((err) => {
console.error(err);
callback(`Message parsing error.`);
});
};
let authorizedClients = [];
if (typeof process.env.AUTHORIZE_CLIENTS !== "undefined") {
authorizedClients = process.env.AUTHORIZE_CLIENTS.split(",");
}
SmtpServer({
authorizedClients,
serverOpts: { onData },
})
.on("error", (err) => {
console.log("Error %s", err.message);
})
.listen(port);