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