From b4e7dc746fca9b7164004466ed771c12611e2e99 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Fri, 16 Jan 2009 19:56:15 +0100
Subject: [PATCH] Enotify: cleaned up method API.

---
 TODO                                          |  6 +--
 src/lib-sieve/plugins/enotify/cmd-notify.c    | 10 +++-
 .../plugins/enotify/ext-enotify-common.c      | 14 +++++
 .../plugins/enotify/ext-enotify-common.h      |  8 +++
 src/lib-sieve/plugins/enotify/ntfy-mailto.c   | 53 ++++++++++++-------
 .../plugins/enotify/sieve-ext-enotify.h       | 11 +++-
 src/lib-sieve/sieve-result.c                  | 19 ++++---
 src/lib-sieve/sieve-result.h                  | 11 ++--
 8 files changed, 97 insertions(+), 35 deletions(-)

diff --git a/TODO b/TODO
index 199916f65..3044ae4f0 100644
--- a/TODO
+++ b/TODO
@@ -1,8 +1,6 @@
 Current:
 
-* Finish enotify extension:
-	- Cleanup method API.
-	- Regorous testing
+* Test new enotify extension extensively.
 * Test new multiscript support extensively. 
 
 Next (in order of descending priority/precedence):
@@ -63,7 +61,7 @@ Next (in order of descending priority/precedence):
 
 * Implement editheader extension
 * Implement mimeloop extension
-* Variables extension: implement compile time evaluation of contant values
+* Variables extension: implement compile time evaluation of constant values
 * Enotify extension: detect use of variable values extracted from the message 
   that are used in the method argument. RFC reports this as a security issue.
 * Import ManageSieve into this package and provide support for alternate types
diff --git a/src/lib-sieve/plugins/enotify/cmd-notify.c b/src/lib-sieve/plugins/enotify/cmd-notify.c
index e2929a9e3..e257c3f1d 100644
--- a/src/lib-sieve/plugins/enotify/cmd-notify.c
+++ b/src/lib-sieve/plugins/enotify/cmd-notify.c
@@ -565,8 +565,14 @@ static void act_notify_print
 	sieve_result_action_printf
 		( rpenv, "send notification with method '%s:':", act->method->identifier);
 		
-	if ( act->method->action_print != NULL )
-		act->method->action_print(rpenv, act);
+	if ( act->method->action_print != NULL ) {
+		struct sieve_enotify_print_env penv;
+
+		memset(&penv, 0, sizeof(penv));
+		penv.result_penv = rpenv;
+
+		act->method->action_print(&penv, act);
+	}
 }
 
 /* Result execution */
diff --git a/src/lib-sieve/plugins/enotify/ext-enotify-common.c b/src/lib-sieve/plugins/enotify/ext-enotify-common.c
index 0fa7ba0bd..55b61dffa 100644
--- a/src/lib-sieve/plugins/enotify/ext-enotify-common.c
+++ b/src/lib-sieve/plugins/enotify/ext-enotify-common.c
@@ -551,6 +551,20 @@ int ext_enotify_runtime_check_operands
 	return SIEVE_EXEC_OK;
 }
 
+/*
+ * Notify method printing
+ */
+
+void sieve_enotify_method_printf
+(const struct sieve_enotify_print_env *penv, const char *fmt, ...)
+{
+	va_list args;
+	
+	va_start(args, fmt);	
+	sieve_result_vprintf(penv->result_penv, fmt, args);
+	va_end(args);	 
+}
+
 /*
  * Method logging
  */
diff --git a/src/lib-sieve/plugins/enotify/ext-enotify-common.h b/src/lib-sieve/plugins/enotify/ext-enotify-common.h
index a5e8862c7..52fad08c3 100644
--- a/src/lib-sieve/plugins/enotify/ext-enotify-common.h
+++ b/src/lib-sieve/plugins/enotify/ext-enotify-common.h
@@ -93,6 +93,14 @@ int ext_enotify_runtime_check_operands
 		struct sieve_coded_stringlist *options, 
 		const struct sieve_enotify_method **method_r, void **method_context);
 		
+/*
+ * Method printing
+ */
+
+struct sieve_enotify_print_env {
+	const struct sieve_result_print_env *result_penv;
+};
+
 /*
  * Method logging
  */ 
