Skip to content

Commit

Permalink
Refactored plugins to use the name next() instead of callback()
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Sergeant committed Apr 7, 2011
1 parent 6e3a3e8 commit 5a3d335
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 124 deletions.
20 changes: 10 additions & 10 deletions plugins/auth/flat_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@

var crypto = require('crypto');

exports.hook_capabilities = function (callback, connection) {
exports.hook_capabilities = function (next, connection) {
connection.capabilities.push('AUTH CRAM-MD5');
callback(OK);
next(OK);
}

exports.hook_unrecognized_command = function (callback, connection, params) {
exports.hook_unrecognized_command = function (next, connection, params) {
if (connection.transaction.notes.auth_flat_file_ticket) {
var credentials = unbase64(params[0]).split(' ');
return this.check_user(callback, connection, credentials);
return this.check_user(next, connection, credentials);
}
else if (params[0] === 'AUTH' && params[1] === 'CRAM-MD5') {
var ticket = '<' + hexi(Math.floor(Math.random() * 1000000)) + '.' +
hexi(Date.now()) + '@' + this.config.get('me') + '>';
this.loginfo("ticket: " + ticket);
connection.respond(334, base64(ticket));
connection.transaction.notes.auth_flat_file_ticket = ticket;
return callback(OK);
return next(OK);
}
return callback(CONT);
return next();
}

exports.check_user = function (callback, connection, credentials) {
exports.check_user = function (next, connection, credentials) {
if (!(credentials[0] && credentials[1])) {
connection.respond(504, "Invalid AUTH string");
connection.reset_transaction();
return callback(OK);
return next(OK);
}

var config = this.config.get('auth_flat_file.ini', 'ini');

if (!config.users[credentials[0]]) {
connection.respond(535, "Authentication failed for " + credentials[0]);
connection.reset_transaction();
return callback(OK);
return next(OK);
}

var clear_pw = config.users[credentials[0]];
Expand All @@ -54,7 +54,7 @@ exports.check_user = function (callback, connection, credentials) {
connection.respond(535, "Authentication failed");
connection.reset_transaction();
}
return callback(OK);
return next(OK);
}

function hexi (number) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/data.nomsgid.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Check whether an email has a Message-Id header or not, and reject if not

exports.hook_data_post = function (callback, connection) {
exports.hook_data_post = function (next, connection) {
if (connection.transaction.header.get_all('Message-Id').length === 0) {
callback(DENY, "Mails here must have a Message-Id header");
next(DENY, "Mails here must have a Message-Id header");
}
else {
callback(CONT);
next();
}
}
6 changes: 3 additions & 3 deletions plugins/data.noreceived.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// NB: Don't check this on your outbounds. It's also a pretty strict check
// for inbounds too, so use with caution.

exports.hook_data_post = function (callback, connection) {
exports.hook_data_post = function (next, connection) {
// We always have the received header that Haraka added, so check for 1
if (connection.transaction.header.get_all('Received').length === 1) {
callback(DENY, "Mails here must have a Received header");
next(DENY, "Mails here must have a Received header");
}
else {
callback(CONT);
next();
}
}
10 changes: 5 additions & 5 deletions plugins/data.signatures.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Simple string signatures

exports.hook_data = function (callback, connection) {
exports.hook_data = function (next, connection) {
// enable mail body parsing
connection.transaction.parse_body = 1;
callback(CONT);
next();
}

exports.hook_data_post = function (callback, connection) {
exports.hook_data_post = function (next, connection) {
var sigs = this.config.get('data.signatures', 'list');

if (check_sigs(sigs, connection.transaction.body)) {
return callback(DENY, "Mail matches a known spam signature");
return next(DENY, "Mail matches a known spam signature");
}
return callback(CONT);
return next();
}

function check_sigs (sigs, body) {
Expand Down
18 changes: 9 additions & 9 deletions plugins/data.uribl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ var logger = require('./logger');

var two_level_tlds = {};

exports.hook_data = function (callback, connection) {
exports.hook_data = function (next, connection) {
// enable mail body parsing
connection.transaction.parse_body = 1;
callback(CONT);
next();
}

exports.hook_data_post = function (callback, connection) {
exports.hook_data_post = function (next, connection) {
var zones = this.config.get('data.uribl.zones', 'list');

this.config.get('data.uribl.two_level_tlds', 'list').forEach(function (tld) {
Expand All @@ -29,7 +29,7 @@ exports.hook_data_post = function (callback, connection) {
var hosts = Object.keys(urls);

var pending_queries = 0;
var callback_ran = 0;
var next_ran = 0;
var plugin = this;

for (var i=0,l=hosts.length; i < l; i++) {
Expand All @@ -50,21 +50,21 @@ exports.hook_data_post = function (callback, connection) {
dns.resolveTxt(host + '.' + zones[i], function (err, addresses) {
pending_queries--;
if (!err) {
if (!callback_ran) {
callback_ran++;
return callback(DENY, addresses);
if (!next_ran) {
next_ran++;
return next(DENY, addresses);
}
}
if (pending_queries === 0) {
callback(CONT);
next();
}
});
}
}

if (pending_queries === 0) {
// we didn't execute any DNS queries
callback(CONT);
next();
}
}

Expand Down
8 changes: 4 additions & 4 deletions plugins/dnsbl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ exports.register = function() {
this.register_hook('connect', 'check_ip');
}

exports.check_ip = function(callback, connection) {
exports.check_ip = function(next, connection) {
this.logdebug("check_ip: " + connection.remote_ip);

var ip = new String(connection.remote_ip);
var reverse_ip = ip.split('.').reverse().join('.');

if (!this.zones || !this.zones.length) {
this.logerror("No zones");
return callback(CONT);
return next();
}

var remaining_zones = [];
Expand All @@ -37,12 +37,12 @@ exports.check_ip = function(callback, connection) {
}
if (remaining_zones.length === 0) {
// only call declined if no more results are pending
return callback(CONT);
return next();
}
return;
}
remaining_zones = [];
return callback(DENY, value);
return next(DENY, value);
});

remaining_zones.push(zone);
Expand Down
12 changes: 6 additions & 6 deletions plugins/early_talker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ exports.register = function() {
this.register_hook('data', 'check_early_talker');
};

exports.check_early_talker = function(callback, connection) {
exports.check_early_talker = function(next, connection) {
if (this.pause) {
setTimeout(function () { _check_early_talker(connection, callback) }, this.pause);
setTimeout(function () { _check_early_talker(connection, next) }, this.pause);
}
else {
_check_early_talker(connection, callback);
_check_early_talker(connection, next);
}
};

var _check_early_talker = function (connection, callback) {
var _check_early_talker = function (connection, next) {
if (connection.early_talker) {
callback(DENYDISCONNECT, "You talk too soon");
next(DENYDISCONNECT, "You talk too soon");
}
else {
callback(CONT);
next();
}
};
12 changes: 6 additions & 6 deletions plugins/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,36 +68,36 @@ exports.register = function () {
this.loginfo("http server running on port " + port);
};

exports.hook_deny = function (callback, connection, params) {
exports.hook_deny = function (next, connection, params) {
var plugin = this;
insert.bindArray([new Date().getTime(), params[2]], function (err) {
if (err) {
plugin.logerror("Insert DENY failed: " + err);
return callback(CONT);
return next();
}
insert.fetchAll(function (err, rows) {
if (err) {
plugin.logerror("Insert failed: " + err);
}
try { insert.reset() } catch (err) {}
callback(CONT);
next();
});
});
};

exports.hook_queue_ok = function (callback, connection, params) {
exports.hook_queue_ok = function (next, connection, params) {
var plugin = this;
insert.bindArray([new Date().getTime(), 'accepted'], function (err) {
if (err) {
plugin.logerror("Insert DENY failed: " + err);
return callback(CONT);
return next();
}
insert.fetchAll(function (err, rows) {
if (err) {
plugin.logerror("Insert failed: " + err);
}
try { insert.reset() } catch (err) {}
callback(CONT);
next();
});
});
};
Expand Down
32 changes: 16 additions & 16 deletions plugins/helo.checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@ exports.register = function () {
});
}

exports.helo_no_dot = function (callback, connection, helo) {
exports.helo_no_dot = function (next, connection, helo) {
var config = this.config.get('helo.checks.ini', 'ini');
if (!config.main.check_no_dot) {
return callback(CONT);
return next();
}

/\./.test(helo) ? callback(CONT) : callback(DENY, "HELO must have a dot");
/\./.test(helo) ? next() : next(DENY, "HELO must have a dot");
};

exports.helo_match_re = function (callback, connection, helo) {
exports.helo_match_re = function (next, connection, helo) {
var regexps = this.config.get('helo.checks.regexps', 'list');

for (var i=0,l=regexps.length; i < l; i++) {
var re = new RegExp('^' + regexps[i] + '$');
if (re.test(helo)) {
return callback(DENY, "BAD HELO");
return next(DENY, "BAD HELO");
}
}
return callback(CONT);
return next();
};

exports.helo_raw_ip = function (callback, connection, helo) {
exports.helo_raw_ip = function (next, connection, helo) {
var config = this.config.get('helo.checks.ini', 'ini');
if (!config.main.check_raw_ip) {
return callback(CONT);
return next();
}

// RAW IPs must be formatted: "[1.2.3.4]" not "1.2.3.4" in HELOs
/^\d+\.\d+\.\d+\.\d+$/.test(helo) ?
callback(DENY, "RAW IP HELOs must be correctly formatted")
: callback(CONT);
next(DENY, "RAW IP HELOs must be correctly formatted")
: next();
};

exports.helo_is_dynamic = function (callback, connection, helo) {
return callback(CONT); // TODO!
exports.helo_is_dynamic = function (next, connection, helo) {
return next(); // TODO!
};

exports.helo_big_company = function (callback, connection, helo) {
exports.helo_big_company = function (next, connection, helo) {
var rdns = connection.remote_host;

var big_co = this.config.get('helo.checks.ini', 'ini').bigco;
Expand All @@ -66,13 +66,13 @@ exports.helo_big_company = function (callback, connection, helo) {
for (var i=0,l=allowed_rdns.length; i < l; i++) {
var re = new RegExp(allowed_rdns[i].replace(/\./g, '\\.') + '$');
if (re.test(rdns)) {
return callback(CONT);
return next();
}
}
return callback(DENY, "You are not who you say you are");
return next(DENY, "You are not who you say you are");
}
else {
return callback(CONT);
return next();
}
};

12 changes: 6 additions & 6 deletions plugins/mail_from.is_resolvable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

var dns = require('dns');

exports.hook_mail = function(callback, connection, params) {
exports.hook_mail = function(next, connection, params) {
var mail_from = params[0];
// Check for MAIL FROM without an @ first - ignore those here
if (!mail_from.match(/@/)) {
return callback(CONT);
return next();
}
var matches = mail_from.match(/@([^@>]*)>?/);
if (!matches) {
this.logerror("FROM address does not parse: " + mail_from);
return callback(DENY, "FROM address does not parse");
return next(DENY, "FROM address does not parse");
}

var domain = matches[1];
Expand All @@ -20,11 +20,11 @@ exports.hook_mail = function(callback, connection, params) {
dns.resolveMx(domain, function(err, addresses) {
if (err && err.code != dns.NXDOMAIN) {
plugin.logerror("DNS Error: " + err);
return callback(DENYSOFT, "Temporary resolver error");
return next(DENYSOFT, "Temporary resolver error");
}
if (addresses && addresses.length) {
return callback(CONT, "From address is OK");
return next(, "From address is OK");
}
return callback(DENYSOFT, "No MX for your FROM address");
return next(DENYSOFT, "No MX for your FROM address");
});
}
6 changes: 3 additions & 3 deletions plugins/mail_from.nobounces.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// I don't allow MAIL FROM:<> on my server, because it's all crap and I send
// so little mail anyway that I rarely get real bounces

exports.hook_mail = function (callback, connection, params) {
exports.hook_mail = function (next, connection, params) {
var mail_from = params[0];
if (mail_from === '<>') {
return callback(DENY, "No bounces accepted here");
return next(DENY, "No bounces accepted here");
}
return callback(CONT);
return next();
}
Loading

0 comments on commit 5a3d335

Please sign in to comment.