From ec61c8eaad33162545bae2e602c56bba20b4d90a Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sat, 24 Nov 2007 17:35:48 +0100
Subject: [PATCH] Added binary_load event to the sieve extensions.

---
 src/lib-sieve/ext-envelope.c                  |  3 +-
 src/lib-sieve/ext-fileinto.c                  |  3 +-
 src/lib-sieve/ext-reject.c                    |  3 +-
 .../ext-cmp-i-ascii-numeric.c                 |  2 +-
 .../plugins/imapflags/ext-imapflags.c         |  2 +-
 src/lib-sieve/plugins/regex/ext-regex.c       |  2 +-
 .../plugins/relational/ext-relational.c       |  2 +-
 .../plugins/subaddress/ext-subaddress.c       |  2 +-
 src/lib-sieve/plugins/vacation/ext-vacation.c |  3 +-
 src/lib-sieve/sieve-address-parts.c           |  4 +--
 src/lib-sieve/sieve-binary.c                  | 35 ++++++++++++-------
 src/lib-sieve/sieve-binary.h                  |  6 ++--
 src/lib-sieve/sieve-comparators.c             |  4 +--
 src/lib-sieve/sieve-extensions.c              |  4 +--
 src/lib-sieve/sieve-extensions.h              |  4 ++-
 src/lib-sieve/sieve-interpreter.c             |  2 +-
 src/lib-sieve/sieve-match-types.c             |  4 +--
 17 files changed, 47 insertions(+), 38 deletions(-)

diff --git a/src/lib-sieve/ext-envelope.c b/src/lib-sieve/ext-envelope.c
index dad3a8616..ac962a720 100644
--- a/src/lib-sieve/ext-envelope.c
+++ b/src/lib-sieve/ext-envelope.c
@@ -52,8 +52,7 @@ const struct sieve_extension envelope_extension = {
 	"envelope", 
 	ext_envelope_load,
 	ext_envelope_validator_load, 
-	NULL, 
-	NULL, 
+	NULL, NULL, NULL, 
 	SIEVE_EXT_DEFINE_OPCODE(envelope_opcode), 
 	NULL 
 };
diff --git a/src/lib-sieve/ext-fileinto.c b/src/lib-sieve/ext-fileinto.c
index 5cb29aa4a..d86c638f1 100644
--- a/src/lib-sieve/ext-fileinto.c
+++ b/src/lib-sieve/ext-fileinto.c
@@ -44,8 +44,7 @@ const struct sieve_extension fileinto_extension = {
 	"fileinto", 
 	ext_fileinto_load,
 	ext_fileinto_validator_load, 
-	NULL, 
-	NULL, 
+	NULL, NULL, NULL,
 	SIEVE_EXT_DEFINE_OPCODE(fileinto_opcode), 
 	NULL	
 };
diff --git a/src/lib-sieve/ext-reject.c b/src/lib-sieve/ext-reject.c
index 509481b20..ff7af5004 100644
--- a/src/lib-sieve/ext-reject.c
+++ b/src/lib-sieve/ext-reject.c
@@ -43,8 +43,7 @@ struct sieve_extension reject_extension = {
 	"reject", 
 	ext_reject_load,
 	ext_reject_validator_load, 
-	NULL, 
-	NULL, 
+	NULL, NULL, NULL, 
 	SIEVE_EXT_DEFINE_OPCODE(reject_opcode), 
 	NULL 
 };
