Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mail-listener2 with changes from another forks #74

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 46 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ var async = require('async');
module.exports = MailListener;

function MailListener(options) {
this.markSeen = !! options.markSeen;
this.haveNewEmails = false;
this.parsingUnread = false;
this.markSeen = !!options.markSeen;
this.mailbox = options.mailbox || "INBOX";
if ('string' === typeof options.searchFilter) {
this.searchFilter = [options.searchFilter];
Expand Down Expand Up @@ -37,8 +39,8 @@ function MailListener(options) {
debug: options.debug || null
});

this.imap.once('ready', imapReady.bind(this));
this.imap.once('close', imapClose.bind(this));
this.imap.on('ready', imapReady.bind(this));
this.imap.on('close', imapClose.bind(this));
this.imap.on('error', imapError.bind(this));
}

Expand All @@ -52,6 +54,15 @@ MailListener.prototype.stop = function() {
this.imap.end();
};

MailListener.prototype.restart = function() {
console.log('detaching existing listener');
this.imap.removeAllListeners('mail');
this.imap.removeAllListeners('update');

console.log('calling imap connect');
this.imap.connect();
};

function imapReady() {
var self = this;
this.imap.openBox(this.mailbox, false, function(err, mailbox) {
Expand All @@ -78,7 +89,12 @@ function imapError(err) {
}

function imapMail() {
parseUnread.call(this);
if (!this.haveNewEmails && !this.parsingUnread) {
parseUnread.call(this);
this.parsingUnread = true;
} else if (this.parsingUnread) {
this.haveNewEmails = true;
}
}

function parseUnread() {
Expand All @@ -87,7 +103,15 @@ function parseUnread() {
if (err) {
self.emit('error', err);
} else if (results.length > 0) {
async.each(results, function( result, callback) {

self.imap.setFlags(results, ['\\Seen'], function (err) {
if (err) {
console.log(JSON.stringify(err, null, 2));
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dapanas is it necessary to set a message as \Seen here? It voids markSeen options



async.each(results, function (result, callback) {
var f = self.imap.fetch(result, {
bodies: '',
markSeen: self.markSeen
Expand Down Expand Up @@ -117,10 +141,11 @@ function parseUnread() {
});
} else {
self.emit('mail',mail,seqno,attributes);
callback();
}
});
parser.on("attachment", function (attachment) {
self.emit('attachment', attachment);
parser.on("attachment", function (attachment, email) {
self.emit('attachment', attachment, email);
});
msg.on('body', function(stream, info) {
stream.on('data', function(chunk) {
Expand All @@ -138,11 +163,23 @@ function parseUnread() {
f.once('error', function(err) {
self.emit('error', err);
});
}, function(err){
if( err ) {
}, function (err) {
console.log('all process');
if (err) {
self.emit('error', err);
}


if (self.haveNewEmails) {
self.haveNewEmails = false;
parseUnread.call(self);
} else {
self.parsingUnread = false;
}

});
} else {
self.parsingUnread = false;
}
});
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "Mail listener library for node.js. Get notification when new email arrived.",
"dependencies": {
"imap": "~0.8.14",
"mailparser": "~0.4.6",
"async": "^0.9.0"
"mailparser": "^0.4.8",
"async": "^0.9.0",
"mime": "^1.0.0"
},
"repository": {
"type": "git",
Expand Down
6 changes: 5 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var MailListener = require("./");

var mailListener = new MailListener({
username: "xxxx",
username: "xxx",
password: "xxx",
host: "imap.gmail.com",
port: 993,
Expand All @@ -22,6 +22,10 @@ mailListener.on("server:connected", function(){

mailListener.on("server:disconnected", function(){
console.log("imapDisconnected");
setTimeout(function() {
console.log("Trying to establish imap connection again");
mailListener.restart();
}, 5* 1000);
});

mailListener.on("error", function(err){
Expand Down