Added Sieve parser code in master

This commit is contained in:
the-djmaze 2022-03-09 12:33:31 +01:00
parent e90e8a55ce
commit f4cd25f8ad
21 changed files with 2126 additions and 8 deletions

View file

@ -0,0 +1,47 @@
/**
* https://tools.ietf.org/html/rfc5173
*/
import {
GrammarString,
GrammarStringList,
GrammarTest
} from 'Sieve/Grammar';
export class BodyCommand extends GrammarTest
{
constructor()
{
super();
this.body_transform = ''; // :raw, :content <string-list>, :text
this.key_list = new GrammarStringList;
}
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 GrammarStringList || arg instanceof GrammarString) {
if (':content' === args[i-1]) {
this.body_transform = ':content ' + arg;
} else {
this[args[i+1] ? 'content_list' : 'key_list'] = arg;
}
}
});
}
}
//Sieve.Commands.body = BodyCommand;

View file

@ -0,0 +1,38 @@
/**
* https://tools.ietf.org/html/rfc5183
*/
import {
GrammarQuotedString,
GrammarStringList,
GrammarTest
} from 'Sieve/Grammar';
export class EnvironmentCommand extends GrammarTest
{
constructor()
{
super();
this.name = new GrammarQuotedString;
this.key_list = new GrammarStringList;
}
get require() { return 'environment'; }
toString()
{
return 'environment'
+ (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 = EnvironmentCommand;

View file

@ -0,0 +1,78 @@
/**
* https://tools.ietf.org/html/rfc5229
*/
import {
GrammarCommand,
GrammarQuotedString,
GrammarStringList,
GrammarTest
} from 'Sieve/Grammar';
export class SetCommand extends GrammarCommand
{
constructor()
{
super();
this.modifiers = [];
this._name = new GrammarQuotedString;
this._value = new GrammarQuotedString;
}
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];
}
}
export class StringCommand extends GrammarTest
{
constructor()
{
super();
this.source = new GrammarStringList;
this.key_list = new GrammarStringList;
}
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];
}
}
/*
Object.assign(Sieve.Commands, {
set: SetCommand,
string: StringCommand
});
*/

View file

@ -0,0 +1,100 @@
/**
* https://tools.ietf.org/html/rfc5230
* https://tools.ietf.org/html/rfc6131
*/
import {
GrammarCommand,
GrammarNumber,
GrammarQuotedString,
GrammarStringList
} from 'Sieve/Grammar';
export class VacationCommand extends GrammarCommand
{
constructor()
{
super();
this._days = new GrammarNumber;
// this._seconds = new GrammarNumber;
this._subject = new GrammarQuotedString;
this._from = new GrammarQuotedString;
this.addresses = new GrammarStringList;
this.mime = false;
this._handle = new GrammarQuotedString;
this._reason = new GrammarQuotedString; // 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; // GrammarQuotedString
} else switch (args[i-1]) {
case ':days':
this._days.value = arg.value; // GrammarNumber
break;
// case ':seconds':
// this._seconds.value = arg.value; // GrammarNumber
// break;
case ':subject':
this._subject.value = arg.value; // GrammarQuotedString
break;
case ':from':
this._from.value = arg.value; // GrammarQuotedString
break;
case ':addresses':
this.addresses = arg; // GrammarStringList
break;
case ':handle':
this._from.value = arg.value; // GrammarQuotedString
break;
}
});
}
}

View file

@ -0,0 +1,94 @@
/**
* https://tools.ietf.org/html/rfc5232
*/
import {
GrammarCommand,
GrammarQuotedString,
GrammarString,
GrammarStringList,
GrammarTest
} from 'Sieve/Grammar';
class FlagCommand extends GrammarCommand
{
constructor()
{
super();
this._variablename = new GrammarQuotedString;
this.list_of_flags = new GrammarStringList;
}
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 GrammarQuotedString) {
this._variablename = args[0];
}
if (args[1] instanceof GrammarString) {
this.list_of_flags = args[1];
}
} else if (args[0] instanceof GrammarString) {
this.list_of_flags = args[0];
}
}
}
export class SetFlagCommand extends FlagCommand
{
}
export class AddFlagCommand extends FlagCommand
{
}
export class RemoveFlagCommand extends FlagCommand
{
}
export class HasFlagCommand extends GrammarTest
{
constructor()
{
super();
this.variable_list = new GrammarStringList;
this.list_of_flags = new GrammarStringList;
}
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 GrammarStringList || arg instanceof GrammarString) {
this[args[i+1] ? 'variable_list' : 'list_of_flags'] = arg;
}
});
}
}

