From b0e88cfd2b9a31f79af5f57e1464bfec3cce2ba5 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 28 Jan 2012 22:00:36 -0500 Subject: [PATCH] Fix sequence number-based functions and do not connect to the server before all event handlers are attached first Fixes #46 Fixes #26 --- imap.js | 72 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/imap.js b/imap.js index 2915677..8afbd77 100644 --- a/imap.js +++ b/imap.js @@ -1,5 +1,6 @@ var util = require('util'), net = require('net'), - tls = require('tls'), EventEmitter = require('events').EventEmitter; + tls = require('tls'), EventEmitter = require('events').EventEmitter, + Socket = net.Socket; var emptyFn = function() {}, CRLF = '\r\n', debug=emptyFn, STATES = { NOCONNECT: 0, @@ -68,9 +69,6 @@ function ImapConnection (options) { util.inherits(ImapConnection, EventEmitter); exports.ImapConnection = ImapConnection; -/* Namespace for seqno-based commands */ -ImapConnection.prototype.seq = {}; - ImapConnection.prototype.connect = function(loginCb) { var self = this, fnInit = function() { @@ -100,10 +98,7 @@ ImapConnection.prototype.connect = function(loginCb) { loginCb = loginCb || emptyFn; this._reset(); - this._state.conn = net.createConnection(this._options.port, this._options.host); - - this._state.tmrConn = setTimeout(this._fnTmrConn.bind(this), - this._options.connTimeout, loginCb); + this._state.conn = new Socket(); this._state.conn.setKeepAlive(true); if (this._options.secure) { @@ -510,6 +505,10 @@ ImapConnection.prototype.connect = function(loginCb) { debug('Connection forcefully closed.'); self.emit('close', had_error); }); + + this._state.conn.connect(this._options.port, this._options.host); + this._state.tmrConn = setTimeout(this._fnTmrConn.bind(this), + this._options.connTimeout, loginCb); }; ImapConnection.prototype.isAuthenticated = function() { @@ -602,9 +601,6 @@ ImapConnection.prototype.renameBox = function(oldname, newname, cb) { this._send('RENAME "' + escape(oldname) + '" "' + escape(newname) + '"', cb); }; -ImapConnection.prototype.seq.search = function(options, cb) { - this._search('', options, cb); -}; ImapConnection.prototype.search = function(options, cb) { this._search('UID ', options, cb); }; @@ -617,9 +613,6 @@ ImapConnection.prototype._search = function(which, options, cb) { + buildSearchQuery(options, this.capabilities), cb); }; -ImapConnection.prototype.seq.fetch = function(seqnos, options) { - return this._fetch('', seqnos, options); -}; ImapConnection.prototype.fetch = function(uids, options) { return this._fetch('UID ', uids, options); }; @@ -691,7 +684,7 @@ ImapConnection.prototype._fetch = function(which, uids, options) { extensions = 'X-GM-THRID X-GM-MSGID X-GM-LABELS '; this._send(which + 'FETCH ' + uids.join(',') + ' (' + extensions - + 'FLAGS INTERNALDATE' + + 'UID FLAGS INTERNALDATE' + (opts.request.struct ? ' BODYSTRUCTURE' : '') + (typeof toFetch === 'string' ? ' BODY' + (!opts.markSeen ? '.PEEK' : '') @@ -710,23 +703,14 @@ ImapConnection.prototype._fetch = function(which, uids, options) { return imapFetcher; }; -ImapConnection.prototype.seq.addFlags = function(seqnos, flags, cb) { - this._store('', seqnos, flags, true, cb); -}; ImapConnection.prototype.addFlags = function(uids, flags, cb) { this._store('UID ', uids, flags, true, cb); }; -ImapConnection.prototype.seq.delFlags = function(seqnos, flags, cb) { - this._store('', seqnos, flags, false, cb); -}; ImapConnection.prototype.delFlags = function(uids, flags, cb) { this._store('UID ', uids, flags, false, cb); }; -ImapConnection.prototype.seq.addKeywords = function(seqnos, flags, cb) { - return this._addKeywords('', seqnos, flags, cb); -}; ImapConnection.prototype.addKeywords = function(uids, flags, cb) { return this._addKeywords('UID ', uids, flags, cb); }; @@ -736,16 +720,10 @@ ImapConnection.prototype._addKeywords = function(which, uids, flags, cb) { this._store(which, uids, flags, true, cb); }; -ImapConnection.prototype.seq.delKeywords = function(seqnos, flags, cb) { - this._store('', seqnos, flags, false, cb); -}; ImapConnection.prototype.delKeywords = function(uids, flags, cb) { this._store('UID ', uids, flags, false, cb); }; -ImapConnection.prototype.seq.copy = function(seqnos, boxTo, cb) { - return this._copy('', seqnos, boxTo, cb); -}; ImapConnection.prototype.copy = function(uids, boxTo, cb) { return this._copy('UID ', uids, boxTo, cb); }; @@ -761,9 +739,6 @@ ImapConnection.prototype._copy = function(which, uids, boxTo, cb) { this._send(which + 'COPY ' + uids.join(',') + ' "' + escape(boxTo) + '"', cb); }; -ImapConnection.prototype.seq.move = function(seqnos, boxTo, cb) { - return this._move('', seqnos, boxTo, cb); -}; ImapConnection.prototype.move = function(uids, boxTo, cb) { return this._move('UID ', uids, boxTo, cb); }; @@ -821,6 +796,37 @@ ImapConnection.prototype._move = function(which, uids, boxTo, cb) { } }; +/* Namespace for seqno-based commands */ +ImapConnection.prototype.__defineGetter__('seq', function() { + var self = this; + return { + move: function(seqnos, boxTo, cb) { + return self._move('', seqnos, boxTo, cb); + }, + copy: function(seqnos, boxTo, cb) { + return self._copy('', seqnos, boxTo, cb); + }, + delKeywords: function(seqnos, flags, cb) { + self._store('', seqnos, flags, false, cb); + }, + addKeywords: function(seqnos, flags, cb) { + return self._addKeywords('', seqnos, flags, cb); + }, + delFlags: function(seqnos, flags, cb) { + self._store('', seqnos, flags, false, cb); + }, + addFlags: function(seqnos, flags, cb) { + self._store('', seqnos, flags, true, cb); + }, + fetch: function(seqnos, options) { + return self._fetch('', seqnos, options); + }, + search: function(options, cb) { + self._search('', options, cb); + } + }; +}); + /****** Private Functions ******/