/
interceptDirectReplyEmails.js
268 lines (231 loc) · 6.36 KB
/
interceptDirectReplyEmails.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
import { Meteor } from 'meteor/meteor';
import { settings } from '../../../settings';
import IMAP from 'imap';
import POP3Lib from 'poplib';
import { simpleParser } from 'mailparser';
import { processDirectEmail } from '.';
export class IMAPIntercepter {
constructor() {
this.imap = new IMAP({
user: settings.get('Direct_Reply_Username'),
password: settings.get('Direct_Reply_Password'),
host: settings.get('Direct_Reply_Host'),
port: settings.get('Direct_Reply_Port'),
debug: settings.get('Direct_Reply_Debug') ? console.log : false,
tls: !settings.get('Direct_Reply_IgnoreTLS'),
connTimeout: 30000,
keepalive: true,
});
this.delete = settings.get('Direct_Reply_Delete') ? settings.get('Direct_Reply_Delete') : true;
// On successfully connected.
this.imap.on('ready', Meteor.bindEnvironment(() => {
if (this.imap.state !== 'disconnected') {
this.openInbox(Meteor.bindEnvironment((err) => {
if (err) {
throw err;
}
// fetch new emails & wait [IDLE]
this.getEmails();
// If new message arrived, fetch them
this.imap.on('mail', Meteor.bindEnvironment(() => {
this.getEmails();
}));
}));
} else {
console.log('IMAP didnot connected.');
this.imap.end();
}
}));
this.imap.on('error', (err) => {
console.log('Error occurred ...');
throw err;
});
}
openInbox(cb) {
this.imap.openBox('INBOX', false, cb);
}
start() {
this.imap.connect();
}
isActive() {
if (this.imap && this.imap.state && this.imap.state === 'disconnected') {
return false;
}
return true;
}
stop(callback = new Function) {
this.imap.end();
this.imap.once('end', callback);
}
restart() {
this.stop(() => {
console.log('Restarting IMAP ....');
this.start();
});
}
// Fetch all UNSEEN messages and pass them for further processing
getEmails() {
this.imap.search(['UNSEEN'], Meteor.bindEnvironment((err, newEmails) => {
if (err) {
console.log(err);
throw err;
}
// newEmails => array containing serials of unseen messages
if (newEmails.length > 0) {
const f = this.imap.fetch(newEmails, {
// fetch headers & first body part.
bodies: ['HEADER.FIELDS (FROM TO DATE MESSAGE-ID)', '1'],
struct: true,
markSeen: true,
});
f.on('message', Meteor.bindEnvironment((msg, seqno) => {
const email = {};
msg.on('body', (stream, info) => {
let headerBuffer = '';
let bodyBuffer = '';
stream.on('data', (chunk) => {
if (info.which === '1') {
bodyBuffer += chunk.toString('utf8');
} else {
headerBuffer += chunk.toString('utf8');
}
});
stream.once('end', () => {
if (info.which === '1') {
email.body = bodyBuffer;
} else {
// parse headers
email.headers = IMAP.parseHeader(headerBuffer);
email.headers.to = email.headers.to[0];
email.headers.date = email.headers.date[0];
email.headers.from = email.headers.from[0];
}
});
});
// On fetched each message, pass it further
msg.once('end', Meteor.bindEnvironment(() => {
// delete message from inbox
if (this.delete) {
this.imap.seq.addFlags(seqno, 'Deleted', (err) => {
if (err) { console.log(`Mark deleted error: ${ err }`); }
});
}
processDirectEmail(email);
}));
}));
f.once('error', (err) => {
console.log(`Fetch error: ${ err }`);
});
}
}));
}
}
export class POP3Intercepter {
constructor() {
this.pop3 = new POP3Lib(settings.get('Direct_Reply_Port'), settings.get('Direct_Reply_Host'), {
enabletls: !settings.get('Direct_Reply_IgnoreTLS'),
debug: settings.get('Direct_Reply_Debug') ? console.log : false,
});
this.totalMsgCount = 0;
this.currentMsgCount = 0;
this.pop3.on('connect', Meteor.bindEnvironment(() => {
this.pop3.login(settings.get('Direct_Reply_Username'), settings.get('Direct_Reply_Password'));
}));
this.pop3.on('login', Meteor.bindEnvironment((status) => {
if (status) {
// run on start
this.pop3.list();
} else {
console.log('Unable to Log-in ....');
}
}));
// on getting list of all emails
this.pop3.on('list', Meteor.bindEnvironment((status, msgcount) => {
if (status) {
if (msgcount > 0) {
this.totalMsgCount = msgcount;
this.currentMsgCount = 1;
// Retrieve email
this.pop3.retr(this.currentMsgCount);
} else {
this.pop3.quit();
}
} else {
console.log('Cannot Get Emails ....');
}
}));
// on retrieved email
this.pop3.on('retr', Meteor.bindEnvironment((status, msgnumber, data) => {
if (status) {
// parse raw email data to JSON object
simpleParser(data, Meteor.bindEnvironment((err, mail) => {
this.initialProcess(mail);
}));
this.currentMsgCount += 1;
// delete email
this.pop3.dele(msgnumber);
} else {
console.log('Cannot Retrieve Message ....');
}
}));
// on email deleted
this.pop3.on('dele', Meteor.bindEnvironment((status) => {
if (status) {
// get next email
if (this.currentMsgCount <= this.totalMsgCount) {
this.pop3.retr(this.currentMsgCount);
} else {
// parsed all messages.. so quitting
this.pop3.quit();
}
} else {
console.log('Cannot Delete Message....');
}
}));
// invalid server state
this.pop3.on('invalid-state', function(cmd) {
console.log(`Invalid state. You tried calling ${ cmd }`);
});
// locked => command already running, not finished yet
this.pop3.on('locked', function(cmd) {
console.log(`Current command has not finished yet. You tried calling ${ cmd }`);
});
}
initialProcess(mail) {
const email = {
headers: {
from: mail.from.text,
to: mail.to.text,
date: mail.date,
'message-id': mail.messageId,
},
body: mail.text,
};
processDirectEmail(email);
}
}
export let POP3;
export class POP3Helper {
constructor() {
this.running = false;
}
start() {
// run every x-minutes
if (settings.get('Direct_Reply_Frequency')) {
POP3 = new POP3Intercepter();
this.running = Meteor.setInterval(() => {
// get new emails and process
POP3 = new POP3Intercepter();
}, Math.max(settings.get('Direct_Reply_Frequency') * 60 * 1000, 2 * 60 * 1000));
}
}
isActive() {
return this.running;
}
stop(callback = new Function) {
if (this.isActive()) {
Meteor.clearInterval(this.running);
}
callback();
}
}