2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
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.
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.
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/>.
26 #include "imap-thread.h"
28 #include <sys/types.h>
30 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
31 #include <sys/socket.h>
40 #include "etpan-thread-manager.h"
42 #include "mainwindow.h"
44 #include "ssl_certificate.h"
46 #include "remotefolder.h"
48 #define DISABLE_LOG_DURING_LOGIN
50 static struct etpan_thread_manager * thread_manager = NULL;
51 static chash * courier_workaround_hash = NULL;
52 static chash * imap_hash = NULL;
53 static chash * session_hash = NULL;
54 static guint thread_manager_signal = 0;
55 static GIOChannel * io_channel = NULL;
57 static void delete_imap(Folder *folder, mailimap *imap)
63 key.len = sizeof(folder);
66 chash_delete(session_hash, &key, NULL);
69 key.len = sizeof(imap);
70 chash_delete(courier_workaround_hash, &key, NULL);
71 if (imap && imap->imap_stream) {
72 /* we don't want libetpan to logout */
73 mailstream_close(imap->imap_stream);
74 imap->imap_stream = NULL;
76 debug_print("removing mailimap %p\n", imap);
80 static gboolean thread_manager_event(GIOChannel * source,
81 GIOCondition condition,
84 etpan_thread_manager_loop(thread_manager);
89 static void imap_logger_noop(int direction, const char * str, size_t size)
94 static void imap_logger_cmd(int direction, const char * str, size_t size)
101 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
104 buf = malloc(size+1);
105 memset(buf, 0, size+1);
106 strncpy(buf, str, size);
109 if (!strncmp(buf, "<<<<<<<", 7)
110 || !strncmp(buf, ">>>>>>>", 7)) {
114 while (strstr(buf, "\r"))
115 *strstr(buf, "\r") = ' ';
116 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
117 buf[strlen(buf)-1] = '\0';
119 lines = g_strsplit(buf, "\n", -1);
121 while (lines[i] && *lines[i]) {
122 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
129 static void imap_logger_fetch(int direction, const char * str, size_t size)
135 if (size > 128 && !direction) {
136 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
140 buf = malloc(size+1);
141 memset(buf, 0, size+1);
142 strncpy(buf, str, size);
144 if (!strncmp(buf, "<<<<<<<", 7)
145 || !strncmp(buf, ">>>>>>>", 7)) {
149 while (strstr(buf, "\r"))
150 *strstr(buf, "\r") = ' ';
151 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
152 buf[strlen(buf)-1] = '\0';
154 lines = g_strsplit(buf, "\n", -1);
156 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
157 while (lines[i] && *lines[i]) {
158 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
162 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
168 static void imap_logger_uid(int direction, const char * str, size_t size)
175 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
178 buf = malloc(size+1);
179 memset(buf, 0, size+1);
180 strncpy(buf, str, size);
182 if (!strncmp(buf, "<<<<<<<", 7)
183 || !strncmp(buf, ">>>>>>>", 7)) {
187 while (strstr(buf, "\r"))
188 *strstr(buf, "\r") = ' ';
189 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
190 buf[strlen(buf)-1] = '\0';
192 lines = g_strsplit(buf, "\n", -1);
194 while (lines[i] && *lines[i]) {
195 int llen = strlen(lines[i]);
197 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
200 strncpy2(tmp, lines[i], 63);
201 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
210 static void imap_logger_append(int direction, const char * str, size_t size)
217 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
219 } else if (direction == 0 && size > 64) {
220 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
223 buf = malloc(size+1);
224 memset(buf, 0, size+1);
225 strncpy(buf, str, size);
227 if (!strncmp(buf, "<<<<<<<", 7)
228 || !strncmp(buf, ">>>>>>>", 7)) {
232 while (strstr(buf, "\r"))
233 *strstr(buf, "\r") = ' ';
234 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
235 buf[strlen(buf)-1] = '\0';
237 lines = g_strsplit(buf, "\n", -1);
239 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
240 while (lines[i] && *lines[i]) {
241 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
245 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
251 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
252 gboolean etpan_skip_ssl_cert_check = FALSE;
253 extern void mailsasl_ref(void);
255 void imap_main_init(gboolean skip_ssl_cert_check)
257 int fd_thread_manager;
259 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
260 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
261 mailstream_network_delay.tv_usec = 0;
263 mailstream_debug = 1;
264 mailstream_logger = imap_logger_cmd;
267 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
268 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
269 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
271 thread_manager = etpan_thread_manager_new();
273 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
275 io_channel = g_io_channel_unix_new(fd_thread_manager);
277 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
278 thread_manager_event,
283 void imap_main_set_timeout(int sec)
285 mailstream_network_delay.tv_sec = sec;
286 mailstream_network_delay.tv_usec = 0;
289 void imap_main_done(void)
291 imap_disconnect_all();
292 etpan_thread_manager_stop(thread_manager);
293 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
296 etpan_thread_manager_join(thread_manager);
298 g_source_remove(thread_manager_signal);
299 g_io_channel_unref(io_channel);
301 etpan_thread_manager_free(thread_manager);
303 chash_free(courier_workaround_hash);
304 chash_free(session_hash);
305 chash_free(imap_hash);
308 void imap_init(Folder * folder)
310 struct etpan_thread * thread;
314 thread = etpan_thread_manager_get_thread(thread_manager);
317 key.len = sizeof(folder);
321 chash_set(imap_hash, &key, &value, NULL);
324 void imap_done(Folder * folder)
326 struct etpan_thread * thread;
332 key.len = sizeof(folder);
334 r = chash_get(imap_hash, &key, &value);
340 etpan_thread_unbind(thread);
342 chash_delete(imap_hash, &key, NULL);
344 debug_print("remove thread");
347 static struct etpan_thread * get_thread(Folder * folder)
349 struct etpan_thread * thread;
354 key.len = sizeof(folder);
356 chash_get(imap_hash, &key, &value);
362 static mailimap * get_imap(Folder * folder)
370 key.len = sizeof(folder);
372 r = chash_get(session_hash, &key, &value);
377 debug_print("found imap %p\n", imap);
381 static gboolean cb_show_error(gpointer data)
383 mainwindow_show_error();
387 static void generic_cb(int cancelled, void * result, void * callback_data)
389 struct etpan_thread_op * op;
391 op = (struct etpan_thread_op *) callback_data;
393 debug_print("generic_cb\n");
394 if (op->imap && op->imap->imap_response_info &&
395 op->imap->imap_response_info->rsp_alert) {
396 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
397 op->imap->imap_response_info->rsp_alert);
398 g_timeout_add(10, cb_show_error, NULL);
403 static void threaded_run(Folder * folder, void * param, void * result,
404 void (* func)(struct etpan_thread_op * ))
406 struct etpan_thread_op * op;
407 struct etpan_thread * thread;
409 imap_folder_ref(folder);
411 op = etpan_thread_op_new();
413 op->imap = get_imap(folder);
419 op->callback = generic_cb;
420 op->callback_data = op;
425 thread = get_thread(folder);
426 etpan_thread_op_schedule(thread, op);
428 while (!op->finished) {
429 gtk_main_iteration();
432 etpan_thread_op_free(op);
434 imap_folder_unref(folder);
440 struct connect_param {
442 PrefsAccount *account;
447 struct connect_result {
451 #define CHECK_IMAP() { \
452 if (!param->imap) { \
453 result->error = MAILIMAP_ERROR_BAD_STATE; \
458 static void connect_run(struct etpan_thread_op * op)
461 struct connect_param * param;
462 struct connect_result * result;
469 r = mailimap_socket_connect(param->imap,
470 param->server, param->port);
476 int imap_threaded_connect(Folder * folder, const char * server, int port)
478 struct connect_param param;
479 struct connect_result result;
482 mailimap * imap, * oldimap;
484 oldimap = get_imap(folder);
486 imap = mailimap_new(0, NULL);
489 debug_print("deleting old imap %p\n", oldimap);
490 delete_imap(folder, oldimap);
494 key.len = sizeof(folder);
497 chash_set(session_hash, &key, &value, NULL);
500 param.server = server;
504 threaded_run(folder, ¶m, &result, connect_run);
506 debug_print("connect ok %i with imap %p\n", result.error, imap);
511 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
514 struct connect_param *param = (struct connect_param *)data;
517 if (certificate == NULL || len < 0) {
518 g_warning("no cert presented.\n");
521 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
523 g_warning("IMAP: can't get cert\n");
525 } else if (ssl_certificate_check(cert, NULL,
526 (gchar *)param->server, (gushort)param->port) == TRUE) {
534 struct connect_param *param = (struct connect_param *)data;
535 gnutls_x509_crt cert = NULL;
538 if (certificate == NULL || len < 0) {
539 g_warning("no cert presented.\n");
543 tmp.data = malloc(len);
544 memcpy(tmp.data, certificate, len);
546 gnutls_x509_crt_init(&cert);
547 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
548 g_warning("IMAP: can't get cert\n");
550 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
551 (gchar *)param->server, (gushort)param->port) == TRUE) {
552 gnutls_x509_crt_deinit(cert);
555 gnutls_x509_crt_deinit(cert);
562 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
564 #if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
565 PrefsAccount *account = (PrefsAccount *)data;
566 const gchar *cert_path = NULL;
567 const gchar *password = NULL;
570 EVP_PKEY *pkey = NULL;
572 gnutls_x509_crt x509 = NULL;
573 gnutls_x509_privkey pkey = NULL;
576 if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
577 cert_path = account->in_ssl_client_cert_file;
578 if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
579 password = account->in_ssl_client_cert_pass;
581 if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
582 mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
583 debug_print("Impossible to set the client certificate.\n");
584 x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
585 pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
586 if (!(x509 && pkey)) {
587 /* try pkcs12 format */
588 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
591 unsigned char *x509_der = NULL, *pkey_der = NULL;
592 size_t x509_len, pkey_len;
595 x509_len = (size_t)i2d_X509(x509, &x509_der);
596 pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
598 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
599 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
601 if (x509_len > 0 && pkey_len > 0) {
602 if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
603 mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
604 log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
609 gnutls_x509_crt_deinit(x509);
610 gnutls_x509_privkey_deinit(pkey);
616 static void connect_ssl_run(struct etpan_thread_op * op)
619 struct connect_param * param;
620 struct connect_result * result;
627 r = mailimap_ssl_connect_with_callback(param->imap,
628 param->server, param->port,
629 connect_ssl_context_cb, param->account);
633 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
635 struct connect_param param;
636 struct connect_result result;
639 mailimap * imap, * oldimap;
640 unsigned char *certificate = NULL;
643 oldimap = get_imap(folder);
645 imap = mailimap_new(0, NULL);
648 debug_print("deleting old imap %p\n", oldimap);
649 delete_imap(folder, oldimap);
653 key.len = sizeof(folder);
656 chash_set(session_hash, &key, &value, NULL);
659 param.server = server;
661 param.account = folder->account;
664 threaded_run(folder, ¶m, &result, connect_ssl_run);
666 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
667 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
668 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
669 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
674 debug_print("connect %d with imap %p\n", result.error, imap);
685 struct mailimap_capability_data *caps;
688 static void capability_run(struct etpan_thread_op * op)
691 struct capa_param * param;
692 struct capa_result * result;
693 struct mailimap_capability_data *caps;
700 r = mailimap_capability(param->imap, &caps);
703 result->caps = (r == 0 ? caps : NULL);
707 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
709 struct capa_param param;
710 struct capa_result result;
713 imap = get_imap(folder);
717 threaded_run(folder, ¶m, &result, capability_run);
719 debug_print("capa %d\n", result.error);
728 struct disconnect_param {
732 struct disconnect_result {
736 static void disconnect_run(struct etpan_thread_op * op)
739 struct disconnect_param * param;
740 struct disconnect_result * result;
747 r = mailimap_logout(param->imap);
752 void imap_threaded_disconnect(Folder * folder)
754 struct connect_param param;
755 struct connect_result result;
758 imap = get_imap(folder);
760 debug_print("was disconnected\n");
766 threaded_run(folder, ¶m, &result, disconnect_run);
768 if (imap == get_imap(folder)) {
769 debug_print("deleting old imap %p\n", imap);
770 delete_imap(folder, imap);
772 debug_print("imap already deleted %p\n", imap);
775 debug_print("disconnect ok\n");
782 const char * wildcard;
791 static void list_run(struct etpan_thread_op * op)
793 struct list_param * param;
794 struct list_result * result;
805 if (param->base == NULL || param->wildcard == NULL) {
808 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
812 r = mailimap_lsub(param->imap, param->base,
813 param->wildcard, &list);
815 r = mailimap_list(param->imap, param->base,
816 param->wildcard, &list);
819 debug_print("imap list run - end\n");
822 int imap_threaded_list(Folder * folder, const char * base,
823 const char * wildcard,
826 struct list_param param;
827 struct list_result result;
829 debug_print("imap list - begin\n");
831 param.imap = get_imap(folder);
833 param.wildcard = wildcard;
834 param.sub_only = FALSE;
836 threaded_run(folder, ¶m, &result, list_run);
838 * p_result = result.list;
840 debug_print("imap list - end %p\n", result.list);
845 int imap_threaded_lsub(Folder * folder, const char * base,
846 const char * wildcard,
849 struct list_param param;
850 struct list_result result;
852 debug_print("imap lsub - begin\n");
854 param.imap = get_imap(folder);
856 param.wildcard = wildcard;
857 param.sub_only = TRUE;
859 threaded_run(folder, ¶m, &result, list_run);
861 * p_result = result.list;
863 debug_print("imap lsub - end %p\n", result.list);
868 struct subscribe_param {
874 struct subscribe_result {
878 static void subscribe_run(struct etpan_thread_op * op)
880 struct subscribe_param * param;
881 struct subscribe_result * result;
889 if (param->mb == NULL) {
891 debug_print("no mb\n");
894 if (param->subscribe)
895 r = mailimap_subscribe(param->imap, param->mb);
897 r = mailimap_unsubscribe(param->imap, param->mb);
899 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
902 int imap_threaded_subscribe(Folder * folder, const char * mb,
905 struct subscribe_param param;
906 struct subscribe_result result;
908 debug_print("imap list - begin\n");
910 param.imap = get_imap(folder);
912 param.subscribe = subscribe;
914 threaded_run(folder, ¶m, &result, subscribe_run);
922 const char * password;
927 struct login_result {
931 static void login_run(struct etpan_thread_op * op)
933 struct login_param * param;
934 struct login_result * result;
936 #ifdef DISABLE_LOG_DURING_LOGIN
945 #ifdef DISABLE_LOG_DURING_LOGIN
946 old_debug = mailstream_debug;
947 mailstream_debug = 0;
949 if (!strcmp(param->type, "LOGIN"))
950 r = mailimap_login(param->imap,
951 param->login, param->password);
952 else if (!strcmp(param->type, "GSSAPI"))
953 r = mailimap_authenticate(param->imap,
954 param->type, param->server, NULL, NULL,
955 param->login, param->login,
956 param->password, NULL);
958 r = mailimap_authenticate(param->imap,
959 param->type, NULL, NULL, NULL,
960 param->login, param->login,
961 param->password, NULL);
962 #ifdef DISABLE_LOG_DURING_LOGIN
963 mailstream_debug = old_debug;
967 if (param->imap->imap_response)
968 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
969 debug_print("imap login run - end %i\n", r);
972 int imap_threaded_login(Folder * folder,
973 const char * login, const char * password,
976 struct login_param param;
977 struct login_result result;
979 debug_print("imap login - begin\n");
981 param.imap = get_imap(folder);
983 param.password = password;
985 if (folder && folder->account)
986 param.server = folder->account->recv_server;
990 threaded_run(folder, ¶m, &result, login_run);
992 debug_print("imap login - end\n");
998 struct status_param {
1001 struct mailimap_status_att_list * status_att_list;
1004 struct status_result {
1006 struct mailimap_mailbox_data_status * data_status;
1009 static void status_run(struct etpan_thread_op * op)
1011 struct status_param * param;
1012 struct status_result * result;
1016 result = op->result;
1020 r = mailimap_status(param->imap, param->mb,
1021 param->status_att_list,
1022 &result->data_status);
1025 debug_print("imap status run - end %i\n", r);
1028 int imap_threaded_status(Folder * folder, const char * mb,
1029 struct mailimap_mailbox_data_status ** data_status,
1032 struct status_param param;
1033 struct status_result result;
1034 struct mailimap_status_att_list * status_att_list;
1036 debug_print("imap status - begin\n");
1038 status_att_list = mailimap_status_att_list_new_empty();
1039 if (mask & 1 << 0) {
1040 mailimap_status_att_list_add(status_att_list,
1041 MAILIMAP_STATUS_ATT_MESSAGES);
1043 if (mask & 1 << 1) {
1044 mailimap_status_att_list_add(status_att_list,
1045 MAILIMAP_STATUS_ATT_RECENT);
1047 if (mask & 1 << 2) {
1048 mailimap_status_att_list_add(status_att_list,
1049 MAILIMAP_STATUS_ATT_UIDNEXT);
1051 if (mask & 1 << 3) {
1052 mailimap_status_att_list_add(status_att_list,
1053 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1055 if (mask & 1 << 4) {
1056 mailimap_status_att_list_add(status_att_list,
1057 MAILIMAP_STATUS_ATT_UNSEEN);
1059 param.imap = get_imap(folder);
1061 param.status_att_list = status_att_list;
1063 threaded_run(folder, ¶m, &result, status_run);
1065 debug_print("imap status - end\n");
1067 * data_status = result.data_status;
1069 mailimap_status_att_list_free(status_att_list);
1071 return result.error;
1080 struct noop_result {
1084 static void noop_run(struct etpan_thread_op * op)
1086 struct noop_param * param;
1087 struct noop_result * result;
1091 result = op->result;
1095 r = mailimap_noop(param->imap);
1098 debug_print("imap noop run - end %i\n", r);
1101 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1102 unsigned int *p_recent,
1103 unsigned int *p_expunge,
1104 unsigned int *p_unseen,
1105 unsigned int *p_uidnext,
1106 unsigned int *p_uidval)
1108 struct noop_param param;
1109 struct noop_result result;
1112 debug_print("imap noop - begin\n");
1114 imap = get_imap(folder);
1117 threaded_run(folder, ¶m, &result, noop_run);
1119 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1120 * p_exists = imap->imap_selection_info->sel_exists;
1121 * p_recent = imap->imap_selection_info->sel_recent;
1122 * p_unseen = imap->imap_selection_info->sel_unseen;
1123 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1124 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1132 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1133 imap->imap_response_info->rsp_expunged != NULL) {
1134 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1138 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1139 *p_exists, *p_recent, *p_expunge, *p_unseen,
1140 *p_uidnext, *p_uidval);
1142 return result.error;
1146 struct starttls_result {
1150 static void starttls_run(struct etpan_thread_op * op)
1152 struct connect_param * param;
1153 struct starttls_result * result;
1157 result = op->result;
1161 r = mailimap_starttls(param->imap);
1164 debug_print("imap starttls run - end %i\n", r);
1167 mailimap *imap = param->imap;
1168 mailstream_low *plain_low = NULL;
1169 mailstream_low *tls_low = NULL;
1172 plain_low = mailstream_get_low(imap->imap_stream);
1173 fd = mailstream_low_get_fd(plain_low);
1175 debug_print("imap starttls run - can't get fd\n");
1176 result->error = MAILIMAP_ERROR_STREAM;
1180 tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
1181 if (tls_low == NULL) {
1182 debug_print("imap starttls run - can't tls_open\n");
1183 result->error = MAILIMAP_ERROR_STREAM;
1186 mailstream_low_free(plain_low);
1187 mailstream_set_low(imap->imap_stream, tls_low);
1191 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1193 struct connect_param param;
1194 struct starttls_result result;
1196 unsigned char *certificate;
1198 debug_print("imap starttls - begin\n");
1200 param.imap = get_imap(folder);
1201 param.server = host;
1203 param.account = folder->account;
1205 threaded_run(folder, ¶m, &result, starttls_run);
1207 debug_print("imap starttls - end\n");
1209 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1210 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1211 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1212 result.error = MAILIMAP_ERROR_STREAM;
1216 return result.error;
1221 struct create_param {
1226 struct create_result {
1230 static void create_run(struct etpan_thread_op * op)
1232 struct create_param * param;
1233 struct create_result * result;
1237 result = op->result;
1241 r = mailimap_create(param->imap, param->mb);
1244 debug_print("imap create run - end %i\n", r);
1247 int imap_threaded_create(Folder * folder, const char * mb)
1249 struct create_param param;
1250 struct create_result result;
1252 debug_print("imap create - begin\n");
1254 param.imap = get_imap(folder);
1257 threaded_run(folder, ¶m, &result, create_run);
1259 debug_print("imap create - end\n");
1261 return result.error;
1267 struct rename_param {
1270 const char * new_name;
1273 struct rename_result {
1277 static void rename_run(struct etpan_thread_op * op)
1279 struct rename_param * param;
1280 struct rename_result * result;
1284 result = op->result;
1288 r = mailimap_rename(param->imap, param->mb, param->new_name);
1291 debug_print("imap rename run - end %i\n", r);
1294 int imap_threaded_rename(Folder * folder,
1295 const char * mb, const char * new_name)
1297 struct rename_param param;
1298 struct rename_result result;
1300 debug_print("imap rename - begin\n");
1302 param.imap = get_imap(folder);
1304 param.new_name = new_name;
1306 threaded_run(folder, ¶m, &result, rename_run);
1308 debug_print("imap rename - end\n");
1310 return result.error;
1316 struct delete_param {
1321 struct delete_result {
1325 static void delete_run(struct etpan_thread_op * op)
1327 struct delete_param * param;
1328 struct delete_result * result;
1332 result = op->result;
1336 r = mailimap_delete(param->imap, param->mb);
1339 debug_print("imap delete run - end %i\n", r);
1342 int imap_threaded_delete(Folder * folder, const char * mb)
1344 struct delete_param param;
1345 struct delete_result result;
1347 debug_print("imap delete - begin\n");
1349 param.imap = get_imap(folder);
1352 threaded_run(folder, ¶m, &result, delete_run);
1354 debug_print("imap delete - end\n");
1356 return result.error;
1361 struct select_param {
1366 struct select_result {
1370 static void select_run(struct etpan_thread_op * op)
1372 struct select_param * param;
1373 struct select_result * result;
1377 result = op->result;
1381 r = mailimap_select(param->imap, param->mb);
1384 debug_print("imap select run - end %i\n", r);
1387 int imap_threaded_select(Folder * folder, const char * mb,
1388 gint * exists, gint * recent, gint * unseen,
1389 guint32 * uid_validity,gint *can_create_flags,
1392 struct select_param param;
1393 struct select_result result;
1396 debug_print("imap select - begin\n");
1398 imap = get_imap(folder);
1402 threaded_run(folder, ¶m, &result, select_run);
1404 if (result.error != MAILIMAP_NO_ERROR)
1405 return result.error;
1407 if (!imap || imap->imap_selection_info == NULL)
1408 return MAILIMAP_ERROR_PARSE;
1410 * exists = imap->imap_selection_info->sel_exists;
1411 * recent = imap->imap_selection_info->sel_recent;
1412 * unseen = imap->imap_selection_info->sel_unseen;
1413 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1414 * can_create_flags = FALSE;
1416 if (imap->imap_selection_info->sel_perm_flags) {
1417 GSList *t_flags = NULL;
1419 clist_begin(imap->imap_selection_info->sel_perm_flags);
1420 for (; cur; cur = clist_next(cur)) {
1421 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1422 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1423 *can_create_flags = TRUE;
1424 else if (flag->fl_flag &&
1425 flag->fl_flag->fl_type == 6 &&
1426 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1427 *can_create_flags = TRUE;
1429 MsgPermFlags c_flag = 0;
1430 switch (flag->fl_flag->fl_type) {
1431 case MAILIMAP_FLAG_ANSWERED:
1432 c_flag = IMAP_FLAG_ANSWERED;
1434 case MAILIMAP_FLAG_FLAGGED:
1435 c_flag = IMAP_FLAG_FLAGGED;
1437 case MAILIMAP_FLAG_DELETED:
1438 c_flag = IMAP_FLAG_DELETED;
1440 case MAILIMAP_FLAG_DRAFT:
1441 c_flag = IMAP_FLAG_DRAFT;
1443 case MAILIMAP_FLAG_SEEN:
1444 c_flag = IMAP_FLAG_SEEN;
1446 case MAILIMAP_FLAG_KEYWORD:
1447 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
1448 c_flag = IMAP_FLAG_FORWARDED;
1454 t_flags = g_slist_prepend(t_flags,
1455 GUINT_TO_POINTER(c_flag));
1460 *ok_flags = t_flags;
1462 debug_print("imap select - end\n");
1464 return result.error;
1467 static void close_run(struct etpan_thread_op * op)
1469 struct select_param * param;
1470 struct select_result * result;
1474 result = op->result;
1478 r = mailimap_close(param->imap);
1481 debug_print("imap close run - end %i\n", r);
1484 int imap_threaded_close(Folder * folder)
1486 struct select_param param;
1487 struct select_result result;
1490 debug_print("imap close - begin\n");
1492 imap = get_imap(folder);
1495 threaded_run(folder, ¶m, &result, close_run);
1497 if (result.error != MAILIMAP_NO_ERROR)
1498 return result.error;
1500 debug_print("imap close - end\n");
1502 return result.error;
1505 struct examine_param {
1510 struct examine_result {
1514 static void examine_run(struct etpan_thread_op * op)
1516 struct examine_param * param;
1517 struct examine_result * result;
1521 result = op->result;
1525 r = mailimap_examine(param->imap, param->mb);
1528 debug_print("imap examine run - end %i\n", r);
1531 int imap_threaded_examine(Folder * folder, const char * mb,
1532 gint * exists, gint * recent, gint * unseen,
1533 guint32 * uid_validity)
1535 struct examine_param param;
1536 struct examine_result result;
1539 debug_print("imap examine - begin\n");
1541 imap = get_imap(folder);
1545 threaded_run(folder, ¶m, &result, examine_run);
1547 if (result.error != MAILIMAP_NO_ERROR)
1548 return result.error;
1550 if (!imap || imap->imap_selection_info == NULL)
1551 return MAILIMAP_ERROR_PARSE;
1553 * exists = imap->imap_selection_info->sel_exists;
1554 * recent = imap->imap_selection_info->sel_recent;
1555 * unseen = imap->imap_selection_info->sel_unseen;
1556 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1558 debug_print("imap examine - end\n");
1560 return result.error;
1566 struct search_param {
1569 struct mailimap_set * set;
1572 struct search_result {
1574 clist * search_result;
1577 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1579 return mailimap_set_item_new(orig->set_first, orig->set_last);
1582 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1584 clist *list = orig ? orig->set_list : NULL;
1585 clist *newlist = clist_new();
1590 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1591 clist_append(newlist,
1592 sc_mailimap_set_item_copy(
1593 (struct mailimap_set_item *)clist_content(cur)));
1594 return mailimap_set_new(newlist);
1597 static void search_run(struct etpan_thread_op * op)
1599 struct search_param * param;
1600 struct search_result * result;
1602 struct mailimap_search_key * key = NULL;
1603 struct mailimap_search_key * uid_key = NULL;
1604 struct mailimap_search_key * search_type_key;
1605 clist * search_result;
1608 result = op->result;
1612 /* we copy the mailimap_set because freeing the key is recursive */
1613 if (param->set != NULL) {
1614 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1615 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1616 uid_key = mailimap_search_key_new_all();
1618 search_type_key = NULL;
1619 switch (param->type) {
1620 case IMAP_SEARCH_TYPE_SIMPLE:
1621 search_type_key = NULL;
1624 case IMAP_SEARCH_TYPE_SEEN:
1625 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1626 NULL, NULL, NULL, NULL, NULL,
1627 NULL, NULL, NULL, NULL, NULL,
1628 NULL, NULL, NULL, NULL, 0,
1629 NULL, NULL, NULL, NULL, NULL,
1630 NULL, 0, NULL, NULL, NULL);
1633 case IMAP_SEARCH_TYPE_UNSEEN:
1634 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1635 NULL, NULL, NULL, NULL, NULL,
1636 NULL, NULL, NULL, NULL, NULL,
1637 NULL, NULL, NULL, NULL, 0,
1638 NULL, NULL, NULL, NULL, NULL,
1639 NULL, 0, NULL, NULL, NULL);
1642 case IMAP_SEARCH_TYPE_ANSWERED:
1643 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1644 NULL, NULL, NULL, NULL, NULL,
1645 NULL, NULL, NULL, NULL, NULL,
1646 NULL, NULL, NULL, NULL, 0,
1647 NULL, NULL, NULL, NULL, NULL,
1648 NULL, 0, NULL, NULL, NULL);
1651 case IMAP_SEARCH_TYPE_FLAGGED:
1652 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1653 NULL, NULL, NULL, NULL, NULL,
1654 NULL, NULL, NULL, NULL, NULL,
1655 NULL, NULL, NULL, NULL, 0,
1656 NULL, NULL, NULL, NULL, NULL,
1657 NULL, 0, NULL, NULL, NULL);
1659 case IMAP_SEARCH_TYPE_DELETED:
1660 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1661 NULL, NULL, NULL, NULL, NULL,
1662 NULL, NULL, NULL, NULL, NULL,
1663 NULL, NULL, NULL, NULL, 0,
1664 NULL, NULL, NULL, NULL, NULL,
1665 NULL, 0, NULL, NULL, NULL);
1667 case IMAP_SEARCH_TYPE_FORWARDED:
1668 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1669 NULL, NULL, NULL, NULL, NULL,
1670 strdup("$Forwarded"), NULL, NULL, NULL, NULL,
1671 NULL, NULL, NULL, NULL, 0,
1672 NULL, NULL, NULL, NULL, NULL,
1673 NULL, 0, NULL, NULL, NULL);
1677 if (search_type_key != NULL) {
1678 if (param->set != NULL) {
1679 key = mailimap_search_key_new_multiple_empty();
1680 mailimap_search_key_multiple_add(key, search_type_key);
1681 mailimap_search_key_multiple_add(key, uid_key);
1683 key = search_type_key;
1685 } else if (uid_key != NULL) {
1690 g_warning("no key!");
1691 result = op->result;
1693 result->search_result = NULL;
1695 mailstream_logger = imap_logger_uid;
1697 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1699 mailstream_logger = imap_logger_cmd;
1701 /* free the key (with the imapset) */
1702 mailimap_search_key_free(key);
1705 result->search_result = search_result;
1707 debug_print("imap search run - end %i\n", result->error);
1710 int imap_threaded_search(Folder * folder, int search_type,
1711 struct mailimap_set * set, clist ** search_result)
1713 struct search_param param;
1714 struct search_result result;
1717 debug_print("imap search - begin\n");
1719 imap = get_imap(folder);
1722 param.type = search_type;
1724 threaded_run(folder, ¶m, &result, search_run);
1726 if (result.error != MAILIMAP_NO_ERROR)
1727 return result.error;
1729 debug_print("imap search - end\n");
1731 * search_result = result.search_result;
1733 return result.error;
1738 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1742 struct mailimap_msg_att_dynamic ** patt_dyn);
1745 result_to_uid_list(clist * fetch_result, carray ** result)
1752 tab = carray_new(128);
1754 res = MAILIMAP_ERROR_MEMORY;
1758 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1759 cur = clist_next(cur)) {
1760 struct mailimap_msg_att * msg_att;
1764 msg_att = clist_content(cur);
1767 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1769 puid = malloc(sizeof(* puid));
1771 res = MAILIMAP_ERROR_MEMORY;
1776 r = carray_add(tab, puid, NULL);
1779 res = MAILIMAP_ERROR_MEMORY;
1786 return MAILIMAP_NO_ERROR;
1789 imap_fetch_uid_list_free(tab);
1794 static int imap_get_messages_list(mailimap * imap,
1795 uint32_t first_index,
1800 struct mailimap_fetch_att * fetch_att;
1801 struct mailimap_fetch_type * fetch_type;
1802 struct mailimap_set * set;
1803 clist * fetch_result;
1806 set = mailimap_set_new_interval(first_index, 0);
1808 res = MAILIMAP_ERROR_MEMORY;
1812 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1813 if (fetch_type == NULL) {
1814 res = MAILIMAP_ERROR_MEMORY;
1818 fetch_att = mailimap_fetch_att_new_uid();
1819 if (fetch_att == NULL) {
1820 res = MAILIMAP_ERROR_MEMORY;
1821 goto free_fetch_type;
1824 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1825 if (r != MAILIMAP_NO_ERROR) {
1826 mailimap_fetch_att_free(fetch_att);
1827 res = MAILIMAP_ERROR_MEMORY;
1828 goto free_fetch_type;
1831 mailstream_logger = imap_logger_fetch;
1833 r = mailimap_uid_fetch(imap, set,
1834 fetch_type, &fetch_result);
1836 mailstream_logger = imap_logger_cmd;
1837 mailimap_fetch_type_free(fetch_type);
1838 mailimap_set_free(set);
1840 if (r != MAILIMAP_NO_ERROR) {
1846 r = result_to_uid_list(fetch_result, &env_list);
1847 mailimap_fetch_list_free(fetch_result);
1849 * result = env_list;
1851 return MAILIMAP_NO_ERROR;
1854 mailimap_fetch_type_free(fetch_type);
1856 mailimap_set_free(set);
1864 struct fetch_uid_param {
1866 uint32_t first_index;
1869 struct fetch_uid_result {
1871 carray * fetch_result;
1874 static void fetch_uid_run(struct etpan_thread_op * op)
1876 struct fetch_uid_param * param;
1877 struct fetch_uid_result * result;
1878 carray * fetch_result;
1882 result = op->result;
1886 fetch_result = NULL;
1887 mailstream_logger = imap_logger_noop;
1888 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1890 r = imap_get_messages_list(param->imap, param->first_index,
1893 mailstream_logger = imap_logger_cmd;
1896 result->fetch_result = fetch_result;
1897 debug_print("imap fetch_uid run - end %i\n", r);
1900 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1901 carray ** fetch_result)
1903 struct fetch_uid_param param;
1904 struct fetch_uid_result result;
1907 debug_print("imap fetch_uid - begin\n");
1909 imap = get_imap(folder);
1911 param.first_index = first_index;
1913 threaded_run(folder, ¶m, &result, fetch_uid_run);
1915 if (result.error != MAILIMAP_NO_ERROR)
1916 return result.error;
1918 debug_print("imap fetch_uid - end\n");
1920 * fetch_result = result.fetch_result;
1922 return result.error;
1926 void imap_fetch_uid_list_free(carray * uid_list)
1930 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1933 puid = carray_get(uid_list, i);
1936 carray_free(uid_list);
1942 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1945 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1951 GSList *tags = NULL;
1953 tab = carray_new(128);
1955 res = MAILIMAP_ERROR_MEMORY;
1959 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1960 cur = clist_next(cur)) {
1961 struct mailimap_msg_att * msg_att;
1964 struct mailimap_msg_att_dynamic * att_dyn;
1970 msg_att = clist_content(cur);
1974 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1977 if (att_dyn == NULL)
1980 flags = imap_flags_to_flags(att_dyn, &tags);
1982 puid = malloc(sizeof(* puid));
1984 res = MAILIMAP_ERROR_MEMORY;
1989 r = carray_add(tab, puid, NULL);
1992 res = MAILIMAP_ERROR_MEMORY;
1995 pflags = malloc(sizeof(* pflags));
1996 if (pflags == NULL) {
1997 res = MAILIMAP_ERROR_MEMORY;
2001 r = carray_add(tab, pflags, NULL);
2004 res = MAILIMAP_ERROR_MEMORY;
2007 r = carray_add(tab, tags, NULL);
2010 res = MAILIMAP_ERROR_MEMORY;
2017 return MAILIMAP_NO_ERROR;
2020 imap_fetch_uid_flags_list_free(tab);
2021 slist_free_strings(tags);
2027 static int imap_get_messages_flags_list(mailimap * imap,
2028 uint32_t first_index,
2033 struct mailimap_fetch_att * fetch_att;
2034 struct mailimap_fetch_type * fetch_type;
2035 struct mailimap_set * set;
2036 clist * fetch_result;
2039 set = mailimap_set_new_interval(first_index, 0);
2041 res = MAILIMAP_ERROR_MEMORY;
2045 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2046 if (fetch_type == NULL) {
2047 res = MAILIMAP_ERROR_MEMORY;
2051 fetch_att = mailimap_fetch_att_new_flags();
2052 if (fetch_att == NULL) {
2053 res = MAILIMAP_ERROR_MEMORY;
2054 goto free_fetch_type;
2057 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2058 if (r != MAILIMAP_NO_ERROR) {
2059 mailimap_fetch_att_free(fetch_att);
2060 res = MAILIMAP_ERROR_MEMORY;
2061 goto free_fetch_type;
2064 fetch_att = mailimap_fetch_att_new_uid();
2065 if (fetch_att == NULL) {
2066 res = MAILIMAP_ERROR_MEMORY;
2067 goto free_fetch_type;
2070 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2071 if (r != MAILIMAP_NO_ERROR) {
2072 mailimap_fetch_att_free(fetch_att);
2073 res = MAILIMAP_ERROR_MEMORY;
2074 goto free_fetch_type;
2077 mailstream_logger = imap_logger_fetch;
2079 r = mailimap_uid_fetch(imap, set,
2080 fetch_type, &fetch_result);
2082 mailstream_logger = imap_logger_cmd;
2083 mailimap_fetch_type_free(fetch_type);
2084 mailimap_set_free(set);
2086 if (r != MAILIMAP_NO_ERROR) {
2092 r = result_to_uid_flags_list(fetch_result, &env_list);
2093 mailimap_fetch_list_free(fetch_result);
2095 * result = env_list;
2097 return MAILIMAP_NO_ERROR;
2100 mailimap_fetch_type_free(fetch_type);
2102 mailimap_set_free(set);
2109 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2111 struct fetch_uid_param * param;
2112 struct fetch_uid_result * result;
2113 carray * fetch_result;
2117 result = op->result;
2121 fetch_result = NULL;
2122 r = imap_get_messages_flags_list(param->imap, param->first_index,
2126 result->fetch_result = fetch_result;
2127 debug_print("imap fetch_uid run - end %i\n", r);
2130 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2131 carray ** fetch_result)
2133 struct fetch_uid_param param;
2134 struct fetch_uid_result result;
2137 debug_print("imap fetch_uid - begin\n");
2139 imap = get_imap(folder);
2141 param.first_index = first_index;
2143 mailstream_logger = imap_logger_noop;
2144 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2146 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2148 mailstream_logger = imap_logger_cmd;
2151 if (result.error != MAILIMAP_NO_ERROR)
2152 return result.error;
2154 debug_print("imap fetch_uid - end\n");
2156 * fetch_result = result.fetch_result;
2158 return result.error;
2162 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2166 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2169 data = carray_get(uid_flags_list, i);
2171 data = carray_get(uid_flags_list, i + 1);
2174 carray_free(uid_flags_list);
2179 static int imap_fetch(mailimap * imap,
2182 size_t * result_len)
2185 struct mailimap_set * set;
2186 struct mailimap_fetch_att * fetch_att;
2187 struct mailimap_fetch_type * fetch_type;
2188 clist * fetch_result;
2189 struct mailimap_msg_att * msg_att;
2190 struct mailimap_msg_att_item * msg_att_item;
2195 struct mailimap_section * section;
2197 set = mailimap_set_new_single(msg_index);
2199 res = MAILIMAP_ERROR_MEMORY;
2203 section = mailimap_section_new(NULL);
2204 if (section == NULL) {
2205 res = MAILIMAP_ERROR_MEMORY;
2209 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2210 if (fetch_att == NULL) {
2211 mailimap_section_free(section);
2212 res = MAILIMAP_ERROR_MEMORY;
2216 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2217 if (fetch_type == NULL) {
2218 res = MAILIMAP_ERROR_MEMORY;
2219 goto free_fetch_att;
2222 mailstream_logger = imap_logger_fetch;
2224 r = mailimap_uid_fetch(imap, set,
2225 fetch_type, &fetch_result);
2227 mailstream_logger = imap_logger_cmd;
2229 mailimap_fetch_type_free(fetch_type);
2230 mailimap_set_free(set);
2233 case MAILIMAP_NO_ERROR:
2239 if (clist_begin(fetch_result) == NULL) {
2240 mailimap_fetch_list_free(fetch_result);
2241 return MAILIMAP_ERROR_FETCH;
2244 msg_att = clist_begin(fetch_result)->data;
2249 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2250 cur = clist_next(cur)) {
2251 msg_att_item = clist_content(cur);
2253 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2254 if (msg_att_item->att_data.att_static->att_type ==
2255 MAILIMAP_MSG_ATT_BODY_SECTION) {
2256 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2258 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2260 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2265 mailimap_fetch_list_free(fetch_result);
2268 return MAILIMAP_ERROR_FETCH;
2271 * result_len = text_length;
2273 return MAILIMAP_NO_ERROR;
2276 mailimap_fetch_att_free(fetch_att);
2278 mailimap_set_free(set);
2283 static int imap_fetch_header(mailimap * imap,
2286 size_t * result_len)
2289 struct mailimap_set * set;
2290 struct mailimap_fetch_att * fetch_att;
2291 struct mailimap_fetch_type * fetch_type;
2292 clist * fetch_result;
2293 struct mailimap_msg_att * msg_att;
2294 struct mailimap_msg_att_item * msg_att_item;
2299 struct mailimap_section * section;
2301 set = mailimap_set_new_single(msg_index);
2303 res = MAILIMAP_ERROR_MEMORY;
2307 section = mailimap_section_new_header();
2308 if (section == NULL) {
2309 res = MAILIMAP_ERROR_MEMORY;
2313 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2314 if (fetch_att == NULL) {
2315 mailimap_section_free(section);
2316 res = MAILIMAP_ERROR_MEMORY;
2320 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2321 if (fetch_type == NULL) {
2322 res = MAILIMAP_ERROR_MEMORY;
2323 goto free_fetch_att;
2326 mailstream_logger = imap_logger_fetch;
2328 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2330 mailstream_logger = imap_logger_cmd;
2331 mailimap_fetch_type_free(fetch_type);
2332 mailimap_set_free(set);
2335 case MAILIMAP_NO_ERROR:
2341 if (clist_begin(fetch_result) == NULL) {
2342 mailimap_fetch_list_free(fetch_result);
2343 return MAILIMAP_ERROR_FETCH;
2346 msg_att = clist_begin(fetch_result)->data;
2351 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2352 cur = clist_next(cur)) {
2353 msg_att_item = clist_content(cur);
2355 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2356 if (msg_att_item->att_data.att_static->att_type ==
2357 MAILIMAP_MSG_ATT_BODY_SECTION) {
2358 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2359 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2361 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2366 mailimap_fetch_list_free(fetch_result);
2369 return MAILIMAP_ERROR_FETCH;
2372 * result_len = text_length;
2374 return MAILIMAP_NO_ERROR;
2377 mailimap_fetch_att_free(fetch_att);
2379 mailimap_set_free(set);
2386 struct fetch_content_param {
2389 const char * filename;
2393 struct fetch_content_result {
2397 static void fetch_content_run(struct etpan_thread_op * op)
2399 struct fetch_content_param * param;
2400 struct fetch_content_result * result;
2402 size_t content_size;
2408 result = op->result;
2414 if (param->with_body)
2415 r = imap_fetch(param->imap, param->msg_index,
2416 &content, &content_size);
2418 r = imap_fetch_header(param->imap, param->msg_index,
2419 &content, &content_size);
2423 if (r == MAILIMAP_NO_ERROR) {
2424 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2426 result->error = MAILIMAP_ERROR_FETCH;
2430 f = fdopen(fd, "wb");
2432 result->error = MAILIMAP_ERROR_FETCH;
2436 r = fwrite(content, 1, content_size, f);
2437 if (r < content_size) {
2438 result->error = MAILIMAP_ERROR_FETCH;
2444 result->error = MAILIMAP_ERROR_FETCH;
2455 claws_unlink(param->filename);
2458 /* mmap_string_unref is a simple free in libetpan
2459 * when it has MMAP_UNAVAILABLE defined */
2460 if (mmap_string_unref(content) != 0)
2464 debug_print("imap fetch_content run - end %i\n", result->error);
2467 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2469 const char * filename)
2471 struct fetch_content_param param;
2472 struct fetch_content_result result;
2475 debug_print("imap fetch_content - begin\n");
2477 imap = get_imap(folder);
2479 param.msg_index = msg_index;
2480 param.filename = filename;
2481 param.with_body = with_body;
2483 threaded_run(folder, ¶m, &result, fetch_content_run);
2485 if (result.error != MAILIMAP_NO_ERROR)
2486 return result.error;
2488 debug_print("imap fetch_content - end\n");
2490 return result.error;
2495 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2500 GSList *tags = NULL;
2504 flag_list = att_dyn->att_list;
2505 if (flag_list == NULL)
2508 for(cur = clist_begin(flag_list) ; cur != NULL ;
2509 cur = clist_next(cur)) {
2510 struct mailimap_flag_fetch * flag_fetch;
2512 flag_fetch = clist_content(cur);
2513 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2516 switch (flag_fetch->fl_flag->fl_type) {
2517 case MAILIMAP_FLAG_ANSWERED:
2518 flags |= MSG_REPLIED;
2520 case MAILIMAP_FLAG_FLAGGED:
2521 flags |= MSG_MARKED;
2523 case MAILIMAP_FLAG_DELETED:
2524 flags |= MSG_DELETED;
2526 case MAILIMAP_FLAG_SEEN:
2527 flags &= ~MSG_UNREAD;
2530 case MAILIMAP_FLAG_KEYWORD:
2531 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
2532 flags |= MSG_FORWARDED;
2534 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2544 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2548 struct mailimap_msg_att_dynamic ** patt_dyn)
2550 clistiter * item_cur;
2554 struct mailimap_msg_att_dynamic * att_dyn;
2561 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2562 item_cur = clist_next(item_cur)) {
2563 struct mailimap_msg_att_item * item;
2565 item = clist_content(item_cur);
2567 switch (item->att_type) {
2568 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2569 switch (item->att_data.att_static->att_type) {
2570 case MAILIMAP_MSG_ATT_UID:
2571 uid = item->att_data.att_static->att_data.att_uid;
2574 case MAILIMAP_MSG_ATT_BODY_SECTION:
2575 if (headers == NULL) {
2576 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2579 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2580 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2585 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2586 if (att_dyn == NULL) {
2587 att_dyn = item->att_data.att_dyn;
2595 if (pheaders != NULL)
2596 * pheaders = headers;
2597 if (pref_size != NULL)
2598 * pref_size = ref_size;
2599 if (patt_dyn != NULL)
2600 * patt_dyn = att_dyn;
2602 return MAIL_NO_ERROR;
2605 static struct imap_fetch_env_info *
2606 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2608 struct imap_fetch_env_info * info;
2612 struct mailimap_msg_att_dynamic * att_dyn;
2614 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2619 info = malloc(sizeof(* info));
2621 info->headers = strdup(headers);
2623 info->flags = imap_flags_to_flags(att_dyn, tags);
2629 imap_fetch_result_to_envelop_list(clist * fetch_result,
2630 carray ** p_env_list)
2637 env_list = carray_new(16);
2639 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2640 cur = clist_next(cur)) {
2641 struct mailimap_msg_att * msg_att;
2642 struct imap_fetch_env_info * env_info;
2643 GSList *tags = NULL;
2645 msg_att = clist_content(cur);
2647 env_info = fetch_to_env_info(msg_att, &tags);
2649 return MAILIMAP_ERROR_MEMORY;
2650 carray_add(env_list, env_info, NULL);
2651 carray_add(env_list, tags, NULL);
2654 * p_env_list = env_list;
2656 return MAIL_NO_ERROR;
2659 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2661 struct mailimap_fetch_att * fetch_att;
2665 struct mailimap_header_list * imap_hdrlist;
2666 struct mailimap_section * section;
2668 hdrlist = clist_new();
2670 header = strdup("Date");
2671 r = clist_append(hdrlist, header);
2672 header = strdup("From");
2673 r = clist_append(hdrlist, header);
2674 header = strdup("To");
2675 r = clist_append(hdrlist, header);
2676 header = strdup("Cc");
2677 r = clist_append(hdrlist, header);
2678 header = strdup("Subject");
2679 r = clist_append(hdrlist, header);
2680 header = strdup("Message-ID");
2681 r = clist_append(hdrlist, header);
2682 header = strdup("References");
2683 r = clist_append(hdrlist, header);
2684 header = strdup("In-Reply-To");
2685 r = clist_append(hdrlist, header);
2687 imap_hdrlist = mailimap_header_list_new(hdrlist);
2688 section = mailimap_section_new_header_fields(imap_hdrlist);
2689 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2690 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2692 return MAIL_NO_ERROR;
2695 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2697 struct mailimap_fetch_att * fetch_att;
2698 struct mailimap_section * section;
2700 section = mailimap_section_new_header();
2701 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2702 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2704 return MAIL_NO_ERROR;
2708 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2709 carray ** p_env_list)
2711 struct mailimap_fetch_att * fetch_att;
2712 struct mailimap_fetch_type * fetch_type;
2714 clist * fetch_result;
2716 carray * env_list = NULL;
2720 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2723 fetch_att = mailimap_fetch_att_new_uid();
2724 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2727 fetch_att = mailimap_fetch_att_new_flags();
2728 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2731 fetch_att = mailimap_fetch_att_new_rfc822_size();
2732 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2736 key.len = sizeof(imap);
2737 r = chash_get(courier_workaround_hash, &key, &value);
2739 r = imap_add_envelope_fetch_att(fetch_type);
2741 r = imap_add_header_fetch_att(fetch_type);
2743 mailstream_logger = imap_logger_fetch;
2745 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2747 mailstream_logger = imap_logger_cmd;
2749 case MAILIMAP_NO_ERROR:
2752 mailimap_fetch_type_free(fetch_type);
2753 debug_print("uid_fetch: %d\n", r);
2757 if (clist_begin(fetch_result) == NULL) {
2758 res = MAILIMAP_ERROR_FETCH;
2759 debug_print("clist_begin = NULL\n");
2763 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2764 mailimap_fetch_list_free(fetch_result);
2766 if (r != MAILIMAP_NO_ERROR) {
2767 mailimap_fetch_type_free(fetch_type);
2768 res = MAILIMAP_ERROR_MEMORY;
2769 debug_print("fetch_result_to_envelop_list: %d\n", res);
2773 mailimap_fetch_type_free(fetch_type);
2775 * p_env_list = env_list;
2777 return MAILIMAP_NO_ERROR;
2783 struct fetch_env_param {
2785 struct mailimap_set * set;
2788 struct fetch_env_result {
2789 carray * fetch_env_result;
2793 static void fetch_env_run(struct etpan_thread_op * op)
2795 struct fetch_env_param * param;
2796 struct fetch_env_result * result;
2801 result = op->result;
2806 r = imap_get_envelopes_list(param->imap, param->set,
2810 result->fetch_env_result = env_list;
2812 debug_print("imap fetch_env run - end %i\n", r);
2815 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2816 carray ** p_env_list)
2818 struct fetch_env_param param;
2819 struct fetch_env_result result;
2822 debug_print("imap fetch_env - begin\n");
2824 imap = get_imap(folder);
2828 threaded_run(folder, ¶m, &result, fetch_env_run);
2830 if (result.error != MAILIMAP_NO_ERROR) {
2836 key.len = sizeof(imap);
2837 r = chash_get(courier_workaround_hash, &key, &value);
2841 chash_set(courier_workaround_hash, &key, &value, NULL);
2843 threaded_run(folder, ¶m, &result, fetch_env_run);
2847 if (result.error != MAILIMAP_NO_ERROR)
2848 return result.error;
2850 debug_print("imap fetch_env - end\n");
2852 * p_env_list = result.fetch_env_result;
2854 return result.error;
2857 void imap_fetch_env_free(carray * env_list)
2861 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2862 struct imap_fetch_env_info * env_info;
2864 env_info = carray_get(env_list, i);
2865 free(env_info->headers);
2868 carray_free(env_list);
2875 struct append_param {
2877 const char * mailbox;
2878 const char * filename;
2879 struct mailimap_flag_list * flag_list;
2882 struct append_result {
2887 static void append_run(struct etpan_thread_op * op)
2889 struct append_param * param;
2890 struct append_result * result;
2895 struct stat stat_buf;
2898 guint32 uid = 0, val = 0;
2901 result = op->result;
2906 r = stat(param->filename, &stat_buf);
2908 result->error = MAILIMAP_ERROR_APPEND;
2911 size = stat_buf.st_size;
2913 fd = open(param->filename, O_RDONLY);
2915 result->error = MAILIMAP_ERROR_APPEND;
2919 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2920 if (data == (void *) MAP_FAILED) {
2922 result->error = MAILIMAP_ERROR_APPEND;
2926 data = file_read_to_str_no_recode(param->filename);
2928 result->error = MAILIMAP_ERROR_APPEND;
2931 size = strlen(data);
2933 mailstream_logger = imap_logger_append;
2935 r = mailimap_uidplus_append(param->imap, param->mailbox,
2936 param->flag_list, NULL,
2937 data, size, &val, &uid);
2939 mailstream_logger = imap_logger_cmd;
2950 debug_print("imap append run - end %i uid %d\n", r, uid);
2953 int imap_threaded_append(Folder * folder, const char * mailbox,
2954 const char * filename,
2955 struct mailimap_flag_list * flag_list,
2958 struct append_param param;
2959 struct append_result result;
2962 debug_print("imap append - begin\n");
2964 imap = get_imap(folder);
2966 param.mailbox = mailbox;
2967 param.filename = filename;
2968 param.flag_list = flag_list;
2970 threaded_run(folder, ¶m, &result, append_run);
2972 if (result.error != MAILIMAP_NO_ERROR)
2973 return result.error;
2975 debug_print("imap append - end\n");
2979 return result.error;
2985 struct expunge_param {
2989 struct expunge_result {
2993 static void expunge_run(struct etpan_thread_op * op)
2995 struct expunge_param * param;
2996 struct expunge_result * result;
3000 result = op->result;
3004 r = mailimap_expunge(param->imap);
3007 debug_print("imap expunge run - end %i\n", r);
3010 int imap_threaded_expunge(Folder * folder)
3012 struct expunge_param param;
3013 struct expunge_result result;
3015 debug_print("imap expunge - begin\n");
3017 param.imap = get_imap(folder);
3019 threaded_run(folder, ¶m, &result, expunge_run);
3021 debug_print("imap expunge - end\n");
3023 return result.error;
3029 struct mailimap_set * set;
3033 struct copy_result {
3035 struct mailimap_set *source;
3036 struct mailimap_set *dest;
3039 static void copy_run(struct etpan_thread_op * op)
3041 struct copy_param * param;
3042 struct copy_result * result;
3045 struct mailimap_set *source = NULL, *dest = NULL;
3048 result = op->result;
3052 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3053 &val, &source, &dest);
3057 result->source = source;
3058 result->dest = dest;
3060 result->source = NULL;
3061 result->dest = NULL;
3063 debug_print("imap copy run - end %i\n", r);
3066 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3067 const char * mb, struct mailimap_set **source,
3068 struct mailimap_set **dest)
3070 struct copy_param param;
3071 struct copy_result result;
3074 debug_print("imap copy - begin\n");
3076 imap = get_imap(folder);
3081 threaded_run(folder, ¶m, &result, copy_run);
3085 if (result.error != MAILIMAP_NO_ERROR)
3086 return result.error;
3088 *source = result.source;
3089 *dest = result.dest;
3091 debug_print("imap copy - end\n");
3093 return result.error;
3098 struct store_param {
3100 struct mailimap_set * set;
3101 struct mailimap_store_att_flags * store_att_flags;
3104 struct store_result {
3108 static void store_run(struct etpan_thread_op * op)
3110 struct store_param * param;
3111 struct store_result * result;
3115 result = op->result;
3119 r = mailimap_uid_store(param->imap, param->set,
3120 param->store_att_flags);
3124 debug_print("imap store run - end %i\n", r);
3127 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3128 struct mailimap_store_att_flags * store_att_flags)
3130 struct store_param param;
3131 struct store_result result;
3134 debug_print("imap store - begin\n");
3136 imap = get_imap(folder);
3139 param.store_att_flags = store_att_flags;
3141 threaded_run(folder, ¶m, &result, store_run);
3143 if (result.error != MAILIMAP_NO_ERROR)
3144 return result.error;
3146 debug_print("imap store - end\n");
3148 return result.error;
3152 #define ENV_BUFFER_SIZE 512
3154 static void do_exec_command(int fd, const char * command,
3155 const char * servername, uint16_t port)
3159 char env_buffer[ENV_BUFFER_SIZE];
3163 /* Fork again to become a child of init rather than
3164 the etpan client. */
3169 g_setenv("ETPANSERVER", servername, TRUE);
3171 g_unsetenv("ETPANSERVER");
3176 snprintf(porttext, sizeof(porttext), "%d", port);
3177 g_setenv("ETPANPORT", porttext, TRUE);
3180 g_unsetenv("ETPANPORT");
3183 /* Not a lot we can do if there's an error other than bail. */
3184 if (dup2(fd, 0) == -1)
3186 if (dup2(fd, 1) == -1)
3189 /* Should we close stderr and reopen /dev/null? */
3191 maxopen = sysconf(_SC_OPEN_MAX);
3192 for (i=3; i < maxopen; i++)
3196 /* Detach from the controlling tty if we have one. Otherwise,
3197 SSH might do something stupid like trying to use it instead
3198 of running $SSH_ASKPASS. Doh. */
3199 fd = open("/dev/tty", O_RDONLY);
3201 ioctl(fd, TIOCNOTTY, NULL);
3204 #endif /* TIOCNOTTY */
3206 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3208 /* Eep. Shouldn't reach this */
3212 static int subcommand_connect(const char *command,
3213 const char *servername, uint16_t port)
3215 /* SEB unsupported on Windows */
3219 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3224 do_exec_command(sockfds[1], command, servername, port);
3226 else if (childpid == -1) {
3234 /* Reap child, leaving grandchild process to run */
3235 waitpid(childpid, NULL, 0);
3240 static int socket_connect_cmd(mailimap * imap, const char * command,
3241 const char * server, int port)
3247 fd = subcommand_connect(command, server, port);
3249 return MAILIMAP_ERROR_STREAM;
3251 s = mailstream_socket_open(fd);
3254 return MAILIMAP_ERROR_STREAM;
3257 r = mailimap_connect(imap, s);
3258 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3259 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3260 mailstream_close(s);
3262 imap->imap_stream = NULL;
3271 struct connect_cmd_param {
3273 const char * command;
3274 const char * server;
3278 struct connect_cmd_result {
3282 static void connect_cmd_run(struct etpan_thread_op * op)
3285 struct connect_cmd_param * param;
3286 struct connect_cmd_result * result;
3289 result = op->result;
3293 r = socket_connect_cmd(param->imap, param->command,
3294 param->server, param->port);
3300 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3301 const char * server, int port)
3303 struct connect_cmd_param param;
3304 struct connect_cmd_result result;
3307 mailimap * imap, * oldimap;
3309 oldimap = get_imap(folder);
3311 imap = mailimap_new(0, NULL);
3314 debug_print("deleting old imap %p\n", oldimap);
3315 delete_imap(folder, oldimap);
3319 key.len = sizeof(folder);
3322 chash_set(session_hash, &key, &value, NULL);
3325 param.command = command;
3326 param.server = server;
3329 threaded_run(folder, ¶m, &result, connect_cmd_run);
3331 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3333 return result.error;
3335 #endif /* G_OS_WIN32 */
3337 void imap_threaded_cancel(Folder * folder)
3341 imap = get_imap(folder);
3342 if (imap->imap_stream != NULL)
3343 mailstream_cancel(imap->imap_stream);
3348 void imap_main_init(void)
3351 void imap_main_done(void)
3354 void imap_main_set_timeout(int sec)
3358 void imap_threaded_cancel(Folder * folder);