Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- EML2MBOX, 2019-03-03, AGPL v3
- https://www.reddit.com/user/jcunews1/
- This script will convert one or more EML email files and combine them into a MBOX/MBX/MBS mailbox file.
- Requirements:
- Windows : Windows 95/NT4.
- Linux/Mac: WINE with Windows Script Host installed.
- Usage:
- eml2mbx.js {target file} {source files...}
- Or:
- cscript eml2mbx.js {target file} {source files...}
- Examples:
- cscript eml2mbx.js messages.mbox *.eml
- eml2mbx.js messages.mbs "msg files\*.eml" temp\2019*.eml
- */
- //UI-independent boolean prompt
- function confirm(msg) {
- if (WScript.fullname.charAt(WScript.fullname.lastIndexOf("\\") + 1).toUpperCase() === "C") {
- WScript.stdout.write(msg);
- return WScript.stdin.readline().toUpperCase() === "Y";
- } else return (new ActiveXObject("wscript.shell")).popup(msg, 0, "EML2MBX", 36) === 6;
- }
- //UI-independent text display
- function output(s, title, type) {
- if (WScript.fullname.charAt(WScript.fullname.lastIndexOf("\\") + 1) === "C") {
- WScript.stdout.write(s);
- } else (new ActiveXObject("wscript.shell")).popup(s, 0, title, type || 64);
- }
- function out(s, type) {
- output(s, "EML2MBX", type);
- }
- function help() {
- out("Usage: eml2mbx {target file} {source files...}");
- WScript.quit(1);
- }
- //split specified file path and returns the directory and file name in an array
- function splitPath(s) {
- var b, i;
- if (s.charAt(1) === ":") {
- b = s.slice(0, 3);
- s = s.slice(3);
- } else if (s.slice(0, 2) === "\\\\") { //absolute remote path
- b = "\\\\";
- s = s.slice(2);
- } else b = "";
- if (s.charAt(s.length - 1) === "\\") s = s.slice(0, s.length - 1);
- if ((i = s.lastIndexOf("\\")) >= 0) {
- return [b + s.slice(0, i + 1), s.slice(i + 1)];
- } else return [b + ".\\", s];
- }
- //compare specified file name against a file mask
- function matchMask(s, mask) {
- return (new RegExp("^" + mask.replace(/./g, function(a) {
- switch (a) {
- case "$":
- case "(":
- case "+":
- case ".":
- case "[":
- case "\\":
- case "^":
- case "{":
- case "|":
- return "\\" + a;
- case "*":
- return ".*?";
- case "?":
- return ".";
- default:
- return a;
- }
- }) + "$", "i")).test(s);
- }
- dt = null;
- em = "";
- function checkfield(s) {
- var i, m, d;
- s = s.replace(/\r?\n/g, " ").replace(/^\s+|\s+$/, "");;
- if ((s.slice(0, 9) === "Received:")) {
- i = s.lastIndexOf(";");
- if (i < 0) return;
- s = s.slice(i + 1).replace(/^\s+/, "");
- if (!s) return;
- d = new Date(s);
- if (!isNaN(d.getTime())) dt = d;
- } else if ((m = s.match(/^From:\s+(.*<([^>]+)|.*)/)) && !em) {
- em = m[2] || m[1];
- }
- }
- fs = new ActiveXObject("scripting.filesystemobject");
- tfp = WScript.arguments(0);
- sfs = [];
- for (i = 1; i < WScript.arguments.length; i++) {
- fn = (pt = splitPath(WScript.arguments(i)))[1];
- pt = pt[0];
- try {
- dr = fs.getfolder(pt);
- pt = dr.path;
- if (pt.charAt(pt.length - 1) !== "\\") pt += "\\";
- } catch(z) {
- out("Path is inaccessible: " + pt, 16);
- WScript.quit(2);
- }
- sfs.push([pt, fn]);
- }
- if (!sfs.length || !tfp) help();
- if (fs.fileexists(tfp) && !confirm("Target file is already exists.\nDo you want to overwrite it?")) WScript.quit(3);
- try {
- tf = fs.createtextfile(tfp, true, false);
- } catch(z) {
- out("Can not overwrite target file.", 16);
- WScript.quit(3);
- }
- cnt = 0;
- for (i = 0; i < sfs.length; i++) {
- ls = new Enumerator(fs.getfolder(sfs[i][0]).files);
- for (; !ls.atEnd(); ls.moveNext()) {
- if (!matchMask(ls.item().name, sfs[i][1])) continue;
- try {
- sf = fs.opentextfile(ls.item().path, 1, false, 0);
- } catch(z) {
- }
- buf = "";
- hdr = "";
- dt = null;
- em = "";
- while (!sf.atendofstream) {
- s = sf.readline();
- if (s.charAt(0) !== " ") {
- hdr = s + "\r\n";
- } else hdr += s + "\r\n";
- checkfield(hdr);
- buf += s + "\r\n";
- }
- if (dt && em) {
- if (WScript.fullname.charAt(WScript.fullname.lastIndexOf("\\") + 1).toUpperCase() === "C") out(em + " " + dt.toDateString() + " " + dt.toTimeString().replace(/UTC/, ""));
- tf.writeline("From " + em + " " + dt.toDateString() + " " + dt.toTimeString().replace(/UTC/, "") + "\r\n" + buf);
- } else {
- if (!dt) {
- if (!em) {
- out("Missing received date and sender's email address in file: " + ls.item().path, 16);
- } else out("Missing received date in file: " + ls.item().path, 16);
- } else out("Missing sender's email address in file: " + ls.item().path, 16);
- WScript.quit(4);
- }
- sf.close();
- cnt++;
- }
- }
- out("\nProcessed " + cnt + " messages.");
Add Comment
Please, Sign In to add comment