From 0131fea33e4fc6ba4a0b9888ad4af598b720960c Mon Sep 17 00:00:00 2001 From: Connectif Date: Thu, 19 Oct 2017 19:53:25 -0500 Subject: [PATCH 1/7] Avoid to process the same email multiple times --- index.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 183c911..2f41d0c 100644 --- a/index.js +++ b/index.js @@ -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]; @@ -78,7 +80,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() { @@ -87,7 +94,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)); + } + }); + + + async.each(results, function (result, callback) { var f = self.imap.fetch(result, { bodies: '', markSeen: self.markSeen @@ -117,6 +132,7 @@ function parseUnread() { }); } else { self.emit('mail',mail,seqno,attributes); + callback(); } }); parser.on("attachment", function (attachment) { @@ -138,10 +154,20 @@ 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; + } + }); } }); From 107d9fb5b686d1899f6aec7a48958f93abc0b5f6 Mon Sep 17 00:00:00 2001 From: Sergio Lorca Date: Thu, 19 Oct 2017 19:55:28 -0500 Subject: [PATCH 2/7] Fix unread param on empty respones --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 2f41d0c..5548187 100644 --- a/index.js +++ b/index.js @@ -169,6 +169,8 @@ function parseUnread() { } }); + } else { + self.parsingUnread = false; } }); } From 8295fc5edacc87f42bd140c7e6ac844a47fb1ed8 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Siddiqui Date: Thu, 19 Oct 2017 20:11:32 -0500 Subject: [PATCH 3/7] added restart function to reconnect to imap server --- index.js | 10 ++++++++-- test.js | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5548187..831cf98 100644 --- a/index.js +++ b/index.js @@ -39,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)); } @@ -54,6 +54,12 @@ MailListener.prototype.stop = function() { this.imap.end(); }; +MailListener.prototype.restart = function() { + this.imap.removeAllListeners('mail'); + this.imap.removeAllListeners('update'); + this.imap.connect(); +}; + function imapReady() { var self = this; this.imap.openBox(this.mailbox, false, function(err, mailbox) { diff --git a/test.js b/test.js index c22f3b5..f932ede 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ var MailListener = require("./"); var mailListener = new MailListener({ - username: "xxxx", + username: "xxx", password: "xxx", host: "imap.gmail.com", port: 993, @@ -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){ From a2bb502afc6214a7c57e9beea6b2540e9ef3278f Mon Sep 17 00:00:00 2001 From: Waqas Hussain Siddiqui Date: Thu, 19 Oct 2017 20:13:12 -0500 Subject: [PATCH 4/7] enhanced logging --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 831cf98..4cc6c28 100644 --- a/index.js +++ b/index.js @@ -55,8 +55,11 @@ MailListener.prototype.stop = function() { }; 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(); }; From 4e88fe87fe8c586050a9ead163657ecc8bace32f Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Thu, 19 Oct 2017 20:32:19 -0500 Subject: [PATCH 5/7] pass email to "attachment" event --- index.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4cc6c28..00d16ad 100644 --- a/index.js +++ b/index.js @@ -144,8 +144,8 @@ function parseUnread() { 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) { diff --git a/package.json b/package.json index 253e5a4..cee8cf6 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Mail listener library for node.js. Get notification when new email arrived.", "dependencies": { "imap": "~0.8.14", - "mailparser": "~0.4.6", + "mailparser": "^0.4.8", "async": "^0.9.0" }, "repository": { From f6a9fe701c28365c0b4119ff0ab9fe366019efd8 Mon Sep 17 00:00:00 2001 From: ahendri Date: Thu, 19 Oct 2017 20:36:16 -0500 Subject: [PATCH 6/7] Update package.json Set mime dependency version to ^1.0.0 because mime 2.0.0 package breaks the API --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index cee8cf6..40b0e1e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "dependencies": { "imap": "~0.8.14", "mailparser": "^0.4.8", - "async": "^0.9.0" + "async": "^0.9.0", + "mime": "^1.0.0" }, "repository": { "type": "git", From 36ffefda99a4f3c9a5ba4fb68bce5dfcbcfbb5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Pe=CC=81rez?= Date: Fri, 20 Oct 2017 12:16:18 -0500 Subject: [PATCH 7/7] Update package.json to publish library in npm as a new library. Update readme to describe what changes are included and be in line with the name of the new library and new features. --- package.json | 16 ++++++++-------- readme.md | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 40b0e1e..13bce4f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "mail-listener2", - "version": "0.3.1", - "description": "Mail listener library for node.js. Get notification when new email arrived.", + "name": "mail-listener2-updated", + "version": "0.3.2", + "description": "Updated Mail listener library for node.js. Get notification when new email arrived.", "dependencies": { "imap": "~0.8.14", "mailparser": "^0.4.8", @@ -10,9 +10,9 @@ }, "repository": { "type": "git", - "url": "git://github.com/chirag04/mail-listener2.git" + "url": "git://github.com/dapanas/mail-listener2.git" }, - "homepage": "https://github.com/chirag04/mail-listener2", + "homepage": "https://github.com/dapanas/mail-listener2", "keywords": [ "mail", "job", @@ -22,9 +22,9 @@ "email parser" ], "author": { - "name": "Chirag Jain", - "email": "jain_chirag04@yahoo.com", - "url": "http://chiragjain.tumblr.com" + "name": "Daniel PĂ©rez", + "email": "dapanas@gmail.com", + "url": "http://blogcito.info" }, "license": "MIT" } diff --git a/readme.md b/readme.md index 2f46edb..612a7d5 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,17 @@ # Overview -Mail-listener2 library for node.js. Get notification when new email arrived to inbox or when message metadata (e.g. flags) changes externally. Uses IMAP protocol. +Updated Mail-listener2 library for node.js. Get notification when new email arrived to inbox or when message metadata (e.g. flags) changes externally. Uses IMAP protocol. + +Updated Mail-listener2 includes changes and improvements from another forks. + +The changes are: + +* Avoid to process the same email multiple times +* Fix unread param on empty response +* Add a restart function to reconnect to imap server +* Enhanced logging for the new features +* Pass email object to attachment event +* Update mime dependency version to ^1.0.0 because mime 2.0.0 package break the API We are using these libraries: [node-imap](https://github.com/mscdex/node-imap), [mailparser](https://github.com/andris9/mailparser). @@ -10,7 +21,7 @@ Heavily inspired by [mail-listener](https://github.com/circuithub/mail-listener) Install -`npm install mail-listener2` +`npm install mail-listener2-updated` JavaScript Code: @@ -18,7 +29,7 @@ JavaScript Code: ```javascript -var MailListener = require("mail-listener2"); +var MailListener = require("mail-listener2-updated"); var mailListener = new MailListener({ username: "imap-username", @@ -62,7 +73,7 @@ mailListener.on("mail", function(mail, seqno, attributes){ // mail processing code goes here }); -mailListener.on("attachment", function(attachment){ +mailListener.on("attachment", function(attachment, email){ console.log(attachment.path); });