View file

@ -0,0 +1,77 @@
/**
* https://tools.ietf.org/html/rfc5235
*/
import {
GrammarQuotedString,
GrammarString,
GrammarTest
} from 'Sieve/Grammar';
export class SpamTestCommand extends GrammarTest
{
constructor()
{
super();
this.percent = false, // 0 - 100 else 0 - 10
this.value = new GrammarQuotedString;
}
// 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 GrammarString) {
this.value = arg;
}
});
}
}
export class VirusTestCommand extends GrammarTest
{
constructor()
{
super();
this.value = new GrammarQuotedString; // 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 GrammarString) {
this.value = arg;
}
});
}
}
/*
Object.assign(Sieve.Commands, {
spamtest: SpamTestCommand,
virustest: VirusTestCommand
});
*/

View file

@ -0,0 +1,106 @@
/**
* https://tools.ietf.org/html/rfc5260
*/
import {
GrammarNumber,
GrammarQuotedString,
GrammarStringList,
GrammarTest
} from 'Sieve/Grammar';
export class DateCommand extends GrammarTest
{
constructor()
{
super();
this.zone = new GrammarQuotedString;
this.originalzone = false;
this.header_name = new GrammarQuotedString;
this.date_part = new GrammarQuotedString;
this.key_list = new GrammarStringList;
// rfc5260#section-6
this.index = new GrammarNumber;
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;
}
});
}
}
export class CurrentDateCommand extends GrammarTest
{
constructor()
{
super('date');
this.zone = new GrammarQuotedString;
this.date_part = new GrammarQuotedString;
this.key_list = new GrammarStringList;
}
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;
}
});
}
}
/*
Object.assign(Sieve.Commands, {
date: DateTestCommand,
currentdate: CurrentDateCommand
*/

View file

@ -0,0 +1,91 @@
/**
* https://tools.ietf.org/html/rfc5293
*/
import {
GrammarCommand,
GrammarNumber,
GrammarQuotedString,
GrammarString,
GrammarStringList
} from 'Sieve/Grammar';
export class AddHeaderCommand extends GrammarCommand
{
constructor()
{
super();
this.last = false;
this.field_name = new GrammarQuotedString;
this.value = new GrammarQuotedString;
}
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];
}
}
export class DeleteHeaderCommand extends GrammarCommand
{
constructor()
{
super();
this.index = new GrammarNumber;
this.last = false;
this.comparator = '',
this.match_type = ':is',
this.field_name = new GrammarQuotedString;
this.value_patterns = new GrammarStringList;
}
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 GrammarString) {
this.field_name = args[l-1];
this.value_patterns = args[l];
} else {
this.field_name = args[l];
}
}
}
/*
Object.assign(Sieve.Commands, {
addheader: AddHeaderCommand,
deleteheader: DeleteHeaderCommand
});
*/

View file

@ -0,0 +1,81 @@
/**
* https://tools.ietf.org/html/rfc5429
*/
import {
GrammarCommand,
GrammarQuotedString,
GrammarString
} from 'Sieve/Grammar';
/**
* https://tools.ietf.org/html/rfc5429#section-2.1
*/
export class ErejectCommand extends GrammarCommand
{
constructor()
{
super();
this._reason = new GrammarQuotedString;
}
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 GrammarString) {
this._reason = args[0];
}
}
}
/**
* https://tools.ietf.org/html/rfc5429#section-2.2
*/
export class RejectCommand extends GrammarCommand
{
constructor()
{
super();
this._reason = new GrammarQuotedString;
}
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 GrammarString) {
this._reason = args[0];
}
}
}

View file

@ -0,0 +1,47 @@
/**
* https://tools.ietf.org/html/rfc6609
*/
import {
GrammarCommand,
GrammarQuotedString
} from 'Sieve/Grammar';
export class IncludeCommand extends GrammarCommand
{
constructor()
{
super();
this.global = false; // ':personal' / ':global';
this.once = false;
this.optional = false;
this.value = new GrammarQuotedString;
}
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 GrammarQuotedString) {
this.value = arg;
}
});
}
}
export class ReturnCommand extends GrammarCommand
{
get require() { return 'include'; }
}