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