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