From ec01e96e2b3185167eb87e6aec8bd3838a89139a Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 30 Nov 2012 11:33:38 -0500 Subject: [PATCH] Add UTF-7 support where needed for mailbox names --- README.md | 2 ++ lib/imap.js | 20 +++++++++++++++----- package.json | 5 ++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 20d4495..ad83c81 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ node-imap exposes one object: **ImapConnection**. * _Box_ is an object representing the currently open mailbox, and has the following properties: * **name** - < _string_ > - The name of this mailbox. + * **displayName** - < _string_ > - The UTF-7-decoded name of this mailbox. * **readOnly** - < _boolean_ > - True if this mailbox was opened in read-only mode. * **uidvalidity** - < _integer_ > - A 32-bit number that can be used to determine if UIDs in this mailbox have changed since the last time this mailbox was opened. It is possible for this to change during a session, in which case a 'uidvalidity' event will be emitted on the ImapConnection instance. * **uidnext** - < _integer_ > - The uid that will be assigned to the next message that arrives at this mailbox. @@ -416,6 +417,7 @@ ImapConnection Functions { INBOX: // mailbox name { attribs: [] // mailbox attributes. An attribute of 'NOSELECT' indicates the mailbox cannot // be opened + , displayName: 'INBOX' // the UTF-7-decoded version of the mailbox name , delimiter: '/' // hierarchy delimiter for accessing this mailbox's direct children. , children: null // an object containing another structure similar in format to this top level, // otherwise null if no children diff --git a/lib/imap.js b/lib/imap.js index 2c2c371..8f1e1f5 100644 --- a/lib/imap.js +++ b/lib/imap.js @@ -3,6 +3,7 @@ var assert = require('assert'), inherits = require('util').inherits, Socket = require('net').Socket, EventEmitter = require('events').EventEmitter, + utf7 = require('utf7').imap, MIMEParser = require('./mimeparser'), XRegExp = require('./xregexp').XRegExp; @@ -73,7 +74,8 @@ function ImapConnection (options) { keywords: [], permFlags: [], name: null, - messages: { total: 0, new: 0 } + messages: { total: 0, new: 0 }, + _newName: undefined }, ext: { // Capability-specific state info @@ -392,6 +394,7 @@ ImapConnection.prototype.connect = function(loginCb) { return attr.substr(1); }), delimiter: m.delimiter, + displayName: undefined, children: null, parent: null }, @@ -412,6 +415,7 @@ ImapConnection.prototype.connect = function(loginCb) { } box.parent = parent; } + box.displayName = utf7.decode(name); if (!curChildren[name]) curChildren[name] = box; } @@ -569,8 +573,11 @@ ImapConnection.prototype.connect = function(loginCb) { } if (requests[0].cmd === 'RENAME') { - state.box.name = state.box._newName; - delete state.box._newName; + if (state.box._newName) { + state.box.name = state.box._newName; + state.box.displayName = utf7.decode(state.box.name); + state.box._newName = undefined; + } sendBox = true; } @@ -684,6 +691,7 @@ ImapConnection.prototype.openBox = function(name, readOnly, cb) { } this._state.box.name = name; + this._state.box.displayName = utf7.decode(name); this._send((readOnly ? 'EXAMINE' : 'SELECT') + ' "' + utils.escape(name) + '"', cb); @@ -736,7 +744,7 @@ ImapConnection.prototype.addBox = function(name, cb) { if (typeof name !== 'string' || name.length === 0) throw new Error('Mailbox name must be a string describing the full path' + ' of a new mailbox to be created'); - this._send('CREATE "' + utils.escape(name) + '"', cb); + this._send('CREATE "' + utils.escape(utf7.encode(name)) + '"', cb); }; ImapConnection.prototype.delBox = function(name, cb) { @@ -1286,7 +1294,9 @@ ImapConnection.prototype._resetBox = function() { this._state.box.uidvalidity = 0; this._state.box.permFlags = []; this._state.box.keywords = []; - this._state.box.name = null; + this._state.box.name = undefined; + this._State.box.displayName = undefined; + this._State.box._newName = undefined; this._state.box.messages.total = 0; this._state.box.messages.new = 0; }; diff --git a/package.json b/package.json index 93fd628..6951769 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,10 @@ "description": "An IMAP module for node.js that makes communicating with IMAP servers easy", "main": "./lib/imap", "engines": { "node" : ">=0.4.0" }, + "dependencies": { + "utf7": "1.0.0" + }, "keywords": [ "imap", "mail", "email", "reader", "client" ], "licenses": [ { "type": "MIT", "url": "http://github.com/mscdex/node-imap/raw/master/LICENSE" } ], - "repository" : { "type": "git", "url": "http://github.com/mscdex/node-imap.git" } + "repository": { "type": "git", "url": "http://github.com/mscdex/node-imap.git" } } \ No newline at end of file