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 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) {
539 g_warning("IMAP: can't get cert\n");
541 } else if (ssl_certificate_check(cert, (guint)-1, (gchar *)param->server,
542 (gushort)param->port) == TRUE) {
543 gnutls_x509_crt_deinit(cert);
546 gnutls_x509_crt_deinit(cert);
553 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
556 PrefsAccount *account = (PrefsAccount *)data;
557 const gchar *cert_path = NULL;
558 const gchar *password = NULL;
559 gnutls_x509_crt x509 = NULL;
560 gnutls_x509_privkey pkey = NULL;
562 if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
563 cert_path = account->in_ssl_client_cert_file;
564 if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
565 password = account->in_ssl_client_cert_pass;
567 if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
568 mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
569 debug_print("Impossible to set the client certificate.\n");
570 x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
571 pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
572 if (!(x509 && pkey)) {
573 /* try pkcs12 format */
574 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
577 unsigned char *x509_der = NULL, *pkey_der = NULL;
578 size_t x509_len, pkey_len;
580 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
581 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
582 if (x509_len > 0 && pkey_len > 0) {
583 if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
584 mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
585 log_error(LOG_PROTOCOL, _("Impossible to set the client certificate.\n"));
589 gnutls_x509_crt_deinit(x509);
590 gnutls_x509_privkey_deinit(pkey);
595 static void connect_ssl_run(struct etpan_thread_op * op)
598 struct connect_param * param;
599 struct connect_result * result;
606 r = mailimap_ssl_connect_with_callback(param->imap,
607 param->server, param->port,
608 connect_ssl_context_cb, param->account);
612 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
614 struct connect_param param;
615 struct connect_result result;
618 mailimap * imap, * oldimap;
619 unsigned char *certificate = NULL;
622 oldimap = get_imap(folder);
624 imap = mailimap_new(0, NULL);
627 debug_print("deleting old imap %p\n", oldimap);
628 delete_imap(folder, oldimap);
632 key.len = sizeof(folder);
635 chash_set(session_hash, &key, &value, NULL);
638 param.server = server;
640 param.account = folder->account;
643 threaded_run(folder, ¶m, &result, connect_ssl_run);
645 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
646 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
647 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
648 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
653 debug_print("connect %d with imap %p\n", result.error, imap);
664 struct mailimap_capability_data *caps;
667 static void capability_run(struct etpan_thread_op * op)
670 struct capa_param * param;
671 struct capa_result * result;
672 struct mailimap_capability_data *caps;
679 r = mailimap_capability(param->imap, &caps);
682 result->caps = (r == 0 ? caps : NULL);
686 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
688 struct capa_param param;
689 struct capa_result result;
692 imap = get_imap(folder);
696 threaded_run(folder, ¶m, &result, capability_run);
698 debug_print("capa %d\n", result.error);
707 struct disconnect_param {
711 struct disconnect_result {
715 static void disconnect_run(struct etpan_thread_op * op)
718 struct disconnect_param * param;
719 struct disconnect_result * result;
726 r = mailimap_logout(param->imap);
731 void imap_threaded_disconnect(Folder * folder)
733 struct connect_param param;
734 struct connect_result result;
737 imap = get_imap(folder);
739 debug_print("was disconnected\n");
745 threaded_run(folder, ¶m, &result, disconnect_run);
747 if (imap == get_imap(folder)) {
748 debug_print("deleting old imap %p\n", imap);
749 delete_imap(folder, imap);
751 debug_print("imap already deleted %p\n", imap);
754 debug_print("disconnect ok\n");
761 const char * wildcard;
770 static void list_run(struct etpan_thread_op * op)
772 struct list_param * param;
773 struct list_result * result;
784 if (param->base == NULL || param->wildcard == NULL) {
787 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
791 r = mailimap_lsub(param->imap, param->base,
792 param->wildcard, &list);
794 r = mailimap_list(param->imap, param->base,
795 param->wildcard, &list);
798 debug_print("imap list run - end\n");
801 int imap_threaded_list(Folder * folder, const char * base,
802 const char * wildcard,
805 struct list_param param;
806 struct list_result result;
808 debug_print("imap list - begin\n");
810 param.imap = get_imap(folder);
812 param.wildcard = wildcard;
813 param.sub_only = FALSE;
815 threaded_run(folder, ¶m, &result, list_run);
817 * p_result = result.list;
819 debug_print("imap list - end %p\n", result.list);
824 int imap_threaded_lsub(Folder * folder, const char * base,
825 const char * wildcard,
828 struct list_param param;
829 struct list_result result;
831 debug_print("imap lsub - begin\n");
833 param.imap = get_imap(folder);
835 param.wildcard = wildcard;
836 param.sub_only = TRUE;
838 threaded_run(folder, ¶m, &result, list_run);
840 * p_result = result.list;
842 debug_print("imap lsub - end %p\n", result.list);
847 struct subscribe_param {
853 struct subscribe_result {
857 static void subscribe_run(struct etpan_thread_op * op)
859 struct subscribe_param * param;
860 struct subscribe_result * result;
868 if (param->mb == NULL) {
870 debug_print("no mb\n");
873 if (param->subscribe)
874 r = mailimap_subscribe(param->imap, param->mb);
876 r = mailimap_unsubscribe(param->imap, param->mb);
878 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
881 int imap_threaded_subscribe(Folder * folder, const char * mb,
884 struct subscribe_param param;
885 struct subscribe_result result;
887 debug_print("imap list - begin\n");
889 param.imap = get_imap(folder);
891 param.subscribe = subscribe;
893 threaded_run(folder, ¶m, &result, subscribe_run);
901 const char * password;
906 struct login_result {
910 static void login_run(struct etpan_thread_op * op)
912 struct login_param * param;
913 struct login_result * result;
915 #ifdef DISABLE_LOG_DURING_LOGIN
924 #ifdef DISABLE_LOG_DURING_LOGIN
925 old_debug = mailstream_debug;
926 mailstream_debug = 0;
928 if (!strcmp(param->type, "LOGIN"))
929 r = mailimap_login(param->imap,
930 param->login, param->password);
931 else if (!strcmp(param->type, "GSSAPI"))
932 r = mailimap_authenticate(param->imap,
933 param->type, param->server, NULL, NULL,
934 param->login, param->login,
935 param->password, NULL);
937 r = mailimap_authenticate(param->imap,
938 param->type, NULL, NULL, NULL,
939 param->login, param->login,
940 param->password, NULL);
941 #ifdef DISABLE_LOG_DURING_LOGIN
942 mailstream_debug = old_debug;
946 if (param->imap->imap_response)
947 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
948 debug_print("imap login run - end %i\n", r);
951 int imap_threaded_login(Folder * folder,
952 const char * login, const char * password,
955 struct login_param param;
956 struct login_result result;
958 debug_print("imap login - begin\n");
960 param.imap = get_imap(folder);
962 param.password = password;
964 if (folder && folder->account)
965 param.server = folder->account->recv_server;
969 threaded_run(folder, ¶m, &result, login_run);
971 debug_print("imap login - end\n");
977 struct status_param {
980 struct mailimap_status_att_list * status_att_list;
983 struct status_result {
985 struct mailimap_mailbox_data_status * data_status;
988 static void status_run(struct etpan_thread_op * op)
990 struct status_param * param;
991 struct status_result * result;
999 r = mailimap_status(param->imap, param->mb,
1000 param->status_att_list,
1001 &result->data_status);
1004 debug_print("imap status run - end %i\n", r);
1007 int imap_threaded_status(Folder * folder, const char * mb,
1008 struct mailimap_mailbox_data_status ** data_status,
1011 struct status_param param;
1012 struct status_result result;
1013 struct mailimap_status_att_list * status_att_list;
1015 debug_print("imap status - begin\n");
1017 status_att_list = mailimap_status_att_list_new_empty();
1018 if (mask & 1 << 0) {
1019 mailimap_status_att_list_add(status_att_list,
1020 MAILIMAP_STATUS_ATT_MESSAGES);
1022 if (mask & 1 << 1) {
1023 mailimap_status_att_list_add(status_att_list,
1024 MAILIMAP_STATUS_ATT_RECENT);
1026 if (mask & 1 << 2) {
1027 mailimap_status_att_list_add(status_att_list,
1028 MAILIMAP_STATUS_ATT_UIDNEXT);
1030 if (mask & 1 << 3) {
1031 mailimap_status_att_list_add(status_att_list,
1032 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1034 if (mask & 1 << 4) {
1035 mailimap_status_att_list_add(status_att_list,
1036 MAILIMAP_STATUS_ATT_UNSEEN);
1038 param.imap = get_imap(folder);
1040 param.status_att_list = status_att_list;
1042 threaded_run(folder, ¶m, &result, status_run);
1044 debug_print("imap status - end\n");
1046 * data_status = result.data_status;
1048 mailimap_status_att_list_free(status_att_list);
1050 return result.error;
1059 struct noop_result {
1063 static void noop_run(struct etpan_thread_op * op)
1065 struct noop_param * param;
1066 struct noop_result * result;
1070 result = op->result;
1074 r = mailimap_noop(param->imap);
1077 debug_print("imap noop run - end %i\n", r);
1080 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1081 unsigned int *p_recent,
1082 unsigned int *p_expunge,
1083 unsigned int *p_unseen,
1084 unsigned int *p_uidnext,
1085 unsigned int *p_uidval)
1087 struct noop_param param;
1088 struct noop_result result;
1091 debug_print("imap noop - begin\n");
1093 imap = get_imap(folder);
1096 threaded_run(folder, ¶m, &result, noop_run);
1098 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1099 * p_exists = imap->imap_selection_info->sel_exists;
1100 * p_recent = imap->imap_selection_info->sel_recent;
1101 * p_unseen = imap->imap_selection_info->sel_unseen;
1102 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1103 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1111 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1112 imap->imap_response_info->rsp_expunged != NULL) {
1113 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1117 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1118 *p_exists, *p_recent, *p_expunge, *p_unseen,
1119 *p_uidnext, *p_uidval);
1121 return result.error;
1125 struct starttls_result {
1129 static void starttls_run(struct etpan_thread_op * op)
1131 struct connect_param * param;
1132 struct starttls_result * result;
1136 result = op->result;
1140 r = mailimap_starttls(param->imap);
1143 debug_print("imap starttls run - end %i\n", r);
1146 mailimap *imap = param->imap;
1147 mailstream_low *plain_low = NULL;
1148 mailstream_low *tls_low = NULL;
1151 plain_low = mailstream_get_low(imap->imap_stream);
1152 fd = mailstream_low_get_fd(plain_low);
1154 debug_print("imap starttls run - can't get fd\n");
1155 result->error = MAILIMAP_ERROR_STREAM;
1159 tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
1160 if (tls_low == NULL) {
1161 debug_print("imap starttls run - can't tls_open\n");
1162 result->error = MAILIMAP_ERROR_STREAM;
1165 mailstream_low_free(plain_low);
1166 mailstream_set_low(imap->imap_stream, tls_low);
1170 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1172 struct connect_param param;
1173 struct starttls_result result;
1175 unsigned char *certificate = NULL;
1177 debug_print("imap starttls - begin\n");
1179 param.imap = get_imap(folder);
1180 param.server = host;
1182 param.account = folder->account;
1184 threaded_run(folder, ¶m, &result, starttls_run);
1186 debug_print("imap starttls - end\n");
1188 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1189 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1190 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1191 result.error = MAILIMAP_ERROR_STREAM;
1195 return result.error;
1200 struct create_param {
1205 struct create_result {
1209 static void create_run(struct etpan_thread_op * op)
1211 struct create_param * param;
1212 struct create_result * result;
1216 result = op->result;
1220 r = mailimap_create(param->imap, param->mb);
1223 debug_print("imap create run - end %i\n", r);
1226 int imap_threaded_create(Folder * folder, const char * mb)
1228 struct create_param param;
1229 struct create_result result;
1231 debug_print("imap create - begin\n");
1233 param.imap = get_imap(folder);
1236 threaded_run(folder, ¶m, &result, create_run);
1238 debug_print("imap create - end\n");
1240 return result.error;
1246 struct rename_param {
1249 const char * new_name;
1252 struct rename_result {
1256 static void rename_run(struct etpan_thread_op * op)
1258 struct rename_param * param;
1259 struct rename_result * result;
1263 result = op->result;
1267 r = mailimap_rename(param->imap, param->mb, param->new_name);
1270 debug_print("imap rename run - end %i\n", r);
1273 int imap_threaded_rename(Folder * folder,
1274 const char * mb, const char * new_name)
1276 struct rename_param param;
1277 struct rename_result result;
1279 debug_print("imap rename - begin\n");
1281 param.imap = get_imap(folder);
1283 param.new_name = new_name;
1285 threaded_run(folder, ¶m, &result, rename_run);
1287 debug_print("imap rename - end\n");
1289 return result.error;
1295 struct delete_param {
1300 struct delete_result {
1304 static void delete_run(struct etpan_thread_op * op)
1306 struct delete_param * param;
1307 struct delete_result * result;
1311 result = op->result;
1315 r = mailimap_delete(param->imap, param->mb);
1318 debug_print("imap delete run - end %i\n", r);
1321 int imap_threaded_delete(Folder * folder, const char * mb)
1323 struct delete_param param;
1324 struct delete_result result;
1326 debug_print("imap delete - begin\n");
1328 param.imap = get_imap(folder);
1331 threaded_run(folder, ¶m, &result, delete_run);
1333 debug_print("imap delete - end\n");
1335 return result.error;
1340 struct select_param {
1345 struct select_result {
1349 static void select_run(struct etpan_thread_op * op)
1351 struct select_param * param;
1352 struct select_result * result;
1356 result = op->result;
1360 r = mailimap_select(param->imap, param->mb);
1363 debug_print("imap select run - end %i\n", r);
1366 int imap_threaded_select(Folder * folder, const char * mb,
1367 gint * exists, gint * recent, gint * unseen,
1368 guint32 * uid_validity,gint *can_create_flags,
1371 struct select_param param;
1372 struct select_result result;
1375 debug_print("imap select - begin\n");
1377 imap = get_imap(folder);
1381 threaded_run(folder, ¶m, &result, select_run);
1383 if (result.error != MAILIMAP_NO_ERROR)
1384 return result.error;
1386 if (!imap || imap->imap_selection_info == NULL)
1387 return MAILIMAP_ERROR_PARSE;
1389 * exists = imap->imap_selection_info->sel_exists;
1390 * recent = imap->imap_selection_info->sel_recent;
1391 * unseen = imap->imap_selection_info->sel_unseen;
1392 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1393 * can_create_flags = FALSE;
1395 if (imap->imap_selection_info->sel_perm_flags) {
1396 GSList *t_flags = NULL;
1397 clistiter *cur = NULL;
1398 if (imap->imap_selection_info->sel_perm_flags)
1399 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1401 for (; cur; cur = clist_next(cur)) {
1402 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1403 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1404 *can_create_flags = TRUE;
1405 else if (flag->fl_flag &&
1406 flag->fl_flag->fl_type == 6 &&
1407 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1408 *can_create_flags = TRUE;
1409 if (flag->fl_flag && ok_flags) {
1410 MsgPermFlags c_flag = 0;
1411 switch (flag->fl_flag->fl_type) {
1412 case MAILIMAP_FLAG_ANSWERED:
1413 c_flag = IMAP_FLAG_ANSWERED;
1415 case MAILIMAP_FLAG_FLAGGED:
1416 c_flag = IMAP_FLAG_FLAGGED;
1418 case MAILIMAP_FLAG_DELETED:
1419 c_flag = IMAP_FLAG_DELETED;
1421 case MAILIMAP_FLAG_DRAFT:
1422 c_flag = IMAP_FLAG_DRAFT;
1424 case MAILIMAP_FLAG_SEEN:
1425 c_flag = IMAP_FLAG_SEEN;
1427 case MAILIMAP_FLAG_KEYWORD:
1428 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1429 c_flag = IMAP_FLAG_FORWARDED;
1430 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1431 c_flag = IMAP_FLAG_SPAM;
1432 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1433 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1434 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1435 c_flag = IMAP_FLAG_HAM;
1441 t_flags = g_slist_prepend(t_flags,
1442 GUINT_TO_POINTER(c_flag));
1447 *ok_flags = t_flags;
1449 debug_print("imap select - end\n");
1451 return result.error;
1454 static void close_run(struct etpan_thread_op * op)
1456 struct select_param * param;
1457 struct select_result * result;
1461 result = op->result;
1465 r = mailimap_close(param->imap);
1468 debug_print("imap close run - end %i\n", r);
1471 int imap_threaded_close(Folder * folder)
1473 struct select_param param;
1474 struct select_result result;
1477 debug_print("imap close - begin\n");
1479 imap = get_imap(folder);
1482 threaded_run(folder, ¶m, &result, close_run);
1484 if (result.error != MAILIMAP_NO_ERROR)
1485 return result.error;
1487 debug_print("imap close - end\n");
1489 return result.error;
1492 struct examine_param {
1497 struct examine_result {
1501 static void examine_run(struct etpan_thread_op * op)
1503 struct examine_param * param;
1504 struct examine_result * result;
1508 result = op->result;
1512 r = mailimap_examine(param->imap, param->mb);
1515 debug_print("imap examine run - end %i\n", r);
1518 int imap_threaded_examine(Folder * folder, const char * mb,
1519 gint * exists, gint * recent, gint * unseen,
1520 guint32 * uid_validity)
1522 struct examine_param param;
1523 struct examine_result result;
1526 debug_print("imap examine - begin\n");
1528 imap = get_imap(folder);
1532 threaded_run(folder, ¶m, &result, examine_run);
1534 if (result.error != MAILIMAP_NO_ERROR)
1535 return result.error;
1537 if (!imap || imap->imap_selection_info == NULL)
1538 return MAILIMAP_ERROR_PARSE;
1540 * exists = imap->imap_selection_info->sel_exists;
1541 * recent = imap->imap_selection_info->sel_recent;
1542 * unseen = imap->imap_selection_info->sel_unseen;
1543 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1545 debug_print("imap examine - end\n");
1547 return result.error;
1553 struct search_param {
1556 struct mailimap_set * set;
1560 struct search_result {
1562 clist * search_result;
1565 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1567 return mailimap_set_item_new(orig->set_first, orig->set_last);
1570 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1572 clist *list = orig ? orig->set_list : NULL;
1573 clist *newlist = clist_new();
1578 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1579 clist_append(newlist,
1580 sc_mailimap_set_item_copy(
1581 (struct mailimap_set_item *)clist_content(cur)));
1582 return mailimap_set_new(newlist);
1585 static void search_run(struct etpan_thread_op * op)
1587 struct search_param * param;
1588 struct search_result * result;
1590 struct mailimap_search_key * key = NULL;
1591 struct mailimap_search_key * uid_key = NULL;
1592 struct mailimap_search_key * search_type_key = NULL;
1593 clist * search_result;
1594 static const char *charset = NULL;
1597 result = op->result;
1601 /* we copy the mailimap_set because freeing the key is recursive */
1602 if (param->set != NULL) {
1603 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1604 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1605 uid_key = mailimap_search_key_new_all();
1607 switch (param->type) {
1608 case IMAP_SEARCH_TYPE_SIMPLE:
1609 search_type_key = NULL;
1611 case IMAP_SEARCH_TYPE_SEEN:
1612 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1614 case IMAP_SEARCH_TYPE_UNSEEN:
1615 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1617 case IMAP_SEARCH_TYPE_ANSWERED:
1618 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1620 case IMAP_SEARCH_TYPE_FLAGGED:
1621 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1623 case IMAP_SEARCH_TYPE_DELETED:
1624 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1626 case IMAP_SEARCH_TYPE_FORWARDED:
1627 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1629 case IMAP_SEARCH_TYPE_SPAM:
1630 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1632 case IMAP_SEARCH_TYPE_KEYED:
1634 search_type_key = param->key;
1638 if (search_type_key != NULL) {
1639 if (param->set != NULL) {
1640 key = mailimap_search_key_new_multiple_empty();
1641 mailimap_search_key_multiple_add(key, search_type_key);
1642 mailimap_search_key_multiple_add(key, uid_key);
1644 key = search_type_key;
1646 } else if (uid_key != NULL) {
1651 g_warning("no key!");
1652 result = op->result;
1654 result->search_result = NULL;
1656 mailstream_logger = imap_logger_uid;
1658 r = mailimap_uid_search(param->imap, charset, key, &search_result);
1660 mailstream_logger = imap_logger_cmd;
1662 /* free the key (with the imapset) */
1663 mailimap_search_key_free(key);
1666 result->search_result = search_result;
1668 debug_print("imap search run - end %i\n", result->error);
1671 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1672 struct mailimap_set * set, clist ** search_result)
1674 struct search_param param;
1675 struct search_result result;
1678 debug_print("imap search - begin\n");
1680 imap = get_imap(folder);
1683 param.type = search_type;
1686 threaded_run(folder, ¶m, &result, search_run);
1688 if (result.error != MAILIMAP_NO_ERROR)
1689 return result.error;
1691 debug_print("imap search - end\n");
1693 * search_result = result.search_result;
1695 return result.error;
1699 struct _IMAPSearchKey {
1700 struct mailimap_search_key* key;
1703 IMAPSearchKey* imap_search_new(gint criteria,
1704 const gchar *header,
1708 char* sk_bcc = NULL;
1709 struct mailimap_date* sk_before = NULL;
1710 char* sk_body = NULL;
1712 char* sk_from = NULL;
1713 char* sk_keyword = NULL;
1714 struct mailimap_date* sk_on = NULL;
1715 struct mailimap_date* sk_since = NULL;
1716 char* sk_subject = NULL;
1717 char* sk_text = NULL;
1719 char* sk_unkeyword = NULL;
1720 char* sk_header_name = NULL;
1721 char* sk_header_value = NULL;
1722 uint32_t sk_larger = 0;
1723 struct mailimap_search_key* sk_not = NULL;
1724 struct mailimap_search_key* sk_or1 = NULL;
1725 struct mailimap_search_key* sk_or2 = NULL;
1726 struct mailimap_date* sk_sentbefore = NULL;
1727 struct mailimap_date* sk_senton = NULL;
1728 struct mailimap_date* sk_sentsince = NULL;
1729 uint32_t sk_smaller = 0;
1730 struct mailimap_set* sk_uid = NULL;
1731 struct mailimap_set* sk_set = NULL;
1732 clist* sk_multiple = NULL;
1733 int etpan_matcher_type;
1736 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1737 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1738 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1739 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1740 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1741 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1742 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1744 case IMAP_SEARCH_CRITERIA_TAG:
1745 sk_keyword = strdup(expr);
1746 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1749 case IMAP_SEARCH_CRITERIA_SUBJECT:
1750 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1751 sk_subject = strdup(expr);
1754 case IMAP_SEARCH_CRITERIA_TO:
1755 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1756 sk_to = strdup(expr);
1759 case IMAP_SEARCH_CRITERIA_CC:
1760 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1761 sk_cc = strdup(expr);
1764 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1765 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1768 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1771 localtime_r(&limit, &tm);
1772 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1773 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1774 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1776 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1777 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1782 case IMAP_SEARCH_CRITERIA_BODY:
1783 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1784 sk_body = strdup(expr);
1787 case IMAP_SEARCH_CRITERIA_MESSAGE:
1788 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1789 sk_text = strdup(expr);
1792 case IMAP_SEARCH_CRITERIA_HEADER:
1793 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1794 sk_header_name = strdup(header);
1795 sk_header_value = strdup(expr);
1798 case IMAP_SEARCH_CRITERIA_FROM:
1799 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1800 sk_from = strdup(expr);
1803 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1804 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1808 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1809 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1817 return mailimap_search_key_new(etpan_matcher_type,
1818 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1819 sk_on, sk_since, sk_subject, sk_text, sk_to,
1820 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1821 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1822 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1825 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1827 return mailimap_search_key_new_not(key);
1830 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1832 return mailimap_search_key_new_or(l, r);
1835 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1837 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1838 mailimap_search_key_multiple_add(result, l);
1839 mailimap_search_key_multiple_add(result, r);
1844 void imap_search_free(IMAPSearchKey* key)
1849 mailimap_search_key_free(key);
1854 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1858 struct mailimap_msg_att_dynamic ** patt_dyn);
1861 result_to_uid_list(clist * fetch_result, carray ** result)
1863 clistiter * cur = NULL;
1868 tab = carray_new(128);
1870 res = MAILIMAP_ERROR_MEMORY;
1875 cur = clist_begin(fetch_result);
1877 for(; cur != NULL ; cur = clist_next(cur)) {
1878 struct mailimap_msg_att * msg_att;
1882 msg_att = clist_content(cur);
1885 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1887 puid = malloc(sizeof(* puid));
1889 res = MAILIMAP_ERROR_MEMORY;
1894 r = carray_add(tab, puid, NULL);
1897 res = MAILIMAP_ERROR_MEMORY;
1904 return MAILIMAP_NO_ERROR;
1907 imap_fetch_uid_list_free(tab);
1912 static int imap_get_messages_list(mailimap * imap,
1913 uint32_t first_index,
1918 struct mailimap_fetch_att * fetch_att;
1919 struct mailimap_fetch_type * fetch_type;
1920 struct mailimap_set * set;
1921 clist * fetch_result;
1924 set = mailimap_set_new_interval(first_index, 0);
1926 res = MAILIMAP_ERROR_MEMORY;
1930 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1931 if (fetch_type == NULL) {
1932 res = MAILIMAP_ERROR_MEMORY;
1936 fetch_att = mailimap_fetch_att_new_uid();
1937 if (fetch_att == NULL) {
1938 res = MAILIMAP_ERROR_MEMORY;
1939 goto free_fetch_type;
1942 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1943 if (r != MAILIMAP_NO_ERROR) {
1944 mailimap_fetch_att_free(fetch_att);
1945 res = MAILIMAP_ERROR_MEMORY;
1946 goto free_fetch_type;
1949 mailstream_logger = imap_logger_fetch;
1951 r = mailimap_uid_fetch(imap, set,
1952 fetch_type, &fetch_result);
1954 mailstream_logger = imap_logger_cmd;
1955 mailimap_fetch_type_free(fetch_type);
1956 mailimap_set_free(set);
1958 if (r != MAILIMAP_NO_ERROR) {
1964 r = result_to_uid_list(fetch_result, &env_list);
1965 mailimap_fetch_list_free(fetch_result);
1967 * result = env_list;
1969 return MAILIMAP_NO_ERROR;
1972 mailimap_fetch_type_free(fetch_type);
1974 mailimap_set_free(set);
1982 struct fetch_uid_param {
1984 uint32_t first_index;
1987 struct fetch_uid_result {
1989 carray * fetch_result;
1992 static void fetch_uid_run(struct etpan_thread_op * op)
1994 struct fetch_uid_param * param;
1995 struct fetch_uid_result * result;
1996 carray * fetch_result;
2000 result = op->result;
2004 fetch_result = NULL;
2005 mailstream_logger = imap_logger_noop;
2006 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
2008 r = imap_get_messages_list(param->imap, param->first_index,
2011 mailstream_logger = imap_logger_cmd;
2014 result->fetch_result = fetch_result;
2015 debug_print("imap fetch_uid run - end %i\n", r);
2018 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
2019 carray ** fetch_result)
2021 struct fetch_uid_param param;
2022 struct fetch_uid_result result;
2025 debug_print("imap fetch_uid - begin\n");
2027 imap = get_imap(folder);
2029 param.first_index = first_index;
2031 threaded_run(folder, ¶m, &result, fetch_uid_run);
2033 if (result.error != MAILIMAP_NO_ERROR)
2034 return result.error;
2036 debug_print("imap fetch_uid - end\n");
2038 * fetch_result = result.fetch_result;
2040 return result.error;
2044 void imap_fetch_uid_list_free(carray * uid_list)
2048 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
2051 puid = carray_get(uid_list, i);
2054 carray_free(uid_list);
2060 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
2063 result_to_uid_flags_list(clist * fetch_result, carray ** result)
2065 clistiter * cur = NULL;
2069 GSList *tags = NULL;
2071 tab = carray_new(128);
2073 res = MAILIMAP_ERROR_MEMORY;
2078 cur = clist_begin(fetch_result);
2080 for(; cur != NULL ; cur = clist_next(cur)) {
2081 struct mailimap_msg_att * msg_att;
2084 struct mailimap_msg_att_dynamic * att_dyn;
2090 msg_att = clist_content(cur);
2094 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2097 if (att_dyn == NULL)
2100 flags = imap_flags_to_flags(att_dyn, &tags);
2102 puid = malloc(sizeof(* puid));
2104 res = MAILIMAP_ERROR_MEMORY;
2109 r = carray_add(tab, puid, NULL);
2112 res = MAILIMAP_ERROR_MEMORY;
2115 pflags = malloc(sizeof(* pflags));
2116 if (pflags == NULL) {
2117 res = MAILIMAP_ERROR_MEMORY;
2121 r = carray_add(tab, pflags, NULL);
2124 res = MAILIMAP_ERROR_MEMORY;
2127 r = carray_add(tab, tags, NULL);
2130 res = MAILIMAP_ERROR_MEMORY;
2137 return MAILIMAP_NO_ERROR;
2140 imap_fetch_uid_flags_list_free(tab);
2141 slist_free_strings_full(tags);
2146 static int imap_get_messages_flags_list(mailimap * imap,
2147 uint32_t first_index,
2152 struct mailimap_fetch_att * fetch_att;
2153 struct mailimap_fetch_type * fetch_type;
2154 struct mailimap_set * set;
2155 clist * fetch_result;
2158 set = mailimap_set_new_interval(first_index, 0);
2160 res = MAILIMAP_ERROR_MEMORY;
2164 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2165 if (fetch_type == NULL) {
2166 res = MAILIMAP_ERROR_MEMORY;
2170 fetch_att = mailimap_fetch_att_new_flags();
2171 if (fetch_att == NULL) {
2172 res = MAILIMAP_ERROR_MEMORY;
2173 goto free_fetch_type;
2176 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2177 if (r != MAILIMAP_NO_ERROR) {
2178 mailimap_fetch_att_free(fetch_att);
2179 res = MAILIMAP_ERROR_MEMORY;
2180 goto free_fetch_type;
2183 fetch_att = mailimap_fetch_att_new_uid();
2184 if (fetch_att == NULL) {
2185 res = MAILIMAP_ERROR_MEMORY;
2186 goto free_fetch_type;
2189 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2190 if (r != MAILIMAP_NO_ERROR) {
2191 mailimap_fetch_att_free(fetch_att);
2192 res = MAILIMAP_ERROR_MEMORY;
2193 goto free_fetch_type;
2196 mailstream_logger = imap_logger_fetch;
2198 r = mailimap_uid_fetch(imap, set,
2199 fetch_type, &fetch_result);
2201 mailstream_logger = imap_logger_cmd;
2202 mailimap_fetch_type_free(fetch_type);
2203 mailimap_set_free(set);
2205 if (r != MAILIMAP_NO_ERROR) {
2211 r = result_to_uid_flags_list(fetch_result, &env_list);
2212 mailimap_fetch_list_free(fetch_result);
2214 * result = env_list;
2216 return MAILIMAP_NO_ERROR;
2219 mailimap_fetch_type_free(fetch_type);
2221 mailimap_set_free(set);
2228 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2230 struct fetch_uid_param * param;
2231 struct fetch_uid_result * result;
2232 carray * fetch_result;
2236 result = op->result;
2240 fetch_result = NULL;
2241 r = imap_get_messages_flags_list(param->imap, param->first_index,
2245 result->fetch_result = fetch_result;
2246 debug_print("imap fetch_uid run - end %i\n", r);
2249 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2250 carray ** fetch_result)
2252 struct fetch_uid_param param;
2253 struct fetch_uid_result result;
2256 debug_print("imap fetch_uid - begin\n");
2258 imap = get_imap(folder);
2260 param.first_index = first_index;
2262 mailstream_logger = imap_logger_noop;
2263 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2265 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2267 mailstream_logger = imap_logger_cmd;
2270 if (result.error != MAILIMAP_NO_ERROR)
2271 return result.error;
2273 debug_print("imap fetch_uid - end\n");
2275 * fetch_result = result.fetch_result;
2277 return result.error;
2281 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2285 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2288 data = carray_get(uid_flags_list, i);
2290 data = carray_get(uid_flags_list, i + 1);
2293 carray_free(uid_flags_list);
2298 static int imap_fetch(mailimap * imap,
2301 size_t * result_len)
2304 struct mailimap_set * set;
2305 struct mailimap_fetch_att * fetch_att;
2306 struct mailimap_fetch_type * fetch_type;
2307 clist * fetch_result;
2308 struct mailimap_msg_att * msg_att;
2309 struct mailimap_msg_att_item * msg_att_item;
2314 struct mailimap_section * section;
2316 set = mailimap_set_new_single(msg_index);
2318 res = MAILIMAP_ERROR_MEMORY;
2322 section = mailimap_section_new(NULL);
2323 if (section == NULL) {
2324 res = MAILIMAP_ERROR_MEMORY;
2328 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2329 if (fetch_att == NULL) {
2330 mailimap_section_free(section);
2331 res = MAILIMAP_ERROR_MEMORY;
2335 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2336 if (fetch_type == NULL) {
2337 res = MAILIMAP_ERROR_MEMORY;
2338 goto free_fetch_att;
2341 mailstream_logger = imap_logger_fetch;
2343 r = mailimap_uid_fetch(imap, set,
2344 fetch_type, &fetch_result);
2346 mailstream_logger = imap_logger_cmd;
2348 mailimap_fetch_type_free(fetch_type);
2349 mailimap_set_free(set);
2352 case MAILIMAP_NO_ERROR:
2358 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2359 mailimap_fetch_list_free(fetch_result);
2360 return MAILIMAP_ERROR_FETCH;
2363 msg_att = clist_begin(fetch_result)->data;
2368 if (msg_att->att_list)
2369 cur = clist_begin(msg_att->att_list);
2373 for(; cur != NULL ; cur = clist_next(cur)) {
2374 msg_att_item = clist_content(cur);
2376 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2377 if (msg_att_item->att_data.att_static->att_type ==
2378 MAILIMAP_MSG_ATT_BODY_SECTION) {
2379 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2381 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2383 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2388 mailimap_fetch_list_free(fetch_result);
2391 return MAILIMAP_ERROR_FETCH;
2394 * result_len = text_length;
2396 return MAILIMAP_NO_ERROR;
2399 mailimap_fetch_att_free(fetch_att);
2401 mailimap_set_free(set);
2406 static int imap_fetch_header(mailimap * imap,
2409 size_t * result_len)
2412 struct mailimap_set * set;
2413 struct mailimap_fetch_att * fetch_att;
2414 struct mailimap_fetch_type * fetch_type;
2415 clist * fetch_result;
2416 struct mailimap_msg_att * msg_att;
2417 struct mailimap_msg_att_item * msg_att_item;
2422 struct mailimap_section * section;
2424 set = mailimap_set_new_single(msg_index);
2426 res = MAILIMAP_ERROR_MEMORY;
2430 section = mailimap_section_new_header();
2431 if (section == NULL) {
2432 res = MAILIMAP_ERROR_MEMORY;
2436 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2437 if (fetch_att == NULL) {
2438 mailimap_section_free(section);
2439 res = MAILIMAP_ERROR_MEMORY;
2443 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2444 if (fetch_type == NULL) {
2445 res = MAILIMAP_ERROR_MEMORY;
2446 goto free_fetch_att;
2449 mailstream_logger = imap_logger_fetch;
2451 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2453 mailstream_logger = imap_logger_cmd;
2454 mailimap_fetch_type_free(fetch_type);
2455 mailimap_set_free(set);
2458 case MAILIMAP_NO_ERROR:
2464 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2465 mailimap_fetch_list_free(fetch_result);
2466 return MAILIMAP_ERROR_FETCH;
2469 msg_att = clist_begin(fetch_result)->data;
2474 if (msg_att->att_list)
2475 cur = clist_begin(msg_att->att_list);
2479 for(; cur != NULL ; cur = clist_next(cur)) {
2480 msg_att_item = clist_content(cur);
2482 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2483 if (msg_att_item->att_data.att_static->att_type ==
2484 MAILIMAP_MSG_ATT_BODY_SECTION) {
2485 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2486 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2488 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2493 mailimap_fetch_list_free(fetch_result);
2496 return MAILIMAP_ERROR_FETCH;
2499 * result_len = text_length;
2501 return MAILIMAP_NO_ERROR;
2504 mailimap_fetch_att_free(fetch_att);
2506 mailimap_set_free(set);
2513 struct fetch_content_param {
2516 const char * filename;
2520 struct fetch_content_result {
2524 static void fetch_content_run(struct etpan_thread_op * op)
2526 struct fetch_content_param * param;
2527 struct fetch_content_result * result;
2529 size_t content_size;
2535 result = op->result;
2541 if (param->with_body)
2542 r = imap_fetch(param->imap, param->msg_index,
2543 &content, &content_size);
2545 r = imap_fetch_header(param->imap, param->msg_index,
2546 &content, &content_size);
2550 if (r == MAILIMAP_NO_ERROR) {
2551 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2553 result->error = MAILIMAP_ERROR_FETCH;
2557 f = fdopen(fd, "wb");
2559 result->error = MAILIMAP_ERROR_FETCH;
2563 r = fwrite(content, 1, content_size, f);
2564 if (r < content_size) {
2565 result->error = MAILIMAP_ERROR_FETCH;
2571 result->error = MAILIMAP_ERROR_FETCH;
2582 claws_unlink(param->filename);
2585 /* mmap_string_unref is a simple free in libetpan
2586 * when it has MMAP_UNAVAILABLE defined */
2587 if (mmap_string_unref(content) != 0)
2591 debug_print("imap fetch_content run - end %i\n", result->error);
2594 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2596 const char * filename)
2598 struct fetch_content_param param;
2599 struct fetch_content_result result;
2602 debug_print("imap fetch_content - begin\n");
2604 imap = get_imap(folder);
2606 param.msg_index = msg_index;
2607 param.filename = filename;
2608 param.with_body = with_body;
2610 threaded_run(folder, ¶m, &result, fetch_content_run);
2612 if (result.error != MAILIMAP_NO_ERROR)
2613 return result.error;
2615 debug_print("imap fetch_content - end\n");
2617 return result.error;
2622 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2627 GSList *tags = NULL;
2631 flag_list = att_dyn->att_list;
2632 if (flag_list == NULL)
2635 for(cur = clist_begin(flag_list) ; cur != NULL ;
2636 cur = clist_next(cur)) {
2637 struct mailimap_flag_fetch * flag_fetch;
2639 flag_fetch = clist_content(cur);
2640 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2643 switch (flag_fetch->fl_flag->fl_type) {
2644 case MAILIMAP_FLAG_ANSWERED:
2645 flags |= MSG_REPLIED;
2647 case MAILIMAP_FLAG_FLAGGED:
2648 flags |= MSG_MARKED;
2650 case MAILIMAP_FLAG_DELETED:
2651 flags |= MSG_DELETED;
2653 case MAILIMAP_FLAG_SEEN:
2654 flags &= ~MSG_UNREAD;
2657 case MAILIMAP_FLAG_KEYWORD:
2658 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2659 flags |= MSG_FORWARDED;
2660 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2662 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2663 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2664 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2667 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2677 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2681 struct mailimap_msg_att_dynamic ** patt_dyn)
2683 clistiter * item_cur;
2687 struct mailimap_msg_att_dynamic * att_dyn;
2694 if (msg_att->att_list)
2695 item_cur = clist_begin(msg_att->att_list);
2698 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2699 struct mailimap_msg_att_item * item;
2701 item = clist_content(item_cur);
2703 switch (item->att_type) {
2704 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2705 switch (item->att_data.att_static->att_type) {
2706 case MAILIMAP_MSG_ATT_UID:
2707 uid = item->att_data.att_static->att_data.att_uid;
2710 case MAILIMAP_MSG_ATT_BODY_SECTION:
2711 if (headers == NULL) {
2712 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2715 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2716 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2721 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2722 if (att_dyn == NULL) {
2723 att_dyn = item->att_data.att_dyn;
2731 if (pheaders != NULL)
2732 * pheaders = headers;
2733 if (pref_size != NULL)
2734 * pref_size = ref_size;
2735 if (patt_dyn != NULL)
2736 * patt_dyn = att_dyn;
2738 return MAIL_NO_ERROR;
2741 static struct imap_fetch_env_info *
2742 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2744 struct imap_fetch_env_info * info;
2748 struct mailimap_msg_att_dynamic * att_dyn;
2750 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2755 info = malloc(sizeof(* info));
2757 info->headers = strdup(headers);
2759 info->flags = imap_flags_to_flags(att_dyn, tags);
2765 imap_fetch_result_to_envelop_list(clist * fetch_result,
2766 carray ** p_env_list)
2771 env_list = carray_new(16);
2774 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2775 cur = clist_next(cur)) {
2776 struct mailimap_msg_att * msg_att;
2777 struct imap_fetch_env_info * env_info;
2778 GSList *tags = NULL;
2780 msg_att = clist_content(cur);
2782 env_info = fetch_to_env_info(msg_att, &tags);
2784 return MAILIMAP_ERROR_MEMORY;
2785 carray_add(env_list, env_info, NULL);
2786 carray_add(env_list, tags, NULL);
2788 * p_env_list = env_list;
2790 * p_env_list = NULL;
2793 return MAIL_NO_ERROR;
2796 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2798 struct mailimap_fetch_att * fetch_att;
2802 struct mailimap_header_list * imap_hdrlist;
2803 struct mailimap_section * section;
2805 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2806 "References", "In-Reply-To", NULL
2809 hdrlist = clist_new();
2811 while (headers[i] != NULL) {
2812 header = strdup(headers[i]);
2813 if (header == NULL || clist_append(hdrlist, header) != 0)
2814 return MAIL_ERROR_MEMORY;
2818 imap_hdrlist = mailimap_header_list_new(hdrlist);
2819 section = mailimap_section_new_header_fields(imap_hdrlist);
2820 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2821 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2823 return MAIL_NO_ERROR;
2826 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2828 struct mailimap_fetch_att * fetch_att;
2829 struct mailimap_section * section;
2831 section = mailimap_section_new_header();
2832 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2833 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2835 return MAIL_NO_ERROR;
2839 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2840 carray ** p_env_list)
2842 struct mailimap_fetch_att * fetch_att;
2843 struct mailimap_fetch_type * fetch_type;
2845 clist * fetch_result;
2847 carray * env_list = NULL;
2851 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2854 fetch_att = mailimap_fetch_att_new_uid();
2855 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2858 fetch_att = mailimap_fetch_att_new_flags();
2859 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2862 fetch_att = mailimap_fetch_att_new_rfc822_size();
2863 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2867 key.len = sizeof(imap);
2868 r = chash_get(courier_workaround_hash, &key, &value);
2870 r = imap_add_envelope_fetch_att(fetch_type);
2872 r = imap_add_header_fetch_att(fetch_type);
2874 mailstream_logger = imap_logger_fetch;
2876 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2878 mailstream_logger = imap_logger_cmd;
2880 case MAILIMAP_NO_ERROR:
2883 mailimap_fetch_type_free(fetch_type);
2884 debug_print("uid_fetch: %d\n", r);
2888 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2889 res = MAILIMAP_ERROR_FETCH;
2890 debug_print("clist_begin = NULL\n");
2894 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2895 mailimap_fetch_list_free(fetch_result);
2897 if (r != MAILIMAP_NO_ERROR) {
2898 mailimap_fetch_type_free(fetch_type);
2899 res = MAILIMAP_ERROR_MEMORY;
2900 debug_print("fetch_result_to_envelop_list: %d\n", res);
2904 mailimap_fetch_type_free(fetch_type);
2906 * p_env_list = env_list;
2908 return MAILIMAP_NO_ERROR;
2914 struct fetch_env_param {
2916 struct mailimap_set * set;
2919 struct fetch_env_result {
2920 carray * fetch_env_result;
2924 static void fetch_env_run(struct etpan_thread_op * op)
2926 struct fetch_env_param * param;
2927 struct fetch_env_result * result;
2932 result = op->result;
2937 r = imap_get_envelopes_list(param->imap, param->set,
2941 result->fetch_env_result = env_list;
2943 debug_print("imap fetch_env run - end %i\n", r);
2946 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2947 carray ** p_env_list)
2949 struct fetch_env_param param;
2950 struct fetch_env_result result;
2953 debug_print("imap fetch_env - begin\n");
2955 imap = get_imap(folder);
2959 threaded_run(folder, ¶m, &result, fetch_env_run);
2961 if (result.error != MAILIMAP_NO_ERROR) {
2967 key.len = sizeof(imap);
2968 r = chash_get(courier_workaround_hash, &key, &value);
2972 chash_set(courier_workaround_hash, &key, &value, NULL);
2974 threaded_run(folder, ¶m, &result, fetch_env_run);
2978 if (result.error != MAILIMAP_NO_ERROR)
2979 return result.error;
2981 debug_print("imap fetch_env - end\n");
2983 * p_env_list = result.fetch_env_result;
2985 return result.error;
2988 void imap_fetch_env_free(carray * env_list)
2992 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2993 struct imap_fetch_env_info * env_info;
2995 env_info = carray_get(env_list, i);
2996 free(env_info->headers);
2999 carray_free(env_list);
3006 struct append_param {
3008 const char * mailbox;
3009 const char * filename;
3010 struct mailimap_flag_list * flag_list;
3013 struct append_result {
3018 static void append_run(struct etpan_thread_op * op)
3020 struct append_param * param;
3021 struct append_result * result;
3026 struct stat stat_buf;
3029 guint32 uid = 0, val = 0;
3032 result = op->result;
3037 r = stat(param->filename, &stat_buf);
3039 result->error = MAILIMAP_ERROR_APPEND;
3042 size = stat_buf.st_size;
3044 fd = g_open(param->filename, O_RDONLY, 0);
3046 result->error = MAILIMAP_ERROR_APPEND;
3050 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
3051 if (data == (void *) MAP_FAILED) {
3053 result->error = MAILIMAP_ERROR_APPEND;
3057 data = file_read_to_str_no_recode(param->filename);
3059 result->error = MAILIMAP_ERROR_APPEND;
3062 size = strlen(data);
3064 mailstream_logger = imap_logger_append;
3066 r = mailimap_uidplus_append(param->imap, param->mailbox,
3067 param->flag_list, NULL,
3068 data, size, &val, &uid);
3070 mailstream_logger = imap_logger_cmd;
3081 debug_print("imap append run - end %i uid %d\n", r, uid);
3084 int imap_threaded_append(Folder * folder, const char * mailbox,
3085 const char * filename,
3086 struct mailimap_flag_list * flag_list,
3089 struct append_param param;
3090 struct append_result result;
3093 debug_print("imap append - begin\n");
3095 imap = get_imap(folder);
3097 param.mailbox = mailbox;
3098 param.filename = filename;
3099 param.flag_list = flag_list;
3101 threaded_run(folder, ¶m, &result, append_run);
3103 if (result.error != MAILIMAP_NO_ERROR)
3104 return result.error;
3106 debug_print("imap append - end\n");
3110 return result.error;
3116 struct expunge_param {
3120 struct expunge_result {
3124 static void expunge_run(struct etpan_thread_op * op)
3126 struct expunge_param * param;
3127 struct expunge_result * result;
3131 result = op->result;
3135 r = mailimap_expunge(param->imap);
3138 debug_print("imap expunge run - end %i\n", r);
3141 int imap_threaded_expunge(Folder * folder)
3143 struct expunge_param param;
3144 struct expunge_result result;
3146 debug_print("imap expunge - begin\n");
3148 param.imap = get_imap(folder);
3150 threaded_run(folder, ¶m, &result, expunge_run);
3152 debug_print("imap expunge - end\n");
3154 return result.error;
3160 struct mailimap_set * set;
3164 struct copy_result {
3166 struct mailimap_set *source;
3167 struct mailimap_set *dest;
3170 static void copy_run(struct etpan_thread_op * op)
3172 struct copy_param * param;
3173 struct copy_result * result;
3176 struct mailimap_set *source = NULL, *dest = NULL;
3179 result = op->result;
3183 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3184 &val, &source, &dest);
3188 result->source = source;
3189 result->dest = dest;
3191 result->source = NULL;
3192 result->dest = NULL;
3194 debug_print("imap copy run - end %i\n", r);
3197 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3198 const char * mb, struct mailimap_set **source,
3199 struct mailimap_set **dest)
3201 struct copy_param param;
3202 struct copy_result result;
3205 debug_print("imap copy - begin\n");
3207 imap = get_imap(folder);
3212 threaded_run(folder, ¶m, &result, copy_run);
3216 if (result.error != MAILIMAP_NO_ERROR)
3217 return result.error;
3219 *source = result.source;
3220 *dest = result.dest;
3222 debug_print("imap copy - end\n");
3224 return result.error;
3229 struct store_param {
3231 struct mailimap_set * set;
3232 struct mailimap_store_att_flags * store_att_flags;
3235 struct store_result {
3239 static void store_run(struct etpan_thread_op * op)
3241 struct store_param * param;
3242 struct store_result * result;
3246 result = op->result;
3250 r = mailimap_uid_store(param->imap, param->set,
3251 param->store_att_flags);
3255 debug_print("imap store run - end %i\n", r);
3258 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3259 struct mailimap_store_att_flags * store_att_flags)
3261 struct store_param param;
3262 struct store_result result;
3265 debug_print("imap store - begin\n");
3267 imap = get_imap(folder);
3270 param.store_att_flags = store_att_flags;
3272 threaded_run(folder, ¶m, &result, store_run);
3274 if (result.error != MAILIMAP_NO_ERROR)
3275 return result.error;
3277 debug_print("imap store - end\n");
3279 return result.error;
3283 #define ENV_BUFFER_SIZE 512
3285 static void do_exec_command(int fd, const char * command,
3286 const char * servername, uint16_t port)
3290 char env_buffer[ENV_BUFFER_SIZE];
3294 /* Fork again to become a child of init rather than
3295 the etpan client. */
3300 g_setenv("ETPANSERVER", servername, TRUE);
3302 g_unsetenv("ETPANSERVER");
3307 snprintf(porttext, sizeof(porttext), "%d", port);
3308 g_setenv("ETPANPORT", porttext, TRUE);
3311 g_unsetenv("ETPANPORT");
3314 /* Not a lot we can do if there's an error other than bail. */
3315 if (dup2(fd, 0) == -1)
3317 if (dup2(fd, 1) == -1)
3320 /* Should we close stderr and reopen /dev/null? */
3322 maxopen = sysconf(_SC_OPEN_MAX);
3323 for (i=3; i < maxopen; i++)
3327 /* Detach from the controlling tty if we have one. Otherwise,
3328 SSH might do something stupid like trying to use it instead
3329 of running $SSH_ASKPASS. Doh. */
3330 fd = g_open("/dev/tty", O_RDONLY, 0);
3332 ioctl(fd, TIOCNOTTY, NULL);
3335 #endif /* TIOCNOTTY */
3337 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3339 /* Eep. Shouldn't reach this */
3343 static int subcommand_connect(const char *command,
3344 const char *servername, uint16_t port)
3346 /* SEB unsupported on Windows */
3350 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3355 do_exec_command(sockfds[1], command, servername, port);
3357 else if (childpid == -1) {
3365 /* Reap child, leaving grandchild process to run */
3366 waitpid(childpid, NULL, 0);
3371 static int socket_connect_cmd(mailimap * imap, const char * command,
3372 const char * server, int port)
3378 fd = subcommand_connect(command, server, port);
3380 return MAILIMAP_ERROR_STREAM;
3382 s = mailstream_socket_open(fd);
3385 return MAILIMAP_ERROR_STREAM;
3388 r = mailimap_connect(imap, s);
3389 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3390 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3391 mailstream_close(s);
3393 imap->imap_stream = NULL;
3402 struct connect_cmd_param {
3404 const char * command;
3405 const char * server;
3409 struct connect_cmd_result {
3413 static void connect_cmd_run(struct etpan_thread_op * op)
3416 struct connect_cmd_param * param;
3417 struct connect_cmd_result * result;
3420 result = op->result;
3424 r = socket_connect_cmd(param->imap, param->command,
3425 param->server, param->port);
3431 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3432 const char * server, int port)
3434 struct connect_cmd_param param;
3435 struct connect_cmd_result result;
3438 mailimap * imap, * oldimap;
3440 oldimap = get_imap(folder);
3442 imap = mailimap_new(0, NULL);
3445 debug_print("deleting old imap %p\n", oldimap);
3446 delete_imap(folder, oldimap);
3450 key.len = sizeof(folder);
3453 chash_set(session_hash, &key, &value, NULL);
3456 param.command = command;
3457 param.server = server;
3460 threaded_run(folder, ¶m, &result, connect_cmd_run);
3462 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3464 return result.error;
3466 #endif /* G_OS_WIN32 */
3468 void imap_threaded_cancel(Folder * folder)
3472 imap = get_imap(folder);
3473 if (imap->imap_stream != NULL)
3474 mailstream_cancel(imap->imap_stream);
3479 void imap_main_init(void)
3482 void imap_main_done(gboolean have_connectivity)
3485 void imap_main_set_timeout(int sec)
3489 void imap_threaded_cancel(Folder * folder);