mail-user.c revision 4654f788834c9d7920a351306b89cf5d1c21772e
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2008 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen#include "lib.h"
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#include "array.h"
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen#include "mail-namespace.h"
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen#include "mail-user.h"
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenvoid (*hook_mail_user_created)(struct mail_user *user) = NULL;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen mail_namespaces_deinit(&user->namespaces);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen pool_unref(&user->pool);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen}
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenstruct mail_user *mail_user_init(const char *username, const char *home)
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen{
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen struct mail_user *user;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen pool_t pool;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen i_assert(username != NULL);
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen pool = pool_alloconly_create("mail user", 512);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen user = p_new(pool, struct mail_user, 1);
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen user->pool = pool;
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen user->username = p_strdup(pool, username);
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainen user->home = p_strdup(pool, home);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen user->v.deinit = mail_user_deinit_base;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (hook_mail_user_created != NULL)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen hook_mail_user_created(user);
79fcd3f95a6266cc62ceaa753e56dd4456ab7c4bTimo Sirainen return user;
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen}
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainen
3ddbbe03fe74b3ee7b1dff4e08ec706d7880d052Timo Sirainenvoid mail_user_deinit(struct mail_user **_user)
79fcd3f95a6266cc62ceaa753e56dd4456ab7c4bTimo Sirainen{
79fcd3f95a6266cc62ceaa753e56dd4456ab7c4bTimo Sirainen struct mail_user *user = *_user;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen *_user = NULL;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen user->v.deinit(user);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
d6c5ceea8521b92d10e51a59da00c792f6140b1dTimo Sirainenvoid mail_user_add_namespace(struct mail_user *user, struct mail_namespace *ns)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen struct mail_namespace **tmp, *next;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen
b1f57b7183fe44d7d7ba30b944d3de1f015b9253Timo Sirainen for (; ns != NULL; ns = next) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen next = ns->next;
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
a342a31752dd71ac444259ca57ad33ea6b79a572Timo Sirainen tmp = &user->namespaces;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen for (; *tmp != NULL; tmp = &(*tmp)->next) {
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen break;
88ea893b45d3ed8d68000921db9156c03cbe1b00Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen ns->next = *tmp;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen *tmp = ns;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
0011b7d11624ff4d72c1832081285c3edb90c2c3Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
15144da8b116d887e6954bc5ec6c86b1a61565e7Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen{
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen (void)mail_user_try_home_expand(user, &path);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen return path;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen}
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainen{
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainen const char *path = *pathp;
8039af9679af6fb56116b353fe44f7dd4c08f031Timo Sirainen
a5a0a25a2aa6c85e1bbf0f281d515b45018e190eTimo Sirainen if (path[0] == '~' && (path[1] == '/' || path[1] == '\0')) {
5ce2084ada06ade9f44fc2914c34658e9a842dc1Timo Sirainen if (user->home == NULL)
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return -1;
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen
ca9b4bd99287f7aef3065c9973f8d440bf5bbbc5Timo Sirainen *pathp = t_strconcat(user->home, path + 1, NULL);
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen }
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen return 0;
95d493c371891ea3b7598926d6b5464eabe27860Timo Sirainen}
2767104d81e97a109f0aa9758792bfa1da325a97Timo Sirainen