-
Notifications
You must be signed in to change notification settings - Fork 116
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 #72
Changes from all commits
0131fea
107d9fb
8295fc5
a2bb502
4e88fe8
f6a9fe7
36ffefd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]; | ||
|
@@ -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)); | ||
} | ||
|
||
|
@@ -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) { | ||
|
@@ -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() { | ||
|
@@ -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)); | ||
} | ||
}); | ||
|
||
|
||
async.each(results, function (result, callback) { | ||
var f = self.imap.fetch(result, { | ||
bodies: '', | ||
markSeen: self.markSeen | ||
|
@@ -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) { | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. console.log is redundant |
||
if (err) { | ||
self.emit('error', err); | ||
} | ||
|
||
|
||
if (self.haveNewEmails) { | ||
self.haveNewEmails = false; | ||
parseUnread.call(self); | ||
} else { | ||
self.parsingUnread = false; | ||
} | ||
|
||
}); | ||
} else { | ||
self.parsingUnread = false; | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
{ | ||
"name": "mail-listener2", | ||
"version": "0.3.1", | ||
"description": "Mail listener library for node.js. Get notification when new email arrived.", | ||
"name": "mail-listener2-updated", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name is wrong |
||
"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.6", | ||
"async": "^0.9.0" | ||
"mailparser": "^0.4.8", | ||
"async": "^0.9.0", | ||
"mime": "^1.0.0" | ||
}, | ||
"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", | ||
|
@@ -21,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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. url, homepage, author are wrong |
||
}, | ||
"license": "MIT" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. description should be fixed |
||
|
||
We are using these libraries: [node-imap](https://github.com/mscdex/node-imap), [mailparser](https://github.com/andris9/mailparser). | ||
|
||
|
@@ -10,15 +21,15 @@ Heavily inspired by [mail-listener](https://github.com/circuithub/mail-listener) | |
|
||
Install | ||
|
||
`npm install mail-listener2` | ||
`npm install mail-listener2-updated` | ||
|
||
|
||
JavaScript Code: | ||
|
||
|
||
```javascript | ||
|
||
var MailListener = require("mail-listener2"); | ||
var MailListener = require("mail-listener2-updated"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. package name is wrong |
||
|
||
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); | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two consoles probably can be removed