2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2016 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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
28 #include "imap-thread.h"
30 #include <sys/types.h>
32 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
33 #include <sys/socket.h>
42 #include "etpan-thread-manager.h"
43 #include "etpan-ssl.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 gboolean thread_manager_event(GIOChannel * source,
62 GIOCondition condition,
69 if (condition & G_IO_IN)
70 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
72 etpan_thread_manager_loop(thread_manager);
77 static void imap_logger_noop(int direction, const char * str, size_t size)
82 static void imap_logger_cmd(int direction, const char * str, size_t size)
89 log_print(LOG_PROTOCOL, "IMAP%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
93 memset(buf, 0, size+1);
94 strncpy(buf, str, size);
97 if (!strncmp(buf, "<<<<<<<", 7)
98 || !strncmp(buf, ">>>>>>>", 7)) {
102 while (strstr(buf, "\r"))
103 *strstr(buf, "\r") = ' ';
104 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
105 buf[strlen(buf)-1] = '\0';
107 lines = g_strsplit(buf, "\n", -1);
109 while (lines[i] && *lines[i]) {
110 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
117 static void imap_logger_fetch(int direction, const char * str, size_t size)
123 if (size > 128 && !direction) {
124 log_print(LOG_PROTOCOL, "IMAP%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
128 buf = malloc(size+1);
129 memset(buf, 0, size+1);
130 strncpy(buf, str, size);
132 if (!strncmp(buf, "<<<<<<<", 7)
133 || !strncmp(buf, ">>>>>>>", 7)) {
137 while (strstr(buf, "\r"))
138 *strstr(buf, "\r") = ' ';
139 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
140 buf[strlen(buf)-1] = '\0';
142 lines = g_strsplit(buf, "\n", -1);
144 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
145 while (lines[i] && *lines[i]) {
146 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
150 log_print(LOG_PROTOCOL, "IMAP%c [data - %zd bytes]\n", direction?'>':'<', size);
156 static void imap_logger_uid(int direction, const char * str, size_t size)
163 log_print(LOG_PROTOCOL, "IMAP%c [UID data - %zd bytes]\n", direction?'>':'<', size);
166 buf = malloc(size+1);
167 memset(buf, 0, size+1);
168 strncpy(buf, str, size);
170 if (!strncmp(buf, "<<<<<<<", 7)
171 || !strncmp(buf, ">>>>>>>", 7)) {
175 while (strstr(buf, "\r"))
176 *strstr(buf, "\r") = ' ';
177 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
178 buf[strlen(buf)-1] = '\0';
180 lines = g_strsplit(buf, "\n", -1);
182 while (lines[i] && *lines[i]) {
183 int llen = strlen(lines[i]);
185 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
188 strncpy2(tmp, lines[i], 63);
189 log_print(LOG_PROTOCOL, "IMAP%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
198 static void imap_logger_append(int direction, const char * str, size_t size)
205 log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
207 } else if (direction == 0 && size > 64) {
208 log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
211 buf = malloc(size+1);
212 memset(buf, 0, size+1);
213 strncpy(buf, str, size);
215 if (!strncmp(buf, "<<<<<<<", 7)
216 || !strncmp(buf, ">>>>>>>", 7)) {
220 while (strstr(buf, "\r"))
221 *strstr(buf, "\r") = ' ';
222 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
223 buf[strlen(buf)-1] = '\0';
225 lines = g_strsplit(buf, "\n", -1);
227 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
228 while (lines[i] && *lines[i]) {
229 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
233 log_print(LOG_PROTOCOL, "IMAP%c [data - %zd bytes]\n", direction?'>':'<', size);
239 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
240 gboolean etpan_skip_ssl_cert_check = FALSE;
241 extern void mailsasl_ref(void);
243 void imap_main_init(gboolean skip_ssl_cert_check)
245 int fd_thread_manager;
247 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
248 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
249 mailstream_network_delay.tv_usec = 0;
251 mailstream_debug = 1;
252 mailstream_logger = imap_logger_cmd;
255 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
256 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
257 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
259 thread_manager = etpan_thread_manager_new();
261 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
264 io_channel = g_io_channel_unix_new(fd_thread_manager);
266 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
268 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
269 thread_manager_event,
274 void imap_main_set_timeout(int sec)
276 mailstream_network_delay.tv_sec = sec;
277 mailstream_network_delay.tv_usec = 0;
280 void imap_main_done(gboolean have_connectivity)
282 imap_disconnect_all(have_connectivity);
283 etpan_thread_manager_stop(thread_manager);
284 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
287 etpan_thread_manager_join(thread_manager);
289 g_source_remove(thread_manager_signal);
290 g_io_channel_unref(io_channel);
292 etpan_thread_manager_free(thread_manager);
294 chash_free(courier_workaround_hash);
295 chash_free(session_hash);
296 chash_free(imap_hash);
299 void imap_init(Folder * folder)
301 struct etpan_thread * thread;
305 thread = etpan_thread_manager_get_thread(thread_manager);
308 key.len = sizeof(folder);
312 chash_set(imap_hash, &key, &value, NULL);
315 void imap_done(Folder * folder)
317 struct etpan_thread * thread;
323 key.len = sizeof(folder);
325 r = chash_get(imap_hash, &key, &value);
331 etpan_thread_unbind(thread);
333 chash_delete(imap_hash, &key, NULL);
335 debug_print("remove thread\n");
338 static struct etpan_thread * get_thread(Folder * folder)
340 struct etpan_thread * thread;
346 key.len = sizeof(folder);
348 r = chash_get(imap_hash, &key, &value);
357 static mailimap * get_imap(Folder * folder)
365 key.len = sizeof(folder);
367 r = chash_get(session_hash, &key, &value);
372 debug_print("found imap %p\n", imap);
376 static gboolean cb_show_error(gpointer data)
378 mainwindow_show_error();
382 static void generic_cb(int cancelled, void * result, void * callback_data)
384 struct etpan_thread_op * op;
386 op = (struct etpan_thread_op *) callback_data;
388 debug_print("generic_cb\n");
389 if (op->imap && op->imap->imap_response_info &&
390 op->imap->imap_response_info->rsp_alert) {
391 log_error(LOG_PROTOCOL, "IMAP< Alert: %s\n",
392 op->imap->imap_response_info->rsp_alert);
393 g_timeout_add(10, cb_show_error, NULL);
398 /* Please do *not* blindly use imap pointers after this function returns,
399 * someone may have deleted it while this function was waiting for completion.
400 * Check return value to see if imap is still valid.
401 * Run get_imap(folder) again to get a fresh and valid pointer.
403 static int threaded_run(Folder * folder, void * param, void * result,
404 void (* func)(struct etpan_thread_op * ))
406 struct etpan_thread_op * op;
407 struct etpan_thread * thread;
408 struct mailimap * imap = get_imap(folder);
410 imap_folder_ref(folder);
412 op = etpan_thread_op_new();
419 op->callback = generic_cb;
420 op->callback_data = op;
422 thread = get_thread(folder);
423 etpan_thread_op_schedule(thread, op);
425 while (!op->finished) {
426 gtk_main_iteration();
429 etpan_thread_op_free(op);
431 imap_folder_unref(folder);
433 if (imap != get_imap(folder)) {
434 g_warning("returning from operation on a stale imap %p", imap);
444 struct connect_param {
446 PrefsAccount *account;
451 struct connect_result {
455 #define CHECK_IMAP() { \
456 if (!param->imap) { \
457 result->error = MAILIMAP_ERROR_BAD_STATE; \
463 static void delete_imap_run(struct etpan_thread_op * op)
465 mailimap * imap = op->imap;
467 /* we don't want libetpan to logout */
468 if (imap->imap_stream) {
469 mailstream_close(imap->imap_stream);
470 imap->imap_stream = NULL;
476 static void threaded_delete_imap(Folder *folder, mailimap *imap)
478 struct etpan_thread_op * op;
480 /* No need to wait for completion, threaded_run() won't work here. */
481 op = etpan_thread_op_new();
483 op->run = delete_imap_run;
484 op->cleanup = etpan_thread_op_free;
486 etpan_thread_op_schedule(get_thread(folder), op);
488 debug_print("threaded delete imap posted\n");
491 static void delete_imap(Folder *folder, mailimap *imap)
496 key.len = sizeof(folder);
497 chash_delete(session_hash, &key, NULL);
502 key.len = sizeof(imap);
503 chash_delete(courier_workaround_hash, &key, NULL);
504 /* We can't just free imap here as there may be ops on it pending
505 * in the thread. Posting freeing as an op will synchronize against
506 * existing jobs and as imap is already removed from session_hash
507 * we are sure no new ops can be posted. */
508 threaded_delete_imap(folder, imap);
511 static void connect_run(struct etpan_thread_op * op)
514 struct connect_param * param;
515 struct connect_result * result;
522 r = mailimap_socket_connect(param->imap,
523 param->server, param->port);
529 int imap_threaded_connect(Folder * folder, const char * server, int port)
531 struct connect_param param;
532 struct connect_result result;
535 mailimap * imap, * oldimap;
537 oldimap = get_imap(folder);
539 imap = mailimap_new(0, NULL);
542 debug_print("deleting old imap %p\n", oldimap);
543 delete_imap(folder, oldimap);
547 key.len = sizeof(folder);
550 chash_set(session_hash, &key, &value, NULL);
553 param.server = server;
557 threaded_run(folder, ¶m, &result, connect_run);
559 debug_print("connect ok %i with imap %p\n", result.error, imap);
564 static void connect_ssl_run(struct etpan_thread_op * op)
567 struct connect_param * param;
568 struct connect_result * result;
575 r = mailimap_ssl_connect_with_callback(param->imap,
576 param->server, param->port,
577 etpan_connect_ssl_context_cb, param->account);
581 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
583 struct connect_param param;
584 struct connect_result result;
587 mailimap * imap, * oldimap;
588 gboolean accept_if_valid = FALSE;
590 oldimap = get_imap(folder);
592 imap = mailimap_new(0, NULL);
595 debug_print("deleting old imap %p\n", oldimap);
596 delete_imap(folder, oldimap);
600 key.len = sizeof(folder);
603 chash_set(session_hash, &key, &value, NULL);
606 param.server = server;
608 param.account = folder->account;
611 accept_if_valid = folder->account->ssl_certs_auto_accept;
614 if (threaded_run(folder, ¶m, &result, connect_ssl_run))
615 return MAILIMAP_ERROR_INVAL;
617 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
618 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
619 if (etpan_certificate_check(imap->imap_stream, server, port,
620 accept_if_valid) != TRUE)
621 result.error = MAILIMAP_ERROR_SSL;
623 debug_print("connect %d with imap %p\n", result.error, imap);
634 struct mailimap_capability_data *caps;
637 static void capability_run(struct etpan_thread_op * op)
640 struct capa_param * param;
641 struct capa_result * result;
642 struct mailimap_capability_data *caps;
649 r = mailimap_capability(param->imap, &caps);
652 result->caps = (r == 0 ? caps : NULL);
656 int imap_threaded_capability(Folder *folder, struct mailimap_capability_data ** caps)
658 struct capa_param param;
659 struct capa_result result;
662 imap = get_imap(folder);
666 threaded_run(folder, ¶m, &result, capability_run);
668 debug_print("capa %d\n", result.error);
670 if (result.error == MAILIMAP_NO_ERROR)
677 struct disconnect_param {
681 struct disconnect_result {
685 static void disconnect_run(struct etpan_thread_op * op)
688 struct disconnect_param * param;
689 struct disconnect_result * result;
696 r = mailimap_logout(param->imap);
701 void imap_threaded_disconnect(Folder * folder)
703 struct connect_param param;
704 struct connect_result result;
707 imap = get_imap(folder);
709 debug_print("was disconnected\n");
715 if (threaded_run(folder, ¶m, &result, disconnect_run)) {
716 debug_print("imap already deleted %p\n", imap);
718 debug_print("deleting old imap %p\n", imap);
719 delete_imap(folder, imap);
722 debug_print("disconnect ok\n");
729 const char * wildcard;
738 static void list_run(struct etpan_thread_op * op)
740 struct list_param * param;
741 struct list_result * result;
752 if (param->base == NULL || param->wildcard == NULL) {
755 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
759 r = mailimap_lsub(param->imap, param->base,
760 param->wildcard, &list);
762 r = mailimap_list(param->imap, param->base,
763 param->wildcard, &list);
766 debug_print("imap list run - end\n");
769 int imap_threaded_list(Folder * folder, const char * base,
770 const char * wildcard,
773 struct list_param param;
774 struct list_result result;
776 debug_print("imap list - begin\n");
778 param.imap = get_imap(folder);
780 param.wildcard = wildcard;
781 param.sub_only = FALSE;
783 threaded_run(folder, ¶m, &result, list_run);
785 * p_result = result.list;
787 debug_print("imap list - end %p\n", result.list);
792 int imap_threaded_lsub(Folder * folder, const char * base,
793 const char * wildcard,
796 struct list_param param;
797 struct list_result result;
799 debug_print("imap lsub - begin\n");
801 param.imap = get_imap(folder);
803 param.wildcard = wildcard;
804 param.sub_only = TRUE;
806 threaded_run(folder, ¶m, &result, list_run);
808 * p_result = result.list;
810 debug_print("imap lsub - end %p\n", result.list);
815 struct subscribe_param {
821 struct subscribe_result {
825 static void subscribe_run(struct etpan_thread_op * op)
827 struct subscribe_param * param;
828 struct subscribe_result * result;
836 if (param->mb == NULL) {
838 debug_print("no mb\n");
841 if (param->subscribe)
842 r = mailimap_subscribe(param->imap, param->mb);
844 r = mailimap_unsubscribe(param->imap, param->mb);
846 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
849 int imap_threaded_subscribe(Folder * folder, const char * mb,
852 struct subscribe_param param;
853 struct subscribe_result result;
855 debug_print("imap list - begin\n");
857 param.imap = get_imap(folder);
859 param.subscribe = subscribe;
861 threaded_run(folder, ¶m, &result, subscribe_run);
869 const char * password;
874 struct login_result {
878 static void login_run(struct etpan_thread_op * op)
880 struct login_param * param;
881 struct login_result * result;
883 #ifdef DISABLE_LOG_DURING_LOGIN
892 #ifdef DISABLE_LOG_DURING_LOGIN
893 old_debug = mailstream_debug;
894 mailstream_debug = 0;
896 if (!strcmp(param->type, "plaintext"))
897 r = mailimap_login(param->imap,
898 param->login, param->password);
899 else if (!strcmp(param->type, "GSSAPI"))
900 r = mailimap_authenticate(param->imap,
901 param->type, param->server, NULL, NULL,
902 param->login, param->login,
903 param->password, NULL);
904 else if (!strcmp(param->type, "SCRAM-SHA-1"))
905 /* 7th argument has to be NULL here, to stop libetpan sending the
906 * a= attribute in its initial SCRAM-SHA-1 message to server. At least
907 * Dovecot 2.2 doesn't seem to like that, and will not authenticate
909 r = mailimap_authenticate(param->imap,
910 param->type, NULL, NULL, NULL,
912 param->password, NULL);
914 r = mailimap_authenticate(param->imap,
915 param->type, NULL, NULL, NULL,
916 param->login, param->login,
917 param->password, NULL);
918 #ifdef DISABLE_LOG_DURING_LOGIN
919 mailstream_debug = old_debug;
923 if (param->imap->imap_response)
924 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
925 debug_print("imap login run - end %i\n", r);
928 int imap_threaded_login(Folder * folder,
929 const char * login, const char * password,
932 struct login_param param;
933 struct login_result result;
935 debug_print("imap login - begin\n");
938 return MAILIMAP_ERROR_INVAL;
940 param.imap = get_imap(folder);
942 param.password = password;
944 if (folder && folder->account)
945 param.server = folder->account->recv_server;
949 threaded_run(folder, ¶m, &result, login_run);
951 debug_print("imap login - end\n");
957 struct status_param {
960 struct mailimap_status_att_list * status_att_list;
963 struct status_result {
965 struct mailimap_mailbox_data_status * data_status;
968 static void status_run(struct etpan_thread_op * op)
970 struct status_param * param;
971 struct status_result * result;
979 r = mailimap_status(param->imap, param->mb,
980 param->status_att_list,
981 &result->data_status);
984 debug_print("imap status run - end %i\n", r);
987 int imap_threaded_status(Folder * folder, const char * mb,
988 struct mailimap_mailbox_data_status ** data_status,
991 struct status_param param;
992 struct status_result result;
993 struct mailimap_status_att_list * status_att_list;
995 debug_print("imap status - begin\n");
997 status_att_list = mailimap_status_att_list_new_empty();
999 mailimap_status_att_list_add(status_att_list,
1000 MAILIMAP_STATUS_ATT_MESSAGES);
1002 if (mask & 1 << 1) {
1003 mailimap_status_att_list_add(status_att_list,
1004 MAILIMAP_STATUS_ATT_RECENT);
1006 if (mask & 1 << 2) {
1007 mailimap_status_att_list_add(status_att_list,
1008 MAILIMAP_STATUS_ATT_UIDNEXT);
1010 if (mask & 1 << 3) {
1011 mailimap_status_att_list_add(status_att_list,
1012 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1014 if (mask & 1 << 4) {
1015 mailimap_status_att_list_add(status_att_list,
1016 MAILIMAP_STATUS_ATT_UNSEEN);
1018 param.imap = get_imap(folder);
1020 param.status_att_list = status_att_list;
1022 threaded_run(folder, ¶m, &result, status_run);
1024 debug_print("imap status - end\n");
1026 * data_status = result.data_status;
1028 mailimap_status_att_list_free(status_att_list);
1030 return result.error;
1039 struct noop_result {
1043 static void noop_run(struct etpan_thread_op * op)
1045 struct noop_param * param;
1046 struct noop_result * result;
1050 result = op->result;
1054 r = mailimap_noop(param->imap);
1057 debug_print("imap noop run - end %i\n", r);
1060 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1061 unsigned int *p_recent,
1062 unsigned int *p_expunge,
1063 unsigned int *p_unseen,
1064 unsigned int *p_uidnext,
1065 unsigned int *p_uidval)
1067 struct noop_param param;
1068 struct noop_result result;
1071 debug_print("imap noop - begin\n");
1073 imap = get_imap(folder);
1076 if (threaded_run(folder, ¶m, &result, noop_run))
1077 return MAILIMAP_ERROR_INVAL;
1079 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1080 * p_exists = imap->imap_selection_info->sel_exists;
1081 * p_recent = imap->imap_selection_info->sel_recent;
1082 * p_unseen = imap->imap_selection_info->sel_unseen;
1083 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1084 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1092 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1093 imap->imap_response_info->rsp_expunged != NULL) {
1094 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1098 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1099 *p_exists, *p_recent, *p_expunge, *p_unseen,
1100 *p_uidnext, *p_uidval);
1102 return result.error;
1106 struct starttls_result {
1110 static void starttls_run(struct etpan_thread_op * op)
1112 struct connect_param * param;
1113 struct starttls_result * result;
1117 result = op->result;
1121 r = mailimap_starttls(param->imap);
1124 debug_print("imap STARTTLS run - end %i\n", r);
1127 mailimap *imap = param->imap;
1128 mailstream_low *plain_low = NULL;
1129 mailstream_low *tls_low = NULL;
1132 plain_low = mailstream_get_low(imap->imap_stream);
1133 fd = mailstream_low_get_fd(plain_low);
1135 debug_print("imap STARTTLS run - can't get fd\n");
1136 result->error = MAILIMAP_ERROR_STREAM;
1140 tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
1141 if (tls_low == NULL) {
1142 debug_print("imap STARTTLS run - can't tls_open\n");
1143 result->error = MAILIMAP_ERROR_STREAM;
1146 mailstream_low_free(plain_low);
1147 mailstream_set_low(imap->imap_stream, tls_low);
1151 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1153 struct connect_param param;
1154 struct starttls_result result;
1155 gboolean accept_if_valid = FALSE;
1157 debug_print("imap STARTTLS - begin\n");
1159 param.imap = get_imap(folder);
1160 param.server = host;
1162 param.account = folder->account;
1164 if (folder->account)
1165 accept_if_valid = folder->account->ssl_certs_auto_accept;
1167 if (threaded_run(folder, ¶m, &result, starttls_run))
1168 return MAILIMAP_ERROR_INVAL;
1170 debug_print("imap STARTTLS - end\n");
1172 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1173 if (etpan_certificate_check(param.imap->imap_stream, host, port,
1174 accept_if_valid) != TRUE)
1175 return MAILIMAP_ERROR_SSL;
1177 return result.error;
1181 struct create_param {
1186 struct create_result {
1190 static void create_run(struct etpan_thread_op * op)
1192 struct create_param * param;
1193 struct create_result * result;
1197 result = op->result;
1201 r = mailimap_create(param->imap, param->mb);
1204 debug_print("imap create run - end %i\n", r);
1207 int imap_threaded_create(Folder * folder, const char * mb)
1209 struct create_param param;
1210 struct create_result result;
1212 debug_print("imap create - begin\n");
1214 param.imap = get_imap(folder);
1217 threaded_run(folder, ¶m, &result, create_run);
1219 debug_print("imap create - end\n");
1221 return result.error;
1227 struct rename_param {
1230 const char * new_name;
1233 struct rename_result {
1237 static void rename_run(struct etpan_thread_op * op)
1239 struct rename_param * param;
1240 struct rename_result * result;
1244 result = op->result;
1248 r = mailimap_rename(param->imap, param->mb, param->new_name);
1251 debug_print("imap rename run - end %i\n", r);
1254 int imap_threaded_rename(Folder * folder,
1255 const char * mb, const char * new_name)
1257 struct rename_param param;
1258 struct rename_result result;
1260 debug_print("imap rename - begin\n");
1262 param.imap = get_imap(folder);
1264 param.new_name = new_name;
1266 threaded_run(folder, ¶m, &result, rename_run);
1268 debug_print("imap rename - end\n");
1270 return result.error;
1276 struct delete_param {
1281 struct delete_result {
1285 static void delete_run(struct etpan_thread_op * op)
1287 struct delete_param * param;
1288 struct delete_result * result;
1292 result = op->result;
1296 r = mailimap_delete(param->imap, param->mb);
1299 debug_print("imap delete run - end %i\n", r);
1302 int imap_threaded_delete(Folder * folder, const char * mb)
1304 struct delete_param param;
1305 struct delete_result result;
1307 debug_print("imap delete - begin\n");
1309 param.imap = get_imap(folder);
1312 threaded_run(folder, ¶m, &result, delete_run);
1314 debug_print("imap delete - end\n");
1316 return result.error;
1321 struct select_param {
1326 struct select_result {
1330 static void select_run(struct etpan_thread_op * op)
1332 struct select_param * param;
1333 struct select_result * result;
1337 result = op->result;
1341 r = mailimap_select(param->imap, param->mb);
1344 debug_print("imap select run - end %i\n", r);
1347 int imap_threaded_select(Folder * folder, const char * mb,
1348 gint * exists, gint * recent, gint * unseen,
1349 guint32 * uid_validity,gint *can_create_flags,
1352 struct select_param param;
1353 struct select_result result;
1356 debug_print("imap select - begin\n");
1358 imap = get_imap(folder);
1362 if (threaded_run(folder, ¶m, &result, select_run))
1363 return MAILIMAP_ERROR_INVAL;
1365 if (result.error != MAILIMAP_NO_ERROR)
1366 return result.error;
1368 if (!imap || imap->imap_selection_info == NULL)
1369 return MAILIMAP_ERROR_PARSE;
1371 * exists = imap->imap_selection_info->sel_exists;
1372 * recent = imap->imap_selection_info->sel_recent;
1373 * unseen = imap->imap_selection_info->sel_unseen;
1374 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1375 * can_create_flags = FALSE;
1377 if (imap->imap_selection_info->sel_perm_flags) {
1378 GSList *t_flags = NULL;
1379 clistiter *cur = NULL;
1380 if (imap->imap_selection_info->sel_perm_flags)
1381 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1383 for (; cur; cur = clist_next(cur)) {
1384 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1385 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1386 *can_create_flags = TRUE;
1387 else if (flag->fl_flag &&
1388 flag->fl_flag->fl_type == 6 &&
1389 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1390 *can_create_flags = TRUE;
1391 if (flag->fl_flag && ok_flags) {
1392 MsgPermFlags c_flag = 0;
1393 switch (flag->fl_flag->fl_type) {
1394 case MAILIMAP_FLAG_ANSWERED:
1395 c_flag = IMAP_FLAG_ANSWERED;
1397 case MAILIMAP_FLAG_FLAGGED:
1398 c_flag = IMAP_FLAG_FLAGGED;
1400 case MAILIMAP_FLAG_DELETED:
1401 c_flag = IMAP_FLAG_DELETED;
1403 case MAILIMAP_FLAG_DRAFT:
1404 c_flag = IMAP_FLAG_DRAFT;
1406 case MAILIMAP_FLAG_SEEN:
1407 c_flag = IMAP_FLAG_SEEN;
1409 case MAILIMAP_FLAG_KEYWORD:
1410 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1411 c_flag = IMAP_FLAG_FORWARDED;
1412 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1413 c_flag = IMAP_FLAG_SPAM;
1414 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1415 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1416 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1417 c_flag = IMAP_FLAG_HAM;
1423 t_flags = g_slist_prepend(t_flags,
1424 GUINT_TO_POINTER(c_flag));
1429 *ok_flags = t_flags;
1431 debug_print("imap select - end\n");
1433 return result.error;
1436 static void close_run(struct etpan_thread_op * op)
1438 struct select_param * param;
1439 struct select_result * result;
1443 result = op->result;
1447 r = mailimap_close(param->imap);
1450 debug_print("imap close run - end %i\n", r);
1453 int imap_threaded_close(Folder * folder)
1455 struct select_param param;
1456 struct select_result result;
1459 debug_print("imap close - begin\n");
1461 imap = get_imap(folder);
1464 threaded_run(folder, ¶m, &result, close_run);
1466 if (result.error != MAILIMAP_NO_ERROR)
1467 return result.error;
1469 debug_print("imap close - end\n");
1471 return result.error;
1474 struct examine_param {
1479 struct examine_result {
1483 static void examine_run(struct etpan_thread_op * op)
1485 struct examine_param * param;
1486 struct examine_result * result;
1490 result = op->result;
1494 r = mailimap_examine(param->imap, param->mb);
1497 debug_print("imap examine run - end %i\n", r);
1500 int imap_threaded_examine(Folder * folder, const char * mb,
1501 gint * exists, gint * recent, gint * unseen,
1502 guint32 * uid_validity)
1504 struct examine_param param;
1505 struct examine_result result;
1508 debug_print("imap examine - begin\n");
1510 imap = get_imap(folder);
1514 if (threaded_run(folder, ¶m, &result, examine_run))
1515 return MAILIMAP_ERROR_INVAL;
1517 if (result.error != MAILIMAP_NO_ERROR)
1518 return result.error;
1520 if (!imap || imap->imap_selection_info == NULL)
1521 return MAILIMAP_ERROR_PARSE;
1523 * exists = imap->imap_selection_info->sel_exists;
1524 * recent = imap->imap_selection_info->sel_recent;
1525 * unseen = imap->imap_selection_info->sel_unseen;
1526 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1528 debug_print("imap examine - end\n");
1530 return result.error;
1536 struct search_param {
1539 const char *charset;
1540 struct mailimap_set * set;
1544 struct search_result {
1546 clist * search_result;
1549 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1551 return mailimap_set_item_new(orig->set_first, orig->set_last);
1554 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1556 clist *list = orig ? orig->set_list : NULL;
1563 newlist = clist_new();
1567 for (cur = clist_begin(list); cur; cur = clist_next(cur)) {
1568 if (clist_append(newlist,
1569 sc_mailimap_set_item_copy(
1570 (struct mailimap_set_item *)clist_content(cur))) != 0) {
1571 clist_free(newlist);
1575 return mailimap_set_new(newlist);
1578 static void search_run(struct etpan_thread_op * op)
1580 struct search_param * param;
1581 struct search_result * result;
1583 struct mailimap_search_key * key = NULL;
1584 struct mailimap_search_key * uid_key = NULL;
1585 struct mailimap_search_key * search_type_key = NULL;
1586 clist * search_result;
1589 result = op->result;
1593 /* we copy the mailimap_set because freeing the key is recursive */
1594 if (param->set != NULL) {
1595 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1596 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1597 uid_key = mailimap_search_key_new_all();
1599 switch (param->type) {
1600 case IMAP_SEARCH_TYPE_SIMPLE:
1601 search_type_key = NULL;
1603 case IMAP_SEARCH_TYPE_SEEN:
1604 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1606 case IMAP_SEARCH_TYPE_UNSEEN:
1607 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1609 case IMAP_SEARCH_TYPE_ANSWERED:
1610 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1612 case IMAP_SEARCH_TYPE_FLAGGED:
1613 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1615 case IMAP_SEARCH_TYPE_DELETED:
1616 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1618 case IMAP_SEARCH_TYPE_FORWARDED:
1619 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1621 case IMAP_SEARCH_TYPE_SPAM:
1622 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1624 case IMAP_SEARCH_TYPE_KEYED:
1625 search_type_key = param->key;
1629 if (search_type_key != NULL) {
1630 if (param->set != NULL) {
1631 key = mailimap_search_key_new_multiple_empty();
1632 mailimap_search_key_multiple_add(key, search_type_key);
1633 mailimap_search_key_multiple_add(key, uid_key);
1635 key = search_type_key;
1637 } else if (uid_key != NULL) {
1642 g_warning("no key!");
1643 result = op->result;
1645 result->search_result = NULL;
1647 mailstream_logger = imap_logger_uid;
1649 r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
1651 mailstream_logger = imap_logger_cmd;
1653 /* free the key (with the imapset) */
1654 mailimap_search_key_free(key);
1657 result->search_result = search_result;
1659 debug_print("imap search run - end %i\n", result->error);
1662 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1663 const char *charset, struct mailimap_set * set,
1664 clist ** search_result)
1666 struct search_param param;
1667 struct search_result result;
1670 debug_print("imap search - begin\n");
1672 imap = get_imap(folder);
1675 param.charset = charset;
1676 param.type = search_type;
1679 threaded_run(folder, ¶m, &result, search_run);
1681 if (result.error != MAILIMAP_NO_ERROR)
1682 return result.error;
1684 debug_print("imap search - end\n");
1686 * search_result = result.search_result;
1688 return result.error;
1692 struct _IMAPSearchKey {
1693 struct mailimap_search_key* key;
1696 IMAPSearchKey* imap_search_new(gint criteria,
1697 const gchar *header,
1701 char* sk_bcc = NULL;
1702 struct mailimap_date* sk_before = NULL;
1703 char* sk_body = NULL;
1705 char* sk_from = NULL;
1706 char* sk_keyword = NULL;
1707 struct mailimap_date* sk_on = NULL;
1708 struct mailimap_date* sk_since = NULL;
1709 char* sk_subject = NULL;
1710 char* sk_text = NULL;
1712 char* sk_unkeyword = NULL;
1713 char* sk_header_name = NULL;
1714 char* sk_header_value = NULL;
1715 uint32_t sk_larger = 0;
1716 struct mailimap_search_key* sk_not = NULL;
1717 struct mailimap_search_key* sk_or1 = NULL;
1718 struct mailimap_search_key* sk_or2 = NULL;
1719 struct mailimap_date* sk_sentbefore = NULL;
1720 struct mailimap_date* sk_senton = NULL;
1721 struct mailimap_date* sk_sentsince = NULL;
1722 uint32_t sk_smaller = 0;
1723 struct mailimap_set* sk_uid = NULL;
1724 struct mailimap_set* sk_set = NULL;
1725 clist* sk_multiple = NULL;
1726 int etpan_matcher_type;
1729 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1730 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1731 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1732 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1733 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1734 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1735 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1737 case IMAP_SEARCH_CRITERIA_TAG:
1738 sk_keyword = strdup(expr);
1739 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1742 case IMAP_SEARCH_CRITERIA_SUBJECT:
1743 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1744 sk_subject = strdup(expr);
1747 case IMAP_SEARCH_CRITERIA_TO:
1748 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1749 sk_to = strdup(expr);
1752 case IMAP_SEARCH_CRITERIA_CC:
1753 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1754 sk_cc = strdup(expr);
1757 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1758 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1761 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1764 localtime_r(&limit, &tm);
1765 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1766 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1767 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1769 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1770 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1775 case IMAP_SEARCH_CRITERIA_BODY:
1776 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1777 sk_body = strdup(expr);
1780 case IMAP_SEARCH_CRITERIA_MESSAGE:
1781 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1782 sk_text = strdup(expr);
1785 case IMAP_SEARCH_CRITERIA_HEADER:
1786 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1787 sk_header_name = strdup(header);
1788 sk_header_value = strdup(expr);
1791 case IMAP_SEARCH_CRITERIA_FROM:
1792 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1793 sk_from = strdup(expr);
1796 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1797 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1801 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1802 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1810 return mailimap_search_key_new(etpan_matcher_type,
1811 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1812 sk_on, sk_since, sk_subject, sk_text, sk_to,
1813 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1814 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1815 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1818 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1820 return mailimap_search_key_new_not(key);
1823 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1825 return mailimap_search_key_new_or(l, r);
1828 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1830 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1831 mailimap_search_key_multiple_add(result, l);
1832 mailimap_search_key_multiple_add(result, r);
1837 void imap_search_free(IMAPSearchKey* key)
1842 mailimap_search_key_free(key);
1847 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1851 struct mailimap_msg_att_dynamic ** patt_dyn);
1854 result_to_uid_list(clist * fetch_result, carray ** result)
1856 clistiter * cur = NULL;
1861 tab = carray_new(128);
1863 res = MAILIMAP_ERROR_MEMORY;
1868 cur = clist_begin(fetch_result);
1870 for(; cur != NULL ; cur = clist_next(cur)) {
1871 struct mailimap_msg_att * msg_att;
1875 msg_att = clist_content(cur);
1878 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1880 puid = malloc(sizeof(* puid));
1882 res = MAILIMAP_ERROR_MEMORY;
1887 r = carray_add(tab, puid, NULL);
1890 res = MAILIMAP_ERROR_MEMORY;
1897 return MAILIMAP_NO_ERROR;
1900 imap_fetch_uid_list_free(tab);
1905 static int imap_get_messages_list(mailimap * imap,
1906 uint32_t first_index,
1911 struct mailimap_fetch_att * fetch_att;
1912 struct mailimap_fetch_type * fetch_type;
1913 struct mailimap_set * set;
1914 clist * fetch_result;
1917 set = mailimap_set_new_interval(first_index, 0);
1919 res = MAILIMAP_ERROR_MEMORY;
1923 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1924 if (fetch_type == NULL) {
1925 res = MAILIMAP_ERROR_MEMORY;
1929 fetch_att = mailimap_fetch_att_new_uid();
1930 if (fetch_att == NULL) {
1931 res = MAILIMAP_ERROR_MEMORY;
1932 goto free_fetch_type;
1935 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1936 if (r != MAILIMAP_NO_ERROR) {
1937 mailimap_fetch_att_free(fetch_att);
1938 res = MAILIMAP_ERROR_MEMORY;
1939 goto free_fetch_type;
1942 mailstream_logger = imap_logger_fetch;
1944 r = mailimap_uid_fetch(imap, set,
1945 fetch_type, &fetch_result);
1947 mailstream_logger = imap_logger_cmd;
1948 mailimap_fetch_type_free(fetch_type);
1949 mailimap_set_free(set);
1951 if (r != MAILIMAP_NO_ERROR) {
1957 r = result_to_uid_list(fetch_result, &env_list);
1958 mailimap_fetch_list_free(fetch_result);
1960 * result = env_list;
1962 return MAILIMAP_NO_ERROR;
1965 mailimap_fetch_type_free(fetch_type);
1967 mailimap_set_free(set);
1975 struct fetch_uid_param {
1977 uint32_t first_index;
1980 struct fetch_uid_result {
1982 carray * fetch_result;
1985 static void fetch_uid_run(struct etpan_thread_op * op)
1987 struct fetch_uid_param * param;
1988 struct fetch_uid_result * result;
1989 carray * fetch_result;
1993 result = op->result;
1997 fetch_result = NULL;
1998 mailstream_logger = imap_logger_noop;
1999 log_print(LOG_PROTOCOL, "IMAP- [fetching UIDs...]\n");
2001 r = imap_get_messages_list(param->imap, param->first_index,
2004 mailstream_logger = imap_logger_cmd;
2007 result->fetch_result = fetch_result;
2008 debug_print("imap fetch_uid run - end %i\n", r);
2011 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
2012 carray ** fetch_result)
2014 struct fetch_uid_param param;
2015 struct fetch_uid_result result;
2018 debug_print("imap fetch_uid - begin\n");
2020 imap = get_imap(folder);
2022 param.first_index = first_index;
2024 threaded_run(folder, ¶m, &result, fetch_uid_run);
2026 if (result.error != MAILIMAP_NO_ERROR)
2027 return result.error;
2029 debug_print("imap fetch_uid - end\n");
2031 * fetch_result = result.fetch_result;
2033 return result.error;
2037 void imap_fetch_uid_list_free(carray * uid_list)
2041 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
2044 puid = carray_get(uid_list, i);
2047 carray_free(uid_list);
2053 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
2056 result_to_uid_flags_list(clist * fetch_result, carray ** result)
2058 clistiter * cur = NULL;
2062 GSList *tags = NULL;
2064 tab = carray_new(128);
2066 res = MAILIMAP_ERROR_MEMORY;
2071 cur = clist_begin(fetch_result);
2073 for(; cur != NULL ; cur = clist_next(cur)) {
2074 struct mailimap_msg_att * msg_att;
2077 struct mailimap_msg_att_dynamic * att_dyn;
2083 msg_att = clist_content(cur);
2087 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2090 if (att_dyn == NULL)
2093 flags = imap_flags_to_flags(att_dyn, &tags);
2095 puid = malloc(sizeof(* puid));
2097 res = MAILIMAP_ERROR_MEMORY;
2102 r = carray_add(tab, puid, NULL);
2105 res = MAILIMAP_ERROR_MEMORY;
2108 pflags = malloc(sizeof(* pflags));
2109 if (pflags == NULL) {
2110 res = MAILIMAP_ERROR_MEMORY;
2114 r = carray_add(tab, pflags, NULL);
2117 res = MAILIMAP_ERROR_MEMORY;
2120 r = carray_add(tab, tags, NULL);
2123 res = MAILIMAP_ERROR_MEMORY;
2130 return MAILIMAP_NO_ERROR;
2133 imap_fetch_uid_flags_list_free(tab);
2134 slist_free_strings_full(tags);
2139 static int imap_get_messages_flags_list(mailimap * imap,
2140 uint32_t first_index,
2145 struct mailimap_fetch_att * fetch_att;
2146 struct mailimap_fetch_type * fetch_type;
2147 struct mailimap_set * set;
2148 clist * fetch_result;
2151 set = mailimap_set_new_interval(first_index, 0);
2153 res = MAILIMAP_ERROR_MEMORY;
2157 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2158 if (fetch_type == NULL) {
2159 res = MAILIMAP_ERROR_MEMORY;
2163 fetch_att = mailimap_fetch_att_new_flags();
2164 if (fetch_att == NULL) {
2165 res = MAILIMAP_ERROR_MEMORY;
2166 goto free_fetch_type;
2169 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2170 if (r != MAILIMAP_NO_ERROR) {
2171 mailimap_fetch_att_free(fetch_att);
2172 res = MAILIMAP_ERROR_MEMORY;
2173 goto free_fetch_type;
2176 fetch_att = mailimap_fetch_att_new_uid();
2177 if (fetch_att == NULL) {
2178 res = MAILIMAP_ERROR_MEMORY;
2179 goto free_fetch_type;
2182 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2183 if (r != MAILIMAP_NO_ERROR) {
2184 mailimap_fetch_att_free(fetch_att);
2185 res = MAILIMAP_ERROR_MEMORY;
2186 goto free_fetch_type;
2189 mailstream_logger = imap_logger_fetch;
2191 r = mailimap_uid_fetch(imap, set,
2192 fetch_type, &fetch_result);
2194 mailstream_logger = imap_logger_cmd;
2195 mailimap_fetch_type_free(fetch_type);
2196 mailimap_set_free(set);
2198 if (r != MAILIMAP_NO_ERROR) {
2204 r = result_to_uid_flags_list(fetch_result, &env_list);
2205 mailimap_fetch_list_free(fetch_result);
2207 * result = env_list;
2209 return MAILIMAP_NO_ERROR;
2212 mailimap_fetch_type_free(fetch_type);
2214 mailimap_set_free(set);
2221 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2223 struct fetch_uid_param * param;
2224 struct fetch_uid_result * result;
2225 carray * fetch_result;
2229 result = op->result;
2233 fetch_result = NULL;
2234 r = imap_get_messages_flags_list(param->imap, param->first_index,
2238 result->fetch_result = fetch_result;
2239 debug_print("imap fetch_uid run - end %i\n", r);
2242 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2243 carray ** fetch_result)
2245 struct fetch_uid_param param;
2246 struct fetch_uid_result result;
2249 debug_print("imap fetch_uid - begin\n");
2251 imap = get_imap(folder);
2253 param.first_index = first_index;
2255 mailstream_logger = imap_logger_noop;
2256 log_print(LOG_PROTOCOL, "IMAP- [fetching flags...]\n");
2258 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2260 mailstream_logger = imap_logger_cmd;
2263 if (result.error != MAILIMAP_NO_ERROR)
2264 return result.error;
2266 debug_print("imap fetch_uid - end\n");
2268 * fetch_result = result.fetch_result;
2270 return result.error;
2274 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2278 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2281 data = carray_get(uid_flags_list, i);
2283 data = carray_get(uid_flags_list, i + 1);
2286 carray_free(uid_flags_list);
2291 static int imap_fetch(mailimap * imap,
2294 size_t * result_len)
2297 struct mailimap_set * set;
2298 struct mailimap_fetch_att * fetch_att;
2299 struct mailimap_fetch_type * fetch_type;
2300 clist * fetch_result;
2301 struct mailimap_msg_att * msg_att;
2302 struct mailimap_msg_att_item * msg_att_item;
2307 struct mailimap_section * section;
2309 set = mailimap_set_new_single(msg_index);
2311 res = MAILIMAP_ERROR_MEMORY;
2315 section = mailimap_section_new(NULL);
2316 if (section == NULL) {
2317 res = MAILIMAP_ERROR_MEMORY;
2321 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2322 if (fetch_att == NULL) {
2323 mailimap_section_free(section);
2324 res = MAILIMAP_ERROR_MEMORY;
2328 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2329 if (fetch_type == NULL) {
2330 res = MAILIMAP_ERROR_MEMORY;
2331 goto free_fetch_att;
2334 mailstream_logger = imap_logger_fetch;
2336 r = mailimap_uid_fetch(imap, set,
2337 fetch_type, &fetch_result);
2339 mailstream_logger = imap_logger_cmd;
2341 mailimap_fetch_type_free(fetch_type);
2342 mailimap_set_free(set);
2345 case MAILIMAP_NO_ERROR:
2351 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2352 mailimap_fetch_list_free(fetch_result);
2353 return MAILIMAP_ERROR_FETCH;
2356 msg_att = clist_begin(fetch_result)->data;
2361 if (msg_att->att_list)
2362 cur = clist_begin(msg_att->att_list);
2366 for(; cur != NULL ; cur = clist_next(cur)) {
2367 msg_att_item = clist_content(cur);
2369 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2370 if (msg_att_item->att_data.att_static->att_type ==
2371 MAILIMAP_MSG_ATT_BODY_SECTION) {
2372 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2374 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2376 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2381 mailimap_fetch_list_free(fetch_result);
2384 return MAILIMAP_ERROR_FETCH;
2387 * result_len = text_length;
2389 return MAILIMAP_NO_ERROR;
2392 mailimap_fetch_att_free(fetch_att);
2394 mailimap_set_free(set);
2399 static int imap_fetch_header(mailimap * imap,
2402 size_t * result_len)
2405 struct mailimap_set * set;
2406 struct mailimap_fetch_att * fetch_att;
2407 struct mailimap_fetch_type * fetch_type;
2408 clist * fetch_result;
2409 struct mailimap_msg_att * msg_att;
2410 struct mailimap_msg_att_item * msg_att_item;
2415 struct mailimap_section * section;
2417 set = mailimap_set_new_single(msg_index);
2419 res = MAILIMAP_ERROR_MEMORY;
2423 section = mailimap_section_new_header();
2424 if (section == NULL) {
2425 res = MAILIMAP_ERROR_MEMORY;
2429 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2430 if (fetch_att == NULL) {
2431 mailimap_section_free(section);
2432 res = MAILIMAP_ERROR_MEMORY;
2436 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2437 if (fetch_type == NULL) {
2438 res = MAILIMAP_ERROR_MEMORY;
2439 goto free_fetch_att;
2442 mailstream_logger = imap_logger_fetch;
2444 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2446 mailstream_logger = imap_logger_cmd;
2447 mailimap_fetch_type_free(fetch_type);
2448 mailimap_set_free(set);
2451 case MAILIMAP_NO_ERROR:
2457 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2458 mailimap_fetch_list_free(fetch_result);
2459 return MAILIMAP_ERROR_FETCH;
2462 msg_att = clist_begin(fetch_result)->data;
2467 if (msg_att->att_list)
2468 cur = clist_begin(msg_att->att_list);
2472 for(; cur != NULL ; cur = clist_next(cur)) {
2473 msg_att_item = clist_content(cur);
2475 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2476 if (msg_att_item->att_data.att_static->att_type ==
2477 MAILIMAP_MSG_ATT_BODY_SECTION) {
2478 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2479 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2481 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2486 mailimap_fetch_list_free(fetch_result);
2489 return MAILIMAP_ERROR_FETCH;
2492 * result_len = text_length;
2494 return MAILIMAP_NO_ERROR;
2497 mailimap_fetch_att_free(fetch_att);
2499 mailimap_set_free(set);
2506 struct fetch_content_param {
2509 const char * filename;
2513 struct fetch_content_result {
2517 static void fetch_content_run(struct etpan_thread_op * op)
2519 struct fetch_content_param * param;
2520 struct fetch_content_result * result;
2522 size_t content_size;
2528 result = op->result;
2534 if (param->with_body)
2535 r = imap_fetch(param->imap, param->msg_index,
2536 &content, &content_size);
2538 r = imap_fetch_header(param->imap, param->msg_index,
2539 &content, &content_size);
2543 if (r == MAILIMAP_NO_ERROR) {
2544 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2546 result->error = MAILIMAP_ERROR_FETCH;
2550 f = fdopen(fd, "wb");
2552 result->error = MAILIMAP_ERROR_FETCH;
2556 r = fwrite(content, 1, content_size, f);
2557 if (r < content_size) {
2558 result->error = MAILIMAP_ERROR_FETCH;
2564 result->error = MAILIMAP_ERROR_FETCH;
2575 claws_unlink(param->filename);
2578 /* mmap_string_unref is a simple free in libetpan
2579 * when it has MMAP_UNAVAILABLE defined */
2580 if (mmap_string_unref(content) != 0)
2584 debug_print("imap fetch_content run - end %i\n", result->error);
2587 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2589 const char * filename)
2591 struct fetch_content_param param;
2592 struct fetch_content_result result;
2595 debug_print("imap fetch_content - begin\n");
2597 imap = get_imap(folder);
2599 param.msg_index = msg_index;
2600 param.filename = filename;
2601 param.with_body = with_body;
2603 threaded_run(folder, ¶m, &result, fetch_content_run);
2605 if (result.error != MAILIMAP_NO_ERROR)
2606 return result.error;
2608 debug_print("imap fetch_content - end\n");
2610 return result.error;
2615 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2620 GSList *tags = NULL;
2624 flag_list = att_dyn->att_list;
2625 if (flag_list == NULL)
2628 for(cur = clist_begin(flag_list) ; cur != NULL ;
2629 cur = clist_next(cur)) {
2630 struct mailimap_flag_fetch * flag_fetch;
2632 flag_fetch = clist_content(cur);
2633 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2636 switch (flag_fetch->fl_flag->fl_type) {
2637 case MAILIMAP_FLAG_ANSWERED:
2638 flags |= MSG_REPLIED;
2640 case MAILIMAP_FLAG_FLAGGED:
2641 flags |= MSG_MARKED;
2643 case MAILIMAP_FLAG_DELETED:
2644 flags |= MSG_DELETED;
2646 case MAILIMAP_FLAG_SEEN:
2647 flags &= ~MSG_UNREAD;
2650 case MAILIMAP_FLAG_KEYWORD:
2651 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2652 flags |= MSG_FORWARDED;
2653 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2655 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2656 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2657 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2660 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2670 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2674 struct mailimap_msg_att_dynamic ** patt_dyn)
2676 clistiter * item_cur;
2680 struct mailimap_msg_att_dynamic * att_dyn;
2687 if (msg_att->att_list)
2688 item_cur = clist_begin(msg_att->att_list);
2691 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2692 struct mailimap_msg_att_item * item;
2694 item = clist_content(item_cur);
2696 switch (item->att_type) {
2697 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2698 switch (item->att_data.att_static->att_type) {
2699 case MAILIMAP_MSG_ATT_UID:
2700 uid = item->att_data.att_static->att_data.att_uid;
2703 case MAILIMAP_MSG_ATT_BODY_SECTION:
2704 if (headers == NULL) {
2705 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2708 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2709 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2714 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2715 if (att_dyn == NULL) {
2716 att_dyn = item->att_data.att_dyn;
2724 if (pheaders != NULL)
2725 * pheaders = headers;
2726 if (pref_size != NULL)
2727 * pref_size = ref_size;
2728 if (patt_dyn != NULL)
2729 * patt_dyn = att_dyn;
2731 return MAIL_NO_ERROR;
2734 static struct imap_fetch_env_info *
2735 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2737 struct imap_fetch_env_info * info;
2741 struct mailimap_msg_att_dynamic * att_dyn;
2743 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2748 info = malloc(sizeof(* info));
2750 info->headers = strdup(headers);
2752 info->flags = imap_flags_to_flags(att_dyn, tags);
2758 imap_fetch_result_to_envelop_list(clist * fetch_result,
2759 carray ** p_env_list)
2765 env_list = carray_new(16);
2767 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2768 cur = clist_next(cur)) {
2769 struct mailimap_msg_att * msg_att;
2770 struct imap_fetch_env_info * env_info;
2771 GSList *tags = NULL;
2773 msg_att = clist_content(cur);
2775 env_info = fetch_to_env_info(msg_att, &tags);
2777 || carray_add(env_list, env_info, NULL) != 0
2778 || carray_add(env_list, tags, NULL) != 0) {
2779 carray_free(env_list);
2780 return MAILIMAP_ERROR_MEMORY;
2783 * p_env_list = env_list;
2785 * p_env_list = NULL;
2788 return MAIL_NO_ERROR;
2791 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2793 struct mailimap_fetch_att * fetch_att;
2797 struct mailimap_header_list * imap_hdrlist;
2798 struct mailimap_section * section;
2800 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2801 "References", "In-Reply-To", NULL
2804 hdrlist = clist_new();
2806 return MAIL_ERROR_MEMORY;
2808 while (headers[i] != NULL) {
2809 header = strdup(headers[i]);
2810 if (header == NULL || clist_append(hdrlist, header) != 0) {
2811 clist_free(hdrlist);
2812 return MAIL_ERROR_MEMORY;
2817 imap_hdrlist = mailimap_header_list_new(hdrlist);
2818 section = mailimap_section_new_header_fields(imap_hdrlist);
2819 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2820 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2822 return MAIL_NO_ERROR;
2825 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2827 struct mailimap_fetch_att * fetch_att;
2828 struct mailimap_section * section;
2830 section = mailimap_section_new_header();
2831 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2832 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2834 return MAIL_NO_ERROR;
2838 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2839 carray ** p_env_list)
2841 struct mailimap_fetch_att * fetch_att;
2842 struct mailimap_fetch_type * fetch_type;
2844 clist * fetch_result;
2846 carray * env_list = NULL;
2850 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2853 fetch_att = mailimap_fetch_att_new_uid();
2854 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2857 fetch_att = mailimap_fetch_att_new_flags();
2858 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2861 fetch_att = mailimap_fetch_att_new_rfc822_size();
2862 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2866 key.len = sizeof(imap);
2867 r = chash_get(courier_workaround_hash, &key, &value);
2869 r = imap_add_envelope_fetch_att(fetch_type);
2871 r = imap_add_header_fetch_att(fetch_type);
2873 if (r != MAILIMAP_NO_ERROR) {
2874 debug_print("add fetch attr: %d\n", r);
2878 mailstream_logger = imap_logger_fetch;
2880 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2882 mailstream_logger = imap_logger_cmd;
2884 case MAILIMAP_NO_ERROR:
2887 mailimap_fetch_type_free(fetch_type);
2888 debug_print("uid_fetch: %d\n", r);
2892 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2893 res = MAILIMAP_ERROR_FETCH;
2894 debug_print("clist_begin = NULL\n");
2898 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2899 mailimap_fetch_list_free(fetch_result);
2901 if (r != MAILIMAP_NO_ERROR) {
2902 mailimap_fetch_type_free(fetch_type);
2903 res = MAILIMAP_ERROR_MEMORY;
2904 debug_print("fetch_result_to_envelop_list: %d\n", res);
2908 mailimap_fetch_type_free(fetch_type);
2910 * p_env_list = env_list;
2912 return MAILIMAP_NO_ERROR;
2918 struct fetch_env_param {
2920 struct mailimap_set * set;
2923 struct fetch_env_result {
2924 carray * fetch_env_result;
2928 static void fetch_env_run(struct etpan_thread_op * op)
2930 struct fetch_env_param * param;
2931 struct fetch_env_result * result;
2936 result = op->result;
2941 r = imap_get_envelopes_list(param->imap, param->set,
2945 result->fetch_env_result = env_list;
2947 debug_print("imap fetch_env run - end %i\n", r);
2950 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2951 carray ** p_env_list)
2953 struct fetch_env_param param;
2954 struct fetch_env_result result;
2957 debug_print("imap fetch_env - begin\n");
2959 imap = get_imap(folder);
2963 if (threaded_run(folder, ¶m, &result, fetch_env_run))
2964 return MAILIMAP_ERROR_INVAL;
2966 if (result.error != MAILIMAP_NO_ERROR) {
2972 key.len = sizeof(imap);
2973 r = chash_get(courier_workaround_hash, &key, &value);
2977 chash_set(courier_workaround_hash, &key, &value, NULL);
2979 threaded_run(folder, ¶m, &result, fetch_env_run);
2983 if (result.error != MAILIMAP_NO_ERROR)
2984 return result.error;
2986 debug_print("imap fetch_env - end\n");
2988 * p_env_list = result.fetch_env_result;
2990 return result.error;
2993 void imap_fetch_env_free(carray * env_list)
2997 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2998 struct imap_fetch_env_info * env_info;
3000 env_info = carray_get(env_list, i);
3001 free(env_info->headers);
3004 carray_free(env_list);
3011 struct append_param {
3013 const char * mailbox;
3014 const char * filename;
3015 struct mailimap_flag_list * flag_list;
3018 struct append_result {
3023 static void append_run(struct etpan_thread_op * op)
3025 struct append_param * param;
3026 struct append_result * result;
3031 struct stat stat_buf;
3034 guint32 uid = 0, val = 0;
3037 result = op->result;
3042 r = stat(param->filename, &stat_buf);
3044 result->error = MAILIMAP_ERROR_APPEND;
3047 size = stat_buf.st_size;
3049 fd = g_open(param->filename, O_RDONLY, 0);
3051 result->error = MAILIMAP_ERROR_APPEND;
3055 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
3056 if (data == (void *) MAP_FAILED) {
3058 result->error = MAILIMAP_ERROR_APPEND;
3062 data = file_read_to_str_no_recode(param->filename);
3064 result->error = MAILIMAP_ERROR_APPEND;
3067 size = strlen(data);
3069 mailstream_logger = imap_logger_append;
3071 r = mailimap_uidplus_append(param->imap, param->mailbox,
3072 param->flag_list, NULL,
3073 data, size, &val, &uid);
3075 mailstream_logger = imap_logger_cmd;
3086 debug_print("imap append run - end %i uid %d\n", r, uid);
3089 int imap_threaded_append(Folder * folder, const char * mailbox,
3090 const char * filename,
3091 struct mailimap_flag_list * flag_list,
3094 struct append_param param;
3095 struct append_result result;
3098 debug_print("imap append - begin\n");
3100 imap = get_imap(folder);
3102 param.mailbox = mailbox;
3103 param.filename = filename;
3104 param.flag_list = flag_list;
3106 threaded_run(folder, ¶m, &result, append_run);
3108 if (result.error != MAILIMAP_NO_ERROR)
3109 return result.error;
3111 debug_print("imap append - end\n");
3115 return result.error;
3121 struct expunge_param {
3125 struct expunge_result {
3129 static void expunge_run(struct etpan_thread_op * op)
3131 struct expunge_param * param;
3132 struct expunge_result * result;
3136 result = op->result;
3140 r = mailimap_expunge(param->imap);
3143 debug_print("imap expunge run - end %i\n", r);
3146 int imap_threaded_expunge(Folder * folder)
3148 struct expunge_param param;
3149 struct expunge_result result;
3151 debug_print("imap expunge - begin\n");
3153 param.imap = get_imap(folder);
3155 threaded_run(folder, ¶m, &result, expunge_run);
3157 debug_print("imap expunge - end\n");
3159 return result.error;
3165 struct mailimap_set * set;
3169 struct copy_result {
3171 struct mailimap_set *source;
3172 struct mailimap_set *dest;
3175 static void copy_run(struct etpan_thread_op * op)
3177 struct copy_param * param;
3178 struct copy_result * result;
3181 struct mailimap_set *source = NULL, *dest = NULL;
3184 result = op->result;
3188 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3189 &val, &source, &dest);
3193 result->source = source;
3194 result->dest = dest;
3196 result->source = NULL;
3197 result->dest = NULL;
3199 debug_print("imap copy run - end %i\n", r);
3202 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3203 const char * mb, struct mailimap_set **source,
3204 struct mailimap_set **dest)
3206 struct copy_param param;
3207 struct copy_result result;
3210 debug_print("imap copy - begin\n");
3212 imap = get_imap(folder);
3217 threaded_run(folder, ¶m, &result, copy_run);
3221 if (result.error != MAILIMAP_NO_ERROR)
3222 return result.error;
3224 *source = result.source;
3225 *dest = result.dest;
3227 debug_print("imap copy - end\n");
3229 return result.error;
3234 struct store_param {
3236 struct mailimap_set * set;
3237 struct mailimap_store_att_flags * store_att_flags;
3240 struct store_result {
3244 static void store_run(struct etpan_thread_op * op)
3246 struct store_param * param;
3247 struct store_result * result;
3251 result = op->result;
3255 r = mailimap_uid_store(param->imap, param->set,
3256 param->store_att_flags);
3260 debug_print("imap store run - end %i\n", r);
3263 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3264 struct mailimap_store_att_flags * store_att_flags)
3266 struct store_param param;
3267 struct store_result result;
3270 debug_print("imap store - begin\n");
3272 imap = get_imap(folder);
3275 param.store_att_flags = store_att_flags;
3277 threaded_run(folder, ¶m, &result, store_run);
3279 if (result.error != MAILIMAP_NO_ERROR)
3280 return result.error;
3282 debug_print("imap store - end\n");
3284 return result.error;
3288 #define ENV_BUFFER_SIZE 512
3290 static void do_exec_command(int fd, const char * command,
3291 const char * servername, uint16_t port)
3295 char env_buffer[ENV_BUFFER_SIZE];
3299 /* Fork again to become a child of init rather than
3300 the etpan client. */
3305 g_setenv("ETPANSERVER", servername, TRUE);
3307 g_unsetenv("ETPANSERVER");
3312 snprintf(porttext, sizeof(porttext), "%d", port);
3313 g_setenv("ETPANPORT", porttext, TRUE);
3316 g_unsetenv("ETPANPORT");
3319 /* Not a lot we can do if there's an error other than bail. */
3320 if (dup2(fd, 0) == -1)
3322 if (dup2(fd, 1) == -1)
3325 /* Should we close stderr and reopen /dev/null? */
3327 maxopen = sysconf(_SC_OPEN_MAX);
3328 for (i=3; i < maxopen; i++)
3332 /* Detach from the controlling tty if we have one. Otherwise,
3333 SSH might do something stupid like trying to use it instead
3334 of running $SSH_ASKPASS. Doh. */
3335 fd = g_open("/dev/tty", O_RDONLY, 0);
3337 ioctl(fd, TIOCNOTTY, NULL);
3340 #endif /* TIOCNOTTY */
3342 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3344 /* Eep. Shouldn't reach this */
3348 static int subcommand_connect(const char *command,
3349 const char *servername, uint16_t port)
3351 /* SEB unsupported on Windows */
3355 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3360 do_exec_command(sockfds[1], command, servername, port);
3362 else if (childpid == -1) {
3370 /* Reap child, leaving grandchild process to run */
3371 waitpid(childpid, NULL, 0);
3376 static int socket_connect_cmd(mailimap * imap, const char * command,
3377 const char * server, int port)
3383 fd = subcommand_connect(command, server, port);
3385 return MAILIMAP_ERROR_STREAM;
3387 s = mailstream_socket_open(fd);
3390 return MAILIMAP_ERROR_STREAM;
3393 r = mailimap_connect(imap, s);
3394 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3395 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3396 mailstream_close(s);
3398 imap->imap_stream = NULL;
3407 struct connect_cmd_param {
3409 const char * command;
3410 const char * server;
3414 struct connect_cmd_result {
3418 static void connect_cmd_run(struct etpan_thread_op * op)
3421 struct connect_cmd_param * param;
3422 struct connect_cmd_result * result;
3425 result = op->result;
3429 r = socket_connect_cmd(param->imap, param->command,
3430 param->server, param->port);
3436 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3437 const char * server, int port)
3439 struct connect_cmd_param param;
3440 struct connect_cmd_result result;
3443 mailimap * imap, * oldimap;
3445 oldimap = get_imap(folder);
3447 imap = mailimap_new(0, NULL);
3450 debug_print("deleting old imap %p\n", oldimap);
3451 delete_imap(folder, oldimap);
3455 key.len = sizeof(folder);
3458 chash_set(session_hash, &key, &value, NULL);
3461 param.command = command;
3462 param.server = server;
3465 threaded_run(folder, ¶m, &result, connect_cmd_run);
3467 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3469 return result.error;
3471 #endif /* G_OS_WIN32 */
3473 void imap_threaded_cancel(Folder * folder)
3477 imap = get_imap(folder);
3478 if (imap->imap_stream != NULL)
3479 mailstream_cancel(imap->imap_stream);
3484 void imap_main_init(void)
3487 void imap_main_done(gboolean have_connectivity)
3490 void imap_main_set_timeout(int sec)
3494 void imap_threaded_cancel(Folder * folder);