-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (115 loc) · 3.13 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
var fs = require('fs');
var log4js = require('log4js');
log4js.configure('log4js.json', {reloadSecs: 10});
var loggerSrc = log4js.getLogger('imapSrc');
var loggerDest = log4js.getLogger('imapDest');
var config = require('./config');
var Imap = require('imap');
var reconnectDelay = 5000;
var destReady = false;
var untreatedResults;
var tryReconnectSrc;
var tryReconnectDest;
loggerSrc.info('Starting...');
var imapSrc = new Imap(config.src);
imapSrc.on('error', function (err) {
loggerSrc.error(err);
});
imapSrc.on('end', function () {
loggerSrc.info('Connection ended');
tryReconnectSrc = setInterval(function () {
loggerSrc.info('Try reconnecting');
imapSrc.connect();
}, reconnectDelay);
});
imapSrc.connect();
var imapDest = new Imap(config.dest);
imapDest.on('error', function (err) {
loggerDest.error(err);
});
imapDest.on('end', function () {
loggerDest.info('Connection ended');
destReady = false;
tryReconnectDest = setInterval(function () {
loggerDest.info('Try reconnecting');
imapDest.connect();
}, reconnectDelay);
});
imapDest.connect();
function forwardMail(mail, seqNumber) {
loggerDest.debug('Forwarding mail #%s', seqNumber);
imapDest.append(
mail,
{
mailbox: 'INBOX'
}
);
}
function fetchAndForward(results) {
var f = imapSrc.fetch(
results,
{
markSeen: true,
bodies: ''
}
);
f.on('message', function (message, seqNumber) {
loggerSrc.debug('Treating mail #%s', seqNumber);
message.on('body', function (stream) {
var mail = '';
stream.on('data', function (chunk) {
mail += chunk;
});
stream.on('end', function () {
forwardMail(mail, seqNumber);
});
});
});
f.on('error', function (err) {
loggerSrc.error('Fetch error: %s', err);
});
f.on('end', function () {
loggerSrc.info('Done fetching all messages!');
untreatedResults = null;
});
}
function unseen() {
imapSrc.search(['UNSEEN'], function (err, results) {
if (err) {
loggerSrc.error(err);
throw err;
}
loggerSrc.info('%s unread mail', results.length);
if (results.length > 0) {
if (destReady) {
fetchAndForward(results);
} else {
untreatedResults = results;
loggerSrc.info('Untreated results: %s', untreatedResults);
}
}
})
}
imapDest.on('ready', function () {
loggerDest.info('Ready');
destReady = true;
clearInterval(tryReconnectDest);
if (untreatedResults) {
fetchAndForward(untreatedResults);
}
});
imapSrc.on('ready', function () {
loggerSrc.info('Ready');
clearInterval(tryReconnectSrc);
imapSrc.openBox('INBOX', false, function (err) {
if (err) {
loggerSrc.error(err);
throw err;
}
unseen();
imapSrc.on('mail', function (nb) {
loggerSrc.info('%s new mail', nb);
unseen();
});
});
});