-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple-Imap-Copy.js
executable file
·232 lines (172 loc) · 6.28 KB
/
Simple-Imap-Copy.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
#! /usr/bin/env node
/**
* Simple-Imap-Copy v0.1 by TimeWaster
*
* Licensed under Creative Commons CC0 1.0 Universal
*
* Configure your mail servers below.
* If your servers use special settings you can configure them following this documentation:
* https://github.com/mscdex/node-imap#user-content-connection-instance-methods
*
* --- ✂ ------ ✂ ------ ✂ ------ ✂ ---
*/
const sourceAccountConfig = {
user: '[email protected]',
password: 'password',
host: 'imap-server.tld',
port: 993,
tls: true,
};
const destinationAccountConfig = {
user: '[email protected]',
password: 'password',
host: 'imap-server.tld',
port: 993,
tls: true,
};
const debug = false;
/**
* --- ✂ ------ ✂ ------ ✂ ------ ✂ ---
*/
const Imap = require('imap');
const clc = require('cli-color');
const log = (text) => console.log(text);
const error = (text) => console.log(clc.redBright.bold(text));
const success = (text) => console.log(clc.greenBright(text));
const keepAliveConfig = { interval: 1000, forceNoop: true };
sourceAccountConfig.keepalive = keepAliveConfig;
destinationAccountConfig.keepalive = keepAliveConfig;
const srcServer = new Imap(sourceAccountConfig);
const destServer = new Imap(destinationAccountConfig);
const throwIt = (err, action) => {
srcServer.destroy();
destServer.destroy();
error(`\n${err.message} (${action})`);
if (debug) throw err;
process.exit(1);
};
const closeConnections = () => {
log(`\nAll done, closing connections`);
srcServer.end();
destServer.end();
};
const closeBox = (box, nextCallback) => {
destServer.closeBox((err) => {
if (err) throwIt(err);
success(`Destination server: ${box.dest} closed`);
srcServer.closeBox((err) => {
if (err) throwIt(err);
success(`Source server: ${box.src} closed`);
nextCallback();
});
});
};
const copyBox = (box, nextCallback) => {
srcServer.search(['ALL'], (err, results) => {
if (err) throwIt(err, 'Source Server: Search Mails');
if (!results || !results.length) {
log('No emails found');
closeBox(box, nextCallback);
return;
}
let mailCount = results.length;
log(`Emails to copy: ${mailCount}`);
const fetch = srcServer.fetch(results, { bodies: '' });
fetch.on('message', (msg, seqno) => {
let email;
let attrs;
msg.on('body', (stream) => {
stream.on('data', (chunk) => {
email += chunk.toString('utf8');
});
});
msg.once('attributes', (attributes) => {
attrs = attributes;
});
msg.once('end', () => {
destServer.append(email, { date: new Date(attrs.date), flags: attrs.flags }, (err) => {
if (err) throwIt(err, 'Destination Server: Create Mail');
success(`Copied email No. ${seqno}`);
if (--mailCount === 0) {
closeBox(box, nextCallback);
}
});
});
});
fetch.once('error', (err) => throwIt(err, 'Source Server: Fetch Mail'));
});
};
const openBox = (box, nextCallback) => {
srcServer.openBox(box.src, true, (err) => {
if (err) throwIt(err, 'Source Server: Open Box');
success(`Source server: ${box.src} opened`);
destServer.openBox(box.dest, false, (err) => {
if (err) throwIt(err, 'Destination Server: Open Box');
success(`Destination server: ${box.dest} opened`);
copyBox(box, nextCallback);
});
});
};
const boxCallback = (box, nextCallback) => () => {
log(`\nCopying ${box.src}`);
if (box.create) {
destServer.addBox(box.dest, (err) => {
if (err) throwIt(err, 'Destination Server: Create Box');
destServer.subscribeBox(box.dest, (err) => {
if (err) throwIt(err, 'Destination Server: Subscribe Box');
success(`Destination server: ${box.dest} was created`);
openBox(box, nextCallback);
});
});
} else {
openBox(box, nextCallback);
}
};
const parseBoxes = (boxes, delimiter, output = [], prefix = '') => {
for (const [key, value] of Object.entries(boxes)) {
const name = `${prefix}${key}`;
output.push(name);
if (value.children) parseBoxes(value.children, delimiter, output, `${name}${delimiter}`);
}
return output;
};
const loadBoxes = () => {
log('\nLoad existing boxes');
srcServer.getBoxes((err, srcBoxes) => {
if (err) throwIt(err, 'Source Server: Load Boxes');
success('Source server: Boxes loaded');
destServer.getBoxes((err, destBoxes) => {
if (err) throwIt(err, 'Destination Server: Load Boxes');
success('Destination server: Boxes loaded');
const srcBoxesParsed = parseBoxes(srcBoxes, srcServer.delimiter);
const destBoxesParsed = parseBoxes(destBoxes, destServer.delimiter);
const boxes = [];
srcBoxesParsed.forEach((srcBox) => {
const destBox = srcBox.replace(new RegExp(`[\\${srcServer.delimiter}]`, 'g'), destServer.delimiter);
boxes.push({ src: srcBox, dest: destBox, create: destBoxesParsed.includes(destBox) ? false : true });
});
log('\nStart copying boxes');
// node-imap does not expose the internal promises, make sure everything is executed in sequence
let callbackChain = closeConnections;
boxes.reverse().forEach((box) => {
callbackChain = boxCallback(box, callbackChain);
});
callbackChain();
});
});
};
const connectServer = (server, name, callback) => {
server.once('ready', () => {
success(`${name} server: Connected`);
callback();
});
server.once('error', (err) => throwIt(err, `${name} Server: Connect`));
server.once('end', () => success(`${name} server: Disconnected`));
server.connect();
};
try {
log('Connecting to servers:');
connectServer(srcServer, 'Source', () => connectServer(destServer, 'Destination', loadBoxes));
} catch (err) {
throwIt(err, 'Unknown');
}