Skip to content

Instantly share code, notes, and snippets.

@mems
Created October 6, 2016 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mems/802213a81ab819afda66083e25e85684 to your computer and use it in GitHub Desktop.
Save mems/802213a81ab819afda66083e25e85684 to your computer and use it in GitHub Desktop.
Convert Thunderbird mbox to maildir

Email file name is the timestamp of received email (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:

// 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}"`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment