Connection: refactor _store() and add setFlags() and setKeywords()

fork
mscdex 11 years ago
parent 8ea6b78532
commit f831fd7fa0

@ -574,10 +574,14 @@ Connection Instance Methods
* **delFlags**(< _mixed_ >source, < _mixed_ >flags, < _function_ >callback) - _(void)_ - Removes flag(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. `flags` is either a single flag or an _array_ of flags. `callback` has 1 parameter: < _Error_ >err.
* **setFlags**(< _mixed_ >source, < _mixed_ >flags, < _function_ >callback) - _(void)_ - Sets the flag(s) for 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. `flags` is either a single flag or an _array_ of flags. `callback` has 1 parameter: < _Error_ >err.
* **addKeywords**(< _mixed_ >source, < _mixed_ >keywords, < _function_ >callback) - _(void)_ - Adds keyword(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. `keywords` is either a single keyword or an _array_ of keywords. `callback` has 1 parameter: < _Error_ >err.
* **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. `callback` has 1 parameter: < _Error_ >err.
* **setKeywords**(< _mixed_ >source, < _mixed_ >keywords, < _function_ >callback) - _(void)_ - Sets keyword(s) for 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. `callback` has 1 parameter: < _Error_ >err.
* **serverSupports**(< _string_ >capability) - _boolean_ - Checks if the server supports the specified capability.

@ -432,30 +432,33 @@ Connection.prototype._search = function(which, criteria, cb) {
};
Connection.prototype.addFlags = function(uids, flags, cb) {
this._store('UID ', uids, flags, true, cb);
this._store('UID ', uids, { mode: '+', flags: flags }, cb);
};
Connection.prototype.delFlags = function(uids, flags, cb) {
this._store('UID ', uids, flags, false, cb);
this._store('UID ', uids, { mode: '-', flags: flags }, cb);
};
Connection.prototype.addKeywords = function(uids, flags, cb) {
this._addKeywords('UID ', uids, flags, cb);
Connection.prototype.setFlags = function(uids, flags, cb) {
this._store('UID ', uids, { mode: '', flags: flags }, cb);
};
Connection.prototype._addKeywords = function(which, uids, flags, cb) {
if (this._box && !this._box.newKeywords)
throw new Error('This mailbox does not allow new keywords to be added');
this._store(which, uids, flags, true, cb);
Connection.prototype.addKeywords = function(uids, keywords, cb) {
this._store('UID ', uids, { mode: '+', keywords: keywords }, cb);
};
Connection.prototype.delKeywords = function(uids, flags, cb) {
this._store('UID ', uids, flags, false, cb);
Connection.prototype.delKeywords = function(uids, keywords, cb) {
this._store('UID ', uids, { mode: '-', keywords: keywords }, cb);
};
Connection.prototype._store = function(which, uids, flags, isAdding, cb) {
var isKeywords = (arguments.callee.caller === this._addKeywords
|| arguments.callee.caller === this.delKeywords);
Connection.prototype.setKeywords = function(uids, keywords, cb) {
this._store('UID ', uids, { mode: '', keywords: keywords }, cb);
};
Connection.prototype._store = function(which, uids, cfg, cb) {
var mode = cfg.mode,
isFlags = (cfg.flags !== undefined),
items = (isFlags ? cfg.flags : cfg.keywords);
if (this._box === undefined)
throw new Error('No mailbox is currently selected');
else if (uids === undefined)
@ -465,32 +468,34 @@ Connection.prototype._store = function(which, uids, flags, isAdding, cb) {
uids = [uids];
validateUIDList(uids);
if ((!Array.isArray(flags) && typeof flags !== 'string')
|| (Array.isArray(flags) && flags.length === 0))
throw new Error((isKeywords ? 'Keywords' : 'Flags')
if ((!Array.isArray(items) && typeof items !== 'string')
|| (Array.isArray(items) && items.length === 0))
throw new Error((isFlags ? 'Flags' : 'Keywords')
+ ' argument must be a string or a non-empty Array');
if (!Array.isArray(flags))
flags = [flags];
for (var i = 0, len = flags.length; i < len; ++i) {
if (!isKeywords) {
if (flags[i][0] !== '\\')
flags[i] = '\\' + flags[i];
if (!Array.isArray(items))
items = [items];
for (var i = 0, len = items.length; i < len; ++i) {
if (isFlags) {
if (items[i][0] !== '\\')
items[i] = '\\' + items[i];
} else {
// keyword contains any char except control characters (%x00-1F and %x7F)
// and: '(', ')', '{', ' ', '%', '*', '\', '"', ']'
if (RE_INVALID_KW_CHARS.test(flags[i])) {
throw new Error('The keyword "' + flags[i]
if (RE_INVALID_KW_CHARS.test(items[i])) {
throw new Error('The keyword "' + items[i]
+ '" contains invalid characters');
}
}
}
flags = flags.join(' ');
items = items.join(' ');
uids = uids.join(',');
var modifiers = '';
this._enqueue(which + 'STORE ' + uids + ' ' + (isAdding ? '+' : '-')
+ 'FLAGS.SILENT (' + flags + ')', cb);
this._enqueue(which + 'STORE ' + uids + ' '
+ modifiers
+ mode + 'FLAGS.SILENT (' + items + ')', cb);
};
Connection.prototype.copy = function(uids, boxTo, cb) {
@ -867,24 +872,39 @@ Connection.prototype._thread = function(which, algorithm, criteria, cb) {
Connection.prototype.__defineGetter__('seq', function() {
var self = this;
return {
delKeywords: function(seqnos, keywords, cb) {
self._store('', seqnos, { mode: '-', keywords: keywords }, cb);
},
addKeywords: function(seqnos, keywords, cb) {
self._store('', seqnos, { mode: '+', keywords: keywords }, cb);
},
setKeywords: function(seqnos, keywords, cb) {
self._store('', seqnos, { mode: '', keywords: keywords }, cb);
},
delFlags: function(seqnos, flags, cb) {
self._store('', seqnos, { mode: '-', flags: flags }, cb);
},
addFlags: function(seqnos, flags, cb) {
self._store('', seqnos, { mode: '+', flags: flags }, cb);
},
setFlags: function(seqnos, flags, cb) {
self._store('', seqnos, { mode: '', flags: flags }, cb);
},
move: function(seqnos, boxTo, cb) {
self._move('', seqnos, boxTo, cb);
},
copy: function(seqnos, boxTo, cb) {
self._copy('', seqnos, boxTo, cb);
},
delKeywords: function(seqnos, flags, cb) {
self._store('', seqnos, flags, false, cb);
},
addKeywords: function(seqnos, flags, cb) {
self._addKeywords('', seqnos, flags, cb);
},
delFlags: function(seqnos, flags, cb) {
self._store('', seqnos, flags, false, cb);
fetch: function(seqnos, options, what, cb) {
return self._fetch('', seqnos, options, what, cb);
},
addFlags: function(seqnos, flags, cb) {
self._store('', seqnos, flags, true, cb);
search: function(options, cb) {
self._search('', options, cb);
},
delLabels: function(seqnos, labels, cb) {
self._storeLabels('', seqnos, labels, '-', cb);
},

Loading…
Cancel
Save