--- dovecot-1.0-stable/src/imap/cmd-store.c.orig	Mon Jan 31 16:37:54 2005
+++ dovecot-1.0-stable/src/imap/cmd-store.c	Tue May 17 16:03:02 2005
@@ -78,6 +78,12 @@
 	}
 
 	box = client->mailbox;
+
+	if (mailbox_is_readonly(box)) {
+		client_send_tagline(client, "NO Mailbox is readonly.");
+		return TRUE;
+	}
+
 	search_arg = imap_search_get_arg(client, messageset, client->cmd_uid);
 	if (search_arg == NULL)
 		return TRUE;
--- dovecot-1.0-stable/src/imap/cmd-expunge.c.orig	Mon Jan 31 16:37:54 2005
+++ dovecot-1.0-stable/src/imap/cmd-expunge.c	Tue May 17 16:03:35 2005
@@ -23,6 +23,11 @@
 		return TRUE;
 	}
 
+	if (mailbox_is_readonly(client->mailbox)) {
+		client_send_tagline(client, "NO Mailbox is readonly.");
+		return TRUE;
+	}
+
 	search_arg = imap_search_get_arg(client, uidset, TRUE);
 	if (search_arg == NULL)
 		return TRUE;
@@ -41,6 +46,11 @@
 {
 	if (!client_verify_open_mailbox(client))
 		return TRUE;
+
+	if (mailbox_is_readonly(client->mailbox)) {
+		client_send_tagline(client, "NO Mailbox is readonly.");
+		return TRUE;
+	}
 
 	if (imap_expunge(client->mailbox, NULL)) {
 		return cmd_sync(client, MAILBOX_SYNC_FLAG_FAST,
--- dovecot-1.0-stable/src/imap/cmd-append.c.orig	Tue Mar 15 22:00:45 2005
+++ dovecot-1.0-stable/src/imap/cmd-append.c	Wed May 18 15:19:29 2005
@@ -359,11 +359,15 @@
 {
         struct cmd_append_context *ctx;
 	struct mailbox_status status;
-	const char *mailbox;
+	const char *mailbox, *mailbox_copy;
 
 	/* <mailbox> */
 	if (!client_read_string_args(client, 1, &mailbox))
 		return FALSE;
+
+	mailbox_copy = t_strdup(mailbox);
+	if (client_find_rw_storage(client, &mailbox_copy) == NULL)
+		return TRUE;
 
 	ctx = p_new(client->cmd_pool, struct cmd_append_context, 1);
 	ctx->client = client;
--- dovecot-1.0-stable/src/imap/cmd-rename.c.orig	Mon Jan 31 16:37:54 2005
+++ dovecot-1.0-stable/src/imap/cmd-rename.c	Wed May 18 15:03:32 2005
@@ -15,7 +15,7 @@
 	if (!client_verify_mailbox_name(client, newname, FALSE, TRUE))
 		return TRUE;
 
-	old_storage = client_find_storage(client, &oldname);
+	old_storage = client_find_rw_storage(client, &oldname);
 	if (old_storage == NULL)
 		return TRUE;
 
--- dovecot-1.0-stable/src/imap/cmd-delete.c.orig	Mon Jan 31 16:37:54 2005
+++ dovecot-1.0-stable/src/imap/cmd-delete.c	Wed May 18 15:05:12 2005
@@ -22,13 +22,17 @@
 	mailbox = client->mailbox;
 	if (mailbox != NULL && strcmp(mailbox_get_name(mailbox), name) == 0) {
 		/* deleting selected mailbox. close it first */
+		if (mailbox_is_readonly(mailbox)) {
+			client_send_tagline(client, "NO Mailbox is readonly.");
+			return TRUE;
+		}
 		storage = mailbox_get_storage(mailbox);
 		client->mailbox = NULL;
 
 		if (mailbox_close(mailbox) < 0)
 			client_send_untagged_storage_error(client, storage);
 	} else {
-		storage = client_find_storage(client, &name);
+		storage = client_find_rw_storage(client, &name);
 		if (storage == NULL)
 			return TRUE;
 	}
--- dovecot-1.0-stable/src/imap/cmd-create.c.orig	Mon May 16 15:58:06 2005
+++ dovecot-1.0-stable/src/imap/cmd-create.c	Wed May 18 15:06:16 2005
@@ -15,7 +15,7 @@
 		return FALSE;
 	full_mailbox = mailbox;
 
-	storage = client_find_storage(client, &mailbox);
+	storage = client_find_rw_storage(client, &mailbox);
 	if (storage == NULL)
 		return TRUE;
 
--- dovecot-1.0-stable/src/imap/cmd-copy.c.orig	Tue Mar 15 22:00:46 2005
+++ dovecot-1.0-stable/src/imap/cmd-copy.c	Wed May 18 15:09:49 2005
@@ -73,7 +73,7 @@
 	if (search_arg == NULL)
 		return TRUE;
 
-	storage = client_find_storage(client, &mailbox);
+	storage = client_find_rw_storage(client, &mailbox);
 	if (storage == NULL)
 		return TRUE;
 
--- dovecot-1.0-stable/src/imap/commands-util.c.orig	Tue Mar 15 22:00:46 2005
+++ dovecot-1.0-stable/src/imap/commands-util.c	Wed May 18 14:57:39 2005
@@ -29,6 +29,24 @@
 	return NULL;
 }
 
+struct mail_storage *
+client_find_rw_storage(struct client *client, const char **mailbox)
+{
+	struct namespace *ns;
+
+	ns = namespace_find(client->namespaces, mailbox);
+	if (ns == NULL) {
+		client_send_tagline(client, "NO Unknown namespace.");
+		return NULL;
+	}
+	if (ns->readonly) {
+		client_send_tagline(client, "NO Namespace is readonly.");
+		return NULL;
+	}
+
+	return ns->storage;
+}
+
 int client_verify_mailbox_name(struct client *client, const char *mailbox,
 			       int should_exist, int should_not_exist)
 {
--- dovecot-1.0-stable/src/imap/commands-util.h.orig	Tue Mar 15 22:00:46 2005
+++ dovecot-1.0-stable/src/imap/commands-util.h	Wed May 18 14:59:38 2005
@@ -13,6 +13,13 @@
 struct mail_storage *
 client_find_storage(struct client *client, const char **mailbox);
 
+/* Finds writeable mail storage for given mailbox from namespaces. If not found,
+   sends "Unknown namespace" error message to client or "Namespace is readonly"
+   error message if the namespace has the readonly attribute. */
+
+struct mail_storage *
+client_find_rw_storage(struct client *client, const char **mailbox);
+
 /* If should_exist is TRUE, this function returns TRUE if the mailbox
    exists. If it doesn't exist but would be a valid mailbox name, the
    error message is prefixed with [TRYCREATE].
