Added: rfc5233

Activate: comparator
Bugfix: StringList must use .toString()
Bugfix: redirect command
Bugfix: MultiLine
This commit is contained in:
djmaze 2021-01-15 16:09:03 +01:00
parent 3566f4a538
commit 73e26d388a
12 changed files with 86 additions and 50 deletions

View file

@ -156,7 +156,7 @@ class Redirect extends Command
toString() toString()
{ {
return 'redirect ' + this.address + ';'; return 'redirect ' + this._address + ';';
} }
get address() get address()
@ -168,6 +168,13 @@ class Redirect extends Command
{ {
this._address.value = value; this._address.value = value;
} }
pushArguments(args)
{
if (args[0] instanceof Grammar.StringType) {
this._address = args[0];
}
}
} }
/** /**

View file

@ -20,10 +20,10 @@ class Body extends Grammar.Test
toString() toString()
{ {
return 'body' return 'body'
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.body_transform + ' ' + this.body_transform
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)

View file

@ -20,10 +20,10 @@ class Environment extends Grammar.Test
toString() toString()
{ {
return 'body' return 'body'
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.name + ' ' + this.name
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)

View file

@ -55,9 +55,9 @@ class String extends Grammar.Test
{ {
return 'string' return 'string'
+ ' ' + this.match_type + ' ' + this.match_type
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.source + ' ' + this.source.toString()
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)

View file

@ -40,7 +40,7 @@ class Vacation extends Grammar.Command
result += ' :from ' + this.arguments[':from']; result += ' :from ' + this.arguments[':from'];
} }
if (this.addresses.length) { if (this.addresses.length) {
result += ' :addresses ' + this.addresses; result += ' :addresses ' + this.addresses.toString();
} }
if (this.mime) { if (this.mime) {
result += ' :mime'; result += ' :mime';

View file

@ -19,7 +19,7 @@ class Flag extends Grammar.Command
toString() toString()
{ {
return this.identifier + ' ' + this._variablename + ' ' + this.list_of_flags + ';'; return this.identifier + ' ' + this._variablename + ' ' + this.list_of_flags.toString() + ';';
} }
get variablename() get variablename()
@ -86,9 +86,9 @@ class HasFlag extends Grammar.Test
{ {
return 'hasflag' return 'hasflag'
+ ' ' + this.match_type + ' ' + this.match_type
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.variable_list + ' ' + this.variable_list.toString()
+ ' ' + this.list_of_flags; + ' ' + this.list_of_flags.toString();
} }
pushArguments(args) pushArguments(args)

View file

@ -16,14 +16,13 @@ class SpamTest extends Grammar.Test
} }
// get require() { return this.percent ? 'spamtestplus' : 'spamtest'; } // get require() { return this.percent ? 'spamtestplus' : 'spamtest'; }
// get require() { return /:value|:count/.test(this.match_type) ? 'relational' : ''; } get require() { return /:value|:count/.test(this.match_type) ? ['spamtestplus','relational'] : 'spamtestplus'; }
get require() { return 'spamtestplus'; }
toString() toString()
{ {
return 'spamtest' return 'spamtest'
+ (this.percent ? ' :percent' : '') + (this.percent ? ' :percent' : '')
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.value; + ' ' + this.value;
} }
@ -48,13 +47,12 @@ class VirusTest extends Grammar.Test
this.value = new Grammar.QuotedString; // 1 - 5 this.value = new Grammar.QuotedString; // 1 - 5
} }
// get require() { return /:value|:count/.test(this.match_type) ? ['virustest','relational'] : 'virustest'; } get require() { return /:value|:count/.test(this.match_type) ? ['virustest','relational'] : 'virustest'; }
get require() { return 'virustest'; }
toString() toString()
{ {
return 'virustest' return 'virustest'
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.value; + ' ' + this.value;
} }

View file

@ -29,11 +29,11 @@ class DateTest extends Grammar.Test
return 'date' return 'date'
+ (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : '')) + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.originalzone ? ' :originalzone' : (this.zone.length ? ' :zone ' + this.zone : '')) + (this.originalzone ? ' :originalzone' : (this.zone.length ? ' :zone ' + this.zone : ''))
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.header_name + ' ' + this.header_name
+ ' ' + this.date_part + ' ' + this.date_part
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)
@ -75,10 +75,10 @@ class CurrentDate extends Grammar.Test
{ {
return 'date' return 'date'
+ (this.zone.length ? ' :zone ' + this.zone : '') + (this.zone.length ? ' :zone ' + this.zone : '')
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.date_part + ' ' + this.date_part
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)

View file

@ -53,7 +53,7 @@ class DeleteHeader extends Grammar.Command
{ {
return this.identifier return this.identifier
+ (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : '')) + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.field_name + ' ' + this.field_name
+ ' ' + this.value_patterns + ';'; + ' ' + this.value_patterns + ';';

View file

@ -203,12 +203,22 @@ class MultiLine extends StringType
toString() toString()
{ {
return 'text:' return 'text:'
+ (this.comment ? "#${this.comment}" : '') + "\r\n" + (this.comment ? '# ' + this.comment : '') + "\r\n"
// + \rtrim(this.value, "\r\n") + this.value
+ "\r\n.\r\n"; + "\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 * https://tools.ietf.org/html/rfc5228#section-5
@ -226,7 +236,10 @@ class Test
toString() toString()
{ {
return (this.identifier + ' ' + Sieve.arrayToString(this.arguments, ' ')).trim(); return (this.identifier
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ (this.match_type ? ' ' + this.match_type : '')
+ ' ' + Sieve.arrayToString(this.arguments, ' ')).trim();
} }
pushArguments(args) pushArguments(args)

View file

@ -152,6 +152,9 @@ Sieve.parseScript = (script, name = 'script.sieve') => {
(Array.isArray(command.require) ? command.require : [command.require]) (Array.isArray(command.require) ? command.require : [command.require])
.forEach(string => requires.push(string)); .forEach(string => requires.push(string));
} }
if (command.comparator) {
requires.push('comparator-' + this.comparator);
}
break; } break; }
// Arguments // Arguments
@ -162,7 +165,7 @@ Sieve.parseScript = (script, name = 'script.sieve') => {
pushArg(Grammar.StringList.fromString(value)); pushArg(Grammar.StringList.fromString(value));
break; break;
case T_MULTILINE_STRING: case T_MULTILINE_STRING:
pushArg(new Grammar.MultiLine(value)); pushArg(Grammar.MultiLine.fromString(value));
break; break;
case T_QUOTED_STRING: case T_QUOTED_STRING:
pushArg(new Grammar.QuotedString(value.substr(1,value.length-2))); pushArg(new Grammar.QuotedString(value.substr(1,value.length-2)));
@ -254,6 +257,7 @@ Sieve.parseScript = (script, name = 'script.sieve') => {
line += (value.split('\n').length - 1); // (value.match(/\n/g) || []).length; line += (value.split('\n').length - 1); // (value.match(/\n/g) || []).length;
} }
tree.requires = requires;
return tree; return tree;
}; };

View file

@ -6,7 +6,11 @@
const Grammar = Sieve.Grammar, const Grammar = Sieve.Grammar,
Test = Grammar.Test, Test = Grammar.Test,
StringList = Grammar.StringList; 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 * https://tools.ietf.org/html/rfc5228#section-5.1
@ -16,7 +20,7 @@ class Address extends Test
constructor() constructor()
{ {
super('address'); super('address');
this.address_part = ':all'; // :localpart | :domain | :all this.address_part = ':all';
this.header_list = new StringList; this.header_list = new StringList;
this.key_list = new StringList; this.key_list = new StringList;
// rfc5260#section-6 // rfc5260#section-6
@ -24,23 +28,28 @@ class Address extends Test
// this.last = false; // this.last = false;
} }
get require() { return this.last ? 'index' : ''; } get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
return requires;
}
toString() toString()
{ {
return 'address' return 'address'
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : '')) // + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.address_part + ' ' + this.address_part
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.header_list + ' ' + this.header_list.toString()
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)
{ {
args.forEach((arg, i) => { args.forEach((arg, i) => {
if (':localpart' === arg || ':domain' === arg || ':all' === arg) { if (isAddressPart(arg)) {
this.address_part = arg; this.address_part = arg;
} else if (':last' === arg) { } else if (':last' === arg) {
this.last = true; this.last = true;
@ -67,7 +76,7 @@ class AllOf extends Test
toString() toString()
{ {
return 'allof ' + this.tests; return 'allof ' + this.tests.toString();
} }
} }
@ -84,7 +93,7 @@ class AnyOf extends Test
toString() toString()
{ {
return 'anyof ' + this.tests; return 'anyof ' + this.tests.toString();
} }
} }
@ -96,27 +105,27 @@ class Envelope extends Test
constructor() constructor()
{ {
super('envelope'); super('envelope');
this.address_part = ':all'; // :localpart | :domain | :all this.address_part = ':all';
this.envelope_part = new StringList; this.envelope_part = new StringList;
this.key_list = new StringList; this.key_list = new StringList;
} }
get require() { return 'envelope'; } get require() { return isSubAddressPart(this.address_part) ? ['envelope','subaddress'] : 'envelope'; }
toString() toString()
{ {
return 'envelope' return 'envelope'
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.address_part + ' ' + this.address_part
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.envelope_part + ' ' + this.envelope_part.toString()
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)
{ {
args.forEach((arg, i) => { args.forEach((arg, i) => {
if (':localpart' === arg || ':domain' === arg || ':all' === arg) { if (isAddressPart(arg)) {
this.address_part = arg; this.address_part = arg;
} else if (arg instanceof StringList || arg instanceof Grammar.StringType) { } else if (arg instanceof StringList || arg instanceof Grammar.StringType) {
this[args[i+1] ? 'envelope_part' : 'key_list'] = arg; this[args[i+1] ? 'envelope_part' : 'key_list'] = arg;
@ -139,7 +148,7 @@ class Exists extends Test
toString() toString()
{ {
return 'exists ' + this.header_names; return 'exists ' + this.header_names.toString();
} }
pushArguments(args) pushArguments(args)
@ -171,7 +180,7 @@ class Header extends Test
constructor() constructor()
{ {
super('header'); super('header');
this.address_part = ':all'; // :localpart | :domain | :all this.address_part = ':all';
this.header_names = new StringList; this.header_names = new StringList;
this.key_list = new StringList; this.key_list = new StringList;
// rfc5260#section-6 // rfc5260#section-6
@ -179,22 +188,27 @@ class Header extends Test
// this.last = false; // this.last = false;
} }
get require() { return this.last ? 'index' : ''; } get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
return requires;
}
toString() toString()
{ {
return 'header' return 'header'
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : '')) // + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
// + ' ' + this.comparator + (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type + ' ' + this.match_type
+ ' ' + this.header_names + ' ' + this.header_names.toString()
+ ' ' + this.key_list; + ' ' + this.key_list.toString();
} }
pushArguments(args) pushArguments(args)
{ {
args.forEach((arg, i) => { args.forEach((arg, i) => {
if (':localpart' === arg || ':domain' === arg || ':all' === arg) { if (isAddressPart(arg)) {
this.address_part = arg; this.address_part = arg;
} else if (':last' === arg) { } else if (':last' === arg) {
this.last = true; this.last = true;