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;
1596 result = op->result;
1600 /* we copy the mailimap_set because freeing the key is recursive */
1601 if (param->set != NULL) {
1602 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1603 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1604 uid_key = mailimap_search_key_new_all();
1606 switch (param->type) {
1607 case IMAP_SEARCH_TYPE_SIMPLE:
1608 search_type_key = NULL;
1610 case IMAP_SEARCH_TYPE_SEEN:
1611 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1613 case IMAP_SEARCH_TYPE_UNSEEN:
1614 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1616 case IMAP_SEARCH_TYPE_ANSWERED:
1617 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1619 case IMAP_SEARCH_TYPE_FLAGGED:
1620 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1622 case IMAP_SEARCH_TYPE_DELETED:
1623 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1625 case IMAP_SEARCH_TYPE_FORWARDED:
1626 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1628 case IMAP_SEARCH_TYPE_SPAM:
1629 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1631 case IMAP_SEARCH_TYPE_KEYED:
1632 search_type_key = param->key;
1636 if (search_type_key != NULL) {
1637 if (param->set != NULL) {
1638 key = mailimap_search_key_new_multiple_empty();
1639 mailimap_search_key_multiple_add(key, search_type_key);
1640 mailimap_search_key_multiple_add(key, uid_key);
1642 key = search_type_key;
1644 } else if (uid_key != NULL) {
1649 g_warning("no key!");
1650 result = op->result;
1652 result->search_result = NULL;
1654 mailstream_logger = imap_logger_uid;
1656 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1658 mailstream_logger = imap_logger_cmd;
1660 /* free the key (with the imapset) */
1661 mailimap_search_key_free(key);
1664 result->search_result = search_result;
1666 debug_print("imap search run - end %i\n", result->error);
1669 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1670 struct mailimap_set * set, clist ** search_result)
1672 struct search_param param;
1673 struct search_result result;
1676 debug_print("imap search - begin\n");
1678 imap = get_imap(folder);
1681 param.type = search_type;
1684 threaded_run(folder, ¶m, &result, search_run);
1686 if (result.error != MAILIMAP_NO_ERROR)
1687 return result.error;
1689 debug_print("imap search - end\n");
1691 * search_result = result.search_result;
1693 return result.error;
1697 struct _IMAPSearchKey {
1698 struct mailimap_search_key* key;
1701 IMAPSearchKey* imap_search_new(gint criteria,
1702 const gchar *header,
1706 char* sk_bcc = NULL;
1707 struct mailimap_date* sk_before = NULL;
1708 char* sk_body = NULL;
1710 char* sk_from = NULL;
1711 char* sk_keyword = NULL;
1712 struct mailimap_date* sk_on = NULL;
1713 struct mailimap_date* sk_since = NULL;
1714 char* sk_subject = NULL;
1715 char* sk_text = NULL;
1717 char* sk_unkeyword = NULL;
1718 char* sk_header_name = NULL;
1719 char* sk_header_value = NULL;
1720 uint32_t sk_larger = 0;
1721 struct mailimap_search_key* sk_not = NULL;
1722 struct mailimap_search_key* sk_or1 = NULL;
1723 struct mailimap_search_key* sk_or2 = NULL;
1724 struct mailimap_date* sk_sentbefore = NULL;
1725 struct mailimap_date* sk_senton = NULL;
1726 struct mailimap_date* sk_sentsince = NULL;
1727 uint32_t sk_smaller = 0;
1728 struct mailimap_set* sk_uid = NULL;
1729 struct mailimap_set* sk_set = NULL;
1730 clist* sk_multiple = NULL;
1731 int etpan_matcher_type;
1734 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1735 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1736 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1737 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1738 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1739 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1740 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1742 case IMAP_SEARCH_CRITERIA_TAG:
1743 sk_keyword = strdup(expr);
1744 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1747 case IMAP_SEARCH_CRITERIA_SUBJECT:
1748 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1749 sk_subject = strdup(expr);
1752 case IMAP_SEARCH_CRITERIA_TO:
1753 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1754 sk_to = strdup(expr);
1757 case IMAP_SEARCH_CRITERIA_CC:
1758 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1759 sk_cc = strdup(expr);
1762 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1763 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1766 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1769 localtime_r(&limit, &tm);
1770 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1771 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1772 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1774 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1775 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1780 case IMAP_SEARCH_CRITERIA_BODY:
1781 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1782 sk_body = strdup(expr);
1785 case IMAP_SEARCH_CRITERIA_MESSAGE:
1786 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1787 sk_text = strdup(expr);
1790 case IMAP_SEARCH_CRITERIA_HEADER:
1791 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1792 sk_header_name = strdup(header);
1793 sk_header_value = strdup(expr);
1796 case IMAP_SEARCH_CRITERIA_FROM:
1797 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1798 sk_from = strdup(expr);
1801 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1802 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1806 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1807 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1815 return mailimap_search_key_new(etpan_matcher_type,
1816 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1817 sk_on, sk_since, sk_subject, sk_text, sk_to,
1818 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1819 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1820 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1823 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1825 return mailimap_search_key_new_not(key);
1828 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1830 return mailimap_search_key_new_or(l, r);
1833 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1835 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1836 mailimap_search_key_multiple_add(result, l);
1837 mailimap_search_key_multiple_add(result, r);
1842 void imap_search_free(IMAPSearchKey* key)
1847 mailimap_search_key_free(key);
1852 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1856 struct mailimap_msg_att_dynamic ** patt_dyn);
1859 result_to_uid_list(clist * fetch_result, carray ** result)
1861 clistiter * cur = NULL;
1866 tab = carray_new(128);
1868 res = MAILIMAP_ERROR_MEMORY;
1873 cur = clist_begin(fetch_result);
1875 for(; cur != NULL ; cur = clist_next(cur)) {
1876 struct mailimap_msg_att * msg_att;
1880 msg_att = clist_content(cur);
1883 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1885 puid = malloc(sizeof(* puid));
1887 res = MAILIMAP_ERROR_MEMORY;
1892 r = carray_add(tab, puid, NULL);
1895 res = MAILIMAP_ERROR_MEMORY;
1902 return MAILIMAP_NO_ERROR;
1905 imap_fetch_uid_list_free(tab);
1910 static int imap_get_messages_list(mailimap * imap,
1911 uint32_t first_index,
1916 struct mailimap_fetch_att * fetch_att;
1917 struct mailimap_fetch_type * fetch_type;
1918 struct mailimap_set * set;
1919 clist * fetch_result;
1922 set = mailimap_set_new_interval(first_index, 0);
1924 res = MAILIMAP_ERROR_MEMORY;
1928 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1929 if (fetch_type == NULL) {
1930 res = MAILIMAP_ERROR_MEMORY;
1934 fetch_att = mailimap_fetch_att_new_uid();
1935 if (fetch_att == NULL) {
1936 res = MAILIMAP_ERROR_MEMORY;
1937 goto free_fetch_type;
1940 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1941 if (r != MAILIMAP_NO_ERROR) {
1942 mailimap_fetch_att_free(fetch_att);
1943 res = MAILIMAP_ERROR_MEMORY;
1944 goto free_fetch_type;
1947 mailstream_logger = imap_logger_fetch;
1949 r = mailimap_uid_fetch(imap, set,
1950 fetch_type, &fetch_result);
1952 mailstream_logger = imap_logger_cmd;
1953 mailimap_fetch_type_free(fetch_type);
1954 mailimap_set_free(set);
1956 if (r != MAILIMAP_NO_ERROR) {
1962 r = result_to_uid_list(fetch_result, &env_list);
1963 mailimap_fetch_list_free(fetch_result);
1965 * result = env_list;
1967 return MAILIMAP_NO_ERROR;
1970 mailimap_fetch_type_free(fetch_type);
1972 mailimap_set_free(set);
1980 struct fetch_uid_param {
1982 uint32_t first_index;
1985 struct fetch_uid_result {
1987 carray * fetch_result;
1990 static void fetch_uid_run(struct etpan_thread_op * op)
1992 struct fetch_uid_param * param;
1993 struct fetch_uid_result * result;
1994 carray * fetch_result;
1998 result = op->result;
2002 fetch_result = NULL;
2003 mailstream_logger = imap_logger_noop;
2004 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
2006 r = imap_get_messages_list(param->imap, param->first_index,
2009 mailstream_logger = imap_logger_cmd;
2012 result->fetch_result = fetch_result;
2013 debug_print("imap fetch_uid run - end %i\n", r);
2016 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
2017 carray ** fetch_result)
2019 struct fetch_uid_param param;
2020 struct fetch_uid_result result;
2023 debug_print("imap fetch_uid - begin\n");
2025 imap = get_imap(folder);
2027 param.first_index = first_index;
2029 threaded_run(folder, ¶m, &result, fetch_uid_run);
2031 if (result.error != MAILIMAP_NO_ERROR)
2032 return result.error;
2034 debug_print("imap fetch_uid - end\n");
2036 * fetch_result = result.fetch_result;
2038 return result.error;
2042 void imap_fetch_uid_list_free(carray * uid_list)
2046 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
2049 puid = carray_get(uid_list, i);
2052 carray_free(uid_list);
2058 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
2061 result_to_uid_flags_list(clist * fetch_result, carray ** result)
2063 clistiter * cur = NULL;
2067 GSList *tags = NULL;
2069 tab = carray_new(128);
2071 res = MAILIMAP_ERROR_MEMORY;
2076 cur = clist_begin(fetch_result);
2078 for(; cur != NULL ; cur = clist_next(cur)) {
2079 struct mailimap_msg_att * msg_att;
2082 struct mailimap_msg_att_dynamic * att_dyn;
2088 msg_att = clist_content(cur);
2092 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2095 if (att_dyn == NULL)
2098 flags = imap_flags_to_flags(att_dyn, &tags);
2100 puid = malloc(sizeof(* puid));
2102 res = MAILIMAP_ERROR_MEMORY;
2107 r = carray_add(tab, puid, NULL);
2110 res = MAILIMAP_ERROR_MEMORY;
2113 pflags = malloc(sizeof(* pflags));
2114 if (pflags == NULL) {
2115 res = MAILIMAP_ERROR_MEMORY;
2119 r = carray_add(tab, pflags, NULL);
2122 res = MAILIMAP_ERROR_MEMORY;
2125 r = carray_add(tab, tags, NULL);
2128 res = MAILIMAP_ERROR_MEMORY;
2135 return MAILIMAP_NO_ERROR;
2138 imap_fetch_uid_flags_list_free(tab);
2139 slist_free_strings_full(tags);
2144 static int imap_get_messages_flags_list(mailimap * imap,
2145 uint32_t first_index,
2150 struct mailimap_fetch_att * fetch_att;
2151 struct mailimap_fetch_type * fetch_type;
2152 struct mailimap_set * set;
2153 clist * fetch_result;
2156 set = mailimap_set_new_interval(first_index, 0);
2158 res = MAILIMAP_ERROR_MEMORY;
2162 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2163 if (fetch_type == NULL) {
2164 res = MAILIMAP_ERROR_MEMORY;
2168 fetch_att = mailimap_fetch_att_new_flags();
2169 if (fetch_att == NULL) {
2170 res = MAILIMAP_ERROR_MEMORY;
2171 goto free_fetch_type;
2174 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2175 if (r != MAILIMAP_NO_ERROR) {
2176 mailimap_fetch_att_free(fetch_att);
2177 res = MAILIMAP_ERROR_MEMORY;
2178 goto free_fetch_type;
2181 fetch_att = mailimap_fetch_att_new_uid();
2182 if (fetch_att == NULL) {
2183 res = MAILIMAP_ERROR_MEMORY;
2184 goto free_fetch_type;
2187 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2188 if (r != MAILIMAP_NO_ERROR) {
2189 mailimap_fetch_att_free(fetch_att);
2190 res = MAILIMAP_ERROR_MEMORY;
2191 goto free_fetch_type;
2194 mailstream_logger = imap_logger_fetch;
2196 r = mailimap_uid_fetch(imap, set,
2197 fetch_type, &fetch_result);
2199 mailstream_logger = imap_logger_cmd;
2200 mailimap_fetch_type_free(fetch_type);
2201 mailimap_set_free(set);
2203 if (r != MAILIMAP_NO_ERROR) {
2209 r = result_to_uid_flags_list(fetch_result, &env_list);
2210 mailimap_fetch_list_free(fetch_result);
2212 * result = env_list;
2214 return MAILIMAP_NO_ERROR;
2217 mailimap_fetch_type_free(fetch_type);
2219 mailimap_set_free(set);
2226 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2228 struct fetch_uid_param * param;
2229 struct fetch_uid_result * result;
2230 carray * fetch_result;
2234 result = op->result;
2238 fetch_result = NULL;
2239 r = imap_get_messages_flags_list(param->imap, param->first_index,
2243 result->fetch_result = fetch_result;
2244 debug_print("imap fetch_uid run - end %i\n", r);
2247 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2248 carray ** fetch_result)
2250 struct fetch_uid_param param;
2251 struct fetch_uid_result result;
2254 debug_print("imap fetch_uid - begin\n");
2256 imap = get_imap(folder);
2258 param.first_index = first_index;
2260 mailstream_logger = imap_logger_noop;
2261 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2263 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2265 mailstream_logger = imap_logger_cmd;
2268 if (result.error != MAILIMAP_NO_ERROR)
2269 return result.error;
2271 debug_print("imap fetch_uid - end\n");
2273 * fetch_result = result.fetch_result;
2275 return result.error;
2279 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2283 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2286 data = carray_get(uid_flags_list, i);
2288 data = carray_get(uid_flags_list, i + 1);
2291 carray_free(uid_flags_list);
2296 static int imap_fetch(mailimap * imap,
2299 size_t * result_len)
2302 struct mailimap_set * set;
2303 struct mailimap_fetch_att * fetch_att;
2304 struct mailimap_fetch_type * fetch_type;
2305 clist * fetch_result;
2306 struct mailimap_msg_att * msg_att;
2307 struct mailimap_msg_att_item * msg_att_item;
2312 struct mailimap_section * section;
2314 set = mailimap_set_new_single(msg_index);
2316 res = MAILIMAP_ERROR_MEMORY;
2320 section = mailimap_section_new(NULL);
2321 if (section == NULL) {
2322 res = MAILIMAP_ERROR_MEMORY;
2326 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2327 if (fetch_att == NULL) {
2328 mailimap_section_free(section);
2329 res = MAILIMAP_ERROR_MEMORY;
2333 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2334 if (fetch_type == NULL) {
2335 res = MAILIMAP_ERROR_MEMORY;
2336 goto free_fetch_att;
2339 mailstream_logger = imap_logger_fetch;
2341 r = mailimap_uid_fetch(imap, set,
2342 fetch_type, &fetch_result);
2344 mailstream_logger = imap_logger_cmd;
2346 mailimap_fetch_type_free(fetch_type);
2347 mailimap_set_free(set);
2350 case MAILIMAP_NO_ERROR:
2356 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2357 mailimap_fetch_list_free(fetch_result);
2358 return MAILIMAP_ERROR_FETCH;
2361 msg_att = clist_begin(fetch_result)->data;
2366 if (msg_att->att_list)
2367 cur = clist_begin(msg_att->att_list);
2371 for(; cur != NULL ; cur = clist_next(cur)) {
2372 msg_att_item = clist_content(cur);
2374 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2375 if (msg_att_item->att_data.att_static->att_type ==
2376 MAILIMAP_MSG_ATT_BODY_SECTION) {
2377 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2379 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2381 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2386 mailimap_fetch_list_free(fetch_result);
2389 return MAILIMAP_ERROR_FETCH;
2392 * result_len = text_length;
2394 return MAILIMAP_NO_ERROR;
2397 mailimap_fetch_att_free(fetch_att);
2399 mailimap_set_free(set);
2404 static int imap_fetch_header(mailimap * imap,
2407 size_t * result_len)
2410 struct mailimap_set * set;
2411 struct mailimap_fetch_att * fetch_att;
2412 struct mailimap_fetch_type * fetch_type;
2413 clist * fetch_result;
2414 struct mailimap_msg_att * msg_att;
2415 struct mailimap_msg_att_item * msg_att_item;
2420 struct mailimap_section * section;
2422 set = mailimap_set_new_single(msg_index);
2424 res = MAILIMAP_ERROR_MEMORY;
2428 section = mailimap_section_new_header();
2429 if (section == NULL) {
2430 res = MAILIMAP_ERROR_MEMORY;
2434 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2435 if (fetch_att == NULL) {
2436 mailimap_section_free(section);
2437 res = MAILIMAP_ERROR_MEMORY;
2441 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2442 if (fetch_type == NULL) {
2443 res = MAILIMAP_ERROR_MEMORY;
2444 goto free_fetch_att;
2447 mailstream_logger = imap_logger_fetch;
2449 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2451 mailstream_logger = imap_logger_cmd;
2452 mailimap_fetch_type_free(fetch_type);
2453 mailimap_set_free(set);
2456 case MAILIMAP_NO_ERROR:
2462 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2463 mailimap_fetch_list_free(fetch_result);
2464 return MAILIMAP_ERROR_FETCH;
2467 msg_att = clist_begin(fetch_result)->data;
2472 if (msg_att->att_list)
2473 cur = clist_begin(msg_att->att_list);
2477 for(; cur != NULL ; cur = clist_next(cur)) {
2478 msg_att_item = clist_content(cur);
2480 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2481 if (msg_att_item->att_data.att_static->att_type ==
2482 MAILIMAP_MSG_ATT_BODY_SECTION) {
2483 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2484 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2486 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2491 mailimap_fetch_list_free(fetch_result);
2494 return MAILIMAP_ERROR_FETCH;
2497 * result_len = text_length;
2499 return MAILIMAP_NO_ERROR;
2502 mailimap_fetch_att_free(fetch_att);
2504 mailimap_set_free(set);
2511 struct fetch_content_param {
2514 const char * filename;
2518 struct fetch_content_result {
2522 static void fetch_content_run(struct etpan_thread_op * op)
2524 struct fetch_content_param * param;
2525 struct fetch_content_result * result;
2527 size_t content_size;
2533 result = op->result;
2539 if (param->with_body)
2540 r = imap_fetch(param->imap, param->msg_index,
2541 &content, &content_size);
2543 r = imap_fetch_header(param->imap, param->msg_index,
2544 &content, &content_size);
2548 if (r == MAILIMAP_NO_ERROR) {
2549 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2551 result->error = MAILIMAP_ERROR_FETCH;
2555 f = fdopen(fd, "wb");
2557 result->error = MAILIMAP_ERROR_FETCH;
2561 r = fwrite(content, 1, content_size, f);
2562 if (r < content_size) {
2563 result->error = MAILIMAP_ERROR_FETCH;
2569 result->error = MAILIMAP_ERROR_FETCH;
2580 claws_unlink(param->filename);
2583 /* mmap_string_unref is a simple free in libetpan
2584 * when it has MMAP_UNAVAILABLE defined */
2585 if (mmap_string_unref(content) != 0)
2589 debug_print("imap fetch_content run - end %i\n", result->error);
2592 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2594 const char * filename)
2596 struct fetch_content_param param;
2597 struct fetch_content_result result;
2600 debug_print("imap fetch_content - begin\n");
2602 imap = get_imap(folder);
2604 param.msg_index = msg_index;
2605 param.filename = filename;
2606 param.with_body = with_body;
2608 threaded_run(folder, ¶m, &result, fetch_content_run);
2610 if (result.error != MAILIMAP_NO_ERROR)
2611 return result.error;
2613 debug_print("imap fetch_content - end\n");
2615 return result.error;
2620 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2625 GSList *tags = NULL;
2629 flag_list = att_dyn->att_list;
2630 if (flag_list == NULL)
2633 for(cur = clist_begin(flag_list) ; cur != NULL ;
2634 cur = clist_next(cur)) {
2635 struct mailimap_flag_fetch * flag_fetch;
2637 flag_fetch = clist_content(cur);
2638 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2641 switch (flag_fetch->fl_flag->fl_type) {
2642 case MAILIMAP_FLAG_ANSWERED:
2643 flags |= MSG_REPLIED;
2645 case MAILIMAP_FLAG_FLAGGED:
2646 flags |= MSG_MARKED;
2648 case MAILIMAP_FLAG_DELETED:
2649 flags |= MSG_DELETED;
2651 case MAILIMAP_FLAG_SEEN:
2652 flags &= ~MSG_UNREAD;
2655 case MAILIMAP_FLAG_KEYWORD:
2656 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2657 flags |= MSG_FORWARDED;
2658 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2660 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2661 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2662 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2665 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2675 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2679 struct mailimap_msg_att_dynamic ** patt_dyn)
2681 clistiter * item_cur;
2685 struct mailimap_msg_att_dynamic * att_dyn;
2692 if (msg_att->att_list)
2693 item_cur = clist_begin(msg_att->att_list);
2696 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2697 struct mailimap_msg_att_item * item;
2699 item = clist_content(item_cur);
2701 switch (item->att_type) {
2702 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2703 switch (item->att_data.att_static->att_type) {
2704 case MAILIMAP_MSG_ATT_UID:
2705 uid = item->att_data.att_static->att_data.att_uid;
2708 case MAILIMAP_MSG_ATT_BODY_SECTION:
2709 if (headers == NULL) {
2710 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2713 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2714 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2719 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2720 if (att_dyn == NULL) {
2721 att_dyn = item->att_data.att_dyn;
2729 if (pheaders != NULL)
2730 * pheaders = headers;
2731 if (pref_size != NULL)
2732 * pref_size = ref_size;
2733 if (patt_dyn != NULL)
2734 * patt_dyn = att_dyn;
2736 return MAIL_NO_ERROR;
2739 static struct imap_fetch_env_info *
2740 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2742 struct imap_fetch_env_info * info;
2746 struct mailimap_msg_att_dynamic * att_dyn;
2748 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2753 info = malloc(sizeof(* info));
2755 info->headers = strdup(headers);
2757 info->flags = imap_flags_to_flags(att_dyn, tags);
2763 imap_fetch_result_to_envelop_list(clist * fetch_result,
2764 carray ** p_env_list)
2769 env_list = carray_new(16);
2772 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2773 cur = clist_next(cur)) {
2774 struct mailimap_msg_att * msg_att;
2775 struct imap_fetch_env_info * env_info;
2776 GSList *tags = NULL;
2778 msg_att = clist_content(cur);
2780 env_info = fetch_to_env_info(msg_att, &tags);
2782 return MAILIMAP_ERROR_MEMORY;
2783 carray_add(env_list, env_info, NULL);
2784 carray_add(env_list, tags, NULL);
2786 * p_env_list = env_list;
2788 * p_env_list = NULL;
2791 return MAIL_NO_ERROR;
2794 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2796 struct mailimap_fetch_att * fetch_att;
2800 struct mailimap_header_list * imap_hdrlist;
2801 struct mailimap_section * section;
2803 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2804 "References", "In-Reply-To", NULL
2807 hdrlist = clist_new();
2809 while (headers[i] != NULL) {
2810 header = strdup(headers[i]);
2811 if (header == NULL || clist_append(hdrlist, header) != 0)
2812 return MAIL_ERROR_MEMORY;
2816 imap_hdrlist = mailimap_header_list_new(hdrlist);
2817 section = mailimap_section_new_header_fields(imap_hdrlist);
2818 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2819 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2821 return MAIL_NO_ERROR;
2824 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2826 struct mailimap_fetch_att * fetch_att;
2827 struct mailimap_section * section;
2829 section = mailimap_section_new_header();
2830 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2831 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2833 return MAIL_NO_ERROR;
2837 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2838 carray ** p_env_list)
2840 struct mailimap_fetch_att * fetch_att;
2841 struct mailimap_fetch_type * fetch_type;
2843 clist * fetch_result;
2845 carray * env_list = NULL;
2849 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2852 fetch_att = mailimap_fetch_att_new_uid();
2853 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2856 fetch_att = mailimap_fetch_att_new_flags();
2857 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2860 fetch_att = mailimap_fetch_att_new_rfc822_size();
2861 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2865 key.len = sizeof(imap);
2866 r = chash_get(courier_workaround_hash, &key, &value);
2868 r = imap_add_envelope_fetch_att(fetch_type);
2870 r = imap_add_header_fetch_att(fetch_type);
2872 mailstream_logger = imap_logger_fetch;
2874 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2876 mailstream_logger = imap_logger_cmd;
2878 case MAILIMAP_NO_ERROR:
2881 mailimap_fetch_type_free(fetch_type);
2882 debug_print("uid_fetch: %d\n", r);
2886 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2887 res = MAILIMAP_ERROR_FETCH;
2888 debug_print("clist_begin = NULL\n");
2892 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2893 mailimap_fetch_list_free(fetch_result);
2895 if (r != MAILIMAP_NO_ERROR) {
2896 mailimap_fetch_type_free(fetch_type);
2897 res = MAILIMAP_ERROR_MEMORY;
2898 debug_print("fetch_result_to_envelop_list: %d\n", res);
2902 mailimap_fetch_type_free(fetch_type);
2904 * p_env_list = env_list;
2906 return MAILIMAP_NO_ERROR;
2912 struct fetch_env_param {
2914 struct mailimap_set * set;
2917 struct fetch_env_result {
2918 carray * fetch_env_result;
2922 static void fetch_env_run(struct etpan_thread_op * op)
2924 struct fetch_env_param * param;
2925 struct fetch_env_result * result;
2930 result = op->result;
2935 r = imap_get_envelopes_list(param->imap, param->set,
2939 result->fetch_env_result = env_list;
2941 debug_print("imap fetch_env run - end %i\n", r);
2944 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2945 carray ** p_env_list)
2947 struct fetch_env_param param;
2948 struct fetch_env_result result;
2951 debug_print("imap fetch_env - begin\n");
2953 imap = get_imap(folder);
2957 threaded_run(folder, ¶m, &result, fetch_env_run);
2959 if (result.error != MAILIMAP_NO_ERROR) {
2965 key.len = sizeof(imap);
2966 r = chash_get(courier_workaround_hash, &key, &value);
2970 chash_set(courier_workaround_hash, &key, &value, NULL);
2972 threaded_run(folder, ¶m, &result, fetch_env_run);
2976 if (result.error != MAILIMAP_NO_ERROR)
2977 return result.error;
2979 debug_print("imap fetch_env - end\n");
2981 * p_env_list = result.fetch_env_result;
2983 return result.error;
2986 void imap_fetch_env_free(carray * env_list)
2990 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2991 struct imap_fetch_env_info * env_info;
2993 env_info = carray_get(env_list, i);
2994 free(env_info->headers);
2997 carray_free(env_list);
3004 struct append_param {
3006 const char * mailbox;
3007 const char * filename;
3008 struct mailimap_flag_list * flag_list;
3011 struct append_result {
3016 static void append_run(struct etpan_thread_op * op)
3018 struct append_param * param;
3019 struct append_result * result;
3024 struct stat stat_buf;
3027 guint32 uid = 0, val = 0;
3030 result = op->result;
3035 r = stat(param->filename, &stat_buf);
3037 result->error = MAILIMAP_ERROR_APPEND;
3040 size = stat_buf.st_size;
3042 fd = g_open(param->filename, O_RDONLY, 0);
3044 result->error = MAILIMAP_ERROR_APPEND;
3048 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
3049 if (data == (void *) MAP_FAILED) {
3051 result->error = MAILIMAP_ERROR_APPEND;
3055 data = file_read_to_str_no_recode(param->filename);
3057 result->error = MAILIMAP_ERROR_APPEND;
3060 size = strlen(data);
3062 mailstream_logger = imap_logger_append;
3064 r = mailimap_uidplus_append(param->imap, param->mailbox,
3065 param->flag_list, NULL,
3066 data, size, &val, &uid);
3068 mailstream_logger = imap_logger_cmd;
3079 debug_print("imap append run - end %i uid %d\n", r, uid);
3082 int imap_threaded_append(Folder * folder, const char * mailbox,
3083 const char * filename,
3084 struct mailimap_flag_list * flag_list,
3087 struct append_param param;
3088 struct append_result result;
3091 debug_print("imap append - begin\n");
3093 imap = get_imap(folder);
3095 param.mailbox = mailbox;
3096 param.filename = filename;
3097 param.flag_list = flag_list;
3099 threaded_run(folder, ¶m, &result, append_run);
3101 if (result.error != MAILIMAP_NO_ERROR)
3102 return result.error;
3104 debug_print("imap append - end\n");
3108 return result.error;
3114 struct expunge_param {
3118 struct expunge_result {
3122 static void expunge_run(struct etpan_thread_op * op)
3124 struct expunge_param * param;
3125 struct expunge_result * result;
3129 result = op->result;
3133 r = mailimap_expunge(param->imap);
3136 debug_print("imap expunge run - end %i\n", r);
3139 int imap_threaded_expunge(Folder * folder)
3141 struct expunge_param param;
3142 struct expunge_result result;
3144 debug_print("imap expunge - begin\n");
3146 param.imap = get_imap(folder);
3148 threaded_run(folder, ¶m, &result, expunge_run);
3150 debug_print("imap expunge - end\n");
3152 return result.error;
3158 struct mailimap_set * set;
3162 struct copy_result {
3164 struct mailimap_set *source;
3165 struct mailimap_set *dest;
3168 static void copy_run(struct etpan_thread_op * op)
3170 struct copy_param * param;
3171 struct copy_result * result;
3174 struct mailimap_set *source = NULL, *dest = NULL;
3177 result = op->result;
3181 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3182 &val, &source, &dest);
3186 result->source = source;
3187 result->dest = dest;
3189 result->source = NULL;
3190 result->dest = NULL;
3192 debug_print("imap copy run - end %i\n", r);
3195 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3196 const char * mb, struct mailimap_set **source,
3197 struct mailimap_set **dest)
3199 struct copy_param param;
3200 struct copy_result result;
3203 debug_print("imap copy - begin\n");
3205 imap = get_imap(folder);
3210 threaded_run(folder, ¶m, &result, copy_run);
3214 if (result.error != MAILIMAP_NO_ERROR)
3215 return result.error;
3217 *source = result.source;
3218 *dest = result.dest;
3220 debug_print("imap copy - end\n");
3222 return result.error;
3227 struct store_param {
3229 struct mailimap_set * set;
3230 struct mailimap_store_att_flags * store_att_flags;
3233 struct store_result {
3237 static void store_run(struct etpan_thread_op * op)
3239 struct store_param * param;
3240 struct store_result * result;
3244 result = op->result;
3248 r = mailimap_uid_store(param->imap, param->set,
3249 param->store_att_flags);
3253 debug_print("imap store run - end %i\n", r);
3256 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3257 struct mailimap_store_att_flags * store_att_flags)
3259 struct store_param param;
3260 struct store_result result;
3263 debug_print("imap store - begin\n");
3265 imap = get_imap(folder);
3268 param.store_att_flags = store_att_flags;
3270 threaded_run(folder, ¶m, &result, store_run);
3272 if (result.error != MAILIMAP_NO_ERROR)
3273 return result.error;
3275 debug_print("imap store - end\n");
3277 return result.error;
3281 #define ENV_BUFFER_SIZE 512
3283 static void do_exec_command(int fd, const char * command,
3284 const char * servername, uint16_t port)
3288 char env_buffer[ENV_BUFFER_SIZE];
3292 /* Fork again to become a child of init rather than
3293 the etpan client. */
3298 g_setenv("ETPANSERVER", servername, TRUE);
3300 g_unsetenv("ETPANSERVER");
3305 snprintf(porttext, sizeof(porttext), "%d", port);
3306 g_setenv("ETPANPORT", porttext, TRUE);
3309 g_unsetenv("ETPANPORT");
3312 /* Not a lot we can do if there's an error other than bail. */
3313 if (dup2(fd, 0) == -1)
3315 if (dup2(fd, 1) == -1)
3318 /* Should we close stderr and reopen /dev/null? */
3320 maxopen = sysconf(_SC_OPEN_MAX);
3321 for (i=3; i < maxopen; i++)
3325 /* Detach from the controlling tty if we have one. Otherwise,
3326 SSH might do something stupid like trying to use it instead
3327 of running $SSH_ASKPASS. Doh. */
3328 fd = g_open("/dev/tty", O_RDONLY, 0);
3330 ioctl(fd, TIOCNOTTY, NULL);
3333 #endif /* TIOCNOTTY */
3335 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3337 /* Eep. Shouldn't reach this */
3341 static int subcommand_connect(const char *command,
3342 const char *servername, uint16_t port)
3344 /* SEB unsupported on Windows */
3348 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3353 do_exec_command(sockfds[1], command, servername, port);
3355 else if (childpid == -1) {
3363 /* Reap child, leaving grandchild process to run */
3364 waitpid(childpid, NULL, 0);
3369 static int socket_connect_cmd(mailimap * imap, const char * command,
3370 const char * server, int port)
3376 fd = subcommand_connect(command, server, port);
3378 return MAILIMAP_ERROR_STREAM;
3380 s = mailstream_socket_open(fd);
3383 return MAILIMAP_ERROR_STREAM;
3386 r = mailimap_connect(imap, s);
3387 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3388 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3389 mailstream_close(s);
3391 imap->imap_stream = NULL;
3400 struct connect_cmd_param {
3402 const char * command;
3403 const char * server;
3407 struct connect_cmd_result {
3411 static void connect_cmd_run(struct etpan_thread_op * op)
3414 struct connect_cmd_param * param;
3415 struct connect_cmd_result * result;
3418 result = op->result;
3422 r = socket_connect_cmd(param->imap, param->command,
3423 param->server, param->port);
3429 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3430 const char * server, int port)
3432 struct connect_cmd_param param;
3433 struct connect_cmd_result result;
3436 mailimap * imap, * oldimap;
3438 oldimap = get_imap(folder);
3440 imap = mailimap_new(0, NULL);
3443 debug_print("deleting old imap %p\n", oldimap);
3444 delete_imap(folder, oldimap);
3448 key.len = sizeof(folder);
3451 chash_set(session_hash, &key, &value, NULL);
3454 param.command = command;
3455 param.server = server;
3458 threaded_run(folder, ¶m, &result, connect_cmd_run);
3460 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3462 return result.error;
3464 #endif /* G_OS_WIN32 */
3466 void imap_threaded_cancel(Folder * folder)
3470 imap = get_imap(folder);
3471 if (imap->imap_stream != NULL)
3472 mailstream_cancel(imap->imap_stream);
3477 void imap_main_init(void)
3480 void imap_main_done(gboolean have_connectivity)
3483 void imap_main_set_timeout(int sec)
3487 void imap_threaded_cancel(Folder * folder);