drop sieve parser from sievescripts branch

This commit is contained in:
djmaze 2021-01-20 11:43:03 +01:00
parent 2024088ea0
commit ca91a6e408
18 changed files with 1 additions and 2138 deletions

View file

@ -1,227 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5228#section-2.9
*/
(Sieve => {
const Grammar = Sieve.Grammar, Command = Grammar.Command;
/**
* https://tools.ietf.org/html/rfc5228#section-3.1
* Usage:
* if <test1: test> <block1: block>
* elsif <test2: test> <block2: block>
* else <block3: block>
*/
class Conditional extends Command
{
constructor(identifier = 'if')
{
super(identifier);
this.test = null;
}
toString()
{
return this.identifier + ' ' + this.test + ' ' + this.commands;
}
/*
public function pushArguments(array $args): void
{
print_r($args);
exit;
}
*/
}
class If extends Conditional
{
constructor()
{
super('if');
}
}
class ElsIf extends Conditional
{
constructor()
{
super('elsif');
}
}
class Else extends Conditional
{
constructor()
{
super('else');
}
toString()
{
return this.identifier + ' ' + this.commands;
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-3.2
*/
class Require extends Command
{
constructor()
{
super('require');
this.capabilities = new Grammar.StringList();
}
toString()
{
return 'require ' + this.capabilities.toString() + ';';
}
pushArguments(args)
{
if (args[0] instanceof Grammar.StringList) {
this.capabilities = args[0];
} else if (args[0] instanceof Grammar.QuotedString) {
this.capabilities.push(args[0]);
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-3.3
*/
class Stop extends Command
{
constructor()
{
super('stop');
}
toString()
{
return 'stop;';
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.1
*/
class FileInto extends Command
{
constructor()
{
super('fileinto');
// QuotedString / MultiLine
this._mailbox = new Grammar.QuotedString();
}
get require() { return 'fileinto'; }
toString()
{
return 'fileinto ' + this._mailbox + ';';
}
get mailbox()
{
return this._mailbox.value;
}
set mailbox(value)
{
this._mailbox.value = value;
}
pushArguments(args)
{
if (args[0] instanceof Grammar.StringType) {
this._mailbox = args[0];
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.2
*/
class Redirect extends Command
{
constructor()
{
super('redirect');
// QuotedString / MultiLine
this._address = new Grammar.QuotedString();
}
toString()
{
return 'redirect ' + this._address + ';';
}
get address()
{
return this._address.value;
}
set address(value)
{
this._address.value = value;
}
pushArguments(args)
{
if (args[0] instanceof Grammar.StringType) {
this._address = args[0];
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.3
*/
class Keep extends Command
{
constructor()
{
super('keep');
}
toString()
{
return 'keep;';
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.4
*/
class Discard extends Command
{
constructor()
{
super('discard');
}
toString()
{
return 'discard;';
}
}
Sieve.Commands = {
// Control commands
if: If,
elsif: ElsIf,
else: Else,
conditional: Conditional,
require: Require,
stop: Stop,
// Action commands
discard: Discard,
fileinto: FileInto,
keep: Keep,
redirect: Redirect
};
})(this.Sieve);

View file

@ -1,48 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5173
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class Body extends Grammar.Test
{
constructor()
{
super('body');
this.body_transform = ''; // :raw, :content <string-list>, :text
this.key_list = new Grammar.StringList;
}
get require() { return 'body'; }
toString()
{
return 'body'
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.body_transform
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
args.forEach((arg, i) => {
if (':raw' === arg || ':text' === arg) {
this.body_transform = arg;
} else if (arg instanceof Grammar.StringList || arg instanceof Grammar.StringType) {
if (':content' === args[i-1]) {
this.body_transform = ':content ' + arg;
} else {
this[args[i+1] ? 'content_list' : 'key_list'] = arg;
}
}
});
}
}
Sieve.Commands.body = Body;
})(this.Sieve);

View file

@ -1,38 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5183
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class Environment extends Grammar.Test
{
constructor()
{
super('environment');
this.name = new Grammar.QuotedString;
this.key_list = new Grammar.StringList;
}
get require() { return 'environment'; }
toString()
{
return 'body'
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.name
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
this.name = args[args.length-2];
this.key_list = args[args.length-1];
}
}
Sieve.Commands.environment = Environment;
})(this.Sieve);

View file

@ -1,73 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5229
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class Set extends Grammar.Command
{
constructor()
{
super('set');
this.modifiers = [];
this._name = new Grammar.QuotedString;
this._value = new Grammar.QuotedString;
}
get require() { return 'variables'; }
toString()
{
return 'set'
+ ' ' + this.modifiers.join(' ')
+ ' ' + this._name
+ ' ' + this._value;
}
get name() { return this._name.value; }
set name(str) { this._name.value = str; }
get value() { return this._value.value; }
set value(str) { this._value.value = str; }
pushArguments(args)
{
[':lower', ':upper', ':lowerfirst', ':upperfirst', ':quotewildcard', ':length'].forEach(modifier => {
args.includes(modifier) && this.modifiers.push(modifier);
});
this._name = args[args.length-2];
this._value = args[args.length-1];
}
}
class String extends Grammar.Test
{
constructor()
{
super('string');
this.source = new Grammar.StringList;
this.key_list = new Grammar.StringList;
}
toString()
{
return 'string'
+ ' ' + this.match_type
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.source.toString()
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
this.source = args[args.length-2];
this.key_list = args[args.length-1];
}
}
Sieve.Commands.set = Set;
Sieve.Commands.string = String;
})(this.Sieve);

View file

@ -1,101 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5230
* https://tools.ietf.org/html/rfc6131
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class Vacation extends Grammar.Command
{
constructor()
{
super('vacation');
this._days = new Grammar.Number;
// this._seconds = new Grammar.Number;
this._subject = new Grammar.QuotedString;
this._from = new Grammar.QuotedString;
this.addresses = new Grammar.StringList;
this.mime = false;
this._handle = new Grammar.QuotedString;
this._reason = new Grammar.QuotedString; // QuotedString / MultiLine
}
// get require() { return ['vacation','vacation-seconds']; }
get require() { return 'vacation'; }
toString()
{
let result = 'vacation';
if (0 < this._days.value) {
result += ' :days ' + this._days;
// } else if (0 < this._seconds.value) {
// result += ' :seconds ' + this._seconds;
}
if (this._subject.length) {
result += ' :subject ' + this._subject;
}
if (this._from.length) {
result += ' :from ' + this.arguments[':from'];
}
if (this.addresses.length) {
result += ' :addresses ' + this.addresses.toString();
}
if (this.mime) {
result += ' :mime';
}
if (this._handle.length) {
result += ' :handle ' + this._handle;
}
return result + ' ' + this._reason;
}
get days() { return this._days.value; }
// get seconds() { return this._seconds.value; }
get subject() { return this._subject.value; }
get from() { return this._from.value; }
get handle() { return this._handle.value; }
get reason() { return this._reason.value; }
set days(int) { this._days.value = int; }
// set seconds(int) { this._seconds.value = int; }
set subject(str) { this._subject.value = str; }
set from(str) { this._from.value = str; }
set handle(str) { this._handle.value = str; }
set reason(str) { this._reason.value = str; }
pushArguments(args)
{
args.forEach((arg, i) => {
if (':mime' === arg) {
this.mime = true;
} else if (i === args.length-1) {
this._reason.value = arg.value; // Grammar.QuotedString
} else switch (args[i-1]) {
case ':days':
this._days.value = arg.value; // Grammar.Number
break;
// case ':seconds':
// this._seconds.value = arg.value; // Grammar.Number
// break;
case ':subject':
this._subject.value = arg.value; // Grammar.QuotedString
break;
case ':from':
this._from.value = arg.value; // Grammar.QuotedString
break;
case ':addresses':
this.addresses = arg; // Grammar.StringList
break;
case ':handle':
this._from.value = arg.value; // Grammar.QuotedString
break;
}
});
}
}
Sieve.Commands.vacation = Vacation;
})(this.Sieve);

View file

@ -1,112 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5232
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class Flag extends Grammar.Command
{
constructor(identifier)
{
super(identifier);
this._variablename = new Grammar.QuotedString;
this.list_of_flags = new Grammar.StringList;
}
get require() { return 'imap4flags'; }
toString()
{
return this.identifier + ' ' + this._variablename + ' ' + this.list_of_flags.toString() + ';';
}
get variablename()
{
return this._variablename.value;
}
set variablename(value)
{
this._variablename.value = value;
}
pushArguments(args)
{
if (args[1]) {
if (args[0] instanceof Grammar.QuotedString) {
this._variablename = args[0];
}
if (args[1] instanceof Grammar.StringType) {
this.list_of_flags = args[1];
}
} else if (args[0] instanceof Grammar.StringType) {
this.list_of_flags = args[0];
}
}
}
class SetFlag extends Flag
{
constructor()
{
super('setflag');
}
}
class AddFlag extends Flag
{
constructor()
{
super('addflag');
}
}
class RemoveFlag extends Flag
{
constructor()
{
super('removeflag');
}
}
class HasFlag extends Grammar.Test
{
constructor()
{
super('hasflag');
this.variable_list = new Grammar.StringList;
this.list_of_flags = new Grammar.StringList;
}
get require() { return 'imap4flags'; }
toString()
{
return 'hasflag'
+ ' ' + this.match_type
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.variable_list.toString()
+ ' ' + this.list_of_flags.toString();
}
pushArguments(args)
{
args.forEach((arg, i) => {
if (arg instanceof Grammar.StringList || arg instanceof Grammar.StringType) {
this[args[i+1] ? 'variable_list' : 'list_of_flags'] = arg;
}
});
}
}
Object.assign(Sieve.Commands, {
setflag: SetFlag,
addflag: AddFlag,
removeflag: RemoveFlag,
hasflag: HasFlag
// mark and unmark never made it into the RFC
});
})(this.Sieve);

View file

@ -1,76 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5235
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class SpamTest extends Grammar.Test
{
constructor()
{
super('spamtest');
this.percent = false, // 0 - 100 else 0 - 10
this.value = new Grammar.QuotedString;
}
// get require() { return this.percent ? 'spamtestplus' : 'spamtest'; }
get require() { return /:value|:count/.test(this.match_type) ? ['spamtestplus','relational'] : 'spamtestplus'; }
toString()
{
return 'spamtest'
+ (this.percent ? ' :percent' : '')
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.value;
}
pushArguments(args)
{
args.forEach(arg => {
if (':percent' === arg) {
this.percent = true;
} else if (arg instanceof Grammar.StringType) {
this.value = arg;
}
});
}
}
class VirusTest extends Grammar.Test
{
constructor()
{
super('virustest');
this.value = new Grammar.QuotedString; // 1 - 5
}
get require() { return /:value|:count/.test(this.match_type) ? ['virustest','relational'] : 'virustest'; }
toString()
{
return 'virustest'
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.value;
}
pushArguments(args)
{
args.forEach(arg => {
if (arg instanceof Grammar.StringType) {
this.value = arg;
}
});
}
}
Object.assign(Sieve.Commands, {
spamtest: SpamTest,
virustest: VirusTest
});
})(this.Sieve);

View file

@ -1,103 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5260
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class DateTest extends Grammar.Test
{
constructor()
{
super('date');
this.zone = new Grammar.QuotedString;
this.originalzone = false;
this.header_name = new Grammar.QuotedString;
this.date_part = new Grammar.QuotedString;
this.key_list = new Grammar.StringList;
// rfc5260#section-6
this.index = new Grammar.Number;
this.last = false;
}
// get require() { return ['date','index']; }
get require() { return 'date'; }
toString()
{
return 'date'
+ (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.originalzone ? ' :originalzone' : (this.zone.length ? ' :zone ' + this.zone : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.header_name
+ ' ' + this.date_part
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
let l = args.length - 1;
args.forEach((arg, i) => {
if (':originalzone' === arg) {
this.originalzone = true;
} else if (':last' === arg) {
this.last = true;
} else if (':zone' === args[i-1]) {
this.zone.value = arg.value;
} else if (':index' === args[i-1]) {
this.index.value = arg.value;
} else if (l-2 === i) {
this.header_name = arg;
} else if (l-1 === i) {
this.date_part = arg;
} else if (l === i) {
this.key_list = arg;
}
});
}
}
class CurrentDate extends Grammar.Test
{
constructor()
{
super('date');
this.zone = new Grammar.QuotedString;
this.date_part = new Grammar.QuotedString;
this.key_list = new Grammar.StringList;
}
get require() { return 'date'; }
toString()
{
return 'date'
+ (this.zone.length ? ' :zone ' + this.zone : '')
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.date_part
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
let l = args.length - 1;
args.forEach((arg, i) => {
if (':zone' === args[i-1]) {
this.zone.value = arg.value;
} else if (l-1 === i) {
this.date_part = arg;
} else if (l === i) {
this.key_list = arg;
}
});
}
}
Sieve.Commands.date = DateTest;
Sieve.Commands.currentdate = CurrentDate;
})(this.Sieve);

View file

@ -1,88 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5293
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class AddHeader extends Grammar.Command
{
constructor()
{
super('addheader');
this.last = false;
this.field_name = new Grammar.QuotedString;
this.value = new Grammar.QuotedString;
}
get require() { return 'editheader'; }
toString()
{
return this.identifier
+ (this.last ? ' :last' : '')
+ ' ' + this.field_name
+ ' ' + this.value + ';';
}
pushArguments(args)
{
this.last = args.includes(':last');
this.field_name = args[args.length - 2];
this.value = args[args.length - 1];
}
}
class DeleteHeader extends Grammar.Command
{
constructor()
{
super('deleteheader');
this.index = new Grammar.Number;
this.last = false;
this.comparator = '',
this.match_type = ':is',
this.field_name = new Grammar.QuotedString;
this.value_patterns = new Grammar.StringList;
}
get require() { return 'editheader'; }
toString()
{
return this.identifier
+ (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.field_name
+ ' ' + this.value_patterns + ';';
}
pushArguments(args)
{
let l = args.length - 1;
args.forEach((arg, i) => {
if (':last' === arg) {
this.last = true;
} else if (':index' === args[i-1]) {
this.index.value = arg.value;
args[i] = null;
}
});
if (args[l-1] instanceof Grammar.StringType) {
this.field_name = args[l-1];
this.value_patterns = args[l];
} else {
this.field_name = args[l];
}
}
}
Object.assign(Sieve.Commands, {
addheader: AddHeader,
deleteheader: DeleteHeader
});
})(this.Sieve);

View file

@ -1,84 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5429
*/
(Sieve => {
const Grammar = Sieve.Grammar;
/**
* https://tools.ietf.org/html/rfc5429#section-2.1
*/
class Ereject extends Grammar.Command
{
constructor()
{
super('ereject');
this._reason = new Grammar.QuotedString;
}
get require() { return 'ereject'; }
toString()
{
return 'ereject ' + this._reason + ';';
}
get reason()
{
return this._reason.value;
}
set reason(value)
{
this._reason.value = value;
}
pushArguments(args)
{
if (args[0] instanceof Grammar.StringType) {
this._reason = args[0];
}
}
}
/**
* https://tools.ietf.org/html/rfc5429#section-2.2
*/
class Reject extends Grammar.Command
{
constructor()
{
super('reject');
this._reason = new Grammar.QuotedString;
}
get require() { return 'reject'; }
toString()
{
return 'reject ' + this._reason + ';';
}
get reason()
{
return this._reason.value;
}
set reason(value)
{
this._reason.value = value;
}
pushArguments(args)
{
if (args[0] instanceof Grammar.StringType) {
this._reason = args[0];
}
}
}
Sieve.Commands.ereject = Ereject;
Sieve.Commands.reject = Reject;
})(this.Sieve);

View file

@ -1,63 +0,0 @@
/**
* https://tools.ietf.org/html/rfc6609
*/
(Sieve => {
const Grammar = Sieve.Grammar;
class Include extends Grammar.Command
{
constructor()
{
super('include');
this.global = false; // ':personal' / ':global';
this.once = false;
this.optional = false;
this.value = new Grammar.QuotedString;
}
get require() { return 'include'; }
toString()
{
return this.identifier
+ (this.global ? ' :global' : '')
+ (this.once ? ' :once' : '')
+ (this.optional ? ' :optional' : '')
+ ' ' + this.value + ';';
}
pushArguments(args)
{
args.forEach(arg => {
if (':global' === arg || ':once' === arg || ':optional' === arg) {
this[arg.substr(1)] = true;
} else if (arg instanceof Grammar.QuotedString) {
this.value = arg;
}
});
}
}
class Return extends Grammar.Command
{
constructor()
{
super('return');
}
get require() { return 'include'; }
toString()
{
return 'return;';
}
}
Object.assign(Sieve.Commands, {
include: Include,
return: Return
});
})(this.Sieve);

View file

@ -1,293 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5228#section-8.2
*/
(Sieve => {
const RegEx = Sieve.RegEx;
/**
* abstract
*/
class StringType /*extends String*/
{
constructor(value = '')
{
this._value = value;
}
toString()
{
return this._value;
}
get value()
{
return this._value;
}
set value(value)
{
this._value = value;
}
get length()
{
return this._value.length;
}
}
/**
* abstract
*/
class Comment extends StringType
{
}
/**
* https://tools.ietf.org/html/rfc5228#section-2.9
*/
class Command
{
constructor(identifier)
{
this.identifier = identifier;
this.arguments = [];
this.commands = new Commands;
}
toString()
{
let result = this.identifier;
if (this.arguments.length) {
result += Sieve.arrayToString(this.arguments, ' ');
}
if (this.commands.length) {
result += ' ' + this.commands.toString();
} else {
result += ';';
}
return result;
}
getComparators()
{
return ['i;ascii-casemap'];
}
getMatchTypes()
{
return [':is', ':contains', ':matches'];
}
pushArguments(args)
{
this.arguments = args;
}
}
class Commands extends Array
{
toString()
{
return this.length
? '{\r\n\t' + Sieve.arrayToString(this, '\r\n\t') + '\r\n}'
: '{}';
}
push(value)
{
if (value instanceof Command || value instanceof Comment) {
super.push(value);
}
}
}
class BracketComment extends Comment
{
toString()
{
return '/* ' + super.toString() + ' */';
}
}
class HashComment extends Comment
{
toString()
{
return '# ' + super.toString();
}
}
class NumberType /*extends Number*/
{
constructor(value = '0')
{
this._value = value;
}
toString()
{
return this._value;
}
get value()
{
return this._value;
}
set value(value)
{
this._value = value;
}
}
class StringList extends Array
{
toString()
{
if (1 < this.length) {
return '[' + this.join(',') + ']';
}
return this.length ? this[0] : '';
}
push(value)
{
if (!(value instanceof StringType)) {
value = new QuotedString(value);
}
super.push(value);
}
}
StringList.fromString = list => {
let string,
obj = new StringList,
regex = RegExp('(?:^\\s*|\\s*,\\s*)(?:"(' + RegEx.QUOTED_TEXT + ')"|text:[ \\t]*('
+ RegEx.HASH_COMMENT + ')?\\r\\n'
+ '((?:' + RegEx.MULTILINE_LITERAL + '|' + RegEx.MULTILINE_DOTSTART + ')*)'
+ '\\.\\r\\n)', 'gm');
list = list.replace(/^[\r\n\t[]+/, '');
while ((string = regex.exec(list))) {
if (string[3]) {
obj.push(new MultiLine(string[3], string[2]));
} else {
obj.push(new QuotedString(string[1]));
}
}
return obj;
}
class QuotedString extends StringType
{
toString()
{
return '"' + this._value.replace(/[\\"]/g, '\\$&') + '"';
// return '"' + super.toString().replace(/[\\"]/g, '\\$&') + '"';
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-8.1
*/
class MultiLine extends StringType
{
constructor(value, comment = '')
{
super();
this.value = value;
this.comment = comment;
}
toString()
{
return 'text:'
+ (this.comment ? '# ' + this.comment : '') + "\r\n"
+ this.value
+ "\r\n.\r\n";
}
}
let MultiLineRegEx = RegExp('text:[ \\t]*(' + RegEx.HASH_COMMENT + ')?\\r\\n'
+ '((?:' + RegEx.MULTILINE_LITERAL + '|' + RegEx.MULTILINE_DOTSTART + ')*)'
+ '\\.\\r\\n', 'm');
MultiLine.fromString = string => {
string = string.match(MultiLineRegEx);
if (string[2]) {
return new MultiLine(string[2].replace(/\r\n$/, ''), string[1]);
}
return new MultiLine();
}
/**
* https://tools.ietf.org/html/rfc5228#section-5
*/
class Test
{
constructor(identifier)
{
this.identifier = identifier;
// Almost every test has a comparator and match_type, so define them here
this.comparator = '',
this.match_type = ':is',
this.arguments = [];
}
toString()
{
return (this.identifier
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ (this.match_type ? ' ' + this.match_type : '')
+ ' ' + Sieve.arrayToString(this.arguments, ' ')).trim();
}
pushArguments(args)
{
this.arguments = args;
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.2
* https://tools.ietf.org/html/rfc5228#section-5.3
*/
class TestList extends Array
{
constructor()
{
super();
}
toString()
{
if (1 < this.length) {
// return '(\r\n\t' + Sieve.arrayToString(this, ',\r\n\t') + '\r\n)';
return '(' + this.join(', ') + ')';
}
return this.length ? this[0] : '';
}
push(value)
{
if (!(value instanceof Test)) {
throw 'Not an instanceof Test';
}
super.push(value);
}
}
Sieve.Grammar = {
Command: Command,
BracketComment: BracketComment,
HashComment: HashComment,
MultiLine: MultiLine,
Number: NumberType,
QuotedString: QuotedString,
StringList: StringList,
StringType: StringType,
Test: Test,
TestList: TestList
};
})(this.Sieve);

View file

@ -1,264 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5228#section-8
*/
(Sieve => {
const
RegEx = Sieve.RegEx,
Grammar = Sieve.Grammar,
Commands = Sieve.Commands,
T_UNKNOWN = 0,
T_STRING_LIST = 1,
T_QUOTED_STRING = 2,
T_MULTILINE_STRING = 3,
T_HASH_COMMENT = 4,
T_BRACKET_COMMENT = 5,
T_BLOCK_START = 6,
T_BLOCK_END = 7,
T_LEFT_PARENTHESIS = 8,
T_RIGHT_PARENTHESIS = 9,
T_COMMA = 10,
T_SEMICOLON = 11,
T_TAG = 12,
T_IDENTIFIER = 13,
T_NUMBER = 14,
T_WHITESPACE = 15,
TokensRegEx = '(' + [
/* T_STRING_LIST */ RegEx.STRING_LIST,
/* T_QUOTED_STRING */ RegEx.QUOTED_STRING,
/* T_MULTILINE_STRING */ RegEx.MULTI_LINE,
/* T_HASH_COMMENT */ RegEx.HASH_COMMENT,
/* T_BRACKET_COMMENT */ RegEx.BRACKET_COMMENT,
/* T_BLOCK_START */ '\\{',
/* T_BLOCK_END */ '\\}',
/* T_LEFT_PARENTHESIS */ '\\(', // anyof / allof
/* T_RIGHT_PARENTHESIS */ '\\)', // anyof / allof
/* T_COMMA */ ',',
/* T_SEMICOLON */ ';',
/* T_TAG */ RegEx.TAG,
/* T_IDENTIFIER */ RegEx.IDENTIFIER,
/* T_NUMBER */ RegEx.NUMBER,
/* T_WHITESPACE */ '(?: |\\r\\n|\\t)+',
/* T_UNKNOWN */ '[^ \\r\\n\\t]+'
].join(')|(') + ')';
Sieve.parseScript = (script, name = 'script.sieve') => {
let match,
line = 1,
tree = [],
// Create one regex to find the tokens
// Use exec() to forward since lastIndex
regex = RegExp(TokensRegEx, 'gm'),
levels = [],
command = null,
requires = [],
args = [];
const
error = message => {
// throw new SyntaxError(message + ' at ' + regex.lastIndex + ' line ' + line, name, line)
throw new SyntaxError(message + ' on line ' + line
+ ' around: ' + script.substr(regex.lastIndex - 10, 20).replace(/\r\n/gm, '\\r\\n'), name, line)
},
pushArg = arg => {
command || error('Argument not part of command');
let prev_arg = args[args.length-1];
if (':is' === arg || ':contains' === arg || ':matches' === arg) {
command.match_type = arg;
} else if (':value' === prev_arg || ':count' === prev_arg) {
// Sieve relational [RFC5231] match types
/^(gt|ge|lt|le|eq|ne)$/.test(arg.value) || error('Invalid relational match-type ' + arg);
command.match_type = prev_arg + ' ' + arg;
--args.length;
// requires.push('relational');
} else if (':comparator' === prev_arg) {
command.comparator = arg;
--args.length;
} else {
args.push(arg);
}
},
pushArgs = () => {
if (args.length) {
command && command.pushArguments(args);
args = [];
}
};
levels.last = () => levels[levels.length - 1];
while ((match = regex.exec(script))) {
// the last element in match will contain the matched value and the key will be the type
let type = match.findIndex((v,i) => 0 < i && undefined !== v),
value = match[type];
// create the part
switch (type)
{
case T_IDENTIFIER: {
pushArgs();
value = value.toLowerCase();
let new_command;
if ('if' === value) {
new_command = new Commands.conditional(value);
} else if ('elsif' === value || 'else' === value) {
// (prev_command instanceof Commands.conditional) || error('Not after IF condition');
new_command = new Commands.conditional(value);
} else if (Commands[value]) {
if ('allof' === value || 'anyof' === value) {
// (command instanceof Commands.conditional || command instanceof Commands.not) || error('Test-list not in conditional');
}
new_command = new Commands[value]();
} else {
console.error('Unknown command: ' + value);
if (command && (
command instanceof Commands.conditional
|| command instanceof Commands.not
|| command.tests instanceof Grammar.TestList)) {
new_command = new Grammar.Test(value);
} else {
new_command = new Grammar.Command(value);
}
}
if (new_command instanceof Grammar.Test) {
if (command instanceof Commands.conditional || command instanceof Commands.not) {
// if/elsif/else new_command
// not new_command
command.test = new_command;
} else if (command.tests instanceof Grammar.TestList) {
// allof/anyof .tests[] new_command
command.tests.push(new_command);
} else {
error('Test "' + value + '" not allowed in "' + command.identifier + '" command');
}
} else if (command) {
if (command.commands) {
command.commands.push(new_command);
} else {
error('commands not allowed in "' + command.identifier + '" command');
}
} else {
tree.push(new_command);
}
levels.push(new_command);
command = new_command;
if (command.require) {
(Array.isArray(command.require) ? command.require : [command.require])
.forEach(string => requires.push(string));
}
if (command.comparator) {
requires.push('comparator-' + this.comparator);
}
break; }
// Arguments
case T_TAG:
pushArg(value.toLowerCase());
break;
case T_STRING_LIST:
pushArg(Grammar.StringList.fromString(value));
break;
case T_MULTILINE_STRING:
pushArg(Grammar.MultiLine.fromString(value));
break;
case T_QUOTED_STRING:
pushArg(new Grammar.QuotedString(value.substr(1,value.length-2)));
break;
case T_NUMBER:
pushArg(new Grammar.Number(value));
break;
// Comments
case T_BRACKET_COMMENT:
case T_HASH_COMMENT: {
let obj = (T_HASH_COMMENT == type)
? new Grammar.HashComment(value.substr(1).trim())
: new Grammar.BracketComment(value.substr(2, value.length-4));
if (command) {
if (!command.comments) {
command.comments = [];
}
(command.commands || command.comments).push(obj);
} else {
tree.push(obj);
}
break; }
case T_WHITESPACE:
// (command ? command.commands : tree).push(value.trim());
command || tree.push(value.trim());
break;
// Command end
case T_SEMICOLON:
command || error('Semicolon not at end of command');
pushArgs();
if (command instanceof Commands.require) {
command.capabilities.forEach(string => requires.push(string.value));
}
levels.pop();
command = levels.last();
break;
// Command block
case T_BLOCK_START:
pushArgs();
// https://tools.ietf.org/html/rfc5228#section-2.9
// Action commands do not take tests or blocks
while (command && !(command instanceof Commands.conditional)) {
levels.pop();
command = levels.last();
}
command || error('Block start not part of control command');
break;
case T_BLOCK_END:
(command instanceof Commands.conditional) || error('Block end has no matching block start');
levels.pop();
// prev_command = command;
command = levels.last();
break;
// anyof / allof ( ... , ... )
case T_LEFT_PARENTHESIS:
pushArgs();
while (command && !(command.tests instanceof Grammar.TestList)) {
levels.pop();
command = levels.last();
}
command || error('Test start not part of anyof/allof test');
break;
case T_RIGHT_PARENTHESIS:
pushArgs();
levels.pop();
command = levels.last();
(command.tests instanceof Grammar.TestList) || error('Test end not part of test-list');
break;
case T_COMMA:
pushArgs();
// Must be inside PARENTHESIS aka test-list
while (command && !(command.tests instanceof Grammar.TestList)) {
levels.pop();
command = levels.last();
}
command || error('Comma not part of test-list');
break;
case T_UNKNOWN:
error('Invalid token ' + value);
}
// Set current script position
line += (value.split('\n').length - 1); // (value.match(/\n/g) || []).length;
}
tree.requires = requires;
return tree;
};
})(this.Sieve);

View file

@ -1,229 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5228#section-8
*/
(Sieve => {
const
/**************************************************
* https://tools.ietf.org/html/rfc5228#section-8.1
**************************************************/
/**
* octet-not-crlf = %x01-09 / %x0B-0C / %x0E-FF
* a single octet other than NUL, CR, or LF
*/
OCTET_NOT_CRLF = '[^\\x00\\r\\n]',
/**
* octet-not-period = %x01-09 / %x0B-0C / %x0E-2D / %x2F-FF
* a single octet other than NUL, CR, LF, or period
*/
OCTET_NOT_PERIOD = '[^\\x00\\r\\n\\.]',
/**
* octet-not-qspecial = %x01-09 / %x0B-0C / %x0E-21 / %x23-5B / %x5D-FF
* a single octet other than NUL, CR, LF, double-quote, or backslash
*/
OCTET_NOT_QSPECIAL = '[^\\x00\\r\\n"\\\\]',
/**
* hash-comment = "#" *octet-not-crlf CRLF
*/
HASH_COMMENT = '#' + OCTET_NOT_CRLF + '*\\r\\n',
/**
* QUANTIFIER = "K" / "M" / "G"
*/
QUANTIFIER = '[KMGkmg]',
/**
* quoted-safe = CRLF / octet-not-qspecial
* either a CRLF pair, OR a single octet other than NUL, CR, LF, double-quote, or backslash
*/
QUOTED_SAFE = '\\r\\n|' + OCTET_NOT_QSPECIAL,
/**
* quoted-special = "\" (DQUOTE / "\")
* represents just a double-quote or backslash
*/
QUOTED_SPECIAL = '\\\\\\\\|\\\\"',
/**
* quoted-text = *(quoted-safe / quoted-special / quoted-other)
*/
QUOTED_TEXT = '(?:' + QUOTED_SAFE + '|' + QUOTED_SPECIAL + ')*',
/**
* multiline-literal = [ octet-not-period *octet-not-crlf ] CRLF
*/
MULTILINE_LITERAL = OCTET_NOT_PERIOD + OCTET_NOT_CRLF + '*\\r\\n',
/**
* multiline-dotstart = "." 1*octet-not-crlf CRLF
; A line containing only "." ends the multi-line.
; Remove a leading '.' if followed by another '.'.
*/
MULTILINE_DOTSTART = '\\.' + OCTET_NOT_CRLF + '+\\r\\n',
/**
* not-star = CRLF / %x01-09 / %x0B-0C / %x0E-29 / %x2B-FF
* either a CRLF pair, OR a single octet other than NUL, CR, LF, or star
*/
// NOT_STAR: '\\r\\n|[^\\x00\\r\\n*]',
/**
* not-star-slash = CRLF / %x01-09 / %x0B-0C / %x0E-29 / %x2B-2E / %x30-FF
* either a CRLF pair, OR a single octet other than NUL, CR, LF, star, or slash
*/
// NOT_STAR_SLASH: '\\r\\n|[^\\x00\\r\\n*\\\\]',
/**
* STAR = "*"
*/
// STAR = '\\*',
RegEx = {
/**
* bracket-comment = "/*" *not-star 1*STAR *(not-star-slash *not-star 1*STAR) "/"
*/
BRACKET_COMMENT: '/\\*[\\s\\S]*?\\*/',
/**
* hash-comment = "#" *octet-not-crlf CRLF
*/
HASH_COMMENT: HASH_COMMENT,
/**
* identifier = (ALPHA / "_") *(ALPHA / DIGIT / "_")
*/
IDENTIFIER: '[a-zA-Z_][a-zA-Z0-9_]*',
/**
* multi-line = "text:" *(SP / HTAB) (hash-comment / CRLF)
*(multiline-literal / multiline-dotstart)
"." CRLF
*/
MULTI_LINE: 'text:[ \\t]*(?:' + HASH_COMMENT + ')?\\r\\n'
+ '(?:' + MULTILINE_LITERAL + '|' + MULTILINE_DOTSTART + ')*'
+ '\\.\\r\\n',
MULTILINE_LITERAL: MULTILINE_LITERAL,
MULTILINE_DOTSTART: MULTILINE_DOTSTART,
/**
* number = 1*DIGIT [ QUANTIFIER ]
*/
NUMBER: '[0-9]+' + QUANTIFIER + '?',
/**
* quoted-string = DQUOTE quoted-text DQUOTE
*/
QUOTED_STRING: '"' + QUOTED_TEXT + '"',
QUOTED_TEXT: QUOTED_TEXT,
/**
* tag = ":" identifier
*/
TAG: ':[a-zA-Z_][a-zA-Z0-9_]*',
/**************************************************
* https://tools.ietf.org/html/rfc5228#section-8.3
**************************************************/
/**
* ADDRESS-PART = ":localpart" / ":domain" / ":all"
*/
// ADDRESS_PART: ':localpart|:domain|:all',
/**
* MATCH-TYPE = ":is" / ":contains" / ":matches"
*/
// MATCH_TYPE: ':is|:contains|:matches'
};
/**
* comment = bracket-comment / hash-comment
*/
//RegEx.COMMENT = RegEx.BRACKET_COMMENT + '|' + HASH_COMMENT;
/**************************************************
* https://tools.ietf.org/html/rfc5228#section-8.2
**************************************************/
/**
* string = quoted-string / multi-line
*/
RegEx.STRING = RegEx.QUOTED_STRING + '|' + RegEx.MULTI_LINE;
/**
* string-list = "[" string *("," string) "]" / string
* if there is only a single string, the brackets are optional
*/
RegEx.STRING_LIST = '\\[\\s*(?:' + RegEx.STRING + ')(?:\\s*,\\s*(?:' + RegEx.STRING + '))*\\s*\\]';
// + '|(?:' + RegEx.STRING + ')';
/**
* argument = string-list / number / tag
*/
RegEx.ARGUMENT = RegEx.STRING_LIST + '|' + RegEx.STRING + '|' + RegEx.NUMBER + '|' + RegEx.TAG;
/**
* arguments = *argument [ test / test-list ]
* This is not possible with regular expressions
*/
// ARGUMENTS = '(?:\\s+' . self::ARGUMENT . ')*(\\s+?:' . self::TEST . '|' . self::TEST_LIST . ')?';
/**
* block = "{" commands "}"
* This is not possible with regular expressions
*/
// BLOCK = '{' . self::COMMANDS . '}';
/**
* command = identifier arguments (";" / block)
* This is not possible with regular expressions
*/
// COMMAND = self::IDENTIFIER . self::ARGUMENTS . '\\s+(?:;|' . self::BLOCK . ')';
/**
* commands = *command
* This is not possible with regular expressions
*/
// COMMANDS = '(?:' . self::COMMAND . ')*';
/**
* start = commands
* This is not possible with regular expressions
*/
// START = self::COMMANDS;
/**
* test = identifier arguments
* This is not possible with regular expressions
*/
// TEST = self::IDENTIFIER . self::ARGUMENTS;
/**
* test-list = "(" test *("," test) ")"
* This is not possible with regular expressions
*/
// TEST_LIST = '\\(\\s*' . self::TEST . '(?:\\s*,\\s*' . self::TEST . ')*\\s*\\)';
/**************************************************
* https://tools.ietf.org/html/rfc5228#section-8.3
**************************************************/
/**
* COMPARATOR = ":comparator" string
*/
// RegEx.COMPARATOR = ':comparator\\s+(?:' + RegEx.STRING + ')';
Sieve.RegEx = RegEx;
})(this.Sieve);

View file

@ -1,300 +0,0 @@
/**
* https://tools.ietf.org/html/rfc5228#section-5
*/
(Sieve => {
const Grammar = Sieve.Grammar,
Test = Grammar.Test,
StringList = Grammar.StringList,
isAddressPart = tag => ':localpart' === tag || ':domain' === tag || ':all' === tag || isSubAddressPart(tag),
// https://tools.ietf.org/html/rfc5233
isSubAddressPart = tag => ':user' === tag || ':detail' === tag;
/**
* https://tools.ietf.org/html/rfc5228#section-5.1
*/
class Address extends Test
{
constructor()
{
super('address');
this.address_part = ':all';
this.header_list = new StringList;
this.key_list = new StringList;
// rfc5260#section-6
// this.index = new Grammar.Number;
// this.last = false;
}
get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
return requires;
}
toString()
{
return 'address'
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.address_part
+ ' ' + this.match_type
+ ' ' + this.header_list.toString()
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
args.forEach((arg, i) => {
if (isAddressPart(arg)) {
this.address_part = arg;
} else if (':last' === arg) {
this.last = true;
} else if (':index' === args[i-1]) {
this.index.value = arg.value;
} else if (arg instanceof StringList || arg instanceof Grammar.StringType) {
this[args[i+1] ? 'header_list' : 'key_list'] = arg;
// (args[i+1] ? this.header_list : this.key_list) = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.2
*/
class AllOf extends Test
{
constructor()
{
super('allof');
this.tests = new Grammar.TestList;
}
toString()
{
return 'allof ' + this.tests.toString();
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.3
*/
class AnyOf extends Test
{
constructor()
{
super('anyof');
this.tests = new Grammar.TestList;
}
toString()
{
return 'anyof ' + this.tests.toString();
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.4
*/
class Envelope extends Test
{
constructor()
{
super('envelope');
this.address_part = ':all';
this.envelope_part = new StringList;
this.key_list = new StringList;
}
get require() { return isSubAddressPart(this.address_part) ? ['envelope','subaddress'] : 'envelope'; }
toString()
{
return 'envelope'
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.address_part
+ ' ' + this.match_type
+ ' ' + this.envelope_part.toString()
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
args.forEach((arg, i) => {
if (isAddressPart(arg)) {
this.address_part = arg;
} else if (arg instanceof StringList || arg instanceof Grammar.StringType) {
this[args[i+1] ? 'envelope_part' : 'key_list'] = arg;
// (args[i+1] ? this.envelope_part : this.key_list) = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.5
*/
class Exists extends Test
{
constructor()
{
super('exists');
this.header_names = new StringList;
}
toString()
{
return 'exists ' + this.header_names.toString();
}
pushArguments(args)
{
if (args[0] instanceof StringList) {
this.header_names = args;
} else if (args[0] instanceof Grammar.StringType) {
this.header_names.push(args[0].value);
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.6
*/
class False extends Test
{
toString()
{
return "false";
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.7
*/
class Header extends Test
{
constructor()
{
super('header');
this.address_part = ':all';
this.header_names = new StringList;
this.key_list = new StringList;
// rfc5260#section-6
// this.index = new Grammar.Number;
// this.last = false;
}
get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
return requires;
}
toString()
{
return 'header'
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.header_names.toString()
+ ' ' + this.key_list.toString();
}
pushArguments(args)
{
args.forEach((arg, i) => {
if (isAddressPart(arg)) {
this.address_part = arg;
} else if (':last' === arg) {
this.last = true;
} else if (':index' === args[i-1]) {
this.index.value = arg.value;
} else if (arg instanceof StringList || arg instanceof Grammar.StringType) {
this[args[i+1] ? 'header_names' : 'key_list'] = arg;
// (args[i+1] ? this.header_names : this.key_list) = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.8
*/
class Not extends Test
{
constructor()
{
super('not');
this.test = new Test;
}
toString()
{
return 'not ' + this.test;
}
pushArguments()
{
throw 'No arguments';
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.9
*/
class Size extends Test
{
constructor()
{
super('size');
this.mode = ':over'; // :under
this.limit = 0;
}
toString()
{
return 'size ' + this.mode + ' ' + this.limit;
}
pushArguments(args)
{
args.forEach(arg => {
if (':over' === arg || ':under' === arg) {
this.mode = arg;
} else if (arg instanceof Grammar.Number) {
this.limit = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.10
*/
class True extends Test
{
toString()
{
return 'true';
}
}
Object.assign(Sieve.Commands, {
address: Address,
allof: AllOf,
anyof: AnyOf,
envelope: Envelope,
exists: Exists,
false: False,
header: Header,
not: Not,
size: Size,
true: True
});
})(this.Sieve);

View file

@ -1,15 +0,0 @@
(win => {
win.Sieve = {
/*
RegEx: {},
Grammar: {},
Commands: {},
parseScript: ()=>{},
*/
arrayToString: (arr, separator) =>
arr.map(item => item.toString ? item.toString() : item).join(separator)
};
})(this);

View file

@ -81,18 +81,6 @@ config.paths.js = {
'dev/External/SquireUI.js' 'dev/External/SquireUI.js'
] ]
}, },
sieve: {
name: 'sieve.js',
src: [
'dev/sieve.js',
'dev/Sieve/RegEx.js',
'dev/Sieve/Grammar.js',
'dev/Sieve/Commands.js',
'dev/Sieve/Tests.js',
'dev/Sieve/Extensions/*.js',
'dev/Sieve/Parser.js'
]
},
app: { app: {
name: 'app.js' name: 'app.js'
}, },

View file

@ -46,17 +46,6 @@ const jsLibs = () => {
.pipe(gulp.dest(config.paths.staticJS)); .pipe(gulp.dest(config.paths.staticJS));
}; };
// sieve
const jsSieve = () => {
const src = config.paths.js.sieve.src;
return gulp
.src(src)
.pipe(expect.real({ errorOnFailure: true }, src))
.pipe(concat(config.paths.js.sieve.name, { separator: '\n\n' }))
.pipe(eol('\n', true))
.pipe(gulp.dest(config.paths.staticJS));
};
// app // app
const jsApp = () => const jsApp = () =>
gulp gulp
@ -125,7 +114,7 @@ const jsLint = () =>
.pipe(eslint.failAfterError()); .pipe(eslint.failAfterError());
const jsState1 = gulp.series(jsLint); const jsState1 = gulp.series(jsLint);
const jsState3 = gulp.parallel(jsBoot, jsServiceWorker, jsLibs, jsSieve, jsApp, jsAdmin); const jsState3 = gulp.parallel(jsBoot, jsServiceWorker, jsLibs, jsApp, jsAdmin);
const jsState2 = gulp.series(jsClean, webpack, jsState3, jsMin); const jsState2 = gulp.series(jsClean, webpack, jsState3, jsMin);
exports.jsLint = jsLint; exports.jsLint = jsLint;