From c5ba9c1654954346c93eb25cf915a1bef5f2236e Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Thu, 28 Nov 2019 10:34:40 +0200
Subject: [PATCH] debugging extract message id

---
 javascript/src/helpers/mailparser.js | 48 +++++++++++++++++++++-------
 1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/javascript/src/helpers/mailparser.js b/javascript/src/helpers/mailparser.js
index 2471b8f..7bdaf51 100644
--- a/javascript/src/helpers/mailparser.js
+++ b/javascript/src/helpers/mailparser.js
@@ -61,7 +61,10 @@ function calculateParts(body, from, to, previousBondary) {
   let boundary = findFirstBoundary(body, from, to);
 
   if (boundary == null) {
-    return [{ indices: { from: from, to: to }, boundary: previousBondary, leaf: true }];
+    return [{ indices: { from,
+      to },
+    boundary: previousBondary,
+    leaf: true }];
   }
 
   const realBoundary = boundary;
@@ -74,12 +77,16 @@ function calculateParts(body, from, to, previousBondary) {
   for (let i = 0; i < boundaryIndicesLength; i++) {
     const startBoundary = boundaryIndices[i];
     const endBoundary = body.indexOf("\r\n", startBoundary);
-    boundaryPairs.push({ start: startBoundary, end: endBoundary });
+    boundaryPairs.push({ start: startBoundary,
+      end: endBoundary });
   }
 
   let bodies = [];
   if (previousBondary !== null) {
-    bodies.push({indices: {from: from, to: to}, boundary: previousBondary, leaf: false});
+    bodies.push({indices: {from,
+      to},
+    boundary: previousBondary,
+    leaf: false});
   }
 
   for (let i = 0; i < boundaryIndicesLength - 1; i++) {
@@ -123,29 +130,45 @@ export function fixNewLines(mime) {
 }
 
 export function extractMessageID(mime) {
+  debugger;
   if (mime.startsWith("\r\n")) {
     mime = mime.substring(2); //should not happen
   }
+  debugger;
+
   const headersEndIndex = mime.indexOf("\r\n\r\n"); //the first empty line
   if (headersEndIndex < 0) {
     return null;
   }
+  debugger;
+
   const mimeHeaders = mime.substring(0, headersEndIndex);
+  debugger;
+
   const headers = libmime.decodeHeaders(mimeHeaders);
+  debugger;
+
   let messageId = headers["Message-ID"];
+  debugger;
+
   if (Array.isArray(messageId) && messageId.length > 0) {
+    debugger;
+
     messageId = messageId[0];
   }
   if (messageId && typeof messageId === "string") {
     messageId = messageId.replace(/^</, '').replace(/>$/, '');
+    debugger;
+
     return messageId;
   }
+  debugger;
   return null;
 }
 
 export function parseMIME(mime) {
   let mimeStart = 0;
-  let headersEnd = mime.indexOf("\r\n\r\n"); //the first empty line
+  const headersEnd = mime.indexOf("\r\n\r\n"); //the first empty line
   let mimeBody = "";
   if (headersEnd < 0 && mime.startsWith("\r\n")) {
     mime = mime.substring(2); //should not happen
@@ -156,9 +179,9 @@ export function parseMIME(mime) {
     mimeStart = headersEnd + 4;
   }
 
-  let headers = libmime.decodeHeaders(mime.substring(0, headersEnd));
+  const headers = libmime.decodeHeaders(mime.substring(0, headersEnd));
 
-  let indexOfSMIME = mimeBody.indexOf(SMIMEStart);
+  const indexOfSMIME = mimeBody.indexOf(SMIMEStart);
 
   if (indexOfSMIME >= 0) {
     mimeBody = mimeBody.substring(indexOfSMIME + SMIMEStart.length);
@@ -177,7 +200,9 @@ export function parseMIME(mime) {
   }
 
   parts.push({
-    indices: { from: 0, to: mime.length, headersEnd: headersEnd },
+    indices: { from: 0,
+      to: mime.length,
+      headersEnd },
     headers,
     boundary: "mimemessage",
     leaf: false
@@ -234,7 +259,7 @@ export function decodeMimeBody(descriptor, mimeString) {
   let contentType = getHeaderValue("content-type", descriptor);
   if (contentType) {
     contentType = contentType[0];
-    let parsedContentType = libmime.parseHeaderValue(contentType);
+    const parsedContentType = libmime.parseHeaderValue(contentType);
     if (
       parsedContentType &&
       parsedContentType.params &&
@@ -251,10 +276,10 @@ export function decodeMimeBody(descriptor, mimeString) {
   }
 
   if (contentTransferEncoding.toLowerCase() === "quoted-printable") {
-    let buff = libqp.decode(mimeBody);
+    const buff = libqp.decode(mimeBody);
     return buff.toString(charset);
   } else if (contentTransferEncoding.toLowerCase() === "base64") {
-    let buff = Buffer.from(mimeBody, "base64");
+    const buff = Buffer.from(mimeBody, "base64");
     return buff.toString(charset);
   }
 
@@ -372,5 +397,6 @@ export function getAttachment(mime, part) {
     base64 = window.btoa(body);
   }
 
-  return { contentType, base64 };
+  return { contentType,
+    base64 };
 }
-- 
GitLab