Diffstat (limited to 'libetpan/src/driver/implementation/mbox') (more/less context) (show whitespace changes)
13 files changed, 3536 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver.c b/libetpan/src/driver/implementation/mbox/mboxdriver.c new file mode 100644 index 0000000..72afa6d --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver.c | |||
@@ -0,0 +1,515 @@ | |||
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 "mboxdriver.h" | ||
37 | |||
38 | #include <stdio.h> | ||
39 | #include <string.h> | ||
40 | #include <sys/types.h> | ||
41 | #include <dirent.h> | ||
42 | #include <unistd.h> | ||
43 | #include <sys/stat.h> | ||
44 | #include <ctype.h> | ||
45 | #include <stdlib.h> | ||
46 | #include <sys/times.h> | ||
47 | |||
48 | #include "mail.h" | ||
49 | #include "maildriver_tools.h" | ||
50 | #include "mailmbox.h" | ||
51 | #include "mboxdriver_tools.h" | ||
52 | #include "maildriver.h" | ||
53 | #include "carray.h" | ||
54 | #include "mboxdriver_message.h" | ||
55 | #include "mailmessage.h" | ||
56 | |||
57 | static int mboxdriver_initialize(mailsession * session); | ||
58 | |||
59 | static void mboxdriver_uninitialize(mailsession * session); | ||
60 | |||
61 | static int mboxdriver_parameters(mailsession * session, | ||
62 | int id, void * value); | ||
63 | |||
64 | static int mboxdriver_connect_path(mailsession * session, char * path); | ||
65 | |||
66 | static int mboxdriver_logout(mailsession * session); | ||
67 | |||
68 | static int mboxdriver_expunge_folder(mailsession * session); | ||
69 | |||
70 | static int mboxdriver_status_folder(mailsession * session, char * mb, | ||
71 | uint32_t * result_messages, uint32_t * result_recent, | ||
72 | uint32_t * result_unseen); | ||
73 | |||
74 | static int mboxdriver_messages_number(mailsession * session, char * mb, | ||
75 | uint32_t * result); | ||
76 | |||
77 | static int mboxdriver_append_message(mailsession * session, | ||
78 | char * message, size_t size); | ||
79 | |||
80 | static int mboxdriver_append_message_flags(mailsession * session, | ||
81 | char * message, size_t size, struct mail_flags * flags); | ||
82 | |||
83 | static int mboxdriver_get_messages_list(mailsession * session, | ||
84 | struct mailmessage_list ** result); | ||
85 | |||
86 | static int | ||
87 | mboxdriver_get_envelopes_list(mailsession * session, | ||
88 | struct mailmessage_list * env_list); | ||
89 | |||
90 | static int mboxdriver_remove_message(mailsession * session, uint32_t num); | ||
91 | |||
92 | static int mboxdriver_get_message(mailsession * session, | ||
93 | uint32_t num, mailmessage ** result); | ||
94 | |||
95 | static int mboxdriver_get_message_by_uid(mailsession * session, | ||
96 | const char * uid, | ||
97 | mailmessage ** result); | ||
98 | |||
99 | static mailsession_driver local_mbox_session_driver = { | ||
100 | .sess_name = "mbox", | ||
101 | |||
102 | .sess_initialize = mboxdriver_initialize, | ||
103 | .sess_uninitialize = mboxdriver_uninitialize, | ||
104 | |||
105 | .sess_parameters = mboxdriver_parameters, | ||
106 | |||
107 | .sess_connect_path = mboxdriver_connect_path, | ||
108 | .sess_connect_stream = NULL, | ||
109 | .sess_starttls = NULL, | ||
110 | .sess_login = NULL, | ||
111 | .sess_logout = mboxdriver_logout, | ||
112 | .sess_noop = NULL, | ||
113 | |||
114 | .sess_build_folder_name = NULL, | ||
115 | .sess_create_folder = NULL, | ||
116 | .sess_delete_folder = NULL, | ||
117 | .sess_rename_folder = NULL, | ||
118 | .sess_check_folder = NULL, | ||
119 | .sess_examine_folder = NULL, | ||
120 | .sess_select_folder = NULL, | ||
121 | .sess_expunge_folder = mboxdriver_expunge_folder, | ||
122 | .sess_status_folder = mboxdriver_status_folder, | ||
123 | .sess_messages_number = mboxdriver_messages_number, | ||
124 | .sess_recent_number = mboxdriver_messages_number, | ||
125 | .sess_unseen_number = mboxdriver_messages_number, | ||
126 | .sess_list_folders = NULL, | ||
127 | .sess_lsub_folders = NULL, | ||
128 | .sess_subscribe_folder = NULL, | ||
129 | .sess_unsubscribe_folder = NULL, | ||
130 | |||
131 | .sess_append_message = mboxdriver_append_message, | ||
132 | .sess_append_message_flags = mboxdriver_append_message_flags, | ||
133 | .sess_copy_message = NULL, | ||
134 | .sess_move_message = NULL, | ||
135 | |||
136 | .sess_get_messages_list = mboxdriver_get_messages_list, | ||
137 | .sess_get_envelopes_list = mboxdriver_get_envelopes_list, | ||
138 | .sess_remove_message = mboxdriver_remove_message, | ||
139 | #if 0 | ||
140 | .sess_search_messages = maildriver_generic_search_messages, | ||
141 | #endif | ||
142 | |||
143 | .sess_get_message = mboxdriver_get_message, | ||
144 | .sess_get_message_by_uid = mboxdriver_get_message_by_uid, | ||
145 | }; | ||
146 | |||
147 | mailsession_driver * mbox_session_driver = &local_mbox_session_driver; | ||
148 | |||
149 | static inline struct mbox_session_state_data * get_data(mailsession * session) | ||
150 | { | ||
151 | return session->sess_data; | ||
152 | } | ||
153 | |||
154 | static inline struct mailmbox_folder * get_mbox_session(mailsession * session) | ||
155 | { | ||
156 | return get_data(session)->mbox_folder; | ||
157 | } | ||
158 | |||
159 | static int mboxdriver_initialize(mailsession * session) | ||
160 | { | ||
161 | struct mbox_session_state_data * data; | ||
162 | |||
163 | data = malloc(sizeof(* data)); | ||
164 | if (data == NULL) | ||
165 | goto err; | ||
166 | |||
167 | data->mbox_folder = NULL; | ||
168 | |||
169 | data->mbox_force_read_only = FALSE; | ||
170 | data->mbox_force_no_uid = TRUE; | ||
171 | |||
172 | session->sess_data = data; | ||
173 | |||
174 | return MAIL_NO_ERROR; | ||
175 | |||
176 | err: | ||
177 | return MAIL_ERROR_MEMORY; | ||
178 | } | ||
179 | |||
180 | static void free_state(struct mbox_session_state_data * mbox_data) | ||
181 | { | ||
182 | if (mbox_data->mbox_folder != NULL) { | ||
183 | mailmbox_done(mbox_data->mbox_folder); | ||
184 | mbox_data->mbox_folder = NULL; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | static void mboxdriver_uninitialize(mailsession * session) | ||
189 | { | ||
190 | struct mbox_session_state_data * data; | ||
191 | |||
192 | data = get_data(session); | ||
193 | |||
194 | free_state(data); | ||
195 | |||
196 | free(data); | ||
197 | } | ||
198 | |||
199 | static int mboxdriver_parameters(mailsession * session, | ||
200 | int id, void * value) | ||
201 | { | ||
202 | struct mbox_session_state_data * data; | ||
203 | |||
204 | data = get_data(session); | ||
205 | |||
206 | switch (id) { | ||
207 | case MBOXDRIVER_SET_READ_ONLY: | ||
208 | { | ||
209 | int * param; | ||
210 | |||
211 | param = value; | ||
212 | |||
213 | data->mbox_force_read_only = * param; | ||
214 | return MAIL_NO_ERROR; | ||
215 | } | ||
216 | |||
217 | case MBOXDRIVER_SET_NO_UID: | ||
218 | { | ||
219 | int * param; | ||
220 | |||
221 | param = value; | ||
222 | |||
223 | data->mbox_force_no_uid = * param; | ||
224 | return MAIL_NO_ERROR; | ||
225 | } | ||
226 | } | ||
227 | |||
228 | return MAIL_ERROR_INVAL; | ||
229 | } | ||
230 | |||
231 | |||
232 | static int mboxdriver_connect_path(mailsession * session, char * path) | ||
233 | { | ||
234 | struct mbox_session_state_data * mbox_data; | ||
235 | struct mailmbox_folder * folder; | ||
236 | int r; | ||
237 | |||
238 | mbox_data = get_data(session); | ||
239 | |||
240 | if (mbox_data->mbox_folder != NULL) | ||
241 | return MAIL_ERROR_BAD_STATE; | ||
242 | |||
243 | r = mailmbox_init(path, | ||
244 | mbox_data->mbox_force_read_only, | ||
245 | mbox_data->mbox_force_no_uid, | ||
246 | 0, | ||
247 | &folder); | ||
248 | |||
249 | if (r != MAILMBOX_NO_ERROR) | ||
250 | return mboxdriver_mbox_error_to_mail_error(r); | ||
251 | |||
252 | mbox_data->mbox_folder = folder; | ||
253 | |||
254 | return MAIL_NO_ERROR; | ||
255 | } | ||
256 | |||
257 | static int mboxdriver_logout(mailsession * session) | ||
258 | { | ||
259 | struct mbox_session_state_data * mbox_data; | ||
260 | |||
261 | mbox_data = get_data(session); | ||
262 | |||
263 | if (mbox_data->mbox_folder == NULL) | ||
264 | return MAIL_ERROR_BAD_STATE; | ||
265 | |||
266 | free_state(mbox_data); | ||
267 | |||
268 | mbox_data->mbox_folder = NULL; | ||
269 | |||
270 | return MAIL_NO_ERROR; | ||
271 | } | ||
272 | |||
273 | static int mboxdriver_expunge_folder(mailsession * session) | ||
274 | { | ||
275 | int r; | ||
276 | struct mbox_session_state_data * mbox_data; | ||
277 | |||
278 | mbox_data = get_data(session); | ||
279 | |||
280 | if (mbox_data->mbox_folder == NULL) | ||
281 | return MAIL_ERROR_BAD_STATE; | ||
282 | |||
283 | r = mailmbox_expunge(mbox_data->mbox_folder); | ||
284 | if (r != MAILMBOX_NO_ERROR) | ||
285 | return mboxdriver_mbox_error_to_mail_error(r); | ||
286 | |||
287 | return MAIL_NO_ERROR; | ||
288 | } | ||
289 | |||
290 | static int mboxdriver_status_folder(mailsession * session, char * mb, | ||
291 | uint32_t * result_messages, uint32_t * result_recent, | ||
292 | uint32_t * result_unseen) | ||
293 | { | ||
294 | uint32_t count; | ||
295 | int r; | ||
296 | |||
297 | r = mboxdriver_messages_number(session, mb, &count); | ||
298 | if (r != MAIL_NO_ERROR) | ||
299 | return r; | ||
300 | |||
301 | * result_messages = count; | ||
302 | * result_recent = count; | ||
303 | * result_unseen = count; | ||
304 | |||
305 | return MAIL_NO_ERROR; | ||
306 | } | ||
307 | |||
308 | static int mboxdriver_messages_number(mailsession * session, char * mb, | ||
309 | uint32_t * result) | ||
310 | { | ||
311 | struct mailmbox_folder * folder; | ||
312 | int r; | ||
313 | |||
314 | folder = get_mbox_session(session); | ||
315 | if (folder == NULL) | ||
316 | return MAIL_ERROR_STATUS; | ||
317 | |||
318 | r = mailmbox_validate_read_lock(folder); | ||
319 | if (r != MAIL_NO_ERROR) | ||
320 | return r; | ||
321 | |||
322 | mailmbox_read_unlock(folder); | ||
323 | |||
324 | * result = carray_count(folder->mb_tab) - folder->mb_deleted_count; | ||
325 | |||
326 | return MAILMBOX_NO_ERROR; | ||
327 | } | ||
328 | |||
329 | /* messages operations */ | ||
330 | |||
331 | static int mboxdriver_append_message(mailsession * session, | ||
332 | char * message, size_t size) | ||
333 | { | ||
334 | int r; | ||
335 | struct mailmbox_folder * folder; | ||
336 | |||
337 | folder = get_mbox_session(session); | ||
338 | if (folder == NULL) | ||
339 | return MAIL_ERROR_APPEND; | ||
340 | |||
341 | r = mailmbox_append_message(folder, message, size); | ||
342 | |||
343 | switch (r) { | ||
344 | case MAILMBOX_ERROR_FILE: | ||
345 | return MAIL_ERROR_DISKSPACE; | ||
346 | default: | ||
347 | return mboxdriver_mbox_error_to_mail_error(r); | ||
348 | } | ||
349 | } | ||
350 | |||
351 | static int mboxdriver_append_message_flags(mailsession * session, | ||
352 | char * message, size_t size, struct mail_flags * flags) | ||
353 | { | ||
354 | return mboxdriver_append_message(session, message, size); | ||
355 | } | ||
356 | |||
357 | static int mboxdriver_get_messages_list(mailsession * session, | ||
358 | struct mailmessage_list ** result) | ||
359 | { | ||
360 | struct mailmbox_folder * folder; | ||
361 | int res; | ||
362 | |||
363 | folder = get_mbox_session(session); | ||
364 | if (folder == NULL) { | ||
365 | res = MAIL_ERROR_BAD_STATE; | ||
366 | goto err; | ||
367 | } | ||
368 | |||
369 | return mbox_get_messages_list(folder, session, mbox_message_driver, result); | ||
370 | |||
371 | err: | ||
372 | return res; | ||
373 | } | ||
374 | |||
375 | static int | ||
376 | mboxdriver_get_envelopes_list(mailsession * session, | ||
377 | struct mailmessage_list * env_list) | ||
378 | { | ||
379 | struct mailmbox_folder * folder; | ||
380 | unsigned int i; | ||
381 | int r; | ||
382 | int res; | ||
383 | |||
384 | folder = get_mbox_session(session); | ||
385 | if (folder == NULL) { | ||
386 | res = MAIL_ERROR_BAD_STATE; | ||
387 | goto err; | ||
388 | } | ||
389 | |||
390 | r = mailmbox_validate_read_lock(folder); | ||
391 | if (r != MAILMBOX_NO_ERROR) { | ||
392 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
393 | goto err; | ||
394 | } | ||
395 | |||
396 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
397 | mailmessage * msg; | ||
398 | struct mailimf_fields * fields; | ||
399 | char * headers; | ||
400 | size_t headers_len; | ||
401 | size_t cur_token; | ||
402 | |||
403 | msg = carray_get(env_list->msg_tab, i); | ||
404 | if (msg == NULL) | ||
405 | continue; | ||
406 | |||
407 | if (msg->msg_fields != NULL) | ||
408 | continue; | ||
409 | |||
410 | r = mailmbox_fetch_msg_headers_no_lock(folder, | ||
411 | msg->msg_index, &headers, &headers_len); | ||
412 | if (r != MAILMBOX_NO_ERROR) { | ||
413 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
414 | goto unlock; | ||
415 | } | ||
416 | |||
417 | cur_token = 0; | ||
418 | r = mailimf_envelope_fields_parse(headers, headers_len, | ||
419 | &cur_token, &fields); | ||
420 | |||
421 | if (r != MAILIMF_NO_ERROR) | ||
422 | continue; | ||
423 | |||
424 | msg->msg_fields = fields; | ||
425 | } | ||
426 | |||
427 | mailmbox_read_unlock(folder); | ||
428 | |||
429 | return MAIL_NO_ERROR; | ||
430 | |||
431 | unlock: | ||
432 | mailmbox_read_unlock(folder); | ||
433 | err: | ||
434 | return res; | ||
435 | } | ||
436 | |||
437 | |||
438 | static int mboxdriver_remove_message(mailsession * session, uint32_t num) | ||
439 | { | ||
440 | int r; | ||
441 | struct mailmbox_folder * folder; | ||
442 | |||
443 | folder = get_mbox_session(session); | ||
444 | if (folder == NULL) | ||
445 | return MAIL_ERROR_DELETE; | ||
446 | |||
447 | r = mailmbox_delete_msg(folder, num); | ||
448 | |||
449 | return mboxdriver_mbox_error_to_mail_error(r); | ||
450 | } | ||
451 | |||
452 | static int mboxdriver_get_message(mailsession * session, | ||
453 | uint32_t num, mailmessage ** result) | ||
454 | { | ||
455 | mailmessage * msg_info; | ||
456 | int r; | ||
457 | |||
458 | msg_info = mailmessage_new(); | ||
459 | if (msg_info == NULL) | ||
460 | return MAIL_ERROR_MEMORY; | ||
461 | |||
462 | r = mailmessage_init(msg_info, session, mbox_message_driver, num, 0); | ||
463 | if (r != MAIL_NO_ERROR) { | ||
464 | mailmessage_free(msg_info); | ||
465 | return r; | ||
466 | } | ||
467 | |||
468 | * result = msg_info; | ||
469 | |||
470 | return MAIL_NO_ERROR; | ||
471 | } | ||
472 | |||
473 | static int mboxdriver_get_message_by_uid(mailsession * session, | ||
474 | const char * uid, | ||
475 | mailmessage ** result) | ||
476 | { | ||
477 | uint32_t num; | ||
478 | char * p; | ||
479 | chashdatum key; | ||
480 | chashdatum data; | ||
481 | struct mailmbox_msg_info * info; | ||
482 | struct mailmbox_folder * folder; | ||
483 | int r; | ||
484 | |||
485 | if (uid == NULL) | ||
486 | return MAIL_ERROR_INVAL; | ||
487 | |||
488 | num = strtoul(uid, &p, 10); | ||
489 | if (p == uid || * p != '-') | ||
490 | return MAIL_ERROR_INVAL; | ||
491 | |||
492 | folder = get_mbox_session(session); | ||
493 | if (folder == NULL) | ||
494 | return MAIL_ERROR_BAD_STATE; | ||
495 | |||
496 | key.data = # | ||
497 | key.len = sizeof(num); | ||
498 | |||
499 | r = chash_get(folder->mb_hash, &key, &data); | ||
500 | if (r == 0) { | ||
501 | char * body_len_p = p + 1; | ||
502 | size_t body_len; | ||
503 | |||
504 | info = data.data; | ||
505 | /* Check if the cached message has the same UID */ | ||
506 | body_len = strtoul(body_len_p, &p, 10); | ||
507 | if (p == body_len_p || * p != '\0') | ||
508 | return MAIL_ERROR_INVAL; | ||
509 | |||
510 | if (body_len == info->msg_body_len) | ||
511 | return mboxdriver_get_message(session, num, result); | ||
512 | } | ||
513 | |||
514 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
515 | } | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver.h b/libetpan/src/driver/implementation/mbox/mboxdriver.h new file mode 100644 index 0000000..9b37aa4 --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver.h | |||
@@ -0,0 +1,52 @@ | |||
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 | #ifndef MBOXDRIVER_H | ||
37 | |||
38 | #define MBOXDRIVER_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | #include <libetpan/mboxdriver_types.h> | ||
45 | |||
46 | extern mailsession_driver * mbox_session_driver; | ||
47 | |||
48 | #ifdef __cplusplus | ||
49 | } | ||
50 | #endif | ||
51 | |||
52 | #endif | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_cached.c b/libetpan/src/driver/implementation/mbox/mboxdriver_cached.c new file mode 100644 index 0000000..eaa0e48 --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_cached.c | |||
@@ -0,0 +1,1337 @@ | |||
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 "mboxdriver_cached.h" | ||
37 | |||
38 | #include <stdio.h> | ||
39 | #include <string.h> | ||
40 | #include <dirent.h> | ||
41 | #include <unistd.h> | ||
42 | #include <ctype.h> | ||
43 | #include <sys/types.h> | ||
44 | #include <sys/stat.h> | ||
45 | #include <fcntl.h> | ||
46 | #include <stdlib.h> | ||
47 | |||
48 | #include "mail.h" | ||
49 | #include "mail_cache_db.h" | ||
50 | #include "mboxdriver.h" | ||
51 | #include "mboxdriver_tools.h" | ||
52 | #include "maildriver_tools.h" | ||
53 | #include "mailmbox.h" | ||
54 | #include "maildriver.h" | ||
55 | #include "carray.h" | ||
56 | #include "generic_cache.h" | ||
57 | #include "imfcache.h" | ||
58 | #include "mboxdriver_cached_message.h" | ||
59 | #include "libetpan-config.h" | ||
60 | |||
61 | static int mboxdriver_cached_initialize(mailsession * session); | ||
62 | |||
63 | static void mboxdriver_cached_uninitialize(mailsession * session); | ||
64 | |||
65 | static int mboxdriver_cached_parameters(mailsession * session, | ||
66 | int id, void * value); | ||
67 | |||
68 | static int mboxdriver_cached_connect_path(mailsession * session, char * path); | ||
69 | |||
70 | static int mboxdriver_cached_logout(mailsession * session); | ||
71 | |||
72 | static int mboxdriver_cached_check_folder(mailsession * session); | ||
73 | |||
74 | static int mboxdriver_cached_expunge_folder(mailsession * session); | ||
75 | |||
76 | static int mboxdriver_cached_status_folder(mailsession * session, char * mb, | ||
77 | uint32_t * result_messages, uint32_t * result_recent, | ||
78 | uint32_t * result_unseen); | ||
79 | static int mboxdriver_cached_messages_number(mailsession * session, char * mb, | ||
80 | uint32_t * result); | ||
81 | static int mboxdriver_cached_recent_number(mailsession * session, char * mb, | ||
82 | uint32_t * result); | ||
83 | static int mboxdriver_cached_unseen_number(mailsession * session, char * mb, | ||
84 | uint32_t * result); | ||
85 | |||
86 | static int mboxdriver_cached_append_message(mailsession * session, | ||
87 | char * message, size_t size); | ||
88 | |||
89 | static int mboxdriver_cached_append_message_flags(mailsession * session, | ||
90 | char * message, size_t size, struct mail_flags * flags); | ||
91 | |||
92 | static int | ||
93 | mboxdriver_cached_get_messages_list(mailsession * session, | ||
94 | struct mailmessage_list ** result); | ||
95 | |||
96 | static int | ||
97 | mboxdriver_cached_get_envelopes_list(mailsession * session, | ||
98 | struct mailmessage_list * env_list); | ||
99 | |||
100 | static int mboxdriver_cached_remove_message(mailsession * session, | ||
101 | uint32_t num); | ||
102 | |||
103 | static int mboxdriver_cached_get_message(mailsession * session, | ||
104 | uint32_t num, mailmessage ** result); | ||
105 | |||
106 | static int mboxdriver_cached_get_message_by_uid(mailsession * session, | ||
107 | const char * uid, | ||
108 | mailmessage ** result); | ||
109 | |||
110 | static mailsession_driver local_mbox_cached_session_driver = { | ||
111 | .sess_name = "mbox-cached", | ||
112 | |||
113 | .sess_initialize = mboxdriver_cached_initialize, | ||
114 | .sess_uninitialize = mboxdriver_cached_uninitialize, | ||
115 | |||
116 | .sess_parameters = mboxdriver_cached_parameters, | ||
117 | |||
118 | .sess_connect_path = mboxdriver_cached_connect_path, | ||
119 | .sess_connect_stream = NULL, | ||
120 | .sess_starttls = NULL, | ||
121 | .sess_login = NULL, | ||
122 | .sess_logout = mboxdriver_cached_logout, | ||
123 | .sess_noop = NULL, | ||
124 | |||
125 | .sess_build_folder_name = NULL, | ||
126 | .sess_create_folder = NULL, | ||
127 | .sess_delete_folder = NULL, | ||
128 | .sess_rename_folder = NULL, | ||
129 | .sess_check_folder = mboxdriver_cached_check_folder, | ||
130 | .sess_examine_folder = NULL, | ||
131 | .sess_select_folder = NULL, | ||
132 | .sess_expunge_folder = mboxdriver_cached_expunge_folder, | ||
133 | .sess_status_folder = mboxdriver_cached_status_folder, | ||
134 | .sess_messages_number = mboxdriver_cached_messages_number, | ||
135 | .sess_recent_number = mboxdriver_cached_recent_number, | ||
136 | .sess_unseen_number = mboxdriver_cached_unseen_number, | ||
137 | .sess_list_folders = NULL, | ||
138 | .sess_lsub_folders = NULL, | ||
139 | .sess_subscribe_folder = NULL, | ||
140 | .sess_unsubscribe_folder = NULL, | ||
141 | |||
142 | .sess_append_message = mboxdriver_cached_append_message, | ||
143 | .sess_append_message_flags = mboxdriver_cached_append_message_flags, | ||
144 | |||
145 | .sess_copy_message = NULL, | ||
146 | .sess_move_message = NULL, | ||
147 | |||
148 | .sess_get_messages_list = mboxdriver_cached_get_messages_list, | ||
149 | .sess_get_envelopes_list = mboxdriver_cached_get_envelopes_list, | ||
150 | .sess_remove_message = mboxdriver_cached_remove_message, | ||
151 | #if 0 | ||
152 | .sess_search_messages = maildriver_generic_search_messages, | ||
153 | #endif | ||
154 | |||
155 | .sess_get_message = mboxdriver_cached_get_message, | ||
156 | .sess_get_message_by_uid = mboxdriver_cached_get_message_by_uid, | ||
157 | }; | ||
158 | |||
159 | mailsession_driver * mbox_cached_session_driver = | ||
160 | &local_mbox_cached_session_driver; | ||
161 | |||
162 | |||
163 | #define ENV_NAME "env.db" | ||
164 | #define FLAGS_NAME "flags.db" | ||
165 | |||
166 | |||
167 | |||
168 | static int mbox_error_to_mail_error(int error) | ||
169 | { | ||
170 | switch (error) { | ||
171 | case MAILMBOX_NO_ERROR: | ||
172 | return MAIL_NO_ERROR; | ||
173 | |||
174 | case MAILMBOX_ERROR_PARSE: | ||
175 | return MAIL_ERROR_PARSE; | ||
176 | |||
177 | case MAILMBOX_ERROR_INVAL: | ||
178 | return MAIL_ERROR_INVAL; | ||
179 | |||
180 | case MAILMBOX_ERROR_FILE_NOT_FOUND: | ||
181 | return MAIL_ERROR_PARSE; | ||
182 | |||
183 | case MAILMBOX_ERROR_MEMORY: | ||
184 | return MAIL_ERROR_MEMORY; | ||
185 | |||
186 | case MAILMBOX_ERROR_TEMPORARY_FILE: | ||
187 | return MAIL_ERROR_PARSE; | ||
188 | |||
189 | case MAILMBOX_ERROR_FILE: | ||
190 | return MAIL_ERROR_FILE; | ||
191 | |||
192 | case MAILMBOX_ERROR_MSG_NOT_FOUND: | ||
193 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
194 | |||
195 | case MAILMBOX_ERROR_READONLY: | ||
196 | return MAIL_ERROR_READONLY; | ||
197 | |||
198 | default: | ||
199 | return MAIL_ERROR_INVAL; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | |||
204 | |||
205 | |||
206 | static inline struct mbox_cached_session_state_data * | ||
207 | get_cached_data(mailsession * session) | ||
208 | { | ||
209 | return session->sess_data; | ||
210 | } | ||
211 | |||
212 | static inline mailsession * get_ancestor(mailsession * session) | ||
213 | { | ||
214 | return get_cached_data(session)->mbox_ancestor; | ||
215 | } | ||
216 | |||
217 | static inline struct mbox_session_state_data * | ||
218 | get_ancestor_data(mailsession * session) | ||
219 | { | ||
220 | return get_ancestor(session)->sess_data; | ||
221 | } | ||
222 | |||
223 | static inline struct mailmbox_folder * | ||
224 | get_mbox_session(mailsession * session) | ||
225 | { | ||
226 | return get_ancestor_data(session)->mbox_folder; | ||
227 | } | ||
228 | |||
229 | static int mboxdriver_cached_initialize(mailsession * session) | ||
230 | { | ||
231 | struct mbox_cached_session_state_data * cached_data; | ||
232 | struct mbox_session_state_data * mbox_data; | ||
233 | |||
234 | cached_data = malloc(sizeof(* cached_data)); | ||
235 | if (cached_data == NULL) | ||
236 | goto err; | ||
237 | |||
238 | cached_data->mbox_flags_store = mail_flags_store_new(); | ||
239 | if (cached_data->mbox_flags_store == NULL) | ||
240 | goto free; | ||
241 | |||
242 | cached_data->mbox_ancestor = mailsession_new(mbox_session_driver); | ||
243 | if (cached_data->mbox_ancestor == NULL) | ||
244 | goto free_store; | ||
245 | |||
246 | cached_data->mbox_quoted_mb = NULL; | ||
247 | /* | ||
248 | UID must be enabled to take advantage of the cache | ||
249 | */ | ||
250 | mbox_data = cached_data->mbox_ancestor->sess_data; | ||
251 | mbox_data->mbox_force_no_uid = FALSE; | ||
252 | |||
253 | session->sess_data = cached_data; | ||
254 | |||
255 | return MAIL_NO_ERROR; | ||
256 | |||
257 | free_store: | ||
258 | mail_flags_store_free(cached_data->mbox_flags_store); | ||
259 | free: | ||
260 | free(cached_data); | ||
261 | err: | ||
262 | return MAIL_ERROR_MEMORY; | ||
263 | } | ||
264 | |||
265 | static void free_state(struct mbox_cached_session_state_data * mbox_data) | ||
266 | { | ||
267 | if (mbox_data->mbox_quoted_mb) { | ||
268 | free(mbox_data->mbox_quoted_mb); | ||
269 | mbox_data->mbox_quoted_mb = NULL; | ||
270 | } | ||
271 | } | ||
272 | |||
273 | static int mbox_flags_store_process(char * flags_directory, char * quoted_mb, | ||
274 | struct mail_flags_store * flags_store) | ||
275 | { | ||
276 | char filename_flags[PATH_MAX]; | ||
277 | struct mail_cache_db * cache_db_flags; | ||
278 | MMAPString * mmapstr; | ||
279 | unsigned int i; | ||
280 | int r; | ||
281 | int res; | ||
282 | |||
283 | if (carray_count(flags_store->fls_tab) == 0) | ||
284 | return MAIL_NO_ERROR; | ||
285 | |||
286 | if (quoted_mb == NULL) | ||
287 | return MAIL_NO_ERROR; | ||
288 | |||
289 | snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s", | ||
290 | flags_directory, MAIL_DIR_SEPARATOR, quoted_mb, | ||
291 | MAIL_DIR_SEPARATOR, FLAGS_NAME); | ||
292 | |||
293 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
294 | if (r < 0) { | ||
295 | res = MAIL_ERROR_FILE; | ||
296 | goto err; | ||
297 | } | ||
298 | |||
299 | mmapstr = mmap_string_new(""); | ||
300 | if (mmapstr == NULL) { | ||
301 | res = MAIL_ERROR_MEMORY; | ||
302 | goto close_db_flags; | ||
303 | } | ||
304 | |||
305 | for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) { | ||
306 | mailmessage * msg; | ||
307 | |||
308 | msg = carray_get(flags_store->fls_tab, i); | ||
309 | |||
310 | r = mboxdriver_write_cached_flags(cache_db_flags, mmapstr, | ||
311 | msg->msg_uid, msg->msg_flags); | ||
312 | if (r != MAIL_NO_ERROR) { | ||
313 | /* ignore errors */ | ||
314 | } | ||
315 | } | ||
316 | |||
317 | mmap_string_free(mmapstr); | ||
318 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
319 | |||
320 | mail_flags_store_clear(flags_store); | ||
321 | |||
322 | return MAIL_NO_ERROR; | ||
323 | |||
324 | close_db_flags: | ||
325 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
326 | err: | ||
327 | return res; | ||
328 | } | ||
329 | |||
330 | static void mboxdriver_cached_uninitialize(mailsession * session) | ||
331 | { | ||
332 | struct mbox_cached_session_state_data * data; | ||
333 | |||
334 | data = get_cached_data(session); | ||
335 | |||
336 | mbox_flags_store_process(data->mbox_flags_directory, | ||
337 | data->mbox_quoted_mb, | ||
338 | data->mbox_flags_store); | ||
339 | |||
340 | mail_flags_store_free(data->mbox_flags_store); | ||
341 | |||
342 | free_state(data); | ||
343 | mailsession_free(data->mbox_ancestor); | ||
344 | free(data); | ||
345 | |||
346 | session->sess_data = NULL; | ||
347 | } | ||
348 | |||
349 | static int mboxdriver_cached_parameters(mailsession * session, | ||
350 | int id, void * value) | ||
351 | { | ||
352 | struct mbox_cached_session_state_data * data; | ||
353 | int r; | ||
354 | |||
355 | data = get_cached_data(session); | ||
356 | |||
357 | switch (id) { | ||
358 | case MBOXDRIVER_CACHED_SET_CACHE_DIRECTORY: | ||
359 | strncpy(data->mbox_cache_directory, value, PATH_MAX); | ||
360 | data->mbox_cache_directory[PATH_MAX - 1] = '\0'; | ||
361 | |||
362 | r = generic_cache_create_dir(data->mbox_cache_directory); | ||
363 | if (r != MAIL_NO_ERROR) | ||
364 | return r; | ||
365 | |||
366 | return MAIL_NO_ERROR; | ||
367 | |||
368 | case MBOXDRIVER_CACHED_SET_FLAGS_DIRECTORY: | ||
369 | strncpy(data->mbox_flags_directory, value, PATH_MAX); | ||
370 | data->mbox_flags_directory[PATH_MAX - 1] = '\0'; | ||
371 | |||
372 | r = generic_cache_create_dir(data->mbox_flags_directory); | ||
373 | if (r != MAIL_NO_ERROR) | ||
374 | return r; | ||
375 | |||
376 | return MAIL_NO_ERROR; | ||
377 | |||
378 | case MBOXDRIVER_SET_NO_UID: | ||
379 | return MAIL_ERROR_INVAL; | ||
380 | |||
381 | default: | ||
382 | return mailsession_parameters(data->mbox_ancestor, id, value); | ||
383 | } | ||
384 | } | ||
385 | |||
386 | |||
387 | static int get_cache_directory(mailsession * session, | ||
388 | char * path, char ** result) | ||
389 | { | ||
390 | char * quoted_mb; | ||
391 | char dirname[PATH_MAX]; | ||
392 | int res; | ||
393 | int r; | ||
394 | struct mbox_cached_session_state_data * cached_data; | ||
395 | |||
396 | cached_data = get_cached_data(session); | ||
397 | |||
398 | quoted_mb = maildriver_quote_mailbox(path); | ||
399 | if (quoted_mb == NULL) { | ||
400 | res = MAIL_ERROR_MEMORY; | ||
401 | goto err; | ||
402 | } | ||
403 | |||
404 | snprintf(dirname, PATH_MAX, "%s%c%s", | ||
405 | cached_data->mbox_cache_directory, MAIL_DIR_SEPARATOR, quoted_mb); | ||
406 | |||
407 | r = generic_cache_create_dir(dirname); | ||
408 | if (r != MAIL_NO_ERROR) { | ||
409 | res = r; | ||
410 | goto free; | ||
411 | } | ||
412 | |||
413 | snprintf(dirname, PATH_MAX, "%s%c%s", | ||
414 | cached_data->mbox_flags_directory, MAIL_DIR_SEPARATOR, quoted_mb); | ||
415 | |||
416 | r = generic_cache_create_dir(dirname); | ||
417 | if (r != MAIL_NO_ERROR) { | ||
418 | res = r; | ||
419 | goto free; | ||
420 | } | ||
421 | |||
422 | * result = quoted_mb; | ||
423 | |||
424 | return MAIL_NO_ERROR; | ||
425 | |||
426 | free: | ||
427 | free(quoted_mb); | ||
428 | err: | ||
429 | return res; | ||
430 | } | ||
431 | |||
432 | |||
433 | |||
434 | |||
435 | #define FILENAME_MAX_UID "max-uid" | ||
436 | |||
437 | /* write max uid current value */ | ||
438 | |||
439 | static int write_max_uid_value(mailsession * session) | ||
440 | { | ||
441 | int r; | ||
442 | char filename[PATH_MAX]; | ||
443 | FILE * f; | ||
444 | int res; | ||
445 | |||
446 | #if 0 | ||
447 | struct mbox_session_state_data * mbox_data; | ||
448 | #endif | ||
449 | struct mbox_cached_session_state_data * cached_data; | ||
450 | int fd; | ||
451 | |||
452 | MMAPString * mmapstr; | ||
453 | size_t cur_token; | ||
454 | struct mailmbox_folder * folder; | ||
455 | |||
456 | /* expunge the mailbox */ | ||
457 | |||
458 | #if 0 | ||
459 | mbox_data = get_ancestor(session)->data; | ||
460 | #endif | ||
461 | folder = get_mbox_session(session); | ||
462 | |||
463 | r = mailmbox_validate_write_lock(folder); | ||
464 | if (r != MAILMBOX_NO_ERROR) { | ||
465 | res = mbox_error_to_mail_error(r); | ||
466 | goto err; | ||
467 | } | ||
468 | |||
469 | r = mailmbox_expunge_no_lock(folder); | ||
470 | if (r != MAILMBOX_NO_ERROR) { | ||
471 | res = r; | ||
472 | goto unlock; | ||
473 | } | ||
474 | |||
475 | cached_data = get_cached_data(session); | ||
476 | |||
477 | snprintf(filename, PATH_MAX, "%s%c%s%c%s", | ||
478 | cached_data->mbox_flags_directory, MAIL_DIR_SEPARATOR, | ||
479 | cached_data->mbox_quoted_mb, MAIL_DIR_SEPARATOR, FILENAME_MAX_UID); | ||
480 | |||
481 | fd = creat(filename, S_IRUSR | S_IWUSR); | ||
482 | if (fd < 0) { | ||
483 | res = MAIL_ERROR_FILE; | ||
484 | goto err; | ||
485 | } | ||
486 | |||
487 | f = fdopen(fd, "w"); | ||
488 | if (f == NULL) { | ||
489 | close(fd); | ||
490 | res = MAIL_ERROR_FILE; | ||
491 | goto unlock; | ||
492 | } | ||
493 | |||
494 | mmapstr = mmap_string_new(""); | ||
495 | if (mmapstr == NULL) { | ||
496 | res = MAIL_ERROR_MEMORY; | ||
497 | goto close; | ||
498 | } | ||
499 | |||
500 | r = mail_serialize_clear(mmapstr, &cur_token); | ||
501 | if (r != MAIL_NO_ERROR) { | ||
502 | res = r; | ||
503 | goto free_mmapstr; | ||
504 | } | ||
505 | |||
506 | r = mailimf_cache_int_write(mmapstr, &cur_token, | ||
507 | folder->mb_written_uid); | ||
508 | if (r != MAIL_NO_ERROR) { | ||
509 | res = r; | ||
510 | goto free_mmapstr; | ||
511 | } | ||
512 | |||
513 | fwrite(mmapstr->str, 1, mmapstr->len, f); | ||
514 | |||
515 | mmap_string_free(mmapstr); | ||
516 | fclose(f); | ||
517 | mailmbox_write_unlock(folder); | ||
518 | |||
519 | return MAIL_NO_ERROR; | ||
520 | |||
521 | free_mmapstr: | ||
522 | mmap_string_free(mmapstr); | ||
523 | close: | ||
524 | fclose(f); | ||
525 | unlock: | ||
526 | mailmbox_read_unlock(folder); | ||
527 | err: | ||
528 | return res; | ||
529 | } | ||
530 | |||
531 | static int read_max_uid_value(mailsession * session, uint32_t * result) | ||
532 | { | ||
533 | int r; | ||
534 | char filename[PATH_MAX]; | ||
535 | FILE * f; | ||
536 | uint32_t written_uid; | ||
537 | int res; | ||
538 | |||
539 | struct mbox_cached_session_state_data * cached_data; | ||
540 | |||
541 | MMAPString * mmapstr; | ||
542 | size_t cur_token; | ||
543 | char buf[sizeof(uint32_t)]; | ||
544 | size_t read_size; | ||
545 | |||
546 | cached_data = get_cached_data(session); | ||
547 | |||
548 | snprintf(filename, PATH_MAX, "%s%c%s%c%s", | ||
549 | cached_data->mbox_flags_directory, MAIL_DIR_SEPARATOR, | ||
550 | cached_data->mbox_quoted_mb, MAIL_DIR_SEPARATOR, FILENAME_MAX_UID); | ||
551 | |||
552 | f = fopen(filename, "r"); | ||
553 | if (f == NULL) { | ||
554 | res = MAIL_ERROR_FILE; | ||
555 | goto err; | ||
556 | } | ||
557 | |||
558 | read_size = fread(buf, 1, sizeof(uint32_t), f); | ||
559 | |||
560 | mmapstr = mmap_string_new_len(buf, read_size); | ||
561 | if (mmapstr == NULL) { | ||
562 | res = MAIL_ERROR_MEMORY; | ||
563 | goto close; | ||
564 | } | ||
565 | |||
566 | cur_token = 0; | ||
567 | |||
568 | r = mailimf_cache_int_read(mmapstr, &cur_token, &written_uid); | ||
569 | if (r != MAIL_NO_ERROR) { | ||
570 | fclose(f); | ||
571 | res = r; | ||
572 | goto free_mmapstr; | ||
573 | } | ||
574 | |||
575 | mmap_string_free(mmapstr); | ||
576 | fclose(f); | ||
577 | |||
578 | * result = written_uid; | ||
579 | |||
580 | return MAIL_NO_ERROR; | ||
581 | |||
582 | free_mmapstr: | ||
583 | mmap_string_free(mmapstr); | ||
584 | close: | ||
585 | fclose(f); | ||
586 | err: | ||
587 | return res; | ||
588 | } | ||
589 | |||
590 | static int mboxdriver_cached_connect_path(mailsession * session, char * path) | ||
591 | { | ||
592 | int r; | ||
593 | int res; | ||
594 | char * quoted_mb; | ||
595 | struct mbox_cached_session_state_data * cached_data; | ||
596 | struct mbox_session_state_data * ancestor_data; | ||
597 | struct mailmbox_folder * folder; | ||
598 | uint32_t written_uid; | ||
599 | |||
600 | folder = get_mbox_session(session); | ||
601 | if (folder != NULL) { | ||
602 | res = MAIL_ERROR_BAD_STATE; | ||
603 | goto err; | ||
604 | } | ||
605 | |||
606 | r = get_cache_directory(session, path, "ed_mb); | ||
607 | if (r != MAIL_NO_ERROR) { | ||
608 | res = r; | ||
609 | goto err; | ||
610 | } | ||
611 | |||
612 | cached_data = get_cached_data(session); | ||
613 | free_state(cached_data); | ||
614 | |||
615 | cached_data->mbox_quoted_mb = quoted_mb; | ||
616 | |||
617 | written_uid = 0; | ||
618 | r = read_max_uid_value(session, &written_uid); | ||
619 | /* ignore errors */ | ||
620 | |||
621 | ancestor_data = get_ancestor_data(session); | ||
622 | |||
623 | r = mailmbox_init(path, | ||
624 | ancestor_data->mbox_force_read_only, | ||
625 | ancestor_data->mbox_force_no_uid, | ||
626 | written_uid, | ||
627 | &folder); | ||
628 | |||
629 | if (r != MAILMBOX_NO_ERROR) { | ||
630 | cached_data->mbox_quoted_mb = NULL; | ||
631 | |||
632 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
633 | goto free; | ||
634 | } | ||
635 | |||
636 | ancestor_data->mbox_folder = folder; | ||
637 | |||
638 | return MAIL_NO_ERROR; | ||
639 | |||
640 | free: | ||
641 | free(quoted_mb); | ||
642 | err: | ||
643 | return res; | ||
644 | } | ||
645 | |||
646 | |||
647 | static int mboxdriver_cached_logout(mailsession * session) | ||
648 | { | ||
649 | struct mbox_cached_session_state_data * cached_data; | ||
650 | int r; | ||
651 | |||
652 | r = write_max_uid_value(session); | ||
653 | |||
654 | cached_data = get_cached_data(session); | ||
655 | |||
656 | mbox_flags_store_process(cached_data->mbox_flags_directory, | ||
657 | cached_data->mbox_quoted_mb, | ||
658 | cached_data->mbox_flags_store); | ||
659 | |||
660 | r = mailsession_logout(get_ancestor(session)); | ||
661 | if (r != MAIL_NO_ERROR) | ||
662 | return r; | ||
663 | |||
664 | free_state(cached_data); | ||
665 | |||
666 | return MAIL_NO_ERROR; | ||
667 | } | ||
668 | |||
669 | static int mboxdriver_cached_check_folder(mailsession * session) | ||
670 | { | ||
671 | struct mbox_cached_session_state_data * cached_data; | ||
672 | |||
673 | cached_data = get_cached_data(session); | ||
674 | |||
675 | mbox_flags_store_process(cached_data->mbox_flags_directory, | ||
676 | cached_data->mbox_quoted_mb, | ||
677 | cached_data->mbox_flags_store); | ||
678 | |||
679 | return MAIL_NO_ERROR; | ||
680 | } | ||
681 | |||
682 | static int mboxdriver_cached_expunge_folder(mailsession * session) | ||
683 | { | ||
684 | struct mailmbox_folder * folder; | ||
685 | int res; | ||
686 | char filename_flags[PATH_MAX]; | ||
687 | struct mail_cache_db * cache_db_flags; | ||
688 | MMAPString * mmapstr; | ||
689 | struct mbox_cached_session_state_data * data; | ||
690 | int r; | ||
691 | unsigned int i; | ||
692 | |||
693 | folder = get_mbox_session(session); | ||
694 | if (folder == NULL) { | ||
695 | res = MAIL_ERROR_BAD_STATE; | ||
696 | goto err; | ||
697 | } | ||
698 | |||
699 | data = get_cached_data(session); | ||
700 | if (data->mbox_quoted_mb == NULL) { | ||
701 | res = MAIL_ERROR_BAD_STATE; | ||
702 | goto err; | ||
703 | } | ||
704 | |||
705 | mbox_flags_store_process(data->mbox_flags_directory, | ||
706 | data->mbox_quoted_mb, | ||
707 | data->mbox_flags_store); | ||
708 | |||
709 | snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s", | ||
710 | data->mbox_flags_directory, MAIL_DIR_SEPARATOR, data->mbox_quoted_mb, | ||
711 | MAIL_DIR_SEPARATOR, FLAGS_NAME); | ||
712 | |||
713 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
714 | if (r < 0) { | ||
715 | res = MAIL_ERROR_FILE; | ||
716 | goto err; | ||
717 | } | ||
718 | |||
719 | mmapstr = mmap_string_new(""); | ||
720 | if (mmapstr == NULL) { | ||
721 | res = MAIL_ERROR_MEMORY; | ||
722 | goto close_db_flags; | ||
723 | } | ||
724 | |||
725 | for(i = 0 ; i < carray_count(folder->mb_tab) ; i ++) { | ||
726 | struct mailmbox_msg_info * msg_info; | ||
727 | struct mail_flags * flags; | ||
728 | |||
729 | msg_info = carray_get(folder->mb_tab, i); | ||
730 | if (msg_info == NULL) | ||
731 | continue; | ||
732 | |||
733 | if (msg_info->msg_deleted) | ||
734 | continue; | ||
735 | |||
736 | r = mboxdriver_get_cached_flags(cache_db_flags, mmapstr, | ||
737 | session, msg_info->msg_uid, &flags); | ||
738 | if (r != MAIL_NO_ERROR) | ||
739 | continue; | ||
740 | |||
741 | if (flags->fl_flags & MAIL_FLAG_DELETED) { | ||
742 | r = mailmbox_delete_msg(folder, msg_info->msg_uid); | ||
743 | } | ||
744 | |||
745 | mail_flags_free(flags); | ||
746 | } | ||
747 | |||
748 | mmap_string_free(mmapstr); | ||
749 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
750 | |||
751 | r = mailmbox_expunge(folder); | ||
752 | |||
753 | return MAIL_NO_ERROR; | ||
754 | |||
755 | close_db_flags: | ||
756 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
757 | err: | ||
758 | return res; | ||
759 | } | ||
760 | |||
761 | static int mboxdriver_cached_status_folder(mailsession * session, char * mb, | ||
762 | uint32_t * result_messages, uint32_t * result_recent, | ||
763 | uint32_t * result_unseen) | ||
764 | { | ||
765 | struct mailmbox_folder * folder; | ||
766 | int res; | ||
767 | char filename_flags[PATH_MAX]; | ||
768 | struct mail_cache_db * cache_db_flags; | ||
769 | MMAPString * mmapstr; | ||
770 | struct mbox_cached_session_state_data * data; | ||
771 | int r; | ||
772 | unsigned int i; | ||
773 | uint32_t recent; | ||
774 | uint32_t unseen; | ||
775 | uint32_t num; | ||
776 | |||
777 | num = 0; | ||
778 | recent = 0; | ||
779 | unseen = 0; | ||
780 | |||
781 | folder = get_mbox_session(session); | ||
782 | if (folder == NULL) { | ||
783 | res = MAIL_ERROR_BAD_STATE; | ||
784 | goto err; | ||
785 | } | ||
786 | |||
787 | data = get_cached_data(session); | ||
788 | if (data->mbox_quoted_mb == NULL) { | ||
789 | res = MAIL_ERROR_BAD_STATE; | ||
790 | goto err; | ||
791 | } | ||
792 | |||
793 | r = mailmbox_validate_read_lock(folder); | ||
794 | if (r != MAIL_NO_ERROR) { | ||
795 | res = MAIL_ERROR_BAD_STATE; | ||
796 | goto err; | ||
797 | } | ||
798 | |||
799 | mailmbox_read_unlock(folder); | ||
800 | |||
801 | mbox_flags_store_process(data->mbox_flags_directory, data->mbox_quoted_mb, | ||
802 | data->mbox_flags_store); | ||
803 | |||
804 | snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s", | ||
805 | data->mbox_flags_directory, MAIL_DIR_SEPARATOR, data->mbox_quoted_mb, | ||
806 | MAIL_DIR_SEPARATOR, FLAGS_NAME); | ||
807 | |||
808 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
809 | if (r < 0) { | ||
810 | res = MAIL_ERROR_FILE; | ||
811 | goto err; | ||
812 | } | ||
813 | |||
814 | mmapstr = mmap_string_new(""); | ||
815 | if (mmapstr == NULL) { | ||
816 | res = MAIL_ERROR_MEMORY; | ||
817 | goto close_db_flags; | ||
818 | } | ||
819 | |||
820 | for(i = 0 ; i < carray_count(folder->mb_tab) ; i ++) { | ||
821 | struct mailmbox_msg_info * msg_info; | ||
822 | struct mail_flags * flags; | ||
823 | |||
824 | msg_info = carray_get(folder->mb_tab, i); | ||
825 | if (msg_info == NULL) | ||
826 | continue; | ||
827 | |||
828 | if (msg_info->msg_deleted) | ||
829 | continue; | ||
830 | |||
831 | r = mboxdriver_get_cached_flags(cache_db_flags, mmapstr, | ||
832 | session, msg_info->msg_uid, &flags); | ||
833 | if (r != MAIL_NO_ERROR) { | ||
834 | recent ++; | ||
835 | unseen ++; | ||
836 | num ++; | ||
837 | continue; | ||
838 | } | ||
839 | |||
840 | if ((flags->fl_flags & MAIL_FLAG_NEW) != 0) { | ||
841 | recent ++; | ||
842 | } | ||
843 | if ((flags->fl_flags & MAIL_FLAG_SEEN) == 0) { | ||
844 | unseen ++; | ||
845 | } | ||
846 | |||
847 | num ++; | ||
848 | |||
849 | mail_flags_free(flags); | ||
850 | } | ||
851 | |||
852 | mmap_string_free(mmapstr); | ||
853 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
854 | |||
855 | * result_messages = num; | ||
856 | * result_recent = recent; | ||
857 | * result_unseen = unseen; | ||
858 | |||
859 | return MAIL_NO_ERROR; | ||
860 | |||
861 | close_db_flags: | ||
862 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
863 | err: | ||
864 | return res; | ||
865 | } | ||
866 | |||
867 | static int mboxdriver_cached_messages_number(mailsession * session, char * mb, | ||
868 | uint32_t * result) | ||
869 | { | ||
870 | return mailsession_messages_number(get_ancestor(session), mb, result); | ||
871 | } | ||
872 | |||
873 | |||
874 | static int mboxdriver_cached_recent_number(mailsession * session, char * mb, | ||
875 | uint32_t * result) | ||
876 | { | ||
877 | uint32_t messages; | ||
878 | uint32_t recent; | ||
879 | uint32_t unseen; | ||
880 | int r; | ||
881 | |||
882 | r = mboxdriver_cached_status_folder(session, mb, &messages, &recent, &unseen); | ||
883 | if (r != MAIL_NO_ERROR) | ||
884 | return r; | ||
885 | |||
886 | * result = recent; | ||
887 | |||
888 | return MAIL_NO_ERROR; | ||
889 | } | ||
890 | |||
891 | static int mboxdriver_cached_unseen_number(mailsession * session, char * mb, | ||
892 | uint32_t * result) | ||
893 | { | ||
894 | uint32_t messages; | ||
895 | uint32_t recent; | ||
896 | uint32_t unseen; | ||
897 | int r; | ||
898 | |||
899 | r = mboxdriver_cached_status_folder(session, mb, | ||
900 | &messages, &recent, &unseen); | ||
901 | if (r != MAIL_NO_ERROR) | ||
902 | return r; | ||
903 | |||
904 | * result = unseen; | ||
905 | |||
906 | return MAIL_NO_ERROR; | ||
907 | } | ||
908 | |||
909 | /* messages operations */ | ||
910 | |||
911 | static int mboxdriver_cached_append_message(mailsession * session, | ||
912 | char * message, size_t size) | ||
913 | { | ||
914 | return mboxdriver_cached_append_message_flags(session, | ||
915 | message, size, NULL); | ||
916 | } | ||
917 | |||
918 | static int mboxdriver_cached_append_message_flags(mailsession * session, | ||
919 | char * message, size_t size, struct mail_flags * flags) | ||
920 | { | ||
921 | int r; | ||
922 | struct mailmbox_folder * folder; | ||
923 | struct mbox_cached_session_state_data * data; | ||
924 | unsigned int uid; | ||
925 | struct mailmbox_msg_info * msg_info; | ||
926 | chashdatum key; | ||
927 | chashdatum value; | ||
928 | struct mail_cache_db * cache_db_flags; | ||
929 | char filename_flags[PATH_MAX]; | ||
930 | MMAPString * mmapstr; | ||
931 | char keyname[PATH_MAX]; | ||
932 | |||
933 | folder = get_mbox_session(session); | ||
934 | if (folder == NULL) | ||
935 | return MAIL_ERROR_APPEND; | ||
936 | |||
937 | r = mailmbox_append_message_uid(folder, message, size, &uid); | ||
938 | |||
939 | switch (r) { | ||
940 | case MAILMBOX_ERROR_FILE: | ||
941 | return MAIL_ERROR_DISKSPACE; | ||
942 | case MAILMBOX_NO_ERROR: | ||
943 | break; | ||
944 | default: | ||
945 | return mboxdriver_mbox_error_to_mail_error(r); | ||
946 | } | ||
947 | |||
948 | /* could store in flags store instead */ | ||
949 | |||
950 | if (flags == NULL) | ||
951 | goto exit; | ||
952 | |||
953 | key.data = &uid; | ||
954 | key.len = sizeof(uid); | ||
955 | r = chash_get(folder->mb_hash, &key, &value); | ||
956 | if (r < 0) | ||
957 | goto exit; | ||
958 | |||
959 | msg_info = value.data; | ||
960 | |||
961 | data = get_cached_data(session); | ||
962 | |||
963 | snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s", | ||
964 | data->mbox_flags_directory, MAIL_DIR_SEPARATOR, data->mbox_quoted_mb, | ||
965 | MAIL_DIR_SEPARATOR, FLAGS_NAME); | ||
966 | |||
967 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
968 | if (r < 0) | ||
969 | goto exit; | ||
970 | |||
971 | mmapstr = mmap_string_new(""); | ||
972 | if (mmapstr == NULL) | ||
973 | goto close_db_flags; | ||
974 | |||
975 | snprintf(keyname, PATH_MAX, "%u-%lu", uid, | ||
976 | (unsigned long) msg_info->msg_body_len); | ||
977 | |||
978 | r = mboxdriver_write_cached_flags(cache_db_flags, mmapstr, keyname, flags); | ||
979 | |||
980 | mmap_string_free(mmapstr); | ||
981 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
982 | |||
983 | if (r != MAIL_NO_ERROR) | ||
984 | goto exit; | ||
985 | |||
986 | return MAIL_NO_ERROR; | ||
987 | |||
988 | close_db_flags: | ||
989 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
990 | exit: | ||
991 | return MAIL_NO_ERROR; | ||
992 | } | ||
993 | |||
994 | static int | ||
995 | mboxdriver_cached_get_messages_list(mailsession * session, | ||
996 | struct mailmessage_list ** result) | ||
997 | { | ||
998 | struct mailmbox_folder * folder; | ||
999 | int res; | ||
1000 | |||
1001 | folder = get_mbox_session(session); | ||
1002 | if (folder == NULL) { | ||
1003 | res = MAIL_ERROR_BAD_STATE; | ||
1004 | goto err; | ||
1005 | } | ||
1006 | |||
1007 | return mbox_get_uid_messages_list(folder, | ||
1008 | session, mbox_cached_message_driver, result); | ||
1009 | |||
1010 | err: | ||
1011 | return res; | ||
1012 | } | ||
1013 | |||
1014 | static int | ||
1015 | get_cached_envelope(struct mail_cache_db * cache_db, MMAPString * mmapstr, | ||
1016 | mailsession * session, uint32_t num, | ||
1017 | struct mailimf_fields ** result) | ||
1018 | { | ||
1019 | int r; | ||
1020 | char keyname[PATH_MAX]; | ||
1021 | struct mailimf_fields * fields; | ||
1022 | int res; | ||
1023 | struct mailmbox_msg_info * info; | ||
1024 | struct mailmbox_folder * folder; | ||
1025 | chashdatum key; | ||
1026 | chashdatum data; | ||
1027 | |||
1028 | folder = get_mbox_session(session); | ||
1029 | if (folder == NULL) { | ||
1030 | res = MAIL_ERROR_BAD_STATE; | ||
1031 | goto err; | ||
1032 | } | ||
1033 | |||
1034 | key.data = # | ||
1035 | key.len = sizeof(num); | ||
1036 | |||
1037 | r = chash_get(folder->mb_hash, &key, &data); | ||
1038 | if (r < 0) { | ||
1039 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
1040 | goto err; | ||
1041 | } | ||
1042 | |||
1043 | info = data.data; | ||
1044 | |||
1045 | snprintf(keyname, PATH_MAX, "%u-%lu-envelope", num, | ||
1046 | (unsigned long) info->msg_body_len); | ||
1047 | |||
1048 | r = generic_cache_fields_read(cache_db, mmapstr, keyname, &fields); | ||
1049 | if (r != MAIL_NO_ERROR) { | ||
1050 | res = r; | ||
1051 | goto err; | ||
1052 | } | ||
1053 | |||
1054 | * result = fields; | ||
1055 | |||
1056 | return MAIL_NO_ERROR; | ||
1057 | |||
1058 | err: | ||
1059 | return res; | ||
1060 | } | ||
1061 | |||
1062 | static int | ||
1063 | write_cached_envelope(struct mail_cache_db * cache_db, MMAPString * mmapstr, | ||
1064 | mailsession * session, uint32_t num, | ||
1065 | struct mailimf_fields * fields) | ||
1066 | { | ||
1067 | int r; | ||
1068 | char keyname[PATH_MAX]; | ||
1069 | int res; | ||
1070 | struct mailmbox_msg_info * info; | ||
1071 | struct mailmbox_folder * folder; | ||
1072 | chashdatum key; | ||
1073 | chashdatum data; | ||
1074 | |||
1075 | folder = get_mbox_session(session); | ||
1076 | if (folder == NULL) { | ||
1077 | res = MAIL_ERROR_BAD_STATE; | ||
1078 | goto err; | ||
1079 | } | ||
1080 | |||
1081 | key.data = # | ||
1082 | key.len = sizeof(num); | ||
1083 | |||
1084 | r = chash_get(folder->mb_hash, &key, &data); | ||
1085 | if (r < 0) { | ||
1086 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
1087 | goto err; | ||
1088 | } | ||
1089 | |||
1090 | info = data.data; | ||
1091 | |||
1092 | snprintf(keyname, PATH_MAX, "%u-%lu-envelope", num, | ||
1093 | (unsigned long) info->msg_body_len); | ||
1094 | |||
1095 | r = generic_cache_fields_write(cache_db, mmapstr, keyname, fields); | ||
1096 | if (r != MAIL_NO_ERROR) { | ||
1097 | res = r; | ||
1098 | goto err; | ||
1099 | } | ||
1100 | |||
1101 | return MAIL_NO_ERROR; | ||
1102 | |||
1103 | err: | ||
1104 | return res; | ||
1105 | } | ||
1106 | |||
1107 | static int | ||
1108 | mboxdriver_cached_get_envelopes_list(mailsession * session, | ||
1109 | struct mailmessage_list * env_list) | ||
1110 | { | ||
1111 | int r; | ||
1112 | unsigned int i; | ||
1113 | struct mbox_cached_session_state_data * cached_data; | ||
1114 | char filename_env[PATH_MAX]; | ||
1115 | char filename_flags[PATH_MAX]; | ||
1116 | struct mail_cache_db * cache_db_env; | ||
1117 | struct mail_cache_db * cache_db_flags; | ||
1118 | MMAPString * mmapstr; | ||
1119 | int res; | ||
1120 | struct mailmbox_folder * folder; | ||
1121 | |||
1122 | folder = get_mbox_session(session); | ||
1123 | if (folder == NULL) { | ||
1124 | res = MAIL_ERROR_BAD_STATE; | ||
1125 | goto err; | ||
1126 | } | ||
1127 | |||
1128 | cached_data = get_cached_data(session); | ||
1129 | if (cached_data->mbox_quoted_mb == NULL) { | ||
1130 | res = MAIL_ERROR_BAD_STATE; | ||
1131 | goto err; | ||
1132 | } | ||
1133 | |||
1134 | mbox_flags_store_process(cached_data->mbox_flags_directory, | ||
1135 | cached_data->mbox_quoted_mb, | ||
1136 | cached_data->mbox_flags_store); | ||
1137 | |||
1138 | mmapstr = mmap_string_new(""); | ||
1139 | if (mmapstr == NULL) { | ||
1140 | res = MAIL_ERROR_MEMORY; | ||
1141 | goto err; | ||
1142 | } | ||
1143 | |||
1144 | snprintf(filename_env, PATH_MAX, "%s%c%s%c%s", | ||
1145 | cached_data->mbox_cache_directory, MAIL_DIR_SEPARATOR, | ||
1146 | cached_data->mbox_quoted_mb, | ||
1147 | MAIL_DIR_SEPARATOR, ENV_NAME); | ||
1148 | |||
1149 | r = mail_cache_db_open_lock(filename_env, &cache_db_env); | ||
1150 | if (r < 0) { | ||
1151 | res = MAIL_ERROR_MEMORY; | ||
1152 | goto free_mmapstr; | ||
1153 | } | ||
1154 | |||
1155 | snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s", | ||
1156 | cached_data->mbox_flags_directory, MAIL_DIR_SEPARATOR, | ||
1157 | cached_data->mbox_quoted_mb, | ||
1158 | MAIL_DIR_SEPARATOR, FLAGS_NAME); | ||
1159 | |||
1160 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
1161 | if (r < 0) { | ||
1162 | res = MAIL_ERROR_FILE; | ||
1163 | goto close_db_env; | ||
1164 | } | ||
1165 | |||
1166 | /* fill with cached */ | ||
1167 | |||
1168 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
1169 | mailmessage * msg; | ||
1170 | struct mailimf_fields * fields; | ||
1171 | struct mail_flags * flags; | ||
1172 | |||
1173 | msg = carray_get(env_list->msg_tab, i); | ||
1174 | |||
1175 | if (msg->msg_fields == NULL) { | ||
1176 | r = get_cached_envelope(cache_db_env, mmapstr, session, | ||
1177 | msg->msg_index, &fields); | ||
1178 | if (r == MAIL_NO_ERROR) { | ||
1179 | msg->msg_cached = TRUE; | ||
1180 | msg->msg_fields = fields; | ||
1181 | } | ||
1182 | } | ||
1183 | |||
1184 | if (msg->msg_flags == NULL) { | ||
1185 | r = mboxdriver_get_cached_flags(cache_db_flags, mmapstr, | ||
1186 | session, msg->msg_index, | ||
1187 | &flags); | ||
1188 | if (r == MAIL_NO_ERROR) { | ||
1189 | msg->msg_flags = flags; | ||
1190 | } | ||
1191 | } | ||
1192 | } | ||
1193 | |||
1194 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
1195 | mail_cache_db_close_unlock(filename_env, cache_db_env); | ||
1196 | |||
1197 | r = mailsession_get_envelopes_list(get_ancestor(session), env_list); | ||
1198 | |||
1199 | if (r != MAIL_NO_ERROR) { | ||
1200 | res = r; | ||
1201 | goto free_mmapstr; | ||
1202 | } | ||
1203 | |||
1204 | /* add flags */ | ||
1205 | |||
1206 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
1207 | mailmessage * msg; | ||
1208 | |||
1209 | msg = carray_get(env_list->msg_tab, i); | ||
1210 | |||
1211 | if (msg->msg_flags == NULL) | ||
1212 | msg->msg_flags = mail_flags_new_empty(); | ||
1213 | } | ||
1214 | |||
1215 | r = mail_cache_db_open_lock(filename_env, &cache_db_env); | ||
1216 | if (r < 0) { | ||
1217 | res = MAIL_ERROR_MEMORY; | ||
1218 | goto free_mmapstr; | ||
1219 | } | ||
1220 | |||
1221 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
1222 | if (r < 0) { | ||
1223 | res = MAIL_ERROR_FILE; | ||
1224 | goto close_db_env; | ||
1225 | } | ||
1226 | |||
1227 | /* must write cache */ | ||
1228 | |||
1229 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
1230 | mailmessage * msg; | ||
1231 | |||
1232 | msg = carray_get(env_list->msg_tab, i); | ||
1233 | |||
1234 | if (msg->msg_fields != NULL) { | ||
1235 | if (!msg->msg_cached) { | ||
1236 | /* msg->msg_index is the numerical UID of the message */ | ||
1237 | r = write_cached_envelope(cache_db_env, mmapstr, | ||
1238 | session, msg->msg_index, msg->msg_fields); | ||
1239 | } | ||
1240 | } | ||
1241 | |||
1242 | if (msg->msg_flags != NULL) { | ||
1243 | r = mboxdriver_write_cached_flags(cache_db_flags, mmapstr, | ||
1244 | msg->msg_uid, msg->msg_flags); | ||
1245 | } | ||
1246 | } | ||
1247 | |||
1248 | /* flush cache */ | ||
1249 | |||
1250 | maildriver_cache_clean_up(cache_db_env, cache_db_flags, env_list); | ||
1251 | |||
1252 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
1253 | mail_cache_db_close_unlock(filename_env, cache_db_env); | ||
1254 | |||
1255 | mmap_string_free(mmapstr); | ||
1256 | |||
1257 | return MAIL_NO_ERROR; | ||
1258 | |||
1259 | close_db_env: | ||
1260 | mail_cache_db_close_unlock(filename_env, cache_db_env); | ||
1261 | free_mmapstr: | ||
1262 | mmap_string_free(mmapstr); | ||
1263 | err: | ||
1264 | return res; | ||
1265 | } | ||
1266 | |||
1267 | |||
1268 | static int | ||
1269 | mboxdriver_cached_remove_message(mailsession * session, uint32_t num) | ||
1270 | { | ||
1271 | return mailsession_remove_message(get_ancestor(session), num); | ||
1272 | } | ||
1273 | |||
1274 | static int mboxdriver_cached_get_message(mailsession * session, | ||
1275 | uint32_t num, mailmessage ** result) | ||
1276 | { | ||
1277 | mailmessage * msg_info; | ||
1278 | int r; | ||
1279 | |||
1280 | msg_info = mailmessage_new(); | ||
1281 | if (msg_info == NULL) | ||
1282 | return MAIL_ERROR_MEMORY; | ||
1283 | |||
1284 | r = mailmessage_init(msg_info, session, mbox_cached_message_driver, num, 0); | ||
1285 | if (r != MAIL_NO_ERROR) { | ||
1286 | mailmessage_free(msg_info); | ||
1287 | return r; | ||
1288 | } | ||
1289 | |||
1290 | * result = msg_info; | ||
1291 | |||
1292 | return MAIL_NO_ERROR; | ||
1293 | } | ||
1294 | |||
1295 | static int mboxdriver_cached_get_message_by_uid(mailsession * session, | ||
1296 | const char * uid, | ||
1297 | mailmessage ** result) | ||
1298 | { | ||
1299 | uint32_t num; | ||
1300 | char * p; | ||
1301 | chashdatum key; | ||
1302 | chashdatum data; | ||
1303 | struct mailmbox_msg_info * info; | ||
1304 | struct mailmbox_folder * folder; | ||
1305 | int r; | ||
1306 | |||
1307 | if (uid == NULL) | ||
1308 | return MAIL_ERROR_INVAL; | ||
1309 | |||
1310 | num = strtoul(uid, &p, 10); | ||
1311 | if (p == uid || * p != '-') | ||
1312 | return MAIL_ERROR_INVAL; | ||
1313 | |||
1314 | folder = get_mbox_session(session); | ||
1315 | if (folder == NULL) | ||
1316 | return MAIL_ERROR_BAD_STATE; | ||
1317 | |||
1318 | key.data = # | ||
1319 | key.len = sizeof(num); | ||
1320 | |||
1321 | r = chash_get(folder->mb_hash, &key, &data); | ||
1322 | if (r == 0) { | ||
1323 | char * body_len_p = p + 1; | ||
1324 | size_t body_len; | ||
1325 | |||
1326 | info = data.data; | ||
1327 | /* Check if the cached message has the same UID */ | ||
1328 | body_len = strtoul(body_len_p, &p, 10); | ||
1329 | if (p == body_len_p || * p != '\0') | ||
1330 | return MAIL_ERROR_INVAL; | ||
1331 | |||
1332 | if (body_len == info->msg_body_len) | ||
1333 | return mboxdriver_cached_get_message(session, num, result); | ||
1334 | } | ||
1335 | |||
1336 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
1337 | } | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_cached.h b/libetpan/src/driver/implementation/mbox/mboxdriver_cached.h new file mode 100644 index 0000000..25c4027 --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_cached.h | |||
@@ -0,0 +1,54 @@ | |||
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 | #ifndef MBOXDRIVER_CACHED_H | ||
37 | |||
38 | #define MBOXDRIVER_CACHED_H | ||
39 | |||
40 | #include <libetpan/libetpan-config.h> | ||
41 | |||
42 | #include <libetpan/mboxdriver_types.h> | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | extern "C" { | ||
46 | #endif | ||
47 | |||
48 | extern mailsession_driver * mbox_cached_session_driver; | ||
49 | |||
50 | #ifdef __cplusplus | ||
51 | } | ||
52 | #endif | ||
53 | |||
54 | #endif | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_cached_message.c b/libetpan/src/driver/implementation/mbox/mboxdriver_cached_message.c new file mode 100644 index 0000000..9f77d32 --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_cached_message.c | |||
@@ -0,0 +1,361 @@ | |||
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 "mboxdriver_cached_message.h" | ||
37 | |||
38 | #include "mailmessage_tools.h" | ||
39 | #include "mboxdriver_tools.h" | ||
40 | #include "mboxdriver_cached.h" | ||
41 | #include "mboxdriver.h" | ||
42 | #include "mailmbox.h" | ||
43 | #include "mail_cache_db.h" | ||
44 | #include "generic_cache.h" | ||
45 | |||
46 | #include <unistd.h> | ||
47 | #include <sys/mman.h> | ||
48 | #include <sys/types.h> | ||
49 | #include <sys/stat.h> | ||
50 | #include <fcntl.h> | ||
51 | #include <string.h> | ||
52 | #include <stdlib.h> | ||
53 | |||
54 | static int mbox_prefetch(mailmessage * msg_info); | ||
55 | |||
56 | static void mbox_prefetch_free(struct generic_message_t * msg); | ||
57 | |||
58 | static int mbox_initialize(mailmessage * msg_info); | ||
59 | |||
60 | static void mbox_uninitialize(mailmessage * msg_info); | ||
61 | |||
62 | static void mbox_flush(mailmessage * msg_info); | ||
63 | |||
64 | static void mbox_check(mailmessage * msg_info); | ||
65 | |||
66 | static int mbox_fetch_size(mailmessage * msg_info, | ||
67 | size_t * result); | ||
68 | |||
69 | static int mbox_get_flags(mailmessage * msg_info, | ||
70 | struct mail_flags ** result); | ||
71 | |||
72 | static int mbox_fetch_header(mailmessage * msg_info, | ||
73 | char ** result, | ||
74 | size_t * result_len); | ||
75 | |||
76 | static mailmessage_driver local_mbox_cached_message_driver = { | ||
77 | .msg_name = "mbox-cached", | ||
78 | |||
79 | .msg_initialize = mbox_initialize, | ||
80 | .msg_uninitialize = mbox_uninitialize, | ||
81 | |||
82 | .msg_flush = mbox_flush, | ||
83 | .msg_check = mbox_check, | ||
84 | |||
85 | .msg_fetch_result_free = mailmessage_generic_fetch_result_free, | ||
86 | |||
87 | .msg_fetch = mailmessage_generic_fetch, | ||
88 | .msg_fetch_header = mbox_fetch_header, | ||
89 | .msg_fetch_body = mailmessage_generic_fetch_body, | ||
90 | .msg_fetch_size = mbox_fetch_size, | ||
91 | .msg_get_bodystructure = mailmessage_generic_get_bodystructure, | ||
92 | .msg_fetch_section = mailmessage_generic_fetch_section, | ||
93 | .msg_fetch_section_header = mailmessage_generic_fetch_section_header, | ||
94 | .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime, | ||
95 | .msg_fetch_section_body = mailmessage_generic_fetch_section_body, | ||
96 | .msg_fetch_envelope = mailmessage_generic_fetch_envelope, | ||
97 | |||
98 | .msg_get_flags = mbox_get_flags, | ||
99 | }; | ||
100 | |||
101 | mailmessage_driver * mbox_cached_message_driver = | ||
102 | &local_mbox_cached_message_driver; | ||
103 | |||
104 | static inline struct mbox_cached_session_state_data * | ||
105 | get_cached_session_data(mailmessage * msg) | ||
106 | { | ||
107 | return msg->msg_session->sess_data; | ||
108 | } | ||
109 | |||
110 | static inline mailsession * get_ancestor_session(mailmessage * msg) | ||
111 | { | ||
112 | return get_cached_session_data(msg)->mbox_ancestor; | ||
113 | } | ||
114 | |||
115 | static inline struct mbox_session_state_data * | ||
116 | get_ancestor_session_data(mailmessage * msg) | ||
117 | { | ||
118 | return get_ancestor_session(msg)->sess_data; | ||
119 | } | ||
120 | |||
121 | static inline struct mailmbox_folder * | ||
122 | get_mbox_session(mailmessage * msg) | ||
123 | { | ||
124 | return get_ancestor_session_data(msg)->mbox_folder; | ||
125 | } | ||
126 | |||
127 | static int mbox_prefetch(mailmessage * msg_info) | ||
128 | { | ||
129 | struct generic_message_t * msg; | ||
130 | int r; | ||
131 | char * msg_content; | ||
132 | size_t msg_length; | ||
133 | |||
134 | r = mboxdriver_fetch_msg(get_ancestor_session(msg_info), | ||
135 | msg_info->msg_index, | ||
136 | &msg_content, &msg_length); | ||
137 | if (r != MAIL_NO_ERROR) | ||
138 | return r; | ||
139 | |||
140 | msg = msg_info->msg_data; | ||
141 | |||
142 | msg->msg_message = msg_content; | ||
143 | msg->msg_length = msg_length; | ||
144 | |||
145 | return MAIL_NO_ERROR; | ||
146 | } | ||
147 | |||
148 | static void mbox_prefetch_free(struct generic_message_t * msg) | ||
149 | { | ||
150 | if (msg->msg_message != NULL) { | ||
151 | mmap_string_unref(msg->msg_message); | ||
152 | msg->msg_message = NULL; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | static int mbox_initialize(mailmessage * msg_info) | ||
157 | { | ||
158 | struct generic_message_t * msg; | ||
159 | int r; | ||
160 | char * uid; | ||
161 | char static_uid[PATH_MAX]; | ||
162 | struct mailmbox_msg_info * info; | ||
163 | struct mailmbox_folder * folder; | ||
164 | int res; | ||
165 | chashdatum key; | ||
166 | chashdatum data; | ||
167 | |||
168 | folder = get_mbox_session(msg_info); | ||
169 | if (folder == NULL) { | ||
170 | res = MAIL_ERROR_BAD_STATE; | ||
171 | goto err; | ||
172 | } | ||
173 | |||
174 | key.data = (char *) &msg_info->msg_index; | ||
175 | key.len = sizeof(msg_info->msg_index); | ||
176 | |||
177 | r = chash_get(folder->mb_hash, &key, &data); | ||
178 | if (r < 0) { | ||
179 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
180 | goto err; | ||
181 | } | ||
182 | |||
183 | info = (struct mailmbox_msg_info *) data.data; | ||
184 | |||
185 | snprintf(static_uid, PATH_MAX, "%u-%lu", | ||
186 | msg_info->msg_index, (unsigned long) info->msg_body_len); | ||
187 | uid = strdup(static_uid); | ||
188 | if (uid == NULL) { | ||
189 | res = MAIL_ERROR_MEMORY; | ||
190 | goto err; | ||
191 | } | ||
192 | |||
193 | r = mailmessage_generic_initialize(msg_info); | ||
194 | if (r != MAIL_NO_ERROR) { | ||
195 | free(uid); | ||
196 | res = r; | ||
197 | goto err; | ||
198 | } | ||
199 | |||
200 | msg = msg_info->msg_data; | ||
201 | |||
202 | msg->msg_prefetch = mbox_prefetch; | ||
203 | msg->msg_prefetch_free = mbox_prefetch_free; | ||
204 | msg_info->msg_uid = uid; | ||
205 | |||
206 | return MAIL_NO_ERROR; | ||
207 | |||
208 | err: | ||
209 | return res; | ||
210 | } | ||
211 | |||
212 | static void mbox_uninitialize(mailmessage * msg_info) | ||
213 | { | ||
214 | mailmessage_generic_uninitialize(msg_info); | ||
215 | } | ||
216 | |||
217 | #define FLAGS_NAME "flags.db" | ||
218 | |||
219 | static void mbox_flush(mailmessage * msg_info) | ||
220 | { | ||
221 | mailmessage_generic_flush(msg_info); | ||
222 | } | ||
223 | |||
224 | static void mbox_check(mailmessage * msg_info) | ||
225 | { | ||
226 | int r; | ||
227 | |||
228 | if (msg_info->msg_flags != NULL) { | ||
229 | r = mail_flags_store_set(get_cached_session_data(msg_info)->mbox_flags_store, | ||
230 | msg_info); | ||
231 | /* ignore errors */ | ||
232 | } | ||
233 | } | ||
234 | |||
235 | |||
236 | static int mbox_fetch_size(mailmessage * msg_info, | ||
237 | size_t * result) | ||
238 | { | ||
239 | int r; | ||
240 | size_t size; | ||
241 | |||
242 | r = mboxdriver_fetch_size(get_ancestor_session(msg_info), | ||
243 | msg_info->msg_index, &size); | ||
244 | if (r != MAIL_NO_ERROR) | ||
245 | return r; | ||
246 | |||
247 | * result = size; | ||
248 | |||
249 | return MAIL_NO_ERROR; | ||
250 | } | ||
251 | |||
252 | static int mbox_get_flags(mailmessage * msg_info, | ||
253 | struct mail_flags ** result) | ||
254 | { | ||
255 | int r; | ||
256 | struct mail_flags * flags; | ||
257 | struct mail_cache_db * cache_db_flags; | ||
258 | char filename_flags[PATH_MAX]; | ||
259 | int res; | ||
260 | struct mbox_cached_session_state_data * cached_data; | ||
261 | MMAPString * mmapstr; | ||
262 | struct mailmbox_folder * folder; | ||
263 | |||
264 | if (msg_info->msg_flags != NULL) { | ||
265 | * result = msg_info->msg_flags; | ||
266 | |||
267 | return MAIL_NO_ERROR; | ||
268 | } | ||
269 | |||
270 | flags = mail_flags_store_get(get_cached_session_data(msg_info)->mbox_flags_store, | ||
271 | msg_info->msg_index); | ||
272 | |||
273 | if (flags == NULL) { | ||
274 | folder = get_mbox_session(msg_info); | ||
275 | if (folder == NULL) { | ||
276 | res = MAIL_ERROR_BAD_STATE; | ||
277 | goto err; | ||
278 | } | ||
279 | |||
280 | cached_data = get_cached_session_data(msg_info); | ||
281 | if (cached_data->mbox_quoted_mb == NULL) { | ||
282 | res = MAIL_ERROR_BAD_STATE; | ||
283 | goto err; | ||
284 | } | ||
285 | |||
286 | snprintf(filename_flags, PATH_MAX, "%s/%s/%s", | ||
287 | cached_data->mbox_flags_directory, | ||
288 | cached_data->mbox_quoted_mb, FLAGS_NAME); | ||
289 | |||
290 | r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); | ||
291 | if (r < 0) { | ||
292 | res = MAIL_ERROR_MEMORY; | ||
293 | goto err; | ||
294 | } | ||
295 | |||
296 | mmapstr = mmap_string_new(""); | ||
297 | if (mmapstr == NULL) { | ||
298 | res = MAIL_ERROR_MEMORY; | ||
299 | goto close_db_flags; | ||
300 | } | ||
301 | |||
302 | if (msg_info->msg_index > folder->mb_written_uid) { | ||
303 | flags = mail_flags_new_empty(); | ||
304 | } | ||
305 | else { | ||
306 | r = mboxdriver_get_cached_flags(cache_db_flags, mmapstr, | ||
307 | msg_info->msg_session, | ||
308 | msg_info->msg_index, &flags); | ||
309 | if (r != MAIL_NO_ERROR) { | ||
310 | flags = mail_flags_new_empty(); | ||
311 | if (flags == NULL) { | ||
312 | res = MAIL_ERROR_MEMORY; | ||
313 | goto free_mmapstr; | ||
314 | } | ||
315 | } | ||
316 | } | ||
317 | |||
318 | mmap_string_free(mmapstr); | ||
319 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
320 | } | ||
321 | |||
322 | msg_info->msg_flags = flags; | ||
323 | |||
324 | * result = flags; | ||
325 | |||
326 | return MAIL_NO_ERROR; | ||
327 | |||
328 | free_mmapstr: | ||
329 | mmap_string_free(mmapstr); | ||
330 | close_db_flags: | ||
331 | mail_cache_db_close_unlock(filename_flags, cache_db_flags); | ||
332 | err: | ||
333 | return res; | ||
334 | } | ||
335 | |||
336 | static int mbox_fetch_header(mailmessage * msg_info, | ||
337 | char ** result, | ||
338 | size_t * result_len) | ||
339 | { | ||
340 | struct generic_message_t * msg; | ||
341 | int r; | ||
342 | char * msg_content; | ||
343 | size_t msg_length; | ||
344 | |||
345 | msg = msg_info->msg_data; | ||
346 | if (msg->msg_message != NULL) { | ||
347 | return mailmessage_generic_fetch_header(msg_info, result, result_len); | ||
348 | } | ||
349 | else { | ||
350 | r = mboxdriver_fetch_header(get_ancestor_session(msg_info), | ||
351 | msg_info->msg_index, | ||
352 | &msg_content, &msg_length); | ||
353 | if (r != MAIL_NO_ERROR) | ||
354 | return r; | ||
355 | |||
356 | * result = msg_content; | ||
357 | * result_len = msg_length; | ||
358 | |||
359 | return MAIL_NO_ERROR; | ||
360 | } | ||
361 | } | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_cached_message.h b/libetpan/src/driver/implementation/mbox/mboxdriver_cached_message.h new file mode 100644 index 0000000..144e2da --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_cached_message.h | |||
@@ -0,0 +1,52 @@ | |||
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 | #ifndef MBOXDRIVER_CACHED_MESSAGE_H | ||
37 | |||
38 | #define MBOXDRIVER_CACHED_MESSAGE_H | ||
39 | |||
40 | #include <libetpan/mailmessage.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | extern mailmessage_driver * mbox_cached_message_driver; | ||
47 | |||
48 | #ifdef __cplusplus | ||
49 | } | ||
50 | #endif | ||
51 | |||
52 | #endif | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_message.c b/libetpan/src/driver/implementation/mbox/mboxdriver_message.c new file mode 100644 index 0000000..9bb5a18 --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_message.c | |||
@@ -0,0 +1,225 @@ | |||
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 "mboxdriver_message.h" | ||
37 | |||
38 | #include "mailmessage_tools.h" | ||
39 | #include "mboxdriver_tools.h" | ||
40 | #include "mboxdriver.h" | ||
41 | #include "mailmbox.h" | ||
42 | |||
43 | #include <unistd.h> | ||
44 | #include <sys/mman.h> | ||
45 | #include <sys/types.h> | ||
46 | #include <sys/stat.h> | ||
47 | #include <fcntl.h> | ||
48 | #include <string.h> | ||
49 | #include <stdlib.h> | ||
50 | |||
51 | static int mbox_prefetch(mailmessage * msg_info); | ||
52 | |||
53 | static void mbox_prefetch_free(struct generic_message_t * msg); | ||
54 | |||
55 | static int mbox_initialize(mailmessage * msg_info); | ||
56 | |||
57 | static int mbox_fetch_size(mailmessage * msg_info, | ||
58 | size_t * result); | ||
59 | |||
60 | static int mbox_fetch_header(mailmessage * msg_info, | ||
61 | char ** result, | ||
62 | size_t * result_len); | ||
63 | |||
64 | static mailmessage_driver local_mbox_message_driver = { | ||
65 | .msg_name = "mbox", | ||
66 | |||
67 | .msg_initialize = mbox_initialize, | ||
68 | .msg_uninitialize = mailmessage_generic_uninitialize, | ||
69 | |||
70 | .msg_flush = mailmessage_generic_flush, | ||
71 | .msg_check = NULL, | ||
72 | |||
73 | .msg_fetch_result_free = mailmessage_generic_fetch_result_free, | ||
74 | |||
75 | .msg_fetch = mailmessage_generic_fetch, | ||
76 | .msg_fetch_header = mbox_fetch_header, | ||
77 | .msg_fetch_body = mailmessage_generic_fetch_body, | ||
78 | .msg_fetch_size = mbox_fetch_size, | ||
79 | .msg_get_bodystructure = mailmessage_generic_get_bodystructure, | ||
80 | .msg_fetch_section = mailmessage_generic_fetch_section, | ||
81 | .msg_fetch_section_header = mailmessage_generic_fetch_section_header, | ||
82 | .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime, | ||
83 | .msg_fetch_section_body = mailmessage_generic_fetch_section_body, | ||
84 | .msg_fetch_envelope = mailmessage_generic_fetch_envelope, | ||
85 | |||
86 | .msg_get_flags = NULL, | ||
87 | }; | ||
88 | |||
89 | mailmessage_driver * mbox_message_driver = &local_mbox_message_driver; | ||
90 | |||
91 | static inline struct mbox_session_state_data * get_data(mailmessage * msg) | ||
92 | { | ||
93 | return msg->msg_session->sess_data; | ||
94 | } | ||
95 | |||
96 | static inline struct mailmbox_folder * get_mbox_session(mailmessage * msg) | ||
97 | { | ||
98 | return get_data(msg)->mbox_folder; | ||
99 | } | ||
100 | |||
101 | |||
102 | static int mbox_prefetch(mailmessage * msg_info) | ||
103 | { | ||
104 | struct generic_message_t * msg; | ||
105 | int r; | ||
106 | char * msg_content; | ||
107 | size_t msg_length; | ||
108 | |||
109 | r = mboxdriver_fetch_msg(msg_info->msg_session, msg_info->msg_index, | ||
110 | &msg_content, &msg_length); | ||
111 | if (r != MAIL_NO_ERROR) | ||
112 | return r; | ||
113 | |||
114 | msg = msg_info->msg_data; | ||
115 | |||
116 | msg->msg_message = msg_content; | ||
117 | msg->msg_length = msg_length; | ||
118 | |||
119 | return MAIL_NO_ERROR; | ||
120 | } | ||
121 | |||
122 | static void mbox_prefetch_free(struct generic_message_t * msg) | ||
123 | { | ||
124 | if (msg->msg_message != NULL) { | ||
125 | mmap_string_unref(msg->msg_message); | ||
126 | msg->msg_message = NULL; | ||
127 | } | ||
128 | } | ||
129 | |||
130 | static int mbox_initialize(mailmessage * msg_info) | ||
131 | { | ||
132 | struct generic_message_t * msg; | ||
133 | int r; | ||
134 | char * uid; | ||
135 | char static_uid[PATH_MAX]; | ||
136 | struct mailmbox_msg_info * info; | ||
137 | struct mailmbox_folder * folder; | ||
138 | int res; | ||
139 | chashdatum key; | ||
140 | chashdatum data; | ||
141 | |||
142 | folder = get_mbox_session(msg_info); | ||
143 | if (folder == NULL) { | ||
144 | res = MAIL_ERROR_BAD_STATE; | ||
145 | goto err; | ||
146 | } | ||
147 | |||
148 | key.data = &msg_info->msg_index; | ||
149 | key.len = sizeof(msg_info->msg_index); | ||
150 | |||
151 | r = chash_get(folder->mb_hash, &key, &data); | ||
152 | if (r < 0) { | ||
153 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
154 | goto err; | ||
155 | } | ||
156 | |||
157 | info = data.data; | ||
158 | |||
159 | snprintf(static_uid, PATH_MAX, "%u-%lu", | ||
160 | msg_info->msg_index, (unsigned long) info->msg_body_len); | ||
161 | uid = strdup(static_uid); | ||
162 | if (uid == NULL) { | ||
163 | res = MAIL_ERROR_MEMORY; | ||
164 | goto err; | ||
165 | } | ||
166 | |||
167 | r = mailmessage_generic_initialize(msg_info); | ||
168 | if (r != MAIL_NO_ERROR) { | ||
169 | free(uid); | ||
170 | res = r; | ||
171 | goto err; | ||
172 | } | ||
173 | |||
174 | msg = msg_info->msg_data; | ||
175 | msg->msg_prefetch = mbox_prefetch; | ||
176 | msg->msg_prefetch_free = mbox_prefetch_free; | ||
177 | msg_info->msg_uid = uid; | ||
178 | |||
179 | return MAIL_NO_ERROR; | ||
180 | |||
181 | err: | ||
182 | return res; | ||
183 | } | ||
184 | |||
185 | static int mbox_fetch_size(mailmessage * msg_info, | ||
186 | size_t * result) | ||
187 | { | ||
188 | int r; | ||
189 | size_t size; | ||
190 | |||
191 | r = mboxdriver_fetch_size(msg_info->msg_session, | ||
192 | msg_info->msg_index, &size); | ||
193 | if (r != MAIL_NO_ERROR) | ||
194 | return r; | ||
195 | |||
196 | * result = size; | ||
197 | |||
198 | return MAIL_NO_ERROR; | ||
199 | } | ||
200 | |||
201 | static int mbox_fetch_header(mailmessage * msg_info, | ||
202 | char ** result, | ||
203 | size_t * result_len) | ||
204 | { | ||
205 | struct generic_message_t * msg; | ||
206 | int r; | ||
207 | char * msg_content; | ||
208 | size_t msg_length; | ||
209 | |||
210 | msg = msg_info->msg_data; | ||
211 | if (msg->msg_message != NULL) { | ||
212 | return mailmessage_generic_fetch_header(msg_info, result, result_len); | ||
213 | } | ||
214 | else { | ||
215 | r = mboxdriver_fetch_header(msg_info->msg_session, msg_info->msg_index, | ||
216 | &msg_content, &msg_length); | ||
217 | if (r != MAIL_NO_ERROR) | ||
218 | return r; | ||
219 | |||
220 | * result = msg_content; | ||
221 | * result_len = msg_length; | ||
222 | |||
223 | return MAIL_NO_ERROR; | ||
224 | } | ||
225 | } | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_message.h b/libetpan/src/driver/implementation/mbox/mboxdriver_message.h new file mode 100644 index 0000000..686e46e --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_message.h | |||
@@ -0,0 +1,52 @@ | |||
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 | #ifndef MBOXDRIVER_MESSAGE_H | ||
37 | |||
38 | #define MBOXDRIVER_MESSAGE_H | ||
39 | |||
40 | #include <libetpan/mboxdriver_types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | extern mailmessage_driver * mbox_message_driver; | ||
47 | |||
48 | #ifdef __cplusplus | ||
49 | } | ||
50 | #endif | ||
51 | |||
52 | #endif | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_tools.c b/libetpan/src/driver/implementation/mbox/mboxdriver_tools.c new file mode 100644 index 0000000..dc38cbd --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_tools.c | |||
@@ -0,0 +1,435 @@ | |||
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 "mboxdriver_tools.h" | ||
37 | |||
38 | #include <sys/types.h> | ||
39 | #include <sys/stat.h> | ||
40 | #include <fcntl.h> | ||
41 | #include <unistd.h> | ||
42 | |||
43 | #include "maildriver_types.h" | ||
44 | #include "mailmbox.h" | ||
45 | #include "mboxdriver_cached.h" | ||
46 | #include "mboxdriver.h" | ||
47 | #include "generic_cache.h" | ||
48 | #include "mailmessage.h" | ||
49 | #include "imfcache.h" | ||
50 | #include "mail_cache_db.h" | ||
51 | |||
52 | static inline struct mbox_session_state_data * | ||
53 | session_get_data(mailsession * session) | ||
54 | { | ||
55 | return session->sess_data; | ||
56 | } | ||
57 | |||
58 | static inline struct mailmbox_folder * | ||
59 | session_get_mbox_session(mailsession * session) | ||
60 | { | ||
61 | return session_get_data(session)->mbox_folder; | ||
62 | } | ||
63 | |||
64 | static inline struct mbox_cached_session_state_data * | ||
65 | cached_session_get_data(mailsession * session) | ||
66 | { | ||
67 | return session->sess_data; | ||
68 | } | ||
69 | |||
70 | static inline mailsession * | ||
71 | cached_session_get_ancestor(mailsession * session) | ||
72 | { | ||
73 | return cached_session_get_data(session)->mbox_ancestor; | ||
74 | } | ||
75 | |||
76 | static inline struct mbox_session_state_data * | ||
77 | cached_session_get_ancestor_data(mailsession * session) | ||
78 | { | ||
79 | return cached_session_get_ancestor(session)->sess_data; | ||
80 | } | ||
81 | |||
82 | static inline struct mailmbox_folder * | ||
83 | cached_session_get_mbox_session(mailsession * session) | ||
84 | { | ||
85 | return session_get_mbox_session(cached_session_get_ancestor(session)); | ||
86 | } | ||
87 | |||
88 | |||
89 | int mboxdriver_mbox_error_to_mail_error(int error) | ||
90 | { | ||
91 | switch (error) { | ||
92 | case MAILMBOX_NO_ERROR: | ||
93 | return MAIL_NO_ERROR; | ||
94 | |||
95 | case MAILMBOX_ERROR_PARSE: | ||
96 | return MAIL_ERROR_PARSE; | ||
97 | |||
98 | case MAILMBOX_ERROR_INVAL: | ||
99 | return MAIL_ERROR_INVAL; | ||
100 | |||
101 | case MAILMBOX_ERROR_FILE_NOT_FOUND: | ||
102 | return MAIL_ERROR_PARSE; | ||
103 | |||
104 | case MAILMBOX_ERROR_MEMORY: | ||
105 | return MAIL_ERROR_MEMORY; | ||
106 | |||
107 | case MAILMBOX_ERROR_TEMPORARY_FILE: | ||
108 | return MAIL_ERROR_PARSE; | ||
109 | |||
110 | case MAILMBOX_ERROR_FILE: | ||
111 | return MAIL_ERROR_FILE; | ||
112 | |||
113 | case MAILMBOX_ERROR_MSG_NOT_FOUND: | ||
114 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
115 | |||
116 | case MAILMBOX_ERROR_READONLY: | ||
117 | return MAIL_ERROR_READONLY; | ||
118 | |||
119 | default: | ||
120 | return MAIL_ERROR_INVAL; | ||
121 | } | ||
122 | } | ||
123 | |||
124 | int mboxdriver_fetch_msg(mailsession * session, uint32_t index, | ||
125 | char ** result, size_t * result_len) | ||
126 | { | ||
127 | int r; | ||
128 | char * msg_content; | ||
129 | size_t msg_length; | ||
130 | struct mailmbox_folder * folder; | ||
131 | |||
132 | folder = session_get_mbox_session(session); | ||
133 | if (folder == NULL) | ||
134 | return MAIL_ERROR_BAD_STATE; | ||
135 | |||
136 | r = mailmbox_fetch_msg(folder, index, &msg_content, &msg_length); | ||
137 | if (r != MAILMBOX_NO_ERROR) | ||
138 | return mboxdriver_mbox_error_to_mail_error(r); | ||
139 | |||
140 | * result = msg_content; | ||
141 | * result_len = msg_length; | ||
142 | |||
143 | return MAIL_NO_ERROR; | ||
144 | } | ||
145 | |||
146 | |||
147 | int mboxdriver_fetch_size(mailsession * session, uint32_t index, | ||
148 | size_t * result) | ||
149 | { | ||
150 | struct mailmbox_folder * folder; | ||
151 | int r; | ||
152 | char * data; | ||
153 | size_t len; | ||
154 | int res; | ||
155 | |||
156 | folder = session_get_mbox_session(session); | ||
157 | if (folder == NULL) { | ||
158 | res = MAIL_ERROR_FETCH; | ||
159 | goto err; | ||
160 | } | ||
161 | |||
162 | r = mailmbox_validate_read_lock(folder); | ||
163 | if (r != MAILMBOX_NO_ERROR) { | ||
164 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
165 | goto err; | ||
166 | } | ||
167 | |||
168 | r = mailmbox_fetch_msg_no_lock(folder, index, &data, &len); | ||
169 | if (r != MAILMBOX_NO_ERROR) { | ||
170 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
171 | goto unlock; | ||
172 | } | ||
173 | |||
174 | mailmbox_read_unlock(folder); | ||
175 | |||
176 | * result = len; | ||
177 | |||
178 | return MAIL_NO_ERROR; | ||
179 | |||
180 | unlock: | ||
181 | mailmbox_read_unlock(folder); | ||
182 | err: | ||
183 | return res; | ||
184 | } | ||
185 | |||
186 | |||
187 | int | ||
188 | mboxdriver_get_cached_flags(struct mail_cache_db * cache_db, | ||
189 | MMAPString * mmapstr, | ||
190 | mailsession * session, | ||
191 | uint32_t num, | ||
192 | struct mail_flags ** result) | ||
193 | { | ||
194 | int r; | ||
195 | char keyname[PATH_MAX]; | ||
196 | struct mail_flags * flags; | ||
197 | int res; | ||
198 | struct mailmbox_msg_info * info; | ||
199 | struct mailmbox_folder * folder; | ||
200 | chashdatum key; | ||
201 | chashdatum data; | ||
202 | |||
203 | folder = cached_session_get_mbox_session(session); | ||
204 | if (folder == NULL) { | ||
205 | res = MAIL_ERROR_BAD_STATE; | ||
206 | goto err; | ||
207 | } | ||
208 | |||
209 | key.data = # | ||
210 | key.len = sizeof(num); | ||
211 | |||
212 | r = chash_get(folder->mb_hash, &key, &data); | ||
213 | if (r < 0) { | ||
214 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
215 | goto err; | ||
216 | } | ||
217 | |||
218 | info = data.data; | ||
219 | |||
220 | snprintf(keyname, PATH_MAX, "%u-%lu-flags", num, | ||
221 | (unsigned long) info->msg_body_len); | ||
222 | |||
223 | r = generic_cache_flags_read(cache_db, mmapstr, keyname, &flags); | ||
224 | if (r != MAIL_NO_ERROR) { | ||
225 | res = r; | ||
226 | goto err; | ||
227 | } | ||
228 | |||
229 | * result = flags; | ||
230 | |||
231 | return MAIL_NO_ERROR; | ||
232 | |||
233 | err: | ||
234 | return res; | ||
235 | } | ||
236 | |||
237 | int | ||
238 | mboxdriver_write_cached_flags(struct mail_cache_db * cache_db, | ||
239 | MMAPString * mmapstr, | ||
240 | char * uid, | ||
241 | struct mail_flags * flags) | ||
242 | { | ||
243 | int r; | ||
244 | char keyname[PATH_MAX]; | ||
245 | int res; | ||
246 | |||
247 | snprintf(keyname, PATH_MAX, "%s-flags", uid); | ||
248 | |||
249 | r = generic_cache_flags_write(cache_db, mmapstr, keyname, flags); | ||
250 | if (r != MAIL_NO_ERROR) { | ||
251 | res = r; | ||
252 | goto err; | ||
253 | } | ||
254 | |||
255 | return MAIL_NO_ERROR; | ||
256 | |||
257 | err: | ||
258 | return res; | ||
259 | } | ||
260 | |||
261 | |||
262 | int mboxdriver_fetch_header(mailsession * session, uint32_t index, | ||
263 | char ** result, size_t * result_len) | ||
264 | { | ||
265 | int r; | ||
266 | char * msg_content; | ||
267 | size_t msg_length; | ||
268 | struct mailmbox_folder * folder; | ||
269 | |||
270 | folder = session_get_mbox_session(session); | ||
271 | if (folder == NULL) | ||
272 | return MAIL_ERROR_BAD_STATE; | ||
273 | |||
274 | r = mailmbox_fetch_msg_headers(folder, index, &msg_content, &msg_length); | ||
275 | if (r != MAILMBOX_NO_ERROR) | ||
276 | return mboxdriver_mbox_error_to_mail_error(r); | ||
277 | |||
278 | * result = msg_content; | ||
279 | * result_len = msg_length; | ||
280 | |||
281 | return MAIL_NO_ERROR; | ||
282 | } | ||
283 | |||
284 | int mbox_get_locked_messages_list(struct mailmbox_folder * folder, | ||
285 | mailsession * session, | ||
286 | mailmessage_driver * driver, | ||
287 | int (* lock)(struct mailmbox_folder *), | ||
288 | int (* unlock)(struct mailmbox_folder *), | ||
289 | struct mailmessage_list ** result) | ||
290 | { | ||
291 | struct mailmessage_list * env_list; | ||
292 | unsigned int i; | ||
293 | int r; | ||
294 | int res; | ||
295 | carray * tab; | ||
296 | |||
297 | tab = carray_new(128); | ||
298 | if (tab == NULL) { | ||
299 | res = MAIL_ERROR_MEMORY; | ||
300 | goto err; | ||
301 | } | ||
302 | |||
303 | r = lock(folder); | ||
304 | if (r != MAIL_NO_ERROR) { | ||
305 | res = r; | ||
306 | goto free; | ||
307 | } | ||
308 | |||
309 | for(i = 0 ; i < carray_count(folder->mb_tab) ; i ++) { | ||
310 | struct mailmbox_msg_info * msg_info; | ||
311 | mailmessage * msg; | ||
312 | |||
313 | msg_info = carray_get(folder->mb_tab, i); | ||
314 | if (msg_info == NULL) | ||
315 | continue; | ||
316 | |||
317 | if (msg_info->msg_deleted) | ||
318 | continue; | ||
319 | |||
320 | msg = mailmessage_new(); | ||
321 | if (msg == NULL) { | ||
322 | res = MAIL_ERROR_MEMORY; | ||
323 | goto unlock; | ||
324 | } | ||
325 | |||
326 | r = mailmessage_init(msg, session, driver, msg_info->msg_uid, | ||
327 | msg_info->msg_size - msg_info->msg_start_len); | ||
328 | if (r != MAIL_NO_ERROR) { | ||
329 | res = r; | ||
330 | goto unlock; | ||
331 | } | ||
332 | |||
333 | r = carray_add(tab, msg, NULL); | ||
334 | if (r < 0) { | ||
335 | mailmessage_free(msg); | ||
336 | res = MAIL_ERROR_MEMORY; | ||
337 | goto unlock; | ||
338 | } | ||
339 | } | ||
340 | |||
341 | env_list = mailmessage_list_new(tab); | ||
342 | if (env_list == NULL) { | ||
343 | res = MAIL_ERROR_MEMORY; | ||
344 | goto unlock; | ||
345 | } | ||
346 | |||
347 | unlock(folder); | ||
348 | |||
349 | * result = env_list; | ||
350 | |||
351 | return MAIL_NO_ERROR; | ||
352 | |||
353 | unlock: | ||
354 | unlock(folder); | ||
355 | free: | ||
356 | for(i = 0 ; i < carray_count(tab) ; i ++) | ||
357 | mailmessage_free(carray_get(tab, i)); | ||
358 | carray_free(tab); | ||
359 | err: | ||
360 | return res; | ||
361 | } | ||
362 | |||
363 | static int release_read_mbox(struct mailmbox_folder * folder) | ||
364 | { | ||
365 | int r; | ||
366 | |||
367 | r = mailmbox_read_unlock(folder); | ||
368 | return mboxdriver_mbox_error_to_mail_error(r); | ||
369 | } | ||
370 | |||
371 | static int acquire_read_mbox(struct mailmbox_folder * folder) | ||
372 | { | ||
373 | int r; | ||
374 | |||
375 | r = mailmbox_validate_read_lock(folder); | ||
376 | return mboxdriver_mbox_error_to_mail_error(r); | ||
377 | } | ||
378 | |||
379 | static int release_write_mbox(struct mailmbox_folder * folder) | ||
380 | { | ||
381 | int r; | ||
382 | |||
383 | r = mailmbox_write_unlock(folder); | ||
384 | return mboxdriver_mbox_error_to_mail_error(r); | ||
385 | } | ||
386 | |||
387 | static int acquire_write_mbox(struct mailmbox_folder * folder) | ||
388 | { | ||
389 | int r; | ||
390 | int res; | ||
391 | |||
392 | r = mailmbox_validate_write_lock(folder); | ||
393 | if (r != MAILMBOX_NO_ERROR) { | ||
394 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
395 | goto err; | ||
396 | } | ||
397 | |||
398 | if (folder->mb_written_uid < folder->mb_max_uid) { | ||
399 | r = mailmbox_expunge_no_lock(folder); | ||
400 | if (r != MAILMBOX_NO_ERROR) { | ||
401 | res = mboxdriver_mbox_error_to_mail_error(r); | ||
402 | goto unlock; | ||
403 | } | ||
404 | } | ||
405 | |||
406 | return MAIL_NO_ERROR; | ||
407 | |||
408 | unlock: | ||
409 | mailmbox_write_unlock(folder); | ||
410 | err: | ||
411 | return res; | ||
412 | } | ||
413 | |||
414 | /* get message list with all valid written UID */ | ||
415 | |||
416 | int mbox_get_uid_messages_list(struct mailmbox_folder * folder, | ||
417 | mailsession * session, | ||
418 | mailmessage_driver * driver, | ||
419 | struct mailmessage_list ** result) | ||
420 | { | ||
421 | return mbox_get_locked_messages_list(folder, session, driver, | ||
422 | acquire_write_mbox, release_write_mbox, result); | ||
423 | } | ||
424 | |||
425 | |||
426 | /* get message list */ | ||
427 | |||
428 | int mbox_get_messages_list(struct mailmbox_folder * folder, | ||
429 | mailsession * session, | ||
430 | mailmessage_driver * driver, | ||
431 | struct mailmessage_list ** result) | ||
432 | { | ||
433 | return mbox_get_locked_messages_list(folder, session, driver, | ||
434 | acquire_read_mbox, release_read_mbox, result); | ||
435 | } | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_tools.h b/libetpan/src/driver/implementation/mbox/mboxdriver_tools.h new file mode 100644 index 0000000..eebf98c --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_tools.h | |||
@@ -0,0 +1,85 @@ | |||
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 | #ifndef MBOXDRIVER_TOOLS_H | ||
37 | |||
38 | #define MBOXDRIVER_TOOLS_H | ||
39 | |||
40 | #include "mail_cache_db_types.h" | ||
41 | #include "mboxdriver_types.h" | ||
42 | #include "mailmbox.h" | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | extern "C" { | ||
46 | #endif | ||
47 | |||
48 | int mboxdriver_mbox_error_to_mail_error(int error); | ||
49 | |||
50 | int mboxdriver_fetch_msg(mailsession * session, uint32_t index, | ||
51 | char ** result, size_t * result_len); | ||
52 | |||
53 | int mboxdriver_fetch_size(mailsession * session, uint32_t index, | ||
54 | size_t * result); | ||
55 | |||
56 | int | ||
57 | mboxdriver_get_cached_flags(struct mail_cache_db * cache_db, | ||
58 | MMAPString * mmapstr, | ||
59 | mailsession * session, | ||
60 | uint32_t num, | ||
61 | struct mail_flags ** result); | ||
62 | |||
63 | int | ||
64 | mboxdriver_write_cached_flags(struct mail_cache_db * cache_db, | ||
65 | MMAPString * mmapstr, | ||
66 | char * uid, struct mail_flags * flags); | ||
67 | |||
68 | int mbox_get_uid_messages_list(struct mailmbox_folder * folder, | ||
69 | mailsession * session, | ||
70 | mailmessage_driver * driver, | ||
71 | struct mailmessage_list ** result); | ||
72 | |||
73 | int mbox_get_messages_list(struct mailmbox_folder * folder, | ||
74 | mailsession * session, | ||
75 | mailmessage_driver * driver, | ||
76 | struct mailmessage_list ** result); | ||
77 | |||
78 | int mboxdriver_fetch_header(mailsession * session, uint32_t index, | ||
79 | char ** result, size_t * result_len); | ||
80 | |||
81 | #ifdef __cplusplus | ||
82 | } | ||
83 | #endif | ||
84 | |||
85 | #endif | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxdriver_types.h b/libetpan/src/driver/implementation/mbox/mboxdriver_types.h new file mode 100644 index 0000000..23b9acf --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxdriver_types.h | |||
@@ -0,0 +1,107 @@ | |||
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 | #ifndef MBOXDRIVER_TYPES_H | ||
37 | |||
38 | #define MBOXDRIVER_TYPES_H | ||
39 | |||
40 | #include <libetpan/maildriver_types.h> | ||
41 | #include <libetpan/mailmbox.h> | ||
42 | #include <libetpan/mailstorage_types.h> | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | extern "C" { | ||
46 | #endif | ||
47 | |||
48 | /* mbox driver */ | ||
49 | |||
50 | enum { | ||
51 | MBOXDRIVER_SET_READ_ONLY = 1, | ||
52 | MBOXDRIVER_SET_NO_UID, | ||
53 | }; | ||
54 | |||
55 | struct mbox_session_state_data { | ||
56 | struct mailmbox_folder * mbox_folder; | ||
57 | int mbox_force_read_only; | ||
58 | int mbox_force_no_uid; | ||
59 | }; | ||
60 | |||
61 | /* cached version */ | ||
62 | |||
63 | enum { | ||
64 | /* the mapping of the parameters should be the same as for mbox */ | ||
65 | MBOXDRIVER_CACHED_SET_READ_ONLY = 1, | ||
66 | MBOXDRIVER_CACHED_SET_NO_UID, | ||
67 | /* cache specific */ | ||
68 | MBOXDRIVER_CACHED_SET_CACHE_DIRECTORY, | ||
69 | MBOXDRIVER_CACHED_SET_FLAGS_DIRECTORY, | ||
70 | }; | ||
71 | |||
72 | struct mbox_cached_session_state_data { | ||
73 | mailsession * mbox_ancestor; | ||
74 | char * mbox_quoted_mb; | ||
75 | char mbox_cache_directory[PATH_MAX]; | ||
76 | char mbox_flags_directory[PATH_MAX]; | ||
77 | struct mail_flags_store * mbox_flags_store; | ||
78 | }; | ||
79 | |||
80 | /* mbox storage */ | ||
81 | |||
82 | /* | ||
83 | mbox_mailstorage is the state data specific to the mbox storage. | ||
84 | |||
85 | - pathname is the filename that contains the mailbox. | ||
86 | |||
87 | - cached if this value is != 0, a persistant cache will be | ||
88 | stored on local system. | ||
89 | |||
90 | - cache_directory is the location of the cache. | ||
91 | |||
92 | - flags_directory is the location of the flags. | ||
93 | */ | ||
94 | |||
95 | struct mbox_mailstorage { | ||
96 | char * mbox_pathname; | ||
97 | |||
98 | int mbox_cached; | ||
99 | char * mbox_cache_directory; | ||
100 | char * mbox_flags_directory; | ||
101 | }; | ||
102 | |||
103 | #ifdef __cplusplus | ||
104 | } | ||
105 | #endif | ||
106 | |||
107 | #endif | ||
diff --git a/libetpan/src/driver/implementation/mbox/mboxstorage.c b/libetpan/src/driver/implementation/mbox/mboxstorage.c new file mode 100644 index 0000000..3944c3b --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxstorage.c | |||
@@ -0,0 +1,192 @@ | |||
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 "mboxstorage.h" | ||
37 | |||
38 | #include "mail.h" | ||
39 | #include "mailmessage.h" | ||
40 | #include "mboxdriver.h" | ||
41 | #include "mboxdriver_cached.h" | ||
42 | #include "maildriver.h" | ||
43 | |||
44 | #include <stdlib.h> | ||
45 | #include <string.h> | ||
46 | |||
47 | /* mbox storage */ | ||
48 | |||
49 | static int mbox_mailstorage_connect(struct mailstorage * storage); | ||
50 | static int | ||
51 | mbox_mailstorage_get_folder_session(struct mailstorage * storage, | ||
52 | char * pathname, mailsession ** result); | ||
53 | static void mbox_mailstorage_uninitialize(struct mailstorage * storage); | ||
54 | |||
55 | static mailstorage_driver mbox_mailstorage_driver = { | ||
56 | .sto_name = "mbox", | ||
57 | .sto_connect = mbox_mailstorage_connect, | ||
58 | .sto_get_folder_session = mbox_mailstorage_get_folder_session, | ||
59 | .sto_uninitialize = mbox_mailstorage_uninitialize, | ||
60 | }; | ||
61 | |||
62 | int mbox_mailstorage_init(struct mailstorage * storage, | ||
63 | char * mbox_pathname, int mbox_cached, | ||
64 | char * mbox_cache_directory, char * mbox_flags_directory) | ||
65 | { | ||
66 | struct mbox_mailstorage * mbox_storage; | ||
67 | |||
68 | mbox_storage = malloc(sizeof(* mbox_storage)); | ||
69 | if (mbox_storage == NULL) | ||
70 | goto err; | ||
71 | |||
72 | mbox_storage->mbox_pathname = strdup(mbox_pathname); | ||
73 | if (mbox_storage->mbox_pathname == NULL) | ||
74 | goto free; | ||
75 | |||
76 | mbox_storage->mbox_cached = mbox_cached; | ||
77 | |||
78 | if (mbox_cached && (mbox_cache_directory != NULL) && | ||
79 | (mbox_flags_directory != NULL)) { | ||
80 | mbox_storage->mbox_cache_directory = strdup(mbox_cache_directory); | ||
81 | if (mbox_storage->mbox_cache_directory == NULL) | ||
82 | goto free_pathname; | ||
83 | |||
84 | mbox_storage->mbox_flags_directory = strdup(mbox_flags_directory); | ||
85 | if (mbox_storage->mbox_flags_directory == NULL) | ||
86 | goto free_cache_directory; | ||
87 | } | ||
88 | else { | ||
89 | mbox_storage->mbox_cached = FALSE; | ||
90 | mbox_storage->mbox_cache_directory = NULL; | ||
91 | mbox_storage->mbox_flags_directory = NULL; | ||
92 | } | ||
93 | |||
94 | storage->sto_data = mbox_storage; | ||
95 | storage->sto_driver = &mbox_mailstorage_driver; | ||
96 | |||
97 | return MAIL_NO_ERROR; | ||
98 | |||
99 | free_cache_directory: | ||
100 | free(mbox_storage->mbox_cache_directory); | ||
101 | free_pathname: | ||
102 | free(mbox_storage->mbox_pathname); | ||
103 | free: | ||
104 | free(mbox_storage); | ||
105 | err: | ||
106 | return MAIL_ERROR_MEMORY; | ||
107 | } | ||
108 | |||
109 | static void mbox_mailstorage_uninitialize(struct mailstorage * storage) | ||
110 | { | ||
111 | struct mbox_mailstorage * mbox_storage; | ||
112 | |||
113 | mbox_storage = storage->sto_data; | ||
114 | if (mbox_storage->mbox_flags_directory != NULL) | ||
115 | free(mbox_storage->mbox_flags_directory); | ||
116 | if (mbox_storage->mbox_cache_directory != NULL) | ||
117 | free(mbox_storage->mbox_cache_directory); | ||
118 | free(mbox_storage->mbox_pathname); | ||
119 | free(mbox_storage); | ||
120 | |||
121 | storage->sto_data = NULL; | ||
122 | } | ||
123 | |||
124 | static int mbox_mailstorage_connect(struct mailstorage * storage) | ||
125 | { | ||
126 | struct mbox_mailstorage * mbox_storage; | ||
127 | mailsession_driver * driver; | ||
128 | int r; | ||
129 | int res; | ||
130 | mailsession * session; | ||
131 | |||
132 | mbox_storage = storage->sto_data; | ||
133 | |||
134 | if (mbox_storage->mbox_cached) | ||
135 | driver = mbox_cached_session_driver; | ||
136 | else | ||
137 | driver = mbox_session_driver; | ||
138 | |||
139 | session = mailsession_new(driver); | ||
140 | if (session == NULL) { | ||
141 | res = MAIL_ERROR_MEMORY; | ||
142 | goto err; | ||
143 | } | ||
144 | |||
145 | if (mbox_storage->mbox_cached) { | ||
146 | r = mailsession_parameters(session, | ||
147 | MBOXDRIVER_CACHED_SET_CACHE_DIRECTORY, | ||
148 | mbox_storage->mbox_cache_directory); | ||
149 | if (r != MAIL_NO_ERROR) { | ||
150 | res = r; | ||
151 | goto free; | ||
152 | } | ||
153 | |||
154 | r = mailsession_parameters(session, | ||
155 | MBOXDRIVER_CACHED_SET_FLAGS_DIRECTORY, | ||
156 | mbox_storage->mbox_flags_directory); | ||
157 | if (r != MAIL_NO_ERROR) { | ||
158 | res = r; | ||
159 | goto free; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | r = mailsession_connect_path(session, mbox_storage->mbox_pathname); | ||
164 | switch (r) { | ||
165 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
166 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
167 | case MAIL_NO_ERROR: | ||
168 | break; | ||
169 | default: | ||
170 | res = r; | ||
171 | goto free; | ||
172 | } | ||
173 | |||
174 | storage->sto_session = session; | ||
175 | |||
176 | return MAIL_NO_ERROR; | ||
177 | |||
178 | free: | ||
179 | mailsession_free(session); | ||
180 | err: | ||
181 | return res; | ||
182 | } | ||
183 | |||
184 | static int | ||
185 | mbox_mailstorage_get_folder_session(struct mailstorage * storage, | ||
186 | char * pathname, mailsession ** result) | ||
187 | { | ||
188 | * result = storage->sto_session; | ||
189 | |||
190 | return MAIL_NO_ERROR; | ||
191 | } | ||
192 | |||
diff --git a/libetpan/src/driver/implementation/mbox/mboxstorage.h b/libetpan/src/driver/implementation/mbox/mboxstorage.h new file mode 100644 index 0000000..45aed7b --- a/dev/null +++ b/libetpan/src/driver/implementation/mbox/mboxstorage.h | |||
@@ -0,0 +1,69 @@ | |||
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 | #ifndef MBOXSTORAGE_H | ||
37 | |||
38 | #define MBOXSTORAGE_H | ||
39 | |||
40 | #include <libetpan/mboxdriver_types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | /* | ||
47 | mbox_mailstorage_init is the constructor for a mbox storage. | ||
48 | |||
49 | @param storage this is the storage to initialize. | ||
50 | |||
51 | @param pathname is the filename that contains the mailbox. | ||
52 | |||
53 | @param cached if this value is != 0, a persistant cache will be | ||
54 | stored on local system. | ||
55 | |||
56 | @param cache_directory is the location of the cache | ||
57 | |||
58 | @param flags_directory is the location of the flags | ||
59 | */ | ||
60 | |||
61 | int mbox_mailstorage_init(struct mailstorage * storage, | ||
62 | char * mb_pathname, int mb_cached, | ||
63 | char * mb_cache_directory, char * mb_flags_directory); | ||
64 | |||
65 | #ifdef __cplusplus | ||
66 | } | ||
67 | #endif | ||
68 | |||
69 | #endif | ||