mail-storage.c revision df8a8bb3ce466273ef4239ed0ad7069348e59514
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (C) 2002-2006 Timo Sirainen */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "lib.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "ioloop.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "array.h"
472369cba85d9f7c995dda60e7cd01d78b4a960aTimo Sirainen#include "var-expand.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen#include "mail-index-private.h"
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include "mail-storage-private.h"
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include <stdlib.h>
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include <time.h>
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include <ctype.h>
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen/* Message to show to users when critical error occurs */
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen#define CRITICAL_MSG \
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen "Internal error occurred. Refer to server log for more information."
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen#define CRITICAL_MSG_STAMP CRITICAL_MSG " [%Y-%m-%d %H:%M:%S]"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen/* 20 * (200+1) < 4096 which is the standard PATH_MAX. Having these settings
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen prevents malicious user from creating eg. "a/a/a/.../a" mailbox name and
91b203fd2132510a47a4b34252c0ae0efd688a19Timo Sirainen then start renaming them to larger names from end to beginning, which
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen eventually would start causing the failures when trying to use too
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen long mailbox names. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#define MAILBOX_MAX_HIERARCHY_LEVELS 20
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen#define MAILBOX_MAX_HIERARCHY_NAME_LENGTH 200
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainenunsigned int mail_storage_module_id = 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenunsigned int mail_storage_mail_index_module_id = 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid (*hook_mailbox_opened)(struct mailbox *box) = NULL;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic ARRAY_DEFINE(storages, struct mail_storage *);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid mail_storage_init(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_array_init(&storages, 8);
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen mail_storage_mail_index_module_id = mail_index_module_id++;
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen}
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen
de486b59018016977015ef42e6071155b60e82e1Timo Sirainenvoid mail_storage_deinit(void)
de4d9f77d6378a416c9963963eac5ac18b75ec0bTimo Sirainen{
de4d9f77d6378a416c9963963eac5ac18b75ec0bTimo Sirainen if (array_is_created(&storages))
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen array_free(&storages);
de486b59018016977015ef42e6071155b60e82e1Timo Sirainen}
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainenvoid mail_storage_class_register(struct mail_storage *storage_class)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (storage_class->v.class_init != NULL)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen storage_class->v.class_init();
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen /* append it after the list, so the autodetection order is correct */
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen array_append(&storages, &storage_class, 1);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenvoid mail_storage_class_unregister(struct mail_storage *storage_class)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen struct mail_storage *const *classes;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen unsigned int i, count;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen classes = array_get(&storages, &count);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; i < count; i++) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (classes[i] == storage_class) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen array_delete(&storages, i, 1);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen break;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen storage_class->v.class_deinit();
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid mail_storage_parse_env(enum mail_storage_flags *flags_r,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum mail_storage_lock_method *lock_method_r)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const char *str;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen *flags_r = 0;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen if (getenv("FULL_FILESYSTEM_ACCESS") != NULL)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen *flags_r |= MAIL_STORAGE_FLAG_FULL_FS_ACCESS;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen if (getenv("DEBUG") != NULL)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen *flags_r |= MAIL_STORAGE_FLAG_DEBUG;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (getenv("MMAP_DISABLE") != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *flags_r |= MAIL_STORAGE_FLAG_MMAP_DISABLE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (getenv("MMAP_NO_WRITE") != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *flags_r |= MAIL_STORAGE_FLAG_MMAP_NO_WRITE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (getenv("MAIL_READ_MMAPED") != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *flags_r |= MAIL_STORAGE_FLAG_MMAP_MAILS;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (getenv("MAIL_SAVE_CRLF") != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *flags_r |= MAIL_STORAGE_FLAG_SAVE_CRLF;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str = getenv("POP3_UIDL_FORMAT");
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (str != NULL && (str = strchr(str, '%')) != NULL &&
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str != NULL && var_get_key(str + 1) == 'm')
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *flags_r |= MAIL_STORAGE_FLAG_KEEP_HEADER_MD5;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str = getenv("LOCK_METHOD");
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (str == NULL || strcmp(str, "fcntl") == 0)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen *lock_method_r = MAIL_STORAGE_LOCK_FCNTL;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen else if (strcmp(str, "flock") == 0)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen *lock_method_r = MAIL_STORAGE_LOCK_FLOCK;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen else if (strcmp(str, "dotlock") == 0)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen *lock_method_r = MAIL_STORAGE_LOCK_DOTLOCK;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen else
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen i_fatal("Unknown lock_method: %s", str);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen}
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenstatic struct mail_storage *mail_storage_find(const char *name)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen{
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen struct mail_storage *const *classes;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen unsigned int i, count;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(name != NULL);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen classes = array_get(&storages, &count);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; i < count; i++) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (strcasecmp(classes[i]->name, name) == 0)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return classes[i];
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return NULL;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstruct mail_storage *
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenmail_storage_create(const char *name, const char *data, const char *user,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum mail_storage_flags flags,
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen enum mail_storage_lock_method lock_method)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct mail_storage *storage;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen storage = mail_storage_find(name);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if (storage != NULL)
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen return storage->v.create(data, user, flags, lock_method);
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen else
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen return NULL;
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen}
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainenstruct mail_storage *
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainenmail_storage_create_default(const char *user, enum mail_storage_flags flags,
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen enum mail_storage_lock_method lock_method)
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen{
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen struct mail_storage *const *classes;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen struct mail_storage *storage;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen unsigned int i, count;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen classes = array_get(&storages, &count);
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen for (i = 0; i < count; i++) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen storage = classes[i]->v.create(NULL, user, flags, lock_method);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (storage != NULL)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return storage;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return NULL;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainenstatic struct mail_storage *
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenmail_storage_autodetect(const char *data, enum mail_storage_flags flags)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen struct mail_storage *const *classes;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen unsigned int i, count;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen classes = array_get(&storages, &count);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen for (i = 0; i < count; i++) {
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen if (classes[i]->v.autodetect(data, flags))
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen return classes[i];
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen }
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen return NULL;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstruct mail_storage *
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenmail_storage_create_with_data(const char *data, const char *user,
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen enum mail_storage_flags flags,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen enum mail_storage_lock_method lock_method)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct mail_storage *storage;
27586e4785d56aeb76e1fd96af8db799688dc64aTimo Sirainen const char *p, *name;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (data == NULL || *data == '\0')
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return mail_storage_create_default(user, flags, lock_method);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* check if we're in the form of mailformat:data
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen (eg. maildir:Maildir) */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen p = data;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen while (i_isalnum(*p)) p++;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (*p == ':') {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* no autodetection if the storage format is given. */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen flags |= MAIL_STORAGE_FLAG_NO_AUTODETECTION;
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk name = t_strdup_until(data, p);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk storage = mail_storage_create(name, p+1, user, flags,
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk lock_method);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk } else {
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk storage = mail_storage_autodetect(data, flags);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk if (storage == NULL) {
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk i_error("Ambiguous mail location setting, "
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "don't know what to do with it: %s "
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "(try prefixing it with mbox: or maildir:)",
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen data);
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen } else {
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen storage = storage->v.create(data, user, flags,
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen lock_method);
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen }
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen }
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen return storage;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenvoid mail_storage_destroy(struct mail_storage **_storage)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen{
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen struct mail_storage *storage = *_storage;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen i_assert(storage != NULL);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen *_storage = NULL;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen storage->v.destroy(storage);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen}
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainenvoid mail_storage_clear_error(struct mail_storage *storage)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen{
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen i_free(storage->error);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen storage->error = NULL;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen storage->syntax_error = FALSE;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen storage->temporary_error = FALSE;
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen}
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainenvoid mail_storage_set_error(struct mail_storage *storage, const char *fmt, ...)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen{
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen va_list va;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen mail_storage_clear_error(storage);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (fmt != NULL) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen va_start(va, fmt);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen storage->error = i_strdup_vprintf(fmt, va);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen va_end(va);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen}
5c597df6aa8d81de4053c6986fab7739f3b44b20Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenvoid mail_storage_set_syntax_error(struct mail_storage *storage,
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen const char *fmt, ...)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen va_list va;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen mail_storage_clear_error(storage);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (fmt != NULL) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen va_start(va, fmt);
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen storage->error = i_strdup_vprintf(fmt, va);
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen storage->syntax_error = TRUE;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen va_end(va);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenvoid mail_storage_set_internal_error(struct mail_storage *storage)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen struct tm *tm;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen char str[256];
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen tm = localtime(&ioloop_time);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_free(storage->error);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen storage->error =
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen strftime(str, sizeof(str), CRITICAL_MSG_STAMP, tm) > 0 ?
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen i_strdup(str) : i_strdup(CRITICAL_MSG);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen storage->syntax_error = FALSE;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen storage->temporary_error = TRUE;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenvoid mail_storage_set_critical(struct mail_storage *storage,
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen const char *fmt, ...)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen va_list va;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen mail_storage_clear_error(storage);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (fmt != NULL) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen va_start(va, fmt);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen i_error("%s", t_strdup_vprintf(fmt, va));
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen va_end(va);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen /* critical errors may contain sensitive data, so let user
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen see only "Internal error" with a timestamp to make it
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen easier to look from log files the actual error message. */
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen mail_storage_set_internal_error(storage);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainenchar mail_storage_get_hierarchy_sep(struct mail_storage *storage)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen{
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen return storage->hierarchy_sep;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenvoid mail_storage_set_callbacks(struct mail_storage *storage,
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen struct mail_storage_callbacks *callbacks,
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen void *context)
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen{
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen storage->v.set_callbacks(storage, callbacks, context);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainenint mail_storage_mailbox_create(struct mail_storage *storage, const char *name,
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen bool directory)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen{
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen return storage->v.mailbox_create(storage, name, directory);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenint mail_storage_mailbox_delete(struct mail_storage *storage, const char *name)
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen{
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen return storage->v.mailbox_delete(storage, name);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainenint mail_storage_mailbox_rename(struct mail_storage *storage,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *oldname, const char *newname)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return storage->v.mailbox_rename(storage, oldname, newname);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenstruct mailbox_list_context *
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenmail_storage_mailbox_list_init(struct mail_storage *storage,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *ref, const char *mask,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen enum mailbox_list_flags flags)
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen{
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen return storage->v.mailbox_list_init(storage, ref, mask, flags);
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen}
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainenstruct mailbox_list *
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainenmail_storage_mailbox_list_next(struct mailbox_list_context *ctx)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return ctx->storage->v.mailbox_list_next(ctx);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mail_storage_mailbox_list_deinit(struct mailbox_list_context **_ctx)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mailbox_list_context *ctx = *_ctx;
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen
4a514fb20e04df397842cde11cc9ea92abfe9728Timo Sirainen *_ctx = NULL;
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen return ctx->storage->v.mailbox_list_deinit(ctx);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mail_storage_set_subscribed(struct mail_storage *storage,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *name, bool set)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return storage->v.set_subscribed(storage, name, set);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mail_storage_get_mailbox_name_status(struct mail_storage *storage,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *name,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen enum mailbox_name_status *status)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return storage->v.get_mailbox_name_status(storage, name, status);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenconst char *mail_storage_get_last_error(struct mail_storage *storage,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen bool *syntax_error_r,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen bool *temporary_error_r)
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return storage->v.get_last_error(storage, syntax_error_r,
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen temporary_error_r);
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenconst char *mail_storage_get_mailbox_path(struct mail_storage *storage,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const char *name, bool *is_file_r)
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return storage->v.get_mailbox_path(storage, name, is_file_r);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenconst char *mail_storage_get_mailbox_control_dir(struct mail_storage *storage,
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen const char *name)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return storage->v.get_mailbox_control_dir(storage, name);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenconst char *mail_storage_get_mailbox_index_dir(struct mail_storage *storage,
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen const char *name)
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen{
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen return storage->v.get_mailbox_index_dir(storage, name);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen}
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenstruct mailbox *mailbox_open(struct mail_storage *storage, const char *name,
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen struct istream *input,
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen enum mailbox_open_flags flags)
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen{
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen struct mailbox *box;
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen box = storage->v.mailbox_open(storage, name, input, flags);
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen if (hook_mailbox_opened != NULL && box != NULL)
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen hook_mailbox_opened(box);
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen return box;
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen}
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainenint mailbox_close(struct mailbox **_box)
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen{
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen struct mailbox *box = *_box;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen *_box = NULL;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen return box->v.close(box);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen}
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstruct mail_storage *mailbox_get_storage(struct mailbox *box)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return box->storage;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenconst char *mailbox_get_name(struct mailbox *box)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return box->name;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenbool mailbox_is_readonly(struct mailbox *box)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return box->v.is_readonly(box);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenbool mailbox_allow_new_keywords(struct mailbox *box)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen return box->v.allow_new_keywords(box);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenint mailbox_get_status(struct mailbox *box,
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen enum mailbox_status_items items,
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen struct mailbox_status *status)
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen{
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen return box->v.get_status(box, items, status);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen}
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenstruct mailbox_sync_context *
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenmailbox_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen{
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen return box->v.sync_init(box, flags);
b82d6d7f02734007c129fa25bc876049c8d9bddeTimo Sirainen}
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenint mailbox_sync_next(struct mailbox_sync_context *ctx,
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen struct mailbox_sync_rec *sync_rec_r)
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen{
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen return ctx->box->v.sync_next(ctx, sync_rec_r);
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen}
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkint mailbox_sync_deinit(struct mailbox_sync_context **_ctx,
c78e7a94528078728cc639b26a1c83e11b4d7e1bTimo Sirainen struct mailbox_status *status_r)
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen{
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen struct mailbox_sync_context *ctx = *_ctx;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen *_ctx = NULL;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen return ctx->box->v.sync_deinit(ctx, status_r);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenvoid mailbox_notify_changes(struct mailbox *box, unsigned int min_interval,
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen mailbox_notify_callback_t *callback, void *context)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
0910ea0672c0295c442eb686cc41e98656831f37Timo Sirainen box->v.notify_changes(box, min_interval, callback, context);
0910ea0672c0295c442eb686cc41e98656831f37Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e91543761d0b7b97a1dc28e036e44d76405545c2Timo Sirainenstruct mail_keywords *
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenmailbox_keywords_create(struct mailbox_transaction_context *t,
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen const char *const keywords[])
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen return t->box->v.keywords_create(t, keywords);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenvoid mailbox_keywords_free(struct mailbox_transaction_context *t,
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen struct mail_keywords **_keywords)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen struct mail_keywords *keywords = *_keywords;
93688bfedcfb2b9c02750b8d4d409123a386de5cTimo Sirainen
93688bfedcfb2b9c02750b8d4d409123a386de5cTimo Sirainen *_keywords = NULL;
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen t->box->v.keywords_free(t, keywords);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenint mailbox_get_uids(struct mailbox *box, uint32_t uid1, uint32_t uid2,
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen uint32_t *seq1_r, uint32_t *seq2_r)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen return box->v.get_uids(box, uid1, uid2, seq1_r, seq2_r);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainenstruct mailbox_header_lookup_ctx *
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenmailbox_header_lookup_init(struct mailbox *box, const char *const headers[])
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen return box->v.header_lookup_init(box, headers);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenvoid mailbox_header_lookup_deinit(struct mailbox_header_lookup_ctx **_ctx)
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen{
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen struct mailbox_header_lookup_ctx *ctx = *_ctx;
fe813f74aaccb12f38e1bd9cd338c6a37fa646e5Timo Sirainen
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk *_ctx = NULL;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen ctx->box->v.header_lookup_deinit(ctx);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen}
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkstruct mail_search_context *
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkmailbox_search_init(struct mailbox_transaction_context *t,
c78e7a94528078728cc639b26a1c83e11b4d7e1bTimo Sirainen const char *charset, struct mail_search_arg *args,
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen const enum mail_sort_type *sort_program)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen return t->box->v.search_init(t, charset, args, sort_program);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainenint mailbox_search_deinit(struct mail_search_context **_ctx)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen struct mail_search_context *ctx = *_ctx;
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen *_ctx = NULL;
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen return ctx->transaction->box->v.search_deinit(ctx);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenint mailbox_search_next(struct mail_search_context *ctx, struct mail *mail)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
c78e7a94528078728cc639b26a1c83e11b4d7e1bTimo Sirainen return ctx->transaction->box->v.search_next(ctx, mail);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainenstruct mailbox_transaction_context *
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainenmailbox_transaction_begin(struct mailbox *box,
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen enum mailbox_transaction_flags flags)
b82d6d7f02734007c129fa25bc876049c8d9bddeTimo Sirainen{
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen return box->v.transaction_begin(box, flags);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen}
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenint mailbox_transaction_commit(struct mailbox_transaction_context **_t,
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen enum mailbox_sync_flags flags)
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen{
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen struct mailbox_transaction_context *t = *_t;
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen *_t = NULL;
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen return t->box->v.transaction_commit(t, flags);
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen}
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainenvoid mailbox_transaction_rollback(struct mailbox_transaction_context **_t)
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen{
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen struct mailbox_transaction_context *t = *_t;
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen *_t = NULL;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen t->box->v.transaction_rollback(t);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mailbox_save_init(struct mailbox_transaction_context *t,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen enum mail_flags flags, struct mail_keywords *keywords,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen time_t received_date, int timezone_offset,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *from_envelope, struct istream *input,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mail *dest_mail, struct mail_save_context **ctx_r)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (t->box->v.save_init(t, flags, keywords,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen received_date, timezone_offset,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen from_envelope, input, dest_mail, ctx_r) < 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return -1;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen (*ctx_r)->dest_mail = dest_mail;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mailbox_save_continue(struct mail_save_context *ctx)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return ctx->transaction->box->v.save_continue(ctx);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mailbox_save_finish(struct mail_save_context **_ctx)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mail_save_context *ctx = *_ctx;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen *_ctx = NULL;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return ctx->transaction->box->v.save_finish(ctx);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenvoid mailbox_save_cancel(struct mail_save_context **_ctx)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mail_save_context *ctx = *_ctx;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen *_ctx = NULL;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen ctx->transaction->box->v.save_cancel(ctx);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenint mailbox_copy(struct mailbox_transaction_context *t, struct mail *mail,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen enum mail_flags flags, struct mail_keywords *keywords,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mail *dest_mail)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return t->box->v.copy(t, mail, flags, keywords, dest_mail);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenbool mailbox_is_inconsistent(struct mailbox *box)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return box->v.is_inconsistent(box);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenbool mailbox_name_is_too_large(const char *name, char sep)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen unsigned int levels = 1, level_len = 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen for (; *name != '\0'; name++) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (*name == sep) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return TRUE;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen levels++;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen level_len = 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen } else {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen level_len++;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return TRUE;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (levels > MAILBOX_MAX_HIERARCHY_LEVELS)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return TRUE;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return FALSE;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen