2007-04-27 [colin] 2.9.1cvs31
[claws.git] / src / etpan / imap-thread.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #ifdef HAVE_LIBETPAN
25
26 #include "imap-thread.h"
27 #include <imap.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
31 #include <sys/socket.h>
32 #endif
33 #include <fcntl.h>
34 #include <sys/mman.h>
35 #include <sys/wait.h>
36
37 #include <gtk/gtk.h>
38 #include <log.h>
39 #include "etpan-thread-manager.h"
40 #include "utils.h"
41 #include "mainwindow.h"
42 #include "ssl_certificate.h"
43 #include "socket.h"
44 #include "remotefolder.h"
45
46 #define DISABLE_LOG_DURING_LOGIN
47
48 static struct etpan_thread_manager * thread_manager = NULL;
49 static chash * courier_workaround_hash = NULL;
50 static chash * imap_hash = NULL;
51 static chash * session_hash = NULL;
52 static guint thread_manager_signal = 0;
53 static GIOChannel * io_channel = NULL;
54
55 static void delete_imap(Folder *folder, mailimap *imap)
56 {
57         chashdatum key;
58         chashdatum value;
59
60         key.data = &folder;
61         key.len = sizeof(folder);
62         value.data = imap;
63         value.len = 0;
64         chash_delete(session_hash, &key, NULL);
65         
66         key.data = &imap;
67         key.len = sizeof(imap);
68         chash_delete(courier_workaround_hash, &key, NULL);
69         if (imap && imap->imap_stream) {
70                 /* we don't want libetpan to logout */
71                 mailstream_close(imap->imap_stream);
72                 imap->imap_stream = NULL;
73         }
74         debug_print("removing mailimap %p\n", imap);
75         mailimap_free(imap);    
76 }
77
78 static gboolean thread_manager_event(GIOChannel * source,
79     GIOCondition condition,
80     gpointer data)
81 {
82         etpan_thread_manager_loop(thread_manager);
83         
84         return TRUE;
85 }
86
87 static void imap_logger_cmd(int direction, const char * str, size_t size) 
88 {
89         gchar *buf;
90         gchar **lines;
91         int i = 0;
92
93         if (size > 8192) {
94                 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
95                 return;
96         }
97         buf = malloc(size+1);
98         memset(buf, 0, size+1);
99         strncpy(buf, str, size);
100         buf[size] = '\0';
101
102         if (!strncmp(buf, "<<<<<<<", 7) 
103         ||  !strncmp(buf, ">>>>>>>", 7)) {
104                 free(buf);
105                 return;
106         }
107         while (strstr(buf, "\r"))
108                 *strstr(buf, "\r") = ' ';
109         while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
110                 buf[strlen(buf)-1] = '\0';
111
112         lines = g_strsplit(buf, "\n", -1);
113
114         while (lines[i] && *lines[i]) {
115                 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
116                 i++;
117         }
118         g_strfreev(lines);
119         free(buf);
120 }
121
122 static void imap_logger_fetch(int direction, const char * str, size_t size) 
123 {
124         gchar *buf;
125         gchar **lines;
126         int i = 0;
127
128         if (size > 8192) {
129                 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
130                 return;
131         }
132         
133         buf = malloc(size+1);
134         memset(buf, 0, size+1);
135         strncpy(buf, str, size);
136         buf[size] = '\0';
137         if (!strncmp(buf, "<<<<<<<", 7) 
138         ||  !strncmp(buf, ">>>>>>>", 7)) {
139                 free(buf);
140                 return;
141         }
142         while (strstr(buf, "\r"))
143                 *strstr(buf, "\r") = ' ';
144         while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
145                 buf[strlen(buf)-1] = '\0';
146
147         lines = g_strsplit(buf, "\n", -1);
148
149         if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
150                 while (lines[i] && *lines[i]) {
151                         log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
152                         i++;
153                 }
154         } else {
155                 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
156         }
157         g_strfreev(lines);
158         free(buf);
159 }
160
161 static void imap_logger_uid(int direction, const char * str, size_t size) 
162 {
163         gchar *buf;
164         gchar **lines;
165         int i = 0;
166
167         if (size > 8192) {
168                 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
169                 return;
170         }
171         buf = malloc(size+1);
172         memset(buf, 0, size+1);
173         strncpy(buf, str, size);
174         buf[size] = '\0';
175         if (!strncmp(buf, "<<<<<<<", 7) 
176         ||  !strncmp(buf, ">>>>>>>", 7)) {
177                 free(buf);
178                 return;
179         }
180         while (strstr(buf, "\r"))
181                 *strstr(buf, "\r") = ' ';
182         while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
183                 buf[strlen(buf)-1] = '\0';
184
185         lines = g_strsplit(buf, "\n", -1);
186
187         while (lines[i] && *lines[i]) {
188                 int llen = strlen(lines[i]);
189                 if (llen < 64)
190                         log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
191                 else {
192                         gchar tmp[64];
193                         strncpy2(tmp, lines[i], 63);
194                         log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
195                                   llen-64);
196                 }
197                 i++;
198         }
199         g_strfreev(lines);
200         free(buf);
201 }
202
203 static void imap_logger_append(int direction, const char * str, size_t size) 
204 {
205         gchar *buf;
206         gchar **lines;
207         int i = 0;
208
209         if (size > 8192) {
210                 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
211                 return;
212         } else if (direction == 0 && size > 64) {
213                 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
214                 return;
215         } 
216         buf = malloc(size+1);
217         memset(buf, 0, size+1);
218         strncpy(buf, str, size);
219         buf[size] = '\0';
220         if (!strncmp(buf, "<<<<<<<", 7) 
221         ||  !strncmp(buf, ">>>>>>>", 7)) {
222                 free(buf);
223                 return;
224         }
225         while (strstr(buf, "\r"))
226                 *strstr(buf, "\r") = ' ';
227         while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
228                 buf[strlen(buf)-1] = '\0';
229
230         lines = g_strsplit(buf, "\n", -1);
231
232         if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
233                 while (lines[i] && *lines[i]) {
234                         log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
235                         i++;
236                 }
237         } else {
238                 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
239         }
240         g_strfreev(lines);
241         free(buf);
242 }
243
244 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
245 static gboolean etpan_skip_ssl_cert_check = FALSE;
246 extern void mailsasl_ref(void);
247
248 void imap_main_init(gboolean skip_ssl_cert_check)
249 {
250         int fd_thread_manager;
251         
252         etpan_skip_ssl_cert_check = skip_ssl_cert_check;
253         mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
254         mailstream_network_delay.tv_usec = 0;
255         
256         mailstream_debug = 1;
257         mailstream_logger = imap_logger_cmd;
258         mailsasl_ref();
259         
260         imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
261         session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
262         courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
263         
264         thread_manager = etpan_thread_manager_new();
265         
266         fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
267         
268         io_channel = g_io_channel_unix_new(fd_thread_manager);
269         
270         thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
271                                                     thread_manager_event,
272                                                     (gpointer) NULL,
273                                                     NULL);
274 }
275
276 void imap_main_set_timeout(int sec)
277 {
278         mailstream_network_delay.tv_sec = sec;
279         mailstream_network_delay.tv_usec = 0;
280 }
281
282 void imap_main_done(void)
283 {
284         etpan_thread_manager_stop(thread_manager);
285         etpan_thread_manager_join(thread_manager);
286         
287         g_source_remove(thread_manager_signal);
288         g_io_channel_unref(io_channel);
289         
290         etpan_thread_manager_free(thread_manager);
291         
292         chash_free(courier_workaround_hash);
293         chash_free(session_hash);
294         chash_free(imap_hash);
295 }
296
297 void imap_init(Folder * folder)
298 {
299         struct etpan_thread * thread;
300         chashdatum key;
301         chashdatum value;
302         
303         thread = etpan_thread_manager_get_thread(thread_manager);
304         
305         key.data = &folder;
306         key.len = sizeof(folder);
307         value.data = thread;
308         value.len = 0;
309         
310         chash_set(imap_hash, &key, &value, NULL);
311 }
312
313 void imap_done(Folder * folder)
314 {
315         struct etpan_thread * thread;
316         chashdatum key;
317         chashdatum value;
318         int r;
319         
320         key.data = &folder;
321         key.len = sizeof(folder);
322         
323         r = chash_get(imap_hash, &key, &value);
324         if (r < 0)
325                 return;
326         
327         thread = value.data;
328         
329         etpan_thread_unbind(thread);
330         
331         chash_delete(imap_hash, &key, NULL);
332         
333         debug_print("remove thread");
334 }
335
336 static struct etpan_thread * get_thread(Folder * folder)
337 {
338         struct etpan_thread * thread;
339         chashdatum key;
340         chashdatum value;
341         
342         key.data = &folder;
343         key.len = sizeof(folder);
344         
345         chash_get(imap_hash, &key, &value);
346         thread = value.data;
347         
348         return thread;
349 }
350
351 static mailimap * get_imap(Folder * folder)
352 {
353         mailimap * imap;
354         chashdatum key;
355         chashdatum value;
356         int r;
357         
358         key.data = &folder;
359         key.len = sizeof(folder);
360         
361         r = chash_get(session_hash, &key, &value);
362         if (r < 0)
363                 return NULL;
364         
365         imap = value.data;
366         debug_print("found imap %p\n", imap);
367         return imap;
368 }
369
370
371 static void generic_cb(int cancelled, void * result, void * callback_data)
372 {
373         struct etpan_thread_op * op;
374         
375         op = (struct etpan_thread_op *) callback_data;
376
377         debug_print("generic_cb\n");
378         if (op->imap && op->imap->imap_response_info &&
379             op->imap->imap_response_info->rsp_alert) {
380                 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n", 
381                         op->imap->imap_response_info->rsp_alert);
382                 mainwindow_show_error();
383         } 
384         op->finished = 1;
385 }
386
387 static void threaded_run(Folder * folder, void * param, void * result,
388                          void (* func)(struct etpan_thread_op * ))
389 {
390         struct etpan_thread_op * op;
391         struct etpan_thread * thread;
392         
393         imap_folder_ref(folder);
394
395         op = etpan_thread_op_new();
396         
397         op->imap = get_imap(folder);
398         op->param = param;
399         op->result = result;
400         
401         op->cancellable = 0;
402         op->run = func;
403         op->callback = generic_cb;
404         op->callback_data = op;
405         op->cleanup = NULL;
406         
407         op->finished = 0;
408         
409         thread = get_thread(folder);
410         etpan_thread_op_schedule(thread, op);
411         
412         while (!op->finished) {
413                 gtk_main_iteration();
414         }
415         
416         etpan_thread_op_free(op);
417
418         imap_folder_unref(folder);
419 }
420
421
422 /* connect */
423
424 struct connect_param {
425         mailimap * imap;
426         const char * server;
427         int port;
428 };
429
430 struct connect_result {
431         int error;
432 };
433
434 #define CHECK_IMAP() {                                          \
435         if (!param->imap) {                                     \
436                 result->error = MAILIMAP_ERROR_BAD_STATE;       \
437                 return;                                         \
438         }                                                       \
439 }
440
441 static void connect_run(struct etpan_thread_op * op)
442 {
443         int r;
444         struct connect_param * param;
445         struct connect_result * result;
446         
447         param = op->param;
448         result = op->result;
449         
450         CHECK_IMAP();
451
452         r = mailimap_socket_connect(param->imap,
453                                     param->server, param->port);
454         
455         result->error = r;
456 }
457
458
459 int imap_threaded_connect(Folder * folder, const char * server, int port)
460 {
461         struct connect_param param;
462         struct connect_result result;
463         chashdatum key;
464         chashdatum value;
465         mailimap * imap, * oldimap;
466         
467         oldimap = get_imap(folder);
468
469         imap = mailimap_new(0, NULL);
470         
471         if (oldimap) {
472                 debug_print("deleting old imap %p\n", oldimap);
473                 delete_imap(folder, oldimap);
474         }
475         
476         key.data = &folder;
477         key.len = sizeof(folder);
478         value.data = imap;
479         value.len = 0;
480         chash_set(session_hash, &key, &value, NULL);
481         
482         param.imap = imap;
483         param.server = server;
484         param.port = port;
485         
486         refresh_resolvers();
487         threaded_run(folder, &param, &result, connect_run);
488         
489         debug_print("connect ok %i with imap %p\n", result.error, imap);
490         
491         return result.error;
492 }
493
494 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
495 {
496 #ifdef USE_OPENSSL
497         struct connect_param *param = (struct connect_param *)data;
498         X509 *cert = NULL;
499         
500         if (certificate == NULL || len < 0) {
501                 g_warning("no cert presented.\n");
502                 return 0;
503         }
504         cert = d2i_X509(NULL, &certificate, len);
505         if (cert == NULL) {
506                 g_warning("can't get cert\n");
507                 return 0;
508         } else if (ssl_certificate_check(cert, NULL,
509                 (gchar *)param->server, (gushort)param->port) == TRUE) {
510                 X509_free(cert);
511                 return 0;
512         } else {
513                 X509_free(cert);
514                 return -1;
515         }
516 #else
517         return 0;
518 #endif
519 }
520
521 static void connect_ssl_run(struct etpan_thread_op * op)
522 {
523         int r;
524         struct connect_param * param;
525         struct connect_result * result;
526         
527         param = op->param;
528         result = op->result;
529         
530         CHECK_IMAP();
531
532         r = mailimap_ssl_connect(param->imap,
533                                  param->server, param->port);
534         result->error = r;
535 }
536
537 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
538 {
539         struct connect_param param;
540         struct connect_result result;
541         chashdatum key;
542         chashdatum value;
543         mailimap * imap, * oldimap;
544         unsigned char *certificate = NULL;
545         int cert_len;
546         
547         oldimap = get_imap(folder);
548
549         imap = mailimap_new(0, NULL);
550         
551         if (oldimap) {
552                 debug_print("deleting old imap %p\n", oldimap);
553                 delete_imap(folder, oldimap);
554         }
555
556         key.data = &folder;
557         key.len = sizeof(folder);
558         value.data = imap;
559         value.len = 0;
560         chash_set(session_hash, &key, &value, NULL);
561         
562         param.imap = imap;
563         param.server = server;
564         param.port = port;
565         
566         refresh_resolvers();
567         threaded_run(folder, &param, &result, connect_ssl_run);
568
569         if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
570              result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
571                 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
572                 if (etpan_certificate_check(certificate, cert_len, &param) < 0)
573                         return -1;
574                 if (certificate) 
575                         free(certificate); 
576         }
577         debug_print("connect %d with imap %p\n", result.error, imap);
578         
579         return result.error;
580 }
581
582 struct capa_param {
583         mailimap * imap;
584 };
585
586 struct capa_result {
587         int error;
588         struct mailimap_capability_data *caps;
589 };
590
591 static void capability_run(struct etpan_thread_op * op)
592 {
593         int r;
594         struct capa_param * param;
595         struct capa_result * result;
596         struct mailimap_capability_data *caps;
597
598         param = op->param;
599         result = op->result;
600         
601         CHECK_IMAP();
602
603         r = mailimap_capability(param->imap, &caps);
604         
605         result->error = r;
606         result->caps = (r == 0 ? caps : NULL);
607 }
608
609
610 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
611 {
612         struct capa_param param;
613         struct capa_result result;
614         mailimap *imap;
615         
616         imap = get_imap(folder);
617         
618         param.imap = imap;
619         
620         threaded_run(folder, &param, &result, capability_run);
621         
622         debug_print("capa %d\n", result.error);
623         
624         if (ok)
625                 *ok = result.error;
626
627         return result.caps;
628         
629 }
630         
631 struct disconnect_param {
632         mailimap * imap;
633 };
634
635 struct disconnect_result {
636         int error;
637 };
638
639 static void disconnect_run(struct etpan_thread_op * op)
640 {
641         int r;
642         struct disconnect_param * param;
643         struct disconnect_result * result;
644         
645         param = op->param;
646         result = op->result;
647         
648         CHECK_IMAP();
649
650         r = mailimap_logout(param->imap);
651         
652         result->error = r;
653 }
654
655 void imap_threaded_disconnect(Folder * folder)
656 {
657         struct connect_param param;
658         struct connect_result result;
659         mailimap * imap;
660         
661         imap = get_imap(folder);
662         if (imap == NULL) {
663                 debug_print("was disconnected\n");
664                 return;
665         }
666         
667         param.imap = imap;
668         
669         threaded_run(folder, &param, &result, disconnect_run);
670         
671         if (imap == get_imap(folder)) {
672                 debug_print("deleting old imap %p\n", imap);
673                 delete_imap(folder, imap);
674         } else {
675                 debug_print("imap already deleted %p\n", imap);
676         }
677         
678         debug_print("disconnect ok\n");
679 }
680
681
682 struct list_param {
683         mailimap * imap;
684         const char * base;
685         const char * wildcard;
686         gboolean sub_only;
687 };
688
689 struct list_result {
690         int error;
691         clist * list;
692 };
693
694 static void list_run(struct etpan_thread_op * op)
695 {
696         struct list_param * param;
697         struct list_result * result;
698         int r;
699         clist * list;
700         
701         param = op->param;
702         result = op->result;
703
704         CHECK_IMAP();
705
706         list = NULL;
707         
708         if (param->base == NULL || param->wildcard == NULL) {
709                 result->list = list;
710                 result->error = -1;
711                 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
712                 return;
713         }
714         if (param->sub_only)
715                 r = mailimap_lsub(param->imap, param->base,
716                           param->wildcard, &list);
717         else
718                 r = mailimap_list(param->imap, param->base,
719                           param->wildcard, &list);
720         result->error = r;
721         result->list = list;
722         debug_print("imap list run - end\n");
723 }
724
725 int imap_threaded_list(Folder * folder, const char * base,
726                        const char * wildcard,
727                        clist ** p_result)
728 {
729         struct list_param param;
730         struct list_result result;
731         
732         debug_print("imap list - begin\n");
733         
734         param.imap = get_imap(folder);
735         param.base = base;
736         param.wildcard = wildcard;
737         param.sub_only = FALSE;
738
739         threaded_run(folder, &param, &result, list_run);
740         
741         * p_result = result.list;
742         
743         debug_print("imap list - end %p\n", result.list);
744         
745         return result.error;
746 }
747
748 int imap_threaded_lsub(Folder * folder, const char * base,
749                        const char * wildcard,
750                        clist ** p_result)
751 {
752         struct list_param param;
753         struct list_result result;
754         
755         debug_print("imap lsub - begin\n");
756         
757         param.imap = get_imap(folder);
758         param.base = base;
759         param.wildcard = wildcard;
760         param.sub_only = TRUE;
761         
762         threaded_run(folder, &param, &result, list_run);
763         
764         * p_result = result.list;
765         
766         debug_print("imap lsub - end %p\n", result.list);
767         
768         return result.error;
769 }
770
771 struct subscribe_param {
772         mailimap * imap;
773         const char * mb;
774         gboolean subscribe;
775 };
776
777 struct subscribe_result {
778         int error;
779 };
780
781 static void subscribe_run(struct etpan_thread_op * op)
782 {
783         struct subscribe_param * param;
784         struct subscribe_result * result;
785         int r;
786         
787         param = op->param;
788         result = op->result;
789
790         CHECK_IMAP();
791
792         if (param->mb == NULL) {
793                 result->error = -1;
794                 debug_print("no mb\n");
795                 return;
796         }
797         if (param->subscribe)
798                 r = mailimap_subscribe(param->imap, param->mb);
799         else
800                 r = mailimap_unsubscribe(param->imap, param->mb);
801         result->error = r;
802         debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
803 }
804
805 int imap_threaded_subscribe(Folder * folder, const char * mb,
806                        gboolean subscribe)
807 {
808         struct subscribe_param param;
809         struct subscribe_result result;
810         
811         debug_print("imap list - begin\n");
812         
813         param.imap = get_imap(folder);
814         param.mb = mb;
815         param.subscribe = subscribe;
816
817         threaded_run(folder, &param, &result, subscribe_run);
818         
819         return result.error;
820 }
821
822 struct login_param {
823         mailimap * imap;
824         const char * login;
825         const char * password;
826         const char * type;
827         const char * server;
828 };
829
830 struct login_result {
831         int error;
832 };
833
834 static void login_run(struct etpan_thread_op * op)
835 {
836         struct login_param * param;
837         struct login_result * result;
838         int r;
839 #ifdef DISABLE_LOG_DURING_LOGIN
840         int old_debug;
841 #endif
842         
843         param = op->param;
844         result = op->result;
845
846         CHECK_IMAP();
847
848 #ifdef DISABLE_LOG_DURING_LOGIN
849         old_debug = mailstream_debug;
850         mailstream_debug = 0;
851 #endif
852         if (!strcmp(param->type, "LOGIN"))
853                 r = mailimap_login(param->imap,
854                            param->login, param->password);
855         else if (!strcmp(param->type, "GSSAPI"))
856                 r = mailimap_authenticate(param->imap,
857                         param->type, param->server, NULL, NULL,
858                         param->login, param->login,
859                         param->password, NULL);
860         else 
861                 r = mailimap_authenticate(param->imap,
862                         param->type, NULL, NULL, NULL,
863                         param->login, param->login,
864                         param->password, NULL);
865 #ifdef DISABLE_LOG_DURING_LOGIN
866         mailstream_debug = old_debug;
867 #endif
868         
869         result->error = r;
870         debug_print("imap login run - end %i\n", r);
871 }
872
873 int imap_threaded_login(Folder * folder,
874                         const char * login, const char * password,
875                         const char * type)
876 {
877         struct login_param param;
878         struct login_result result;
879         
880         debug_print("imap login - begin\n");
881         
882         param.imap = get_imap(folder);
883         param.login = login;
884         param.password = password;
885         param.type = type;
886         if (folder && folder->account)
887                 param.server = folder->account->recv_server;
888         else
889                 param.server = NULL;
890
891         threaded_run(folder, &param, &result, login_run);
892         
893         debug_print("imap login - end\n");
894         
895         return result.error;
896 }
897
898
899 struct status_param {
900         mailimap * imap;
901         const char * mb;
902         struct mailimap_status_att_list * status_att_list;
903 };
904
905 struct status_result {
906         int error;
907         struct mailimap_mailbox_data_status * data_status;
908 };
909
910 static void status_run(struct etpan_thread_op * op)
911 {
912         struct status_param * param;
913         struct status_result * result;
914         int r;
915         
916         param = op->param;
917         result = op->result;
918         
919         CHECK_IMAP();
920
921         r = mailimap_status(param->imap, param->mb,
922                             param->status_att_list,
923                             &result->data_status);
924         
925         result->error = r;
926         debug_print("imap status run - end %i\n", r);
927 }
928
929 int imap_threaded_status(Folder * folder, const char * mb,
930                          struct mailimap_mailbox_data_status ** data_status,
931                          guint mask)
932 {
933         struct status_param param;
934         struct status_result result;
935         struct mailimap_status_att_list * status_att_list;
936         
937         debug_print("imap status - begin\n");
938         
939         status_att_list = mailimap_status_att_list_new_empty();
940         if (mask & 1 << 0) {
941                 mailimap_status_att_list_add(status_att_list,
942                                      MAILIMAP_STATUS_ATT_MESSAGES);
943         }
944         if (mask & 1 << 1) {
945                 mailimap_status_att_list_add(status_att_list,
946                                      MAILIMAP_STATUS_ATT_RECENT);
947         }
948         if (mask & 1 << 2) {
949                 mailimap_status_att_list_add(status_att_list,
950                                      MAILIMAP_STATUS_ATT_UIDNEXT);
951         }
952         if (mask & 1 << 3) {
953                 mailimap_status_att_list_add(status_att_list,
954                                      MAILIMAP_STATUS_ATT_UIDVALIDITY);
955         }
956         if (mask & 1 << 4) {
957                 mailimap_status_att_list_add(status_att_list,
958                                      MAILIMAP_STATUS_ATT_UNSEEN);
959         }
960         param.imap = get_imap(folder);
961         param.mb = mb;
962         param.status_att_list = status_att_list;
963         
964         threaded_run(folder, &param, &result, status_run);
965         
966         debug_print("imap status - end\n");
967         
968         * data_status = result.data_status;
969         
970         mailimap_status_att_list_free(status_att_list);
971         
972         return result.error;
973 }
974
975
976
977 struct noop_param {
978         mailimap * imap;
979 };
980
981 struct noop_result {
982         int error;
983 };
984
985 static void noop_run(struct etpan_thread_op * op)
986 {
987         struct noop_param * param;
988         struct noop_result * result;
989         int r;
990         
991         param = op->param;
992         result = op->result;
993
994         CHECK_IMAP();
995
996         r = mailimap_noop(param->imap);
997         
998         result->error = r;
999         debug_print("imap noop run - end %i\n", r);
1000 }
1001
1002 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
1003 {
1004         struct noop_param param;
1005         struct noop_result result;
1006         mailimap * imap;
1007         
1008         debug_print("imap noop - begin\n");
1009         
1010         imap = get_imap(folder);
1011         param.imap = imap;
1012
1013         threaded_run(folder, &param, &result, noop_run);
1014         
1015         if (imap && imap->imap_selection_info != NULL) {
1016                 * p_exists = imap->imap_selection_info->sel_exists;
1017         }
1018         else {
1019                 * p_exists = 0;
1020         }
1021         
1022         debug_print("imap noop - end\n");
1023         
1024         return result.error;
1025 }
1026
1027
1028 struct starttls_result {
1029         int error;
1030 };
1031
1032 static void starttls_run(struct etpan_thread_op * op)
1033 {
1034         struct connect_param * param;
1035         struct starttls_result * result;
1036         int r;
1037
1038         param = op->param;
1039         result = op->result;
1040
1041         CHECK_IMAP();
1042
1043         r = mailimap_starttls(param->imap);
1044         
1045         result->error = r;
1046         debug_print("imap starttls run - end %i\n", r);
1047         
1048         if (r == 0) {
1049                 mailimap *imap = param->imap;
1050                 mailstream_low *plain_low = NULL;
1051                 mailstream_low *tls_low = NULL;
1052                 int fd = -1;
1053                 
1054                 plain_low = mailstream_get_low(imap->imap_stream);
1055                 fd = mailstream_low_get_fd(plain_low);
1056                 if (fd == -1) {
1057                         debug_print("imap starttls run - can't get fd\n");
1058                         result->error = MAILIMAP_ERROR_STREAM;
1059                         return;
1060                 }
1061
1062                 tls_low = mailstream_low_tls_open(fd);
1063                 if (tls_low == NULL) {
1064                         debug_print("imap starttls run - can't tls_open\n");
1065                         result->error = MAILIMAP_ERROR_STREAM;
1066                         return;
1067                 }
1068                 mailstream_low_free(plain_low);
1069                 mailstream_set_low(imap->imap_stream, tls_low);
1070         }
1071 }
1072
1073 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1074 {
1075         struct connect_param param;
1076         struct starttls_result result;
1077         int cert_len;
1078         unsigned char *certificate;
1079         
1080         debug_print("imap starttls - begin\n");
1081         
1082         param.imap = get_imap(folder);
1083         param.server = host;
1084         param.port = port;
1085         
1086         threaded_run(folder, &param, &result, starttls_run);
1087         
1088         debug_print("imap starttls - end\n");
1089
1090         if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1091                 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1092                 if (etpan_certificate_check(certificate, cert_len, &param) < 0)
1093                         result.error = MAILIMAP_ERROR_STREAM;
1094                 if (certificate) 
1095                         free(certificate); 
1096         }       
1097         return result.error;
1098 }
1099
1100
1101
1102 struct create_param {
1103         mailimap * imap;
1104         const char * mb;
1105 };
1106
1107 struct create_result {
1108         int error;
1109 };
1110
1111 static void create_run(struct etpan_thread_op * op)
1112 {
1113         struct create_param * param;
1114         struct create_result * result;
1115         int r;
1116         
1117         param = op->param;
1118         result = op->result;
1119
1120         CHECK_IMAP();
1121
1122         r = mailimap_create(param->imap, param->mb);
1123         
1124         result->error = r;
1125         debug_print("imap create run - end %i\n", r);
1126 }
1127
1128 int imap_threaded_create(Folder * folder, const char * mb)
1129 {
1130         struct create_param param;
1131         struct create_result result;
1132         
1133         debug_print("imap create - begin\n");
1134         
1135         param.imap = get_imap(folder);
1136         param.mb = mb;
1137         
1138         threaded_run(folder, &param, &result, create_run);
1139         
1140         debug_print("imap create - end\n");
1141         
1142         return result.error;
1143 }
1144
1145
1146
1147
1148 struct rename_param {
1149         mailimap * imap;
1150         const char * mb;
1151         const char * new_name;
1152 };
1153
1154 struct rename_result {
1155         int error;
1156 };
1157
1158 static void rename_run(struct etpan_thread_op * op)
1159 {
1160         struct rename_param * param;
1161         struct rename_result * result;
1162         int r;
1163         
1164         param = op->param;
1165         result = op->result;
1166
1167         CHECK_IMAP();
1168
1169         r = mailimap_rename(param->imap, param->mb, param->new_name);
1170         
1171         result->error = r;
1172         debug_print("imap rename run - end %i\n", r);
1173 }
1174
1175 int imap_threaded_rename(Folder * folder,
1176                          const char * mb, const char * new_name)
1177 {
1178         struct rename_param param;
1179         struct rename_result result;
1180         
1181         debug_print("imap rename - begin\n");
1182         
1183         param.imap = get_imap(folder);
1184         param.mb = mb;
1185         param.new_name = new_name;
1186         
1187         threaded_run(folder, &param, &result, rename_run);
1188         
1189         debug_print("imap rename - end\n");
1190         
1191         return result.error;
1192 }
1193
1194
1195
1196
1197 struct delete_param {
1198         mailimap * imap;
1199         const char * mb;
1200 };
1201
1202 struct delete_result {
1203         int error;
1204 };
1205
1206 static void delete_run(struct etpan_thread_op * op)
1207 {
1208         struct delete_param * param;
1209         struct delete_result * result;
1210         int r;
1211         
1212         param = op->param;
1213         result = op->result;
1214
1215         CHECK_IMAP();
1216
1217         r = mailimap_delete(param->imap, param->mb);
1218         
1219         result->error = r;
1220         debug_print("imap delete run - end %i\n", r);
1221 }
1222
1223 int imap_threaded_delete(Folder * folder, const char * mb)
1224 {
1225         struct delete_param param;
1226         struct delete_result result;
1227         
1228         debug_print("imap delete - begin\n");
1229         
1230         param.imap = get_imap(folder);
1231         param.mb = mb;
1232         
1233         threaded_run(folder, &param, &result, delete_run);
1234         
1235         debug_print("imap delete - end\n");
1236         
1237         return result.error;
1238 }
1239
1240
1241
1242 struct select_param {
1243         mailimap * imap;
1244         const char * mb;
1245 };
1246
1247 struct select_result {
1248         int error;
1249 };
1250
1251 static void select_run(struct etpan_thread_op * op)
1252 {
1253         struct select_param * param;
1254         struct select_result * result;
1255         int r;
1256         
1257         param = op->param;
1258         result = op->result;
1259
1260         CHECK_IMAP();
1261
1262         r = mailimap_select(param->imap, param->mb);
1263         
1264         result->error = r;
1265         debug_print("imap select run - end %i\n", r);
1266 }
1267
1268 int imap_threaded_select(Folder * folder, const char * mb,
1269                          gint * exists, gint * recent, gint * unseen,
1270                          guint32 * uid_validity)
1271 {
1272         struct select_param param;
1273         struct select_result result;
1274         mailimap * imap;
1275         
1276         debug_print("imap select - begin\n");
1277         
1278         imap = get_imap(folder);
1279         param.imap = imap;
1280         param.mb = mb;
1281         
1282         threaded_run(folder, &param, &result, select_run);
1283         
1284         if (result.error != MAILIMAP_NO_ERROR)
1285                 return result.error;
1286         
1287         if (!imap || imap->imap_selection_info == NULL)
1288                 return MAILIMAP_ERROR_PARSE;
1289         
1290         * exists = imap->imap_selection_info->sel_exists;
1291         * recent = imap->imap_selection_info->sel_recent;
1292         * unseen = imap->imap_selection_info->sel_unseen;
1293         * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1294         
1295         debug_print("imap select - end\n");
1296         
1297         return result.error;
1298 }
1299
1300
1301
1302 struct examine_param {
1303         mailimap * imap;
1304         const char * mb;
1305 };
1306
1307 struct examine_result {
1308         int error;
1309 };
1310
1311 static void examine_run(struct etpan_thread_op * op)
1312 {
1313         struct examine_param * param;
1314         struct examine_result * result;
1315         int r;
1316         
1317         param = op->param;
1318         result = op->result;
1319
1320         CHECK_IMAP();
1321
1322         r = mailimap_examine(param->imap, param->mb);
1323         
1324         result->error = r;
1325         debug_print("imap examine run - end %i\n", r);
1326 }
1327
1328 int imap_threaded_examine(Folder * folder, const char * mb,
1329                           gint * exists, gint * recent, gint * unseen,
1330                           guint32 * uid_validity)
1331 {
1332         struct examine_param param;
1333         struct examine_result result;
1334         mailimap * imap;
1335         
1336         debug_print("imap examine - begin\n");
1337         
1338         imap = get_imap(folder);
1339         param.imap = imap;
1340         param.mb = mb;
1341         
1342         threaded_run(folder, &param, &result, examine_run);
1343         
1344         if (result.error != MAILIMAP_NO_ERROR)
1345                 return result.error;
1346         
1347         if (!imap || imap->imap_selection_info == NULL)
1348                 return MAILIMAP_ERROR_PARSE;
1349         
1350         * exists = imap->imap_selection_info->sel_exists;
1351         * recent = imap->imap_selection_info->sel_recent;
1352         * unseen = imap->imap_selection_info->sel_unseen;
1353         * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1354         
1355         debug_print("imap examine - end\n");
1356         
1357         return result.error;
1358 }
1359
1360
1361
1362
1363 struct search_param {
1364         mailimap * imap;
1365         int type;
1366         struct mailimap_set * set;
1367 };
1368
1369 struct search_result {
1370         int error;
1371         clist * search_result;
1372 };
1373
1374 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1375 {
1376         return mailimap_set_item_new(orig->set_first, orig->set_last);
1377 }
1378
1379 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1380 {
1381         clist *list = orig ? orig->set_list : NULL;
1382         clist *newlist = clist_new();
1383         clistiter *cur;
1384         
1385         if (!orig)
1386                 return NULL;
1387         for (cur = clist_begin(list); cur; cur = clist_next(cur))
1388                 clist_append(newlist, 
1389                         sc_mailimap_set_item_copy(
1390                         (struct mailimap_set_item *)clist_content(cur)));
1391         return mailimap_set_new(newlist);
1392 }
1393
1394 static void search_run(struct etpan_thread_op * op)
1395 {
1396         struct search_param * param;
1397         struct search_result * result;
1398         int r;
1399         struct mailimap_search_key * key = NULL;
1400         struct mailimap_search_key * uid_key = NULL;
1401         struct mailimap_search_key * search_type_key;
1402         clist * search_result;
1403         
1404         param = op->param;
1405         result = op->result;
1406
1407         CHECK_IMAP();
1408
1409         /* we copy the mailimap_set because freeing the key is recursive */
1410         if (param->set != NULL) {
1411                 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1412         } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1413                 uid_key = mailimap_search_key_new_all();
1414         }
1415         search_type_key = NULL;
1416         switch (param->type) {
1417         case IMAP_SEARCH_TYPE_SIMPLE:
1418                 search_type_key = NULL;
1419                 break;
1420                 
1421         case IMAP_SEARCH_TYPE_SEEN:
1422                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1423                                                           NULL, NULL, NULL, NULL, NULL,
1424                                                           NULL, NULL, NULL, NULL, NULL,
1425                                                           NULL, NULL, NULL, NULL, 0,
1426                                                           NULL, NULL, NULL, NULL, NULL,
1427                                                           NULL, 0, NULL, NULL, NULL);
1428                 break;
1429                 
1430         case IMAP_SEARCH_TYPE_UNSEEN:
1431                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1432                                                           NULL, NULL, NULL, NULL, NULL,
1433                                                           NULL, NULL, NULL, NULL, NULL,
1434                                                           NULL, NULL, NULL, NULL, 0,
1435                                                           NULL, NULL, NULL, NULL, NULL,
1436                                                           NULL, 0, NULL, NULL, NULL);
1437                 break;
1438                 
1439         case IMAP_SEARCH_TYPE_ANSWERED:
1440                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1441                                                           NULL, NULL, NULL, NULL, NULL,
1442                                                           NULL, NULL, NULL, NULL, NULL,
1443                                                           NULL, NULL, NULL, NULL, 0,
1444                                                           NULL, NULL, NULL, NULL, NULL,
1445                                                           NULL, 0, NULL, NULL, NULL);
1446                 break;
1447                 
1448         case IMAP_SEARCH_TYPE_FLAGGED:
1449                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1450                                                           NULL, NULL, NULL, NULL, NULL,
1451                                                           NULL, NULL, NULL, NULL, NULL,
1452                                                           NULL, NULL, NULL, NULL, 0,
1453                                                           NULL, NULL, NULL, NULL, NULL,
1454                                                           NULL, 0, NULL, NULL, NULL);
1455                 break;
1456         case IMAP_SEARCH_TYPE_DELETED:
1457                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1458                                                           NULL, NULL, NULL, NULL, NULL,
1459                                                           NULL, NULL, NULL, NULL, NULL,
1460                                                           NULL, NULL, NULL, NULL, 0,
1461                                                           NULL, NULL, NULL, NULL, NULL,
1462                                                           NULL, 0, NULL, NULL, NULL);
1463                 break;
1464         }
1465         
1466         if (search_type_key != NULL) {
1467                 if (param->set != NULL) {
1468                         key = mailimap_search_key_new_multiple_empty();
1469                         mailimap_search_key_multiple_add(key, search_type_key);
1470                         mailimap_search_key_multiple_add(key, uid_key);
1471                 } else {
1472                         key = search_type_key;
1473                 }
1474         } else if (uid_key != NULL) {
1475                 key = uid_key;
1476         }
1477         
1478         if (key == NULL) {
1479                 g_warning("no key!");
1480                 result = op->result;
1481                 result->error = -1;
1482                 result->search_result = NULL;
1483         } else {
1484                 mailstream_logger = imap_logger_uid;
1485
1486                 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1487
1488                 mailstream_logger = imap_logger_cmd;
1489
1490                 /* free the key (with the imapset) */
1491                 mailimap_search_key_free(key);
1492
1493                 result->error = r;
1494                 result->search_result = search_result;
1495         }
1496         debug_print("imap search run - end %i\n", result->error);
1497 }
1498
1499 int imap_threaded_search(Folder * folder, int search_type,
1500                          struct mailimap_set * set, clist ** search_result)
1501 {
1502         struct search_param param;
1503         struct search_result result;
1504         mailimap * imap;
1505         
1506         debug_print("imap search - begin\n");
1507
1508         imap = get_imap(folder);
1509         param.imap = imap;
1510         param.set = set;
1511         param.type = search_type;
1512         
1513         threaded_run(folder, &param, &result, search_run);
1514         
1515         if (result.error != MAILIMAP_NO_ERROR)
1516                 return result.error;
1517         
1518         debug_print("imap search - end\n");
1519         
1520         * search_result = result.search_result;
1521         
1522         return result.error;
1523 }
1524
1525
1526
1527 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1528                                  uint32_t * puid,
1529                                  char ** pheaders,
1530                                  size_t * pref_size,
1531                                  struct mailimap_msg_att_dynamic ** patt_dyn);
1532
1533 static int
1534 result_to_uid_list(clist * fetch_result, carray ** result)
1535 {
1536         clistiter * cur;
1537         int r;
1538         int res;
1539         carray * tab;
1540         
1541         tab = carray_new(128);
1542         if (tab == NULL) {
1543                 res = MAILIMAP_ERROR_MEMORY;
1544                 goto err;
1545         }
1546         
1547         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1548             cur = clist_next(cur)) {
1549                 struct mailimap_msg_att * msg_att;
1550                 uint32_t uid;
1551                 uint32_t * puid;
1552                 
1553                 msg_att = clist_content(cur);
1554                 
1555                 uid = 0;
1556                 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1557                 
1558                 puid = malloc(sizeof(* puid));
1559                 if (puid == NULL) {
1560                         res = MAILIMAP_ERROR_MEMORY;
1561                         goto free_list;
1562                 }
1563                 * puid = uid;
1564                         
1565                 r = carray_add(tab, puid, NULL);
1566                 if (r < 0) {
1567                         free(puid);
1568                         res = MAILIMAP_ERROR_MEMORY;
1569                         goto free_list;
1570                 }
1571         }
1572                 
1573         * result = tab;
1574
1575         return MAILIMAP_NO_ERROR;
1576   
1577  free_list:
1578         imap_fetch_uid_list_free(tab);
1579  err:
1580         return res;
1581 }
1582
1583 static int imap_get_messages_list(mailimap * imap,
1584                                   uint32_t first_index,
1585                                   carray ** result)
1586 {
1587         carray * env_list;
1588         int r;
1589         struct mailimap_fetch_att * fetch_att;
1590         struct mailimap_fetch_type * fetch_type;
1591         struct mailimap_set * set;
1592         clist * fetch_result;
1593         int res;
1594         
1595         set = mailimap_set_new_interval(first_index, 0);
1596         if (set == NULL) {
1597                 res = MAILIMAP_ERROR_MEMORY;
1598                 goto err;
1599         }
1600
1601         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1602         if (fetch_type == NULL) {
1603                 res = MAILIMAP_ERROR_MEMORY;
1604                 goto free_set;
1605         }
1606
1607         fetch_att = mailimap_fetch_att_new_uid();
1608         if (fetch_att == NULL) {
1609                 res = MAILIMAP_ERROR_MEMORY;
1610                 goto free_fetch_type;
1611         }
1612
1613         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1614         if (r != MAILIMAP_NO_ERROR) {
1615                 mailimap_fetch_att_free(fetch_att);
1616                 res = MAILIMAP_ERROR_MEMORY;
1617                 goto free_fetch_type;
1618         }
1619
1620         r = mailimap_uid_fetch(imap, set,
1621                                fetch_type, &fetch_result);
1622
1623         mailimap_fetch_type_free(fetch_type);
1624         mailimap_set_free(set);
1625
1626         if (r != MAILIMAP_NO_ERROR) {
1627                 res = r;
1628                 goto err;
1629         }
1630
1631         env_list = NULL;
1632         r = result_to_uid_list(fetch_result, &env_list);
1633         mailimap_fetch_list_free(fetch_result);
1634         
1635         * result = env_list;
1636
1637         return MAILIMAP_NO_ERROR;
1638
1639  free_fetch_type:
1640         mailimap_fetch_type_free(fetch_type);
1641  free_set:
1642         mailimap_set_free(set);
1643  err:
1644         return res;
1645 }
1646
1647
1648
1649
1650 struct fetch_uid_param {
1651         mailimap * imap;
1652         uint32_t first_index;
1653 };
1654
1655 struct fetch_uid_result {
1656         int error;
1657         carray * fetch_result;
1658 };
1659
1660 static void fetch_uid_run(struct etpan_thread_op * op)
1661 {
1662         struct fetch_uid_param * param;
1663         struct fetch_uid_result * result;
1664         carray * fetch_result;
1665         int r;
1666         
1667         param = op->param;
1668         result = op->result;
1669
1670         CHECK_IMAP();
1671
1672         fetch_result = NULL;
1673         mailstream_logger = NULL;
1674         log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1675
1676         r = imap_get_messages_list(param->imap, param->first_index,
1677                                    &fetch_result);
1678         
1679         mailstream_logger = imap_logger_cmd;
1680
1681         result->error = r;
1682         result->fetch_result = fetch_result;
1683         debug_print("imap fetch_uid run - end %i\n", r);
1684 }
1685
1686 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1687                             carray ** fetch_result)
1688 {
1689         struct fetch_uid_param param;
1690         struct fetch_uid_result result;
1691         mailimap * imap;
1692         
1693         debug_print("imap fetch_uid - begin\n");
1694         
1695         imap = get_imap(folder);
1696         param.imap = imap;
1697         param.first_index = first_index;
1698         
1699         threaded_run(folder, &param, &result, fetch_uid_run);
1700         
1701         if (result.error != MAILIMAP_NO_ERROR)
1702                 return result.error;
1703         
1704         debug_print("imap fetch_uid - end\n");
1705         
1706         * fetch_result = result.fetch_result;
1707         
1708         return result.error;
1709 }
1710
1711
1712 void imap_fetch_uid_list_free(carray * uid_list)
1713 {
1714         unsigned int i;
1715         
1716         for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1717                 uint32_t * puid;
1718                 
1719                 puid = carray_get(uid_list, i);
1720                 free(puid);
1721         }
1722         carray_free(uid_list);
1723 }
1724
1725
1726
1727
1728 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn);
1729
1730 static int
1731 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1732 {
1733         clistiter * cur;
1734         int r;
1735         int res;
1736         carray * tab;
1737         
1738         tab = carray_new(128);
1739         if (tab == NULL) {
1740                 res = MAILIMAP_ERROR_MEMORY;
1741                 goto err;
1742         }
1743         
1744         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1745             cur = clist_next(cur)) {
1746                 struct mailimap_msg_att * msg_att;
1747                 uint32_t uid;
1748                 uint32_t * puid;
1749                 struct mailimap_msg_att_dynamic * att_dyn;
1750                 int flags;
1751                 int * pflags;
1752                 
1753                 msg_att = clist_content(cur);
1754                 
1755                 uid = 0;
1756                 att_dyn = NULL;
1757                 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1758                 if (uid == 0)
1759                         continue;
1760                 if (att_dyn == NULL)
1761                         continue;
1762                 
1763                 flags = imap_flags_to_flags(att_dyn);
1764                 
1765                 puid = malloc(sizeof(* puid));
1766                 if (puid == NULL) {
1767                         res = MAILIMAP_ERROR_MEMORY;
1768                         goto free_list;
1769                 }
1770                 * puid = uid;
1771                 
1772                 r = carray_add(tab, puid, NULL);
1773                 if (r < 0) {
1774                         free(puid);
1775                         res = MAILIMAP_ERROR_MEMORY;
1776                         goto free_list;
1777                 }
1778                 pflags = malloc(sizeof(* pflags));
1779                 if (pflags == NULL) {
1780                         res = MAILIMAP_ERROR_MEMORY;
1781                         goto free_list;
1782                 }
1783                 * pflags = flags;
1784                 r = carray_add(tab, pflags, NULL);
1785                 if (r < 0) {
1786                         free(pflags);
1787                         res = MAILIMAP_ERROR_MEMORY;
1788                         goto free_list;
1789                 }
1790         }
1791                 
1792         * result = tab;
1793
1794         return MAILIMAP_NO_ERROR;
1795   
1796  free_list:
1797         imap_fetch_uid_flags_list_free(tab);
1798  err:
1799         return res;
1800 }
1801
1802 static int imap_get_messages_flags_list(mailimap * imap,
1803                                         uint32_t first_index,
1804                                         carray ** result)
1805 {
1806         carray * env_list;
1807         int r;
1808         struct mailimap_fetch_att * fetch_att;
1809         struct mailimap_fetch_type * fetch_type;
1810         struct mailimap_set * set;
1811         clist * fetch_result;
1812         int res;
1813         
1814         set = mailimap_set_new_interval(first_index, 0);
1815         if (set == NULL) {
1816                 res = MAILIMAP_ERROR_MEMORY;
1817                 goto err;
1818         }
1819
1820         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1821         if (fetch_type == NULL) {
1822                 res = MAILIMAP_ERROR_MEMORY;
1823                 goto free_set;
1824         }
1825
1826         fetch_att = mailimap_fetch_att_new_flags();
1827         if (fetch_att == NULL) {
1828                 res = MAILIMAP_ERROR_MEMORY;
1829                 goto free_fetch_type;
1830         }
1831         
1832         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1833         if (r != MAILIMAP_NO_ERROR) {
1834                 mailimap_fetch_att_free(fetch_att);
1835                 res = MAILIMAP_ERROR_MEMORY;
1836                 goto free_fetch_type;
1837         }
1838         
1839         fetch_att = mailimap_fetch_att_new_uid();
1840         if (fetch_att == NULL) {
1841                 res = MAILIMAP_ERROR_MEMORY;
1842                 goto free_fetch_type;
1843         }
1844
1845         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1846         if (r != MAILIMAP_NO_ERROR) {
1847                 mailimap_fetch_att_free(fetch_att);
1848                 res = MAILIMAP_ERROR_MEMORY;
1849                 goto free_fetch_type;
1850         }
1851
1852         r = mailimap_uid_fetch(imap, set,
1853                                fetch_type, &fetch_result);
1854
1855         mailimap_fetch_type_free(fetch_type);
1856         mailimap_set_free(set);
1857
1858         if (r != MAILIMAP_NO_ERROR) {
1859                 res = r;
1860                 goto err;
1861         }
1862
1863         env_list = NULL;
1864         r = result_to_uid_flags_list(fetch_result, &env_list);
1865         mailimap_fetch_list_free(fetch_result);
1866         
1867         * result = env_list;
1868
1869         return MAILIMAP_NO_ERROR;
1870
1871  free_fetch_type:
1872         mailimap_fetch_type_free(fetch_type);
1873  free_set:
1874         mailimap_set_free(set);
1875  err:
1876         return res;
1877 }
1878
1879
1880
1881 static void fetch_uid_flags_run(struct etpan_thread_op * op)
1882 {
1883         struct fetch_uid_param * param;
1884         struct fetch_uid_result * result;
1885         carray * fetch_result;
1886         int r;
1887         
1888         param = op->param;
1889         result = op->result;
1890
1891         CHECK_IMAP();
1892
1893         fetch_result = NULL;
1894         r = imap_get_messages_flags_list(param->imap, param->first_index,
1895                                          &fetch_result);
1896         
1897         result->error = r;
1898         result->fetch_result = fetch_result;
1899         debug_print("imap fetch_uid run - end %i\n", r);
1900 }
1901
1902 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
1903                                   carray ** fetch_result)
1904 {
1905         struct fetch_uid_param param;
1906         struct fetch_uid_result result;
1907         mailimap * imap;
1908         
1909         debug_print("imap fetch_uid - begin\n");
1910         
1911         imap = get_imap(folder);
1912         param.imap = imap;
1913         param.first_index = first_index;
1914         
1915         mailstream_logger = NULL;
1916         log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
1917
1918         threaded_run(folder, &param, &result, fetch_uid_flags_run);
1919
1920         mailstream_logger = imap_logger_cmd;
1921
1922         
1923         if (result.error != MAILIMAP_NO_ERROR)
1924                 return result.error;
1925         
1926         debug_print("imap fetch_uid - end\n");
1927         
1928         * fetch_result = result.fetch_result;
1929         
1930         return result.error;
1931 }
1932
1933
1934 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
1935 {
1936         unsigned int i;
1937         
1938         for(i = 0 ; i < carray_count(uid_flags_list) ; i ++) {
1939                 void * data;
1940                 
1941                 data = carray_get(uid_flags_list, i);
1942                 free(data);
1943         }
1944         carray_free(uid_flags_list);
1945 }
1946
1947
1948
1949 static int imap_fetch(mailimap * imap,
1950                       uint32_t msg_index,
1951                       char ** result,
1952                       size_t * result_len)
1953 {
1954         int r;
1955         struct mailimap_set * set;
1956         struct mailimap_fetch_att * fetch_att;
1957         struct mailimap_fetch_type * fetch_type;
1958         clist * fetch_result;
1959         struct mailimap_msg_att * msg_att;
1960         struct mailimap_msg_att_item * msg_att_item;
1961         char * text;
1962         size_t text_length;
1963         int res;
1964         clistiter * cur;
1965         struct mailimap_section * section;
1966
1967         set = mailimap_set_new_single(msg_index);
1968         if (set == NULL) {
1969                 res = MAILIMAP_ERROR_MEMORY;
1970                 goto err;
1971         }
1972
1973         section = mailimap_section_new(NULL);
1974         if (section == NULL) {
1975                 res = MAILIMAP_ERROR_MEMORY;
1976                 goto free_set;
1977         }
1978   
1979         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1980         if (fetch_att == NULL) {
1981                 mailimap_section_free(section);
1982                 res = MAILIMAP_ERROR_MEMORY;
1983                 goto free_set;
1984         }
1985   
1986         fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1987         if (fetch_type == NULL) {
1988                 res = MAILIMAP_ERROR_MEMORY;
1989                 goto free_fetch_att;
1990         }
1991
1992         mailstream_logger = imap_logger_fetch;
1993         
1994         r = mailimap_uid_fetch(imap, set,
1995                                fetch_type, &fetch_result);
1996   
1997         mailstream_logger = imap_logger_cmd;
1998         
1999         mailimap_fetch_type_free(fetch_type);
2000         mailimap_set_free(set);
2001   
2002         switch (r) {
2003         case MAILIMAP_NO_ERROR:
2004                 break;
2005         default:
2006                 return r;
2007         }
2008   
2009         if (clist_begin(fetch_result) == NULL) {
2010                 mailimap_fetch_list_free(fetch_result);
2011                 return MAILIMAP_ERROR_FETCH;
2012         }
2013
2014         msg_att = clist_begin(fetch_result)->data;
2015
2016         text = NULL;
2017         text_length = 0;
2018
2019         for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2020             cur = clist_next(cur)) {
2021                 msg_att_item = clist_content(cur);
2022
2023                 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2024                         if (msg_att_item->att_data.att_static->att_type ==
2025                             MAILIMAP_MSG_ATT_BODY_SECTION) {
2026                                 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2027                                 /* detach */
2028                                 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2029                                 text_length =
2030                                         msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2031                         }
2032                 }
2033         }
2034
2035         mailimap_fetch_list_free(fetch_result);
2036
2037         if (text == NULL)
2038                 return MAILIMAP_ERROR_FETCH;
2039
2040         * result = text;
2041         * result_len = text_length;
2042   
2043         return MAILIMAP_NO_ERROR;
2044
2045  free_fetch_att:
2046         mailimap_fetch_att_free(fetch_att);
2047  free_set:
2048         mailimap_set_free(set);
2049  err:
2050         return res;
2051 }
2052
2053 static int imap_fetch_header(mailimap * imap,
2054                              uint32_t msg_index,
2055                              char ** result,
2056                              size_t * result_len)
2057 {
2058   int r;
2059   struct mailimap_set * set;
2060   struct mailimap_fetch_att * fetch_att;
2061   struct mailimap_fetch_type * fetch_type;
2062   clist * fetch_result;
2063   struct mailimap_msg_att * msg_att;
2064   struct mailimap_msg_att_item * msg_att_item;
2065   char * text;
2066   size_t text_length;
2067   int res;
2068   clistiter * cur;
2069   struct mailimap_section * section;
2070   
2071   set = mailimap_set_new_single(msg_index);
2072   if (set == NULL) {
2073     res = MAILIMAP_ERROR_MEMORY;
2074     goto err;
2075   }
2076
2077   section = mailimap_section_new_header();
2078   if (section == NULL) {
2079     res = MAILIMAP_ERROR_MEMORY;
2080     goto free_set;
2081   }
2082   
2083   fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2084   if (fetch_att == NULL) {
2085     mailimap_section_free(section);
2086     res = MAILIMAP_ERROR_MEMORY;
2087     goto free_set;
2088   }
2089   
2090   fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2091   if (fetch_type == NULL) {
2092     res = MAILIMAP_ERROR_MEMORY;
2093     goto free_fetch_att;
2094   }
2095
2096   r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2097   
2098   mailimap_fetch_type_free(fetch_type);
2099   mailimap_set_free(set);
2100
2101   switch (r) {
2102   case MAILIMAP_NO_ERROR:
2103     break;
2104   default:
2105     return r;
2106   }
2107
2108   if (clist_begin(fetch_result) == NULL) {
2109     mailimap_fetch_list_free(fetch_result);
2110     return MAILIMAP_ERROR_FETCH;
2111   }
2112
2113   msg_att = clist_begin(fetch_result)->data;
2114
2115   text = NULL;
2116   text_length = 0;
2117
2118   for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2119       cur = clist_next(cur)) {
2120     msg_att_item = clist_content(cur);
2121
2122     if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2123       if (msg_att_item->att_data.att_static->att_type ==
2124           MAILIMAP_MSG_ATT_BODY_SECTION) {
2125         text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2126         msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2127         text_length =
2128           msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2129       }
2130     }
2131   }
2132
2133   mailimap_fetch_list_free(fetch_result);
2134
2135   if (text == NULL)
2136     return MAILIMAP_ERROR_FETCH;
2137
2138   * result = text;
2139   * result_len = text_length;
2140
2141   return MAILIMAP_NO_ERROR;
2142
2143  free_fetch_att:
2144   mailimap_fetch_att_free(fetch_att);
2145  free_set:
2146   mailimap_set_free(set);
2147  err:
2148   return res;
2149 }
2150
2151
2152
2153 struct fetch_content_param {
2154         mailimap * imap;
2155         uint32_t msg_index;
2156         const char * filename;
2157         int with_body;
2158 };
2159
2160 struct fetch_content_result {
2161         int error;
2162 };
2163
2164 static void fetch_content_run(struct etpan_thread_op * op)
2165 {
2166         struct fetch_content_param * param;
2167         struct fetch_content_result * result;
2168         char * content;
2169         size_t content_size;
2170         int r;
2171         int fd;
2172         FILE * f;
2173         
2174         param = op->param;
2175         result = op->result;
2176
2177         CHECK_IMAP();
2178
2179         content = NULL;
2180         content_size = 0;
2181         if (param->with_body)
2182                 r = imap_fetch(param->imap, param->msg_index,
2183                                &content, &content_size);
2184         else
2185                 r = imap_fetch_header(param->imap, param->msg_index,
2186                                       &content, &content_size);
2187         
2188         result->error = r;
2189         
2190         if (r == MAILIMAP_NO_ERROR) {
2191                 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2192                 if (fd < 0) {
2193                         result->error = MAILIMAP_ERROR_FETCH;
2194                         goto free;
2195                 }
2196                 
2197                 f = fdopen(fd, "wb");
2198                 if (f == NULL) {
2199                         result->error = MAILIMAP_ERROR_FETCH;
2200                         goto close;
2201                 }
2202                 
2203                 r = fwrite(content, 1, content_size, f);
2204                 if (r == 0) {
2205                         goto fclose;
2206                 }
2207                 
2208                 r = fclose(f);
2209                 if (r == EOF) {
2210                         g_unlink(param->filename);
2211                         goto close;
2212                 }
2213                 goto free;
2214                 
2215         fclose:
2216                 fclose(f);
2217                 goto unlink;
2218         close:
2219                 close(fd);
2220         unlink:
2221                 g_unlink(param->filename);
2222         
2223         free:
2224                 if (mmap_string_unref(content) != 0)
2225                         free(content);
2226         }
2227         
2228         debug_print("imap fetch_content run - end %i\n", r);
2229 }
2230
2231 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2232                                 int with_body,
2233                                 const char * filename)
2234 {
2235         struct fetch_content_param param;
2236         struct fetch_content_result result;
2237         mailimap * imap;
2238         
2239         debug_print("imap fetch_content - begin\n");
2240         
2241         imap = get_imap(folder);
2242         param.imap = imap;
2243         param.msg_index = msg_index;
2244         param.filename = filename;
2245         param.with_body = with_body;
2246         
2247         threaded_run(folder, &param, &result, fetch_content_run);
2248         
2249         if (result.error != MAILIMAP_NO_ERROR)
2250                 return result.error;
2251         
2252         debug_print("imap fetch_content - end\n");
2253         
2254         return result.error;
2255 }
2256
2257
2258
2259 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
2260 {
2261         int flags;
2262         clist * flag_list;
2263         clistiter * cur;
2264         
2265         flags = MSG_UNREAD;
2266         
2267         flag_list = att_dyn->att_list;
2268         if (flag_list == NULL)
2269                 return flags;
2270         
2271         for(cur = clist_begin(flag_list) ; cur != NULL ;
2272             cur = clist_next(cur)) {
2273                 struct mailimap_flag_fetch * flag_fetch;
2274                         
2275                 flag_fetch = clist_content(cur);
2276                 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2277                         flags |= MSG_NEW;
2278                 else {
2279                         switch (flag_fetch->fl_flag->fl_type) {
2280                         case MAILIMAP_FLAG_ANSWERED:
2281                                 flags |= MSG_REPLIED;
2282                                 break;
2283                         case MAILIMAP_FLAG_FLAGGED:
2284                                 flags |= MSG_MARKED;
2285                                 break;
2286                         case MAILIMAP_FLAG_DELETED:
2287                                 flags |= MSG_DELETED;
2288                                 break;
2289                         case MAILIMAP_FLAG_SEEN:
2290                                 flags &= ~MSG_UNREAD;
2291                                 flags &= ~MSG_NEW;
2292                                 break;
2293                         }
2294                 }
2295         }
2296         
2297         return flags;
2298 }
2299
2300 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2301                                  uint32_t * puid,
2302                                  char ** pheaders,
2303                                  size_t * pref_size,
2304                                  struct mailimap_msg_att_dynamic ** patt_dyn)
2305 {
2306   clistiter * item_cur;
2307   uint32_t uid;
2308   char * headers;
2309   size_t ref_size;
2310   struct mailimap_msg_att_dynamic * att_dyn;
2311
2312   uid = 0;
2313   headers = NULL;
2314   ref_size = 0;
2315   att_dyn = NULL;
2316
2317   for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2318       item_cur = clist_next(item_cur)) {
2319     struct mailimap_msg_att_item * item;
2320
2321     item = clist_content(item_cur);
2322       
2323     switch (item->att_type) {
2324     case MAILIMAP_MSG_ATT_ITEM_STATIC:
2325       switch (item->att_data.att_static->att_type) {
2326       case MAILIMAP_MSG_ATT_UID:
2327         uid = item->att_data.att_static->att_data.att_uid;
2328         break;
2329
2330       case MAILIMAP_MSG_ATT_BODY_SECTION:
2331         if (headers == NULL) {
2332           headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2333         }
2334         break;
2335       case MAILIMAP_MSG_ATT_RFC822_SIZE:
2336               ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2337               break;
2338       }
2339       break;
2340       
2341     case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2342       if (att_dyn == NULL) {
2343         att_dyn = item->att_data.att_dyn;
2344       }
2345       break;
2346     }
2347   }
2348
2349   if (puid != NULL)
2350     * puid = uid;
2351   if (pheaders != NULL)
2352     * pheaders = headers;
2353   if (pref_size != NULL)
2354     * pref_size = ref_size;
2355   if (patt_dyn != NULL)
2356     * patt_dyn = att_dyn;
2357
2358   return MAIL_NO_ERROR;
2359 }
2360
2361 static struct imap_fetch_env_info *
2362 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2363 {
2364         struct imap_fetch_env_info * info;
2365         uint32_t uid;
2366         char * headers;
2367         size_t size;
2368         struct mailimap_msg_att_dynamic * att_dyn;
2369         
2370         imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2371                               &att_dyn);
2372         
2373         if (!headers)
2374                 return NULL;
2375         info = malloc(sizeof(* info));
2376         info->uid = uid;
2377         info->headers = strdup(headers);
2378         info->size = size;
2379         info->flags = imap_flags_to_flags(att_dyn);
2380         
2381         return info;
2382 }
2383
2384 static int
2385 imap_fetch_result_to_envelop_list(clist * fetch_result,
2386                                   carray ** p_env_list)
2387 {
2388         clistiter * cur;
2389         unsigned int i;
2390         carray * env_list;
2391         i = 0;
2392   
2393         env_list = carray_new(16);
2394   
2395         for(cur = clist_begin(fetch_result) ; cur != NULL ;
2396             cur = clist_next(cur)) {
2397                 struct mailimap_msg_att * msg_att;
2398                 struct imap_fetch_env_info * env_info;
2399     
2400                 msg_att = clist_content(cur);
2401
2402                 env_info = fetch_to_env_info(msg_att);
2403                 if (!env_info)
2404                         return MAILIMAP_ERROR_MEMORY;
2405                 carray_add(env_list, env_info, NULL);
2406         }
2407   
2408         * p_env_list = env_list;
2409   
2410         return MAIL_NO_ERROR;
2411 }
2412
2413 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2414 {
2415         struct mailimap_fetch_att * fetch_att;
2416         int r;
2417         char * header;
2418         clist * hdrlist;
2419         struct mailimap_header_list * imap_hdrlist;
2420         struct mailimap_section * section;
2421
2422         hdrlist = clist_new();
2423   
2424         header = strdup("Date");
2425         r = clist_append(hdrlist, header);
2426         header = strdup("From");
2427         r = clist_append(hdrlist, header);
2428         header = strdup("To");
2429         r = clist_append(hdrlist, header);
2430         header = strdup("Cc");
2431         r = clist_append(hdrlist, header);
2432         header = strdup("Subject");
2433         r = clist_append(hdrlist, header);
2434         header = strdup("Message-ID");
2435         r = clist_append(hdrlist, header);
2436         header = strdup("References");
2437         r = clist_append(hdrlist, header);
2438         header = strdup("In-Reply-To");
2439         r = clist_append(hdrlist, header);
2440   
2441         imap_hdrlist = mailimap_header_list_new(hdrlist);
2442         section = mailimap_section_new_header_fields(imap_hdrlist);
2443         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2444         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2445   
2446         return MAIL_NO_ERROR;
2447 }
2448
2449 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2450 {
2451         struct mailimap_fetch_att * fetch_att;
2452         struct mailimap_section * section;
2453         
2454         section = mailimap_section_new_header();
2455         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2456         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2457         
2458         return MAIL_NO_ERROR;
2459 }
2460
2461 static int
2462 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2463                         carray ** p_env_list)
2464 {
2465         struct mailimap_fetch_att * fetch_att;
2466         struct mailimap_fetch_type * fetch_type;
2467         int res;
2468         clist * fetch_result;
2469         int r;
2470         carray * env_list = NULL;
2471         chashdatum key;
2472         chashdatum value;
2473         
2474         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2475   
2476         /* uid */
2477         fetch_att = mailimap_fetch_att_new_uid();
2478         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2479   
2480         /* flags */
2481         fetch_att = mailimap_fetch_att_new_flags();
2482         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2483   
2484         /* rfc822 size */
2485         fetch_att = mailimap_fetch_att_new_rfc822_size();
2486         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2487   
2488         /* headers */
2489         key.data = &imap;
2490         key.len = sizeof(imap);
2491         r = chash_get(courier_workaround_hash, &key, &value);
2492         if (r < 0)
2493                 r = imap_add_envelope_fetch_att(fetch_type);
2494         else
2495                 r = imap_add_header_fetch_att(fetch_type);
2496         
2497         r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2498         
2499         switch (r) {
2500         case MAILIMAP_NO_ERROR:
2501                 break;
2502         default:
2503                 mailimap_fetch_type_free(fetch_type);
2504                 debug_print("uid_fetch: %d\n", r);
2505                 return r;
2506         }
2507         
2508         if (clist_begin(fetch_result) == NULL) {
2509                 res = MAILIMAP_ERROR_FETCH;
2510                 debug_print("clist_begin = NULL\n");
2511                 goto err;
2512         }
2513         
2514         r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2515         mailimap_fetch_list_free(fetch_result);
2516         
2517         if (r != MAILIMAP_NO_ERROR) {
2518                 mailimap_fetch_type_free(fetch_type);
2519                 res = MAILIMAP_ERROR_MEMORY;
2520                 debug_print("fetch_result_to_envelop_list: %d\n", res);
2521                 goto err;
2522         }
2523         
2524         mailimap_fetch_type_free(fetch_type);
2525         
2526         * p_env_list = env_list;
2527         
2528         return MAILIMAP_NO_ERROR;
2529   
2530  err:
2531         return res;
2532 }
2533
2534 struct fetch_env_param {
2535         mailimap * imap;
2536         struct mailimap_set * set;
2537 };
2538
2539 struct fetch_env_result {
2540         carray * fetch_env_result;
2541         int error;
2542 };
2543
2544 static void fetch_env_run(struct etpan_thread_op * op)
2545 {
2546         struct fetch_env_param * param;
2547         struct fetch_env_result * result;
2548         carray * env_list;
2549         int r;
2550         
2551         param = op->param;
2552         result = op->result;
2553
2554         CHECK_IMAP();
2555
2556         env_list = NULL;
2557         r = imap_get_envelopes_list(param->imap, param->set,
2558                                     &env_list);
2559         
2560         result->error = r;
2561         result->fetch_env_result = env_list;
2562         
2563         debug_print("imap fetch_env run - end %i\n", r);
2564 }
2565
2566 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2567                             carray ** p_env_list)
2568 {
2569         struct fetch_env_param param;
2570         struct fetch_env_result result;
2571         mailimap * imap;
2572         
2573         debug_print("imap fetch_env - begin\n");
2574         
2575         imap = get_imap(folder);
2576         param.imap = imap;
2577         param.set = set;
2578         
2579         threaded_run(folder, &param, &result, fetch_env_run);
2580         
2581         if (result.error != MAILIMAP_NO_ERROR) {
2582                 chashdatum key;
2583                 chashdatum value;
2584                 int r;
2585                 
2586                 key.data = &imap;
2587                 key.len = sizeof(imap);
2588                 r = chash_get(courier_workaround_hash, &key, &value);
2589                 if (r < 0) {
2590                         value.data = NULL;
2591                         value.len = 0;
2592                         chash_set(courier_workaround_hash, &key, &value, NULL);
2593                         
2594                         threaded_run(folder, &param, &result, fetch_env_run);
2595                 }
2596         }
2597         
2598         if (result.error != MAILIMAP_NO_ERROR)
2599                 return result.error;
2600         
2601         debug_print("imap fetch_env - end\n");
2602         
2603         * p_env_list = result.fetch_env_result;
2604         
2605         return result.error;
2606 }
2607
2608 void imap_fetch_env_free(carray * env_list)
2609 {
2610         unsigned int i;
2611         
2612         for(i = 0 ; i < carray_count(env_list) ; i ++) {
2613                 struct imap_fetch_env_info * env_info;
2614                 
2615                 env_info = carray_get(env_list, i);
2616                 free(env_info->headers);
2617                 free(env_info);
2618         }
2619         carray_free(env_list);
2620 }
2621
2622
2623
2624
2625
2626 struct append_param {
2627         mailimap * imap;
2628         const char * mailbox;
2629         const char * filename;
2630         struct mailimap_flag_list * flag_list;
2631 };
2632
2633 struct append_result {
2634         int error;
2635         int uid;
2636 };
2637
2638 static void append_run(struct etpan_thread_op * op)
2639 {
2640         struct append_param * param;
2641         struct append_result * result;
2642         int r;
2643         char * data;
2644         size_t size;
2645         struct stat stat_buf;
2646         int fd;
2647         guint32 uid = 0, val = 0;
2648         
2649         param = op->param;
2650         result = op->result;
2651         
2652         CHECK_IMAP();
2653
2654         r = stat(param->filename, &stat_buf);
2655         if (r < 0) {
2656                 result->error = MAILIMAP_ERROR_APPEND;
2657                 return;
2658         }
2659         size = stat_buf.st_size;
2660         
2661         fd = open(param->filename, O_RDONLY);
2662         if (fd < 0) {
2663                 result->error = MAILIMAP_ERROR_APPEND;
2664                 return;
2665         }
2666         
2667         data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2668         if (data == (void *) MAP_FAILED) {
2669                 close(fd);
2670                 result->error = MAILIMAP_ERROR_APPEND;
2671                 return;
2672         }
2673         
2674         mailstream_logger = imap_logger_append;
2675         
2676         r = mailimap_uidplus_append(param->imap, param->mailbox,
2677                             param->flag_list, NULL,
2678                             data, size, &val, &uid);
2679
2680         mailstream_logger = imap_logger_cmd;
2681         
2682         munmap(data, size);
2683         close(fd);
2684         
2685         result->error = r;
2686         result->uid = uid;
2687         debug_print("imap append run - end %i uid %d\n", r, uid);
2688 }
2689
2690 int imap_threaded_append(Folder * folder, const char * mailbox,
2691                          const char * filename,
2692                          struct mailimap_flag_list * flag_list,
2693                          int *uid)
2694 {
2695         struct append_param param;
2696         struct append_result result;
2697         mailimap * imap;
2698         
2699         debug_print("imap append - begin\n");
2700         
2701         imap = get_imap(folder);
2702         param.imap = imap;
2703         param.mailbox = mailbox;
2704         param.filename = filename;
2705         param.flag_list = flag_list;
2706         
2707         threaded_run(folder, &param, &result, append_run);
2708         
2709         if (result.error != MAILIMAP_NO_ERROR)
2710                 return result.error;
2711         
2712         debug_print("imap append - end\n");
2713         if (uid != NULL)
2714                 *uid = result.uid;
2715
2716         return result.error;
2717 }
2718
2719
2720
2721
2722 struct expunge_param {
2723         mailimap * imap;
2724 };
2725
2726 struct expunge_result {
2727         int error;
2728 };
2729
2730 static void expunge_run(struct etpan_thread_op * op)
2731 {
2732         struct expunge_param * param;
2733         struct expunge_result * result;
2734         int r;
2735         
2736         param = op->param;
2737         result = op->result;
2738
2739         CHECK_IMAP();
2740
2741         r = mailimap_expunge(param->imap);
2742         
2743         result->error = r;
2744         debug_print("imap expunge run - end %i\n", r);
2745 }
2746
2747 int imap_threaded_expunge(Folder * folder)
2748 {
2749         struct expunge_param param;
2750         struct expunge_result result;
2751         
2752         debug_print("imap expunge - begin\n");
2753         
2754         param.imap = get_imap(folder);
2755         
2756         threaded_run(folder, &param, &result, expunge_run);
2757         
2758         debug_print("imap expunge - end\n");
2759         
2760         return result.error;
2761 }
2762
2763
2764 struct copy_param {
2765         mailimap * imap;
2766         struct mailimap_set * set;
2767         const char * mb;
2768 };
2769
2770 struct copy_result {
2771         int error;
2772         struct mailimap_set *source;
2773         struct mailimap_set *dest;
2774 };
2775
2776 static void copy_run(struct etpan_thread_op * op)
2777 {
2778         struct copy_param * param;
2779         struct copy_result * result;
2780         int r;
2781         guint32 val;
2782         struct mailimap_set *source = NULL, *dest = NULL;
2783
2784         param = op->param;
2785         result = op->result;
2786
2787         CHECK_IMAP();
2788
2789         r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2790                 &val, &source, &dest);
2791         
2792         result->error = r;
2793         if (r == 0) {
2794                 result->source = source;
2795                 result->dest = dest;
2796         } else {
2797                 result->source = NULL;
2798                 result->dest = NULL;
2799         }
2800         debug_print("imap copy run - end %i\n", r);
2801 }
2802
2803 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2804                        const char * mb, struct mailimap_set **source,
2805                        struct mailimap_set **dest)
2806 {
2807         struct copy_param param;
2808         struct copy_result result;
2809         mailimap * imap;
2810         
2811         debug_print("imap copy - begin\n");
2812         
2813         imap = get_imap(folder);
2814         param.imap = imap;
2815         param.set = set;
2816         param.mb = mb;
2817         
2818         threaded_run(folder, &param, &result, copy_run);
2819         *source = NULL;
2820         *dest = NULL;
2821         
2822         if (result.error != MAILIMAP_NO_ERROR)
2823                 return result.error;
2824         
2825         *source = result.source;
2826         *dest = result.dest;
2827
2828         debug_print("imap copy - end\n");
2829         
2830         return result.error;
2831 }
2832
2833
2834
2835 struct store_param {
2836         mailimap * imap;
2837         struct mailimap_set * set;
2838         struct mailimap_store_att_flags * store_att_flags;
2839 };
2840
2841 struct store_result {
2842         int error;
2843 };
2844
2845 static void store_run(struct etpan_thread_op * op)
2846 {
2847         struct store_param * param;
2848         struct store_result * result;
2849         int r;
2850         
2851         param = op->param;
2852         result = op->result;
2853
2854         CHECK_IMAP();
2855
2856         r = mailimap_uid_store(param->imap, param->set,
2857                                param->store_att_flags);
2858         
2859         result->error = r;
2860         
2861         debug_print("imap store run - end %i\n", r);
2862 }
2863
2864 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2865                         struct mailimap_store_att_flags * store_att_flags)
2866 {
2867         struct store_param param;
2868         struct store_result result;
2869         mailimap * imap;
2870         
2871         debug_print("imap store - begin\n");
2872         
2873         imap = get_imap(folder);
2874         param.imap = imap;
2875         param.set = set;
2876         param.store_att_flags = store_att_flags;
2877         
2878         threaded_run(folder, &param, &result, store_run);
2879         
2880         if (result.error != MAILIMAP_NO_ERROR)
2881                 return result.error;
2882         
2883         debug_print("imap store - end\n");
2884         
2885         return result.error;
2886 }
2887
2888
2889 #define ENV_BUFFER_SIZE 512
2890
2891 static void do_exec_command(int fd, const char * command,
2892                             const char * servername, uint16_t port)
2893 {
2894         int i, maxopen;
2895 #ifdef SOLARIS
2896         char env_buffer[ENV_BUFFER_SIZE];
2897 #endif
2898         
2899         if (fork() > 0) {
2900                 /* Fork again to become a child of init rather than
2901                    the etpan client. */
2902                 exit(0);
2903         }
2904   
2905 #ifdef SOLARIS
2906         if (servername)
2907                 snprintf(env_buffer, ENV_BUFFER_SIZE,
2908                          "ETPANSERVER=%s", servername);
2909         else
2910                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2911         putenv(env_buffer);
2912 #else
2913         if (servername)
2914                 setenv("ETPANSERVER", servername, 1);
2915         else
2916                 unsetenv("ETPANSERVER");
2917 #endif
2918   
2919 #ifdef SOLARIS
2920         if (port)
2921                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2922         else
2923                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2924         putenv(env_buffer);
2925 #else
2926         if (port) {
2927                 char porttext[20];
2928                 
2929                 snprintf(porttext, sizeof(porttext), "%d", port);
2930                 setenv("ETPANPORT", porttext, 1);
2931         }
2932         else {
2933                 unsetenv("ETPANPORT");
2934         }
2935 #endif
2936                 
2937         /* Not a lot we can do if there's an error other than bail. */
2938         if (dup2(fd, 0) == -1)
2939                 exit(1);
2940         if (dup2(fd, 1) == -1)
2941                 exit(1);
2942   
2943         /* Should we close stderr and reopen /dev/null? */
2944   
2945         maxopen = sysconf(_SC_OPEN_MAX);
2946         for (i=3; i < maxopen; i++)
2947                 close(i);
2948   
2949 #ifdef TIOCNOTTY
2950         /* Detach from the controlling tty if we have one. Otherwise,
2951            SSH might do something stupid like trying to use it instead
2952            of running $SSH_ASKPASS. Doh. */
2953         fd = open("/dev/tty", O_RDONLY);
2954         if (fd != -1) {
2955                 ioctl(fd, TIOCNOTTY, NULL);
2956                 close(fd);
2957         }
2958 #endif /* TIOCNOTTY */
2959
2960         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2961   
2962         /* Eep. Shouldn't reach this */
2963         exit(1);
2964 }
2965
2966 static int subcommand_connect(const char *command,
2967                               const char *servername, uint16_t port)
2968 {
2969         /* SEB unsupported on Windows */
2970         int sockfds[2];
2971         pid_t childpid;
2972   
2973         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2974                 return -1;
2975   
2976         childpid = fork();
2977         if (!childpid) {
2978                 do_exec_command(sockfds[1], command, servername, port);
2979         }
2980         else if (childpid == -1) {
2981                 close(sockfds[0]);
2982                 close(sockfds[1]);
2983                 return -1;
2984         }
2985   
2986         close(sockfds[1]);
2987   
2988         /* Reap child, leaving grandchild process to run */
2989         waitpid(childpid, NULL, 0);
2990   
2991         return sockfds[0];
2992 }
2993
2994 static int socket_connect_cmd(mailimap * imap, const char * command,
2995                        const char * server, int port)
2996 {
2997         int fd;
2998         mailstream * s;
2999         int r;
3000         
3001         fd = subcommand_connect(command, server, port);
3002         if (fd < 0)
3003                 return MAILIMAP_ERROR_STREAM;
3004         
3005         s = mailstream_socket_open(fd);
3006         if (s == NULL) {
3007                 close(fd);
3008                 return MAILIMAP_ERROR_STREAM;
3009         }
3010         
3011         r = mailimap_connect(imap, s);
3012         if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3013         &&  r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3014                 mailstream_close(s);
3015                 if (imap)
3016                         imap->imap_stream = NULL;
3017                 return r;
3018         }
3019         
3020         return r;
3021 }
3022
3023 /* connect cmd */
3024
3025 struct connect_cmd_param {
3026         mailimap * imap;
3027         const char * command;
3028         const char * server;
3029         int port;
3030 };
3031
3032 struct connect_cmd_result {
3033         int error;
3034 };
3035
3036 static void connect_cmd_run(struct etpan_thread_op * op)
3037 {
3038         int r;
3039         struct connect_cmd_param * param;
3040         struct connect_cmd_result * result;
3041         
3042         param = op->param;
3043         result = op->result;
3044         
3045         CHECK_IMAP();
3046
3047         r = socket_connect_cmd(param->imap, param->command,
3048                                param->server, param->port);
3049         
3050         result->error = r;
3051 }
3052
3053
3054 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3055                               const char * server, int port)
3056 {
3057         struct connect_cmd_param param;
3058         struct connect_cmd_result result;
3059         chashdatum key;
3060         chashdatum value;
3061         mailimap * imap, * oldimap;
3062         
3063         oldimap = get_imap(folder);
3064
3065         imap = mailimap_new(0, NULL);
3066         
3067         if (oldimap) {
3068                 debug_print("deleting old imap %p\n", oldimap);
3069                 delete_imap(folder, oldimap);
3070         }
3071
3072         key.data = &folder;
3073         key.len = sizeof(folder);
3074         value.data = imap;
3075         value.len = 0;
3076         chash_set(session_hash, &key, &value, NULL);
3077         
3078         param.imap = imap;
3079         param.command = command;
3080         param.server = server;
3081         param.port = port;
3082         
3083         threaded_run(folder, &param, &result, connect_cmd_run);
3084         
3085         debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3086         
3087         return result.error;
3088 }
3089
3090 void imap_threaded_cancel(Folder * folder)
3091 {
3092         mailimap * imap;
3093         
3094         imap = get_imap(folder);
3095         if (imap->imap_stream != NULL)
3096                 mailstream_cancel(imap->imap_stream);
3097 }
3098
3099 #else
3100
3101 void imap_main_init(void)
3102 {
3103 }
3104 void imap_main_done(void)
3105 {
3106 }
3107 void imap_main_set_timeout(int sec)
3108 {
3109 }
3110
3111 void imap_threaded_cancel(Folder * folder);
3112 {
3113 }
3114
3115 #endif