summaryrefslogtreecommitdiffabout
path: root/libetpan/src/driver/implementation/maildir
Side-by-side diff
Diffstat (limited to 'libetpan/src/driver/implementation/maildir') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver.c676
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver.h53
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_cached.c1158
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_cached.h53
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.c334
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.h52
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_message.c255
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_message.h52
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_tools.c198
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_tools.h53
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver_types.h96
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirstorage.c193
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirstorage.h69
13 files changed, 3242 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver.c b/libetpan/src/driver/implementation/maildir/maildirdriver.c
new file mode 100644
index 0000000..a97fd43
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver.c
@@ -0,0 +1,676 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+
+/*
+ flags directory MUST be kept so that we can have other flags
+ than standards
+*/
+
+#include "maildirdriver.h"
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "maildir.h"
+#include "maildriver_tools.h"
+#include "maildirdriver_message.h"
+#include "maildirdriver_tools.h"
+#include "mailmessage.h"
+#include "generic_cache.h"
+
+static int initialize(mailsession * session);
+
+static void uninitialize(mailsession * session);
+
+static int connect_path(mailsession * session, char * path);
+
+static int logout(mailsession * session);
+
+static int expunge_folder(mailsession * session);
+
+static int status_folder(mailsession * session, char * mb,
+ uint32_t * result_messages, uint32_t * result_recent,
+ uint32_t * result_unseen);
+
+static int recent_number(mailsession * session, char * mb,
+ uint32_t * result);
+
+static int unseen_number(mailsession * session, char * mb,
+ uint32_t * result);
+
+static int messages_number(mailsession * session, char * mb,
+ uint32_t * result);
+
+static int append_message(mailsession * session,
+ char * message, size_t size);
+
+static int append_message_flags(mailsession * session,
+ char * message, size_t size, struct mail_flags * flags);
+
+static int get_messages_list(mailsession * session,
+ struct mailmessage_list ** result);
+
+static int get_envelopes_list(mailsession * session,
+ struct mailmessage_list * env_list);
+
+static int check_folder(mailsession * session);
+
+static int get_message_by_uid(mailsession * session,
+ const char * uid, mailmessage ** result);
+
+static mailsession_driver local_maildir_session_driver = {
+ .sess_name = "maildir",
+
+ .sess_initialize = initialize,
+ .sess_uninitialize = uninitialize,
+
+ .sess_parameters = NULL,
+
+ .sess_connect_stream = NULL,
+ .sess_connect_path = connect_path,
+ .sess_starttls = NULL,
+ .sess_login = NULL,
+ .sess_logout = logout,
+ .sess_noop = NULL,
+
+ .sess_build_folder_name = NULL,
+ .sess_create_folder = NULL,
+ .sess_delete_folder = NULL,
+ .sess_rename_folder = NULL,
+ .sess_check_folder = check_folder,
+ .sess_examine_folder = NULL,
+ .sess_select_folder = NULL,
+ .sess_expunge_folder = expunge_folder,
+ .sess_status_folder = status_folder,
+ .sess_messages_number = messages_number,
+ .sess_recent_number = recent_number,
+ .sess_unseen_number = unseen_number,
+ .sess_list_folders = NULL,
+ .sess_lsub_folders = NULL,
+ .sess_subscribe_folder = NULL,
+ .sess_unsubscribe_folder = NULL,
+
+ .sess_append_message = append_message,
+ .sess_append_message_flags = append_message_flags,
+ .sess_copy_message = NULL,
+ .sess_move_message = NULL,
+
+ .sess_get_messages_list = get_messages_list,
+ .sess_get_envelopes_list = get_envelopes_list,
+ .sess_remove_message = NULL,
+#if 0
+ .sess_search_messages = maildriver_generic_search_messages,
+#endif
+
+ .sess_get_message = NULL,
+ .sess_get_message_by_uid = get_message_by_uid,
+};
+
+mailsession_driver * maildir_session_driver = &local_maildir_session_driver;
+
+
+static int flags_store_process(struct maildir * md,
+ struct mail_flags_store * flags_store);
+
+
+static inline struct maildir_session_state_data * get_data(mailsession * session)
+{
+ return session->sess_data;
+}
+
+static struct maildir * get_maildir_session(mailsession * session)
+{
+ return get_data(session)->md_session;
+}
+
+static int initialize(mailsession * session)
+{
+ struct maildir_session_state_data * data;
+
+ data = malloc(sizeof(* data));
+ if (data == NULL)
+ goto err;
+
+ data->md_session = NULL;
+
+ data->md_flags_store = mail_flags_store_new();
+ if (data->md_flags_store == NULL)
+ goto free;
+
+ session->sess_data = data;
+
+ return MAIL_NO_ERROR;
+
+ free:
+ free(data);
+ err:
+ return MAIL_ERROR_MEMORY;
+}
+
+static void uninitialize(mailsession * session)
+{
+ struct maildir_session_state_data * data;
+
+ data = get_data(session);
+
+ if (data->md_session != NULL)
+ flags_store_process(data->md_session, data->md_flags_store);
+
+ mail_flags_store_free(data->md_flags_store);
+ if (data->md_session != NULL)
+ maildir_free(data->md_session);
+
+ free(data);
+
+ session->sess_data = NULL;
+}
+
+
+static int connect_path(mailsession * session, char * path)
+{
+ struct maildir * md;
+ int res;
+ int r;
+
+ if (get_maildir_session(session) != NULL) {
+ res = MAIL_ERROR_BAD_STATE;
+ goto err;
+ }
+
+ md = maildir_new(path);
+ if (md == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto free;
+ }
+
+ get_data(session)->md_session = md;
+
+ return MAIL_NO_ERROR;
+
+ free:
+ maildir_free(md);
+ err:
+ return res;
+}
+
+static int logout(mailsession * session)
+{
+ struct maildir * md;
+
+ check_folder(session);
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ maildir_free(md);
+ get_data(session)->md_session = NULL;
+
+ return MAIL_NO_ERROR;
+}
+
+/* folders operations */
+
+static int status_folder(mailsession * session, char * mb,
+ uint32_t * result_messages, uint32_t * result_recent,
+ uint32_t * result_unseen)
+{
+ int r;
+ struct maildir * md;
+ unsigned int i;
+ uint32_t messages;
+ uint32_t recent;
+ uint32_t unseen;
+
+ check_folder(session);
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR)
+ return maildirdriver_maildir_error_to_mail_error(r);
+
+ messages = 0;
+ recent = 0;
+ unseen = 0;
+ for(i = 0 ; i < carray_count(md->mdir_msg_list) ; i ++) {
+ struct maildir_msg * msg;
+
+ msg = carray_get(md->mdir_msg_list, i);
+ if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0)
+ recent ++;
+ if ((msg->msg_flags & MAILDIR_FLAG_SEEN) == 0)
+ unseen ++;
+ messages ++;
+ }
+
+ * result_messages = messages;
+ * result_recent = recent;
+ * result_unseen = unseen;
+
+ return MAIL_NO_ERROR;
+}
+
+static int messages_number(mailsession * session, char * mb,
+ uint32_t * result)
+{
+ struct maildir * md;
+ int r;
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR)
+ return maildirdriver_maildir_error_to_mail_error(r);
+
+ * result = carray_count(md->mdir_msg_list);
+
+ return MAIL_NO_ERROR;
+}
+
+static int unseen_number(mailsession * session, char * mb,
+ uint32_t * result)
+{
+ uint32_t messages;
+ uint32_t recent;
+ uint32_t unseen;
+ int r;
+
+ r = status_folder(session, mb, &messages, &recent, &unseen);
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ * result = unseen;
+
+ return MAIL_NO_ERROR;
+}
+
+static int recent_number(mailsession * session, char * mb,
+ uint32_t * result)
+{
+ uint32_t messages;
+ uint32_t recent;
+ uint32_t unseen;
+ int r;
+
+ r = status_folder(session, mb, &messages, &recent, &unseen);
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ * result = recent;
+
+ return MAIL_NO_ERROR;
+}
+
+
+/* messages operations */
+
+static int append_message(mailsession * session,
+ char * message, size_t size)
+{
+#if 0
+ struct maildir * md;
+ int r;
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_message_add(md, message, size);
+ if (r != MAILDIR_NO_ERROR)
+ return maildirdriver_maildir_error_to_mail_error(r);
+
+ return MAIL_NO_ERROR;
+#endif
+
+ return append_message_flags(session, message, size, NULL);
+}
+
+static int append_message_flags(mailsession * session,
+ char * message, size_t size, struct mail_flags * flags)
+{
+ struct maildir * md;
+ int r;
+ char uid[PATH_MAX];
+ struct maildir_msg * md_msg;
+ chashdatum key;
+ chashdatum value;
+ uint32_t md_flags;
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_message_add_uid(md, message, size,
+ uid, sizeof(uid));
+ if (r != MAILDIR_NO_ERROR)
+ return maildirdriver_maildir_error_to_mail_error(r);
+
+ if (flags == NULL)
+ goto exit;
+
+ key.data = uid;
+ key.len = strlen(uid);
+ r = chash_get(md->mdir_msg_hash, &key, &value);
+ if (r < 0)
+ goto exit;
+
+ md_msg = value.data;
+
+ md_flags = maildirdriver_flags_to_maildir_flags(flags->fl_flags);
+
+ r = maildir_message_change_flags(md, uid, md_flags);
+ if (r != MAILDIR_NO_ERROR)
+ goto exit;
+
+ return MAIL_NO_ERROR;
+
+ exit:
+ return MAIL_NO_ERROR;
+}
+
+static int get_messages_list(mailsession * session,
+ struct mailmessage_list ** result)
+{
+ struct maildir * md;
+ int r;
+ struct mailmessage_list * env_list;
+ int res;
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ r = maildir_get_messages_list(session, md,
+ maildir_message_driver, &env_list);
+ if (r != MAILDIR_NO_ERROR) {
+ res = r;
+ goto free_list;
+ }
+
+ * result = env_list;
+
+ return MAIL_NO_ERROR;
+
+ free_list:
+ mailmessage_list_free(env_list);
+ err:
+ return res;
+}
+
+static int get_envelopes_list(mailsession * session,
+ struct mailmessage_list * env_list)
+{
+ int r;
+ struct maildir * md;
+ unsigned int i;
+ int res;
+
+ check_folder(session);
+
+ md = get_maildir_session(session);
+ if (md == NULL) {
+ res = MAIL_ERROR_BAD_STATE;
+ goto err;
+ }
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ r = maildriver_generic_get_envelopes_list(session, env_list);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i++) {
+ struct maildir_msg * md_msg;
+ mailmessage * msg;
+ uint32_t driver_flags;
+ clist * ext;
+ chashdatum key;
+ chashdatum value;
+
+ msg = carray_get(env_list->msg_tab, i);
+
+ key.data = msg->msg_uid;
+ key.len = strlen(msg->msg_uid);
+ r = chash_get(md->mdir_msg_hash, &key, &value);
+ if (r < 0)
+ continue;
+
+ md_msg = value.data;
+
+ driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags);
+
+ if (msg->msg_flags == NULL) {
+ ext = clist_new();
+ if (ext == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ continue;
+ }
+
+ msg->msg_flags = mail_flags_new(driver_flags, ext);
+ if (msg->msg_flags == NULL) {
+ clist_free(ext);
+ res = MAIL_ERROR_MEMORY;
+ continue;
+ }
+
+ if ((md_msg->msg_flags & MAILDIR_FLAG_NEW) != 0) {
+ mail_flags_store_set(get_data(session)->md_flags_store, msg);
+ }
+ }
+ else {
+ msg->msg_flags->fl_flags &= MAIL_FLAG_FORWARDED;
+ msg->msg_flags->fl_flags |= driver_flags;
+ }
+ }
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
+
+
+static int expunge_folder(mailsession * session)
+{
+ unsigned int i;
+ int r;
+ int res;
+ struct maildir * md;
+
+ check_folder(session);
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ for(i = 0 ; i < carray_count(md->mdir_msg_list) ; i++) {
+ struct maildir_msg * md_msg;
+
+ md_msg = carray_get(md->mdir_msg_list, i);
+
+ if ((md_msg->msg_flags & MAILDIR_FLAG_TRASHED) != 0)
+ maildir_message_remove(md, md_msg->msg_uid);
+ }
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
+
+
+static int flags_store_process(struct maildir * md,
+ struct mail_flags_store * flags_store)
+{
+ unsigned int i;
+
+ if (carray_count(flags_store->fls_tab) == 0)
+ return MAIL_NO_ERROR;
+
+ for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
+ mailmessage * msg;
+ uint32_t md_flags;
+
+ msg = carray_get(flags_store->fls_tab, i);
+ md_flags = maildirdriver_flags_to_maildir_flags(msg->msg_flags->fl_flags);
+ md_flags &= ~MAILDIR_FLAG_NEW;
+
+ maildir_message_change_flags(md, msg->msg_uid, md_flags);
+ }
+
+ mail_flags_store_clear(flags_store);
+
+ return MAIL_NO_ERROR;
+}
+
+
+
+static int check_folder(mailsession * session)
+{
+ struct mail_flags_store * flags_store;
+ struct maildir_session_state_data * data;
+ struct maildir * md;
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ data = get_data(session);
+ flags_store = data->md_flags_store;
+
+ return flags_store_process(md, flags_store);
+}
+
+static int get_message_by_uid(mailsession * session,
+ const char * uid, mailmessage ** result)
+{
+ int r;
+ struct maildir * md;
+ int res;
+ mailmessage * msg;
+ char * msg_filename;
+ struct stat stat_info;
+
+ md = get_maildir_session(session);
+
+ /* update maildir data */
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ msg_filename = maildir_message_get(md, uid);
+ if (msg_filename == NULL) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ r = stat(msg_filename, &stat_info);
+ free(msg_filename);
+ if (r < 0) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ /* create message */
+
+ msg = mailmessage_new();
+ if (msg == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ r = mailmessage_init(msg, session, maildir_message_driver,
+ 0, stat_info.st_size);
+ if (r != MAIL_NO_ERROR) {
+ mailmessage_free(msg);
+ res = r;
+ goto err;
+ }
+
+ msg->msg_uid = strdup(uid);
+ if (msg->msg_uid == NULL) {
+ mailmessage_free(msg);
+ res = r;
+ goto err;
+ }
+
+ * result = msg;
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver.h b/libetpan/src/driver/implementation/maildir/maildirdriver.h
new file mode 100644
index 0000000..0abe09d
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver.h
@@ -0,0 +1,53 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRDRIVER_H
+
+#define MAILDIRDRIVER_H
+
+#include <libetpan/maildriver.h>
+#include <libetpan/maildirdriver_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern mailsession_driver * maildir_session_driver;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_cached.c b/libetpan/src/driver/implementation/maildir/maildirdriver_cached.c
new file mode 100644
index 0000000..3664362
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_cached.c
@@ -0,0 +1,1158 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#include "maildirdriver.h"
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mail.h"
+#include "maildir.h"
+#include "maildriver_tools.h"
+#include "maildirdriver_tools.h"
+#include "maildirdriver_cached_message.h"
+#include "mailmessage.h"
+#include "generic_cache.h"
+#include "imfcache.h"
+#include "mail_cache_db.h"
+#include "libetpan-config.h"
+
+static int initialize(mailsession * session);
+
+static void uninitialize(mailsession * session);
+
+static int parameters(mailsession * session,
+ int id, void * value);
+
+static int connect_path(mailsession * session, char * path);
+
+static int logout(mailsession * session);
+
+static int expunge_folder(mailsession * session);
+
+static int status_folder(mailsession * session, char * mb,
+ uint32_t * result_messages, uint32_t * result_recent,
+ uint32_t * result_unseen);
+
+static int recent_number(mailsession * session, char * mb,
+ uint32_t * result);
+
+static int unseen_number(mailsession * session, char * mb,
+ uint32_t * result);
+
+static int messages_number(mailsession * session, char * mb,
+ uint32_t * result);
+
+static int append_message(mailsession * session,
+ char * message, size_t size);
+
+static int append_message_flags(mailsession * session,
+ char * message, size_t size, struct mail_flags * flags);
+
+static int get_messages_list(mailsession * session,
+ struct mailmessage_list ** result);
+
+static int get_envelopes_list(mailsession * session,
+ struct mailmessage_list * env_list);
+
+static int check_folder(mailsession * session);
+
+static int get_message(mailsession * session,
+ uint32_t num, mailmessage ** result);
+
+static int get_message_by_uid(mailsession * session,
+ const char * uid, mailmessage ** result);
+
+static mailsession_driver local_maildir_cached_session_driver = {
+ .sess_name = "maildir-cached",
+
+ .sess_initialize = initialize,
+ .sess_uninitialize = uninitialize,
+
+ .sess_parameters = parameters,
+
+ .sess_connect_stream = NULL,
+ .sess_connect_path = connect_path,
+ .sess_starttls = NULL,
+ .sess_login = NULL,
+ .sess_logout = logout,
+ .sess_noop = NULL,
+
+ .sess_build_folder_name = NULL,
+ .sess_create_folder = NULL,
+ .sess_delete_folder = NULL,
+ .sess_rename_folder = NULL,
+ .sess_check_folder = check_folder,
+ .sess_examine_folder = NULL,
+ .sess_select_folder = NULL,
+ .sess_expunge_folder = expunge_folder,
+ .sess_status_folder = status_folder,
+ .sess_messages_number = messages_number,
+ .sess_recent_number = recent_number,
+ .sess_unseen_number = unseen_number,
+ .sess_list_folders = NULL,
+ .sess_lsub_folders = NULL,
+ .sess_subscribe_folder = NULL,
+ .sess_unsubscribe_folder = NULL,
+
+ .sess_append_message = append_message,
+ .sess_append_message_flags = append_message_flags,
+ .sess_copy_message = NULL,
+ .sess_move_message = NULL,
+
+ .sess_get_messages_list = get_messages_list,
+ .sess_get_envelopes_list = get_envelopes_list,
+ .sess_remove_message = NULL,
+#if 0
+ .sess_search_messages = maildriver_generic_search_messages,
+#endif
+
+ .sess_get_message = get_message,
+ .sess_get_message_by_uid = get_message_by_uid,
+};
+
+mailsession_driver * maildir_cached_session_driver =
+&local_maildir_cached_session_driver;
+
+
+static inline struct maildir_cached_session_state_data *
+get_cached_data(mailsession * session)
+{
+ return session->sess_data;
+}
+
+static inline mailsession * get_ancestor(mailsession * session)
+{
+ return get_cached_data(session)->md_ancestor;
+}
+
+static inline struct maildir_session_state_data *
+get_ancestor_data(mailsession * session)
+{
+ return get_ancestor(session)->sess_data;
+}
+
+
+static struct maildir * get_maildir_session(mailsession * session)
+{
+ return get_ancestor_data(session)->md_session;
+}
+
+static int initialize(mailsession * session)
+{
+ struct maildir_cached_session_state_data * data;
+
+ data = malloc(sizeof(* data));
+ if (data == NULL)
+ goto err;
+
+ data->md_ancestor = mailsession_new(maildir_session_driver);
+ if (data->md_ancestor == NULL)
+ goto free;
+
+ data->md_flags_store = mail_flags_store_new();
+ if (data->md_flags_store == NULL)
+ goto free_session;
+
+ data->md_quoted_mb = NULL;
+ data->md_cache_directory[0] = '\0';
+ data->md_flags_directory[0] = '\0';
+
+ session->sess_data = data;
+
+ return MAIL_NO_ERROR;
+
+ free_session:
+ mailsession_free(data->md_ancestor);
+ free:
+ free(data);
+ err:
+ return MAIL_ERROR_MEMORY;
+}
+
+static void
+free_quoted_mb(struct maildir_cached_session_state_data * maildir_cached_data)
+{
+ if (maildir_cached_data->md_quoted_mb != NULL) {
+ free(maildir_cached_data->md_quoted_mb);
+ maildir_cached_data->md_quoted_mb = NULL;
+ }
+}
+
+static int
+write_cached_flags(struct mail_cache_db * cache_db,
+ MMAPString * mmapstr,
+ char * uid, struct mail_flags * flags);
+
+#define ENV_NAME "env.db"
+#define FLAGS_NAME "flags.db"
+
+static int flags_store_process(char * flags_directory, char * quoted_mb,
+ struct mail_flags_store * flags_store)
+{
+ char filename_flags[PATH_MAX];
+ struct mail_cache_db * cache_db_flags;
+ MMAPString * mmapstr;
+ unsigned int i;
+ int r;
+ int res;
+
+ if (carray_count(flags_store->fls_tab) == 0)
+ return MAIL_NO_ERROR;
+
+ if (quoted_mb == NULL)
+ return MAIL_NO_ERROR;
+
+ snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s",
+ flags_directory, MAIL_DIR_SEPARATOR, quoted_mb,
+ MAIL_DIR_SEPARATOR, FLAGS_NAME);
+
+ r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
+ if (r < 0) {
+ res = MAIL_ERROR_FILE;
+ goto err;
+ }
+
+ mmapstr = mmap_string_new("");
+ if (mmapstr == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto close_db_flags;
+ }
+
+ for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
+ mailmessage * msg;
+
+ msg = carray_get(flags_store->fls_tab, i);
+
+ r = write_cached_flags(cache_db_flags, mmapstr,
+ msg->msg_uid, msg->msg_flags);
+ if (r != MAIL_NO_ERROR) {
+ /* ignore errors */
+ }
+ }
+
+ mmap_string_free(mmapstr);
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+
+ mail_flags_store_clear(flags_store);
+
+ return MAIL_NO_ERROR;
+
+ close_db_flags:
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+ err:
+ return res;
+}
+
+static void uninitialize(mailsession * session)
+{
+ struct maildir_cached_session_state_data * data;
+
+ data = get_cached_data(session);
+
+ flags_store_process(data->md_flags_directory,
+ data->md_quoted_mb,
+ data->md_flags_store);
+
+ mail_flags_store_free(data->md_flags_store);
+ mailsession_free(data->md_ancestor);
+ free_quoted_mb(data);
+ free(data);
+
+ session->sess_data = data;
+}
+
+
+static int parameters(mailsession * session,
+ int id, void * value)
+{
+ struct maildir_cached_session_state_data * data;
+ int r;
+
+ data = get_cached_data(session);
+
+ switch (id) {
+ case MAILDIRDRIVER_CACHED_SET_CACHE_DIRECTORY:
+ strncpy(data->md_cache_directory, value, PATH_MAX);
+ data->md_cache_directory[PATH_MAX - 1] = '\0';
+
+ r = generic_cache_create_dir(data->md_cache_directory);
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ return MAIL_NO_ERROR;
+
+ case MAILDIRDRIVER_CACHED_SET_FLAGS_DIRECTORY:
+ strncpy(data->md_flags_directory, value, PATH_MAX);
+ data->md_flags_directory[PATH_MAX - 1] = '\0';
+
+ r = generic_cache_create_dir(data->md_flags_directory);
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ return MAIL_NO_ERROR;
+
+ default:
+ return mailsession_parameters(data->md_ancestor, id, value);
+ }
+}
+
+
+static int get_cache_folder(mailsession * session, char ** result)
+{
+ struct maildir * md;
+ char * quoted_mb;
+ int res;
+ int r;
+ char key[PATH_MAX];
+ struct maildir_cached_session_state_data * data;
+
+ md = get_maildir_session(session);
+ data = get_cached_data(session);
+
+ quoted_mb = maildriver_quote_mailbox(md->mdir_path);
+ if (quoted_mb == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ snprintf(key, PATH_MAX, "%s/%s", data->md_cache_directory, quoted_mb);
+ r = generic_cache_create_dir(key);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto free_quoted_mb;
+ }
+
+ snprintf(key, PATH_MAX, "%s/%s", data->md_flags_directory, quoted_mb);
+ r = generic_cache_create_dir(key);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto free_quoted_mb;
+ }
+
+ * result = quoted_mb;
+
+ return MAIL_NO_ERROR;
+
+ free_quoted_mb:
+ free(quoted_mb);
+ err:
+ return res;
+}
+
+
+static int connect_path(mailsession * session, char * path)
+{
+ int r;
+ int res;
+ char * quoted_mb;
+
+ r = mailsession_connect_path(get_ancestor(session), path);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ r = get_cache_folder(session, &quoted_mb);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto logout;
+ }
+
+ get_cached_data(session)->md_quoted_mb = quoted_mb;
+
+ return MAILDIR_NO_ERROR;
+
+ logout:
+ mailsession_logout(get_ancestor(session));
+ err:
+ return res;
+}
+
+static int logout(mailsession * session)
+{
+ struct maildir_cached_session_state_data * data;
+ int r;
+
+ data = get_cached_data(session);
+
+ flags_store_process(data->md_flags_directory,
+ data->md_quoted_mb, data->md_flags_store);
+
+ r = mailsession_logout(get_ancestor(session));
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ free_quoted_mb(get_cached_data(session));
+
+ return MAIL_NO_ERROR;
+}
+
+static int status_folder(mailsession * session, char * mb,
+ uint32_t * result_messages, uint32_t * result_recent,
+ uint32_t * result_unseen)
+{
+ return mailsession_status_folder(get_ancestor(session), mb,
+ result_messages, result_recent, result_unseen);
+}
+
+static int messages_number(mailsession * session, char * mb,
+ uint32_t * result)
+{
+ return mailsession_messages_number(get_ancestor(session), mb, result);
+}
+
+static int unseen_number(mailsession * session, char * mb,
+ uint32_t * result)
+{
+ return mailsession_unseen_number(get_ancestor(session), mb, result);
+}
+
+static int recent_number(mailsession * session, char * mb,
+ uint32_t * result)
+{
+ return mailsession_recent_number(get_ancestor(session), mb, result);
+}
+
+
+static int append_message(mailsession * session,
+ char * message, size_t size)
+{
+#if 0
+ return mailsession_append_message(get_ancestor(session), message, size);
+#endif
+ return append_message_flags(session, message, size, NULL);
+}
+
+static int append_message_flags(mailsession * session,
+ char * message, size_t size, struct mail_flags * flags)
+{
+ struct maildir * md;
+ int r;
+ char uid[PATH_MAX];
+ struct maildir_msg * md_msg;
+ chashdatum key;
+ chashdatum value;
+ uint32_t md_flags;
+ struct mail_cache_db * cache_db_flags;
+ char filename_flags[PATH_MAX];
+ MMAPString * mmapstr;
+ struct maildir_cached_session_state_data * data;
+
+ md = get_maildir_session(session);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ r = maildir_message_add_uid(md, message, size,
+ uid, sizeof(uid));
+ if (r != MAILDIR_NO_ERROR)
+ return maildirdriver_maildir_error_to_mail_error(r);
+
+ if (flags == NULL)
+ goto exit;
+
+ data = get_cached_data(session);
+
+ snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s",
+ data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, FLAGS_NAME);
+
+ r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
+ if (r < 0)
+ goto exit;
+
+ mmapstr = mmap_string_new("");
+ if (mmapstr == NULL)
+ goto close_db_flags;
+
+ r = write_cached_flags(cache_db_flags, mmapstr,
+ uid, flags);
+
+ mmap_string_free(mmapstr);
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+
+ if (r != MAIL_NO_ERROR)
+ goto exit;
+
+ key.data = uid;
+ key.len = strlen(uid);
+ r = chash_get(md->mdir_msg_hash, &key, &value);
+ if (r < 0)
+ goto exit;
+
+ md_msg = value.data;
+
+ md_flags = maildirdriver_flags_to_maildir_flags(flags->fl_flags);
+
+ r = maildir_message_change_flags(md, uid, md_flags);
+ if (r != MAILDIR_NO_ERROR)
+ goto exit;
+
+ return MAIL_NO_ERROR;
+
+ close_db_flags:
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+ exit:
+ return MAIL_NO_ERROR;
+}
+
+#define UID_NAME "uid.db"
+
+static int uid_clean_up(struct mail_cache_db * uid_db,
+ struct mailmessage_list * env_list)
+{
+ chash * hash_exist;
+ int res;
+ int r;
+ unsigned int i;
+ chashdatum key;
+ chashdatum value;
+ char key_str[PATH_MAX];
+
+ /* flush cache */
+
+ hash_exist = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL);
+ if (hash_exist == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ value.data = NULL;
+ value.len = 0;
+
+ key.data = "max-uid";
+ key.len = strlen("max-uid");
+ r = chash_set(hash_exist, &key, &value, NULL);
+
+ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
+ mailmessage * msg;
+
+ msg = carray_get(env_list->msg_tab, i);
+
+ value.data = NULL;
+ value.len = 0;
+
+ key.data = msg->msg_uid;
+ key.len = strlen(msg->msg_uid);
+ r = chash_set(hash_exist, &key, &value, NULL);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto free;
+ }
+
+ snprintf(key_str, sizeof(key_str), "uid-%lu",
+ (unsigned long) msg->msg_index);
+ key.data = key_str;
+ key.len = strlen(key_str);
+ r = chash_set(hash_exist, &key, &value, NULL);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto free;
+ }
+ }
+
+ mail_cache_db_clean_up(uid_db, hash_exist);
+
+ chash_free(hash_exist);
+
+ return MAIL_NO_ERROR;
+
+ free:
+ chash_free(hash_exist);
+ err:
+ return res;
+}
+
+static int get_messages_list(mailsession * session,
+ struct mailmessage_list ** result)
+{
+ struct maildir * md;
+ int r;
+ struct mailmessage_list * env_list;
+ int res;
+ uint32_t max_uid;
+ char filename[PATH_MAX];
+ struct mail_cache_db * uid_db;
+ void * value;
+ size_t value_len;
+ unsigned long i;
+ struct maildir_cached_session_state_data * data;
+ char key[PATH_MAX];
+
+ data = get_cached_data(session);
+
+ md = get_maildir_session(session);
+ if (md == NULL) {
+ res = MAIL_ERROR_BAD_STATE;
+ goto err;
+ }
+
+ check_folder(session);
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ r = maildir_get_messages_list(session, md,
+ maildir_cached_message_driver, &env_list);
+ if (r != MAILDIR_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ /* read/write DB */
+
+ snprintf(filename, sizeof(filename), "%s%c%s%c%s",
+ data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, UID_NAME);
+
+ r = mail_cache_db_open_lock(filename, &uid_db);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto free_list;
+ }
+
+ max_uid = 0;
+ r = mail_cache_db_get(uid_db, "max-uid", sizeof("max-uid") - 1,
+ &value, &value_len);
+ if (r == 0) {
+ memcpy(&max_uid, value, sizeof(max_uid));
+ }
+
+ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
+ mailmessage * msg;
+ uint32_t index;
+
+ msg = carray_get(env_list->msg_tab, i);
+
+ r = mail_cache_db_get(uid_db, msg->msg_uid,
+ strlen(msg->msg_uid), &value, &value_len);
+ if (r < 0) {
+ max_uid ++;
+ msg->msg_index = max_uid;
+ mail_cache_db_put(uid_db, msg->msg_uid,
+ strlen(msg->msg_uid), &msg->msg_index, sizeof(msg->msg_index));
+
+ snprintf(key, sizeof(key), "uid-%lu", (unsigned long) msg->msg_index);
+ mail_cache_db_put(uid_db, key, strlen(key),
+ msg->msg_uid, strlen(msg->msg_uid));
+ }
+ else {
+ memcpy(&index, value, sizeof(index));
+ msg->msg_index = index;
+ }
+ }
+
+ mail_cache_db_put(uid_db, "max-uid", sizeof("max-uid") - 1,
+ &max_uid, sizeof(max_uid));
+
+ uid_clean_up(uid_db, env_list);
+
+ mail_cache_db_close_unlock(filename, uid_db);
+
+ * result = env_list;
+
+ return MAIL_NO_ERROR;
+
+ free_list:
+ mailmessage_list_free(env_list);
+ err:
+ return res;
+}
+
+static int
+get_cached_flags(struct mail_cache_db * cache_db,
+ MMAPString * mmapstr,
+ mailsession * session,
+ char * uid,
+ struct mail_flags ** result)
+{
+ int r;
+ char keyname[PATH_MAX];
+ struct mail_flags * flags;
+ int res;
+
+ snprintf(keyname, PATH_MAX, "%s-flags", uid);
+
+ r = generic_cache_flags_read(cache_db, mmapstr, keyname, &flags);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ * result = flags;
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
+
+static int
+get_cached_envelope(struct mail_cache_db * cache_db, MMAPString * mmapstr,
+ mailsession * session, char * uid,
+ struct mailimf_fields ** result)
+{
+ int r;
+ char keyname[PATH_MAX];
+ struct mailimf_fields * fields;
+ int res;
+
+ snprintf(keyname, PATH_MAX, "%s-envelope", uid);
+
+ r = generic_cache_fields_read(cache_db, mmapstr, keyname, &fields);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ * result = fields;
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
+
+static int
+write_cached_envelope(struct mail_cache_db * cache_db,
+ MMAPString * mmapstr,
+ mailsession * session, char * uid,
+ struct mailimf_fields * fields)
+{
+ int r;
+ char keyname[PATH_MAX];
+ int res;
+
+ snprintf(keyname, PATH_MAX, "%s-envelope", uid);
+
+ r = generic_cache_fields_write(cache_db, mmapstr, keyname, fields);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
+
+static int
+write_cached_flags(struct mail_cache_db * cache_db,
+ MMAPString * mmapstr,
+ char * uid, struct mail_flags * flags)
+{
+ int r;
+ char keyname[PATH_MAX];
+ int res;
+
+ snprintf(keyname, PATH_MAX, "%s-flags", uid);
+
+ r = generic_cache_flags_write(cache_db, mmapstr, keyname, flags);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ return MAIL_NO_ERROR;
+
+ err:
+ return res;
+}
+
+
+static int get_envelopes_list(mailsession * session,
+ struct mailmessage_list * env_list)
+{
+ int r;
+ unsigned int i;
+ int res;
+ struct maildir_cached_session_state_data * data;
+ char filename_env[PATH_MAX];
+ char filename_flags[PATH_MAX];
+ struct mail_cache_db * cache_db_env;
+ struct mail_cache_db * cache_db_flags;
+ MMAPString * mmapstr;
+
+ data = get_cached_data(session);
+
+ flags_store_process(data->md_flags_directory,
+ data->md_quoted_mb, data->md_flags_store);
+
+ mmapstr = mmap_string_new("");
+ if (mmapstr == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ snprintf(filename_env, PATH_MAX, "%s%c%s%c%s",
+ data->md_cache_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, ENV_NAME);
+
+ r = mail_cache_db_open_lock(filename_env, &cache_db_env);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto free_mmapstr;
+ }
+
+ snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s",
+ data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, FLAGS_NAME);
+
+ r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
+ if (r < 0) {
+ res = MAIL_ERROR_FILE;
+ goto close_db_env;
+ }
+
+ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i++) {
+ mailmessage * msg;
+ struct mailimf_fields * fields;
+ struct mail_flags * flags;
+
+ msg = carray_get(env_list->msg_tab, i);
+
+ if (msg->msg_fields == NULL) {
+ r = get_cached_envelope(cache_db_env, mmapstr, session,
+ msg->msg_uid, &fields);
+ if (r == MAIL_NO_ERROR) {
+ msg->msg_cached = TRUE;
+ msg->msg_fields = fields;
+ }
+ }
+
+ if (msg->msg_flags == NULL) {
+ r = get_cached_flags(cache_db_flags, mmapstr,
+ session, msg->msg_uid, &flags);
+ if (r == MAIL_NO_ERROR) {
+ msg->msg_flags = flags;
+ }
+ }
+ }
+
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+ mail_cache_db_close_unlock(filename_env, cache_db_env);
+
+ r = mailsession_get_envelopes_list(get_ancestor(session), env_list);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto free_mmapstr;
+ }
+
+ r = mail_cache_db_open_lock(filename_env, &cache_db_env);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto free_mmapstr;
+ }
+
+ r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
+ if (r < 0) {
+ res = MAIL_ERROR_FILE;
+ goto close_db_env;
+ }
+
+ /* must write cache */
+
+ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
+ mailmessage * msg;
+
+ msg = carray_get(env_list->msg_tab, i);
+
+ if (msg->msg_fields != NULL) {
+ if (!msg->msg_cached) {
+ /* msg->index is the numerical UID of the message */
+ r = write_cached_envelope(cache_db_env, mmapstr,
+ session, msg->msg_uid, msg->msg_fields);
+ }
+ }
+
+ if (msg->msg_flags != NULL) {
+ r = write_cached_flags(cache_db_flags, mmapstr,
+ msg->msg_uid, msg->msg_flags);
+ }
+ }
+
+ /* flush cache */
+
+ maildriver_cache_clean_up(cache_db_env, cache_db_flags, env_list);
+
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+ mail_cache_db_close_unlock(filename_env, cache_db_env);
+
+ mmap_string_free(mmapstr);
+
+ return MAIL_NO_ERROR;
+
+ close_db_env:
+ mail_cache_db_close_unlock(filename_env, cache_db_env);
+ free_mmapstr:
+ mmap_string_free(mmapstr);
+ err:
+ return res;
+}
+
+static int expunge_folder(mailsession * session)
+{
+ return mailsession_expunge_folder(get_ancestor(session));
+}
+
+static int check_folder(mailsession * session)
+{
+ struct maildir_cached_session_state_data * data;
+
+ data = get_cached_data(session);
+
+ flags_store_process(data->md_flags_directory,
+ data->md_quoted_mb, data->md_flags_store);
+
+ return mailsession_check_folder(get_ancestor(session));
+}
+
+static int get_message(mailsession * session,
+ uint32_t num, mailmessage ** result)
+{
+ struct maildir * md;
+ int res;
+ mailmessage * msg;
+ char filename[PATH_MAX];
+ struct mail_cache_db * uid_db;
+ char * msg_filename;
+ struct stat stat_info;
+ char key_str[PATH_MAX];
+ void * value;
+ size_t value_len;
+ char uid[PATH_MAX];
+ struct maildir_cached_session_state_data * data;
+ int r;
+
+ data = get_cached_data(session);
+
+ md = get_maildir_session(session);
+
+ /* a get_messages_list() should have been done once before */
+
+ /* read DB */
+
+ snprintf(filename, sizeof(filename), "%s%c%s%c%s",
+ data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, UID_NAME);
+
+ r = mail_cache_db_open_lock(filename, &uid_db);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ snprintf(key_str, sizeof(key_str), "uid-%lu", (unsigned long) num);
+
+ r = mail_cache_db_get(uid_db, key_str, strlen(key_str), &value, &value_len);
+ if (r < 0) {
+ res = MAIL_ERROR_INVAL;
+ goto close_db;
+ }
+
+ if (value_len >= PATH_MAX) {
+ res = MAIL_ERROR_INVAL;
+ goto close_db;
+ }
+
+ memcpy(uid, value, value_len);
+ uid[value_len] = '\0';
+
+ mail_cache_db_close_unlock(filename, uid_db);
+
+ /* update maildir data */
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ msg_filename = maildir_message_get(md, uid);
+ if (msg_filename == NULL) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ r = stat(msg_filename, &stat_info);
+ free(msg_filename);
+ if (r < 0) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ /* create message */
+
+ msg = mailmessage_new();
+ if (msg == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ r = mailmessage_init(msg, session, maildir_cached_message_driver,
+ num, stat_info.st_size);
+ if (r != MAIL_NO_ERROR) {
+ mailmessage_free(msg);
+ res = r;
+ goto err;
+ }
+
+ msg->msg_uid = strdup(uid);
+ if (msg->msg_uid == NULL) {
+ mailmessage_free(msg);
+ res = r;
+ goto err;
+ }
+
+ * result = msg;
+
+ return MAIL_NO_ERROR;
+
+ close_db:
+ mail_cache_db_close_unlock(filename, uid_db);
+ err:
+ return res;
+}
+
+
+static int get_message_by_uid(mailsession * session,
+ const char * uid, mailmessage ** result)
+{
+ int r;
+ struct maildir * md;
+ int res;
+ mailmessage * msg;
+ char filename[PATH_MAX];
+ struct mail_cache_db * uid_db;
+ char * msg_filename;
+ struct stat stat_info;
+ void * value;
+ size_t value_len;
+ struct maildir_cached_session_state_data * data;
+ uint32_t index;
+
+ data = get_cached_data(session);
+
+ md = get_maildir_session(session);
+
+ /* a get_messages_list() should have been done once before */
+
+ /* read DB */
+
+ snprintf(filename, sizeof(filename), "%s%c%s%c%s",
+ data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, UID_NAME);
+
+ r = mail_cache_db_open_lock(filename, &uid_db);
+ if (r < 0) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ r = mail_cache_db_get(uid_db, uid, strlen(uid), &value, &value_len);
+ if (r < 0) {
+ res = MAIL_ERROR_INVAL;
+ goto close_db;
+ }
+
+ memcpy(&index, value, sizeof(index));
+
+ mail_cache_db_close_unlock(filename, uid_db);
+
+ /* update maildir data */
+
+ r = maildir_update(md);
+ if (r != MAILDIR_NO_ERROR) {
+ res = maildirdriver_maildir_error_to_mail_error(r);
+ goto err;
+ }
+
+ msg_filename = maildir_message_get(md, uid);
+ if (msg_filename == NULL) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ r = stat(msg_filename, &stat_info);
+ free(msg_filename);
+ if (r < 0) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ /* create message */
+
+ msg = mailmessage_new();
+ if (msg == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ r = mailmessage_init(msg, session, maildir_cached_message_driver,
+ index, stat_info.st_size);
+ if (r != MAIL_NO_ERROR) {
+ mailmessage_free(msg);
+ res = r;
+ goto err;
+ }
+
+ msg->msg_uid = strdup(uid);
+ if (msg->msg_uid == NULL) {
+ mailmessage_free(msg);
+ res = r;
+ goto err;
+ }
+
+ * result = msg;
+
+ return MAIL_NO_ERROR;
+
+ close_db:
+ mail_cache_db_close_unlock(filename, uid_db);
+ err:
+ return res;
+}
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_cached.h b/libetpan/src/driver/implementation/maildir/maildirdriver_cached.h
new file mode 100644
index 0000000..5c3d8a9
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_cached.h
@@ -0,0 +1,53 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRDRIVER_CACHED_H
+
+#define MAILDIRDRIVER_CACHED_H
+
+#include <libetpan/maildriver.h>
+#include <libetpan/maildirdriver_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern mailsession_driver * maildir_cached_session_driver;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.c b/libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.c
new file mode 100644
index 0000000..d2c30cc
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.c
@@ -0,0 +1,334 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#include "maildirdriver_message.h"
+
+#include "mailmessage_tools.h"
+#include "maildirdriver.h"
+#include "maildir.h"
+#include "generic_cache.h"
+#include "mail_cache_db.h"
+#include "maildirdriver_tools.h"
+
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+
+static int get_flags(mailmessage * msg_info,
+ struct mail_flags ** result);
+
+static int prefetch(mailmessage * msg_info);
+
+static void prefetch_free(struct generic_message_t * msg);
+
+static int initialize(mailmessage * msg_info);
+
+static void check(mailmessage * msg_info);
+
+static mailmessage_driver local_maildir_cached_message_driver = {
+ .msg_name = "maildir-cached",
+
+ .msg_initialize = initialize,
+ .msg_uninitialize = mailmessage_generic_uninitialize,
+
+ .msg_flush = mailmessage_generic_flush,
+ .msg_check = check,
+
+ .msg_fetch_result_free = mailmessage_generic_fetch_result_free,
+
+ .msg_fetch = mailmessage_generic_fetch,
+ .msg_fetch_header = mailmessage_generic_fetch_header,
+ .msg_fetch_body = mailmessage_generic_fetch_header,
+ .msg_fetch_size = NULL,
+ .msg_get_bodystructure = mailmessage_generic_get_bodystructure,
+ .msg_fetch_section = mailmessage_generic_fetch_section,
+ .msg_fetch_section_header = mailmessage_generic_fetch_section_header,
+ .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime,
+ .msg_fetch_section_body = mailmessage_generic_fetch_section_body,
+ .msg_fetch_envelope = mailmessage_generic_fetch_envelope,
+
+ .msg_get_flags = get_flags,
+};
+
+mailmessage_driver * maildir_cached_message_driver =
+&local_maildir_cached_message_driver;
+
+struct maildir_msg_data {
+ int fd;
+};
+
+#if 0
+static inline struct maildir_cached_session_state_data *
+get_cached_session_data(mailmessage * msg)
+{
+ return msg->session->data;
+}
+
+static inline mailsession * cached_session_get_ancestor(mailsession * session)
+{
+ return get_data(session)->session;
+}
+
+static inline struct maildir_session_state_data *
+cached_session_get_ancestor_data(mailsession * session)
+{
+ return get_ancestor(session)->data;
+}
+
+static struct maildir * get_maildir_session(mailmessage * msg)
+{
+ return cached_session_get_ancestor_data(msg->session)->session;
+}
+#endif
+static inline struct maildir_cached_session_state_data *
+get_cached_session_data(mailmessage * msg)
+{
+ return msg->msg_session->sess_data;
+}
+
+static inline struct maildir_cached_session_state_data *
+cached_session_get_data(mailsession * s)
+{
+ return s->sess_data;
+}
+
+static inline mailsession * cached_session_get_ancestor(mailsession * s)
+{
+ return cached_session_get_data(s)->md_ancestor;
+}
+
+static inline struct maildir_session_state_data *
+cached_session_get_ancestor_data(mailsession * s)
+{
+ return cached_session_get_ancestor(s)->sess_data;
+}
+
+static inline struct maildir_session_state_data *
+get_session_ancestor_data(mailmessage * msg)
+{
+ return cached_session_get_ancestor_data(msg->msg_session);
+}
+
+static inline struct maildir *
+cached_session_get_maildir_session(mailsession * session)
+{
+ return cached_session_get_ancestor_data(session)->md_session;
+}
+
+static inline struct maildir * get_maildir_session(mailmessage * msg)
+{
+ return cached_session_get_maildir_session(msg->msg_session);
+}
+
+static int prefetch(mailmessage * msg_info)
+{
+ struct generic_message_t * msg;
+ int res;
+ struct maildir_msg_data * data;
+ char * filename;
+ int fd;
+ char * mapping;
+ struct maildir * md;
+
+ md = get_maildir_session(msg_info);
+
+ filename = maildir_message_get(md, msg_info->msg_uid);
+ if (filename == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ fd = open(filename, O_RDONLY);
+ free(filename);
+ if (fd == -1) {
+ res = MAIL_ERROR_FILE;
+ goto err;
+ }
+
+ mapping = mmap(NULL, msg_info->msg_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (mapping == MAP_FAILED) {
+ res = MAIL_ERROR_FILE;
+ goto close;
+ }
+
+ data = malloc(sizeof(* data));
+ if (data == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto unmap;
+ }
+
+ data->fd = fd;
+
+ msg = msg_info->msg_data;
+
+ msg->msg_data = data;
+ msg->msg_message = mapping;
+ msg->msg_length = msg_info->msg_size;
+
+ return MAIL_NO_ERROR;
+
+ unmap:
+ munmap(mapping, msg_info->msg_size);
+ close:
+ close(fd);
+ err:
+ return res;
+}
+
+static void prefetch_free(struct generic_message_t * msg)
+{
+ if (msg->msg_message != NULL) {
+ struct maildir_msg_data * data;
+
+ munmap(msg->msg_message, msg->msg_length);
+ msg->msg_message = NULL;
+ data = msg->msg_data;
+ close(data->fd);
+ free(data);
+ }
+}
+
+static int initialize(mailmessage * msg_info)
+{
+ struct generic_message_t * msg;
+ int r;
+
+ r = mailmessage_generic_initialize(msg_info);
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ msg = msg_info->msg_data;
+ msg->msg_prefetch = prefetch;
+ msg->msg_prefetch_free = prefetch_free;
+
+ return MAIL_NO_ERROR;
+}
+
+static void check(mailmessage * msg_info)
+{
+ int r;
+
+ if (msg_info->msg_flags != NULL) {
+ r = mail_flags_store_set(get_session_ancestor_data(msg_info)->md_flags_store, msg_info);
+
+ r = mail_flags_store_set(get_cached_session_data(msg_info)->md_flags_store, msg_info);
+ /* ignore errors */
+ }
+}
+
+#define FLAGS_NAME "flags.db"
+
+static int get_flags(mailmessage * msg_info,
+ struct mail_flags ** result)
+{
+ struct mail_cache_db * cache_db_flags;
+ chashdatum key;
+ chashdatum value;
+ struct maildir * md;
+ struct mail_flags * flags;
+ struct maildir_cached_session_state_data * data;
+ struct maildir_msg * md_msg;
+ int r;
+ uint32_t driver_flags;
+ char filename_flags[PATH_MAX];
+ char keyname[PATH_MAX];
+ MMAPString * mmapstr;
+
+ if (msg_info->msg_flags != NULL) {
+ * result = msg_info->msg_flags;
+ return MAIL_NO_ERROR;
+ }
+
+ data = get_cached_session_data(msg_info);
+ flags = mail_flags_store_get(data->md_flags_store,
+ msg_info->msg_index);
+ if (flags != NULL) {
+ msg_info->msg_flags = flags;
+ * result = msg_info->msg_flags;
+ return MAIL_NO_ERROR;
+ }
+
+ snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s",
+ data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb,
+ MAIL_DIR_SEPARATOR, FLAGS_NAME);
+
+ r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
+ if (r < 0)
+ return MAIL_ERROR_FILE;
+
+ snprintf(keyname, PATH_MAX, "%s-flags", msg_info->msg_uid);
+
+ mmapstr = mmap_string_new("");
+ if (mmapstr == NULL) {
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+ return MAIL_ERROR_MEMORY;
+ }
+
+ r = generic_cache_flags_read(cache_db_flags, mmapstr, keyname, &flags);
+ mmap_string_free(mmapstr);
+
+ mail_cache_db_close_unlock(filename_flags, cache_db_flags);
+
+ if (r != MAIL_NO_ERROR) {
+ flags = mail_flags_new_empty();
+ if (flags == NULL)
+ return MAIL_ERROR_MEMORY;
+ }
+
+ md = get_maildir_session(msg_info);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ key.data = msg_info->msg_uid;
+ key.len = strlen(msg_info->msg_uid);
+ r = chash_get(md->mdir_msg_hash, &key, &value);
+ if (r < 0)
+ return MAIL_ERROR_MSG_NOT_FOUND;
+
+ md_msg = value.data;
+
+ driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags);
+
+ flags->fl_flags = driver_flags;
+ msg_info->msg_flags = flags;
+
+ * result = msg_info->msg_flags;
+
+ return MAIL_NO_ERROR;
+}
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.h b/libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.h
new file mode 100644
index 0000000..b9e0215
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_cached_message.h
@@ -0,0 +1,52 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRDRIVER_CACHED_MESSAGE_H
+
+#define MAILDIRDRIVER_CACHED_MESSAGE_H
+
+#include <libetpan/maildirdriver_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern mailmessage_driver * maildir_cached_message_driver;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_message.c b/libetpan/src/driver/implementation/maildir/maildirdriver_message.c
new file mode 100644
index 0000000..58bc6bd
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_message.c
@@ -0,0 +1,255 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#include "maildirdriver_message.h"
+#include "maildirdriver_tools.h"
+
+#include "mailmessage_tools.h"
+#include "maildirdriver.h"
+#include "maildir.h"
+#include "generic_cache.h"
+
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+
+static int get_flags(mailmessage * msg_info,
+ struct mail_flags ** result);
+
+static int prefetch(mailmessage * msg_info);
+
+static void prefetch_free(struct generic_message_t * msg);
+
+static int initialize(mailmessage * msg_info);
+
+static void check(mailmessage * msg_info);
+
+static mailmessage_driver local_maildir_message_driver = {
+ .msg_name = "maildir",
+
+ .msg_initialize = initialize,
+ .msg_uninitialize = mailmessage_generic_uninitialize,
+
+ .msg_flush = mailmessage_generic_flush,
+ .msg_check = check,
+
+ .msg_fetch_result_free = mailmessage_generic_fetch_result_free,
+
+ .msg_fetch = mailmessage_generic_fetch,
+ .msg_fetch_header = mailmessage_generic_fetch_header,
+ .msg_fetch_body = mailmessage_generic_fetch_header,
+ .msg_fetch_size = NULL,
+ .msg_get_bodystructure = mailmessage_generic_get_bodystructure,
+ .msg_fetch_section = mailmessage_generic_fetch_section,
+ .msg_fetch_section_header = mailmessage_generic_fetch_section_header,
+ .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime,
+ .msg_fetch_section_body = mailmessage_generic_fetch_section_body,
+ .msg_fetch_envelope = mailmessage_generic_fetch_envelope,
+
+ .msg_get_flags = get_flags,
+};
+
+mailmessage_driver * maildir_message_driver = &local_maildir_message_driver;
+
+struct maildir_msg_data {
+ int fd;
+};
+
+static inline struct maildir_session_state_data *
+get_session_data(mailmessage * msg)
+{
+ return msg->msg_session->sess_data;
+}
+
+static struct maildir * get_maildir_session(mailmessage * msg)
+{
+ return get_session_data(msg)->md_session;
+}
+
+static int prefetch(mailmessage * msg_info)
+{
+ struct generic_message_t * msg;
+ int res;
+ struct maildir_msg_data * data;
+ char * filename;
+ int fd;
+ char * mapping;
+ struct maildir * md;
+
+ md = get_maildir_session(msg_info);
+
+ if (msg_info->msg_uid == NULL) {
+ res = MAIL_ERROR_INVAL;
+ goto err;
+ }
+
+ filename = maildir_message_get(md, msg_info->msg_uid);
+ if (filename == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ fd = open(filename, O_RDONLY);
+ free(filename);
+ if (fd == -1) {
+ res = MAIL_ERROR_FILE;
+ goto err;
+ }
+
+ mapping = mmap(NULL, msg_info->msg_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (mapping == MAP_FAILED) {
+ res = MAIL_ERROR_FILE;
+ goto close;
+ }
+
+ data = malloc(sizeof(* data));
+ if (data == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto unmap;
+ }
+
+ data->fd = fd;
+
+ msg = msg_info->msg_data;
+
+ msg->msg_data = data;
+ msg->msg_message = mapping;
+ msg->msg_length = msg_info->msg_size;
+
+ return MAIL_NO_ERROR;
+
+ unmap:
+ munmap(mapping, msg_info->msg_size);
+ close:
+ close(fd);
+ err:
+ return res;
+}
+
+static void prefetch_free(struct generic_message_t * msg)
+{
+ if (msg->msg_message != NULL) {
+ struct maildir_msg_data * data;
+
+ munmap(msg->msg_message, msg->msg_length);
+ msg->msg_message = NULL;
+ data = msg->msg_data;
+ close(data->fd);
+ free(data);
+ }
+}
+
+static int initialize(mailmessage * msg_info)
+{
+ struct generic_message_t * msg;
+ int r;
+
+ r = mailmessage_generic_initialize(msg_info);
+ if (r != MAIL_NO_ERROR)
+ return r;
+
+ msg = msg_info->msg_data;
+ msg->msg_prefetch = prefetch;
+ msg->msg_prefetch_free = prefetch_free;
+
+ return MAIL_NO_ERROR;
+}
+
+static void check(mailmessage * msg_info)
+{
+ int r;
+
+ if (msg_info->msg_flags != NULL) {
+ r = mail_flags_store_set(get_session_data(msg_info)->md_flags_store,
+ msg_info);
+ /* ignore errors */
+ }
+}
+
+static int get_flags(mailmessage * msg_info,
+ struct mail_flags ** result)
+{
+ chashdatum key;
+ chashdatum value;
+ struct maildir * md;
+ struct mail_flags * flags;
+ struct maildir_session_state_data * data;
+ struct maildir_msg * md_msg;
+ int r;
+ uint32_t driver_flags;
+ clist * ext;
+
+ if (msg_info->msg_flags != NULL) {
+ * result = msg_info->msg_flags;
+ return MAIL_NO_ERROR;
+ }
+
+ data = get_session_data(msg_info);
+ flags = mail_flags_store_get(data->md_flags_store,
+ msg_info->msg_index);
+ if (flags != NULL) {
+ msg_info->msg_flags = flags;
+ * result = msg_info->msg_flags;
+ return MAIL_NO_ERROR;
+ }
+
+ md = get_maildir_session(msg_info);
+ if (md == NULL)
+ return MAIL_ERROR_BAD_STATE;
+
+ key.data = msg_info->msg_uid;
+ key.len = strlen(msg_info->msg_uid);
+ r = chash_get(md->mdir_msg_hash, &key, &value);
+ if (r < 0)
+ return MAIL_ERROR_MSG_NOT_FOUND;
+
+ md_msg = value.data;
+
+ driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags);
+
+ ext = clist_new();
+ if (ext == NULL)
+ return MAIL_ERROR_MEMORY;
+
+ msg_info->msg_flags = mail_flags_new(driver_flags, ext);
+
+ * result = msg_info->msg_flags;
+
+ return MAIL_NO_ERROR;
+}
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_message.h b/libetpan/src/driver/implementation/maildir/maildirdriver_message.h
new file mode 100644
index 0000000..ed0a4d1
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_message.h
@@ -0,0 +1,52 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRDRIVER_MESSAGE_H
+
+#define MAILDIRDRIVER_MESSAGE_H
+
+#include <libetpan/maildirdriver_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern mailmessage_driver * maildir_message_driver;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_tools.c b/libetpan/src/driver/implementation/maildir/maildirdriver_tools.c
new file mode 100644
index 0000000..e3036e8
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_tools.c
@@ -0,0 +1,198 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#include "mailmessage.h"
+#include "maildirdriver_tools.h"
+#include "maildir.h"
+#include "generic_cache.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <string.h>
+
+int maildirdriver_maildir_error_to_mail_error(int error)
+{
+ switch (error) {
+ case MAILDIR_NO_ERROR:
+ return MAIL_NO_ERROR;
+
+ case MAILDIR_ERROR_CREATE:
+ return MAIL_ERROR_FILE;
+
+ case MAILDIR_ERROR_DIRECTORY:
+ return MAIL_ERROR_FILE;
+
+ case MAILDIR_ERROR_MEMORY:
+ return MAIL_ERROR_MEMORY;
+
+ case MAILDIR_ERROR_FILE:
+ return MAIL_ERROR_FILE;
+
+ case MAILDIR_ERROR_FOLDER:
+ return MAIL_ERROR_FOLDER;
+
+ case MAILDIR_ERROR_NOT_FOUND:
+ return MAIL_ERROR_MSG_NOT_FOUND;
+
+ default:
+ return MAIL_ERROR_INVAL;
+ }
+}
+
+
+
+uint32_t maildirdriver_maildir_flags_to_flags(uint32_t md_flags)
+{
+ uint32_t flags;
+
+ flags = 0;
+ if ((md_flags & MAILDIR_FLAG_NEW) != 0)
+ flags |= MAIL_FLAG_NEW;
+
+ if ((md_flags & MAILDIR_FLAG_SEEN) != 0)
+ flags |= MAIL_FLAG_SEEN;
+
+ if ((md_flags & MAILDIR_FLAG_REPLIED) != 0)
+ flags |= MAIL_FLAG_ANSWERED;
+
+ if ((md_flags & MAILDIR_FLAG_FLAGGED) != 0)
+ flags |= MAIL_FLAG_FLAGGED;
+
+ if ((md_flags & MAILDIR_FLAG_TRASHED) != 0)
+ flags |= MAIL_FLAG_DELETED;
+
+ return flags;
+}
+
+uint32_t maildirdriver_flags_to_maildir_flags(uint32_t flags)
+{
+ uint32_t md_flags;
+
+ md_flags = 0;
+ if ((flags & MAIL_FLAG_NEW) != 0)
+ md_flags |= MAILDIR_FLAG_NEW;
+
+ if ((flags & MAIL_FLAG_SEEN) != 0)
+ md_flags |= MAILDIR_FLAG_SEEN;
+
+ if ((flags & MAIL_FLAG_ANSWERED) != 0)
+ md_flags |= MAILDIR_FLAG_REPLIED;
+
+ if ((flags & MAIL_FLAG_FLAGGED) != 0)
+ md_flags |= MAILDIR_FLAG_FLAGGED;
+
+ if ((flags & MAIL_FLAG_DELETED) != 0)
+ md_flags |= MAILDIR_FLAG_TRASHED;
+
+ return md_flags;
+}
+
+
+int maildir_get_messages_list(mailsession * session, struct maildir * md,
+ mailmessage_driver * message_driver,
+ struct mailmessage_list ** result)
+{
+ unsigned int i;
+ struct mailmessage_list * env_list;
+ int r;
+ carray * tab;
+ int res;
+
+ tab = carray_new(128);
+ if (tab == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ for(i = 0 ; i < carray_count(md->mdir_msg_list) ; i++) {
+ struct maildir_msg * md_msg;
+ mailmessage * msg;
+ char * filename;
+ struct stat stat_info;
+
+ md_msg = carray_get(md->mdir_msg_list, i);
+
+ filename = maildir_message_get(md, md_msg->msg_uid);
+ r = stat(filename, &stat_info);
+ free(filename);
+ if (r < 0)
+ continue;
+
+ msg = mailmessage_new();
+ if (msg == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto free_list;
+ }
+
+ r = mailmessage_init(msg, session, message_driver,
+ i + 1, stat_info.st_size);
+ if (r != MAIL_NO_ERROR) {
+ mailmessage_free(msg);
+ res = r;
+ goto free_list;
+ }
+
+ msg->msg_uid = strdup(md_msg->msg_uid);
+ if (msg->msg_uid == NULL) {
+ mailmessage_free(msg);
+ res = MAIL_ERROR_MEMORY;
+ goto free_list;
+ }
+
+ r = carray_add(tab, msg, NULL);
+ if (r < 0) {
+ mailmessage_free(msg);
+ res = MAIL_ERROR_MEMORY;
+ goto free_list;
+ }
+ }
+
+ env_list = mailmessage_list_new(tab);
+ if (env_list == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto free_list;
+ }
+
+ * result = env_list;
+
+ return MAIL_NO_ERROR;
+
+ free_list:
+ for(i = 0 ; i < carray_count(tab) ; i ++)
+ mailmessage_free(carray_get(tab, i));
+ carray_free(tab);
+ err:
+ return res;
+}
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_tools.h b/libetpan/src/driver/implementation/maildir/maildirdriver_tools.h
new file mode 100644
index 0000000..0a738c9
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_tools.h
@@ -0,0 +1,53 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRDRIVER_TOOLS_H
+
+#define MAILDIRDRIVER_TOOLS_H
+
+#include "maildriver_types.h"
+#include "maildir.h"
+
+int maildirdriver_maildir_error_to_mail_error(int error);
+
+uint32_t maildirdriver_maildir_flags_to_flags(uint32_t md_flags);
+
+uint32_t maildirdriver_flags_to_maildir_flags(uint32_t flags);
+
+int maildir_get_messages_list(mailsession * session, struct maildir * md,
+ mailmessage_driver * message_driver,
+ struct mailmessage_list ** result);
+
+#endif
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver_types.h b/libetpan/src/driver/implementation/maildir/maildirdriver_types.h
new file mode 100644
index 0000000..c965b3e
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver_types.h
@@ -0,0 +1,96 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRDRIVER_TYPES_H
+
+#define MAILDIRDRIVER_TYPES_H
+
+#include <libetpan/libetpan-config.h>
+
+#include <libetpan/maildriver_types.h>
+#include <libetpan/maildir.h>
+#include <libetpan/generic_cache_types.h>
+#include <libetpan/mailstorage_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct maildir_session_state_data {
+ struct maildir * md_session;
+ struct mail_flags_store * md_flags_store;
+};
+
+enum {
+ MAILDIRDRIVER_CACHED_SET_CACHE_DIRECTORY = 1,
+ MAILDIRDRIVER_CACHED_SET_FLAGS_DIRECTORY,
+};
+
+struct maildir_cached_session_state_data {
+ mailsession * md_ancestor;
+ char * md_quoted_mb;
+ struct mail_flags_store * md_flags_store;
+ char md_cache_directory[PATH_MAX];
+ char md_flags_directory[PATH_MAX];
+};
+
+/* maildir storage */
+
+/*
+ maildir_mailstorage is the state data specific to the maildir storage.
+
+ - pathname is the path of the maildir storage.
+
+ - cached if this value is != 0, a persistant cache will be
+ stored on local system.
+
+ - cache_directory is the location of the cache.
+
+ - flags_directory is the location of the flags.
+*/
+
+struct maildir_mailstorage {
+ char * md_pathname;
+
+ int md_cached;
+ char * md_cache_directory;
+ char * md_flags_directory;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libetpan/src/driver/implementation/maildir/maildirstorage.c b/libetpan/src/driver/implementation/maildir/maildirstorage.c
new file mode 100644
index 0000000..09f95c7
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirstorage.c
@@ -0,0 +1,193 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#include "maildirstorage.h"
+#include "mailstorage.h"
+
+#include "mail.h"
+#include "mailmessage.h"
+#include "maildirdriver.h"
+#include "maildirdriver_cached.h"
+#include "maildriver.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/* maildir storage */
+
+static int maildir_mailstorage_connect(struct mailstorage * storage);
+static int
+maildir_mailstorage_get_folder_session(struct mailstorage * storage,
+ char * pathname, mailsession ** result);
+static void maildir_mailstorage_uninitialize(struct mailstorage * storage);
+
+static mailstorage_driver maildir_mailstorage_driver = {
+ .sto_name = "maildir",
+ .sto_connect = maildir_mailstorage_connect,
+ .sto_get_folder_session = maildir_mailstorage_get_folder_session,
+ .sto_uninitialize = maildir_mailstorage_uninitialize,
+};
+
+int maildir_mailstorage_init(struct mailstorage * storage,
+ char * md_pathname, int md_cached,
+ char * md_cache_directory, char * md_flags_directory)
+{
+ struct maildir_mailstorage * maildir_storage;
+
+ maildir_storage = malloc(sizeof(* maildir_storage));
+ if (maildir_storage == NULL)
+ goto err;
+
+ maildir_storage->md_pathname = strdup(md_pathname);
+ if (maildir_storage->md_pathname == NULL)
+ goto free;
+
+ maildir_storage->md_cached = md_cached;
+
+ if (md_cached && (md_cache_directory != NULL) &&
+ (md_flags_directory != NULL)) {
+ maildir_storage->md_cache_directory = strdup(md_cache_directory);
+ if (maildir_storage->md_cache_directory == NULL)
+ goto free_pathname;
+
+ maildir_storage->md_flags_directory = strdup(md_flags_directory);
+ if (maildir_storage->md_flags_directory == NULL)
+ goto free_cache_directory;
+ }
+ else {
+ maildir_storage->md_cached = FALSE;
+ maildir_storage->md_cache_directory = NULL;
+ maildir_storage->md_flags_directory = NULL;
+ }
+
+ storage->sto_data = maildir_storage;
+ storage->sto_driver = &maildir_mailstorage_driver;
+
+ return MAIL_NO_ERROR;
+
+ free_cache_directory:
+ free(maildir_storage->md_cache_directory);
+ free_pathname:
+ free(maildir_storage->md_pathname);
+ free:
+ free(maildir_storage);
+ err:
+ return MAIL_ERROR_MEMORY;
+}
+
+static void maildir_mailstorage_uninitialize(struct mailstorage * storage)
+{
+ struct maildir_mailstorage * maildir_storage;
+
+ maildir_storage = storage->sto_data;
+ if (maildir_storage->md_flags_directory != NULL)
+ free(maildir_storage->md_flags_directory);
+ if (maildir_storage->md_cache_directory != NULL)
+ free(maildir_storage->md_cache_directory);
+ free(maildir_storage->md_pathname);
+ free(maildir_storage);
+
+ storage->sto_data = NULL;
+}
+
+static int maildir_mailstorage_connect(struct mailstorage * storage)
+{
+ struct maildir_mailstorage * maildir_storage;
+ mailsession_driver * driver;
+ int r;
+ int res;
+ mailsession * session;
+
+ maildir_storage = storage->sto_data;
+
+ if (maildir_storage->md_cached)
+ driver = maildir_cached_session_driver;
+ else
+ driver = maildir_session_driver;
+
+ session = mailsession_new(driver);
+ if (session == NULL) {
+ res = MAIL_ERROR_MEMORY;
+ goto err;
+ }
+
+ if (maildir_storage->md_cached) {
+ r = mailsession_parameters(session,
+ MAILDIRDRIVER_CACHED_SET_CACHE_DIRECTORY,
+ maildir_storage->md_cache_directory);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto free;
+ }
+
+ r = mailsession_parameters(session,
+ MAILDIRDRIVER_CACHED_SET_FLAGS_DIRECTORY,
+ maildir_storage->md_flags_directory);
+ if (r != MAIL_NO_ERROR) {
+ res = r;
+ goto free;
+ }
+ }
+
+ r = mailsession_connect_path(session, maildir_storage->md_pathname);
+ switch (r) {
+ case MAIL_NO_ERROR_NON_AUTHENTICATED:
+ case MAIL_NO_ERROR_AUTHENTICATED:
+ case MAIL_NO_ERROR:
+ break;
+ default:
+ res = r;
+ goto free;
+ }
+
+ storage->sto_session = session;
+
+ return MAIL_NO_ERROR;
+
+ free:
+ mailsession_free(session);
+ err:
+ return res;
+}
+
+static int
+maildir_mailstorage_get_folder_session(struct mailstorage * storage,
+ char * pathname, mailsession ** result)
+{
+ * result = storage->sto_session;
+
+ return MAIL_NO_ERROR;
+}
+
diff --git a/libetpan/src/driver/implementation/maildir/maildirstorage.h b/libetpan/src/driver/implementation/maildir/maildirstorage.h
new file mode 100644
index 0000000..0ad04b9
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirstorage.h
@@ -0,0 +1,69 @@
+/*
+ * libEtPan! -- a mail stuff library
+ *
+ * Copyright (C) 2001, 2005 - DINH Viet Hoa
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the libEtPan! project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef MAILDIRSTORAGE_H
+
+#define MAILDIRSTORAGE_H
+
+#include <libetpan/maildirdriver_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ maildir_mailstorage_init is the constructor for a maildir storage.
+
+ @param storage this is the storage to initialize.
+
+ @param pathname is the directory that contains the mailbox.
+
+ @param cached if this value is != 0, a persistant cache will be
+ stored on local system.
+
+ @param cache_directory is the location of the cache
+
+ @param flags_directory is the location of the flags
+*/
+
+int maildir_mailstorage_init(struct mailstorage * storage,
+ char * md_pathname, int md_cached,
+ char * md_cache_directory, char * md_flags_directory);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif