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