Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #181 from smfreegard/master

Make server object available to the plugins
  • Loading branch information...
commit 60dd66ce3729179928aa60c2af4be67006d7ffdd 2 parents 510a100 + d3e278e
@baudehlo authored
Showing with 47 additions and 4 deletions.
  1. +37 −0 docs/Plugins.md
  2. +5 −0 plugins.js
  3. +5 −4 server.js
View
37 docs/Plugins.md
@@ -173,6 +173,43 @@ 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.
+Sharing State
+-------------
+
+There are several cases where you might need to share information between
+plugins. This is done using `notes` - there are three types available:
+
+* server.notes
+
+ Available in all plugins. This is created at PID start-up and is shared
+ amongst all plugins on the same PID and listener.
+ Typical uses for notes at this level would be to share database
+ connections between multiple plugins or connection pools etc.
+
+* connection.notes
+
+ Available on any hook that passes 'connection' as a function parameter.
+ This is shared amongst all plugins for a single connection and is
+ destroyed after the client disconnects.
+ Typical uses for notes at this level would be to store information
+ about the connected client e.g. rDNS names, HELO/EHLO, white/black
+ list status etc.
+
+* connection.transaction.notes
+
+ Available on any hook that passes 'connection' as a function parameter
+ between hook_mail and hook_data_post.
+ This is shared amongst all plugins for this transaction (e.g. MAIL FROM
+ through until a message is received or the connection is reset).
+ Typical uses for notes at this level would be to store information
+ on things like greylisting which uses client, sender and recipient
+ information etc.
+
+All of these notes are simply a Javascript object underneath - so you use
+them like a simple key/value store e.g.
+
+ connection.transaction.notes.test = 'testing';
+
Further Reading
--------------
View
5 plugins.js
@@ -94,6 +94,10 @@ plugins.load_plugin = function(name) {
return plugin;
}
+// Set in server.js; initialized to empty object
+// to prevent it from blowing up any unit tests.
+plugins.server = {};
+
plugins._load_and_compile_plugin = function(name) {
var plugin = new Plugin(name);
var fp = plugin.full_paths,
@@ -128,6 +132,7 @@ plugins._load_and_compile_plugin = function(name) {
process: process,
Buffer: Buffer,
Math: Math,
+ server: plugins.server,
};
constants.import(sandbox);
try {
View
9 server.js
@@ -57,14 +57,15 @@ Server.createServer = function (params) {
// config_data defaults
apply_defaults(config_data.main);
-
- plugins.load_plugins();
-
+
var server = net.createServer(function (client) {
client.setTimeout((config_data.main.inactivity_timeout || 300) * 1000);
conn.createConnection(client, server);
});
server.notes = {};
+ plugins.server = server;
+ plugins.load_plugins();
+
if (cluster && config_data.main.nodes) {
var c = cluster(server);
@@ -146,6 +147,6 @@ function listening () {
logger.lognotice("Listening on port " + config_data.main.port);
Server.ready = 1;
-
+
out.load_queue();
}
Please sign in to comment.
Something went wrong with that request. Please try again.