From 639b71a310246ce695bfe560707b931ce255708e Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Tue, 23 Oct 2007 19:32:31 +0200
Subject: [PATCH] * Minor changes to the extension implementation * Started the
 lda plugin source

---
 src/lib-sieve/Makefile.am                |  3 +
 src/lib-sieve/ext-fileinto.c             | 29 ++++++++
 src/lib-sieve/sieve-interpreter.h        |  9 +++
 src/plugins/Makefile.am                  |  1 +
 src/plugins/lda-sieve/Makefile.am        | 33 +++++++++
 src/plugins/lda-sieve/lda-sieve-plugin.c | 91 ++++++++++++++++++++++++
 src/plugins/lda-sieve/lda-sieve-plugin.h | 12 ++++
 7 files changed, 178 insertions(+)
 create mode 100644 src/plugins/Makefile.am
 create mode 100644 src/plugins/lda-sieve/Makefile.am
 create mode 100644 src/plugins/lda-sieve/lda-sieve-plugin.c
 create mode 100644 src/plugins/lda-sieve/lda-sieve-plugin.h

diff --git a/src/lib-sieve/Makefile.am b/src/lib-sieve/Makefile.am
index 84f2675a0..31d87cc3e 100644
--- a/src/lib-sieve/Makefile.am
+++ b/src/lib-sieve/Makefile.am
@@ -39,6 +39,7 @@ sievec_SOURCES = \
 	sieve-validator.c \
 	sieve-generator.c \
 	sieve-interpreter.c \
+	sieve-result.c \
 	sieve-error.c \
 	sieve-commands.c \
 	sieve-code.c \
@@ -51,10 +52,12 @@ sievec_SOURCES = \
 noinst_HEADERS = \
 	sieve-lexer.h \
 	sieve-ast.h \
+	sieve-binary.h \
 	sieve-parser.h \
 	sieve-validator.h \
 	sieve-generator.h \
 	sieve-interpreter.h \
+	sieve-result.h
 	sieve-error.h \
 	sieve-commands.h \
 	sieve-code.h
diff --git a/src/lib-sieve/ext-fileinto.c b/src/lib-sieve/ext-fileinto.c
index 8c6cd58f4..c4fc2b58f 100644
--- a/src/lib-sieve/ext-fileinto.c
+++ b/src/lib-sieve/ext-fileinto.c
@@ -79,3 +79,32 @@ static bool ext_fileinto_opcode_dump(struct sieve_interpreter *interpreter)
 	return TRUE;
 }
 
+/*
+ * Execution
+ */
+
+static bool ext_fileinto_opcode_execute(struct sieve_interpreter *interpreter)
+{
+}
+
+/*
+static int sieve_fileinto(void *ac,
+              void *ic ATTR_UNUSED,
+              void *sc,
+              void *mc,
+              const char **errmsg ATTR_UNUSED)
+{
+    sieve_fileinto_context_t *fc = (sieve_fileinto_context_t *) ac;
+    script_data_t *sd = (script_data_t *) sc;
+    sieve_msgdata_t *md = (sieve_msgdata_t *) mc;
+    enum mail_flags flags;
+    const char *const *keywords;
+
+    get_flags(fc->imapflags, &flags, &keywords);
+
+    if (deliver_save(sd->namespaces, sd->storage_r, fc->mailbox,
+             md->mail, flags, keywords) < 0)
+        return SIEVE_FAIL;
+
+    return SIEVE_OK;
+}*/
diff --git a/src/lib-sieve/sieve-interpreter.h b/src/lib-sieve/sieve-interpreter.h
index 28cdcba97..7f8906a84 100644
--- a/src/lib-sieve/sieve-interpreter.h
+++ b/src/lib-sieve/sieve-interpreter.h
@@ -3,6 +3,7 @@
 
 #include "lib.h"
 #include "buffer.h"
+#include "mail-storage.h"
 
 #include "sieve-binary.h"
 
@@ -10,6 +11,14 @@ struct sieve_coded_stringlist;
 
 struct sieve_interpreter;
 
