From e78f190500f6f6145239059aea28bf37e006cdbc Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sun, 2 Aug 2009 10:42:58 +0200
Subject: [PATCH] Imap4flags: now reads initial flags and keywords from
 supplied mail. Upon delivery this is empty, which still matches the
 specification.

---
 .../imap4flags/ext-imap4flags-common.c        | 33 +++++++++++++++++++
 tests/extensions/imap4flags/basic.svtest      | 19 +++++++++--
 2 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/src/lib-sieve/plugins/imap4flags/ext-imap4flags-common.c b/src/lib-sieve/plugins/imap4flags/ext-imap4flags-common.c
index 239cde941..ee975b0fe 100644
--- a/src/lib-sieve/plugins/imap4flags/ext-imap4flags-common.c
+++ b/src/lib-sieve/plugins/imap4flags/ext-imap4flags-common.c
@@ -167,6 +167,38 @@ struct ext_imap4flags_result_context {
     string_t *internal_flags;
 };
 
+static void _get_initial_flags
+(struct sieve_result *result, string_t *flags)
+{
+	const struct sieve_message_data *msgdata = 
+		sieve_result_get_message_data(result);
+	enum mail_flags mail_flags;
+	const char *const *mail_keywords;
+
+	mail_flags = mail_get_flags(msgdata->mail);
+	mail_keywords = mail_get_keywords(msgdata->mail);	
+
+	if ( (mail_flags & MAIL_FLAGGED) > 0 )
+		str_printfa(flags, " \\flagged");
+
+	if ( (mail_flags & MAIL_ANSWERED) > 0 )
+		str_printfa(flags, " \\answered");
+
+	if ( (mail_flags & MAIL_DELETED) > 0 )
+		str_printfa(flags, " \\deleted");
+
+	if ( (mail_flags & MAIL_SEEN) > 0 )
+		str_printfa(flags, " \\seen");
+
+	if ( (mail_flags & MAIL_DRAFT) > 0 )
+		str_printfa(flags, " \\draft");
+
+	while ( *mail_keywords != NULL ) {
+		str_printfa(flags, " %s", *mail_keywords);
+		mail_keywords++;
+	}	
+}
+
 static inline struct ext_imap4flags_result_context *_get_result_context
 (struct sieve_result *result)
 {
@@ -179,6 +211,7 @@ static inline struct ext_imap4flags_result_context *_get_result_context
 
 		rctx =p_new(pool, struct ext_imap4flags_result_context, 1);
 		rctx->internal_flags = str_new(pool, 32);
+		_get_initial_flags(result, rctx->internal_flags);
 
 		sieve_result_extension_set_context
 			(result, &imap4flags_extension, rctx);
diff --git a/tests/extensions/imap4flags/basic.svtest b/tests/extensions/imap4flags/basic.svtest
index 046fd6ee3..16682e220 100644
--- a/tests/extensions/imap4flags/basic.svtest
+++ b/tests/extensions/imap4flags/basic.svtest
@@ -11,11 +11,26 @@ require "comparator-i;ascii-numeric";
 
 test "Hasflag empty" {
 	if hasflag "\\Seen" {
-		test_fail "hasflag sees flags were there should be none";
+		test_fail "hasflag sees initial \\seen flag were there should be none";
+	}
+	if hasflag "\\draft" {
+		test_fail "hasflag sees initial \\draft flag were there should be none";
+	}
+	if hasflag "\\recent" {
+		test_fail "hasflag sees initial \\recent flag were there should be none";
+	}
+	if hasflag "\\flagged" {
+		test_fail "hasflag sees initial \\flagged flag were there should be none";
+	}
+	if hasflag "\\answered" {
+		test_fail "hasflag sees initial \\answered flag were there should be none";
+	}
+	if hasflag "\\deleted" {
+		test_fail "hasflag sees initial \\deleted flag were there should be none";
 	}
 
 	if hasflag :comparator "i;ascii-numeric" :count "ge" "1" {
-		test_fail "hasflag sees flags were there should be none";
+		test_fail "hasflag sees initial flags were there should be none";
 	}
 }
 
-- 
GitLab