forked from maildev/maildev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (100 loc) · 2.58 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
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
/**
* MailDev - index.js
*
* Author: Dan Farrelly <[email protected]>
* Licensed under the MIT License.
*/
const program = require('commander')
const async = require('async')
const pkg = require('./package.json')
const web = require('./lib/web')
const mailserver = require('./lib/mailserver')
const logger = require('./lib/logger')
const { options, appendOptions } = require('./lib/options')
module.exports = function (config) {
const version = pkg.version
if (!config) {
// CLI
config = appendOptions(program.version(version).allowUnknownOption(true), options)
.parse(process.argv)
.opts()
}
if (config.verbose) {
logger.setLevel(2)
} else if (config.silent) {
logger.setLevel(0)
}
// Start the Mailserver
mailserver.create(
config.smtp,
config.ip,
config.mailDirectory,
config.incomingUser,
config.incomingPass,
config.hideExtensions,
config.incomingSecure,
config.incomingCert,
config.incomingKey
)
if (
config.outgoingHost ||
config.outgoingPort ||
config.outgoingUser ||
config.outgoingPass ||
config.outgoingSecure
) {
mailserver.setupOutgoing(
config.outgoingHost,
parseInt(config.outgoingPort),
config.outgoingUser,
config.outgoingPass,
config.outgoingSecure
)
}
if (config.autoRelay) {
const emailAddress = typeof config.autoRelay === 'string' ? config.autoRelay : null
mailserver.setAutoRelayMode(true, config.autoRelayRules, emailAddress)
}
if (config.mailDirectory) {
mailserver.loadMailsFromDirectory()
}
// Start the web server
if (!config.disableWeb) {
const secure = {
https: config.https,
cert: config.httpsCert,
key: config.httpsKey
}
// Default to run on same IP as smtp
const webIp = config.webIp ? config.webIp : config.ip
web.start(
config.web,
webIp,
mailserver,
config.webUser,
config.webPass,
config.basePathname,
secure
)
// Close the web server when the mailserver closes
mailserver.on('close', web.close)
}
if (config.logMailContents) {
mailserver.on('new', function (mail) {
const mailContents = JSON.stringify(mail, null, 2)
logger.info(`Received the following mail contents:\n${mailContents}`)
})
}
function shutdown () {
logger.info('Received shutdown signal, shutting down now...')
async.parallel([
mailserver.close,
web.close
], function () {
process.exit(0)
})
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
return mailserver
}