Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 286 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ ImapSimple.prototype.openBox = function (boxName, callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.openBox(boxName, function (err, result) {

Expand Down Expand Up @@ -115,6 +140,32 @@ ImapSimple.prototype.closeBox = function (autoExpunge = true, callback) {

return new Promise(function (resolve, reject) {

function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.closeBox(autoExpunge, function (err, result) {

if (err) {
Expand Down Expand Up @@ -255,6 +306,33 @@ ImapSimple.prototype.getPartData = function (message, part, callback) {
}

return new Promise(function (resolve, reject) {

function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

var fetch = self.imap.fetch(message.attributes.uid, {
bodies: [part.partID],
struct: true
Expand Down Expand Up @@ -344,6 +422,32 @@ ImapSimple.prototype.moveMessage = function (source, boxName, callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.move(source, boxName, function (err) {
if (err) {
reject(err);
Expand Down Expand Up @@ -375,6 +479,32 @@ ImapSimple.prototype.addMessageLabel = function (source, labels, callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.addLabels(source, labels, function (err) {
if (err) {
reject(err);
Expand Down Expand Up @@ -404,6 +534,32 @@ ImapSimple.prototype.addFlags = function (uid, flags, callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.addFlags(uid, flags, function (err) {
if (err) {
reject(err);
Expand Down Expand Up @@ -433,6 +589,32 @@ ImapSimple.prototype.delFlags = function (uid, flags, callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.delFlags(uid, flags, function (err) {
if (err) {
reject(err);
Expand Down Expand Up @@ -466,6 +648,32 @@ ImapSimple.prototype.append = function (message, options, callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.append(message, options, function (err) {
if (err) {
reject(err);
Expand Down Expand Up @@ -493,6 +701,32 @@ ImapSimple.prototype.getBoxes = function (callback) {
}

return new Promise(function (resolve, reject) {
function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.getBoxes(function (err, boxes) {
if (err) {
reject(err);
Expand Down Expand Up @@ -521,6 +755,32 @@ ImapSimple.prototype.addBox = function (boxName, callback) {

return new Promise(function (resolve, reject) {

function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.addBox(boxName, function (err) {

if (err) {
Expand Down Expand Up @@ -550,6 +810,32 @@ ImapSimple.prototype.delBox = function (boxName, callback) {

return new Promise(function (resolve, reject) {

function imapOnError(err) {
if (err.source === 'timeout-auth') {
err = new errors.ConnectionTimeoutError(25000); // Temporary, need to take authTimeout from 'options'
}

self.imap.removeListener('close', imapOnClose);
self.imap.removeListener('end', imapOnEnd);
reject(err);
}

function imapOnEnd() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('close', imapOnClose);
reject(new Error('Connection ended unexpectedly'));
}

function imapOnClose() {
self.imap.removeListener('error', imapOnError);
self.imap.removeListener('end', imapOnEnd);
reject(new Error('Connection closed unexpectedly'));
}

self.imap.once('error', imapOnError);
self.imap.once('close', imapOnClose);
self.imap.once('end', imapOnEnd);

self.imap.delBox(boxName, function (err) {

if (err) {
Expand Down