diff --git a/src/lib-sieve/plugins/enotify/ntfy-mailto.c b/src/lib-sieve/plugins/enotify/ntfy-mailto.c
index 0376a7a33..ecb5f7003 100644
--- a/src/lib-sieve/plugins/enotify/ntfy-mailto.c
+++ b/src/lib-sieve/plugins/enotify/ntfy-mailto.c
@@ -32,9 +32,6 @@
 #include "sieve-address.h"
 #include "sieve-message.h"
 
-/* FIXME: notify API: methods should not need to include sieve-result.h */
-#include "sieve-result.h"
-
 /*
  * Configuration
  */
@@ -85,7 +82,7 @@ static int ntfy_mailto_action_check_duplicates
 		const char *dupl_location);
 
 static void ntfy_mailto_action_print
-	(const struct sieve_result_print_env *rpenv, 
+	(const struct sieve_enotify_print_env *penv, 
 		const struct sieve_enotify_action *act);	
 
 static bool ntfy_mailto_action_execute
@@ -798,7 +795,7 @@ static int ntfy_mailto_action_check_duplicates
 		}
 	}
 
-	/* Perform pernding deletion */
+	/* Perform pending deletion */
 	if ( del_len > 0 ) {
 		array_delete(&mt_new->recipients, del_start, del_len);			
 	}
@@ -811,7 +808,7 @@ static int ntfy_mailto_action_check_duplicates
  */
  
 static void ntfy_mailto_action_print
