diff --git a/README.md b/README.md index d08cfcf..bd26a3f 100644 --- a/README.md +++ b/README.md @@ -596,6 +596,12 @@ ImapConnection Functions * **delKeywords**(<_mixed_>source, <_mixed_>keywords, <_function_>callback) - _(void)_ - Removes keyword(s) from message(s). source can be a message UID, a message UID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message UIDs and/or message UID ranges. keywords is either a single keyword or an array of keywords. The callback has one parameter: the error (falsey if none). +* **setLabels**(<_mixed_>source, <_mixed_>labels, <_function_>callback) - _(void)_ - When X-GM-EXT1 capability present, replaces labels(s) of message(s). source can be a message UID, a message UID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message UIDs and/or message UID ranges. labels is either a single label or an array of labels. The callback has one parameter: the error (falsey if none). + +* **addLabels**(<_mixed_>source, <_mixed_>labels, <_function_>callback) - _(void)_ - When X-GM-EXT1 capability present, adds labels(s) to message(s). source can be a message UID, a message UID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message UIDs and/or message UID ranges. labels is either a single label or an array of labels. The callback has one parameter: the error (falsey if none). + +* **delLabels**(<_mixed_>source, <_mixed_>labels, <_function_>callback) - _(void)_ - When X-GM-EXT1 capability present, removes labels(s) from message(s). source can be a message UID, a message UID range (e.g. '2504:2507' or '\*' or '2504:\*'), or an array of message UIDs and/or message UID ranges. labels is either a single label or an array of labels. The callback has one parameter: the error (falsey if none). + Extensions Supported -------------------- diff --git a/lib/imap.js b/lib/imap.js index 4bf4928..10df32d 100644 --- a/lib/imap.js +++ b/lib/imap.js @@ -962,6 +962,41 @@ ImapConnection.prototype.delKeywords = function(uids, flags, cb) { this._store('UID ', uids, flags, false, cb); }; +ImapConnection.prototype.setLabels = function(uids, labels, cb) { + this._storeLabels('UID ', uids, labels, '', cb); +} + +ImapConnection.prototype.addLabels = function(uids, labels, cb) { + this._storeLabels('UID ', uids, labels, '+', cb); +} + +ImapConnection.prototype.delLabels = function(uids, labels, cb) { + this._storeLabels('UID ', uids, labels, '-', cb); +} + +ImapConnection.prototype._storeLabels = function(which, uids, labels, mode, cb) { + if (!this._serverSupports('X-GM-EXT-1')) + throw new Error('Server must support X-GM-EXT-1 capability'); + if (this._state.status !== STATES.BOXSELECTED) + throw new Error('No mailbox is currently selected'); + if (uids === undefined) + throw new Error('The message ID(s) must be specified'); + + if (!Array.isArray(uids)) + uids = [uids]; + utils.validateUIDList(uids); + + if ((!Array.isArray(labels) && typeof labels !== 'string') + || (Array.isArray(labels) && labels.length === 0)) + throw new Error('labels argument must be a string or a non-empty Array'); + if (!Array.isArray(labels)) + labels = [labels]; + labels = labels.join(' '); + cb = arguments[arguments.length-1]; + + this._send(which + 'STORE ' + uids.join(',') + ' ' + mode + 'X-GM-LABELS.SILENT (' + labels + ')', cb); +} + ImapConnection.prototype.copy = function(uids, boxTo, cb) { return this._copy('UID ', uids, boxTo, cb); }; @@ -1059,6 +1094,15 @@ ImapConnection.prototype.__defineGetter__('seq', function() { addFlags: function(seqnos, flags, cb) { self._store('', seqnos, flags, true, cb); }, + delLabels: function(seqnos, labels, cb) { + self._storeLabels('', seqnos, labels, '-', cb); + }, + addLabels: function(seqnos, labels, cb) { + self._storeLabels('', seqnos, labels, '+', cb); + }, + setLabels: function(seqnos, labels, cb) { + self._storeLabels('', seqnos, labels, '', cb); + }, fetch: function(seqnos, options) { return self._fetch('', seqnos, options); },