Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 257d920

Browse files
author
the-djmaze
committed
Improved Sieve parser validation
1 parent c773b85 commit 257d920

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

dev/Sieve/Grammar.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ export class GrammarString /*extends String*/
4545
/**
4646
* abstract
4747
*/
48-
export class GrammarComment extends GrammarString
48+
export /*abstract*/ class GrammarComment extends GrammarString
4949
{
50+
/*
51+
constructor()
52+
{
53+
if (this.constructor == GrammarComment) {
54+
throw Error("Abstract class can't be instantiated.");
55+
}
56+
}
57+
*/
5058
}
5159

5260
/**

dev/Sieve/Parser.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import {
1919
GrammarBracketComment,
2020
GrammarCommand,
21+
GrammarComment,
2122
GrammarHashComment,
2223
GrammarMultiLine,
2324
GrammarNumber,
@@ -28,7 +29,7 @@ import {
2829
} from 'Sieve/Grammar';
2930

3031
import { availableCommands } from 'Sieve/Commands';
31-
import { ConditionalCommand, RequireCommand } from 'Sieve/Commands/Controls';
32+
import { ConditionalCommand, IfCommand, RequireCommand } from 'Sieve/Commands/Controls';
3233
import { NotTest } from 'Sieve/Commands/Tests';
3334

3435
const
@@ -156,10 +157,17 @@ export const parseScript = (script, name = 'script.sieve') => {
156157
let new_command;
157158
if (Commands[value]) {
158159
if ('elsif' === value || 'else' === value) {
159-
// (prev_command instanceof ConditionalCommand) || error('Not after IF condition');
160-
}
161-
if ('allof' === value || 'anyof' === value) {
162-
// (command instanceof ConditionalCommand || command instanceof NotTest) || error('Test-list not in conditional');
160+
let valid = false, cmd = (command ? command?.commands : tree), i = cmd?.length;
161+
while (i) {
162+
cmd[--i];
163+
if (cmd[i] instanceof IfCommand) {
164+
valid = true;
165+
break;
166+
} else if (typeof cmd[i] !== 'string' && !(cmd[i] instanceof GrammarComment)) {
167+
break;
168+
}
169+
}
170+
valid || error('Not after IF/ELSIF condition');
163171
}
164172
new_command = new Commands[value]();
165173
} else {
@@ -266,7 +274,6 @@ export const parseScript = (script, name = 'script.sieve') => {
266274
break;
267275
case T_BLOCK_END:
268276
(command instanceof ConditionalCommand) || error(TokenError[type]);
269-
// prev_command = command;
270277
command = levels.up();
271278
break;
272279

snappymail/v/0.0.0/app/templates/Views/User/PopupsSieveScript.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h3>
1111

1212
<div class="control-group" data-bind="css: {'error': nameError}, hidden: exists">
1313
<input type="text" class="span5"
14-
data-bind="value: name, hasfocus: !exists()"
14+
data-bind="value: name"
1515
autocorrect="off" autocapitalize="off"
1616
data-i18n="[placeholder]GLOBAL/NAME">
1717
</div>

0 commit comments

Comments
 (0)