imapc-storage.c revision 5235a79ca4ea7a9bc7e64bd17ed1617357a25034
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher/* Copyright (c) 2011 Dovecot authors, see the included COPYING file */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
ee359fe1384507fed6c2274e7bfe81d288de4542Stephen Gallagher#include "lib.h"
33396dc46ea52c18f47db1b5d590880806521005Sumit Bose#include "ioloop.h"
ee359fe1384507fed6c2274e7bfe81d288de4542Stephen Gallagher#include "str.h"
33396dc46ea52c18f47db1b5d590880806521005Sumit Bose#include "imap-arg.h"
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher#include "imap-resp-code.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose#include "imapc-mail.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose#include "imapc-client.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose#include "imapc-list.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose#include "imapc-sync.h"
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher#include "imapc-storage.h"
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher#define DNS_CLIENT_SOCKET_NAME "dns-client"
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagherstruct imapc_open_context {
e65df5b966b27e13283c65f59f99ac44781e0333Simo Sorce struct imapc_mailbox *mbox;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher int ret;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher};
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstruct imapc_resp_code_map {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char *code;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher enum mail_error error;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher};
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher
cc98edd9479d4622634a1275c98058916c14059aStephen Gallagherextern struct mail_storage imapc_storage;
ee359fe1384507fed6c2274e7bfe81d288de4542Stephen Gallagherextern struct mailbox imapc_mailbox;
cc98edd9479d4622634a1275c98058916c14059aStephen Gallagher
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březinastatic struct imapc_resp_code_map imapc_resp_code_map[] = {
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina { IMAP_RESP_CODE_UNAVAILABLE, MAIL_ERROR_TEMP },
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina { IMAP_RESP_CODE_AUTHFAILED, MAIL_ERROR_PERM },
1183d29d87c5c7439cf2364b7d7324d4a13b6e35Stephen Gallagher { IMAP_RESP_CODE_AUTHZFAILED, MAIL_ERROR_PERM },
1183d29d87c5c7439cf2364b7d7324d4a13b6e35Stephen Gallagher { IMAP_RESP_CODE_EXPIRED, MAIL_ERROR_PERM },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_PRIVACYREQUIRED, MAIL_ERROR_PERM },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_CONTACTADMIN, MAIL_ERROR_PERM },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_NOPERM, MAIL_ERROR_PERM },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_INUSE, MAIL_ERROR_INUSE },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_EXPUNGEISSUED, MAIL_ERROR_EXPUNGED },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_CORRUPTION, MAIL_ERROR_TEMP },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_SERVERBUG, MAIL_ERROR_TEMP },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher /* { IMAP_RESP_CODE_CLIENTBUG, 0 }, */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_CANNOT, MAIL_ERROR_NOTPOSSIBLE },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_LIMIT, MAIL_ERROR_NOTPOSSIBLE },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_OVERQUOTA, MAIL_ERROR_NOSPACE },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_ALREADYEXISTS, MAIL_ERROR_EXISTS },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { IMAP_RESP_CODE_NONEXISTENT, MAIL_ERROR_NOTFOUND }
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce};
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorcestatic void imapc_untagged_status(const struct imapc_untagged_reply *reply,
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce struct imapc_storage *storage);
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorcestatic bool
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherimap_resp_text_code_parse(const char *str, enum mail_error *error_r)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned int i;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (str == NULL)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher return FALSE;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher for (i = 0; i < N_ELEMENTS(imapc_resp_code_map); i++) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (strcmp(imapc_resp_code_map[i].code, str) == 0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher *error_r = imapc_resp_code_map[i].error;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher return TRUE;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
d921c1eba437662437847279f251a0a5d8f70127Maxim return FALSE;
d921c1eba437662437847279f251a0a5d8f70127Maxim}
d921c1eba437662437847279f251a0a5d8f70127Maxim
d921c1eba437662437847279f251a0a5d8f70127Maximstatic struct mail_storage *imapc_storage_alloc(void)
d921c1eba437662437847279f251a0a5d8f70127Maxim{
d921c1eba437662437847279f251a0a5d8f70127Maxim struct imapc_storage *storage;
d921c1eba437662437847279f251a0a5d8f70127Maxim pool_t pool;
327127bb7fcc07f882209f029e14026de1b23c94Maxim
327127bb7fcc07f882209f029e14026de1b23c94Maxim pool = pool_alloconly_create("imapc storage", 512+256);
327127bb7fcc07f882209f029e14026de1b23c94Maxim storage = p_new(pool, struct imapc_storage, 1);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher storage->storage = imapc_storage;
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina storage->storage.pool = pool;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher return &storage->storage;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid imapc_copy_error_from_reply(struct imapc_storage *storage,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher enum mail_error default_error,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const struct imapc_command_reply *reply)
eb2e21b764d03544d8161e9956d7f70b07b75f77Simo Sorce{
bc9235cfb80bd64a3bfa959e8d26d5ad1be0bdf4Jakub Hrozek enum mail_error error;
bc9235cfb80bd64a3bfa959e8d26d5ad1be0bdf4Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (imap_resp_text_code_parse(reply->resp_text_key, &error)) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher mail_storage_set_error(&storage->storage, error,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher reply->text_without_resp);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher mail_storage_set_error(&storage->storage, default_error,
4b6a0d0b3d42e5fdb457f47d9adfa5e66b160256Stephen Gallagher reply->text_without_resp);
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid imapc_simple_callback(const struct imapc_command_reply *reply,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher void *context)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
068dbee9ca7bf5b37330eff91c94ae10f288d09fJakub Hrozek struct imapc_simple_context *ctx = context;
98ce3c3e85a4bb2e1822bf8ab2a1c2ab9e3dd61dJakub Hrozek
be65f065fef1d387281096ef095a2acef39ecc12Jakub Hrozek if (reply->state == IMAPC_COMMAND_STATE_OK)
e124844907ed6973915e4d56f5442ecd07535a12Jakub Hrozek ctx->ret = 0;
f36078af138f052cd9a30360867b0ebd0805af5eJakub Hrozek else if (reply->state == IMAPC_COMMAND_STATE_NO) {
34c78b745eb349eef2b0f13ef2b722632aebe619Jan Cholasta imapc_copy_error_from_reply(ctx->storage, MAIL_ERROR_PARAMS, reply);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher ctx->ret = -1;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher mail_storage_set_critical(&ctx->storage->storage,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher "imapc: Command failed: %s", reply->text_full);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher ctx->ret = -1;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher imapc_client_stop(ctx->storage->client);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervoid imapc_async_stop_callback(const struct imapc_command_reply *reply,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher void *context)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct imapc_storage *storage = context;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (reply->state == IMAPC_COMMAND_STATE_OK)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher ;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher else if (reply->state == IMAPC_COMMAND_STATE_NO) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher imapc_copy_error_from_reply(storage, MAIL_ERROR_PARAMS, reply);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher mail_storage_set_critical(&storage->storage,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher "imapc: Command failed: %s", reply->text_full);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher imapc_client_stop(storage->client);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bosestatic void imapc_storage_untagged_cb(const struct imapc_untagged_reply *reply,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher void *context)
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher{
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher struct imapc_storage *storage = context;
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher struct imapc_mailbox *mbox = reply->untagged_box_context;
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher const struct imapc_storage_event_callback *cb;
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher const struct imapc_mailbox_event_callback *mcb;
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher array_foreach(&storage->untagged_callbacks, cb) {
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher if (strcasecmp(reply->name, cb->name) == 0)
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher cb->callback(reply, storage);
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher }
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher if (mbox == NULL)
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher return;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek array_foreach(&mbox->untagged_callbacks, mcb) {
e6e26182d58c05d896f72f2925426658a6dc70b5Jakub Hrozek if (strcasecmp(reply->name, mcb->name) == 0)
e6e26182d58c05d896f72f2925426658a6dc70b5Jakub Hrozek mcb->callback(reply, mbox);
e6e26182d58c05d896f72f2925426658a6dc70b5Jakub Hrozek }
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (reply->resp_text_key != NULL) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher array_foreach(&mbox->resp_text_callbacks, mcb) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (strcasecmp(reply->resp_text_key, mcb->name) == 0)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher mcb->callback(reply, mbox);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic int
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherimapc_storage_create(struct mail_storage *_storage,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct mail_namespace *ns,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char **error_r)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct imapc_storage *storage = (struct imapc_storage *)_storage;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct imapc_client_settings set;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char *port, *value;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher string_t *str;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek memset(&set, 0, sizeof(set));
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek set.host = ns->list->set.root_dir;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek port = mail_user_plugin_getenv(_storage->user, "imapc_port");
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek if (port == NULL)
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek set.port = 143;
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek else {
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek if (str_to_uint(port, &set.port) < 0 ||
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek set.port == 0 || set.port >= 65536) {
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek *error_r = t_strdup_printf("Invalid port: %s", port);
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek return -1;
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek }
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek }
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek set.username = mail_user_plugin_getenv(_storage->user, "imapc_user");
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek if (set.username == NULL)
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek set.username = _storage->user->username;
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek set.password = mail_user_plugin_getenv(_storage->user, "pass");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (set.password == NULL) {
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek *error_r = "missing pass";
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher return -1;
f5b6f977d4144c28e9c66f3f1c9d634d595d1117Marko Myllynen }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher set.dns_client_socket_path =
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher t_strconcat(_storage->user->set->base_dir, "/",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher DNS_CLIENT_SOCKET_NAME, NULL);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher str = t_str_new(128);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher mail_user_set_get_temp_prefix(str, _storage->user->set);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher set.temp_path_prefix = str_c(str);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher set.ssl_ca_dir = mail_user_plugin_getenv(_storage->user,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher "imapc_ssl_ca_dir");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (set.ssl_ca_dir != NULL) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher value = mail_user_plugin_getenv(_storage->user,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher "imapc_ssl_starttls");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher set.ssl_mode = value != NULL ?
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher IMAPC_CLIENT_SSL_MODE_STARTTLS :
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher IMAPC_CLIENT_SSL_MODE_IMMEDIATE;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
5352c9b3609bca63814f9f6f03dbbbadf6c6333aStephen Gallagher storage->list = (struct imapc_mailbox_list *)ns->list;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher storage->list->storage = storage;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher storage->client = imapc_client_init(&set);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek p_array_init(&storage->untagged_callbacks, _storage->pool, 16);
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek imapc_client_register_untagged(storage->client,
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek imapc_storage_untagged_cb, storage);
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek imapc_list_register_callbacks(storage->list);
505383ec905863bb8f4f563f694b9bf077f9002cJakub Hrozek imapc_storage_register_untagged(storage, "STATUS",
505383ec905863bb8f4f563f694b9bf077f9002cJakub Hrozek imapc_untagged_status);
172c07013d1ea99447a780fd36f49d5c3a76981bJakub Hrozek return 0;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic void imapc_storage_destroy(struct mail_storage *_storage)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce struct imapc_storage *storage = (struct imapc_storage *)_storage;
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce imapc_client_deinit(&storage->client);
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce}
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce
336879aabae137f9a81304f147fb0d43001654b0Simo Sorcevoid imapc_storage_register_untagged(struct imapc_storage *storage,
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce const char *name,
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce imapc_storage_callback_t *callback)
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce{
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce struct imapc_storage_event_callback *cb;
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce cb = array_append_space(&storage->untagged_callbacks);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher cb->name = p_strdup(storage->storage.pool, name);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher cb->callback = callback;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic void
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherimapc_storage_get_list_settings(const struct mail_namespace *ns ATTR_UNUSED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct mailbox_list_settings *set)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher set->layout = MAILBOX_LIST_NAME_IMAPC;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic struct mailbox *
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherimapc_mailbox_alloc(struct mail_storage *storage, struct mailbox_list *list,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char *vname, enum mailbox_flags flags)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct imapc_mailbox *mbox;
96c73559adfbdac96720008fc022cb1d540b53c3Jakub Hrozek struct index_mailbox_context *ibox;
4dd615c01357b8715711aad6820ba9595d3ad377Stephen Gallagher pool_t pool;
70e59ed31c5a9c9ed02d9065ddf92be87c887efbJakub Hrozek
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose flags |= MAILBOX_FLAG_NO_INDEX_FILES;
f660877b38e563c4aa0cb1431624069808873fecJakub Hrozek
f1ce53a3b5656361557f80f61dfd42a371230c65Stephen Gallagher pool = pool_alloconly_create("imapc mailbox", 1024*3);
f1ce53a3b5656361557f80f61dfd42a371230c65Stephen Gallagher mbox = p_new(pool, struct imapc_mailbox, 1);
f1ce53a3b5656361557f80f61dfd42a371230c65Stephen Gallagher mbox->box = imapc_mailbox;
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher mbox->box.pool = pool;
mbox->box.storage = storage;
mbox->box.list = list;
mbox->box.mail_vfuncs = &imapc_mail_vfuncs;
index_storage_mailbox_alloc(&mbox->box, vname, flags, NULL);
ibox = INDEX_STORAGE_CONTEXT(&mbox->box);
ibox->save_commit_pre = imapc_transaction_save_commit_pre;
ibox->save_commit_post = imapc_transaction_save_commit_post;
ibox->save_rollback = imapc_transaction_save_rollback;
mbox->storage = (struct imapc_storage *)storage;
p_array_init(&mbox->untagged_callbacks, pool, 16);
p_array_init(&mbox->resp_text_callbacks, pool, 16);
imapc_mailbox_register_callbacks(mbox);
return &mbox->box;
}
static void
imapc_mailbox_open_callback(const struct imapc_command_reply *reply,
void *context)
{
struct imapc_open_context *ctx = context;
if (reply->state == IMAPC_COMMAND_STATE_OK)
ctx->ret = 0;
else if (reply->state == IMAPC_COMMAND_STATE_NO) {
imapc_copy_error_from_reply(ctx->mbox->storage,
MAIL_ERROR_NOTFOUND, reply);
ctx->ret = -1;
} else {
mail_storage_set_critical(ctx->mbox->box.storage,
"imapc: Opening mailbox '%s' failed: %s",
ctx->mbox->box.name, reply->text_full);
ctx->ret = -1;
}
if (!ctx->mbox->new_msgs)
imapc_client_stop(ctx->mbox->storage->client);
}
static int imapc_mailbox_open(struct mailbox *box)
{
struct imapc_mailbox *mbox = (struct imapc_mailbox *)box;
struct imapc_open_context ctx;
if (index_storage_mailbox_open(box, FALSE) < 0)
return -1;
if (box->deleting || (box->flags & MAILBOX_FLAG_SAVEONLY) != 0) {
/* We don't actually want to SELECT the mailbox. */
return 0;
}
mbox->opening = TRUE;
ctx.mbox = mbox;
ctx.ret = -1;
mbox->client_box =
imapc_client_mailbox_open(mbox->storage->client, box->name,
imapc_mailbox_open_callback,
&ctx, mbox);
imapc_client_run(mbox->storage->client);
mbox->opening = FALSE;
if (ctx.ret < 0) {
mailbox_close(box);
return -1;
}
return 0;
}
static void imapc_mailbox_close(struct mailbox *box)
{
struct imapc_mailbox *mbox = (struct imapc_mailbox *)box;
if (mbox->client_box != NULL)
imapc_client_mailbox_close(&mbox->client_box);
if (mbox->delayed_sync_view != NULL)
mail_index_view_close(&mbox->delayed_sync_view);
if (mbox->delayed_sync_trans != NULL) {
if (mail_index_transaction_commit(&mbox->delayed_sync_trans) < 0)
mail_storage_set_index_error(&mbox->box);
}
if (mbox->to_idle != NULL)
timeout_remove(&mbox->to_idle);
return index_storage_mailbox_close(box);
}
static int
imapc_mailbox_create(struct mailbox *box,
const struct mailbox_update *update ATTR_UNUSED,
bool directory)
{
struct imapc_mailbox *mbox = (struct imapc_mailbox *)box;
struct imapc_simple_context ctx;
const char *name = box->name;
if (directory) {
name = t_strdup_printf("%s%c", name,
mailbox_list_get_hierarchy_sep(box->list));
}
ctx.storage = mbox->storage;
imapc_client_cmdf(mbox->storage->client, imapc_simple_callback, &ctx,
"CREATE %s", name);
imapc_client_run(mbox->storage->client);
return ctx.ret;
}
static int imapc_mailbox_update(struct mailbox *box,
const struct mailbox_update *update ATTR_UNUSED)
{
mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
"Not supported");
return -1;
}
static void imapc_untagged_status(const struct imapc_untagged_reply *reply,
struct imapc_storage *storage)
{
struct mailbox_status *status;
const struct imap_arg *list;
const char *name, *key, *value;
uint32_t num;
unsigned int i;
if (!imap_arg_get_astring(&reply->args[0], &name) ||
!imap_arg_get_list(&reply->args[1], &list))
return;
if (storage->cur_status_box == NULL ||
strcmp(storage->cur_status_box->box.name, name) != 0)
return;
status = storage->cur_status;
for (i = 0; list[i].type != IMAP_ARG_EOL; i += 2) {
if (!imap_arg_get_atom(&list[i], &key) ||
!imap_arg_get_atom(&list[i+1], &value) ||
str_to_uint32(value, &num) < 0)
return;
if (strcasecmp(key, "MESSAGES") == 0)
status->messages = num;
else if (strcasecmp(key, "RECENT") == 0)
status->recent = num;
else if (strcasecmp(key, "UIDNEXT") == 0)
status->uidnext = num;
else if (strcasecmp(key, "UIDVALIDITY") == 0)
status->uidvalidity = num;
else if (strcasecmp(key, "UNSEEN") == 0)
status->unseen = num;
else if (strcasecmp(key, "HIGHESTMODSEQ") == 0)
status->highest_modseq = num;
}
}
static void imapc_mailbox_get_selected_status(struct imapc_mailbox *mbox,
enum mailbox_status_items items,
struct mailbox_status *status_r)
{
index_storage_get_status(&mbox->box, items, status_r);
}
static int imapc_mailbox_get_status(struct mailbox *box,
enum mailbox_status_items items,
struct mailbox_status *status_r)
{
struct imapc_mailbox *mbox = (struct imapc_mailbox *)box;
struct imapc_simple_context ctx;
string_t *str;
memset(status_r, 0, sizeof(*status_r));
if (box->opened) {
imapc_mailbox_get_selected_status(mbox, items, status_r);
return 0;
}
/* mailbox isn't opened yet */
if ((items & (STATUS_FIRST_UNSEEN_SEQ | STATUS_KEYWORDS)) != 0) {
/* getting these requires opening the mailbox */
if (mailbox_open(box) < 0)
return -1;
imapc_mailbox_get_selected_status(mbox, items, status_r);
return 0;
}
str = t_str_new(256);
if ((items & STATUS_MESSAGES) != 0)
str_append(str, " MESSAGES");
if ((items & STATUS_RECENT) != 0)
str_append(str, " RECENT");
if ((items & STATUS_UIDNEXT) != 0)
str_append(str, " UIDNEXT");
if ((items & STATUS_UIDVALIDITY) != 0)
str_append(str, " UIDVALIDITY");
if ((items & STATUS_UNSEEN) != 0)
str_append(str, " UNSEEN");
if ((items & STATUS_HIGHESTMODSEQ) != 0)
str_append(str, " HIGHESTMODSEQ");
if (str_len(str) == 0) {
/* nothing requested */
return 0;
}
ctx.storage = mbox->storage;
mbox->storage->cur_status_box = mbox;
mbox->storage->cur_status = status_r;
imapc_client_cmdf(mbox->storage->client,
imapc_simple_callback, &ctx,
"STATUS %s (%1s)", box->name, str_c(str)+1);
imapc_client_run(mbox->storage->client);
mbox->storage->cur_status_box = NULL;
mbox->storage->cur_status = NULL;
return ctx.ret;
}
static int imapc_mailbox_get_metadata(struct mailbox *box,
enum mailbox_metadata_items items,
struct mailbox_metadata *metadata_r)
{
if ((items & MAILBOX_METADATA_GUID) != 0) {
/* a bit ugly way to do this, but better than nothing for now.
FIXME: if indexes are enabled, keep this there. */
mail_generate_guid_128_hash(box->name, metadata_r->guid);
}
return index_mailbox_get_metadata(box, items, metadata_r);
}
static void imapc_notify_changes(struct mailbox *box ATTR_UNUSED)
{
/* we're doing IDLE all the time anyway - nothing to do here */
}
struct mail_storage imapc_storage = {
.name = IMAPC_STORAGE_NAME,
.class_flags = 0,
.v = {
NULL,
imapc_storage_alloc,
imapc_storage_create,
imapc_storage_destroy,
NULL,
imapc_storage_get_list_settings,
NULL,
imapc_mailbox_alloc,
NULL
}
};
struct mailbox imapc_mailbox = {
.v = {
index_storage_is_readonly,
index_storage_allow_new_keywords,
index_storage_mailbox_enable,
index_storage_mailbox_exists,
imapc_mailbox_open,
imapc_mailbox_close,
index_storage_mailbox_free,
imapc_mailbox_create,
imapc_mailbox_update,
index_storage_mailbox_delete,
index_storage_mailbox_rename,
imapc_mailbox_get_status,
imapc_mailbox_get_metadata,
NULL,
NULL,
imapc_mailbox_sync_init,
index_mailbox_sync_next,
imapc_mailbox_sync_deinit,
NULL,
imapc_notify_changes,
index_transaction_begin,
index_transaction_commit,
index_transaction_rollback,
NULL,
imapc_mail_alloc,
imapc_search_init,
imapc_search_deinit,
imapc_search_next_nonblock,
imapc_search_next_update_seq,
imapc_save_alloc,
imapc_save_begin,
imapc_save_continue,
imapc_save_finish,
imapc_save_cancel,
imapc_copy,
index_storage_is_inconsistent
}
};