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"
44 #include "etpan-ssl.h"
46 #include "mainwindow.h"
48 #include "ssl_certificate.h"
50 #include "remotefolder.h"
53 #define DISABLE_LOG_DURING_LOGIN
55 static struct etpan_thread_manager * thread_manager = NULL;
56 static chash * courier_workaround_hash = NULL;
57 static chash * imap_hash = NULL;
58 static chash * session_hash = NULL;
59 static guint thread_manager_signal = 0;
60 static GIOChannel * io_channel = NULL;
62 static gboolean thread_manager_event(GIOChannel * source,
63 GIOCondition condition,
70 if (condition & G_IO_IN)
71 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
73 etpan_thread_manager_loop(thread_manager);
78 static void imap_logger_noop(int direction, const char * str, size_t size)
83 static void imap_logger_cmd(int direction, const char * str, size_t size)
90 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
94 memset(buf, 0, size+1);
95 strncpy(buf, str, size);
98 if (!strncmp(buf, "<<<<<<<", 7)
99 || !strncmp(buf, ">>>>>>>", 7)) {
103 while (strstr(buf, "\r"))
104 *strstr(buf, "\r") = ' ';
105 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
106 buf[strlen(buf)-1] = '\0';
108 lines = g_strsplit(buf, "\n", -1);
110 while (lines[i] && *lines[i]) {
111 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
118 static void imap_logger_fetch(int direction, const char * str, size_t size)
124 if (size > 128 && !direction) {
125 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
129 buf = malloc(size+1);
130 memset(buf, 0, size+1);
131 strncpy(buf, str, size);
133 if (!strncmp(buf, "<<<<<<<", 7)
134 || !strncmp(buf, ">>>>>>>", 7)) {
138 while (strstr(buf, "\r"))
139 *strstr(buf, "\r") = ' ';
140 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
141 buf[strlen(buf)-1] = '\0';
143 lines = g_strsplit(buf, "\n", -1);
145 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
146 while (lines[i] && *lines[i]) {
147 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
151 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
157 static void imap_logger_uid(int direction, const char * str, size_t size)
164 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
167 buf = malloc(size+1);
168 memset(buf, 0, size+1);
169 strncpy(buf, str, size);
171 if (!strncmp(buf, "<<<<<<<", 7)
172 || !strncmp(buf, ">>>>>>>", 7)) {
176 while (strstr(buf, "\r"))
177 *strstr(buf, "\r") = ' ';
178 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
179 buf[strlen(buf)-1] = '\0';
181 lines = g_strsplit(buf, "\n", -1);
183 while (lines[i] && *lines[i]) {
184 int llen = strlen(lines[i]);
186 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
189 strncpy2(tmp, lines[i], 63);
190 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
199 static void imap_logger_append(int direction, const char * str, size_t size)
206 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
208 } else if (direction == 0 && size > 64) {
209 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
212 buf = malloc(size+1);
213 memset(buf, 0, size+1);
214 strncpy(buf, str, size);
216 if (!strncmp(buf, "<<<<<<<", 7)
217 || !strncmp(buf, ">>>>>>>", 7)) {
221 while (strstr(buf, "\r"))
222 *strstr(buf, "\r") = ' ';
223 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
224 buf[strlen(buf)-1] = '\0';
226 lines = g_strsplit(buf, "\n", -1);
228 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
229 while (lines[i] && *lines[i]) {
230 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
234 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
240 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
241 gboolean etpan_skip_ssl_cert_check = FALSE;
242 extern void mailsasl_ref(void);
244 void imap_main_init(gboolean skip_ssl_cert_check)
246 int fd_thread_manager;
248 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
249 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
250 mailstream_network_delay.tv_usec = 0;
252 mailstream_debug = 1;
253 mailstream_logger = imap_logger_cmd;
256 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
257 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
258 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
260 thread_manager = etpan_thread_manager_new();
262 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
265 io_channel = g_io_channel_unix_new(fd_thread_manager);
267 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
269 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
270 thread_manager_event,
275 void imap_main_set_timeout(int sec)
277 mailstream_network_delay.tv_sec = sec;
278 mailstream_network_delay.tv_usec = 0;
281 void imap_main_done(gboolean have_connectivity)
283 imap_disconnect_all(have_connectivity);
284 etpan_thread_manager_stop(thread_manager);
285 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
288 etpan_thread_manager_join(thread_manager);
290 g_source_remove(thread_manager_signal);
291 g_io_channel_unref(io_channel);
293 etpan_thread_manager_free(thread_manager);
295 chash_free(courier_workaround_hash);
296 chash_free(session_hash);
297 chash_free(imap_hash);
300 void imap_init(Folder * folder)
302 struct etpan_thread * thread;
306 thread = etpan_thread_manager_get_thread(thread_manager);
309 key.len = sizeof(folder);
313 chash_set(imap_hash, &key, &value, NULL);
316 void imap_done(Folder * folder)
318 struct etpan_thread * thread;
324 key.len = sizeof(folder);
326 r = chash_get(imap_hash, &key, &value);
332 etpan_thread_unbind(thread);
334 chash_delete(imap_hash, &key, NULL);
336 debug_print("remove thread");
339 static struct etpan_thread * get_thread(Folder * folder)
341 struct etpan_thread * thread;
347 key.len = sizeof(folder);
349 r = chash_get(imap_hash, &key, &value);
358 static mailimap * get_imap(Folder * folder)
366 key.len = sizeof(folder);
368 r = chash_get(session_hash, &key, &value);
373 debug_print("found imap %p\n", imap);
377 static gboolean cb_show_error(gpointer data)
379 mainwindow_show_error();
383 static void generic_cb(int cancelled, void * result, void * callback_data)
385 struct etpan_thread_op * op;
387 op = (struct etpan_thread_op *) callback_data;
389 debug_print("generic_cb\n");
390 if (op->imap && op->imap->imap_response_info &&
391 op->imap->imap_response_info->rsp_alert) {
392 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
393 op->imap->imap_response_info->rsp_alert);
394 g_timeout_add(10, cb_show_error, NULL);
399 /* Please do *not* blindly use imap pointers after this function returns,
400 * someone may have deleted it while this function was waiting for completion.
401 * Check return value to see if imap is still valid.
402 * Run get_imap(folder) again to get a fresh and valid pointer.
404 static int threaded_run(Folder * folder, void * param, void * result,
405 void (* func)(struct etpan_thread_op * ))
407 struct etpan_thread_op * op;
408 struct etpan_thread * thread;
409 struct mailimap * imap = get_imap(folder);
411 imap_folder_ref(folder);
413 op = etpan_thread_op_new();
420 op->callback = generic_cb;
421 op->callback_data = op;
423 thread = get_thread(folder);
424 etpan_thread_op_schedule(thread, op);
426 while (!op->finished) {
427 gtk_main_iteration();
430 etpan_thread_op_free(op);
432 imap_folder_unref(folder);
434 if (imap != get_imap(folder)) {
435 g_warning("returning from operation on a stale imap %p", imap);
445 struct connect_param {
447 PrefsAccount *account;
452 struct connect_result {
456 #define CHECK_IMAP() { \
457 if (!param->imap) { \
458 result->error = MAILIMAP_ERROR_BAD_STATE; \
464 static void delete_imap_run(struct etpan_thread_op * op)
466 mailimap * imap = op->imap;
468 /* we don't want libetpan to logout */
469 if (imap->imap_stream) {
470 mailstream_close(imap->imap_stream);
471 imap->imap_stream = NULL;
477 static void threaded_delete_imap(Folder *folder, mailimap *imap)
479 struct etpan_thread_op * op;
481 /* No need to wait for completion, threaded_run() won't work here. */
482 op = etpan_thread_op_new();
484 op->run = delete_imap_run;
485 op->cleanup = etpan_thread_op_free;
487 etpan_thread_op_schedule(get_thread(folder), op);
489 debug_print("threaded delete imap posted\n");
492 static void delete_imap(Folder *folder, mailimap *imap)
497 key.len = sizeof(folder);
498 chash_delete(session_hash, &key, NULL);
503 key.len = sizeof(imap);
504 chash_delete(courier_workaround_hash, &key, NULL);
505 /* We can't just free imap here as there may be ops on it pending
506 * in the thread. Posting freeing as an op will synchronize against
507 * existing jobs and as imap is already removed from session_hash
508 * we are sure no new ops can be posted. */
509 threaded_delete_imap(folder, imap);
512 static void connect_run(struct etpan_thread_op * op)
515 struct connect_param * param;
516 struct connect_result * result;
523 r = mailimap_socket_connect(param->imap,
524 param->server, param->port);
530 int imap_threaded_connect(Folder * folder, const char * server, int port)
532 struct connect_param param;
533 struct connect_result result;
536 mailimap * imap, * oldimap;
538 oldimap = get_imap(folder);
540 imap = mailimap_new(0, NULL);
543 debug_print("deleting old imap %p\n", oldimap);
544 delete_imap(folder, oldimap);
548 key.len = sizeof(folder);
551 chash_set(session_hash, &key, &value, NULL);
554 param.server = server;
558 threaded_run(folder, ¶m, &result, connect_run);
560 debug_print("connect ok %i with imap %p\n", result.error, imap);
565 static void connect_ssl_run(struct etpan_thread_op * op)
568 struct connect_param * param;
569 struct connect_result * result;
576 r = mailimap_ssl_connect_with_callback(param->imap,
577 param->server, param->port,
578 etpan_connect_ssl_context_cb, param->account);
582 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
584 struct connect_param param;
585 struct connect_result result;
588 mailimap * imap, * oldimap;
589 gboolean accept_if_valid = FALSE;
591 oldimap = get_imap(folder);
593 imap = mailimap_new(0, NULL);
596 debug_print("deleting old imap %p\n", oldimap);
597 delete_imap(folder, oldimap);
601 key.len = sizeof(folder);
604 chash_set(session_hash, &key, &value, NULL);
607 param.server = server;
609 param.account = folder->account;
612 accept_if_valid = folder->account->ssl_certs_auto_accept;
615 if (threaded_run(folder, ¶m, &result, connect_ssl_run))
616 return MAILIMAP_ERROR_INVAL;
618 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
619 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
620 if (etpan_certificate_check(imap->imap_stream, server, port,
621 accept_if_valid) != TRUE)
622 result.error = MAILIMAP_ERROR_SSL;
624 debug_print("connect %d with imap %p\n", result.error, imap);
635 struct mailimap_capability_data *caps;
638 static void capability_run(struct etpan_thread_op * op)
641 struct capa_param * param;
642 struct capa_result * result;
643 struct mailimap_capability_data *caps;
650 r = mailimap_capability(param->imap, &caps);
653 result->caps = (r == 0 ? caps : NULL);
657 int imap_threaded_capability(Folder *folder, struct mailimap_capability_data ** caps)
659 struct capa_param param;
660 struct capa_result result;
663 imap = get_imap(folder);
667 threaded_run(folder, ¶m, &result, capability_run);
669 debug_print("capa %d\n", result.error);
671 if (result.error == MAILIMAP_NO_ERROR)
678 struct disconnect_param {
682 struct disconnect_result {
686 static void disconnect_run(struct etpan_thread_op * op)
689 struct disconnect_param * param;
690 struct disconnect_result * result;
697 r = mailimap_logout(param->imap);
702 void imap_threaded_disconnect(Folder * folder)
704 struct connect_param param;
705 struct connect_result result;
708 imap = get_imap(folder);
710 debug_print("was disconnected\n");
716 if (threaded_run(folder, ¶m, &result, disconnect_run)) {
717 debug_print("imap already deleted %p\n", imap);
719 debug_print("deleting old imap %p\n", imap);
720 delete_imap(folder, imap);
723 debug_print("disconnect ok\n");
730 const char * wildcard;
739 static void list_run(struct etpan_thread_op * op)
741 struct list_param * param;
742 struct list_result * result;
753 if (param->base == NULL || param->wildcard == NULL) {
756 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
760 r = mailimap_lsub(param->imap, param->base,
761 param->wildcard, &list);
763 r = mailimap_list(param->imap, param->base,
764 param->wildcard, &list);
767 debug_print("imap list run - end\n");
770 int imap_threaded_list(Folder * folder, const char * base,
771 const char * wildcard,
774 struct list_param param;
775 struct list_result result;
777 debug_print("imap list - begin\n");
779 param.imap = get_imap(folder);
781 param.wildcard = wildcard;
782 param.sub_only = FALSE;
784 threaded_run(folder, ¶m, &result, list_run);
786 * p_result = result.list;
788 debug_print("imap list - end %p\n", result.list);
793 int imap_threaded_lsub(Folder * folder, const char * base,
794 const char * wildcard,
797 struct list_param param;
798 struct list_result result;
800 debug_print("imap lsub - begin\n");
802 param.imap = get_imap(folder);
804 param.wildcard = wildcard;
805 param.sub_only = TRUE;
807 threaded_run(folder, ¶m, &result, list_run);
809 * p_result = result.list;
811 debug_print("imap lsub - end %p\n", result.list);
816 struct subscribe_param {
822 struct subscribe_result {
826 static void subscribe_run(struct etpan_thread_op * op)
828 struct subscribe_param * param;
829 struct subscribe_result * result;
837 if (param->mb == NULL) {
839 debug_print("no mb\n");
842 if (param->subscribe)
843 r = mailimap_subscribe(param->imap, param->mb);
845 r = mailimap_unsubscribe(param->imap, param->mb);
847 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
850 int imap_threaded_subscribe(Folder * folder, const char * mb,
853 struct subscribe_param param;
854 struct subscribe_result result;
856 debug_print("imap list - begin\n");
858 param.imap = get_imap(folder);
860 param.subscribe = subscribe;
862 threaded_run(folder, ¶m, &result, subscribe_run);
870 const char * password;
875 struct login_result {
879 static void login_run(struct etpan_thread_op * op)
881 struct login_param * param;
882 struct login_result * result;
884 #ifdef DISABLE_LOG_DURING_LOGIN
893 #ifdef DISABLE_LOG_DURING_LOGIN
894 old_debug = mailstream_debug;
895 mailstream_debug = 0;
897 if (!strcmp(param->type, "LOGIN"))
898 r = mailimap_login(param->imap,
899 param->login, param->password);
900 else if (!strcmp(param->type, "GSSAPI"))
901 r = mailimap_authenticate(param->imap,
902 param->type, param->server, NULL, NULL,
903 param->login, param->login,
904 param->password, NULL);
905 else if (!strcmp(param->type, "SCRAM-SHA-1"))
906 /* 7th argument has to be NULL here, to stop libetpan sending the
907 * a= attribute in its initial SCRAM-SHA-1 message to server. At least
908 * Dovecot 2.2 doesn't seem to like that, and will not authenticate
910 r = mailimap_authenticate(param->imap,
911 param->type, NULL, NULL, NULL,
913 param->password, NULL);
915 r = mailimap_authenticate(param->imap,
916 param->type, NULL, NULL, NULL,
917 param->login, param->login,
918 param->password, NULL);
919 #ifdef DISABLE_LOG_DURING_LOGIN
920 mailstream_debug = old_debug;
924 if (param->imap->imap_response)
925 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
926 debug_print("imap login run - end %i\n", r);
929 int imap_threaded_login(Folder * folder,
930 const char * login, const char * password,
933 struct login_param param;
934 struct login_result result;
936 debug_print("imap login - begin\n");
939 return MAILIMAP_ERROR_INVAL;
941 param.imap = get_imap(folder);
943 param.password = password;
945 if (folder && folder->account)
946 param.server = folder->account->recv_server;
950 threaded_run(folder, ¶m, &result, login_run);
952 debug_print("imap login - end\n");
958 struct status_param {
961 struct mailimap_status_att_list * status_att_list;
964 struct status_result {
966 struct mailimap_mailbox_data_status * data_status;
969 static void status_run(struct etpan_thread_op * op)
971 struct status_param * param;
972 struct status_result * result;
980 r = mailimap_status(param->imap, param->mb,
981 param->status_att_list,
982 &result->data_status);
985 debug_print("imap status run - end %i\n", r);
988 int imap_threaded_status(Folder * folder, const char * mb,
989 struct mailimap_mailbox_data_status ** data_status,
992 struct status_param param;
993 struct status_result result;
994 struct mailimap_status_att_list * status_att_list;
996 debug_print("imap status - begin\n");
998 status_att_list = mailimap_status_att_list_new_empty();
1000 mailimap_status_att_list_add(status_att_list,
1001 MAILIMAP_STATUS_ATT_MESSAGES);
1003 if (mask & 1 << 1) {
1004 mailimap_status_att_list_add(status_att_list,
1005 MAILIMAP_STATUS_ATT_RECENT);
1007 if (mask & 1 << 2) {
1008 mailimap_status_att_list_add(status_att_list,
1009 MAILIMAP_STATUS_ATT_UIDNEXT);
1011 if (mask & 1 << 3) {
1012 mailimap_status_att_list_add(status_att_list,
1013 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1015 if (mask & 1 << 4) {
1016 mailimap_status_att_list_add(status_att_list,
1017 MAILIMAP_STATUS_ATT_UNSEEN);
1019 param.imap = get_imap(folder);
1021 param.status_att_list = status_att_list;
1023 threaded_run(folder, ¶m, &result, status_run);
1025 debug_print("imap status - end\n");
1027 * data_status = result.data_status;
1029 mailimap_status_att_list_free(status_att_list);
1031 return result.error;
1040 struct noop_result {
1044 static void noop_run(struct etpan_thread_op * op)
1046 struct noop_param * param;
1047 struct noop_result * result;
1051 result = op->result;
1055 r = mailimap_noop(param->imap);
1058 debug_print("imap noop run - end %i\n", r);
1061 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1062 unsigned int *p_recent,
1063 unsigned int *p_expunge,
1064 unsigned int *p_unseen,
1065 unsigned int *p_uidnext,
1066 unsigned int *p_uidval)
1068 struct noop_param param;
1069 struct noop_result result;
1072 debug_print("imap noop - begin\n");
1074 imap = get_imap(folder);
1077 if (threaded_run(folder, ¶m, &result, noop_run))
1078 return MAILIMAP_ERROR_INVAL;
1080 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1081 * p_exists = imap->imap_selection_info->sel_exists;
1082 * p_recent = imap->imap_selection_info->sel_recent;
1083 * p_unseen = imap->imap_selection_info->sel_unseen;
1084 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1085 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1093 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1094 imap->imap_response_info->rsp_expunged != NULL) {
1095 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1099 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1100 *p_exists, *p_recent, *p_expunge, *p_unseen,
1101 *p_uidnext, *p_uidval);
1103 return result.error;
1107 struct starttls_result {
1111 static void starttls_run(struct etpan_thread_op * op)
1113 struct connect_param * param;
1114 struct starttls_result * result;
1118 result = op->result;
1122 r = mailimap_starttls(param->imap);
1125 debug_print("imap starttls run - end %i\n", r);
1128 mailimap *imap = param->imap;
1129 mailstream_low *plain_low = NULL;
1130 mailstream_low *tls_low = NULL;
1133 plain_low = mailstream_get_low(imap->imap_stream);
1134 fd = mailstream_low_get_fd(plain_low);
1136 debug_print("imap starttls run - can't get fd\n");
1137 result->error = MAILIMAP_ERROR_STREAM;
1141 tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
1142 if (tls_low == NULL) {
1143 debug_print("imap starttls run - can't tls_open\n");
1144 result->error = MAILIMAP_ERROR_STREAM;
1147 mailstream_low_free(plain_low);
1148 mailstream_set_low(imap->imap_stream, tls_low);
1152 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1154 struct connect_param param;
1155 struct starttls_result result;
1156 gboolean accept_if_valid = FALSE;
1158 debug_print("imap starttls - begin\n");
1160 param.imap = get_imap(folder);
1161 param.server = host;
1163 param.account = folder->account;
1165 if (folder->account)
1166 accept_if_valid = folder->account->ssl_certs_auto_accept;
1168 if (threaded_run(folder, ¶m, &result, starttls_run))
1169 return MAILIMAP_ERROR_INVAL;
1171 debug_print("imap starttls - end\n");
1173 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1174 if (etpan_certificate_check(param.imap->imap_stream, host, port,
1175 accept_if_valid) != TRUE)
1176 return MAILIMAP_ERROR_SSL;
1178 return result.error;
1182 struct create_param {
1187 struct create_result {
1191 static void create_run(struct etpan_thread_op * op)
1193 struct create_param * param;
1194 struct create_result * result;
1198 result = op->result;
1202 r = mailimap_create(param->imap, param->mb);
1205 debug_print("imap create run - end %i\n", r);
1208 int imap_threaded_create(Folder * folder, const char * mb)
1210 struct create_param param;
1211 struct create_result result;
1213 debug_print("imap create - begin\n");
1215 param.imap = get_imap(folder);
1218 threaded_run(folder, ¶m, &result, create_run);
1220 debug_print("imap create - end\n");
1222 return result.error;
1228 struct rename_param {
1231 const char * new_name;
1234 struct rename_result {
1238 static void rename_run(struct etpan_thread_op * op)
1240 struct rename_param * param;
1241 struct rename_result * result;
1245 result = op->result;
1249 r = mailimap_rename(param->imap, param->mb, param->new_name);
1252 debug_print("imap rename run - end %i\n", r);
1255 int imap_threaded_rename(Folder * folder,
1256 const char * mb, const char * new_name)
1258 struct rename_param param;
1259 struct rename_result result;
1261 debug_print("imap rename - begin\n");
1263 param.imap = get_imap(folder);
1265 param.new_name = new_name;
1267 threaded_run(folder, ¶m, &result, rename_run);
1269 debug_print("imap rename - end\n");
1271 return result.error;
1277 struct delete_param {
1282 struct delete_result {
1286 static void delete_run(struct etpan_thread_op * op)
1288 struct delete_param * param;
1289 struct delete_result * result;
1293 result = op->result;
1297 r = mailimap_delete(param->imap, param->mb);
1300 debug_print("imap delete run - end %i\n", r);
1303 int imap_threaded_delete(Folder * folder, const char * mb)
1305 struct delete_param param;
1306 struct delete_result result;
1308 debug_print("imap delete - begin\n");
1310 param.imap = get_imap(folder);
1313 threaded_run(folder, ¶m, &result, delete_run);
1315 debug_print("imap delete - end\n");
1317 return result.error;
1322 struct select_param {
1327 struct select_result {
1331 static void select_run(struct etpan_thread_op * op)
1333 struct select_param * param;
1334 struct select_result * result;
1338 result = op->result;
1342 r = mailimap_select(param->imap, param->mb);
1345 debug_print("imap select run - end %i\n", r);
1348 int imap_threaded_select(Folder * folder, const char * mb,
1349 gint * exists, gint * recent, gint * unseen,
1350 guint32 * uid_validity,gint *can_create_flags,
1353 struct select_param param;
1354 struct select_result result;
1357 debug_print("imap select - begin\n");
1359 imap = get_imap(folder);
1363 if (threaded_run(folder, ¶m, &result, select_run))
1364 return MAILIMAP_ERROR_INVAL;
1366 if (result.error != MAILIMAP_NO_ERROR)
1367 return result.error;
1369 if (!imap || imap->imap_selection_info == NULL)
1370 return MAILIMAP_ERROR_PARSE;
1372 * exists = imap->imap_selection_info->sel_exists;
1373 * recent = imap->imap_selection_info->sel_recent;
1374 * unseen = imap->imap_selection_info->sel_unseen;
1375 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1376 * can_create_flags = FALSE;
1378 if (imap->imap_selection_info->sel_perm_flags) {
1379 GSList *t_flags = NULL;
1380 clistiter *cur = NULL;
1381 if (imap->imap_selection_info->sel_perm_flags)
1382 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1384 for (; cur; cur = clist_next(cur)) {
1385 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1386 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1387 *can_create_flags = TRUE;
1388 else if (flag->fl_flag &&
1389 flag->fl_flag->fl_type == 6 &&
1390 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1391 *can_create_flags = TRUE;
1392 if (flag->fl_flag && ok_flags) {
1393 MsgPermFlags c_flag = 0;
1394 switch (flag->fl_flag->fl_type) {
1395 case MAILIMAP_FLAG_ANSWERED:
1396 c_flag = IMAP_FLAG_ANSWERED;
1398 case MAILIMAP_FLAG_FLAGGED:
1399 c_flag = IMAP_FLAG_FLAGGED;
1401 case MAILIMAP_FLAG_DELETED:
1402 c_flag = IMAP_FLAG_DELETED;
1404 case MAILIMAP_FLAG_DRAFT:
1405 c_flag = IMAP_FLAG_DRAFT;
1407 case MAILIMAP_FLAG_SEEN:
1408 c_flag = IMAP_FLAG_SEEN;
1410 case MAILIMAP_FLAG_KEYWORD:
1411 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1412 c_flag = IMAP_FLAG_FORWARDED;
1413 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1414 c_flag = IMAP_FLAG_SPAM;
1415 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1416 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1417 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1418 c_flag = IMAP_FLAG_HAM;
1424 t_flags = g_slist_prepend(t_flags,
1425 GUINT_TO_POINTER(c_flag));
1430 *ok_flags = t_flags;
1432 debug_print("imap select - end\n");
1434 return result.error;
1437 static void close_run(struct etpan_thread_op * op)
1439 struct select_param * param;
1440 struct select_result * result;
1444 result = op->result;
1448 r = mailimap_close(param->imap);
1451 debug_print("imap close run - end %i\n", r);
1454 int imap_threaded_close(Folder * folder)
1456 struct select_param param;
1457 struct select_result result;
1460 debug_print("imap close - begin\n");
1462 imap = get_imap(folder);
1465 threaded_run(folder, ¶m, &result, close_run);
1467 if (result.error != MAILIMAP_NO_ERROR)
1468 return result.error;
1470 debug_print("imap close - end\n");
1472 return result.error;
1475 struct examine_param {
1480 struct examine_result {
1484 static void examine_run(struct etpan_thread_op * op)
1486 struct examine_param * param;
1487 struct examine_result * result;
1491 result = op->result;
1495 r = mailimap_examine(param->imap, param->mb);
1498 debug_print("imap examine run - end %i\n", r);
1501 int imap_threaded_examine(Folder * folder, const char * mb,
1502 gint * exists, gint * recent, gint * unseen,
1503 guint32 * uid_validity)
1505 struct examine_param param;
1506 struct examine_result result;
1509 debug_print("imap examine - begin\n");
1511 imap = get_imap(folder);
1515 if (threaded_run(folder, ¶m, &result, examine_run))
1516 return MAILIMAP_ERROR_INVAL;
1518 if (result.error != MAILIMAP_NO_ERROR)
1519 return result.error;
1521 if (!imap || imap->imap_selection_info == NULL)
1522 return MAILIMAP_ERROR_PARSE;
1524 * exists = imap->imap_selection_info->sel_exists;
1525 * recent = imap->imap_selection_info->sel_recent;
1526 * unseen = imap->imap_selection_info->sel_unseen;
1527 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1529 debug_print("imap examine - end\n");
1531 return result.error;
1537 struct search_param {
1540 const char *charset;
1541 struct mailimap_set * set;
1545 struct search_result {
1547 clist * search_result;
1550 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1552 return mailimap_set_item_new(orig->set_first, orig->set_last);
1555 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1557 clist *list = orig ? orig->set_list : NULL;
1564 newlist = clist_new();
1568 for (cur = clist_begin(list); cur; cur = clist_next(cur)) {
1569 if (clist_append(newlist,
1570 sc_mailimap_set_item_copy(
1571 (struct mailimap_set_item *)clist_content(cur))) != 0) {
1572 clist_free(newlist);
1576 return mailimap_set_new(newlist);
1579 static void search_run(struct etpan_thread_op * op)
1581 struct search_param * param;
1582 struct search_result * result;
1584 struct mailimap_search_key * key = NULL;
1585 struct mailimap_search_key * uid_key = NULL;
1586 struct mailimap_search_key * search_type_key = NULL;
1587 clist * search_result;
1590 result = op->result;
1594 /* we copy the mailimap_set because freeing the key is recursive */
1595 if (param->set != NULL) {
1596 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1597 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1598 uid_key = mailimap_search_key_new_all();
1600 switch (param->type) {
1601 case IMAP_SEARCH_TYPE_SIMPLE:
1602 search_type_key = NULL;
1604 case IMAP_SEARCH_TYPE_SEEN:
1605 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1607 case IMAP_SEARCH_TYPE_UNSEEN:
1608 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1610 case IMAP_SEARCH_TYPE_ANSWERED:
1611 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1613 case IMAP_SEARCH_TYPE_FLAGGED:
1614 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1616 case IMAP_SEARCH_TYPE_DELETED:
1617 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1619 case IMAP_SEARCH_TYPE_FORWARDED:
1620 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1622 case IMAP_SEARCH_TYPE_SPAM:
1623 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1625 case IMAP_SEARCH_TYPE_KEYED:
1626 search_type_key = param->key;
1630 if (search_type_key != NULL) {
1631 if (param->set != NULL) {
1632 key = mailimap_search_key_new_multiple_empty();
1633 mailimap_search_key_multiple_add(key, search_type_key);
1634 mailimap_search_key_multiple_add(key, uid_key);
1636 key = search_type_key;
1638 } else if (uid_key != NULL) {
1643 g_warning("no key!");
1644 result = op->result;
1646 result->search_result = NULL;
1648 mailstream_logger = imap_logger_uid;
1650 r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
1652 mailstream_logger = imap_logger_cmd;
1654 /* free the key (with the imapset) */
1655 mailimap_search_key_free(key);
1658 result->search_result = search_result;
1660 debug_print("imap search run - end %i\n", result->error);
1663 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1664 const char *charset, struct mailimap_set * set,
1665 clist ** search_result)
1667 struct search_param param;
1668 struct search_result result;
1671 debug_print("imap search - begin\n");
1673 imap = get_imap(folder);
1676 param.charset = charset;
1677 param.type = search_type;
1680 threaded_run(folder, ¶m, &result, search_run);
1682 if (result.error != MAILIMAP_NO_ERROR)
1683 return result.error;
1685 debug_print("imap search - end\n");
1687 * search_result = result.search_result;
1689 return result.error;
1693 struct _IMAPSearchKey {
1694 struct mailimap_search_key* key;
1697 IMAPSearchKey* imap_search_new(gint criteria,
1698 const gchar *header,
1702 char* sk_bcc = NULL;
1703 struct mailimap_date* sk_before = NULL;
1704 char* sk_body = NULL;
1706 char* sk_from = NULL;
1707 char* sk_keyword = NULL;
1708 struct mailimap_date* sk_on = NULL;
1709 struct mailimap_date* sk_since = NULL;
1710 char* sk_subject = NULL;
1711 char* sk_text = NULL;
1713 char* sk_unkeyword = NULL;
1714 char* sk_header_name = NULL;
1715 char* sk_header_value = NULL;
1716 uint32_t sk_larger = 0;
1717 struct mailimap_search_key* sk_not = NULL;
1718 struct mailimap_search_key* sk_or1 = NULL;
1719 struct mailimap_search_key* sk_or2 = NULL;
1720 struct mailimap_date* sk_sentbefore = NULL;
1721 struct mailimap_date* sk_senton = NULL;
1722 struct mailimap_date* sk_sentsince = NULL;
1723 uint32_t sk_smaller = 0;
1724 struct mailimap_set* sk_uid = NULL;
1725 struct mailimap_set* sk_set = NULL;
1726 clist* sk_multiple = NULL;
1727 int etpan_matcher_type;
1730 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1731 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1732 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1733 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1734 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1735 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1736 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1738 case IMAP_SEARCH_CRITERIA_TAG:
1739 sk_keyword = strdup(expr);
1740 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1743 case IMAP_SEARCH_CRITERIA_SUBJECT:
1744 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1745 sk_subject = strdup(expr);
1748 case IMAP_SEARCH_CRITERIA_TO:
1749 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1750 sk_to = strdup(expr);
1753 case IMAP_SEARCH_CRITERIA_CC:
1754 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1755 sk_cc = strdup(expr);
1758 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1759 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1762 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1765 localtime_r(&limit, &tm);
1766 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1767 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1768 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1770 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1771 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1776 case IMAP_SEARCH_CRITERIA_BODY:
1777 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1778 sk_body = strdup(expr);
1781 case IMAP_SEARCH_CRITERIA_MESSAGE:
1782 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1783 sk_text = strdup(expr);
1786 case IMAP_SEARCH_CRITERIA_HEADER:
1787 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1788 sk_header_name = strdup(header);
1789 sk_header_value = strdup(expr);
1792 case IMAP_SEARCH_CRITERIA_FROM:
1793 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1794 sk_from = strdup(expr);
1797 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1798 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1802 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1803 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1811 return mailimap_search_key_new(etpan_matcher_type,
1812 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1813 sk_on, sk_since, sk_subject, sk_text, sk_to,
1814 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1815 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1816 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1819 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1821 return mailimap_search_key_new_not(key);
1824 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1826 return mailimap_search_key_new_or(l, r);
1829 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1831 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1832 mailimap_search_key_multiple_add(result, l);
1833 mailimap_search_key_multiple_add(result, r);
1838 void imap_search_free(IMAPSearchKey* key)
1843 mailimap_search_key_free(key);
1848 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1852 struct mailimap_msg_att_dynamic ** patt_dyn);
1855 result_to_uid_list(clist * fetch_result, carray ** result)
1857 clistiter * cur = NULL;
1862 tab = carray_new(128);
1864 res = MAILIMAP_ERROR_MEMORY;
1869 cur = clist_begin(fetch_result);
1871 for(; cur != NULL ; cur = clist_next(cur)) {
1872 struct mailimap_msg_att * msg_att;
1876 msg_att = clist_content(cur);
1879 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1881 puid = malloc(sizeof(* puid));
1883 res = MAILIMAP_ERROR_MEMORY;
1888 r = carray_add(tab, puid, NULL);
1891 res = MAILIMAP_ERROR_MEMORY;
1898 return MAILIMAP_NO_ERROR;
1901 imap_fetch_uid_list_free(tab);
1906 static int imap_get_messages_list(mailimap * imap,
1907 uint32_t first_index,
1912 struct mailimap_fetch_att * fetch_att;
1913 struct mailimap_fetch_type * fetch_type;
1914 struct mailimap_set * set;
1915 clist * fetch_result;
1918 set = mailimap_set_new_interval(first_index, 0);
1920 res = MAILIMAP_ERROR_MEMORY;
1924 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1925 if (fetch_type == NULL) {
1926 res = MAILIMAP_ERROR_MEMORY;
1930 fetch_att = mailimap_fetch_att_new_uid();
1931 if (fetch_att == NULL) {
1932 res = MAILIMAP_ERROR_MEMORY;
1933 goto free_fetch_type;
1936 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1937 if (r != MAILIMAP_NO_ERROR) {
1938 mailimap_fetch_att_free(fetch_att);
1939 res = MAILIMAP_ERROR_MEMORY;
1940 goto free_fetch_type;
1943 mailstream_logger = imap_logger_fetch;
1945 r = mailimap_uid_fetch(imap, set,
1946 fetch_type, &fetch_result);
1948 mailstream_logger = imap_logger_cmd;
1949 mailimap_fetch_type_free(fetch_type);
1950 mailimap_set_free(set);
1952 if (r != MAILIMAP_NO_ERROR) {
1958 r = result_to_uid_list(fetch_result, &env_list);
1959 mailimap_fetch_list_free(fetch_result);
1961 * result = env_list;
1963 return MAILIMAP_NO_ERROR;
1966 mailimap_fetch_type_free(fetch_type);
1968 mailimap_set_free(set);
1976 struct fetch_uid_param {
1978 uint32_t first_index;
1981 struct fetch_uid_result {
1983 carray * fetch_result;
1986 static void fetch_uid_run(struct etpan_thread_op * op)
1988 struct fetch_uid_param * param;
1989 struct fetch_uid_result * result;
1990 carray * fetch_result;
1994 result = op->result;
1998 fetch_result = NULL;
1999 mailstream_logger = imap_logger_noop;
2000 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
2002 r = imap_get_messages_list(param->imap, param->first_index,
2005 mailstream_logger = imap_logger_cmd;
2008 result->fetch_result = fetch_result;
2009 debug_print("imap fetch_uid run - end %i\n", r);
2012 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
2013 carray ** fetch_result)
2015 struct fetch_uid_param param;
2016 struct fetch_uid_result result;
2019 debug_print("imap fetch_uid - begin\n");
2021 imap = get_imap(folder);
2023 param.first_index = first_index;
2025 threaded_run(folder, ¶m, &result, fetch_uid_run);
2027 if (result.error != MAILIMAP_NO_ERROR)
2028 return result.error;
2030 debug_print("imap fetch_uid - end\n");
2032 * fetch_result = result.fetch_result;
2034 return result.error;
2038 void imap_fetch_uid_list_free(carray * uid_list)
2042 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
2045 puid = carray_get(uid_list, i);
2048 carray_free(uid_list);
2054 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
2057 result_to_uid_flags_list(clist * fetch_result, carray ** result)
2059 clistiter * cur = NULL;
2063 GSList *tags = NULL;
2065 tab = carray_new(128);
2067 res = MAILIMAP_ERROR_MEMORY;
2072 cur = clist_begin(fetch_result);
2074 for(; cur != NULL ; cur = clist_next(cur)) {
2075 struct mailimap_msg_att * msg_att;
2078 struct mailimap_msg_att_dynamic * att_dyn;
2084 msg_att = clist_content(cur);
2088 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2091 if (att_dyn == NULL)
2094 flags = imap_flags_to_flags(att_dyn, &tags);
2096 puid = malloc(sizeof(* puid));
2098 res = MAILIMAP_ERROR_MEMORY;
2103 r = carray_add(tab, puid, NULL);
2106 res = MAILIMAP_ERROR_MEMORY;
2109 pflags = malloc(sizeof(* pflags));
2110 if (pflags == NULL) {
2111 res = MAILIMAP_ERROR_MEMORY;
2115 r = carray_add(tab, pflags, NULL);
2118 res = MAILIMAP_ERROR_MEMORY;
2121 r = carray_add(tab, tags, NULL);
2124 res = MAILIMAP_ERROR_MEMORY;
2131 return MAILIMAP_NO_ERROR;
2134 imap_fetch_uid_flags_list_free(tab);
2135 slist_free_strings_full(tags);
2140 static int imap_get_messages_flags_list(mailimap * imap,
2141 uint32_t first_index,
2146 struct mailimap_fetch_att * fetch_att;
2147 struct mailimap_fetch_type * fetch_type;
2148 struct mailimap_set * set;
2149 clist * fetch_result;
2152 set = mailimap_set_new_interval(first_index, 0);
2154 res = MAILIMAP_ERROR_MEMORY;
2158 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2159 if (fetch_type == NULL) {
2160 res = MAILIMAP_ERROR_MEMORY;
2164 fetch_att = mailimap_fetch_att_new_flags();
2165 if (fetch_att == NULL) {
2166 res = MAILIMAP_ERROR_MEMORY;
2167 goto free_fetch_type;
2170 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2171 if (r != MAILIMAP_NO_ERROR) {
2172 mailimap_fetch_att_free(fetch_att);
2173 res = MAILIMAP_ERROR_MEMORY;
2174 goto free_fetch_type;
2177 fetch_att = mailimap_fetch_att_new_uid();
2178 if (fetch_att == NULL) {
2179 res = MAILIMAP_ERROR_MEMORY;
2180 goto free_fetch_type;
2183 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2184 if (r != MAILIMAP_NO_ERROR) {
2185 mailimap_fetch_att_free(fetch_att);
2186 res = MAILIMAP_ERROR_MEMORY;
2187 goto free_fetch_type;
2190 mailstream_logger = imap_logger_fetch;
2192 r = mailimap_uid_fetch(imap, set,
2193 fetch_type, &fetch_result);
2195 mailstream_logger = imap_logger_cmd;
2196 mailimap_fetch_type_free(fetch_type);
2197 mailimap_set_free(set);
2199 if (r != MAILIMAP_NO_ERROR) {
2205 r = result_to_uid_flags_list(fetch_result, &env_list);
2206 mailimap_fetch_list_free(fetch_result);
2208 * result = env_list;
2210 return MAILIMAP_NO_ERROR;
2213 mailimap_fetch_type_free(fetch_type);
2215 mailimap_set_free(set);
2222 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2224 struct fetch_uid_param * param;
2225 struct fetch_uid_result * result;
2226 carray * fetch_result;
2230 result = op->result;
2234 fetch_result = NULL;
2235 r = imap_get_messages_flags_list(param->imap, param->first_index,
2239 result->fetch_result = fetch_result;
2240 debug_print("imap fetch_uid run - end %i\n", r);
2243 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2244 carray ** fetch_result)
2246 struct fetch_uid_param param;
2247 struct fetch_uid_result result;
2250 debug_print("imap fetch_uid - begin\n");
2252 imap = get_imap(folder);
2254 param.first_index = first_index;
2256 mailstream_logger = imap_logger_noop;
2257 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2259 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2261 mailstream_logger = imap_logger_cmd;
2264 if (result.error != MAILIMAP_NO_ERROR)
2265 return result.error;
2267 debug_print("imap fetch_uid - end\n");
2269 * fetch_result = result.fetch_result;
2271 return result.error;
2275 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2279 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2282 data = carray_get(uid_flags_list, i);
2284 data = carray_get(uid_flags_list, i + 1);
2287 carray_free(uid_flags_list);
2292 static int imap_fetch(mailimap * imap,
2295 size_t * result_len)
2298 struct mailimap_set * set;
2299 struct mailimap_fetch_att * fetch_att;
2300 struct mailimap_fetch_type * fetch_type;
2301 clist * fetch_result;
2302 struct mailimap_msg_att * msg_att;
2303 struct mailimap_msg_att_item * msg_att_item;
2308 struct mailimap_section * section;
2310 set = mailimap_set_new_single(msg_index);
2312 res = MAILIMAP_ERROR_MEMORY;
2316 section = mailimap_section_new(NULL);
2317 if (section == NULL) {
2318 res = MAILIMAP_ERROR_MEMORY;
2322 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2323 if (fetch_att == NULL) {
2324 mailimap_section_free(section);
2325 res = MAILIMAP_ERROR_MEMORY;
2329 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2330 if (fetch_type == NULL) {
2331 res = MAILIMAP_ERROR_MEMORY;
2332 goto free_fetch_att;
2335 mailstream_logger = imap_logger_fetch;
2337 r = mailimap_uid_fetch(imap, set,
2338 fetch_type, &fetch_result);
2340 mailstream_logger = imap_logger_cmd;
2342 mailimap_fetch_type_free(fetch_type);
2343 mailimap_set_free(set);
2346 case MAILIMAP_NO_ERROR:
2352 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2353 mailimap_fetch_list_free(fetch_result);
2354 return MAILIMAP_ERROR_FETCH;
2357 msg_att = clist_begin(fetch_result)->data;
2362 if (msg_att->att_list)
2363 cur = clist_begin(msg_att->att_list);
2367 for(; cur != NULL ; cur = clist_next(cur)) {
2368 msg_att_item = clist_content(cur);
2370 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2371 if (msg_att_item->att_data.att_static->att_type ==
2372 MAILIMAP_MSG_ATT_BODY_SECTION) {
2373 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2375 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2377 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2382 mailimap_fetch_list_free(fetch_result);
2385 return MAILIMAP_ERROR_FETCH;
2388 * result_len = text_length;
2390 return MAILIMAP_NO_ERROR;
2393 mailimap_fetch_att_free(fetch_att);
2395 mailimap_set_free(set);
2400 static int imap_fetch_header(mailimap * imap,
2403 size_t * result_len)
2406 struct mailimap_set * set;
2407 struct mailimap_fetch_att * fetch_att;
2408 struct mailimap_fetch_type * fetch_type;
2409 clist * fetch_result;
2410 struct mailimap_msg_att * msg_att;
2411 struct mailimap_msg_att_item * msg_att_item;
2416 struct mailimap_section * section;
2418 set = mailimap_set_new_single(msg_index);
2420 res = MAILIMAP_ERROR_MEMORY;
2424 section = mailimap_section_new_header();
2425 if (section == NULL) {
2426 res = MAILIMAP_ERROR_MEMORY;
2430 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2431 if (fetch_att == NULL) {
2432 mailimap_section_free(section);
2433 res = MAILIMAP_ERROR_MEMORY;
2437 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2438 if (fetch_type == NULL) {
2439 res = MAILIMAP_ERROR_MEMORY;
2440 goto free_fetch_att;
2443 mailstream_logger = imap_logger_fetch;
2445 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2447 mailstream_logger = imap_logger_cmd;
2448 mailimap_fetch_type_free(fetch_type);
2449 mailimap_set_free(set);
2452 case MAILIMAP_NO_ERROR:
2458 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2459 mailimap_fetch_list_free(fetch_result);
2460 return MAILIMAP_ERROR_FETCH;
2463 msg_att = clist_begin(fetch_result)->data;
2468 if (msg_att->att_list)
2469 cur = clist_begin(msg_att->att_list);
2473 for(; cur != NULL ; cur = clist_next(cur)) {
2474 msg_att_item = clist_content(cur);
2476 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2477 if (msg_att_item->att_data.att_static->att_type ==
2478 MAILIMAP_MSG_ATT_BODY_SECTION) {
2479 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2480 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2482 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2487 mailimap_fetch_list_free(fetch_result);
2490 return MAILIMAP_ERROR_FETCH;
2493 * result_len = text_length;
2495 return MAILIMAP_NO_ERROR;
2498 mailimap_fetch_att_free(fetch_att);
2500 mailimap_set_free(set);
2507 struct fetch_content_param {
2510 const char * filename;
2514 struct fetch_content_result {
2518 static void fetch_content_run(struct etpan_thread_op * op)
2520 struct fetch_content_param * param;
2521 struct fetch_content_result * result;
2523 size_t content_size;
2529 result = op->result;
2535 if (param->with_body)
2536 r = imap_fetch(param->imap, param->msg_index,
2537 &content, &content_size);
2539 r = imap_fetch_header(param->imap, param->msg_index,
2540 &content, &content_size);
2544 if (r == MAILIMAP_NO_ERROR) {
2545 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2547 result->error = MAILIMAP_ERROR_FETCH;
2551 f = fdopen(fd, "wb");
2553 result->error = MAILIMAP_ERROR_FETCH;
2557 r = fwrite(content, 1, content_size, f);
2558 if (r < content_size) {
2559 result->error = MAILIMAP_ERROR_FETCH;
2565 result->error = MAILIMAP_ERROR_FETCH;
2576 claws_unlink(param->filename);
2579 /* mmap_string_unref is a simple free in libetpan
2580 * when it has MMAP_UNAVAILABLE defined */
2581 if (mmap_string_unref(content) != 0)
2585 debug_print("imap fetch_content run - end %i\n", result->error);
2588 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2590 const char * filename)
2592 struct fetch_content_param param;
2593 struct fetch_content_result result;
2596 debug_print("imap fetch_content - begin\n");
2598 imap = get_imap(folder);
2600 param.msg_index = msg_index;
2601 param.filename = filename;
2602 param.with_body = with_body;
2604 threaded_run(folder, ¶m, &result, fetch_content_run);
2606 if (result.error != MAILIMAP_NO_ERROR)
2607 return result.error;
2609 debug_print("imap fetch_content - end\n");
2611 return result.error;
2616 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2621 GSList *tags = NULL;
2625 flag_list = att_dyn->att_list;
2626 if (flag_list == NULL)
2629 for(cur = clist_begin(flag_list) ; cur != NULL ;
2630 cur = clist_next(cur)) {
2631 struct mailimap_flag_fetch * flag_fetch;
2633 flag_fetch = clist_content(cur);
2634 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2637 switch (flag_fetch->fl_flag->fl_type) {
2638 case MAILIMAP_FLAG_ANSWERED:
2639 flags |= MSG_REPLIED;
2641 case MAILIMAP_FLAG_FLAGGED:
2642 flags |= MSG_MARKED;
2644 case MAILIMAP_FLAG_DELETED:
2645 flags |= MSG_DELETED;
2647 case MAILIMAP_FLAG_SEEN:
2648 flags &= ~MSG_UNREAD;
2651 case MAILIMAP_FLAG_KEYWORD:
2652 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2653 flags |= MSG_FORWARDED;
2654 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2656 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2657 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2658 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2661 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2671 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2675 struct mailimap_msg_att_dynamic ** patt_dyn)
2677 clistiter * item_cur;
2681 struct mailimap_msg_att_dynamic * att_dyn;
2688 if (msg_att->att_list)
2689 item_cur = clist_begin(msg_att->att_list);
2692 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2693 struct mailimap_msg_att_item * item;
2695 item = clist_content(item_cur);
2697 switch (item->att_type) {
2698 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2699 switch (item->att_data.att_static->att_type) {
2700 case MAILIMAP_MSG_ATT_UID:
2701 uid = item->att_data.att_static->att_data.att_uid;
2704 case MAILIMAP_MSG_ATT_BODY_SECTION:
2705 if (headers == NULL) {
2706 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2709 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2710 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2715 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2716 if (att_dyn == NULL) {
2717 att_dyn = item->att_data.att_dyn;
2725 if (pheaders != NULL)
2726 * pheaders = headers;
2727 if (pref_size != NULL)
2728 * pref_size = ref_size;
2729 if (patt_dyn != NULL)
2730 * patt_dyn = att_dyn;
2732 return MAIL_NO_ERROR;
2735 static struct imap_fetch_env_info *
2736 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2738 struct imap_fetch_env_info * info;
2742 struct mailimap_msg_att_dynamic * att_dyn;
2744 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2749 info = malloc(sizeof(* info));
2751 info->headers = strdup(headers);
2753 info->flags = imap_flags_to_flags(att_dyn, tags);
2759 imap_fetch_result_to_envelop_list(clist * fetch_result,
2760 carray ** p_env_list)
2766 env_list = carray_new(16);
2768 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2769 cur = clist_next(cur)) {
2770 struct mailimap_msg_att * msg_att;
2771 struct imap_fetch_env_info * env_info;
2772 GSList *tags = NULL;
2774 msg_att = clist_content(cur);
2776 env_info = fetch_to_env_info(msg_att, &tags);
2778 || carray_add(env_list, env_info, NULL) != 0
2779 || carray_add(env_list, tags, NULL) != 0) {
2780 carray_free(env_list);
2781 return MAILIMAP_ERROR_MEMORY;
2784 * p_env_list = env_list;
2786 * p_env_list = NULL;
2789 return MAIL_NO_ERROR;
2792 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2794 struct mailimap_fetch_att * fetch_att;
2798 struct mailimap_header_list * imap_hdrlist;
2799 struct mailimap_section * section;
2801 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2802 "References", "In-Reply-To", NULL
2805 hdrlist = clist_new();
2807 return MAIL_ERROR_MEMORY;
2809 while (headers[i] != NULL) {
2810 header = strdup(headers[i]);
2811 if (header == NULL || clist_append(hdrlist, header) != 0) {
2812 clist_free(hdrlist);
2813 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 if (threaded_run(folder, ¶m, &result, fetch_env_run))
2960 return MAILIMAP_ERROR_INVAL;
2962 if (result.error != MAILIMAP_NO_ERROR) {
2968 key.len = sizeof(imap);
2969 r = chash_get(courier_workaround_hash, &key, &value);
2973 chash_set(courier_workaround_hash, &key, &value, NULL);
2975 threaded_run(folder, ¶m, &result, fetch_env_run);
2979 if (result.error != MAILIMAP_NO_ERROR)
2980 return result.error;
2982 debug_print("imap fetch_env - end\n");
2984 * p_env_list = result.fetch_env_result;
2986 return result.error;
2989 void imap_fetch_env_free(carray * env_list)
2993 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2994 struct imap_fetch_env_info * env_info;
2996 env_info = carray_get(env_list, i);
2997 free(env_info->headers);
3000 carray_free(env_list);
3007 struct append_param {
3009 const char * mailbox;
3010 const char * filename;
3011 struct mailimap_flag_list * flag_list;
3014 struct append_result {
3019 static void append_run(struct etpan_thread_op * op)
3021 struct append_param * param;
3022 struct append_result * result;
3027 struct stat stat_buf;
3030 guint32 uid = 0, val = 0;
3033 result = op->result;
3038 r = stat(param->filename, &stat_buf);
3040 result->error = MAILIMAP_ERROR_APPEND;
3043 size = stat_buf.st_size;
3045 fd = g_open(param->filename, O_RDONLY, 0);
3047 result->error = MAILIMAP_ERROR_APPEND;
3051 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
3052 if (data == (void *) MAP_FAILED) {
3054 result->error = MAILIMAP_ERROR_APPEND;
3058 data = file_read_to_str_no_recode(param->filename);
3060 result->error = MAILIMAP_ERROR_APPEND;
3063 size = strlen(data);
3065 mailstream_logger = imap_logger_append;
3067 r = mailimap_uidplus_append(param->imap, param->mailbox,
3068 param->flag_list, NULL,
3069 data, size, &val, &uid);
3071 mailstream_logger = imap_logger_cmd;
3082 debug_print("imap append run - end %i uid %d\n", r, uid);
3085 int imap_threaded_append(Folder * folder, const char * mailbox,
3086 const char * filename,
3087 struct mailimap_flag_list * flag_list,
3090 struct append_param param;
3091 struct append_result result;
3094 debug_print("imap append - begin\n");
3096 imap = get_imap(folder);
3098 param.mailbox = mailbox;
3099 param.filename = filename;
3100 param.flag_list = flag_list;
3102 threaded_run(folder, ¶m, &result, append_run);
3104 if (result.error != MAILIMAP_NO_ERROR)
3105 return result.error;
3107 debug_print("imap append - end\n");
3111 return result.error;
3117 struct expunge_param {
3121 struct expunge_result {
3125 static void expunge_run(struct etpan_thread_op * op)
3127 struct expunge_param * param;
3128 struct expunge_result * result;
3132 result = op->result;
3136 r = mailimap_expunge(param->imap);
3139 debug_print("imap expunge run - end %i\n", r);
3142 int imap_threaded_expunge(Folder * folder)
3144 struct expunge_param param;
3145 struct expunge_result result;
3147 debug_print("imap expunge - begin\n");
3149 param.imap = get_imap(folder);
3151 threaded_run(folder, ¶m, &result, expunge_run);
3153 debug_print("imap expunge - end\n");
3155 return result.error;
3161 struct mailimap_set * set;
3165 struct copy_result {
3167 struct mailimap_set *source;
3168 struct mailimap_set *dest;
3171 static void copy_run(struct etpan_thread_op * op)
3173 struct copy_param * param;
3174 struct copy_result * result;
3177 struct mailimap_set *source = NULL, *dest = NULL;
3180 result = op->result;
3184 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3185 &val, &source, &dest);
3189 result->source = source;
3190 result->dest = dest;
3192 result->source = NULL;
3193 result->dest = NULL;
3195 debug_print("imap copy run - end %i\n", r);
3198 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3199 const char * mb, struct mailimap_set **source,
3200 struct mailimap_set **dest)
3202 struct copy_param param;
3203 struct copy_result result;
3206 debug_print("imap copy - begin\n");
3208 imap = get_imap(folder);
3213 threaded_run(folder, ¶m, &result, copy_run);
3217 if (result.error != MAILIMAP_NO_ERROR)
3218 return result.error;
3220 *source = result.source;
3221 *dest = result.dest;
3223 debug_print("imap copy - end\n");
3225 return result.error;
3230 struct store_param {
3232 struct mailimap_set * set;
3233 struct mailimap_store_att_flags * store_att_flags;
3236 struct store_result {
3240 static void store_run(struct etpan_thread_op * op)
3242 struct store_param * param;
3243 struct store_result * result;
3247 result = op->result;
3251 r = mailimap_uid_store(param->imap, param->set,
3252 param->store_att_flags);
3256 debug_print("imap store run - end %i\n", r);
3259 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3260 struct mailimap_store_att_flags * store_att_flags)
3262 struct store_param param;
3263 struct store_result result;
3266 debug_print("imap store - begin\n");
3268 imap = get_imap(folder);
3271 param.store_att_flags = store_att_flags;
3273 threaded_run(folder, ¶m, &result, store_run);
3275 if (result.error != MAILIMAP_NO_ERROR)
3276 return result.error;
3278 debug_print("imap store - end\n");
3280 return result.error;
3284 #define ENV_BUFFER_SIZE 512
3286 static void do_exec_command(int fd, const char * command,
3287 const char * servername, uint16_t port)
3291 char env_buffer[ENV_BUFFER_SIZE];
3295 /* Fork again to become a child of init rather than
3296 the etpan client. */
3301 g_setenv("ETPANSERVER", servername, TRUE);
3303 g_unsetenv("ETPANSERVER");
3308 snprintf(porttext, sizeof(porttext), "%d", port);
3309 g_setenv("ETPANPORT", porttext, TRUE);
3312 g_unsetenv("ETPANPORT");
3315 /* Not a lot we can do if there's an error other than bail. */
3316 if (dup2(fd, 0) == -1)
3318 if (dup2(fd, 1) == -1)
3321 /* Should we close stderr and reopen /dev/null? */
3323 maxopen = sysconf(_SC_OPEN_MAX);
3324 for (i=3; i < maxopen; i++)
3328 /* Detach from the controlling tty if we have one. Otherwise,
3329 SSH might do something stupid like trying to use it instead
3330 of running $SSH_ASKPASS. Doh. */
3331 fd = g_open("/dev/tty", O_RDONLY, 0);
3333 ioctl(fd, TIOCNOTTY, NULL);
3336 #endif /* TIOCNOTTY */
3338 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3340 /* Eep. Shouldn't reach this */
3344 static int subcommand_connect(const char *command,
3345 const char *servername, uint16_t port)
3347 /* SEB unsupported on Windows */
3351 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3356 do_exec_command(sockfds[1], command, servername, port);
3358 else if (childpid == -1) {
3366 /* Reap child, leaving grandchild process to run */
3367 waitpid(childpid, NULL, 0);
3372 static int socket_connect_cmd(mailimap * imap, const char * command,
3373 const char * server, int port)
3379 fd = subcommand_connect(command, server, port);
3381 return MAILIMAP_ERROR_STREAM;
3383 s = mailstream_socket_open(fd);
3386 return MAILIMAP_ERROR_STREAM;
3389 r = mailimap_connect(imap, s);
3390 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3391 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3392 mailstream_close(s);
3394 imap->imap_stream = NULL;
3403 struct connect_cmd_param {
3405 const char * command;
3406 const char * server;
3410 struct connect_cmd_result {
3414 static void connect_cmd_run(struct etpan_thread_op * op)
3417 struct connect_cmd_param * param;
3418 struct connect_cmd_result * result;
3421 result = op->result;
3425 r = socket_connect_cmd(param->imap, param->command,
3426 param->server, param->port);
3432 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3433 const char * server, int port)
3435 struct connect_cmd_param param;
3436 struct connect_cmd_result result;
3439 mailimap * imap, * oldimap;
3441 oldimap = get_imap(folder);
3443 imap = mailimap_new(0, NULL);
3446 debug_print("deleting old imap %p\n", oldimap);
3447 delete_imap(folder, oldimap);
3451 key.len = sizeof(folder);
3454 chash_set(session_hash, &key, &value, NULL);
3457 param.command = command;
3458 param.server = server;
3461 threaded_run(folder, ¶m, &result, connect_cmd_run);
3463 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3465 return result.error;
3467 #endif /* G_OS_WIN32 */
3469 void imap_threaded_cancel(Folder * folder)
3473 imap = get_imap(folder);
3474 if (imap->imap_stream != NULL)
3475 mailstream_cancel(imap->imap_stream);
3480 void imap_main_init(void)
3483 void imap_main_done(gboolean have_connectivity)
3486 void imap_main_set_timeout(int sec)
3490 void imap_threaded_cancel(Folder * folder);