From c4123b21141cb7bd3165914c2c9eab8b2e0c1f9d Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Mon, 25 Aug 2008 11:55:55 +0200
Subject: [PATCH] Cleaned up variables extension.

---
 src/lib-sieve/plugins/variables/cmd-set.c     |  3 ++
 .../variables/ext-variables-arguments.c       | 20 ++++++-
 .../variables/ext-variables-arguments.h       |  3 ++
 .../plugins/variables/ext-variables-common.c  | 35 ++++--------
 .../plugins/variables/ext-variables-common.h  | 54 ++++++++++++++-----
 .../plugins/variables/ext-variables-limits.h  |  3 ++
 .../variables/ext-variables-modifiers.c       | 16 +++++-
 .../variables/ext-variables-modifiers.h       | 14 +++--
 .../plugins/variables/ext-variables-name.c    |  3 ++
 .../plugins/variables/ext-variables-name.h    | 13 ++++-
 .../variables/ext-variables-operands.c        |  3 ++
 .../variables/ext-variables-operands.h        |  3 ++
 .../plugins/variables/ext-variables.c         | 43 +++++++--------
 .../plugins/variables/sieve-ext-variables.h   | 18 +++++--
 src/lib-sieve/plugins/variables/tst-string.c  |  3 ++
 15 files changed, 155 insertions(+), 79 deletions(-)

diff --git a/src/lib-sieve/plugins/variables/cmd-set.c b/src/lib-sieve/plugins/variables/cmd-set.c
index 0d58a7ed3..ade0eb358 100644
--- a/src/lib-sieve/plugins/variables/cmd-set.c
+++ b/src/lib-sieve/plugins/variables/cmd-set.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include "lib.h"
 #include "str.h"
 #include "array.h"
diff --git a/src/lib-sieve/plugins/variables/ext-variables-arguments.c b/src/lib-sieve/plugins/variables/ext-variables-arguments.c
index d686b7de1..194382ec8 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-arguments.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-arguments.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include "lib.h"
 #include "str.h"
 #include "str-sanitize.h"
@@ -92,15 +95,20 @@ static bool _sieve_variable_argument_activate
 		varstr = str_c(variable);
 		varend = PTR_OFFSET(varstr, str_len(variable));
 		nelements = ext_variable_name_parse(&vname, &varstr, varend);
