Skip to content

Commit

Permalink
ZMS-189 (#36)
Browse files Browse the repository at this point in the history
* fix logical error with tempfail

* fix subject issue when using api. add debugJson to gelf

* safer delete with check

* Add better SMTP error message

* make message shorter

* comment

* check for aliases, fix normalization
  • Loading branch information
NickOvt authored Nov 14, 2024
1 parent 7ec588a commit 63f4e05
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ const loggelfForEveryUser = (app, short_message, data) => {
}
};

const normalizeAddress = (authenticatedUser, returnAsObject) => {
let user = authenticatedUser.substr(0, authenticatedUser.lastIndexOf('@')).normalize('NFC').toLowerCase().trim();
let domain = authenticatedUser.substr(authenticatedUser.lastIndexOf('@') + 1);
let addr = user.trim() + '@' + addressTools.normalizeDomain(domain);

if (returnAsObject) {
return {
user,
domain,
addr
};
}
return addr;
};

module.exports.title = 'Zilter';
module.exports.init = async app => {
app.addHook('message:queue', async (envelope, messageinfo) => {
Expand Down Expand Up @@ -124,15 +139,11 @@ module.exports.init = async app => {

const smtpUsernamePatternRegex = /\[([^\]]+)]/;

try {
if (envelope.userId && authenticatedUser) {
// have both userId and user. Probably webmail. Set sender to the userId straight away
// first check though that the userId is a 24 length hex
let passEmail = true; // by default pass email
let isTempFail = true; // by default tempfail

if (envelope.userId.length === 24) {
sender = envelope.userId.toString();
}
} else if (authenticatedUser.includes('@')) {
try {
if (authenticatedUser.includes('@')) {
if (smtpUsernamePatternRegex.test(authenticatedUser)) {
// SMTP username[email]

Expand All @@ -144,7 +155,22 @@ module.exports.init = async app => {

// SMTP email aadress login
// seems to be an email, no need to resolve, straight acquire the user id from addresses
const addressData = await app.db.users.collection('addresses').findOne({ addrview: addressTools.normalizeAddress(authenticatedUser) });
// normalize address
let addrObj = normalizeAddress(authenticatedUser, true);
authenticatedUser = addrObj.addr;

// check for alias
let aliasData = await app.db.users.collection('domainaliases').findOne({ alias: addrObj.domain });

let addrview = authenticatedUser; // default to addrview query as-is without alias

if (aliasData) {
// got alias data
const aliasDomain = aliasData.domain;
addrview = addrObj.user + '@' + aliasDomain; // set new query addrview
}

const addressData = await app.db.users.collection('addresses').findOne({ addrview });
sender = addressData.user.toString();
} else {
// current user authenticated via the username, resolve to email
Expand All @@ -158,19 +184,17 @@ module.exports.init = async app => {
short_message: '[WILDDUCK-ZONEMTA-ZILTER] DB error',
_plugin_status: 'error',
_error: 'DB error. Check DB connection, or collection names, or filter params.',
_authenticated_user: authenticatedUser
_authenticated_user: authenticatedUser,
_err_json: JSON.stringify(err)
});
return;
throw app.reject(envelope, 'tempfail', messageinfo, 'Temporary error, please try again later.');
}

// construct Authorization header
const userBase64 = Buffer.from(`${userName}:${apiKey}`).toString('base64'); // authorization header

const messageSize = envelope.headers.build().length + envelope.bodySize; // RFC822 size (size of Headers + Body)

let passEmail = true; // by default pass email
let isTempFail = true; // by default tempfail

const messageHeadersList = [];

const allHeadersParsed = {};
Expand Down

0 comments on commit 63f4e05

Please sign in to comment.