f50617e34fbe2d4868dc3b5cbb75167cc6650c62
[claws.git] / src / etpan / imap-thread.c
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #ifdef HAVE_LIBETPAN
6
7 #include "imap-thread.h"
8 #include <imap.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #if defined (__NetBSD__)
12 #include <sys/socket.h>
13 #endif
14 #include <fcntl.h>
15 #include <sys/mman.h>
16 #include <sys/wait.h>
17
18 #include <gtk/gtk.h>
19 #include <log.h>
20 #include "etpan-thread-manager.h"
21 #include "utils.h"
22
23 #define DISABLE_LOG_DURING_LOGIN
24
25 static struct etpan_thread_manager * thread_manager = NULL;
26 static chash * courier_workaround_hash = NULL;
27 static chash * imap_hash = NULL;
28 static chash * session_hash = NULL;
29 static guint thread_manager_signal = 0;
30 static GIOChannel * io_channel = NULL;
31
32
33 static gboolean thread_manager_event(GIOChannel * source,
34     GIOCondition condition,
35     gpointer data)
36 {
37         etpan_thread_manager_loop(thread_manager);
38         
39         return TRUE;
40 }
41
42 void imap_logger(int direction, const char * str, size_t size) 
43 {
44         gchar buf[512];
45         strncpy(buf, str, 511);
46         buf[511] = '\0';
47         if (size < 511)
48                 buf[size] = '\0';
49         if (!strncmp(buf, "<<<<<<<", 7) 
50         ||  !strncmp(buf, ">>>>>>>", 7) 
51         ||  buf[0] == '\r' ||  buf[0] == '\n')
52                 return;
53
54         while (strstr(buf, "\r"))
55                 *strstr(buf, "\r") = ' ';
56         while (strstr(buf, "\n"))
57                 *strstr(buf, "\n") = ' ';
58
59         log_print("IMAP4%c %s\n", direction?'>':'<', buf);
60 }
61
62 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
63
64 void imap_main_init(void)
65 {
66         int fd_thread_manager;
67         
68         mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
69         mailstream_network_delay.tv_usec = 0;
70         
71         mailstream_debug = 1;
72         mailstream_logger = imap_logger;
73
74         imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
75         session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
76         courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
77         
78         thread_manager = etpan_thread_manager_new();
79         
80         fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
81         
82         io_channel = g_io_channel_unix_new(fd_thread_manager);
83         
84         thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
85                                                     thread_manager_event,
86                                                     (gpointer) NULL,
87                                                     NULL);
88 }
89
90 void imap_main_set_timeout(int sec)
91 {
92         mailstream_network_delay.tv_sec = sec;
93         mailstream_network_delay.tv_usec = 0;
94 }
95
96 void imap_main_done(void)
97 {
98         etpan_thread_manager_stop(thread_manager);
99         etpan_thread_manager_join(thread_manager);
100         
101         g_source_remove(thread_manager_signal);
102         g_io_channel_unref(io_channel);
103         
104         etpan_thread_manager_free(thread_manager);
105         
106         chash_free(courier_workaround_hash);
107         chash_free(session_hash);
108         chash_free(imap_hash);
109 }
110
111 void imap_init(Folder * folder)
112 {
113         struct etpan_thread * thread;
114         chashdatum key;
115         chashdatum value;
116         
117         thread = etpan_thread_manager_get_thread(thread_manager);
118         
119         key.data = &folder;
120         key.len = sizeof(folder);
121         value.data = thread;
122         value.len = 0;
123         
124         chash_set(imap_hash, &key, &value, NULL);
125 }
126
127 void imap_done(Folder * folder)
128 {
129         struct etpan_thread * thread;
130         chashdatum key;
131         chashdatum value;
132         int r;
133         
134         key.data = &folder;
135         key.len = sizeof(folder);
136         
137         r = chash_get(imap_hash, &key, &value);
138         if (r < 0)
139                 return;
140         
141         thread = value.data;
142         
143         etpan_thread_unbind(thread);
144         
145         chash_delete(imap_hash, &key, NULL);
146         
147         debug_print("remove thread");
148 }
149
150 static struct etpan_thread * get_thread(Folder * folder)
151 {
152         struct etpan_thread * thread;
153         chashdatum key;
154         chashdatum value;
155         
156         key.data = &folder;
157         key.len = sizeof(folder);
158         
159         chash_get(imap_hash, &key, &value);
160         thread = value.data;
161         
162         return thread;
163 }
164
165 static mailimap * get_imap(Folder * folder)
166 {
167         mailimap * imap;
168         chashdatum key;
169         chashdatum value;
170         int r;
171         
172         key.data = &folder;
173         key.len = sizeof(folder);
174         
175         r = chash_get(session_hash, &key, &value);
176         if (r < 0)
177                 return NULL;
178         
179         imap = value.data;
180         
181         return imap;
182 }
183
184
185 static void generic_cb(int cancelled, void * result, void * callback_data)
186 {
187         int * p_finished;
188         
189         p_finished = callback_data;
190
191         debug_print("generic_cb\n");
192         
193         * p_finished = 1;
194 }
195
196 static void threaded_run(Folder * folder, void * param, void * result,
197                          void (* func)(struct etpan_thread_op * ))
198 {
199         struct etpan_thread_op * op;
200         struct etpan_thread * thread;
201         int finished;
202         
203         imap_folder_ref(folder);
204
205         op = etpan_thread_op_new();
206         op->param = param;
207         op->result = result;
208         
209         op->cancellable = 0;
210         op->run = func;
211         op->callback = generic_cb;
212         op->callback_data = &finished;
213         op->cleanup = NULL;
214         
215         finished = 0;
216         
217         thread = get_thread(folder);
218         etpan_thread_op_schedule(thread, op);
219         
220         while (!finished) {
221                 gtk_main_iteration();
222         }
223         
224         etpan_thread_op_free(op);
225
226         imap_folder_unref(folder);
227 }
228
229
230 /* connect */
231
232 struct connect_param {
233         mailimap * imap;
234         const char * server;
235         int port;
236 };
237
238 struct connect_result {
239         int error;
240 };
241
242 static void connect_run(struct etpan_thread_op * op)
243 {
244         int r;
245         struct connect_param * param;
246         struct connect_result * result;
247         
248         param = op->param;
249         result = op->result;
250         
251         r = mailimap_socket_connect(param->imap,
252                                     param->server, param->port);
253         
254         result->error = r;
255 }
256
257
258 int imap_threaded_connect(Folder * folder, const char * server, int port)
259 {
260         struct connect_param param;
261         struct connect_result result;
262         chashdatum key;
263         chashdatum value;
264         mailimap * imap;
265         
266         imap = mailimap_new(0, NULL);
267         
268         key.data = &folder;
269         key.len = sizeof(folder);
270         value.data = imap;
271         value.len = 0;
272         chash_set(session_hash, &key, &value, NULL);
273         
274         param.imap = imap;
275         param.server = server;
276         param.port = port;
277         
278         threaded_run(folder, &param, &result, connect_run);
279         
280         debug_print("connect ok %i\n", result.error);
281         
282         return result.error;
283 }
284
285
286 static void connect_ssl_run(struct etpan_thread_op * op)
287 {
288         int r;
289         struct connect_param * param;
290         struct connect_result * result;
291         
292         param = op->param;
293         result = op->result;
294         
295         r = mailimap_ssl_connect(param->imap,
296                                  param->server, param->port);
297         
298         result->error = r;
299 }
300
301 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
302 {
303         struct connect_param param;
304         struct connect_result result;
305         chashdatum key;
306         chashdatum value;
307         mailimap * imap;
308         
309         imap = mailimap_new(0, NULL);
310         
311         key.data = &folder;
312         key.len = sizeof(folder);
313         value.data = imap;
314         value.len = 0;
315         chash_set(session_hash, &key, &value, NULL);
316         
317         param.imap = imap;
318         param.server = server;
319         param.port = port;
320         
321         threaded_run(folder, &param, &result, connect_ssl_run);
322         
323         debug_print("connect ok\n");
324         
325         return result.error;
326 }
327
328 struct capa_param {
329         mailimap * imap;
330 };
331
332 struct capa_result {
333         int error;
334         struct mailimap_capability_data *caps;
335 };
336
337 static void capability_run(struct etpan_thread_op * op)
338 {
339         int r;
340         struct capa_param * param;
341         struct capa_result * result;
342         struct mailimap_capability_data *caps;
343
344         param = op->param;
345         result = op->result;
346         
347         r = mailimap_capability(param->imap, &caps);
348         
349         result->error = r;
350         result->caps = caps;
351 }
352
353
354 struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
355 {
356         struct capa_param param;
357         struct capa_result result;
358         mailimap *imap;
359         
360         imap = get_imap(folder);
361         
362         param.imap = imap;
363         
364         threaded_run(folder, &param, &result, capability_run);
365         
366         debug_print("capa ok\n");
367         
368         return result.caps;
369         
370 }
371         
372 struct disconnect_param {
373         mailimap * imap;
374 };
375
376 struct disconnect_result {
377         int error;
378 };
379
380 static void disconnect_run(struct etpan_thread_op * op)
381 {
382         int r;
383         struct disconnect_param * param;
384         struct disconnect_result * result;
385         
386         param = op->param;
387         result = op->result;
388         
389         r = mailimap_logout(param->imap);
390         
391         result->error = r;
392 }
393
394 void imap_threaded_disconnect(Folder * folder)
395 {
396         struct connect_param param;
397         struct connect_result result;
398         chashdatum key;
399         chashdatum value;
400         mailimap * imap;
401         
402         imap = get_imap(folder);
403         if (imap == NULL) {
404                 debug_print("was disconnected\n");
405                 return;
406         }
407         
408         param.imap = imap;
409         
410         threaded_run(folder, &param, &result, disconnect_run);
411         
412         key.data = &folder;
413         key.len = sizeof(folder);
414         value.data = imap;
415         value.len = 0;
416         chash_delete(session_hash, &key, NULL);
417         
418         key.data = &imap;
419         key.len = sizeof(imap);
420         chash_delete(courier_workaround_hash, &key, NULL);
421         
422         mailimap_free(imap);
423         
424         debug_print("disconnect ok\n");
425 }
426
427
428 struct list_param {
429         mailimap * imap;
430         const char * base;
431         const char * wildcard;
432 };
433
434 struct list_result {
435         int error;
436         clist * list;
437 };
438
439 static void list_run(struct etpan_thread_op * op)
440 {
441         struct list_param * param;
442         struct list_result * result;
443         int r;
444         clist * list;
445         
446         param = op->param;
447         list = NULL;
448         r = mailimap_list(param->imap, param->base,
449                           param->wildcard, &list);
450         
451         result = op->result;
452         result->error = r;
453         result->list = list;
454         debug_print("imap list run - end\n");
455 }
456
457 int imap_threaded_list(Folder * folder, const char * base,
458                        const char * wildcard,
459                        clist ** p_result)
460 {
461         struct list_param param;
462         struct list_result result;
463         
464         debug_print("imap list - begin\n");
465         
466         param.imap = get_imap(folder);
467         param.base = base;
468         param.wildcard = wildcard;
469         
470         threaded_run(folder, &param, &result, list_run);
471         
472         * p_result = result.list;
473         
474         debug_print("imap list - end %p\n", result.list);
475         
476         return result.error;
477 }
478
479
480
481 struct login_param {
482         mailimap * imap;
483         const char * login;
484         const char * password;
485         const char * type;
486 };
487
488 struct login_result {
489         int error;
490 };
491
492 static void login_run(struct etpan_thread_op * op)
493 {
494         struct login_param * param;
495         struct login_result * result;
496         int r;
497 #ifdef DISABLE_LOG_DURING_LOGIN
498         int old_debug;
499 #endif
500         
501         param = op->param;
502         
503 #ifdef DISABLE_LOG_DURING_LOGIN
504         old_debug = mailstream_debug;
505         mailstream_debug = 0;
506 #endif
507         if (!strcmp(param->type, "LOGIN"))
508                 r = mailimap_login(param->imap,
509                            param->login, param->password);
510         else 
511                 r = mailimap_authenticate(param->imap,
512                         param->type, NULL, NULL, NULL,
513                         param->login, param->login,
514                         param->password, NULL);
515 #ifdef DISABLE_LOG_DURING_LOGIN
516         mailstream_debug = old_debug;
517 #endif
518         
519         result = op->result;
520         result->error = r;
521         debug_print("imap login run - end %i\n", r);
522 }
523
524 int imap_threaded_login(Folder * folder,
525                         const char * login, const char * password,
526                         const char * type)
527 {
528         struct login_param param;
529         struct login_result result;
530         
531         debug_print("imap login - begin\n");
532         
533         param.imap = get_imap(folder);
534         param.login = login;
535         param.password = password;
536         param.type = type;
537
538         threaded_run(folder, &param, &result, login_run);
539         
540         debug_print("imap login - end\n");
541         
542         return result.error;
543 }
544
545
546 struct status_param {
547         mailimap * imap;
548         const char * mb;
549         struct mailimap_status_att_list * status_att_list;
550 };
551
552 struct status_result {
553         int error;
554         struct mailimap_mailbox_data_status * data_status;
555 };
556
557 static void status_run(struct etpan_thread_op * op)
558 {
559         struct status_param * param;
560         struct status_result * result;
561         int r;
562         
563         param = op->param;
564         result = op->result;
565         
566         r = mailimap_status(param->imap, param->mb,
567                             param->status_att_list,
568                             &result->data_status);
569         
570         result->error = r;
571         debug_print("imap status run - end %i\n", r);
572 }
573
574 int imap_threaded_status(Folder * folder, const char * mb,
575                          struct mailimap_mailbox_data_status ** data_status)
576 {
577         struct status_param param;
578         struct status_result result;
579         struct mailimap_status_att_list * status_att_list;
580         
581         debug_print("imap status - begin\n");
582         
583         status_att_list = mailimap_status_att_list_new_empty();
584         mailimap_status_att_list_add(status_att_list,
585                                      MAILIMAP_STATUS_ATT_MESSAGES);
586         mailimap_status_att_list_add(status_att_list,
587                                      MAILIMAP_STATUS_ATT_RECENT);
588         mailimap_status_att_list_add(status_att_list,
589                                      MAILIMAP_STATUS_ATT_UIDNEXT);
590         mailimap_status_att_list_add(status_att_list,
591                                      MAILIMAP_STATUS_ATT_UIDVALIDITY);
592         mailimap_status_att_list_add(status_att_list,
593                                      MAILIMAP_STATUS_ATT_UNSEEN);
594         
595         param.imap = get_imap(folder);
596         param.mb = mb;
597         param.status_att_list = status_att_list;
598         
599         threaded_run(folder, &param, &result, status_run);
600         
601         debug_print("imap status - end\n");
602         
603         * data_status = result.data_status;
604         
605         return result.error;
606 }
607
608
609
610 struct noop_param {
611         mailimap * imap;
612 };
613
614 struct noop_result {
615         int error;
616 };
617
618 static void noop_run(struct etpan_thread_op * op)
619 {
620         struct noop_param * param;
621         struct noop_result * result;
622         int r;
623         
624         param = op->param;
625         r = mailimap_noop(param->imap);
626         
627         result = op->result;
628         result->error = r;
629         debug_print("imap noop run - end %i\n", r);
630 }
631
632 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
633 {
634         struct noop_param param;
635         struct noop_result result;
636         mailimap * imap;
637         
638         debug_print("imap noop - begin\n");
639         
640         imap = get_imap(folder);
641         param.imap = imap;
642         
643         threaded_run(folder, &param, &result, noop_run);
644         
645         if (imap->imap_selection_info != NULL) {
646                 * p_exists = imap->imap_selection_info->sel_exists;
647         }
648         else {
649                 * p_exists = 0;
650         }
651         
652         debug_print("imap noop - end\n");
653         
654         return result.error;
655 }
656
657
658 struct starttls_param {
659         mailimap * imap;
660 };
661
662 struct starttls_result {
663         int error;
664 };
665
666 static void starttls_run(struct etpan_thread_op * op)
667 {
668         struct starttls_param * param;
669         struct starttls_result * result;
670         int r;
671         
672         param = op->param;
673         r = mailimap_starttls(param->imap);
674         
675         result = op->result;
676         result->error = r;
677         debug_print("imap starttls run - end %i\n", r);
678         
679         if (r == 0) {
680                 mailimap *imap = param->imap;
681                 mailstream_low *plain_low = NULL;
682                 mailstream_low *tls_low = NULL;
683                 int fd = -1;
684                 
685                 plain_low = mailstream_get_low(imap->imap_stream);
686                 fd = mailstream_low_get_fd(plain_low);
687                 if (fd == -1) {
688                         debug_print("imap starttls run - can't get fd\n");
689                         result->error = MAILIMAP_ERROR_STREAM;
690                         return;
691                 }
692                 tls_low = mailstream_low_ssl_open(fd);
693                 if (tls_low == NULL) {
694                         debug_print("imap starttls run - can't ssl_open\n");
695                         result->error = MAILIMAP_ERROR_STREAM;
696                         return;
697                 }
698                 mailstream_low_free(plain_low);
699                 mailstream_set_low(imap->imap_stream, tls_low);
700         }
701 }
702
703 int imap_threaded_starttls(Folder * folder)
704 {
705         struct starttls_param param;
706         struct starttls_result result;
707         
708         debug_print("imap starttls - begin\n");
709         
710         param.imap = get_imap(folder);
711         
712         threaded_run(folder, &param, &result, starttls_run);
713         
714         debug_print("imap starttls - end\n");
715         
716         return result.error;
717 }
718
719
720
721 struct create_param {
722         mailimap * imap;
723         const char * mb;
724 };
725
726 struct create_result {
727         int error;
728 };
729
730 static void create_run(struct etpan_thread_op * op)
731 {
732         struct create_param * param;
733         struct create_result * result;
734         int r;
735         
736         param = op->param;
737         r = mailimap_create(param->imap, param->mb);
738         
739         result = op->result;
740         result->error = r;
741         debug_print("imap create run - end %i\n", r);
742 }
743
744 int imap_threaded_create(Folder * folder, const char * mb)
745 {
746         struct create_param param;
747         struct create_result result;
748         
749         debug_print("imap create - begin\n");
750         
751         param.imap = get_imap(folder);
752         param.mb = mb;
753         
754         threaded_run(folder, &param, &result, create_run);
755         
756         debug_print("imap create - end\n");
757         
758         return result.error;
759 }
760
761
762
763
764 struct rename_param {
765         mailimap * imap;
766         const char * mb;
767         const char * new_name;
768 };
769
770 struct rename_result {
771         int error;
772 };
773
774 static void rename_run(struct etpan_thread_op * op)
775 {
776         struct rename_param * param;
777         struct rename_result * result;
778         int r;
779         
780         param = op->param;
781         r = mailimap_rename(param->imap, param->mb, param->new_name);
782         
783         result = op->result;
784         result->error = r;
785         debug_print("imap rename run - end %i\n", r);
786 }
787
788 int imap_threaded_rename(Folder * folder,
789                          const char * mb, const char * new_name)
790 {
791         struct rename_param param;
792         struct rename_result result;
793         
794         debug_print("imap rename - begin\n");
795         
796         param.imap = get_imap(folder);
797         param.mb = mb;
798         param.new_name = new_name;
799         
800         threaded_run(folder, &param, &result, rename_run);
801         
802         debug_print("imap rename - end\n");
803         
804         return result.error;
805 }
806
807
808
809
810 struct delete_param {
811         mailimap * imap;
812         const char * mb;
813 };
814
815 struct delete_result {
816         int error;
817 };
818
819 static void delete_run(struct etpan_thread_op * op)
820 {
821         struct delete_param * param;
822         struct delete_result * result;
823         int r;
824         
825         param = op->param;
826         r = mailimap_delete(param->imap, param->mb);
827         
828         result = op->result;
829         result->error = r;
830         debug_print("imap delete run - end %i\n", r);
831 }
832
833 int imap_threaded_delete(Folder * folder, const char * mb)
834 {
835         struct delete_param param;
836         struct delete_result result;
837         
838         debug_print("imap delete - begin\n");
839         
840         param.imap = get_imap(folder);
841         param.mb = mb;
842         
843         threaded_run(folder, &param, &result, delete_run);
844         
845         debug_print("imap delete - end\n");
846         
847         return result.error;
848 }
849
850
851
852 struct select_param {
853         mailimap * imap;
854         const char * mb;
855 };
856
857 struct select_result {
858         int error;
859 };
860
861 static void select_run(struct etpan_thread_op * op)
862 {
863         struct select_param * param;
864         struct select_result * result;
865         int r;
866         
867         param = op->param;
868         r = mailimap_select(param->imap, param->mb);
869         
870         result = op->result;
871         result->error = r;
872         debug_print("imap select run - end %i\n", r);
873 }
874
875 int imap_threaded_select(Folder * folder, const char * mb,
876                          gint * exists, gint * recent, gint * unseen,
877                          guint32 * uid_validity)
878 {
879         struct select_param param;
880         struct select_result result;
881         mailimap * imap;
882         
883         debug_print("imap select - begin\n");
884         
885         imap = get_imap(folder);
886         param.imap = imap;
887         param.mb = mb;
888         
889         threaded_run(folder, &param, &result, select_run);
890         
891         if (result.error != MAILIMAP_NO_ERROR)
892                 return result.error;
893         
894         if (imap->imap_selection_info == NULL)
895                 return MAILIMAP_ERROR_PARSE;
896         
897         * exists = imap->imap_selection_info->sel_exists;
898         * recent = imap->imap_selection_info->sel_recent;
899         * unseen = imap->imap_selection_info->sel_unseen;
900         * uid_validity = imap->imap_selection_info->sel_uidvalidity;
901         
902         debug_print("imap select - end\n");
903         
904         return result.error;
905 }
906
907
908
909 struct examine_param {
910         mailimap * imap;
911         const char * mb;
912 };
913
914 struct examine_result {
915         int error;
916 };
917
918 static void examine_run(struct etpan_thread_op * op)
919 {
920         struct examine_param * param;
921         struct examine_result * result;
922         int r;
923         
924         param = op->param;
925         r = mailimap_examine(param->imap, param->mb);
926         
927         result = op->result;
928         result->error = r;
929         debug_print("imap examine run - end %i\n", r);
930 }
931
932 int imap_threaded_examine(Folder * folder, const char * mb,
933                           gint * exists, gint * recent, gint * unseen,
934                           guint32 * uid_validity)
935 {
936         struct examine_param param;
937         struct examine_result result;
938         mailimap * imap;
939         
940         debug_print("imap examine - begin\n");
941         
942         imap = get_imap(folder);
943         param.imap = imap;
944         param.mb = mb;
945         
946         threaded_run(folder, &param, &result, examine_run);
947         
948         if (result.error != MAILIMAP_NO_ERROR)
949                 return result.error;
950         
951         if (imap->imap_selection_info == NULL)
952                 return MAILIMAP_ERROR_PARSE;
953         
954         * exists = imap->imap_selection_info->sel_exists;
955         * recent = imap->imap_selection_info->sel_recent;
956         * unseen = imap->imap_selection_info->sel_unseen;
957         * uid_validity = imap->imap_selection_info->sel_uidvalidity;
958         
959         debug_print("imap examine - end\n");
960         
961         return result.error;
962 }
963
964
965
966
967 struct search_param {
968         mailimap * imap;
969         int type;
970         struct mailimap_set * set;
971 };
972
973 struct search_result {
974         int error;
975         clist * search_result;
976 };
977
978 static void search_run(struct etpan_thread_op * op)
979 {
980         struct search_param * param;
981         struct search_result * result;
982         int r;
983         struct mailimap_search_key * key;
984         struct mailimap_search_key * uid_key;
985         struct mailimap_search_key * search_type_key;
986         clist * search_result;
987         
988         param = op->param;
989         
990         uid_key = mailimap_search_key_new_uid(param->set);
991         
992         search_type_key = NULL;
993         switch (param->type) {
994         case IMAP_SEARCH_TYPE_SIMPLE:
995                 search_type_key = NULL;
996                 break;
997                 
998         case IMAP_SEARCH_TYPE_SEEN:
999                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1000                                                           NULL, NULL, NULL, NULL, NULL,
1001                                                           NULL, NULL, NULL, NULL, NULL,
1002                                                           NULL, NULL, NULL, NULL, 0,
1003                                                           NULL, NULL, NULL, NULL, NULL,
1004                                                           NULL, 0, NULL, NULL, NULL);
1005                 break;
1006                 
1007         case IMAP_SEARCH_TYPE_UNSEEN:
1008                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1009                                                           NULL, NULL, NULL, NULL, NULL,
1010                                                           NULL, NULL, NULL, NULL, NULL,
1011                                                           NULL, NULL, NULL, NULL, 0,
1012                                                           NULL, NULL, NULL, NULL, NULL,
1013                                                           NULL, 0, NULL, NULL, NULL);
1014                 break;
1015                 
1016         case IMAP_SEARCH_TYPE_ANSWERED:
1017                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1018                                                           NULL, NULL, NULL, NULL, NULL,
1019                                                           NULL, NULL, NULL, NULL, NULL,
1020                                                           NULL, NULL, NULL, NULL, 0,
1021                                                           NULL, NULL, NULL, NULL, NULL,
1022                                                           NULL, 0, NULL, NULL, NULL);
1023                 break;
1024                 
1025         case IMAP_SEARCH_TYPE_FLAGGED:
1026                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1027                                                           NULL, NULL, NULL, NULL, NULL,
1028                                                           NULL, NULL, NULL, NULL, NULL,
1029                                                           NULL, NULL, NULL, NULL, 0,
1030                                                           NULL, NULL, NULL, NULL, NULL,
1031                                                           NULL, 0, NULL, NULL, NULL);
1032                 break;
1033         }
1034         
1035         if (search_type_key != NULL) {
1036                 key = mailimap_search_key_new_multiple_empty();
1037                 mailimap_search_key_multiple_add(key, search_type_key);
1038                 mailimap_search_key_multiple_add(key, uid_key);
1039         }
1040         else {
1041                 key = uid_key;
1042         }
1043         
1044         r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1045         
1046         result = op->result;
1047         result->error = r;
1048         result->search_result = search_result;
1049         debug_print("imap search run - end %i\n", r);
1050 }
1051
1052 int imap_threaded_search(Folder * folder, int search_type,
1053                          struct mailimap_set * set, clist ** search_result)
1054 {
1055         struct search_param param;
1056         struct search_result result;
1057         mailimap * imap;
1058         
1059         debug_print("imap search - begin\n");
1060         
1061         imap = get_imap(folder);
1062         param.imap = imap;
1063         param.set = set;
1064         param.type = search_type;
1065         
1066         threaded_run(folder, &param, &result, search_run);
1067         
1068         if (result.error != MAILIMAP_NO_ERROR)
1069                 return result.error;
1070         
1071         debug_print("imap search - end\n");
1072         
1073         * search_result = result.search_result;
1074         
1075         return result.error;
1076 }
1077
1078
1079
1080
1081 static int
1082 uid_list_to_env_list(clist * fetch_result, carray ** result)
1083 {
1084         clistiter * cur;
1085         int r;
1086         int res;
1087         carray * tab;
1088         unsigned int i;
1089         
1090         tab = carray_new(128);
1091         if (tab == NULL) {
1092                 res = MAILIMAP_ERROR_MEMORY;
1093                 goto err;
1094         }
1095         
1096         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1097             cur = clist_next(cur)) {
1098                 struct mailimap_msg_att * msg_att;
1099                 clistiter * item_cur;
1100                 uint32_t uid;
1101                 size_t size;
1102                 uint32_t * puid;
1103                 
1104                 msg_att = clist_content(cur);
1105                 
1106                 uid = 0;
1107                 size = 0;
1108                 for(item_cur = clist_begin(msg_att->att_list) ;
1109                     item_cur != NULL ;
1110                     item_cur = clist_next(item_cur)) {
1111                         struct mailimap_msg_att_item * item;
1112                         
1113                         item = clist_content(item_cur);
1114                         
1115                         switch (item->att_type) {
1116                         case MAILIMAP_MSG_ATT_ITEM_STATIC:
1117                                 switch (item->att_data.att_static->att_type) {
1118                                 case MAILIMAP_MSG_ATT_UID:
1119                                         uid = item->att_data.att_static->att_data.att_uid;
1120                                         break;
1121                                 }
1122                         }
1123                 }
1124                 
1125                 puid = malloc(sizeof(* puid));
1126                 if (puid == NULL) {
1127                         res = MAILIMAP_ERROR_MEMORY;
1128                         goto free_list;
1129                 }
1130                 * puid = uid;
1131                         
1132                 r = carray_add(tab, puid, NULL);
1133                 if (r < 0) {
1134                         free(puid);
1135                         res = MAILIMAP_ERROR_MEMORY;
1136                         goto free_list;
1137                 }
1138         }
1139                 
1140         * result = tab;
1141
1142         return MAILIMAP_NO_ERROR;
1143   
1144  free_list:
1145         for(i = 0 ; i < carray_count(tab) ; i++)
1146                 mailmessage_free(carray_get(tab, i));
1147  err:
1148         return res;
1149 }
1150
1151 static int imap_get_messages_list(mailimap * imap,
1152                                   uint32_t first_index,
1153                                   carray ** result)
1154 {
1155         carray * env_list;
1156         int r;
1157         struct mailimap_fetch_att * fetch_att;
1158         struct mailimap_fetch_type * fetch_type;
1159         struct mailimap_set * set;
1160         clist * fetch_result;
1161         int res;
1162         
1163         set = mailimap_set_new_interval(first_index, 0);
1164         if (set == NULL) {
1165                 res = MAILIMAP_ERROR_MEMORY;
1166                 goto err;
1167         }
1168
1169         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1170         if (fetch_type == NULL) {
1171                 res = MAILIMAP_ERROR_MEMORY;
1172                 goto free_set;
1173         }
1174
1175         fetch_att = mailimap_fetch_att_new_uid();
1176         if (fetch_att == NULL) {
1177                 res = MAILIMAP_ERROR_MEMORY;
1178                 goto free_fetch_type;
1179         }
1180
1181         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1182         if (r != MAILIMAP_NO_ERROR) {
1183                 mailimap_fetch_att_free(fetch_att);
1184                 res = MAILIMAP_ERROR_MEMORY;
1185                 goto free_fetch_type;
1186         }
1187
1188         r = mailimap_uid_fetch(imap, set,
1189                                fetch_type, &fetch_result);
1190
1191         mailimap_fetch_type_free(fetch_type);
1192         mailimap_set_free(set);
1193
1194         if (r != MAILIMAP_NO_ERROR) {
1195                 res = r;
1196                 goto err;
1197         }
1198
1199         env_list = NULL;
1200         r = uid_list_to_env_list(fetch_result, &env_list);
1201         mailimap_fetch_list_free(fetch_result);
1202         
1203         * result = env_list;
1204
1205         return MAILIMAP_NO_ERROR;
1206
1207  free_fetch_type:
1208         mailimap_fetch_type_free(fetch_type);
1209  free_set:
1210         mailimap_set_free(set);
1211  err:
1212         return res;
1213 }
1214
1215
1216
1217
1218 struct fetch_uid_param {
1219         mailimap * imap;
1220         uint32_t first_index;
1221 };
1222
1223 struct fetch_uid_result {
1224         int error;
1225         carray * fetch_result;
1226 };
1227
1228 static void fetch_uid_run(struct etpan_thread_op * op)
1229 {
1230         struct fetch_uid_param * param;
1231         struct fetch_uid_result * result;
1232         carray * fetch_result;
1233         int r;
1234         
1235         param = op->param;
1236         
1237         fetch_result = NULL;
1238         r = imap_get_messages_list(param->imap, param->first_index,
1239                                    &fetch_result);
1240         
1241         result = op->result;
1242         result->error = r;
1243         result->fetch_result = fetch_result;
1244         debug_print("imap fetch_uid run - end %i\n", r);
1245 }
1246
1247 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1248                             carray ** fetch_result)
1249 {
1250         struct fetch_uid_param param;
1251         struct fetch_uid_result result;
1252         mailimap * imap;
1253         
1254         debug_print("imap fetch_uid - begin\n");
1255         
1256         imap = get_imap(folder);
1257         param.imap = imap;
1258         param.first_index = first_index;
1259         
1260         threaded_run(folder, &param, &result, fetch_uid_run);
1261         
1262         if (result.error != MAILIMAP_NO_ERROR)
1263                 return result.error;
1264         
1265         debug_print("imap fetch_uid - end\n");
1266         
1267         * fetch_result = result.fetch_result;
1268         
1269         return result.error;
1270 }
1271
1272
1273 void imap_fetch_uid_list_free(carray * uid_list)
1274 {
1275         unsigned int i;
1276         
1277         for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1278                 uint32_t * puid;
1279                 
1280                 puid = carray_get(uid_list, i);
1281                 free(puid);
1282         }
1283         carray_free(uid_list);
1284 }
1285
1286
1287
1288 static int imap_fetch(mailimap * imap,
1289                       uint32_t msg_index,
1290                       char ** result,
1291                       size_t * result_len)
1292 {
1293         int r;
1294         struct mailimap_set * set;
1295         struct mailimap_fetch_att * fetch_att;
1296         struct mailimap_fetch_type * fetch_type;
1297         clist * fetch_result;
1298         struct mailimap_msg_att * msg_att;
1299         struct mailimap_msg_att_item * msg_att_item;
1300         char * text;
1301         size_t text_length;
1302         int res;
1303         clistiter * cur;
1304         struct mailimap_section * section;
1305
1306         set = mailimap_set_new_single(msg_index);
1307         if (set == NULL) {
1308                 res = MAILIMAP_ERROR_MEMORY;
1309                 goto err;
1310         }
1311
1312         section = mailimap_section_new(NULL);
1313         if (section == NULL) {
1314                 res = MAILIMAP_ERROR_MEMORY;
1315                 goto free_set;
1316         }
1317   
1318         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1319         if (fetch_att == NULL) {
1320                 mailimap_section_free(section);
1321                 res = MAILIMAP_ERROR_MEMORY;
1322                 goto free_set;
1323         }
1324   
1325         fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1326         if (fetch_type == NULL) {
1327                 res = MAILIMAP_ERROR_MEMORY;
1328                 goto free_fetch_att;
1329         }
1330
1331         r = mailimap_uid_fetch(imap, set,
1332                                fetch_type, &fetch_result);
1333   
1334         mailimap_fetch_type_free(fetch_type);
1335         mailimap_set_free(set);
1336   
1337         switch (r) {
1338         case MAILIMAP_NO_ERROR:
1339                 break;
1340         default:
1341                 return r;
1342         }
1343   
1344         if (clist_begin(fetch_result) == NULL) {
1345                 mailimap_fetch_list_free(fetch_result);
1346                 return MAILIMAP_ERROR_FETCH;
1347         }
1348
1349         msg_att = clist_begin(fetch_result)->data;
1350
1351         text = NULL;
1352         text_length = 0;
1353
1354         for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1355             cur = clist_next(cur)) {
1356                 msg_att_item = clist_content(cur);
1357
1358                 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1359                         if (msg_att_item->att_data.att_static->att_type ==
1360                             MAILIMAP_MSG_ATT_BODY_SECTION) {
1361                                 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1362                                 /* detach */
1363                                 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1364                                 text_length =
1365                                         msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1366                         }
1367                 }
1368         }
1369
1370         mailimap_fetch_list_free(fetch_result);
1371
1372         if (text == NULL)
1373                 return MAILIMAP_ERROR_FETCH;
1374
1375         * result = text;
1376         * result_len = text_length;
1377   
1378         return MAILIMAP_NO_ERROR;
1379
1380  free_fetch_att:
1381         mailimap_fetch_att_free(fetch_att);
1382  free_set:
1383         mailimap_set_free(set);
1384  err:
1385         return res;
1386 }
1387
1388 static int imap_fetch_header(mailimap * imap,
1389                              uint32_t msg_index,
1390                              char ** result,
1391                              size_t * result_len)
1392 {
1393   int r;
1394   struct mailimap_set * set;
1395   struct mailimap_fetch_att * fetch_att;
1396   struct mailimap_fetch_type * fetch_type;
1397   clist * fetch_result;
1398   struct mailimap_msg_att * msg_att;
1399   struct mailimap_msg_att_item * msg_att_item;
1400   char * text;
1401   size_t text_length;
1402   int res;
1403   clistiter * cur;
1404   struct mailimap_section * section;
1405   
1406   set = mailimap_set_new_single(msg_index);
1407   if (set == NULL) {
1408     res = MAILIMAP_ERROR_MEMORY;
1409     goto err;
1410   }
1411
1412   section = mailimap_section_new_header();
1413   if (section == NULL) {
1414     res = MAILIMAP_ERROR_MEMORY;
1415     goto free_set;
1416   }
1417   
1418   fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1419   if (fetch_att == NULL) {
1420     mailimap_section_free(section);
1421     res = MAILIMAP_ERROR_MEMORY;
1422     goto free_set;
1423   }
1424   
1425   fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1426   if (fetch_type == NULL) {
1427     res = MAILIMAP_ERROR_MEMORY;
1428     goto free_fetch_att;
1429   }
1430
1431   r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1432   
1433   mailimap_fetch_type_free(fetch_type);
1434   mailimap_set_free(set);
1435
1436   switch (r) {
1437   case MAILIMAP_NO_ERROR:
1438     break;
1439   default:
1440     return r;
1441   }
1442
1443   if (clist_begin(fetch_result) == NULL) {
1444     mailimap_fetch_list_free(fetch_result);
1445     return MAILIMAP_ERROR_FETCH;
1446   }
1447
1448   msg_att = clist_begin(fetch_result)->data;
1449
1450   text = NULL;
1451   text_length = 0;
1452
1453   for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1454       cur = clist_next(cur)) {
1455     msg_att_item = clist_content(cur);
1456
1457     if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1458       if (msg_att_item->att_data.att_static->att_type ==
1459           MAILIMAP_MSG_ATT_BODY_SECTION) {
1460         text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1461         msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1462         text_length =
1463           msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1464       }
1465     }
1466   }
1467
1468   mailimap_fetch_list_free(fetch_result);
1469
1470   if (text == NULL)
1471     return MAILIMAP_ERROR_FETCH;
1472
1473   * result = text;
1474   * result_len = text_length;
1475
1476   return MAILIMAP_NO_ERROR;
1477
1478  free_fetch_att:
1479   mailimap_fetch_att_free(fetch_att);
1480  free_set:
1481   mailimap_set_free(set);
1482  err:
1483   return res;
1484 }
1485
1486
1487
1488 struct fetch_content_param {
1489         mailimap * imap;
1490         uint32_t msg_index;
1491         const char * filename;
1492         int with_body;
1493 };
1494
1495 struct fetch_content_result {
1496         int error;
1497 };
1498
1499 static void fetch_content_run(struct etpan_thread_op * op)
1500 {
1501         struct fetch_content_param * param;
1502         struct fetch_content_result * result;
1503         char * content;
1504         size_t content_size;
1505         int r;
1506         int fd;
1507         FILE * f;
1508         
1509         param = op->param;
1510         
1511         content = NULL;
1512         content_size = 0;
1513         if (param->with_body)
1514                 r = imap_fetch(param->imap, param->msg_index,
1515                                &content, &content_size);
1516         else
1517                 r = imap_fetch_header(param->imap, param->msg_index,
1518                                       &content, &content_size);
1519         
1520         result = op->result;
1521         result->error = r;
1522         
1523         if (r == MAILIMAP_NO_ERROR) {
1524                 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1525                 if (fd < 0) {
1526                         result->error = MAILIMAP_ERROR_FETCH;
1527                         goto free;
1528                 }
1529                 
1530                 f = fdopen(fd, "wb");
1531                 if (f == NULL) {
1532                         result->error = MAILIMAP_ERROR_FETCH;
1533                         goto close;
1534                 }
1535                 
1536                 r = fwrite(content, 1, content_size, f);
1537                 if (r == 0) {
1538                         goto fclose;
1539                 }
1540                 
1541                 r = fclose(f);
1542                 if (r == EOF) {
1543                         g_unlink(param->filename);
1544                         goto close;
1545                 }
1546                 goto free;
1547                 
1548         fclose:
1549                 fclose(f);
1550                 goto unlink;
1551         close:
1552                 close(fd);
1553         unlink:
1554                 g_unlink(param->filename);
1555         
1556         free:
1557                 if (mmap_string_unref(content) != 0)
1558                         free(content);
1559         }
1560         
1561         debug_print("imap fetch_content run - end %i\n", r);
1562 }
1563
1564 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1565                                 int with_body,
1566                                 const char * filename)
1567 {
1568         struct fetch_content_param param;
1569         struct fetch_content_result result;
1570         mailimap * imap;
1571         
1572         debug_print("imap fetch_content - begin\n");
1573         
1574         imap = get_imap(folder);
1575         param.imap = imap;
1576         param.msg_index = msg_index;
1577         param.filename = filename;
1578         param.with_body = with_body;
1579         
1580         threaded_run(folder, &param, &result, fetch_content_run);
1581         
1582         if (result.error != MAILIMAP_NO_ERROR)
1583                 return result.error;
1584         
1585         debug_print("imap fetch_content - end\n");
1586         
1587         return result.error;
1588 }
1589
1590
1591
1592 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1593 {
1594         int flags;
1595         clist * flag_list;
1596         clistiter * cur;
1597         
1598         flags = MSG_UNREAD;
1599         
1600         flag_list = att_dyn->att_list;
1601         if (flag_list == NULL)
1602                 return flags;
1603         
1604         for(cur = clist_begin(flag_list) ; cur != NULL ;
1605             cur = clist_next(cur)) {
1606                 struct mailimap_flag_fetch * flag_fetch;
1607                         
1608                 flag_fetch = clist_content(cur);
1609                 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1610                         flags |= MSG_NEW;
1611                 else {
1612                         switch (flag_fetch->fl_flag->fl_type) {
1613                         case MAILIMAP_FLAG_ANSWERED:
1614                                 flags |= MSG_REPLIED;
1615                                 break;
1616                         case MAILIMAP_FLAG_FLAGGED:
1617                                 flags |= MSG_MARKED;
1618                                 break;
1619                         case MAILIMAP_FLAG_DELETED:
1620                                 flags |= MSG_DELETED;
1621                                 break;
1622                         case MAILIMAP_FLAG_SEEN:
1623                                 flags &= ~MSG_UNREAD;
1624                                 flags &= ~MSG_NEW;
1625                                 break;
1626                         }
1627                 }
1628         }
1629         
1630         return flags;
1631 }
1632
1633 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1634                                  uint32_t * puid,
1635                                  char ** pheaders,
1636                                  size_t * pref_size,
1637                                  struct mailimap_msg_att_dynamic ** patt_dyn)
1638 {
1639   clistiter * item_cur;
1640   uint32_t uid;
1641   char * headers;
1642   size_t ref_size;
1643   struct mailimap_msg_att_dynamic * att_dyn;
1644
1645   uid = 0;
1646   headers = NULL;
1647   ref_size = 0;
1648   att_dyn = NULL;
1649
1650   for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1651       item_cur = clist_next(item_cur)) {
1652     struct mailimap_msg_att_item * item;
1653
1654     item = clist_content(item_cur);
1655       
1656     switch (item->att_type) {
1657     case MAILIMAP_MSG_ATT_ITEM_STATIC:
1658       switch (item->att_data.att_static->att_type) {
1659       case MAILIMAP_MSG_ATT_UID:
1660         uid = item->att_data.att_static->att_data.att_uid;
1661         break;
1662
1663       case MAILIMAP_MSG_ATT_BODY_SECTION:
1664         if (headers == NULL) {
1665           headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1666         }
1667         break;
1668       case MAILIMAP_MSG_ATT_RFC822_SIZE:
1669               ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1670               break;
1671       }
1672       break;
1673       
1674     case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1675       if (att_dyn == NULL) {
1676         att_dyn = item->att_data.att_dyn;
1677       }
1678       break;
1679     }
1680   }
1681
1682   if (puid != NULL)
1683     * puid = uid;
1684   if (pheaders != NULL)
1685     * pheaders = headers;
1686   if (pref_size != NULL)
1687     * pref_size = ref_size;
1688   if (patt_dyn != NULL)
1689     * patt_dyn = att_dyn;
1690
1691   return MAIL_NO_ERROR;
1692 }
1693
1694 static struct imap_fetch_env_info *
1695 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1696 {
1697         struct imap_fetch_env_info * info;
1698         uint32_t uid;
1699         char * headers;
1700         size_t size;
1701         struct mailimap_msg_att_dynamic * att_dyn;
1702         
1703         imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1704                               &att_dyn);
1705         
1706         info = malloc(sizeof(* info));
1707         info->uid = uid;
1708         info->headers = strdup(headers);
1709         info->size = size;
1710         info->flags = imap_flags_to_flags(att_dyn);
1711         
1712         return info;
1713 }
1714
1715 static int
1716 imap_fetch_result_to_envelop_list(clist * fetch_result,
1717                                   carray ** p_env_list)
1718 {
1719         clistiter * cur;
1720         unsigned int i;
1721         carray * env_list;
1722   
1723         i = 0;
1724   
1725         env_list = carray_new(16);
1726   
1727         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1728             cur = clist_next(cur)) {
1729                 struct mailimap_msg_att * msg_att;
1730                 struct imap_fetch_env_info * env_info;
1731     
1732                 msg_att = clist_content(cur);
1733
1734                 env_info = fetch_to_env_info(msg_att);
1735                 carray_add(env_list, env_info, NULL);
1736         }
1737   
1738         * p_env_list = env_list;
1739   
1740         return MAIL_NO_ERROR;
1741 }
1742
1743 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1744 {
1745         struct mailimap_fetch_att * fetch_att;
1746         int r;
1747         char * header;
1748         clist * hdrlist;
1749         struct mailimap_header_list * imap_hdrlist;
1750         struct mailimap_section * section;
1751
1752         hdrlist = clist_new();
1753   
1754         header = strdup("Date");
1755         r = clist_append(hdrlist, header);
1756         header = strdup("From");
1757         r = clist_append(hdrlist, header);
1758         header = strdup("To");
1759         r = clist_append(hdrlist, header);
1760         header = strdup("Cc");
1761         r = clist_append(hdrlist, header);
1762         header = strdup("Subject");
1763         r = clist_append(hdrlist, header);
1764         header = strdup("Message-ID");
1765         r = clist_append(hdrlist, header);
1766         header = strdup("References");
1767         r = clist_append(hdrlist, header);
1768         header = strdup("In-Reply-To");
1769         r = clist_append(hdrlist, header);
1770   
1771         imap_hdrlist = mailimap_header_list_new(hdrlist);
1772         section = mailimap_section_new_header_fields(imap_hdrlist);
1773         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1774         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1775   
1776         return MAIL_NO_ERROR;
1777 }
1778
1779 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1780 {
1781         struct mailimap_fetch_att * fetch_att;
1782         struct mailimap_section * section;
1783         
1784         section = mailimap_section_new_header();
1785         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1786         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1787         
1788         return MAIL_NO_ERROR;
1789 }
1790
1791 static int
1792 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1793                         carray ** p_env_list)
1794 {
1795         struct mailimap_fetch_att * fetch_att;
1796         struct mailimap_fetch_type * fetch_type;
1797         int res;
1798         clist * fetch_result;
1799         int r;
1800         carray * env_list;
1801         chashdatum key;
1802         chashdatum value;
1803         
1804         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1805   
1806         /* uid */
1807         fetch_att = mailimap_fetch_att_new_uid();
1808         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1809   
1810         /* flags */
1811         fetch_att = mailimap_fetch_att_new_flags();
1812         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1813   
1814         /* rfc822 size */
1815         fetch_att = mailimap_fetch_att_new_rfc822_size();
1816         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1817   
1818         /* headers */
1819         key.data = &imap;
1820         key.len = sizeof(imap);
1821         r = chash_get(courier_workaround_hash, &key, &value);
1822         if (r < 0)
1823                 r = imap_add_envelope_fetch_att(fetch_type);
1824         else
1825                 r = imap_add_header_fetch_att(fetch_type);
1826         
1827         r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1828         
1829         switch (r) {
1830         case MAILIMAP_NO_ERROR:
1831                 break;
1832         default:
1833                 mailimap_fetch_type_free(fetch_type);
1834                 return r;
1835         }
1836         
1837         if (clist_begin(fetch_result) == NULL) {
1838                 res = MAILIMAP_ERROR_FETCH;
1839                 goto err;
1840         }
1841         
1842         r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1843         mailimap_fetch_list_free(fetch_result);
1844         
1845         if (r != MAILIMAP_NO_ERROR) {
1846                 mailimap_fetch_type_free(fetch_type);
1847                 res = MAILIMAP_ERROR_MEMORY;
1848                 goto err;
1849         }
1850         
1851         mailimap_fetch_type_free(fetch_type);
1852         
1853         * p_env_list = env_list;
1854         
1855         return MAILIMAP_NO_ERROR;
1856   
1857  err:
1858         return res;
1859 }
1860
1861 struct fetch_env_param {
1862         mailimap * imap;
1863         struct mailimap_set * set;
1864 };
1865
1866 struct fetch_env_result {
1867         carray * fetch_env_result;
1868         int error;
1869 };
1870
1871 static void fetch_env_run(struct etpan_thread_op * op)
1872 {
1873         struct fetch_env_param * param;
1874         struct fetch_env_result * result;
1875         carray * env_list;
1876         int r;
1877         
1878         param = op->param;
1879         
1880         env_list = NULL;
1881         r = imap_get_envelopes_list(param->imap, param->set,
1882                                     &env_list);
1883         
1884         result = op->result;
1885         result->error = r;
1886         result->fetch_env_result = env_list;
1887         
1888         debug_print("imap fetch_env run - end %i\n", r);
1889 }
1890
1891 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1892                             carray ** p_env_list)
1893 {
1894         struct fetch_env_param param;
1895         struct fetch_env_result result;
1896         mailimap * imap;
1897         
1898         debug_print("imap fetch_env - begin\n");
1899         
1900         imap = get_imap(folder);
1901         param.imap = imap;
1902         param.set = set;
1903         
1904         threaded_run(folder, &param, &result, fetch_env_run);
1905         
1906         if (result.error != MAILIMAP_NO_ERROR) {
1907                 chashdatum key;
1908                 chashdatum value;
1909                 int r;
1910                 
1911                 key.data = &imap;
1912                 key.len = sizeof(imap);
1913                 r = chash_get(courier_workaround_hash, &key, &value);
1914                 if (r < 0) {
1915                         value.data = NULL;
1916                         value.len = 0;
1917                         chash_set(courier_workaround_hash, &key, &value, NULL);
1918                         
1919                         threaded_run(folder, &param, &result, fetch_env_run);
1920                 }
1921         }
1922         
1923         if (result.error != MAILIMAP_NO_ERROR)
1924                 return result.error;
1925         
1926         debug_print("imap fetch_env - end\n");
1927         
1928         * p_env_list = result.fetch_env_result;
1929         
1930         return result.error;
1931 }
1932
1933 void imap_fetch_env_free(carray * env_list)
1934 {
1935         unsigned int i;
1936         
1937         for(i = 0 ; i < carray_count(env_list) ; i ++) {
1938                 struct imap_fetch_env_info * env_info;
1939                 
1940                 env_info = carray_get(env_list, i);
1941                 free(env_info->headers);
1942                 free(env_info);
1943         }
1944         carray_free(env_list);
1945 }
1946
1947
1948
1949
1950
1951 struct append_param {
1952         mailimap * imap;
1953         const char * mailbox;
1954         const char * filename;
1955         struct mailimap_flag_list * flag_list;
1956 };
1957
1958 struct append_result {
1959         int error;
1960 };
1961
1962 static void append_run(struct etpan_thread_op * op)
1963 {
1964         struct append_param * param;
1965         struct append_result * result;
1966         int r;
1967         char * data;
1968         size_t size;
1969         struct stat stat_buf;
1970         int fd;
1971         
1972         param = op->param;
1973         result = op->result;
1974         
1975         r = stat(param->filename, &stat_buf);
1976         if (r < 0) {
1977                 result->error = MAILIMAP_ERROR_APPEND;
1978                 return;
1979         }
1980         size = stat_buf.st_size;
1981         
1982         fd = open(param->filename, O_RDONLY);
1983         if (fd < 0) {
1984                 result->error = MAILIMAP_ERROR_APPEND;
1985                 return;
1986         }
1987         
1988         data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1989         if (data == (void *) MAP_FAILED) {
1990                 close(fd);
1991                 result->error = MAILIMAP_ERROR_APPEND;
1992                 return;
1993         }
1994         
1995         r = mailimap_append(param->imap, param->mailbox,
1996                             param->flag_list, NULL,
1997                             data, size);
1998         
1999         munmap(data, size);
2000         close(fd);
2001         
2002         result->error = r;
2003         
2004         debug_print("imap append run - end %i\n", r);
2005 }
2006
2007 int imap_threaded_append(Folder * folder, const char * mailbox,
2008                          const char * filename,
2009                          struct mailimap_flag_list * flag_list)
2010 {
2011         struct append_param param;
2012         struct append_result result;
2013         mailimap * imap;
2014         
2015         debug_print("imap append - begin\n");
2016         
2017         imap = get_imap(folder);
2018         param.imap = imap;
2019         param.mailbox = mailbox;
2020         param.filename = filename;
2021         param.flag_list = flag_list;
2022         
2023         threaded_run(folder, &param, &result, append_run);
2024         
2025         if (result.error != MAILIMAP_NO_ERROR)
2026                 return result.error;
2027         
2028         debug_print("imap append - end\n");
2029         
2030         return result.error;
2031 }
2032
2033
2034
2035
2036 struct expunge_param {
2037         mailimap * imap;
2038 };
2039
2040 struct expunge_result {
2041         int error;
2042 };
2043
2044 static void expunge_run(struct etpan_thread_op * op)
2045 {
2046         struct expunge_param * param;
2047         struct expunge_result * result;
2048         int r;
2049         
2050         param = op->param;
2051         r = mailimap_expunge(param->imap);
2052         
2053         result = op->result;
2054         result->error = r;
2055         debug_print("imap expunge run - end %i\n", r);
2056 }
2057
2058 int imap_threaded_expunge(Folder * folder)
2059 {
2060         struct expunge_param param;
2061         struct expunge_result result;
2062         
2063         debug_print("imap expunge - begin\n");
2064         
2065         param.imap = get_imap(folder);
2066         
2067         threaded_run(folder, &param, &result, expunge_run);
2068         
2069         debug_print("imap expunge - end\n");
2070         
2071         return result.error;
2072 }
2073
2074
2075 struct copy_param {
2076         mailimap * imap;
2077         struct mailimap_set * set;
2078         const char * mb;
2079 };
2080
2081 struct copy_result {
2082         int error;
2083 };
2084
2085 static void copy_run(struct etpan_thread_op * op)
2086 {
2087         struct copy_param * param;
2088         struct copy_result * result;
2089         int r;
2090         
2091         param = op->param;
2092         
2093         r = mailimap_uid_copy(param->imap, param->set, param->mb);
2094         
2095         result = op->result;
2096         result->error = r;
2097         
2098         debug_print("imap copy run - end %i\n", r);
2099 }
2100
2101 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2102                        const char * mb)
2103 {
2104         struct copy_param param;
2105         struct copy_result result;
2106         mailimap * imap;
2107         
2108         debug_print("imap copy - begin\n");
2109         
2110         imap = get_imap(folder);
2111         param.imap = imap;
2112         param.set = set;
2113         param.mb = mb;
2114         
2115         threaded_run(folder, &param, &result, copy_run);
2116         
2117         if (result.error != MAILIMAP_NO_ERROR)
2118                 return result.error;
2119         
2120         debug_print("imap copy - end\n");
2121         
2122         return result.error;
2123 }
2124
2125
2126
2127 struct store_param {
2128         mailimap * imap;
2129         struct mailimap_set * set;
2130         struct mailimap_store_att_flags * store_att_flags;
2131 };
2132
2133 struct store_result {
2134         int error;
2135 };
2136
2137 static void store_run(struct etpan_thread_op * op)
2138 {
2139         struct store_param * param;
2140         struct store_result * result;
2141         int r;
2142         
2143         param = op->param;
2144         
2145         r = mailimap_uid_store(param->imap, param->set,
2146                                param->store_att_flags);
2147         
2148         result = op->result;
2149         result->error = r;
2150         
2151         debug_print("imap store run - end %i\n", r);
2152 }
2153
2154 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2155                         struct mailimap_store_att_flags * store_att_flags)
2156 {
2157         struct store_param param;
2158         struct store_result result;
2159         mailimap * imap;
2160         
2161         debug_print("imap store - begin\n");
2162         
2163         imap = get_imap(folder);
2164         param.imap = imap;
2165         param.set = set;
2166         param.store_att_flags = store_att_flags;
2167         
2168         threaded_run(folder, &param, &result, store_run);
2169         
2170         if (result.error != MAILIMAP_NO_ERROR)
2171                 return result.error;
2172         
2173         debug_print("imap store - end\n");
2174         
2175         return result.error;
2176 }
2177
2178
2179 #define ENV_BUFFER_SIZE 512
2180
2181 static void do_exec_command(int fd, const char * command,
2182                             const char * servername, uint16_t port)
2183 {
2184         int i, maxopen;
2185 #ifdef SOLARIS
2186         char env_buffer[ENV_BUFFER_SIZE];
2187 #endif
2188         
2189         if (fork() > 0) {
2190                 /* Fork again to become a child of init rather than
2191                    the etpan client. */
2192                 exit(0);
2193         }
2194   
2195 #ifdef SOLARIS
2196         if (servername)
2197                 snprintf(env_buffer, ENV_BUFFER_SIZE,
2198                          "ETPANSERVER=%s", servername);
2199         else
2200                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2201         putenv(env_buffer);
2202 #else
2203         if (servername)
2204                 setenv("ETPANSERVER", servername, 1);
2205         else
2206                 unsetenv("ETPANSERVER");
2207 #endif
2208   
2209 #ifdef SOLARIS
2210         if (port)
2211                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2212         else
2213                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2214         putenv(env_buffer);
2215 #else
2216         if (port) {
2217                 char porttext[20];
2218                 
2219                 snprintf(porttext, sizeof(porttext), "%d", port);
2220                 setenv("ETPANPORT", porttext, 1);
2221         }
2222         else {
2223                 unsetenv("ETPANPORT");
2224         }
2225 #endif
2226                 
2227         /* Not a lot we can do if there's an error other than bail. */
2228         if (dup2(fd, 0) == -1)
2229                 exit(1);
2230         if (dup2(fd, 1) == -1)
2231                 exit(1);
2232   
2233         /* Should we close stderr and reopen /dev/null? */
2234   
2235         maxopen = sysconf(_SC_OPEN_MAX);
2236         for (i=3; i < maxopen; i++)
2237                 close(i);
2238   
2239 #ifdef TIOCNOTTY
2240         /* Detach from the controlling tty if we have one. Otherwise,
2241            SSH might do something stupid like trying to use it instead
2242            of running $SSH_ASKPASS. Doh. */
2243         fd = open("/dev/tty", O_RDONLY);
2244         if (fd != -1) {
2245                 ioctl(fd, TIOCNOTTY, NULL);
2246                 close(fd);
2247         }
2248 #endif /* TIOCNOTTY */
2249
2250         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2251   
2252         /* Eep. Shouldn't reach this */
2253         exit(1);
2254 }
2255
2256 static int subcommand_connect(const char *command,
2257                               const char *servername, uint16_t port)
2258 {
2259         /* SEB unsupported on Windows */
2260         int sockfds[2];
2261         pid_t childpid;
2262   
2263         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2264                 return -1;
2265   
2266         childpid = fork();
2267         if (!childpid) {
2268                 do_exec_command(sockfds[1], command, servername, port);
2269         }
2270         else if (childpid == -1) {
2271                 close(sockfds[0]);
2272                 close(sockfds[1]);
2273                 return -1;
2274         }
2275   
2276         close(sockfds[1]);
2277   
2278         /* Reap child, leaving grandchild process to run */
2279         waitpid(childpid, NULL, 0);
2280   
2281         return sockfds[0];
2282 }
2283
2284 int socket_connect_cmd(mailimap * imap, const char * command,
2285                        const char * server, int port)
2286 {
2287         int fd;
2288         mailstream * s;
2289         int r;
2290         
2291         fd = subcommand_connect(command, server, port);
2292         if (fd < 0)
2293                 return MAILIMAP_ERROR_STREAM;
2294         
2295         s = mailstream_socket_open(fd);
2296         if (s == NULL) {
2297                 close(fd);
2298                 return MAILIMAP_ERROR_STREAM;
2299         }
2300         
2301         r = mailimap_connect(imap, s);
2302         if (r != MAILIMAP_NO_ERROR) {
2303                 mailstream_close(s);
2304                 return r;
2305         }
2306         
2307         return MAILIMAP_NO_ERROR;
2308 }
2309
2310 /* connect cmd */
2311
2312 struct connect_cmd_param {
2313         mailimap * imap;
2314         const char * command;
2315         const char * server;
2316         int port;
2317 };
2318
2319 struct connect_cmd_result {
2320         int error;
2321 };
2322
2323 static void connect_cmd_run(struct etpan_thread_op * op)
2324 {
2325         int r;
2326         struct connect_cmd_param * param;
2327         struct connect_cmd_result * result;
2328         
2329         param = op->param;
2330         result = op->result;
2331         
2332         r = socket_connect_cmd(param->imap, param->command,
2333                                param->server, param->port);
2334         
2335         result->error = r;
2336 }
2337
2338
2339 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2340                               const char * server, int port)
2341 {
2342         struct connect_cmd_param param;
2343         struct connect_cmd_result result;
2344         chashdatum key;
2345         chashdatum value;
2346         mailimap * imap;
2347         
2348         imap = mailimap_new(0, NULL);
2349         
2350         key.data = &folder;
2351         key.len = sizeof(folder);
2352         value.data = imap;
2353         value.len = 0;
2354         chash_set(session_hash, &key, &value, NULL);
2355         
2356         param.imap = imap;
2357         param.command = command;
2358         param.server = server;
2359         param.port = port;
2360         
2361         threaded_run(folder, &param, &result, connect_cmd_run);
2362         
2363         debug_print("connect_cmd ok %i\n", result.error);
2364         
2365         return result.error;
2366 }
2367 #else
2368
2369 void imap_main_init(void)
2370 {
2371 }
2372 void imap_main_done(void)
2373 {
2374 }
2375 void imap_main_set_timeout(int sec)
2376 {
2377 }
2378
2379 #endif