summaryrefslogtreecommitdiffabout
path: root/libetpan/src/driver/implementation/maildir/maildirdriver.c
Unidiff
Diffstat (limited to 'libetpan/src/driver/implementation/maildir/maildirdriver.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/driver/implementation/maildir/maildirdriver.c676
1 files changed, 676 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/maildir/maildirdriver.c b/libetpan/src/driver/implementation/maildir/maildirdriver.c
new file mode 100644
index 0000000..a97fd43
--- a/dev/null
+++ b/libetpan/src/driver/implementation/maildir/maildirdriver.c
@@ -0,0 +1,676 @@
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
37/*
38 flags directory MUST be kept so that we can have other flags
39 than standards
40*/
41
42#include "maildirdriver.h"
43
44#include <stdio.h>
45#include <sys/types.h>
46#include <dirent.h>
47#include <unistd.h>
48#include <sys/stat.h>
49#include <ctype.h>
50#include <fcntl.h>
51#include <sys/mman.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include "maildir.h"
56#include "maildriver_tools.h"
57#include "maildirdriver_message.h"
58#include "maildirdriver_tools.h"
59#include "mailmessage.h"
60#include "generic_cache.h"
61
62static int initialize(mailsession * session);
63
64static void uninitialize(mailsession * session);
65
66static int connect_path(mailsession * session, char * path);
67
68static int logout(mailsession * session);
69
70static int expunge_folder(mailsession * session);
71
72static int status_folder(mailsession * session, char * mb,
73 uint32_t * result_messages, uint32_t * result_recent,
74 uint32_t * result_unseen);
75
76static int recent_number(mailsession * session, char * mb,
77 uint32_t * result);
78
79static int unseen_number(mailsession * session, char * mb,
80 uint32_t * result);
81
82static int messages_number(mailsession * session, char * mb,
83 uint32_t * result);
84
85static int append_message(mailsession * session,
86 char * message, size_t size);
87
88static int append_message_flags(mailsession * session,
89 char * message, size_t size, struct mail_flags * flags);
90
91static int get_messages_list(mailsession * session,
92 struct mailmessage_list ** result);
93
94static int get_envelopes_list(mailsession * session,
95 struct mailmessage_list * env_list);
96
97static int check_folder(mailsession * session);
98
99static int get_message_by_uid(mailsession * session,
100 const char * uid, mailmessage ** result);
101
102static mailsession_driver local_maildir_session_driver = {
103 .sess_name = "maildir",
104
105 .sess_initialize = initialize,
106 .sess_uninitialize = uninitialize,
107
108 .sess_parameters = NULL,
109
110 .sess_connect_stream = NULL,
111 .sess_connect_path = connect_path,
112 .sess_starttls = NULL,
113 .sess_login = NULL,
114 .sess_logout = logout,
115 .sess_noop = NULL,
116
117 .sess_build_folder_name = NULL,
118 .sess_create_folder = NULL,
119 .sess_delete_folder = NULL,
120 .sess_rename_folder = NULL,
121 .sess_check_folder = check_folder,
122 .sess_examine_folder = NULL,
123 .sess_select_folder = NULL,
124 .sess_expunge_folder = expunge_folder,
125 .sess_status_folder = status_folder,
126 .sess_messages_number = messages_number,
127 .sess_recent_number = recent_number,
128 .sess_unseen_number = unseen_number,
129 .sess_list_folders = NULL,
130 .sess_lsub_folders = NULL,
131 .sess_subscribe_folder = NULL,
132 .sess_unsubscribe_folder = NULL,
133
134 .sess_append_message = append_message,
135 .sess_append_message_flags = append_message_flags,
136 .sess_copy_message = NULL,
137 .sess_move_message = NULL,
138
139 .sess_get_messages_list = get_messages_list,
140 .sess_get_envelopes_list = get_envelopes_list,
141 .sess_remove_message = NULL,
142#if 0
143 .sess_search_messages = maildriver_generic_search_messages,
144#endif
145
146 .sess_get_message = NULL,
147 .sess_get_message_by_uid = get_message_by_uid,
148};
149
150mailsession_driver * maildir_session_driver = &local_maildir_session_driver;
151
152
153static int flags_store_process(struct maildir * md,
154 struct mail_flags_store * flags_store);
155
156
157static inline struct maildir_session_state_data * get_data(mailsession * session)
158{
159 return session->sess_data;
160}
161
162static struct maildir * get_maildir_session(mailsession * session)
163{
164 return get_data(session)->md_session;
165}
166
167static int initialize(mailsession * session)
168{
169 struct maildir_session_state_data * data;
170
171 data = malloc(sizeof(* data));
172 if (data == NULL)
173 goto err;
174
175 data->md_session = NULL;
176
177 data->md_flags_store = mail_flags_store_new();
178 if (data->md_flags_store == NULL)
179 goto free;
180
181 session->sess_data = data;
182
183 return MAIL_NO_ERROR;
184
185 free:
186 free(data);
187 err:
188 return MAIL_ERROR_MEMORY;
189}
190
191static void uninitialize(mailsession * session)
192{
193 struct maildir_session_state_data * data;
194
195 data = get_data(session);
196
197 if (data->md_session != NULL)
198 flags_store_process(data->md_session, data->md_flags_store);
199
200 mail_flags_store_free(data->md_flags_store);
201 if (data->md_session != NULL)
202 maildir_free(data->md_session);
203
204 free(data);
205
206 session->sess_data = NULL;
207}
208
209
210static int connect_path(mailsession * session, char * path)
211{
212 struct maildir * md;
213 int res;
214 int r;
215
216 if (get_maildir_session(session) != NULL) {
217 res = MAIL_ERROR_BAD_STATE;
218 goto err;
219 }
220
221 md = maildir_new(path);
222 if (md == NULL) {
223 res = MAIL_ERROR_MEMORY;
224 goto err;
225 }
226
227 r = maildir_update(md);
228 if (r != MAILDIR_NO_ERROR) {
229 res = maildirdriver_maildir_error_to_mail_error(r);
230 goto free;
231 }
232
233 get_data(session)->md_session = md;
234
235 return MAIL_NO_ERROR;
236
237 free:
238 maildir_free(md);
239 err:
240 return res;
241}
242
243static int logout(mailsession * session)
244{
245 struct maildir * md;
246
247 check_folder(session);
248
249 md = get_maildir_session(session);
250 if (md == NULL)
251 return MAIL_ERROR_BAD_STATE;
252
253 maildir_free(md);
254 get_data(session)->md_session = NULL;
255
256 return MAIL_NO_ERROR;
257}
258
259/* folders operations */
260
261static int status_folder(mailsession * session, char * mb,
262 uint32_t * result_messages, uint32_t * result_recent,
263 uint32_t * result_unseen)
264{
265 int r;
266 struct maildir * md;
267 unsigned int i;
268 uint32_t messages;
269 uint32_t recent;
270 uint32_t unseen;
271
272 check_folder(session);
273
274 md = get_maildir_session(session);
275 if (md == NULL)
276 return MAIL_ERROR_BAD_STATE;
277
278 r = maildir_update(md);
279 if (r != MAILDIR_NO_ERROR)
280 return maildirdriver_maildir_error_to_mail_error(r);
281
282 messages = 0;
283 recent = 0;
284 unseen = 0;
285 for(i = 0 ; i < carray_count(md->mdir_msg_list) ; i ++) {
286 struct maildir_msg * msg;
287
288 msg = carray_get(md->mdir_msg_list, i);
289 if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0)
290 recent ++;
291 if ((msg->msg_flags & MAILDIR_FLAG_SEEN) == 0)
292 unseen ++;
293 messages ++;
294 }
295
296 * result_messages = messages;
297 * result_recent = recent;
298 * result_unseen = unseen;
299
300 return MAIL_NO_ERROR;
301}
302
303static int messages_number(mailsession * session, char * mb,
304 uint32_t * result)
305{
306 struct maildir * md;
307 int r;
308
309 md = get_maildir_session(session);
310 if (md == NULL)
311 return MAIL_ERROR_BAD_STATE;
312
313 r = maildir_update(md);
314 if (r != MAILDIR_NO_ERROR)
315 return maildirdriver_maildir_error_to_mail_error(r);
316
317 * result = carray_count(md->mdir_msg_list);
318
319 return MAIL_NO_ERROR;
320}
321
322static int unseen_number(mailsession * session, char * mb,
323 uint32_t * result)
324{
325 uint32_t messages;
326 uint32_t recent;
327 uint32_t unseen;
328 int r;
329
330 r = status_folder(session, mb, &messages, &recent, &unseen);
331 if (r != MAIL_NO_ERROR)
332 return r;
333
334 * result = unseen;
335
336 return MAIL_NO_ERROR;
337}
338
339static int recent_number(mailsession * session, char * mb,
340 uint32_t * result)
341{
342 uint32_t messages;
343 uint32_t recent;
344 uint32_t unseen;
345 int r;
346
347 r = status_folder(session, mb, &messages, &recent, &unseen);
348 if (r != MAIL_NO_ERROR)
349 return r;
350
351 * result = recent;
352
353 return MAIL_NO_ERROR;
354}
355
356
357/* messages operations */
358
359static int append_message(mailsession * session,
360 char * message, size_t size)
361{
362#if 0
363 struct maildir * md;
364 int r;
365
366 md = get_maildir_session(session);
367 if (md == NULL)
368 return MAIL_ERROR_BAD_STATE;
369
370 r = maildir_message_add(md, message, size);
371 if (r != MAILDIR_NO_ERROR)
372 return maildirdriver_maildir_error_to_mail_error(r);
373
374 return MAIL_NO_ERROR;
375#endif
376
377 return append_message_flags(session, message, size, NULL);
378}
379
380static int append_message_flags(mailsession * session,
381 char * message, size_t size, struct mail_flags * flags)
382{
383 struct maildir * md;
384 int r;
385 char uid[PATH_MAX];
386 struct maildir_msg * md_msg;
387 chashdatum key;
388 chashdatum value;
389 uint32_t md_flags;
390
391 md = get_maildir_session(session);
392 if (md == NULL)
393 return MAIL_ERROR_BAD_STATE;
394
395 r = maildir_message_add_uid(md, message, size,
396 uid, sizeof(uid));
397 if (r != MAILDIR_NO_ERROR)
398 return maildirdriver_maildir_error_to_mail_error(r);
399
400 if (flags == NULL)
401 goto exit;
402
403 key.data = uid;
404 key.len = strlen(uid);
405 r = chash_get(md->mdir_msg_hash, &key, &value);
406 if (r < 0)
407 goto exit;
408
409 md_msg = value.data;
410
411 md_flags = maildirdriver_flags_to_maildir_flags(flags->fl_flags);
412
413 r = maildir_message_change_flags(md, uid, md_flags);
414 if (r != MAILDIR_NO_ERROR)
415 goto exit;
416
417 return MAIL_NO_ERROR;
418
419 exit:
420 return MAIL_NO_ERROR;
421}
422
423static int get_messages_list(mailsession * session,
424 struct mailmessage_list ** result)
425{
426 struct maildir * md;
427 int r;
428 struct mailmessage_list * env_list;
429 int res;
430
431 md = get_maildir_session(session);
432 if (md == NULL)
433 return MAIL_ERROR_BAD_STATE;
434
435 r = maildir_update(md);
436 if (r != MAILDIR_NO_ERROR) {
437 res = maildirdriver_maildir_error_to_mail_error(r);
438 goto err;
439 }
440
441 r = maildir_get_messages_list(session, md,
442 maildir_message_driver, &env_list);
443 if (r != MAILDIR_NO_ERROR) {
444 res = r;
445 goto free_list;
446 }
447
448 * result = env_list;
449
450 return MAIL_NO_ERROR;
451
452 free_list:
453 mailmessage_list_free(env_list);
454 err:
455 return res;
456}
457
458static int get_envelopes_list(mailsession * session,
459 struct mailmessage_list * env_list)
460{
461 int r;
462 struct maildir * md;
463 unsigned int i;
464 int res;
465
466 check_folder(session);
467
468 md = get_maildir_session(session);
469 if (md == NULL) {
470 res = MAIL_ERROR_BAD_STATE;
471 goto err;
472 }
473
474 r = maildir_update(md);
475 if (r != MAILDIR_NO_ERROR) {
476 res = maildirdriver_maildir_error_to_mail_error(r);
477 goto err;
478 }
479
480 r = maildriver_generic_get_envelopes_list(session, env_list);
481 if (r != MAIL_NO_ERROR) {
482 res = r;
483 goto err;
484 }
485
486 for(i = 0 ; i < carray_count(env_list->msg_tab) ; i++) {
487 struct maildir_msg * md_msg;
488 mailmessage * msg;
489 uint32_t driver_flags;
490 clist * ext;
491 chashdatum key;
492 chashdatum value;
493
494 msg = carray_get(env_list->msg_tab, i);
495
496 key.data = msg->msg_uid;
497 key.len = strlen(msg->msg_uid);
498 r = chash_get(md->mdir_msg_hash, &key, &value);
499 if (r < 0)
500 continue;
501
502 md_msg = value.data;
503
504 driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags);
505
506 if (msg->msg_flags == NULL) {
507 ext = clist_new();
508 if (ext == NULL) {
509 res = MAIL_ERROR_MEMORY;
510 continue;
511 }
512
513 msg->msg_flags = mail_flags_new(driver_flags, ext);
514 if (msg->msg_flags == NULL) {
515 clist_free(ext);
516 res = MAIL_ERROR_MEMORY;
517 continue;
518 }
519
520 if ((md_msg->msg_flags & MAILDIR_FLAG_NEW) != 0) {
521 mail_flags_store_set(get_data(session)->md_flags_store, msg);
522 }
523 }
524 else {
525 msg->msg_flags->fl_flags &= MAIL_FLAG_FORWARDED;
526 msg->msg_flags->fl_flags |= driver_flags;
527 }
528 }
529
530 return MAIL_NO_ERROR;
531
532 err:
533 return res;
534}
535
536
537static int expunge_folder(mailsession * session)
538{
539 unsigned int i;
540 int r;
541 int res;
542 struct maildir * md;
543
544 check_folder(session);
545
546 md = get_maildir_session(session);
547 if (md == NULL)
548 return MAIL_ERROR_BAD_STATE;
549
550 r = maildir_update(md);
551 if (r != MAILDIR_NO_ERROR) {
552 res = maildirdriver_maildir_error_to_mail_error(r);
553 goto err;
554 }
555
556 for(i = 0 ; i < carray_count(md->mdir_msg_list) ; i++) {
557 struct maildir_msg * md_msg;
558
559 md_msg = carray_get(md->mdir_msg_list, i);
560
561 if ((md_msg->msg_flags & MAILDIR_FLAG_TRASHED) != 0)
562 maildir_message_remove(md, md_msg->msg_uid);
563 }
564
565 return MAIL_NO_ERROR;
566
567 err:
568 return res;
569}
570
571
572static int flags_store_process(struct maildir * md,
573 struct mail_flags_store * flags_store)
574{
575 unsigned int i;
576
577 if (carray_count(flags_store->fls_tab) == 0)
578 return MAIL_NO_ERROR;
579
580 for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
581 mailmessage * msg;
582 uint32_t md_flags;
583
584 msg = carray_get(flags_store->fls_tab, i);
585 md_flags = maildirdriver_flags_to_maildir_flags(msg->msg_flags->fl_flags);
586 md_flags &= ~MAILDIR_FLAG_NEW;
587
588 maildir_message_change_flags(md, msg->msg_uid, md_flags);
589 }
590
591 mail_flags_store_clear(flags_store);
592
593 return MAIL_NO_ERROR;
594}
595
596
597
598static int check_folder(mailsession * session)
599{
600 struct mail_flags_store * flags_store;
601 struct maildir_session_state_data * data;
602 struct maildir * md;
603
604 md = get_maildir_session(session);
605 if (md == NULL)
606 return MAIL_ERROR_BAD_STATE;
607
608 data = get_data(session);
609 flags_store = data->md_flags_store;
610
611 return flags_store_process(md, flags_store);
612}
613
614static int get_message_by_uid(mailsession * session,
615 const char * uid, mailmessage ** result)
616{
617 int r;
618 struct maildir * md;
619 int res;
620 mailmessage * msg;
621 char * msg_filename;
622 struct stat stat_info;
623
624 md = get_maildir_session(session);
625
626 /* update maildir data */
627
628 r = maildir_update(md);
629 if (r != MAILDIR_NO_ERROR) {
630 res = maildirdriver_maildir_error_to_mail_error(r);
631 goto err;
632 }
633
634 msg_filename = maildir_message_get(md, uid);
635 if (msg_filename == NULL) {
636 res = MAIL_ERROR_INVAL;
637 goto err;
638 }
639
640 r = stat(msg_filename, &stat_info);
641 free(msg_filename);
642 if (r < 0) {
643 res = MAIL_ERROR_INVAL;
644 goto err;
645 }
646
647 /* create message */
648
649 msg = mailmessage_new();
650 if (msg == NULL) {
651 res = MAIL_ERROR_MEMORY;
652 goto err;
653 }
654
655 r = mailmessage_init(msg, session, maildir_message_driver,
656 0, stat_info.st_size);
657 if (r != MAIL_NO_ERROR) {
658 mailmessage_free(msg);
659 res = r;
660 goto err;
661 }
662
663 msg->msg_uid = strdup(uid);
664 if (msg->msg_uid == NULL) {
665 mailmessage_free(msg);
666 res = r;
667 goto err;
668 }
669
670 * result = msg;
671
672 return MAIL_NO_ERROR;
673
674 err:
675 return res;
676}