<commit_before>// jme.rcpt_to.aliases
// Description: Allows arbitrary aliases for addresses
var Address = require('./address').Address;

function addAddressToTransaction(connection, address) {
	connection.transaction.rcpt_to.pop();
	connection.transaction.rcpt_to.push(new Address('<' + address + '>'));
}

function setParams(params, address) {
	params[0].user = address.split("@", 2)[0];
	params[0].host = address.split("@", 2)[1];
}

function getAliases(plugin, connection, config) {
	var authMethod = config['authMethod'] || "file";
	var aliases = false;
	if (authMethod == "file") {
		connection.logdebug(plugin, "Using file-auth");
		if (config['aliases']) {
			aliases = config['aliases'];
		} else {
			connection.logdebug(plugin, "No aliases table in config file");
		}
	}
	return aliases;
}

function resolveAlias(plugin, connection, user, host, config) {
	var aliasTo;
	var localAliases = config[host];
	var bottomAddress = user + "@" + host;
	if (localAliases && localAliases[user]) {
		if (user == localAliases[user]) {
			connection.logdebug(plugin, "Cyclic alias found, stopping here: " + bottomAddress);
		} else {
			if (Object.prototype.toString.call(localAliases[user]) === '[object Array]') {
				if (localAliases[user][0] === "|") {
					connection.transaction.notes.pipe = true;
					connection.transaction.notes.pipeCommand = localAliases[user][1] || null;
					connection.transaction.notes.pipeArgs = localAliases[user][2] || [];
				}
			} else {
				aliasTo = localAliases[user].split("@", 2);
				user = aliasTo[0];
				if (aliasTo[1]) {
					host = aliasTo[1];
				}
				bottomAddress = resolveAlias(plugin, connection, user, host, config);
			}
		}
	}
	return bottomAddress;
}

exports.hook_rcpt = function(next, connection, params) {
	var config      = this.config.get('jme.rcpt_to.aliases', 'json') || {};
	var rcpt        = params[0];
	var address     = rcpt.address();
	var user        = rcpt.user;
	var host        = rcpt.host;

	connection.loginfo(this, "Checking aliases for: " + address);
	var addressBefore = user + "@" + host;
	var aliases = getAliases(this, connection, config);
	var addressAfter = resolveAlias(this, connection, user, host, aliases);

	if (addressBefore == addressAfter) {
		connection.logdebug(this, "Found no aliases for: " + addressBefore);
	} else {
		connection.logdebug(this, addressBefore + " aliases to " + addressAfter);
		setParams(params, addressAfter);
		addAddressToTransaction(connection, addressAfter);
	}
	return next();
};<commit_msg>Added support for + as a delimiter. Will add config options later!<commit_after>// jme.rcpt_to.aliases
// Description: Allows arbitrary aliases for addresses
var Address = require('./address').Address;

function addAddressToTransaction(connection, address) {
	connection.transaction.rcpt_to.pop();
	connection.transaction.rcpt_to.push(new Address('<' + address + '>'));
}

function setParams(params, address) {
	params[0].user = address.split("@", 2)[0];
	params[0].host = address.split("@", 2)[1];
}

function getAliases(plugin, connection, config) {
	var authMethod = config['authMethod'] || "file";
	var aliases = false;
	if (authMethod == "file") {
		connection.logdebug(plugin, "Using file-auth");
		if (config['aliases']) {
			aliases = config['aliases'];
		} else {
			connection.logdebug(plugin, "No aliases table in config file");
		}
	}
	return aliases;
}

function resolveAlias(plugin, connection, user, host, config) {
	var aliasTo;
	var localAliases = config[host];
	var bottomAddress = user + "@" + host;
	if (localAliases && localAliases[user]) {
		if (user == localAliases[user]) {
			connection.logdebug(plugin, "Cyclic alias found, stopping here: " + bottomAddress);
		} else {
			if (Object.prototype.toString.call(localAliases[user]) === '[object Array]') {
				if (localAliases[user][0] === "|") {
					connection.transaction.notes.pipe = true;
					connection.transaction.notes.pipeCommand = localAliases[user][1] || null;
					connection.transaction.notes.pipeArgs = localAliases[user][2] || [];
				}
			} else {
				aliasTo = localAliases[user].split("@", 2);
				user = aliasTo[0];
				if (aliasTo[1]) {
					host = aliasTo[1];
				}
				bottomAddress = resolveAlias(plugin, connection, user, host, config);
			}
		}
	}
	return bottomAddress;
}

exports.hook_rcpt = function(next, connection, params) {
	var config        = this.config.get('jme.rcpt_to.aliases', 'json') || {};
	var rcpt          = params[0];
	var address       = rcpt.address();
	var user          = rcpt.user;
	var host          = rcpt.host;
	var delimitedUser = user.match(/([^\+]+)(\+.*)?/);
	var targetUser    = delimitedAddress[1];
	var wildcardPart  = delimitedAddress[2] || null;

	connection.loginfo(this, "Checking aliases for: " + address);
	var addressBefore = targetUser + "@" + host;
	var aliases = getAliases(this, connection, config);
	var addressAfter = resolveAlias(this, connection, targetUser, host, aliases);

	if (addressBefore == addressAfter) {
		connection.logdebug(this, "Found no aliases for: " + user + "@" + host);
	} else {
		connection.logdebug(this, user + "@" + host + " aliases to " + addressAfter);
		setParams(params, addressAfter);
		addAddressToTransaction(connection, addressAfter);
	}
	return next();
};
{'fraction_non_alphanumeric': 0.1572926596758818, 'fraction_numerical': 0.005719733079122974, 'mean_word_length': 4.2565130260521045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '642831', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1677, 'n_words': 382}