forked from Pranav-Dakshina/mail-listener2
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
144 lines (135 loc) · 4.32 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
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
/**@module mail-listener5
* @author Matej Malicek <[email protected]>
* @version 2.1.2
* @date 26 January 2023
*/
// Require statements
var Imap = require('imap');
var EventEmitter = require('events').EventEmitter;
var simpleParser = require('mailparser').simpleParser;
var fs = require('fs');
var async = require('async');
class MailListener extends EventEmitter {
constructor(options) {
super();
this.markSeen = !!options.markSeen;
this.mailbox = options.mailbox || 'INBOX';
if ('string' === typeof options.searchFilter) {
this.searchFilter = [options.searchFilter];
}
else {
this.searchFilter = options.searchFilter || ['UNSEEN'];
}
this.fetchUnreadOnStart = !!options.fetchUnreadOnStart;
this.mailParserOptions = options.mailParserOptions || {};
if (options.attachments && options.attachmentOptions && options.attachmentOptions.stream) {
this.mailParserOptions.streamAttachments = true;
}
this.attachmentOptions = options.attachmentOptions || {};
this.attachments = options.attachments || false;
this.attachmentOptions.directory = (this.attachmentOptions.directory ? this.attachmentOptions.directory : '');
this.imap = new Imap({
xoauth2: options.xoauth2,
user: options.username,
password: options.password,
host: options.host,
port: options.port,
tls: options.tls,
autotls: options.autotls || null,
tlsOptions: options.tlsOptions || {},
connTimeout: options.connTimeout || null,
authTimeout: options.authTimeout || null,
debug: options.debug || null
});
this.imap.once('ready', this.imapReady.bind(this));
this.imap.once('close', this.imapClose.bind(this));
this.imap.on('error', this.imapError.bind(this));
}
start() {
this.imap.connect();
}
stop() {
this.imap.end();
}
imapReady() {
this.imap.openBox(this.mailbox, false, (error, mailbox) => {
if (error) {
this.emit('error', error);
}
else {
this.emit('server:connected');
this.emit('mailbox', mailbox);
if (this.fetchUnreadOnStart) {
this.parseUnread.call(this);
}
let listener = this.imapMail.bind(this);
this.imap.on('mail', listener);
this.imap.on('update', listener);
}
});
}
imapClose() {
this.emit('server:disconnected');
}
imapError(error) {
if (error) {
this.emit('error', error);
}
}
imapMail() {
this.parseUnread.call(this);
}
parseUnread() {
let self = this;
self.imap.search(self.searchFilter, (error, results) => {
if (error) {
self.emit('error', error);
}
else if (results.length > 0) {
async.each(results, (result, callback) => {
let f = self.imap.fetch(result, {
bodies: '',
markSeen: self.markSeen
});
f.on('message', (msg, seqno) => {
let attrs;
msg.on('attributes', (a) => {
attrs = a;
});
msg.on('body', async (stream, info) => {
let parsed = await simpleParser(stream);
self.emit('mail', parsed, seqno, attrs);
self.emit('headers', parsed.headers, seqno, attrs);
self.emit('body', { html: parsed.html, text: parsed.text, textAsHtml: parsed.textAsHtml }, seqno, attrs);
if (parsed.attachments.length > 0) {
for (let att of parsed.attachments) {
if (self.attachments) {
await fs.writeFile(`${self.attachmentOptions.directory}${att.filename}`, att.content, (error) => {
if (error) {
self.emit('error', error);
}
});
self.emit('attachment', att, `${self.attachmentOptions.directory}${att.filename}`, seqno, attrs);
}
else {
self.emit('attachment', att, null, seqno, attrs);
}
}
}
});
});
f.once('error', (error) => {
if (error) {
self.emit('error', error);
}
});
}, (error) => {
if (error) {
self.emit('error', error);
}
});
}
});
}
};
module.exports.MailListener = MailListener;