Files
wekan/models/lib/mboxSplit.js
T
Lauri Ojansivu ccfa6cf362 Import and export mail, calendars, chats and notes: mbox/.eml, iCalendar, Google Takeout, Slack, Mattermost, Telegram, WhatsApp, Discord, Evernote.
The Import page reads nine more kinds of export, each into the view it
is for: mail (mbox, .eml) as cards of Board View / Email, one per
conversation with the replies as comments; an .ics file's tasks by
status and its events, with their dates; a Google Takeout .zip as one
board with a swimlane per product (Mail, Tasks, Keep, Calendar, Google
Chat); a Slack workspace export, a Mattermost bulk export, Telegram
Desktop's JSON, a WhatsApp chat .txt and a DiscordChatExporter JSON as
channels whose messages are comments, for Board View / Chat; and an
Evernote .enex as notes with their to-dos as checklists. The Export menu
writes a board as an .mbox, an .enex and a Mattermost bulk import file,
each read back by the same import.

MIME is decoded with mailparser and ENEX with htmlparser2 on the server,
and the zips are read under the zip-bomb guards of the other zip imports.
An imported message's Message-IDs pass the import sanitizer without
their brackets, so a later reply to the board joins the thread. The
instructions of the nine are translated into every language. TODO.md
also gets the next items: grouped menus, large-file import and export,
and the reports to add.

Thanks to xet7 !
2026-11-20 05:03:27 +02:00

39 lines
1.4 KiB
JavaScript

'use strict';
// Splitting an mbox (RFC 4155) into its raw messages; CommonJS so the server's
// decoder (server/lib/mailImport.js) and models/lib/mailFormat.js share it.
const MAX_MBOX_LENGTH = 256 * 1024 * 1024;
const MAX_MAIL_MESSAGES = 20000;
// The "From " separator line of an mbox: "From sender date", at the start of
// the file or after a line break.
const SEPARATOR = /^From [^\r\n]*$/;
// The raw messages of an mbox. A line ">From ..." inside a message is an
// escaped "From " (mboxrd and mboxo both escape it); one ">" is taken off.
// Text that does not start with a separator line is one message (.eml).
function splitMbox(text) {
const source = String(text == null ? '' : text).replace(/^\uFEFF/, '');
if (source.length > MAX_MBOX_LENGTH) throw new Error('Mail file is too large');
const lines = source.split(/\r\n|\n/);
if (!SEPARATOR.test(lines[0] || '')) return source.trim() ? [source] : [];
const messages = [];
let current = null;
for (const line of lines) {
if (SEPARATOR.test(line)) {
if (current) messages.push(current.join('\n'));
if (messages.length >= MAX_MAIL_MESSAGES) {
current = null;
break;
}
current = [];
} else if (current) {
current.push(/^>+From /.test(line) ? line.slice(1) : line);
}
}
if (current) messages.push(current.join('\n'));
return messages.filter(message => message.trim());
}
module.exports = { MAX_MBOX_LENGTH, MAX_MAIL_MESSAGES, splitMbox };