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;
548 oldimap = get_imap(folder);
550 imap = mailimap_new(0, NULL);
553 debug_print("deleting old imap %p\n", oldimap);
554 delete_imap(folder, oldimap);
558 key.len = sizeof(folder);
561 chash_set(session_hash, &key, &value, NULL);
564 param.server = server;
566 param.account = folder->account;
569 threaded_run(folder, ¶m, &result, connect_ssl_run);
571 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
572 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
573 if (etpan_certificate_check(imap->imap_stream, server, port) != TRUE)
574 result.error = MAILIMAP_ERROR_SSL;
576 debug_print("connect %d with imap %p\n", result.error, imap);
587 struct mailimap_capability_data *caps;
590 static void capability_run(struct etpan_thread_op * op)
593 struct capa_param * param;
594 struct capa_result * result;
595 struct mailimap_capability_data *caps;
602 r = mailimap_capability(param->imap, &caps);
605 result->caps = (r == 0 ? caps : NULL);
609 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
611 struct capa_param param;
612 struct capa_result result;
615 imap = get_imap(folder);
619 threaded_run(folder, ¶m, &result, capability_run);
621 debug_print("capa %d\n", result.error);
630 struct disconnect_param {
634 struct disconnect_result {
638 static void disconnect_run(struct etpan_thread_op * op)
641 struct disconnect_param * param;
642 struct disconnect_result * result;
649 r = mailimap_logout(param->imap);
654 void imap_threaded_disconnect(Folder * folder)
656 struct connect_param param;
657 struct connect_result result;
660 imap = get_imap(folder);
662 debug_print("was disconnected\n");
668 threaded_run(folder, ¶m, &result, disconnect_run);
670 if (imap == get_imap(folder)) {
671 debug_print("deleting old imap %p\n", imap);
672 delete_imap(folder, imap);
674 debug_print("imap already deleted %p\n", imap);
677 debug_print("disconnect ok\n");
684 const char * wildcard;
693 static void list_run(struct etpan_thread_op * op)
695 struct list_param * param;
696 struct list_result * result;
707 if (param->base == NULL || param->wildcard == NULL) {
710 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
714 r = mailimap_lsub(param->imap, param->base,
715 param->wildcard, &list);
717 r = mailimap_list(param->imap, param->base,
718 param->wildcard, &list);
721 debug_print("imap list run - end\n");
724 int imap_threaded_list(Folder * folder, const char * base,
725 const char * wildcard,
728 struct list_param param;
729 struct list_result result;
731 debug_print("imap list - begin\n");
733 param.imap = get_imap(folder);
735 param.wildcard = wildcard;
736 param.sub_only = FALSE;
738 threaded_run(folder, ¶m, &result, list_run);
740 * p_result = result.list;
742 debug_print("imap list - end %p\n", result.list);
747 int imap_threaded_lsub(Folder * folder, const char * base,
748 const char * wildcard,
751 struct list_param param;
752 struct list_result result;
754 debug_print("imap lsub - begin\n");
756 param.imap = get_imap(folder);
758 param.wildcard = wildcard;
759 param.sub_only = TRUE;
761 threaded_run(folder, ¶m, &result, list_run);
763 * p_result = result.list;
765 debug_print("imap lsub - end %p\n", result.list);
770 struct subscribe_param {
776 struct subscribe_result {
780 static void subscribe_run(struct etpan_thread_op * op)
782 struct subscribe_param * param;
783 struct subscribe_result * result;
791 if (param->mb == NULL) {
793 debug_print("no mb\n");
796 if (param->subscribe)
797 r = mailimap_subscribe(param->imap, param->mb);
799 r = mailimap_unsubscribe(param->imap, param->mb);
801 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
804 int imap_threaded_subscribe(Folder * folder, const char * mb,
807 struct subscribe_param param;
808 struct subscribe_result result;
810 debug_print("imap list - begin\n");
812 param.imap = get_imap(folder);
814 param.subscribe = subscribe;
816 threaded_run(folder, ¶m, &result, subscribe_run);
824 const char * password;
829 struct login_result {
833 static void login_run(struct etpan_thread_op * op)
835 struct login_param * param;
836 struct login_result * result;
838 #ifdef DISABLE_LOG_DURING_LOGIN
847 #ifdef DISABLE_LOG_DURING_LOGIN
848 old_debug = mailstream_debug;
849 mailstream_debug = 0;
851 if (!strcmp(param->type, "LOGIN"))
852 r = mailimap_login(param->imap,
853 param->login, param->password);
854 else if (!strcmp(param->type, "GSSAPI"))
855 r = mailimap_authenticate(param->imap,
856 param->type, param->server, NULL, NULL,
857 param->login, param->login,
858 param->password, NULL);
860 r = mailimap_authenticate(param->imap,
861 param->type, NULL, NULL, NULL,
862 param->login, param->login,
863 param->password, NULL);
864 #ifdef DISABLE_LOG_DURING_LOGIN
865 mailstream_debug = old_debug;
869 if (param->imap->imap_response)
870 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
871 debug_print("imap login run - end %i\n", r);
874 int imap_threaded_login(Folder * folder,
875 const char * login, const char * password,
878 struct login_param param;
879 struct login_result result;
881 debug_print("imap login - begin\n");
883 param.imap = get_imap(folder);
885 param.password = password;
887 if (folder && folder->account)
888 param.server = folder->account->recv_server;
892 threaded_run(folder, ¶m, &result, login_run);
894 debug_print("imap login - end\n");
900 struct status_param {
903 struct mailimap_status_att_list * status_att_list;
906 struct status_result {
908 struct mailimap_mailbox_data_status * data_status;
911 static void status_run(struct etpan_thread_op * op)
913 struct status_param * param;
914 struct status_result * result;
922 r = mailimap_status(param->imap, param->mb,
923 param->status_att_list,
924 &result->data_status);
927 debug_print("imap status run - end %i\n", r);
930 int imap_threaded_status(Folder * folder, const char * mb,
931 struct mailimap_mailbox_data_status ** data_status,
934 struct status_param param;
935 struct status_result result;
936 struct mailimap_status_att_list * status_att_list;
938 debug_print("imap status - begin\n");
940 status_att_list = mailimap_status_att_list_new_empty();
942 mailimap_status_att_list_add(status_att_list,
943 MAILIMAP_STATUS_ATT_MESSAGES);
946 mailimap_status_att_list_add(status_att_list,
947 MAILIMAP_STATUS_ATT_RECENT);
950 mailimap_status_att_list_add(status_att_list,
951 MAILIMAP_STATUS_ATT_UIDNEXT);
954 mailimap_status_att_list_add(status_att_list,
955 MAILIMAP_STATUS_ATT_UIDVALIDITY);
958 mailimap_status_att_list_add(status_att_list,
959 MAILIMAP_STATUS_ATT_UNSEEN);
961 param.imap = get_imap(folder);
963 param.status_att_list = status_att_list;
965 threaded_run(folder, ¶m, &result, status_run);
967 debug_print("imap status - end\n");
969 * data_status = result.data_status;
971 mailimap_status_att_list_free(status_att_list);
986 static void noop_run(struct etpan_thread_op * op)
988 struct noop_param * param;
989 struct noop_result * result;
997 r = mailimap_noop(param->imap);
1000 debug_print("imap noop run - end %i\n", r);
1003 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1004 unsigned int *p_recent,
1005 unsigned int *p_expunge,
1006 unsigned int *p_unseen,
1007 unsigned int *p_uidnext,
1008 unsigned int *p_uidval)
1010 struct noop_param param;
1011 struct noop_result result;
1014 debug_print("imap noop - begin\n");
1016 imap = get_imap(folder);
1019 threaded_run(folder, ¶m, &result, noop_run);
1021 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1022 * p_exists = imap->imap_selection_info->sel_exists;
1023 * p_recent = imap->imap_selection_info->sel_recent;
1024 * p_unseen = imap->imap_selection_info->sel_unseen;
1025 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1026 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1034 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1035 imap->imap_response_info->rsp_expunged != NULL) {
1036 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1040 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1041 *p_exists, *p_recent, *p_expunge, *p_unseen,
1042 *p_uidnext, *p_uidval);
1044 return result.error;
1048 struct starttls_result {
1052 static void starttls_run(struct etpan_thread_op * op)
1054 struct connect_param * param;
1055 struct starttls_result * result;
1059 result = op->result;
1063 r = mailimap_starttls(param->imap);
1066 debug_print("imap starttls run - end %i\n", r);
1069 mailimap *imap = param->imap;
1070 mailstream_low *plain_low = NULL;
1071 mailstream_low *tls_low = NULL;
1074 plain_low = mailstream_get_low(imap->imap_stream);
1075 fd = mailstream_low_get_fd(plain_low);
1077 debug_print("imap starttls run - can't get fd\n");
1078 result->error = MAILIMAP_ERROR_STREAM;
1082 tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
1083 if (tls_low == NULL) {
1084 debug_print("imap starttls run - can't tls_open\n");
1085 result->error = MAILIMAP_ERROR_STREAM;
1088 mailstream_low_free(plain_low);
1089 mailstream_set_low(imap->imap_stream, tls_low);
1093 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1095 struct connect_param param;
1096 struct starttls_result result;
1098 debug_print("imap starttls - begin\n");
1100 param.imap = get_imap(folder);
1101 param.server = host;
1103 param.account = folder->account;
1105 threaded_run(folder, ¶m, &result, starttls_run);
1107 debug_print("imap starttls - end\n");
1109 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1110 if (etpan_certificate_check(param.imap->imap_stream, host, port) != TRUE)
1111 return MAILIMAP_ERROR_SSL;
1113 return result.error;
1118 struct create_param {
1123 struct create_result {
1127 static void create_run(struct etpan_thread_op * op)
1129 struct create_param * param;
1130 struct create_result * result;
1134 result = op->result;
1138 r = mailimap_create(param->imap, param->mb);
1141 debug_print("imap create run - end %i\n", r);
1144 int imap_threaded_create(Folder * folder, const char * mb)
1146 struct create_param param;
1147 struct create_result result;
1149 debug_print("imap create - begin\n");
1151 param.imap = get_imap(folder);
1154 threaded_run(folder, ¶m, &result, create_run);
1156 debug_print("imap create - end\n");
1158 return result.error;
1164 struct rename_param {
1167 const char * new_name;
1170 struct rename_result {
1174 static void rename_run(struct etpan_thread_op * op)
1176 struct rename_param * param;
1177 struct rename_result * result;
1181 result = op->result;
1185 r = mailimap_rename(param->imap, param->mb, param->new_name);
1188 debug_print("imap rename run - end %i\n", r);
1191 int imap_threaded_rename(Folder * folder,
1192 const char * mb, const char * new_name)
1194 struct rename_param param;
1195 struct rename_result result;
1197 debug_print("imap rename - begin\n");
1199 param.imap = get_imap(folder);
1201 param.new_name = new_name;
1203 threaded_run(folder, ¶m, &result, rename_run);
1205 debug_print("imap rename - end\n");
1207 return result.error;
1213 struct delete_param {
1218 struct delete_result {
1222 static void delete_run(struct etpan_thread_op * op)
1224 struct delete_param * param;
1225 struct delete_result * result;
1229 result = op->result;
1233 r = mailimap_delete(param->imap, param->mb);
1236 debug_print("imap delete run - end %i\n", r);
1239 int imap_threaded_delete(Folder * folder, const char * mb)
1241 struct delete_param param;
1242 struct delete_result result;
1244 debug_print("imap delete - begin\n");
1246 param.imap = get_imap(folder);
1249 threaded_run(folder, ¶m, &result, delete_run);
1251 debug_print("imap delete - end\n");
1253 return result.error;
1258 struct select_param {
1263 struct select_result {
1267 static void select_run(struct etpan_thread_op * op)
1269 struct select_param * param;
1270 struct select_result * result;
1274 result = op->result;
1278 r = mailimap_select(param->imap, param->mb);
1281 debug_print("imap select run - end %i\n", r);
1284 int imap_threaded_select(Folder * folder, const char * mb,
1285 gint * exists, gint * recent, gint * unseen,
1286 guint32 * uid_validity,gint *can_create_flags,
1289 struct select_param param;
1290 struct select_result result;
1293 debug_print("imap select - begin\n");
1295 imap = get_imap(folder);
1299 threaded_run(folder, ¶m, &result, select_run);
1301 if (result.error != MAILIMAP_NO_ERROR)
1302 return result.error;
1304 if (!imap || imap->imap_selection_info == NULL)
1305 return MAILIMAP_ERROR_PARSE;
1307 * exists = imap->imap_selection_info->sel_exists;
1308 * recent = imap->imap_selection_info->sel_recent;
1309 * unseen = imap->imap_selection_info->sel_unseen;
1310 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1311 * can_create_flags = FALSE;
1313 if (imap->imap_selection_info->sel_perm_flags) {
1314 GSList *t_flags = NULL;
1315 clistiter *cur = NULL;
1316 if (imap->imap_selection_info->sel_perm_flags)
1317 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1319 for (; cur; cur = clist_next(cur)) {
1320 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1321 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1322 *can_create_flags = TRUE;
1323 else if (flag->fl_flag &&
1324 flag->fl_flag->fl_type == 6 &&
1325 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1326 *can_create_flags = TRUE;
1327 if (flag->fl_flag && ok_flags) {
1328 MsgPermFlags c_flag = 0;
1329 switch (flag->fl_flag->fl_type) {
1330 case MAILIMAP_FLAG_ANSWERED:
1331 c_flag = IMAP_FLAG_ANSWERED;
1333 case MAILIMAP_FLAG_FLAGGED:
1334 c_flag = IMAP_FLAG_FLAGGED;
1336 case MAILIMAP_FLAG_DELETED:
1337 c_flag = IMAP_FLAG_DELETED;
1339 case MAILIMAP_FLAG_DRAFT:
1340 c_flag = IMAP_FLAG_DRAFT;
1342 case MAILIMAP_FLAG_SEEN:
1343 c_flag = IMAP_FLAG_SEEN;
1345 case MAILIMAP_FLAG_KEYWORD:
1346 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1347 c_flag = IMAP_FLAG_FORWARDED;
1348 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1349 c_flag = IMAP_FLAG_SPAM;
1350 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1351 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1352 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1353 c_flag = IMAP_FLAG_HAM;
1359 t_flags = g_slist_prepend(t_flags,
1360 GUINT_TO_POINTER(c_flag));
1365 *ok_flags = t_flags;
1367 debug_print("imap select - end\n");
1369 return result.error;
1372 static void close_run(struct etpan_thread_op * op)
1374 struct select_param * param;
1375 struct select_result * result;
1379 result = op->result;
1383 r = mailimap_close(param->imap);
1386 debug_print("imap close run - end %i\n", r);
1389 int imap_threaded_close(Folder * folder)
1391 struct select_param param;
1392 struct select_result result;
1395 debug_print("imap close - begin\n");
1397 imap = get_imap(folder);
1400 threaded_run(folder, ¶m, &result, close_run);
1402 if (result.error != MAILIMAP_NO_ERROR)
1403 return result.error;
1405 debug_print("imap close - end\n");
1407 return result.error;
1410 struct examine_param {
1415 struct examine_result {
1419 static void examine_run(struct etpan_thread_op * op)
1421 struct examine_param * param;
1422 struct examine_result * result;
1426 result = op->result;
1430 r = mailimap_examine(param->imap, param->mb);
1433 debug_print("imap examine run - end %i\n", r);
1436 int imap_threaded_examine(Folder * folder, const char * mb,
1437 gint * exists, gint * recent, gint * unseen,
1438 guint32 * uid_validity)
1440 struct examine_param param;
1441 struct examine_result result;
1444 debug_print("imap examine - begin\n");
1446 imap = get_imap(folder);
1450 threaded_run(folder, ¶m, &result, examine_run);
1452 if (result.error != MAILIMAP_NO_ERROR)
1453 return result.error;
1455 if (!imap || imap->imap_selection_info == NULL)
1456 return MAILIMAP_ERROR_PARSE;
1458 * exists = imap->imap_selection_info->sel_exists;
1459 * recent = imap->imap_selection_info->sel_recent;
1460 * unseen = imap->imap_selection_info->sel_unseen;
1461 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1463 debug_print("imap examine - end\n");
1465 return result.error;
1471 struct search_param {
1474 const char *charset;
1475 struct mailimap_set * set;
1479 struct search_result {
1481 clist * search_result;
1484 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1486 return mailimap_set_item_new(orig->set_first, orig->set_last);
1489 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1491 clist *list = orig ? orig->set_list : NULL;
1492 clist *newlist = clist_new();
1497 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1498 clist_append(newlist,
1499 sc_mailimap_set_item_copy(
1500 (struct mailimap_set_item *)clist_content(cur)));
1501 return mailimap_set_new(newlist);
1504 static void search_run(struct etpan_thread_op * op)
1506 struct search_param * param;
1507 struct search_result * result;
1509 struct mailimap_search_key * key = NULL;
1510 struct mailimap_search_key * uid_key = NULL;
1511 struct mailimap_search_key * search_type_key = NULL;
1512 clist * search_result;
1515 result = op->result;
1519 /* we copy the mailimap_set because freeing the key is recursive */
1520 if (param->set != NULL) {
1521 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1522 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1523 uid_key = mailimap_search_key_new_all();
1525 switch (param->type) {
1526 case IMAP_SEARCH_TYPE_SIMPLE:
1527 search_type_key = NULL;
1529 case IMAP_SEARCH_TYPE_SEEN:
1530 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1532 case IMAP_SEARCH_TYPE_UNSEEN:
1533 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1535 case IMAP_SEARCH_TYPE_ANSWERED:
1536 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1538 case IMAP_SEARCH_TYPE_FLAGGED:
1539 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1541 case IMAP_SEARCH_TYPE_DELETED:
1542 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1544 case IMAP_SEARCH_TYPE_FORWARDED:
1545 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1547 case IMAP_SEARCH_TYPE_SPAM:
1548 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1550 case IMAP_SEARCH_TYPE_KEYED:
1551 search_type_key = param->key;
1555 if (search_type_key != NULL) {
1556 if (param->set != NULL) {
1557 key = mailimap_search_key_new_multiple_empty();
1558 mailimap_search_key_multiple_add(key, search_type_key);
1559 mailimap_search_key_multiple_add(key, uid_key);
1561 key = search_type_key;
1563 } else if (uid_key != NULL) {
1568 g_warning("no key!");
1569 result = op->result;
1571 result->search_result = NULL;
1573 mailstream_logger = imap_logger_uid;
1575 r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
1577 mailstream_logger = imap_logger_cmd;
1579 /* free the key (with the imapset) */
1580 mailimap_search_key_free(key);
1583 result->search_result = search_result;
1585 debug_print("imap search run - end %i\n", result->error);
1588 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1589 const char *charset, struct mailimap_set * set,
1590 clist ** search_result)
1592 struct search_param param;
1593 struct search_result result;
1596 debug_print("imap search - begin\n");
1598 imap = get_imap(folder);
1601 param.charset = charset;
1602 param.type = search_type;
1605 threaded_run(folder, ¶m, &result, search_run);
1607 if (result.error != MAILIMAP_NO_ERROR)
1608 return result.error;
1610 debug_print("imap search - end\n");
1612 * search_result = result.search_result;
1614 return result.error;
1618 struct _IMAPSearchKey {
1619 struct mailimap_search_key* key;
1622 IMAPSearchKey* imap_search_new(gint criteria,
1623 const gchar *header,
1627 char* sk_bcc = NULL;
1628 struct mailimap_date* sk_before = NULL;
1629 char* sk_body = NULL;
1631 char* sk_from = NULL;
1632 char* sk_keyword = NULL;
1633 struct mailimap_date* sk_on = NULL;
1634 struct mailimap_date* sk_since = NULL;
1635 char* sk_subject = NULL;
1636 char* sk_text = NULL;
1638 char* sk_unkeyword = NULL;
1639 char* sk_header_name = NULL;
1640 char* sk_header_value = NULL;
1641 uint32_t sk_larger = 0;
1642 struct mailimap_search_key* sk_not = NULL;
1643 struct mailimap_search_key* sk_or1 = NULL;
1644 struct mailimap_search_key* sk_or2 = NULL;
1645 struct mailimap_date* sk_sentbefore = NULL;
1646 struct mailimap_date* sk_senton = NULL;
1647 struct mailimap_date* sk_sentsince = NULL;
1648 uint32_t sk_smaller = 0;
1649 struct mailimap_set* sk_uid = NULL;
1650 struct mailimap_set* sk_set = NULL;
1651 clist* sk_multiple = NULL;
1652 int etpan_matcher_type;
1655 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1656 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1657 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1658 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1659 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1660 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1661 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1663 case IMAP_SEARCH_CRITERIA_TAG:
1664 sk_keyword = strdup(expr);
1665 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1668 case IMAP_SEARCH_CRITERIA_SUBJECT:
1669 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1670 sk_subject = strdup(expr);
1673 case IMAP_SEARCH_CRITERIA_TO:
1674 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1675 sk_to = strdup(expr);
1678 case IMAP_SEARCH_CRITERIA_CC:
1679 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1680 sk_cc = strdup(expr);
1683 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1684 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1687 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1690 localtime_r(&limit, &tm);
1691 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1692 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1693 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1695 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1696 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1701 case IMAP_SEARCH_CRITERIA_BODY:
1702 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1703 sk_body = strdup(expr);
1706 case IMAP_SEARCH_CRITERIA_MESSAGE:
1707 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1708 sk_text = strdup(expr);
1711 case IMAP_SEARCH_CRITERIA_HEADER:
1712 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1713 sk_header_name = strdup(header);
1714 sk_header_value = strdup(expr);
1717 case IMAP_SEARCH_CRITERIA_FROM:
1718 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1719 sk_from = strdup(expr);
1722 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1723 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1727 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1728 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1736 return mailimap_search_key_new(etpan_matcher_type,
1737 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1738 sk_on, sk_since, sk_subject, sk_text, sk_to,
1739 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1740 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1741 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1744 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1746 return mailimap_search_key_new_not(key);
1749 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1751 return mailimap_search_key_new_or(l, r);
1754 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1756 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1757 mailimap_search_key_multiple_add(result, l);
1758 mailimap_search_key_multiple_add(result, r);
1763 void imap_search_free(IMAPSearchKey* key)
1768 mailimap_search_key_free(key);
1773 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1777 struct mailimap_msg_att_dynamic ** patt_dyn);
1780 result_to_uid_list(clist * fetch_result, carray ** result)
1782 clistiter * cur = NULL;
1787 tab = carray_new(128);
1789 res = MAILIMAP_ERROR_MEMORY;
1794 cur = clist_begin(fetch_result);
1796 for(; cur != NULL ; cur = clist_next(cur)) {
1797 struct mailimap_msg_att * msg_att;
1801 msg_att = clist_content(cur);
1804 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1806 puid = malloc(sizeof(* puid));
1808 res = MAILIMAP_ERROR_MEMORY;
1813 r = carray_add(tab, puid, NULL);
1816 res = MAILIMAP_ERROR_MEMORY;
1823 return MAILIMAP_NO_ERROR;
1826 imap_fetch_uid_list_free(tab);
1831 static int imap_get_messages_list(mailimap * imap,
1832 uint32_t first_index,
1837 struct mailimap_fetch_att * fetch_att;
1838 struct mailimap_fetch_type * fetch_type;
1839 struct mailimap_set * set;
1840 clist * fetch_result;
1843 set = mailimap_set_new_interval(first_index, 0);
1845 res = MAILIMAP_ERROR_MEMORY;
1849 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1850 if (fetch_type == NULL) {
1851 res = MAILIMAP_ERROR_MEMORY;
1855 fetch_att = mailimap_fetch_att_new_uid();
1856 if (fetch_att == NULL) {
1857 res = MAILIMAP_ERROR_MEMORY;
1858 goto free_fetch_type;
1861 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1862 if (r != MAILIMAP_NO_ERROR) {
1863 mailimap_fetch_att_free(fetch_att);
1864 res = MAILIMAP_ERROR_MEMORY;
1865 goto free_fetch_type;
1868 mailstream_logger = imap_logger_fetch;
1870 r = mailimap_uid_fetch(imap, set,
1871 fetch_type, &fetch_result);
1873 mailstream_logger = imap_logger_cmd;
1874 mailimap_fetch_type_free(fetch_type);
1875 mailimap_set_free(set);
1877 if (r != MAILIMAP_NO_ERROR) {
1883 r = result_to_uid_list(fetch_result, &env_list);
1884 mailimap_fetch_list_free(fetch_result);
1886 * result = env_list;
1888 return MAILIMAP_NO_ERROR;
1891 mailimap_fetch_type_free(fetch_type);
1893 mailimap_set_free(set);
1901 struct fetch_uid_param {
1903 uint32_t first_index;
1906 struct fetch_uid_result {
1908 carray * fetch_result;
1911 static void fetch_uid_run(struct etpan_thread_op * op)
1913 struct fetch_uid_param * param;
1914 struct fetch_uid_result * result;
1915 carray * fetch_result;
1919 result = op->result;
1923 fetch_result = NULL;
1924 mailstream_logger = imap_logger_noop;
1925 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1927 r = imap_get_messages_list(param->imap, param->first_index,
1930 mailstream_logger = imap_logger_cmd;
1933 result->fetch_result = fetch_result;
1934 debug_print("imap fetch_uid run - end %i\n", r);
1937 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1938 carray ** fetch_result)
1940 struct fetch_uid_param param;
1941 struct fetch_uid_result result;
1944 debug_print("imap fetch_uid - begin\n");
1946 imap = get_imap(folder);
1948 param.first_index = first_index;
1950 threaded_run(folder, ¶m, &result, fetch_uid_run);
1952 if (result.error != MAILIMAP_NO_ERROR)
1953 return result.error;
1955 debug_print("imap fetch_uid - end\n");
1957 * fetch_result = result.fetch_result;
1959 return result.error;
1963 void imap_fetch_uid_list_free(carray * uid_list)
1967 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1970 puid = carray_get(uid_list, i);
1973 carray_free(uid_list);
1979 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1982 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1984 clistiter * cur = NULL;
1988 GSList *tags = NULL;
1990 tab = carray_new(128);
1992 res = MAILIMAP_ERROR_MEMORY;
1997 cur = clist_begin(fetch_result);
1999 for(; cur != NULL ; cur = clist_next(cur)) {
2000 struct mailimap_msg_att * msg_att;
2003 struct mailimap_msg_att_dynamic * att_dyn;
2009 msg_att = clist_content(cur);
2013 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2016 if (att_dyn == NULL)
2019 flags = imap_flags_to_flags(att_dyn, &tags);
2021 puid = malloc(sizeof(* puid));
2023 res = MAILIMAP_ERROR_MEMORY;
2028 r = carray_add(tab, puid, NULL);
2031 res = MAILIMAP_ERROR_MEMORY;
2034 pflags = malloc(sizeof(* pflags));
2035 if (pflags == NULL) {
2036 res = MAILIMAP_ERROR_MEMORY;
2040 r = carray_add(tab, pflags, NULL);
2043 res = MAILIMAP_ERROR_MEMORY;
2046 r = carray_add(tab, tags, NULL);
2049 res = MAILIMAP_ERROR_MEMORY;
2056 return MAILIMAP_NO_ERROR;
2059 imap_fetch_uid_flags_list_free(tab);
2060 slist_free_strings_full(tags);
2065 static int imap_get_messages_flags_list(mailimap * imap,
2066 uint32_t first_index,
2071 struct mailimap_fetch_att * fetch_att;
2072 struct mailimap_fetch_type * fetch_type;
2073 struct mailimap_set * set;
2074 clist * fetch_result;
2077 set = mailimap_set_new_interval(first_index, 0);
2079 res = MAILIMAP_ERROR_MEMORY;
2083 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2084 if (fetch_type == NULL) {
2085 res = MAILIMAP_ERROR_MEMORY;
2089 fetch_att = mailimap_fetch_att_new_flags();
2090 if (fetch_att == NULL) {
2091 res = MAILIMAP_ERROR_MEMORY;
2092 goto free_fetch_type;
2095 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2096 if (r != MAILIMAP_NO_ERROR) {
2097 mailimap_fetch_att_free(fetch_att);
2098 res = MAILIMAP_ERROR_MEMORY;
2099 goto free_fetch_type;
2102 fetch_att = mailimap_fetch_att_new_uid();
2103 if (fetch_att == NULL) {
2104 res = MAILIMAP_ERROR_MEMORY;
2105 goto free_fetch_type;
2108 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2109 if (r != MAILIMAP_NO_ERROR) {
2110 mailimap_fetch_att_free(fetch_att);
2111 res = MAILIMAP_ERROR_MEMORY;
2112 goto free_fetch_type;
2115 mailstream_logger = imap_logger_fetch;
2117 r = mailimap_uid_fetch(imap, set,
2118 fetch_type, &fetch_result);
2120 mailstream_logger = imap_logger_cmd;
2121 mailimap_fetch_type_free(fetch_type);
2122 mailimap_set_free(set);
2124 if (r != MAILIMAP_NO_ERROR) {
2130 r = result_to_uid_flags_list(fetch_result, &env_list);
2131 mailimap_fetch_list_free(fetch_result);
2133 * result = env_list;
2135 return MAILIMAP_NO_ERROR;
2138 mailimap_fetch_type_free(fetch_type);
2140 mailimap_set_free(set);
2147 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2149 struct fetch_uid_param * param;
2150 struct fetch_uid_result * result;
2151 carray * fetch_result;
2155 result = op->result;
2159 fetch_result = NULL;
2160 r = imap_get_messages_flags_list(param->imap, param->first_index,
2164 result->fetch_result = fetch_result;
2165 debug_print("imap fetch_uid run - end %i\n", r);
2168 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2169 carray ** fetch_result)
2171 struct fetch_uid_param param;
2172 struct fetch_uid_result result;
2175 debug_print("imap fetch_uid - begin\n");
2177 imap = get_imap(folder);
2179 param.first_index = first_index;
2181 mailstream_logger = imap_logger_noop;
2182 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2184 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2186 mailstream_logger = imap_logger_cmd;
2189 if (result.error != MAILIMAP_NO_ERROR)
2190 return result.error;
2192 debug_print("imap fetch_uid - end\n");
2194 * fetch_result = result.fetch_result;
2196 return result.error;
2200 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2204 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2207 data = carray_get(uid_flags_list, i);
2209 data = carray_get(uid_flags_list, i + 1);
2212 carray_free(uid_flags_list);
2217 static int imap_fetch(mailimap * imap,
2220 size_t * result_len)
2223 struct mailimap_set * set;
2224 struct mailimap_fetch_att * fetch_att;
2225 struct mailimap_fetch_type * fetch_type;
2226 clist * fetch_result;
2227 struct mailimap_msg_att * msg_att;
2228 struct mailimap_msg_att_item * msg_att_item;
2233 struct mailimap_section * section;
2235 set = mailimap_set_new_single(msg_index);
2237 res = MAILIMAP_ERROR_MEMORY;
2241 section = mailimap_section_new(NULL);
2242 if (section == NULL) {
2243 res = MAILIMAP_ERROR_MEMORY;
2247 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2248 if (fetch_att == NULL) {
2249 mailimap_section_free(section);
2250 res = MAILIMAP_ERROR_MEMORY;
2254 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2255 if (fetch_type == NULL) {
2256 res = MAILIMAP_ERROR_MEMORY;
2257 goto free_fetch_att;
2260 mailstream_logger = imap_logger_fetch;
2262 r = mailimap_uid_fetch(imap, set,
2263 fetch_type, &fetch_result);
2265 mailstream_logger = imap_logger_cmd;
2267 mailimap_fetch_type_free(fetch_type);
2268 mailimap_set_free(set);
2271 case MAILIMAP_NO_ERROR:
2277 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2278 mailimap_fetch_list_free(fetch_result);
2279 return MAILIMAP_ERROR_FETCH;
2282 msg_att = clist_begin(fetch_result)->data;
2287 if (msg_att->att_list)
2288 cur = clist_begin(msg_att->att_list);
2292 for(; cur != NULL ; cur = clist_next(cur)) {
2293 msg_att_item = clist_content(cur);
2295 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2296 if (msg_att_item->att_data.att_static->att_type ==
2297 MAILIMAP_MSG_ATT_BODY_SECTION) {
2298 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2300 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2302 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2307 mailimap_fetch_list_free(fetch_result);
2310 return MAILIMAP_ERROR_FETCH;
2313 * result_len = text_length;
2315 return MAILIMAP_NO_ERROR;
2318 mailimap_fetch_att_free(fetch_att);
2320 mailimap_set_free(set);
2325 static int imap_fetch_header(mailimap * imap,
2328 size_t * result_len)
2331 struct mailimap_set * set;
2332 struct mailimap_fetch_att * fetch_att;
2333 struct mailimap_fetch_type * fetch_type;
2334 clist * fetch_result;
2335 struct mailimap_msg_att * msg_att;
2336 struct mailimap_msg_att_item * msg_att_item;
2341 struct mailimap_section * section;
2343 set = mailimap_set_new_single(msg_index);
2345 res = MAILIMAP_ERROR_MEMORY;
2349 section = mailimap_section_new_header();
2350 if (section == NULL) {
2351 res = MAILIMAP_ERROR_MEMORY;
2355 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2356 if (fetch_att == NULL) {
2357 mailimap_section_free(section);
2358 res = MAILIMAP_ERROR_MEMORY;
2362 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2363 if (fetch_type == NULL) {
2364 res = MAILIMAP_ERROR_MEMORY;
2365 goto free_fetch_att;
2368 mailstream_logger = imap_logger_fetch;
2370 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2372 mailstream_logger = imap_logger_cmd;
2373 mailimap_fetch_type_free(fetch_type);
2374 mailimap_set_free(set);
2377 case MAILIMAP_NO_ERROR:
2383 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2384 mailimap_fetch_list_free(fetch_result);
2385 return MAILIMAP_ERROR_FETCH;
2388 msg_att = clist_begin(fetch_result)->data;
2393 if (msg_att->att_list)
2394 cur = clist_begin(msg_att->att_list);
2398 for(; cur != NULL ; cur = clist_next(cur)) {
2399 msg_att_item = clist_content(cur);
2401 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2402 if (msg_att_item->att_data.att_static->att_type ==
2403 MAILIMAP_MSG_ATT_BODY_SECTION) {
2404 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2405 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2407 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2412 mailimap_fetch_list_free(fetch_result);
2415 return MAILIMAP_ERROR_FETCH;
2418 * result_len = text_length;
2420 return MAILIMAP_NO_ERROR;
2423 mailimap_fetch_att_free(fetch_att);
2425 mailimap_set_free(set);
2432 struct fetch_content_param {
2435 const char * filename;
2439 struct fetch_content_result {
2443 static void fetch_content_run(struct etpan_thread_op * op)
2445 struct fetch_content_param * param;
2446 struct fetch_content_result * result;
2448 size_t content_size;
2454 result = op->result;
2460 if (param->with_body)
2461 r = imap_fetch(param->imap, param->msg_index,
2462 &content, &content_size);
2464 r = imap_fetch_header(param->imap, param->msg_index,
2465 &content, &content_size);
2469 if (r == MAILIMAP_NO_ERROR) {
2470 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2472 result->error = MAILIMAP_ERROR_FETCH;
2476 f = fdopen(fd, "wb");
2478 result->error = MAILIMAP_ERROR_FETCH;
2482 r = fwrite(content, 1, content_size, f);
2483 if (r < content_size) {
2484 result->error = MAILIMAP_ERROR_FETCH;
2490 result->error = MAILIMAP_ERROR_FETCH;
2501 claws_unlink(param->filename);
2504 /* mmap_string_unref is a simple free in libetpan
2505 * when it has MMAP_UNAVAILABLE defined */
2506 if (mmap_string_unref(content) != 0)
2510 debug_print("imap fetch_content run - end %i\n", result->error);
2513 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2515 const char * filename)
2517 struct fetch_content_param param;
2518 struct fetch_content_result result;
2521 debug_print("imap fetch_content - begin\n");
2523 imap = get_imap(folder);
2525 param.msg_index = msg_index;
2526 param.filename = filename;
2527 param.with_body = with_body;
2529 threaded_run(folder, ¶m, &result, fetch_content_run);
2531 if (result.error != MAILIMAP_NO_ERROR)
2532 return result.error;
2534 debug_print("imap fetch_content - end\n");
2536 return result.error;
2541 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2546 GSList *tags = NULL;
2550 flag_list = att_dyn->att_list;
2551 if (flag_list == NULL)
2554 for(cur = clist_begin(flag_list) ; cur != NULL ;
2555 cur = clist_next(cur)) {
2556 struct mailimap_flag_fetch * flag_fetch;
2558 flag_fetch = clist_content(cur);
2559 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2562 switch (flag_fetch->fl_flag->fl_type) {
2563 case MAILIMAP_FLAG_ANSWERED:
2564 flags |= MSG_REPLIED;
2566 case MAILIMAP_FLAG_FLAGGED:
2567 flags |= MSG_MARKED;
2569 case MAILIMAP_FLAG_DELETED:
2570 flags |= MSG_DELETED;
2572 case MAILIMAP_FLAG_SEEN:
2573 flags &= ~MSG_UNREAD;
2576 case MAILIMAP_FLAG_KEYWORD:
2577 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2578 flags |= MSG_FORWARDED;
2579 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2581 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2582 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2583 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2586 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2596 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2600 struct mailimap_msg_att_dynamic ** patt_dyn)
2602 clistiter * item_cur;
2606 struct mailimap_msg_att_dynamic * att_dyn;
2613 if (msg_att->att_list)
2614 item_cur = clist_begin(msg_att->att_list);
2617 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2618 struct mailimap_msg_att_item * item;
2620 item = clist_content(item_cur);
2622 switch (item->att_type) {
2623 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2624 switch (item->att_data.att_static->att_type) {
2625 case MAILIMAP_MSG_ATT_UID:
2626 uid = item->att_data.att_static->att_data.att_uid;
2629 case MAILIMAP_MSG_ATT_BODY_SECTION:
2630 if (headers == NULL) {
2631 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2634 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2635 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2640 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2641 if (att_dyn == NULL) {
2642 att_dyn = item->att_data.att_dyn;
2650 if (pheaders != NULL)
2651 * pheaders = headers;
2652 if (pref_size != NULL)
2653 * pref_size = ref_size;
2654 if (patt_dyn != NULL)
2655 * patt_dyn = att_dyn;
2657 return MAIL_NO_ERROR;
2660 static struct imap_fetch_env_info *
2661 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2663 struct imap_fetch_env_info * info;
2667 struct mailimap_msg_att_dynamic * att_dyn;
2669 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2674 info = malloc(sizeof(* info));
2676 info->headers = strdup(headers);
2678 info->flags = imap_flags_to_flags(att_dyn, tags);
2684 imap_fetch_result_to_envelop_list(clist * fetch_result,
2685 carray ** p_env_list)
2690 env_list = carray_new(16);
2693 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2694 cur = clist_next(cur)) {
2695 struct mailimap_msg_att * msg_att;
2696 struct imap_fetch_env_info * env_info;
2697 GSList *tags = NULL;
2699 msg_att = clist_content(cur);
2701 env_info = fetch_to_env_info(msg_att, &tags);
2703 return MAILIMAP_ERROR_MEMORY;
2704 carray_add(env_list, env_info, NULL);
2705 carray_add(env_list, tags, NULL);
2707 * p_env_list = env_list;
2709 * p_env_list = NULL;
2712 return MAIL_NO_ERROR;
2715 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2717 struct mailimap_fetch_att * fetch_att;
2721 struct mailimap_header_list * imap_hdrlist;
2722 struct mailimap_section * section;
2724 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2725 "References", "In-Reply-To", NULL
2728 hdrlist = clist_new();
2730 while (headers[i] != NULL) {
2731 header = strdup(headers[i]);
2732 if (header == NULL || clist_append(hdrlist, header) != 0)
2733 return MAIL_ERROR_MEMORY;
2737 imap_hdrlist = mailimap_header_list_new(hdrlist);
2738 section = mailimap_section_new_header_fields(imap_hdrlist);
2739 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2740 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2742 return MAIL_NO_ERROR;
2745 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2747 struct mailimap_fetch_att * fetch_att;
2748 struct mailimap_section * section;
2750 section = mailimap_section_new_header();
2751 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2752 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2754 return MAIL_NO_ERROR;
2758 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2759 carray ** p_env_list)
2761 struct mailimap_fetch_att * fetch_att;
2762 struct mailimap_fetch_type * fetch_type;
2764 clist * fetch_result;
2766 carray * env_list = NULL;
2770 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2773 fetch_att = mailimap_fetch_att_new_uid();
2774 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2777 fetch_att = mailimap_fetch_att_new_flags();
2778 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2781 fetch_att = mailimap_fetch_att_new_rfc822_size();
2782 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2786 key.len = sizeof(imap);
2787 r = chash_get(courier_workaround_hash, &key, &value);
2789 r = imap_add_envelope_fetch_att(fetch_type);
2791 r = imap_add_header_fetch_att(fetch_type);
2793 mailstream_logger = imap_logger_fetch;
2795 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2797 mailstream_logger = imap_logger_cmd;
2799 case MAILIMAP_NO_ERROR:
2802 mailimap_fetch_type_free(fetch_type);
2803 debug_print("uid_fetch: %d\n", r);
2807 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2808 res = MAILIMAP_ERROR_FETCH;
2809 debug_print("clist_begin = NULL\n");
2813 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2814 mailimap_fetch_list_free(fetch_result);
2816 if (r != MAILIMAP_NO_ERROR) {
2817 mailimap_fetch_type_free(fetch_type);
2818 res = MAILIMAP_ERROR_MEMORY;
2819 debug_print("fetch_result_to_envelop_list: %d\n", res);
2823 mailimap_fetch_type_free(fetch_type);
2825 * p_env_list = env_list;
2827 return MAILIMAP_NO_ERROR;
2833 struct fetch_env_param {
2835 struct mailimap_set * set;
2838 struct fetch_env_result {
2839 carray * fetch_env_result;
2843 static void fetch_env_run(struct etpan_thread_op * op)
2845 struct fetch_env_param * param;
2846 struct fetch_env_result * result;
2851 result = op->result;
2856 r = imap_get_envelopes_list(param->imap, param->set,
2860 result->fetch_env_result = env_list;
2862 debug_print("imap fetch_env run - end %i\n", r);
2865 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2866 carray ** p_env_list)
2868 struct fetch_env_param param;
2869 struct fetch_env_result result;
2872 debug_print("imap fetch_env - begin\n");
2874 imap = get_imap(folder);
2878 threaded_run(folder, ¶m, &result, fetch_env_run);
2880 if (result.error != MAILIMAP_NO_ERROR) {
2886 key.len = sizeof(imap);
2887 r = chash_get(courier_workaround_hash, &key, &value);
2891 chash_set(courier_workaround_hash, &key, &value, NULL);
2893 threaded_run(folder, ¶m, &result, fetch_env_run);
2897 if (result.error != MAILIMAP_NO_ERROR)
2898 return result.error;
2900 debug_print("imap fetch_env - end\n");
2902 * p_env_list = result.fetch_env_result;
2904 return result.error;
2907 void imap_fetch_env_free(carray * env_list)
2911 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2912 struct imap_fetch_env_info * env_info;
2914 env_info = carray_get(env_list, i);
2915 free(env_info->headers);
2918 carray_free(env_list);
2925 struct append_param {
2927 const char * mailbox;
2928 const char * filename;
2929 struct mailimap_flag_list * flag_list;
2932 struct append_result {
2937 static void append_run(struct etpan_thread_op * op)
2939 struct append_param * param;
2940 struct append_result * result;
2945 struct stat stat_buf;
2948 guint32 uid = 0, val = 0;
2951 result = op->result;
2956 r = stat(param->filename, &stat_buf);
2958 result->error = MAILIMAP_ERROR_APPEND;
2961 size = stat_buf.st_size;
2963 fd = g_open(param->filename, O_RDONLY, 0);
2965 result->error = MAILIMAP_ERROR_APPEND;
2969 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2970 if (data == (void *) MAP_FAILED) {
2972 result->error = MAILIMAP_ERROR_APPEND;
2976 data = file_read_to_str_no_recode(param->filename);
2978 result->error = MAILIMAP_ERROR_APPEND;
2981 size = strlen(data);
2983 mailstream_logger = imap_logger_append;
2985 r = mailimap_uidplus_append(param->imap, param->mailbox,
2986 param->flag_list, NULL,
2987 data, size, &val, &uid);
2989 mailstream_logger = imap_logger_cmd;
3000 debug_print("imap append run - end %i uid %d\n", r, uid);
3003 int imap_threaded_append(Folder * folder, const char * mailbox,
3004 const char * filename,
3005 struct mailimap_flag_list * flag_list,
3008 struct append_param param;
3009 struct append_result result;
3012 debug_print("imap append - begin\n");
3014 imap = get_imap(folder);
3016 param.mailbox = mailbox;
3017 param.filename = filename;
3018 param.flag_list = flag_list;
3020 threaded_run(folder, ¶m, &result, append_run);
3022 if (result.error != MAILIMAP_NO_ERROR)
3023 return result.error;
3025 debug_print("imap append - end\n");
3029 return result.error;
3035 struct expunge_param {
3039 struct expunge_result {
3043 static void expunge_run(struct etpan_thread_op * op)
3045 struct expunge_param * param;
3046 struct expunge_result * result;
3050 result = op->result;
3054 r = mailimap_expunge(param->imap);
3057 debug_print("imap expunge run - end %i\n", r);
3060 int imap_threaded_expunge(Folder * folder)
3062 struct expunge_param param;
3063 struct expunge_result result;
3065 debug_print("imap expunge - begin\n");
3067 param.imap = get_imap(folder);
3069 threaded_run(folder, ¶m, &result, expunge_run);
3071 debug_print("imap expunge - end\n");
3073 return result.error;
3079 struct mailimap_set * set;
3083 struct copy_result {
3085 struct mailimap_set *source;
3086 struct mailimap_set *dest;
3089 static void copy_run(struct etpan_thread_op * op)
3091 struct copy_param * param;
3092 struct copy_result * result;
3095 struct mailimap_set *source = NULL, *dest = NULL;
3098 result = op->result;
3102 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3103 &val, &source, &dest);
3107 result->source = source;
3108 result->dest = dest;
3110 result->source = NULL;
3111 result->dest = NULL;
3113 debug_print("imap copy run - end %i\n", r);
3116 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3117 const char * mb, struct mailimap_set **source,
3118 struct mailimap_set **dest)
3120 struct copy_param param;
3121 struct copy_result result;
3124 debug_print("imap copy - begin\n");
3126 imap = get_imap(folder);
3131 threaded_run(folder, ¶m, &result, copy_run);
3135 if (result.error != MAILIMAP_NO_ERROR)
3136 return result.error;
3138 *source = result.source;
3139 *dest = result.dest;
3141 debug_print("imap copy - end\n");
3143 return result.error;
3148 struct store_param {
3150 struct mailimap_set * set;
3151 struct mailimap_store_att_flags * store_att_flags;
3154 struct store_result {
3158 static void store_run(struct etpan_thread_op * op)
3160 struct store_param * param;
3161 struct store_result * result;
3165 result = op->result;
3169 r = mailimap_uid_store(param->imap, param->set,
3170 param->store_att_flags);
3174 debug_print("imap store run - end %i\n", r);
3177 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3178 struct mailimap_store_att_flags * store_att_flags)
3180 struct store_param param;
3181 struct store_result result;
3184 debug_print("imap store - begin\n");
3186 imap = get_imap(folder);
3189 param.store_att_flags = store_att_flags;
3191 threaded_run(folder, ¶m, &result, store_run);
3193 if (result.error != MAILIMAP_NO_ERROR)
3194 return result.error;
3196 debug_print("imap store - end\n");
3198 return result.error;
3202 #define ENV_BUFFER_SIZE 512
3204 static void do_exec_command(int fd, const char * command,
3205 const char * servername, uint16_t port)
3209 char env_buffer[ENV_BUFFER_SIZE];
3213 /* Fork again to become a child of init rather than
3214 the etpan client. */
3219 g_setenv("ETPANSERVER", servername, TRUE);
3221 g_unsetenv("ETPANSERVER");
3226 snprintf(porttext, sizeof(porttext), "%d", port);
3227 g_setenv("ETPANPORT", porttext, TRUE);
3230 g_unsetenv("ETPANPORT");
3233 /* Not a lot we can do if there's an error other than bail. */
3234 if (dup2(fd, 0) == -1)
3236 if (dup2(fd, 1) == -1)
3239 /* Should we close stderr and reopen /dev/null? */
3241 maxopen = sysconf(_SC_OPEN_MAX);
3242 for (i=3; i < maxopen; i++)
3246 /* Detach from the controlling tty if we have one. Otherwise,
3247 SSH might do something stupid like trying to use it instead
3248 of running $SSH_ASKPASS. Doh. */
3249 fd = g_open("/dev/tty", O_RDONLY, 0);
3251 ioctl(fd, TIOCNOTTY, NULL);
3254 #endif /* TIOCNOTTY */
3256 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3258 /* Eep. Shouldn't reach this */
3262 static int subcommand_connect(const char *command,
3263 const char *servername, uint16_t port)
3265 /* SEB unsupported on Windows */
3269 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3274 do_exec_command(sockfds[1], command, servername, port);
3276 else if (childpid == -1) {
3284 /* Reap child, leaving grandchild process to run */
3285 waitpid(childpid, NULL, 0);
3290 static int socket_connect_cmd(mailimap * imap, const char * command,
3291 const char * server, int port)
3297 fd = subcommand_connect(command, server, port);
3299 return MAILIMAP_ERROR_STREAM;
3301 s = mailstream_socket_open(fd);
3304 return MAILIMAP_ERROR_STREAM;
3307 r = mailimap_connect(imap, s);
3308 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3309 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3310 mailstream_close(s);
3312 imap->imap_stream = NULL;
3321 struct connect_cmd_param {
3323 const char * command;
3324 const char * server;
3328 struct connect_cmd_result {
3332 static void connect_cmd_run(struct etpan_thread_op * op)
3335 struct connect_cmd_param * param;
3336 struct connect_cmd_result * result;
3339 result = op->result;
3343 r = socket_connect_cmd(param->imap, param->command,
3344 param->server, param->port);
3350 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3351 const char * server, int port)
3353 struct connect_cmd_param param;
3354 struct connect_cmd_result result;
3357 mailimap * imap, * oldimap;
3359 oldimap = get_imap(folder);
3361 imap = mailimap_new(0, NULL);
3364 debug_print("deleting old imap %p\n", oldimap);
3365 delete_imap(folder, oldimap);
3369 key.len = sizeof(folder);
3372 chash_set(session_hash, &key, &value, NULL);
3375 param.command = command;
3376 param.server = server;
3379 threaded_run(folder, ¶m, &result, connect_cmd_run);
3381 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3383 return result.error;
3385 #endif /* G_OS_WIN32 */
3387 void imap_threaded_cancel(Folder * folder)
3391 imap = get_imap(folder);
3392 if (imap->imap_stream != NULL)
3393 mailstream_cancel(imap->imap_stream);
3398 void imap_main_init(void)
3401 void imap_main_done(gboolean have_connectivity)
3404 void imap_main_set_timeout(int sec)
3408 void imap_threaded_cancel(Folder * folder);