cmd-subscribe.c revision 80863ea81a6d6975c8b0d97657d850e8ac64e12e
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "common.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "commands.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "mail-namespace.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenbool cmd_subscribe_full(struct client_command_context *cmd, bool subscribe)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen{
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct mail_namespace *ns, *ns2;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct mail_storage *storage;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen struct mailbox_list *list;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen const char *mailbox, *verify_name;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen /* <mailbox> */
91e2dc36b9c0c91f0af716be81dc2aa6cbbed6c2Timo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen return FALSE;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen verify_name = mailbox;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ns = mail_namespace_find_subscribable(cmd->client->namespaces,
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen &mailbox);
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen if (ns == NULL) {
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen client_send_tagline(cmd, "NO Unknown namespace.");
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen return TRUE;
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen }
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen storage = ns->storage;
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen if ((client_workarounds & WORKAROUND_TB_EXTRA_MAILBOX_SEP) != 0 &&
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen *mailbox != '\0' && mailbox[strlen(mailbox)-1] ==
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen mail_storage_get_hierarchy_sep(storage)) {
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen /* verify the validity without the trailing '/' */
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen verify_name = t_strndup(verify_name, strlen(verify_name)-1);
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen }
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen ns2 = mail_namespace_find_prefix_nosep(cmd->client->namespaces,
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen mailbox);
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen if (ns2 != NULL && (ns2->flags & NAMESPACE_FLAG_LIST) != 0) {
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen /* subscribing to a listable namespace prefix, allow it. */
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen } else {
5c253723e8ef84cb71a80ced19efe597e8a90ea6Timo Sirainen if (!client_verify_mailbox_name(cmd, verify_name,
3313a51ef9b245248d672c20f930c52a577a42f7Timo Sirainen subscribe, FALSE))
ee6df9526e9716b3f1734d85b566e00fc41208bcTimo Sirainen return TRUE;
ee6df9526e9716b3f1734d85b566e00fc41208bcTimo Sirainen }
cd75c360f244c96b9ee10e01ee3a66fad13183c8Timo Sirainen
58bc77731bb25e900498a28409337e747f622722Timo Sirainen list = mail_storage_get_list(storage);
c2f24d55319fad0b6c03425f402f0cb0cb1a318bTimo Sirainen if (mailbox_list_set_subscribed(list, mailbox, subscribe) < 0)
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen client_send_storage_error(cmd, storage);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen else {
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen client_send_tagline(cmd, subscribe ?
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen "OK Subscribe completed." :
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen "OK Unsubscribe completed.");
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen }
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen return TRUE;
b0e9375a1ff97c9c7d40655922af5ccc73ecaa76Timo Sirainen}
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
b0e9375a1ff97c9c7d40655922af5ccc73ecaa76Timo Sirainenbool cmd_subscribe(struct client_command_context *cmd)
b0e9375a1ff97c9c7d40655922af5ccc73ecaa76Timo Sirainen{
b0e9375a1ff97c9c7d40655922af5ccc73ecaa76Timo Sirainen return cmd_subscribe_full(cmd, TRUE);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen