2007-08-19 [colin] 2.10.0cvs133
[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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 > 128 && !direction) {
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                        unsigned int *p_recent, 
1004                        unsigned int *p_expunge,
1005                        unsigned int *p_unseen,
1006                        unsigned int *p_uidnext,
1007                        unsigned int *p_uidval)
1008 {
1009         struct noop_param param;
1010         struct noop_result result;
1011         mailimap * imap;
1012         
1013         debug_print("imap noop - begin\n");
1014         
1015         imap = get_imap(folder);
1016         param.imap = imap;
1017
1018         threaded_run(folder, &param, &result, noop_run);
1019         
1020         if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1021                 * p_exists = imap->imap_selection_info->sel_exists;
1022                 * p_recent = imap->imap_selection_info->sel_recent;
1023                 * p_unseen = imap->imap_selection_info->sel_unseen;
1024                 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1025                 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1026         } else {
1027                 * p_exists = 0;
1028                 * p_recent = 0;
1029                 * p_unseen = 0;
1030                 * p_uidnext = 0;
1031                 * p_uidval = 0;
1032         }
1033         if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1034             imap->imap_response_info->rsp_expunged != NULL) {
1035                 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1036         } else {
1037                 * p_expunge = 0;
1038         }       
1039         debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1040                 *p_exists, *p_recent, *p_expunge, *p_unseen,
1041                 *p_uidnext, *p_uidval);
1042         
1043         return result.error;
1044 }
1045
1046
1047 struct starttls_result {
1048         int error;
1049 };
1050
1051 static void starttls_run(struct etpan_thread_op * op)
1052 {
1053         struct connect_param * param;
1054         struct starttls_result * result;
1055         int r;
1056
1057         param = op->param;
1058         result = op->result;
1059
1060         CHECK_IMAP();
1061
1062         r = mailimap_starttls(param->imap);
1063         
1064         result->error = r;
1065         debug_print("imap starttls run - end %i\n", r);
1066         
1067         if (r == 0) {
1068                 mailimap *imap = param->imap;
1069                 mailstream_low *plain_low = NULL;
1070                 mailstream_low *tls_low = NULL;
1071                 int fd = -1;
1072                 
1073                 plain_low = mailstream_get_low(imap->imap_stream);
1074                 fd = mailstream_low_get_fd(plain_low);
1075                 if (fd == -1) {
1076                         debug_print("imap starttls run - can't get fd\n");
1077                         result->error = MAILIMAP_ERROR_STREAM;
1078                         return;
1079                 }
1080
1081                 tls_low = mailstream_low_tls_open(fd);
1082                 if (tls_low == NULL) {
1083                         debug_print("imap starttls run - can't tls_open\n");
1084                         result->error = MAILIMAP_ERROR_STREAM;
1085                         return;
1086                 }
1087                 mailstream_low_free(plain_low);
1088                 mailstream_set_low(imap->imap_stream, tls_low);
1089         }
1090 }
1091
1092 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1093 {
1094         struct connect_param param;
1095         struct starttls_result result;
1096         int cert_len;
1097         unsigned char *certificate;
1098         
1099         debug_print("imap starttls - begin\n");
1100         
1101         param.imap = get_imap(folder);
1102         param.server = host;
1103         param.port = port;
1104         
1105         threaded_run(folder, &param, &result, starttls_run);
1106         
1107         debug_print("imap starttls - end\n");
1108
1109         if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1110                 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1111                 if (etpan_certificate_check(certificate, cert_len, &param) < 0)
1112                         result.error = MAILIMAP_ERROR_STREAM;
1113                 if (certificate) 
1114                         free(certificate); 
1115         }       
1116         return result.error;
1117 }
1118
1119
1120
1121 struct create_param {
1122         mailimap * imap;
1123         const char * mb;
1124 };
1125
1126 struct create_result {
1127         int error;
1128 };
1129
1130 static void create_run(struct etpan_thread_op * op)
1131 {
1132         struct create_param * param;
1133         struct create_result * result;
1134         int r;
1135         
1136         param = op->param;
1137         result = op->result;
1138
1139         CHECK_IMAP();
1140
1141         r = mailimap_create(param->imap, param->mb);
1142         
1143         result->error = r;
1144         debug_print("imap create run - end %i\n", r);
1145 }
1146
1147 int imap_threaded_create(Folder * folder, const char * mb)
1148 {
1149         struct create_param param;
1150         struct create_result result;
1151         
1152         debug_print("imap create - begin\n");
1153         
1154         param.imap = get_imap(folder);
1155         param.mb = mb;
1156         
1157         threaded_run(folder, &param, &result, create_run);
1158         
1159         debug_print("imap create - end\n");
1160         
1161         return result.error;
1162 }
1163
1164
1165
1166
1167 struct rename_param {
1168         mailimap * imap;
1169         const char * mb;
1170         const char * new_name;
1171 };
1172
1173 struct rename_result {
1174         int error;
1175 };
1176
1177 static void rename_run(struct etpan_thread_op * op)
1178 {
1179         struct rename_param * param;
1180         struct rename_result * result;
1181         int r;
1182         
1183         param = op->param;
1184         result = op->result;
1185
1186         CHECK_IMAP();
1187
1188         r = mailimap_rename(param->imap, param->mb, param->new_name);
1189         
1190         result->error = r;
1191         debug_print("imap rename run - end %i\n", r);
1192 }
1193
1194 int imap_threaded_rename(Folder * folder,
1195                          const char * mb, const char * new_name)
1196 {
1197         struct rename_param param;
1198         struct rename_result result;
1199         
1200         debug_print("imap rename - begin\n");
1201         
1202         param.imap = get_imap(folder);
1203         param.mb = mb;
1204         param.new_name = new_name;
1205         
1206         threaded_run(folder, &param, &result, rename_run);
1207         
1208         debug_print("imap rename - end\n");
1209         
1210         return result.error;
1211 }
1212
1213
1214
1215
1216 struct delete_param {
1217         mailimap * imap;
1218         const char * mb;
1219 };
1220
1221 struct delete_result {
1222         int error;
1223 };
1224
1225 static void delete_run(struct etpan_thread_op * op)
1226 {
1227         struct delete_param * param;
1228         struct delete_result * result;
1229         int r;
1230         
1231         param = op->param;
1232         result = op->result;
1233
1234         CHECK_IMAP();
1235
1236         r = mailimap_delete(param->imap, param->mb);
1237         
1238         result->error = r;
1239         debug_print("imap delete run - end %i\n", r);
1240 }
1241
1242 int imap_threaded_delete(Folder * folder, const char * mb)
1243 {
1244         struct delete_param param;
1245         struct delete_result result;
1246         
1247         debug_print("imap delete - begin\n");
1248         
1249         param.imap = get_imap(folder);
1250         param.mb = mb;
1251         
1252         threaded_run(folder, &param, &result, delete_run);
1253         
1254         debug_print("imap delete - end\n");
1255         
1256         return result.error;
1257 }
1258
1259
1260
1261 struct select_param {
1262         mailimap * imap;
1263         const char * mb;
1264 };
1265
1266 struct select_result {
1267         int error;
1268 };
1269
1270 static void select_run(struct etpan_thread_op * op)
1271 {
1272         struct select_param * param;
1273         struct select_result * result;
1274         int r;
1275         
1276         param = op->param;
1277         result = op->result;
1278
1279         CHECK_IMAP();
1280
1281         r = mailimap_select(param->imap, param->mb);
1282         
1283         result->error = r;
1284         debug_print("imap select run - end %i\n", r);
1285 }
1286
1287 int imap_threaded_select(Folder * folder, const char * mb,
1288                          gint * exists, gint * recent, gint * unseen,
1289                          guint32 * uid_validity)
1290 {
1291         struct select_param param;
1292         struct select_result result;
1293         mailimap * imap;
1294         
1295         debug_print("imap select - begin\n");
1296         
1297         imap = get_imap(folder);
1298         param.imap = imap;
1299         param.mb = mb;
1300         
1301         threaded_run(folder, &param, &result, select_run);
1302         
1303         if (result.error != MAILIMAP_NO_ERROR)
1304                 return result.error;
1305         
1306         if (!imap || imap->imap_selection_info == NULL)
1307                 return MAILIMAP_ERROR_PARSE;
1308         
1309         * exists = imap->imap_selection_info->sel_exists;
1310         * recent = imap->imap_selection_info->sel_recent;
1311         * unseen = imap->imap_selection_info->sel_unseen;
1312         * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1313         
1314         debug_print("imap select - end\n");
1315         
1316         return result.error;
1317 }
1318
1319 static void close_run(struct etpan_thread_op * op)
1320 {
1321         struct select_param * param;
1322         struct select_result * result;
1323         int r;
1324         
1325         param = op->param;
1326         result = op->result;
1327
1328         CHECK_IMAP();
1329
1330         r = mailimap_close(param->imap);
1331         
1332         result->error = r;
1333         debug_print("imap close run - end %i\n", r);
1334 }
1335
1336 int imap_threaded_close(Folder * folder)
1337 {
1338         struct select_param param;
1339         struct select_result result;
1340         mailimap * imap;
1341         
1342         debug_print("imap close - begin\n");
1343         
1344         imap = get_imap(folder);
1345         param.imap = imap;
1346         
1347         threaded_run(folder, &param, &result, close_run);
1348         
1349         if (result.error != MAILIMAP_NO_ERROR)
1350                 return result.error;
1351         
1352         debug_print("imap close - end\n");
1353         
1354         return result.error;
1355 }
1356
1357 struct examine_param {
1358         mailimap * imap;
1359         const char * mb;
1360 };
1361
1362 struct examine_result {
1363         int error;
1364 };
1365
1366 static void examine_run(struct etpan_thread_op * op)
1367 {
1368         struct examine_param * param;
1369         struct examine_result * result;
1370         int r;
1371         
1372         param = op->param;
1373         result = op->result;
1374
1375         CHECK_IMAP();
1376
1377         r = mailimap_examine(param->imap, param->mb);
1378         
1379         result->error = r;
1380         debug_print("imap examine run - end %i\n", r);
1381 }
1382
1383 int imap_threaded_examine(Folder * folder, const char * mb,
1384                           gint * exists, gint * recent, gint * unseen,
1385                           guint32 * uid_validity)
1386 {
1387         struct examine_param param;
1388         struct examine_result result;
1389         mailimap * imap;
1390         
1391         debug_print("imap examine - begin\n");
1392         
1393         imap = get_imap(folder);
1394         param.imap = imap;
1395         param.mb = mb;
1396         
1397         threaded_run(folder, &param, &result, examine_run);
1398         
1399         if (result.error != MAILIMAP_NO_ERROR)
1400                 return result.error;
1401         
1402         if (!imap || imap->imap_selection_info == NULL)
1403                 return MAILIMAP_ERROR_PARSE;
1404         
1405         * exists = imap->imap_selection_info->sel_exists;
1406         * recent = imap->imap_selection_info->sel_recent;
1407         * unseen = imap->imap_selection_info->sel_unseen;
1408         * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1409         
1410         debug_print("imap examine - end\n");
1411         
1412         return result.error;
1413 }
1414
1415
1416
1417
1418 struct search_param {
1419         mailimap * imap;
1420         int type;
1421         struct mailimap_set * set;
1422 };
1423
1424 struct search_result {
1425         int error;
1426         clist * search_result;
1427 };
1428
1429 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1430 {
1431         return mailimap_set_item_new(orig->set_first, orig->set_last);
1432 }
1433
1434 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1435 {
1436         clist *list = orig ? orig->set_list : NULL;
1437         clist *newlist = clist_new();
1438         clistiter *cur;
1439         
1440         if (!orig)
1441                 return NULL;
1442         for (cur = clist_begin(list); cur; cur = clist_next(cur))
1443                 clist_append(newlist, 
1444                         sc_mailimap_set_item_copy(
1445                         (struct mailimap_set_item *)clist_content(cur)));
1446         return mailimap_set_new(newlist);
1447 }
1448
1449 static void search_run(struct etpan_thread_op * op)
1450 {
1451         struct search_param * param;
1452         struct search_result * result;
1453         int r;
1454         struct mailimap_search_key * key = NULL;
1455         struct mailimap_search_key * uid_key = NULL;
1456         struct mailimap_search_key * search_type_key;
1457         clist * search_result;
1458         
1459         param = op->param;
1460         result = op->result;
1461
1462         CHECK_IMAP();
1463
1464         /* we copy the mailimap_set because freeing the key is recursive */
1465         if (param->set != NULL) {
1466                 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1467         } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1468                 uid_key = mailimap_search_key_new_all();
1469         }
1470         search_type_key = NULL;
1471         switch (param->type) {
1472         case IMAP_SEARCH_TYPE_SIMPLE:
1473                 search_type_key = NULL;
1474                 break;
1475                 
1476         case IMAP_SEARCH_TYPE_SEEN:
1477                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1478                                                           NULL, NULL, NULL, NULL, NULL,
1479                                                           NULL, NULL, NULL, NULL, NULL,
1480                                                           NULL, NULL, NULL, NULL, 0,
1481                                                           NULL, NULL, NULL, NULL, NULL,
1482                                                           NULL, 0, NULL, NULL, NULL);
1483                 break;
1484                 
1485         case IMAP_SEARCH_TYPE_UNSEEN:
1486                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1487                                                           NULL, NULL, NULL, NULL, NULL,
1488                                                           NULL, NULL, NULL, NULL, NULL,
1489                                                           NULL, NULL, NULL, NULL, 0,
1490                                                           NULL, NULL, NULL, NULL, NULL,
1491                                                           NULL, 0, NULL, NULL, NULL);
1492                 break;
1493                 
1494         case IMAP_SEARCH_TYPE_ANSWERED:
1495                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1496                                                           NULL, NULL, NULL, NULL, NULL,
1497                                                           NULL, NULL, NULL, NULL, NULL,
1498                                                           NULL, NULL, NULL, NULL, 0,
1499                                                           NULL, NULL, NULL, NULL, NULL,
1500                                                           NULL, 0, NULL, NULL, NULL);
1501                 break;
1502                 
1503         case IMAP_SEARCH_TYPE_FLAGGED:
1504                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1505                                                           NULL, NULL, NULL, NULL, NULL,
1506                                                           NULL, NULL, NULL, NULL, NULL,
1507                                                           NULL, NULL, NULL, NULL, 0,
1508                                                           NULL, NULL, NULL, NULL, NULL,
1509                                                           NULL, 0, NULL, NULL, NULL);
1510                 break;
1511         case IMAP_SEARCH_TYPE_DELETED:
1512                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1513                                                           NULL, NULL, NULL, NULL, NULL,
1514                                                           NULL, NULL, NULL, NULL, NULL,
1515                                                           NULL, NULL, NULL, NULL, 0,
1516                                                           NULL, NULL, NULL, NULL, NULL,
1517                                                           NULL, 0, NULL, NULL, NULL);
1518                 break;
1519         }
1520         
1521         if (search_type_key != NULL) {
1522                 if (param->set != NULL) {
1523                         key = mailimap_search_key_new_multiple_empty();
1524                         mailimap_search_key_multiple_add(key, search_type_key);
1525                         mailimap_search_key_multiple_add(key, uid_key);
1526                 } else {
1527                         key = search_type_key;
1528                 }
1529         } else if (uid_key != NULL) {
1530                 key = uid_key;
1531         }
1532         
1533         if (key == NULL) {
1534                 g_warning("no key!");
1535                 result = op->result;
1536                 result->error = -1;
1537                 result->search_result = NULL;
1538         } else {
1539                 mailstream_logger = imap_logger_uid;
1540
1541                 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1542
1543                 mailstream_logger = imap_logger_cmd;
1544
1545                 /* free the key (with the imapset) */
1546                 mailimap_search_key_free(key);
1547
1548                 result->error = r;
1549                 result->search_result = search_result;
1550         }
1551         debug_print("imap search run - end %i\n", result->error);
1552 }
1553
1554 int imap_threaded_search(Folder * folder, int search_type,
1555                          struct mailimap_set * set, clist ** search_result)
1556 {
1557         struct search_param param;
1558         struct search_result result;
1559         mailimap * imap;
1560         
1561         debug_print("imap search - begin\n");
1562
1563         imap = get_imap(folder);
1564         param.imap = imap;
1565         param.set = set;
1566         param.type = search_type;
1567         
1568         threaded_run(folder, &param, &result, search_run);
1569         
1570         if (result.error != MAILIMAP_NO_ERROR)
1571                 return result.error;
1572         
1573         debug_print("imap search - end\n");
1574         
1575         * search_result = result.search_result;
1576         
1577         return result.error;
1578 }
1579
1580
1581
1582 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1583                                  uint32_t * puid,
1584                                  char ** pheaders,
1585                                  size_t * pref_size,
1586                                  struct mailimap_msg_att_dynamic ** patt_dyn);
1587
1588 static int
1589 result_to_uid_list(clist * fetch_result, carray ** result)
1590 {
1591         clistiter * cur;
1592         int r;
1593         int res;
1594         carray * tab;
1595         
1596         tab = carray_new(128);
1597         if (tab == NULL) {
1598                 res = MAILIMAP_ERROR_MEMORY;
1599                 goto err;
1600         }
1601         
1602         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1603             cur = clist_next(cur)) {
1604                 struct mailimap_msg_att * msg_att;
1605                 uint32_t uid;
1606                 uint32_t * puid;
1607                 
1608                 msg_att = clist_content(cur);
1609                 
1610                 uid = 0;
1611                 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1612                 
1613                 puid = malloc(sizeof(* puid));
1614                 if (puid == NULL) {
1615                         res = MAILIMAP_ERROR_MEMORY;
1616                         goto free_list;
1617                 }
1618                 * puid = uid;
1619                         
1620                 r = carray_add(tab, puid, NULL);
1621                 if (r < 0) {
1622                         free(puid);
1623                         res = MAILIMAP_ERROR_MEMORY;
1624                         goto free_list;
1625                 }
1626         }
1627                 
1628         * result = tab;
1629
1630         return MAILIMAP_NO_ERROR;
1631   
1632  free_list:
1633         imap_fetch_uid_list_free(tab);
1634  err:
1635         return res;
1636 }
1637
1638 static int imap_get_messages_list(mailimap * imap,
1639                                   uint32_t first_index,
1640                                   carray ** result)
1641 {
1642         carray * env_list;
1643         int r;
1644         struct mailimap_fetch_att * fetch_att;
1645         struct mailimap_fetch_type * fetch_type;
1646         struct mailimap_set * set;
1647         clist * fetch_result;
1648         int res;
1649         
1650         set = mailimap_set_new_interval(first_index, 0);
1651         if (set == NULL) {
1652                 res = MAILIMAP_ERROR_MEMORY;
1653                 goto err;
1654         }
1655
1656         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1657         if (fetch_type == NULL) {
1658                 res = MAILIMAP_ERROR_MEMORY;
1659                 goto free_set;
1660         }
1661
1662         fetch_att = mailimap_fetch_att_new_uid();
1663         if (fetch_att == NULL) {
1664                 res = MAILIMAP_ERROR_MEMORY;
1665                 goto free_fetch_type;
1666         }
1667
1668         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1669         if (r != MAILIMAP_NO_ERROR) {
1670                 mailimap_fetch_att_free(fetch_att);
1671                 res = MAILIMAP_ERROR_MEMORY;
1672                 goto free_fetch_type;
1673         }
1674
1675         mailstream_logger = imap_logger_fetch;
1676         
1677         r = mailimap_uid_fetch(imap, set,
1678                                fetch_type, &fetch_result);
1679
1680         mailstream_logger = imap_logger_cmd;
1681         mailimap_fetch_type_free(fetch_type);
1682         mailimap_set_free(set);
1683
1684         if (r != MAILIMAP_NO_ERROR) {
1685                 res = r;
1686                 goto err;
1687         }
1688
1689         env_list = NULL;
1690         r = result_to_uid_list(fetch_result, &env_list);
1691         mailimap_fetch_list_free(fetch_result);
1692         
1693         * result = env_list;
1694
1695         return MAILIMAP_NO_ERROR;
1696
1697  free_fetch_type:
1698         mailimap_fetch_type_free(fetch_type);
1699  free_set:
1700         mailimap_set_free(set);
1701  err:
1702         return res;
1703 }
1704
1705
1706
1707
1708 struct fetch_uid_param {
1709         mailimap * imap;
1710         uint32_t first_index;
1711 };
1712
1713 struct fetch_uid_result {
1714         int error;
1715         carray * fetch_result;
1716 };
1717
1718 static void fetch_uid_run(struct etpan_thread_op * op)
1719 {
1720         struct fetch_uid_param * param;
1721         struct fetch_uid_result * result;
1722         carray * fetch_result;
1723         int r;
1724         
1725         param = op->param;
1726         result = op->result;
1727
1728         CHECK_IMAP();
1729
1730         fetch_result = NULL;
1731         mailstream_logger = NULL;
1732         log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1733
1734         r = imap_get_messages_list(param->imap, param->first_index,
1735                                    &fetch_result);
1736         
1737         mailstream_logger = imap_logger_cmd;
1738
1739         result->error = r;
1740         result->fetch_result = fetch_result;
1741         debug_print("imap fetch_uid run - end %i\n", r);
1742 }
1743
1744 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1745                             carray ** fetch_result)
1746 {
1747         struct fetch_uid_param param;
1748         struct fetch_uid_result result;
1749         mailimap * imap;
1750         
1751         debug_print("imap fetch_uid - begin\n");
1752         
1753         imap = get_imap(folder);
1754         param.imap = imap;
1755         param.first_index = first_index;
1756         
1757         threaded_run(folder, &param, &result, fetch_uid_run);
1758         
1759         if (result.error != MAILIMAP_NO_ERROR)
1760                 return result.error;
1761         
1762         debug_print("imap fetch_uid - end\n");
1763         
1764         * fetch_result = result.fetch_result;
1765         
1766         return result.error;
1767 }
1768
1769
1770 void imap_fetch_uid_list_free(carray * uid_list)
1771 {
1772         unsigned int i;
1773         
1774         for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1775                 uint32_t * puid;
1776                 
1777                 puid = carray_get(uid_list, i);
1778                 free(puid);
1779         }
1780         carray_free(uid_list);
1781 }
1782
1783
1784
1785
1786 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn);
1787
1788 static int
1789 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1790 {
1791         clistiter * cur;
1792         int r;
1793         int res;
1794         carray * tab;
1795         
1796         tab = carray_new(128);
1797         if (tab == NULL) {
1798                 res = MAILIMAP_ERROR_MEMORY;
1799                 goto err;
1800         }
1801         
1802         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1803             cur = clist_next(cur)) {
1804                 struct mailimap_msg_att * msg_att;
1805                 uint32_t uid;
1806                 uint32_t * puid;
1807                 struct mailimap_msg_att_dynamic * att_dyn;
1808                 int flags;
1809                 int * pflags;
1810                 
1811                 msg_att = clist_content(cur);
1812                 
1813                 uid = 0;
1814                 att_dyn = NULL;
1815                 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1816                 if (uid == 0)
1817                         continue;
1818                 if (att_dyn == NULL)
1819                         continue;
1820                 
1821                 flags = imap_flags_to_flags(att_dyn);
1822                 
1823                 puid = malloc(sizeof(* puid));
1824                 if (puid == NULL) {
1825                         res = MAILIMAP_ERROR_MEMORY;
1826                         goto free_list;
1827                 }
1828                 * puid = uid;
1829                 
1830                 r = carray_add(tab, puid, NULL);
1831                 if (r < 0) {
1832                         free(puid);
1833                         res = MAILIMAP_ERROR_MEMORY;
1834                         goto free_list;
1835                 }
1836                 pflags = malloc(sizeof(* pflags));
1837                 if (pflags == NULL) {
1838                         res = MAILIMAP_ERROR_MEMORY;
1839                         goto free_list;
1840                 }
1841                 * pflags = flags;
1842                 r = carray_add(tab, pflags, NULL);
1843                 if (r < 0) {
1844                         free(pflags);
1845                         res = MAILIMAP_ERROR_MEMORY;
1846                         goto free_list;
1847                 }
1848         }
1849                 
1850         * result = tab;
1851
1852         return MAILIMAP_NO_ERROR;
1853   
1854  free_list:
1855         imap_fetch_uid_flags_list_free(tab);
1856  err:
1857         return res;
1858 }
1859
1860 static int imap_get_messages_flags_list(mailimap * imap,
1861                                         uint32_t first_index,
1862                                         carray ** result)
1863 {
1864         carray * env_list;
1865         int r;
1866         struct mailimap_fetch_att * fetch_att;
1867         struct mailimap_fetch_type * fetch_type;
1868         struct mailimap_set * set;
1869         clist * fetch_result;
1870         int res;
1871         
1872         set = mailimap_set_new_interval(first_index, 0);
1873         if (set == NULL) {
1874                 res = MAILIMAP_ERROR_MEMORY;
1875                 goto err;
1876         }
1877
1878         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1879         if (fetch_type == NULL) {
1880                 res = MAILIMAP_ERROR_MEMORY;
1881                 goto free_set;
1882         }
1883
1884         fetch_att = mailimap_fetch_att_new_flags();
1885         if (fetch_att == NULL) {
1886                 res = MAILIMAP_ERROR_MEMORY;
1887                 goto free_fetch_type;
1888         }
1889         
1890         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1891         if (r != MAILIMAP_NO_ERROR) {
1892                 mailimap_fetch_att_free(fetch_att);
1893                 res = MAILIMAP_ERROR_MEMORY;
1894                 goto free_fetch_type;
1895         }
1896         
1897         fetch_att = mailimap_fetch_att_new_uid();
1898         if (fetch_att == NULL) {
1899                 res = MAILIMAP_ERROR_MEMORY;
1900                 goto free_fetch_type;
1901         }
1902
1903         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1904         if (r != MAILIMAP_NO_ERROR) {
1905                 mailimap_fetch_att_free(fetch_att);
1906                 res = MAILIMAP_ERROR_MEMORY;
1907                 goto free_fetch_type;
1908         }
1909
1910         mailstream_logger = imap_logger_fetch;
1911         
1912         r = mailimap_uid_fetch(imap, set,
1913                                fetch_type, &fetch_result);
1914
1915         mailstream_logger = imap_logger_cmd;
1916         mailimap_fetch_type_free(fetch_type);
1917         mailimap_set_free(set);
1918
1919         if (r != MAILIMAP_NO_ERROR) {
1920                 res = r;
1921                 goto err;
1922         }
1923
1924         env_list = NULL;
1925         r = result_to_uid_flags_list(fetch_result, &env_list);
1926         mailimap_fetch_list_free(fetch_result);
1927         
1928         * result = env_list;
1929
1930         return MAILIMAP_NO_ERROR;
1931
1932  free_fetch_type:
1933         mailimap_fetch_type_free(fetch_type);
1934  free_set:
1935         mailimap_set_free(set);
1936  err:
1937         return res;
1938 }
1939
1940
1941
1942 static void fetch_uid_flags_run(struct etpan_thread_op * op)
1943 {
1944         struct fetch_uid_param * param;
1945         struct fetch_uid_result * result;
1946         carray * fetch_result;
1947         int r;
1948         
1949         param = op->param;
1950         result = op->result;
1951
1952         CHECK_IMAP();
1953
1954         fetch_result = NULL;
1955         r = imap_get_messages_flags_list(param->imap, param->first_index,
1956                                          &fetch_result);
1957         
1958         result->error = r;
1959         result->fetch_result = fetch_result;
1960         debug_print("imap fetch_uid run - end %i\n", r);
1961 }
1962
1963 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
1964                                   carray ** fetch_result)
1965 {
1966         struct fetch_uid_param param;
1967         struct fetch_uid_result result;
1968         mailimap * imap;
1969         
1970         debug_print("imap fetch_uid - begin\n");
1971         
1972         imap = get_imap(folder);
1973         param.imap = imap;
1974         param.first_index = first_index;
1975         
1976         mailstream_logger = NULL;
1977         log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
1978
1979         threaded_run(folder, &param, &result, fetch_uid_flags_run);
1980
1981         mailstream_logger = imap_logger_cmd;
1982
1983         
1984         if (result.error != MAILIMAP_NO_ERROR)
1985                 return result.error;
1986         
1987         debug_print("imap fetch_uid - end\n");
1988         
1989         * fetch_result = result.fetch_result;
1990         
1991         return result.error;
1992 }
1993
1994
1995 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
1996 {
1997         unsigned int i;
1998         
1999         for(i = 0 ; i < carray_count(uid_flags_list) ; i ++) {
2000                 void * data;
2001                 
2002                 data = carray_get(uid_flags_list, i);
2003                 free(data);
2004         }
2005         carray_free(uid_flags_list);
2006 }
2007
2008
2009
2010 static int imap_fetch(mailimap * imap,
2011                       uint32_t msg_index,
2012                       char ** result,
2013                       size_t * result_len)
2014 {
2015         int r;
2016         struct mailimap_set * set;
2017         struct mailimap_fetch_att * fetch_att;
2018         struct mailimap_fetch_type * fetch_type;
2019         clist * fetch_result;
2020         struct mailimap_msg_att * msg_att;
2021         struct mailimap_msg_att_item * msg_att_item;
2022         char * text;
2023         size_t text_length;
2024         int res;
2025         clistiter * cur;
2026         struct mailimap_section * section;
2027
2028         set = mailimap_set_new_single(msg_index);
2029         if (set == NULL) {
2030                 res = MAILIMAP_ERROR_MEMORY;
2031                 goto err;
2032         }
2033
2034         section = mailimap_section_new(NULL);
2035         if (section == NULL) {
2036                 res = MAILIMAP_ERROR_MEMORY;
2037                 goto free_set;
2038         }
2039   
2040         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2041         if (fetch_att == NULL) {
2042                 mailimap_section_free(section);
2043                 res = MAILIMAP_ERROR_MEMORY;
2044                 goto free_set;
2045         }
2046   
2047         fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2048         if (fetch_type == NULL) {
2049                 res = MAILIMAP_ERROR_MEMORY;
2050                 goto free_fetch_att;
2051         }
2052
2053         mailstream_logger = imap_logger_fetch;
2054         
2055         r = mailimap_uid_fetch(imap, set,
2056                                fetch_type, &fetch_result);
2057   
2058         mailstream_logger = imap_logger_cmd;
2059         
2060         mailimap_fetch_type_free(fetch_type);
2061         mailimap_set_free(set);
2062   
2063         switch (r) {
2064         case MAILIMAP_NO_ERROR:
2065                 break;
2066         default:
2067                 return r;
2068         }
2069   
2070         if (clist_begin(fetch_result) == NULL) {
2071                 mailimap_fetch_list_free(fetch_result);
2072                 return MAILIMAP_ERROR_FETCH;
2073         }
2074
2075         msg_att = clist_begin(fetch_result)->data;
2076
2077         text = NULL;
2078         text_length = 0;
2079
2080         for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2081             cur = clist_next(cur)) {
2082                 msg_att_item = clist_content(cur);
2083
2084                 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2085                         if (msg_att_item->att_data.att_static->att_type ==
2086                             MAILIMAP_MSG_ATT_BODY_SECTION) {
2087                                 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2088                                 /* detach */
2089                                 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2090                                 text_length =
2091                                         msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2092                         }
2093                 }
2094         }
2095
2096         mailimap_fetch_list_free(fetch_result);
2097
2098         if (text == NULL)
2099                 return MAILIMAP_ERROR_FETCH;
2100
2101         * result = text;
2102         * result_len = text_length;
2103   
2104         return MAILIMAP_NO_ERROR;
2105
2106  free_fetch_att:
2107         mailimap_fetch_att_free(fetch_att);
2108  free_set:
2109         mailimap_set_free(set);
2110  err:
2111         return res;
2112 }
2113
2114 static int imap_fetch_header(mailimap * imap,
2115                              uint32_t msg_index,
2116                              char ** result,
2117                              size_t * result_len)
2118 {
2119   int r;
2120   struct mailimap_set * set;
2121   struct mailimap_fetch_att * fetch_att;
2122   struct mailimap_fetch_type * fetch_type;
2123   clist * fetch_result;
2124   struct mailimap_msg_att * msg_att;
2125   struct mailimap_msg_att_item * msg_att_item;
2126   char * text;
2127   size_t text_length;
2128   int res;
2129   clistiter * cur;
2130   struct mailimap_section * section;
2131   
2132   set = mailimap_set_new_single(msg_index);
2133   if (set == NULL) {
2134     res = MAILIMAP_ERROR_MEMORY;
2135     goto err;
2136   }
2137
2138   section = mailimap_section_new_header();
2139   if (section == NULL) {
2140     res = MAILIMAP_ERROR_MEMORY;
2141     goto free_set;
2142   }
2143   
2144   fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2145   if (fetch_att == NULL) {
2146     mailimap_section_free(section);
2147     res = MAILIMAP_ERROR_MEMORY;
2148     goto free_set;
2149   }
2150   
2151   fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2152   if (fetch_type == NULL) {
2153     res = MAILIMAP_ERROR_MEMORY;
2154     goto free_fetch_att;
2155   }
2156
2157   mailstream_logger = imap_logger_fetch;
2158   
2159   r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2160   
2161   mailstream_logger = imap_logger_cmd;
2162   mailimap_fetch_type_free(fetch_type);
2163   mailimap_set_free(set);
2164
2165   switch (r) {
2166   case MAILIMAP_NO_ERROR:
2167     break;
2168   default:
2169     return r;
2170   }
2171
2172   if (clist_begin(fetch_result) == NULL) {
2173     mailimap_fetch_list_free(fetch_result);
2174     return MAILIMAP_ERROR_FETCH;
2175   }
2176
2177   msg_att = clist_begin(fetch_result)->data;
2178
2179   text = NULL;
2180   text_length = 0;
2181
2182   for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2183       cur = clist_next(cur)) {
2184     msg_att_item = clist_content(cur);
2185
2186     if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2187       if (msg_att_item->att_data.att_static->att_type ==
2188           MAILIMAP_MSG_ATT_BODY_SECTION) {
2189         text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2190         msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2191         text_length =
2192           msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2193       }
2194     }
2195   }
2196
2197   mailimap_fetch_list_free(fetch_result);
2198
2199   if (text == NULL)
2200     return MAILIMAP_ERROR_FETCH;
2201
2202   * result = text;
2203   * result_len = text_length;
2204
2205   return MAILIMAP_NO_ERROR;
2206
2207  free_fetch_att:
2208   mailimap_fetch_att_free(fetch_att);
2209  free_set:
2210   mailimap_set_free(set);
2211  err:
2212   return res;
2213 }
2214
2215
2216
2217 struct fetch_content_param {
2218         mailimap * imap;
2219         uint32_t msg_index;
2220         const char * filename;
2221         int with_body;
2222 };
2223
2224 struct fetch_content_result {
2225         int error;
2226 };
2227
2228 static void fetch_content_run(struct etpan_thread_op * op)
2229 {
2230         struct fetch_content_param * param;
2231         struct fetch_content_result * result;
2232         char * content;
2233         size_t content_size;
2234         int r;
2235         int fd;
2236         FILE * f;
2237         
2238         param = op->param;
2239         result = op->result;
2240
2241         CHECK_IMAP();
2242
2243         content = NULL;
2244         content_size = 0;
2245         if (param->with_body)
2246                 r = imap_fetch(param->imap, param->msg_index,
2247                                &content, &content_size);
2248         else
2249                 r = imap_fetch_header(param->imap, param->msg_index,
2250                                       &content, &content_size);
2251         
2252         result->error = r;
2253         
2254         if (r == MAILIMAP_NO_ERROR) {
2255                 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2256                 if (fd < 0) {
2257                         result->error = MAILIMAP_ERROR_FETCH;
2258                         goto free;
2259                 }
2260                 
2261                 f = fdopen(fd, "wb");
2262                 if (f == NULL) {
2263                         result->error = MAILIMAP_ERROR_FETCH;
2264                         goto close;
2265                 }
2266                 
2267                 r = fwrite(content, 1, content_size, f);
2268                 if (r == 0) {
2269                         goto fclose;
2270                 }
2271                 
2272                 r = fclose(f);
2273                 if (r == EOF) {
2274                         g_unlink(param->filename);
2275                         goto close;
2276                 }
2277                 goto free;
2278                 
2279         fclose:
2280                 fclose(f);
2281                 goto unlink;
2282         close:
2283                 close(fd);
2284         unlink:
2285                 g_unlink(param->filename);
2286         
2287         free:
2288                 if (mmap_string_unref(content) != 0)
2289                         free(content);
2290         }
2291         
2292         debug_print("imap fetch_content run - end %i\n", r);
2293 }
2294
2295 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2296                                 int with_body,
2297                                 const char * filename)
2298 {
2299         struct fetch_content_param param;
2300         struct fetch_content_result result;
2301         mailimap * imap;
2302         
2303         debug_print("imap fetch_content - begin\n");
2304         
2305         imap = get_imap(folder);
2306         param.imap = imap;
2307         param.msg_index = msg_index;
2308         param.filename = filename;
2309         param.with_body = with_body;
2310         
2311         threaded_run(folder, &param, &result, fetch_content_run);
2312         
2313         if (result.error != MAILIMAP_NO_ERROR)
2314                 return result.error;
2315         
2316         debug_print("imap fetch_content - end\n");
2317         
2318         return result.error;
2319 }
2320
2321
2322
2323 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
2324 {
2325         int flags;
2326         clist * flag_list;
2327         clistiter * cur;
2328         
2329         flags = MSG_UNREAD;
2330         
2331         flag_list = att_dyn->att_list;
2332         if (flag_list == NULL)
2333                 return flags;
2334         
2335         for(cur = clist_begin(flag_list) ; cur != NULL ;
2336             cur = clist_next(cur)) {
2337                 struct mailimap_flag_fetch * flag_fetch;
2338                         
2339                 flag_fetch = clist_content(cur);
2340                 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2341                         flags |= MSG_NEW;
2342                 else {
2343                         switch (flag_fetch->fl_flag->fl_type) {
2344                         case MAILIMAP_FLAG_ANSWERED:
2345                                 flags |= MSG_REPLIED;
2346                                 break;
2347                         case MAILIMAP_FLAG_FLAGGED:
2348                                 flags |= MSG_MARKED;
2349                                 break;
2350                         case MAILIMAP_FLAG_DELETED:
2351                                 flags |= MSG_DELETED;
2352                                 break;
2353                         case MAILIMAP_FLAG_SEEN:
2354                                 flags &= ~MSG_UNREAD;
2355                                 flags &= ~MSG_NEW;
2356                                 break;
2357                         }
2358                 }
2359         }
2360         
2361         return flags;
2362 }
2363
2364 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2365                                  uint32_t * puid,
2366                                  char ** pheaders,
2367                                  size_t * pref_size,
2368                                  struct mailimap_msg_att_dynamic ** patt_dyn)
2369 {
2370   clistiter * item_cur;
2371   uint32_t uid;
2372   char * headers;
2373   size_t ref_size;
2374   struct mailimap_msg_att_dynamic * att_dyn;
2375
2376   uid = 0;
2377   headers = NULL;
2378   ref_size = 0;
2379   att_dyn = NULL;
2380
2381   for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2382       item_cur = clist_next(item_cur)) {
2383     struct mailimap_msg_att_item * item;
2384
2385     item = clist_content(item_cur);
2386       
2387     switch (item->att_type) {
2388     case MAILIMAP_MSG_ATT_ITEM_STATIC:
2389       switch (item->att_data.att_static->att_type) {
2390       case MAILIMAP_MSG_ATT_UID:
2391         uid = item->att_data.att_static->att_data.att_uid;
2392         break;
2393
2394       case MAILIMAP_MSG_ATT_BODY_SECTION:
2395         if (headers == NULL) {
2396           headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2397         }
2398         break;
2399       case MAILIMAP_MSG_ATT_RFC822_SIZE:
2400               ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2401               break;
2402       }
2403       break;
2404       
2405     case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2406       if (att_dyn == NULL) {
2407         att_dyn = item->att_data.att_dyn;
2408       }
2409       break;
2410     }
2411   }
2412
2413   if (puid != NULL)
2414     * puid = uid;
2415   if (pheaders != NULL)
2416     * pheaders = headers;
2417   if (pref_size != NULL)
2418     * pref_size = ref_size;
2419   if (patt_dyn != NULL)
2420     * patt_dyn = att_dyn;
2421
2422   return MAIL_NO_ERROR;
2423 }
2424
2425 static struct imap_fetch_env_info *
2426 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2427 {
2428         struct imap_fetch_env_info * info;
2429         uint32_t uid;
2430         char * headers;
2431         size_t size;
2432         struct mailimap_msg_att_dynamic * att_dyn;
2433         
2434         imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2435                               &att_dyn);
2436         
2437         if (!headers)
2438                 return NULL;
2439         info = malloc(sizeof(* info));
2440         info->uid = uid;
2441         info->headers = strdup(headers);
2442         info->size = size;
2443         info->flags = imap_flags_to_flags(att_dyn);
2444         
2445         return info;
2446 }
2447
2448 static int
2449 imap_fetch_result_to_envelop_list(clist * fetch_result,
2450                                   carray ** p_env_list)
2451 {
2452         clistiter * cur;
2453         unsigned int i;
2454         carray * env_list;
2455         i = 0;
2456   
2457         env_list = carray_new(16);
2458   
2459         for(cur = clist_begin(fetch_result) ; cur != NULL ;
2460             cur = clist_next(cur)) {
2461                 struct mailimap_msg_att * msg_att;
2462                 struct imap_fetch_env_info * env_info;
2463     
2464                 msg_att = clist_content(cur);
2465
2466                 env_info = fetch_to_env_info(msg_att);
2467                 if (!env_info)
2468                         return MAILIMAP_ERROR_MEMORY;
2469                 carray_add(env_list, env_info, NULL);
2470         }
2471   
2472         * p_env_list = env_list;
2473   
2474         return MAIL_NO_ERROR;
2475 }
2476
2477 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2478 {
2479         struct mailimap_fetch_att * fetch_att;
2480         int r;
2481         char * header;
2482         clist * hdrlist;
2483         struct mailimap_header_list * imap_hdrlist;
2484         struct mailimap_section * section;
2485
2486         hdrlist = clist_new();
2487   
2488         header = strdup("Date");
2489         r = clist_append(hdrlist, header);
2490         header = strdup("From");
2491         r = clist_append(hdrlist, header);
2492         header = strdup("To");
2493         r = clist_append(hdrlist, header);
2494         header = strdup("Cc");
2495         r = clist_append(hdrlist, header);
2496         header = strdup("Subject");
2497         r = clist_append(hdrlist, header);
2498         header = strdup("Message-ID");
2499         r = clist_append(hdrlist, header);
2500         header = strdup("References");
2501         r = clist_append(hdrlist, header);
2502         header = strdup("In-Reply-To");
2503         r = clist_append(hdrlist, header);
2504   
2505         imap_hdrlist = mailimap_header_list_new(hdrlist);
2506         section = mailimap_section_new_header_fields(imap_hdrlist);
2507         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2508         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2509   
2510         return MAIL_NO_ERROR;
2511 }
2512
2513 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2514 {
2515         struct mailimap_fetch_att * fetch_att;
2516         struct mailimap_section * section;
2517         
2518         section = mailimap_section_new_header();
2519         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2520         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2521         
2522         return MAIL_NO_ERROR;
2523 }
2524
2525 static int
2526 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2527                         carray ** p_env_list)
2528 {
2529         struct mailimap_fetch_att * fetch_att;
2530         struct mailimap_fetch_type * fetch_type;
2531         int res;
2532         clist * fetch_result;
2533         int r;
2534         carray * env_list = NULL;
2535         chashdatum key;
2536         chashdatum value;
2537         
2538         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2539   
2540         /* uid */
2541         fetch_att = mailimap_fetch_att_new_uid();
2542         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2543   
2544         /* flags */
2545         fetch_att = mailimap_fetch_att_new_flags();
2546         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2547   
2548         /* rfc822 size */
2549         fetch_att = mailimap_fetch_att_new_rfc822_size();
2550         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2551   
2552         /* headers */
2553         key.data = &imap;
2554         key.len = sizeof(imap);
2555         r = chash_get(courier_workaround_hash, &key, &value);
2556         if (r < 0)
2557                 r = imap_add_envelope_fetch_att(fetch_type);
2558         else
2559                 r = imap_add_header_fetch_att(fetch_type);
2560         
2561         mailstream_logger = imap_logger_fetch;
2562         
2563         r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2564         
2565         mailstream_logger = imap_logger_cmd;
2566         switch (r) {
2567         case MAILIMAP_NO_ERROR:
2568                 break;
2569         default:
2570                 mailimap_fetch_type_free(fetch_type);
2571                 debug_print("uid_fetch: %d\n", r);
2572                 return r;
2573         }
2574         
2575         if (clist_begin(fetch_result) == NULL) {
2576                 res = MAILIMAP_ERROR_FETCH;
2577                 debug_print("clist_begin = NULL\n");
2578                 goto err;
2579         }
2580         
2581         r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2582         mailimap_fetch_list_free(fetch_result);
2583         
2584         if (r != MAILIMAP_NO_ERROR) {
2585                 mailimap_fetch_type_free(fetch_type);
2586                 res = MAILIMAP_ERROR_MEMORY;
2587                 debug_print("fetch_result_to_envelop_list: %d\n", res);
2588                 goto err;
2589         }
2590         
2591         mailimap_fetch_type_free(fetch_type);
2592         
2593         * p_env_list = env_list;
2594         
2595         return MAILIMAP_NO_ERROR;
2596   
2597  err:
2598         return res;
2599 }
2600
2601 struct fetch_env_param {
2602         mailimap * imap;
2603         struct mailimap_set * set;
2604 };
2605
2606 struct fetch_env_result {
2607         carray * fetch_env_result;
2608         int error;
2609 };
2610
2611 static void fetch_env_run(struct etpan_thread_op * op)
2612 {
2613         struct fetch_env_param * param;
2614         struct fetch_env_result * result;
2615         carray * env_list;
2616         int r;
2617         
2618         param = op->param;
2619         result = op->result;
2620
2621         CHECK_IMAP();
2622
2623         env_list = NULL;
2624         r = imap_get_envelopes_list(param->imap, param->set,
2625                                     &env_list);
2626         
2627         result->error = r;
2628         result->fetch_env_result = env_list;
2629         
2630         debug_print("imap fetch_env run - end %i\n", r);
2631 }
2632
2633 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2634                             carray ** p_env_list)
2635 {
2636         struct fetch_env_param param;
2637         struct fetch_env_result result;
2638         mailimap * imap;
2639         
2640         debug_print("imap fetch_env - begin\n");
2641         
2642         imap = get_imap(folder);
2643         param.imap = imap;
2644         param.set = set;
2645         
2646         threaded_run(folder, &param, &result, fetch_env_run);
2647         
2648         if (result.error != MAILIMAP_NO_ERROR) {
2649                 chashdatum key;
2650                 chashdatum value;
2651                 int r;
2652                 
2653                 key.data = &imap;
2654                 key.len = sizeof(imap);
2655                 r = chash_get(courier_workaround_hash, &key, &value);
2656                 if (r < 0) {
2657                         value.data = NULL;
2658                         value.len = 0;
2659                         chash_set(courier_workaround_hash, &key, &value, NULL);
2660                         
2661                         threaded_run(folder, &param, &result, fetch_env_run);
2662                 }
2663         }
2664         
2665         if (result.error != MAILIMAP_NO_ERROR)
2666                 return result.error;
2667         
2668         debug_print("imap fetch_env - end\n");
2669         
2670         * p_env_list = result.fetch_env_result;
2671         
2672         return result.error;
2673 }
2674
2675 void imap_fetch_env_free(carray * env_list)
2676 {
2677         unsigned int i;
2678         
2679         for(i = 0 ; i < carray_count(env_list) ; i ++) {
2680                 struct imap_fetch_env_info * env_info;
2681                 
2682                 env_info = carray_get(env_list, i);
2683                 free(env_info->headers);
2684                 free(env_info);
2685         }
2686         carray_free(env_list);
2687 }
2688
2689
2690
2691
2692
2693 struct append_param {
2694         mailimap * imap;
2695         const char * mailbox;
2696         const char * filename;
2697         struct mailimap_flag_list * flag_list;
2698 };
2699
2700 struct append_result {
2701         int error;
2702         int uid;
2703 };
2704
2705 static void append_run(struct etpan_thread_op * op)
2706 {
2707         struct append_param * param;
2708         struct append_result * result;
2709         int r;
2710         char * data;
2711         size_t size;
2712         struct stat stat_buf;
2713         int fd;
2714         guint32 uid = 0, val = 0;
2715         
2716         param = op->param;
2717         result = op->result;
2718         
2719         CHECK_IMAP();
2720
2721         r = stat(param->filename, &stat_buf);
2722         if (r < 0) {
2723                 result->error = MAILIMAP_ERROR_APPEND;
2724                 return;
2725         }
2726         size = stat_buf.st_size;
2727         
2728         fd = open(param->filename, O_RDONLY);
2729         if (fd < 0) {
2730                 result->error = MAILIMAP_ERROR_APPEND;
2731                 return;
2732         }
2733         
2734         data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2735         if (data == (void *) MAP_FAILED) {
2736                 close(fd);
2737                 result->error = MAILIMAP_ERROR_APPEND;
2738                 return;
2739         }
2740         
2741         mailstream_logger = imap_logger_append;
2742         
2743         r = mailimap_uidplus_append(param->imap, param->mailbox,
2744                             param->flag_list, NULL,
2745                             data, size, &val, &uid);
2746
2747         mailstream_logger = imap_logger_cmd;
2748         
2749         munmap(data, size);
2750         close(fd);
2751         
2752         result->error = r;
2753         result->uid = uid;
2754         debug_print("imap append run - end %i uid %d\n", r, uid);
2755 }
2756
2757 int imap_threaded_append(Folder * folder, const char * mailbox,
2758                          const char * filename,
2759                          struct mailimap_flag_list * flag_list,
2760                          int *uid)
2761 {
2762         struct append_param param;
2763         struct append_result result;
2764         mailimap * imap;
2765         
2766         debug_print("imap append - begin\n");
2767         
2768         imap = get_imap(folder);
2769         param.imap = imap;
2770         param.mailbox = mailbox;
2771         param.filename = filename;
2772         param.flag_list = flag_list;
2773         
2774         threaded_run(folder, &param, &result, append_run);
2775         
2776         if (result.error != MAILIMAP_NO_ERROR)
2777                 return result.error;
2778         
2779         debug_print("imap append - end\n");
2780         if (uid != NULL)
2781                 *uid = result.uid;
2782
2783         return result.error;
2784 }
2785
2786
2787
2788
2789 struct expunge_param {
2790         mailimap * imap;
2791 };
2792
2793 struct expunge_result {
2794         int error;
2795 };
2796
2797 static void expunge_run(struct etpan_thread_op * op)
2798 {
2799         struct expunge_param * param;
2800         struct expunge_result * result;
2801         int r;
2802         
2803         param = op->param;
2804         result = op->result;
2805
2806         CHECK_IMAP();
2807
2808         r = mailimap_expunge(param->imap);
2809         
2810         result->error = r;
2811         debug_print("imap expunge run - end %i\n", r);
2812 }
2813
2814 int imap_threaded_expunge(Folder * folder)
2815 {
2816         struct expunge_param param;
2817         struct expunge_result result;
2818         
2819         debug_print("imap expunge - begin\n");
2820         
2821         param.imap = get_imap(folder);
2822         
2823         threaded_run(folder, &param, &result, expunge_run);
2824         
2825         debug_print("imap expunge - end\n");
2826         
2827         return result.error;
2828 }
2829
2830
2831 struct copy_param {
2832         mailimap * imap;
2833         struct mailimap_set * set;
2834         const char * mb;
2835 };
2836
2837 struct copy_result {
2838         int error;
2839         struct mailimap_set *source;
2840         struct mailimap_set *dest;
2841 };
2842
2843 static void copy_run(struct etpan_thread_op * op)
2844 {
2845         struct copy_param * param;
2846         struct copy_result * result;
2847         int r;
2848         guint32 val;
2849         struct mailimap_set *source = NULL, *dest = NULL;
2850
2851         param = op->param;
2852         result = op->result;
2853
2854         CHECK_IMAP();
2855
2856         r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2857                 &val, &source, &dest);
2858         
2859         result->error = r;
2860         if (r == 0) {
2861                 result->source = source;
2862                 result->dest = dest;
2863         } else {
2864                 result->source = NULL;
2865                 result->dest = NULL;
2866         }
2867         debug_print("imap copy run - end %i\n", r);
2868 }
2869
2870 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2871                        const char * mb, struct mailimap_set **source,
2872                        struct mailimap_set **dest)
2873 {
2874         struct copy_param param;
2875         struct copy_result result;
2876         mailimap * imap;
2877         
2878         debug_print("imap copy - begin\n");
2879         
2880         imap = get_imap(folder);
2881         param.imap = imap;
2882         param.set = set;
2883         param.mb = mb;
2884         
2885         threaded_run(folder, &param, &result, copy_run);
2886         *source = NULL;
2887         *dest = NULL;
2888         
2889         if (result.error != MAILIMAP_NO_ERROR)
2890                 return result.error;
2891         
2892         *source = result.source;
2893         *dest = result.dest;
2894
2895         debug_print("imap copy - end\n");
2896         
2897         return result.error;
2898 }
2899
2900
2901
2902 struct store_param {
2903         mailimap * imap;
2904         struct mailimap_set * set;
2905         struct mailimap_store_att_flags * store_att_flags;
2906 };
2907
2908 struct store_result {
2909         int error;
2910 };
2911
2912 static void store_run(struct etpan_thread_op * op)
2913 {
2914         struct store_param * param;
2915         struct store_result * result;
2916         int r;
2917         
2918         param = op->param;
2919         result = op->result;
2920
2921         CHECK_IMAP();
2922
2923         r = mailimap_uid_store(param->imap, param->set,
2924                                param->store_att_flags);
2925         
2926         result->error = r;
2927         
2928         debug_print("imap store run - end %i\n", r);
2929 }
2930
2931 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2932                         struct mailimap_store_att_flags * store_att_flags)
2933 {
2934         struct store_param param;
2935         struct store_result result;
2936         mailimap * imap;
2937         
2938         debug_print("imap store - begin\n");
2939         
2940         imap = get_imap(folder);
2941         param.imap = imap;
2942         param.set = set;
2943         param.store_att_flags = store_att_flags;
2944         
2945         threaded_run(folder, &param, &result, store_run);
2946         
2947         if (result.error != MAILIMAP_NO_ERROR)
2948                 return result.error;
2949         
2950         debug_print("imap store - end\n");
2951         
2952         return result.error;
2953 }
2954
2955
2956 #define ENV_BUFFER_SIZE 512
2957
2958 static void do_exec_command(int fd, const char * command,
2959                             const char * servername, uint16_t port)
2960 {
2961         int i, maxopen;
2962 #ifdef SOLARIS
2963         char env_buffer[ENV_BUFFER_SIZE];
2964 #endif
2965         
2966         if (fork() > 0) {
2967                 /* Fork again to become a child of init rather than
2968                    the etpan client. */
2969                 exit(0);
2970         }
2971   
2972 #ifdef SOLARIS
2973         if (servername)
2974                 snprintf(env_buffer, ENV_BUFFER_SIZE,
2975                          "ETPANSERVER=%s", servername);
2976         else
2977                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2978         putenv(env_buffer);
2979 #else
2980         if (servername)
2981                 setenv("ETPANSERVER", servername, 1);
2982         else
2983                 unsetenv("ETPANSERVER");
2984 #endif
2985   
2986 #ifdef SOLARIS
2987         if (port)
2988                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2989         else
2990                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2991         putenv(env_buffer);
2992 #else
2993         if (port) {
2994                 char porttext[20];
2995                 
2996                 snprintf(porttext, sizeof(porttext), "%d", port);
2997                 setenv("ETPANPORT", porttext, 1);
2998         }
2999         else {
3000                 unsetenv("ETPANPORT");
3001         }
3002 #endif
3003                 
3004         /* Not a lot we can do if there's an error other than bail. */
3005         if (dup2(fd, 0) == -1)
3006                 exit(1);
3007         if (dup2(fd, 1) == -1)
3008                 exit(1);
3009   
3010         /* Should we close stderr and reopen /dev/null? */
3011   
3012         maxopen = sysconf(_SC_OPEN_MAX);
3013         for (i=3; i < maxopen; i++)
3014                 close(i);
3015   
3016 #ifdef TIOCNOTTY
3017         /* Detach from the controlling tty if we have one. Otherwise,
3018            SSH might do something stupid like trying to use it instead
3019            of running $SSH_ASKPASS. Doh. */
3020         fd = open("/dev/tty", O_RDONLY);
3021         if (fd != -1) {
3022                 ioctl(fd, TIOCNOTTY, NULL);
3023                 close(fd);
3024         }
3025 #endif /* TIOCNOTTY */
3026
3027         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3028   
3029         /* Eep. Shouldn't reach this */
3030         exit(1);
3031 }
3032
3033 static int subcommand_connect(const char *command,
3034                               const char *servername, uint16_t port)
3035 {
3036         /* SEB unsupported on Windows */
3037         int sockfds[2];
3038         pid_t childpid;
3039   
3040         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3041                 return -1;
3042   
3043         childpid = fork();
3044         if (!childpid) {
3045                 do_exec_command(sockfds[1], command, servername, port);
3046         }
3047         else if (childpid == -1) {
3048                 close(sockfds[0]);
3049                 close(sockfds[1]);
3050                 return -1;
3051         }
3052   
3053         close(sockfds[1]);
3054   
3055         /* Reap child, leaving grandchild process to run */
3056         waitpid(childpid, NULL, 0);
3057   
3058         return sockfds[0];
3059 }
3060
3061 static int socket_connect_cmd(mailimap * imap, const char * command,
3062                        const char * server, int port)
3063 {
3064         int fd;
3065         mailstream * s;
3066         int r;
3067         
3068         fd = subcommand_connect(command, server, port);
3069         if (fd < 0)
3070                 return MAILIMAP_ERROR_STREAM;
3071         
3072         s = mailstream_socket_open(fd);
3073         if (s == NULL) {
3074                 close(fd);
3075                 return MAILIMAP_ERROR_STREAM;
3076         }
3077         
3078         r = mailimap_connect(imap, s);
3079         if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3080         &&  r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3081                 mailstream_close(s);
3082                 if (imap)
3083                         imap->imap_stream = NULL;
3084                 return r;
3085         }
3086         
3087         return r;
3088 }
3089
3090 /* connect cmd */
3091
3092 struct connect_cmd_param {
3093         mailimap * imap;
3094         const char * command;
3095         const char * server;
3096         int port;
3097 };
3098
3099 struct connect_cmd_result {
3100         int error;
3101 };
3102
3103 static void connect_cmd_run(struct etpan_thread_op * op)
3104 {
3105         int r;
3106         struct connect_cmd_param * param;
3107         struct connect_cmd_result * result;
3108         
3109         param = op->param;
3110         result = op->result;
3111         
3112         CHECK_IMAP();
3113
3114         r = socket_connect_cmd(param->imap, param->command,
3115                                param->server, param->port);
3116         
3117         result->error = r;
3118 }
3119
3120
3121 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3122                               const char * server, int port)
3123 {
3124         struct connect_cmd_param param;
3125         struct connect_cmd_result result;
3126         chashdatum key;
3127         chashdatum value;
3128         mailimap * imap, * oldimap;
3129         
3130         oldimap = get_imap(folder);
3131
3132         imap = mailimap_new(0, NULL);
3133         
3134         if (oldimap) {
3135                 debug_print("deleting old imap %p\n", oldimap);
3136                 delete_imap(folder, oldimap);
3137         }
3138
3139         key.data = &folder;
3140         key.len = sizeof(folder);
3141         value.data = imap;
3142         value.len = 0;
3143         chash_set(session_hash, &key, &value, NULL);
3144         
3145         param.imap = imap;
3146         param.command = command;
3147         param.server = server;
3148         param.port = port;
3149         
3150         threaded_run(folder, &param, &result, connect_cmd_run);
3151         
3152         debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3153         
3154         return result.error;
3155 }
3156
3157 void imap_threaded_cancel(Folder * folder)
3158 {
3159         mailimap * imap;
3160         
3161         imap = get_imap(folder);
3162         if (imap->imap_stream != NULL)
3163                 mailstream_cancel(imap->imap_stream);
3164 }
3165
3166 #else
3167
3168 void imap_main_init(void)
3169 {
3170 }
3171 void imap_main_done(void)
3172 {
3173 }
3174 void imap_main_set_timeout(int sec)
3175 {
3176 }
3177
3178 void imap_threaded_cancel(Folder * folder);
3179 {
3180 }
3181
3182 #endif