Skip to content

Commit

Permalink
fix(headers-graylog): ZMS-166 (#20)
Browse files Browse the repository at this point in the history
* MessageID header safe replace

* add libmime decoding. Fix gelf, do not include falsy values
  • Loading branch information
NickOvt authored Aug 22, 2024
1 parent 5f8d964 commit 3b6fadf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
40 changes: 34 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,40 @@

const undici = require('undici');
const addressTools = require('zone-mta/lib/address-tools');
const libmime = require('libmime');
const { randomBytes } = require('node:crypto');
const os = require('os');

function decodeHeaderLineIntoKeyValuePair(headerLine) {
let decodedHeaderStr;
let headerSeparatorPos = headerLine.indexOf(':');
if (headerSeparatorPos < 0) {
return headerLine;
}

let headerKey = headerLine.substring(0, headerSeparatorPos);
let headerValue = headerLine.substring(headerSeparatorPos + 1);

try {
decodedHeaderStr = libmime.decodeWords(headerValue);
} catch (err) {
// keep the value as is
}

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();
Expand All @@ -13,7 +44,7 @@ const loggelfForEveryUser = (app, short_message, data) => {
// send for every recipient

for (const rcpt of data._rcpt) {
app.gelf.emit('gelf.log', {
loggelf(app, {
short_message,
...data,
_rcpt: rcpt,
Expand All @@ -26,7 +57,7 @@ const loggelfForEveryUser = (app, short_message, data) => {
data._rcpt = [];
}

app.gelf.emit('gelf.log', {
loggelf(app, {
short_message,
...data,
_rcpt: data._rcpt[0] || '', // single recipient
Expand Down Expand Up @@ -138,10 +169,7 @@ module.exports.init = async app => {
// Change headers to the format that Zilter will accept
for (const headerObj of envelope.headers.getList()) {
// Get header Key and Value from line
const splittedByFirstColumn = headerObj.line.split(/:(.*)/s);

const headerKey = splittedByFirstColumn[0].trim();
const headerValue = splittedByFirstColumn[1].trim();
const [headerKey, headerValue] = decodeHeaderLineIntoKeyValuePair(headerObj.line);

allHeadersParsed[headerKey] = headerValue;

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"homepage": "https://github.com/zone-eu/wildduck-zonemta-zilter#readme",
"dependencies": {
"libmime": "5.3.5",
"undici": "6.18.2",
"zone-mta": "3.9.6"
},
Expand Down

0 comments on commit 3b6fadf

Please sign in to comment.