Writing Haraka Plugins

All aspects of receiving an email in Haraka are controlled via plugins, to the extent that no mail will even be received unless you have a minimum of a 'rcpt' plugin and a 'queue' plugin.

The 'rcpt' plugin is used to determine if a particular recipient should be allowed to be relayed or received for. The 'queue' plugin queue's the email somewhere - perhaps to disk, or perhaps to an onward SMTP server.

Anatomy of a Plugin

Plugins in Haraka are simply Javascript files in the plugins/ directory.

To enable a plugin, simply add its name to config/plugins.

In order to hook into the "rcpt" event, simply create a method in exports to hook it:

exports.hook_rcpt = function (next, connection, params) {
    // email address is in params[0]
    // do something with the address... then call:
    next();
};

We've introduced a couple of new concepts here, so let's go through them:

The next() method is the most critical thing here - since Haraka is an event based SMTP server, we may need to go off and fetch network information before we can return a result. We can do that asynchronously and simply run next() when we are done, which allows Haraka to go on processing other clients while we fetch our information.

See "The Next Function" below for more details.

Logging

Plugins inherit all the logging methods of logger.js, which are:

It should also be noted that if plugins throw an exception directly when in a hook the exception will be caught and generate a logcrit level error. However they will not be caught quite as gracefully if you are in async code within your plugin. Use error codes for that, log the error, and run your next() function appropriately.

Multiple Hooks

You can hook the same event multipe times, to do that provide a register() method and hook it:

exports.register = function() {
    this.register_hook('queue', 'try_queue_my_way');
    this.register_hook('queue', 'try_queue_highway');
};

Then when the earlier hook calls next() (without parameters) it continues on to the next hook you registered to try that one.

The Next Function

The next() function takes two optional parameters: code and msg

The code is one of the below listed return values. The msg corresponds with the string to send to the client in case of a failure. Use an Array if you need to send back a multi-line response. The msg should NOT contain the code number - that is taken care of by the Haraka internals.

Return Values

These constants are compiled into your plugin when it is loaded, you do not need to define them:

Available Hooks

These are just the name of the hook, with any parameter sent to it:

The rcpt hook is slightly special. If we have a plugin (prior to rcpt) that sets the connection.relaying = true flag, then we do not need any rcpt hooks, or if we do, none of them need call next(OK). However if connection.relaying remains false (as is the default - you don't want an open relay!), then one rcpt plugin MUST return next(OK) or your sender will receive the error message "I cannot deliver for that user". The most obvious choice for this activity is the rcpt_to.in_host_list plugin, which lists the domains for which you wish to receive email.

If a rcpt plugin DOES call next(OK) then the rcpt_ok hook is run. This is primarily used by the queue/smtp_proxy plugin which needs to run after all rcpt hooks.

Further Reading

Now you want to read about the Connection object.