Email file name is the timestamp when the email is stored, moved (in microseconds). Example: 1466698612265640, start with From - Thu Jun 23 18:16:52 2016 (Thu Jun 23 2016 18:16:52 GMT+0200 (CEST))
Use NodeJS to convert mbox to maildir:
// Note: this script not work properly if an email contains "\n\nFrom - "
const fs = require('fs');
const path = require('path');
const readline = require('readline');
let file = process.argv[2];
let tmpFile = file + ".tmp";
fs.renameSync(file, tmpFile);
let folder = file;
fs.mkdirSync(folder);
let curFolder = folder + path.sep + "cur";
fs.mkdirSync(curFolder);
fs.mkdirSync(folder + path.sep + "tmp");
let rl = readline.createInterface({
input: fs.createReadStream(tmpFile)
});
// each entry is separated by "\n\n" (or "\r\n", "\n\r", "\r\r")
let emptyLines = 0;//consecutive empty lines
let buffer = "";
let count = 0;
function writeEntry(data = ""){
//if(!data.startWith("From - ")){
if(!data.substr(0, 7) === "From - "){
throw new Error(`Invalid entry "${data.substr(0, 10)}…"`);
}
data += "\n";// newline at the end
let date = data.substring(7, data.indexOf("\n"));//"From - (.*)"
let timestamp = Date.parse(date);// What about timezone?
if(Number.isNaN(timestamp)){
throw new Error(`Invalid entry date "${date}"`);
}
timestamp *= 1000;// in microseconds
timestamp += Math.round(Math.random() * 999999);//add random milliseconds + microseconds
fs.writeFile(curFolder + "/" + timestamp, data, "utf8", error => error && console.warn(error));
}
// Each line
rl.on("line", (line) => {
// If an empty line
if(line === ""){
emptyLines++;
}
// If previous lines are empty
else{
// End of previous entry
// What happend a message contains "\n\nFrom - " ? like embedded emails
//if(emptyLines >= 1 && line.startWith("From - ")){
if(emptyLines >= 1 && line.substr(0, 7) === "From - "){
count++;
writeEntry(buffer.trim());
buffer = "";
}
emptyLines = 0;
}
buffer += line + "\n";
});
rl.on("close", () => {
// For the last entry
buffer = buffer.trim();
if(buffer !== ""){
count++;
writeEntry(buffer);
}
console.log(`${count} entrie(s) founded in "${file}"`);
});