snappymail/dev/Sieve/Commands/Tests.js

365 lines
7 KiB
JavaScript
Raw Normal View History

2022-03-09 19:33:31 +08:00
/**
* https://tools.ietf.org/html/rfc5228#section-5
*/
2022-03-16 19:05:50 +08:00
import { capa } from 'Sieve/Utils';
2022-03-09 19:33:31 +08:00
import {
GrammarNumber,
GrammarString,
GrammarStringList,
TestCommand,
2022-03-09 19:33:31 +08:00
GrammarTestList
} from 'Sieve/Grammar';
const
isAddressPart = tag => ':localpart' === tag || ':domain' === tag || ':all' === tag || isSubAddressPart(tag),
// https://tools.ietf.org/html/rfc5233
2022-03-16 19:05:50 +08:00
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;
};
2022-03-09 19:33:31 +08:00
/**
* https://tools.ietf.org/html/rfc5228#section-5.1
*/
export class AddressTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
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;
2022-03-16 19:05:50 +08:00
// rfc5703#section-6
// this.mime
// this.anychild
2022-03-09 19:33:31 +08:00
}
get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
2022-03-16 19:05:50 +08:00
(this.mime || this.anychild) && requires.push('mime');
2022-03-09 19:33:31 +08:00
return requires;
}
toString()
{
2022-03-16 19:05:50 +08:00
let result = 'address';
if (capa.includes('mime')) {
if (this.mime) {
result += ' :mime';
}
if (this.anychild) {
result += ' :anychild';
}
}
return result
2022-03-09 19:33:31 +08:00
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.address_part
+ ' ' + this.match_type
2022-03-16 21:21:23 +08:00
+ ' ' + this.header_list
+ ' ' + this.key_list;
2022-03-09 19:33:31 +08:00
}
pushArguments(args)
{
2022-03-16 19:05:50 +08:00
this.key_list = asStringList(args.pop());
this.header_list = asStringList(args.pop());
2022-03-09 19:33:31 +08:00
args.forEach((arg, i) => {
if (isAddressPart(arg)) {
this.address_part = arg;
} else if (':last' === arg) {
this.last = true;
2022-03-16 19:05:50 +08:00
} else if (':mime' === arg) {
this.mime = true;
} else if (':anychild' === arg) {
this.anychild = true;
2022-05-17 20:48:38 +08:00
} else if (i && ':index' === args[i-1]) {
2022-03-09 19:33:31 +08:00
this.index.value = arg.value;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.2
*/
export class AllOfTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
constructor()
{
super();
this.tests = new GrammarTestList;
}
toString()
{
2022-03-16 21:21:23 +08:00
return 'allof ' + this.tests;
2022-03-09 19:33:31 +08:00
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.3
*/
export class AnyOfTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
constructor()
{
super();
this.tests = new GrammarTestList;
}
toString()
{
2022-03-16 21:21:23 +08:00
return 'anyof ' + this.tests;
2022-03-09 19:33:31 +08:00
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.4
*/
export class EnvelopeTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
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
2022-03-16 21:21:23 +08:00
+ ' ' + this.envelope_part
+ ' ' + this.key_list;
2022-03-09 19:33:31 +08:00
}
pushArguments(args)
{
2022-03-16 19:05:50 +08:00
this.key_list = asStringList(args.pop());
this.envelope_part = asStringList(args.pop());
args.forEach(arg => {
2022-03-09 19:33:31 +08:00
if (isAddressPart(arg)) {
this.address_part = arg;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.5
*/
export class ExistsTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
constructor()
{
super();
this.header_names = new GrammarStringList;
2022-03-16 19:05:50 +08:00
// rfc5703#section-6
// this.mime
// this.anychild
}
get require() {
return (this.mime || this.anychild) ? ['mime'] : null;
2022-03-09 19:33:31 +08:00
}
toString()
{
2022-03-16 19:05:50 +08:00
let result = 'exists';
if (capa.includes('mime')) {
if (this.mime) {
result += ' :mime';
}
if (this.anychild) {
result += ' :anychild';
}
}
2022-03-16 21:21:23 +08:00
return result + ' ' + this.header_names;
2022-03-09 19:33:31 +08:00
}
pushArguments(args)
{
2022-03-16 19:05:50 +08:00
this.header_names = asStringList(args.pop());
args.forEach(arg => {
if (':mime' === arg) {
this.mime = true;
} else if (':anychild' === arg) {
2022-05-17 20:48:38 +08:00
this.anychild = true;
2022-03-16 19:05:50 +08:00
}
});
2022-03-09 19:33:31 +08:00
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.6
*/
export class FalseTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
toString()
{
return "false";
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.7
*/
export class HeaderTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
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;
2022-03-16 19:05:50 +08:00
// 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;
2022-03-09 19:33:31 +08:00
}
get require() {
let requires = [];
isSubAddressPart(this.address_part) && requires.push('subaddress');
(this.last || (this.index && this.index.value)) && requires.push('index');
2022-03-16 19:05:50 +08:00
(this.mime || this.anychild) && requires.push('mime');
2022-03-09 19:33:31 +08:00
return requires;
}
toString()
{
2022-03-16 19:05:50 +08:00
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
2022-03-09 19:33:31 +08:00
// + (this.last ? ' :last' : (this.index.value ? ' :index ' + this.index : ''))
+ (this.comparator ? ' :comparator ' + this.comparator : '')
+ ' ' + this.match_type
2022-03-16 21:21:23 +08:00
+ ' ' + this.header_names
+ ' ' + this.key_list;
2022-03-09 19:33:31 +08:00
}
pushArguments(args)
{
2022-03-16 19:05:50 +08:00
this.key_list = asStringList(args.pop());
this.header_names = asStringList(args.pop());
2022-03-09 19:33:31 +08:00
args.forEach((arg, i) => {
if (isAddressPart(arg)) {
this.address_part = arg;
} else if (':last' === arg) {
this.last = true;
2022-05-17 20:48:38 +08:00
} else if (i && ':index' === args[i-1]) {
2022-03-09 19:33:31 +08:00
this.index.value = arg.value;
}
});
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.8
*/
export class NotTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
constructor()
{
super();
this.test = new TestCommand;
2022-03-09 19:33:31 +08:00
}
toString()
{
return 'not ' + this.test;
}
pushArguments()
{
throw 'No arguments';
}
}
/**
* https://tools.ietf.org/html/rfc5228#section-5.9
*/
export class SizeTest extends TestCommand
2022-03-09 19:33:31 +08:00
{
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
2022-03-09 19:33:31 +08:00
{
toString()
{
return 'true';
}
}