import Imap, { ImapMessageAttributes, Box } from "imap"; import { getMailbox, updateMailbox } from "../../db/imap/imap-db"; import { Attrs, AttrsWithEnvelope } from "../../interfaces/mail/attrs.interface"; import logger from "../../system/Logger"; import RegisterMessageInApp from "../message/saveMessage"; import { saveMessage } from "../message/storeMessage"; import updateMessage from "../message/updateMessage"; export interface ImapInfo { uid: number; modseq: string; flags: string[]; } export default class Mailbox { imap: Imap; boxName: string; id: number; box: Box; msgToSync: number; syncing: boolean; constructor(_imap: Imap, _boxId: number, _boxName: string) { this.imap = _imap; this.boxName = _boxName; this.id = _boxId; this.box; this.msgToSync = 0; this.syncing = false; this.init(); } async init() { // get mailbox from the database this.box = (await getMailbox(this.id))[0]; const isReadOnly = false; this.imap.openBox(this.boxName, isReadOnly, (err, box) => { if (err) logger.err(err); // sync only if has new messages if (this.box.uidnext < box.uidnext) { this.sync(this.box.uidnext, box.uidnext); } else { logger.log("Already up to date"); } // wait for new mails this.imap.on("mail", (numNewMsgs: number) => { if (!this.syncing) { // if not syncing restart a sync this.sync(this.box.uidnext, this.box.uidnext + numNewMsgs); } else { // else save number of message to sync latter this.msgToSync += numNewMsgs; } }); this.imap.on("update", (seqno: number, info: ImapInfo) => { logger.log(`Update message ${info.uid} with ${info.flags}`); const updateMsg = new updateMessage(info.uid, info.flags); updateMsg.updateFlags(); }); }); } async sync(savedUid: number, currentUid: number) { this.syncing = true; const promises: Promise[] = []; const mails: Attrs[] = []; logger.log(`Syncing from ${savedUid} to ${currentUid} uid`); const f = this.imap.seq.fetch(`${savedUid}:${currentUid}`, { size: true, envelope: true, }); f.on("message", (msg, seqno) => { msg.once("attributes", (attrs: AttrsWithEnvelope) => { mails.push(attrs); promises.push(saveMessage(attrs, this.id, this.imap)); }); }); f.once("error", (err) => { logger.err("Fetch error: " + err); }); f.once("end", async () => { let step = 20; for (let i = 0; i < promises.length; i += step) { for (let j = i; j < (i + step && promises.length); j++) { await new Promise((resolve, reject) => { promises[j] .then(async (res: number) => { const register = new RegisterMessageInApp(res, mails[j], this.id); await register.save(); resolve(""); }) .catch((err) => { reject(err); }); }); } logger.log(`Saved messages ${i + step > promises.length ? promises.length : i + step}/${mails.length}`); updateMailbox(this.id, mails[i].uid); } updateMailbox(this.id, currentUid); this.syncing = false; // if has receive new msg during last sync then start a new sync if (this.msgToSync > 0) { const currentUid = this.box.uidnext; this.box.uidnext += this.msgToSync; // reset value to allow to detect new incoming message while syncing this.msgToSync = 0; this.sync(currentUid, this.box.uidnext); } }); } addFlag(source: string, flags: string[]): Promise { return new Promise((resolve, reject) => { this.imap.addFlags(source, flags, (err) => { if (err) { reject(err); } else { resolve(); } }); }); } removeFlag(source: string, flags: string[]): Promise { return new Promise((resolve, reject) => { this.imap.delFlags(source, flags, (err) => { if (err) { reject(err); } else { resolve(); } }); }); } }