Skip to content

Commit

Permalink
merge upgrade node imap branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreigno committed Mar 26, 2014
1 parent f79e4e1 commit 0ef7410
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ Start listening new mails :
var notifier = require('mail-notifier');

var imap = {
username: "yourimapuser",
user: "yourimapuser",
password: "yourimappassword",
host: "imap.host.com",
port: 993, // imap port
secure: true // use secure connection
tls: true,// use secure connection
tlsOptions: { rejectUnauthorized: false }
};

notifier(imap).on('mail',function(mail){console.log(mail);}).start();
Expand All @@ -40,9 +41,15 @@ The constructor function creates a new `notifier`. Parameter provide options nee

* `host` : imap server host
* `port` : imap server port number
* `username` : imap user name
* `user` : imap user name
* `password` : imap password
* `secure` : need secure connection to server
* `tls` : need a tle connection to server
* `tlsOptions` : see `tls` module options
* `markSeen`: mark mail as read defaults to true
* `box` : mail box read from defaults to 'INBOX'
* `select`: search query defaults to ['UNSEEN']

For backward compatibility `username` is also supported.

.start()
------------------------------------
Expand Down Expand Up @@ -70,4 +77,5 @@ Sent when the IMAP connection is closed. This usually happens after a `stop` met
Dependencies
============

This module relies heavily on [node-imap](https://github.com/mscdex/node-imap).
This module relies heavily on [node-imap](https://github.com/mscdex/node-imap). For more advanced usage, please consider
using it directly.
94 changes: 40 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
'use strict';

var util = require('util'),
ImapConnection = require('imap').ImapConnection,
Imap = require('imap'),
MailParser = require('mailparser').MailParser,
Seq = require('seq'),
EventEmitter = require('events').EventEmitter;


function Notifier(opts) {
EventEmitter.call(this);
var self = this;
self.options = opts;
if (self.options.username) { //backward compat
self.options.user = self.options.username;
}
self.connected = false;
self.imap = new ImapConnection({
username: opts.username,
password: opts.password,
host: opts.host,
port: opts.port,
secure: opts.secure
});
self.imap = new Imap(opts);
self.imap.on('end', function () {
self.connected = false;
self.emit('end');
Expand All @@ -37,70 +33,60 @@ module.exports = function (opts) {

Notifier.prototype.start = function () {
var self = this;
Seq()
.seq(function () {
self.imap.connect(this);
})
.seq(function () {
self.connected = true;
self.imap.openBox('INBOX', false, this);
}).seq(function () {
util.log('successfully opened mail box');
self.imap.on('mail', function (id) {
self.scan();
});
self.imap.once('ready', function () {
self.connected = true;
self.imap.openBox(self.options.box || 'INBOX', false, function () {
self.scan();
}).catch(function (err) {
self.emit('error', err);
});
self.imap.on('mail', function (id) {
self.scan();
});
});
self.imap.connect();
return this;
};

Notifier.prototype.scan = function () {
var self = this;
Seq()
.seq(function () {
self.imap.search(['UNSEEN'], this);
})
.seq(function (seachResults) {
if (!seachResults || seachResults.length === 0) {
util.log('no new mail in INBOX');
return;
}
var fetch = self.imap.fetch(seachResults, {
markSeen: true,
request: {
headers: false,
body: "full"
}
self.imap.search(self.options.search || ['UNSEEN'], function (err, seachResults) {
if (err) {
self.emit('error', err);
}
if (!seachResults || seachResults.length === 0) {
util.log('no new mail in INBOX');
return;
}
var fetch = self.imap.fetch(seachResults, {
markSeen: self.options.markSeen === null || true,
bodies: ''
});
fetch.on('message', function (msg) {
var mp = new MailParser();
mp.once('end', function (mail) {
self.emit('mail', mail);
});
fetch.on('message', function (msg) {
var mp = new MailParser();
mp.on('end', function (mail) {
self.emit('mail', mail);
});
msg.on('data', function (chunk) {
msg.on('body', function (stream, info) {
stream.on('data', function (chunk) {
mp.write(chunk.toString());
});
msg.on('end', function () {
stream.once('end', function () {
mp.end();
});
});
fetch.on('end', function () {
util.log('Done fetching all messages!');
});
}).catch(function (err) {
});
fetch.once('end', function () {
util.log('Done fetching all messages!');
});
fetch.on('error', function () {
self.emit('error', err);
});
});
return this;
};

Notifier.prototype.stop = function () {
if (this.connected) {
this.imap.logout();
}
if (this.imap._state.conn) { //hacking into node-imap
this.imap._state.conn.end();
this.imap.end();
}
return this;
};
};
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "mail-notifier",
"description": "Nodejs library to get notified on new incoming email.",
"keywords" : ["imap", "mail", "notifier"],
"version": "0.1.2",
"author": {
"name" : "jerome creignou",
"url" : "https://github.com/jcreigno/nodejs-mail-notifier"
},
"repository" : {
"type" : "git",
"url" : "git://github.com/jcreigno/nodejs-mail-notifier.git"
},
"dependencies": {
"imap": "~0.3.1",
"mailparser": "~0.4",
"seq": "~0.3.5"
},
"main": "./index.js",
"engines": {
"node": "~0.6.9"
},
"devDependencies": {}
"name": "mail-notifier",
"description": "Nodejs library to get notified on new incoming email.",
"keywords": [
"imap",
"mail",
"notifier"
],
"version": "0.2.0",
"author": {
"name": "jerome creignou",
"url": "https://github.com/jcreigno/nodejs-mail-notifier"
},
"repository": {
"type": "git",
"url": "git://github.com/jcreigno/nodejs-mail-notifier.git"
},
"dependencies": {
"imap": "~0.8.9",
"mailparser": "~0.4"
},
"main": "./index.js",
"devDependencies": {}
}
5 changes: 3 additions & 2 deletions sample/simple-mail-notifier.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var notifier = require('../index.js');

var imap = {
username: "jerome.creignou",
user: "jerome.creignou",
password: "password",
host: "imap.host.com",
port: 993, // imap port
secure: true // use secure connection
tls: true,// use secure connection
tlsOptions: { rejectUnauthorized: false }
};

notifier(imap).on('mail',function(mail){console.log(mail);}).start();
5 changes: 3 additions & 2 deletions sample/toggled-mail-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ function toggleState(){
}

var imap = {
username: "jerome.creignou",
user: "jerome.creignou",
password: "password",
host: "imap.host.com",
port: 993, // imap port
secure: true // use secure connection
tls: true,// use secure connection
tlsOptions: { rejectUnauthorized: false }
};

var n = notifier(imap).on('mail', function(mail){
Expand Down

0 comments on commit 0ef7410

Please sign in to comment.