summaryrefslogtreecommitdiff
path: root/app/imap.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/imap.js')
-rw-r--r--app/imap.js49
1 files changed, 36 insertions, 13 deletions
diff --git a/app/imap.js b/app/imap.js
index 4041089..d8caa0e 100644
--- a/app/imap.js
+++ b/app/imap.js
@@ -1,9 +1,10 @@
const { ipcMain } = require('electron');
const { ImapFlow } = require('imapflow');
-ipcMain.on('imap:listTree:from', listTreeFrom);
-ipcMain.on('imap:listTree:to', listTreeTo);
-ipcMain.on('imap:migrate', migrate);
+ipcMain.on('imap:connect', apiConnect);
+ipcMain.on('imap:listTree:from', apiListTreeFrom);
+ipcMain.on('imap:listTree:to', apiListTreeTo);
+ipcMain.on('imap:migrate', apiMigrate);
/**
* Connect to server
@@ -51,7 +52,22 @@ ImapFlow.prototype.fetchArray = async function (range, query, options = {}) {
* @param {IpcMainEvent} event
* @param {object} options
*/
-async function listTreeFrom (event, options) {
+async function apiConnect (event, options) {
+ try {
+ const client = await connect(options);
+
+ event.reply('imap:connect:reply', true);
+ await client.logout();
+ } catch (e) {
+ event.reply('imap:connect:error', e);
+ }
+}
+
+/**
+ * @param {IpcMainEvent} event
+ * @param {object} options
+ */
+async function apiListTreeFrom (event, options) {
try {
const client = await connect(options);
@@ -66,7 +82,7 @@ async function listTreeFrom (event, options) {
* @param {IpcMainEvent} event
* @param {object} options
*/
-async function listTreeTo (event, options) {
+async function apiListTreeTo (event, options) {
try {
const client = await connect(options);
@@ -82,7 +98,7 @@ async function listTreeTo (event, options) {
* @param {object} from
* @param {object} to
*/
-async function migrate (event, { from, to }) {
+async function apiMigrate (event, { from, to }) {
const fromClient = await connect(from);
const toClient = await connect(to);
@@ -108,17 +124,24 @@ async function migrate (event, { from, to }) {
event.reply('imap:migrate:progress', '- Comparing messages');
const msgs = fromMsgs.filter((fromMsg) => !toMsgs.some((toMsg) => Buffer.compare(toMsg.source, fromMsg.source) === 0));
- event.reply('imap:migrate:progress', `- Message status: Found ${fromMsgs.length} messages total and ${msgs.length} new messages to migrate`);
+ event.reply('imap:migrate:progress', `- Found ${fromMsgs.length} messages total and ${msgs.length} new messages to migrate`);
- await toClient.noop();
- for (const msg of msgs) {
- appendCommands.push(toClient.append(fromFolder.path, msg.source, Array.from(msg.flags), msg.envelope.date));
+ if (msgs.length > 0) {
+ event.reply('imap:migrate:progress', '- Starting migration');
+
+ await toClient.noop();
+ for (const msg of msgs) {
+ appendCommands.push(toClient.append(fromFolder.path, msg.source, Array.from(msg.flags), msg.envelope.date));
+ }
+
+ await Promise.all(appendCommands);
+ event.reply('imap:migrate:progress', '- Done');
}
- } finally {
- await Promise.all(appendCommands);
- event.reply('imap:migrate:progress', '- Done');
+
fromMailbox.release();
toMailbox.release();
+ } catch (e) {
+ event.reply('imap:migrate:error', e);
}
}