Skip to content

Commit

Permalink
fix index.js aliases() so next is always called
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 29, 2024
1 parent 4ca592e commit b36cbd5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 56 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).

### [1.0.2] - 2024-04-29

- index: make separate if blocks in cascading list
- lint: remove duplicate / stale rules from .eslintrc
- dep: eslint-plugin-haraka -> @haraka/eslint-config
- deps: bump to latest versions
- chore: populate [files] in package.json
- doc(CHANGELOG) renamed from Changes
- doc(CONTRIBUTORS): added
- ci: update to shared GHA workflows
- added the option to alias all emails with * (#12)
- added the option to alias all emails with \* (#12)

### 1.0.1 - 2022-05-26

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This handcrafted artisinal software is brought to you by:

| <img height="80" src="https://avatars.githubusercontent.com/u/261635?v=4"><br><a href="https://github.com/msimerson">msimerson</a> (<a href="https://github.com/haraka/haraka-plugin-aliases/commits?author=msimerson">7</a>) |
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| <img height="80" src="https://avatars.githubusercontent.com/u/261635?v=4"><br><a href="https://github.com/msimerson">msimerson</a> (<a href="https://github.com/haraka/haraka-plugin-aliases/commits?author=msimerson">7</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/38138252?v=4"><br><a href="https://github.com/TimoKoole">TimoKoole</a> (<a href="https://github.com/haraka/haraka-plugin-aliases/commits?author=TimoKoole">1</a>) |
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |

<sub>this file is maintained by [.release](https://github.com/msimerson/.release)</sub>
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

# haraka-plugin-aliases

This plugin allows the configuration of aliases that perform an action or
change the RCPT address. Aliases are specified in a JSON formatted config file,
and must have an action. Syntax errors found in the JSON config will stop the server.
This plugin allows the configuration of aliases that perform an action or change the RCPT address. Aliases are specified in a JSON formatted config file, and must have an action. Syntax errors found in the JSON config will stop the server.

IMPORTANT: this plugin must appear in `config/plugins` before other plugins
that run on hook_rcpt
IMPORTANT: this plugin must appear in `config/plugins` before other plugins that run on hook_rcpt

WARNING: DO NOT USE THIS PLUGIN WITH queue/smtp_proxy.

Expand Down Expand Up @@ -75,7 +72,7 @@ It also allows you to route all emails to a certain domain:

```json
{
"*" : { "action" : "alias", "to" : "test15-works@success.com" }
"*": { "action": "alias", "to": "test15-works@success.com" }
}
```

Expand Down
39 changes: 16 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@ exports.register = function () {
}

exports.load_aliases = function () {
const plugin = this
plugin.cfg = plugin.config.get('aliases', 'json', function () {
plugin.load_aliases()
this.cfg = this.config.get('aliases', 'json', () => {
this.load_aliases()
})
if (plugin.cfg === undefined) plugin.cfg = {}
if (this.cfg === undefined) this.cfg = {}
}

exports.aliases = function (next, connection, params) {
const plugin = this
const cfg = plugin.cfg
const cfg = this.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(alias1, action1, drop1) {
const onMatch = (alias1, action1, drop1) => {
switch (action.toLowerCase()) {
case 'drop':
_drop(plugin, connection, drop1)
_drop(this, connection, drop1)
break
case 'alias':
_alias(plugin, connection, alias1, cfg[alias1], host)
_alias(this, connection, alias1, cfg[alias1], host)
break
default:
connection.loginfo(plugin, `unknown action: ${action1}`)
connection.loginfo(this, `unknown action: ${action1}`)
}
}

Expand All @@ -46,16 +44,12 @@ exports.aliases = function (next, connection, params) {
match = rcpt
if (cfg[match].action) action = cfg[match].action
onMatch(match, action, rcpt)
}

if (cfg[`@${host}`]) {
} else if (cfg[`@${host}`]) {
// @domain match
match = `@${host}`
if (cfg[match].action) action = cfg[match].action
onMatch(match, action, match)
}

if (cfg[user]) {
} else if (cfg[user]) {
// user only match
match = user
if (cfg[user].action) action = cfg[user].action
Expand All @@ -70,11 +64,10 @@ exports.aliases = function (next, connection, params) {
if (cfg[match].action) action = cfg[match].action
onMatch(match, action, rcpt)
}

// Match *. When having a * in the alias list it will rewrite all emails that have not been matched by the above rules
if (cfg["*"]) {
if (cfg["*"].action) action = cfg["*"].action;
return onMatch("*", action);
else if (cfg['*']) {
// Match *. When having a * in the alias list it will rewrite all emails that have not been matched by the above rules
if (cfg['*'].action) action = cfg['*'].action
onMatch('*', action)
}

next()
Expand All @@ -100,9 +93,9 @@ function _alias(plugin, connection, key, config, host) {
if (Array.isArray(config.to)) {
connection.logdebug(plugin, `aliasing ${txn.rcpt_to} to ${config.to}`)
txn.rcpt_to.pop()
config.to.forEach((addr) => {
for (const addr of config.to) {
txn.rcpt_to.push(new Address(`<${addr}>`))
})
}
} else {
const to = config.to.search('@') === -1 ? `${config.to}@${host}` : config.to
connection.logdebug(plugin, `aliasing ${txn.rcpt_to} to ${to}`)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
},
"homepage": "https://github.com/haraka/haraka-plugin-aliases#readme",
"dependencies": {
"address-rfc2821": "*"
"address-rfc2821": "^2.1.2"
},
"devDependencies": {
"eslint": "^8.0.0",
"@haraka/eslint-config": "^1.0.0",
"haraka-test-fixtures": "^1.0.0"
"eslint": "^8.57.0",
"@haraka/eslint-config": "^1.1.5",
"haraka-test-fixtures": "^1.3.7"
}
}
39 changes: 19 additions & 20 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ const stub = fixtures.stub.stub
const _set_up = function (done) {
// needed for tests
this.plugin = new fixtures.plugin('aliases')
this.recip = new Address('<test1@example.com>')
this.params = [this.recip]
this.params = [new Address('<test1@example.com>')]

this.connection = new fixtures.connection.createConnection()
this.connection.transaction = new fixtures.transaction.createTransaction()
this.connection.init_transaction()
this.connection.transaction.rcpt_to = [this.params]
this.connection.loginfo = stub()

Expand Down Expand Up @@ -64,7 +63,7 @@ describe('aliases', function () {
it('aliases hook always returns next()', function (done) {
this.plugin.aliases(
(action) => {
assert.equal(undefined, action)
assert.equal(action, undefined)
done()
},
this.connection,
Expand Down Expand Up @@ -95,13 +94,12 @@ describe('aliases', function () {
})

it('should drop test2-specific@example.com', function (done) {
const result = new Address('<test2@example.com>')
this.plugin.aliases(
(action) => {

Check warning on line 98 in test/index.js

View workflow job for this annotation

GitHub Actions / lint / lint

'action' is defined but never used

Check warning on line 98 in test/index.js

View workflow job for this annotation

GitHub Actions / lint / lint

'action' is defined but never used
assert.equal(undefined, this.connection.transaction.notes.discard)
assert.equal(this.connection.transaction.notes.discard, undefined)
assert.ok(this.connection.transaction.rcpt_to)
assert.ok(Array.isArray(this.connection.transaction.rcpt_to))
assert.deepEqual(this.connection.transaction.rcpt_to.pop(), result)
assert.deepEqual(this.connection.transaction.rcpt_to.pop(), new Address('<test2@example.com>'))
done()
},
this.connection,
Expand Down Expand Up @@ -308,19 +306,20 @@ describe('aliases', function () {
)
})

it('should map * to test15-works@success.com': function (done) {
// these will get reset in _set_up everytime
this.recip = new Address('test15@example.com');

this.plugin.aliases((action) => {
assert.ok(this.connection.transaction.rcpt_to);
assert.ok(Array.isArray(this.connection.transaction.rcpt_to));
assert.deepEqual(
this.connection.transaction.rcpt_to.pop(),
new Address('<test15-works@success.com>'),
);
done();
}, this.connection, [this.recip]);
it('should map * to test15-works@success.com', function (done) {
this.plugin.aliases(
(action) => {
assert.ok(this.connection.transaction.rcpt_to)
assert.ok(Array.isArray(this.connection.transaction.rcpt_to))
assert.deepEqual(
this.connection.transaction.rcpt_to.pop(),
new Address('<test15-works@success.com>'),
)
done()
},
this.connection,
[new Address('test15@example.com')],
)
})

it('action alias should fail with loginfo on missing to', function (done) {
Expand Down

0 comments on commit b36cbd5

Please sign in to comment.