Diffstat (limited to 'kmicromail/libetpan/generic/imapdriver.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kmicromail/libetpan/generic/imapdriver.c | 1130 |
1 files changed, 1130 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/imapdriver.c b/kmicromail/libetpan/generic/imapdriver.c new file mode 100644 index 0000000..0d63319 --- a/dev/null +++ b/kmicromail/libetpan/generic/imapdriver.c | |||
@@ -0,0 +1,1130 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - 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 "imapdriver.h" | ||
37 | |||
38 | #include "mail.h" | ||
39 | #include "imapdriver_tools.h" | ||
40 | #include "mailmessage.h" | ||
41 | #include "imapdriver_message.h" | ||
42 | #include "imapdriver_types.h" | ||
43 | #include "maildriver.h" | ||
44 | #include "maildriver_tools.h" | ||
45 | #include "generic_cache.h" | ||
46 | |||
47 | #include <stdlib.h> | ||
48 | #include <string.h> | ||
49 | |||
50 | static int imapdriver_initialize(mailsession * session); | ||
51 | |||
52 | static void imapdriver_uninitialize(mailsession * session); | ||
53 | |||
54 | static int imapdriver_connect_stream(mailsession * session, mailstream * s); | ||
55 | |||
56 | static int imapdriver_starttls(mailsession * session); | ||
57 | |||
58 | static int imapdriver_login(mailsession * session, | ||
59 | char * userid, char * password); | ||
60 | |||
61 | static int imapdriver_logout(mailsession * session); | ||
62 | |||
63 | static int imapdriver_noop(mailsession * session); | ||
64 | |||
65 | static int imapdriver_build_folder_name(mailsession * session, char * mb, | ||
66 | char * name, char ** result); | ||
67 | |||
68 | static int imapdriver_create_folder(mailsession * session, char * mb); | ||
69 | |||
70 | static int imapdriver_delete_folder(mailsession * session, char * mb); | ||
71 | |||
72 | static int imapdriver_rename_folder(mailsession * session, char * mb, | ||
73 | char * new_name); | ||
74 | |||
75 | static int imapdriver_check_folder(mailsession * session); | ||
76 | |||
77 | static int imapdriver_examine_folder(mailsession * session, char * mb); | ||
78 | |||
79 | static int imapdriver_select_folder(mailsession * session, char * mb); | ||
80 | static int imapdriver_expunge_folder(mailsession * session); | ||
81 | |||
82 | static int imapdriver_status_folder(mailsession * session, char * mb, | ||
83 | uint32_t * result_messages, uint32_t * result_recent, | ||
84 | uint32_t * result_unseen); | ||
85 | |||
86 | static int imapdriver_messages_number(mailsession * session, char * mb, | ||
87 | uint32_t * result); | ||
88 | |||
89 | static int imapdriver_recent_number(mailsession * session, char * mb, | ||
90 | uint32_t * result); | ||
91 | |||
92 | static int imapdriver_unseen_number(mailsession * session, char * mb, | ||
93 | uint32_t * result); | ||
94 | |||
95 | static int imapdriver_list_folders(mailsession * session, char * mb, | ||
96 | struct mail_list ** result); | ||
97 | static int imapdriver_lsub_folders(mailsession * session, char * mb, | ||
98 | struct mail_list ** result); | ||
99 | static int imapdriver_subscribe_folder(mailsession * session, char * mb); | ||
100 | static int imapdriver_unsubscribe_folder(mailsession * session, char * mb); | ||
101 | static int imapdriver_append_message(mailsession * session, | ||
102 | char * message, size_t size); | ||
103 | static int imapdriver_copy_message(mailsession * session, | ||
104 | uint32_t num, char * mb); | ||
105 | |||
106 | static int imapdriver_get_messages_list(mailsession * session, | ||
107 | struct mailmessage_list ** result); | ||
108 | |||
109 | static int | ||
110 | imapdriver_get_envelopes_list(mailsession * session, | ||
111 | struct mailmessage_list * env_list); | ||
112 | |||
113 | |||
114 | #if 0 | ||
115 | static int imapdriver_search_messages(mailsession * session, char * charset, | ||
116 | struct mail_search_key * key, | ||
117 | struct mail_search_result ** result); | ||
118 | #endif | ||
119 | |||
120 | static int imapdriver_get_message(mailsession * session, | ||
121 | uint32_t num, mailmessage ** result); | ||
122 | |||
123 | static int imapdriver_get_message_by_uid(mailsession * session, | ||
124 | const char * uid, | ||
125 | mailmessage ** result); | ||
126 | |||
127 | static mailsession_driver local_imap_session_driver = { | ||
128 | .sess_name = "imap", | ||
129 | |||
130 | .sess_initialize = imapdriver_initialize, | ||
131 | .sess_uninitialize = imapdriver_uninitialize, | ||
132 | |||
133 | .sess_parameters = NULL, | ||
134 | |||
135 | .sess_connect_stream = imapdriver_connect_stream, | ||
136 | .sess_connect_path = NULL, | ||
137 | .sess_starttls = imapdriver_starttls, | ||
138 | .sess_login = imapdriver_login, | ||
139 | .sess_logout = imapdriver_logout, | ||
140 | .sess_noop = imapdriver_noop, | ||
141 | |||
142 | .sess_build_folder_name = imapdriver_build_folder_name, | ||
143 | .sess_create_folder = imapdriver_create_folder, | ||
144 | .sess_delete_folder = imapdriver_delete_folder, | ||
145 | .sess_rename_folder = imapdriver_rename_folder, | ||
146 | .sess_check_folder = imapdriver_check_folder, | ||
147 | .sess_examine_folder = imapdriver_examine_folder, | ||
148 | .sess_select_folder = imapdriver_select_folder, | ||
149 | .sess_expunge_folder = imapdriver_expunge_folder, | ||
150 | .sess_status_folder = imapdriver_status_folder, | ||
151 | .sess_messages_number = imapdriver_messages_number, | ||
152 | .sess_recent_number = imapdriver_recent_number, | ||
153 | .sess_unseen_number = imapdriver_unseen_number, | ||
154 | .sess_list_folders = imapdriver_list_folders, | ||
155 | .sess_lsub_folders = imapdriver_lsub_folders, | ||
156 | .sess_subscribe_folder = imapdriver_subscribe_folder, | ||
157 | .sess_unsubscribe_folder = imapdriver_unsubscribe_folder, | ||
158 | |||
159 | .sess_append_message = imapdriver_append_message, | ||
160 | .sess_copy_message = imapdriver_copy_message, | ||
161 | .sess_move_message = NULL, | ||
162 | |||
163 | .sess_get_messages_list = imapdriver_get_messages_list, | ||
164 | .sess_get_envelopes_list = imapdriver_get_envelopes_list, | ||
165 | .sess_remove_message = NULL, | ||
166 | #if 0 | ||
167 | .sess_search_messages = imapdriver_search_messages, | ||
168 | #endif | ||
169 | |||
170 | .sess_get_message = imapdriver_get_message, | ||
171 | .sess_get_message_by_uid = imapdriver_get_message_by_uid, | ||
172 | }; | ||
173 | |||
174 | mailsession_driver * imap_session_driver = &local_imap_session_driver; | ||
175 | |||
176 | static inline struct imap_session_state_data * get_data(mailsession * session) | ||
177 | { | ||
178 | return session->sess_data; | ||
179 | } | ||
180 | |||
181 | static mailimap * get_imap_session(mailsession * session) | ||
182 | { | ||
183 | return get_data(session)->imap_session; | ||
184 | } | ||
185 | |||
186 | static int imapdriver_initialize(mailsession * session) | ||
187 | { | ||
188 | struct imap_session_state_data * data; | ||
189 | mailimap * imap; | ||
190 | struct mail_flags_store * flags_store; | ||
191 | |||
192 | imap = mailimap_new(0, NULL); | ||
193 | if (imap == NULL) | ||
194 | goto err; | ||
195 | |||
196 | flags_store = mail_flags_store_new(); | ||
197 | if (flags_store == NULL) | ||
198 | goto free_session; | ||
199 | |||
200 | data = malloc(sizeof(* data)); | ||
201 | if (data == NULL) | ||
202 | goto free_flags_store; | ||
203 | |||
204 | data->imap_mailbox = NULL; | ||
205 | data->imap_session = imap; | ||
206 | data->imap_flags_store = flags_store; | ||
207 | |||
208 | session->sess_data = data; | ||
209 | |||
210 | return MAIL_NO_ERROR; | ||
211 | |||
212 | free_flags_store: | ||
213 | mail_flags_store_free(flags_store); | ||
214 | free_session: | ||
215 | mailimap_free(imap); | ||
216 | err: | ||
217 | return MAIL_ERROR_MEMORY; | ||
218 | } | ||
219 | |||
220 | static void imap_flags_store_process(mailimap * imap, | ||
221 | struct mail_flags_store * flags_store) | ||
222 | { | ||
223 | unsigned int i; | ||
224 | int r; | ||
225 | mailmessage * first; | ||
226 | mailmessage * last; | ||
227 | |||
228 | mail_flags_store_sort(flags_store); | ||
229 | |||
230 | if (carray_count(flags_store->fls_tab) == 0) | ||
231 | return; | ||
232 | |||
233 | first = carray_get(flags_store->fls_tab, 0); | ||
234 | last = first; | ||
235 | |||
236 | for(i = 1 ; i < carray_count(flags_store->fls_tab) ; i ++) { | ||
237 | mailmessage * msg; | ||
238 | |||
239 | msg = carray_get(flags_store->fls_tab, i); | ||
240 | |||
241 | if (last->msg_index + 1 == msg->msg_index) { | ||
242 | r = mail_flags_compare(first->msg_flags, msg->msg_flags); | ||
243 | if (r == 0) { | ||
244 | last = msg; | ||
245 | continue; | ||
246 | } | ||
247 | } | ||
248 | |||
249 | r = imap_store_flags(imap, first->msg_index, | ||
250 | last->msg_index, first->msg_flags); | ||
251 | |||
252 | first = msg; | ||
253 | last = msg; | ||
254 | } | ||
255 | |||
256 | r = imap_store_flags(imap, first->msg_index, last->msg_index, | ||
257 | first->msg_flags); | ||
258 | |||
259 | mail_flags_store_clear(flags_store); | ||
260 | } | ||
261 | |||
262 | static void imapdriver_uninitialize(mailsession * session) | ||
263 | { | ||
264 | struct imap_session_state_data * data; | ||
265 | |||
266 | data = get_data(session); | ||
267 | |||
268 | imap_flags_store_process(data->imap_session, | ||
269 | data->imap_flags_store); | ||
270 | mail_flags_store_free(data->imap_flags_store); | ||
271 | |||
272 | mailimap_free(data->imap_session); | ||
273 | if (data->imap_mailbox != NULL) | ||
274 | free(data->imap_mailbox); | ||
275 | free(data); | ||
276 | |||
277 | session->sess_data = NULL; | ||
278 | } | ||
279 | |||
280 | static int imapdriver_connect_stream(mailsession * session, mailstream * s) | ||
281 | { | ||
282 | int r; | ||
283 | |||
284 | r = mailimap_connect(get_imap_session(session), s); | ||
285 | |||
286 | return imap_error_to_mail_error(r); | ||
287 | } | ||
288 | |||
289 | static int imapdriver_login(mailsession * session, | ||
290 | char * userid, char * password) | ||
291 | { | ||
292 | int r; | ||
293 | |||
294 | r = mailimap_login(get_imap_session(session), userid, password); | ||
295 | |||
296 | return imap_error_to_mail_error(r); | ||
297 | } | ||
298 | |||
299 | static int imapdriver_logout(mailsession * session) | ||
300 | { | ||
301 | int r; | ||
302 | |||
303 | imap_flags_store_process(get_imap_session(session), | ||
304 | get_data(session)->imap_flags_store); | ||
305 | |||
306 | r = mailimap_logout(get_imap_session(session)); | ||
307 | |||
308 | return imap_error_to_mail_error(r); | ||
309 | } | ||
310 | |||
311 | static int imapdriver_noop(mailsession * session) | ||
312 | { | ||
313 | int r; | ||
314 | |||
315 | r = mailimap_noop(get_imap_session(session)); | ||
316 | |||
317 | return imap_error_to_mail_error(r); | ||
318 | } | ||
319 | |||
320 | static int imapdriver_build_folder_name(mailsession * session, char * mb, | ||
321 | char * name, char ** result) | ||
322 | { | ||
323 | char delimiter[2] = "X"; | ||
324 | char * folder_name; | ||
325 | mailimap * imap; | ||
326 | struct mailimap_mailbox_list * mb_list; | ||
327 | int r; | ||
328 | clist * imap_list; | ||
329 | |||
330 | imap = get_imap_session(session); | ||
331 | |||
332 | r = mailimap_list(imap, mb, "", &imap_list); | ||
333 | if (r != MAILIMAP_NO_ERROR) | ||
334 | return r; | ||
335 | |||
336 | if (clist_begin(imap_list) == NULL) | ||
337 | return MAIL_ERROR_LIST; | ||
338 | |||
339 | mb_list = clist_begin(imap_list)->data; | ||
340 | delimiter[0] = mb_list->mb_delimiter; | ||
341 | |||
342 | folder_name = malloc(strlen(mb) + strlen(delimiter) + strlen(name) + 1); | ||
343 | if (folder_name == NULL) | ||
344 | return MAIL_ERROR_MEMORY; | ||
345 | |||
346 | strcpy(folder_name, mb); | ||
347 | strcat(folder_name, delimiter); | ||
348 | strcat(folder_name, name); | ||
349 | |||
350 | * result = folder_name; | ||
351 | |||
352 | return MAIL_NO_ERROR; | ||
353 | } | ||
354 | |||
355 | /* folders operations */ | ||
356 | |||
357 | static int imapdriver_create_folder(mailsession * session, char * mb) | ||
358 | { | ||
359 | int r; | ||
360 | |||
361 | r = mailimap_create(get_imap_session(session), mb); | ||
362 | |||
363 | return imap_error_to_mail_error(r); | ||
364 | } | ||
365 | |||
366 | static int imapdriver_delete_folder(mailsession * session, char * mb) | ||
367 | { | ||
368 | int r; | ||
369 | |||
370 | r = mailimap_delete(get_imap_session(session), mb); | ||
371 | |||
372 | return imap_error_to_mail_error(r); | ||
373 | } | ||
374 | |||
375 | static int imapdriver_rename_folder(mailsession * session, char * mb, | ||
376 | char * new_name) | ||
377 | { | ||
378 | int r; | ||
379 | |||
380 | r = mailimap_rename(get_imap_session(session), mb, new_name); | ||
381 | |||
382 | return imap_error_to_mail_error(r); | ||
383 | } | ||
384 | |||
385 | static int imapdriver_check_folder(mailsession * session) | ||
386 | { | ||
387 | int r; | ||
388 | |||
389 | imap_flags_store_process(get_imap_session(session), | ||
390 | get_data(session)->imap_flags_store); | ||
391 | |||
392 | r = mailimap_check(get_imap_session(session)); | ||
393 | |||
394 | return imap_error_to_mail_error(r); | ||
395 | } | ||
396 | |||
397 | static int imapdriver_examine_folder(mailsession * session, char * mb) | ||
398 | { | ||
399 | int r; | ||
400 | |||
401 | r = mailimap_examine(get_imap_session(session), mb); | ||
402 | |||
403 | return imap_error_to_mail_error(r); | ||
404 | } | ||
405 | |||
406 | static int imapdriver_select_folder(mailsession * session, char * mb) | ||
407 | { | ||
408 | int r; | ||
409 | char * new_mb; | ||
410 | char * old_mb; | ||
411 | |||
412 | old_mb = get_data(session)->imap_mailbox; | ||
413 | if (old_mb != NULL) | ||
414 | if (strcmp(mb, old_mb) == 0) | ||
415 | return MAIL_NO_ERROR; | ||
416 | |||
417 | imap_flags_store_process(get_imap_session(session), | ||
418 | get_data(session)->imap_flags_store); | ||
419 | |||
420 | r = mailimap_select(get_imap_session(session), mb); | ||
421 | |||
422 | switch (r) { | ||
423 | case MAILIMAP_NO_ERROR: | ||
424 | new_mb = strdup(mb); | ||
425 | if (new_mb == NULL) { | ||
426 | if (old_mb != NULL) | ||
427 | free(old_mb); | ||
428 | get_data(session)->imap_mailbox = NULL; | ||
429 | return MAIL_ERROR_MEMORY; | ||
430 | } | ||
431 | |||
432 | get_data(session)->imap_mailbox = new_mb; | ||
433 | |||
434 | return MAIL_NO_ERROR; | ||
435 | default: | ||
436 | return imap_error_to_mail_error(r); | ||
437 | } | ||
438 | } | ||
439 | |||
440 | static int imapdriver_expunge_folder(mailsession * session) | ||
441 | { | ||
442 | int r; | ||
443 | |||
444 | imap_flags_store_process(get_imap_session(session), | ||
445 | get_data(session)->imap_flags_store); | ||
446 | |||
447 | r = mailimap_expunge(get_imap_session(session)); | ||
448 | |||
449 | return imap_error_to_mail_error(r); | ||
450 | } | ||
451 | |||
452 | static int status_selected_folder(mailsession * session, char * mb, | ||
453 | uint32_t * result_messages, uint32_t * result_recent, | ||
454 | uint32_t * result_unseen) | ||
455 | { | ||
456 | int r; | ||
457 | int res; | ||
458 | mailimap * imap; | ||
459 | uint32_t exists; | ||
460 | uint32_t unseen; | ||
461 | uint32_t recent; | ||
462 | struct mailimap_search_key * search_key; | ||
463 | clist * search_result; | ||
464 | |||
465 | imap = get_imap_session(session); | ||
466 | |||
467 | exists = imap->imap_selection_info->sel_exists; | ||
468 | recent = imap->imap_selection_info->sel_recent; | ||
469 | |||
470 | search_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN, | ||
471 | NULL, NULL, NULL, NULL, NULL, | ||
472 | NULL, NULL, NULL, NULL, NULL, | ||
473 | NULL, NULL, NULL, NULL, 0, | ||
474 | NULL, NULL, NULL, NULL, NULL, | ||
475 | NULL, 0, NULL, NULL, NULL); | ||
476 | if (search_key == NULL) { | ||
477 | res = MAIL_ERROR_MEMORY; | ||
478 | goto err; | ||
479 | } | ||
480 | |||
481 | /* default : use the RECENT count if search fails */ | ||
482 | unseen = recent; | ||
483 | r = mailimap_search(imap, NULL, search_key, &search_result); | ||
484 | mailimap_search_key_free(search_key); | ||
485 | if (r == MAILIMAP_NO_ERROR) { | ||
486 | /* if this succeed, we use the real count */ | ||
487 | unseen = clist_count(search_result); | ||
488 | mailimap_mailbox_data_search_free(search_result); | ||
489 | } | ||
490 | |||
491 | * result_messages = exists; | ||
492 | * result_unseen = unseen; | ||
493 | * result_recent = recent; | ||
494 | |||
495 | return MAIL_NO_ERROR; | ||
496 | |||
497 | err: | ||
498 | return res; | ||
499 | } | ||
500 | |||
501 | static int status_unselected_folder(mailsession * session, char * mb, | ||
502 | uint32_t * result_messages, uint32_t * result_recent, | ||
503 | uint32_t * result_unseen) | ||
504 | { | ||
505 | struct mailimap_status_att_list * att_list; | ||
506 | struct mailimap_mailbox_data_status * status; | ||
507 | int r; | ||
508 | int res; | ||
509 | clistiter * cur; | ||
510 | mailimap * imap; | ||
511 | |||
512 | imap = get_imap_session(session); | ||
513 | |||
514 | att_list = mailimap_status_att_list_new_empty(); | ||
515 | if (att_list == NULL) { | ||
516 | res = MAIL_ERROR_MEMORY; | ||
517 | goto err; | ||
518 | } | ||
519 | |||
520 | r = mailimap_status_att_list_add(att_list, MAILIMAP_STATUS_ATT_MESSAGES); | ||
521 | switch (r) { | ||
522 | case MAILIMAP_NO_ERROR: | ||
523 | break; | ||
524 | default: | ||
525 | res = MAIL_ERROR_MEMORY; | ||
526 | goto free; | ||
527 | } | ||
528 | |||
529 | r = mailimap_status_att_list_add(att_list, MAILIMAP_STATUS_ATT_RECENT); | ||
530 | switch (r) { | ||
531 | case MAILIMAP_NO_ERROR: | ||
532 | break; | ||
533 | default: | ||
534 | res = MAIL_ERROR_MEMORY; | ||
535 | goto free; | ||
536 | } | ||
537 | |||
538 | r = mailimap_status_att_list_add(att_list, MAILIMAP_STATUS_ATT_UNSEEN); | ||
539 | switch (r) { | ||
540 | case MAILIMAP_NO_ERROR: | ||
541 | break; | ||
542 | default: | ||
543 | res = MAIL_ERROR_MEMORY; | ||
544 | goto free; | ||
545 | } | ||
546 | |||
547 | r = mailimap_status(imap, mb, att_list, &status); | ||
548 | |||
549 | switch (r) { | ||
550 | case MAILIMAP_NO_ERROR: | ||
551 | break; | ||
552 | default: | ||
553 | res = imap_error_to_mail_error(r); | ||
554 | goto free; | ||
555 | } | ||
556 | |||
557 | * result_messages = 0; | ||
558 | * result_recent = 0; | ||
559 | * result_unseen = 0; | ||
560 | |||
561 | for (cur = clist_begin(status->st_info_list); | ||
562 | cur != NULL ; cur = clist_next(cur)) { | ||
563 | struct mailimap_status_info * status_info; | ||
564 | |||
565 | status_info = clist_content(cur); | ||
566 | switch (status_info->st_att) { | ||
567 | case MAILIMAP_STATUS_ATT_MESSAGES: | ||
568 | * result_messages = status_info->st_value; | ||
569 | break; | ||
570 | case MAILIMAP_STATUS_ATT_RECENT: | ||
571 | * result_recent = status_info->st_value; | ||
572 | break; | ||
573 | case MAILIMAP_STATUS_ATT_UNSEEN: | ||
574 | * result_unseen = status_info->st_value; | ||
575 | break; | ||
576 | } | ||
577 | } | ||
578 | |||
579 | mailimap_mailbox_data_status_free(status); | ||
580 | mailimap_status_att_list_free(att_list); | ||
581 | |||
582 | return MAIL_NO_ERROR; | ||
583 | |||
584 | free: | ||
585 | mailimap_status_att_list_free(att_list); | ||
586 | err: | ||
587 | return res; | ||
588 | } | ||
589 | |||
590 | static int imapdriver_status_folder(mailsession * session, char * mb, | ||
591 | uint32_t * result_messages, uint32_t * result_recent, | ||
592 | uint32_t * result_unseen) | ||
593 | { | ||
594 | int res; | ||
595 | int current_folder; | ||
596 | char * current_mb; | ||
597 | |||
598 | if (mb == NULL) { | ||
599 | mb = get_data(session)->imap_mailbox; | ||
600 | if (mb == NULL) { | ||
601 | res = MAIL_ERROR_BAD_STATE; | ||
602 | goto err; | ||
603 | } | ||
604 | } | ||
605 | |||
606 | current_mb = get_data(session)->imap_mailbox; | ||
607 | if (strcmp(mb, current_mb) == 0) | ||
608 | current_folder = 1; | ||
609 | else | ||
610 | current_folder = 0; | ||
611 | |||
612 | if (current_folder) | ||
613 | return status_selected_folder(session, mb, result_messages, | ||
614 | result_recent, result_unseen); | ||
615 | else | ||
616 | return status_unselected_folder(session, mb, result_messages, | ||
617 | result_recent, result_unseen); | ||
618 | |||
619 | err: | ||
620 | return res; | ||
621 | } | ||
622 | |||
623 | /* TODO : more efficient functions */ | ||
624 | |||
625 | static int imapdriver_messages_number(mailsession * session, char * mb, | ||
626 | uint32_t * result) | ||
627 | { | ||
628 | uint32_t messages; | ||
629 | uint32_t recent; | ||
630 | uint32_t unseen; | ||
631 | int r; | ||
632 | |||
633 | r = imapdriver_status_folder(session, mb, &messages, &recent, &unseen); | ||
634 | if (r != MAIL_NO_ERROR) | ||
635 | return r; | ||
636 | |||
637 | * result = messages; | ||
638 | |||
639 | return MAIL_NO_ERROR; | ||
640 | } | ||
641 | |||
642 | static int imapdriver_recent_number(mailsession * session, char * mb, | ||
643 | uint32_t * result) | ||
644 | { | ||
645 | uint32_t messages; | ||
646 | uint32_t recent; | ||
647 | uint32_t unseen; | ||
648 | int r; | ||
649 | |||
650 | r = imapdriver_status_folder(session, mb, &messages, &recent, &unseen); | ||
651 | if (r != MAIL_NO_ERROR) | ||
652 | return r; | ||
653 | |||
654 | * result = recent; | ||
655 | |||
656 | return MAIL_NO_ERROR; | ||
657 | } | ||
658 | |||
659 | static int imapdriver_unseen_number(mailsession * session, char * mb, | ||
660 | uint32_t * result) | ||
661 | { | ||
662 | uint32_t messages; | ||
663 | uint32_t recent; | ||
664 | uint32_t unseen; | ||
665 | int r; | ||
666 | |||
667 | r = imapdriver_status_folder(session, mb, &messages, &recent, &unseen); | ||
668 | if (r != MAIL_NO_ERROR) | ||
669 | return r; | ||
670 | |||
671 | * result = unseen; | ||
672 | |||
673 | return MAIL_NO_ERROR; | ||
674 | } | ||
675 | |||
676 | enum { | ||
677 | IMAP_LIST, IMAP_LSUB | ||
678 | }; | ||
679 | |||
680 | static int imapdriver_list_lsub_folders(mailsession * session, int type, | ||
681 | char * mb, | ||
682 | struct mail_list ** result) | ||
683 | { | ||
684 | clist * imap_list; | ||
685 | struct mail_list * resp; | ||
686 | int r; | ||
687 | int res; | ||
688 | |||
689 | switch (type) { | ||
690 | case IMAP_LIST: | ||
691 | r = mailimap_list(get_imap_session(session), mb, | ||
692 | "*", &imap_list); | ||
693 | break; | ||
694 | case IMAP_LSUB: | ||
695 | r = mailimap_lsub(get_imap_session(session), mb, | ||
696 | "*", &imap_list); | ||
697 | break; | ||
698 | default: | ||
699 | res = MAIL_ERROR_LIST; | ||
700 | goto err; | ||
701 | } | ||
702 | |||
703 | switch (r) { | ||
704 | case MAILIMAP_NO_ERROR: | ||
705 | break; | ||
706 | default: | ||
707 | res = imap_error_to_mail_error(r); | ||
708 | goto err; | ||
709 | } | ||
710 | |||
711 | r = imap_list_to_list(imap_list, &resp); | ||
712 | if (r != MAIL_NO_ERROR) { | ||
713 | mailimap_list_result_free(imap_list); | ||
714 | res = r; | ||
715 | goto err; | ||
716 | } | ||
717 | |||
718 | mailimap_list_result_free(imap_list); | ||
719 | |||
720 | * result = resp; | ||
721 | |||
722 | return MAIL_NO_ERROR; | ||
723 | |||
724 | err: | ||
725 | return res; | ||
726 | } | ||
727 | |||
728 | static int imapdriver_list_folders(mailsession * session, char * mb, | ||
729 | struct mail_list ** result) | ||
730 | { | ||
731 | return imapdriver_list_lsub_folders(session, IMAP_LIST, mb, | ||
732 | result); | ||
733 | } | ||
734 | |||
735 | static int imapdriver_lsub_folders(mailsession * session, char * mb, | ||
736 | struct mail_list ** result) | ||
737 | { | ||
738 | return imapdriver_list_lsub_folders(session, IMAP_LSUB, mb, | ||
739 | result); | ||
740 | } | ||
741 | |||
742 | static int imapdriver_subscribe_folder(mailsession * session, char * mb) | ||
743 | { | ||
744 | int r; | ||
745 | |||
746 | r = mailimap_subscribe(get_imap_session(session), mb); | ||
747 | |||
748 | return imap_error_to_mail_error(r); | ||
749 | } | ||
750 | |||
751 | static int imapdriver_unsubscribe_folder(mailsession * session, char * mb) | ||
752 | { | ||
753 | int r; | ||
754 | |||
755 | r = mailimap_unsubscribe(get_imap_session(session), mb); | ||
756 | |||
757 | return imap_error_to_mail_error(r); | ||
758 | } | ||
759 | |||
760 | /* messages operations */ | ||
761 | |||
762 | static int imapdriver_append_message(mailsession * session, | ||
763 | char * message, size_t size) | ||
764 | { | ||
765 | int r; | ||
766 | |||
767 | r = mailimap_append_simple(get_imap_session(session), | ||
768 | get_data(session)->imap_mailbox, | ||
769 | message, size); | ||
770 | |||
771 | return imap_error_to_mail_error(r); | ||
772 | } | ||
773 | |||
774 | static int imapdriver_copy_message(mailsession * session, | ||
775 | uint32_t num, char * mb) | ||
776 | { | ||
777 | int r; | ||
778 | struct mailimap_set * set; | ||
779 | int res; | ||
780 | |||
781 | set = mailimap_set_new_single(num); | ||
782 | if (set == NULL) { | ||
783 | res = MAIL_ERROR_MEMORY; | ||
784 | goto err; | ||
785 | } | ||
786 | |||
787 | r = mailimap_uid_copy(get_imap_session(session), set, mb); | ||
788 | |||
789 | mailimap_set_free(set); | ||
790 | |||
791 | return imap_error_to_mail_error(r); | ||
792 | |||
793 | err: | ||
794 | return res; | ||
795 | } | ||
796 | |||
797 | static int imapdriver_get_messages_list(mailsession * session, | ||
798 | struct mailmessage_list ** result) | ||
799 | { | ||
800 | return imap_get_messages_list(get_imap_session(session), | ||
801 | session, imap_message_driver, 1, | ||
802 | result); | ||
803 | } | ||
804 | |||
805 | |||
806 | |||
807 | static int | ||
808 | imapdriver_get_envelopes_list(mailsession * session, | ||
809 | struct mailmessage_list * env_list) | ||
810 | { | ||
811 | struct mailimap_set * set; | ||
812 | struct mailimap_fetch_att * fetch_att; | ||
813 | struct mailimap_fetch_type * fetch_type; | ||
814 | int res; | ||
815 | clist * fetch_result; | ||
816 | int r; | ||
817 | uint32_t exists; | ||
818 | clist * msg_list; | ||
819 | |||
820 | if (get_imap_session(session)->imap_selection_info == NULL) { | ||
821 | res = MAIL_ERROR_BAD_STATE; | ||
822 | goto err; | ||
823 | } | ||
824 | |||
825 | imap_flags_store_process(get_imap_session(session), | ||
826 | get_data(session)->imap_flags_store); | ||
827 | |||
828 | exists = get_imap_session(session)->imap_selection_info->sel_exists; | ||
829 | |||
830 | if (exists == 0) | ||
831 | return MAIL_NO_ERROR; | ||
832 | |||
833 | fetch_type = mailimap_fetch_type_new_fetch_att_list_empty(); | ||
834 | if (fetch_type == NULL) { | ||
835 | res = MAIL_ERROR_MEMORY; | ||
836 | goto err; | ||
837 | } | ||
838 | |||
839 | fetch_att = mailimap_fetch_att_new_uid(); | ||
840 | if (fetch_att == NULL) { | ||
841 | res = MAIL_ERROR_MEMORY; | ||
842 | goto free_fetch_type; | ||
843 | } | ||
844 | |||
845 | r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att); | ||
846 | if (r != MAILIMAP_NO_ERROR) { | ||
847 | mailimap_fetch_att_free(fetch_att); | ||
848 | res = MAIL_ERROR_MEMORY; | ||
849 | goto free_fetch_type; | ||
850 | } | ||
851 | |||
852 | fetch_att = mailimap_fetch_att_new_flags(); | ||
853 | if (fetch_att == NULL) { | ||
854 | res = MAIL_ERROR_MEMORY; | ||
855 | goto free_fetch_type; | ||
856 | } | ||
857 | |||
858 | r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att); | ||
859 | if (r != MAILIMAP_NO_ERROR) { | ||
860 | mailimap_fetch_att_free(fetch_att); | ||
861 | res = MAIL_ERROR_MEMORY; | ||
862 | goto free_fetch_type; | ||
863 | } | ||
864 | |||
865 | r = imap_add_envelope_fetch_att(fetch_type); | ||
866 | if (r != MAIL_NO_ERROR) { | ||
867 | res = r; | ||
868 | goto free_fetch_type; | ||
869 | } | ||
870 | |||
871 | r = maildriver_env_list_to_msg_list(env_list, &msg_list); | ||
872 | if (r != MAIL_NO_ERROR) { | ||
873 | res = MAIL_ERROR_MEMORY; | ||
874 | goto free_fetch_type; | ||
875 | } | ||
876 | |||
877 | if (clist_begin(msg_list) == NULL) { | ||
878 | /* no need to fetch envelopes */ | ||
879 | |||
880 | mailimap_fetch_type_free(fetch_type); | ||
881 | clist_free(msg_list); | ||
882 | return MAIL_NO_ERROR; | ||
883 | } | ||
884 | |||
885 | r = msg_list_to_imap_set(msg_list, &set); | ||
886 | if (r != MAIL_NO_ERROR) { | ||
887 | clist_foreach(msg_list, (clist_func) free, NULL); | ||
888 | clist_free(msg_list); | ||
889 | res = MAIL_ERROR_MEMORY; | ||
890 | goto free_fetch_type; | ||
891 | } | ||
892 | clist_foreach(msg_list, (clist_func) free, NULL); | ||
893 | clist_free(msg_list); | ||
894 | |||
895 | r = mailimap_uid_fetch(get_imap_session(session), set, | ||
896 | fetch_type, &fetch_result); | ||
897 | |||
898 | mailimap_fetch_type_free(fetch_type); | ||
899 | mailimap_set_free(set); | ||
900 | |||
901 | switch (r) { | ||
902 | case MAILIMAP_NO_ERROR: | ||
903 | break; | ||
904 | default: | ||
905 | return imap_error_to_mail_error(r); | ||
906 | } | ||
907 | |||
908 | if (clist_begin(fetch_result) == NULL) { | ||
909 | res = MAIL_ERROR_FETCH; | ||
910 | goto err; | ||
911 | } | ||
912 | |||
913 | r = imap_fetch_result_to_envelop_list(fetch_result, env_list); | ||
914 | mailimap_fetch_list_free(fetch_result); | ||
915 | |||
916 | if (r != MAIL_NO_ERROR) { | ||
917 | res = MAIL_ERROR_MEMORY; | ||
918 | goto err; | ||
919 | } | ||
920 | |||
921 | return MAIL_NO_ERROR; | ||
922 | |||
923 | free_fetch_type: | ||
924 | mailimap_fetch_type_free(fetch_type); | ||
925 | err: | ||
926 | return res; | ||
927 | } | ||
928 | |||
929 | |||
930 | #if 0 | ||
931 | static int imapdriver_search_messages(mailsession * session, char * charset, | ||
932 | struct mail_search_key * key, | ||
933 | struct mail_search_result ** result) | ||
934 | { | ||
935 | struct mailimap_search_key * imap_key; | ||
936 | int r; | ||
937 | clist * imap_result; | ||
938 | clist * result_list; | ||
939 | struct mail_search_result * search_result; | ||
940 | clistiter * cur; | ||
941 | |||
942 | r = mail_search_to_imap_search(key, &imap_key); | ||
943 | if (r != MAIL_NO_ERROR) | ||
944 | return MAIL_ERROR_MEMORY; | ||
945 | |||
946 | r = mailimap_uid_search(get_imap_session(session), charset, imap_key, | ||
947 | &imap_result); | ||
948 | |||
949 | mailimap_search_key_free(imap_key); | ||
950 | |||
951 | switch (r) { | ||
952 | case MAILIMAP_NO_ERROR: | ||
953 | break; | ||
954 | default: | ||
955 | return imap_error_to_mail_error(r); | ||
956 | } | ||
957 | |||
958 | result_list = clist_new(); | ||
959 | if (result_list == NULL) | ||
960 | return MAIL_ERROR_MEMORY; | ||
961 | |||
962 | for(cur = clist_begin(imap_result) ; cur != NULL ; cur = clist_next(cur)) { | ||
963 | uint32_t val = * (uint32_t *) clist_content(cur); | ||
964 | uint32_t * new; | ||
965 | |||
966 | new = malloc(sizeof(* new)); | ||
967 | if (new == NULL) { | ||
968 | goto free_imap_result; | ||
969 | } | ||
970 | |||
971 | * new = val; | ||
972 | |||
973 | r = clist_append(result_list, new); | ||
974 | if (r != 0) { | ||
975 | free(new); | ||
976 | goto free_imap_result; | ||
977 | } | ||
978 | } | ||
979 | |||
980 | search_result = mail_search_result_new(result_list); | ||
981 | if (search_result == NULL) | ||
982 | goto free_imap_result; | ||
983 | |||
984 | mailimap_search_result_free(imap_result); | ||
985 | |||
986 | * result = search_result; | ||
987 | |||
988 | return MAIL_NO_ERROR; | ||
989 | |||
990 | free_imap_result: | ||
991 | mailimap_search_result_free(imap_result); | ||
992 | return MAIL_ERROR_MEMORY; | ||
993 | } | ||
994 | #endif | ||
995 | |||
996 | static int imapdriver_starttls(mailsession * session) | ||
997 | { | ||
998 | mailimap * imap; | ||
999 | int r; | ||
1000 | struct mailimap_capability_data * cap_data; | ||
1001 | clistiter * cur; | ||
1002 | int starttls; | ||
1003 | int fd; | ||
1004 | mailstream_low * low; | ||
1005 | mailstream_low * new_low; | ||
1006 | int capability_available; | ||
1007 | |||
1008 | imap = get_imap_session(session); | ||
1009 | |||
1010 | capability_available = FALSE; | ||
1011 | if (imap->imap_connection_info != NULL) | ||
1012 | if (imap->imap_connection_info->imap_capability != NULL) { | ||
1013 | capability_available = TRUE; | ||
1014 | cap_data = imap->imap_connection_info->imap_capability; | ||
1015 | } | ||
1016 | |||
1017 | if (!capability_available) { | ||
1018 | r = mailimap_capability(imap, &cap_data); | ||
1019 | switch (r) { | ||
1020 | case MAILIMAP_NO_ERROR: | ||
1021 | break; | ||
1022 | default: | ||
1023 | return imap_error_to_mail_error(r); | ||
1024 | } | ||
1025 | } | ||
1026 | |||
1027 | starttls = FALSE; | ||
1028 | for(cur = clist_begin(cap_data->cap_list) ; cur != NULL ; | ||
1029 | cur = clist_next(cur)) { | ||
1030 | struct mailimap_capability * cap; | ||
1031 | |||
1032 | cap = clist_content(cur); | ||
1033 | |||
1034 | if (cap->cap_type == MAILIMAP_CAPABILITY_NAME) | ||
1035 | if (strcasecmp(cap->cap_data.cap_name, "STARTTLS") == 0) { | ||
1036 | starttls = TRUE; | ||
1037 | break; | ||
1038 | } | ||
1039 | } | ||
1040 | |||
1041 | if (!capability_available) | ||
1042 | mailimap_capability_data_free(cap_data); | ||
1043 | |||
1044 | if (!starttls) | ||
1045 | return MAIL_ERROR_NO_TLS; | ||
1046 | |||
1047 | r = mailimap_starttls(imap); | ||
1048 | |||
1049 | switch (r) { | ||
1050 | case MAILIMAP_NO_ERROR: | ||
1051 | break; | ||
1052 | default: | ||
1053 | return imap_error_to_mail_error(r); | ||
1054 | } | ||
1055 | |||
1056 | low = mailstream_get_low(imap->imap_stream); | ||
1057 | fd = mailstream_low_get_fd(low); | ||
1058 | if (fd == -1) | ||
1059 | return MAIL_ERROR_STREAM; | ||
1060 | |||
1061 | new_low = mailstream_low_ssl_open(fd); | ||
1062 | if (new_low == NULL) | ||
1063 | return MAIL_ERROR_STREAM; | ||
1064 | |||
1065 | mailstream_low_free(low); | ||
1066 | mailstream_set_low(imap->imap_stream, new_low); | ||
1067 | |||
1068 | return MAIL_NO_ERROR; | ||
1069 | } | ||
1070 | |||
1071 | static int imapdriver_get_message(mailsession * session, | ||
1072 | uint32_t num, mailmessage ** result) | ||
1073 | { | ||
1074 | mailmessage * msg_info; | ||
1075 | int r; | ||
1076 | |||
1077 | msg_info = mailmessage_new(); | ||
1078 | if (msg_info == NULL) | ||
1079 | return MAIL_ERROR_MEMORY; | ||
1080 | |||
1081 | r = mailmessage_init(msg_info, session, imap_message_driver, num, 0); | ||
1082 | if (r != MAIL_NO_ERROR) { | ||
1083 | mailmessage_free(msg_info); | ||
1084 | return r; | ||
1085 | } | ||
1086 | |||
1087 | * result = msg_info; | ||
1088 | |||
1089 | return MAIL_NO_ERROR; | ||
1090 | } | ||
1091 | |||
1092 | /* Retrieve a message by UID | ||
1093 | |||
1094 | libEtPan! uid format for IMAP is "UIDVALIDITY-UID" | ||
1095 | where UIDVALIDITY and UID are decimal representation of | ||
1096 | respectively uidvalidity and uid numbers. | ||
1097 | |||
1098 | Return value: | ||
1099 | MAIL_ERROR_INVAL if uid is NULL or has an incorrect format. | ||
1100 | MAIL_ERROR_MSG_NOT_FOUND if uidvalidity has changed or uid was not found | ||
1101 | MAIL_NO_ERROR if message was found. Result is in result | ||
1102 | */ | ||
1103 | |||
1104 | static int imapdriver_get_message_by_uid(mailsession * session, | ||
1105 | const char * uid, | ||
1106 | mailmessage ** result) | ||
1107 | { | ||
1108 | uint32_t uidvalidity; | ||
1109 | uint32_t num; | ||
1110 | char * p1, * p2; | ||
1111 | mailimap * imap; | ||
1112 | |||
1113 | if (uid == NULL) | ||
1114 | return MAIL_ERROR_INVAL; | ||
1115 | |||
1116 | uidvalidity = strtoul(uid, &p1, 10); | ||
1117 | if (p1 == uid || * p1 != '-') | ||
1118 | return MAIL_ERROR_INVAL; | ||
1119 | |||
1120 | p1++; | ||
1121 | num = strtoul(p1, &p2, 10); | ||
1122 | if (p2 == p1 || * p2 != '\0') | ||
1123 | return MAIL_ERROR_INVAL; | ||
1124 | |||
1125 | imap = get_imap_session(session); | ||
1126 | if (imap->imap_selection_info->sel_uidvalidity != uidvalidity) | ||
1127 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
1128 | |||
1129 | return imapdriver_get_message(session, num, result); | ||
1130 | } | ||