Centralize some argument parsing

Improved parser error handling
Fix Sieve Vacation extension
This commit is contained in:
djmaze 2021-01-15 11:34:16 +01:00
parent 55178016a0
commit 68fc9f21bd
7 changed files with 131 additions and 168 deletions

View file

@ -11,7 +11,7 @@ class Body extends Grammar.Test
constructor()
{
super('body');
this.comparator = 'i;ascii-casemap',
this.comparator = '',
this.match_type = ':is',
this.body_transform = ''; // :raw, :content <string-list>, :text
this.key_list = new Grammar.StringList;
@ -31,14 +31,10 @@ class Body extends Grammar.Test
pushArguments(args)
{
args.forEach((arg, i) => {
if (':is' === arg || ':contains' === arg || ':matches' === arg) {
this.match_type = arg;
} else if (':raw' === arg || ':text' === arg) {
if (':raw' === arg || ':text' === arg) {
this.body_transform = arg;
} else if (arg instanceof Grammar.StringList || arg instanceof Grammar.StringType) {
if (':comparator' === args[i-1]) {
this.comparator = arg;
} else if (':content' === args[i-1]) {
if (':content' === args[i-1]) {
this.body_transform = ':content ' + arg;
} else {
this[args[i+1] ? 'content_list' : 'key_list'] = arg;

View file

@ -11,15 +11,13 @@ class Vacation extends Grammar.Command
constructor()
{
super('vacation');
this.arguments = {
':days' : new Grammar.Number,
':subject' : new Grammar.QuotedString,
':from' : new Grammar.QuotedString,
':addresses': new Grammar.StringList,
':mime' : false,
':handle' : new Grammar.QuotedString
};
this.reason = ''; // QuotedString / MultiLine
this._days = 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'; }
@ -27,63 +25,66 @@ class Vacation extends Grammar.Command
toString()
{
let result = 'vacation';
if (0 < this.arguments[':days'].value) {
result += ' :days ' + this.arguments[':days'];
if (0 < this._days.value) {
result += ' :days ' + this._days;
}
if (this.arguments[':subject'].length()) {
result += ' :subject ' + this.arguments[':subject'];
if (this._subject.length) {
result += ' :subject ' + this._subject;
}
if (this.arguments[':from'].length()) {
if (this._from.length) {
result += ' :from ' + this.arguments[':from'];
}
if (this.arguments[':addresses'].length) {
result += ' :addresses ' + this.arguments[':addresses'];
if (this.addresses.length) {
result += ' :addresses ' + this.addresses;
}
if (this.arguments[':mime']) {
if (this.mime) {
result += ' :mime';
}
if (this.arguments[':handle'].length()) {
result += ' :handle ' + this.arguments[':handle'];
}
return result + ' ' + this.reason;
}
/*
function __get($key)
{
if ('reason' === $key) {
return this.reason;
}
if (isset(this.arguments[":{$key}"])) {
return this.arguments[":{$key}"];
if (this._handle.length) {
result += ' :handle ' + this._handle;
}
return result + ' ' + this._reason;
}
function __set($key, $value)
{
if ('days' === $key) {
this.arguments[":{$key}"] = (int) $value;
} else if ('mime' === $key) {
this.arguments[":{$key}"] = (bool) $value;
} else if ('reason' === $key) {
this.reason = (string) $value;
} else if ('addresses' !== $key && isset(this.arguments[":{$key}"])) {
this.arguments[":{$key}"] = (string) $value;
}
}
public function pushArguments(array $args): void
get days() { return this._days.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 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)
{
foreach ($args as $i => $arg) {
if (\in_array($arg, [':days',':subject',':from',':addresses', ':handle'])) {
this.arguments[$arg] = $args[$i+1];
} else if (':mime' === $arg) {
this.arguments[':mime'] = true;
} else if (!isset($args[$i+1])) {
this.reason = $arg;
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 ':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;

View file

@ -76,7 +76,7 @@ class HasFlag extends Grammar.Test
constructor()
{
super('hasflag');
this.comparator = 'i;ascii-casemap',
this.comparator = '',
this.match_type = ':is',
this.variable_list = new Grammar.StringList;
this.list_of_flags = new Grammar.StringList;
@ -96,14 +96,8 @@ class HasFlag extends Grammar.Test
pushArguments(args)
{
args.forEach((arg, i) => {
if (':is' === arg || ':contains' === arg || ':matches' === arg) {
this.match_type = arg;
} else if (arg instanceof Grammar.StringList || arg instanceof Grammar.StringType) {
if (':comparator' === args[i-1]) {
this.comparator = arg;
} else {
this[args[i+1] ? 'variable_list' : 'list_of_flags'] = arg;
}
if (arg instanceof Grammar.StringList || arg instanceof Grammar.StringType) {
this[args[i+1] ? 'variable_list' : 'list_of_flags'] = arg;
}
});
}

View file

@ -12,7 +12,7 @@ class SpamTest extends Grammar.Test
{
super('spamtest');
this.percent = false, // 0 - 100 else 0 - 10
this.comparator = 'i;ascii-casemap',
this.comparator = '',
this.match_type = ':is',
this.value = new Grammar.QuotedString;
}
@ -32,20 +32,11 @@ class SpamTest extends Grammar.Test
pushArguments(args)
{
args.forEach((arg, i) => {
if (':is' === arg || ':contains' === arg || ':matches' === arg) {
this.match_type = arg;
} else if (':percent' === arg) {
args.forEach(arg => {
if (':percent' === arg) {
this.percent = true;
} else if (arg instanceof Grammar.StringType) {
if (':comparator' === args[i-1]) {
this.comparator = arg;
} else if (':value' === args[i-1] || ':count' === args[i-1]) {
// Sieve relational [RFC5231] match types
this.match_type = args[i-1] + ' ' + arg;
} else {
this.value = arg;
}
this.value = arg;
}
});
}
@ -56,7 +47,7 @@ class VirusTest extends Grammar.Test
constructor()
{
super('virustest');
this.comparator = 'i;ascii-casemap',
this.comparator = '',
this.match_type = ':is',
this.value = new Grammar.QuotedString; // 1 - 5
}
@ -74,18 +65,9 @@ class VirusTest extends Grammar.Test
pushArguments(args)
{
args.forEach((arg, i) => {
if (':is' === arg || ':contains' === arg || ':matches' === arg) {
this.match_type = arg;
} else if (arg instanceof Grammar.StringType) {
if (':comparator' === args[i-1]) {
this.comparator = arg;
} else if (':value' === args[i-1] || ':count' === args[i-1]) {
// Sieve relational [RFC5231] match types
this.match_type = args[i-1] + ' ' + arg;
} else {
this.value = arg;
}
args.forEach(arg => {
if (arg instanceof Grammar.StringType) {
this.value = arg;
}
});
}