changeset 5298:143c5942e392 HEAD

Added client_find_namespace(). Handle virtual != real hierarchy separators better. Give an error if real separator is tried to be used in the mailbox name.
author Timo Sirainen <tss@iki.fi>
date Tue, 13 Mar 2007 19:17:09 +0200
parents 67b8d61c988f
children 937afbbf61b2
files src/imap/commands-util.c src/imap/commands-util.h
diffstat 2 files changed, 46 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/imap/commands-util.c	Tue Mar 13 18:01:49 2007 +0200
+++ b/src/imap/commands-util.c	Tue Mar 13 19:17:09 2007 +0200
@@ -18,43 +18,66 @@
    to them, mbox/maildir currently allow paths only up to PATH_MAX. */
 #define MAILBOX_MAX_NAME_LEN 512
 
+struct namespace *
+client_find_namespace(struct client_command_context *cmd, const char **mailbox)
+{
+	struct namespace *ns;
+
+	ns = namespace_find(cmd->client->namespaces, mailbox);
+	if (ns != NULL)
+		return ns;
+
+	client_send_tagline(cmd, "NO Unknown namespace.");
+	return NULL;
+}
+
 struct mail_storage *
 client_find_storage(struct client_command_context *cmd, const char **mailbox)
 {
 	struct namespace *ns;
 
-	ns = namespace_find(cmd->client->namespaces, mailbox);
-	if (ns != NULL)
-		return ns->storage;
-
-	client_send_tagline(cmd, "NO Unknown namespace.");
-	return NULL;
+	ns = client_find_namespace(cmd, mailbox);
+	return ns == NULL ? NULL : ns->storage;
 }
 
 bool client_verify_mailbox_name(struct client_command_context *cmd,
 				const char *mailbox,
 				bool should_exist, bool should_not_exist)
 {
-	struct mail_storage *storage;
+	struct namespace *ns;
 	struct mailbox_list *list;
 	enum mailbox_name_status mailbox_status;
-	const char *p;
-	char sep;
+	const char *orig_mailbox, *p;
 
-	storage = client_find_storage(cmd, &mailbox);
-	if (storage == NULL)
+	orig_mailbox = mailbox;
+	ns = client_find_namespace(cmd, &mailbox);
+	if (ns == NULL)
 		return FALSE;
 
 	/* make sure it even looks valid */
-	sep = mail_storage_get_hierarchy_sep(storage);
 	if (*mailbox == '\0') {
 		client_send_tagline(cmd, "NO Empty mailbox name.");
 		return FALSE;
 	}
 
+	if (ns->real_sep != ns->sep && ns->prefix_len < strlen(orig_mailbox)) {
+		/* make sure there are no real separators used in the mailbox
+		   name. */
+		orig_mailbox += ns->prefix_len;
+		for (p = orig_mailbox; *p != '\0'; p++) {
+			if (*p == ns->real_sep) {
+				client_send_tagline(cmd, t_strdup_printf(
+					"NO Character not allowed "
+					"in mailbox name: '%c'",
+					ns->real_sep));
+				return FALSE;
+			}
+		}
+	}
+
 	/* make sure two hierarchy separators aren't next to each others */
 	for (p = mailbox+1; *p != '\0'; p++) {
-		if (p[0] == sep && p[-1] == sep) {
+		if (p[0] == ns->real_sep && p[-1] == ns->real_sep) {
 			client_send_tagline(cmd, "NO Invalid mailbox name.");
 			return FALSE;
 		}
@@ -66,10 +89,10 @@
 	}
 
 	/* check what our storage thinks of it */
-	list = mail_storage_get_list(storage);
+	list = mail_storage_get_list(ns->storage);
 	if (mailbox_list_get_mailbox_name_status(list, mailbox,
 						 &mailbox_status) < 0) {
-		client_send_storage_error(cmd, storage);
+		client_send_storage_error(cmd, ns->storage);
 		return FALSE;
 	}
 
@@ -87,13 +110,15 @@
 
 		client_send_tagline(cmd, t_strconcat(
 			"NO [TRYCREATE] Mailbox doesn't exist: ",
-			str_sanitize(mailbox, MAILBOX_MAX_NAME_LEN), NULL));
+			str_sanitize(orig_mailbox, MAILBOX_MAX_NAME_LEN),
+			NULL));
 		break;
 
 	case MAILBOX_NAME_INVALID:
 		client_send_tagline(cmd, t_strconcat(
 			"NO Invalid mailbox name: ",
-			str_sanitize(mailbox, MAILBOX_MAX_NAME_LEN), NULL));
+			str_sanitize(orig_mailbox, MAILBOX_MAX_NAME_LEN),
+			NULL));
 		break;
 
 	case MAILBOX_NAME_NOINFERIORS:
--- a/src/imap/commands-util.h	Tue Mar 13 18:01:49 2007 +0200
+++ b/src/imap/commands-util.h	Tue Mar 13 19:17:09 2007 +0200
@@ -9,6 +9,10 @@
 struct mail_full_flags;
 struct mailbox_keywords;
 
+/* Finds namespace for given mailbox from namespaces. If not found,
+   sends "Unknown namespace" error message to client. */
+struct namespace *
+client_find_namespace(struct client_command_context *cmd, const char **mailbox);
 /* Finds mail storage for given mailbox from namespaces. If not found,
    sends "Unknown namespace" error message to client. */
 struct mail_storage *