From deb9f7057bab1a576e945b02b387ddaf87ce1469 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sun, 2 Dec 2007 19:58:29 +0100
Subject: [PATCH] Included sieve plugin into the build process.

---
 configure.in                             |  2 +
 src/Makefile.am                          |  2 +-
 src/lib-sieve/sieve.c                    | 24 ++++++--
 src/lib-sieve/sieve.h                    |  2 +-
 src/plugins/lda-sieve/Makefile.am        | 24 +++-----
 src/plugins/lda-sieve/lda-sieve-plugin.c | 71 +++++++++++++++++++++---
 src/plugins/lda-sieve/lda-sieve-plugin.h |  7 +--
 src/sieve-bin/bin-common.c               | 10 +---
 8 files changed, 98 insertions(+), 44 deletions(-)

diff --git a/configure.in b/configure.in
index 2348bb2eb..694007486 100644
--- a/configure.in
+++ b/configure.in
@@ -66,6 +66,8 @@ src/lib-sieve/plugins/relational/Makefile
 src/lib-sieve/plugins/regex/Makefile
 src/lib-sieve/plugins/imapflags/Makefile
 src/lib-sieve/plugins/copy/Makefile
+src/plugins/Makefile
+src/plugins/lda-sieve/Makefile
 src/sieve-bin/Makefile
 stamp.h])
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 2a33151c1..de5f667f3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1 +1 @@
-SUBDIRS = lib-sieve sieve-bin
+SUBDIRS = lib-sieve sieve-bin plugins
diff --git a/src/lib-sieve/sieve.c b/src/lib-sieve/sieve.c
index 97a9ab9e1..01c35b711 100644
--- a/src/lib-sieve/sieve.c
+++ b/src/lib-sieve/sieve.c
@@ -83,7 +83,7 @@ static struct sieve_binary *sieve_generate(struct sieve_ast *ast)
 	return result;
 }
 
