summaryrefslogtreecommitdiffabout
path: root/libetpan/src/driver/implementation/maildir/maildirstorage.c
Unidiff
Diffstat (limited to 'libetpan/src/driver/implementation/maildir/maildirstorage.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirstorage.c193
1 files changed, 193 insertions, 0 deletions
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 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - DINH Viet Hoa
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * $Id$
34 */
35
36#include "maildirstorage.h"
37#include "mailstorage.h"
38
39#include "mail.h"
40#include "mailmessage.h"
41#include "maildirdriver.h"
42#include "maildirdriver_cached.h"
43#include "maildriver.h"
44
45#include <stdlib.h>
46#include <string.h>
47
48/* maildir storage */
49
50static int maildir_mailstorage_connect(struct mailstorage * storage);
51static int
52maildir_mailstorage_get_folder_session(struct mailstorage * storage,
53 char * pathname, mailsession ** result);
54static void maildir_mailstorage_uninitialize(struct mailstorage * storage);
55
56static mailstorage_driver maildir_mailstorage_driver = {
57 .sto_name = "maildir",
58 .sto_connect = maildir_mailstorage_connect,
59 .sto_get_folder_session = maildir_mailstorage_get_folder_session,
60 .sto_uninitialize = maildir_mailstorage_uninitialize,
61};
62
63int maildir_mailstorage_init(struct mailstorage * storage,
64 char * md_pathname, int md_cached,
65 char * md_cache_directory, char * md_flags_directory)
66{
67 struct maildir_mailstorage * maildir_storage;
68
69 maildir_storage = malloc(sizeof(* maildir_storage));
70 if (maildir_storage == NULL)
71 goto err;
72
73 maildir_storage->md_pathname = strdup(md_pathname);
74 if (maildir_storage->md_pathname == NULL)
75 goto free;
76
77 maildir_storage->md_cached = md_cached;
78
79 if (md_cached && (md_cache_directory != NULL) &&
80 (md_flags_directory != NULL)) {
81 maildir_storage->md_cache_directory = strdup(md_cache_directory);
82 if (maildir_storage->md_cache_directory == NULL)
83 goto free_pathname;
84
85 maildir_storage->md_flags_directory = strdup(md_flags_directory);
86 if (maildir_storage->md_flags_directory == NULL)
87 goto free_cache_directory;
88 }
89 else {
90 maildir_storage->md_cached = FALSE;
91 maildir_storage->md_cache_directory = NULL;
92 maildir_storage->md_flags_directory = NULL;
93 }
94
95 storage->sto_data = maildir_storage;
96 storage->sto_driver = &maildir_mailstorage_driver;
97
98 return MAIL_NO_ERROR;
99
100 free_cache_directory:
101 free(maildir_storage->md_cache_directory);
102 free_pathname:
103 free(maildir_storage->md_pathname);
104 free:
105 free(maildir_storage);
106 err:
107 return MAIL_ERROR_MEMORY;
108}
109
110static void maildir_mailstorage_uninitialize(struct mailstorage * storage)
111{
112 struct maildir_mailstorage * maildir_storage;
113
114 maildir_storage = storage->sto_data;
115 if (maildir_storage->md_flags_directory != NULL)
116 free(maildir_storage->md_flags_directory);
117 if (maildir_storage->md_cache_directory != NULL)
118 free(maildir_storage->md_cache_directory);
119 free(maildir_storage->md_pathname);
120 free(maildir_storage);
121
122 storage->sto_data = NULL;
123}
124
125static int maildir_mailstorage_connect(struct mailstorage * storage)
126{
127 struct maildir_mailstorage * maildir_storage;
128 mailsession_driver * driver;
129 int r;
130 int res;
131 mailsession * session;
132
133 maildir_storage = storage->sto_data;
134
135 if (maildir_storage->md_cached)
136 driver = maildir_cached_session_driver;
137 else
138 driver = maildir_session_driver;
139
140 session = mailsession_new(driver);
141 if (session == NULL) {
142 res = MAIL_ERROR_MEMORY;
143 goto err;
144 }
145
146 if (maildir_storage->md_cached) {
147 r = mailsession_parameters(session,
148 MAILDIRDRIVER_CACHED_SET_CACHE_DIRECTORY,
149 maildir_storage->md_cache_directory);
150 if (r != MAIL_NO_ERROR) {
151 res = r;
152 goto free;
153 }
154
155 r = mailsession_parameters(session,
156 MAILDIRDRIVER_CACHED_SET_FLAGS_DIRECTORY,
157 maildir_storage->md_flags_directory);
158 if (r != MAIL_NO_ERROR) {
159 res = r;
160 goto free;
161 }
162 }
163
164 r = mailsession_connect_path(session, maildir_storage->md_pathname);
165 switch (r) {
166 case MAIL_NO_ERROR_NON_AUTHENTICATED:
167 case MAIL_NO_ERROR_AUTHENTICATED:
168 case MAIL_NO_ERROR:
169 break;
170 default:
171 res = r;
172 goto free;
173 }
174
175 storage->sto_session = session;
176
177 return MAIL_NO_ERROR;
178
179 free:
180 mailsession_free(session);
181 err:
182 return res;
183}
184
185static int
186maildir_mailstorage_get_folder_session(struct mailstorage * storage,
187 char * pathname, mailsession ** result)
188{
189 * result = storage->sto_session;
190
191 return MAIL_NO_ERROR;
192}
193