forked from jcreigno/nodejs-mail-notifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
134 lines (123 loc) · 4 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
/*jslint node: true, vars: true, indent: 4 */
'use strict';
var util = require('util'),
Imap = require('imap'),
debug = require('debug'),
MailParser = require('mailparser').MailParser,
EventEmitter = require('events').EventEmitter;
var dbg = debug('mailnotifier');
function Notifier(opts, dbg) {
EventEmitter.call(this);
var self = this;
self.options = opts;
if (self.options.username) { //backward compat
self.options.user = self.options.username;
}
self.options.box = self.options.box || 'INBOX';
self.options.debug = self.options.debug || debug('imap');
if(dbg) {
self.dbg = dbg;
}
}
util.inherits(Notifier, EventEmitter);
module.exports = function (opts, customDbg) {
return new Notifier(opts, customDbg);
};
Notifier.prototype.start = function () {
var self = this;
self.imap = new Imap(self.options);
self.imap.once('end', function () {
self.dbg('imap end');
self.emit('end');
});
self.imap.once('error', function (err) {
self.dbg('imap error : %s', err);
self.emit('error', err);
});
self.imap.once('close', function (haserr) {
self.dbg('imap close : %s', haserr ? 'errored' : 'normal');
});
self.imap.on('uidvalidity', function (uidvalidity) {
self.dbg('new uidvalidity : %s', uidvalidity);
});
self.imap.once('ready', function () {
self.emit('connected');
self.imap.openBox(self.options.box, false, function (err, box) {
if (err) {
self.dbg('unable to open box : %s', err);
self.emit('error', err);
return;
}
self.scan();
self.imap.on('mail', function (id) {
self.dbg('mail event : %s', id);
self.scan();
});
});
});
self.imap.connect();
return this;
};
Notifier.prototype.scan = function () {
var self = this, search = self.options.search || ['UNSEEN'];
self.dbg('scanning %s with filter `%s`.', self.options.box, search);
self.imap.search(search, function (err, seachResults) {
if (err) {
self.emit('error', err);
return;
}
if (!seachResults || seachResults.length === 0) {
self.dbg('no new mail in %s', self.options.box);
return;
}
self.dbg('found %d new messages', seachResults.length);
var fetch = self.imap.fetch(seachResults, {
markSeen: self.options.markSeen !== false,
bodies: ''
});
fetch.on('message', function (msg) {
var mp = new MailParser();
mp.once('end', function (mail) {
self.emit('mail', mail);
});
var uid;
msg.once('end', function () {
if (self.options.markDeleted && uid) {
self.imap.addFlags(uid, ['DELETED'], function (err) {
if (err) { self.emit('error', err); }
});
}
if (self.options.autoExpunge) {
self.imap.expunge(function (err) {
if (err) { self.emit('error', err); }
});
}
});
msg.once('attributes', function (attrs) {
uid = attrs.uid;
});
msg.once('body', function (stream, info) {
stream.pipe(mp);
});
});
fetch.once('end', function () {
self.dbg('Done fetching all messages!');
});
fetch.once('error', function (err) {
self.dbg('fetch error : ', err);
self.emit('error', err);
});
});
return this;
};
Notifier.prototype.stop = function () {
self.dbg('imap.state before stopping: %s', this.imap.state);
if (this.imap.state !== 'disconnected') {
this.imap.end();
}
self.dbg('notifier stopped');
return this;
};
Notifier.prototype.dbg = function (...args) {
dbg(...args);
}