2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2016 DINH Viet Hoa and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
28 #include "imap-thread.h"
30 #include <sys/types.h>
32 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
33 #include <sys/socket.h>
42 #include "etpan-thread-manager.h"
43 #include "etpan-ssl.h"
45 #include "mainwindow.h"
47 #include "file-utils.h"
49 #include "ssl_certificate.h"
51 #include "remotefolder.h"
54 #define DISABLE_LOG_DURING_LOGIN
56 static struct etpan_thread_manager * thread_manager = NULL;
57 static chash * courier_workaround_hash = NULL;
58 static chash * imap_hash = NULL;
59 static chash * session_hash = NULL;
60 static guint thread_manager_signal = 0;
61 static GIOChannel * io_channel = NULL;
63 static int do_mailimap_socket_connect(mailimap * imap, const char * server,
64 gushort port, ProxyInfo * proxy_info)
70 return mailimap_socket_connect(imap, server, port);
75 sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
78 return MAILIMAP_ERROR_CONNECTION_REFUSED;
80 if (proxy_connect(sock, server, port, proxy_info) < 0) {
81 sock_close(sock, TRUE);
82 return MAILIMAP_ERROR_CONNECTION_REFUSED;
85 stream = mailstream_socket_open_timeout(sock->sock,
88 sock_close(sock, TRUE);
89 return MAILIMAP_ERROR_MEMORY;
92 /* Libetpan now has the socket fd, and we're not interested in
93 * rest of the SockInfo struct. Let's free it, while not touching
94 * the socket itself. */
95 sock_close(sock, FALSE);
97 return mailimap_connect(imap, stream);
100 static int do_mailimap_ssl_connect_with_callback(mailimap * imap, const char * server,
102 void (* callback)(struct mailstream_ssl_context * ssl_context, void * data),
104 ProxyInfo *proxy_info)
110 return mailimap_ssl_connect_with_callback(imap, server,
111 port, callback, data);
116 sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
119 debug_print("Can not connect to proxy %s:%d\n",
120 proxy_info->proxy_host, proxy_info->proxy_port);
121 return MAILIMAP_ERROR_CONNECTION_REFUSED;
124 if (proxy_connect(sock, server, port, proxy_info) < 0) {
125 debug_print("Can not make proxy connection via %s:%d\n",
126 proxy_info->proxy_host, proxy_info->proxy_port);
127 sock_close(sock, TRUE);
128 return MAILIMAP_ERROR_CONNECTION_REFUSED;
131 stream = mailstream_ssl_open_with_callback_timeout(sock->sock,
132 imap->imap_timeout, callback, data);
133 if (stream == NULL) {
134 sock_close(sock, TRUE);
135 return MAILIMAP_ERROR_SSL;
138 /* Libetpan now has the socket fd, and we're not interested in
139 * rest of the SockInfo struct. Let's free it, while not touching
140 * the socket itself. */
141 sock_close(sock, FALSE);
143 return mailimap_connect(imap, stream);
146 static gboolean thread_manager_event(GIOChannel * source,
147 GIOCondition condition,
154 if (condition & G_IO_IN)
155 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
157 etpan_thread_manager_loop(thread_manager);
162 static void imap_logger_noop(int direction, const char * str, size_t size)
164 /* inhibit logging */
167 static void imap_logger_cmd(int direction, const char * str, size_t size)
174 log_print(LOG_PROTOCOL, "IMAP%c [CMD data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
177 buf = malloc(size+1);
178 memset(buf, 0, size+1);
179 strncpy(buf, str, size);
182 if (!strncmp(buf, "<<<<<<<", 7)
183 || !strncmp(buf, ">>>>>>>", 7)) {
187 while (strstr(buf, "\r"))
188 *strstr(buf, "\r") = ' ';
189 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
190 buf[strlen(buf)-1] = '\0';
192 lines = g_strsplit(buf, "\n", -1);
194 while (lines[i] && *lines[i]) {
195 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
202 static void imap_logger_fetch(int direction, const char * str, size_t size)
208 if (size > 128 && !direction) {
209 log_print(LOG_PROTOCOL, "IMAP%c [FETCH data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
213 buf = malloc(size+1);
214 memset(buf, 0, size+1);
215 strncpy(buf, str, size);
217 if (!strncmp(buf, "<<<<<<<", 7)
218 || !strncmp(buf, ">>>>>>>", 7)) {
222 while (strstr(buf, "\r"))
223 *strstr(buf, "\r") = ' ';
224 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
225 buf[strlen(buf)-1] = '\0';
227 lines = g_strsplit(buf, "\n", -1);
229 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
230 while (lines[i] && *lines[i]) {
231 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
235 log_print(LOG_PROTOCOL, "IMAP%c [data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
241 static void imap_logger_uid(int direction, const char * str, size_t size)
248 log_print(LOG_PROTOCOL, "IMAP%c [UID data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
251 buf = malloc(size+1);
252 memset(buf, 0, size+1);
253 strncpy(buf, str, size);
255 if (!strncmp(buf, "<<<<<<<", 7)
256 || !strncmp(buf, ">>>>>>>", 7)) {
260 while (strstr(buf, "\r"))
261 *strstr(buf, "\r") = ' ';
262 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
263 buf[strlen(buf)-1] = '\0';
265 lines = g_strsplit(buf, "\n", -1);
267 while (lines[i] && *lines[i]) {
268 int llen = strlen(lines[i]);
270 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
273 strncpy2(tmp, lines[i], 63);
274 log_print(LOG_PROTOCOL, "IMAP%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
283 static void imap_logger_append(int direction, const char * str, size_t size)
290 log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
292 } else if (direction == 0 && size > 64) {
293 log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
296 buf = malloc(size+1);
297 memset(buf, 0, size+1);
298 strncpy(buf, str, size);
300 if (!strncmp(buf, "<<<<<<<", 7)
301 || !strncmp(buf, ">>>>>>>", 7)) {
305 while (strstr(buf, "\r"))
306 *strstr(buf, "\r") = ' ';
307 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
308 buf[strlen(buf)-1] = '\0';
310 lines = g_strsplit(buf, "\n", -1);
312 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
313 while (lines[i] && *lines[i]) {
314 log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
318 log_print(LOG_PROTOCOL, "IMAP%c [data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
324 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
325 gboolean etpan_skip_ssl_cert_check = FALSE;
326 extern void mailsasl_ref(void);
328 void imap_main_init(gboolean skip_ssl_cert_check)
330 int fd_thread_manager;
332 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
333 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
334 mailstream_network_delay.tv_usec = 0;
336 mailstream_debug = 1;
337 mailstream_logger = imap_logger_cmd;
340 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
341 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
342 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
344 thread_manager = etpan_thread_manager_new();
346 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
349 io_channel = g_io_channel_unix_new(fd_thread_manager);
351 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
353 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
354 thread_manager_event,
359 void imap_main_set_timeout(int sec)
361 mailstream_network_delay.tv_sec = sec;
362 mailstream_network_delay.tv_usec = 0;
365 void imap_main_done(gboolean have_connectivity)
367 imap_disconnect_all(have_connectivity);
368 etpan_thread_manager_stop(thread_manager);
369 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
372 etpan_thread_manager_join(thread_manager);
374 g_source_remove(thread_manager_signal);
375 g_io_channel_unref(io_channel);
377 etpan_thread_manager_free(thread_manager);
379 chash_free(courier_workaround_hash);
380 chash_free(session_hash);
381 chash_free(imap_hash);
384 void imap_init(Folder * folder)
386 struct etpan_thread * thread;
390 thread = etpan_thread_manager_get_thread(thread_manager);
393 key.len = sizeof(folder);
397 chash_set(imap_hash, &key, &value, NULL);
400 void imap_done(Folder * folder)
402 struct etpan_thread * thread;
408 key.len = sizeof(folder);
410 r = chash_get(imap_hash, &key, &value);
416 etpan_thread_unbind(thread);
418 chash_delete(imap_hash, &key, NULL);
420 debug_print("remove thread\n");
423 static struct etpan_thread * get_thread(Folder * folder)
425 struct etpan_thread * thread;
431 key.len = sizeof(folder);
433 r = chash_get(imap_hash, &key, &value);
442 static mailimap * get_imap(Folder * folder)
450 key.len = sizeof(folder);
452 r = chash_get(session_hash, &key, &value);
457 debug_print("found imap %p\n", imap);
461 static gboolean cb_show_error(gpointer data)
463 mainwindow_show_error();
467 static void generic_cb(int cancelled, void * result, void * callback_data)
469 struct etpan_thread_op * op;
471 op = (struct etpan_thread_op *) callback_data;
473 debug_print("generic_cb\n");
474 if (op->imap && op->imap->imap_response_info &&
475 op->imap->imap_response_info->rsp_alert) {
476 log_error(LOG_PROTOCOL, "IMAP< Alert: %s\n",
477 op->imap->imap_response_info->rsp_alert);
478 g_timeout_add(10, cb_show_error, NULL);
483 /* Please do *not* blindly use imap pointers after this function returns,
484 * someone may have deleted it while this function was waiting for completion.
485 * Check return value to see if imap is still valid.
486 * Run get_imap(folder) again to get a fresh and valid pointer.
488 static int threaded_run(Folder * folder, void * param, void * result,
489 void (* func)(struct etpan_thread_op * ))
491 struct etpan_thread_op * op;
492 struct etpan_thread * thread;
493 struct mailimap * imap = get_imap(folder);
495 imap_folder_ref(folder);
497 op = etpan_thread_op_new();
504 op->callback = generic_cb;
505 op->callback_data = op;
507 thread = get_thread(folder);
508 etpan_thread_op_schedule(thread, op);
510 while (!op->finished) {
511 gtk_main_iteration();
514 etpan_thread_op_free(op);
516 imap_folder_unref(folder);
518 if (imap != get_imap(folder)) {
519 g_warning("returning from operation on a stale imap %p", imap);
529 struct connect_param {
531 PrefsAccount *account;
534 ProxyInfo * proxy_info;
537 struct connect_result {
541 #define CHECK_IMAP() { \
542 if (!param->imap) { \
543 result->error = MAILIMAP_ERROR_BAD_STATE; \
549 static void delete_imap_run(struct etpan_thread_op * op)
551 mailimap * imap = op->imap;
553 /* we don't want libetpan to logout */
554 if (imap->imap_stream) {
555 mailstream_close(imap->imap_stream);
556 imap->imap_stream = NULL;
562 static void threaded_delete_imap(Folder *folder, mailimap *imap)
564 struct etpan_thread_op * op;
566 /* No need to wait for completion, threaded_run() won't work here. */
567 op = etpan_thread_op_new();
569 op->run = delete_imap_run;
570 op->cleanup = etpan_thread_op_free;
572 etpan_thread_op_schedule(get_thread(folder), op);
574 debug_print("threaded delete imap posted\n");
577 static void delete_imap(Folder *folder, mailimap *imap)
582 key.len = sizeof(folder);
583 chash_delete(session_hash, &key, NULL);
588 key.len = sizeof(imap);
589 chash_delete(courier_workaround_hash, &key, NULL);
590 /* We can't just free imap here as there may be ops on it pending
591 * in the thread. Posting freeing as an op will synchronize against
592 * existing jobs and as imap is already removed from session_hash
593 * we are sure no new ops can be posted. */
594 threaded_delete_imap(folder, imap);
597 static void connect_run(struct etpan_thread_op * op)
600 struct connect_param * param;
601 struct connect_result * result;
608 r = do_mailimap_socket_connect(param->imap,
609 param->server, param->port, param->proxy_info);
615 int imap_threaded_connect(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
617 struct connect_param param;
618 struct connect_result result;
621 mailimap * imap, * oldimap;
623 oldimap = get_imap(folder);
625 imap = mailimap_new(0, NULL);
628 debug_print("deleting old imap %p\n", oldimap);
629 delete_imap(folder, oldimap);
633 key.len = sizeof(folder);
636 chash_set(session_hash, &key, &value, NULL);
639 param.server = server;
641 param.proxy_info = proxy_info;
644 threaded_run(folder, ¶m, &result, connect_run);
646 debug_print("connect ok %i with imap %p\n", result.error, imap);
651 static void connect_ssl_run(struct etpan_thread_op * op)
654 struct connect_param * param;
655 struct connect_result * result;
662 r = do_mailimap_ssl_connect_with_callback(param->imap,
663 param->server, param->port,
664 etpan_connect_ssl_context_cb, param->account,
669 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
671 struct connect_param param;
672 struct connect_result result;
675 mailimap * imap, * oldimap;
676 gboolean accept_if_valid = FALSE;
678 oldimap = get_imap(folder);
680 imap = mailimap_new(0, NULL);
683 debug_print("deleting old imap %p\n", oldimap);
684 delete_imap(folder, oldimap);
688 key.len = sizeof(folder);
691 chash_set(session_hash, &key, &value, NULL);
694 param.server = server;
696 param.account = folder->account;
697 param.proxy_info = proxy_info;
700 accept_if_valid = folder->account->ssl_certs_auto_accept;
703 if (threaded_run(folder, ¶m, &result, connect_ssl_run))
704 return MAILIMAP_ERROR_INVAL;
706 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
707 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
708 if (etpan_certificate_check(imap->imap_stream, server, port,
709 accept_if_valid) != TRUE)
710 result.error = MAILIMAP_ERROR_SSL;
712 debug_print("connect %d with imap %p\n", result.error, imap);
723 struct mailimap_capability_data *caps;
726 static void capability_run(struct etpan_thread_op * op)
729 struct capa_param * param;
730 struct capa_result * result;
731 struct mailimap_capability_data *caps;
738 r = mailimap_capability(param->imap, &caps);
741 result->caps = (r == 0 ? caps : NULL);
745 int imap_threaded_capability(Folder *folder, struct mailimap_capability_data ** caps)
747 struct capa_param param;
748 struct capa_result result;
751 imap = get_imap(folder);
755 threaded_run(folder, ¶m, &result, capability_run);
757 debug_print("capa %d\n", result.error);
759 if (result.error == MAILIMAP_NO_ERROR)
766 struct disconnect_param {
770 struct disconnect_result {
774 static void disconnect_run(struct etpan_thread_op * op)
777 struct disconnect_param * param;
778 struct disconnect_result * result;
785 r = mailimap_logout(param->imap);
790 void imap_threaded_disconnect(Folder * folder)
792 struct connect_param param;
793 struct connect_result result;
796 imap = get_imap(folder);
798 debug_print("was disconnected\n");
804 if (threaded_run(folder, ¶m, &result, disconnect_run)) {
805 debug_print("imap already deleted %p\n", imap);
807 debug_print("deleting old imap %p\n", imap);
808 delete_imap(folder, imap);
811 debug_print("disconnect ok\n");
818 const char * wildcard;
827 static void list_run(struct etpan_thread_op * op)
829 struct list_param * param;
830 struct list_result * result;
841 if (param->base == NULL || param->wildcard == NULL) {
844 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
848 r = mailimap_lsub(param->imap, param->base,
849 param->wildcard, &list);
851 r = mailimap_list(param->imap, param->base,
852 param->wildcard, &list);
855 debug_print("imap list run - end\n");
858 int imap_threaded_list(Folder * folder, const char * base,
859 const char * wildcard,
862 struct list_param param;
863 struct list_result result;
865 debug_print("imap list - begin\n");
867 param.imap = get_imap(folder);
869 param.wildcard = wildcard;
870 param.sub_only = FALSE;
872 threaded_run(folder, ¶m, &result, list_run);
874 * p_result = result.list;
876 debug_print("imap list - end %p\n", result.list);
881 int imap_threaded_lsub(Folder * folder, const char * base,
882 const char * wildcard,
885 struct list_param param;
886 struct list_result result;
888 debug_print("imap lsub - begin\n");
890 param.imap = get_imap(folder);
892 param.wildcard = wildcard;
893 param.sub_only = TRUE;
895 threaded_run(folder, ¶m, &result, list_run);
897 * p_result = result.list;
899 debug_print("imap lsub - end %p\n", result.list);
904 struct subscribe_param {
910 struct subscribe_result {
914 static void subscribe_run(struct etpan_thread_op * op)
916 struct subscribe_param * param;
917 struct subscribe_result * result;
925 if (param->mb == NULL) {
927 debug_print("no mb\n");
930 if (param->subscribe)
931 r = mailimap_subscribe(param->imap, param->mb);
933 r = mailimap_unsubscribe(param->imap, param->mb);
935 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
938 int imap_threaded_subscribe(Folder * folder, const char * mb,
941 struct subscribe_param param;
942 struct subscribe_result result;
944 debug_print("imap list - begin\n");
946 param.imap = get_imap(folder);
948 param.subscribe = subscribe;
950 threaded_run(folder, ¶m, &result, subscribe_run);
958 const char * password;
963 struct login_result {
967 static void login_run(struct etpan_thread_op * op)
969 struct login_param * param;
970 struct login_result * result;
972 #ifdef DISABLE_LOG_DURING_LOGIN
981 #ifdef DISABLE_LOG_DURING_LOGIN
982 old_debug = mailstream_debug;
983 mailstream_debug = 0;
985 if (!strcmp(param->type, "plaintext"))
986 r = mailimap_login(param->imap,
987 param->login, param->password);
988 else if (!strcmp(param->type, "GSSAPI"))
989 r = mailimap_authenticate(param->imap,
990 param->type, param->server, NULL, NULL,
991 param->login, param->login,
992 param->password, NULL);
993 else if (!strcmp(param->type, "SCRAM-SHA-1"))
994 /* 7th argument has to be NULL here, to stop libetpan sending the
995 * a= attribute in its initial SCRAM-SHA-1 message to server. At least
996 * Dovecot 2.2 doesn't seem to like that, and will not authenticate
998 r = mailimap_authenticate(param->imap,
999 param->type, NULL, NULL, NULL,
1001 param->password, NULL);
1003 r = mailimap_authenticate(param->imap,
1004 param->type, NULL, NULL, NULL,
1005 param->login, param->login,
1006 param->password, NULL);
1007 #ifdef DISABLE_LOG_DURING_LOGIN
1008 mailstream_debug = old_debug;
1012 if (param->imap->imap_response)
1013 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
1014 debug_print("imap login run - end %i\n", r);
1017 int imap_threaded_login(Folder * folder,
1018 const char * login, const char * password,
1021 struct login_param param;
1022 struct login_result result;
1024 debug_print("imap login - begin\n");
1027 return MAILIMAP_ERROR_INVAL;
1029 param.imap = get_imap(folder);
1030 param.login = login;
1031 param.password = password;
1033 if (folder && folder->account)
1034 param.server = folder->account->recv_server;
1036 param.server = NULL;
1038 threaded_run(folder, ¶m, &result, login_run);
1040 debug_print("imap login - end\n");
1042 return result.error;
1046 struct status_param {
1049 struct mailimap_status_att_list * status_att_list;
1052 struct status_result {
1054 struct mailimap_mailbox_data_status * data_status;
1057 static void status_run(struct etpan_thread_op * op)
1059 struct status_param * param;
1060 struct status_result * result;
1064 result = op->result;
1068 r = mailimap_status(param->imap, param->mb,
1069 param->status_att_list,
1070 &result->data_status);
1073 debug_print("imap status run - end %i\n", r);
1076 int imap_threaded_status(Folder * folder, const char * mb,
1077 struct mailimap_mailbox_data_status ** data_status,
1080 struct status_param param;
1081 struct status_result result;
1082 struct mailimap_status_att_list * status_att_list;
1084 debug_print("imap status - begin\n");
1086 status_att_list = mailimap_status_att_list_new_empty();
1087 if (mask & 1 << 0) {
1088 mailimap_status_att_list_add(status_att_list,
1089 MAILIMAP_STATUS_ATT_MESSAGES);
1091 if (mask & 1 << 1) {
1092 mailimap_status_att_list_add(status_att_list,
1093 MAILIMAP_STATUS_ATT_RECENT);
1095 if (mask & 1 << 2) {
1096 mailimap_status_att_list_add(status_att_list,
1097 MAILIMAP_STATUS_ATT_UIDNEXT);
1099 if (mask & 1 << 3) {
1100 mailimap_status_att_list_add(status_att_list,
1101 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1103 if (mask & 1 << 4) {
1104 mailimap_status_att_list_add(status_att_list,
1105 MAILIMAP_STATUS_ATT_UNSEEN);
1107 param.imap = get_imap(folder);
1109 param.status_att_list = status_att_list;
1111 threaded_run(folder, ¶m, &result, status_run);
1113 debug_print("imap status - end\n");
1115 * data_status = result.data_status;
1117 mailimap_status_att_list_free(status_att_list);
1119 return result.error;
1128 struct noop_result {
1132 static void noop_run(struct etpan_thread_op * op)
1134 struct noop_param * param;
1135 struct noop_result * result;
1139 result = op->result;
1143 r = mailimap_noop(param->imap);
1146 debug_print("imap noop run - end %i\n", r);
1149 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1150 unsigned int *p_recent,
1151 unsigned int *p_expunge,
1152 unsigned int *p_unseen,
1153 unsigned int *p_uidnext,
1154 unsigned int *p_uidval)
1156 struct noop_param param;
1157 struct noop_result result;
1160 debug_print("imap noop - begin\n");
1162 imap = get_imap(folder);
1165 if (threaded_run(folder, ¶m, &result, noop_run))
1166 return MAILIMAP_ERROR_INVAL;
1168 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1169 * p_exists = imap->imap_selection_info->sel_exists;
1170 * p_recent = imap->imap_selection_info->sel_recent;
1171 * p_unseen = imap->imap_selection_info->sel_unseen;
1172 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1173 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1181 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1182 imap->imap_response_info->rsp_expunged != NULL) {
1183 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1187 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1188 *p_exists, *p_recent, *p_expunge, *p_unseen,
1189 *p_uidnext, *p_uidval);
1191 return result.error;
1195 struct starttls_result {
1199 static void starttls_run(struct etpan_thread_op * op)
1201 struct connect_param * param;
1202 struct starttls_result * result;
1206 result = op->result;
1210 r = mailimap_starttls(param->imap);
1213 debug_print("imap STARTTLS run - end %i\n", r);
1216 mailimap *imap = param->imap;
1217 mailstream_low *plain_low = NULL;
1218 mailstream_low *tls_low = NULL;
1221 plain_low = mailstream_get_low(imap->imap_stream);
1222 fd = mailstream_low_get_fd(plain_low);
1224 debug_print("imap STARTTLS run - can't get fd\n");
1225 result->error = MAILIMAP_ERROR_STREAM;
1229 tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
1230 if (tls_low == NULL) {
1231 debug_print("imap STARTTLS run - can't tls_open\n");
1232 result->error = MAILIMAP_ERROR_STREAM;
1235 mailstream_low_free(plain_low);
1236 mailstream_set_low(imap->imap_stream, tls_low);
1240 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1242 struct connect_param param;
1243 struct starttls_result result;
1244 gboolean accept_if_valid = FALSE;
1246 debug_print("imap STARTTLS - begin\n");
1248 param.imap = get_imap(folder);
1249 param.server = host;
1251 param.account = folder->account;
1253 if (folder->account)
1254 accept_if_valid = folder->account->ssl_certs_auto_accept;
1256 if (threaded_run(folder, ¶m, &result, starttls_run))
1257 return MAILIMAP_ERROR_INVAL;
1259 debug_print("imap STARTTLS - end\n");
1261 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1262 if (etpan_certificate_check(param.imap->imap_stream, host, port,
1263 accept_if_valid) != TRUE)
1264 return MAILIMAP_ERROR_SSL;
1266 return result.error;
1270 struct create_param {
1275 struct create_result {
1279 static void create_run(struct etpan_thread_op * op)
1281 struct create_param * param;
1282 struct create_result * result;
1286 result = op->result;
1290 r = mailimap_create(param->imap, param->mb);
1293 debug_print("imap create run - end %i\n", r);
1296 int imap_threaded_create(Folder * folder, const char * mb)
1298 struct create_param param;
1299 struct create_result result;
1301 debug_print("imap create - begin\n");
1303 param.imap = get_imap(folder);
1306 threaded_run(folder, ¶m, &result, create_run);
1308 debug_print("imap create - end\n");
1310 return result.error;
1316 struct rename_param {
1319 const char * new_name;
1322 struct rename_result {
1326 static void rename_run(struct etpan_thread_op * op)
1328 struct rename_param * param;
1329 struct rename_result * result;
1333 result = op->result;
1337 r = mailimap_rename(param->imap, param->mb, param->new_name);
1340 debug_print("imap rename run - end %i\n", r);
1343 int imap_threaded_rename(Folder * folder,
1344 const char * mb, const char * new_name)
1346 struct rename_param param;
1347 struct rename_result result;
1349 debug_print("imap rename - begin\n");
1351 param.imap = get_imap(folder);
1353 param.new_name = new_name;
1355 threaded_run(folder, ¶m, &result, rename_run);
1357 debug_print("imap rename - end\n");
1359 return result.error;
1365 struct delete_param {
1370 struct delete_result {
1374 static void delete_run(struct etpan_thread_op * op)
1376 struct delete_param * param;
1377 struct delete_result * result;
1381 result = op->result;
1385 r = mailimap_delete(param->imap, param->mb);
1388 debug_print("imap delete run - end %i\n", r);
1391 int imap_threaded_delete(Folder * folder, const char * mb)
1393 struct delete_param param;
1394 struct delete_result result;
1396 debug_print("imap delete - begin\n");
1398 param.imap = get_imap(folder);
1401 threaded_run(folder, ¶m, &result, delete_run);
1403 debug_print("imap delete - end\n");
1405 return result.error;
1410 struct select_param {
1415 struct select_result {
1419 static void select_run(struct etpan_thread_op * op)
1421 struct select_param * param;
1422 struct select_result * result;
1426 result = op->result;
1430 r = mailimap_select(param->imap, param->mb);
1433 debug_print("imap select run - end %i\n", r);
1436 int imap_threaded_select(Folder * folder, const char * mb,
1437 gint * exists, gint * recent, gint * unseen,
1438 guint32 * uid_validity,gint *can_create_flags,
1441 struct select_param param;
1442 struct select_result result;
1445 debug_print("imap select - begin\n");
1447 imap = get_imap(folder);
1451 if (threaded_run(folder, ¶m, &result, select_run))
1452 return MAILIMAP_ERROR_INVAL;
1454 if (result.error != MAILIMAP_NO_ERROR)
1455 return result.error;
1457 if (!imap || imap->imap_selection_info == NULL)
1458 return MAILIMAP_ERROR_PARSE;
1460 * exists = imap->imap_selection_info->sel_exists;
1461 * recent = imap->imap_selection_info->sel_recent;
1462 * unseen = imap->imap_selection_info->sel_unseen;
1463 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1464 * can_create_flags = FALSE;
1466 if (imap->imap_selection_info->sel_perm_flags) {
1467 GSList *t_flags = NULL;
1468 clistiter *cur = NULL;
1469 if (imap->imap_selection_info->sel_perm_flags)
1470 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1472 for (; cur; cur = clist_next(cur)) {
1473 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1474 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1475 *can_create_flags = TRUE;
1476 else if (flag->fl_flag &&
1477 flag->fl_flag->fl_type == 6 &&
1478 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1479 *can_create_flags = TRUE;
1480 if (flag->fl_flag && ok_flags) {
1481 MsgPermFlags c_flag = 0;
1482 switch (flag->fl_flag->fl_type) {
1483 case MAILIMAP_FLAG_ANSWERED:
1484 c_flag = IMAP_FLAG_ANSWERED;
1486 case MAILIMAP_FLAG_FLAGGED:
1487 c_flag = IMAP_FLAG_FLAGGED;
1489 case MAILIMAP_FLAG_DELETED:
1490 c_flag = IMAP_FLAG_DELETED;
1492 case MAILIMAP_FLAG_DRAFT:
1493 c_flag = IMAP_FLAG_DRAFT;
1495 case MAILIMAP_FLAG_SEEN:
1496 c_flag = IMAP_FLAG_SEEN;
1498 case MAILIMAP_FLAG_KEYWORD:
1499 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1500 c_flag = IMAP_FLAG_FORWARDED;
1501 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1502 c_flag = IMAP_FLAG_SPAM;
1503 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1504 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1505 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1506 c_flag = IMAP_FLAG_HAM;
1512 t_flags = g_slist_prepend(t_flags,
1513 GUINT_TO_POINTER(c_flag));
1518 *ok_flags = t_flags;
1520 debug_print("imap select - end\n");
1522 return result.error;
1525 static void close_run(struct etpan_thread_op * op)
1527 struct select_param * param;
1528 struct select_result * result;
1532 result = op->result;
1536 r = mailimap_close(param->imap);
1539 debug_print("imap close run - end %i\n", r);
1542 int imap_threaded_close(Folder * folder)
1544 struct select_param param;
1545 struct select_result result;
1548 debug_print("imap close - begin\n");
1550 imap = get_imap(folder);
1553 threaded_run(folder, ¶m, &result, close_run);
1555 if (result.error != MAILIMAP_NO_ERROR)
1556 return result.error;
1558 debug_print("imap close - end\n");
1560 return result.error;
1563 struct examine_param {
1568 struct examine_result {
1572 static void examine_run(struct etpan_thread_op * op)
1574 struct examine_param * param;
1575 struct examine_result * result;
1579 result = op->result;
1583 r = mailimap_examine(param->imap, param->mb);
1586 debug_print("imap examine run - end %i\n", r);
1589 int imap_threaded_examine(Folder * folder, const char * mb,
1590 gint * exists, gint * recent, gint * unseen,
1591 guint32 * uid_validity)
1593 struct examine_param param;
1594 struct examine_result result;
1597 debug_print("imap examine - begin\n");
1599 imap = get_imap(folder);
1603 if (threaded_run(folder, ¶m, &result, examine_run))
1604 return MAILIMAP_ERROR_INVAL;
1606 if (result.error != MAILIMAP_NO_ERROR)
1607 return result.error;
1609 if (!imap || imap->imap_selection_info == NULL)
1610 return MAILIMAP_ERROR_PARSE;
1612 * exists = imap->imap_selection_info->sel_exists;
1613 * recent = imap->imap_selection_info->sel_recent;
1614 * unseen = imap->imap_selection_info->sel_unseen;
1615 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1617 debug_print("imap examine - end\n");
1619 return result.error;
1625 struct search_param {
1628 const char *charset;
1629 struct mailimap_set * set;
1633 struct search_result {
1635 clist * search_result;
1638 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1640 return mailimap_set_item_new(orig->set_first, orig->set_last);
1643 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1645 clist *list = orig ? orig->set_list : NULL;
1652 newlist = clist_new();
1656 for (cur = clist_begin(list); cur; cur = clist_next(cur)) {
1657 if (clist_append(newlist,
1658 sc_mailimap_set_item_copy(
1659 (struct mailimap_set_item *)clist_content(cur))) != 0) {
1660 clist_free(newlist);
1664 return mailimap_set_new(newlist);
1667 static void search_run(struct etpan_thread_op * op)
1669 struct search_param * param;
1670 struct search_result * result;
1672 struct mailimap_search_key * key = NULL;
1673 struct mailimap_search_key * uid_key = NULL;
1674 struct mailimap_search_key * search_type_key = NULL;
1675 clist * search_result;
1678 result = op->result;
1682 /* we copy the mailimap_set because freeing the key is recursive */
1683 if (param->set != NULL) {
1684 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1685 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1686 uid_key = mailimap_search_key_new_all();
1688 switch (param->type) {
1689 case IMAP_SEARCH_TYPE_SIMPLE:
1690 search_type_key = NULL;
1692 case IMAP_SEARCH_TYPE_SEEN:
1693 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
1695 case IMAP_SEARCH_TYPE_UNSEEN:
1696 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
1698 case IMAP_SEARCH_TYPE_ANSWERED:
1699 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
1701 case IMAP_SEARCH_TYPE_FLAGGED:
1702 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
1704 case IMAP_SEARCH_TYPE_DELETED:
1705 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
1707 case IMAP_SEARCH_TYPE_FORWARDED:
1708 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
1710 case IMAP_SEARCH_TYPE_SPAM:
1711 search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
1713 case IMAP_SEARCH_TYPE_KEYED:
1714 search_type_key = param->key;
1718 if (search_type_key != NULL) {
1719 if (param->set != NULL) {
1720 key = mailimap_search_key_new_multiple_empty();
1721 mailimap_search_key_multiple_add(key, search_type_key);
1722 mailimap_search_key_multiple_add(key, uid_key);
1724 key = search_type_key;
1726 } else if (uid_key != NULL) {
1731 g_warning("no key!");
1732 result = op->result;
1734 result->search_result = NULL;
1736 mailstream_logger = imap_logger_uid;
1738 r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
1740 mailstream_logger = imap_logger_cmd;
1742 /* free the key (with the imapset) */
1743 mailimap_search_key_free(key);
1746 result->search_result = search_result;
1748 debug_print("imap search run - end %i\n", result->error);
1751 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
1752 const char *charset, struct mailimap_set * set,
1753 clist ** search_result)
1755 struct search_param param;
1756 struct search_result result;
1759 debug_print("imap search - begin\n");
1761 imap = get_imap(folder);
1764 param.charset = charset;
1765 param.type = search_type;
1768 threaded_run(folder, ¶m, &result, search_run);
1770 if (result.error != MAILIMAP_NO_ERROR)
1771 return result.error;
1773 debug_print("imap search - end\n");
1775 * search_result = result.search_result;
1777 return result.error;
1781 struct _IMAPSearchKey {
1782 struct mailimap_search_key* key;
1785 IMAPSearchKey* imap_search_new(gint criteria,
1786 const gchar *header,
1790 char* sk_bcc = NULL;
1791 struct mailimap_date* sk_before = NULL;
1792 char* sk_body = NULL;
1794 char* sk_from = NULL;
1795 char* sk_keyword = NULL;
1796 struct mailimap_date* sk_on = NULL;
1797 struct mailimap_date* sk_since = NULL;
1798 char* sk_subject = NULL;
1799 char* sk_text = NULL;
1801 char* sk_unkeyword = NULL;
1802 char* sk_header_name = NULL;
1803 char* sk_header_value = NULL;
1804 uint32_t sk_larger = 0;
1805 struct mailimap_search_key* sk_not = NULL;
1806 struct mailimap_search_key* sk_or1 = NULL;
1807 struct mailimap_search_key* sk_or2 = NULL;
1808 struct mailimap_date* sk_sentbefore = NULL;
1809 struct mailimap_date* sk_senton = NULL;
1810 struct mailimap_date* sk_sentsince = NULL;
1811 uint32_t sk_smaller = 0;
1812 struct mailimap_set* sk_uid = NULL;
1813 struct mailimap_set* sk_set = NULL;
1814 clist* sk_multiple = NULL;
1815 int etpan_matcher_type;
1818 case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
1819 case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
1820 case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
1821 case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
1822 case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
1823 case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
1824 case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
1826 case IMAP_SEARCH_CRITERIA_TAG:
1827 sk_keyword = strdup(expr);
1828 etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
1831 case IMAP_SEARCH_CRITERIA_SUBJECT:
1832 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
1833 sk_subject = strdup(expr);
1836 case IMAP_SEARCH_CRITERIA_TO:
1837 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
1838 sk_to = strdup(expr);
1841 case IMAP_SEARCH_CRITERIA_CC:
1842 etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
1843 sk_cc = strdup(expr);
1846 case IMAP_SEARCH_CRITERIA_AGE_GREATER:
1847 case IMAP_SEARCH_CRITERIA_AGE_LOWER:
1850 time_t limit = time(NULL) - 60 * 60 * 24 * value;
1853 localtime_r(&limit, &tm);
1854 if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
1855 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
1856 sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1858 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
1859 sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
1864 case IMAP_SEARCH_CRITERIA_BODY:
1865 etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
1866 sk_body = strdup(expr);
1869 case IMAP_SEARCH_CRITERIA_MESSAGE:
1870 etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
1871 sk_text = strdup(expr);
1874 case IMAP_SEARCH_CRITERIA_HEADER:
1875 etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
1876 sk_header_name = strdup(header);
1877 sk_header_value = strdup(expr);
1880 case IMAP_SEARCH_CRITERIA_FROM:
1881 etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
1882 sk_from = strdup(expr);
1885 case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
1886 etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
1890 case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
1891 etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
1899 return mailimap_search_key_new(etpan_matcher_type,
1900 sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
1901 sk_on, sk_since, sk_subject, sk_text, sk_to,
1902 sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
1903 sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
1904 sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
1907 IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
1909 return mailimap_search_key_new_not(key);
1912 IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
1914 return mailimap_search_key_new_or(l, r);
1917 IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
1919 IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
1920 mailimap_search_key_multiple_add(result, l);
1921 mailimap_search_key_multiple_add(result, r);
1926 void imap_search_free(IMAPSearchKey* key)
1931 mailimap_search_key_free(key);
1936 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1940 struct mailimap_msg_att_dynamic ** patt_dyn);
1943 result_to_uid_list(clist * fetch_result, carray ** result)
1945 clistiter * cur = NULL;
1950 tab = carray_new(128);
1952 res = MAILIMAP_ERROR_MEMORY;
1957 cur = clist_begin(fetch_result);
1959 for(; cur != NULL ; cur = clist_next(cur)) {
1960 struct mailimap_msg_att * msg_att;
1964 msg_att = clist_content(cur);
1967 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1969 puid = malloc(sizeof(* puid));
1971 res = MAILIMAP_ERROR_MEMORY;
1976 r = carray_add(tab, puid, NULL);
1979 res = MAILIMAP_ERROR_MEMORY;
1986 return MAILIMAP_NO_ERROR;
1989 imap_fetch_uid_list_free(tab);
1994 static int imap_get_messages_list(mailimap * imap,
1995 uint32_t first_index,
2000 struct mailimap_fetch_att * fetch_att;
2001 struct mailimap_fetch_type * fetch_type;
2002 struct mailimap_set * set;
2003 clist * fetch_result;
2006 set = mailimap_set_new_interval(first_index, 0);
2008 res = MAILIMAP_ERROR_MEMORY;
2012 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2013 if (fetch_type == NULL) {
2014 res = MAILIMAP_ERROR_MEMORY;
2018 fetch_att = mailimap_fetch_att_new_uid();
2019 if (fetch_att == NULL) {
2020 res = MAILIMAP_ERROR_MEMORY;
2021 goto free_fetch_type;
2024 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2025 if (r != MAILIMAP_NO_ERROR) {
2026 mailimap_fetch_att_free(fetch_att);
2027 res = MAILIMAP_ERROR_MEMORY;
2028 goto free_fetch_type;
2031 mailstream_logger = imap_logger_fetch;
2033 r = mailimap_uid_fetch(imap, set,
2034 fetch_type, &fetch_result);
2036 mailstream_logger = imap_logger_cmd;
2037 mailimap_fetch_type_free(fetch_type);
2038 mailimap_set_free(set);
2040 if (r != MAILIMAP_NO_ERROR) {
2046 r = result_to_uid_list(fetch_result, &env_list);
2047 mailimap_fetch_list_free(fetch_result);
2049 * result = env_list;
2051 return MAILIMAP_NO_ERROR;
2054 mailimap_fetch_type_free(fetch_type);
2056 mailimap_set_free(set);
2064 struct fetch_uid_param {
2066 uint32_t first_index;
2069 struct fetch_uid_result {
2071 carray * fetch_result;
2074 static void fetch_uid_run(struct etpan_thread_op * op)
2076 struct fetch_uid_param * param;
2077 struct fetch_uid_result * result;
2078 carray * fetch_result;
2082 result = op->result;
2086 fetch_result = NULL;
2087 mailstream_logger = imap_logger_noop;
2088 log_print(LOG_PROTOCOL, "IMAP- [fetching UIDs...]\n");
2090 r = imap_get_messages_list(param->imap, param->first_index,
2093 mailstream_logger = imap_logger_cmd;
2096 result->fetch_result = fetch_result;
2097 debug_print("imap fetch_uid run - end %i\n", r);
2100 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
2101 carray ** fetch_result)
2103 struct fetch_uid_param param;
2104 struct fetch_uid_result result;
2107 debug_print("imap fetch_uid - begin\n");
2109 imap = get_imap(folder);
2111 param.first_index = first_index;
2113 threaded_run(folder, ¶m, &result, fetch_uid_run);
2115 if (result.error != MAILIMAP_NO_ERROR)
2116 return result.error;
2118 debug_print("imap fetch_uid - end\n");
2120 * fetch_result = result.fetch_result;
2122 return result.error;
2126 void imap_fetch_uid_list_free(carray * uid_list)
2130 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
2133 puid = carray_get(uid_list, i);
2136 carray_free(uid_list);
2142 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
2145 result_to_uid_flags_list(clist * fetch_result, carray ** result)
2147 clistiter * cur = NULL;
2151 GSList *tags = NULL;
2153 tab = carray_new(128);
2155 res = MAILIMAP_ERROR_MEMORY;
2160 cur = clist_begin(fetch_result);
2162 for(; cur != NULL ; cur = clist_next(cur)) {
2163 struct mailimap_msg_att * msg_att;
2166 struct mailimap_msg_att_dynamic * att_dyn;
2172 msg_att = clist_content(cur);
2176 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
2179 if (att_dyn == NULL)
2182 flags = imap_flags_to_flags(att_dyn, &tags);
2184 puid = malloc(sizeof(* puid));
2186 res = MAILIMAP_ERROR_MEMORY;
2191 r = carray_add(tab, puid, NULL);
2194 res = MAILIMAP_ERROR_MEMORY;
2197 pflags = malloc(sizeof(* pflags));
2198 if (pflags == NULL) {
2199 res = MAILIMAP_ERROR_MEMORY;
2203 r = carray_add(tab, pflags, NULL);
2206 res = MAILIMAP_ERROR_MEMORY;
2209 r = carray_add(tab, tags, NULL);
2212 res = MAILIMAP_ERROR_MEMORY;
2219 return MAILIMAP_NO_ERROR;
2222 imap_fetch_uid_flags_list_free(tab);
2223 slist_free_strings_full(tags);
2228 static int imap_get_messages_flags_list(mailimap * imap,
2229 uint32_t first_index,
2234 struct mailimap_fetch_att * fetch_att;
2235 struct mailimap_fetch_type * fetch_type;
2236 struct mailimap_set * set;
2237 clist * fetch_result;
2240 set = mailimap_set_new_interval(first_index, 0);
2242 res = MAILIMAP_ERROR_MEMORY;
2246 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2247 if (fetch_type == NULL) {
2248 res = MAILIMAP_ERROR_MEMORY;
2252 fetch_att = mailimap_fetch_att_new_flags();
2253 if (fetch_att == NULL) {
2254 res = MAILIMAP_ERROR_MEMORY;
2255 goto free_fetch_type;
2258 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2259 if (r != MAILIMAP_NO_ERROR) {
2260 mailimap_fetch_att_free(fetch_att);
2261 res = MAILIMAP_ERROR_MEMORY;
2262 goto free_fetch_type;
2265 fetch_att = mailimap_fetch_att_new_uid();
2266 if (fetch_att == NULL) {
2267 res = MAILIMAP_ERROR_MEMORY;
2268 goto free_fetch_type;
2271 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2272 if (r != MAILIMAP_NO_ERROR) {
2273 mailimap_fetch_att_free(fetch_att);
2274 res = MAILIMAP_ERROR_MEMORY;
2275 goto free_fetch_type;
2278 mailstream_logger = imap_logger_fetch;
2280 r = mailimap_uid_fetch(imap, set,
2281 fetch_type, &fetch_result);
2283 mailstream_logger = imap_logger_cmd;
2284 mailimap_fetch_type_free(fetch_type);
2285 mailimap_set_free(set);
2287 if (r != MAILIMAP_NO_ERROR) {
2293 r = result_to_uid_flags_list(fetch_result, &env_list);
2294 mailimap_fetch_list_free(fetch_result);
2296 * result = env_list;
2298 return MAILIMAP_NO_ERROR;
2301 mailimap_fetch_type_free(fetch_type);
2303 mailimap_set_free(set);
2310 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2312 struct fetch_uid_param * param;
2313 struct fetch_uid_result * result;
2314 carray * fetch_result;
2318 result = op->result;
2322 fetch_result = NULL;
2323 r = imap_get_messages_flags_list(param->imap, param->first_index,
2327 result->fetch_result = fetch_result;
2328 debug_print("imap fetch_uid run - end %i\n", r);
2331 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2332 carray ** fetch_result)
2334 struct fetch_uid_param param;
2335 struct fetch_uid_result result;
2338 debug_print("imap fetch_uid - begin\n");
2340 imap = get_imap(folder);
2342 param.first_index = first_index;
2344 mailstream_logger = imap_logger_noop;
2345 log_print(LOG_PROTOCOL, "IMAP- [fetching flags...]\n");
2347 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2349 mailstream_logger = imap_logger_cmd;
2352 if (result.error != MAILIMAP_NO_ERROR)
2353 return result.error;
2355 debug_print("imap fetch_uid - end\n");
2357 * fetch_result = result.fetch_result;
2359 return result.error;
2363 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2367 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2370 data = carray_get(uid_flags_list, i);
2372 data = carray_get(uid_flags_list, i + 1);
2375 carray_free(uid_flags_list);
2380 static int imap_fetch(mailimap * imap,
2383 size_t * result_len)
2386 struct mailimap_set * set;
2387 struct mailimap_fetch_att * fetch_att;
2388 struct mailimap_fetch_type * fetch_type;
2389 clist * fetch_result;
2390 struct mailimap_msg_att * msg_att;
2391 struct mailimap_msg_att_item * msg_att_item;
2396 struct mailimap_section * section;
2398 set = mailimap_set_new_single(msg_index);
2400 res = MAILIMAP_ERROR_MEMORY;
2404 section = mailimap_section_new(NULL);
2405 if (section == NULL) {
2406 res = MAILIMAP_ERROR_MEMORY;
2410 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2411 if (fetch_att == NULL) {
2412 mailimap_section_free(section);
2413 res = MAILIMAP_ERROR_MEMORY;
2417 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2418 if (fetch_type == NULL) {
2419 res = MAILIMAP_ERROR_MEMORY;
2420 goto free_fetch_att;
2423 mailstream_logger = imap_logger_fetch;
2425 r = mailimap_uid_fetch(imap, set,
2426 fetch_type, &fetch_result);
2428 mailstream_logger = imap_logger_cmd;
2430 mailimap_fetch_type_free(fetch_type);
2431 mailimap_set_free(set);
2434 case MAILIMAP_NO_ERROR:
2440 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2441 mailimap_fetch_list_free(fetch_result);
2442 return MAILIMAP_ERROR_FETCH;
2445 msg_att = clist_begin(fetch_result)->data;
2450 if (msg_att->att_list)
2451 cur = clist_begin(msg_att->att_list);
2455 for(; cur != NULL ; cur = clist_next(cur)) {
2456 msg_att_item = clist_content(cur);
2458 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2459 if (msg_att_item->att_data.att_static->att_type ==
2460 MAILIMAP_MSG_ATT_BODY_SECTION) {
2461 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2463 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2465 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2470 mailimap_fetch_list_free(fetch_result);
2473 return MAILIMAP_ERROR_FETCH;
2476 * result_len = text_length;
2478 return MAILIMAP_NO_ERROR;
2481 mailimap_fetch_att_free(fetch_att);
2483 mailimap_set_free(set);
2488 static int imap_fetch_header(mailimap * imap,
2491 size_t * result_len)
2494 struct mailimap_set * set;
2495 struct mailimap_fetch_att * fetch_att;
2496 struct mailimap_fetch_type * fetch_type;
2497 clist * fetch_result;
2498 struct mailimap_msg_att * msg_att;
2499 struct mailimap_msg_att_item * msg_att_item;
2504 struct mailimap_section * section;
2506 set = mailimap_set_new_single(msg_index);
2508 res = MAILIMAP_ERROR_MEMORY;
2512 section = mailimap_section_new_header();
2513 if (section == NULL) {
2514 res = MAILIMAP_ERROR_MEMORY;
2518 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2519 if (fetch_att == NULL) {
2520 mailimap_section_free(section);
2521 res = MAILIMAP_ERROR_MEMORY;
2525 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2526 if (fetch_type == NULL) {
2527 res = MAILIMAP_ERROR_MEMORY;
2528 goto free_fetch_att;
2531 mailstream_logger = imap_logger_fetch;
2533 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2535 mailstream_logger = imap_logger_cmd;
2536 mailimap_fetch_type_free(fetch_type);
2537 mailimap_set_free(set);
2540 case MAILIMAP_NO_ERROR:
2546 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2547 mailimap_fetch_list_free(fetch_result);
2548 return MAILIMAP_ERROR_FETCH;
2551 msg_att = clist_begin(fetch_result)->data;
2556 if (msg_att->att_list)
2557 cur = clist_begin(msg_att->att_list);
2561 for(; cur != NULL ; cur = clist_next(cur)) {
2562 msg_att_item = clist_content(cur);
2564 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2565 if (msg_att_item->att_data.att_static->att_type ==
2566 MAILIMAP_MSG_ATT_BODY_SECTION) {
2567 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2568 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2570 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2575 mailimap_fetch_list_free(fetch_result);
2578 return MAILIMAP_ERROR_FETCH;
2581 * result_len = text_length;
2583 return MAILIMAP_NO_ERROR;
2586 mailimap_fetch_att_free(fetch_att);
2588 mailimap_set_free(set);
2595 struct fetch_content_param {
2598 const char * filename;
2602 struct fetch_content_result {
2606 static void fetch_content_run(struct etpan_thread_op * op)
2608 struct fetch_content_param * param;
2609 struct fetch_content_result * result;
2611 size_t content_size;
2617 result = op->result;
2623 if (param->with_body)
2624 r = imap_fetch(param->imap, param->msg_index,
2625 &content, &content_size);
2627 r = imap_fetch_header(param->imap, param->msg_index,
2628 &content, &content_size);
2632 if (r == MAILIMAP_NO_ERROR) {
2633 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2635 result->error = MAILIMAP_ERROR_FETCH;
2639 f = claws_fdopen(fd, "wb");
2641 result->error = MAILIMAP_ERROR_FETCH;
2645 r = claws_fwrite(content, 1, content_size, f);
2646 if (r < content_size) {
2647 result->error = MAILIMAP_ERROR_FETCH;
2651 r = claws_safe_fclose(f);
2653 result->error = MAILIMAP_ERROR_FETCH;
2664 claws_unlink(param->filename);
2667 /* mmap_string_unref is a simple free in libetpan
2668 * when it has MMAP_UNAVAILABLE defined */
2669 if (mmap_string_unref(content) != 0)
2673 debug_print("imap fetch_content run - end %i\n", result->error);
2676 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2678 const char * filename)
2680 struct fetch_content_param param;
2681 struct fetch_content_result result;
2684 debug_print("imap fetch_content - begin\n");
2686 imap = get_imap(folder);
2688 param.msg_index = msg_index;
2689 param.filename = filename;
2690 param.with_body = with_body;
2692 threaded_run(folder, ¶m, &result, fetch_content_run);
2694 if (result.error != MAILIMAP_NO_ERROR)
2695 return result.error;
2697 debug_print("imap fetch_content - end\n");
2699 return result.error;
2704 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2709 GSList *tags = NULL;
2713 flag_list = att_dyn->att_list;
2714 if (flag_list == NULL)
2717 for(cur = clist_begin(flag_list) ; cur != NULL ;
2718 cur = clist_next(cur)) {
2719 struct mailimap_flag_fetch * flag_fetch;
2721 flag_fetch = clist_content(cur);
2722 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2725 switch (flag_fetch->fl_flag->fl_type) {
2726 case MAILIMAP_FLAG_ANSWERED:
2727 flags |= MSG_REPLIED;
2729 case MAILIMAP_FLAG_FLAGGED:
2730 flags |= MSG_MARKED;
2732 case MAILIMAP_FLAG_DELETED:
2733 flags |= MSG_DELETED;
2735 case MAILIMAP_FLAG_SEEN:
2736 flags &= ~MSG_UNREAD;
2739 case MAILIMAP_FLAG_KEYWORD:
2740 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2741 flags |= MSG_FORWARDED;
2742 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2744 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2745 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2746 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2749 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2759 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2763 struct mailimap_msg_att_dynamic ** patt_dyn)
2765 clistiter * item_cur;
2769 struct mailimap_msg_att_dynamic * att_dyn;
2776 if (msg_att->att_list)
2777 item_cur = clist_begin(msg_att->att_list);
2780 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2781 struct mailimap_msg_att_item * item;
2783 item = clist_content(item_cur);
2785 switch (item->att_type) {
2786 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2787 switch (item->att_data.att_static->att_type) {
2788 case MAILIMAP_MSG_ATT_UID:
2789 uid = item->att_data.att_static->att_data.att_uid;
2792 case MAILIMAP_MSG_ATT_BODY_SECTION:
2793 if (headers == NULL) {
2794 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2797 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2798 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2803 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2804 if (att_dyn == NULL) {
2805 att_dyn = item->att_data.att_dyn;
2813 if (pheaders != NULL)
2814 * pheaders = headers;
2815 if (pref_size != NULL)
2816 * pref_size = ref_size;
2817 if (patt_dyn != NULL)
2818 * patt_dyn = att_dyn;
2820 return MAIL_NO_ERROR;
2823 static struct imap_fetch_env_info *
2824 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2826 struct imap_fetch_env_info * info;
2830 struct mailimap_msg_att_dynamic * att_dyn;
2832 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2837 info = malloc(sizeof(* info));
2839 info->headers = strdup(headers);
2841 info->flags = imap_flags_to_flags(att_dyn, tags);
2847 imap_fetch_result_to_envelop_list(clist * fetch_result,
2848 carray ** p_env_list)
2854 env_list = carray_new(16);
2856 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2857 cur = clist_next(cur)) {
2858 struct mailimap_msg_att * msg_att;
2859 struct imap_fetch_env_info * env_info;
2860 GSList *tags = NULL;
2862 msg_att = clist_content(cur);
2864 env_info = fetch_to_env_info(msg_att, &tags);
2866 || carray_add(env_list, env_info, NULL) != 0
2867 || carray_add(env_list, tags, NULL) != 0) {
2868 carray_free(env_list);
2869 return MAILIMAP_ERROR_MEMORY;
2872 * p_env_list = env_list;
2874 * p_env_list = NULL;
2877 return MAIL_NO_ERROR;
2880 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2882 struct mailimap_fetch_att * fetch_att;
2886 struct mailimap_header_list * imap_hdrlist;
2887 struct mailimap_section * section;
2889 "Date", "From", "To", "Cc", "Subject", "Message-ID",
2890 "References", "In-Reply-To", NULL
2893 hdrlist = clist_new();
2895 return MAIL_ERROR_MEMORY;
2897 while (headers[i] != NULL) {
2898 header = strdup(headers[i]);
2899 if (header == NULL || clist_append(hdrlist, header) != 0) {
2900 clist_free(hdrlist);
2901 return MAIL_ERROR_MEMORY;
2906 imap_hdrlist = mailimap_header_list_new(hdrlist);
2907 section = mailimap_section_new_header_fields(imap_hdrlist);
2908 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2909 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2911 return MAIL_NO_ERROR;
2914 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2916 struct mailimap_fetch_att * fetch_att;
2917 struct mailimap_section * section;
2919 section = mailimap_section_new_header();
2920 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2921 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2923 return MAIL_NO_ERROR;
2927 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2928 carray ** p_env_list)
2930 struct mailimap_fetch_att * fetch_att;
2931 struct mailimap_fetch_type * fetch_type;
2933 clist * fetch_result;
2935 carray * env_list = NULL;
2939 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2942 fetch_att = mailimap_fetch_att_new_uid();
2943 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2946 fetch_att = mailimap_fetch_att_new_flags();
2947 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2950 fetch_att = mailimap_fetch_att_new_rfc822_size();
2951 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2955 key.len = sizeof(imap);
2956 r = chash_get(courier_workaround_hash, &key, &value);
2958 r = imap_add_envelope_fetch_att(fetch_type);
2960 r = imap_add_header_fetch_att(fetch_type);
2962 if (r != MAILIMAP_NO_ERROR) {
2963 debug_print("add fetch attr: %d\n", r);
2967 mailstream_logger = imap_logger_fetch;
2969 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2971 mailstream_logger = imap_logger_cmd;
2973 case MAILIMAP_NO_ERROR:
2976 mailimap_fetch_type_free(fetch_type);
2977 debug_print("uid_fetch: %d\n", r);
2981 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2982 res = MAILIMAP_ERROR_FETCH;
2983 debug_print("clist_begin = NULL\n");
2987 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2988 mailimap_fetch_list_free(fetch_result);
2990 if (r != MAILIMAP_NO_ERROR) {
2991 mailimap_fetch_type_free(fetch_type);
2992 res = MAILIMAP_ERROR_MEMORY;
2993 debug_print("fetch_result_to_envelop_list: %d\n", res);
2997 mailimap_fetch_type_free(fetch_type);
2999 * p_env_list = env_list;
3001 return MAILIMAP_NO_ERROR;
3007 struct fetch_env_param {
3009 struct mailimap_set * set;
3012 struct fetch_env_result {
3013 carray * fetch_env_result;
3017 static void fetch_env_run(struct etpan_thread_op * op)
3019 struct fetch_env_param * param;
3020 struct fetch_env_result * result;
3025 result = op->result;
3030 r = imap_get_envelopes_list(param->imap, param->set,
3034 result->fetch_env_result = env_list;
3036 debug_print("imap fetch_env run - end %i\n", r);
3039 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
3040 carray ** p_env_list)
3042 struct fetch_env_param param;
3043 struct fetch_env_result result;
3046 debug_print("imap fetch_env - begin\n");
3048 imap = get_imap(folder);
3052 if (threaded_run(folder, ¶m, &result, fetch_env_run))
3053 return MAILIMAP_ERROR_INVAL;
3055 if (result.error != MAILIMAP_NO_ERROR) {
3061 key.len = sizeof(imap);
3062 r = chash_get(courier_workaround_hash, &key, &value);
3066 chash_set(courier_workaround_hash, &key, &value, NULL);
3068 threaded_run(folder, ¶m, &result, fetch_env_run);
3072 if (result.error != MAILIMAP_NO_ERROR)
3073 return result.error;
3075 debug_print("imap fetch_env - end\n");
3077 * p_env_list = result.fetch_env_result;
3079 return result.error;
3082 void imap_fetch_env_free(carray * env_list)
3086 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
3087 struct imap_fetch_env_info * env_info;
3089 env_info = carray_get(env_list, i);
3090 free(env_info->headers);
3093 carray_free(env_list);
3100 struct append_param {
3102 const char * mailbox;
3103 const char * filename;
3104 struct mailimap_flag_list * flag_list;
3107 struct append_result {
3112 static void append_run(struct etpan_thread_op * op)
3114 struct append_param * param;
3115 struct append_result * result;
3120 struct stat stat_buf;
3123 guint32 uid = 0, val = 0;
3126 result = op->result;
3131 r = stat(param->filename, &stat_buf);
3133 result->error = MAILIMAP_ERROR_APPEND;
3136 size = stat_buf.st_size;
3138 fd = g_open(param->filename, O_RDONLY, 0);
3140 result->error = MAILIMAP_ERROR_APPEND;
3144 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
3145 if (data == (void *) MAP_FAILED) {
3147 result->error = MAILIMAP_ERROR_APPEND;
3151 data = file_read_to_str_no_recode(param->filename);
3153 result->error = MAILIMAP_ERROR_APPEND;
3156 size = strlen(data);
3158 mailstream_logger = imap_logger_append;
3160 r = mailimap_uidplus_append(param->imap, param->mailbox,
3161 param->flag_list, NULL,
3162 data, size, &val, &uid);
3164 mailstream_logger = imap_logger_cmd;
3175 debug_print("imap append run - end %i uid %d\n", r, uid);
3178 int imap_threaded_append(Folder * folder, const char * mailbox,
3179 const char * filename,
3180 struct mailimap_flag_list * flag_list,
3183 struct append_param param;
3184 struct append_result result;
3187 debug_print("imap append - begin\n");
3189 imap = get_imap(folder);
3191 param.mailbox = mailbox;
3192 param.filename = filename;
3193 param.flag_list = flag_list;
3195 threaded_run(folder, ¶m, &result, append_run);
3197 if (result.error != MAILIMAP_NO_ERROR)
3198 return result.error;
3200 debug_print("imap append - end\n");
3204 return result.error;
3210 struct expunge_param {
3214 struct expunge_result {
3218 static void expunge_run(struct etpan_thread_op * op)
3220 struct expunge_param * param;
3221 struct expunge_result * result;
3225 result = op->result;
3229 r = mailimap_expunge(param->imap);
3232 debug_print("imap expunge run - end %i\n", r);
3235 int imap_threaded_expunge(Folder * folder)
3237 struct expunge_param param;
3238 struct expunge_result result;
3240 debug_print("imap expunge - begin\n");
3242 param.imap = get_imap(folder);
3244 threaded_run(folder, ¶m, &result, expunge_run);
3246 debug_print("imap expunge - end\n");
3248 return result.error;
3254 struct mailimap_set * set;
3258 struct copy_result {
3260 struct mailimap_set *source;
3261 struct mailimap_set *dest;
3264 static void copy_run(struct etpan_thread_op * op)
3266 struct copy_param * param;
3267 struct copy_result * result;
3270 struct mailimap_set *source = NULL, *dest = NULL;
3273 result = op->result;
3277 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3278 &val, &source, &dest);
3282 result->source = source;
3283 result->dest = dest;
3285 result->source = NULL;
3286 result->dest = NULL;
3288 debug_print("imap copy run - end %i\n", r);
3291 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3292 const char * mb, struct mailimap_set **source,
3293 struct mailimap_set **dest)
3295 struct copy_param param;
3296 struct copy_result result;
3299 debug_print("imap copy - begin\n");
3301 imap = get_imap(folder);
3306 threaded_run(folder, ¶m, &result, copy_run);
3310 if (result.error != MAILIMAP_NO_ERROR)
3311 return result.error;
3313 *source = result.source;
3314 *dest = result.dest;
3316 debug_print("imap copy - end\n");
3318 return result.error;
3323 struct store_param {
3325 struct mailimap_set * set;
3326 struct mailimap_store_att_flags * store_att_flags;
3329 struct store_result {
3333 static void store_run(struct etpan_thread_op * op)
3335 struct store_param * param;
3336 struct store_result * result;
3340 result = op->result;
3344 r = mailimap_uid_store(param->imap, param->set,
3345 param->store_att_flags);
3349 debug_print("imap store run - end %i\n", r);
3352 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3353 struct mailimap_store_att_flags * store_att_flags)
3355 struct store_param param;
3356 struct store_result result;
3359 debug_print("imap store - begin\n");
3361 imap = get_imap(folder);
3364 param.store_att_flags = store_att_flags;
3366 threaded_run(folder, ¶m, &result, store_run);
3368 if (result.error != MAILIMAP_NO_ERROR)
3369 return result.error;
3371 debug_print("imap store - end\n");
3373 return result.error;
3377 #define ENV_BUFFER_SIZE 512
3379 static void do_exec_command(int fd, const char * command,
3380 const char * servername, uint16_t port)
3384 char env_buffer[ENV_BUFFER_SIZE];
3388 /* Fork again to become a child of init rather than
3389 the etpan client. */
3394 g_setenv("ETPANSERVER", servername, TRUE);
3396 g_unsetenv("ETPANSERVER");
3401 snprintf(porttext, sizeof(porttext), "%d", port);
3402 g_setenv("ETPANPORT", porttext, TRUE);
3405 g_unsetenv("ETPANPORT");
3408 /* Not a lot we can do if there's an error other than bail. */
3409 if (dup2(fd, 0) == -1)
3411 if (dup2(fd, 1) == -1)
3414 /* Should we close stderr and reopen /dev/null? */
3416 maxopen = sysconf(_SC_OPEN_MAX);
3417 for (i=3; i < maxopen; i++)
3421 /* Detach from the controlling tty if we have one. Otherwise,
3422 SSH might do something stupid like trying to use it instead
3423 of running $SSH_ASKPASS. Doh. */
3424 fd = g_open("/dev/tty", O_RDONLY, 0);
3426 ioctl(fd, TIOCNOTTY, NULL);
3429 #endif /* TIOCNOTTY */
3431 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3433 /* Eep. Shouldn't reach this */
3437 static int subcommand_connect(const char *command,
3438 const char *servername, uint16_t port)
3440 /* SEB unsupported on Windows */
3444 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3449 do_exec_command(sockfds[1], command, servername, port);
3451 else if (childpid == -1) {
3459 /* Reap child, leaving grandchild process to run */
3460 waitpid(childpid, NULL, 0);
3465 static int socket_connect_cmd(mailimap * imap, const char * command,
3466 const char * server, int port)
3472 fd = subcommand_connect(command, server, port);
3474 return MAILIMAP_ERROR_STREAM;
3476 s = mailstream_socket_open(fd);
3479 return MAILIMAP_ERROR_STREAM;
3482 r = mailimap_connect(imap, s);
3483 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3484 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3485 mailstream_close(s);
3487 imap->imap_stream = NULL;
3496 struct connect_cmd_param {
3498 const char * command;
3499 const char * server;
3503 struct connect_cmd_result {
3507 static void connect_cmd_run(struct etpan_thread_op * op)
3510 struct connect_cmd_param * param;
3511 struct connect_cmd_result * result;
3514 result = op->result;
3518 r = socket_connect_cmd(param->imap, param->command,
3519 param->server, param->port);
3525 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3526 const char * server, int port)
3528 struct connect_cmd_param param;
3529 struct connect_cmd_result result;
3532 mailimap * imap, * oldimap;
3534 oldimap = get_imap(folder);
3536 imap = mailimap_new(0, NULL);
3539 debug_print("deleting old imap %p\n", oldimap);
3540 delete_imap(folder, oldimap);
3544 key.len = sizeof(folder);
3547 chash_set(session_hash, &key, &value, NULL);
3550 param.command = command;
3551 param.server = server;
3554 threaded_run(folder, ¶m, &result, connect_cmd_run);
3556 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3558 return result.error;
3560 #endif /* G_OS_WIN32 */
3562 void imap_threaded_cancel(Folder * folder)
3566 imap = get_imap(folder);
3567 if (imap && imap->imap_stream != NULL)
3568 mailstream_cancel(imap->imap_stream);
3573 void imap_main_init(void)
3576 void imap_main_done(gboolean have_connectivity)
3579 void imap_main_set_timeout(int sec)
3583 void imap_threaded_cancel(Folder * folder);