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