From ee860f97ce3791d7cf7377fd8b6e9ef4ed7e364f Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Wed, 6 Apr 2016 00:56:07 +0200
Subject: [PATCH] lib-sieve: Moved the algorithm for determining the user log
 file path to lib-sieve.

This was duplicated between the lda-sieve and imapsieve plugins.
---
 src/lib-sieve/sieve.c                    | 37 ++++++++++++++++++++++++
 src/lib-sieve/sieve.h                    |  9 ++++++
 src/plugins/imapsieve/imap-sieve.c       | 36 ++---------------------
 src/plugins/lda-sieve/lda-sieve-plugin.c | 29 ++-----------------
 4 files changed, 52 insertions(+), 59 deletions(-)

diff --git a/src/lib-sieve/sieve.c b/src/lib-sieve/sieve.c
index c6fe99485..58584cecd 100644
--- a/src/lib-sieve/sieve.c
+++ b/src/lib-sieve/sieve.c
@@ -799,6 +799,43 @@ size_t sieve_max_script_size(struct sieve_instance *svinst)
 	return svinst->max_script_size;
 }
 
+/*
+ * User log
+ */
+
+const char *sieve_user_get_log_path
+(struct sieve_instance *svinst,
+	struct sieve_script *user_script)
+{
+	const char *log_path = NULL;
+
+	/* Determine user log file path */
+	if ( (log_path=sieve_setting_get
+		(svinst, "sieve_user_log")) == NULL ) {
+		const char *path;
+
+		if ( user_script == NULL ||
+			(path=sieve_file_script_get_path(user_script)) == NULL ) {
+			/* Default */
+			if ( svinst->home_dir != NULL ) {
+				log_path = t_strconcat
+					(svinst->home_dir, "/.dovecot.sieve.log", NULL);
+			}
+		} else {
+			/* Use script file as a base (legacy behavior) */
+			log_path = t_strconcat(path, ".log", NULL);
+		}
+	} else if ( svinst->home_dir != NULL ) {
+		/* Expand home dir if necessary */
+		if ( log_path[0] == '~' ) {
+			log_path = home_expand_tilde(log_path, svinst->home_dir);
+		} else if ( log_path[0] != '/' ) {
+			log_path = t_strconcat(svinst->home_dir, "/", log_path, NULL);
+		}
+	}
+	return log_path;
+}
+
 /*
  * Script trace log
  */
diff --git a/src/lib-sieve/sieve.h b/src/lib-sieve/sieve.h
index 3e108e02a..23d5fe153 100644
--- a/src/lib-sieve/sieve.h
+++ b/src/lib-sieve/sieve.h
@@ -213,6 +213,15 @@ unsigned int sieve_max_redirects(struct sieve_instance *svinst);
 unsigned int sieve_max_actions(struct sieve_instance *svinst);
 size_t sieve_max_script_size(struct sieve_instance *svinst);
 
+/*
+ * User log
+ */
+
+const char *sieve_user_get_log_path
+	(struct sieve_instance *svinst,
+		struct sieve_script *user_script)
+	ATTR_NULL(2);
+
 /*
  * Script trace log
  */
diff --git a/src/plugins/imapsieve/imap-sieve.c b/src/plugins/imapsieve/imap-sieve.c
index 5904aab90..063c4273f 100644
--- a/src/plugins/imapsieve/imap-sieve.c
+++ b/src/plugins/imapsieve/imap-sieve.c
@@ -246,44 +246,14 @@ struct imap_sieve_run {
 };
 
 static void
-imap_sieve_run_init_user_log(
-	struct imap_sieve_run *isrun)
+imap_sieve_run_init_user_log(struct imap_sieve_run *isrun)
 {
 	struct imap_sieve *isieve = isrun->isieve;
 	struct sieve_instance *svinst = isieve->svinst;
 	const char *log_path;
 
-	/* Determine user log file path */ // FIXME: code shared with LDA
-	if ( (log_path=mail_user_plugin_getenv
-		(isieve->user, "sieve_user_log")) == NULL ) {
-		const char *path;
-
-		if ( isrun->user_script == NULL ||
-			(path=sieve_file_script_get_path
-				(isrun->user_script)) == NULL ) {
-			/* Default */
-			if ( isieve->home_dir != NULL ) {
-				log_path = t_strconcat
-					(isieve->home_dir, "/.dovecot.sieve.log", NULL);
-			}
-		} else {
-			/* Use script file as a basis (legacy behavior) */
-			log_path = t_strconcat(path, ".log", NULL);
-		}
-	} else {
-		if ( isieve->home_dir != NULL ) {
-			/* Expand home dir if necessary */
-			if ( log_path[0] == '~' ) {
-				log_path = home_expand_tilde
-					(log_path, isieve->home_dir);
-			} else if ( log_path[0] != '/' ) {
-				log_path = t_strconcat
-					(isieve->home_dir, "/", log_path, NULL);
-			}
-		}
-	}
-
-	/* Initialize user error handler */
+	log_path = sieve_user_get_log_path
+		(svinst, isrun->user_script);
 	if ( log_path != NULL ) {
 		isrun->userlog = p_strdup(isrun->pool, log_path);
 		isrun->user_ehandler = sieve_logfile_ehandler_create
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index 6a7728e44..4e11b673d 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -793,32 +793,9 @@ static int lda_sieve_execute
 		/* Initialize user error handler */
 
 		if ( srctx->user_script != NULL ) {
-			const char *log_path = NULL;
-
-			/* Determine user log file path */
-			if ( (log_path=mail_user_plugin_getenv
-				(mdctx->dest_user, "sieve_user_log")) == NULL ) {
-				const char *path;
-
-				if ( (path=sieve_file_script_get_path(srctx->user_script)) == NULL ) {
-					/* Default */
-					if ( srctx->home_dir != NULL ) {
-						log_path = t_strconcat
-							(srctx->home_dir, "/.dovecot.sieve.log", NULL);
-					}
-				} else {
-					/* Use script file as a basic (legacy behavior) */
-					log_path = t_strconcat(path, ".log", NULL);
-				}
-			} else if ( srctx->home_dir != NULL ) {
-				/* Expand home dir if necessary */
-				if ( log_path[0] == '~' ) {
-					log_path = home_expand_tilde(log_path, srctx->home_dir);
-				} else if ( log_path[0] != '/' ) {
-					log_path = t_strconcat(srctx->home_dir, "/", log_path, NULL);
-				}
-			}
-
+			const char *log_path =
+				sieve_user_get_log_path(svinst, srctx->user_script);
+	
 			if ( log_path != NULL ) {
 				srctx->userlog = log_path;
 				srctx->user_ehandler = sieve_logfile_ehandler_create
-- 
GitLab