diff --git a/src/lib-sieve/plugins/imapflags/tag-flags.c b/src/lib-sieve/plugins/imapflags/tag-flags.c
index ad5a0bd0ab966872f1aa74ab50f9f8ec129ff764..9a7bd3adbdce3642acc612e62a756ff3f59ad50f 100644
--- a/src/lib-sieve/plugins/imapflags/tag-flags.c
+++ b/src/lib-sieve/plugins/imapflags/tag-flags.c
@@ -239,6 +239,8 @@ static bool seff_flags_post_execute
 	struct act_store_transaction *trans = 
 		(struct act_store_transaction *) tr_context;
 
+	if ( trans->dest_mail == NULL ) return TRUE;
+
 	printf("SETTING FLAGS\n");
 	/* Update message flags. */
 	mail_update_flags(trans->dest_mail, MODIFY_ADD, ctx->flags);
diff --git a/src/lib-sieve/sieve-actions.c b/src/lib-sieve/sieve-actions.c
index 45ce31cf99dd2e1397365297a14b12f6eee67fd0..53310cebf0bfa1998d49ca7f41aa7d1fddb12023 100644
--- a/src/lib-sieve/sieve-actions.c
+++ b/src/lib-sieve/sieve-actions.c
@@ -253,24 +253,26 @@ static bool act_store_start
 {  
 	struct act_store_context *ctx = (struct act_store_context *) context;
 	struct act_store_transaction *trans;
-	struct mail_namespace *ns;
-	struct mailbox *box;
+	struct mail_namespace *ns = NULL;
+	struct mailbox *box = NULL;
 	pool_t pool;
 
-	ns = mail_namespace_find(aenv->mailenv->namespaces, &ctx->folder);
-	if (ns == NULL) 
-		return FALSE;
+	if ( aenv->mailenv->namespaces != NULL ) {
+		ns = mail_namespace_find(aenv->mailenv->namespaces, &ctx->folder);
+		if (ns == NULL) 
+			return FALSE;
 		
-	box = mailbox_open(ns->storage, ctx->folder, NULL, MAILBOX_OPEN_FAST |
-		MAILBOX_OPEN_KEEP_RECENT);
-						
+		box = mailbox_open(ns->storage, ctx->folder, NULL, MAILBOX_OPEN_FAST |
+			MAILBOX_OPEN_KEEP_RECENT);
+	}
+					
 	pool = sieve_result_pool(aenv->result);
 	trans = p_new(pool, struct act_store_transaction, 1);
 	trans->context = ctx;
 	trans->namespace = ns;
 	trans->box = box;
 	
-	if ( box == NULL ) 
+	if ( ns != NULL && box == NULL ) 
 		act_store_get_storage_error(aenv, trans);	
 	
 	*tr_context = (void *)trans;
@@ -284,6 +286,9 @@ static bool act_store_execute
 {   
 	struct act_store_transaction *trans = 
 		(struct act_store_transaction *) tr_context;
+
+	if ( trans->namespace == NULL )
+		return TRUE;
 			
 	if ( trans->box == NULL ) return FALSE;
 	
@@ -292,9 +297,9 @@ static bool act_store_execute
 
 	trans->dest_mail = mail_alloc(trans->mail_trans, 0, NULL);
 
-  if (mailbox_copy(trans->mail_trans, aenv->msgdata->mail, MAIL_DRAFT, NULL, 
-  	trans->dest_mail) < 0) {
-  	act_store_get_storage_error(aenv, trans);
+	if (mailbox_copy(trans->mail_trans, aenv->msgdata->mail, 0, NULL, 
+		trans->dest_mail) < 0) {
+		act_store_get_storage_error(aenv, trans);
  		return FALSE;
  	}
  		 	
@@ -312,22 +317,26 @@ static void act_store_log_status
 	else
 		mailbox_name = str_sanitize(mailbox_get_name(trans->box), 80);
 
-	if (!rolled_back && status) {
-		sieve_result_log(aenv, "stored mail into mailbox '%s'", mailbox_name);
-	} else {
-		const char *errstr;
-		enum mail_error error;
+	if ( trans->namespace == NULL ) {
+		sieve_result_log(aenv, "store into mailbox '%s' not performed.", mailbox_name);
+	} else {	
+		if ( !rolled_back && status ) {
+			sieve_result_log(aenv, "stored mail into mailbox '%s'", mailbox_name);
+		} else {
+			const char *errstr;
+			enum mail_error error;
 		
-		if ( trans->error != NULL )
-			errstr = trans->error;
-		else
-			errstr = mail_storage_get_last_error(trans->namespace->storage, &error);
+			if ( trans->error != NULL )
+				errstr = trans->error;
+			else
+				errstr = mail_storage_get_last_error(trans->namespace->storage, &error);
 			
-		if ( status )
-			sieve_result_log(aenv, "store into mailbox '%s' aborted.", mailbox_name);
-		else
-			sieve_result_error(aenv, "failed to store into mailbox '%s': %s", 
-				mailbox_name, errstr);
+			if ( status )
+				sieve_result_log(aenv, "store into mailbox '%s' aborted.", mailbox_name);
+			else
+				sieve_result_error(aenv, "failed to store into mailbox '%s': %s", 
+					mailbox_name, errstr);
+		}
 	}
 }
 
@@ -337,15 +346,21 @@ static bool act_store_commit
 {  
 	struct act_store_transaction *trans = 
 		(struct act_store_transaction *) tr_context;
-	bool status = mailbox_transaction_commit(&trans->mail_trans) == 0;
+	bool status = TRUE;
+
+	if ( trans->namespace != NULL ) {
+		if ( trans->dest_mail != NULL ) 
+			mail_free(&trans->dest_mail);	
+
+		status = mailbox_transaction_commit(&trans->mail_trans) == 0;
+	} 
 	
 	act_store_log_status(trans, aenv, FALSE, status);
-	
 	*keep = !status;
-	
+		
 	if ( trans->box != NULL )
 		mailbox_close(&trans->box);
-	
+
 	return status;
 }
 
@@ -356,7 +371,10 @@ static void act_store_rollback
 	struct act_store_transaction *trans = 
 		(struct act_store_transaction *) tr_context;
 
-  act_store_log_status(trans, aenv, TRUE, success);
+	act_store_log_status(trans, aenv, TRUE, success);
+
+	if ( trans->dest_mail != NULL ) 
+		mail_free(&trans->dest_mail);	
 
 	if ( trans->mail_trans != NULL )
 	  mailbox_transaction_rollback(&trans->mail_trans);
diff --git a/src/lib-sieve/sieve-code-dumper.c b/src/lib-sieve/sieve-code-dumper.c
index 0d24f9d5f5050cb36c4d8396f20bf9f2cfc28dde..0c8ebb7e0e6cdd48382c349fe55c806a2e06c705 100644
--- a/src/lib-sieve/sieve-code-dumper.c
+++ b/src/lib-sieve/sieve-code-dumper.c
@@ -174,6 +174,8 @@ void sieve_code_dumper_run
 	/* Mark end of the binary */
 	dumper->indent = 0;
 	dumper->mark_address = sieve_binary_get_code_size(dumper->dumpenv.sbin);
-
 	sieve_code_dumpf(&(dumper->dumpenv), "[End of code]");	
+
+	/* Add empty line to the file */
+	o_stream_send_str(dumper->dumpenv.stream, "\n");
 }
diff --git a/src/lib-sieve/sieve-error.c b/src/lib-sieve/sieve-error.c
index ba0207fdfed31d613d87073f4b9a0c65d0ce318e..d046b8093b2d8be4f4bb2e94fbe12247007e786f 100644
--- a/src/lib-sieve/sieve-error.c
+++ b/src/lib-sieve/sieve-error.c
@@ -4,6 +4,9 @@
 
 #include "sieve-error.h"
 
+/* FIMXE: This error handling is just a stub for what it should be. 
+ */
+
 struct sieve_error_handler {	
 	int errors;
 	int warnings;
diff --git a/src/lib-sieve/sieve-validator.c b/src/lib-sieve/sieve-validator.c
index d43d9e5d07781434f12e4cda5c1b273b2f434e78..8ef052121604ff5a57e3c7887638cd940d97ca7d 100644
--- a/src/lib-sieve/sieve-validator.c
+++ b/src/lib-sieve/sieve-validator.c
@@ -333,7 +333,6 @@ int sieve_validator_extension_load
 		return -1;
 	}
 	
-	i_info("loaded extension '%s'", ext->name);
 	return ext_id;
 }
 
diff --git a/src/lib-sieve/sieve.c b/src/lib-sieve/sieve.c
index 6843ccd6e9fcee84a80c34b2f3700a588ee81a4f..74b99bca25a04a5581064f5d201766002d9ca9a8 100644
--- a/src/lib-sieve/sieve.c
+++ b/src/lib-sieve/sieve.c
@@ -81,7 +81,7 @@ static struct sieve_binary *sieve_generate(struct sieve_ast *ast)
 	return result;
 }
 
