fixing indent

This commit is contained in:
bwhitn 2018-11-20 22:36:29 -05:00
parent ed17ed2919
commit 13b10a68a2

View file

@ -4,178 +4,179 @@
* @license Apache-2.0 * @license Apache-2.0
*/ */
import Operation from "../Operation"; import Operation from "../Operation";
import OperationError from "../errors/OperationError"; import OperationError from "../errors/OperationError";
import cptable from "../vendor/js-codepage/cptable.js"; import cptable from "../vendor/js-codepage/cptable.js";
import {fromBase64} from "../lib/Base64"; import {fromBase64} from "../lib/Base64";
import {decodeQuotedPrintable} from "../lib/QuotedPrintable" import {decodeQuotedPrintable} from "../lib/QuotedPrintable";
import {MIME_FORMAT} from "../lib/ChrEnc"; import {MIME_FORMAT} from "../lib/ChrEnc";
import Utils from "../Utils";
//TODO: fix function header // TODO: fix function header
/** /**
* Return the conetent encoding for a mime section from a header object. * Return the conetent encoding for a mime section from a header object.
* CONTENT_TYPE returns the content type of a mime header from a header object. * CONTENT_TYPE returns the content type of a mime header from a header object.
* Returns the filename from a mime header object. * Returns the filename from a mime header object.
* Returns the boundary value for the mime section from a header object. * Returns the boundary value for the mime section from a header object.
* @constant * @constant
* @default * @default
*/ */
const FIELD_ITEM = { const FIELD_ITEM = {
FILENAME: [/filename=".*?([^~#%&*\][\\:<>?/|]+)"/, "content-disposition"], FILENAME: [/filename=".*?([^~#%&*\][\\:<>?/|]+)"/, "content-disposition"],
CONTENT_TYPE: [/\s*([^;\s]+)/, "content-type"], CONTENT_TYPE: [/\s*([^;\s]+)/, "content-type"],
BOUNDARY: [/boundary="(.+?)"/, "content-type"], BOUNDARY: [/boundary="(.+?)"/, "content-type"],
CHARSET: [/charset=([a-z0-9-]+)/, "content-type"], CHARSET: [/charset=([a-z0-9-]+)/, "content-type"],
TRANSER_ENCODING: [/\s*([A-Za-z0-9-]+)\s*/, "content-transfer-encoding"], TRANSER_ENCODING: [/\s*([A-Za-z0-9-]+)\s*/, "content-transfer-encoding"],
} }
/** /**
* @constant * @constant
* @default * @default
*/ */
//TODO: should 8 bit and 7 bit be treated the same? // TODO: should 8 bit and 7 bit be treated the same?
const DECODER = { const DECODER = {
"base64": function (input) { "base64": function (input) {
return fromBase64(input, Base64.ALPHABET, "string", true); return fromBase64(input);
}, },
"quoted-printable": function (input) { "quoted-printable": function (input) {
return decodeQuotedPrintable(input); return Utils.byteArrayToUtf8(decodeQuotedPrintable(input));
}, },
"7bit": function (input) { "7bit": function (input) {
return input; return input;
}, },
"8bit": function (input) { "8bit": function (input) {
return input; return input;
}, },
} }
class ParseIMF extends Operation { class ParseIMF extends Operation {
/** /**
* Internet MessageFormat constructor * Internet MessageFormat constructor
*/ */
constructor() { constructor() {
super(); super();
this.name = "Parse Internet Message Format"; this.name = "Parse Internet Message Format";
this.module = "Default"; this.module = "Default";
this.description = ["Parser an IMF formatted messages following RFC5322.", this.description = ["Parser an IMF formatted messages following RFC5322.",
"<br><br>", "<br><br>",
"Parses an IMF formated message. These often have the file extention &quot;.eml&quote; and contain the email headers and body. The output will be a file list of the headers and mime parts.", "Parses an IMF formated message. These often have the file extention &quot;.eml&quote; and contain the email headers and body. The output will be a file list of the headers and mime parts.",
].join("\n"); ].join("\n");
this.infoURL = "https://tools.ietf.org/html/rfc5322"; this.infoURL = "https://tools.ietf.org/html/rfc5322";
this.inputType = "string"; this.inputType = "string";
this.outputType = "string"; this.outputType = "string";
this.args = []; this.args = [];
} }
/** /**
* Basic Email Parser that displays the header and mime sections as files. * Basic Email Parser that displays the header and mime sections as files.
* *
* @param {string} input * @param {string} input
* @param {Object[]} args * @param {Object[]} args
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
if (!input) { if (!input) {
return; return;
}
let headerBody = this.splitHeaderFromBody(input);
let header = headerBody[0];
let headerArray = this.parseHeader(header);
if (args[0]) {
header = this.replaceDecodeWord(header);
}
return JSON.stringify(headerArray);
} }
let headerBody = this.splitHeaderFromBody(input);
let header = headerBody[0]; /**
let headerArray = this.parseHeader(header); * Breaks the header from the body and returns [header, body]
if (args[0]) { *
header = this.replaceDecodeWord(header); * @param {string} input
* @returns {string[]}
*/
splitHeaderFromBody(input) {
const emlRegex = /^([\x20-\x7e\n\r\t]+?)(?:\r?\n){2}([\x20-\x7e\t\n\r]*)/;
let splitEmail = emlRegex.exec(input);
if (splitEmail) {
//TODO: Array splice vs shift?
splitEmail.shift();
return splitEmail;
}
} }
return JSON.stringify(headerArray);
}
/** /**
* Breaks the header from the body and returns [header, body] * Takes a string and decodes quoted words inside them
* * These take the form of =?utf-8?Q?Hello?=
* @param {string} input *
* @returns {string[]} * @param {string} input
*/ * @returns {string}
splitHeaderFromBody(input) { */
const emlRegex = /^([\x20-\x7e\n\r\t]+?)(?:\r?\n){2}([\x20-\x7e\t\n\r]*)/; replaceDecodeWord(input) {
let splitEmail = emlRegex.exec(input); return input.replace(/=\?([^?]+)\?(Q|B)\?([^?]+)\?=/g, function (a, charEnc, contEnc, input) {
if (splitEmail) { //TODO fix Q encoding as it isn't identical to quoted-printable. ie _=" "
//TODO: Array splice vs shift? contEnc = (contEnc === "B") ? "base64" : "quoted-printable";
splitEmail.shift(); return this.decodeMimeData(input, charEnc, contEnc);
return splitEmail; });
} }
}
/** /**
* Takes a string and decodes quoted words inside them * Breaks a header into a object to be used by other functions.
* These take the form of =?utf-8?Q?Hello?= * It removes any line feeds or carriage returns from the values and
* * replaces it with a space.
* @param {string} input *
* @returns {string} * @param {string} input
*/ * @returns {object}
replaceDecodeWord(input) { */
return input.replace(/=\?([^?]+)\?(Q|B)\?([^?]+)\?=/g, function (a, charEnc, contEnc, input) { parseHeader(input) {
//TODO fix Q encoding as it isn't identical to quoted-printable. ie _=" " const sectionRegex = /([A-Z-]+):\s+([\x20-\x7e\r\n\t]+?)(?=$|\r?\n\S)/gi;
contEnc = (contEnc === "B") ? "base64" : "quoted-printable"; let header = {}, section;
return this.decodeMimeData(input, charEnc, contEnc); while ((section = sectionRegex.exec(input))) {
}); let fieldName = section[1].toLowerCase();
} let fieldValue = section[2].replace(/\n|\r/g, " ");
if (header[fieldName]) {
header[fieldName].push(fieldValue);
} else {
header[fieldName] = [fieldValue];
}
}
return header;
}
/** /**
* Breaks a header into a object to be used by other functions. * Return decoded MIME data given the character encoding and content encoding.
* It removes any line feeds or carriage returns from the values and *
* replaces it with a space. * @param {string} input
* * @param {string} charEnc
* @param {string} input * @param {string} contEnc
* @returns {object} * @returns {string}
*/ */
parseHeader(input) { decodeMimeData(input, charEnc, contEnc) {
const sectionRegex = /([A-Z-]+):\s+([\x20-\x7e\r\n\t]+?)(?=$|\r?\n\S)/gi;
let header = {}, section;
while ((section = sectionRegex.exec(input))) {
let fieldName = section[1].toLowerCase();
let fieldValue = section[2].replace(/\n|\r/g, " ");
if (header[fieldName]) {
header[fieldName].push(fieldValue);
} else {
header[fieldName] = [fieldValue];
}
}
return header;
}
/**
* Return decoded MIME data given the character encoding and content encoding.
*
* @param {string} input
* @param {string} charEnc
* @param {string} contEnc
* @returns {string}
*/
decodeMimeData(input, charEnc, contEnc) {
//TODO: make exceptions for unknown charEnc and contEnc? //TODO: make exceptions for unknown charEnc and contEnc?
input = this.DECODER[contEnc](input); input = this.DECODER[contEnc](input);
if (charEnc) { if (charEnc) {
input = cptable.utils.decode(this.MIME_FORMAT[charEnc.toLowerCase()], input); input = cptable.utils.decode(this.MIME_FORMAT[charEnc.toLowerCase()], input);
} }
return input; return input;
} }
/** /**
* Returns a header item given a header object, itemName, and index number. * Returns a header item given a header object, itemName, and index number.
* *
* @param {object} header * @param {object} header
* @param {object} FIELD_ITEM * @param {object} FIELD_ITEM
* @param {integer} fieldNum * @param {integer} fieldNum
* @returns {string} * @returns {string}
*/ */
getHeaderItem(header, fieldItem, fieldNum = 0){ getHeaderItem(header, fieldItem, fieldNum = 0){
if (fieldItem[1] in header && header[fieldItem[1]].length > fieldNum) { if (fieldItem[1] in header && header[fieldItem[1]].length > fieldNum) {
let field = header[fieldItem[1]][fieldNum], item; let field = header[fieldItem[1]][fieldNum], item;
if ((item = fieldItem[0].exec(field))) { if ((item = fieldItem[0].exec(field))) {
return item[1]; return item[1];
} }
} }
} }
} }