-struct sieve_binary *sieve_compile(int fd) 
+static struct sieve_binary *sieve_compile_fd(int fd, const char *name) 
 {
 	struct sieve_binary *result;
 	struct sieve_error_handler *ehandler;
@@ -94,13 +94,13 @@ struct sieve_binary *sieve_compile(int fd)
 	
 	/* Parse */
 	if ( (ast = sieve_parse(fd, ehandler)) == NULL ) {
- 		i_error("failed to parse script");
+ 		i_error("Failed to parse script %s", name);
 		return NULL;
 	}
 
 	/* Validate */
 	if ( !sieve_validate(ast, ehandler) ) {
-		i_error("failed to validate script");
+		i_error("Failed to validate script %s", name);
 		
  		sieve_ast_unref(&ast);
  		return NULL;
@@ -108,7 +108,7 @@ struct sieve_binary *sieve_compile(int fd)
  	
 	/* Generate */
 	if ( (result=sieve_generate(ast)) == NULL ) {
-		i_error("failed to generate script");
+		i_error("Failed to generate script %s", name);
 		
 		sieve_ast_unref(&ast);
 		return NULL;
@@ -120,6 +120,22 @@ struct sieve_binary *sieve_compile(int fd)
 	return result;
 }
 
+struct sieve_binary *sieve_compile(const char *scriptpath)
+{
+	int sfd;
+	struct sieve_binary *sbin;
+	
+	if ( (sfd = open(scriptpath, O_RDONLY)) < 0 ) {
+		i_error("Failed to open sieve script %s: %m", scriptpath);
+		return NULL;
+	}
+	
+	sbin = sieve_compile_fd(sfd, scriptpath);
+		
+	close(sfd);
+	return sbin;
+}
+
 void sieve_dump(struct sieve_binary *binary, struct ostream *stream) 
 {
 	struct sieve_code_dumper *dumpr = sieve_code_dumper_create(binary);			
diff --git a/src/lib-sieve/sieve.h b/src/lib-sieve/sieve.h
index ad82fadf0..776a52028 100644
--- a/src/lib-sieve/sieve.h
+++ b/src/lib-sieve/sieve.h
@@ -45,7 +45,7 @@ struct sieve_mail_environment {
 bool sieve_init(const char *plugins);
 void sieve_deinit(void);
 
-struct sieve_binary *sieve_compile(int fd);
+struct sieve_binary *sieve_compile(const char *scriptpath);
 void sieve_dump(struct sieve_binary *binary, struct ostream *stream);
 bool sieve_test
 	(struct sieve_binary *binary, const struct sieve_message_data *msgdata, 
diff --git a/src/plugins/lda-sieve/Makefile.am b/src/plugins/lda-sieve/Makefile.am
index 706b00fb8..994aafbe3 100644
--- a/src/plugins/lda-sieve/Makefile.am
+++ b/src/plugins/lda-sieve/Makefile.am
@@ -1,5 +1,3 @@
-SUBDIRS = libsieve
-
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib-sieve \
 	-I$(dovecot_incdir) \
@@ -11,23 +9,15 @@ AM_CPPFLAGS = \
 
 lda_moduledir = $(moduledir)/lda
 
-lib90_cmusieve_plugin_la_LDFLAGS = -module -avoid-version
+lib90_sieve_plugin_la_LDFLAGS = -module -avoid-version
 
-lda_module_LTLIBRARIES = \
-	lib90_cmusieve_plugin.la
+lda_module_LTLIBRARIES = lib90_sieve_plugin.la
 
-lib90_cmusieve_plugin_la_LIBADD = \
-	libsieve/libsieve.la
+lib90_sieve_plugin_la_LIBADD = \
+	$(top_srcdir)/src/lib-sieve/libsieve.la
 
-lib90_cmusieve_plugin_la_SOURCES = \
-	cmusieve-plugin.c \
-	imparse.c \
-	map.c \
-	sieve-cmu.c
+lib90_sieve_plugin_la_SOURCES = \
+	lda-sieve-plugin.c 
 
 noinst_HEADERS = \
-	cmusieve-plugin.h \
-	imparse.h \
-	libconfig.h \
-	map.h \
-	xmalloc.h
+	lda-sieve-plugin.h
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index 8b01eeeb6..7b9a463b4 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -3,7 +3,9 @@
 #include "lib.h"
 #include "home-expand.h"
 #include "deliver.h"
-#include "sieve-compiler.h"
+#include "duplicate.h"
+#include "smtp-client.h"
+#include "sieve.h"
 
 #include "lda-sieve-plugin.h"
 
@@ -13,7 +15,6 @@
 #define SIEVE_SCRIPT_PATH "~/.dovecot.sieve"
 
 static deliver_mail_func_t *next_deliver_mail;
-struct et_list *_et_list = NULL;
 
 static const char *get_sieve_path(void)
 {
@@ -60,11 +61,65 @@ static const char *get_sieve_path(void)
 	return script_path;
 }
 
-static int
-lda_sieve_deliver_mail(struct mail_namespace *namespaces,
-		      struct mail_storage **storage_r,
-		      struct mail *mail,
-		      const char *destaddr, const char *mailbox)
+static void *sieve_smtp_open(const char *destination,
+	const char *return_path, FILE **file_r)
+{
+	return (void *) smtp_client_open(destination, return_path, file_r);
+}
+
+static bool sieve_smtp_close(void *handle)
+{
+	struct smtp_client *smtp_client = (struct smtp_client *) handle;
+	
+	return ( smtp_client_close(smtp_client) >= 0 );
+}
+
+static int lda_sieve_run
+(struct mail_namespace *namespaces, struct mail *mail, const char *script_path,
+	const char *destaddr, const char *username, const char *mailbox)
+{
+	bool debug = ( getenv("DEBUG") != NULL );
+	struct sieve_message_data msgdata;
+	struct sieve_mail_environment mailenv;
+	struct sieve_binary *sbin;
+	
+	if ( debug )
+		i_info("lda-sieve: Compiling script %s", script_path);
+	
+	if ( (sbin=sieve_compile(script_path)) == NULL )
+		return -1;
+
+	/* Collect necessary message data */
+	memset(&msgdata, 0, sizeof(msgdata));
+	msgdata.mail = mail;
+	msgdata.return_path = deliver_get_return_address(mail);
+	msgdata.to_address = destaddr;
+	msgdata.auth_user = username;
+	(void)mail_get_first_header(mail, "Message-ID", &msgdata.id);
+	
+	memset(&mailenv, 0, sizeof(mailenv));
+	mailenv.inbox = mailbox;
+	mailenv.namespaces = namespaces;
+	mailenv.username = username;
+	mailenv.hostname = deliver_set->hostname;
+	mailenv.postmaster_address = deliver_set->postmaster_address;
+	mailenv.smtp_open = sieve_smtp_open;
+	mailenv.smtp_close = sieve_smtp_close;
+	mailenv.duplicate_mark = duplicate_mark;
+	mailenv.duplicate_check = duplicate_check;
+
+	if ( debug )
+		i_info("lda-sieve: Executing (in-memory) script %s", script_path);
+	
+	if ( sieve_execute(sbin, &msgdata, &mailenv) )
+		return 1;
+		
+	return -1;
+}
+
+static int lda_sieve_deliver_mail
+(struct mail_namespace *namespaces, struct mail_storage **storage_r ATTR_UNUSED, 
+	struct mail *mail, const char *destaddr, const char *mailbox)
 {
 	const char *script_path;
 
@@ -75,7 +130,7 @@ lda_sieve_deliver_mail(struct mail_namespace *namespaces,
 	if (getenv("DEBUG") != NULL)
 		i_info("sieve: Using sieve path: %s", script_path);
 
-	return sieve_run(namespaces, storage_r, mail, script_path,
+	return lda_sieve_run(namespaces, mail, script_path,
 			     destaddr, getenv("USER"), mailbox);
 }
 
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.h b/src/plugins/lda-sieve/lda-sieve-plugin.h
index a138d15b8..d105e14b0 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.h
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.h
@@ -1,10 +1,9 @@
 #ifndef __LDA_SIEVE_PLUGIN_H
 #define __LDA_SIEVE_PLUGIN_H
 
-int lda_sieve_run(struct mail_namespace *namespaces,
-		  struct mail_storage **storage_r, struct mail *mail,
-		  const char *script_path, const char *destaddr,
-		  const char *username, const char *mailbox);
+static int lda_sieve_run
+(struct mail_namespace *namespaces, struct mail *mail, const char *script_path,
+    const char *destaddr, const char *username, const char *mailbox);
 
 void lda_sieve_plugin_init(void);
 void lda_sieve_plugin_deinit(void);
diff --git a/src/sieve-bin/bin-common.c b/src/sieve-bin/bin-common.c
index 934f5b394..ff97f03ee 100644
--- a/src/sieve-bin/bin-common.c
+++ b/src/sieve-bin/bin-common.c
@@ -68,19 +68,11 @@ const char *bin_get_user(void)
 
 struct sieve_binary *bin_compile_sieve_script(const char *filename)
 {
-	int sfd;
 	struct sieve_binary *sbin;
 	
-	if ( (sfd = open(filename, O_RDONLY)) < 0 ) 
-		i_fatal("Failed to open sieve script %s: %m", filename);
-	
-	if ( (sbin = sieve_compile(sfd)) == NULL ) 
-	{
-		close(sfd);
+	if ( (sbin = sieve_compile(filename)) == NULL ) 
 		i_fatal("Failed to compile sieve script\n");
-	}
 		
-	close(sfd);
 	return sbin;
 }
 	
-- 
GitLab