-(const struct sieve_result_print_env *rpenv, 
+(const struct sieve_enotify_print_env *penv, 
 	const struct sieve_enotify_action *act)
 {
 	unsigned int count, i;
@@ -820,42 +817,60 @@ static void ntfy_mailto_action_print
 	struct ntfy_mailto_context *mtctx = 
 		(struct ntfy_mailto_context *) act->method_context;
 	
-	sieve_result_printf(rpenv,   "    => importance   : %d\n", act->importance);
+	/* Print main method parameters */
+
+	sieve_enotify_method_printf
+		(penv,   "    => importance   : %d\n", act->importance);
+
 	if ( act->message != NULL )
-		sieve_result_printf(rpenv, "    => subject      : %s\n", act->message);
+		sieve_enotify_method_printf
+			(penv, "    => subject      : %s\n", act->message);
 	else if ( mtctx->subject != NULL )
-		sieve_result_printf(rpenv, "    => subject      : %s\n", mtctx->subject);
+		sieve_enotify_method_printf
+			(penv, "    => subject      : %s\n", mtctx->subject);
+
 	if ( act->from != NULL )
-		sieve_result_printf(rpenv, "    => from         : %s\n", act->from);
+		sieve_enotify_method_printf
+			(penv, "    => from         : %s\n", act->from);
 
-	sieve_result_printf(rpenv,   "    => recipients   :\n" );
+	/* Print mailto: recipients */
+
+	sieve_enotify_method_printf(penv,   "    => recipients   :\n" );
 
 	recipients = array_get(&mtctx->recipients, &count);
 	if ( count == 0 ) {
-		sieve_result_printf(rpenv,   "       NONE, action has no effect\n");
+		sieve_enotify_method_printf(penv,   "       NONE, action has no effect\n");
 	} else {
 		for ( i = 0; i < count; i++ ) {
 			if ( recipients[i].carbon_copy )
-				sieve_result_printf(rpenv,   "       + Cc: %s\n", recipients[i].full);
+				sieve_enotify_method_printf
+					(penv,   "       + Cc: %s\n", recipients[i].full);
 			else
-				sieve_result_printf(rpenv,   "       + To: %s\n", recipients[i].full);
+				sieve_enotify_method_printf
+					(penv,   "       + To: %s\n", recipients[i].full);
 		}
 	}
+
+	/* Print accepted headers for notification message */
 	
 	headers = array_get(&mtctx->headers, &count);
 	if ( count > 0 ) {
-		sieve_result_printf(rpenv,   "    => headers      :\n" );	
+		sieve_enotify_method_printf(penv,   "    => headers      :\n" );	
 		for ( i = 0; i < count; i++ ) {
-			sieve_result_printf(rpenv,   "       + %s: %s\n", 
+			sieve_enotify_method_printf(penv,   "       + %s: %s\n", 
 				headers[i].name, headers[i].body);
 		}
 	}
+
+	/* Print body for notification message */
 	
 	if ( mtctx->body != NULL )
-		sieve_result_printf(rpenv, "    => body         : \n--\n%s\n--\n\n", 
-			mtctx->body);
+		sieve_enotify_method_printf
+			(penv, "    => body         : \n--\n%s\n--\n", mtctx->body);
+
+	/* Finish output with an empty line */
 
-	sieve_result_printf(rpenv,   "\n");
+	sieve_enotify_method_printf(penv,   "\n");
 }
 
 /*
diff --git a/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h b/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h
index 2e4fa36c4..9a8750e86 100644
--- a/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h
+++ b/src/lib-sieve/plugins/enotify/sieve-ext-enotify.h
@@ -18,6 +18,7 @@
 struct sieve_enotify_log;
 struct sieve_enotify_context; 
 struct sieve_enotify_action;
+struct sieve_enotify_print_env;
 struct sieve_enotify_exec_env;
 
 /*
@@ -76,7 +77,7 @@ struct sieve_enotify_method {
 		
 	/* Action print */
 	void (*action_print)
-		(const struct sieve_result_print_env *rpenv, 
+		(const struct sieve_enotify_print_env *penv, 
 			const struct sieve_enotify_action *act);	
 			
 	/* Action execution */
@@ -87,6 +88,14 @@ struct sieve_enotify_method {
 
 void sieve_enotify_method_register(const struct sieve_enotify_method *method);
 
+/*
+ * Notify method printing
+ */
+
+void sieve_enotify_method_printf
+	(const struct sieve_enotify_print_env *penv, const char *fmt, ...)
+		ATTR_FORMAT(2, 3);
+
 /*
  * Notify execution environment
  */
diff --git a/src/lib-sieve/sieve-result.c b/src/lib-sieve/sieve-result.c
index 2b9c928ac..a9a5c0a4e 100644
--- a/src/lib-sieve/sieve-result.c
+++ b/src/lib-sieve/sieve-result.c
@@ -573,17 +573,24 @@ void sieve_result_set_failure_action
  * Result printing
  */
 
+void sieve_result_vprintf
+(const struct sieve_result_print_env *penv, const char *fmt, va_list args)
+{	
+	string_t *outbuf = t_str_new(128);
+
+	str_vprintfa(outbuf, fmt, args);
+	
+	o_stream_send(penv->stream, str_data(outbuf), str_len(outbuf));
+}
+
 void sieve_result_printf
 (const struct sieve_result_print_env *penv, const char *fmt, ...)
 {	
-	string_t *outbuf = t_str_new(128);
 	va_list args;
 	
 	va_start(args, fmt);	
-	str_vprintfa(outbuf, fmt, args);
-	va_end(args);
-	
-	o_stream_send(penv->stream, str_data(outbuf), str_len(outbuf));
+	sieve_result_vprintf(penv, fmt, args);
+	va_end(args);	
 }
 
 void sieve_result_action_printf
@@ -669,7 +676,7 @@ bool sieve_result_print
 				if ( act->print != NULL )
 					act->print(act, &penv, rac->data.context, &impl_keep);
 				else
-					sieve_result_action_printf(&penv, act->name); 
+					sieve_result_action_printf(&penv, "%s", act->name); 
 			} else {
 				if ( rac->keep ) {
 					sieve_result_action_printf(&penv, "keep");
diff --git a/src/lib-sieve/sieve-result.h b/src/lib-sieve/sieve-result.h
index afe96fcaa..6692f2b13 100644
--- a/src/lib-sieve/sieve-result.h
+++ b/src/lib-sieve/sieve-result.h
@@ -50,12 +50,17 @@ struct sieve_result_print_env {
 	struct ostream *stream;
 };
 
+void sieve_result_vprintf
+	(const struct sieve_result_print_env *penv, const char *fmt, va_list args);
 void sieve_result_printf
-	(const struct sieve_result_print_env *penv, const char *fmt, ...);
+	(const struct sieve_result_print_env *penv, const char *fmt, ...)
+		ATTR_FORMAT(2, 3);
 void sieve_result_action_printf
-	(const struct sieve_result_print_env *penv, const char *fmt, ...);
+	(const struct sieve_result_print_env *penv, const char *fmt, ...)
+		ATTR_FORMAT(2, 3);
 void sieve_result_seffect_printf
-	(const struct sieve_result_print_env *penv, const char *fmt, ...);
+	(const struct sieve_result_print_env *penv, const char *fmt, ...)
+		ATTR_FORMAT(2, 3);
 
 bool sieve_result_print
 	(struct sieve_result *result, const struct sieve_script_env *senv, 
-- 
GitLab