Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "herald",
"version": "2.0.0",
"version": "2.0.2",
"description": "A package to parse text message dispatches and send them to Slack",
"main": "server.js",
"scripts": {
Expand Down
24 changes: 22 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ function parseKeyValueText(text) {
return obj;
}

// Decode simple quoted-printable artifacts commonly found in raw email bodies.
// Removes soft line breaks (=<CR><LF>) and converts =HH hex escapes to characters.
function decodeQuotedPrintable(s) {
if (!s || typeof s !== "string") return s;
// remove soft line breaks
let out = s.replace(/=\r?\n/g, "");
// handle common hex escapes like =0A, sometimes mis-encoded as =OA
out = out.replace(/=0A/gi, "\n");
out = out.replace(/=0D/gi, "\r");
out = out.replace(/=OA/gi, "\n");
out = out.replace(/=OD/gi, "\r");
// general =HH hex escapes
out = out.replace(/=([0-9A-Fa-f]{2})/g, function (_, hex) {
return String.fromCharCode(parseInt(hex, 16));
});
return out;
}

// Use Google Geocoding API to turn an address into lat/lng.
// We "bias" to Troy, NY by setting components=locality:Troy|administrative_area:NY|country:US
// which will prefer results in Troy, NY.
Expand Down Expand Up @@ -272,7 +290,7 @@ async function handleDispatchText(text) {
}

// Compose the short text and post to Slack
const shortText = `${info["CALL TYPE"].determinant.toLowerCase()} ${info["CALL TYPE"].complaint.toLowerCase()} at ${info.Location}`;
const shortText = `${info["CALL TYPE"].determinant.toUpperCase()} ${info["CALL TYPE"].complaint.toLowerCase()} at ${info.Location}`;

await postMessage({
token: SLACK_TOKEN,
Expand Down Expand Up @@ -355,6 +373,8 @@ const server = new SMTPServer({
}

// Trim and pass to handler
// Decode quoted-printable artifacts (soft breaks, =0A etc.) before parsing.
bodyCandidate = decodeQuotedPrintable(bodyCandidate);
await handleDispatchText(bodyCandidate);
callback();
} catch (err) {
Expand All @@ -370,7 +390,7 @@ const server = new SMTPServer({

server.listen(PORT, () => {
console.log(`Dispatch SMTP server listening on port ${PORT}`);
console.log(`Expecting messages sent TO: ${RECEIVE_EMAIL}`);
console.log(`Expecting messages sent to: ${RECEIVE_EMAIL}`);
console.log(`herald v${VERSION} running`);
});

Expand Down
Loading