-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (82 loc) · 2.03 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
/**
* @module Block_Msp
*/
module.exports.title = 'Block msp';
/**
* Build and return error with message
*
* @param {*} app
* @param {*} envelope
* @param {*} messageInfo
* @param {prototype} next
* @param {string} err
*
* @returns {ErrorConstructor}
*/
const raise_error = ( app, address, session, next, msg ) => {
/**
* Contruct Error object
* @type {ErrorConstructor}
*/
let err = new Error ( msg );
err.responseCode = 550;
return err;
}
/**
* Retrieve current rcpt_to domain
*
* @param {*} app
* @param {*} envelope
* @param {*} messageInfo
* @param {prototype} next
*
* @returns {string} domain
*/
const get_receiver_domain = ( app, address, session, next ) => {
/**
* Retrieve domain from rcpt_to address
* @type {string}
*/
const res = address.address.split ( '@' ) [ 1 ] .trim ();
return res.split ( '>' ) [ 0 ];
}
/**
* Define if receiver domain is currently blocked
*
* @param {*} app
* @param {*} address
* @param {*} session
* @param {*} next
* @param {*} blocked_zone
* @param {*} receiver_domain
* @returns
*/
const is_receiver_domain_blocked = ( app, address, session, next, blocked_zone, receiver_domain ) => {
if ( blocked_zone.includes( receiver_domain) ) {
return true;
}
return false;
}
/**
* Module entry point
*
* @param {*} app
* @param {*} done
*/
module.exports.init = ( app, done ) => {
app.addHook( 'smtp:rcpt_to', ( address, session, next ) => {
const blocked_zone = app.config.zone_block;
const receiver_domain = get_receiver_domain ( app, address, session, next );
if ( is_receiver_domain_blocked ( app, address, session, next, blocked_zone, receiver_domain ) )
{
return next (
raise_error (
app, address, session, next,
`The mail service provider (${receiver_domain}) you are trying to reach has been temporarily disabled`
)
);
}
next ();
} );
done ();
}