2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2011 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/>.
27 #include <glib/gi18n.h>
28 #include "imap-thread.h"
30 #include <sys/types.h>
32 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
33 #include <sys/socket.h>
42 #include "etpan-thread-manager.h"
44 #include "mainwindow.h"
46 #include "ssl_certificate.h"
48 #include "remotefolder.h"
51 #define DISABLE_LOG_DURING_LOGIN
53 static struct etpan_thread_manager * thread_manager = NULL;
54 static chash * courier_workaround_hash = NULL;
55 static chash * imap_hash = NULL;
56 static chash * session_hash = NULL;
57 static guint thread_manager_signal = 0;
58 static GIOChannel * io_channel = NULL;
60 static void delete_imap(Folder *folder, mailimap *imap)
66 key.len = sizeof(folder);
69 chash_delete(session_hash, &key, NULL);
72 key.len = sizeof(imap);
73 chash_delete(courier_workaround_hash, &key, NULL);
74 if (imap && imap->imap_stream) {
75 /* we don't want libetpan to logout */
76 mailstream_close(imap->imap_stream);
77 imap->imap_stream = NULL;
79 debug_print("removing mailimap %p\n", imap);
83 static gboolean thread_manager_event(GIOChannel * source,
84 GIOCondition condition,
91 if (condition & G_IO_IN)
92 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
94 etpan_thread_manager_loop(thread_manager);
99 static void imap_logger_noop(int direction, const char * str, size_t size)
101 /* inhibit logging */
104 static void imap_logger_cmd(int direction, const char * str, size_t size)
111 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
114 buf = malloc(size+1);
115 memset(buf, 0, size+1);
116 strncpy(buf, str, size);
119 if (!strncmp(buf, "<<<<<<<", 7)
120 || !strncmp(buf, ">>>>>>>", 7)) {
124 while (strstr(buf, "\r"))
125 *strstr(buf, "\r") = ' ';
126 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
127 buf[strlen(buf)-1] = '\0';
129 lines = g_strsplit(buf, "\n", -1);
131 while (lines[i] && *lines[i]) {
132 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
139 static void imap_logger_fetch(int direction, const char * str, size_t size)
145 if (size > 128 && !direction) {
146 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
150 buf = malloc(size+1);
151 memset(buf, 0, size+1);
152 strncpy(buf, str, size);
154 if (!strncmp(buf, "<<<<<<<", 7)
155 || !strncmp(buf, ">>>>>>>", 7)) {
159 while (strstr(buf, "\r"))
160 *strstr(buf, "\r") = ' ';
161 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
162 buf[strlen(buf)-1] = '\0';
164 lines = g_strsplit(buf, "\n", -1);
166 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
167 while (lines[i] && *lines[i]) {
168 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
172 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
178 static void imap_logger_uid(int direction, const char * str, size_t size)
185 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
188 buf = malloc(size+1);
189 memset(buf, 0, size+1);
190 strncpy(buf, str, size);
192 if (!strncmp(buf, "<<<<<<<", 7)
193 || !strncmp(buf, ">>>>>>>", 7)) {
197 while (strstr(buf, "\r"))
198 *strstr(buf, "\r") = ' ';
199 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
200 buf[strlen(buf)-1] = '\0';
202 lines = g_strsplit(buf, "\n", -1);
204 while (lines[i] && *lines[i]) {
205 int llen = strlen(lines[i]);
207 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
210 strncpy2(tmp, lines[i], 63);
211 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
220 static void imap_logger_append(int direction, const char * str, size_t size)
227 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
229 } else if (direction == 0 && size > 64) {
230 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
233 buf = malloc(size+1);
234 memset(buf, 0, size+1);
235 strncpy(buf, str, size);
237 if (!strncmp(buf, "<<<<<<<", 7)
238 || !strncmp(buf, ">>>>>>>", 7)) {
242 while (strstr(buf, "\r"))
243 *strstr(buf, "\r") = ' ';
244 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
245 buf[strlen(buf)-1] = '\0';
247 lines = g_strsplit(buf, "\n", -1);
249 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
250 while (lines[i] && *lines[i]) {
251 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
255 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
261 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
262 gboolean etpan_skip_ssl_cert_check = FALSE;
263 extern void mailsasl_ref(void);
265 void imap_main_init(gboolean skip_ssl_cert_check)
267 int fd_thread_manager;
269 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
270 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
271 mailstream_network_delay.tv_usec = 0;
273 mailstream_debug = 1;
274 mailstream_logger = imap_logger_cmd;
277 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
278 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
279 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
281 thread_manager = etpan_thread_manager_new();
283 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
286 io_channel = g_io_channel_unix_new(fd_thread_manager);
288 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
290 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
291 thread_manager_event,
296 void imap_main_set_timeout(int sec)
298 mailstream_network_delay.tv_sec = sec;
299 mailstream_network_delay.tv_usec = 0;
302 void imap_main_done(gboolean have_connectivity)
304 imap_disconnect_all(have_connectivity);
305 etpan_thread_manager_stop(thread_manager);
306 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
309 etpan_thread_manager_join(thread_manager);
311 g_source_remove(thread_manager_signal);
312 g_io_channel_unref(io_channel);
314 etpan_thread_manager_free(thread_manager);
316 chash_free(courier_workaround_hash);
317 chash_free(session_hash);
318 chash_free(imap_hash);
321 void imap_init(Folder * folder)
323 struct etpan_thread * thread;
327 thread = etpan_thread_manager_get_thread(thread_manager);
330 key.len = sizeof(folder);
334 chash_set(imap_hash, &key, &value, NULL);
337 void imap_done(Folder * folder)
339 struct etpan_thread * thread;
345 key.len = sizeof(folder);
347 r = chash_get(imap_hash, &key, &value);
353 etpan_thread_unbind(thread);
355 chash_delete(imap_hash, &key, NULL);
357 debug_print("remove thread");
360 static struct etpan_thread * get_thread(Folder * folder)
362 struct etpan_thread * thread;
367 key.len = sizeof(folder);
369 chash_get(imap_hash, &key, &value);
375 static mailimap * get_imap(Folder * folder)
383 key.len = sizeof(folder);
385 r = chash_get(session_hash, &key, &value);
390 debug_print("found imap %p\n", imap);
394 static gboolean cb_show_error(gpointer data)
396 mainwindow_show_error();
400 static void generic_cb(int cancelled, void * result, void * callback_data)
402 struct etpan_thread_op * op;
404 op = (struct etpan_thread_op *) callback_data;
406 debug_print("generic_cb\n");
407 if (op->imap && op->imap->imap_response_info &&
408 op->imap->imap_response_info->rsp_alert) {
409 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
410 op->imap->imap_response_info->rsp_alert);
411 g_timeout_add(10, cb_show_error, NULL);
416 static void threaded_run(Folder * folder, void * param, void * result,
417 void (* func)(struct etpan_thread_op * ))
419 struct etpan_thread_op * op;
420 struct etpan_thread * thread;
422 imap_folder_ref(folder);
424 op = etpan_thread_op_new();
426 op->imap = get_imap(folder);
432 op->callback = generic_cb;
433 op->callback_data = op;
438 thread = get_thread(folder);
439 etpan_thread_op_schedule(thread, op);
441 while (!op->finished) {
442 gtk_main_iteration();
445 etpan_thread_op_free(op);
447 imap_folder_unref(folder);
453 struct connect_param {
455 PrefsAccount *account;
460 struct connect_result {
464 #define CHECK_IMAP() { \
465 if (!param->imap) { \
466 result->error = MAILIMAP_ERROR_BAD_STATE; \
471 static void connect_run(struct etpan_thread_op * op)
474 struct connect_param * param;
475 struct connect_result * result;
482 r = mailimap_socket_connect(param->imap,
483 param->server, param->port);
489 int imap_threaded_connect(Folder * folder, const char * server, int port)
491 struct connect_param param;
492 struct connect_result result;
495 mailimap * imap, * oldimap;
497 oldimap = get_imap(folder);
499 imap = mailimap_new(0, NULL);
502 debug_print("deleting old imap %p\n", oldimap);
503 delete_imap(folder, oldimap);
507 key.len = sizeof(folder);
510 chash_set(session_hash, &key, &value, NULL);
513 param.server = server;
517 threaded_run(folder, ¶m, &result, connect_run);
519 debug_print("connect ok %i with imap %p\n", result.error, imap);
524 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
527 struct connect_param *param = (struct connect_param *)data;
530 if (certificate == NULL || len < 0) {
531 g_warning("no cert presented.\n");
534 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
536 g_warning("IMAP: can't get cert\n");
538 } else if (ssl_certificate_check(cert, (gchar *)param->server,
539 (gushort)param->port) == TRUE) {
547 struct connect_param *param = (struct connect_param *)data;
548 gnutls_x509_crt cert = NULL;
551 if (certificate == NULL || len < 0) {
552 g_warning("no cert presented.\n");
556 tmp.data = malloc(len);
557 memcpy(tmp.data, certificate, len);
559 gnutls_x509_crt_init(&cert);
560 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
561 g_warning("IMAP: can't get cert\n");
563 } else if (ssl_certificate_check(cert, (guint)-1, (gchar *)param->server,
564 (gushort)param->port) == TRUE) {
565 gnutls_x509_crt_deinit(cert);
568 gnutls_x509_crt_deinit(cert);
575 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
577 #if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
578 PrefsAccount *account = (PrefsAccount *)data;
579 const gchar *cert_path = NULL;
580 const gchar *password = NULL;
583 EVP_PKEY *pkey = NULL;
585 gnutls_x509_crt x509 = NULL;
586 gnutls_x509_privkey pkey = NULL;
589 if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
590 cert_path = account->in_ssl_client_cert_file;
591 if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
592 password = account->in_ssl_client_cert_pass;
594 if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
595 mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
596 debug_print("Impossible to set the client certificate.\n");
597 x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
598 pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
599 if (!(x509 && pkey)) {
600 /* try pkcs12 format */
601 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
604 unsigned char *x509_der = NULL, *pkey_der = NULL;
605 size_t x509_len, pkey_len;
608 x509_len = (size_t)i2d_X509(x509, &x509_der);
609 pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
611 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
612 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
614 if (x509_len > 0 && pkey_len > 0) {
615 if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
616 mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
617 log_error(LOG_PROTOCOL, _("Impossible to set the client certificate.\n"));
622 gnutls_x509_crt_deinit(x509);
623 gnutls_x509_privkey_deinit(pkey);
629 static void connect_ssl_run(struct etpan_thread_op * op)
632 struct connect_param * param;
633 struct connect_result * result;
640 r = mailimap_ssl_connect_with_callback(param->imap,
641 param->server, param->port,
642 connect_ssl_context_cb, param->account);
646 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
648 struct connect_param param;
649 struct connect_result result;
652 mailimap * imap, * oldimap;
653 unsigned char *certificate = NULL;
656 oldimap = get_imap(folder);
658 imap = mailimap_new(0, NULL);
661 debug_print("deleting old imap %p\n", oldimap);
662 delete_imap(folder, oldimap);
666 key.len = sizeof(folder);
669 chash_set(session_hash, &key, &value, NULL);
672 param.server = server;
674 param.account = folder->account;
677 threaded_run(folder, ¶m, &result, connect_ssl_run);
679 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
680 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
681 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
682 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
687 debug_print("connect %d with imap %p\n", result.error, imap);
698 struct mailimap_capability_data *caps;
701 static void capability_run(struct etpan_thread_op * op)
704 struct capa_param * param;
705 struct capa_result * result;
706 struct mailimap_capability_data *caps;
713 r = mailimap_capability(param->imap, &caps);
716 result->caps = (r == 0 ? caps : NULL);
720 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
722 struct capa_param param;
723 struct capa_result result;
726 imap = get_imap(folder);
730 threaded_run(folder, ¶m, &result, capability_run);
732 debug_print("capa %d\n", result.error);
741 struct disconnect_param {
745 struct disconnect_result {
749 static void disconnect_run(struct etpan_thread_op * op)
752 struct disconnect_param * param;
753 struct disconnect_result * result;
760 r = mailimap_logout(param->imap);
765 void imap_threaded_disconnect(Folder * folder)
767 struct connect_param param;
768 struct connect_result result;
771 imap = get_imap(folder);
773 debug_print("was disconnected\n");
779 threaded_run(folder, ¶m, &result, disconnect_run);
781 if (imap == get_imap(folder)) {
782 debug_print("deleting old imap %p\n", imap);
783 delete_imap(folder, imap);
785 debug_print("imap already deleted %p\n", imap);
788 debug_print("disconnect ok\n");
795 const char * wildcard;
804 static void list_run(struct etpan_thread_op * op)
806 struct list_param * param;
807 struct list_result * result;
818 if (param->base == NULL || param->wildcard == NULL) {
821 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
825 r = mailimap_lsub(param->imap, param->base,
826 param->wildcard, &list);
828 r = mailimap_list(param->imap, param->base,
829 param->wildcard, &list);
832 debug_print("imap list run - end\n");
835 int imap_threaded_list(Folder * folder, const char * base,
836 const char * wildcard,
839 struct list_param param;
840 struct list_result result;
842 debug_print("imap list - begin\n");
844 param.imap = get_imap(folder);
846 param.wildcard = wildcard;
847 param.sub_only = FALSE;
849 threaded_run(folder, ¶m, &result, list_run);
851 * p_result = result.list;
853 debug_print("imap list - end %p\n", result.list);
858 int imap_threaded_lsub(Folder * folder, const char * base,
859 const char * wildcard,
862 struct list_param param;
863 struct list_result result;
865 debug_print("imap lsub - begin\n");
867 param.imap = get_imap(folder);
869 param.wildcard = wildcard;
870 param.sub_only = TRUE;
872 threaded_run(folder, ¶m, &result, list_run);
874 * p_result = result.list;
876 debug_print("imap lsub - end %p\n", result.list);
881 struct subscribe_param {
887 struct subscribe_result {
891 static void subscribe_run(struct etpan_thread_op * op)
893 struct subscribe_param * param;
894 struct subscribe_result * result;
902 if (param->mb == NULL) {
904 debug_print("no mb\n");
907 if (param->subscribe)
908 r = mailimap_subscribe(param->imap, param->mb);
910 r = mailimap_unsubscribe(param->imap, param->mb);
912 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
915 int imap_threaded_subscribe(Folder * folder, const char * mb,
918 struct subscribe_param param;
919 struct subscribe_result result;
921 debug_print("imap list - begin\n");
923 param.imap = get_imap(folder);
925 param.subscribe = subscribe;
927 threaded_run(folder, ¶m, &result, subscribe_run);
935 const char * password;
940 struct login_result {
944 static void login_run(struct etpan_thread_op * op)
946 struct login_param * param;
947 struct login_result * result;
949 #ifdef DISABLE_LOG_DURING_LOGIN
958 #ifdef DISABLE_LOG_DURING_LOGIN
959 old_debug = mailstream_debug;
960 mailstream_debug = 0;
962 if (!strcmp(param->type, "LOGIN"))
963 r = mailimap_login(param->imap,
964 param->login, param->password);
965 else if (!strcmp(param->type, "GSSAPI"))
966 r = mailimap_authenticate(param->imap,
967 param->type, param->server, NULL, NULL,
968 param->login, param->login,
969 param->password, NULL);
971 r = mailimap_authenticate(param->imap,
972 param->type, NULL, NULL, NULL,
973 param->login, param->login,
974 param->password, NULL);
975 #ifdef DISABLE_LOG_DURING_LOGIN
976 mailstream_debug = old_debug;
980 if (param->imap->imap_response)
981 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
982 debug_print("imap login run - end %i\n", r);
985 int imap_threaded_login(Folder * folder,
986 const char * login, const char * password,
989 struct login_param param;
990 struct login_result result;
992 debug_print("imap login - begin\n");
994 param.imap = get_imap(folder);
996 param.password = password;
998 if (folder && folder->account)
999 param.server = folder->account->recv_server;
1001 param.server = NULL;
1003 threaded_run(folder, ¶m, &result, login_run);
1005 debug_print("imap login - end\n");
1007 return result.error;
1011 struct status_param {
1014 struct mailimap_status_att_list * status_att_list;
1017 struct status_result {
1019 struct mailimap_mailbox_data_status * data_status;
1022 static void status_run(struct etpan_thread_op * op)
1024 struct status_param * param;
1025 struct status_result * result;
1029 result = op->result;
1033 r = mailimap_status(param->imap, param->mb,
1034 param->status_att_list,
1035 &result->data_status);
1038 debug_print("imap status run - end %i\n", r);
1041 int imap_threaded_status(Folder * folder, const char * mb,
1042 struct mailimap_mailbox_data_status ** data_status,
1045 struct status_param param;
1046 struct status_result result;
1047 struct mailimap_status_att_list * status_att_list;
1049 debug_print("imap status - begin\n");
1051 status_att_list = mailimap_status_att_list_new_empty();
1052 if (mask & 1 << 0) {
1053 mailimap_status_att_list_add(status_att_list,
1054 MAILIMAP_STATUS_ATT_MESSAGES);
1056 if (mask & 1 << 1) {
1057 mailimap_status_att_list_add(status_att_list,
1058 MAILIMAP_STATUS_ATT_RECENT);
1060 if (mask & 1 << 2) {
1061 mailimap_status_att_list_add(status_att_list,
1062 MAILIMAP_STATUS_ATT_UIDNEXT);
1064 if (mask & 1 << 3) {
1065 mailimap_status_att_list_add(status_att_list,
1066 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1068 if (mask & 1 << 4) {
1069 mailimap_status_att_list_add(status_att_list,
1070 MAILIMAP_STATUS_ATT_UNSEEN);
1072 param.imap = get_imap(folder);
1074 param.status_att_list = status_att_list;
1076 threaded_run(folder, ¶m, &result, status_run);
1078 debug_print("imap status - end\n");
1080 * data_status = result.data_status;
1082 mailimap_status_att_list_free(status_att_list);
1084 return result.error;
1093 struct noop_result {
1097 static void noop_run(struct etpan_thread_op * op)
1099 struct noop_param * param;
1100 struct noop_result * result;
1104 result = op->result;
1108 r = mailimap_noop(param->imap);
1111 debug_print("imap noop run - end %i\n", r);
1114 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1115 unsigned int *p_recent,
1116 unsigned int *p_expunge,
1117 unsigned int *p_unseen,
1118 unsigned int *p_uidnext,
1119 unsigned int *p_uidval)
1121 struct noop_param param;
1122 struct noop_result result;
1125 debug_print("imap noop - begin\n");
1127 imap = get_imap(folder);
1130 threaded_run(folder, ¶m, &result, noop_run);
1132 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1133 * p_exists = imap->imap_selection_info->sel_exists;
1134 * p_recent = imap->imap_selection_info->sel_recent;
1135 * p_unseen = imap->imap_selection_info->sel_unseen;
1136 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1137 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1145 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1146 imap->imap_response_info->rsp_expunged != NULL) {
1147 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1151 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1152 *p_exists, *p_recent, *p_expunge, *p_unseen,
1153 *p_uidnext, *p_uidval);
1155 return result.error;
1159 struct starttls_result {
1163 static void starttls_run(struct etpan_thread_op * op)
1165 struct connect_param * param;
1166 struct starttls_result * result;
1170 result = op->result;
1174 r = mailimap_starttls(param->imap);
1177 debug_print("imap starttls run - end %i\n", r);
1180 mailimap *imap = param->imap;
1181 mailstream_low *plain_low = NULL;
1182 mailstream_low *tls_low = NULL;
1185 plain_low = mailstream_get_low(imap->imap_stream);
1186 fd = mailstream_low_get_fd(plain_low);
1188 debug_print("imap starttls run - can't get fd\n");
1189 result->error = MAILIMAP_ERROR_STREAM;
1193 tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
1194 if (tls_low == NULL) {
1195 debug_print("imap starttls run - can't tls_open\n");
1196 result->error = MAILIMAP_ERROR_STREAM;
1199 mailstream_low_free(plain_low);
1200 mailstream_set_low(imap->imap_stream, tls_low);
1204 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1206 struct connect_param param;
1207 struct starttls_result result;
1209 unsigned char *certificate = NULL;
1211 debug_print("imap starttls - begin\n");
1213 param.imap = get_imap(folder);
1214 param.server = host;
1216 param.account = folder->account;
1218 threaded_run(folder, ¶m, &result, starttls_run);
1220 debug_print("imap starttls - end\n");
1222 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1223 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1224 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1225 result.error = MAILIMAP_ERROR_STREAM;
1229 return result.error;
1234 struct create_param {
1239 struct create_result {
1243 static void create_run(struct etpan_thread_op * op)
1245 struct create_param * param;
1246 struct create_result * result;
1250 result = op->result;
1254 r = mailimap_create(param->imap, param->mb);
1257 debug_print("imap create run - end %i\n", r);
1260 int imap_threaded_create(Folder * folder, const char * mb)
1262 struct create_param param;
1263 struct create_result result;
1265 debug_print("imap create - begin\n");
1267 param.imap = get_imap(folder);
1270 threaded_run(folder, ¶m, &result, create_run);
1272 debug_print("imap create - end\n");
1274 return result.error;
1280 struct rename_param {
1283 const char * new_name;
1286 struct rename_result {
1290 static void rename_run(struct etpan_thread_op * op)
1292 struct rename_param * param;
1293 struct rename_result * result;
1297 result = op->result;
1301 r = mailimap_rename(param->imap, param->mb, param->new_name);
1304 debug_print("imap rename run - end %i\n", r);
1307 int imap_threaded_rename(Folder * folder,
1308 const char * mb, const char * new_name)
1310 struct rename_param param;
1311 struct rename_result result;
1313 debug_print("imap rename - begin\n");
1315 param.imap = get_imap(folder);
1317 param.new_name = new_name;
1319 threaded_run(folder, ¶m, &result, rename_run);
1321 debug_print("imap rename - end\n");
1323 return result.error;
1329 struct delete_param {
1334 struct delete_result {
1338 static void delete_run(struct etpan_thread_op * op)
1340 struct delete_param * param;
1341 struct delete_result * result;
1345 result = op->result;
1349 r = mailimap_delete(param->imap, param->mb);
1352 debug_print("imap delete run - end %i\n", r);
1355 int imap_threaded_delete(Folder * folder, const char * mb)
1357 struct delete_param param;
1358 struct delete_result result;
1360 debug_print("imap delete - begin\n");
1362 param.imap = get_imap(folder);
1365 threaded_run(folder, ¶m, &result, delete_run);
1367 debug_print("imap delete - end\n");
1369 return result.error;
1374 struct select_param {
1379 struct select_result {
1383 static void select_run(struct etpan_thread_op * op)
1385 struct select_param * param;
1386 struct select_result * result;
1390 result = op->result;
1394 r = mailimap_select(param->imap, param->mb);
1397 debug_print("imap select run - end %i\n", r);
1400 int imap_threaded_select(Folder * folder, const char * mb,
1401 gint * exists, gint * recent, gint * unseen,
1402 guint32 * uid_validity,gint *can_create_flags,
1405 struct select_param param;
1406 struct select_result result;
1409 debug_print("imap select - begin\n");
1411 imap = get_imap(folder);
1415 threaded_run(folder, ¶m, &result, select_run);
1417 if (result.error != MAILIMAP_NO_ERROR)
1418 return result.error;
1420 if (!imap || imap->imap_selection_info == NULL)
1421 return MAILIMAP_ERROR_PARSE;
1423 * exists = imap->imap_selection_info->sel_exists;
1424 * recent = imap->imap_selection_info->sel_recent;
1425 * unseen = imap->imap_selection_info->sel_unseen;
1426 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1427 * can_create_flags = FALSE;
1429 if (imap->imap_selection_info->sel_perm_flags) {
1430 GSList *t_flags = NULL;
1431 clistiter *cur = NULL;
1432 if (imap->imap_selection_info->sel_perm_flags)
1433 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1435 for (; cur; cur = clist_next(cur)) {
1436 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1437 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1438 *can_create_flags = TRUE;
1439 else if (flag->fl_flag &&
1440 flag->fl_flag->fl_type == 6 &&
1441 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1442 *can_create_flags = TRUE;
1443 if (flag->fl_flag && ok_flags) {
1444 MsgPermFlags c_flag = 0;
1445 switch (flag->fl_flag->fl_type) {
1446 case MAILIMAP_FLAG_ANSWERED:
1447 c_flag = IMAP_FLAG_ANSWERED;
1449 case MAILIMAP_FLAG_FLAGGED:
1450 c_flag = IMAP_FLAG_FLAGGED;
1452 case MAILIMAP_FLAG_DELETED:
1453 c_flag = IMAP_FLAG_DELETED;
1455 case MAILIMAP_FLAG_DRAFT:
1456 c_flag = IMAP_FLAG_DRAFT;
1458 case MAILIMAP_FLAG_SEEN:
1459 c_flag = IMAP_FLAG_SEEN;
1461 case MAILIMAP_FLAG_KEYWORD:
1462 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1463 c_flag = IMAP_FLAG_FORWARDED;
1464 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1465 c_flag = IMAP_FLAG_SPAM;
1466 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1467 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1468 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1469 c_flag = IMAP_FLAG_HAM;
1475 t_flags = g_slist_prepend(t_flags,
1476 GUINT_TO_POINTER(c_flag));
1481 *ok_flags = t_flags;
1483 debug_print("imap select - end\n");
1485 return result.error;
1488 static void close_run(struct etpan_thread_op * op)
1490 struct select_param * param;
1491 struct select_result * result;
1495 result = op->result;
1499 r = mailimap_close(param->imap);
1502 debug_print("imap close run - end %i\n", r);
1505 int imap_threaded_close(Folder * folder)
1507 struct select_param param;
1508 struct select_result result;
1511 debug_print("imap close - begin\n");
1513 imap = get_imap(folder);
1516 threaded_run(folder, ¶m, &result, close_run);
1518 if (result.error != MAILIMAP_NO_ERROR)
1519 return result.error;
1521 debug_print("imap close - end\n");
1523 return result.error;
1526 struct examine_param {
1531 struct examine_result {
1535 static void examine_run(struct etpan_thread_op * op)
1537 struct examine_param * param;
1538 struct examine_result * result;
1542 result = op->result;
1546 r = mailimap_examine(param->imap, param->mb);
1549 debug_print("imap examine run - end %i\n", r);
1552 int imap_threaded_examine(Folder * folder, const char * mb,
1553 gint * exists, gint * recent, gint * unseen,
1554 guint32 * uid_validity)
1556 struct examine_param param;
1557 struct examine_result result;
1560 debug_print("imap examine - begin\n");
1562 imap = get_imap(folder);
1566 threaded_run(folder, ¶m, &result, examine_run);
1568 if (result.error != MAILIMAP_NO_ERROR)
1569 return result.error;
1571 if (!imap || imap->imap_selection_info == NULL)
1572 return MAILIMAP_ERROR_PARSE;
1574 * exists = imap->imap_selection_info->sel_exists;
1575 * recent = imap->imap_selection_info->sel_recent;
1576 * unseen = imap->imap_selection_info->sel_unseen;
1577 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1579 debug_print("imap examine - end\n");
1581 return result.error;
1587 struct search_param {
1590 struct mailimap_set * set;
1593 struct search_result {
1595 clist * search_result;
1598 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1600 return mailimap_set_item_new(orig->set_first, orig->set_last);
1603 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1605 clist *list = orig ? orig->set_list : NULL;
1606 clist *newlist = clist_new();
1611 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1612 clist_append(newlist,
1613 sc_mailimap_set_item_copy(
1614 (struct mailimap_set_item *)clist_content(cur)));
1615 return mailimap_set_new(newlist);
1618 static void search_run(struct etpan_thread_op * op)
1620 struct search_param * param;
1621 struct search_result * result;
1623 struct mailimap_search_key * key = NULL;
1624 struct mailimap_search_key * uid_key = NULL;
1625 struct mailimap_search_key * search_type_key;
1626 clist * search_result;
1629 result = op->result;
1633 /* we copy the mailimap_set because freeing the key is recursive */
1634 if (param->set != NULL) {
1635 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1636 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1637 uid_key = mailimap_search_key_new_all();
1639 search_type_key = NULL;
1640 switch (param->type) {
1641 case IMAP_SEARCH_TYPE_SIMPLE:
1642 search_type_key = NULL;
1645 case IMAP_SEARCH_TYPE_SEEN:
1646 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1647 NULL, NULL, NULL, NULL, NULL,
1648 NULL, NULL, NULL, NULL, NULL,
1649 NULL, NULL, NULL, NULL, 0,
1650 NULL, NULL, NULL, NULL, NULL,
1651 NULL, 0, NULL, NULL, NULL);
1654 case IMAP_SEARCH_TYPE_UNSEEN:
1655 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1656 NULL, NULL, NULL, NULL, NULL,
1657 NULL, NULL, NULL, NULL, NULL,
1658 NULL, NULL, NULL, NULL, 0,
1659 NULL, NULL, NULL, NULL, NULL,
1660 NULL, 0, NULL, NULL, NULL);
1663 case IMAP_SEARCH_TYPE_ANSWERED:
1664 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1665 NULL, NULL, NULL, NULL, NULL,
1666 NULL, NULL, NULL, NULL, NULL,
1667 NULL, NULL, NULL, NULL, 0,
1668 NULL, NULL, NULL, NULL, NULL,
1669 NULL, 0, NULL, NULL, NULL);
1672 case IMAP_SEARCH_TYPE_FLAGGED:
1673 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1674 NULL, NULL, NULL, NULL, NULL,
1675 NULL, NULL, NULL, NULL, NULL,
1676 NULL, NULL, NULL, NULL, 0,
1677 NULL, NULL, NULL, NULL, NULL,
1678 NULL, 0, NULL, NULL, NULL);
1680 case IMAP_SEARCH_TYPE_DELETED:
1681 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1682 NULL, NULL, NULL, NULL, NULL,
1683 NULL, NULL, NULL, NULL, NULL,
1684 NULL, NULL, NULL, NULL, 0,
1685 NULL, NULL, NULL, NULL, NULL,
1686 NULL, 0, NULL, NULL, NULL);
1688 case IMAP_SEARCH_TYPE_FORWARDED:
1689 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1690 NULL, NULL, NULL, NULL, NULL,
1691 strdup(RTAG_FORWARDED), NULL, NULL, NULL, NULL,
1692 NULL, NULL, NULL, NULL, 0,
1693 NULL, NULL, NULL, NULL, NULL,
1694 NULL, 0, NULL, NULL, NULL);
1696 case IMAP_SEARCH_TYPE_SPAM:
1697 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1698 NULL, NULL, NULL, NULL, NULL,
1699 strdup(RTAG_JUNK), NULL, NULL, NULL, NULL,
1700 NULL, NULL, NULL, NULL, 0,
1701 NULL, NULL, NULL, NULL, NULL,
1702 NULL, 0, NULL, NULL, NULL);
1706 if (search_type_key != NULL) {
1707 if (param->set != NULL) {
1708 key = mailimap_search_key_new_multiple_empty();
1709 mailimap_search_key_multiple_add(key, search_type_key);
1710 mailimap_search_key_multiple_add(key, uid_key);
1712 key = search_type_key;
1714 } else if (uid_key != NULL) {
1719 g_warning("no key!");
1720 result = op->result;
1722 result->search_result = NULL;
1724 mailstream_logger = imap_logger_uid;
1726 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1728 mailstream_logger = imap_logger_cmd;
1730 /* free the key (with the imapset) */
1731 mailimap_search_key_free(key);
1734 result->search_result = search_result;
1736 debug_print("imap search run - end %i\n", result->error);
1739 int imap_threaded_search(Folder * folder, int search_type,
1740 struct mailimap_set * set, clist ** search_result)
1742 struct search_param param;
1743 struct search_result result;
1746 debug_print("imap search - begin\n");
1748 imap = get_imap(folder);
1751 param.type = search_type;
1753 threaded_run(folder, ¶m, &result, search_run);
1755 if (result.error != MAILIMAP_NO_ERROR)
1756 return result.error;
1758 debug_print("imap search - end\n");
1760 * search_result = result.search_result;
1762 return result.error;
1767 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1771 struct mailimap_msg_att_dynamic ** patt_dyn);
1774 result_to_uid_list(clist * fetch_result, carray ** result)
1776 clistiter * cur = NULL;
1781 tab = carray_new(128);
1783 res = MAILIMAP_ERROR_MEMORY;
1788 cur = clist_begin(fetch_result);
1790 for(; cur != NULL ; cur = clist_next(cur)) {
1791 struct mailimap_msg_att * msg_att;
1795 msg_att = clist_content(cur);
1798 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1800 puid = malloc(sizeof(* puid));
1802 res = MAILIMAP_ERROR_MEMORY;
1807 r = carray_add(tab, puid, NULL);
1810 res = MAILIMAP_ERROR_MEMORY;
1817 return MAILIMAP_NO_ERROR;
1820 imap_fetch_uid_list_free(tab);
1825 static int imap_get_messages_list(mailimap * imap,
1826 uint32_t first_index,
1831 struct mailimap_fetch_att * fetch_att;
1832 struct mailimap_fetch_type * fetch_type;
1833 struct mailimap_set * set;
1834 clist * fetch_result;
1837 set = mailimap_set_new_interval(first_index, 0);
1839 res = MAILIMAP_ERROR_MEMORY;
1843 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1844 if (fetch_type == NULL) {
1845 res = MAILIMAP_ERROR_MEMORY;
1849 fetch_att = mailimap_fetch_att_new_uid();
1850 if (fetch_att == NULL) {
1851 res = MAILIMAP_ERROR_MEMORY;
1852 goto free_fetch_type;
1855 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1856 if (r != MAILIMAP_NO_ERROR) {
1857 mailimap_fetch_att_free(fetch_att);
1858 res = MAILIMAP_ERROR_MEMORY;
1859 goto free_fetch_type;
1862 mailstream_logger = imap_logger_fetch;
1864 r = mailimap_uid_fetch(imap, set,
1865 fetch_type, &fetch_result);
1867 mailstream_logger = imap_logger_cmd;
1868 mailimap_fetch_type_free(fetch_type);
1869 mailimap_set_free(set);
1871 if (r != MAILIMAP_NO_ERROR) {
1877 r = result_to_uid_list(fetch_result, &env_list);
1878 mailimap_fetch_list_free(fetch_result);
1880 * result = env_list;
1882 return MAILIMAP_NO_ERROR;
1885 mailimap_fetch_type_free(fetch_type);
1887 mailimap_set_free(set);
1895 struct fetch_uid_param {
1897 uint32_t first_index;
1900 struct fetch_uid_result {
1902 carray * fetch_result;
1905 static void fetch_uid_run(struct etpan_thread_op * op)
1907 struct fetch_uid_param * param;
1908 struct fetch_uid_result * result;
1909 carray * fetch_result;
1913 result = op->result;
1917 fetch_result = NULL;
1918 mailstream_logger = imap_logger_noop;
1919 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1921 r = imap_get_messages_list(param->imap, param->first_index,
1924 mailstream_logger = imap_logger_cmd;
1927 result->fetch_result = fetch_result;
1928 debug_print("imap fetch_uid run - end %i\n", r);
1931 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1932 carray ** fetch_result)
1934 struct fetch_uid_param param;
1935 struct fetch_uid_result result;
1938 debug_print("imap fetch_uid - begin\n");
1940 imap = get_imap(folder);
1942 param.first_index = first_index;
1944 threaded_run(folder, ¶m, &result, fetch_uid_run);
1946 if (result.error != MAILIMAP_NO_ERROR)
1947 return result.error;
1949 debug_print("imap fetch_uid - end\n");
1951 * fetch_result = result.fetch_result;
1953 return result.error;
1957 void imap_fetch_uid_list_free(carray * uid_list)
1961 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1964 puid = carray_get(uid_list, i);
1967 carray_free(uid_list);
1973 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1976 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1978 clistiter * cur = NULL;
1982 GSList *tags = NULL;
1984 tab = carray_new(128);
1986 res = MAILIMAP_ERROR_MEMORY;
1991 cur = clist_begin(fetch_result);
1993 for(; cur != NULL ; cur = clist_next(cur)) {
1994 struct mailimap_msg_att * msg_att;
1997 struct mailimap_msg_att_dynamic * att_dyn;
2003 msg_att = clist_content(cur);
2007 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2010 if (att_dyn == NULL)
2013 flags = imap_flags_to_flags(att_dyn, &tags);
2015 puid = malloc(sizeof(* puid));
2017 res = MAILIMAP_ERROR_MEMORY;
2022 r = carray_add(tab, puid, NULL);
2025 res = MAILIMAP_ERROR_MEMORY;
2028 pflags = malloc(sizeof(* pflags));
2029 if (pflags == NULL) {
2030 res = MAILIMAP_ERROR_MEMORY;
2034 r = carray_add(tab, pflags, NULL);
2037 res = MAILIMAP_ERROR_MEMORY;
2040 r = carray_add(tab, tags, NULL);
2043 res = MAILIMAP_ERROR_MEMORY;
2050 return MAILIMAP_NO_ERROR;
2053 imap_fetch_uid_flags_list_free(tab);
2054 slist_free_strings(tags);
2060 static int imap_get_messages_flags_list(mailimap * imap,
2061 uint32_t first_index,
2066 struct mailimap_fetch_att * fetch_att;
2067 struct mailimap_fetch_type * fetch_type;
2068 struct mailimap_set * set;
2069 clist * fetch_result;
2072 set = mailimap_set_new_interval(first_index, 0);
2074 res = MAILIMAP_ERROR_MEMORY;
2078 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2079 if (fetch_type == NULL) {
2080 res = MAILIMAP_ERROR_MEMORY;
2084 fetch_att = mailimap_fetch_att_new_flags();
2085 if (fetch_att == NULL) {
2086 res = MAILIMAP_ERROR_MEMORY;
2087 goto free_fetch_type;
2090 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2091 if (r != MAILIMAP_NO_ERROR) {
2092 mailimap_fetch_att_free(fetch_att);
2093 res = MAILIMAP_ERROR_MEMORY;
2094 goto free_fetch_type;
2097 fetch_att = mailimap_fetch_att_new_uid();
2098 if (fetch_att == NULL) {
2099 res = MAILIMAP_ERROR_MEMORY;
2100 goto free_fetch_type;
2103 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2104 if (r != MAILIMAP_NO_ERROR) {
2105 mailimap_fetch_att_free(fetch_att);
2106 res = MAILIMAP_ERROR_MEMORY;
2107 goto free_fetch_type;
2110 mailstream_logger = imap_logger_fetch;
2112 r = mailimap_uid_fetch(imap, set,
2113 fetch_type, &fetch_result);
2115 mailstream_logger = imap_logger_cmd;
2116 mailimap_fetch_type_free(fetch_type);
2117 mailimap_set_free(set);
2119 if (r != MAILIMAP_NO_ERROR) {
2125 r = result_to_uid_flags_list(fetch_result, &env_list);
2126 mailimap_fetch_list_free(fetch_result);
2128 * result = env_list;
2130 return MAILIMAP_NO_ERROR;
2133 mailimap_fetch_type_free(fetch_type);
2135 mailimap_set_free(set);
2142 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2144 struct fetch_uid_param * param;
2145 struct fetch_uid_result * result;
2146 carray * fetch_result;
2150 result = op->result;
2154 fetch_result = NULL;
2155 r = imap_get_messages_flags_list(param->imap, param->first_index,
2159 result->fetch_result = fetch_result;
2160 debug_print("imap fetch_uid run - end %i\n", r);
2163 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2164 carray ** fetch_result)
2166 struct fetch_uid_param param;
2167 struct fetch_uid_result result;
2170 debug_print("imap fetch_uid - begin\n");
2172 imap = get_imap(folder);
2174 param.first_index = first_index;
2176 mailstream_logger = imap_logger_noop;
2177 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2179 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2181 mailstream_logger = imap_logger_cmd;
2184 if (result.error != MAILIMAP_NO_ERROR)
2185 return result.error;
2187 debug_print("imap fetch_uid - end\n");
2189 * fetch_result = result.fetch_result;
2191 return result.error;
2195 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2199 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2202 data = carray_get(uid_flags_list, i);
2204 data = carray_get(uid_flags_list, i + 1);
2207 carray_free(uid_flags_list);
2212 static int imap_fetch(mailimap * imap,
2215 size_t * result_len)
2218 struct mailimap_set * set;
2219 struct mailimap_fetch_att * fetch_att;
2220 struct mailimap_fetch_type * fetch_type;
2221 clist * fetch_result;
2222 struct mailimap_msg_att * msg_att;
2223 struct mailimap_msg_att_item * msg_att_item;
2228 struct mailimap_section * section;
2230 set = mailimap_set_new_single(msg_index);
2232 res = MAILIMAP_ERROR_MEMORY;
2236 section = mailimap_section_new(NULL);
2237 if (section == NULL) {
2238 res = MAILIMAP_ERROR_MEMORY;
2242 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2243 if (fetch_att == NULL) {
2244 mailimap_section_free(section);
2245 res = MAILIMAP_ERROR_MEMORY;
2249 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2250 if (fetch_type == NULL) {
2251 res = MAILIMAP_ERROR_MEMORY;
2252 goto free_fetch_att;
2255 mailstream_logger = imap_logger_fetch;
2257 r = mailimap_uid_fetch(imap, set,
2258 fetch_type, &fetch_result);
2260 mailstream_logger = imap_logger_cmd;
2262 mailimap_fetch_type_free(fetch_type);
2263 mailimap_set_free(set);
2266 case MAILIMAP_NO_ERROR:
2272 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2273 mailimap_fetch_list_free(fetch_result);
2274 return MAILIMAP_ERROR_FETCH;
2277 msg_att = clist_begin(fetch_result)->data;
2282 if (msg_att->att_list)
2283 cur = clist_begin(msg_att->att_list);
2287 for(; cur != NULL ; cur = clist_next(cur)) {
2288 msg_att_item = clist_content(cur);
2290 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2291 if (msg_att_item->att_data.att_static->att_type ==
2292 MAILIMAP_MSG_ATT_BODY_SECTION) {
2293 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2295 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2297 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2302 mailimap_fetch_list_free(fetch_result);
2305 return MAILIMAP_ERROR_FETCH;
2308 * result_len = text_length;
2310 return MAILIMAP_NO_ERROR;
2313 mailimap_fetch_att_free(fetch_att);
2315 mailimap_set_free(set);
2320 static int imap_fetch_header(mailimap * imap,
2323 size_t * result_len)
2326 struct mailimap_set * set;
2327 struct mailimap_fetch_att * fetch_att;
2328 struct mailimap_fetch_type * fetch_type;
2329 clist * fetch_result;
2330 struct mailimap_msg_att * msg_att;
2331 struct mailimap_msg_att_item * msg_att_item;
2336 struct mailimap_section * section;
2338 set = mailimap_set_new_single(msg_index);
2340 res = MAILIMAP_ERROR_MEMORY;
2344 section = mailimap_section_new_header();
2345 if (section == NULL) {
2346 res = MAILIMAP_ERROR_MEMORY;
2350 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2351 if (fetch_att == NULL) {
2352 mailimap_section_free(section);
2353 res = MAILIMAP_ERROR_MEMORY;
2357 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2358 if (fetch_type == NULL) {
2359 res = MAILIMAP_ERROR_MEMORY;
2360 goto free_fetch_att;
2363 mailstream_logger = imap_logger_fetch;
2365 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2367 mailstream_logger = imap_logger_cmd;
2368 mailimap_fetch_type_free(fetch_type);
2369 mailimap_set_free(set);
2372 case MAILIMAP_NO_ERROR:
2378 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2379 mailimap_fetch_list_free(fetch_result);
2380 return MAILIMAP_ERROR_FETCH;
2383 msg_att = clist_begin(fetch_result)->data;
2388 if (msg_att->att_list)
2389 cur = clist_begin(msg_att->att_list);
2393 for(; cur != NULL ; cur = clist_next(cur)) {
2394 msg_att_item = clist_content(cur);
2396 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2397 if (msg_att_item->att_data.att_static->att_type ==
2398 MAILIMAP_MSG_ATT_BODY_SECTION) {
2399 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2400 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2402 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2407 mailimap_fetch_list_free(fetch_result);
2410 return MAILIMAP_ERROR_FETCH;
2413 * result_len = text_length;
2415 return MAILIMAP_NO_ERROR;
2418 mailimap_fetch_att_free(fetch_att);
2420 mailimap_set_free(set);
2427 struct fetch_content_param {
2430 const char * filename;
2434 struct fetch_content_result {
2438 static void fetch_content_run(struct etpan_thread_op * op)
2440 struct fetch_content_param * param;
2441 struct fetch_content_result * result;
2443 size_t content_size;
2449 result = op->result;
2455 if (param->with_body)
2456 r = imap_fetch(param->imap, param->msg_index,
2457 &content, &content_size);
2459 r = imap_fetch_header(param->imap, param->msg_index,
2460 &content, &content_size);
2464 if (r == MAILIMAP_NO_ERROR) {
2465 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2467 result->error = MAILIMAP_ERROR_FETCH;
2471 f = fdopen(fd, "wb");
2473 result->error = MAILIMAP_ERROR_FETCH;
2477 r = fwrite(content, 1, content_size, f);
2478 if (r < content_size) {
2479 result->error = MAILIMAP_ERROR_FETCH;
2485 result->error = MAILIMAP_ERROR_FETCH;
2496 claws_unlink(param->filename);
2499 /* mmap_string_unref is a simple free in libetpan
2500 * when it has MMAP_UNAVAILABLE defined */
2501 if (mmap_string_unref(content) != 0)
2505 debug_print("imap fetch_content run - end %i\n", result->error);
2508 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2510 const char * filename)
2512 struct fetch_content_param param;
2513 struct fetch_content_result result;
2516 debug_print("imap fetch_content - begin\n");
2518 imap = get_imap(folder);
2520 param.msg_index = msg_index;
2521 param.filename = filename;
2522 param.with_body = with_body;
2524 threaded_run(folder, ¶m, &result, fetch_content_run);
2526 if (result.error != MAILIMAP_NO_ERROR)
2527 return result.error;
2529 debug_print("imap fetch_content - end\n");
2531 return result.error;
2536 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2541 GSList *tags = NULL;
2545 flag_list = att_dyn->att_list;
2546 if (flag_list == NULL)
2549 for(cur = clist_begin(flag_list) ; cur != NULL ;
2550 cur = clist_next(cur)) {
2551 struct mailimap_flag_fetch * flag_fetch;
2553 flag_fetch = clist_content(cur);
2554 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2557 switch (flag_fetch->fl_flag->fl_type) {
2558 case MAILIMAP_FLAG_ANSWERED:
2559 flags |= MSG_REPLIED;
2561 case MAILIMAP_FLAG_FLAGGED:
2562 flags |= MSG_MARKED;
2564 case MAILIMAP_FLAG_DELETED:
2565 flags |= MSG_DELETED;
2567 case MAILIMAP_FLAG_SEEN:
2568 flags &= ~MSG_UNREAD;
2571 case MAILIMAP_FLAG_KEYWORD:
2572 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2573 flags |= MSG_FORWARDED;
2574 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2576 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2577 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2578 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2581 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2591 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2595 struct mailimap_msg_att_dynamic ** patt_dyn)
2597 clistiter * item_cur;
2601 struct mailimap_msg_att_dynamic * att_dyn;
2608 if (msg_att->att_list)
2609 item_cur = clist_begin(msg_att->att_list);
2612 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2613 struct mailimap_msg_att_item * item;
2615 item = clist_content(item_cur);
2617 switch (item->att_type) {
2618 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2619 switch (item->att_data.att_static->att_type) {
2620 case MAILIMAP_MSG_ATT_UID:
2621 uid = item->att_data.att_static->att_data.att_uid;
2624 case MAILIMAP_MSG_ATT_BODY_SECTION:
2625 if (headers == NULL) {
2626 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2629 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2630 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2635 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2636 if (att_dyn == NULL) {
2637 att_dyn = item->att_data.att_dyn;
2645 if (pheaders != NULL)
2646 * pheaders = headers;
2647 if (pref_size != NULL)
2648 * pref_size = ref_size;
2649 if (patt_dyn != NULL)
2650 * patt_dyn = att_dyn;
2652 return MAIL_NO_ERROR;
2655 static struct imap_fetch_env_info *
2656 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2658 struct imap_fetch_env_info * info;
2662 struct mailimap_msg_att_dynamic * att_dyn;
2664 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2669 info = malloc(sizeof(* info));
2671 info->headers = strdup(headers);
2673 info->flags = imap_flags_to_flags(att_dyn, tags);
2679 imap_fetch_result_to_envelop_list(clist * fetch_result,
2680 carray ** p_env_list)
2687 env_list = carray_new(16);
2690 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2691 cur = clist_next(cur)) {
2692 struct mailimap_msg_att * msg_att;
2693 struct imap_fetch_env_info * env_info;
2694 GSList *tags = NULL;
2696 msg_att = clist_content(cur);
2698 env_info = fetch_to_env_info(msg_att, &tags);
2700 return MAILIMAP_ERROR_MEMORY;
2701 carray_add(env_list, env_info, NULL);
2702 carray_add(env_list, tags, NULL);
2704 * p_env_list = env_list;
2706 * p_env_list = NULL;
2709 return MAIL_NO_ERROR;
2712 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2714 struct mailimap_fetch_att * fetch_att;
2718 struct mailimap_header_list * imap_hdrlist;
2719 struct mailimap_section * section;
2721 hdrlist = clist_new();
2723 header = strdup("Date");
2724 r = clist_append(hdrlist, header);
2725 header = strdup("From");
2726 r = clist_append(hdrlist, header);
2727 header = strdup("To");
2728 r = clist_append(hdrlist, header);
2729 header = strdup("Cc");
2730 r = clist_append(hdrlist, header);
2731 header = strdup("Subject");
2732 r = clist_append(hdrlist, header);
2733 header = strdup("Message-ID");
2734 r = clist_append(hdrlist, header);
2735 header = strdup("References");
2736 r = clist_append(hdrlist, header);
2737 header = strdup("In-Reply-To");
2738 r = clist_append(hdrlist, header);
2740 imap_hdrlist = mailimap_header_list_new(hdrlist);
2741 section = mailimap_section_new_header_fields(imap_hdrlist);
2742 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2743 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2745 return MAIL_NO_ERROR;
2748 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2750 struct mailimap_fetch_att * fetch_att;
2751 struct mailimap_section * section;
2753 section = mailimap_section_new_header();
2754 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2755 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2757 return MAIL_NO_ERROR;
2761 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2762 carray ** p_env_list)
2764 struct mailimap_fetch_att * fetch_att;
2765 struct mailimap_fetch_type * fetch_type;
2767 clist * fetch_result;
2769 carray * env_list = NULL;
2773 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2776 fetch_att = mailimap_fetch_att_new_uid();
2777 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2780 fetch_att = mailimap_fetch_att_new_flags();
2781 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2784 fetch_att = mailimap_fetch_att_new_rfc822_size();
2785 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2789 key.len = sizeof(imap);
2790 r = chash_get(courier_workaround_hash, &key, &value);
2792 r = imap_add_envelope_fetch_att(fetch_type);
2794 r = imap_add_header_fetch_att(fetch_type);
2796 mailstream_logger = imap_logger_fetch;
2798 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2800 mailstream_logger = imap_logger_cmd;
2802 case MAILIMAP_NO_ERROR:
2805 mailimap_fetch_type_free(fetch_type);
2806 debug_print("uid_fetch: %d\n", r);
2810 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2811 res = MAILIMAP_ERROR_FETCH;
2812 debug_print("clist_begin = NULL\n");
2816 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2817 mailimap_fetch_list_free(fetch_result);
2819 if (r != MAILIMAP_NO_ERROR) {
2820 mailimap_fetch_type_free(fetch_type);
2821 res = MAILIMAP_ERROR_MEMORY;
2822 debug_print("fetch_result_to_envelop_list: %d\n", res);
2826 mailimap_fetch_type_free(fetch_type);
2828 * p_env_list = env_list;
2830 return MAILIMAP_NO_ERROR;
2836 struct fetch_env_param {
2838 struct mailimap_set * set;
2841 struct fetch_env_result {
2842 carray * fetch_env_result;
2846 static void fetch_env_run(struct etpan_thread_op * op)
2848 struct fetch_env_param * param;
2849 struct fetch_env_result * result;
2854 result = op->result;
2859 r = imap_get_envelopes_list(param->imap, param->set,
2863 result->fetch_env_result = env_list;
2865 debug_print("imap fetch_env run - end %i\n", r);
2868 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2869 carray ** p_env_list)
2871 struct fetch_env_param param;
2872 struct fetch_env_result result;
2875 debug_print("imap fetch_env - begin\n");
2877 imap = get_imap(folder);
2881 threaded_run(folder, ¶m, &result, fetch_env_run);
2883 if (result.error != MAILIMAP_NO_ERROR) {
2889 key.len = sizeof(imap);
2890 r = chash_get(courier_workaround_hash, &key, &value);
2894 chash_set(courier_workaround_hash, &key, &value, NULL);
2896 threaded_run(folder, ¶m, &result, fetch_env_run);
2900 if (result.error != MAILIMAP_NO_ERROR)
2901 return result.error;
2903 debug_print("imap fetch_env - end\n");
2905 * p_env_list = result.fetch_env_result;
2907 return result.error;
2910 void imap_fetch_env_free(carray * env_list)
2914 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2915 struct imap_fetch_env_info * env_info;
2917 env_info = carray_get(env_list, i);
2918 free(env_info->headers);
2921 carray_free(env_list);
2928 struct append_param {
2930 const char * mailbox;
2931 const char * filename;
2932 struct mailimap_flag_list * flag_list;
2935 struct append_result {
2940 static void append_run(struct etpan_thread_op * op)
2942 struct append_param * param;
2943 struct append_result * result;
2948 struct stat stat_buf;
2951 guint32 uid = 0, val = 0;
2954 result = op->result;
2959 r = stat(param->filename, &stat_buf);
2961 result->error = MAILIMAP_ERROR_APPEND;
2964 size = stat_buf.st_size;
2966 fd = g_open(param->filename, O_RDONLY, 0);
2968 result->error = MAILIMAP_ERROR_APPEND;
2972 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2973 if (data == (void *) MAP_FAILED) {
2975 result->error = MAILIMAP_ERROR_APPEND;
2979 data = file_read_to_str_no_recode(param->filename);
2981 result->error = MAILIMAP_ERROR_APPEND;
2984 size = strlen(data);
2986 mailstream_logger = imap_logger_append;
2988 r = mailimap_uidplus_append(param->imap, param->mailbox,
2989 param->flag_list, NULL,
2990 data, size, &val, &uid);
2992 mailstream_logger = imap_logger_cmd;
3003 debug_print("imap append run - end %i uid %d\n", r, uid);
3006 int imap_threaded_append(Folder * folder, const char * mailbox,
3007 const char * filename,
3008 struct mailimap_flag_list * flag_list,
3011 struct append_param param;
3012 struct append_result result;
3015 debug_print("imap append - begin\n");
3017 imap = get_imap(folder);
3019 param.mailbox = mailbox;
3020 param.filename = filename;
3021 param.flag_list = flag_list;
3023 threaded_run(folder, ¶m, &result, append_run);
3025 if (result.error != MAILIMAP_NO_ERROR)
3026 return result.error;
3028 debug_print("imap append - end\n");
3032 return result.error;
3038 struct expunge_param {
3042 struct expunge_result {
3046 static void expunge_run(struct etpan_thread_op * op)
3048 struct expunge_param * param;
3049 struct expunge_result * result;
3053 result = op->result;
3057 r = mailimap_expunge(param->imap);
3060 debug_print("imap expunge run - end %i\n", r);
3063 int imap_threaded_expunge(Folder * folder)
3065 struct expunge_param param;
3066 struct expunge_result result;
3068 debug_print("imap expunge - begin\n");
3070 param.imap = get_imap(folder);
3072 threaded_run(folder, ¶m, &result, expunge_run);
3074 debug_print("imap expunge - end\n");
3076 return result.error;
3082 struct mailimap_set * set;
3086 struct copy_result {
3088 struct mailimap_set *source;
3089 struct mailimap_set *dest;
3092 static void copy_run(struct etpan_thread_op * op)
3094 struct copy_param * param;
3095 struct copy_result * result;
3098 struct mailimap_set *source = NULL, *dest = NULL;
3101 result = op->result;
3105 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3106 &val, &source, &dest);
3110 result->source = source;
3111 result->dest = dest;
3113 result->source = NULL;
3114 result->dest = NULL;
3116 debug_print("imap copy run - end %i\n", r);
3119 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3120 const char * mb, struct mailimap_set **source,
3121 struct mailimap_set **dest)
3123 struct copy_param param;
3124 struct copy_result result;
3127 debug_print("imap copy - begin\n");
3129 imap = get_imap(folder);
3134 threaded_run(folder, ¶m, &result, copy_run);
3138 if (result.error != MAILIMAP_NO_ERROR)
3139 return result.error;
3141 *source = result.source;
3142 *dest = result.dest;
3144 debug_print("imap copy - end\n");
3146 return result.error;
3151 struct store_param {
3153 struct mailimap_set * set;
3154 struct mailimap_store_att_flags * store_att_flags;
3157 struct store_result {
3161 static void store_run(struct etpan_thread_op * op)
3163 struct store_param * param;
3164 struct store_result * result;
3168 result = op->result;
3172 r = mailimap_uid_store(param->imap, param->set,
3173 param->store_att_flags);
3177 debug_print("imap store run - end %i\n", r);
3180 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3181 struct mailimap_store_att_flags * store_att_flags)
3183 struct store_param param;
3184 struct store_result result;
3187 debug_print("imap store - begin\n");
3189 imap = get_imap(folder);
3192 param.store_att_flags = store_att_flags;
3194 threaded_run(folder, ¶m, &result, store_run);
3196 if (result.error != MAILIMAP_NO_ERROR)
3197 return result.error;
3199 debug_print("imap store - end\n");
3201 return result.error;
3205 #define ENV_BUFFER_SIZE 512
3207 static void do_exec_command(int fd, const char * command,
3208 const char * servername, uint16_t port)
3212 char env_buffer[ENV_BUFFER_SIZE];
3216 /* Fork again to become a child of init rather than
3217 the etpan client. */
3222 g_setenv("ETPANSERVER", servername, TRUE);
3224 g_unsetenv("ETPANSERVER");
3229 snprintf(porttext, sizeof(porttext), "%d", port);
3230 g_setenv("ETPANPORT", porttext, TRUE);
3233 g_unsetenv("ETPANPORT");
3236 /* Not a lot we can do if there's an error other than bail. */
3237 if (dup2(fd, 0) == -1)
3239 if (dup2(fd, 1) == -1)
3242 /* Should we close stderr and reopen /dev/null? */
3244 maxopen = sysconf(_SC_OPEN_MAX);
3245 for (i=3; i < maxopen; i++)
3249 /* Detach from the controlling tty if we have one. Otherwise,
3250 SSH might do something stupid like trying to use it instead
3251 of running $SSH_ASKPASS. Doh. */
3252 fd = g_open("/dev/tty", O_RDONLY, 0);
3254 ioctl(fd, TIOCNOTTY, NULL);
3257 #endif /* TIOCNOTTY */
3259 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3261 /* Eep. Shouldn't reach this */
3265 static int subcommand_connect(const char *command,
3266 const char *servername, uint16_t port)
3268 /* SEB unsupported on Windows */
3272 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3277 do_exec_command(sockfds[1], command, servername, port);
3279 else if (childpid == -1) {
3287 /* Reap child, leaving grandchild process to run */
3288 waitpid(childpid, NULL, 0);
3293 static int socket_connect_cmd(mailimap * imap, const char * command,
3294 const char * server, int port)
3300 fd = subcommand_connect(command, server, port);
3302 return MAILIMAP_ERROR_STREAM;
3304 s = mailstream_socket_open(fd);
3307 return MAILIMAP_ERROR_STREAM;
3310 r = mailimap_connect(imap, s);
3311 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3312 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3313 mailstream_close(s);
3315 imap->imap_stream = NULL;
3324 struct connect_cmd_param {
3326 const char * command;
3327 const char * server;
3331 struct connect_cmd_result {
3335 static void connect_cmd_run(struct etpan_thread_op * op)
3338 struct connect_cmd_param * param;
3339 struct connect_cmd_result * result;
3342 result = op->result;
3346 r = socket_connect_cmd(param->imap, param->command,
3347 param->server, param->port);
3353 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3354 const char * server, int port)
3356 struct connect_cmd_param param;
3357 struct connect_cmd_result result;
3360 mailimap * imap, * oldimap;
3362 oldimap = get_imap(folder);
3364 imap = mailimap_new(0, NULL);
3367 debug_print("deleting old imap %p\n", oldimap);
3368 delete_imap(folder, oldimap);
3372 key.len = sizeof(folder);
3375 chash_set(session_hash, &key, &value, NULL);
3378 param.command = command;
3379 param.server = server;
3382 threaded_run(folder, ¶m, &result, connect_cmd_run);
3384 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3386 return result.error;
3388 #endif /* G_OS_WIN32 */
3390 void imap_threaded_cancel(Folder * folder)
3394 imap = get_imap(folder);
3395 if (imap->imap_stream != NULL)
3396 mailstream_cancel(imap->imap_stream);
3401 void imap_main_init(void)
3404 void imap_main_done(gboolean have_connectivity)
3407 void imap_main_set_timeout(int sec)
3411 void imap_threaded_cancel(Folder * folder);