Skip to content

Commit

Permalink
fix(normalization&logging): ZMS-189-3 (#46)
Browse files Browse the repository at this point in the history
* fix logical error with tempfail

* fix loggelf, normalize emails with + sign
  • Loading branch information
NickOvt authored Nov 21, 2024
1 parent d470d0d commit 7dd6f5c
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const undici = require('undici');
const libmime = require('libmime');
const { toUnicode } = require('punycode');
const { randomBytes } = require('node:crypto');
const os = require('os');

function decodeHeaderLineIntoKeyValuePair(headerLine) {
let decodedHeaderStr;
Expand All @@ -26,44 +25,26 @@ function decodeHeaderLineIntoKeyValuePair(headerLine) {
return [headerKey.trim(), decodedHeaderStr.trim()];
}

const loggelf = (app, message) => {
Object.keys(message).forEach(key => {
if (!message[key]) {
// remove falsy keys (undefined, null, false, "", 0)
delete message[key];
}
});

app.gelf.emit('gelf.log', message);
};

const loggelfForEveryUser = (app, short_message, data) => {
const timestamp = Date.now() / 1000;
const hostname = os.hostname();

if (data._rcpt.length > 1) {
// send for every recipient

for (const rcpt of data._rcpt) {
loggelf(app, {
app.loggelf({
short_message,
...data,
_rcpt: rcpt,
timestamp,
host: hostname
_rcpt: rcpt
});
}
} else {
if (!data.hasOwnProperty('_rcpt')) {
data._rcpt = [];
}

loggelf(app, {
app.loggelf({
short_message,
...data,
_rcpt: data._rcpt[0] || '', // single recipient
timestamp,
host: hostname
_rcpt: data._rcpt[0] || '' // single recipient
});
}
};
Expand All @@ -86,11 +67,16 @@ const normalizeAddress = (address, asObject) => {
return address || '';
}

const user = address.substr(0, address.lastIndexOf('@')).normalize('NFC').toLowerCase().trim();
const domain = normalizeDomain(address.substr(address.lastIndexOf('@') + 1));
const addr = user + '@' + domain;
const unameview = user.replace(/\./g, '');
const addrview = unameview + '@' + domain;
const user = address
.substr(0, address.lastIndexOf('@'))
.normalize('NFC')
.toLowerCase()
.replace(/\+[^@]*$/, '')
.trim(); // get username from email, normalize it to NFC UTF-8, remove everything after plus sign, trim spaces
const domain = normalizeDomain(address.substr(address.lastIndexOf('@') + 1)); // normalize domain
const addr = user + '@' + domain; // actual user address
const unameview = user.replace(/\./g, ''); // remove dots
const addrview = unameview + '@' + domain; // address view

if (asObject) {
return {
Expand Down

0 comments on commit 7dd6f5c

Please sign in to comment.