-struct sieve_binary *sieve_compile(int fd, bool verbose) 
+struct sieve_binary *sieve_compile(int fd) 
 {
 	struct sieve_binary *result;
 	struct sieve_error_handler *ehandler;
@@ -91,50 +91,27 @@ struct sieve_binary *sieve_compile(int fd, bool verbose)
 	ehandler = sieve_error_handler_create();  
 	
 	/* Parse */
-
-	if ( verbose )
-		printf("Parsing sieve script...\n");
-	
 	if ( (ast = sieve_parse(fd, ehandler)) == NULL ) {
- 		printf("Parse failed.\n");
- 		return NULL;
- 	}
-
-	if ( verbose ) {
-	 	printf("Parse successful.\n");
-	 	sieve_ast_unparse(ast);
+ 		i_error("failed to parse script");
+		return NULL;
 	}
-	
+
 	/* Validate */
-	
-	if ( verbose )
-		printf("Validating script...\n");
-	
 	if ( !sieve_validate(ast, ehandler) ) {
-		printf("Validation failed.\n");
+		i_error("failed to validate script");
 		
  		sieve_ast_unref(&ast);
  		return NULL;
  	}
  	
- 	if ( verbose ) 
-	 	printf("Validation successful.\n");
-	
 	/* Generate */
-	
-	if ( verbose ) 
-		printf("Generating script...\n");
-	
 	if ( (result=sieve_generate(ast)) == NULL ) {
-		printf("Script generation failed.\n");
+		i_error("failed to generate script");
 		
 		sieve_ast_unref(&ast);
 		return NULL;
 	}
 	
-	if ( verbose )	
-		printf("Script generation successful.\n");
-	
 	/* Cleanup */
 	sieve_ast_unref(&ast);
 
@@ -145,7 +122,6 @@ void sieve_dump(struct sieve_binary *binary, struct ostream *stream)
 {
 	struct sieve_code_dumper *dumpr = sieve_code_dumper_create(binary);			
 
-	printf("Code Dump:\n\n");
 	sieve_code_dumper_run(dumpr, stream);	
 	
 	sieve_code_dumper_free(dumpr);
@@ -159,13 +135,10 @@ bool sieve_test
 	struct sieve_interpreter *interp = sieve_interpreter_create(binary);			
 	bool result = TRUE;
 							
-	printf("Code Execute:\n\n");
 	result = sieve_interpreter_run(interp, msgdata, menv, &sres);
 	
-	if ( result ) {
-		printf("Script executed successfully.\n\n");
+	if ( result ) 
 		sieve_result_print(sres);
-	}
 	
 	sieve_interpreter_free(interp);
 	sieve_result_unref(&sres);
@@ -180,7 +153,6 @@ bool sieve_execute
 	struct sieve_interpreter *interp = sieve_interpreter_create(binary);			
 	bool result = TRUE;
 							
-	printf("Code Execute:\n\n");
 	result = sieve_interpreter_run(interp, msgdata, menv, &sres);
 				
 	sieve_interpreter_free(interp);
diff --git a/src/lib-sieve/sieve.h b/src/lib-sieve/sieve.h
index 2f29e550dd99c8795f0ff0e0cc193142c2a648be..08883b40c92d2156f66cd1a4f4c1694bdea5d047 100644
--- a/src/lib-sieve/sieve.h
+++ b/src/lib-sieve/sieve.h
@@ -43,7 +43,7 @@ struct sieve_mail_environment {
 bool sieve_init(const char *plugins);
 void sieve_deinit(void);
 
-struct sieve_binary *sieve_compile(int fd, bool verbose);
+struct sieve_binary *sieve_compile(int fd);
 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/sieve-bin/bin-common.c b/src/sieve-bin/bin-common.c
index 01b9ede9ec66fb18b1be516d4455b50c1a99bfed..934f5b394e49cf4c3f1630e116671f76b3148fa4 100644
--- a/src/sieve-bin/bin-common.c
+++ b/src/sieve-bin/bin-common.c
@@ -1,5 +1,3 @@
-/* Copyright (c) 2005-2007 Dovecot authors, see the included COPYING file */
-
 #include "lib.h"
 #include "lib-signals.h"
 #include "ioloop.h"
@@ -15,6 +13,10 @@
 #include <fcntl.h>
 #include <pwd.h>
 
+/* Functionality common to all sieve test binaries */
+
+/* FIXME: this file is currently very messy */
+
 static struct ioloop *ioloop;
 
 static void sig_die(int signo, void *context ATTR_UNUSED)
@@ -39,7 +41,7 @@ void bin_init(void)
 	lib_signals_ignore(SIGALRM, FALSE);
 
 	if ( !sieve_init("") ) 
-		i_fatal("failed to initialize sieve implementation\n");
+		i_fatal("Failed to initialize sieve implementation\n");
 }
 
 void bin_deinit(void)
@@ -60,7 +62,7 @@ const char *bin_get_user(void)
 		return t_strdup(pw->pw_name);
 	} 
 		
-	i_fatal("couldn't lookup our username (uid=%s)", dec2str(process_euid));
+	i_fatal("Couldn't lookup our username (uid=%s)", dec2str(process_euid));
 	return NULL;
 }
 