diff --git a/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c b/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c
index c9a3a88e6..a8944b3d1 100644
--- a/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c
+++ b/src/lib-sieve/plugins/comparator-i-ascii-numeric/ext-cmp-i-ascii-numeric.c
@@ -34,7 +34,7 @@ const struct sieve_extension comparator_i_ascii_numeric_extension = {
 	"comparator-i;ascii-numeric", 
 	ext_cmp_i_ascii_numeric_load,
 	ext_cmp_i_ascii_numeric_validator_load,
-	NULL, 
+	NULL, NULL,
 	ext_cmp_i_ascii_numeric_interpreter_load,  
 	SIEVE_EXT_DEFINE_NO_OPCODES, 
 	NULL
diff --git a/src/lib-sieve/plugins/imapflags/ext-imapflags.c b/src/lib-sieve/plugins/imapflags/ext-imapflags.c
index fa7ba2f90..04361bd36 100644
--- a/src/lib-sieve/plugins/imapflags/ext-imapflags.c
+++ b/src/lib-sieve/plugins/imapflags/ext-imapflags.c
@@ -57,7 +57,7 @@ const struct sieve_extension imapflags_extension = {
 	"imap4flags", 
 	ext_imapflags_load,
 	ext_imapflags_validator_load, 
-	NULL, 
+	NULL, NULL,
 	ext_imapflags_interpreter_load, 
 	SIEVE_EXT_DEFINE_OPCODES(imapflags_opcodes), 
 	NULL
diff --git a/src/lib-sieve/plugins/regex/ext-regex.c b/src/lib-sieve/plugins/regex/ext-regex.c
index 242d0400f..3e66ba991 100644
--- a/src/lib-sieve/plugins/regex/ext-regex.c
+++ b/src/lib-sieve/plugins/regex/ext-regex.c
@@ -56,7 +56,7 @@ const struct sieve_extension regex_extension = {
 	"regex", 
 	ext_regex_load,
 	ext_regex_validator_load,
-	NULL, 
+	NULL, NULL,
 	ext_regex_interpreter_load,  
 	SIEVE_EXT_DEFINE_NO_OPCODES, 
 	NULL
diff --git a/src/lib-sieve/plugins/relational/ext-relational.c b/src/lib-sieve/plugins/relational/ext-relational.c
index ea5724dda..4860009a0 100644
--- a/src/lib-sieve/plugins/relational/ext-relational.c
+++ b/src/lib-sieve/plugins/relational/ext-relational.c
@@ -70,7 +70,7 @@ const struct sieve_extension relational_extension = {
 	"relational", 
 	ext_relational_load,
 	ext_relational_validator_load,
-	NULL, 
+	NULL, NULL,
 	ext_relational_interpreter_load,  
 	SIEVE_EXT_DEFINE_NO_OPCODES, 
 	NULL
diff --git a/src/lib-sieve/plugins/subaddress/ext-subaddress.c b/src/lib-sieve/plugins/subaddress/ext-subaddress.c
index 8e1d6de24..cdb73012d 100644
--- a/src/lib-sieve/plugins/subaddress/ext-subaddress.c
+++ b/src/lib-sieve/plugins/subaddress/ext-subaddress.c
@@ -42,7 +42,7 @@ const struct sieve_extension subaddress_extension = {
 	"subaddress", 
 	ext_subaddress_load,
 	ext_subaddress_validator_load,
-	NULL, 
+	NULL, NULL,
 	ext_subaddress_interpreter_load,  
 	SIEVE_EXT_DEFINE_NO_OPCODES, 
 	NULL
diff --git a/src/lib-sieve/plugins/vacation/ext-vacation.c b/src/lib-sieve/plugins/vacation/ext-vacation.c
index 3da21bc8f..3cf0b47ac 100644
--- a/src/lib-sieve/plugins/vacation/ext-vacation.c
+++ b/src/lib-sieve/plugins/vacation/ext-vacation.c
@@ -49,8 +49,7 @@ const struct sieve_extension vacation_extension = {
 	"vacation", 
 	ext_vacation_load,
 	ext_vacation_validator_load, 
-	NULL, 
-	NULL, 
+	NULL, NULL, NULL, 
 	SIEVE_EXT_DEFINE_OPCODE(vacation_opcode),
 	NULL
 };
diff --git a/src/lib-sieve/sieve-address-parts.c b/src/lib-sieve/sieve-address-parts.c
index 6274a99d7..bc347ab66 100644
--- a/src/lib-sieve/sieve-address-parts.c
+++ b/src/lib-sieve/sieve-address-parts.c
@@ -40,10 +40,10 @@ static bool addrp_validator_load(struct sieve_validator *validator);
 static bool addrp_interpreter_load(struct sieve_interpreter *interp);
 
 const struct sieve_extension address_part_extension = {
-	"@address-part",
+	"@address-parts",
 	addrp_extension_load,
 	addrp_validator_load,
-	NULL,
+	NULL, NULL,
 	addrp_interpreter_load,
 	SIEVE_EXT_DEFINE_NO_OPCODES,
 	NULL
diff --git a/src/lib-sieve/sieve-binary.c b/src/lib-sieve/sieve-binary.c
index 1ba09a628..251783e5e 100644
--- a/src/lib-sieve/sieve-binary.c
+++ b/src/lib-sieve/sieve-binary.c
@@ -38,33 +38,45 @@ struct sieve_binary *sieve_binary_create_new(void)
 	
 	sbin->data = buffer_create_dynamic(pool, 256);
 	
-	p_array_init(&sbin->extensions, pool, sieve_extensions_get_count());
+	p_array_init(&sbin->extensions, pool, 5);
 	p_array_init(&sbin->extension_index, pool, sieve_extensions_get_count());
 		
 	return sbin;
 }
 
-void sieve_binary_ref(struct sieve_binary *binary) 
+void sieve_binary_ref(struct sieve_binary *sbin) 
 {
-	pool_ref(binary->pool);
+	pool_ref(sbin->pool);
 }
 
-void sieve_binary_unref(struct sieve_binary **binary) 
+void sieve_binary_unref(struct sieve_binary **sbin) 
 {
-	if ( binary != NULL && *binary != NULL ) {
-		pool_unref(&((*binary)->pool));
+	if ( sbin != NULL && *sbin != NULL ) {
+		pool_unref(&((*sbin)->pool));
 	}
-	*binary = NULL;
+	*sbin = NULL;
 }
 
-inline sieve_size_t sieve_binary_get_code_size(struct sieve_binary *binary)
+inline sieve_size_t sieve_binary_get_code_size(struct sieve_binary *sbin)
 {
-	return buffer_get_used_size(binary->data);
+	return buffer_get_used_size(sbin->data);
 }
 
-void sieve_binary_commit(struct sieve_binary *binary)
+void sieve_binary_load(struct sieve_binary *sbin)
 {
-	binary->code = buffer_get_data(binary->data, &(binary->code_size));			
+	unsigned int i;
+	
+	/* Currently only memory binary support */
+	sbin->code = buffer_get_data(sbin->data, &(sbin->code_size));			
+	
+	for ( i = 0; i < array_count(&sbin->extensions); i++ ) {
+		struct sieve_binary_extension * const *aext = 
+			array_idx(&sbin->extensions, i);
+		const struct sieve_extension *ext = (*aext)->extension;
+		
+		if ( ext->binary_load != NULL )
+			ext->binary_load(sbin);
+	}
 }
 
 /* Extension handling */
@@ -128,7 +140,6 @@ int sieve_binary_extensions_count(struct sieve_binary *sbin)
 	return (int) array_count(&sbin->extensions);
 }
 
-
 /*
  * Emission functions
  */
diff --git a/src/lib-sieve/sieve-binary.h b/src/lib-sieve/sieve-binary.h
index 49440d1ce..174a7c3e3 100644
--- a/src/lib-sieve/sieve-binary.h
+++ b/src/lib-sieve/sieve-binary.h
@@ -9,10 +9,10 @@
 struct sieve_binary;
 
 struct sieve_binary *sieve_binary_create_new(void);
-void sieve_binary_ref(struct sieve_binary *binary);
-void sieve_binary_unref(struct sieve_binary **binary);
+void sieve_binary_ref(struct sieve_binary *sbin);
+void sieve_binary_unref(struct sieve_binary **sbin);
 
-void sieve_binary_commit(struct sieve_binary *binary);
+void sieve_binary_load(struct sieve_binary *sbin);
 
 /* 
  * Extension handling 
diff --git a/src/lib-sieve/sieve-comparators.c b/src/lib-sieve/sieve-comparators.c
index 7cb799515..321553fa6 100644
--- a/src/lib-sieve/sieve-comparators.c
+++ b/src/lib-sieve/sieve-comparators.c
@@ -55,10 +55,10 @@ static bool cmp_validator_load(struct sieve_validator *validator);
 static bool cmp_interpreter_load(struct sieve_interpreter *interp);
 
 const struct sieve_extension comparator_extension = {
-	"@comparator",
+	"@comparators",
 	cmp_extension_load,
 	cmp_validator_load,
-	NULL,
+	NULL, NULL,
 	cmp_interpreter_load,
 	SIEVE_EXT_DEFINE_NO_OPCODES,
 	NULL
diff --git a/src/lib-sieve/sieve-extensions.c b/src/lib-sieve/sieve-extensions.c
index aefccd4b0..f5345b9d9 100644
--- a/src/lib-sieve/sieve-extensions.c
+++ b/src/lib-sieve/sieve-extensions.c
@@ -27,14 +27,14 @@ const unsigned int sieve_preloaded_extensions_count =
 
 static const struct sieve_extension comparator_i_octet_extension = {
 	"comparator-i;octet",
-	NULL, NULL, NULL, NULL, 
+	NULL, NULL, NULL, NULL, NULL,
 	SIEVE_EXT_DEFINE_NO_OPCODES, 
 	NULL
 };
 
 static const struct sieve_extension comparator_i_ascii_casemap_extension = {
 	"comparator-i;ascii-casemap",
-	NULL, NULL, NULL, NULL, 
+	NULL, NULL, NULL, NULL, NULL,
 	SIEVE_EXT_DEFINE_NO_OPCODES, 
 	NULL
 };
diff --git a/src/lib-sieve/sieve-extensions.h b/src/lib-sieve/sieve-extensions.h
index 16f97a96c..346835b2d 100644
--- a/src/lib-sieve/sieve-extensions.h
+++ b/src/lib-sieve/sieve-extensions.h
@@ -8,9 +8,11 @@ struct sieve_extension {
 	const char *name;
 	
 	bool (*load)(int ext_id);
-	
+
 	bool (*validator_load)(struct sieve_validator *validator);	
 	bool (*generator_load)(struct sieve_generator *generator);
+
+	bool (*binary_load)(struct sieve_binary *binary);
 	bool (*interpreter_load)(struct sieve_interpreter *interpreter);
 
 	/* Extension can introduce a single or multiple opcodes */
diff --git a/src/lib-sieve/sieve-interpreter.c b/src/lib-sieve/sieve-interpreter.c
index 9033e6ba9..e45995b3a 100644
--- a/src/lib-sieve/sieve-interpreter.c
+++ b/src/lib-sieve/sieve-interpreter.c
@@ -46,7 +46,7 @@ struct sieve_interpreter *sieve_interpreter_create(struct sieve_binary *sbin)
 	
 	interp->runenv.sbin = sbin;
 	sieve_binary_ref(sbin);
-	sieve_binary_commit(sbin);
+	sieve_binary_load(sbin);
 	
 	interp->pc = 0;
 
diff --git a/src/lib-sieve/sieve-match-types.c b/src/lib-sieve/sieve-match-types.c
index 3b7865d22..19e0e9c26 100644
--- a/src/lib-sieve/sieve-match-types.c
+++ b/src/lib-sieve/sieve-match-types.c
@@ -39,10 +39,10 @@ static bool mtch_validator_load(struct sieve_validator *validator);
 static bool mtch_interpreter_load(struct sieve_interpreter *interp);
 
 const struct sieve_extension match_type_extension = {
-	"@match-type",
+	"@match-types",
 	mtch_extension_load,
 	mtch_validator_load,
-	NULL,
+	NULL, NULL,
 	mtch_interpreter_load,
 	SIEVE_EXT_DEFINE_NO_OPCODES,
 	NULL
-- 
GitLab