forked from byskr/node-bounce-handler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (54 loc) · 1.33 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
'use strict';
const MailBounceSnoop = require('./mail-bounce-snoop').MailBounceSnoop;
const Email = require('./email');
function detectCb(email, detail, cb) {
let bounceHandler = new MailBounceSnoop();
let result = bounceHandler.parse_email(email, detail);
cb(result)
}
function detectPromise(email, detail) {
return new Promise((resolve, reject) => {
try {
detectCb(email, detail, resolve);
} catch (e) {
reject(e)
}
});
}
function isBounce(result) {
return result.is === 'bounce'
}
module.exports = {
email: null,
init: function (mail) {
this.email = new Email(mail)
},
isBounced: function (cb) {
if (typeof cb === 'undefined') {
return detectPromise(this.email, false).then(result => {
return isBounce(result)
});
}
detectCb(this.email, false, (result) => {
cb(isBounce(result))
})
},
isBouncedEmail: function (email, cb) {
this.init(email);
return this.isBounced(cb);
},
getBouncedDetail: function (cb) {
if (typeof cb === 'undefined') {
return detectPromise(this.email, true).then(result => {
return result
});
}
detectCb(this.email, true, (result) => {
cb(result)
})
},
getBouncedEmailDetail: function (email, cb) {
this.init(email);
return this.getBouncedDetail(cb);
},
};