2017-03-11 23:21:08 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-16 19:37:33 +08:00
|
|
|
const config = require('wild-config');
|
2017-06-03 14:51:58 +08:00
|
|
|
const log = require('npmlog');
|
|
|
|
const imap = require('./imap');
|
|
|
|
const pop3 = require('./pop3');
|
|
|
|
const lmtp = require('./lmtp');
|
|
|
|
const api = require('./api');
|
2017-10-26 15:21:32 +08:00
|
|
|
const plugins = require('./lib/plugins');
|
2017-06-03 14:51:58 +08:00
|
|
|
const db = require('./lib/db');
|
2017-08-29 15:36:45 +08:00
|
|
|
const errors = require('./lib/errors');
|
2017-03-11 23:21:08 +08:00
|
|
|
|
2017-07-16 19:37:33 +08:00
|
|
|
// preload certificate files
|
|
|
|
require('./lib/certs');
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
// Initialize database connection
|
|
|
|
db.connect(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2017-03-27 15:36:45 +08:00
|
|
|
log.error('Db', 'Failed to setup database connection');
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2017-03-27 15:36:45 +08:00
|
|
|
// Start IMAP server
|
|
|
|
imap(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2017-08-15 21:41:18 +08:00
|
|
|
log.error('App', 'Failed to start IMAP server. %s', err.message);
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2017-04-08 02:29:14 +08:00
|
|
|
// Start POP3 server
|
|
|
|
pop3(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2017-04-08 02:29:14 +08:00
|
|
|
log.error('App', 'Failed to start POP3 server');
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2017-04-12 19:52:29 +08:00
|
|
|
// Start LMTP maildrop server
|
|
|
|
lmtp(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2017-04-12 19:52:29 +08:00
|
|
|
log.error('App', 'Failed to start LMTP server');
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2017-03-27 15:36:45 +08:00
|
|
|
|
2017-04-08 02:29:14 +08:00
|
|
|
// Start HTTP API server
|
|
|
|
api(err => {
|
|
|
|
if (err) {
|
|
|
|
log.error('App', 'Failed to start API server');
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2017-04-08 02:29:14 +08:00
|
|
|
|
2017-11-06 23:32:45 +08:00
|
|
|
// downgrade user and group if needed
|
|
|
|
if (config.group) {
|
|
|
|
try {
|
|
|
|
process.setgid(config.group);
|
|
|
|
log.info('App', 'Changed group to "%s" (%s)', config.group, process.getgid());
|
|
|
|
} catch (E) {
|
|
|
|
log.error('App', 'Failed to change group to "%s" (%s)', config.group, E.message);
|
|
|
|
errors.notify(E);
|
2017-08-29 15:36:45 +08:00
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-04-08 02:29:14 +08:00
|
|
|
}
|
2017-11-06 23:32:45 +08:00
|
|
|
}
|
|
|
|
if (config.user) {
|
|
|
|
try {
|
|
|
|
process.setuid(config.user);
|
|
|
|
log.info('App', 'Changed user to "%s" (%s)', config.user, process.getuid());
|
|
|
|
} catch (E) {
|
|
|
|
log.error('App', 'Failed to change user to "%s" (%s)', config.user, E.message);
|
|
|
|
errors.notify(E);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-09-17 18:31:02 +08:00
|
|
|
}
|
2017-11-06 23:32:45 +08:00
|
|
|
}
|
2017-10-26 15:21:32 +08:00
|
|
|
|
2017-11-06 23:32:45 +08:00
|
|
|
plugins.init(err => {
|
|
|
|
if (err) {
|
|
|
|
log.error('App', 'Failed to start plugins');
|
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
2017-10-26 19:57:19 +08:00
|
|
|
|
2017-11-06 23:32:45 +08:00
|
|
|
plugins.runHooks('init', () => {
|
|
|
|
log.info('App', 'All servers started, ready to process some mail');
|
2017-10-26 15:21:32 +08:00
|
|
|
});
|
2017-09-17 18:31:02 +08:00
|
|
|
});
|
2017-04-08 02:29:14 +08:00
|
|
|
});
|
2017-03-11 23:21:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|