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 void delete_imap(Folder *folder, mailimap *imap)
67 key.len = sizeof(folder);
68 chash_delete(session_hash, &key, NULL);
71 key.len = sizeof(imap);
72 chash_delete(courier_workaround_hash, &key, NULL);
73 if (imap && imap->imap_stream) {
74 /* we don't want libetpan to logout */
75 mailstream_close(imap->imap_stream);
76 imap->imap_stream = NULL;
78 debug_print("removing mailimap %p\n", imap);
82 static gboolean thread_manager_event(GIOChannel * source,
83 GIOCondition condition,
90 if (condition & G_IO_IN)
91 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
93 etpan_thread_manager_loop(thread_manager);
98 static void imap_logger_noop(int direction, const char * str, size_t size)
100 /* inhibit logging */
103 static void imap_logger_cmd(int direction, const char * str, size_t size)
110 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
113 buf = malloc(size+1);
114 memset(buf, 0, size+1);
115 strncpy(buf, str, size);
118 if (!strncmp(buf, "<<<<<<<", 7)
119 || !strncmp(buf, ">>>>>>>", 7)) {
123 while (strstr(buf, "\r"))
124 *strstr(buf, "\r") = ' ';
125 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
126 buf[strlen(buf)-1] = '\0';
128 lines = g_strsplit(buf, "\n", -1);
130 while (lines[i] && *lines[i]) {
131 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
138 static void imap_logger_fetch(int direction, const char * str, size_t size)
144 if (size > 128 && !direction) {
145 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
149 buf = malloc(size+1);
150 memset(buf, 0, size+1);
151 strncpy(buf, str, size);
153 if (!strncmp(buf, "<<<<<<<", 7)
154 || !strncmp(buf, ">>>>>>>", 7)) {
158 while (strstr(buf, "\r"))
159 *strstr(buf, "\r") = ' ';
160 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
161 buf[strlen(buf)-1] = '\0';
163 lines = g_strsplit(buf, "\n", -1);
165 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
166 while (lines[i] && *lines[i]) {
167 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
171 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
177 static void imap_logger_uid(int direction, const char * str, size_t size)
184 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
187 buf = malloc(size+1);
188 memset(buf, 0, size+1);
189 strncpy(buf, str, size);
191 if (!strncmp(buf, "<<<<<<<", 7)
192 || !strncmp(buf, ">>>>>>>", 7)) {
196 while (strstr(buf, "\r"))
197 *strstr(buf, "\r") = ' ';
198 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
199 buf[strlen(buf)-1] = '\0';
201 lines = g_strsplit(buf, "\n", -1);
203 while (lines[i] && *lines[i]) {
204 int llen = strlen(lines[i]);
206 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
209 strncpy2(tmp, lines[i], 63);
210 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
219 static void imap_logger_append(int direction, const char * str, size_t size)
226 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
228 } else if (direction == 0 && size > 64) {
229 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
232 buf = malloc(size+1);
233 memset(buf, 0, size+1);
234 strncpy(buf, str, size);
236 if (!strncmp(buf, "<<<<<<<", 7)
237 || !strncmp(buf, ">>>>>>>", 7)) {
241 while (strstr(buf, "\r"))
242 *strstr(buf, "\r") = ' ';
243 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
244 buf[strlen(buf)-1] = '\0';
246 lines = g_strsplit(buf, "\n", -1);
248 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
249 while (lines[i] && *lines[i]) {
250 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
254 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
260 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
261 gboolean etpan_skip_ssl_cert_check = FALSE;
262 extern void mailsasl_ref(void);
264 void imap_main_init(gboolean skip_ssl_cert_check)
266 int fd_thread_manager;
268 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
269 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
270 mailstream_network_delay.tv_usec = 0;
272 mailstream_debug = 1;
273 mailstream_logger = imap_logger_cmd;
276 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
277 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
278 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
280 thread_manager = etpan_thread_manager_new();
282 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
285 io_channel = g_io_channel_unix_new(fd_thread_manager);
287 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
289 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
290 thread_manager_event,
295 void imap_main_set_timeout(int sec)
297 mailstream_network_delay.tv_sec = sec;
298 mailstream_network_delay.tv_usec = 0;
301 void imap_main_done(gboolean have_connectivity)
303 imap_disconnect_all(have_connectivity);
304 etpan_thread_manager_stop(thread_manager);
305 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
308 etpan_thread_manager_join(thread_manager);
310 g_source_remove(thread_manager_signal);
311 g_io_channel_unref(io_channel);
313 etpan_thread_manager_free(thread_manager);
315 chash_free(courier_workaround_hash);
316 chash_free(session_hash);
317 chash_free(imap_hash);
320 void imap_init(Folder * folder)
322 struct etpan_thread * thread;
326 thread = etpan_thread_manager_get_thread(thread_manager);
329 key.len = sizeof(folder);
333 chash_set(imap_hash, &key, &value, NULL);
336 void imap_done(Folder * folder)
338 struct etpan_thread * thread;
344 key.len = sizeof(folder);
346 r = chash_get(imap_hash, &key, &value);
352 etpan_thread_unbind(thread);
354 chash_delete(imap_hash, &key, NULL);
356 debug_print("remove thread");
359 static struct etpan_thread * get_thread(Folder * folder)
361 struct etpan_thread * thread;
366 key.len = sizeof(folder);
368 chash_get(imap_hash, &key, &value);
374 static mailimap * get_imap(Folder * folder)
382 key.len = sizeof(folder);
384 r = chash_get(session_hash, &key, &value);
389 debug_print("found imap %p\n", imap);
393 static gboolean cb_show_error(gpointer data)
395 mainwindow_show_error();
399 static void generic_cb(int cancelled, void * result, void * callback_data)
401 struct etpan_thread_op * op;
403 op = (struct etpan_thread_op *) callback_data;
405 debug_print("generic_cb\n");
406 if (op->imap && op->imap->imap_response_info &&
407 op->imap->imap_response_info->rsp_alert) {
408 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
409 op->imap->imap_response_info->rsp_alert);
410 g_timeout_add(10, cb_show_error, NULL);
415 static void threaded_run(Folder * folder, void * param, void * result,
416 void (* func)(struct etpan_thread_op * ))
418 struct etpan_thread_op * op;
419 struct etpan_thread * thread;
421 imap_folder_ref(folder);
423 op = etpan_thread_op_new();
425 op->imap = get_imap(folder);
431 op->callback = generic_cb;
432 op->callback_data = op;
437 thread = get_thread(folder);
438 etpan_thread_op_schedule(thread, op);
440 while (!op->finished) {
441 gtk_main_iteration();
444 etpan_thread_op_free(op);
446 imap_folder_unref(folder);
452 struct connect_param {
454 PrefsAccount *account;
459 struct connect_result {
463 #define CHECK_IMAP() { \
464 if (!param->imap) { \
465 result->error = MAILIMAP_ERROR_BAD_STATE; \
470 static void connect_run(struct etpan_thread_op * op)
473 struct connect_param * param;
474 struct connect_result * result;
481 r = mailimap_socket_connect(param->imap,
482 param->server, param->port);
488 int imap_threaded_connect(Folder * folder, const char * server, int port)
490 struct connect_param param;
491 struct connect_result result;
494 mailimap * imap, * oldimap;
496 oldimap = get_imap(folder);
498 imap = mailimap_new(0, NULL);
501 debug_print("deleting old imap %p\n", oldimap);
502 delete_imap(folder, oldimap);
506 key.len = sizeof(folder);
509 chash_set(session_hash, &key, &value, NULL);
512 param.server = server;
516 threaded_run(folder, ¶m, &result, connect_run);
518 debug_print("connect ok %i with imap %p\n", result.error, imap);
523 static void connect_ssl_run(struct etpan_thread_op * op)
526 struct connect_param * param;
527 struct connect_result * result;
534 r = mailimap_ssl_connect_with_callback(param->imap,
535 param->server, param->port,
536 etpan_connect_ssl_context_cb, param->account);
540 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
542 struct connect_param param;
543 struct connect_result result;
546 mailimap * imap, * oldimap;
547 gboolean accept_if_valid = FALSE;
549 oldimap = get_imap(folder);
551 imap = mailimap_new(0, NULL);
554 debug_print("deleting old imap %p\n", oldimap);
555 delete_imap(folder, oldimap);
559 key.len = sizeof(folder);
562 chash_set(session_hash, &key, &value, NULL);
565 param.server = server;
567 param.account = folder->account;
570 accept_if_valid = folder->account->ssl_certs_auto_accept;
573 threaded_run(folder, ¶m, &result, connect_ssl_run);
575 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
576 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
577 if (etpan_certificate_check(imap->imap_stream, server, port,
578 accept_if_valid) != TRUE)
579 result.error = MAILIMAP_ERROR_SSL;
581 debug_print("connect %d with imap %p\n", result.error, imap);
592 struct mailimap_capability_data *caps;
595 static void capability_run(struct etpan_thread_op * op)
598 struct capa_param * param;
599 struct capa_result * result;
600 struct mailimap_capability_data *caps;
607 r = mailimap_capability(param->imap, &caps);
610 result->caps = (r == 0 ? caps : NULL);
614 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
616 struct capa_param param;
617 struct capa_result result;
620 imap = get_imap(folder);
624 threaded_run(folder, ¶m, &result, capability_run);
626 debug_print("capa %d\n", result.error);
635 struct disconnect_param {
639 struct disconnect_result {
643 static void disconnect_run(struct etpan_thread_op * op)
646 struct disconnect_param * param;
647 struct disconnect_result * result;
654 r = mailimap_logout(param->imap);
659 void imap_threaded_disconnect(Folder * folder)
661 struct connect_param param;
662 struct connect_result result;
665 imap = get_imap(folder);
667 debug_print("was disconnected\n");
673 threaded_run(folder, ¶m, &result, disconnect_run);
675 if (imap == get_imap(folder)) {
676 debug_print("deleting old imap %p\n", imap);
677 delete_imap(folder, imap);
679 debug_print("imap already deleted %p\n", imap);
682 debug_print("disconnect ok\n");
689 const char * wildcard;
698 static void list_run(struct etpan_thread_op * op)
700 struct list_param * param;
701 struct list_result * result;
712 if (param->base == NULL || param->wildcard == NULL) {
715 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
719 r = mailimap_lsub(param->imap, param->base,
720 param->wildcard, &list);
722 r = mailimap_list(param->imap, param->base,
723 param->wildcard, &list);
726 debug_print("imap list run - end\n");
729 int imap_threaded_list(Folder * folder, const char * base,
730 const char * wildcard,
733 struct list_param param;
734 struct list_result result;
736 debug_print("imap list - begin\n");
738 param.imap = get_imap(folder);
740 param.wildcard = wildcard;
741 param.sub_only = FALSE;
743 threaded_run(folder, ¶m, &result, list_run);
745 * p_result = result.list;
747 debug_print("imap list - end %p\n", result.list);
752 int imap_threaded_lsub(Folder * folder, const char * base,
753 const char * wildcard,
756 struct list_param param;
757 struct list_result result;
759 debug_print("imap lsub - begin\n");
761 param.imap = get_imap(folder);
763 param.wildcard = wildcard;
764 param.sub_only = TRUE;
766 threaded_run(folder, ¶m, &result, list_run);
768 * p_result = result.list;
770 debug_print("imap lsub - end %p\n", result.list);
775 struct subscribe_param {
781 struct subscribe_result {
785 static void subscribe_run(struct etpan_thread_op * op)
787 struct subscribe_param * param;
788 struct subscribe_result * result;
796 if (param->mb == NULL) {
798 debug_print("no mb\n");
801 if (param->subscribe)
802 r = mailimap_subscribe(param->imap, param->mb);
804 r = mailimap_unsubscribe(param->imap, param->mb);
806 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
809 int imap_threaded_subscribe(Folder * folder, const char * mb,
812 struct subscribe_param param;
813 struct subscribe_result result;
815 debug_print("imap list - begin\n");
817 param.imap = get_imap(folder);
819 param.subscribe = subscribe;
821 threaded_run(folder, ¶m, &result, subscribe_run);
829 const char * password;
834 struct login_result {
838 static void login_run(struct etpan_thread_op * op)
840 struct login_param * param;
841 struct login_result * result;
843 #ifdef DISABLE_LOG_DURING_LOGIN
852 #ifdef DISABLE_LOG_DURING_LOGIN
853 old_debug = mailstream_debug;
854 mailstream_debug = 0;
856 if (!strcmp(param->type, "LOGIN"))
857 r = mailimap_login(param->imap,
858 param->login, param->password);
859 else if (!strcmp(param->type, "GSSAPI"))
860 r = mailimap_authenticate(param->imap,
861 param->type, param->server, NULL, NULL,
862 param->login, param->login,
863 param->password, NULL);
865 r = mailimap_authenticate(param->imap,
866 param->type, NULL, NULL, NULL,
867 param->login, param->login,
868 param->password, NULL);
869 #ifdef DISABLE_LOG_DURING_LOGIN
870 mailstream_debug = old_debug;
874 if (param->imap->imap_response)
875 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
876 debug_print("imap login run - end %i\n", r);
879 int imap_threaded_login(Folder * folder,
880 const char * login, const char * password,
883 struct login_param param;
884 struct login_result result;
886 debug_print("imap login - begin\n");
888 param.imap = get_imap(folder);
890 param.password = password;
892 if (folder && folder->account)
893 param.server = folder->account->recv_server;
897 threaded_run(folder, ¶m, &result, login_run);
899 debug_print("imap login - end\n");
905 struct status_param {
908 struct mailimap_status_att_list * status_att_list;
911 struct status_result {
913 struct mailimap_mailbox_data_status * data_status;
916 static void status_run(struct etpan_thread_op * op)
918 struct status_param * param;
919 struct status_result * result;
927 r = mailimap_status(param->imap, param->mb,
928 param->status_att_list,
929 &result->data_status);
932 debug_print("imap status run - end %i\n", r);
935 int imap_threaded_status(Folder * folder, const char * mb,
936 struct mailimap_mailbox_data_status ** data_status,
939 struct status_param param;
940 struct status_result result;
941 struct mailimap_status_att_list * status_att_list;
943 debug_print("imap status - begin\n");
945 status_att_list = mailimap_status_att_list_new_empty();
947 mailimap_status_att_list_add(status_att_list,
948 MAILIMAP_STATUS_ATT_MESSAGES);
951 mailimap_status_att_list_add(status_att_list,
952 MAILIMAP_STATUS_ATT_RECENT);
955 mailimap_status_att_list_add(status_att_list,
956 MAILIMAP_STATUS_ATT_UIDNEXT);
959 mailimap_status_att_list_add(status_att_list,
960 MAILIMAP_STATUS_ATT_UIDVALIDITY);
963 mailimap_status_att_list_add(status_att_list,
964 MAILIMAP_STATUS_ATT_UNSEEN);
966 param.imap = get_imap(folder);
968 param.status_att_list = status_att_list;
970 threaded_run(folder, ¶m, &result, status_run);
972 debug_print("imap status - end\n");
974 * data_status = result.data_status;
976 mailimap_status_att_list_free(status_att_list);
991 static void noop_run(struct etpan_thread_op * op)
993 struct noop_param * param;
994 struct noop_result * result;
1002 r = mailimap_noop(param->imap);
1005 debug_print("imap noop run - end %i\n", r);
1008 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1009 unsigned int *p_recent,
1010 unsigned int *p_expunge,
1011 unsigned int *p_unseen,
1012 unsigned int *p_uidnext,
1013 unsigned int *p_uidval)
1015 struct noop_param param;
1016 struct noop_result result;
1019 debug_print("imap noop - begin\n");
1021 imap = get_imap(folder);
1024 threaded_run(folder, ¶m, &result, noop_run);
1026 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1027 * p_exists = imap->imap_selection_info->sel_exists;
1028 * p_recent = imap->imap_selection_info->sel_recent;
1029 * p_unseen = imap->imap_selection_info->sel_unseen;
1030 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1031 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1039 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1040 imap->imap_response_info->rsp_expunged != NULL) {
1041 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1045 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1046 *p_exists, *p_recent, *p_expunge, *p_unseen,
1047 *p_uidnext, *p_uidval);
1049 return result.error;
1053 struct starttls_result {
1057 static void starttls_run(struct etpan_thread_op * op)
1059 struct connect_param * param;
1060 struct starttls_result * result;
1064 result = op->result;
1068 r = mailimap_starttls(param->imap);
1071 debug_print("imap starttls run - end %i\n", r);
1074 mailimap *imap = param->imap;
1075 mailstream_low *plain_low = NULL;
1076 mailstream_low *tls_low = NULL;
1079 plain_low = mailstream_get_low(imap->imap_stream);
1080 fd = mailstream_low_get_fd(plain_low);
1082 debug_print("imap starttls run - can't get fd\n");
1083 result->error = MAILIMAP_ERROR_STREAM;
1087 tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
1088 if (tls_low == NULL) {
1089 debug_print("imap starttls run - can't tls_open\n");
1090 result->error = MAILIMAP_ERROR_STREAM;
1093 mailstream_low_free(plain_low);
1094 mailstream_set_low(imap->imap_stream, tls_low);
1098 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1100 struct connect_param param;
1101 struct starttls_result result;
1102 gboolean accept_if_valid = FALSE;
1104 debug_print("imap starttls - begin\n");
1106 param.imap = get_imap(folder);
1107 param.server = host;
1109 param.account = folder->account;
1111 if (folder->account)
1112 accept_if_valid = folder->account->ssl_certs_auto_accept;
1114 threaded_run(folder, ¶m, &result, starttls_run);
1116 debug_print("imap starttls - end\n");
1118 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1119 if (etpan_certificate_check(param.imap->imap_stream, host, port,
1120 accept_if_valid) != TRUE)
1121 return MAILIMAP_ERROR_SSL;
1123 return result.error;
1128 struct create_param {
1133 struct create_result {
1137 static void create_run(struct etpan_thread_op * op)
1139 struct create_param * param;
1140 struct create_result * result;
1144 result = op->result;
1148 r = mailimap_create(param->imap, param->mb);
1151 debug_print("imap create run - end %i\n", r);
1154 int imap_threaded_create(Folder * folder, const char * mb)
1156 struct create_param param;
1157 struct create_result result;
1159 debug_print("imap create - begin\n");
1161 param.imap = get_imap(folder);
1164 threaded_run(folder, ¶m, &result, create_run);
1166 debug_print("imap create - end\n");
1168 return result.error;
1174 struct rename_param {
1177 const char * new_name;
1180 struct rename_result {
1184 static void rename_run(struct etpan_thread_op * op)
1186 struct rename_param * param;
1187 struct rename_result * result;
1191 result = op->result;
1195 r = mailimap_rename(param->imap, param->mb, param->new_name);
1198 debug_print("imap rename run - end %i\n", r);
1201 int imap_threaded_rename(Folder * folder,
1202 const char * mb, const char * new_name)
1204 struct rename_param param;
1205 struct rename_result result;
1207 debug_print("imap rename - begin\n");
1209 param.imap = get_imap(folder);
1211 param.new_name = new_name;
1213 threaded_run(folder, ¶m, &result, rename_run);
1215 debug_print("imap rename - end\n");
1217 return result.error;
1223 struct delete_param {
1228 struct delete_result {
1232 static void delete_run(struct etpan_thread_op * op)
1234 struct delete_param * param;
1235 struct delete_result * result;
1239 result = op->result;
1243 r = mailimap_delete(param->imap, param->mb);
1246 debug_print("imap delete run - end %i\n", r);
1249 int imap_threaded_delete(Folder * folder, const char * mb)
1251 struct delete_param param;
1252 struct delete_result result;
1254 debug_print("imap delete - begin\n");
1256 param.imap = get_imap(folder);
1259 threaded_run(folder, ¶m, &result, delete_run);
1261 debug_print("imap delete - end\n");
1263 return result.error;
1268 struct select_param {
1273 struct select_result {
1277 static void select_run(struct etpan_thread_op * op)
1279 struct select_param * param;
1280 struct select_result * result;
1284 result = op->result;
1288 r = mailimap_select(param->imap, param->mb);
1291 debug_print("imap select run - end %i\n", r);
1294 int imap_threaded_select(Folder * folder, const char * mb,
1295 gint * exists, gint * recent, gint * unseen,
1296 guint32 * uid_validity,gint *can_create_flags,
1299 struct select_param param;
1300 struct select_result result;
1303 debug_print("imap select - begin\n");
1305 imap = get_imap(folder);
1309 threaded_run(folder, ¶m, &result, select_run);
1311 if (result.error != MAILIMAP_NO_ERROR)
1312 return result.error;
1314 if (!imap || imap->imap_selection_info == NULL)
1315 return MAILIMAP_ERROR_PARSE;
1317 * exists = imap->imap_selection_info->sel_exists;
1318 * recent = imap->imap_selection_info->sel_recent;
1319 * unseen = imap->imap_selection_info->sel_unseen;
1320 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1321 * can_create_flags = FALSE;
1323 if (imap->imap_selection_info->sel_perm_flags) {
1324 GSList *t_flags = NULL;
1325 clistiter *cur = NULL;
1326 if (imap->imap_selection_info->sel_perm_flags)
1327 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1329 for (; cur; cur = clist_next(cur)) {
1330 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1331 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1332 *can_create_flags = TRUE;
1333 else if (flag->fl_flag &&
1334 flag->fl_flag->fl_type == 6 &&
1335 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1336 *can_create_flags = TRUE;
1337 if (flag->fl_flag && ok_flags) {
1338 MsgPermFlags c_flag = 0;
1339 switch (flag->fl_flag->fl_type) {
1340 case MAILIMAP_FLAG_ANSWERED:
1341 c_flag = IMAP_FLAG_ANSWERED;
1343 case MAILIMAP_FLAG_FLAGGED:
1344 c_flag = IMAP_FLAG_FLAGGED;
1346 case MAILIMAP_FLAG_DELETED:
1347 c_flag = IMAP_FLAG_DELETED;
1349 case MAILIMAP_FLAG_DRAFT:
1350 c_flag = IMAP_FLAG_DRAFT;
1352 case MAILIMAP_FLAG_SEEN:
1353 c_flag = IMAP_FLAG_SEEN;
1355 case MAILIMAP_FLAG_KEYWORD:
1356 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1357 c_flag = IMAP_FLAG_FORWARDED;
1358 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1359 c_flag = IMAP_FLAG_SPAM;
1360 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1361 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1362 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1363 c_flag = IMAP_FLAG_HAM;
1369 t_flags = g_slist_prepend(t_flags,
1370 GUINT_TO_POINTER(c_flag));
1375 *ok_flags = t_flags;
1377 debug_print("imap select - end\n");
1379 return result.error;
1382 static void close_run(struct etpan_thread_op * op)
1384 struct select_param * param;
1385 struct select_result * result;
1389 result = op->result;
1393 r = mailimap_close(param->imap);
1396 debug_print("imap close run - end %i\n", r);
1399 int imap_threaded_close(Folder * folder)
1401 struct select_param param;
1402 struct select_result result;
1405 debug_print("imap close - begin\n");
1407 imap = get_imap(folder);
1410 threaded_run(folder, ¶m, &result, close_run);
1412 if (result.error != MAILIMAP_NO_ERROR)
1413 return result.error;
1415 debug_print("imap close - end\n");
1417 return result.error;
1420 struct examine_param {
1425 struct examine_result {
1429 static void examine_run(struct etpan_thread_op * op)
1431 struct examine_param * param;
1432 struct examine_result * result;
1436 result = op->result;
1440 r = mailimap_examine(param->imap, param->mb);
1443 debug_print("imap examine run - end %i\n", r);
1446 int imap_threaded_examine(Folder * folder, const char * mb,
1447 gint * exists, gint * recent, gint * unseen,
1448 guint32 * uid_validity)
1450 struct examine_param param;
1451 struct examine_result result;
1454 debug_print("imap examine - begin\n");
1456 imap = get_imap(folder);
1460 threaded_run(folder, ¶m, &result, examine_run);
1462 if (result.error != MAILIMAP_NO_ERROR)
1463 return result.error;
1465 if (!imap || imap->imap_selection_info == NULL)
1466 return MAILIMAP_ERROR_PARSE;
1468 * exists = imap->imap_selection_info->sel_exists;
1469 * recent = imap->imap_selection_info->sel_recent;
1470 * unseen = imap->imap_selection_info->sel_unseen;
1471 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1473 debug_print("imap examine - end\n");
1475 return result.error;
1481 struct search_param {
1484 const char *charset;
1485 struct mailimap_set * set;
1489 struct search_result {
1491 clist * search_result;
1494 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1496 return mailimap_set_item_new(orig->set_first, orig->set_last);
1499 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1501 clist *list = orig ? orig->set_list : NULL;
1502 clist *newlist = clist_new();
1507 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1508 clist_append(newlist,
1509 sc_mailimap_set_item_copy(
1510 (struct mailimap_set_item *)clist_content(cur)));
1511 return mailimap_set_new(newlist);
1514 static void search_run(struct etpan_thread_op * op)
1516 struct search_param * param;
1517 struct search_result * result;
1519 struct mailimap_search_key * key = NULL;
1520 struct mailimap_search_key * uid_key = NULL;
1521 struct mailimap_search_key * search_type_key = NULL;
1522 clist * search_result;
1525 result = op->result;
1529 /* we copy the mailimap_set because freeing the key is recursive */
1530 if (param->set != NULL) {
1531 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1532 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1533 uid_key = mailimap_search_key_new_all();
1535 switch (param->type) {
1536 case IMAP_SEARCH_TYPE_SIMPLE:
1537 search_type_key = NULL;
1539 case IMAP_SEARCH_TYPE_SEEN:
1540 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1542 case IMAP_SEARCH_TYPE_UNSEEN:
1543 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1545 case IMAP_SEARCH_TYPE_ANSWERED:
1546 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1548 case IMAP_SEARCH_TYPE_FLAGGED:
1549 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1551 case IMAP_SEARCH_TYPE_DELETED:
1552 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1554 case IMAP_SEARCH_TYPE_FORWARDED:
1555 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1557 case IMAP_SEARCH_TYPE_SPAM:
1558 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1560 case IMAP_SEARCH_TYPE_KEYED:
1561 search_type_key = param->key;
1565 if (search_type_key != NULL) {
1566 if (param->set != NULL) {
1567 key = mailimap_search_key_new_multiple_empty();
1568 mailimap_search_key_multiple_add(key, search_type_key);
1569 mailimap_search_key_multiple_add(key, uid_key);
1571 key = search_type_key;
1573 } else if (uid_key != NULL) {
1578 g_warning("no key!");
1579 result = op->result;
1581 result->search_result = NULL;
1583 mailstream_logger = imap_logger_uid;
1585 r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
1587 mailstream_logger = imap_logger_cmd;
1589 /* free the key (with the imapset) */
1590 mailimap_search_key_free(key);
1593 result->search_result = search_result;
1595 debug_print("imap search run - end %i\n", result->error);
1598 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1599 const char *charset, struct mailimap_set * set,
1600 clist ** search_result)
1602 struct search_param param;
1603 struct search_result result;
1606 debug_print("imap search - begin\n");
1608 imap = get_imap(folder);
1611 param.charset = charset;
1612 param.type = search_type;
1615 threaded_run(folder, ¶m, &result, search_run);
1617 if (result.error != MAILIMAP_NO_ERROR)
1618 return result.error;
1620 debug_print("imap search - end\n");
1622 * search_result = result.search_result;
1624 return result.error;
1628 struct _IMAPSearchKey {
1629 struct mailimap_search_key* key;
1632 IMAPSearchKey* imap_search_new(gint criteria,
1633 const gchar *header,
1637 char* sk_bcc = NULL;
1638 struct mailimap_date* sk_before = NULL;
1639 char* sk_body = NULL;
1641 char* sk_from = NULL;
1642 char* sk_keyword = NULL;
1643 struct mailimap_date* sk_on = NULL;
1644 struct mailimap_date* sk_since = NULL;
1645 char* sk_subject = NULL;
1646 char* sk_text = NULL;
1648 char* sk_unkeyword = NULL;
1649 char* sk_header_name = NULL;
1650 char* sk_header_value = NULL;
1651 uint32_t sk_larger = 0;
1652 struct mailimap_search_key* sk_not = NULL;
1653 struct mailimap_search_key* sk_or1 = NULL;
1654 struct mailimap_search_key* sk_or2 = NULL;
1655 struct mailimap_date* sk_sentbefore = NULL;
1656 struct mailimap_date* sk_senton = NULL;
1657 struct mailimap_date* sk_sentsince = NULL;
1658 uint32_t sk_smaller = 0;
1659 struct mailimap_set* sk_uid = NULL;
1660 struct mailimap_set* sk_set = NULL;
1661 clist* sk_multiple = NULL;
1662 int etpan_matcher_type;
1665 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1666 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1667 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1668 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1669 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1670 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1671 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1673 case IMAP_SEARCH_CRITERIA_TAG:
1674 sk_keyword = strdup(expr);
1675 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1678 case IMAP_SEARCH_CRITERIA_SUBJECT:
1679 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1680 sk_subject = strdup(expr);
1683 case IMAP_SEARCH_CRITERIA_TO:
1684 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1685 sk_to = strdup(expr);
1688 case IMAP_SEARCH_CRITERIA_CC:
1689 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1690 sk_cc = strdup(expr);
1693 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1694 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1697 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1700 localtime_r(&limit, &tm);
1701 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1702 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1703 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1705 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1706 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1711 case IMAP_SEARCH_CRITERIA_BODY:
1712 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1713 sk_body = strdup(expr);
1716 case IMAP_SEARCH_CRITERIA_MESSAGE:
1717 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1718 sk_text = strdup(expr);
1721 case IMAP_SEARCH_CRITERIA_HEADER:
1722 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1723 sk_header_name = strdup(header);
1724 sk_header_value = strdup(expr);
1727 case IMAP_SEARCH_CRITERIA_FROM:
1728 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1729 sk_from = strdup(expr);
1732 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1733 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1737 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1738 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1746 return mailimap_search_key_new(etpan_matcher_type,
1747 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1748 sk_on, sk_since, sk_subject, sk_text, sk_to,
1749 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1750 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1751 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1754 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1756 return mailimap_search_key_new_not(key);
1759 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1761 return mailimap_search_key_new_or(l, r);
1764 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1766 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1767 mailimap_search_key_multiple_add(result, l);
1768 mailimap_search_key_multiple_add(result, r);
1773 void imap_search_free(IMAPSearchKey* key)
1778 mailimap_search_key_free(key);
1783 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1787 struct mailimap_msg_att_dynamic ** patt_dyn);
1790 result_to_uid_list(clist * fetch_result, carray ** result)
1792 clistiter * cur = NULL;
1797 tab = carray_new(128);
1799 res = MAILIMAP_ERROR_MEMORY;
1804 cur = clist_begin(fetch_result);
1806 for(; cur != NULL ; cur = clist_next(cur)) {
1807 struct mailimap_msg_att * msg_att;
1811 msg_att = clist_content(cur);
1814 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1816 puid = malloc(sizeof(* puid));
1818 res = MAILIMAP_ERROR_MEMORY;
1823 r = carray_add(tab, puid, NULL);
1826 res = MAILIMAP_ERROR_MEMORY;
1833 return MAILIMAP_NO_ERROR;
1836 imap_fetch_uid_list_free(tab);
1841 static int imap_get_messages_list(mailimap * imap,
1842 uint32_t first_index,
1847 struct mailimap_fetch_att * fetch_att;
1848 struct mailimap_fetch_type * fetch_type;
1849 struct mailimap_set * set;
1850 clist * fetch_result;
1853 set = mailimap_set_new_interval(first_index, 0);
1855 res = MAILIMAP_ERROR_MEMORY;
1859 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1860 if (fetch_type == NULL) {
1861 res = MAILIMAP_ERROR_MEMORY;
1865 fetch_att = mailimap_fetch_att_new_uid();
1866 if (fetch_att == NULL) {
1867 res = MAILIMAP_ERROR_MEMORY;
1868 goto free_fetch_type;
1871 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1872 if (r != MAILIMAP_NO_ERROR) {
1873 mailimap_fetch_att_free(fetch_att);
1874 res = MAILIMAP_ERROR_MEMORY;
1875 goto free_fetch_type;
1878 mailstream_logger = imap_logger_fetch;
1880 r = mailimap_uid_fetch(imap, set,
1881 fetch_type, &fetch_result);
1883 mailstream_logger = imap_logger_cmd;
1884 mailimap_fetch_type_free(fetch_type);
1885 mailimap_set_free(set);
1887 if (r != MAILIMAP_NO_ERROR) {
1893 r = result_to_uid_list(fetch_result, &env_list);
1894 mailimap_fetch_list_free(fetch_result);
1896 * result = env_list;
1898 return MAILIMAP_NO_ERROR;
1901 mailimap_fetch_type_free(fetch_type);
1903 mailimap_set_free(set);
1911 struct fetch_uid_param {
1913 uint32_t first_index;
1916 struct fetch_uid_result {
1918 carray * fetch_result;
1921 static void fetch_uid_run(struct etpan_thread_op * op)
1923 struct fetch_uid_param * param;
1924 struct fetch_uid_result * result;
1925 carray * fetch_result;
1929 result = op->result;
1933 fetch_result = NULL;
1934 mailstream_logger = imap_logger_noop;
1935 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1937 r = imap_get_messages_list(param->imap, param->first_index,
1940 mailstream_logger = imap_logger_cmd;
1943 result->fetch_result = fetch_result;
1944 debug_print("imap fetch_uid run - end %i\n", r);
1947 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1948 carray ** fetch_result)
1950 struct fetch_uid_param param;
1951 struct fetch_uid_result result;
1954 debug_print("imap fetch_uid - begin\n");
1956 imap = get_imap(folder);
1958 param.first_index = first_index;
1960 threaded_run(folder, ¶m, &result, fetch_uid_run);
1962 if (result.error != MAILIMAP_NO_ERROR)
1963 return result.error;
1965 debug_print("imap fetch_uid - end\n");
1967 * fetch_result = result.fetch_result;
1969 return result.error;
1973 void imap_fetch_uid_list_free(carray * uid_list)
1977 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1980 puid = carray_get(uid_list, i);
1983 carray_free(uid_list);
1989 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1992 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1994 clistiter * cur = NULL;
1998 GSList *tags = NULL;
2000 tab = carray_new(128);
2002 res = MAILIMAP_ERROR_MEMORY;
2007 cur = clist_begin(fetch_result);
2009 for(; cur != NULL ; cur = clist_next(cur)) {
2010 struct mailimap_msg_att * msg_att;
2013 struct mailimap_msg_att_dynamic * att_dyn;
2019 msg_att = clist_content(cur);
2023 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2026 if (att_dyn == NULL)
2029 flags = imap_flags_to_flags(att_dyn, &tags);
2031 puid = malloc(sizeof(* puid));
2033 res = MAILIMAP_ERROR_MEMORY;
2038 r = carray_add(tab, puid, NULL);
2041 res = MAILIMAP_ERROR_MEMORY;
2044 pflags = malloc(sizeof(* pflags));
2045 if (pflags == NULL) {
2046 res = MAILIMAP_ERROR_MEMORY;
2050 r = carray_add(tab, pflags, NULL);
2053 res = MAILIMAP_ERROR_MEMORY;
2056 r = carray_add(tab, tags, NULL);
2059 res = MAILIMAP_ERROR_MEMORY;
2066 return MAILIMAP_NO_ERROR;
2069 imap_fetch_uid_flags_list_free(tab);
2070 slist_free_strings_full(tags);
2075 static int imap_get_messages_flags_list(mailimap * imap,
2076 uint32_t first_index,
2081 struct mailimap_fetch_att * fetch_att;
2082 struct mailimap_fetch_type * fetch_type;
2083 struct mailimap_set * set;
2084 clist * fetch_result;
2087 set = mailimap_set_new_interval(first_index, 0);
2089 res = MAILIMAP_ERROR_MEMORY;
2093 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2094 if (fetch_type == NULL) {
2095 res = MAILIMAP_ERROR_MEMORY;
2099 fetch_att = mailimap_fetch_att_new_flags();
2100 if (fetch_att == NULL) {
2101 res = MAILIMAP_ERROR_MEMORY;
2102 goto free_fetch_type;
2105 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2106 if (r != MAILIMAP_NO_ERROR) {
2107 mailimap_fetch_att_free(fetch_att);
2108 res = MAILIMAP_ERROR_MEMORY;
2109 goto free_fetch_type;
2112 fetch_att = mailimap_fetch_att_new_uid();
2113 if (fetch_att == NULL) {
2114 res = MAILIMAP_ERROR_MEMORY;
2115 goto free_fetch_type;
2118 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2119 if (r != MAILIMAP_NO_ERROR) {
2120 mailimap_fetch_att_free(fetch_att);
2121 res = MAILIMAP_ERROR_MEMORY;
2122 goto free_fetch_type;
2125 mailstream_logger = imap_logger_fetch;
2127 r = mailimap_uid_fetch(imap, set,
2128 fetch_type, &fetch_result);
2130 mailstream_logger = imap_logger_cmd;
2131 mailimap_fetch_type_free(fetch_type);
2132 mailimap_set_free(set);
2134 if (r != MAILIMAP_NO_ERROR) {
2140 r = result_to_uid_flags_list(fetch_result, &env_list);
2141 mailimap_fetch_list_free(fetch_result);
2143 * result = env_list;
2145 return MAILIMAP_NO_ERROR;
2148 mailimap_fetch_type_free(fetch_type);
2150 mailimap_set_free(set);
2157 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2159 struct fetch_uid_param * param;
2160 struct fetch_uid_result * result;
2161 carray * fetch_result;
2165 result = op->result;
2169 fetch_result = NULL;
2170 r = imap_get_messages_flags_list(param->imap, param->first_index,
2174 result->fetch_result = fetch_result;
2175 debug_print("imap fetch_uid run - end %i\n", r);
2178 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2179 carray ** fetch_result)
2181 struct fetch_uid_param param;
2182 struct fetch_uid_result result;
2185 debug_print("imap fetch_uid - begin\n");
2187 imap = get_imap(folder);
2189 param.first_index = first_index;
2191 mailstream_logger = imap_logger_noop;
2192 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2194 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2196 mailstream_logger = imap_logger_cmd;
2199 if (result.error != MAILIMAP_NO_ERROR)
2200 return result.error;
2202 debug_print("imap fetch_uid - end\n");
2204 * fetch_result = result.fetch_result;
2206 return result.error;
2210 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2214 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2217 data = carray_get(uid_flags_list, i);
2219 data = carray_get(uid_flags_list, i + 1);
2222 carray_free(uid_flags_list);
2227 static int imap_fetch(mailimap * imap,
2230 size_t * result_len)
2233 struct mailimap_set * set;
2234 struct mailimap_fetch_att * fetch_att;
2235 struct mailimap_fetch_type * fetch_type;
2236 clist * fetch_result;
2237 struct mailimap_msg_att * msg_att;
2238 struct mailimap_msg_att_item * msg_att_item;
2243 struct mailimap_section * section;
2245 set = mailimap_set_new_single(msg_index);
2247 res = MAILIMAP_ERROR_MEMORY;
2251 section = mailimap_section_new(NULL);
2252 if (section == NULL) {
2253 res = MAILIMAP_ERROR_MEMORY;
2257 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2258 if (fetch_att == NULL) {
2259 mailimap_section_free(section);
2260 res = MAILIMAP_ERROR_MEMORY;
2264 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2265 if (fetch_type == NULL) {
2266 res = MAILIMAP_ERROR_MEMORY;
2267 goto free_fetch_att;
2270 mailstream_logger = imap_logger_fetch;
2272 r = mailimap_uid_fetch(imap, set,
2273 fetch_type, &fetch_result);
2275 mailstream_logger = imap_logger_cmd;
2277 mailimap_fetch_type_free(fetch_type);
2278 mailimap_set_free(set);
2281 case MAILIMAP_NO_ERROR:
2287 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2288 mailimap_fetch_list_free(fetch_result);
2289 return MAILIMAP_ERROR_FETCH;
2292 msg_att = clist_begin(fetch_result)->data;
2297 if (msg_att->att_list)
2298 cur = clist_begin(msg_att->att_list);
2302 for(; cur != NULL ; cur = clist_next(cur)) {
2303 msg_att_item = clist_content(cur);
2305 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2306 if (msg_att_item->att_data.att_static->att_type ==
2307 MAILIMAP_MSG_ATT_BODY_SECTION) {
2308 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2310 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2312 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2317 mailimap_fetch_list_free(fetch_result);
2320 return MAILIMAP_ERROR_FETCH;
2323 * result_len = text_length;
2325 return MAILIMAP_NO_ERROR;
2328 mailimap_fetch_att_free(fetch_att);
2330 mailimap_set_free(set);
2335 static int imap_fetch_header(mailimap * imap,
2338 size_t * result_len)
2341 struct mailimap_set * set;
2342 struct mailimap_fetch_att * fetch_att;
2343 struct mailimap_fetch_type * fetch_type;
2344 clist * fetch_result;
2345 struct mailimap_msg_att * msg_att;
2346 struct mailimap_msg_att_item * msg_att_item;
2351 struct mailimap_section * section;
2353 set = mailimap_set_new_single(msg_index);
2355 res = MAILIMAP_ERROR_MEMORY;
2359 section = mailimap_section_new_header();
2360 if (section == NULL) {
2361 res = MAILIMAP_ERROR_MEMORY;
2365 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2366 if (fetch_att == NULL) {
2367 mailimap_section_free(section);
2368 res = MAILIMAP_ERROR_MEMORY;
2372 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2373 if (fetch_type == NULL) {
2374 res = MAILIMAP_ERROR_MEMORY;
2375 goto free_fetch_att;
2378 mailstream_logger = imap_logger_fetch;
2380 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2382 mailstream_logger = imap_logger_cmd;
2383 mailimap_fetch_type_free(fetch_type);
2384 mailimap_set_free(set);
2387 case MAILIMAP_NO_ERROR:
2393 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2394 mailimap_fetch_list_free(fetch_result);
2395 return MAILIMAP_ERROR_FETCH;
2398 msg_att = clist_begin(fetch_result)->data;
2403 if (msg_att->att_list)
2404 cur = clist_begin(msg_att->att_list);
2408 for(; cur != NULL ; cur = clist_next(cur)) {
2409 msg_att_item = clist_content(cur);
2411 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2412 if (msg_att_item->att_data.att_static->att_type ==
2413 MAILIMAP_MSG_ATT_BODY_SECTION) {
2414 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2415 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2417 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2422 mailimap_fetch_list_free(fetch_result);
2425 return MAILIMAP_ERROR_FETCH;
2428 * result_len = text_length;
2430 return MAILIMAP_NO_ERROR;
2433 mailimap_fetch_att_free(fetch_att);
2435 mailimap_set_free(set);
2442 struct fetch_content_param {
2445 const char * filename;
2449 struct fetch_content_result {
2453 static void fetch_content_run(struct etpan_thread_op * op)
2455 struct fetch_content_param * param;
2456 struct fetch_content_result * result;
2458 size_t content_size;
2464 result = op->result;
2470 if (param->with_body)
2471 r = imap_fetch(param->imap, param->msg_index,
2472 &content, &content_size);
2474 r = imap_fetch_header(param->imap, param->msg_index,
2475 &content, &content_size);
2479 if (r == MAILIMAP_NO_ERROR) {
2480 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2482 result->error = MAILIMAP_ERROR_FETCH;
2486 f = fdopen(fd, "wb");
2488 result->error = MAILIMAP_ERROR_FETCH;
2492 r = fwrite(content, 1, content_size, f);
2493 if (r < content_size) {
2494 result->error = MAILIMAP_ERROR_FETCH;
2500 result->error = MAILIMAP_ERROR_FETCH;
2511 claws_unlink(param->filename);
2514 /* mmap_string_unref is a simple free in libetpan
2515 * when it has MMAP_UNAVAILABLE defined */
2516 if (mmap_string_unref(content) != 0)
2520 debug_print("imap fetch_content run - end %i\n", result->error);
2523 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2525 const char * filename)
2527 struct fetch_content_param param;
2528 struct fetch_content_result result;
2531 debug_print("imap fetch_content - begin\n");
2533 imap = get_imap(folder);
2535 param.msg_index = msg_index;
2536 param.filename = filename;
2537 param.with_body = with_body;
2539 threaded_run(folder, ¶m, &result, fetch_content_run);
2541 if (result.error != MAILIMAP_NO_ERROR)
2542 return result.error;
2544 debug_print("imap fetch_content - end\n");
2546 return result.error;
2551 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2556 GSList *tags = NULL;
2560 flag_list = att_dyn->att_list;
2561 if (flag_list == NULL)
2564 for(cur = clist_begin(flag_list) ; cur != NULL ;
2565 cur = clist_next(cur)) {
2566 struct mailimap_flag_fetch * flag_fetch;
2568 flag_fetch = clist_content(cur);
2569 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2572 switch (flag_fetch->fl_flag->fl_type) {
2573 case MAILIMAP_FLAG_ANSWERED:
2574 flags |= MSG_REPLIED;
2576 case MAILIMAP_FLAG_FLAGGED:
2577 flags |= MSG_MARKED;
2579 case MAILIMAP_FLAG_DELETED:
2580 flags |= MSG_DELETED;
2582 case MAILIMAP_FLAG_SEEN:
2583 flags &= ~MSG_UNREAD;
2586 case MAILIMAP_FLAG_KEYWORD:
2587 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2588 flags |= MSG_FORWARDED;
2589 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2591 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2592 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2593 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2596 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2606 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2610 struct mailimap_msg_att_dynamic ** patt_dyn)
2612 clistiter * item_cur;
2616 struct mailimap_msg_att_dynamic * att_dyn;
2623 if (msg_att->att_list)
2624 item_cur = clist_begin(msg_att->att_list);
2627 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2628 struct mailimap_msg_att_item * item;
2630 item = clist_content(item_cur);
2632 switch (item->att_type) {
2633 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2634 switch (item->att_data.att_static->att_type) {
2635 case MAILIMAP_MSG_ATT_UID:
2636 uid = item->att_data.att_static->att_data.att_uid;
2639 case MAILIMAP_MSG_ATT_BODY_SECTION:
2640 if (headers == NULL) {
2641 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2644 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2645 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2650 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2651 if (att_dyn == NULL) {
2652 att_dyn = item->att_data.att_dyn;
2660 if (pheaders != NULL)
2661 * pheaders = headers;
2662 if (pref_size != NULL)
2663 * pref_size = ref_size;
2664 if (patt_dyn != NULL)
2665 * patt_dyn = att_dyn;
2667 return MAIL_NO_ERROR;
2670 static struct imap_fetch_env_info *
2671 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2673 struct imap_fetch_env_info * info;
2677 struct mailimap_msg_att_dynamic * att_dyn;
2679 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2684 info = malloc(sizeof(* info));
2686 info->headers = strdup(headers);
2688 info->flags = imap_flags_to_flags(att_dyn, tags);
2694 imap_fetch_result_to_envelop_list(clist * fetch_result,
2695 carray ** p_env_list)
2700 env_list = carray_new(16);
2703 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2704 cur = clist_next(cur)) {
2705 struct mailimap_msg_att * msg_att;
2706 struct imap_fetch_env_info * env_info;
2707 GSList *tags = NULL;
2709 msg_att = clist_content(cur);
2711 env_info = fetch_to_env_info(msg_att, &tags);
2713 return MAILIMAP_ERROR_MEMORY;
2714 carray_add(env_list, env_info, NULL);
2715 carray_add(env_list, tags, NULL);
2717 * p_env_list = env_list;
2719 * p_env_list = NULL;
2722 return MAIL_NO_ERROR;
2725 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2727 struct mailimap_fetch_att * fetch_att;
2731 struct mailimap_header_list * imap_hdrlist;
2732 struct mailimap_section * section;
2734 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2735 "References", "In-Reply-To", NULL
2738 hdrlist = clist_new();
2740 while (headers[i] != NULL) {
2741 header = strdup(headers[i]);
2742 if (header == NULL || clist_append(hdrlist, header) != 0)
2743 return MAIL_ERROR_MEMORY;
2747 imap_hdrlist = mailimap_header_list_new(hdrlist);
2748 section = mailimap_section_new_header_fields(imap_hdrlist);
2749 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2750 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2752 return MAIL_NO_ERROR;
2755 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2757 struct mailimap_fetch_att * fetch_att;
2758 struct mailimap_section * section;
2760 section = mailimap_section_new_header();
2761 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2762 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2764 return MAIL_NO_ERROR;
2768 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2769 carray ** p_env_list)
2771 struct mailimap_fetch_att * fetch_att;
2772 struct mailimap_fetch_type * fetch_type;
2774 clist * fetch_result;
2776 carray * env_list = NULL;
2780 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2783 fetch_att = mailimap_fetch_att_new_uid();
2784 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2787 fetch_att = mailimap_fetch_att_new_flags();
2788 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2791 fetch_att = mailimap_fetch_att_new_rfc822_size();
2792 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2796 key.len = sizeof(imap);
2797 r = chash_get(courier_workaround_hash, &key, &value);
2799 r = imap_add_envelope_fetch_att(fetch_type);
2801 r = imap_add_header_fetch_att(fetch_type);
2803 mailstream_logger = imap_logger_fetch;
2805 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2807 mailstream_logger = imap_logger_cmd;
2809 case MAILIMAP_NO_ERROR:
2812 mailimap_fetch_type_free(fetch_type);
2813 debug_print("uid_fetch: %d\n", r);
2817 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2818 res = MAILIMAP_ERROR_FETCH;
2819 debug_print("clist_begin = NULL\n");
2823 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2824 mailimap_fetch_list_free(fetch_result);
2826 if (r != MAILIMAP_NO_ERROR) {
2827 mailimap_fetch_type_free(fetch_type);
2828 res = MAILIMAP_ERROR_MEMORY;
2829 debug_print("fetch_result_to_envelop_list: %d\n", res);
2833 mailimap_fetch_type_free(fetch_type);
2835 * p_env_list = env_list;
2837 return MAILIMAP_NO_ERROR;
2843 struct fetch_env_param {
2845 struct mailimap_set * set;
2848 struct fetch_env_result {
2849 carray * fetch_env_result;
2853 static void fetch_env_run(struct etpan_thread_op * op)
2855 struct fetch_env_param * param;
2856 struct fetch_env_result * result;
2861 result = op->result;
2866 r = imap_get_envelopes_list(param->imap, param->set,
2870 result->fetch_env_result = env_list;
2872 debug_print("imap fetch_env run - end %i\n", r);
2875 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2876 carray ** p_env_list)
2878 struct fetch_env_param param;
2879 struct fetch_env_result result;
2882 debug_print("imap fetch_env - begin\n");
2884 imap = get_imap(folder);
2888 threaded_run(folder, ¶m, &result, fetch_env_run);
2890 if (result.error != MAILIMAP_NO_ERROR) {
2896 key.len = sizeof(imap);
2897 r = chash_get(courier_workaround_hash, &key, &value);
2901 chash_set(courier_workaround_hash, &key, &value, NULL);
2903 threaded_run(folder, ¶m, &result, fetch_env_run);
2907 if (result.error != MAILIMAP_NO_ERROR)
2908 return result.error;
2910 debug_print("imap fetch_env - end\n");
2912 * p_env_list = result.fetch_env_result;
2914 return result.error;
2917 void imap_fetch_env_free(carray * env_list)
2921 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2922 struct imap_fetch_env_info * env_info;
2924 env_info = carray_get(env_list, i);
2925 free(env_info->headers);
2928 carray_free(env_list);
2935 struct append_param {
2937 const char * mailbox;
2938 const char * filename;
2939 struct mailimap_flag_list * flag_list;
2942 struct append_result {
2947 static void append_run(struct etpan_thread_op * op)
2949 struct append_param * param;
2950 struct append_result * result;
2955 struct stat stat_buf;
2958 guint32 uid = 0, val = 0;
2961 result = op->result;
2966 r = stat(param->filename, &stat_buf);
2968 result->error = MAILIMAP_ERROR_APPEND;
2971 size = stat_buf.st_size;
2973 fd = g_open(param->filename, O_RDONLY, 0);
2975 result->error = MAILIMAP_ERROR_APPEND;
2979 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2980 if (data == (void *) MAP_FAILED) {
2982 result->error = MAILIMAP_ERROR_APPEND;
2986 data = file_read_to_str_no_recode(param->filename);
2988 result->error = MAILIMAP_ERROR_APPEND;
2991 size = strlen(data);
2993 mailstream_logger = imap_logger_append;
2995 r = mailimap_uidplus_append(param->imap, param->mailbox,
2996 param->flag_list, NULL,
2997 data, size, &val, &uid);
2999 mailstream_logger = imap_logger_cmd;
3010 debug_print("imap append run - end %i uid %d\n", r, uid);
3013 int imap_threaded_append(Folder * folder, const char * mailbox,
3014 const char * filename,
3015 struct mailimap_flag_list * flag_list,
3018 struct append_param param;
3019 struct append_result result;
3022 debug_print("imap append - begin\n");
3024 imap = get_imap(folder);
3026 param.mailbox = mailbox;
3027 param.filename = filename;
3028 param.flag_list = flag_list;
3030 threaded_run(folder, ¶m, &result, append_run);
3032 if (result.error != MAILIMAP_NO_ERROR)
3033 return result.error;
3035 debug_print("imap append - end\n");
3039 return result.error;
3045 struct expunge_param {
3049 struct expunge_result {
3053 static void expunge_run(struct etpan_thread_op * op)
3055 struct expunge_param * param;
3056 struct expunge_result * result;
3060 result = op->result;
3064 r = mailimap_expunge(param->imap);
3067 debug_print("imap expunge run - end %i\n", r);
3070 int imap_threaded_expunge(Folder * folder)
3072 struct expunge_param param;
3073 struct expunge_result result;
3075 debug_print("imap expunge - begin\n");
3077 param.imap = get_imap(folder);
3079 threaded_run(folder, ¶m, &result, expunge_run);
3081 debug_print("imap expunge - end\n");
3083 return result.error;
3089 struct mailimap_set * set;
3093 struct copy_result {
3095 struct mailimap_set *source;
3096 struct mailimap_set *dest;
3099 static void copy_run(struct etpan_thread_op * op)
3101 struct copy_param * param;
3102 struct copy_result * result;
3105 struct mailimap_set *source = NULL, *dest = NULL;
3108 result = op->result;
3112 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3113 &val, &source, &dest);
3117 result->source = source;
3118 result->dest = dest;
3120 result->source = NULL;
3121 result->dest = NULL;
3123 debug_print("imap copy run - end %i\n", r);
3126 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3127 const char * mb, struct mailimap_set **source,
3128 struct mailimap_set **dest)
3130 struct copy_param param;
3131 struct copy_result result;
3134 debug_print("imap copy - begin\n");
3136 imap = get_imap(folder);
3141 threaded_run(folder, ¶m, &result, copy_run);
3145 if (result.error != MAILIMAP_NO_ERROR)
3146 return result.error;
3148 *source = result.source;
3149 *dest = result.dest;
3151 debug_print("imap copy - end\n");
3153 return result.error;
3158 struct store_param {
3160 struct mailimap_set * set;
3161 struct mailimap_store_att_flags * store_att_flags;
3164 struct store_result {
3168 static void store_run(struct etpan_thread_op * op)
3170 struct store_param * param;
3171 struct store_result * result;
3175 result = op->result;
3179 r = mailimap_uid_store(param->imap, param->set,
3180 param->store_att_flags);
3184 debug_print("imap store run - end %i\n", r);
3187 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3188 struct mailimap_store_att_flags * store_att_flags)
3190 struct store_param param;
3191 struct store_result result;
3194 debug_print("imap store - begin\n");
3196 imap = get_imap(folder);
3199 param.store_att_flags = store_att_flags;
3201 threaded_run(folder, ¶m, &result, store_run);
3203 if (result.error != MAILIMAP_NO_ERROR)
3204 return result.error;
3206 debug_print("imap store - end\n");
3208 return result.error;
3212 #define ENV_BUFFER_SIZE 512
3214 static void do_exec_command(int fd, const char * command,
3215 const char * servername, uint16_t port)
3219 char env_buffer[ENV_BUFFER_SIZE];
3223 /* Fork again to become a child of init rather than
3224 the etpan client. */
3229 g_setenv("ETPANSERVER", servername, TRUE);
3231 g_unsetenv("ETPANSERVER");
3236 snprintf(porttext, sizeof(porttext), "%d", port);
3237 g_setenv("ETPANPORT", porttext, TRUE);
3240 g_unsetenv("ETPANPORT");
3243 /* Not a lot we can do if there's an error other than bail. */
3244 if (dup2(fd, 0) == -1)
3246 if (dup2(fd, 1) == -1)
3249 /* Should we close stderr and reopen /dev/null? */
3251 maxopen = sysconf(_SC_OPEN_MAX);
3252 for (i=3; i < maxopen; i++)
3256 /* Detach from the controlling tty if we have one. Otherwise,
3257 SSH might do something stupid like trying to use it instead
3258 of running $SSH_ASKPASS. Doh. */
3259 fd = g_open("/dev/tty", O_RDONLY, 0);
3261 ioctl(fd, TIOCNOTTY, NULL);
3264 #endif /* TIOCNOTTY */
3266 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3268 /* Eep. Shouldn't reach this */
3272 static int subcommand_connect(const char *command,
3273 const char *servername, uint16_t port)
3275 /* SEB unsupported on Windows */
3279 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3284 do_exec_command(sockfds[1], command, servername, port);
3286 else if (childpid == -1) {
3294 /* Reap child, leaving grandchild process to run */
3295 waitpid(childpid, NULL, 0);
3300 static int socket_connect_cmd(mailimap * imap, const char * command,
3301 const char * server, int port)
3307 fd = subcommand_connect(command, server, port);
3309 return MAILIMAP_ERROR_STREAM;
3311 s = mailstream_socket_open(fd);
3314 return MAILIMAP_ERROR_STREAM;
3317 r = mailimap_connect(imap, s);
3318 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3319 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3320 mailstream_close(s);
3322 imap->imap_stream = NULL;
3331 struct connect_cmd_param {
3333 const char * command;
3334 const char * server;
3338 struct connect_cmd_result {
3342 static void connect_cmd_run(struct etpan_thread_op * op)
3345 struct connect_cmd_param * param;
3346 struct connect_cmd_result * result;
3349 result = op->result;
3353 r = socket_connect_cmd(param->imap, param->command,
3354 param->server, param->port);
3360 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3361 const char * server, int port)
3363 struct connect_cmd_param param;
3364 struct connect_cmd_result result;
3367 mailimap * imap, * oldimap;
3369 oldimap = get_imap(folder);
3371 imap = mailimap_new(0, NULL);
3374 debug_print("deleting old imap %p\n", oldimap);
3375 delete_imap(folder, oldimap);
3379 key.len = sizeof(folder);
3382 chash_set(session_hash, &key, &value, NULL);
3385 param.command = command;
3386 param.server = server;
3389 threaded_run(folder, ¶m, &result, connect_cmd_run);
3391 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3393 return result.error;
3395 #endif /* G_OS_WIN32 */
3397 void imap_threaded_cancel(Folder * folder)
3401 imap = get_imap(folder);
3402 if (imap->imap_stream != NULL)
3403 mailstream_cancel(imap->imap_stream);
3408 void imap_main_init(void)
3411 void imap_main_done(gboolean have_connectivity)
3414 void imap_main_set_timeout(int sec)
3418 void imap_threaded_cancel(Folder * folder);