Prepare some Sieve restructuring for GUI

This commit is contained in:
the-djmaze 2022-03-17 09:03:02 +01:00
parent 37bb9a9a97
commit ff9a89380c
15 changed files with 259 additions and 194 deletions

View file

@ -0,0 +1,111 @@
/**
* https://datatracker.ietf.org/doc/html/rfc5228#section-4
* Action commands do not take tests or blocks as arguments.
*/
import { capa } from 'Sieve/Utils';
import {
ActionCommand,
GrammarString,
GrammarQuotedString
} from 'Sieve/Grammar';
/**
* https://tools.ietf.org/html/rfc5228#section-4.1
*/
export class FileIntoCommand extends ActionCommand
{
constructor()
{
super();
// QuotedString / MultiLine
this._mailbox = new GrammarQuotedString();
}
get require() { return 'fileinto'; }
toString()
{
return 'fileinto'
// https://datatracker.ietf.org/doc/html/rfc3894
+ ((this.copy && capa.includes('copy')) ? ' :copy' : '')
// https://datatracker.ietf.org/doc/html/rfc5490#section-3.2
+ ((this.create && capa.includes('mailbox')) ? ' :create' : '')
+ ' ' + this._mailbox
+ ';';
}
get mailbox()
{
return this._mailbox.value;
}
set mailbox(value)
{
this._mailbox.value = value;
}
pushArguments(args)
{
if (args[0] instanceof GrammarString) {
this._mailbox = args[0];
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.2
*/
export class RedirectCommand extends ActionCommand
{
constructor()
{
super();
// QuotedString / MultiLine
this._address = new GrammarQuotedString();
}
toString()
{
return 'redirect'
// https://datatracker.ietf.org/doc/html/rfc6134#section-2.3
// + ((this.list && capa.includes('extlists')) ? ' :list ' + this.list : '')
// https://datatracker.ietf.org/doc/html/rfc3894
+ ((this.copy && capa.includes('copy')) ? ' :copy' : '')
+ ' ' + this._address
+ ';';
}
get address()
{
return this._address.value;
}
set address(value)
{
this._address.value = value;
}
pushArguments(args)
{
if (args[0] instanceof GrammarString) {
this._address = args[0];
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.3
*/
export class KeepCommand extends ActionCommand
{
}
/**
* https://tools.ietf.org/html/rfc5228#section-4.4
*/
export class DiscardCommand extends ActionCommand
{
}

View file

@ -0,0 +1,92 @@
/**
* https://tools.ietf.org/html/rfc5228#section-2.9
* A control structure is a control command that ends with a block instead of a semicolon.
*/
import {
GrammarCommand,
GrammarStringList,
GrammarQuotedString
} from 'Sieve/Grammar';
/**
* https://tools.ietf.org/html/rfc5228#section-3.1
* Usage:
* if <test1: test> <block1: block>
* elsif <test2: test> <block2: block>
* else <block3: block>
*/
export class ConditionalCommand extends GrammarCommand
{
constructor()
{
super();
this.test = null;
}
toString()
{
return this.identifier + ' ' + this.test + ' ' + this.commands;
}
/*
public function pushArguments(array $args): void
{
args.forEach((arg, i) => {
if (':' === args[i-1][0]) {
this[args[i-1].replace(':','_')].value = arg.value;
}
});
print_r($args);
exit;
}
*/
}
export class IfCommand extends ConditionalCommand
{
}
export class ElsIfCommand extends ConditionalCommand
{
}
export class ElseCommand extends ConditionalCommand
{
toString()
{
return this.identifier + ' ' + this.commands;
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-3.2
*/
export class RequireCommand extends GrammarCommand
{
constructor()
{
super();
this.capabilities = new GrammarStringList();
}
toString()
{
return 'require ' + this.capabilities + ';';
}
pushArguments(args)
{
if (args[0] instanceof GrammarStringList) {
this.capabilities = args[0];
} else if (args[0] instanceof GrammarQuotedString) {
this.capabilities.push(args[0]);
}
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-3.3
*/
export class StopCommand extends GrammarCommand
{
}

364
dev/Sieve/Commands/Tests.js Normal file
View file

@ -0,0 +1,364 @@
/**
* https://tools.ietf.org/html/rfc5228#section-5
*/
import { capa } from 'Sieve/Utils';
import {
GrammarNumber,
GrammarString,
GrammarStringList,
TestCommand,
GrammarTestList
} from 'Sieve/Grammar';
const
isAddressPart = tag => ':localpart' === tag || ':domain' === tag || ':all' === tag || isSubAddressPart(tag),
// https://tools.ietf.org/html/rfc5233
isSubAddressPart = tag => ':user' === tag || ':detail' === tag,
asStringList = arg => {
if (arg instanceof GrammarStringList) {
return arg;
}
let args = new GrammarStringList();
if (arg instanceof GrammarString) {
args.push(arg.value);
}
return args;
};
/**
* https://tools.ietf.org/html/rfc5228#section-5.1
*/
export class AddressTest extends TestCommand
{
constructor()
{
super();
this.address_part = ':all';
this.header_list = new GrammarStringList;
this.key_list = new GrammarStringList;
// rfc5260#section-6
// this.index = new GrammarNumber;
// this.last = false;
// rfc5703#section-6
// this.mime
// this.anychild
}
get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
(this.mime || this.anychild) && requires.push('mime');
return requires;
}
toString()
{
let result = 'address';
if (capa.includes('mime')) {
if (this.mime) {
result += ' :mime';
}
if (this.anychild) {
result += ' :anychild';
}
}
return result
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.address_part
+ ' ' + this.match_type
+ ' ' + this.header_list
+ ' ' + this.key_list;
}
pushArguments(args)
{
this.key_list = asStringList(args.pop());
this.header_list = asStringList(args.pop());
args.forEach((arg, i) => {
if (isAddressPart(arg)) {
this.address_part = arg;
} else if (':last' === arg) {
this.last = true;
} else if (':mime' === arg) {
this.mime = true;
} else if (':anychild' === arg) {
this.anychild = true;
} else if (':index' === args[i-1]) {
this.index.value = arg.value;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.2
*/
export class AllOfTest extends TestCommand
{
constructor()
{
super();
this.tests = new GrammarTestList;
}
toString()
{
return 'allof ' + this.tests;
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.3
*/
export class AnyOfTest extends TestCommand
{
constructor()
{
super();
this.tests = new GrammarTestList;
}
toString()
{
return 'anyof ' + this.tests;
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.4
*/
export class EnvelopeTest extends TestCommand
{
constructor()
{
super();
this.address_part = ':all';
this.envelope_part = new GrammarStringList;
this.key_list = new GrammarStringList;
}
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
+ ' ' + this.key_list;
}
pushArguments(args)
{
this.key_list = asStringList(args.pop());
this.envelope_part = asStringList(args.pop());
args.forEach(arg => {
if (isAddressPart(arg)) {
this.address_part = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.5
*/
export class ExistsTest extends TestCommand
{
constructor()
{
super();
this.header_names = new GrammarStringList;
// rfc5703#section-6
// this.mime
// this.anychild
}
get require() {
return (this.mime || this.anychild) ? ['mime'] : null;
}
toString()
{
let result = 'exists';
if (capa.includes('mime')) {
if (this.mime) {
result += ' :mime';
}
if (this.anychild) {
result += ' :anychild';
}
}
return result + ' ' + this.header_names;
}
pushArguments(args)
{
this.header_names = asStringList(args.pop());
args.forEach(arg => {
if (':mime' === arg) {
this.mime = true;
} else if (':anychild' === arg) {
this.mime = true;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.6
*/
export class FalseTest extends TestCommand
{
toString()
{
return "false";
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.7
*/
export class HeaderTest extends TestCommand
{
constructor()
{
super();
this.address_part = ':all';
this.header_names = new GrammarStringList;
this.key_list = new GrammarStringList;
// rfc5260#section-6
// this.index = new GrammarNumber;
// this.last = false;
// rfc5703#section-6
this.mime = false;
this.anychild = false;
// when ":mime" is used:
this.type = false;
this.subtype = false;
this.contenttype = false;
this.param = new GrammarStringList;
}
get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
(this.mime || this.anychild) && requires.push('mime');
return requires;
}
toString()
{
let result = 'header';
if (capa.includes('mime')) {
if (this.mime) {
result += ' :mime';
if (this.type) {
result += ' :type';
}
if (this.subtype) {
result += ' :subtype';
}
if (this.contenttype) {
result += ' :contenttype';
}
if (this.param.length) {
result += ' :param ' + this.param;
}
}
if (this.anychild) {
result += ' :anychild';
}
}
return result
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
+ ' ' + this.header_names
+ ' ' + this.key_list;
}
pushArguments(args)
{
this.key_list = asStringList(args.pop());
this.header_names = asStringList(args.pop());
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;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.8
*/
export class NotTest extends TestCommand
{
constructor()
{
super();
this.test = new TestCommand;
}
toString()
{
return 'not ' + this.test;
}
pushArguments()
{
throw 'No arguments';
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.9
*/
export class SizeTest extends TestCommand
{
constructor()
{
super();
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 GrammarNumber) {
this.limit = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.10
*/
export class TrueTest extends TestCommand
{
toString()
{
return 'true';
}
}