Permalink
Browse files

Merge pull request #26 from jakestubbz/master

Updated to work with node-imap v0.8.0
  • Loading branch information...
2 parents 3ee9e9f + 9581b61 commit a299e41305b3b7b685f3216da2a0666c6067f9b2 @andrewseddon andrewseddon committed Jul 29, 2013
Showing with 61 additions and 59 deletions.
  1. +1 −0 .gitignore
  2. +35 −36 dist/mail.listener.js
  3. +2 −2 package.json
  4. BIN src/.DS_Store
  5. +23 −21 src/mail.listener.coffee
View
@@ -2,3 +2,4 @@ node_modules/
mail-listener.sublime-project
mail-listener.sublime-workspace
*example.coffee
+.DS_Store
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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
Binary file not shown.
View
@@ -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

0 comments on commit a299e41

Please sign in to comment.