+struct sieve_mail_context {
+	struct mail_namespace *namespaces;
+	struct mail_storage **storage_r;
+	struct mail *mail;
+	const char *destaddr;
+	const char *mailbox;
+};
+
 struct sieve_interpreter *sieve_interpreter_create(struct sieve_binary *binary);
 void sieve_interpreter_free(struct sieve_interpreter *interpreter);
 
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
new file mode 100644
index 000000000..4623241f1
--- /dev/null
+++ b/src/plugins/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = lda-sieve
diff --git a/src/plugins/lda-sieve/Makefile.am b/src/plugins/lda-sieve/Makefile.am
new file mode 100644
index 000000000..706b00fb8
--- /dev/null
+++ b/src/plugins/lda-sieve/Makefile.am
@@ -0,0 +1,33 @@
+SUBDIRS = libsieve
+
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/src/lib-sieve \
+	-I$(dovecot_incdir) \
+	-I$(dovecot_incdir)/src/lib \
+	-I$(dovecot_incdir)/src/lib-dict \
+	-I$(dovecot_incdir)/src/lib-mail \
+	-I$(dovecot_incdir)/src/lib-storage \
+	-I$(dovecot_incdir)/src/deliver
+
+lda_moduledir = $(moduledir)/lda
+
+lib90_cmusieve_plugin_la_LDFLAGS = -module -avoid-version
+
+lda_module_LTLIBRARIES = \
+	lib90_cmusieve_plugin.la
+
+lib90_cmusieve_plugin_la_LIBADD = \
+	libsieve/libsieve.la
+
+lib90_cmusieve_plugin_la_SOURCES = \
+	cmusieve-plugin.c \
+	imparse.c \
+	map.c \
+	sieve-cmu.c
+
+noinst_HEADERS = \
+	cmusieve-plugin.h \
+	imparse.h \
+	libconfig.h \
+	map.h \
+	xmalloc.h
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
new file mode 100644
index 000000000..01a9809ac
--- /dev/null
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -0,0 +1,91 @@
+/* Copyright (C) 2006 Timo Sirainen */
+
+#include "lib.h"
+#include "home-expand.h"
+#include "deliver.h"
+#include "sieve-compiler.h"
+
+#include "lda-sieve-plugin.h"
+
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#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)
+{
+	const char *script_path, *home;
+	struct stat st;
+
+	home = getenv("HOME");
+
+	/* userdb may specify Sieve path */
+	script_path = getenv("SIEVE");
+	if (script_path != NULL) {
+		if (*script_path == '\0') {
+			/* disabled */
+			return NULL;
+		}
+
+		if (*script_path != '/' && *script_path != '\0') {
+			/* relative path. change to absolute. */
+			script_path = t_strconcat(getenv("HOME"), "/",
+						  script_path, NULL);
+		}
+	} else {
+		if (home == NULL) {
+			i_error("Per-user script path is unknown. See "
+				"http://wiki.dovecot.org/LDA/Sieve#location");
+			return NULL;
+		}
+
+		script_path = home_expand(SIEVE_SCRIPT_PATH);
+	}
+
+	if (stat(script_path, &st) < 0) {
+		if (errno != ENOENT)
+			i_error("stat(%s) failed: %m", script_path);
+
+		/* use global script instead, if one exists */
+		script_path = getenv("SIEVE_GLOBAL_PATH");
+		if (script_path == NULL) {
+			/* for backwards compatibility */
+			script_path = getenv("GLOBAL_SCRIPT_PATH");
+		}
+	}
+
+	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)
+{
+	const char *script_path;
+
+	script_path = get_sieve_path();
+	if (script_path == NULL)
+		return 0;
+
+	if (getenv("DEBUG") != NULL)
+		i_info("sieve: Using sieve path: %s", script_path);
+
+	return sieve_run(namespaces, storage_r, mail, script_path,
+			     destaddr, getenv("USER"), mailbox);
+}
+
+void lda_sieve_plugin_init(void)
+{
+	next_deliver_mail = deliver_mail;
+	deliver_mail = lda_sieve_deliver_mail;
+}
+
+void lda_sieve_plugin_deinit(void)
+{
+	deliver_mail = next_deliver_mail;
+}
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.h b/src/plugins/lda-sieve/lda-sieve-plugin.h
new file mode 100644
index 000000000..a138d15b8
--- /dev/null
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.h
@@ -0,0 +1,12 @@
+#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);
+
+void lda_sieve_plugin_init(void);
+void lda_sieve_plugin_deinit(void);
+
+#endif
-- 
GitLab