Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Updated to work with node-imap v0.8.0 #26

Merged
merged 5 commits into from

3 participants

@jakestubbz

Updated to make current functionality work with node-imap v0.8.0.

Fixes #24
Fixes #25

Jake Stubbs and others added some commits
Jake Stubbs Updated to work with node-imap v0.8.0 fcd080d
Jake Stubbs Ignored .DS_Store cb39f21
@jakestubbz jakestubbz Delete .DS_Store d64c725
Jake Stubbs Increased imap dependencie version 85542c6
Jake Stubbs Bug fix
Fixed bug with markSeen not correctly updating messages.
9581b61
@jakestubbz jakestubbz closed this
@jakestubbz jakestubbz reopened this
@chirag04

@jakestubbz you might want to publish your fork as a new module and maintain it urself. I would love to contribute. I dont think mail-listener is maintained anymore.

Also, adding test-cases would be great.

@jakestubbz

I would have no problem maintaining this as a separate module but the code is pretty much @podviaznikov. It wouldn't make sense to run this as another module if mail-listener is still going to be maintained as it would be identical.

Let's give it a couple of days and see what happens with this pull request.

@andrewseddon andrewseddon merged commit a299e41 into from
@chirag04

@jakestubbz I started maintaining my fork here. https://npmjs.org/package/mail-listener2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 27, 2013
  1. Updated to work with node-imap v0.8.0

    Jake Stubbs authored
  2. Ignored .DS_Store

    Jake Stubbs authored
  3. @jakestubbz

    Delete .DS_Store

    jakestubbz authored
  4. Increased imap dependencie version

    Jake Stubbs authored
