wildduck/imap-core/lib/handler/imap-formal-syntax.js

148 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-03-06 05:45:50 +08:00
/* eslint object-shorthand:0, new-cap: 0, no-useless-concat: 0 */
'use strict';
// IMAP Formal Syntax
// http://tools.ietf.org/html/rfc3501#section-9
function expandRange(start, end) {
let chars = [];
for (let i = start; i <= end; i++) {
chars.push(i);
}
return String.fromCharCode(...chars);
}
function excludeChars(source, exclude) {
let sourceArr = Array.prototype.slice.call(source);
for (let i = sourceArr.length - 1; i >= 0; i--) {
if (exclude.indexOf(sourceArr[i]) >= 0) {
sourceArr.splice(i, 1);
}
}
return sourceArr.join('');
}
module.exports = {
2017-06-03 14:51:58 +08:00
CHAR: function() {
let value = expandRange(0x01, 0x7f);
this.CHAR = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
CHAR8: function() {
let value = expandRange(0x01, 0xff);
this.CHAR8 = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
SP: function() {
2017-03-06 05:45:50 +08:00
return ' ';
},
2017-06-03 14:51:58 +08:00
CTL: function() {
let value = expandRange(0x00, 0x1f) + '\x7F';
this.CTL = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
DQUOTE: function() {
2017-03-06 05:45:50 +08:00
return '"';
},
2017-06-03 14:51:58 +08:00
ALPHA: function() {
let value = expandRange(0x41, 0x5a) + expandRange(0x61, 0x7a);
this.ALPHA = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
DIGIT: function() {
let value = expandRange(0x30, 0x39) + expandRange(0x61, 0x7a);
this.DIGIT = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
'ATOM-CHAR': function() {
2017-03-06 05:45:50 +08:00
let value = excludeChars(this.CHAR(), this['atom-specials']());
2017-06-03 14:51:58 +08:00
this['ATOM-CHAR'] = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
'ASTRING-CHAR': function() {
2017-03-06 05:45:50 +08:00
let value = this['ATOM-CHAR']() + this['resp-specials']();
2017-06-03 14:51:58 +08:00
this['ASTRING-CHAR'] = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
'TEXT-CHAR': function() {
2017-03-06 05:45:50 +08:00
let value = excludeChars(this.CHAR(), '\r\n');
2017-06-03 14:51:58 +08:00
this['TEXT-CHAR'] = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
'atom-specials': function() {
let value = '(' + ')' + '{' + this.SP() + this.CTL() + this['list-wildcards']() + this['quoted-specials']() + this['resp-specials']();
this['atom-specials'] = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
'list-wildcards': function() {
2017-03-06 05:45:50 +08:00
return '%' + '*';
},
2017-06-03 14:51:58 +08:00
'quoted-specials': function() {
2017-03-06 05:45:50 +08:00
let value = this.DQUOTE() + '\\';
2017-06-03 14:51:58 +08:00
this['quoted-specials'] = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
'resp-specials': function() {
2017-03-06 05:45:50 +08:00
return ']';
},
2017-06-03 14:51:58 +08:00
tag: function() {
2017-03-06 05:45:50 +08:00
let value = excludeChars(this['ASTRING-CHAR'](), '+');
2017-06-03 14:51:58 +08:00
this.tag = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
command: function() {
let value = this.ALPHA() + this.DIGIT() + '-';
2017-06-03 14:51:58 +08:00
this.command = function() {
2017-03-06 05:45:50 +08:00
return value;
};
return value;
},
2017-06-03 14:51:58 +08:00
verify: function(str, allowedChars) {
2017-03-06 05:45:50 +08:00
for (let i = 0, len = str.length; i < len; i++) {
if (allowedChars.indexOf(str.charAt(i)) < 0) {
return i;
}
}
return -1;
}
};