forked from byteclubfr/mailbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
396 lines (336 loc) · 11.3 KB
/
bot.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'use strict';
const imap = require('./imap');
const {
parseAddresses,
stripTags,
extractSignature
} = require('./helpers');
const {
MailParser
} = require('mailparser'); // Requires 0.x as 2.x will fail listing all attachments
const debug = require('debug')('mailbot');
const MATCH_CID = /<img .*?src=["']?cid:(.+?)(<|>|\n|"|'|\s|$).*?>/gi;
const REPLACE_CID = '{[CID($1)]}';
const MATCH_CID_TOKENS = /\{\[CID\(.*?\)\]\}/gi;
const CID_TOKEN_PREFIX_LEN = REPLACE_CID.indexOf('$1');
const CID_TOKEN_SUFFIX_LEN = REPLACE_CID.length - CID_TOKEN_PREFIX_LEN - 2;
const RE_SUBJECT_PREFIX = /^(?:(?:R[eé]f?|Fwd|Forward)[:\.]\s*)* /i;
const createBot = (conf = {}) => {
conf = Object.assign({
imap: Object.assign({
// user,
// password,
host: 'imap.googlemail.com',
port: 993,
keepalive: true,
tls: true,
tlsOptions: {
rejectUnauthorized: false
}
}, conf.imap),
mailbox: 'INBOX',
filter: ['UNSEEN'],
markSeen: true,
triggerOnHeaders: false,
trigger: mail => false, // eslint-disable-line no-unused-vars
mailHandler: (mail, trigger) => {}, // eslint-disable-line no-unused-vars
errorHandler: (error, context) => console.error('MailBot Error', context, error), // eslint-disable-line no-console
autoReconnect: true,
autoReconnectTimeout: 5000,
streamAttachments: true,
removeTextSignature: true,
ignoreAttachmentsInSignature: true,
cleanSubject: true,
searchPeriod: false // falsey to disable, otherwise milliseconds period
}, conf);
// Timeout instance for planned periodic search
// Note that this is bot-wide and not client-wide
// as client can be re-set during bot's life
let searchTimeout = null;
const handleError = (context, mail, uid) => error => {
debug('Error', context, error);
if (uid) {
// Remove from 'doneUids'
doneUids = doneUids.filter(_uid => _uid !== uid);
// Note: we don't automatically retry later, it could be an option
// Instead, this mail will not be checked again unless marked as unread (depends on options and filters) and a new mail is received
}
Promise.resolve()
.then(() => conf.errorHandler(error, context, mail))
.catch(err => console.error('MAILBOT: ErrorHandler Error!', context, err)); // eslint-disable-line no-console
};
const handleMail = (mail, triggerResult, uid) => {
Promise.resolve()
.then(() => formatMail(mail))
.then(() => {
conf.mailHandler(mail, triggerResult);
if (conf.markDeleted) {
client.seq.addFlags(uid, 'DELETED', err => debug(err));
}
})
.catch(handleError('MAIL', mail, uid));
};
// Reformat mail: ignore images embedded in signature, extract text signature, etc…
const formatMail = mail => {
// Extract text signature
if (conf.removeTextSignature && mail.text) {
const extract = extractSignature(mail.text);
mail.textOriginal = mail.text;
mail.textSignature = extract.signature;
mail.text = extract.text;
debug('Extracted text signature');
} else {
mail.textOriginal = null;
mail.textSignature = null;
}
// Ignore attachments embedded in signature
mail.ignoredAttachments = [];
if (conf.ignoreAttachmentsInSignature && mail.html && mail.attachments) {
// Replace IMG tags with CID by a token to not lose them when stripping tags
const html = mail.html.replace(MATCH_CID, REPLACE_CID);
const text = stripTags(html);
const extract = extractSignature(text);
if (extract && extract.signature) {
// Extract CID tokens from signature
const found = extract.signature.match(MATCH_CID_TOKENS) || [];
const cids = found.map(token => token.substring(CID_TOKEN_PREFIX_LEN, token.length - CID_TOKEN_SUFFIX_LEN));
const {
kept,
ignored
} = mail.attachments.reduce((result, attachment) => {
if (attachment.contentId && cids.includes(attachment.contentId)) {
result.ignored.push(attachment);
} else {
result.kept.push(attachment);
}
return result;
}, {
ignored: [],
kept: []
});
debug('Ignored attachments in HTML signature:', ignored.length);
mail.attachments = kept;
mail.ignoredAttachments = ignored;
}
}
// Cleanup subject
if (conf.cleanSubject) {
mail.cleanSubject = mail.subject.replace(RE_SUBJECT_PREFIX, '');
} else {
mail.cleanSubject = null;
}
};
// Do not fetch same mail multiple times to properly handle incoming emails
let doneUids = [];
// Current client:
// We replace the instance whenever connection info change
let client = null;
let shouldRecreateClient = false;
const initClient = () => {
// Interrupt and re-initialize any pending periodic search
clearTimeout(searchTimeout);
searchTimeout = null;
// Open mailbox
client.once('ready', () => {
debug('IMAP ready');
client.openBoxP(conf.mailbox, false)
.then(() => {
debug('Mailbox open');
return search();
})
.then(watch, watch); // whatever happened
});
const newMailSearch = nb => {
debug('New mail', nb);
search();
};
const periodicSearch = () => {
debug('Periodic search');
search();
};
const watch = () => client.on('mail', newMailSearch);
const search = () => {
// Whenever it comes in the middle of a scheduled search, cancel it
clearTimeout(searchTimeout);
return client.searchP(conf.filter)
.then(uids => {
const newUids = uids.filter(uid => !doneUids.includes(uid));
debug('Search', newUids);
// Optimistically mark uids as done, this prevents double-triggers if a mail
// is received while we're handling one and option 'markSeen' is not enabled
doneUids = uids;
return newUids;
})
.then(fetchAndParse)
.catch(handleError('SEARCH'))
.then(() => {
// Finally, plan a new search if applicable
if (conf.searchPeriod) {
searchTimeout = setTimeout(periodicSearch, conf.searchPeriod);
}
});
};
client.on('close', err => {
debug('IMAP disconnected', err);
if (err && conf.autoReconnect) {
debug('Trying to reconnect…');
setTimeout(() => client.connect(), conf.autoReconnectTimeout);
} else {
debug('No reconnection (user close or no autoReconnect)');
}
});
client.on('error', handleError('IMAP_ERROR'));
const fetchAndParse = source => {
debug('Fetch', source);
if (source.length === 0) {
return Promise.resolve([]);
}
const fetcher = client.fetch(source, {
bodies: '',
struct: true,
markSeen: conf.markSeen
});
fetcher.on('message', parseMessage);
return new Promise((resolve, reject) => {
fetcher.on('end', resolve);
fetcher.on('error', reject);
});
};
};
const parseMessage = (message, uid) => {
debug('Parse message');
const parser = new MailParser({
debug: conf.debugMailParser,
streamAttachments: conf.streamAttachments,
showAttachmentLinks: true
});
// Message stream, so we can interrupt parsing if required
let messageStream = null;
// Result of conf.trigger, testing if mail should trigger handler or not
let triggerResult;
if (conf.triggerOnHeaders) {
parser.on('headers', headers => {
triggerResult = Promise.resolve().then(() => conf.trigger({
headers
}));
triggerResult.then(result => {
if (result) {
debug('Triggered (on headers)', {
result,
subject: headers.subject
});
} else {
debug('Not triggered (on headers)', {
result,
subject: headers.subject
});
debug('Not triggered: Immediately interrupt parsing');
messageStream.pause();
parser.end();
}
return result;
})
.catch(() => {}); // Prevent unhandled rejection, it will be handled later catching triggerResult
});
}
// Once mail is ready and parsed…
parser.on('end', mail => {
// …check if it should trigger handler…
if (!conf.triggerOnHeaders) {
triggerResult = Promise.resolve().then(() => conf.trigger(mail));
triggerResult.then(result => {
if (result) {
debug('Triggered (on end)', {
result,
subject: mail.subject
});
} else {
debug('Not triggered (on end)', {
result,
subject: mail.subject
});
}
})
.catch(() => {}); // Prevent unhandled rejection, it will be handled later catching triggerResult
}
// …and handle it if applicable
triggerResult
.then(result => result && handleMail(mail, result, uid))
.catch(handleError('TRIGGER', mail, uid));
});
// Stream mail once ready
message.once('body', stream => {
messageStream = stream;
stream.pipe(parser);
});
};
// Public bot API
return {
start() {
debug('Connecting…');
if (!client || shouldRecreateClient) {
client = imap(conf.imap);
}
initClient();
client.connect();
this.client = client;
return new Promise((resolve, reject) => {
const onReady = () => {
debug('Connected!');
client.removeListener('error', onError);
resolve();
};
const onError = err => {
debug('Connection error!', err);
client.removeListener('ready', onReady);
reject(err);
};
client.once('ready', onReady);
client.once('error', onError);
});
},
stop(destroy = false) {
debug('Stopping (' + (destroy ? 'BRUTAL' : 'graceful') + ')…');
if (destroy) {
console.warn('destroy() should be used with high caution! Use graceful stop to remove this warning and avoid losing data.'); // eslint-disable-line no-console
}
client[destroy ? 'destroy' : 'end']();
this.client = null;
return new Promise((resolve, reject) => {
const onEnd = () => {
debug('Stopped!');
client.removeListener('error', onError);
resolve();
};
const onError = err => {
debug('Stop error!', err);
client.removeListener('end', onEnd);
reject(err);
};
client.once('end', onEnd);
client.once('error', onError);
});
},
restart(destroy = false) {
return this.stop(destroy).then(() => this.start());
},
configure(option, value, autoRestart = true, destroy = false) {
conf[option] = value;
if (autoRestart && (option === 'imap' || option === 'mailbox' || option === 'filter')) {
shouldRecreateClient = true;
return this.restart(destroy);
}
return Promise.resolve();
},
client: null
};
};
// Public API
module.exports = {
// Main function
createBot,
// Helpers
parseAddresses,
extractSignature,
stripTags
};