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