Skip to content

Commit

Permalink
lib-storage: Add mail_namespace_alloc()
Browse files Browse the repository at this point in the history
This makes it easier to create new namespaces.
Based on patch by Jeff Sipek
  • Loading branch information
sirainen committed Nov 18, 2016
1 parent 2479d44 commit d5fa1f2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
56 changes: 38 additions & 18 deletions src/lib-storage/mail-namespace.c
Expand Up @@ -82,19 +82,23 @@ namespace_has_special_use_mailboxes(struct mail_namespace_settings *ns_set)
return FALSE;
}

int mail_namespaces_init_add(struct mail_user *user,
struct mail_namespace_settings *ns_set,
struct mail_namespace_settings *unexpanded_ns_set,
struct mail_namespace **ns_p, const char **error_r)
int mail_namespace_alloc(struct mail_user *user,
struct mail_namespace_settings *ns_set,
struct mail_namespace_settings *unexpanded_set,
struct mail_namespace **ns_r,
const char **error_r)
{
const struct mail_storage_settings *mail_set =
mail_user_set_get_storage_set(user);
struct mail_namespace *ns;
const char *driver, *error;
struct mail_namespace *ns;

ns = i_new(struct mail_namespace, 1);
ns->refcount = 1;
ns->user = user;
ns->prefix = i_strdup(ns_set->prefix);
ns->set = ns_set;
ns->unexpanded_set = unexpanded_set;
ns->mail_set = mail_user_set_get_storage_set(user);
i_array_init(&ns->all_storages, 2);

if (strcmp(ns_set->type, "private") == 0) {
ns->owner = user;
ns->type = MAIL_NAMESPACE_TYPE_PRIVATE;
Expand Down Expand Up @@ -128,11 +132,22 @@ int mail_namespaces_init_add(struct mail_user *user,
ns->flags |= NAMESPACE_FLAG_HIDDEN;
if (ns_set->subscriptions)
ns->flags |= NAMESPACE_FLAG_SUBSCRIPTIONS;
if (ns_set == &prefixless_ns_set) {
/* autocreated prefix="" namespace */
ns->flags |= NAMESPACE_FLAG_UNUSABLE |
NAMESPACE_FLAG_AUTOCREATED;
}

*ns_r = ns;

return 0;
}

int mail_namespaces_init_add(struct mail_user *user,
struct mail_namespace_settings *ns_set,
struct mail_namespace_settings *unexpanded_ns_set,
struct mail_namespace **ns_p, const char **error_r)
{
const struct mail_storage_settings *mail_set =
mail_user_set_get_storage_set(user);
struct mail_namespace *ns;
const char *driver, *error;
int ret;

if (*ns_set->location == '\0')
ns_set->location = mail_set->mail_location;
Expand All @@ -149,12 +164,17 @@ int mail_namespaces_init_add(struct mail_user *user,
ns_set->subscriptions ? "yes" : "no", ns_set->location);
}

ns->set = ns_set;
ns->unexpanded_set = unexpanded_ns_set;
ns->mail_set = mail_set;
ns->prefix = i_strdup(ns_set->prefix);
if ((ret = mail_namespace_alloc(user, ns_set, unexpanded_ns_set,
&ns, error_r)) < 0)
return ret;

if (ns_set == &prefixless_ns_set) {
/* autocreated prefix="" namespace */
ns->flags |= NAMESPACE_FLAG_UNUSABLE |
NAMESPACE_FLAG_AUTOCREATED;
}

ns->special_use_mailboxes = namespace_has_special_use_mailboxes(ns_set);
i_array_init(&ns->all_storages, 2);

if (ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
(strchr(ns->prefix, '%') != NULL ||
Expand Down
16 changes: 15 additions & 1 deletion src/lib-storage/mail-namespace.h
Expand Up @@ -77,15 +77,29 @@ struct mail_namespace {
unsigned int destroyed:1;
};


/* Allocate a new namespace, and fill it based on the passed in settings.
This is the most low-level namespace creation function. The storage isn't
initialized for the namespace. */
int mail_namespace_alloc(struct mail_user *user,
struct mail_namespace_settings *ns_set,
struct mail_namespace_settings *unexpanded_set,
struct mail_namespace **ns_r,
const char **error_r);

/* Add and initialize namespaces to user based on namespace settings. */
int mail_namespaces_init(struct mail_user *user, const char **error_r);
/* Add and initialize INBOX namespace to user based on the given location. */
int mail_namespaces_init_location(struct mail_user *user, const char *location,
const char **error_r) ATTR_NULL(2);
/* Add an empty namespace to user. */
struct mail_namespace *mail_namespaces_init_empty(struct mail_user *user);
/* Deinitialize all namespaces. mail_user_deinit() calls this automatically
for user's namespaces. */
void mail_namespaces_deinit(struct mail_namespace **namespaces);

/* Manually initialize namespaces one by one. */
/* Allocate a new namespace and initialize it. This is called automatically by
mail_namespaces_init(). */
int mail_namespaces_init_add(struct mail_user *user,
struct mail_namespace_settings *ns_set,
struct mail_namespace_settings *unexpanded_ns_set,
Expand Down

0 comments on commit d5fa1f2

Please sign in to comment.