-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
233 lines (165 loc) · 3.97 KB
/
monitor.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
/**
* dependencies
*/
var notifier = require('mail-notifier'),
events = require('events'),
util = require('util');
/**
* @constructor
* @description Generates a Monitor object
*/
function Monitor(){
// create emitter
events.EventEmitter.call(this);
};
/**
* extend the prototype & export the module
*/
util.inherits(Monitor, events.EventEmitter);
exports.Monitor = Monitor;
/**
* @method setup
* @memberOf Monitor
* @param {Object} config
* @description Setups the module
*/
Monitor.prototype.setup = function(config){
var self = this;
// cache config
this.config = config;
// setup self repair counter
this.repairCounter = 0;
// update flag
this.forceFail = config.forceFail;
// create instance
this.notifier = notifier(config.imap);
// setup events
this.notifier.on('mail', function(mail){
// trigger custom event
self.emit('monitor:mail:new', mail);
if (mail.html) {
// cache last email
self.lastMail = mail;
}
});
this.notifier.on('error', function(error){
// trigger custom event
self.emit('monitor:mail:error', error);
});
};
/**
* @method start
* @memberOf Monitor
* @param {Object} emailer
* @param {Object} message
* @param {Object} transport
* @description Starts monitoring
*/
Monitor.prototype.start = function(emailer, message, transport){
var self = this;
// start IMAP
this.notifier.start();
// send test message on every 60 minutes
this.interval = setInterval(function(){
self.send(emailer, message, transport);
}, this.config.timers.send);
// send test message on startup
if (this.config.startImmediately) {
this.send(emailer, message, transport);
}
};
/**
* @method stop
* @memberOf Monitor
* @description Stops monitoring
*/
Monitor.prototype.stop = function(){
// stop IMAP
this.notifier.stop();
// clear the interval
clearInterval(this.interval);
// clear check timer
if (this.timer) {
clearTimeout(this.timer);
}
};
/**
* @method send
* @memberOf Monitor
* @param {Object} emailer
* @param {Object} message
* @param {Object} transport
* @description Sends a test message
*/
Monitor.prototype.send = function(emailer, message, transport){
// generate random string & update the message
this.currRandom = message.text = '[' + this.random() + ']';
// send the test message
emailer.send(message, transport);
};
/**
* @method check
* @memberOf Monitor
* @description Checks the email
*/
Monitor.prototype.check = function(){
var self = this;
// clear timer
if (this.timer) {
clearTimeout(this.timer);
}
if (this.warningTimer) {
clearTimeout(this.warningTimer);
}
// set warning timer
this.warningTimer = setTimeout(function(){
if (!self.lastMail && self.currRandom) {
self.emit('monitor:warning');
}
}, this.config.timers.warning);
// set timer
this.timer = setTimeout(function(){
// clear warning timer
clearTimeout(self.warningTimer);
if (self.currRandom) {
if (self.lastMail) {
// get id
var matches = self.lastMail.html.match("\\[.*\\]");
if (matches) {
var id = matches[0];
// force fail at startup
if (self.forceFail) {
self.forceFail = false;
id = 'willfail';
}
// check id
if (id != self.currRandom) {
self.emit('monitor:check:error', { id: id, reason: '[Wrong ID]' });
// update counter
self.repairCounter++;
} else {
self.emit('monitor:check:success', id);
if (self.repairCounter > 0) {
self.repairCounter = 0;
self.emit('monitor:repair');
}
}
}
// reset lastMail
delete self.lastMail;
} else {
self.emit('monitor:check:error', { id: self.currRandom, reason: '[No new mail]' });
// update counter
self.repairCounter++;
}
}
}, this.config.timers.check);
};
/**
* @method random
* @memberOf Monitor
* @description Generates a random string
*/
Monitor.prototype.random = function(){
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
};