@@ -69,15 +71,13 @@ struct sieve_binary *bin_compile_sieve_script(const char *filename)
 	int sfd;
 	struct sieve_binary *sbin;
 	
-	i_info("compiling sieve script '%s'...\n", filename);
-
 	if ( (sfd = open(filename, O_RDONLY)) < 0 ) 
-		i_info("failed to open sieve script %s: %m", filename);
+		i_fatal("Failed to open sieve script %s: %m", filename);
 	
-	if ( (sbin = sieve_compile(sfd, FALSE)) == NULL ) 
+	if ( (sbin = sieve_compile(sfd)) == NULL ) 
 	{
 		close(sfd);
-		i_fatal("failed to compile sieve script\n");
+		i_fatal("Failed to compile sieve script\n");
 	}
 		
 	close(sfd);
@@ -89,11 +89,13 @@ void bin_dump_sieve_binary_to(struct sieve_binary *sbin, const char *filename)
 	int dfd = -1;
 	struct ostream *dumpstream;
 	
+	if ( filename == NULL ) return;
+	
 	if ( strcmp(filename, "-") == 0 ) 
 		dumpstream = o_stream_create_fd(1, 0, FALSE);
 	else {
-		if ( (dfd = open(filename, O_WRONLY)) < 0 ) {
-			i_fatal("failed to open dump-file for writing: %m");
+		if ( (dfd = open(filename, O_WRONLY | O_CREAT)) < 0 ) {
+			i_fatal("Failed to open dump-file for writing: %m");
 			exit(1);
 		}
 		
@@ -104,9 +106,52 @@ void bin_dump_sieve_binary_to(struct sieve_binary *sbin, const char *filename)
 		(void) sieve_dump(sbin, dumpstream);
 		o_stream_destroy(&dumpstream);
 	} else {
-		i_fatal("failed to create stream for sieve code dump.");
+		i_fatal("Failed to create stream for sieve code dump.");
 	}
 	
 	if ( dfd != -1 )
 		close(dfd);
-}	
+}
+
+int bin_open_mail_file(const char *filename)
+{
+	int mfd;
+	
+	if ( strcmp(filename, "-") == 0 )
+		return 0;
+
+	if ( (mfd = open(filename, O_RDONLY)) < 0 ) 
+		i_fatal("Failed to open mail file: %m");			
+	
+	return mfd;
+}
+
+void bin_close_mail_file(int mfd)
+{
+	if ( mfd != 0 )
+		close(mfd);
+}
+
+void bin_fill_in_envelope
+	(struct mail *mail, const char **recipient, const char **sender)
+{
+	/* Get recipient address */
+	if ( *recipient == NULL ) 
+		(void)mail_get_first_header(mail, "Envelope-To", recipient);
+	if ( *recipient == NULL ) 
+		(void)mail_get_first_header(mail, "To", recipient);
+	if ( *recipient == NULL ) 
+		*recipient = "recipient@example.com";
+	
+	/* Get sender address */
+	if ( *sender == NULL ) 
+		(void)mail_get_first_header(mail, "Return-path", sender);
+	if ( *sender == NULL ) 
+		(void)mail_get_first_header(mail, "Sender", sender);
+	if ( *sender == NULL ) 
+		(void)mail_get_first_header(mail, "From", sender);
+	if ( *sender == NULL ) 
+		*sender = "sender@example.com";
+}
+
+
diff --git a/src/sieve-bin/bin-common.h b/src/sieve-bin/bin-common.h
index 1b75eb9fb8eafc11afa7cf4108389cb2e8d12241..1136196f4405237e78d955c6512aa0a2252c0ccc 100644
--- a/src/sieve-bin/bin-common.h
+++ b/src/sieve-bin/bin-common.h
@@ -3,11 +3,20 @@
 
 #include "sieve.h"
 
