forked from haraka/haraka-plugin-aliases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (91 loc) · 2.94 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
// aliases
// Do not run this plugin with the queue/smtp_proxy plugin.
const Address = require('address-rfc2821').Address;
exports.register = function () {
this.inherits('queue/discard');
this.load_aliases();
this.register_hook('rcpt', 'aliases');
};
exports.load_aliases = function () {
const plugin = this;
plugin.cfg = plugin.config.get('aliases', 'json', function () {
plugin.load_aliases();
}) || {};
}
exports.aliases = function (next, connection, params) {
const plugin = this;
const cfg = plugin.cfg;
const rcpt = params[0].address();
const user = params[0].user;
const host = params[0].host;
let match = user.split(/[+-]/, 1);
let action = "<missing>";
function onMatch (match1, action1) {
switch (action.toLowerCase()) {
case 'drop':
_drop(plugin, connection, match1);
break;
case 'alias':
_alias(plugin, connection, match1, cfg[match1], host);
break;
default:
connection.loginfo(plugin, "unknown action: " + action1);
}
next();
}
// full email address match
if (cfg[rcpt]) {
if (cfg[rcpt].action) action = cfg[rcpt].action;
return onMatch(rcpt, action);
}
// @domain match
const dom_match = `@${host}`;
if (cfg[`@${host}`]) {
if (cfg[dom_match].action) action = cfg[dom_match].action;
match = dom_match;
return onMatch(dom_match, action);
}
// user only match
if (cfg[user]) {
if (cfg[user].action) action = cfg[user].action;
return onMatch(user, action);
}
// user prefix match
if (cfg[match[0]]) {
if (cfg[match[0]].action) action = cfg[match[0]].action;
return onMatch(match[0], action);
}
// user prefix + domain match
const prefix_dom = `${match[0]}@${host}`;
if (cfg[prefix_dom]) {
if (cfg[prefix_dom].action) action = cfg[prefix_dom].action;
return onMatch(prefix_dom, action);
}
next();
};
function _drop (plugin, connection, rcpt) {
connection.logdebug(plugin, "marking " + rcpt + " for drop");
connection.transaction.notes.discard = true;
}
function _alias (plugin, connection, key, config, host) {
const txn = connection.transaction;
if (!config.to) {
connection.loginfo(plugin, `alias failed for ${key}, no "to" field in alias config`);
return;
}
if (Array.isArray(config.to)) {
connection.logdebug(plugin, `aliasing ${txn.rcpt_to} to ${config.to}`);
txn.rcpt_to.pop();
config.to.forEach((addr) => {
txn.rcpt_to.push(new Address(`<${addr}>`));
})
return;
}
let to = config.to;
if (to.search("@") === -1) {
to = config.to + '@' + host;
}
connection.logdebug(plugin, "aliasing " + txn.rcpt_to + " to " + to);
txn.rcpt_to.pop();
txn.rcpt_to.push(new Address(`<${to}>`));
}