Comparing version 0.7.6 to 0.7.7
@@ -126,5 +126,8 @@ var assert = require('assert'), | ||
if (this._options.secure) | ||
socket = tls.connect({ socket: state.conn }, onconnect); | ||
else | ||
if (this._options.secure) { | ||
if (process.version.indexOf('v0.6.') > -1) | ||
socket = tls.connect(null, { socket: state.conn }, onconnect); | ||
else | ||
socket = tls.connect({ socket: state.conn }, onconnect); | ||
} else | ||
state.conn.once('connect', onconnect); | ||
@@ -144,2 +147,3 @@ | ||
self.debug&&self.debug('[connection] FIN packet received. Disconnecting...'); | ||
clearTimeout(state.tmrConn); | ||
self.emit('end'); | ||
@@ -159,5 +163,7 @@ }); | ||
clearTimeout(state.tmrConn); | ||
err.level = 'socket'; | ||
if (state.status === STATES.NOCONNECT) | ||
loginCb(new Error('Unable to connect. Reason: ' + err)); | ||
self.emit('error', err); | ||
loginCb(err); | ||
else | ||
self.emit('error', err); | ||
self.debug&&self.debug('[connection] Error occurred: ' + err); | ||
@@ -575,4 +581,12 @@ }); | ||
case 'BAD': | ||
if (state.status === STATES.NOAUTH) | ||
if (state.status === STATES.NOAUTH) { | ||
clearTimeout(state.tmrConn); | ||
var err = new Error('Received negative welcome (' + m[3] + ')'); | ||
err.level = 'protocol'; | ||
if (state.status === STATES.NOCONNECT) | ||
loginCb(err); | ||
else | ||
self.emit('error', err); | ||
state.conn.end(); | ||
} | ||
break; | ||
@@ -638,2 +652,3 @@ } | ||
err = new Error('Unexpected continuation'); | ||
err.level = 'protocol'; | ||
err.type = 'continuation'; | ||
@@ -648,2 +663,3 @@ err.request = cmdstr; | ||
err = new Error(m[3]); | ||
err.level = 'protocol'; | ||
err.type = 'failure'; | ||
@@ -716,3 +732,5 @@ err.code = m[2]; | ||
state.conn = undefined; | ||
loginCb(new Error('Connection timed out')); | ||
var err = new Error('Connection timed out'); | ||
err.level = 'timeout'; | ||
loginCb(err); | ||
}, this._options.connTimeout); | ||
@@ -925,3 +943,4 @@ }; | ||
ImapConnection.prototype._fetch = function(which, uids, options, what, cb) { | ||
if (uids === undefined || uids === null | ||
if (uids === undefined | ||
|| uids === null | ||
|| (Array.isArray(uids) && uids.length === 0)) | ||
@@ -935,10 +954,43 @@ throw new Error('Nothing to fetch'); | ||
var toFetch = '', prefix = ' BODY[', extensions, self = this, | ||
parse = true, headers, key, stream, | ||
parse, headers, key, stream, | ||
fetchers = {}; | ||
if (typeof what === 'function') { | ||
cb = what; | ||
what = options; | ||
options = {}; | ||
// argument detection! | ||
if (cb === undefined) { | ||
// fetch(uids, xxxx, yyyy) | ||
if (what === undefined) { | ||
// fetch(uids, xxxx) | ||
if (options === undefined) { | ||
// fetch(uids) | ||
what = options = {}; | ||
} else if (typeof options === 'function') { | ||
// fetch(uids, callback) | ||
cb = options; | ||
what = options = {}; | ||
} else if (options.struct !== undefined | ||
|| options.size !== undefined | ||
|| options.markSeen !== undefined) { | ||
// fetch(uids, options) | ||
what = {}; | ||
} else { | ||
// fetch(uids, what) | ||
what = options; | ||
options = {}; | ||
} | ||
} else if (typeof what === 'function') { | ||
// fetch(uids, xxxx, callback) | ||
cb = what; | ||
if (options.struct !== undefined | ||
|| options.size !== undefined | ||
|| options.markSeen !== undefined) { | ||
// fetch(uids, options, callback) | ||
what = {}; | ||
} else { | ||
// fetch(uids, what, callback) | ||
what = options; | ||
options = {}; | ||
} | ||
} | ||
} | ||
if (!Array.isArray(what)) | ||
@@ -949,2 +1001,3 @@ what = [what]; | ||
wp = what[i]; | ||
parse = true; | ||
if (wp.id !== undefined && !/^(?:[\d]+[\.]{0,1})*[\d]+$/.test(''+wp.id)) | ||
@@ -1151,3 +1204,3 @@ throw new Error('Invalid part id: ' + wp.id); | ||
} | ||
cb(err); | ||
cb&&cb(err); | ||
}); | ||
@@ -1154,0 +1207,0 @@ |
{ "name": "imap", | ||
"version": "0.7.6", | ||
"version": "0.7.7", | ||
"author": "Brian White <mscdex@mscdex.net>", | ||
"description": "An IMAP module for node.js that makes communicating with IMAP servers easy", | ||
"main": "./lib/imap", | ||
"engines": { "node" : ">=0.4.0" }, | ||
"engines": { "node" : ">=0.6.0" }, | ||
"dependencies": { | ||
@@ -8,0 +8,0 @@ "utf7": "1.0.0" |
177
README.md
@@ -13,3 +13,3 @@ Description | ||
* [node.js](http://nodejs.org/) -- v0.4.0 or newer | ||
* [node.js](http://nodejs.org/) -- v0.6.0 or newer | ||
* An IMAP server -- tested with gmail | ||
@@ -29,81 +29,43 @@ | ||
```javascript | ||
var Imap = require('imap'); | ||
var imap = new Imap({ | ||
user: 'mygmailname@gmail.com', | ||
password: 'mygmailpassword', | ||
host: 'imap.gmail.com', | ||
port: 993, | ||
secure: true | ||
}); | ||
var Imap = require('imap'), | ||
inspect = require('util').inspect; | ||
var imap = new Imap({ | ||
user: 'mygmailname@gmail.com', | ||
password: 'mygmailpassword', | ||
host: 'imap.gmail.com', | ||
port: 993, | ||
secure: true | ||
}); | ||
function show(obj) { | ||
return util.inspect(obj, false, Infinity); | ||
} | ||
function show(obj) { | ||
return inspect(obj, false, Infinity); | ||
} | ||
function die(err) { | ||
console.log('Uh oh: ' + err); | ||
process.exit(1); | ||
} | ||
function die(err) { | ||
console.log('Uh oh: ' + err); | ||
process.exit(1); | ||
} | ||
function openInbox(cb) { | ||
imap.connect(function(err) { | ||
if (err) die(err); | ||
imap.openBox('INBOX', true, cb); | ||
}); | ||
} | ||
openInbox(function(err, mailbox) { | ||
function openInbox(cb) { | ||
imap.connect(function(err) { | ||
if (err) die(err); | ||
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { | ||
if (err) die(err); | ||
imap.fetch(results, | ||
{ headers: ['from', 'to', 'subject', 'date'], | ||
cb: function(fetch) { | ||
fetch.on('message', function(msg) { | ||
console.log('Saw message no. ' + msg.seqno); | ||
msg.on('headers', function(hdrs) { | ||
console.log('Headers for no. ' + msg.seqno + ': ' + show(hdrs)); | ||
}); | ||
msg.on('end', function() { | ||
console.log('Finished message no. ' + msg.seqno); | ||
}); | ||
}); | ||
} | ||
}, function(err) { | ||
if (err) throw err; | ||
console.log('Done fetching all messages!'); | ||
imap.logout(); | ||
} | ||
); | ||
}); | ||
imap.openBox('INBOX', true, cb); | ||
}); | ||
``` | ||
} | ||
* Retrieve the 'from' header and buffer the entire body of the newest message: | ||
```javascript | ||
// using the functions and variables already defined in the first example ... | ||
openInbox(function(err, mailbox) { | ||
openInbox(function(err, mailbox) { | ||
if (err) die(err); | ||
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { | ||
if (err) die(err); | ||
imap.seq.fetch(mailbox.messages.total + ':*', { struct: false }, | ||
{ headers: 'from', | ||
body: true, | ||
imap.fetch(results, | ||
{ headers: ['from', 'to', 'subject', 'date'], | ||
cb: function(fetch) { | ||
fetch.on('message', function(msg) { | ||
console.log('Saw message no. ' + msg.seqno); | ||
var body = ''; | ||
msg.on('headers', function(hdrs) { | ||
console.log('Headers for no. ' + msg.seqno + ': ' + show(hdrs)); | ||
}); | ||
msg.on('data', function(chunk) { | ||
body += chunk.toString('utf8'); | ||
}); | ||
msg.on('end', function() { | ||
console.log('Finished message no. ' + msg.seqno); | ||
console.log('UID: ' + msg.uid); | ||
console.log('Flags: ' + msg.flags); | ||
console.log('Date: ' + msg.date); | ||
console.log('Body: ' + show(body)); | ||
}); | ||
@@ -119,36 +81,75 @@ }); | ||
}); | ||
}); | ||
``` | ||
* Retrieve the 'from' header and buffer the entire body of the newest message: | ||
```javascript | ||
// using the functions and variables already defined in the first example ... | ||
openInbox(function(err, mailbox) { | ||
if (err) die(err); | ||
imap.seq.fetch(mailbox.messages.total + ':*', { struct: false }, | ||
{ headers: 'from', | ||
body: true, | ||
cb: function(fetch) { | ||
fetch.on('message', function(msg) { | ||
console.log('Saw message no. ' + msg.seqno); | ||
var body = ''; | ||
msg.on('headers', function(hdrs) { | ||
console.log('Headers for no. ' + msg.seqno + ': ' + show(hdrs)); | ||
}); | ||
msg.on('data', function(chunk) { | ||
body += chunk.toString('utf8'); | ||
}); | ||
msg.on('end', function() { | ||
console.log('Finished message no. ' + msg.seqno); | ||
console.log('UID: ' + msg.uid); | ||
console.log('Flags: ' + msg.flags); | ||
console.log('Date: ' + msg.date); | ||
console.log('Body: ' + show(body)); | ||
}); | ||
}); | ||
} | ||
}, function(err) { | ||
if (err) throw err; | ||
console.log('Done fetching all messages!'); | ||
imap.logout(); | ||
} | ||
); | ||
}); | ||
``` | ||
* Save raw unread emails since May 20, 2010 to files: | ||
```javascript | ||
// using the functions and variables already defined in the first example ... | ||
// using the functions and variables already defined in the first example ... | ||
var fs = require('fs'), fileStream; | ||
var fs = require('fs'), fileStream; | ||
openInbox(function(err, mailbox) { | ||
openInbox(function(err, mailbox) { | ||
if (err) die(err); | ||
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { | ||
if (err) die(err); | ||
imap.search([ 'UNSEEN', ['SINCE', 'May 20, 2010'] ], function(err, results) { | ||
if (err) die(err); | ||
imap.fetch(results, | ||
{ headers: { parse: false }, | ||
body: true, | ||
cb: function(fetch) { | ||
fetch.on('message', function(msg) { | ||
console.log('Got a message with sequence number ' + msg.seqno); | ||
fileStream = fs.createWriteStream('msg-' + msg.seqno + '-body.txt'); | ||
msg.on('data', function(chunk) { | ||
fileStream.write(chunk); | ||
}); | ||
msg.on('end', function() { | ||
fileStream.end(); | ||
console.log('Finished message no. ' + msg.seqno); | ||
}); | ||
imap.fetch(results, | ||
{ headers: { parse: false }, | ||
body: true, | ||
cb: function(fetch) { | ||
fetch.on('message', function(msg) { | ||
console.log('Got a message with sequence number ' + msg.seqno); | ||
fileStream = fs.createWriteStream('msg-' + msg.seqno + '-body.txt'); | ||
msg.on('data', function(chunk) { | ||
fileStream.write(chunk); | ||
}); | ||
} | ||
}, function(err) { | ||
msg.on('end', function() { | ||
fileStream.end(); | ||
console.log('Finished message no. ' + msg.seqno); | ||
}); | ||
}); | ||
} | ||
); | ||
}); | ||
}, function(err) { | ||
} | ||
); | ||
}); | ||
}); | ||
``` | ||
@@ -155,0 +156,0 @@ |
318467
6253
663