From 545ba98e437a56bf273669a5b2e6107b36d1aa91 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Thu, 19 Nov 2009 21:22:56 +0100
Subject: [PATCH] Made homedir aquisition a callback.

---
 TODO                                          |  2 --
 src/lib-sieve-tool/sieve-tool.c               | 11 +++++++++--
 src/lib-sieve-tool/sieve-tool.h               |  5 +++++
 .../plugins/include/ext-include-common.c      |  4 +---
 src/lib-sieve/sieve-settings.h                | 19 +++++++++++++++++++
 src/lib-sieve/sieve-types.h                   |  1 +
 src/plugins/lda-sieve/lda-sieve-plugin.c      | 16 ++++++++++++++++
 src/testsuite/testsuite.c                     |  1 +
 8 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/TODO b/TODO
index ce4533369..6f2d23202 100644
--- a/TODO
+++ b/TODO
@@ -91,8 +91,6 @@ Next (in order of descending priority/precedence):
 	  through IMAP (no specification for something like this is available; we will 
 	  have to provide our own)
 * Variables extension: implement compile time evaluation of constant values
-* Make the engine and its extensions much more configurable. Possibly this can 
-  be merged with Dovecot's new master config implementation.
 * Add development documentation, i.e. comment on library functions and document
   the binary and byte-code format. 
 * Give the byte code format some more thought, it is currently quite rough and
diff --git a/src/lib-sieve-tool/sieve-tool.c b/src/lib-sieve-tool/sieve-tool.c
index 61d95f0e2..a14d6c059 100644
--- a/src/lib-sieve-tool/sieve-tool.c
+++ b/src/lib-sieve-tool/sieve-tool.c
@@ -31,13 +31,20 @@ struct sieve_instance *sieve_instance;
  * Settings management
  */
 
-static const char *sieve_tool_get_setting
+const char *sieve_tool_get_setting
 (void *context ATTR_UNUSED, const char *identifier)
 {
 	return getenv(t_str_ucase(identifier));
 }
 
-static const struct sieve_callbacks sieve_tool_callbacks = {
+const char *sieve_tool_get_homedir
+(void *context ATTR_UNUSED)
+{
+	return getenv("HOME");
+}
+
+const struct sieve_callbacks sieve_tool_callbacks = {
+	sieve_tool_get_homedir,
 	sieve_tool_get_setting
 };
 
diff --git a/src/lib-sieve-tool/sieve-tool.h b/src/lib-sieve-tool/sieve-tool.h
index dacda5e51..0958aa2c4 100644
--- a/src/lib-sieve-tool/sieve-tool.h
+++ b/src/lib-sieve-tool/sieve-tool.h
@@ -14,6 +14,11 @@
 
 struct sieve_instance *sieve_instance;
 
+const char *sieve_tool_get_setting(void *context, const char *identifier);
+const char *sieve_tool_get_homedir(void *context);
+
+const struct sieve_callbacks sieve_tool_callbacks;
+
 /*
  * Initialization
  */
diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c
index 0941f9d06..ce0a7f716 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.c
+++ b/src/lib-sieve/plugins/include/ext-include-common.c
@@ -79,9 +79,7 @@ const char *ext_include_get_script_directory
 	case EXT_INCLUDE_LOCATION_PERSONAL:
  		sieve_dir = sieve_get_setting(svinst, "sieve_dir");
 
-		home = sieve_get_setting(svinst, "home");
-
-		if ( home == NULL ) home = getenv("HOME");
+		home = sieve_get_homedir(svinst);
 
 		if ( sieve_dir == NULL ) {
 			if ( home == NULL )	{		
diff --git a/src/lib-sieve/sieve-settings.h b/src/lib-sieve/sieve-settings.h
index e0cd91187..2acf1f487 100644
--- a/src/lib-sieve/sieve-settings.h
+++ b/src/lib-sieve/sieve-settings.h
@@ -6,6 +6,10 @@
 
 #include "sieve-common.h"
 
+/*
+ * Settings
+ */
+
 static inline const char *sieve_get_setting
 (struct sieve_instance *svinst, const char *identifier)
 {
@@ -25,4 +29,19 @@ bool sieve_get_int_setting
 (struct sieve_instance *svinst, const char *identifier,
 	long long int *value_r);
 
+/*
+ * Home directory
+ */
+
+static inline const char *sieve_get_homedir
+(struct sieve_instance *svinst)
+{
+	const struct sieve_callbacks *callbacks = svinst->callbacks;
+
+	if ( callbacks == NULL || callbacks->get_homedir == NULL )
+		return NULL;
+
+	return callbacks->get_homedir(svinst->context);
+}
+
 #endif /* __SIEVE_SETTINGS_H */
diff --git a/src/lib-sieve/sieve-types.h b/src/lib-sieve/sieve-types.h
index ea4445b99..519d2354d 100644
--- a/src/lib-sieve/sieve-types.h
+++ b/src/lib-sieve/sieve-types.h
@@ -30,6 +30,7 @@ struct sieve_exec_status;
  */
 
 struct sieve_callbacks {
+	const char *(*get_homedir)(void *context);
 	const char *(*get_setting)(void *context, const char *identifier);
 };
 
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index 6f615c1db..9f99a4ec1 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -38,6 +38,21 @@ static deliver_mail_func_t *next_deliver_mail;
  * Settings handling
  */
 
+static const char *lda_sieve_get_homedir
+(void *context)
+{
+	struct mail_user *mail_user = (struct mail_user *) context;
+	const char *home = NULL;
+
+	if ( mail_user == NULL )
+		return NULL;
+
+	if ( mail_user_get_home(mail_user, &home) <= 0 )
+		return NULL;
+
+	return home;
+}
+
 static const char *lda_sieve_get_setting
 (void *context, const char *identifier)
 {
@@ -50,6 +65,7 @@ static const char *lda_sieve_get_setting
 }
 
 static const struct sieve_callbacks lda_sieve_callbacks = {
+	lda_sieve_get_homedir,
     lda_sieve_get_setting
 };
 
diff --git a/src/testsuite/testsuite.c b/src/testsuite/testsuite.c
index 3c6a4ee2e..4e5b638bb 100644
--- a/src/testsuite/testsuite.c
+++ b/src/testsuite/testsuite.c
@@ -51,6 +51,7 @@ const struct sieve_script_env *testsuite_scriptenv;
  */
 
 static const struct sieve_callbacks testsuite_sieve_callbacks = {
+	sieve_tool_get_homedir,
 	testsuite_setting_get
 };
 
-- 
GitLab