2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2012 DINH Viet Hoa and the Claws Mail team
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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
29 #include "imap-thread.h"
31 #include <sys/types.h>
33 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
34 #include <sys/socket.h>
43 #include "etpan-thread-manager.h"
45 #include "mainwindow.h"
47 #include "ssl_certificate.h"
49 #include "remotefolder.h"
52 #define DISABLE_LOG_DURING_LOGIN
54 static struct etpan_thread_manager * thread_manager = NULL;
55 static chash * courier_workaround_hash = NULL;
56 static chash * imap_hash = NULL;
57 static chash * session_hash = NULL;
58 static guint thread_manager_signal = 0;
59 static GIOChannel * io_channel = NULL;
61 static void delete_imap(Folder *folder, mailimap *imap)
66 key.len = sizeof(folder);
67 chash_delete(session_hash, &key, NULL);
70 key.len = sizeof(imap);
71 chash_delete(courier_workaround_hash, &key, NULL);
72 if (imap && imap->imap_stream) {
73 /* we don't want libetpan to logout */
74 mailstream_close(imap->imap_stream);
75 imap->imap_stream = NULL;
77 debug_print("removing mailimap %p\n", imap);
81 static gboolean thread_manager_event(GIOChannel * source,
82 GIOCondition condition,
89 if (condition & G_IO_IN)
90 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
92 etpan_thread_manager_loop(thread_manager);
97 static void imap_logger_noop(int direction, const char * str, size_t size)
102 static void imap_logger_cmd(int direction, const char * str, size_t size)
109 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
112 buf = malloc(size+1);
113 memset(buf, 0, size+1);
114 strncpy(buf, str, size);
117 if (!strncmp(buf, "<<<<<<<", 7)
118 || !strncmp(buf, ">>>>>>>", 7)) {
122 while (strstr(buf, "\r"))
123 *strstr(buf, "\r") = ' ';
124 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
125 buf[strlen(buf)-1] = '\0';
127 lines = g_strsplit(buf, "\n", -1);
129 while (lines[i] && *lines[i]) {
130 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
137 static void imap_logger_fetch(int direction, const char * str, size_t size)
143 if (size > 128 && !direction) {
144 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
148 buf = malloc(size+1);
149 memset(buf, 0, size+1);
150 strncpy(buf, str, size);
152 if (!strncmp(buf, "<<<<<<<", 7)
153 || !strncmp(buf, ">>>>>>>", 7)) {
157 while (strstr(buf, "\r"))
158 *strstr(buf, "\r") = ' ';
159 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
160 buf[strlen(buf)-1] = '\0';
162 lines = g_strsplit(buf, "\n", -1);
164 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
165 while (lines[i] && *lines[i]) {
166 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
170 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
176 static void imap_logger_uid(int direction, const char * str, size_t size)
183 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
186 buf = malloc(size+1);
187 memset(buf, 0, size+1);
188 strncpy(buf, str, size);
190 if (!strncmp(buf, "<<<<<<<", 7)
191 || !strncmp(buf, ">>>>>>>", 7)) {
195 while (strstr(buf, "\r"))
196 *strstr(buf, "\r") = ' ';
197 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
198 buf[strlen(buf)-1] = '\0';
200 lines = g_strsplit(buf, "\n", -1);
202 while (lines[i] && *lines[i]) {
203 int llen = strlen(lines[i]);
205 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
208 strncpy2(tmp, lines[i], 63);
209 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
218 static void imap_logger_append(int direction, const char * str, size_t size)
225 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
227 } else if (direction == 0 && size > 64) {
228 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
231 buf = malloc(size+1);
232 memset(buf, 0, size+1);
233 strncpy(buf, str, size);
235 if (!strncmp(buf, "<<<<<<<", 7)
236 || !strncmp(buf, ">>>>>>>", 7)) {
240 while (strstr(buf, "\r"))
241 *strstr(buf, "\r") = ' ';
242 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
243 buf[strlen(buf)-1] = '\0';
245 lines = g_strsplit(buf, "\n", -1);
247 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
248 while (lines[i] && *lines[i]) {
249 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
253 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
259 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
260 gboolean etpan_skip_ssl_cert_check = FALSE;
261 extern void mailsasl_ref(void);
263 void imap_main_init(gboolean skip_ssl_cert_check)
265 int fd_thread_manager;
267 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
268 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
269 mailstream_network_delay.tv_usec = 0;
271 mailstream_debug = 1;
272 mailstream_logger = imap_logger_cmd;
275 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
276 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
277 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
279 thread_manager = etpan_thread_manager_new();
281 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
284 io_channel = g_io_channel_unix_new(fd_thread_manager);
286 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
288 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
289 thread_manager_event,
294 void imap_main_set_timeout(int sec)
296 mailstream_network_delay.tv_sec = sec;
297 mailstream_network_delay.tv_usec = 0;
300 void imap_main_done(gboolean have_connectivity)
302 imap_disconnect_all(have_connectivity);
303 etpan_thread_manager_stop(thread_manager);
304 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
307 etpan_thread_manager_join(thread_manager);
309 g_source_remove(thread_manager_signal);
310 g_io_channel_unref(io_channel);
312 etpan_thread_manager_free(thread_manager);
314 chash_free(courier_workaround_hash);
315 chash_free(session_hash);
316 chash_free(imap_hash);
319 void imap_init(Folder * folder)
321 struct etpan_thread * thread;
325 thread = etpan_thread_manager_get_thread(thread_manager);
328 key.len = sizeof(folder);
332 chash_set(imap_hash, &key, &value, NULL);
335 void imap_done(Folder * folder)
337 struct etpan_thread * thread;
343 key.len = sizeof(folder);
345 r = chash_get(imap_hash, &key, &value);
351 etpan_thread_unbind(thread);
353 chash_delete(imap_hash, &key, NULL);
355 debug_print("remove thread");
358 static struct etpan_thread * get_thread(Folder * folder)
360 struct etpan_thread * thread;
365 key.len = sizeof(folder);
367 chash_get(imap_hash, &key, &value);
373 static mailimap * get_imap(Folder * folder)
381 key.len = sizeof(folder);
383 r = chash_get(session_hash, &key, &value);
388 debug_print("found imap %p\n", imap);
392 static gboolean cb_show_error(gpointer data)
394 mainwindow_show_error();
398 static void generic_cb(int cancelled, void * result, void * callback_data)
400 struct etpan_thread_op * op;
402 op = (struct etpan_thread_op *) callback_data;
404 debug_print("generic_cb\n");
405 if (op->imap && op->imap->imap_response_info &&
406 op->imap->imap_response_info->rsp_alert) {
407 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
408 op->imap->imap_response_info->rsp_alert);
409 g_timeout_add(10, cb_show_error, NULL);
414 static void threaded_run(Folder * folder, void * param, void * result,
415 void (* func)(struct etpan_thread_op * ))
417 struct etpan_thread_op * op;
418 struct etpan_thread * thread;
420 imap_folder_ref(folder);
422 op = etpan_thread_op_new();
424 op->imap = get_imap(folder);
430 op->callback = generic_cb;
431 op->callback_data = op;
436 thread = get_thread(folder);
437 etpan_thread_op_schedule(thread, op);
439 while (!op->finished) {
440 gtk_main_iteration();
443 etpan_thread_op_free(op);
445 imap_folder_unref(folder);
451 struct connect_param {
453 PrefsAccount *account;
458 struct connect_result {
462 #define CHECK_IMAP() { \
463 if (!param->imap) { \
464 result->error = MAILIMAP_ERROR_BAD_STATE; \
469 static void connect_run(struct etpan_thread_op * op)
472 struct connect_param * param;
473 struct connect_result * result;
480 r = mailimap_socket_connect(param->imap,
481 param->server, param->port);
487 int imap_threaded_connect(Folder * folder, const char * server, int port)
489 struct connect_param param;
490 struct connect_result result;
493 mailimap * imap, * oldimap;
495 oldimap = get_imap(folder);
497 imap = mailimap_new(0, NULL);
500 debug_print("deleting old imap %p\n", oldimap);
501 delete_imap(folder, oldimap);
505 key.len = sizeof(folder);
508 chash_set(session_hash, &key, &value, NULL);
511 param.server = server;
515 threaded_run(folder, ¶m, &result, connect_run);
517 debug_print("connect ok %i with imap %p\n", result.error, imap);
522 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
525 struct connect_param *param = (struct connect_param *)data;
526 gnutls_x509_crt_t cert = NULL;
529 if (certificate == NULL || len < 0) {
530 g_warning("no cert presented.\n");
534 tmp.data = malloc(len);
535 memcpy(tmp.data, certificate, len);
537 gnutls_x509_crt_init(&cert);
538 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
540 g_warning("IMAP: can't get cert\n");
542 } else if (ssl_certificate_check(cert, (guint)-1, (gchar *)param->server,
543 (gushort)param->port) == TRUE) {
545 gnutls_x509_crt_deinit(cert);
549 gnutls_x509_crt_deinit(cert);
556 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
559 PrefsAccount *account = (PrefsAccount *)data;
560 const gchar *cert_path = NULL;
561 const gchar *password = NULL;
562 gnutls_x509_crt_t x509 = NULL;
563 gnutls_x509_privkey_t pkey = NULL;
565 if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
566 cert_path = account->in_ssl_client_cert_file;
567 if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
568 password = account->in_ssl_client_cert_pass;
570 if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
571 mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
572 debug_print("Impossible to set the client certificate.\n");
573 x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
574 pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
575 if (!(x509 && pkey)) {
576 /* try pkcs12 format */
577 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
580 unsigned char *x509_der = NULL, *pkey_der = NULL;
581 size_t x509_len, pkey_len;
583 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
584 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
585 if (x509_len > 0 && pkey_len > 0) {
586 if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
587 mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
588 log_error(LOG_PROTOCOL, _("Impossible to set the client certificate.\n"));
592 gnutls_x509_crt_deinit(x509);
593 gnutls_x509_privkey_deinit(pkey);
598 static void connect_ssl_run(struct etpan_thread_op * op)
601 struct connect_param * param;
602 struct connect_result * result;
609 r = mailimap_ssl_connect_with_callback(param->imap,
610 param->server, param->port,
611 connect_ssl_context_cb, param->account);
615 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
617 struct connect_param param;
618 struct connect_result result;
621 mailimap * imap, * oldimap;
622 unsigned char *certificate = NULL;
625 oldimap = get_imap(folder);
627 imap = mailimap_new(0, NULL);
630 debug_print("deleting old imap %p\n", oldimap);
631 delete_imap(folder, oldimap);
635 key.len = sizeof(folder);
638 chash_set(session_hash, &key, &value, NULL);
641 param.server = server;
643 param.account = folder->account;
646 threaded_run(folder, ¶m, &result, connect_ssl_run);
648 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
649 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
650 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
651 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
656 debug_print("connect %d with imap %p\n", result.error, imap);
667 struct mailimap_capability_data *caps;
670 static void capability_run(struct etpan_thread_op * op)
673 struct capa_param * param;
674 struct capa_result * result;
675 struct mailimap_capability_data *caps;
682 r = mailimap_capability(param->imap, &caps);
685 result->caps = (r == 0 ? caps : NULL);
689 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
691 struct capa_param param;
692 struct capa_result result;
695 imap = get_imap(folder);
699 threaded_run(folder, ¶m, &result, capability_run);
701 debug_print("capa %d\n", result.error);
710 struct disconnect_param {
714 struct disconnect_result {
718 static void disconnect_run(struct etpan_thread_op * op)
721 struct disconnect_param * param;
722 struct disconnect_result * result;
729 r = mailimap_logout(param->imap);
734 void imap_threaded_disconnect(Folder * folder)
736 struct connect_param param;
737 struct connect_result result;
740 imap = get_imap(folder);
742 debug_print("was disconnected\n");
748 threaded_run(folder, ¶m, &result, disconnect_run);
750 if (imap == get_imap(folder)) {
751 debug_print("deleting old imap %p\n", imap);
752 delete_imap(folder, imap);
754 debug_print("imap already deleted %p\n", imap);
757 debug_print("disconnect ok\n");
764 const char * wildcard;
773 static void list_run(struct etpan_thread_op * op)
775 struct list_param * param;
776 struct list_result * result;
787 if (param->base == NULL || param->wildcard == NULL) {
790 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
794 r = mailimap_lsub(param->imap, param->base,
795 param->wildcard, &list);
797 r = mailimap_list(param->imap, param->base,
798 param->wildcard, &list);
801 debug_print("imap list run - end\n");
804 int imap_threaded_list(Folder * folder, const char * base,
805 const char * wildcard,
808 struct list_param param;
809 struct list_result result;
811 debug_print("imap list - begin\n");
813 param.imap = get_imap(folder);
815 param.wildcard = wildcard;
816 param.sub_only = FALSE;
818 threaded_run(folder, ¶m, &result, list_run);
820 * p_result = result.list;
822 debug_print("imap list - end %p\n", result.list);
827 int imap_threaded_lsub(Folder * folder, const char * base,
828 const char * wildcard,
831 struct list_param param;
832 struct list_result result;
834 debug_print("imap lsub - begin\n");
836 param.imap = get_imap(folder);
838 param.wildcard = wildcard;
839 param.sub_only = TRUE;
841 threaded_run(folder, ¶m, &result, list_run);
843 * p_result = result.list;
845 debug_print("imap lsub - end %p\n", result.list);
850 struct subscribe_param {
856 struct subscribe_result {
860 static void subscribe_run(struct etpan_thread_op * op)
862 struct subscribe_param * param;
863 struct subscribe_result * result;
871 if (param->mb == NULL) {
873 debug_print("no mb\n");
876 if (param->subscribe)
877 r = mailimap_subscribe(param->imap, param->mb);
879 r = mailimap_unsubscribe(param->imap, param->mb);
881 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
884 int imap_threaded_subscribe(Folder * folder, const char * mb,
887 struct subscribe_param param;
888 struct subscribe_result result;
890 debug_print("imap list - begin\n");
892 param.imap = get_imap(folder);
894 param.subscribe = subscribe;
896 threaded_run(folder, ¶m, &result, subscribe_run);
904 const char * password;
909 struct login_result {
913 static void login_run(struct etpan_thread_op * op)
915 struct login_param * param;
916 struct login_result * result;
918 #ifdef DISABLE_LOG_DURING_LOGIN
927 #ifdef DISABLE_LOG_DURING_LOGIN
928 old_debug = mailstream_debug;
929 mailstream_debug = 0;
931 if (!strcmp(param->type, "LOGIN"))
932 r = mailimap_login(param->imap,
933 param->login, param->password);
934 else if (!strcmp(param->type, "GSSAPI"))
935 r = mailimap_authenticate(param->imap,
936 param->type, param->server, NULL, NULL,
937 param->login, param->login,
938 param->password, NULL);
940 r = mailimap_authenticate(param->imap,
941 param->type, NULL, NULL, NULL,
942 param->login, param->login,
943 param->password, NULL);
944 #ifdef DISABLE_LOG_DURING_LOGIN
945 mailstream_debug = old_debug;
949 if (param->imap->imap_response)
950 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
951 debug_print("imap login run - end %i\n", r);
954 int imap_threaded_login(Folder * folder,
955 const char * login, const char * password,
958 struct login_param param;
959 struct login_result result;
961 debug_print("imap login - begin\n");
963 param.imap = get_imap(folder);
965 param.password = password;
967 if (folder && folder->account)
968 param.server = folder->account->recv_server;
972 threaded_run(folder, ¶m, &result, login_run);
974 debug_print("imap login - end\n");
980 struct status_param {
983 struct mailimap_status_att_list * status_att_list;
986 struct status_result {
988 struct mailimap_mailbox_data_status * data_status;
991 static void status_run(struct etpan_thread_op * op)
993 struct status_param * param;
994 struct status_result * result;
1002 r = mailimap_status(param->imap, param->mb,
1003 param->status_att_list,
1004 &result->data_status);
1007 debug_print("imap status run - end %i\n", r);
1010 int imap_threaded_status(Folder * folder, const char * mb,
1011 struct mailimap_mailbox_data_status ** data_status,
1014 struct status_param param;
1015 struct status_result result;
1016 struct mailimap_status_att_list * status_att_list;
1018 debug_print("imap status - begin\n");
1020 status_att_list = mailimap_status_att_list_new_empty();
1021 if (mask & 1 << 0) {
1022 mailimap_status_att_list_add(status_att_list,
1023 MAILIMAP_STATUS_ATT_MESSAGES);
1025 if (mask & 1 << 1) {
1026 mailimap_status_att_list_add(status_att_list,
1027 MAILIMAP_STATUS_ATT_RECENT);
1029 if (mask & 1 << 2) {
1030 mailimap_status_att_list_add(status_att_list,
1031 MAILIMAP_STATUS_ATT_UIDNEXT);
1033 if (mask & 1 << 3) {
1034 mailimap_status_att_list_add(status_att_list,
1035 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1037 if (mask & 1 << 4) {
1038 mailimap_status_att_list_add(status_att_list,
1039 MAILIMAP_STATUS_ATT_UNSEEN);
1041 param.imap = get_imap(folder);
1043 param.status_att_list = status_att_list;
1045 threaded_run(folder, ¶m, &result, status_run);
1047 debug_print("imap status - end\n");
1049 * data_status = result.data_status;
1051 mailimap_status_att_list_free(status_att_list);
1053 return result.error;
1062 struct noop_result {
1066 static void noop_run(struct etpan_thread_op * op)
1068 struct noop_param * param;
1069 struct noop_result * result;
1073 result = op->result;
1077 r = mailimap_noop(param->imap);
1080 debug_print("imap noop run - end %i\n", r);
1083 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1084 unsigned int *p_recent,
1085 unsigned int *p_expunge,
1086 unsigned int *p_unseen,
1087 unsigned int *p_uidnext,
1088 unsigned int *p_uidval)
1090 struct noop_param param;
1091 struct noop_result result;
1094 debug_print("imap noop - begin\n");
1096 imap = get_imap(folder);
1099 threaded_run(folder, ¶m, &result, noop_run);
1101 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1102 * p_exists = imap->imap_selection_info->sel_exists;
1103 * p_recent = imap->imap_selection_info->sel_recent;
1104 * p_unseen = imap->imap_selection_info->sel_unseen;
1105 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1106 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1114 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1115 imap->imap_response_info->rsp_expunged != NULL) {
1116 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1120 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1121 *p_exists, *p_recent, *p_expunge, *p_unseen,
1122 *p_uidnext, *p_uidval);
1124 return result.error;
1128 struct starttls_result {
1132 static void starttls_run(struct etpan_thread_op * op)
1134 struct connect_param * param;
1135 struct starttls_result * result;
1139 result = op->result;
1143 r = mailimap_starttls(param->imap);
1146 debug_print("imap starttls run - end %i\n", r);
1149 mailimap *imap = param->imap;
1150 mailstream_low *plain_low = NULL;
1151 mailstream_low *tls_low = NULL;
1154 plain_low = mailstream_get_low(imap->imap_stream);
1155 fd = mailstream_low_get_fd(plain_low);
1157 debug_print("imap starttls run - can't get fd\n");
1158 result->error = MAILIMAP_ERROR_STREAM;
1162 tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
1163 if (tls_low == NULL) {
1164 debug_print("imap starttls run - can't tls_open\n");
1165 result->error = MAILIMAP_ERROR_STREAM;
1168 mailstream_low_free(plain_low);
1169 mailstream_set_low(imap->imap_stream, tls_low);
1173 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1175 struct connect_param param;
1176 struct starttls_result result;
1178 unsigned char *certificate = NULL;
1180 debug_print("imap starttls - begin\n");
1182 param.imap = get_imap(folder);
1183 param.server = host;
1185 param.account = folder->account;
1187 threaded_run(folder, ¶m, &result, starttls_run);
1189 debug_print("imap starttls - end\n");
1191 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1192 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1193 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1194 result.error = MAILIMAP_ERROR_STREAM;
1198 return result.error;
1203 struct create_param {
1208 struct create_result {
1212 static void create_run(struct etpan_thread_op * op)
1214 struct create_param * param;
1215 struct create_result * result;
1219 result = op->result;
1223 r = mailimap_create(param->imap, param->mb);
1226 debug_print("imap create run - end %i\n", r);
1229 int imap_threaded_create(Folder * folder, const char * mb)
1231 struct create_param param;
1232 struct create_result result;
1234 debug_print("imap create - begin\n");
1236 param.imap = get_imap(folder);
1239 threaded_run(folder, ¶m, &result, create_run);
1241 debug_print("imap create - end\n");
1243 return result.error;
1249 struct rename_param {
1252 const char * new_name;
1255 struct rename_result {
1259 static void rename_run(struct etpan_thread_op * op)
1261 struct rename_param * param;
1262 struct rename_result * result;
1266 result = op->result;
1270 r = mailimap_rename(param->imap, param->mb, param->new_name);
1273 debug_print("imap rename run - end %i\n", r);
1276 int imap_threaded_rename(Folder * folder,
1277 const char * mb, const char * new_name)
1279 struct rename_param param;
1280 struct rename_result result;
1282 debug_print("imap rename - begin\n");
1284 param.imap = get_imap(folder);
1286 param.new_name = new_name;
1288 threaded_run(folder, ¶m, &result, rename_run);
1290 debug_print("imap rename - end\n");
1292 return result.error;
1298 struct delete_param {
1303 struct delete_result {
1307 static void delete_run(struct etpan_thread_op * op)
1309 struct delete_param * param;
1310 struct delete_result * result;
1314 result = op->result;
1318 r = mailimap_delete(param->imap, param->mb);
1321 debug_print("imap delete run - end %i\n", r);
1324 int imap_threaded_delete(Folder * folder, const char * mb)
1326 struct delete_param param;
1327 struct delete_result result;
1329 debug_print("imap delete - begin\n");
1331 param.imap = get_imap(folder);
1334 threaded_run(folder, ¶m, &result, delete_run);
1336 debug_print("imap delete - end\n");
1338 return result.error;
1343 struct select_param {
1348 struct select_result {
1352 static void select_run(struct etpan_thread_op * op)
1354 struct select_param * param;
1355 struct select_result * result;
1359 result = op->result;
1363 r = mailimap_select(param->imap, param->mb);
1366 debug_print("imap select run - end %i\n", r);
1369 int imap_threaded_select(Folder * folder, const char * mb,
1370 gint * exists, gint * recent, gint * unseen,
1371 guint32 * uid_validity,gint *can_create_flags,
1374 struct select_param param;
1375 struct select_result result;
1378 debug_print("imap select - begin\n");
1380 imap = get_imap(folder);
1384 threaded_run(folder, ¶m, &result, select_run);
1386 if (result.error != MAILIMAP_NO_ERROR)
1387 return result.error;
1389 if (!imap || imap->imap_selection_info == NULL)
1390 return MAILIMAP_ERROR_PARSE;
1392 * exists = imap->imap_selection_info->sel_exists;
1393 * recent = imap->imap_selection_info->sel_recent;
1394 * unseen = imap->imap_selection_info->sel_unseen;
1395 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1396 * can_create_flags = FALSE;
1398 if (imap->imap_selection_info->sel_perm_flags) {
1399 GSList *t_flags = NULL;
1400 clistiter *cur = NULL;
1401 if (imap->imap_selection_info->sel_perm_flags)
1402 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1404 for (; cur; cur = clist_next(cur)) {
1405 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1406 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1407 *can_create_flags = TRUE;
1408 else if (flag->fl_flag &&
1409 flag->fl_flag->fl_type == 6 &&
1410 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1411 *can_create_flags = TRUE;
1412 if (flag->fl_flag && ok_flags) {
1413 MsgPermFlags c_flag = 0;
1414 switch (flag->fl_flag->fl_type) {
1415 case MAILIMAP_FLAG_ANSWERED:
1416 c_flag = IMAP_FLAG_ANSWERED;
1418 case MAILIMAP_FLAG_FLAGGED:
1419 c_flag = IMAP_FLAG_FLAGGED;
1421 case MAILIMAP_FLAG_DELETED:
1422 c_flag = IMAP_FLAG_DELETED;
1424 case MAILIMAP_FLAG_DRAFT:
1425 c_flag = IMAP_FLAG_DRAFT;
1427 case MAILIMAP_FLAG_SEEN:
1428 c_flag = IMAP_FLAG_SEEN;
1430 case MAILIMAP_FLAG_KEYWORD:
1431 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1432 c_flag = IMAP_FLAG_FORWARDED;
1433 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1434 c_flag = IMAP_FLAG_SPAM;
1435 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1436 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1437 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1438 c_flag = IMAP_FLAG_HAM;
1444 t_flags = g_slist_prepend(t_flags,
1445 GUINT_TO_POINTER(c_flag));
1450 *ok_flags = t_flags;
1452 debug_print("imap select - end\n");
1454 return result.error;
1457 static void close_run(struct etpan_thread_op * op)
1459 struct select_param * param;
1460 struct select_result * result;
1464 result = op->result;
1468 r = mailimap_close(param->imap);
1471 debug_print("imap close run - end %i\n", r);
1474 int imap_threaded_close(Folder * folder)
1476 struct select_param param;
1477 struct select_result result;
1480 debug_print("imap close - begin\n");
1482 imap = get_imap(folder);
1485 threaded_run(folder, ¶m, &result, close_run);
1487 if (result.error != MAILIMAP_NO_ERROR)
1488 return result.error;
1490 debug_print("imap close - end\n");
1492 return result.error;
1495 struct examine_param {
1500 struct examine_result {
1504 static void examine_run(struct etpan_thread_op * op)
1506 struct examine_param * param;
1507 struct examine_result * result;
1511 result = op->result;
1515 r = mailimap_examine(param->imap, param->mb);
1518 debug_print("imap examine run - end %i\n", r);
1521 int imap_threaded_examine(Folder * folder, const char * mb,
1522 gint * exists, gint * recent, gint * unseen,
1523 guint32 * uid_validity)
1525 struct examine_param param;
1526 struct examine_result result;
1529 debug_print("imap examine - begin\n");
1531 imap = get_imap(folder);
1535 threaded_run(folder, ¶m, &result, examine_run);
1537 if (result.error != MAILIMAP_NO_ERROR)
1538 return result.error;
1540 if (!imap || imap->imap_selection_info == NULL)
1541 return MAILIMAP_ERROR_PARSE;
1543 * exists = imap->imap_selection_info->sel_exists;
1544 * recent = imap->imap_selection_info->sel_recent;
1545 * unseen = imap->imap_selection_info->sel_unseen;
1546 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1548 debug_print("imap examine - end\n");
1550 return result.error;
1556 struct search_param {
1559 const char *charset;
1560 struct mailimap_set * set;
1564 struct search_result {
1566 clist * search_result;
1569 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1571 return mailimap_set_item_new(orig->set_first, orig->set_last);
1574 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1576 clist *list = orig ? orig->set_list : NULL;
1577 clist *newlist = clist_new();
1582 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1583 clist_append(newlist,
1584 sc_mailimap_set_item_copy(
1585 (struct mailimap_set_item *)clist_content(cur)));
1586 return mailimap_set_new(newlist);
1589 static void search_run(struct etpan_thread_op * op)
1591 struct search_param * param;
1592 struct search_result * result;
1594 struct mailimap_search_key * key = NULL;
1595 struct mailimap_search_key * uid_key = NULL;
1596 struct mailimap_search_key * search_type_key = NULL;
1597 clist * search_result;
1600 result = op->result;
1604 /* we copy the mailimap_set because freeing the key is recursive */
1605 if (param->set != NULL) {
1606 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1607 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1608 uid_key = mailimap_search_key_new_all();
1610 switch (param->type) {
1611 case IMAP_SEARCH_TYPE_SIMPLE:
1612 search_type_key = NULL;
1614 case IMAP_SEARCH_TYPE_SEEN:
1615 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1617 case IMAP_SEARCH_TYPE_UNSEEN:
1618 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1620 case IMAP_SEARCH_TYPE_ANSWERED:
1621 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1623 case IMAP_SEARCH_TYPE_FLAGGED:
1624 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1626 case IMAP_SEARCH_TYPE_DELETED:
1627 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1629 case IMAP_SEARCH_TYPE_FORWARDED:
1630 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1632 case IMAP_SEARCH_TYPE_SPAM:
1633 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1635 case IMAP_SEARCH_TYPE_KEYED:
1636 search_type_key = param->key;
1640 if (search_type_key != NULL) {
1641 if (param->set != NULL) {
1642 key = mailimap_search_key_new_multiple_empty();
1643 mailimap_search_key_multiple_add(key, search_type_key);
1644 mailimap_search_key_multiple_add(key, uid_key);
1646 key = search_type_key;
1648 } else if (uid_key != NULL) {
1653 g_warning("no key!");
1654 result = op->result;
1656 result->search_result = NULL;
1658 mailstream_logger = imap_logger_uid;
1660 r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
1662 mailstream_logger = imap_logger_cmd;
1664 /* free the key (with the imapset) */
1665 mailimap_search_key_free(key);
1668 result->search_result = search_result;
1670 debug_print("imap search run - end %i\n", result->error);
1673 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1674 const char *charset, struct mailimap_set * set,
1675 clist ** search_result)
1677 struct search_param param;
1678 struct search_result result;
1681 debug_print("imap search - begin\n");
1683 imap = get_imap(folder);
1686 param.charset = charset;
1687 param.type = search_type;
1690 threaded_run(folder, ¶m, &result, search_run);
1692 if (result.error != MAILIMAP_NO_ERROR)
1693 return result.error;
1695 debug_print("imap search - end\n");
1697 * search_result = result.search_result;
1699 return result.error;
1703 struct _IMAPSearchKey {
1704 struct mailimap_search_key* key;
1707 IMAPSearchKey* imap_search_new(gint criteria,
1708 const gchar *header,
1712 char* sk_bcc = NULL;
1713 struct mailimap_date* sk_before = NULL;
1714 char* sk_body = NULL;
1716 char* sk_from = NULL;
1717 char* sk_keyword = NULL;
1718 struct mailimap_date* sk_on = NULL;
1719 struct mailimap_date* sk_since = NULL;
1720 char* sk_subject = NULL;
1721 char* sk_text = NULL;
1723 char* sk_unkeyword = NULL;
1724 char* sk_header_name = NULL;
1725 char* sk_header_value = NULL;
1726 uint32_t sk_larger = 0;
1727 struct mailimap_search_key* sk_not = NULL;
1728 struct mailimap_search_key* sk_or1 = NULL;
1729 struct mailimap_search_key* sk_or2 = NULL;
1730 struct mailimap_date* sk_sentbefore = NULL;
1731 struct mailimap_date* sk_senton = NULL;
1732 struct mailimap_date* sk_sentsince = NULL;
1733 uint32_t sk_smaller = 0;
1734 struct mailimap_set* sk_uid = NULL;
1735 struct mailimap_set* sk_set = NULL;
1736 clist* sk_multiple = NULL;
1737 int etpan_matcher_type;
1740 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1741 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1742 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1743 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1744 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1745 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1746 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1748 case IMAP_SEARCH_CRITERIA_TAG:
1749 sk_keyword = strdup(expr);
1750 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1753 case IMAP_SEARCH_CRITERIA_SUBJECT:
1754 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1755 sk_subject = strdup(expr);
1758 case IMAP_SEARCH_CRITERIA_TO:
1759 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1760 sk_to = strdup(expr);
1763 case IMAP_SEARCH_CRITERIA_CC:
1764 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1765 sk_cc = strdup(expr);
1768 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1769 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1772 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1775 localtime_r(&limit, &tm);
1776 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1777 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1778 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1780 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1781 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1786 case IMAP_SEARCH_CRITERIA_BODY:
1787 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1788 sk_body = strdup(expr);
1791 case IMAP_SEARCH_CRITERIA_MESSAGE:
1792 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1793 sk_text = strdup(expr);
1796 case IMAP_SEARCH_CRITERIA_HEADER:
1797 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1798 sk_header_name = strdup(header);
1799 sk_header_value = strdup(expr);
1802 case IMAP_SEARCH_CRITERIA_FROM:
1803 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1804 sk_from = strdup(expr);
1807 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1808 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1812 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1813 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1821 return mailimap_search_key_new(etpan_matcher_type,
1822 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1823 sk_on, sk_since, sk_subject, sk_text, sk_to,
1824 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1825 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1826 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1829 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1831 return mailimap_search_key_new_not(key);
1834 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1836 return mailimap_search_key_new_or(l, r);
1839 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1841 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1842 mailimap_search_key_multiple_add(result, l);
1843 mailimap_search_key_multiple_add(result, r);
1848 void imap_search_free(IMAPSearchKey* key)
1853 mailimap_search_key_free(key);
1858 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1862 struct mailimap_msg_att_dynamic ** patt_dyn);
1865 result_to_uid_list(clist * fetch_result, carray ** result)
1867 clistiter * cur = NULL;
1872 tab = carray_new(128);
1874 res = MAILIMAP_ERROR_MEMORY;
1879 cur = clist_begin(fetch_result);
1881 for(; cur != NULL ; cur = clist_next(cur)) {
1882 struct mailimap_msg_att * msg_att;
1886 msg_att = clist_content(cur);
1889 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1891 puid = malloc(sizeof(* puid));
1893 res = MAILIMAP_ERROR_MEMORY;
1898 r = carray_add(tab, puid, NULL);
1901 res = MAILIMAP_ERROR_MEMORY;
1908 return MAILIMAP_NO_ERROR;
1911 imap_fetch_uid_list_free(tab);
1916 static int imap_get_messages_list(mailimap * imap,
1917 uint32_t first_index,
1922 struct mailimap_fetch_att * fetch_att;
1923 struct mailimap_fetch_type * fetch_type;
1924 struct mailimap_set * set;
1925 clist * fetch_result;
1928 set = mailimap_set_new_interval(first_index, 0);
1930 res = MAILIMAP_ERROR_MEMORY;
1934 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1935 if (fetch_type == NULL) {
1936 res = MAILIMAP_ERROR_MEMORY;
1940 fetch_att = mailimap_fetch_att_new_uid();
1941 if (fetch_att == NULL) {
1942 res = MAILIMAP_ERROR_MEMORY;
1943 goto free_fetch_type;
1946 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1947 if (r != MAILIMAP_NO_ERROR) {
1948 mailimap_fetch_att_free(fetch_att);
1949 res = MAILIMAP_ERROR_MEMORY;
1950 goto free_fetch_type;
1953 mailstream_logger = imap_logger_fetch;
1955 r = mailimap_uid_fetch(imap, set,
1956 fetch_type, &fetch_result);
1958 mailstream_logger = imap_logger_cmd;
1959 mailimap_fetch_type_free(fetch_type);
1960 mailimap_set_free(set);
1962 if (r != MAILIMAP_NO_ERROR) {
1968 r = result_to_uid_list(fetch_result, &env_list);
1969 mailimap_fetch_list_free(fetch_result);
1971 * result = env_list;
1973 return MAILIMAP_NO_ERROR;
1976 mailimap_fetch_type_free(fetch_type);
1978 mailimap_set_free(set);
1986 struct fetch_uid_param {
1988 uint32_t first_index;
1991 struct fetch_uid_result {
1993 carray * fetch_result;
1996 static void fetch_uid_run(struct etpan_thread_op * op)
1998 struct fetch_uid_param * param;
1999 struct fetch_uid_result * result;
2000 carray * fetch_result;
2004 result = op->result;
2008 fetch_result = NULL;
2009 mailstream_logger = imap_logger_noop;
2010 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
2012 r = imap_get_messages_list(param->imap, param->first_index,
2015 mailstream_logger = imap_logger_cmd;
2018 result->fetch_result = fetch_result;
2019 debug_print("imap fetch_uid run - end %i\n", r);
2022 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
2023 carray ** fetch_result)
2025 struct fetch_uid_param param;
2026 struct fetch_uid_result result;
2029 debug_print("imap fetch_uid - begin\n");
2031 imap = get_imap(folder);
2033 param.first_index = first_index;
2035 threaded_run(folder, ¶m, &result, fetch_uid_run);
2037 if (result.error != MAILIMAP_NO_ERROR)
2038 return result.error;
2040 debug_print("imap fetch_uid - end\n");
2042 * fetch_result = result.fetch_result;
2044 return result.error;
2048 void imap_fetch_uid_list_free(carray * uid_list)
2052 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
2055 puid = carray_get(uid_list, i);
2058 carray_free(uid_list);
2064 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
2067 result_to_uid_flags_list(clist * fetch_result, carray ** result)
2069 clistiter * cur = NULL;
2073 GSList *tags = NULL;
2075 tab = carray_new(128);
2077 res = MAILIMAP_ERROR_MEMORY;
2082 cur = clist_begin(fetch_result);
2084 for(; cur != NULL ; cur = clist_next(cur)) {
2085 struct mailimap_msg_att * msg_att;
2088 struct mailimap_msg_att_dynamic * att_dyn;
2094 msg_att = clist_content(cur);
2098 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2101 if (att_dyn == NULL)
2104 flags = imap_flags_to_flags(att_dyn, &tags);
2106 puid = malloc(sizeof(* puid));
2108 res = MAILIMAP_ERROR_MEMORY;
2113 r = carray_add(tab, puid, NULL);
2116 res = MAILIMAP_ERROR_MEMORY;
2119 pflags = malloc(sizeof(* pflags));
2120 if (pflags == NULL) {
2121 res = MAILIMAP_ERROR_MEMORY;
2125 r = carray_add(tab, pflags, NULL);
2128 res = MAILIMAP_ERROR_MEMORY;
2131 r = carray_add(tab, tags, NULL);
2134 res = MAILIMAP_ERROR_MEMORY;
2141 return MAILIMAP_NO_ERROR;
2144 imap_fetch_uid_flags_list_free(tab);
2145 slist_free_strings_full(tags);
2150 static int imap_get_messages_flags_list(mailimap * imap,
2151 uint32_t first_index,
2156 struct mailimap_fetch_att * fetch_att;
2157 struct mailimap_fetch_type * fetch_type;
2158 struct mailimap_set * set;
2159 clist * fetch_result;
2162 set = mailimap_set_new_interval(first_index, 0);
2164 res = MAILIMAP_ERROR_MEMORY;
2168 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2169 if (fetch_type == NULL) {
2170 res = MAILIMAP_ERROR_MEMORY;
2174 fetch_att = mailimap_fetch_att_new_flags();
2175 if (fetch_att == NULL) {
2176 res = MAILIMAP_ERROR_MEMORY;
2177 goto free_fetch_type;
2180 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2181 if (r != MAILIMAP_NO_ERROR) {
2182 mailimap_fetch_att_free(fetch_att);
2183 res = MAILIMAP_ERROR_MEMORY;
2184 goto free_fetch_type;
2187 fetch_att = mailimap_fetch_att_new_uid();
2188 if (fetch_att == NULL) {
2189 res = MAILIMAP_ERROR_MEMORY;
2190 goto free_fetch_type;
2193 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2194 if (r != MAILIMAP_NO_ERROR) {
2195 mailimap_fetch_att_free(fetch_att);
2196 res = MAILIMAP_ERROR_MEMORY;
2197 goto free_fetch_type;
2200 mailstream_logger = imap_logger_fetch;
2202 r = mailimap_uid_fetch(imap, set,
2203 fetch_type, &fetch_result);
2205 mailstream_logger = imap_logger_cmd;
2206 mailimap_fetch_type_free(fetch_type);
2207 mailimap_set_free(set);
2209 if (r != MAILIMAP_NO_ERROR) {
2215 r = result_to_uid_flags_list(fetch_result, &env_list);
2216 mailimap_fetch_list_free(fetch_result);
2218 * result = env_list;
2220 return MAILIMAP_NO_ERROR;
2223 mailimap_fetch_type_free(fetch_type);
2225 mailimap_set_free(set);
2232 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2234 struct fetch_uid_param * param;
2235 struct fetch_uid_result * result;
2236 carray * fetch_result;
2240 result = op->result;
2244 fetch_result = NULL;
2245 r = imap_get_messages_flags_list(param->imap, param->first_index,
2249 result->fetch_result = fetch_result;
2250 debug_print("imap fetch_uid run - end %i\n", r);
2253 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2254 carray ** fetch_result)
2256 struct fetch_uid_param param;
2257 struct fetch_uid_result result;
2260 debug_print("imap fetch_uid - begin\n");
2262 imap = get_imap(folder);
2264 param.first_index = first_index;
2266 mailstream_logger = imap_logger_noop;
2267 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2269 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2271 mailstream_logger = imap_logger_cmd;
2274 if (result.error != MAILIMAP_NO_ERROR)
2275 return result.error;
2277 debug_print("imap fetch_uid - end\n");
2279 * fetch_result = result.fetch_result;
2281 return result.error;
2285 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2289 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2292 data = carray_get(uid_flags_list, i);
2294 data = carray_get(uid_flags_list, i + 1);
2297 carray_free(uid_flags_list);
2302 static int imap_fetch(mailimap * imap,
2305 size_t * result_len)
2308 struct mailimap_set * set;
2309 struct mailimap_fetch_att * fetch_att;
2310 struct mailimap_fetch_type * fetch_type;
2311 clist * fetch_result;
2312 struct mailimap_msg_att * msg_att;
2313 struct mailimap_msg_att_item * msg_att_item;
2318 struct mailimap_section * section;
2320 set = mailimap_set_new_single(msg_index);
2322 res = MAILIMAP_ERROR_MEMORY;
2326 section = mailimap_section_new(NULL);
2327 if (section == NULL) {
2328 res = MAILIMAP_ERROR_MEMORY;
2332 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2333 if (fetch_att == NULL) {
2334 mailimap_section_free(section);
2335 res = MAILIMAP_ERROR_MEMORY;
2339 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2340 if (fetch_type == NULL) {
2341 res = MAILIMAP_ERROR_MEMORY;
2342 goto free_fetch_att;
2345 mailstream_logger = imap_logger_fetch;
2347 r = mailimap_uid_fetch(imap, set,
2348 fetch_type, &fetch_result);
2350 mailstream_logger = imap_logger_cmd;
2352 mailimap_fetch_type_free(fetch_type);
2353 mailimap_set_free(set);
2356 case MAILIMAP_NO_ERROR:
2362 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2363 mailimap_fetch_list_free(fetch_result);
2364 return MAILIMAP_ERROR_FETCH;
2367 msg_att = clist_begin(fetch_result)->data;
2372 if (msg_att->att_list)
2373 cur = clist_begin(msg_att->att_list);
2377 for(; cur != NULL ; cur = clist_next(cur)) {
2378 msg_att_item = clist_content(cur);
2380 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2381 if (msg_att_item->att_data.att_static->att_type ==
2382 MAILIMAP_MSG_ATT_BODY_SECTION) {
2383 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2385 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2387 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2392 mailimap_fetch_list_free(fetch_result);
2395 return MAILIMAP_ERROR_FETCH;
2398 * result_len = text_length;
2400 return MAILIMAP_NO_ERROR;
2403 mailimap_fetch_att_free(fetch_att);
2405 mailimap_set_free(set);
2410 static int imap_fetch_header(mailimap * imap,
2413 size_t * result_len)
2416 struct mailimap_set * set;
2417 struct mailimap_fetch_att * fetch_att;
2418 struct mailimap_fetch_type * fetch_type;
2419 clist * fetch_result;
2420 struct mailimap_msg_att * msg_att;
2421 struct mailimap_msg_att_item * msg_att_item;
2426 struct mailimap_section * section;
2428 set = mailimap_set_new_single(msg_index);
2430 res = MAILIMAP_ERROR_MEMORY;
2434 section = mailimap_section_new_header();
2435 if (section == NULL) {
2436 res = MAILIMAP_ERROR_MEMORY;
2440 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2441 if (fetch_att == NULL) {
2442 mailimap_section_free(section);
2443 res = MAILIMAP_ERROR_MEMORY;
2447 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2448 if (fetch_type == NULL) {
2449 res = MAILIMAP_ERROR_MEMORY;
2450 goto free_fetch_att;
2453 mailstream_logger = imap_logger_fetch;
2455 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2457 mailstream_logger = imap_logger_cmd;
2458 mailimap_fetch_type_free(fetch_type);
2459 mailimap_set_free(set);
2462 case MAILIMAP_NO_ERROR:
2468 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2469 mailimap_fetch_list_free(fetch_result);
2470 return MAILIMAP_ERROR_FETCH;
2473 msg_att = clist_begin(fetch_result)->data;
2478 if (msg_att->att_list)
2479 cur = clist_begin(msg_att->att_list);
2483 for(; cur != NULL ; cur = clist_next(cur)) {
2484 msg_att_item = clist_content(cur);
2486 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2487 if (msg_att_item->att_data.att_static->att_type ==
2488 MAILIMAP_MSG_ATT_BODY_SECTION) {
2489 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2490 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2492 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2497 mailimap_fetch_list_free(fetch_result);
2500 return MAILIMAP_ERROR_FETCH;
2503 * result_len = text_length;
2505 return MAILIMAP_NO_ERROR;
2508 mailimap_fetch_att_free(fetch_att);
2510 mailimap_set_free(set);
2517 struct fetch_content_param {
2520 const char * filename;
2524 struct fetch_content_result {
2528 static void fetch_content_run(struct etpan_thread_op * op)
2530 struct fetch_content_param * param;
2531 struct fetch_content_result * result;
2533 size_t content_size;
2539 result = op->result;
2545 if (param->with_body)
2546 r = imap_fetch(param->imap, param->msg_index,
2547 &content, &content_size);
2549 r = imap_fetch_header(param->imap, param->msg_index,
2550 &content, &content_size);
2554 if (r == MAILIMAP_NO_ERROR) {
2555 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2557 result->error = MAILIMAP_ERROR_FETCH;
2561 f = fdopen(fd, "wb");
2563 result->error = MAILIMAP_ERROR_FETCH;
2567 r = fwrite(content, 1, content_size, f);
2568 if (r < content_size) {
2569 result->error = MAILIMAP_ERROR_FETCH;
2575 result->error = MAILIMAP_ERROR_FETCH;
2586 claws_unlink(param->filename);
2589 /* mmap_string_unref is a simple free in libetpan
2590 * when it has MMAP_UNAVAILABLE defined */
2591 if (mmap_string_unref(content) != 0)
2595 debug_print("imap fetch_content run - end %i\n", result->error);
2598 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2600 const char * filename)
2602 struct fetch_content_param param;
2603 struct fetch_content_result result;
2606 debug_print("imap fetch_content - begin\n");
2608 imap = get_imap(folder);
2610 param.msg_index = msg_index;
2611 param.filename = filename;
2612 param.with_body = with_body;
2614 threaded_run(folder, ¶m, &result, fetch_content_run);
2616 if (result.error != MAILIMAP_NO_ERROR)
2617 return result.error;
2619 debug_print("imap fetch_content - end\n");
2621 return result.error;
2626 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2631 GSList *tags = NULL;
2635 flag_list = att_dyn->att_list;
2636 if (flag_list == NULL)
2639 for(cur = clist_begin(flag_list) ; cur != NULL ;
2640 cur = clist_next(cur)) {
2641 struct mailimap_flag_fetch * flag_fetch;
2643 flag_fetch = clist_content(cur);
2644 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2647 switch (flag_fetch->fl_flag->fl_type) {
2648 case MAILIMAP_FLAG_ANSWERED:
2649 flags |= MSG_REPLIED;
2651 case MAILIMAP_FLAG_FLAGGED:
2652 flags |= MSG_MARKED;
2654 case MAILIMAP_FLAG_DELETED:
2655 flags |= MSG_DELETED;
2657 case MAILIMAP_FLAG_SEEN:
2658 flags &= ~MSG_UNREAD;
2661 case MAILIMAP_FLAG_KEYWORD:
2662 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2663 flags |= MSG_FORWARDED;
2664 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2666 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2667 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2668 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2671 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2681 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2685 struct mailimap_msg_att_dynamic ** patt_dyn)
2687 clistiter * item_cur;
2691 struct mailimap_msg_att_dynamic * att_dyn;
2698 if (msg_att->att_list)
2699 item_cur = clist_begin(msg_att->att_list);
2702 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2703 struct mailimap_msg_att_item * item;
2705 item = clist_content(item_cur);
2707 switch (item->att_type) {
2708 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2709 switch (item->att_data.att_static->att_type) {
2710 case MAILIMAP_MSG_ATT_UID:
2711 uid = item->att_data.att_static->att_data.att_uid;
2714 case MAILIMAP_MSG_ATT_BODY_SECTION:
2715 if (headers == NULL) {
2716 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2719 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2720 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2725 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2726 if (att_dyn == NULL) {
2727 att_dyn = item->att_data.att_dyn;
2735 if (pheaders != NULL)
2736 * pheaders = headers;
2737 if (pref_size != NULL)
2738 * pref_size = ref_size;
2739 if (patt_dyn != NULL)
2740 * patt_dyn = att_dyn;
2742 return MAIL_NO_ERROR;
2745 static struct imap_fetch_env_info *
2746 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2748 struct imap_fetch_env_info * info;
2752 struct mailimap_msg_att_dynamic * att_dyn;
2754 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2759 info = malloc(sizeof(* info));
2761 info->headers = strdup(headers);
2763 info->flags = imap_flags_to_flags(att_dyn, tags);
2769 imap_fetch_result_to_envelop_list(clist * fetch_result,
2770 carray ** p_env_list)
2775 env_list = carray_new(16);
2778 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2779 cur = clist_next(cur)) {
2780 struct mailimap_msg_att * msg_att;
2781 struct imap_fetch_env_info * env_info;
2782 GSList *tags = NULL;
2784 msg_att = clist_content(cur);
2786 env_info = fetch_to_env_info(msg_att, &tags);
2788 return MAILIMAP_ERROR_MEMORY;
2789 carray_add(env_list, env_info, NULL);
2790 carray_add(env_list, tags, NULL);
2792 * p_env_list = env_list;
2794 * p_env_list = NULL;
2797 return MAIL_NO_ERROR;
2800 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2802 struct mailimap_fetch_att * fetch_att;
2806 struct mailimap_header_list * imap_hdrlist;
2807 struct mailimap_section * section;
2809 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2810 "References", "In-Reply-To", NULL
2813 hdrlist = clist_new();
2815 while (headers[i] != NULL) {
2816 header = strdup(headers[i]);
2817 if (header == NULL || clist_append(hdrlist, header) != 0)
2818 return MAIL_ERROR_MEMORY;
2822 imap_hdrlist = mailimap_header_list_new(hdrlist);
2823 section = mailimap_section_new_header_fields(imap_hdrlist);
2824 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2825 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2827 return MAIL_NO_ERROR;
2830 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2832 struct mailimap_fetch_att * fetch_att;
2833 struct mailimap_section * section;
2835 section = mailimap_section_new_header();
2836 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2837 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2839 return MAIL_NO_ERROR;
2843 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2844 carray ** p_env_list)
2846 struct mailimap_fetch_att * fetch_att;
2847 struct mailimap_fetch_type * fetch_type;
2849 clist * fetch_result;
2851 carray * env_list = NULL;
2855 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2858 fetch_att = mailimap_fetch_att_new_uid();
2859 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2862 fetch_att = mailimap_fetch_att_new_flags();
2863 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2866 fetch_att = mailimap_fetch_att_new_rfc822_size();
2867 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2871 key.len = sizeof(imap);
2872 r = chash_get(courier_workaround_hash, &key, &value);
2874 r = imap_add_envelope_fetch_att(fetch_type);
2876 r = imap_add_header_fetch_att(fetch_type);
2878 mailstream_logger = imap_logger_fetch;
2880 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2882 mailstream_logger = imap_logger_cmd;
2884 case MAILIMAP_NO_ERROR:
2887 mailimap_fetch_type_free(fetch_type);
2888 debug_print("uid_fetch: %d\n", r);
2892 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2893 res = MAILIMAP_ERROR_FETCH;
2894 debug_print("clist_begin = NULL\n");
2898 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2899 mailimap_fetch_list_free(fetch_result);
2901 if (r != MAILIMAP_NO_ERROR) {
2902 mailimap_fetch_type_free(fetch_type);
2903 res = MAILIMAP_ERROR_MEMORY;
2904 debug_print("fetch_result_to_envelop_list: %d\n", res);
2908 mailimap_fetch_type_free(fetch_type);
2910 * p_env_list = env_list;
2912 return MAILIMAP_NO_ERROR;
2918 struct fetch_env_param {
2920 struct mailimap_set * set;
2923 struct fetch_env_result {
2924 carray * fetch_env_result;
2928 static void fetch_env_run(struct etpan_thread_op * op)
2930 struct fetch_env_param * param;
2931 struct fetch_env_result * result;
2936 result = op->result;
2941 r = imap_get_envelopes_list(param->imap, param->set,
2945 result->fetch_env_result = env_list;
2947 debug_print("imap fetch_env run - end %i\n", r);
2950 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2951 carray ** p_env_list)
2953 struct fetch_env_param param;
2954 struct fetch_env_result result;
2957 debug_print("imap fetch_env - begin\n");
2959 imap = get_imap(folder);
2963 threaded_run(folder, ¶m, &result, fetch_env_run);
2965 if (result.error != MAILIMAP_NO_ERROR) {
2971 key.len = sizeof(imap);
2972 r = chash_get(courier_workaround_hash, &key, &value);
2976 chash_set(courier_workaround_hash, &key, &value, NULL);
2978 threaded_run(folder, ¶m, &result, fetch_env_run);
2982 if (result.error != MAILIMAP_NO_ERROR)
2983 return result.error;
2985 debug_print("imap fetch_env - end\n");
2987 * p_env_list = result.fetch_env_result;
2989 return result.error;
2992 void imap_fetch_env_free(carray * env_list)
2996 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2997 struct imap_fetch_env_info * env_info;
2999 env_info = carray_get(env_list, i);
3000 free(env_info->headers);
3003 carray_free(env_list);
3010 struct append_param {
3012 const char * mailbox;
3013 const char * filename;
3014 struct mailimap_flag_list * flag_list;
3017 struct append_result {
3022 static void append_run(struct etpan_thread_op * op)
3024 struct append_param * param;
3025 struct append_result * result;
3030 struct stat stat_buf;
3033 guint32 uid = 0, val = 0;
3036 result = op->result;
3041 r = stat(param->filename, &stat_buf);
3043 result->error = MAILIMAP_ERROR_APPEND;
3046 size = stat_buf.st_size;
3048 fd = g_open(param->filename, O_RDONLY, 0);
3050 result->error = MAILIMAP_ERROR_APPEND;
3054 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
3055 if (data == (void *) MAP_FAILED) {
3057 result->error = MAILIMAP_ERROR_APPEND;
3061 data = file_read_to_str_no_recode(param->filename);
3063 result->error = MAILIMAP_ERROR_APPEND;
3066 size = strlen(data);
3068 mailstream_logger = imap_logger_append;
3070 r = mailimap_uidplus_append(param->imap, param->mailbox,
3071 param->flag_list, NULL,
3072 data, size, &val, &uid);
3074 mailstream_logger = imap_logger_cmd;
3085 debug_print("imap append run - end %i uid %d\n", r, uid);
3088 int imap_threaded_append(Folder * folder, const char * mailbox,
3089 const char * filename,
3090 struct mailimap_flag_list * flag_list,
3093 struct append_param param;
3094 struct append_result result;
3097 debug_print("imap append - begin\n");
3099 imap = get_imap(folder);
3101 param.mailbox = mailbox;
3102 param.filename = filename;
3103 param.flag_list = flag_list;
3105 threaded_run(folder, ¶m, &result, append_run);
3107 if (result.error != MAILIMAP_NO_ERROR)
3108 return result.error;
3110 debug_print("imap append - end\n");
3114 return result.error;
3120 struct expunge_param {
3124 struct expunge_result {
3128 static void expunge_run(struct etpan_thread_op * op)
3130 struct expunge_param * param;
3131 struct expunge_result * result;
3135 result = op->result;
3139 r = mailimap_expunge(param->imap);
3142 debug_print("imap expunge run - end %i\n", r);
3145 int imap_threaded_expunge(Folder * folder)
3147 struct expunge_param param;
3148 struct expunge_result result;
3150 debug_print("imap expunge - begin\n");
3152 param.imap = get_imap(folder);
3154 threaded_run(folder, ¶m, &result, expunge_run);
3156 debug_print("imap expunge - end\n");
3158 return result.error;
3164 struct mailimap_set * set;
3168 struct copy_result {
3170 struct mailimap_set *source;
3171 struct mailimap_set *dest;
3174 static void copy_run(struct etpan_thread_op * op)
3176 struct copy_param * param;
3177 struct copy_result * result;
3180 struct mailimap_set *source = NULL, *dest = NULL;
3183 result = op->result;
3187 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3188 &val, &source, &dest);
3192 result->source = source;
3193 result->dest = dest;
3195 result->source = NULL;
3196 result->dest = NULL;
3198 debug_print("imap copy run - end %i\n", r);
3201 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3202 const char * mb, struct mailimap_set **source,
3203 struct mailimap_set **dest)
3205 struct copy_param param;
3206 struct copy_result result;
3209 debug_print("imap copy - begin\n");
3211 imap = get_imap(folder);
3216 threaded_run(folder, ¶m, &result, copy_run);
3220 if (result.error != MAILIMAP_NO_ERROR)
3221 return result.error;
3223 *source = result.source;
3224 *dest = result.dest;
3226 debug_print("imap copy - end\n");
3228 return result.error;
3233 struct store_param {
3235 struct mailimap_set * set;
3236 struct mailimap_store_att_flags * store_att_flags;
3239 struct store_result {
3243 static void store_run(struct etpan_thread_op * op)
3245 struct store_param * param;
3246 struct store_result * result;
3250 result = op->result;
3254 r = mailimap_uid_store(param->imap, param->set,
3255 param->store_att_flags);
3259 debug_print("imap store run - end %i\n", r);
3262 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3263 struct mailimap_store_att_flags * store_att_flags)
3265 struct store_param param;
3266 struct store_result result;
3269 debug_print("imap store - begin\n");
3271 imap = get_imap(folder);
3274 param.store_att_flags = store_att_flags;
3276 threaded_run(folder, ¶m, &result, store_run);
3278 if (result.error != MAILIMAP_NO_ERROR)
3279 return result.error;
3281 debug_print("imap store - end\n");
3283 return result.error;
3287 #define ENV_BUFFER_SIZE 512
3289 static void do_exec_command(int fd, const char * command,
3290 const char * servername, uint16_t port)
3294 char env_buffer[ENV_BUFFER_SIZE];
3298 /* Fork again to become a child of init rather than
3299 the etpan client. */
3304 g_setenv("ETPANSERVER", servername, TRUE);
3306 g_unsetenv("ETPANSERVER");
3311 snprintf(porttext, sizeof(porttext), "%d", port);
3312 g_setenv("ETPANPORT", porttext, TRUE);
3315 g_unsetenv("ETPANPORT");
3318 /* Not a lot we can do if there's an error other than bail. */
3319 if (dup2(fd, 0) == -1)
3321 if (dup2(fd, 1) == -1)
3324 /* Should we close stderr and reopen /dev/null? */
3326 maxopen = sysconf(_SC_OPEN_MAX);
3327 for (i=3; i < maxopen; i++)
3331 /* Detach from the controlling tty if we have one. Otherwise,
3332 SSH might do something stupid like trying to use it instead
3333 of running $SSH_ASKPASS. Doh. */
3334 fd = g_open("/dev/tty", O_RDONLY, 0);
3336 ioctl(fd, TIOCNOTTY, NULL);
3339 #endif /* TIOCNOTTY */
3341 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3343 /* Eep. Shouldn't reach this */
3347 static int subcommand_connect(const char *command,
3348 const char *servername, uint16_t port)
3350 /* SEB unsupported on Windows */
3354 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3359 do_exec_command(sockfds[1], command, servername, port);
3361 else if (childpid == -1) {
3369 /* Reap child, leaving grandchild process to run */
3370 waitpid(childpid, NULL, 0);
3375 static int socket_connect_cmd(mailimap * imap, const char * command,
3376 const char * server, int port)
3382 fd = subcommand_connect(command, server, port);
3384 return MAILIMAP_ERROR_STREAM;
3386 s = mailstream_socket_open(fd);
3389 return MAILIMAP_ERROR_STREAM;
3392 r = mailimap_connect(imap, s);
3393 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3394 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3395 mailstream_close(s);
3397 imap->imap_stream = NULL;
3406 struct connect_cmd_param {
3408 const char * command;
3409 const char * server;
3413 struct connect_cmd_result {
3417 static void connect_cmd_run(struct etpan_thread_op * op)
3420 struct connect_cmd_param * param;
3421 struct connect_cmd_result * result;
3424 result = op->result;
3428 r = socket_connect_cmd(param->imap, param->command,
3429 param->server, param->port);
3435 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3436 const char * server, int port)
3438 struct connect_cmd_param param;
3439 struct connect_cmd_result result;
3442 mailimap * imap, * oldimap;
3444 oldimap = get_imap(folder);
3446 imap = mailimap_new(0, NULL);
3449 debug_print("deleting old imap %p\n", oldimap);
3450 delete_imap(folder, oldimap);
3454 key.len = sizeof(folder);
3457 chash_set(session_hash, &key, &value, NULL);
3460 param.command = command;
3461 param.server = server;
3464 threaded_run(folder, ¶m, &result, connect_cmd_run);
3466 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3468 return result.error;
3470 #endif /* G_OS_WIN32 */
3472 void imap_threaded_cancel(Folder * folder)
3476 imap = get_imap(folder);
3477 if (imap->imap_stream != NULL)
3478 mailstream_cancel(imap->imap_stream);
3483 void imap_main_init(void)
3486 void imap_main_done(gboolean have_connectivity)
3489 void imap_main_set_timeout(int sec)
3493 void imap_threaded_cancel(Folder * folder);