Commits on Jul 28, 2013
  1. Bug fix

    Jake Stubbs authored
    Fixed bug with markSeen not correctly updating messages.
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -2,3 +2,4 @@ node_modules/
mail-listener.sublime-project
mail-listener.sublime-workspace
*example.coffee
+.DS_Store
View
71 dist/mail.listener.js
@@ -1,6 +1,6 @@
-// Generated by CoffeeScript 1.6.2
+// Generated by CoffeeScript 1.6.3
(function() {
- var EventEmitter, ImapConnection, MailListener, MailParser,
+ var EventEmitter, Imap, MailListener, MailParser,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -9,7 +9,7 @@
MailParser = require("mailparser").MailParser;
- ImapConnection = require("imap").ImapConnection;
+ Imap = require("imap");
MailListener = (function(_super) {
var imap;
@@ -19,22 +19,22 @@
function MailListener(options) {
this._parseUnreadEmails = __bind(this._parseUnreadEmails, this);
this.stop = __bind(this.stop, this);
- this.start = __bind(this.start, this); this.fetchUnreadOnStart = options.fetchUnreadOnStart;
+ this.start = __bind(this.start, this);
+ this.fetchUnreadOnStart = options.fetchUnreadOnStart;
this.markSeen = options.markSeen;
- this.imap = new ImapConnection({
- username: options.username,
+ this.imap = new Imap({
+ user: options.username,
password: options.password,
host: options.host,
port: options.port,
- secure: options.secure
+ tls: options.secure
});
this.mailbox = options.mailbox || "INBOX";
}
MailListener.prototype.start = function() {
var _this = this;
-
- return this.imap.connect(function(err) {
+ this.imap.once('ready', function(err) {
if (err) {
return _this.emit("error", err);
} else {
@@ -54,11 +54,11 @@
});
}
});
+ return this.imap.connect();
};
MailListener.prototype.stop = function() {
var _this = this;
-
return this.imap.logout(function() {
return _this.emit("server:disconnected");
});
@@ -66,42 +66,41 @@
MailListener.prototype._parseUnreadEmails = function() {
var _this = this;
-
return this.imap.search(["UNSEEN"], function(err, searchResults) {
- var params;
-
+ var fetch, markSeen;
if (err) {
return _this.emit("error", err);
} else {
if (Array.isArray(searchResults) && searchResults.length === 0) {
return;
}
- params = {};
if (_this.markSeen) {
- params.markSeen = true;
+ markSeen = true;
}
- return _this.imap.fetch(searchResults, params, {
- headers: {
- parse: true
- },
- body: true,
- cb: function(fetch) {
- return fetch.on("message", function(msg) {
- var parser;
-
- parser = new MailParser;
- parser.on("end", function(mail) {
- mail.uid = msg.uid;
- return _this.emit("mail:parsed", mail);
- });
- msg.on("data", function(data) {
- return parser.write(data.toString());
- });
- return msg.on("end", function() {
- return parser.end();
- });
+ fetch = _this.imap.fetch(searchResults, {
+ bodies: '',
+ markSeen: markSeen
+ });
+ return fetch.on("message", function(msg, id) {
+ var parser;
+ parser = new MailParser;
+ parser.on("end", function(mail) {
+ mail.uid = id;
+ return _this.emit("mail:parsed", mail);
+ });
+ msg.on("body", function(stream, info) {
+ var buffer;
+ buffer = '';
+ stream.on("data", function(chunk) {
+ return buffer += chunk;
});
- }
+ return stream.once("end", function() {
+ return parser.write(buffer);
+ });
+ });
+ return msg.on("end", function() {
+ return parser.end();
+ });
});
}
});
View
4 package.json
@@ -3,7 +3,7 @@
"name": "mail-listener",
"description": "Mail listener library for node.js. Get notification when new email arrived.",
"keywords": ["mail", "job", "imap", "mail listener", "email", "email parser"],
- "version": "0.7.1",
+ "version": "0.7.2",
"repository": {
"type": "git",
"url": "git://github.com/circuithub/mail-listener.git"
@@ -13,7 +13,7 @@
"node": ">=0.8.10"
},
"dependencies": {
- "imap": ">=0.7.8",
+ "imap": ">=0.8.0",
"mailparser": ">=0.3.4"
},
"devDependencies": {
View
BIN  src/.DS_Store
Binary file not shown
View
44 src/mail.listener.coffee
@@ -1,6 +1,6 @@
{EventEmitter} = require "events"
{MailParser} = require "mailparser"
-{ImapConnection} = require "imap"
+Imap = require "imap"
# MailListener class. Can `emit` events in `node.js` fashion.
class MailListener extends EventEmitter
@@ -10,18 +10,18 @@ class MailListener extends EventEmitter
@fetchUnreadOnStart = options.fetchUnreadOnStart
@markSeen = options.markSeen
# TODO add validation for required parameters
- @imap = new ImapConnection
- username: options.username
+ @imap = new Imap
+ user: options.username
password: options.password
host: options.host
port: options.port
- secure: options.secure
+ tls: options.secure
@mailbox = options.mailbox || "INBOX"
# start listener
start: =>
# 1. connect to imap server
- @imap.connect (err) =>
+ @imap.once 'ready', (err) =>
if err
@emit "error", err
else
@@ -38,6 +38,7 @@ class MailListener extends EventEmitter
@emit "mail:arrived", id
# 4. find all unseen emails
@_parseUnreadEmails()
+ @imap.connect()
# stop listener
stop: =>
@@ -52,23 +53,24 @@ class MailListener extends EventEmitter
if Array.isArray(searchResults) and searchResults.length == 0
return
# 5. fetch emails
- params = {}
if @markSeen
- params.markSeen = true
- @imap.fetch searchResults, params,
- headers:
- parse: true
- body: true
- cb: (fetch) =>
- # 6. email was fetched. Parse it!
- fetch.on "message", (msg) =>
- parser = new MailParser
- parser.on "end", (mail) =>
- mail.uid = msg.uid
- @emit "mail:parsed", mail
- msg.on "data", (data) -> parser.write data.toString()
- msg.on "end", ->
- parser.end()
+ markSeen = true
+
+ fetch = @imap.fetch(searchResults, { bodies: '', markSeen: markSeen })
+ # 6. email was fetched. Parse it!
+ fetch.on "message", (msg, id) =>
+ parser = new MailParser
+ parser.on "end", (mail) =>
+ mail.uid = id
+ @emit "mail:parsed", mail
+ msg.on "body", (stream, info) =>
+ buffer = '';
+ stream.on "data", (chunk) =>
+ buffer += chunk
+ stream.once "end", ->
+ parser.write buffer
+ msg.on "end", ->
+ parser.end()
# imap
imap = @imap
Something went wrong with that request. Please try again.