diff --git a/lib/tools.js b/lib/tools.js index aca38934..13b848af 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -108,6 +108,25 @@ function normalizeAddress(address, withNames, options) { return addr; } +/** + * Generate a list of possible wildcard addresses by generating all posible + * substrings of the username email address part. + * + * @param {String} username - The username part of the email address. + * @param {String} domain - The domain part of the email address. + * @return {Array} The list of all possible username wildcard addresses, + * that would match this email address (as given by the params). + */ +function getWildcardAddresses(username, domain) { + if (typeof username !== 'string' || typeof domain !== 'string') return []; + let result = []; + // <= generates the 'simple' wildcard (a la '*@') address. + for (let i = 1; i <= username.length; i++) { + result.push('*' + username.substr(i) + '@' + domain); + } + return result; +} + // returns a redis config object with a retry strategy function redisConfig(defaultConfig) { return defaultConfig; @@ -430,6 +449,7 @@ function uview(address) { module.exports = { normalizeAddress, normalizeDomain, + getWildcardAddresses, redisConfig, checkRangeQuery, decodeAddresses, diff --git a/lib/user-handler.js b/lib/user-handler.js index 0afe87d9..1120ec49 100644 --- a/lib/user-handler.js +++ b/lib/user-handler.js @@ -121,13 +121,22 @@ class UserHandler { return false; } + let _partialWildcards = tools.getWildcardAddresses( + username, + domain + ); + let query = { - addrview: '*@' + domain + addrview: { $in: _partialWildcards } }; if (aliasDomain) { // search for alias domain as well - query.addrview = { $in: [query.addrview, '*@' + aliasDomain] }; + let _aliasWildcards = tools.getWildcardAddresses( + username, + aliasDomain + ); + query.addrview.$in = query.addrview.$in.concat(_aliasWildcards); } // try to find a catch-all address