forked from nodemailer/wildduck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap.js
298 lines (258 loc) · 8.92 KB
/
imap.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
'use strict';
const log = require('npmlog');
const config = require('wild-config');
const IMAPServerModule = require('./imap-core');
const IMAPServer = IMAPServerModule.IMAPServer;
const ImapNotifier = require('./lib/imap-notifier');
const Indexer = require('./imap-core/lib/indexer/indexer');
const MessageHandler = require('./lib/message-handler');
const UserHandler = require('./lib/user-handler');
const MailboxHandler = require('./lib/mailbox-handler');
const { SettingsHandler } = require('./lib/settings-handler');
const db = require('./lib/db');
const packageData = require('./package.json');
const certs = require('./lib/certs');
const Gelf = require('gelf');
const os = require('os');
const Lock = require('ioredfour');
const onFetch = require('./lib/handlers/on-fetch');
const onAuth = require('./lib/handlers/on-auth');
const onList = require('./lib/handlers/on-list');
const onLsub = require('./lib/handlers/on-lsub');
const onSubscribe = require('./lib/handlers/on-subscribe');
const onUnsubscribe = require('./lib/handlers/on-unsubscribe');
const onCreate = require('./lib/handlers/on-create');
const onRename = require('./lib/handlers/on-rename');
const onDelete = require('./lib/handlers/on-delete');
const onOpen = require('./lib/handlers/on-open');
const onStatus = require('./lib/handlers/on-status');
const onAppend = require('./lib/handlers/on-append');
const onStore = require('./lib/handlers/on-store');
const onExpunge = require('./lib/handlers/on-expunge');
const onCopy = require('./lib/handlers/on-copy');
const onMove = require('./lib/handlers/on-move');
const onSearch = require('./lib/handlers/on-search');
const onGetQuotaRoot = require('./lib/handlers/on-get-quota-root');
const onGetQuota = require('./lib/handlers/on-get-quota');
const onXAPPLEPUSHSERVICE = require('./lib/handlers/on-xapplepushservice');
let logger = {
info(...args) {
args.shift();
log.info('IMAP', ...args);
},
debug(...args) {
args.shift();
log.silly('IMAP', ...args);
},
error(...args) {
args.shift();
log.error('IMAP', ...args);
}
};
let indexer;
let notifier;
let messageHandler;
let userHandler;
let mailboxHandler;
let loggelf;
let createInterface = (ifaceOptions, callback) => {
// Setup server
const serverOptions = {
secure: ifaceOptions.secure,
secured: ifaceOptions.secured,
disableSTARTTLS: ifaceOptions.disableSTARTTLS,
ignoreSTARTTLS: ifaceOptions.ignoreSTARTTLS,
useProxy: !!config.imap.useProxy,
ignoredHosts: config.imap.ignoredHosts,
id: {
name: config.imap.name || 'WildDuck IMAP Server',
version: config.imap.version || packageData.version,
vendor: config.imap.vendor || 'Kreata'
},
aps: config.imap.aps,
logger,
maxMessage: config.imap.maxMB * 1024 * 1024,
settingsHandler: ifaceOptions.settingsHandler,
enableCompression: !!config.imap.enableCompression,
skipFetchLog: config.log.skipFetchLog,
SNICallback(servername, cb) {
certs
.getContextForServername(
servername,
serverOptions,
{
source: 'imap'
},
{
loggelf: message => loggelf(message)
}
)
.then(context => cb(null, context))
.catch(err => cb(err));
}
};
certs.loadTLSOptions(serverOptions, 'imap');
serverOptions.logoutMessages = config.imap.quotes;
const server = new IMAPServer(serverOptions);
certs.registerReload(server, 'imap');
let started = false;
server.on('error', err => {
if (!started) {
started = true;
return callback(err);
}
logger.error(
{
err
},
'%s',
err.message
);
});
// TODO: is this even used anywhere?
server.indexer = indexer;
server.notifier = notifier;
server.lock = new Lock({
redis: db.redis,
namespace: 'mail'
});
// setup command handlers for the server instance
server.onFetch = onFetch(server, messageHandler, userHandler.userCache);
server.onAuth = onAuth(server, userHandler, userHandler.userCache);
server.onList = onList(server);
server.onLsub = onLsub(server);
server.onSubscribe = onSubscribe(server);
server.onUnsubscribe = onUnsubscribe(server);
server.onCreate = onCreate(server, mailboxHandler);
server.onRename = onRename(server, mailboxHandler);
server.onDelete = onDelete(server, mailboxHandler);
server.onOpen = onOpen(server);
server.onStatus = onStatus(server);
server.onAppend = onAppend(server, messageHandler, userHandler.userCache);
server.onStore = onStore(server);
server.onExpunge = onExpunge(server, messageHandler);
server.onCopy = onCopy(server, messageHandler);
server.onMove = onMove(server, messageHandler);
server.onSearch = onSearch(server);
server.onGetQuotaRoot = onGetQuotaRoot(server);
server.onGetQuota = onGetQuota(server);
server.onXAPPLEPUSHSERVICE = onXAPPLEPUSHSERVICE(server);
if (loggelf) {
server.loggelf = loggelf;
}
// start listening
server.listen(ifaceOptions.port, ifaceOptions.host, () => {
if (started) {
return server.close();
}
started = true;
callback(null, server);
});
};
module.exports = done => {
if (!config.imap.enabled) {
return setImmediate(() => done(null, false));
}
const component = config.log.gelf.component || 'wildduck';
const hostname = config.log.gelf.hostname || os.hostname();
const gelf =
config.log.gelf && config.log.gelf.enabled
? new Gelf(config.log.gelf.options)
: {
// placeholder
emit: (key, message) => log.info('Gelf', JSON.stringify(message))
};
loggelf = message => {
if (typeof message === 'string') {
message = {
short_message: message
};
}
message = message || {};
if (!message.short_message || message.short_message.indexOf(component.toUpperCase()) !== 0) {
message.short_message = component.toUpperCase() + ' ' + (message.short_message || '');
}
message.facility = component; // facility is deprecated but set by the driver if not provided
message.host = hostname;
message.timestamp = Date.now() / 1000;
message._component = component;
Object.keys(message).forEach(key => {
if (!message[key]) {
delete message[key];
}
});
try {
gelf.emit('gelf.log', message);
} catch (err) {
log.error('Gelf', err);
}
};
indexer = new Indexer({
database: db.database
});
// setup notification system for updates
notifier = new ImapNotifier({
database: db.database,
redis: db.redis
});
messageHandler = new MessageHandler({
users: db.users,
database: db.database,
redis: db.redis,
gridfs: db.gridfs,
attachments: config.attachments,
loggelf: message => loggelf(message)
});
userHandler = new UserHandler({
database: db.database,
users: db.users,
redis: db.redis,
loggelf: message => loggelf(message)
});
mailboxHandler = new MailboxHandler({
database: db.database,
users: db.users,
redis: db.redis,
notifier,
loggelf: message => loggelf(message)
});
let settingsHandler = new SettingsHandler({ db: db.database });
let ifaceOptions = [
{
enabled: true,
secure: config.imap.secure,
disableSTARTTLS: config.imap.disableSTARTTLS || false,
ignoreSTARTTLS: config.imap.ignoreSTARTTLS || false,
host: config.imap.host,
port: config.imap.port,
settingsHandler
}
]
.concat(config.imap.interface || [])
.filter(iface => iface.enabled);
let iPos = 0;
let startInterfaces = () => {
if (iPos >= ifaceOptions.length) {
return db.redis.del('lim:imap', () => done());
}
let opts = ifaceOptions[iPos++];
createInterface(opts, err => {
if (err) {
logger.error(
{
err,
tnx: 'bind'
},
'Failed starting %sIMAP interface %s:%s. %s',
opts.secure ? 'secure ' : '',
opts.host,
opts.port,
err.message
);
return done(err);
}
setImmediate(startInterfaces);
});
};
setImmediate(startInterfaces);
};