changeset 9512:e4ba9799a1ac HEAD

Added imap_parse_system_flag().
author Timo Sirainen <tss@iki.fi>
date Tue, 23 Jun 2009 14:44:46 -0400
parents a81dfcf5a78d
children 0b7617d66ab1
files src/lib-imap/Makefile.am src/lib-imap/imap-util.c src/lib-imap/imap-util.h src/lib-imap/test-imap-util.c
diffstat 4 files changed, 57 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-imap/Makefile.am	Tue Jun 23 14:44:24 2009 -0400
+++ b/src/lib-imap/Makefile.am	Tue Jun 23 14:44:46 2009 -0400
@@ -43,7 +43,8 @@
 test_programs = \
 	test-imap-match \
 	test-imap-parser \
-	test-imap-utf7
+	test-imap-utf7 \
+	test-imap-util
 
 noinst_PROGRAMS = $(test_programs)
 
@@ -63,6 +64,10 @@
 test_imap_utf7_LDADD = imap-utf7.lo $(test_libs)
 test_imap_utf7_DEPENDENCIES = imap-utf7.lo $(test_libs)
 
+test_imap_util_SOURCES = test-imap-util.c
+test_imap_util_LDADD = imap-util.lo $(test_libs)
+test_imap_util_DEPENDENCIES = imap-util.lo $(test_libs)
+
 check: check-am check-test
 check-test: $(test_programs)
 	for bin in $(test_programs); do \
--- a/src/lib-imap/imap-util.c	Tue Jun 23 14:44:24 2009 -0400
+++ b/src/lib-imap/imap-util.c	Tue Jun 23 14:44:46 2009 -0400
@@ -40,6 +40,24 @@
 		str_truncate(dest, str_len(dest)-1);
 }
 
+enum mail_flags imap_parse_system_flag(const char *str)
+{
+	if (strcasecmp(str, "\\Answered") == 0)
+		return MAIL_ANSWERED;
+	else if (strcasecmp(str, "\\Flagged") == 0)
+		return MAIL_FLAGGED;
+	else if (strcasecmp(str, "\\Deleted") == 0)
+		return MAIL_DELETED;
+	else if (strcasecmp(str, "\\Seen") == 0)
+		return MAIL_SEEN;
+	else if (strcasecmp(str, "\\Draft") == 0)
+		return MAIL_DRAFT;
+	else if (strcasecmp(str, "\\Recent") == 0)
+		return MAIL_RECENT;
+	else
+		return 0;
+}
+
 void imap_write_seq_range(string_t *dest, const ARRAY_TYPE(seq_range) *array)
 {
 	const struct seq_range *range;
--- a/src/lib-imap/imap-util.h	Tue Jun 23 14:44:24 2009 -0400
+++ b/src/lib-imap/imap-util.h	Tue Jun 23 14:44:46 2009 -0400
@@ -9,6 +9,8 @@
 /* Write flags as a space separated string. */
 void imap_write_flags(string_t *dest, enum mail_flags flags,
 		      const char *const *keywords);
+/* Parse system flag from a string, or return 0 if it's invalid. */
+enum mail_flags imap_parse_system_flag(const char *str);
 
 /* Write sequence range as IMAP sequence-set */
 void imap_write_seq_range(string_t *dest, const ARRAY_TYPE(seq_range) *array);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-imap/test-imap-util.c	Tue Jun 23 14:44:46 2009 -0400
@@ -0,0 +1,31 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "mail-types.h"
+#include "imap-util.h"
+#include "test-common.h"
+
+static void test_imap_parse_system_flag(void)
+{
+	test_begin("imap_parse_system_flag");
+	test_assert(imap_parse_system_flag("\\aNswered") == MAIL_ANSWERED);
+	test_assert(imap_parse_system_flag("\\fLagged") == MAIL_FLAGGED);
+	test_assert(imap_parse_system_flag("\\dEleted") == MAIL_DELETED);
+	test_assert(imap_parse_system_flag("\\sEen") == MAIL_SEEN);
+	test_assert(imap_parse_system_flag("\\dRaft") == MAIL_DRAFT);
+	test_assert(imap_parse_system_flag("\\rEcent") == MAIL_RECENT);
+	test_assert(imap_parse_system_flag("answered") == 0);
+	test_assert(imap_parse_system_flag("\\broken") == 0);
+	test_assert(imap_parse_system_flag("\\") == 0);
+	test_assert(imap_parse_system_flag("") == 0);
+	test_end();
+}
+
+int main(void)
+{
+	static void (*test_functions[])(void) = {
+		test_imap_parse_system_flag,
+		NULL
+	};
+	return test_run(test_functions);
+}