author | zautrix <zautrix> | 2004-07-03 16:33:12 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-07-03 16:33:12 (UTC) |
commit | e3b89230f065c48c84b48c88edb6eb088374c487 (patch) (unidiff) | |
tree | 162ea2ef909a6f82ccfcedf45d80d6c821174912 /kmicromail/libetpan/maildir | |
parent | 2dd6ac0b2d24c91d35ce674a6c26351352df2b15 (diff) | |
download | kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.zip kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.gz kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.bz2 |
Initial revision
-rw-r--r-- | kmicromail/libetpan/maildir/.libs/libmaildir.a | bin | 0 -> 21790 bytes | |||
-rw-r--r-- | kmicromail/libetpan/maildir/maildir.c | 710 | ||||
-rw-r--r-- | kmicromail/libetpan/maildir/maildir.h | 60 | ||||
-rw-r--r-- | kmicromail/libetpan/maildir/maildir_types.h | 90 |
4 files changed, 860 insertions, 0 deletions
diff --git a/kmicromail/libetpan/maildir/.libs/libmaildir.a b/kmicromail/libetpan/maildir/.libs/libmaildir.a new file mode 100644 index 0000000..9eaf93e --- a/dev/null +++ b/kmicromail/libetpan/maildir/.libs/libmaildir.a | |||
Binary files differ | |||
diff --git a/kmicromail/libetpan/maildir/maildir.c b/kmicromail/libetpan/maildir/maildir.c new file mode 100644 index 0000000..320ef81 --- a/dev/null +++ b/kmicromail/libetpan/maildir/maildir.c | |||
@@ -0,0 +1,710 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001 - 2003 - 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 REGENTS 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 REGENTS 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 "maildir.h" | ||
37 | |||
38 | #include <string.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <stdio.h> | ||
41 | #include <unistd.h> | ||
42 | #include <sys/types.h> | ||
43 | #include <dirent.h> | ||
44 | #include <time.h> | ||
45 | #include <sys/stat.h> | ||
46 | #include <sys/mman.h> | ||
47 | |||
48 | #ifdef LIBETPAN_SYSTEM_BASENAME | ||
49 | #include <libgen.h> | ||
50 | #endif | ||
51 | |||
52 | /* | ||
53 | We suppose the maildir mailbox remains on one unique filesystem. | ||
54 | */ | ||
55 | |||
56 | struct maildir * maildir_new(const char * path) | ||
57 | { | ||
58 | struct maildir * md; | ||
59 | |||
60 | md = malloc(sizeof(* md)); | ||
61 | if (md == NULL) | ||
62 | goto err; | ||
63 | |||
64 | md->mdir_counter = 0; | ||
65 | md->mdir_mtime_new = (time_t) -1; | ||
66 | md->mdir_mtime_cur = (time_t) -1; | ||
67 | |||
68 | md->mdir_pid = getpid(); | ||
69 | gethostname(md->mdir_hostname, sizeof(md->mdir_hostname)); | ||
70 | strncpy(md->mdir_path, path, sizeof(md->mdir_path)); | ||
71 | md->mdir_path[PATH_MAX - 1] = '\0'; | ||
72 | |||
73 | md->mdir_msg_list = carray_new(128); | ||
74 | if (md->mdir_msg_list == NULL) | ||
75 | goto free; | ||
76 | |||
77 | md->mdir_msg_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYNONE); | ||
78 | if (md->mdir_msg_hash == NULL) | ||
79 | goto free_msg_list; | ||
80 | |||
81 | return md; | ||
82 | |||
83 | free_msg_list: | ||
84 | carray_free(md->mdir_msg_list); | ||
85 | free: | ||
86 | free(md); | ||
87 | err: | ||
88 | return NULL; | ||
89 | } | ||
90 | |||
91 | static void maildir_flush(struct maildir * md, int msg_new); | ||
92 | |||
93 | void maildir_free(struct maildir * md) | ||
94 | { | ||
95 | maildir_flush(md, 0); | ||
96 | maildir_flush(md, 1); | ||
97 | chash_free(md->mdir_msg_hash); | ||
98 | carray_free(md->mdir_msg_list); | ||
99 | free(md); | ||
100 | } | ||
101 | |||
102 | #define MAX_TRY_ALLOC 32 | ||
103 | |||
104 | static char * maildir_get_new_message_filename(struct maildir * md, | ||
105 | char * tmpfile) | ||
106 | { | ||
107 | char filename[PATH_MAX]; | ||
108 | char basename[PATH_MAX]; | ||
109 | int k; | ||
110 | time_t now; | ||
111 | |||
112 | now = time(NULL); | ||
113 | k = 0; | ||
114 | while (k < MAX_TRY_ALLOC) { | ||
115 | snprintf(basename, sizeof(basename), "%lu.%u_%u.%s", | ||
116 | (unsigned long) now, md->mdir_pid, md->mdir_counter, md->mdir_hostname); | ||
117 | snprintf(filename, sizeof(filename), "%s/tmp/%s", | ||
118 | md->mdir_path, basename); | ||
119 | |||
120 | if (link(tmpfile, filename) == 0) { | ||
121 | char * dup_filename; | ||
122 | |||
123 | dup_filename = strdup(filename); | ||
124 | if (dup_filename == NULL) { | ||
125 | unlink(filename); | ||
126 | return NULL; | ||
127 | } | ||
128 | |||
129 | unlink(tmpfile); | ||
130 | md->mdir_counter ++; | ||
131 | |||
132 | return dup_filename; | ||
133 | } | ||
134 | |||
135 | md->mdir_counter ++; | ||
136 | k ++; | ||
137 | } | ||
138 | |||
139 | return NULL; | ||
140 | } | ||
141 | |||
142 | |||
143 | static void msg_free(struct maildir_msg * msg) | ||
144 | { | ||
145 | free(msg->msg_uid); | ||
146 | free(msg->msg_filename); | ||
147 | free(msg); | ||
148 | } | ||
149 | |||
150 | /* | ||
151 | msg_new() | ||
152 | |||
153 | filename is given without path | ||
154 | */ | ||
155 | |||
156 | static struct maildir_msg * msg_new(char * filename, int new_msg) | ||
157 | { | ||
158 | struct maildir_msg * msg; | ||
159 | char * p; | ||
160 | int flags; | ||
161 | size_t uid_len; | ||
162 | char * begin_uid; | ||
163 | |||
164 | /* name of file : xxx-xxx_xxx-xxx:2,SRFT */ | ||
165 | |||
166 | msg = malloc(sizeof(* msg)); | ||
167 | if (msg == NULL) | ||
168 | goto err; | ||
169 | |||
170 | msg->msg_filename = strdup(filename); | ||
171 | if (msg->msg_filename == NULL) | ||
172 | goto free; | ||
173 | |||
174 | begin_uid = filename; | ||
175 | |||
176 | uid_len = strlen(begin_uid); | ||
177 | |||
178 | flags = 0; | ||
179 | p = strstr(filename, ":2,"); | ||
180 | if (p != NULL) { | ||
181 | uid_len = p - begin_uid; | ||
182 | |||
183 | p += 3; | ||
184 | |||
185 | /* parse flags */ | ||
186 | while (* p != '\0') { | ||
187 | switch (* p) { | ||
188 | case 'S': | ||
189 | flags |= MAILDIR_FLAG_SEEN; | ||
190 | break; | ||
191 | case 'R': | ||
192 | flags |= MAILDIR_FLAG_REPLIED; | ||
193 | break; | ||
194 | case 'F': | ||
195 | flags |= MAILDIR_FLAG_FLAGGED; | ||
196 | break; | ||
197 | case 'T': | ||
198 | flags |= MAILDIR_FLAG_TRASHED; | ||
199 | break; | ||
200 | } | ||
201 | p ++; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | if (new_msg) | ||
206 | flags |= MAILDIR_FLAG_NEW; | ||
207 | |||
208 | msg->msg_flags = flags; | ||
209 | |||
210 | msg->msg_uid = malloc(uid_len + 1); | ||
211 | if (msg->msg_uid == NULL) | ||
212 | goto free_filename; | ||
213 | |||
214 | strncpy(msg->msg_uid, begin_uid, uid_len); | ||
215 | msg->msg_uid[uid_len] = '\0'; | ||
216 | |||
217 | return msg; | ||
218 | |||
219 | free_filename: | ||
220 | free(msg->msg_filename); | ||
221 | free: | ||
222 | free(msg); | ||
223 | err: | ||
224 | return NULL; | ||
225 | } | ||
226 | |||
227 | static void maildir_flush(struct maildir * md, int msg_new) | ||
228 | { | ||
229 | unsigned int i; | ||
230 | |||
231 | i = 0; | ||
232 | while (i < carray_count(md->mdir_msg_list)) { | ||
233 | struct maildir_msg * msg; | ||
234 | int delete; | ||
235 | |||
236 | msg = carray_get(md->mdir_msg_list, i); | ||
237 | |||
238 | if (msg_new) { | ||
239 | delete = 0; | ||
240 | if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0) | ||
241 | delete = 1; | ||
242 | } | ||
243 | else { | ||
244 | delete = 1; | ||
245 | if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0) | ||
246 | delete = 0; | ||
247 | } | ||
248 | |||
249 | if (delete) { | ||
250 | chashdatum key; | ||
251 | |||
252 | key.data = msg->msg_uid; | ||
253 | key.len = strlen(msg->msg_uid); | ||
254 | chash_delete(md->mdir_msg_hash, &key, NULL); | ||
255 | |||
256 | carray_delete(md->mdir_msg_list, i); | ||
257 | msg_free(msg); | ||
258 | } | ||
259 | else { | ||
260 | i ++; | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | |||
265 | static int add_message(struct maildir * md, | ||
266 | char * filename, int is_new) | ||
267 | { | ||
268 | struct maildir_msg * msg; | ||
269 | chashdatum key; | ||
270 | chashdatum value; | ||
271 | unsigned int i; | ||
272 | int res; | ||
273 | int r; | ||
274 | |||
275 | msg = msg_new(filename, is_new); | ||
276 | if (msg == NULL) { | ||
277 | res = MAILDIR_ERROR_MEMORY; | ||
278 | goto err; | ||
279 | } | ||
280 | |||
281 | r = carray_add(md->mdir_msg_list, msg, &i); | ||
282 | if (r < 0) { | ||
283 | res = MAILDIR_ERROR_MEMORY; | ||
284 | goto free_msg; | ||
285 | } | ||
286 | |||
287 | key.data = msg->msg_uid; | ||
288 | key.len = strlen(msg->msg_uid); | ||
289 | value.data = msg; | ||
290 | value.len = 0; | ||
291 | |||
292 | r = chash_set(md->mdir_msg_hash, &key, &value, NULL); | ||
293 | if (r < 0) { | ||
294 | res = MAILDIR_ERROR_MEMORY; | ||
295 | goto delete; | ||
296 | } | ||
297 | |||
298 | return MAILDIR_NO_ERROR; | ||
299 | |||
300 | delete: | ||
301 | carray_delete(md->mdir_msg_list, i); | ||
302 | free_msg: | ||
303 | msg_free(msg); | ||
304 | err: | ||
305 | return res; | ||
306 | } | ||
307 | |||
308 | static int add_directory(struct maildir * md, char * path, int is_new) | ||
309 | { | ||
310 | DIR * d; | ||
311 | struct dirent * entry; | ||
312 | int res; | ||
313 | int r; | ||
314 | char filename[PATH_MAX]; | ||
315 | |||
316 | d = opendir(path); | ||
317 | if (d == NULL) { | ||
318 | res = MAILDIR_ERROR_DIRECTORY; | ||
319 | goto err; | ||
320 | } | ||
321 | |||
322 | while ((entry = readdir(d)) != NULL) { | ||
323 | struct stat stat_info; | ||
324 | |||
325 | snprintf(filename, sizeof(filename), "%s/%s", path, entry->d_name); | ||
326 | r = stat(filename, &stat_info); | ||
327 | if (r < 0) | ||
328 | continue; | ||
329 | |||
330 | if (S_ISDIR(stat_info.st_mode)) | ||
331 | continue; | ||
332 | |||
333 | r = add_message(md, entry->d_name, is_new); | ||
334 | if (r != MAILDIR_NO_ERROR) { | ||
335 | /* ignore errors */ | ||
336 | } | ||
337 | } | ||
338 | |||
339 | closedir(d); | ||
340 | |||
341 | return MAILDIR_NO_ERROR; | ||
342 | |||
343 | err: | ||
344 | return res; | ||
345 | } | ||
346 | |||
347 | int maildir_update(struct maildir * md) | ||
348 | { | ||
349 | struct stat stat_info; | ||
350 | char path_new[PATH_MAX]; | ||
351 | char path_cur[PATH_MAX]; | ||
352 | int r; | ||
353 | int res; | ||
354 | |||
355 | snprintf(path_new, sizeof(path_new), "%s/new", md->mdir_path); | ||
356 | snprintf(path_cur, sizeof(path_cur), "%s/cur", md->mdir_path); | ||
357 | |||
358 | /* did new/ changed ? */ | ||
359 | |||
360 | r = stat(path_new, &stat_info); | ||
361 | if (r < 0) { | ||
362 | res = MAILDIR_ERROR_DIRECTORY; | ||
363 | goto free; | ||
364 | } | ||
365 | |||
366 | if (md->mdir_mtime_new != stat_info.st_mtime) { | ||
367 | md->mdir_mtime_new = stat_info.st_mtime; | ||
368 | |||
369 | maildir_flush(md, 1); | ||
370 | |||
371 | /* messages in new */ | ||
372 | r = add_directory(md, path_new, 1); | ||
373 | if (r != MAILDIR_NO_ERROR) { | ||
374 | res = r; | ||
375 | goto free; | ||
376 | } | ||
377 | } | ||
378 | |||
379 | /* did cur/ changed ? */ | ||
380 | |||
381 | r = stat(path_cur, &stat_info); | ||
382 | if (r < 0) { | ||
383 | res = MAILDIR_ERROR_DIRECTORY; | ||
384 | goto free; | ||
385 | } | ||
386 | |||
387 | if (md->mdir_mtime_cur != stat_info.st_mtime) { | ||
388 | md->mdir_mtime_cur = stat_info.st_mtime; | ||
389 | |||
390 | maildir_flush(md, 0); | ||
391 | |||
392 | /* messages in cur */ | ||
393 | r = add_directory(md, path_cur, 0); | ||
394 | if (r != MAILDIR_NO_ERROR) { | ||
395 | res = r; | ||
396 | goto free; | ||
397 | } | ||
398 | } | ||
399 | |||
400 | return MAILDIR_NO_ERROR; | ||
401 | |||
402 | free: | ||
403 | maildir_flush(md, 0); | ||
404 | maildir_flush(md, 1); | ||
405 | md->mdir_mtime_cur = (time_t) -1; | ||
406 | md->mdir_mtime_new = (time_t) -1; | ||
407 | return res; | ||
408 | } | ||
409 | |||
410 | #ifndef LIBETPAN_SYSTEM_BASENAME | ||
411 | static char * libetpan_basename(char * filename) | ||
412 | { | ||
413 | char * next; | ||
414 | char * p; | ||
415 | |||
416 | p = filename; | ||
417 | next = strchr(p, '/'); | ||
418 | |||
419 | while (next != NULL) { | ||
420 | p = next; | ||
421 | next = strchr(p + 1, '/'); | ||
422 | } | ||
423 | |||
424 | if (p == filename) | ||
425 | return filename; | ||
426 | else | ||
427 | return p + 1; | ||
428 | } | ||
429 | #else | ||
430 | #define libetpan_basename(a) basename(a) | ||
431 | #endif | ||
432 | |||
433 | int maildir_message_add(struct maildir * md, | ||
434 | const char * message, size_t size) | ||
435 | { | ||
436 | char path_new[PATH_MAX]; | ||
437 | char tmpname[PATH_MAX]; | ||
438 | int fd; | ||
439 | int r; | ||
440 | char * mapping; | ||
441 | char * delivery_tmp_name; | ||
442 | char * delivery_tmp_basename; | ||
443 | char delivery_new_name[PATH_MAX]; | ||
444 | char * delivery_new_basename; | ||
445 | int res; | ||
446 | struct stat stat_info; | ||
447 | |||
448 | r = maildir_update(md); | ||
449 | if (r != MAILDIR_NO_ERROR) { | ||
450 | res = r; | ||
451 | goto err; | ||
452 | } | ||
453 | |||
454 | /* write to tmp/ with a classic temporary file */ | ||
455 | |||
456 | snprintf(tmpname, sizeof(tmpname), "%s/tmp/etpan-maildir-XXXXXX", md->mdir_path); | ||
457 | fd = mkstemp(tmpname); | ||
458 | if (fd < 0) { | ||
459 | res = MAILDIR_ERROR_FILE; | ||
460 | goto err; | ||
461 | } | ||
462 | |||
463 | r = ftruncate(fd, size); | ||
464 | if (r < 0) { | ||
465 | res = MAILDIR_ERROR_FILE; | ||
466 | goto close; | ||
467 | } | ||
468 | |||
469 | mapping = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
470 | if (mapping == MAP_FAILED) { | ||
471 | res = MAILDIR_ERROR_FILE; | ||
472 | goto close; | ||
473 | } | ||
474 | |||
475 | memcpy(mapping, message, size); | ||
476 | |||
477 | msync(mapping, size, MS_SYNC); | ||
478 | munmap(mapping, size); | ||
479 | |||
480 | close(fd); | ||
481 | |||
482 | /* write to tmp/ with maildir standard name */ | ||
483 | |||
484 | delivery_tmp_name = maildir_get_new_message_filename(md, tmpname); | ||
485 | if (delivery_tmp_name == NULL) { | ||
486 | res = MAILDIR_ERROR_FILE; | ||
487 | goto unlink; | ||
488 | } | ||
489 | |||
490 | /* write to new/ with maildir standard name */ | ||
491 | |||
492 | strncpy(tmpname, delivery_tmp_name, sizeof(tmpname)); | ||
493 | tmpname[sizeof(tmpname) - 1] = '\0'; | ||
494 | |||
495 | delivery_tmp_basename = libetpan_basename(tmpname); | ||
496 | |||
497 | snprintf(delivery_new_name, sizeof(delivery_new_name), "%s/new/%s", | ||
498 | md->mdir_path, delivery_tmp_basename); | ||
499 | |||
500 | r = link(delivery_tmp_name, delivery_new_name); | ||
501 | if (r < 0) { | ||
502 | res = MAILDIR_ERROR_FILE; | ||
503 | goto unlink_tmp; | ||
504 | } | ||
505 | |||
506 | snprintf(path_new, sizeof(path_new), "%s/new", md->mdir_path); | ||
507 | r = stat(path_new, &stat_info); | ||
508 | if (r < 0) { | ||
509 | unlink(delivery_new_name); | ||
510 | res = MAILDIR_ERROR_FILE; | ||
511 | goto unlink_tmp; | ||
512 | } | ||
513 | |||
514 | md->mdir_mtime_new = stat_info.st_mtime; | ||
515 | |||
516 | delivery_new_basename = libetpan_basename(delivery_new_name); | ||
517 | |||
518 | r = add_message(md, delivery_new_basename, 1); | ||
519 | if (r != MAILDIR_NO_ERROR) { | ||
520 | unlink(delivery_new_name); | ||
521 | res = MAILDIR_ERROR_FILE; | ||
522 | goto unlink_tmp; | ||
523 | } | ||
524 | |||
525 | unlink(delivery_tmp_name); | ||
526 | free(delivery_tmp_name); | ||
527 | |||
528 | return MAILDIR_NO_ERROR; | ||
529 | |||
530 | unlink_tmp: | ||
531 | unlink(delivery_tmp_name); | ||
532 | free(delivery_tmp_name); | ||
533 | goto err; | ||
534 | close: | ||
535 | close(fd); | ||
536 | unlink: | ||
537 | unlink(tmpname); | ||
538 | err: | ||
539 | return res; | ||
540 | } | ||
541 | |||
542 | int maildir_message_add_file(struct maildir * md, int fd) | ||
543 | { | ||
544 | char * message; | ||
545 | struct stat buf; | ||
546 | int r; | ||
547 | |||
548 | if (fstat(fd, &buf) == -1) | ||
549 | return MAILDIR_ERROR_FILE; | ||
550 | |||
551 | message = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
552 | if (message == MAP_FAILED) | ||
553 | return MAILDIR_ERROR_FILE; | ||
554 | |||
555 | r = maildir_message_add(md, message, buf.st_size); | ||
556 | |||
557 | munmap(message, buf.st_size); | ||
558 | |||
559 | return r; | ||
560 | } | ||
561 | |||
562 | char * maildir_message_get(struct maildir * md, const char * uid) | ||
563 | { | ||
564 | chashdatum key; | ||
565 | chashdatum value; | ||
566 | char filename[PATH_MAX]; | ||
567 | char * dup_filename; | ||
568 | struct maildir_msg * msg; | ||
569 | char * dir; | ||
570 | int r; | ||
571 | |||
572 | key.data = (void *) uid; | ||
573 | key.len = strlen(uid); | ||
574 | r = chash_get(md->mdir_msg_hash, &key, &value); | ||
575 | if (r < 0) | ||
576 | return NULL; | ||
577 | |||
578 | msg = value.data; | ||
579 | if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0) | ||
580 | dir = "new"; | ||
581 | else | ||
582 | dir = "cur"; | ||
583 | |||
584 | snprintf(filename, sizeof(filename), "%s/%s/%s", | ||
585 | md->mdir_path, dir, msg->msg_filename); | ||
586 | |||
587 | dup_filename = strdup(filename); | ||
588 | if (dup_filename == NULL) | ||
589 | return NULL; | ||
590 | |||
591 | return dup_filename; | ||
592 | } | ||
593 | |||
594 | int maildir_message_remove(struct maildir * md, const char * uid) | ||
595 | { | ||
596 | chashdatum key; | ||
597 | chashdatum value; | ||
598 | char filename[PATH_MAX]; | ||
599 | struct maildir_msg * msg; | ||
600 | char * dir; | ||
601 | int r; | ||
602 | int res; | ||
603 | |||
604 | key.data = (void *) uid; | ||
605 | key.len = strlen(uid); | ||
606 | r = chash_get(md->mdir_msg_hash, &key, &value); | ||
607 | if (r < 0) { | ||
608 | res = MAILDIR_ERROR_NOT_FOUND; | ||
609 | goto err; | ||
610 | } | ||
611 | |||
612 | msg = value.data; | ||
613 | if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0) | ||
614 | dir = "new"; | ||
615 | else | ||
616 | dir = "cur"; | ||
617 | |||
618 | snprintf(filename, sizeof(filename), "%s/%s/%s", | ||
619 | md->mdir_path, dir, msg->msg_filename); | ||
620 | |||
621 | r = unlink(filename); | ||
622 | if (r < 0) { | ||
623 | res = MAILDIR_ERROR_FILE; | ||
624 | goto err; | ||
625 | } | ||
626 | |||
627 | return MAILDIR_NO_ERROR; | ||
628 | |||
629 | err: | ||
630 | return res; | ||
631 | } | ||
632 | |||
633 | int maildir_message_change_flags(struct maildir * md, | ||
634 | const char * uid, int new_flags) | ||
635 | { | ||
636 | chashdatum key; | ||
637 | chashdatum value; | ||
638 | char filename[PATH_MAX]; | ||
639 | struct maildir_msg * msg; | ||
640 | char * dir; | ||
641 | int r; | ||
642 | char new_filename[PATH_MAX]; | ||
643 | char flag_str[5]; | ||
644 | size_t i; | ||
645 | int res; | ||
646 | |||
647 | key.data = (void *) uid; | ||
648 | key.len = strlen(uid); | ||
649 | r = chash_get(md->mdir_msg_hash, &key, &value); | ||
650 | if (r < 0) { | ||
651 | res = MAILDIR_ERROR_NOT_FOUND; | ||
652 | goto err; | ||
653 | } | ||
654 | |||
655 | msg = value.data; | ||
656 | if ((msg->msg_flags & MAILDIR_FLAG_NEW) != 0) | ||
657 | dir = "new"; | ||
658 | else | ||
659 | dir = "cur"; | ||
660 | |||
661 | snprintf(filename, sizeof(filename), "%s/%s/%s", | ||
662 | md->mdir_path, dir, msg->msg_filename); | ||
663 | |||
664 | if ((new_flags & MAILDIR_FLAG_NEW) != 0) | ||
665 | dir = "new"; | ||
666 | else | ||
667 | dir = "cur"; | ||
668 | |||
669 | i = 0; | ||
670 | if ((new_flags & MAILDIR_FLAG_SEEN) != 0) { | ||
671 | flag_str[i] = 'S'; | ||
672 | i ++; | ||
673 | } | ||
674 | if ((new_flags & MAILDIR_FLAG_REPLIED) != 0) { | ||
675 | flag_str[i] = 'R'; | ||
676 | i ++; | ||
677 | } | ||
678 | if ((new_flags & MAILDIR_FLAG_FLAGGED) != 0) { | ||
679 | flag_str[i] = 'F'; | ||
680 | i ++; | ||
681 | } | ||
682 | if ((new_flags & MAILDIR_FLAG_TRASHED) != 0) { | ||
683 | flag_str[i] = 'T'; | ||
684 | i ++; | ||
685 | } | ||
686 | flag_str[i] = 0; | ||
687 | |||
688 | if (flag_str[0] == '\0') | ||
689 | snprintf(new_filename, sizeof(new_filename), "%s/%s/%s", | ||
690 | md->mdir_path, dir, msg->msg_uid); | ||
691 | else | ||
692 | snprintf(new_filename, sizeof(new_filename), "%s/%s/%s:2,%s", | ||
693 | md->mdir_path, dir, msg->msg_uid, flag_str); | ||
694 | |||
695 | if (strcmp(filename, new_filename) == 0) | ||
696 | return MAILDIR_NO_ERROR; | ||
697 | |||
698 | r = link(filename, new_filename); | ||
699 | if (r < 0) { | ||
700 | res = MAILDIR_ERROR_FILE; | ||
701 | goto err; | ||
702 | } | ||
703 | |||
704 | unlink(filename); | ||
705 | |||
706 | return MAILDIR_NO_ERROR; | ||
707 | |||
708 | err: | ||
709 | return res; | ||
710 | } | ||
diff --git a/kmicromail/libetpan/maildir/maildir.h b/kmicromail/libetpan/maildir/maildir.h new file mode 100644 index 0000000..b782484 --- a/dev/null +++ b/kmicromail/libetpan/maildir/maildir.h | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001 - 2003 - 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 REGENTS 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 REGENTS 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 MAILDIR_H | ||
37 | |||
38 | #define MAILDIR_H | ||
39 | |||
40 | #include <libetpan/maildir_types.h> | ||
41 | |||
42 | struct maildir * maildir_new(const char * path); | ||
43 | |||
44 | void maildir_free(struct maildir * md); | ||
45 | |||
46 | int maildir_update(struct maildir * md); | ||
47 | |||
48 | int maildir_message_add(struct maildir * md, | ||
49 | const char * message, size_t size); | ||
50 | |||
51 | int maildir_message_add_file(struct maildir * md, int fd); | ||
52 | |||
53 | char * maildir_message_get(struct maildir * md, const char * uid); | ||
54 | |||
55 | int maildir_message_remove(struct maildir * md, const char * uid); | ||
56 | |||
57 | int maildir_message_change_flags(struct maildir * md, | ||
58 | const char * uid, int new_flags); | ||
59 | |||
60 | #endif | ||
diff --git a/kmicromail/libetpan/maildir/maildir_types.h b/kmicromail/libetpan/maildir/maildir_types.h new file mode 100644 index 0000000..c7e368e --- a/dev/null +++ b/kmicromail/libetpan/maildir/maildir_types.h | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001 - 2003 - 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 REGENTS 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 REGENTS 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 MAILDIR_TYPES_H | ||
37 | |||
38 | #define MAILDIR_TYPES_H | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <unistd.h> | ||
42 | #include <libetpan/chash.h> | ||
43 | #include <libetpan/carray.h> | ||
44 | #include <inttypes.h> | ||
45 | |||
46 | #include <libetpan/libetpan-config.h> | ||
47 | |||
48 | #define LIBETPAN_MAILDIR | ||
49 | |||
50 | enum { | ||
51 | MAILDIR_NO_ERROR = 0, | ||
52 | MAILDIR_ERROR_CREATE, | ||
53 | MAILDIR_ERROR_DIRECTORY, | ||
54 | MAILDIR_ERROR_MEMORY, | ||
55 | MAILDIR_ERROR_FILE, | ||
56 | MAILDIR_ERROR_NOT_FOUND, | ||
57 | }; | ||
58 | |||
59 | #define MAILDIR_FLAG_NEW (1 << 0) | ||
60 | #define MAILDIR_FLAG_SEEN (1 << 1) | ||
61 | #define MAILDIR_FLAG_REPLIED (1 << 2) | ||
62 | #define MAILDIR_FLAG_FLAGGED (1 << 3) | ||
63 | #define MAILDIR_FLAG_TRASHED (1 << 4) | ||
64 | |||
65 | struct maildir_msg { | ||
66 | char * msg_uid; | ||
67 | char * msg_filename; | ||
68 | int msg_flags; | ||
69 | }; | ||
70 | |||
71 | /* | ||
72 | work around for missing #define HOST_NAME_MAX in Linux | ||
73 | */ | ||
74 | |||
75 | #ifndef HOST_NAME_MAX | ||
76 | #define HOST_NAME_MAX 255 | ||
77 | #endif | ||
78 | |||
79 | struct maildir { | ||
80 | pid_t mdir_pid; | ||
81 | char mdir_hostname[HOST_NAME_MAX]; | ||
82 | char mdir_path[PATH_MAX]; | ||
83 | uint32_t mdir_counter; | ||
84 | time_t mdir_mtime_new; | ||
85 | time_t mdir_mtime_cur; | ||
86 | carray * mdir_msg_list; | ||
87 | chash * mdir_msg_hash; | ||
88 | }; | ||
89 | |||
90 | #endif | ||