-	
+
+		/* Check whether name parsing succeeded */	
 		if ( nelements < 0 || varstr != varend ) {
+			/* Parse failed */
 			sieve_command_validate_error(validator, cmd, 
 				"invalid variable name '%s'", str_sanitize(str_c(variable),80));
 		} else if ( nelements == 1 ) {
+			/* Normal (match) variable */
+
 			const struct ext_variable_name *cur_element = 
 				array_idx(&vname, 0);
 
 			if ( cur_element->num_variable < 0 ) {
+				/* Variable */
 				var = ext_variables_validator_get_variable
 					(validator, str_c(cur_element->identifier), TRUE);
 
@@ -114,6 +122,7 @@ static bool _sieve_variable_argument_activate
 					result = TRUE;
 				}
 			} else {
+				/* Match value */
 				if ( !assignment ) {
 					if ( cur_element->num_variable > SIEVE_VARIABLES_MAX_MATCH_INDEX ) {
 						_ext_variables_match_index_error
@@ -130,6 +139,8 @@ static bool _sieve_variable_argument_activate
 				}
 			}
 		} else {
+			/* Namespace variable */
+
 			const struct ext_variable_name *cur_element = 
 				array_idx(&vname, 0);
 
@@ -288,6 +299,7 @@ static bool arg_variable_string_validate
 		strstart = p;
 		while ( result && p < strend ) {
 			switch ( state ) {
+
 			/* Nothing found yet */
 			case ST_NONE:
 				if ( *p == '$' ) {
@@ -296,6 +308,7 @@ static bool arg_variable_string_validate
 				}
 				p++;
 				break;
+
 			/* Got '$' */
 			case ST_OPEN:
 				if ( *p == '{' ) {
@@ -304,6 +317,7 @@ static bool arg_variable_string_validate
 				} else 
 					state = ST_NONE;
 				break;
+
 			/* Got '${' */ 
 			case ST_VARIABLE:
 				nelements = ext_variable_name_parse(&substitution, &p, strend);
@@ -314,6 +328,8 @@ static bool arg_variable_string_validate
 					state = ST_CLOSE;
 			
 				break;
+
+			/* Finished parsing name, expecting '}' */
 			case ST_CLOSE:
 				if ( *p == '}' ) {				
 					struct sieve_ast_argument *strarg;
@@ -405,8 +421,10 @@ static bool arg_variable_string_validate
 		}
 	} T_END;
 
+	/* Bail out early if substitution is invalid */
 	if ( !result ) return FALSE;
 	
+	/* Check whether any substitutions were found */
 	if ( arglist == NULL ) {
 		/* No substitutions in this string, pass it on to any other substution
 		 * extension.
diff --git a/src/lib-sieve/plugins/variables/ext-variables-arguments.h b/src/lib-sieve/plugins/variables/ext-variables-arguments.h
index beaf8ee65..ceb669234 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-arguments.h
+++ b/src/lib-sieve/plugins/variables/ext-variables-arguments.h
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #ifndef __EXT_VARIABLES_ARGUMENTS_H
 #define __EXT_VARIABLES_ARGUMENTS_H
 
diff --git a/src/lib-sieve/plugins/variables/ext-variables-common.c b/src/lib-sieve/plugins/variables/ext-variables-common.c
index d1969ffee..cf8c04f93 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-common.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-common.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include "lib.h"
 #include "hash.h"
 #include "str.h"
@@ -21,27 +24,6 @@
 #include "ext-variables-name.h"
 #include "ext-variables-modifiers.h"
 
-/* 
- * Forward declarations 
- */
-
-/* Core modifiers */
-
-extern const struct ext_variables_set_modifier lower_modifier;
-extern const struct ext_variables_set_modifier upper_modifier;
-extern const struct ext_variables_set_modifier lowerfirst_modifier;
-extern const struct ext_variables_set_modifier upperfirst_modifier;
-extern const struct ext_variables_set_modifier quotewildcard_modifier;
-extern const struct ext_variables_set_modifier length_modifier;
-
-const struct ext_variables_set_modifier *default_set_modifiers[] = { 
-	&lower_modifier, &upper_modifier, &lowerfirst_modifier, &upperfirst_modifier,
-	&quotewildcard_modifier, &length_modifier
-};
-
-const unsigned int default_set_modifiers_count = 
-	N_ELEMENTS(default_set_modifiers);
-
 /*
  * Variable scope 
  */
@@ -209,8 +191,9 @@ struct sieve_variable * const *sieve_variable_scope_get_variables
 	return array_get(&scope->variable_index, size_r);
 }
 
-
-/* Variable storage */
+/* 
+ * Variable storage 
+ */
 
 struct sieve_variable_storage {
 	pool_t pool;
@@ -314,8 +297,6 @@ bool sieve_variable_assign
 	return TRUE;
 }
 
-
-
 /*
  * AST Context
  */
@@ -401,7 +382,9 @@ bool sieve_ext_variables_is_active(struct sieve_validator *valdtr)
 	return ( ext_variables_validator_context_get(valdtr) != NULL );
 }
 
-/* Interpreter context */
+/* 
+ * Interpreter context 
+ */
 
 struct ext_variables_interpreter_context {
 	struct sieve_variable_storage *local_storage;
diff --git a/src/lib-sieve/plugins/variables/ext-variables-common.h b/src/lib-sieve/plugins/variables/ext-variables-common.h
index 13b6b8132..6108abf5e 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-common.h
+++ b/src/lib-sieve/plugins/variables/ext-variables-common.h
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #ifndef __EXT_VARIABLES_COMMON_H
 #define __EXT_VARIABLES_COMMON_H
 
@@ -6,15 +9,36 @@
 
 #include "sieve-ext-variables.h"
 
-extern int ext_variables_my_id;
+/*
+ * Extension
+ */
 
 extern struct sieve_extension variables_extension;
 
+/* 
+ * Commands 
+ */
+
+extern const struct sieve_command cmd_set;
+extern const struct sieve_command tst_string;
+
+/*
+ * Operations
+ */
+
+extern const struct sieve_operation cmd_set_operation;
+extern const struct sieve_operation tst_string_operation;
+
 enum ext_variables_opcode {
 	EXT_VARIABLES_OPERATION_SET,
 	EXT_VARIABLES_OPERATION_STRING
 };
 
+/* 
+ * Operands
+ */
+
+
 enum ext_variables_operand {
 	EXT_VARIABLES_OPERAND_VARIABLE,
 	EXT_VARIABLES_OPERAND_MATCH_VALUE,
@@ -22,7 +46,9 @@ enum ext_variables_operand {
 	EXT_VARIABLES_OPERAND_MODIFIER
 };
 
-/* Context */
+/* 
+ * Validator context 
+ */
 
 struct ext_variables_validator_context {
 	struct sieve_validator_object_registry *modifiers;
@@ -31,16 +57,26 @@ struct ext_variables_validator_context {
 };
 
 void ext_variables_validator_initialize(struct sieve_validator *validator);
-void ext_variables_interpreter_initialize(struct sieve_interpreter *interp);
 	
 static inline struct ext_variables_validator_context *
 ext_variables_validator_context_get(struct sieve_validator *valdtr)
 {
 	return (struct ext_variables_validator_context *)
 		sieve_validator_extension_get_context(valdtr, &variables_extension);
-}	
-	
-/* Variables */
+}
+
+struct sieve_variable *ext_variables_validator_get_variable
+	(struct sieve_validator *validator, const char *variable, bool declare);
+
+/*
+ * Interpreter context
+ */	
+
+void ext_variables_interpreter_initialize(struct sieve_interpreter *interp);
+
+/* 
+ * Variable coding 
+ */
 
 void ext_variables_opr_variable_emit
 	(struct sieve_binary *sbin, struct sieve_variable *var);
@@ -56,11 +92,5 @@ void ext_variables_opr_variable_string_emit
 bool ext_variables_variable_assignment_activate
 (struct sieve_validator *validator, struct sieve_ast_argument *arg,
 	struct sieve_command_context *cmd);
-
-struct sieve_variable *ext_variables_validator_get_variable
-(struct sieve_validator *validator, const char *variable, bool declare);
-
-struct sieve_variable_storage *ext_variables_interpreter_get_storage
-	(struct sieve_interpreter *interp, const struct sieve_extension *ext);
 	
 #endif /* __EXT_VARIABLES_COMMON_H */
diff --git a/src/lib-sieve/plugins/variables/ext-variables-limits.h b/src/lib-sieve/plugins/variables/ext-variables-limits.h
index 5e9b227d4..abe07c843 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-limits.h
+++ b/src/lib-sieve/plugins/variables/ext-variables-limits.h
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #ifndef __EXT_VARIABLES_LIMITS_H
 #define __EXT_VARIABLES_LIMITS_H
 
diff --git a/src/lib-sieve/plugins/variables/ext-variables-modifiers.c b/src/lib-sieve/plugins/variables/ext-variables-modifiers.c
index 72fd58c1c..f56341aea 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-modifiers.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-modifiers.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include "sieve-common.h"
 #include "sieve-commands.h"
 #include "sieve-code.h"
@@ -19,6 +22,15 @@ extern const struct sieve_variables_modifier upperfirst_modifier;
 extern const struct sieve_variables_modifier quotewildcard_modifier;
 extern const struct sieve_variables_modifier length_modifier;
 
+enum ext_variables_modifier_code {
+    EXT_VARIABLES_MODIFIER_LOWER,
+    EXT_VARIABLES_MODIFIER_UPPER,
+    EXT_VARIABLES_MODIFIER_LOWERFIRST,
+    EXT_VARIABLES_MODIFIER_UPPERFIRST,
+    EXT_VARIABLES_MODIFIER_QUOTEWILDCARD,
+    EXT_VARIABLES_MODIFIER_LENGTH
+};
+
 const struct sieve_variables_modifier *ext_variables_core_modifiers[] = {
 	&lower_modifier,
 	&upper_modifier,
@@ -32,7 +44,7 @@ const unsigned int ext_variables_core_modifiers_count =
     N_ELEMENTS(ext_variables_core_modifiers);
 
 /*
- * Set modifier registry
+ * Modifier registry
  */
 
 void sieve_variables_modifier_register
@@ -69,7 +81,7 @@ void ext_variables_register_core_modifiers
 }
 
 /*
- * Set modifier coding
+ * Modifier coding
  */
  
 const struct sieve_operand_class ext_variables_modifier_operand_class = 
diff --git a/src/lib-sieve/plugins/variables/ext-variables-modifiers.h b/src/lib-sieve/plugins/variables/ext-variables-modifiers.h
index c47db35b8..38f62f1fe 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-modifiers.h
+++ b/src/lib-sieve/plugins/variables/ext-variables-modifiers.h
@@ -1,17 +1,15 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #ifndef __EXT_VARIABLES_MODIFIERS_H
 #define __EXT_VARIABLES_MODIFIERS_H
 
 #include "ext-variables-common.h"
 #include "sieve-ext-variables.h"
 
-enum ext_variables_modifier_code {
-	EXT_VARIABLES_MODIFIER_LOWER,
-	EXT_VARIABLES_MODIFIER_UPPER,
-	EXT_VARIABLES_MODIFIER_LOWERFIRST,
-	EXT_VARIABLES_MODIFIER_UPPERFIRST,
-	EXT_VARIABLES_MODIFIER_QUOTEWILDCARD,
-	EXT_VARIABLES_MODIFIER_LENGTH
-};
+/*
+ * Modifier registry
+ */
 
 const struct sieve_variables_modifier *ext_variables_modifier_find
 	(struct sieve_validator *validator, const char *identifier);
diff --git a/src/lib-sieve/plugins/variables/ext-variables-name.c b/src/lib-sieve/plugins/variables/ext-variables-name.c
index 2419913a1..9a829c5b9 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-name.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-name.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include "lib.h"
 #include "str.h"
 #include "array.h"
diff --git a/src/lib-sieve/plugins/variables/ext-variables-name.h b/src/lib-sieve/plugins/variables/ext-variables-name.h
index 7102b56cb..18e10ff83 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-name.h
+++ b/src/lib-sieve/plugins/variables/ext-variables-name.h
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #ifndef __EXT_VARIABLES_NAME
 #define __EXT_VARIABLES_NAME
 
@@ -32,13 +35,21 @@
 #include "sieve-common.h"
 
 #include "ext-variables-common.h"
- 
+
+/*
+ * Types
+ */ 
+
 struct ext_variable_name {
 	string_t *identifier;
 	int num_variable;
 };
 
 ARRAY_DEFINE_TYPE(ext_variable_name, struct ext_variable_name);
+
+/*
+ * Variable name parsing
+ */
  
 int ext_variable_name_parse
 	(ARRAY_TYPE(ext_variable_name) *vname, const char **str, const char *strend);
diff --git a/src/lib-sieve/plugins/variables/ext-variables-operands.c b/src/lib-sieve/plugins/variables/ext-variables-operands.c
index cc1d82bfa..4799523b6 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-operands.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-operands.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include "lib.h"
 #include "hash.h"
 #include "str.h"
diff --git a/src/lib-sieve/plugins/variables/ext-variables-operands.h b/src/lib-sieve/plugins/variables/ext-variables-operands.h
index 202866a05..aa3e5c1fb 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-operands.h
+++ b/src/lib-sieve/plugins/variables/ext-variables-operands.h
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #ifndef __EXT_VARIABLES_OPERANDS_H
 #define __EXT_VARIABLES_OPERANDS_H
 
diff --git a/src/lib-sieve/plugins/variables/ext-variables.c b/src/lib-sieve/plugins/variables/ext-variables.c
index a4da1b35a..8a94a0364 100644
--- a/src/lib-sieve/plugins/variables/ext-variables.c
+++ b/src/lib-sieve/plugins/variables/ext-variables.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 /* Extension variables 
  * -------------------
  *
@@ -30,28 +33,18 @@
 #include "ext-variables-operands.h"
 #include "ext-variables-modifiers.h"
 
-/* Forward declarations */
-
-static bool ext_variables_load(int ext_id);
-static bool ext_variables_validator_load(struct sieve_validator *validator);
-static bool ext_variables_interpreter_load(struct sieve_interpreter *interp);
-
-/* Commands */
-
-extern const struct sieve_command cmd_set;
-extern const struct sieve_command tst_string;
-
-/* Operations */
-
-extern const struct sieve_operation cmd_set_operation;
-extern const struct sieve_operation tst_string_operation;
+/* 
+ * Operations 
+ */
 
 const struct sieve_operation *ext_variables_operations[] = {
 	&cmd_set_operation, 
 	&tst_string_operation
 };
 
-/* Operands */
+/* 
+ * Operands 
+ */
 
 const struct sieve_operand *ext_variables_operands[] = {
 	&variable_operand, 
@@ -60,13 +53,19 @@ const struct sieve_operand *ext_variables_operands[] = {
 	&modifier_operand
 };
 
-/* Extension definitions */
+/* 
+ * Extension 
+ */
 
-int ext_variables_my_id;
+static bool ext_variables_load(int ext_id);
+static bool ext_variables_validator_load(struct sieve_validator *validator);
+static bool ext_variables_interpreter_load(struct sieve_interpreter *interp);
+
+static int ext_my_id;
 	
 struct sieve_extension variables_extension = { 
 	"variables", 
-	&ext_variables_my_id,
+	&ext_my_id,
 	ext_variables_load,
 	ext_variables_validator_load, 
 	NULL,
@@ -78,12 +77,10 @@ struct sieve_extension variables_extension = {
 
 static bool ext_variables_load(int ext_id) 
 {
-	ext_variables_my_id = ext_id;
+	ext_my_id = ext_id;
 	return TRUE;
 }
 
-/* Load extension into validator */
-
 static bool ext_variables_validator_load
 	(struct sieve_validator *validator)
 {
@@ -98,8 +95,6 @@ static bool ext_variables_validator_load
 	return TRUE;
 }
 
-/* Load extension into interpreter */
-
 static bool ext_variables_interpreter_load
 	(struct sieve_interpreter *interp)
 {
diff --git a/src/lib-sieve/plugins/variables/sieve-ext-variables.h b/src/lib-sieve/plugins/variables/sieve-ext-variables.h
index 8824c044b..f46f697de 100644
--- a/src/lib-sieve/plugins/variables/sieve-ext-variables.h
+++ b/src/lib-sieve/plugins/variables/sieve-ext-variables.h
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 /* 
  * Public interface for other extensions to use 
  */
@@ -64,7 +67,6 @@ unsigned int sieve_variable_scope_size
 struct sieve_variable * const *sieve_variable_scope_get_variables
 	(struct sieve_variable_scope *scope, unsigned int *size_r);
 
-
 /* 
  * Variable storage
  */	
@@ -102,13 +104,17 @@ void sieve_ext_variables_set_storage
 	(struct sieve_interpreter *interp, struct sieve_variable_storage *storage,
 		const struct sieve_extension *ext);	
 		
-/* Variable arguments */
+/* 
+ * Variable arguments 
+ */
 
 bool sieve_variable_argument_activate
 (struct sieve_validator *validator, struct sieve_command_context *cmd, 
 	struct sieve_ast_argument *arg, bool assignment);
 	
-/* Variable operands */
+/* 
+ * Variable operands 
+ */
 
 extern const struct sieve_operand variable_operand;
 
@@ -126,7 +132,9 @@ static inline bool sieve_operand_is_variable
 	return ( operand != NULL && operand == &variable_operand );
 }	
 
-/* Modifiers */
+/* 
+ * Modifiers 
+ */
 
 struct sieve_variables_modifier {
 	struct sieve_object object;
@@ -140,6 +148,6 @@ struct sieve_variables_modifier {
 #define SIEVE_VARIABLES_DEFINE_MODIFIERS(OPS) SIEVE_EXT_DEFINE_OBJECTS(OPS)
 
 void sieve_variables_modifier_register
-(struct sieve_validator *valdtr, const struct sieve_variables_modifier *smodf);
+	(struct sieve_validator *valdtr, const struct sieve_variables_modifier *smodf);
 
 #endif /* __SIEVE_EXT_VARIABLES_H */
diff --git a/src/lib-sieve/plugins/variables/tst-string.c b/src/lib-sieve/plugins/variables/tst-string.c
index dfe4c8cb8..149c7c752 100644
--- a/src/lib-sieve/plugins/variables/tst-string.c
+++ b/src/lib-sieve/plugins/variables/tst-string.c
@@ -1,3 +1,6 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
 #include <stdio.h>
 
 #include "sieve-common.h"
-- 
GitLab