+/* Functionality common to all sieve test binaries */
+
 void bin_init(void);
 void bin_deinit(void);
 
 const char *bin_get_user(void);
+
 struct sieve_binary *bin_compile_sieve_script(const char *filename);
 void bin_dump_sieve_binary_to(struct sieve_binary *sbin, const char *filename);
 
+int bin_open_mail_file(const char *filename);
+void bin_close_mail_file(int mfd);
+
+void bin_fill_in_envelope
+	(struct mail *mail, const char **recipient, const char **sender);
+
 #endif /* __BIN_COMMON_H */
diff --git a/src/sieve-bin/sieve-exec.c b/src/sieve-bin/sieve-exec.c
index e061d97494f4a4f1b647c259d6634be8818fc884..19a4c21818495370d6f95b16e1a04e3f53baf8d0 100644
--- a/src/sieve-bin/sieve-exec.c
+++ b/src/sieve-bin/sieve-exec.c
@@ -54,10 +54,21 @@ static void duplicate_mark
 	i_info("marked duplicate for user %s.\n", user);
 }
 
+static void print_help(void)
+{
+	printf(
+"Usage: sieve-exec [-r <recipient address>][-s <envelope sender>]\n"
+"                  [-m <mailbox>][-d <dump filename>][-l <mail location>]\n"
+"                  <scriptfile> <mailfile>\n"
+	);
+}
+
 int main(int argc, char **argv) 
 {
+	const char *scriptfile, *recipient, *sender, *mailbox, *dumpfile, *mailfile;
+	const char *mailloc; 
 	const char *user;
-	int mfd;
+	int i, mfd;
 	pool_t namespaces_pool;
 	struct mail_namespace *ns;
 	struct mail_raw *mailr;
@@ -67,57 +78,105 @@ int main(int argc, char **argv)
 
 	bin_init();
 
-	if ( argc < 2 ) {
-		printf( "Usage: sieve-exec <sieve-file> [<mailfile>/-]\n");
- 		exit(1);
- 	}
-  
- 	/* Open mail file */
- 
-	if ( argc > 2 )
-	{
-		if ( strcmp(argv[2], "-") == 0 )
-			mfd = 0;
-		else {
-			if ( (mfd = open(argv[2], O_RDONLY)) < 0 ) {
-				perror("Failed to open mail file");
-				exit(1);
-			}
+	/* Parse arguments */
+	scriptfile = recipient = sender = mailbox = dumpfile = mailfile = NULL;
+	mailloc = NULL;
+	for (i = 1; i < argc; i++) {
+		if (strcmp(argv[i], "-r") == 0) {
+			/* recipient address */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -r argument");
+			recipient = argv[i];
+		} else if (strcmp(argv[i], "-s") == 0) {
+			/* envelope sender */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -s argument");
+			sender = argv[i];
+		} else if (strcmp(argv[i], "-m") == 0) {
+			/* default mailbox (keep box) */
+			i++;
+			if (i == argc) 
+				i_fatal("Missing -m argument");
+			mailbox = argv[i];
+		} else if (strcmp(argv[i], "-d") == 0) {
+			/* dump file */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -d argument");
+			dumpfile = argv[i];
+		} else if (strcmp(argv[i], "-l") == 0) {
+			/* mail location */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -l argument");
+			mailloc = argv[i];
+		} else if ( scriptfile == NULL ) {
+			scriptfile = argv[i];
+		} else if ( mailfile == NULL ) {
+			mailfile = argv[i];
+		} else {
+			print_help();
+			i_fatal("Unknown argument: %s", argv[i]);
 		}
-	} else
-		mfd = 0;
-
-	sbin = bin_compile_sieve_script(argv[1]);	 	
-	bin_dump_sieve_binary_to(sbin, "-");
+	}
+	
+	if ( scriptfile == NULL ) {
+		print_help();
+		i_fatal("Missing <scriptfile> argument");
+	}
+	
+	if ( mailfile == NULL ) {
+		print_help();
+		i_fatal("Missing <mailfile> argument");
+	}
 
+	/* Open the mail file */
+	mfd = bin_open_mail_file(mailfile);
+	
+	/* Compile sieve script */
+	sbin = bin_compile_sieve_script(scriptfile);
+	
+	/* Dump script */
+	bin_dump_sieve_binary_to(sbin, dumpfile);
+	
 	user = bin_get_user();
-
-	env_put(t_strdup_printf("NAMESPACE_1=%s", "maildir:/home/stephan/Maildir"));
-	env_put("NAMESPACE_1_INBOX=1");
-	env_put("NAMESPACE_1_LIST=1");
-	env_put("NAMESPACE_1_SEP=.");
-	env_put("NAMESPACE_1_SUBSCRIPTIONS=1");
-
 	namespaces_pool = namespaces_init();
+
+	if ( mailloc != NULL ) {
+		env_put(t_strdup_printf("NAMESPACE_1=%s", mailloc));
+		env_put("NAMESPACE_1_INBOX=1");
+		env_put("NAMESPACE_1_LIST=1");
+		env_put("NAMESPACE_1_SEP=.");
+		env_put("NAMESPACE_1_SUBSCRIPTIONS=1");
 	
-	if (mail_namespaces_init(namespaces_pool, user, &ns) < 0)
-		i_fatal("Namespace initialization failed");
+		if (mail_namespaces_init(namespaces_pool, user, &ns) < 0)
+			i_fatal("Namespace initialization failed");
+	} else {
+		ns = NULL;
+	}
 
 	mail_raw_init(namespaces_pool, user);
 	mailr = mail_raw_open(mfd);
 
+	bin_fill_in_envelope(mailr->mail, &recipient, &sender);
+
+	if ( mailbox == NULL )
+		mailbox = "INBOX";
+		
 	/* Collect necessary message data */
 	memset(&msgdata, 0, sizeof(msgdata));
 	msgdata.mail = mailr->mail;
-	msgdata.return_path = "nico@example.com";
-	msgdata.to_address = "sirius@rename-it.nl";
+	msgdata.return_path = sender;
+	msgdata.to_address = recipient;
 	msgdata.auth_user = "nico";
 	(void)mail_get_first_header(mailr->mail, "Message-ID", &msgdata.id);
 	
 	memset(&mailenv, 0, sizeof(mailenv));
 	mailenv.inbox = "INBOX";
 	mailenv.namespaces = ns;
-	mailenv.username = "stephan";
+	mailenv.username = user;
 	mailenv.hostname = "host.example.com";
 	mailenv.postmaster_address = "postmaster@example.com";
 	mailenv.smtp_open = sieve_smtp_open;
@@ -128,12 +187,12 @@ int main(int argc, char **argv)
 	/* Run */
 	sieve_execute(sbin, &msgdata, &mailenv);
 
+	bin_close_mail_file(mfd);
 	mail_raw_close(mailr);
-	close(mfd);
-
 	mail_raw_deinit();
-	mail_namespaces_deinit(&ns);
 	namespaces_deinit();
+
 	bin_deinit();  
 	return 0;
 }
+
diff --git a/src/sieve-bin/sieve-test.c b/src/sieve-bin/sieve-test.c
index 1385ba71e4119ef7a7de3398b30fc85a2ac5784a..e27b5adea8e2af7dec3aaa5578e11ee22bd17526 100644
--- a/src/sieve-bin/sieve-test.c
+++ b/src/sieve-bin/sieve-test.c
@@ -17,10 +17,19 @@
 #define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail"
 #define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
 
+static void print_help(void)
+{
+	printf(
+"Usage: sieve-test [-r <recipient address>][-s <envelope sender>]\n"
+"                  [-m <mailbox>][-d <dump filename>] <scriptfile> <mailfile>\n"
+	);
+}
+
 int main(int argc, char **argv) 
 {
+	const char *scriptfile, *recipient, *sender, *mailbox, *dumpfile, *mailfile; 
 	const char *user;
-	int mfd;
+	int i, mfd;
 	pool_t namespaces_pool;
 	struct mail_raw *mailr;
 	struct sieve_binary *sbin;
@@ -29,56 +38,91 @@ int main(int argc, char **argv)
 
 	bin_init();
 
-	if ( argc < 2 ) {
-		printf( "Usage: sieve-test <sieve-file> [<mailfile>/-]\n");
- 		exit(1);
- 	}
-
-	/* Open mail file */
-	if ( argc > 2 ) 
-	{
-		if ( strcmp(argv[2], "-") == 0 )
-			mfd = 0;
-		else {
-			if ( (mfd = open(argv[2], O_RDONLY)) < 0 ) {
-				perror("Failed to open mail file");
-				exit(1);
-			}
+	/* Parse arguments */
+	scriptfile = recipient = sender = mailbox = dumpfile = mailfile = NULL;
+	for (i = 1; i < argc; i++) {
+		if (strcmp(argv[i], "-r") == 0) {
+			/* recipient address */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -r argument");
+			recipient = argv[i];
+		} else if (strcmp(argv[i], "-s") == 0) {
+			/* envelope sender */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -s argument");
+			sender = argv[i];
+		} else if (strcmp(argv[i], "-m") == 0) {
+			/* default mailbox (keep box) */
+			i++;
+			if (i == argc) 
+				i_fatal("Missing -m argument");
+			mailbox = argv[i];
+		} else if (strcmp(argv[i], "-d") == 0) {
+			/* dump file */
+			i++;
+			if (i == argc)
+				i_fatal("Missing -d argument");
+			dumpfile = argv[i];
+		} else if ( scriptfile == NULL ) {
+			scriptfile = argv[i];
+		} else if ( mailfile == NULL ) {
+			mailfile = argv[i];
+		} else {
+			print_help();
+			i_fatal("Unknown argument: %s", argv[i]);
 		}
-	} else 
-		mfd = 0;
+	}
+	
+	if ( scriptfile == NULL ) {
+		print_help();
+		i_fatal("Missing <scriptfile> argument");
+	}
+	
+	if ( mailfile == NULL ) {
+		print_help();
+		i_fatal("Missing <mailfile> argument");
+	}
+
+	/* Open the mail file */
+	mfd = bin_open_mail_file(mailfile);
 	
 	/* Compile sieve script */
-	sbin = bin_compile_sieve_script(argv[1]);
+	sbin = bin_compile_sieve_script(scriptfile);
 	
 	/* Dump script */
-	bin_dump_sieve_binary_to(sbin, "-");
+	bin_dump_sieve_binary_to(sbin, dumpfile);
 	
 	user = bin_get_user();
+
 	namespaces_pool = namespaces_init();
 	mail_raw_init(namespaces_pool, user);
-
 	mailr = mail_raw_open(mfd);
 
+	bin_fill_in_envelope(mailr->mail, &recipient, &sender);
+
+	if ( mailbox == NULL )
+		mailbox = "INBOX";
+
 	/* Collect necessary message data */
 	memset(&msgdata, 0, sizeof(msgdata));
 	msgdata.mail = mailr->mail;
-	msgdata.return_path = "nico@example.com";
-	msgdata.to_address = "sirius+sieve@rename-it.nl";
-	msgdata.auth_user = "stephan";
+	msgdata.return_path = sender;
+	msgdata.to_address = recipient;
+	msgdata.auth_user = user;
 	(void)mail_get_first_header(mailr->mail, "Message-ID", &msgdata.id);
 
 	memset(&mailenv, 0, sizeof(mailenv));
-    mailenv.inbox = "INBOX";
-	mailenv.username = "stephan";
+	mailenv.inbox = "INBOX";
+	mailenv.username = user;
 	
 	/* Run the test */
 	(void) sieve_test(sbin, &msgdata, &mailenv);
 
+	bin_close_mail_file(mfd);
+	
 	mail_raw_close(mailr);
-	if ( mfd > 0 ) 
-		close(mfd);
-
 	mail_raw_deinit();
 	namespaces_deinit();
 
diff --git a/src/sieve-bin/sievec.c b/src/sieve-bin/sievec.c
index 29d91fec363ee9a706e691882ee422072daa1610..3178b0b0d7348275f75bffdc6b7127dad890b20b 100644
--- a/src/sieve-bin/sievec.c
+++ b/src/sieve-bin/sievec.c
@@ -22,7 +22,7 @@ int main(int argc, char **argv) {
  	}
   
 	sbin = bin_compile_sieve_script(argv[1]);
-	bin_dump_sieve_binary_to(sbin, "-");
+	bin_dump_sieve_binary_to(sbin, "frop.sdump");
 
 	bin_deinit();
 }