f3d0d192f963f724544fbd2401cdb3b197434e7b
[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;
1141         struct mailimap_search_key * search_type_key;
1142         clist * search_result;
1143         
1144         param = op->param;
1145         
1146         /* we copy the mailimap_set because freeing the key is recursive */
1147         uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1148         
1149         search_type_key = NULL;
1150         switch (param->type) {
1151         case IMAP_SEARCH_TYPE_SIMPLE:
1152                 search_type_key = NULL;
1153                 break;
1154                 
1155         case IMAP_SEARCH_TYPE_SEEN:
1156                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1157                                                           NULL, NULL, NULL, NULL, NULL,
1158                                                           NULL, NULL, NULL, NULL, NULL,
1159                                                           NULL, NULL, NULL, NULL, 0,
1160                                                           NULL, NULL, NULL, NULL, NULL,
1161                                                           NULL, 0, NULL, NULL, NULL);
1162                 break;
1163                 
1164         case IMAP_SEARCH_TYPE_UNSEEN:
1165                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1166                                                           NULL, NULL, NULL, NULL, NULL,
1167                                                           NULL, NULL, NULL, NULL, NULL,
1168                                                           NULL, NULL, NULL, NULL, 0,
1169                                                           NULL, NULL, NULL, NULL, NULL,
1170                                                           NULL, 0, NULL, NULL, NULL);
1171                 break;
1172                 
1173         case IMAP_SEARCH_TYPE_ANSWERED:
1174                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1175                                                           NULL, NULL, NULL, NULL, NULL,
1176                                                           NULL, NULL, NULL, NULL, NULL,
1177                                                           NULL, NULL, NULL, NULL, 0,
1178                                                           NULL, NULL, NULL, NULL, NULL,
1179                                                           NULL, 0, NULL, NULL, NULL);
1180                 break;
1181                 
1182         case IMAP_SEARCH_TYPE_FLAGGED:
1183                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1184                                                           NULL, NULL, NULL, NULL, NULL,
1185                                                           NULL, NULL, NULL, NULL, NULL,
1186                                                           NULL, NULL, NULL, NULL, 0,
1187                                                           NULL, NULL, NULL, NULL, NULL,
1188                                                           NULL, 0, NULL, NULL, NULL);
1189                 break;
1190         case IMAP_SEARCH_TYPE_DELETED:
1191                 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1192                                                           NULL, NULL, NULL, NULL, NULL,
1193                                                           NULL, NULL, NULL, NULL, NULL,
1194                                                           NULL, NULL, NULL, NULL, 0,
1195                                                           NULL, NULL, NULL, NULL, NULL,
1196                                                           NULL, 0, NULL, NULL, NULL);
1197                 break;
1198         }
1199         
1200         if (search_type_key != NULL) {
1201                 key = mailimap_search_key_new_multiple_empty();
1202                 mailimap_search_key_multiple_add(key, search_type_key);
1203                 mailimap_search_key_multiple_add(key, uid_key);
1204         }
1205         else {
1206                 key = uid_key;
1207         }
1208         
1209         r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1210         
1211         /* free the key (with the imapset) */
1212         mailimap_search_key_free(key);
1213
1214         result = op->result;
1215         result->error = r;
1216         result->search_result = search_result;
1217         debug_print("imap search run - end %i\n", r);
1218 }
1219
1220 int imap_threaded_search(Folder * folder, int search_type,
1221                          struct mailimap_set * set, clist ** search_result)
1222 {
1223         struct search_param param;
1224         struct search_result result;
1225         mailimap * imap;
1226         
1227         debug_print("imap search - begin\n");
1228
1229         imap = get_imap(folder);
1230         param.imap = imap;
1231         param.set = set;
1232         param.type = search_type;
1233         
1234         threaded_run(folder, &param, &result, search_run);
1235         
1236         if (result.error != MAILIMAP_NO_ERROR)
1237                 return result.error;
1238         
1239         debug_print("imap search - end\n");
1240         
1241         * search_result = result.search_result;
1242         
1243         return result.error;
1244 }
1245
1246
1247
1248
1249 static int
1250 uid_list_to_env_list(clist * fetch_result, carray ** result)
1251 {
1252         clistiter * cur;
1253         int r;
1254         int res;
1255         carray * tab;
1256         unsigned int i;
1257         
1258         tab = carray_new(128);
1259         if (tab == NULL) {
1260                 res = MAILIMAP_ERROR_MEMORY;
1261                 goto err;
1262         }
1263         
1264         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1265             cur = clist_next(cur)) {
1266                 struct mailimap_msg_att * msg_att;
1267                 clistiter * item_cur;
1268                 uint32_t uid;
1269                 size_t size;
1270                 uint32_t * puid;
1271                 
1272                 msg_att = clist_content(cur);
1273                 
1274                 uid = 0;
1275                 size = 0;
1276                 for(item_cur = clist_begin(msg_att->att_list) ;
1277                     item_cur != NULL ;
1278                     item_cur = clist_next(item_cur)) {
1279                         struct mailimap_msg_att_item * item;
1280                         
1281                         item = clist_content(item_cur);
1282                         
1283                         switch (item->att_type) {
1284                         case MAILIMAP_MSG_ATT_ITEM_STATIC:
1285                                 switch (item->att_data.att_static->att_type) {
1286                                 case MAILIMAP_MSG_ATT_UID:
1287                                         uid = item->att_data.att_static->att_data.att_uid;
1288                                         break;
1289                                 }
1290                         }
1291                 }
1292                 
1293                 puid = malloc(sizeof(* puid));
1294                 if (puid == NULL) {
1295                         res = MAILIMAP_ERROR_MEMORY;
1296                         goto free_list;
1297                 }
1298                 * puid = uid;
1299                         
1300                 r = carray_add(tab, puid, NULL);
1301                 if (r < 0) {
1302                         free(puid);
1303                         res = MAILIMAP_ERROR_MEMORY;
1304                         goto free_list;
1305                 }
1306         }
1307                 
1308         * result = tab;
1309
1310         return MAILIMAP_NO_ERROR;
1311   
1312  free_list:
1313         for(i = 0 ; i < carray_count(tab) ; i++)
1314                 mailmessage_free(carray_get(tab, i));
1315  err:
1316         return res;
1317 }
1318
1319 static int imap_get_messages_list(mailimap * imap,
1320                                   uint32_t first_index,
1321                                   carray ** result)
1322 {
1323         carray * env_list;
1324         int r;
1325         struct mailimap_fetch_att * fetch_att;
1326         struct mailimap_fetch_type * fetch_type;
1327         struct mailimap_set * set;
1328         clist * fetch_result;
1329         int res;
1330         
1331         set = mailimap_set_new_interval(first_index, 0);
1332         if (set == NULL) {
1333                 res = MAILIMAP_ERROR_MEMORY;
1334                 goto err;
1335         }
1336
1337         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1338         if (fetch_type == NULL) {
1339                 res = MAILIMAP_ERROR_MEMORY;
1340                 goto free_set;
1341         }
1342
1343         fetch_att = mailimap_fetch_att_new_uid();
1344         if (fetch_att == NULL) {
1345                 res = MAILIMAP_ERROR_MEMORY;
1346                 goto free_fetch_type;
1347         }
1348
1349         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1350         if (r != MAILIMAP_NO_ERROR) {
1351                 mailimap_fetch_att_free(fetch_att);
1352                 res = MAILIMAP_ERROR_MEMORY;
1353                 goto free_fetch_type;
1354         }
1355
1356         r = mailimap_uid_fetch(imap, set,
1357                                fetch_type, &fetch_result);
1358
1359         mailimap_fetch_type_free(fetch_type);
1360         mailimap_set_free(set);
1361
1362         if (r != MAILIMAP_NO_ERROR) {
1363                 res = r;
1364                 goto err;
1365         }
1366
1367         env_list = NULL;
1368         r = uid_list_to_env_list(fetch_result, &env_list);
1369         mailimap_fetch_list_free(fetch_result);
1370         
1371         * result = env_list;
1372
1373         return MAILIMAP_NO_ERROR;
1374
1375  free_fetch_type:
1376         mailimap_fetch_type_free(fetch_type);
1377  free_set:
1378         mailimap_set_free(set);
1379  err:
1380         return res;
1381 }
1382
1383
1384
1385
1386 struct fetch_uid_param {
1387         mailimap * imap;
1388         uint32_t first_index;
1389 };
1390
1391 struct fetch_uid_result {
1392         int error;
1393         carray * fetch_result;
1394 };
1395
1396 static void fetch_uid_run(struct etpan_thread_op * op)
1397 {
1398         struct fetch_uid_param * param;
1399         struct fetch_uid_result * result;
1400         carray * fetch_result;
1401         int r;
1402         
1403         param = op->param;
1404         
1405         fetch_result = NULL;
1406         r = imap_get_messages_list(param->imap, param->first_index,
1407                                    &fetch_result);
1408         
1409         result = op->result;
1410         result->error = r;
1411         result->fetch_result = fetch_result;
1412         debug_print("imap fetch_uid run - end %i\n", r);
1413 }
1414
1415 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1416                             carray ** fetch_result)
1417 {
1418         struct fetch_uid_param param;
1419         struct fetch_uid_result result;
1420         mailimap * imap;
1421         
1422         debug_print("imap fetch_uid - begin\n");
1423         
1424         imap = get_imap(folder);
1425         param.imap = imap;
1426         param.first_index = first_index;
1427         
1428         threaded_run(folder, &param, &result, fetch_uid_run);
1429         
1430         if (result.error != MAILIMAP_NO_ERROR)
1431                 return result.error;
1432         
1433         debug_print("imap fetch_uid - end\n");
1434         
1435         * fetch_result = result.fetch_result;
1436         
1437         return result.error;
1438 }
1439
1440
1441 void imap_fetch_uid_list_free(carray * uid_list)
1442 {
1443         unsigned int i;
1444         
1445         for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1446                 uint32_t * puid;
1447                 
1448                 puid = carray_get(uid_list, i);
1449                 free(puid);
1450         }
1451         carray_free(uid_list);
1452 }
1453
1454
1455
1456 static int imap_fetch(mailimap * imap,
1457                       uint32_t msg_index,
1458                       char ** result,
1459                       size_t * result_len)
1460 {
1461         int r;
1462         struct mailimap_set * set;
1463         struct mailimap_fetch_att * fetch_att;
1464         struct mailimap_fetch_type * fetch_type;
1465         clist * fetch_result;
1466         struct mailimap_msg_att * msg_att;
1467         struct mailimap_msg_att_item * msg_att_item;
1468         char * text;
1469         size_t text_length;
1470         int res;
1471         clistiter * cur;
1472         struct mailimap_section * section;
1473
1474         set = mailimap_set_new_single(msg_index);
1475         if (set == NULL) {
1476                 res = MAILIMAP_ERROR_MEMORY;
1477                 goto err;
1478         }
1479
1480         section = mailimap_section_new(NULL);
1481         if (section == NULL) {
1482                 res = MAILIMAP_ERROR_MEMORY;
1483                 goto free_set;
1484         }
1485   
1486         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1487         if (fetch_att == NULL) {
1488                 mailimap_section_free(section);
1489                 res = MAILIMAP_ERROR_MEMORY;
1490                 goto free_set;
1491         }
1492   
1493         fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1494         if (fetch_type == NULL) {
1495                 res = MAILIMAP_ERROR_MEMORY;
1496                 goto free_fetch_att;
1497         }
1498
1499         mailstream_logger = imap_logger_fetch;
1500         
1501         r = mailimap_uid_fetch(imap, set,
1502                                fetch_type, &fetch_result);
1503   
1504         mailstream_logger = imap_logger_cmd;
1505         
1506         mailimap_fetch_type_free(fetch_type);
1507         mailimap_set_free(set);
1508   
1509         switch (r) {
1510         case MAILIMAP_NO_ERROR:
1511                 break;
1512         default:
1513                 return r;
1514         }
1515   
1516         if (clist_begin(fetch_result) == NULL) {
1517                 mailimap_fetch_list_free(fetch_result);
1518                 return MAILIMAP_ERROR_FETCH;
1519         }
1520
1521         msg_att = clist_begin(fetch_result)->data;
1522
1523         text = NULL;
1524         text_length = 0;
1525
1526         for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1527             cur = clist_next(cur)) {
1528                 msg_att_item = clist_content(cur);
1529
1530                 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1531                         if (msg_att_item->att_data.att_static->att_type ==
1532                             MAILIMAP_MSG_ATT_BODY_SECTION) {
1533                                 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1534                                 /* detach */
1535                                 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1536                                 text_length =
1537                                         msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1538                         }
1539                 }
1540         }
1541
1542         mailimap_fetch_list_free(fetch_result);
1543
1544         if (text == NULL)
1545                 return MAILIMAP_ERROR_FETCH;
1546
1547         * result = text;
1548         * result_len = text_length;
1549   
1550         return MAILIMAP_NO_ERROR;
1551
1552  free_fetch_att:
1553         mailimap_fetch_att_free(fetch_att);
1554  free_set:
1555         mailimap_set_free(set);
1556  err:
1557         return res;
1558 }
1559
1560 static int imap_fetch_header(mailimap * imap,
1561                              uint32_t msg_index,
1562                              char ** result,
1563                              size_t * result_len)
1564 {
1565   int r;
1566   struct mailimap_set * set;
1567   struct mailimap_fetch_att * fetch_att;
1568   struct mailimap_fetch_type * fetch_type;
1569   clist * fetch_result;
1570   struct mailimap_msg_att * msg_att;
1571   struct mailimap_msg_att_item * msg_att_item;
1572   char * text;
1573   size_t text_length;
1574   int res;
1575   clistiter * cur;
1576   struct mailimap_section * section;
1577   
1578   set = mailimap_set_new_single(msg_index);
1579   if (set == NULL) {
1580     res = MAILIMAP_ERROR_MEMORY;
1581     goto err;
1582   }
1583
1584   section = mailimap_section_new_header();
1585   if (section == NULL) {
1586     res = MAILIMAP_ERROR_MEMORY;
1587     goto free_set;
1588   }
1589   
1590   fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1591   if (fetch_att == NULL) {
1592     mailimap_section_free(section);
1593     res = MAILIMAP_ERROR_MEMORY;
1594     goto free_set;
1595   }
1596   
1597   fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1598   if (fetch_type == NULL) {
1599     res = MAILIMAP_ERROR_MEMORY;
1600     goto free_fetch_att;
1601   }
1602
1603   r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1604   
1605   mailimap_fetch_type_free(fetch_type);
1606   mailimap_set_free(set);
1607
1608   switch (r) {
1609   case MAILIMAP_NO_ERROR:
1610     break;
1611   default:
1612     return r;
1613   }
1614
1615   if (clist_begin(fetch_result) == NULL) {
1616     mailimap_fetch_list_free(fetch_result);
1617     return MAILIMAP_ERROR_FETCH;
1618   }
1619
1620   msg_att = clist_begin(fetch_result)->data;
1621
1622   text = NULL;
1623   text_length = 0;
1624
1625   for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1626       cur = clist_next(cur)) {
1627     msg_att_item = clist_content(cur);
1628
1629     if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1630       if (msg_att_item->att_data.att_static->att_type ==
1631           MAILIMAP_MSG_ATT_BODY_SECTION) {
1632         text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1633         msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1634         text_length =
1635           msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1636       }
1637     }
1638   }
1639
1640   mailimap_fetch_list_free(fetch_result);
1641
1642   if (text == NULL)
1643     return MAILIMAP_ERROR_FETCH;
1644
1645   * result = text;
1646   * result_len = text_length;
1647
1648   return MAILIMAP_NO_ERROR;
1649
1650  free_fetch_att:
1651   mailimap_fetch_att_free(fetch_att);
1652  free_set:
1653   mailimap_set_free(set);
1654  err:
1655   return res;
1656 }
1657
1658
1659
1660 struct fetch_content_param {
1661         mailimap * imap;
1662         uint32_t msg_index;
1663         const char * filename;
1664         int with_body;
1665 };
1666
1667 struct fetch_content_result {
1668         int error;
1669 };
1670
1671 static void fetch_content_run(struct etpan_thread_op * op)
1672 {
1673         struct fetch_content_param * param;
1674         struct fetch_content_result * result;
1675         char * content;
1676         size_t content_size;
1677         int r;
1678         int fd;
1679         FILE * f;
1680         
1681         param = op->param;
1682         
1683         content = NULL;
1684         content_size = 0;
1685         if (param->with_body)
1686                 r = imap_fetch(param->imap, param->msg_index,
1687                                &content, &content_size);
1688         else
1689                 r = imap_fetch_header(param->imap, param->msg_index,
1690                                       &content, &content_size);
1691         
1692         result = op->result;
1693         result->error = r;
1694         
1695         if (r == MAILIMAP_NO_ERROR) {
1696                 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1697                 if (fd < 0) {
1698                         result->error = MAILIMAP_ERROR_FETCH;
1699                         goto free;
1700                 }
1701                 
1702                 f = fdopen(fd, "wb");
1703                 if (f == NULL) {
1704                         result->error = MAILIMAP_ERROR_FETCH;
1705                         goto close;
1706                 }
1707                 
1708                 r = fwrite(content, 1, content_size, f);
1709                 if (r == 0) {
1710                         goto fclose;
1711                 }
1712                 
1713                 r = fclose(f);
1714                 if (r == EOF) {
1715                         g_unlink(param->filename);
1716                         goto close;
1717                 }
1718                 goto free;
1719                 
1720         fclose:
1721                 fclose(f);
1722                 goto unlink;
1723         close:
1724                 close(fd);
1725         unlink:
1726                 g_unlink(param->filename);
1727         
1728         free:
1729                 if (mmap_string_unref(content) != 0)
1730                         free(content);
1731         }
1732         
1733         debug_print("imap fetch_content run - end %i\n", r);
1734 }
1735
1736 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1737                                 int with_body,
1738                                 const char * filename)
1739 {
1740         struct fetch_content_param param;
1741         struct fetch_content_result result;
1742         mailimap * imap;
1743         
1744         debug_print("imap fetch_content - begin\n");
1745         
1746         imap = get_imap(folder);
1747         param.imap = imap;
1748         param.msg_index = msg_index;
1749         param.filename = filename;
1750         param.with_body = with_body;
1751         
1752         threaded_run(folder, &param, &result, fetch_content_run);
1753         
1754         if (result.error != MAILIMAP_NO_ERROR)
1755                 return result.error;
1756         
1757         debug_print("imap fetch_content - end\n");
1758         
1759         return result.error;
1760 }
1761
1762
1763
1764 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1765 {
1766         int flags;
1767         clist * flag_list;
1768         clistiter * cur;
1769         
1770         flags = MSG_UNREAD;
1771         
1772         flag_list = att_dyn->att_list;
1773         if (flag_list == NULL)
1774                 return flags;
1775         
1776         for(cur = clist_begin(flag_list) ; cur != NULL ;
1777             cur = clist_next(cur)) {
1778                 struct mailimap_flag_fetch * flag_fetch;
1779                         
1780                 flag_fetch = clist_content(cur);
1781                 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1782                         flags |= MSG_NEW;
1783                 else {
1784                         switch (flag_fetch->fl_flag->fl_type) {
1785                         case MAILIMAP_FLAG_ANSWERED:
1786                                 flags |= MSG_REPLIED;
1787                                 break;
1788                         case MAILIMAP_FLAG_FLAGGED:
1789                                 flags |= MSG_MARKED;
1790                                 break;
1791                         case MAILIMAP_FLAG_DELETED:
1792                                 flags |= MSG_DELETED;
1793                                 break;
1794                         case MAILIMAP_FLAG_SEEN:
1795                                 flags &= ~MSG_UNREAD;
1796                                 flags &= ~MSG_NEW;
1797                                 break;
1798                         }
1799                 }
1800         }
1801         
1802         return flags;
1803 }
1804
1805 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1806                                  uint32_t * puid,
1807                                  char ** pheaders,
1808                                  size_t * pref_size,
1809                                  struct mailimap_msg_att_dynamic ** patt_dyn)
1810 {
1811   clistiter * item_cur;
1812   uint32_t uid;
1813   char * headers;
1814   size_t ref_size;
1815   struct mailimap_msg_att_dynamic * att_dyn;
1816
1817   uid = 0;
1818   headers = NULL;
1819   ref_size = 0;
1820   att_dyn = NULL;
1821
1822   for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1823       item_cur = clist_next(item_cur)) {
1824     struct mailimap_msg_att_item * item;
1825
1826     item = clist_content(item_cur);
1827       
1828     switch (item->att_type) {
1829     case MAILIMAP_MSG_ATT_ITEM_STATIC:
1830       switch (item->att_data.att_static->att_type) {
1831       case MAILIMAP_MSG_ATT_UID:
1832         uid = item->att_data.att_static->att_data.att_uid;
1833         break;
1834
1835       case MAILIMAP_MSG_ATT_BODY_SECTION:
1836         if (headers == NULL) {
1837           headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1838         }
1839         break;
1840       case MAILIMAP_MSG_ATT_RFC822_SIZE:
1841               ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1842               break;
1843       }
1844       break;
1845       
1846     case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1847       if (att_dyn == NULL) {
1848         att_dyn = item->att_data.att_dyn;
1849       }
1850       break;
1851     }
1852   }
1853
1854   if (puid != NULL)
1855     * puid = uid;
1856   if (pheaders != NULL)
1857     * pheaders = headers;
1858   if (pref_size != NULL)
1859     * pref_size = ref_size;
1860   if (patt_dyn != NULL)
1861     * patt_dyn = att_dyn;
1862
1863   return MAIL_NO_ERROR;
1864 }
1865
1866 static struct imap_fetch_env_info *
1867 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1868 {
1869         struct imap_fetch_env_info * info;
1870         uint32_t uid;
1871         char * headers;
1872         size_t size;
1873         struct mailimap_msg_att_dynamic * att_dyn;
1874         
1875         imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1876                               &att_dyn);
1877         
1878         if (!headers)
1879                 return NULL;
1880         info = malloc(sizeof(* info));
1881         info->uid = uid;
1882         info->headers = strdup(headers);
1883         info->size = size;
1884         info->flags = imap_flags_to_flags(att_dyn);
1885         
1886         return info;
1887 }
1888
1889 static int
1890 imap_fetch_result_to_envelop_list(clist * fetch_result,
1891                                   carray ** p_env_list)
1892 {
1893         clistiter * cur;
1894         unsigned int i;
1895         carray * env_list;
1896         i = 0;
1897   
1898         env_list = carray_new(16);
1899   
1900         for(cur = clist_begin(fetch_result) ; cur != NULL ;
1901             cur = clist_next(cur)) {
1902                 struct mailimap_msg_att * msg_att;
1903                 struct imap_fetch_env_info * env_info;
1904     
1905                 msg_att = clist_content(cur);
1906
1907                 env_info = fetch_to_env_info(msg_att);
1908                 if (!env_info)
1909                         return MAILIMAP_ERROR_MEMORY;
1910                 carray_add(env_list, env_info, NULL);
1911         }
1912   
1913         * p_env_list = env_list;
1914   
1915         return MAIL_NO_ERROR;
1916 }
1917
1918 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1919 {
1920         struct mailimap_fetch_att * fetch_att;
1921         int r;
1922         char * header;
1923         clist * hdrlist;
1924         struct mailimap_header_list * imap_hdrlist;
1925         struct mailimap_section * section;
1926
1927         hdrlist = clist_new();
1928   
1929         header = strdup("Date");
1930         r = clist_append(hdrlist, header);
1931         header = strdup("From");
1932         r = clist_append(hdrlist, header);
1933         header = strdup("To");
1934         r = clist_append(hdrlist, header);
1935         header = strdup("Cc");
1936         r = clist_append(hdrlist, header);
1937         header = strdup("Subject");
1938         r = clist_append(hdrlist, header);
1939         header = strdup("Message-ID");
1940         r = clist_append(hdrlist, header);
1941         header = strdup("References");
1942         r = clist_append(hdrlist, header);
1943         header = strdup("In-Reply-To");
1944         r = clist_append(hdrlist, header);
1945   
1946         imap_hdrlist = mailimap_header_list_new(hdrlist);
1947         section = mailimap_section_new_header_fields(imap_hdrlist);
1948         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1949         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1950   
1951         return MAIL_NO_ERROR;
1952 }
1953
1954 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1955 {
1956         struct mailimap_fetch_att * fetch_att;
1957         struct mailimap_section * section;
1958         
1959         section = mailimap_section_new_header();
1960         fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1961         mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1962         
1963         return MAIL_NO_ERROR;
1964 }
1965
1966 static int
1967 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1968                         carray ** p_env_list)
1969 {
1970         struct mailimap_fetch_att * fetch_att;
1971         struct mailimap_fetch_type * fetch_type;
1972         int res;
1973         clist * fetch_result;
1974         int r;
1975         carray * env_list = NULL;
1976         chashdatum key;
1977         chashdatum value;
1978         
1979         fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1980   
1981         /* uid */
1982         fetch_att = mailimap_fetch_att_new_uid();
1983         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1984   
1985         /* flags */
1986         fetch_att = mailimap_fetch_att_new_flags();
1987         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1988   
1989         /* rfc822 size */
1990         fetch_att = mailimap_fetch_att_new_rfc822_size();
1991         r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1992   
1993         /* headers */
1994         key.data = &imap;
1995         key.len = sizeof(imap);
1996         r = chash_get(courier_workaround_hash, &key, &value);
1997         if (r < 0)
1998                 r = imap_add_envelope_fetch_att(fetch_type);
1999         else
2000                 r = imap_add_header_fetch_att(fetch_type);
2001         
2002         r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2003         
2004         switch (r) {
2005         case MAILIMAP_NO_ERROR:
2006                 break;
2007         default:
2008                 mailimap_fetch_type_free(fetch_type);
2009                 debug_print("uid_fetch: %d\n", r);
2010                 return r;
2011         }
2012         
2013         if (clist_begin(fetch_result) == NULL) {
2014                 res = MAILIMAP_ERROR_FETCH;
2015                 debug_print("clist_begin = NULL\n");
2016                 goto err;
2017         }
2018         
2019         r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2020         mailimap_fetch_list_free(fetch_result);
2021         
2022         if (r != MAILIMAP_NO_ERROR) {
2023                 mailimap_fetch_type_free(fetch_type);
2024                 res = MAILIMAP_ERROR_MEMORY;
2025                 debug_print("fetch_result_to_envelop_list: %d\n", res);
2026                 goto err;
2027         }
2028         
2029         mailimap_fetch_type_free(fetch_type);
2030         
2031         * p_env_list = env_list;
2032         
2033         return MAILIMAP_NO_ERROR;
2034   
2035  err:
2036         return res;
2037 }
2038
2039 struct fetch_env_param {
2040         mailimap * imap;
2041         struct mailimap_set * set;
2042 };
2043
2044 struct fetch_env_result {
2045         carray * fetch_env_result;
2046         int error;
2047 };
2048
2049 static void fetch_env_run(struct etpan_thread_op * op)
2050 {
2051         struct fetch_env_param * param;
2052         struct fetch_env_result * result;
2053         carray * env_list;
2054         int r;
2055         
2056         param = op->param;
2057         
2058         env_list = NULL;
2059         r = imap_get_envelopes_list(param->imap, param->set,
2060                                     &env_list);
2061         
2062         result = op->result;
2063         result->error = r;
2064         result->fetch_env_result = env_list;
2065         
2066         debug_print("imap fetch_env run - end %i\n", r);
2067 }
2068
2069 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2070                             carray ** p_env_list)
2071 {
2072         struct fetch_env_param param;
2073         struct fetch_env_result result;
2074         mailimap * imap;
2075         
2076         debug_print("imap fetch_env - begin\n");
2077         
2078         imap = get_imap(folder);
2079         param.imap = imap;
2080         param.set = set;
2081         
2082         threaded_run(folder, &param, &result, fetch_env_run);
2083         
2084         if (result.error != MAILIMAP_NO_ERROR) {
2085                 chashdatum key;
2086                 chashdatum value;
2087                 int r;
2088                 
2089                 key.data = &imap;
2090                 key.len = sizeof(imap);
2091                 r = chash_get(courier_workaround_hash, &key, &value);
2092                 if (r < 0) {
2093                         value.data = NULL;
2094                         value.len = 0;
2095                         chash_set(courier_workaround_hash, &key, &value, NULL);
2096                         
2097                         threaded_run(folder, &param, &result, fetch_env_run);
2098                 }
2099         }
2100         
2101         if (result.error != MAILIMAP_NO_ERROR)
2102                 return result.error;
2103         
2104         debug_print("imap fetch_env - end\n");
2105         
2106         * p_env_list = result.fetch_env_result;
2107         
2108         return result.error;
2109 }
2110
2111 void imap_fetch_env_free(carray * env_list)
2112 {
2113         unsigned int i;
2114         
2115         for(i = 0 ; i < carray_count(env_list) ; i ++) {
2116                 struct imap_fetch_env_info * env_info;
2117                 
2118                 env_info = carray_get(env_list, i);
2119                 free(env_info->headers);
2120                 free(env_info);
2121         }
2122         carray_free(env_list);
2123 }
2124
2125
2126
2127
2128
2129 struct append_param {
2130         mailimap * imap;
2131         const char * mailbox;
2132         const char * filename;
2133         struct mailimap_flag_list * flag_list;
2134 };
2135
2136 struct append_result {
2137         int error;
2138         int uid;
2139 };
2140
2141 static void append_run(struct etpan_thread_op * op)
2142 {
2143         struct append_param * param;
2144         struct append_result * result;
2145         int r;
2146         char * data;
2147         size_t size;
2148         struct stat stat_buf;
2149         int fd, uid = 0;
2150         
2151         param = op->param;
2152         result = op->result;
2153         
2154         r = stat(param->filename, &stat_buf);
2155         if (r < 0) {
2156                 result->error = MAILIMAP_ERROR_APPEND;
2157                 return;
2158         }
2159         size = stat_buf.st_size;
2160         
2161         fd = open(param->filename, O_RDONLY);
2162         if (fd < 0) {
2163                 result->error = MAILIMAP_ERROR_APPEND;
2164                 return;
2165         }
2166         
2167         data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2168         if (data == (void *) MAP_FAILED) {
2169                 close(fd);
2170                 result->error = MAILIMAP_ERROR_APPEND;
2171                 return;
2172         }
2173         
2174         mailstream_logger = imap_logger_append;
2175         
2176         r = mailimap_append(param->imap, param->mailbox,
2177                             param->flag_list, NULL,
2178                             data, size/*, &uid */);
2179
2180         mailstream_logger = imap_logger_cmd;
2181         
2182         munmap(data, size);
2183         close(fd);
2184         
2185         result->error = r;
2186         result->uid = uid;
2187         debug_print("imap append run - end %i uid %d\n", r, uid);
2188 }
2189
2190 int imap_threaded_append(Folder * folder, const char * mailbox,
2191                          const char * filename,
2192                          struct mailimap_flag_list * flag_list,
2193                          int *uid)
2194 {
2195         struct append_param param;
2196         struct append_result result;
2197         mailimap * imap;
2198         
2199         debug_print("imap append - begin\n");
2200         
2201         imap = get_imap(folder);
2202         param.imap = imap;
2203         param.mailbox = mailbox;
2204         param.filename = filename;
2205         param.flag_list = flag_list;
2206         
2207         threaded_run(folder, &param, &result, append_run);
2208         
2209         if (result.error != MAILIMAP_NO_ERROR)
2210                 return result.error;
2211         
2212         debug_print("imap append - end\n");
2213         if (uid != NULL)
2214                 *uid = result.uid;
2215
2216         return result.error;
2217 }
2218
2219
2220
2221
2222 struct expunge_param {
2223         mailimap * imap;
2224 };
2225
2226 struct expunge_result {
2227         int error;
2228 };
2229
2230 static void expunge_run(struct etpan_thread_op * op)
2231 {
2232         struct expunge_param * param;
2233         struct expunge_result * result;
2234         int r;
2235         
2236         param = op->param;
2237         r = mailimap_expunge(param->imap);
2238         
2239         result = op->result;
2240         result->error = r;
2241         debug_print("imap expunge run - end %i\n", r);
2242 }
2243
2244 int imap_threaded_expunge(Folder * folder)
2245 {
2246         struct expunge_param param;
2247         struct expunge_result result;
2248         
2249         debug_print("imap expunge - begin\n");
2250         
2251         param.imap = get_imap(folder);
2252         
2253         threaded_run(folder, &param, &result, expunge_run);
2254         
2255         debug_print("imap expunge - end\n");
2256         
2257         return result.error;
2258 }
2259
2260
2261 struct copy_param {
2262         mailimap * imap;
2263         struct mailimap_set * set;
2264         const char * mb;
2265 };
2266
2267 struct copy_result {
2268         int error;
2269 };
2270
2271 static void copy_run(struct etpan_thread_op * op)
2272 {
2273         struct copy_param * param;
2274         struct copy_result * result;
2275         int r;
2276         
2277         param = op->param;
2278         
2279         r = mailimap_uid_copy(param->imap, param->set, param->mb);
2280         
2281         result = op->result;
2282         result->error = r;
2283         
2284         debug_print("imap copy run - end %i\n", r);
2285 }
2286
2287 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2288                        const char * mb)
2289 {
2290         struct copy_param param;
2291         struct copy_result result;
2292         mailimap * imap;
2293         
2294         debug_print("imap copy - begin\n");
2295         
2296         imap = get_imap(folder);
2297         param.imap = imap;
2298         param.set = set;
2299         param.mb = mb;
2300         
2301         threaded_run(folder, &param, &result, copy_run);
2302         
2303         if (result.error != MAILIMAP_NO_ERROR)
2304                 return result.error;
2305         
2306         debug_print("imap copy - end\n");
2307         
2308         return result.error;
2309 }
2310
2311
2312
2313 struct store_param {
2314         mailimap * imap;
2315         struct mailimap_set * set;
2316         struct mailimap_store_att_flags * store_att_flags;
2317 };
2318
2319 struct store_result {
2320         int error;
2321 };
2322
2323 static void store_run(struct etpan_thread_op * op)
2324 {
2325         struct store_param * param;
2326         struct store_result * result;
2327         int r;
2328         
2329         param = op->param;
2330         
2331         r = mailimap_uid_store(param->imap, param->set,
2332                                param->store_att_flags);
2333         
2334         result = op->result;
2335         result->error = r;
2336         
2337         debug_print("imap store run - end %i\n", r);
2338 }
2339
2340 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2341                         struct mailimap_store_att_flags * store_att_flags)
2342 {
2343         struct store_param param;
2344         struct store_result result;
2345         mailimap * imap;
2346         
2347         debug_print("imap store - begin\n");
2348         
2349         imap = get_imap(folder);
2350         param.imap = imap;
2351         param.set = set;
2352         param.store_att_flags = store_att_flags;
2353         
2354         threaded_run(folder, &param, &result, store_run);
2355         
2356         if (result.error != MAILIMAP_NO_ERROR)
2357                 return result.error;
2358         
2359         debug_print("imap store - end\n");
2360         
2361         return result.error;
2362 }
2363
2364
2365 #define ENV_BUFFER_SIZE 512
2366
2367 static void do_exec_command(int fd, const char * command,
2368                             const char * servername, uint16_t port)
2369 {
2370         int i, maxopen;
2371 #ifdef SOLARIS
2372         char env_buffer[ENV_BUFFER_SIZE];
2373 #endif
2374         
2375         if (fork() > 0) {
2376                 /* Fork again to become a child of init rather than
2377                    the etpan client. */
2378                 exit(0);
2379         }
2380   
2381 #ifdef SOLARIS
2382         if (servername)
2383                 snprintf(env_buffer, ENV_BUFFER_SIZE,
2384                          "ETPANSERVER=%s", servername);
2385         else
2386                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2387         putenv(env_buffer);
2388 #else
2389         if (servername)
2390                 setenv("ETPANSERVER", servername, 1);
2391         else
2392                 unsetenv("ETPANSERVER");
2393 #endif
2394   
2395 #ifdef SOLARIS
2396         if (port)
2397                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2398         else
2399                 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2400         putenv(env_buffer);
2401 #else
2402         if (port) {
2403                 char porttext[20];
2404                 
2405                 snprintf(porttext, sizeof(porttext), "%d", port);
2406                 setenv("ETPANPORT", porttext, 1);
2407         }
2408         else {
2409                 unsetenv("ETPANPORT");
2410         }
2411 #endif
2412                 
2413         /* Not a lot we can do if there's an error other than bail. */
2414         if (dup2(fd, 0) == -1)
2415                 exit(1);
2416         if (dup2(fd, 1) == -1)
2417                 exit(1);
2418   
2419         /* Should we close stderr and reopen /dev/null? */
2420   
2421         maxopen = sysconf(_SC_OPEN_MAX);
2422         for (i=3; i < maxopen; i++)
2423                 close(i);
2424   
2425 #ifdef TIOCNOTTY
2426         /* Detach from the controlling tty if we have one. Otherwise,
2427            SSH might do something stupid like trying to use it instead
2428            of running $SSH_ASKPASS. Doh. */
2429         fd = open("/dev/tty", O_RDONLY);
2430         if (fd != -1) {
2431                 ioctl(fd, TIOCNOTTY, NULL);
2432                 close(fd);
2433         }
2434 #endif /* TIOCNOTTY */
2435
2436         execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2437   
2438         /* Eep. Shouldn't reach this */
2439         exit(1);
2440 }
2441
2442 static int subcommand_connect(const char *command,
2443                               const char *servername, uint16_t port)
2444 {
2445         /* SEB unsupported on Windows */
2446         int sockfds[2];
2447         pid_t childpid;
2448   
2449         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2450                 return -1;
2451   
2452         childpid = fork();
2453         if (!childpid) {
2454                 do_exec_command(sockfds[1], command, servername, port);
2455         }
2456         else if (childpid == -1) {
2457                 close(sockfds[0]);
2458                 close(sockfds[1]);
2459                 return -1;
2460         }
2461   
2462         close(sockfds[1]);
2463   
2464         /* Reap child, leaving grandchild process to run */
2465         waitpid(childpid, NULL, 0);
2466   
2467         return sockfds[0];
2468 }
2469
2470 int socket_connect_cmd(mailimap * imap, const char * command,
2471                        const char * server, int port)
2472 {
2473         int fd;
2474         mailstream * s;
2475         int r;
2476         
2477         fd = subcommand_connect(command, server, port);
2478         if (fd < 0)
2479                 return MAILIMAP_ERROR_STREAM;
2480         
2481         s = mailstream_socket_open(fd);
2482         if (s == NULL) {
2483                 close(fd);
2484                 return MAILIMAP_ERROR_STREAM;
2485         }
2486         
2487         r = mailimap_connect(imap, s);
2488         if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2489         &&  r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2490                 mailstream_close(s);
2491                 return r;
2492         }
2493         
2494         return r;
2495 }
2496
2497 /* connect cmd */
2498
2499 struct connect_cmd_param {
2500         mailimap * imap;
2501         const char * command;
2502         const char * server;
2503         int port;
2504 };
2505
2506 struct connect_cmd_result {
2507         int error;
2508 };
2509
2510 static void connect_cmd_run(struct etpan_thread_op * op)
2511 {
2512         int r;
2513         struct connect_cmd_param * param;
2514         struct connect_cmd_result * result;
2515         
2516         param = op->param;
2517         result = op->result;
2518         
2519         r = socket_connect_cmd(param->imap, param->command,
2520                                param->server, param->port);
2521         
2522         result->error = r;
2523 }
2524
2525
2526 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2527                               const char * server, int port)
2528 {
2529         struct connect_cmd_param param;
2530         struct connect_cmd_result result;
2531         chashdatum key;
2532         chashdatum value;
2533         mailimap * imap;
2534         
2535         imap = mailimap_new(0, NULL);
2536         
2537         key.data = &folder;
2538         key.len = sizeof(folder);
2539         value.data = imap;
2540         value.len = 0;
2541         chash_set(session_hash, &key, &value, NULL);
2542         
2543         param.imap = imap;
2544         param.command = command;
2545         param.server = server;
2546         param.port = port;
2547         
2548         threaded_run(folder, &param, &result, connect_cmd_run);
2549         
2550         debug_print("connect_cmd ok %i\n", result.error);
2551         
2552         return result.error;
2553 }
2554 #else
2555
2556 void imap_main_init(void)
2557 {
2558 }
2559 void imap_main_done(void)
2560 {
2561 }
2562 void imap_main_set_timeout(int sec)
2563 {
2564 }
2565
2566 #endif