2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2007 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/>.
26 #include "imap-thread.h"
28 #include <sys/types.h>
30 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
31 #include <sys/socket.h>
40 #include "etpan-thread-manager.h"
42 #include "mainwindow.h"
43 #include "ssl_certificate.h"
45 #include "remotefolder.h"
47 #define DISABLE_LOG_DURING_LOGIN
49 static struct etpan_thread_manager * thread_manager = NULL;
50 static chash * courier_workaround_hash = NULL;
51 static chash * imap_hash = NULL;
52 static chash * session_hash = NULL;
53 static guint thread_manager_signal = 0;
54 static GIOChannel * io_channel = NULL;
56 static void delete_imap(Folder *folder, mailimap *imap)
62 key.len = sizeof(folder);
65 chash_delete(session_hash, &key, NULL);
68 key.len = sizeof(imap);
69 chash_delete(courier_workaround_hash, &key, NULL);
70 if (imap && imap->imap_stream) {
71 /* we don't want libetpan to logout */
72 mailstream_close(imap->imap_stream);
73 imap->imap_stream = NULL;
75 debug_print("removing mailimap %p\n", imap);
79 static gboolean thread_manager_event(GIOChannel * source,
80 GIOCondition condition,
83 etpan_thread_manager_loop(thread_manager);
88 static void imap_logger_noop(int direction, const char * str, size_t size)
93 static void imap_logger_cmd(int direction, const char * str, size_t size)
100 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
103 buf = malloc(size+1);
104 memset(buf, 0, size+1);
105 strncpy(buf, str, size);
108 if (!strncmp(buf, "<<<<<<<", 7)
109 || !strncmp(buf, ">>>>>>>", 7)) {
113 while (strstr(buf, "\r"))
114 *strstr(buf, "\r") = ' ';
115 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
116 buf[strlen(buf)-1] = '\0';
118 lines = g_strsplit(buf, "\n", -1);
120 while (lines[i] && *lines[i]) {
121 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
128 static void imap_logger_fetch(int direction, const char * str, size_t size)
134 if (size > 128 && !direction) {
135 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
139 buf = malloc(size+1);
140 memset(buf, 0, size+1);
141 strncpy(buf, str, size);
143 if (!strncmp(buf, "<<<<<<<", 7)
144 || !strncmp(buf, ">>>>>>>", 7)) {
148 while (strstr(buf, "\r"))
149 *strstr(buf, "\r") = ' ';
150 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
151 buf[strlen(buf)-1] = '\0';
153 lines = g_strsplit(buf, "\n", -1);
155 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
156 while (lines[i] && *lines[i]) {
157 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
161 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
167 static void imap_logger_uid(int direction, const char * str, size_t size)
174 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
177 buf = malloc(size+1);
178 memset(buf, 0, size+1);
179 strncpy(buf, str, size);
181 if (!strncmp(buf, "<<<<<<<", 7)
182 || !strncmp(buf, ">>>>>>>", 7)) {
186 while (strstr(buf, "\r"))
187 *strstr(buf, "\r") = ' ';
188 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
189 buf[strlen(buf)-1] = '\0';
191 lines = g_strsplit(buf, "\n", -1);
193 while (lines[i] && *lines[i]) {
194 int llen = strlen(lines[i]);
196 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
199 strncpy2(tmp, lines[i], 63);
200 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
209 static void imap_logger_append(int direction, const char * str, size_t size)
216 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
218 } else if (direction == 0 && size > 64) {
219 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
222 buf = malloc(size+1);
223 memset(buf, 0, size+1);
224 strncpy(buf, str, size);
226 if (!strncmp(buf, "<<<<<<<", 7)
227 || !strncmp(buf, ">>>>>>>", 7)) {
231 while (strstr(buf, "\r"))
232 *strstr(buf, "\r") = ' ';
233 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
234 buf[strlen(buf)-1] = '\0';
236 lines = g_strsplit(buf, "\n", -1);
238 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
239 while (lines[i] && *lines[i]) {
240 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
244 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
250 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
251 gboolean etpan_skip_ssl_cert_check = FALSE;
252 extern void mailsasl_ref(void);
254 void imap_main_init(gboolean skip_ssl_cert_check)
256 int fd_thread_manager;
258 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
259 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
260 mailstream_network_delay.tv_usec = 0;
262 mailstream_debug = 1;
263 mailstream_logger = imap_logger_cmd;
266 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
267 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
268 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
270 thread_manager = etpan_thread_manager_new();
272 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
274 io_channel = g_io_channel_unix_new(fd_thread_manager);
276 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
277 thread_manager_event,
282 void imap_main_set_timeout(int sec)
284 mailstream_network_delay.tv_sec = sec;
285 mailstream_network_delay.tv_usec = 0;
288 void imap_main_done(void)
290 etpan_thread_manager_stop(thread_manager);
291 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
294 etpan_thread_manager_join(thread_manager);
296 g_source_remove(thread_manager_signal);
297 g_io_channel_unref(io_channel);
299 etpan_thread_manager_free(thread_manager);
301 chash_free(courier_workaround_hash);
302 chash_free(session_hash);
303 chash_free(imap_hash);
306 void imap_init(Folder * folder)
308 struct etpan_thread * thread;
312 thread = etpan_thread_manager_get_thread(thread_manager);
315 key.len = sizeof(folder);
319 chash_set(imap_hash, &key, &value, NULL);
322 void imap_done(Folder * folder)
324 struct etpan_thread * thread;
330 key.len = sizeof(folder);
332 r = chash_get(imap_hash, &key, &value);
338 etpan_thread_unbind(thread);
340 chash_delete(imap_hash, &key, NULL);
342 debug_print("remove thread");
345 static struct etpan_thread * get_thread(Folder * folder)
347 struct etpan_thread * thread;
352 key.len = sizeof(folder);
354 chash_get(imap_hash, &key, &value);
360 static mailimap * get_imap(Folder * folder)
368 key.len = sizeof(folder);
370 r = chash_get(session_hash, &key, &value);
375 debug_print("found imap %p\n", imap);
379 static gboolean cb_show_error(gpointer data)
381 mainwindow_show_error();
385 static void generic_cb(int cancelled, void * result, void * callback_data)
387 struct etpan_thread_op * op;
389 op = (struct etpan_thread_op *) callback_data;
391 debug_print("generic_cb\n");
392 if (op->imap && op->imap->imap_response_info &&
393 op->imap->imap_response_info->rsp_alert) {
394 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
395 op->imap->imap_response_info->rsp_alert);
396 g_timeout_add(10, cb_show_error, NULL);
401 static void threaded_run(Folder * folder, void * param, void * result,
402 void (* func)(struct etpan_thread_op * ))
404 struct etpan_thread_op * op;
405 struct etpan_thread * thread;
407 imap_folder_ref(folder);
409 op = etpan_thread_op_new();
411 op->imap = get_imap(folder);
417 op->callback = generic_cb;
418 op->callback_data = op;
423 thread = get_thread(folder);
424 etpan_thread_op_schedule(thread, op);
426 while (!op->finished) {
427 gtk_main_iteration();
430 etpan_thread_op_free(op);
432 imap_folder_unref(folder);
438 struct connect_param {
444 struct connect_result {
448 #define CHECK_IMAP() { \
449 if (!param->imap) { \
450 result->error = MAILIMAP_ERROR_BAD_STATE; \
455 static void connect_run(struct etpan_thread_op * op)
458 struct connect_param * param;
459 struct connect_result * result;
466 r = mailimap_socket_connect(param->imap,
467 param->server, param->port);
473 int imap_threaded_connect(Folder * folder, const char * server, int port)
475 struct connect_param param;
476 struct connect_result result;
479 mailimap * imap, * oldimap;
481 oldimap = get_imap(folder);
483 imap = mailimap_new(0, NULL);
486 debug_print("deleting old imap %p\n", oldimap);
487 delete_imap(folder, oldimap);
491 key.len = sizeof(folder);
494 chash_set(session_hash, &key, &value, NULL);
497 param.server = server;
501 threaded_run(folder, ¶m, &result, connect_run);
503 debug_print("connect ok %i with imap %p\n", result.error, imap);
508 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
511 struct connect_param *param = (struct connect_param *)data;
514 if (certificate == NULL || len < 0) {
515 g_warning("no cert presented.\n");
518 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
520 g_warning("IMAP: can't get cert\n");
522 } else if (ssl_certificate_check(cert, NULL,
523 (gchar *)param->server, (gushort)param->port) == TRUE) {
531 struct connect_param *param = (struct connect_param *)data;
532 gnutls_x509_crt cert = NULL;
535 if (certificate == NULL || len < 0) {
536 g_warning("no cert presented.\n");
540 tmp.data = malloc(len);
541 memcpy(tmp.data, certificate, len);
543 gnutls_x509_crt_init(&cert);
544 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
545 g_warning("IMAP: can't get cert\n");
547 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
548 (gchar *)param->server, (gushort)param->port) == TRUE) {
549 gnutls_x509_crt_deinit(cert);
552 gnutls_x509_crt_deinit(cert);
559 static void connect_ssl_run(struct etpan_thread_op * op)
562 struct connect_param * param;
563 struct connect_result * result;
570 r = mailimap_ssl_connect(param->imap,
571 param->server, param->port);
575 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
577 struct connect_param param;
578 struct connect_result result;
581 mailimap * imap, * oldimap;
582 unsigned char *certificate = NULL;
585 oldimap = get_imap(folder);
587 imap = mailimap_new(0, NULL);
590 debug_print("deleting old imap %p\n", oldimap);
591 delete_imap(folder, oldimap);
595 key.len = sizeof(folder);
598 chash_set(session_hash, &key, &value, NULL);
601 param.server = server;
605 threaded_run(folder, ¶m, &result, connect_ssl_run);
607 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
608 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
609 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
610 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
615 debug_print("connect %d with imap %p\n", result.error, imap);
626 struct mailimap_capability_data *caps;
629 static void capability_run(struct etpan_thread_op * op)
632 struct capa_param * param;
633 struct capa_result * result;
634 struct mailimap_capability_data *caps;
641 r = mailimap_capability(param->imap, &caps);
644 result->caps = (r == 0 ? caps : NULL);
648 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
650 struct capa_param param;
651 struct capa_result result;
654 imap = get_imap(folder);
658 threaded_run(folder, ¶m, &result, capability_run);
660 debug_print("capa %d\n", result.error);
669 struct disconnect_param {
673 struct disconnect_result {
677 static void disconnect_run(struct etpan_thread_op * op)
680 struct disconnect_param * param;
681 struct disconnect_result * result;
688 r = mailimap_logout(param->imap);
693 void imap_threaded_disconnect(Folder * folder)
695 struct connect_param param;
696 struct connect_result result;
699 imap = get_imap(folder);
701 debug_print("was disconnected\n");
707 threaded_run(folder, ¶m, &result, disconnect_run);
709 if (imap == get_imap(folder)) {
710 debug_print("deleting old imap %p\n", imap);
711 delete_imap(folder, imap);
713 debug_print("imap already deleted %p\n", imap);
716 debug_print("disconnect ok\n");
723 const char * wildcard;
732 static void list_run(struct etpan_thread_op * op)
734 struct list_param * param;
735 struct list_result * result;
746 if (param->base == NULL || param->wildcard == NULL) {
749 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
753 r = mailimap_lsub(param->imap, param->base,
754 param->wildcard, &list);
756 r = mailimap_list(param->imap, param->base,
757 param->wildcard, &list);
760 debug_print("imap list run - end\n");
763 int imap_threaded_list(Folder * folder, const char * base,
764 const char * wildcard,
767 struct list_param param;
768 struct list_result result;
770 debug_print("imap list - begin\n");
772 param.imap = get_imap(folder);
774 param.wildcard = wildcard;
775 param.sub_only = FALSE;
777 threaded_run(folder, ¶m, &result, list_run);
779 * p_result = result.list;
781 debug_print("imap list - end %p\n", result.list);
786 int imap_threaded_lsub(Folder * folder, const char * base,
787 const char * wildcard,
790 struct list_param param;
791 struct list_result result;
793 debug_print("imap lsub - begin\n");
795 param.imap = get_imap(folder);
797 param.wildcard = wildcard;
798 param.sub_only = TRUE;
800 threaded_run(folder, ¶m, &result, list_run);
802 * p_result = result.list;
804 debug_print("imap lsub - end %p\n", result.list);
809 struct subscribe_param {
815 struct subscribe_result {
819 static void subscribe_run(struct etpan_thread_op * op)
821 struct subscribe_param * param;
822 struct subscribe_result * result;
830 if (param->mb == NULL) {
832 debug_print("no mb\n");
835 if (param->subscribe)
836 r = mailimap_subscribe(param->imap, param->mb);
838 r = mailimap_unsubscribe(param->imap, param->mb);
840 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
843 int imap_threaded_subscribe(Folder * folder, const char * mb,
846 struct subscribe_param param;
847 struct subscribe_result result;
849 debug_print("imap list - begin\n");
851 param.imap = get_imap(folder);
853 param.subscribe = subscribe;
855 threaded_run(folder, ¶m, &result, subscribe_run);
863 const char * password;
868 struct login_result {
872 static void login_run(struct etpan_thread_op * op)
874 struct login_param * param;
875 struct login_result * result;
877 #ifdef DISABLE_LOG_DURING_LOGIN
886 #ifdef DISABLE_LOG_DURING_LOGIN
887 old_debug = mailstream_debug;
888 mailstream_debug = 0;
890 if (!strcmp(param->type, "LOGIN"))
891 r = mailimap_login(param->imap,
892 param->login, param->password);
893 else if (!strcmp(param->type, "GSSAPI"))
894 r = mailimap_authenticate(param->imap,
895 param->type, param->server, NULL, NULL,
896 param->login, param->login,
897 param->password, NULL);
899 r = mailimap_authenticate(param->imap,
900 param->type, NULL, NULL, NULL,
901 param->login, param->login,
902 param->password, NULL);
903 #ifdef DISABLE_LOG_DURING_LOGIN
904 mailstream_debug = old_debug;
908 if (param->imap->imap_response)
909 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
910 debug_print("imap login run - end %i\n", r);
913 int imap_threaded_login(Folder * folder,
914 const char * login, const char * password,
917 struct login_param param;
918 struct login_result result;
920 debug_print("imap login - begin\n");
922 param.imap = get_imap(folder);
924 param.password = password;
926 if (folder && folder->account)
927 param.server = folder->account->recv_server;
931 threaded_run(folder, ¶m, &result, login_run);
933 debug_print("imap login - end\n");
939 struct status_param {
942 struct mailimap_status_att_list * status_att_list;
945 struct status_result {
947 struct mailimap_mailbox_data_status * data_status;
950 static void status_run(struct etpan_thread_op * op)
952 struct status_param * param;
953 struct status_result * result;
961 r = mailimap_status(param->imap, param->mb,
962 param->status_att_list,
963 &result->data_status);
966 debug_print("imap status run - end %i\n", r);
969 int imap_threaded_status(Folder * folder, const char * mb,
970 struct mailimap_mailbox_data_status ** data_status,
973 struct status_param param;
974 struct status_result result;
975 struct mailimap_status_att_list * status_att_list;
977 debug_print("imap status - begin\n");
979 status_att_list = mailimap_status_att_list_new_empty();
981 mailimap_status_att_list_add(status_att_list,
982 MAILIMAP_STATUS_ATT_MESSAGES);
985 mailimap_status_att_list_add(status_att_list,
986 MAILIMAP_STATUS_ATT_RECENT);
989 mailimap_status_att_list_add(status_att_list,
990 MAILIMAP_STATUS_ATT_UIDNEXT);
993 mailimap_status_att_list_add(status_att_list,
994 MAILIMAP_STATUS_ATT_UIDVALIDITY);
997 mailimap_status_att_list_add(status_att_list,
998 MAILIMAP_STATUS_ATT_UNSEEN);
1000 param.imap = get_imap(folder);
1002 param.status_att_list = status_att_list;
1004 threaded_run(folder, ¶m, &result, status_run);
1006 debug_print("imap status - end\n");
1008 * data_status = result.data_status;
1010 mailimap_status_att_list_free(status_att_list);
1012 return result.error;
1021 struct noop_result {
1025 static void noop_run(struct etpan_thread_op * op)
1027 struct noop_param * param;
1028 struct noop_result * result;
1032 result = op->result;
1036 r = mailimap_noop(param->imap);
1039 debug_print("imap noop run - end %i\n", r);
1042 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1043 unsigned int *p_recent,
1044 unsigned int *p_expunge,
1045 unsigned int *p_unseen,
1046 unsigned int *p_uidnext,
1047 unsigned int *p_uidval)
1049 struct noop_param param;
1050 struct noop_result result;
1053 debug_print("imap noop - begin\n");
1055 imap = get_imap(folder);
1058 threaded_run(folder, ¶m, &result, noop_run);
1060 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1061 * p_exists = imap->imap_selection_info->sel_exists;
1062 * p_recent = imap->imap_selection_info->sel_recent;
1063 * p_unseen = imap->imap_selection_info->sel_unseen;
1064 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1065 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1073 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1074 imap->imap_response_info->rsp_expunged != NULL) {
1075 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1079 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1080 *p_exists, *p_recent, *p_expunge, *p_unseen,
1081 *p_uidnext, *p_uidval);
1083 return result.error;
1087 struct starttls_result {
1091 static void starttls_run(struct etpan_thread_op * op)
1093 struct connect_param * param;
1094 struct starttls_result * result;
1098 result = op->result;
1102 r = mailimap_starttls(param->imap);
1105 debug_print("imap starttls run - end %i\n", r);
1108 mailimap *imap = param->imap;
1109 mailstream_low *plain_low = NULL;
1110 mailstream_low *tls_low = NULL;
1113 plain_low = mailstream_get_low(imap->imap_stream);
1114 fd = mailstream_low_get_fd(plain_low);
1116 debug_print("imap starttls run - can't get fd\n");
1117 result->error = MAILIMAP_ERROR_STREAM;
1121 tls_low = mailstream_low_tls_open(fd);
1122 if (tls_low == NULL) {
1123 debug_print("imap starttls run - can't tls_open\n");
1124 result->error = MAILIMAP_ERROR_STREAM;
1127 mailstream_low_free(plain_low);
1128 mailstream_set_low(imap->imap_stream, tls_low);
1132 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1134 struct connect_param param;
1135 struct starttls_result result;
1137 unsigned char *certificate;
1139 debug_print("imap starttls - begin\n");
1141 param.imap = get_imap(folder);
1142 param.server = host;
1145 threaded_run(folder, ¶m, &result, starttls_run);
1147 debug_print("imap starttls - end\n");
1149 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1150 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1151 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1152 result.error = MAILIMAP_ERROR_STREAM;
1156 return result.error;
1161 struct create_param {
1166 struct create_result {
1170 static void create_run(struct etpan_thread_op * op)
1172 struct create_param * param;
1173 struct create_result * result;
1177 result = op->result;
1181 r = mailimap_create(param->imap, param->mb);
1184 debug_print("imap create run - end %i\n", r);
1187 int imap_threaded_create(Folder * folder, const char * mb)
1189 struct create_param param;
1190 struct create_result result;
1192 debug_print("imap create - begin\n");
1194 param.imap = get_imap(folder);
1197 threaded_run(folder, ¶m, &result, create_run);
1199 debug_print("imap create - end\n");
1201 return result.error;
1207 struct rename_param {
1210 const char * new_name;
1213 struct rename_result {
1217 static void rename_run(struct etpan_thread_op * op)
1219 struct rename_param * param;
1220 struct rename_result * result;
1224 result = op->result;
1228 r = mailimap_rename(param->imap, param->mb, param->new_name);
1231 debug_print("imap rename run - end %i\n", r);
1234 int imap_threaded_rename(Folder * folder,
1235 const char * mb, const char * new_name)
1237 struct rename_param param;
1238 struct rename_result result;
1240 debug_print("imap rename - begin\n");
1242 param.imap = get_imap(folder);
1244 param.new_name = new_name;
1246 threaded_run(folder, ¶m, &result, rename_run);
1248 debug_print("imap rename - end\n");
1250 return result.error;
1256 struct delete_param {
1261 struct delete_result {
1265 static void delete_run(struct etpan_thread_op * op)
1267 struct delete_param * param;
1268 struct delete_result * result;
1272 result = op->result;
1276 r = mailimap_delete(param->imap, param->mb);
1279 debug_print("imap delete run - end %i\n", r);
1282 int imap_threaded_delete(Folder * folder, const char * mb)
1284 struct delete_param param;
1285 struct delete_result result;
1287 debug_print("imap delete - begin\n");
1289 param.imap = get_imap(folder);
1292 threaded_run(folder, ¶m, &result, delete_run);
1294 debug_print("imap delete - end\n");
1296 return result.error;
1301 struct select_param {
1306 struct select_result {
1310 static void select_run(struct etpan_thread_op * op)
1312 struct select_param * param;
1313 struct select_result * result;
1317 result = op->result;
1321 r = mailimap_select(param->imap, param->mb);
1324 debug_print("imap select run - end %i\n", r);
1327 int imap_threaded_select(Folder * folder, const char * mb,
1328 gint * exists, gint * recent, gint * unseen,
1329 guint32 * uid_validity,gint *can_create_flags,
1332 struct select_param param;
1333 struct select_result result;
1336 debug_print("imap select - begin\n");
1338 imap = get_imap(folder);
1342 threaded_run(folder, ¶m, &result, select_run);
1344 if (result.error != MAILIMAP_NO_ERROR)
1345 return result.error;
1347 if (!imap || imap->imap_selection_info == NULL)
1348 return MAILIMAP_ERROR_PARSE;
1350 * exists = imap->imap_selection_info->sel_exists;
1351 * recent = imap->imap_selection_info->sel_recent;
1352 * unseen = imap->imap_selection_info->sel_unseen;
1353 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1354 * can_create_flags = FALSE;
1356 if (imap->imap_selection_info->sel_perm_flags) {
1357 GSList *t_flags = NULL;
1359 clist_begin(imap->imap_selection_info->sel_perm_flags);
1360 for (; cur; cur = clist_next(cur)) {
1361 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1362 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1363 *can_create_flags = TRUE;
1364 else if (flag->fl_flag &&
1365 flag->fl_flag->fl_type == 6 &&
1366 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1367 *can_create_flags = TRUE;
1369 MsgPermFlags c_flag = 0;
1370 switch (flag->fl_flag->fl_type) {
1371 case MAILIMAP_FLAG_ANSWERED:
1372 c_flag = IMAP_FLAG_ANSWERED;
1374 case MAILIMAP_FLAG_FLAGGED:
1375 c_flag = IMAP_FLAG_FLAGGED;
1377 case MAILIMAP_FLAG_DELETED:
1378 c_flag = IMAP_FLAG_DELETED;
1380 case MAILIMAP_FLAG_DRAFT:
1381 c_flag = IMAP_FLAG_DRAFT;
1383 case MAILIMAP_FLAG_SEEN:
1384 c_flag = IMAP_FLAG_SEEN;
1386 case MAILIMAP_FLAG_KEYWORD:
1387 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
1388 c_flag = IMAP_FLAG_FORWARDED;
1394 t_flags = g_slist_prepend(t_flags,
1395 GUINT_TO_POINTER(c_flag));
1400 *ok_flags = t_flags;
1402 debug_print("imap select - end\n");
1404 return result.error;
1407 static void close_run(struct etpan_thread_op * op)
1409 struct select_param * param;
1410 struct select_result * result;
1414 result = op->result;
1418 r = mailimap_close(param->imap);
1421 debug_print("imap close run - end %i\n", r);
1424 int imap_threaded_close(Folder * folder)
1426 struct select_param param;
1427 struct select_result result;
1430 debug_print("imap close - begin\n");
1432 imap = get_imap(folder);
1435 threaded_run(folder, ¶m, &result, close_run);
1437 if (result.error != MAILIMAP_NO_ERROR)
1438 return result.error;
1440 debug_print("imap close - end\n");
1442 return result.error;
1445 struct examine_param {
1450 struct examine_result {
1454 static void examine_run(struct etpan_thread_op * op)
1456 struct examine_param * param;
1457 struct examine_result * result;
1461 result = op->result;
1465 r = mailimap_examine(param->imap, param->mb);
1468 debug_print("imap examine run - end %i\n", r);
1471 int imap_threaded_examine(Folder * folder, const char * mb,
1472 gint * exists, gint * recent, gint * unseen,
1473 guint32 * uid_validity)
1475 struct examine_param param;
1476 struct examine_result result;
1479 debug_print("imap examine - begin\n");
1481 imap = get_imap(folder);
1485 threaded_run(folder, ¶m, &result, examine_run);
1487 if (result.error != MAILIMAP_NO_ERROR)
1488 return result.error;
1490 if (!imap || imap->imap_selection_info == NULL)
1491 return MAILIMAP_ERROR_PARSE;
1493 * exists = imap->imap_selection_info->sel_exists;
1494 * recent = imap->imap_selection_info->sel_recent;
1495 * unseen = imap->imap_selection_info->sel_unseen;
1496 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1498 debug_print("imap examine - end\n");
1500 return result.error;
1506 struct search_param {
1509 struct mailimap_set * set;
1512 struct search_result {
1514 clist * search_result;
1517 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1519 return mailimap_set_item_new(orig->set_first, orig->set_last);
1522 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1524 clist *list = orig ? orig->set_list : NULL;
1525 clist *newlist = clist_new();
1530 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1531 clist_append(newlist,
1532 sc_mailimap_set_item_copy(
1533 (struct mailimap_set_item *)clist_content(cur)));
1534 return mailimap_set_new(newlist);
1537 static void search_run(struct etpan_thread_op * op)
1539 struct search_param * param;
1540 struct search_result * result;
1542 struct mailimap_search_key * key = NULL;
1543 struct mailimap_search_key * uid_key = NULL;
1544 struct mailimap_search_key * search_type_key;
1545 clist * search_result;
1548 result = op->result;
1552 /* we copy the mailimap_set because freeing the key is recursive */
1553 if (param->set != NULL) {
1554 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1555 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1556 uid_key = mailimap_search_key_new_all();
1558 search_type_key = NULL;
1559 switch (param->type) {
1560 case IMAP_SEARCH_TYPE_SIMPLE:
1561 search_type_key = NULL;
1564 case IMAP_SEARCH_TYPE_SEEN:
1565 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1566 NULL, NULL, NULL, NULL, NULL,
1567 NULL, NULL, NULL, NULL, NULL,
1568 NULL, NULL, NULL, NULL, 0,
1569 NULL, NULL, NULL, NULL, NULL,
1570 NULL, 0, NULL, NULL, NULL);
1573 case IMAP_SEARCH_TYPE_UNSEEN:
1574 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1575 NULL, NULL, NULL, NULL, NULL,
1576 NULL, NULL, NULL, NULL, NULL,
1577 NULL, NULL, NULL, NULL, 0,
1578 NULL, NULL, NULL, NULL, NULL,
1579 NULL, 0, NULL, NULL, NULL);
1582 case IMAP_SEARCH_TYPE_ANSWERED:
1583 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1584 NULL, NULL, NULL, NULL, NULL,
1585 NULL, NULL, NULL, NULL, NULL,
1586 NULL, NULL, NULL, NULL, 0,
1587 NULL, NULL, NULL, NULL, NULL,
1588 NULL, 0, NULL, NULL, NULL);
1591 case IMAP_SEARCH_TYPE_FLAGGED:
1592 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1593 NULL, NULL, NULL, NULL, NULL,
1594 NULL, NULL, NULL, NULL, NULL,
1595 NULL, NULL, NULL, NULL, 0,
1596 NULL, NULL, NULL, NULL, NULL,
1597 NULL, 0, NULL, NULL, NULL);
1599 case IMAP_SEARCH_TYPE_DELETED:
1600 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1601 NULL, NULL, NULL, NULL, NULL,
1602 NULL, NULL, NULL, NULL, NULL,
1603 NULL, NULL, NULL, NULL, 0,
1604 NULL, NULL, NULL, NULL, NULL,
1605 NULL, 0, NULL, NULL, NULL);
1607 case IMAP_SEARCH_TYPE_FORWARDED:
1608 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1609 NULL, NULL, NULL, NULL, NULL,
1610 strdup("$Forwarded"), NULL, NULL, NULL, NULL,
1611 NULL, NULL, NULL, NULL, 0,
1612 NULL, NULL, NULL, NULL, NULL,
1613 NULL, 0, NULL, NULL, NULL);
1617 if (search_type_key != NULL) {
1618 if (param->set != NULL) {
1619 key = mailimap_search_key_new_multiple_empty();
1620 mailimap_search_key_multiple_add(key, search_type_key);
1621 mailimap_search_key_multiple_add(key, uid_key);
1623 key = search_type_key;
1625 } else if (uid_key != NULL) {
1630 g_warning("no key!");
1631 result = op->result;
1633 result->search_result = NULL;
1635 mailstream_logger = imap_logger_uid;
1637 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1639 mailstream_logger = imap_logger_cmd;
1641 /* free the key (with the imapset) */
1642 mailimap_search_key_free(key);
1645 result->search_result = search_result;
1647 debug_print("imap search run - end %i\n", result->error);
1650 int imap_threaded_search(Folder * folder, int search_type,
1651 struct mailimap_set * set, clist ** search_result)
1653 struct search_param param;
1654 struct search_result result;
1657 debug_print("imap search - begin\n");
1659 imap = get_imap(folder);
1662 param.type = search_type;
1664 threaded_run(folder, ¶m, &result, search_run);
1666 if (result.error != MAILIMAP_NO_ERROR)
1667 return result.error;
1669 debug_print("imap search - end\n");
1671 * search_result = result.search_result;
1673 return result.error;
1678 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1682 struct mailimap_msg_att_dynamic ** patt_dyn);
1685 result_to_uid_list(clist * fetch_result, carray ** result)
1692 tab = carray_new(128);
1694 res = MAILIMAP_ERROR_MEMORY;
1698 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1699 cur = clist_next(cur)) {
1700 struct mailimap_msg_att * msg_att;
1704 msg_att = clist_content(cur);
1707 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1709 puid = malloc(sizeof(* puid));
1711 res = MAILIMAP_ERROR_MEMORY;
1716 r = carray_add(tab, puid, NULL);
1719 res = MAILIMAP_ERROR_MEMORY;
1726 return MAILIMAP_NO_ERROR;
1729 imap_fetch_uid_list_free(tab);
1734 static int imap_get_messages_list(mailimap * imap,
1735 uint32_t first_index,
1740 struct mailimap_fetch_att * fetch_att;
1741 struct mailimap_fetch_type * fetch_type;
1742 struct mailimap_set * set;
1743 clist * fetch_result;
1746 set = mailimap_set_new_interval(first_index, 0);
1748 res = MAILIMAP_ERROR_MEMORY;
1752 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1753 if (fetch_type == NULL) {
1754 res = MAILIMAP_ERROR_MEMORY;
1758 fetch_att = mailimap_fetch_att_new_uid();
1759 if (fetch_att == NULL) {
1760 res = MAILIMAP_ERROR_MEMORY;
1761 goto free_fetch_type;
1764 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1765 if (r != MAILIMAP_NO_ERROR) {
1766 mailimap_fetch_att_free(fetch_att);
1767 res = MAILIMAP_ERROR_MEMORY;
1768 goto free_fetch_type;
1771 mailstream_logger = imap_logger_fetch;
1773 r = mailimap_uid_fetch(imap, set,
1774 fetch_type, &fetch_result);
1776 mailstream_logger = imap_logger_cmd;
1777 mailimap_fetch_type_free(fetch_type);
1778 mailimap_set_free(set);
1780 if (r != MAILIMAP_NO_ERROR) {
1786 r = result_to_uid_list(fetch_result, &env_list);
1787 mailimap_fetch_list_free(fetch_result);
1789 * result = env_list;
1791 return MAILIMAP_NO_ERROR;
1794 mailimap_fetch_type_free(fetch_type);
1796 mailimap_set_free(set);
1804 struct fetch_uid_param {
1806 uint32_t first_index;
1809 struct fetch_uid_result {
1811 carray * fetch_result;
1814 static void fetch_uid_run(struct etpan_thread_op * op)
1816 struct fetch_uid_param * param;
1817 struct fetch_uid_result * result;
1818 carray * fetch_result;
1822 result = op->result;
1826 fetch_result = NULL;
1827 mailstream_logger = imap_logger_noop;
1828 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1830 r = imap_get_messages_list(param->imap, param->first_index,
1833 mailstream_logger = imap_logger_cmd;
1836 result->fetch_result = fetch_result;
1837 debug_print("imap fetch_uid run - end %i\n", r);
1840 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1841 carray ** fetch_result)
1843 struct fetch_uid_param param;
1844 struct fetch_uid_result result;
1847 debug_print("imap fetch_uid - begin\n");
1849 imap = get_imap(folder);
1851 param.first_index = first_index;
1853 threaded_run(folder, ¶m, &result, fetch_uid_run);
1855 if (result.error != MAILIMAP_NO_ERROR)
1856 return result.error;
1858 debug_print("imap fetch_uid - end\n");
1860 * fetch_result = result.fetch_result;
1862 return result.error;
1866 void imap_fetch_uid_list_free(carray * uid_list)
1870 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1873 puid = carray_get(uid_list, i);
1876 carray_free(uid_list);
1882 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1885 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1891 GSList *tags = NULL;
1893 tab = carray_new(128);
1895 res = MAILIMAP_ERROR_MEMORY;
1899 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1900 cur = clist_next(cur)) {
1901 struct mailimap_msg_att * msg_att;
1904 struct mailimap_msg_att_dynamic * att_dyn;
1910 msg_att = clist_content(cur);
1914 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1917 if (att_dyn == NULL)
1920 flags = imap_flags_to_flags(att_dyn, &tags);
1922 puid = malloc(sizeof(* puid));
1924 res = MAILIMAP_ERROR_MEMORY;
1929 r = carray_add(tab, puid, NULL);
1932 res = MAILIMAP_ERROR_MEMORY;
1935 pflags = malloc(sizeof(* pflags));
1936 if (pflags == NULL) {
1937 res = MAILIMAP_ERROR_MEMORY;
1941 r = carray_add(tab, pflags, NULL);
1944 res = MAILIMAP_ERROR_MEMORY;
1947 r = carray_add(tab, tags, NULL);
1950 res = MAILIMAP_ERROR_MEMORY;
1957 return MAILIMAP_NO_ERROR;
1960 imap_fetch_uid_flags_list_free(tab);
1961 slist_free_strings(tags);
1967 static int imap_get_messages_flags_list(mailimap * imap,
1968 uint32_t first_index,
1973 struct mailimap_fetch_att * fetch_att;
1974 struct mailimap_fetch_type * fetch_type;
1975 struct mailimap_set * set;
1976 clist * fetch_result;
1979 set = mailimap_set_new_interval(first_index, 0);
1981 res = MAILIMAP_ERROR_MEMORY;
1985 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1986 if (fetch_type == NULL) {
1987 res = MAILIMAP_ERROR_MEMORY;
1991 fetch_att = mailimap_fetch_att_new_flags();
1992 if (fetch_att == NULL) {
1993 res = MAILIMAP_ERROR_MEMORY;
1994 goto free_fetch_type;
1997 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1998 if (r != MAILIMAP_NO_ERROR) {
1999 mailimap_fetch_att_free(fetch_att);
2000 res = MAILIMAP_ERROR_MEMORY;
2001 goto free_fetch_type;
2004 fetch_att = mailimap_fetch_att_new_uid();
2005 if (fetch_att == NULL) {
2006 res = MAILIMAP_ERROR_MEMORY;
2007 goto free_fetch_type;
2010 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2011 if (r != MAILIMAP_NO_ERROR) {
2012 mailimap_fetch_att_free(fetch_att);
2013 res = MAILIMAP_ERROR_MEMORY;
2014 goto free_fetch_type;
2017 mailstream_logger = imap_logger_fetch;
2019 r = mailimap_uid_fetch(imap, set,
2020 fetch_type, &fetch_result);
2022 mailstream_logger = imap_logger_cmd;
2023 mailimap_fetch_type_free(fetch_type);
2024 mailimap_set_free(set);
2026 if (r != MAILIMAP_NO_ERROR) {
2032 r = result_to_uid_flags_list(fetch_result, &env_list);
2033 mailimap_fetch_list_free(fetch_result);
2035 * result = env_list;
2037 return MAILIMAP_NO_ERROR;
2040 mailimap_fetch_type_free(fetch_type);
2042 mailimap_set_free(set);
2049 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2051 struct fetch_uid_param * param;
2052 struct fetch_uid_result * result;
2053 carray * fetch_result;
2057 result = op->result;
2061 fetch_result = NULL;
2062 r = imap_get_messages_flags_list(param->imap, param->first_index,
2066 result->fetch_result = fetch_result;
2067 debug_print("imap fetch_uid run - end %i\n", r);
2070 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2071 carray ** fetch_result)
2073 struct fetch_uid_param param;
2074 struct fetch_uid_result result;
2077 debug_print("imap fetch_uid - begin\n");
2079 imap = get_imap(folder);
2081 param.first_index = first_index;
2083 mailstream_logger = imap_logger_noop;
2084 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2086 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2088 mailstream_logger = imap_logger_cmd;
2091 if (result.error != MAILIMAP_NO_ERROR)
2092 return result.error;
2094 debug_print("imap fetch_uid - end\n");
2096 * fetch_result = result.fetch_result;
2098 return result.error;
2102 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2106 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2109 data = carray_get(uid_flags_list, i);
2111 data = carray_get(uid_flags_list, i + 1);
2114 carray_free(uid_flags_list);
2119 static int imap_fetch(mailimap * imap,
2122 size_t * result_len)
2125 struct mailimap_set * set;
2126 struct mailimap_fetch_att * fetch_att;
2127 struct mailimap_fetch_type * fetch_type;
2128 clist * fetch_result;
2129 struct mailimap_msg_att * msg_att;
2130 struct mailimap_msg_att_item * msg_att_item;
2135 struct mailimap_section * section;
2137 set = mailimap_set_new_single(msg_index);
2139 res = MAILIMAP_ERROR_MEMORY;
2143 section = mailimap_section_new(NULL);
2144 if (section == NULL) {
2145 res = MAILIMAP_ERROR_MEMORY;
2149 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2150 if (fetch_att == NULL) {
2151 mailimap_section_free(section);
2152 res = MAILIMAP_ERROR_MEMORY;
2156 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2157 if (fetch_type == NULL) {
2158 res = MAILIMAP_ERROR_MEMORY;
2159 goto free_fetch_att;
2162 mailstream_logger = imap_logger_fetch;
2164 r = mailimap_uid_fetch(imap, set,
2165 fetch_type, &fetch_result);
2167 mailstream_logger = imap_logger_cmd;
2169 mailimap_fetch_type_free(fetch_type);
2170 mailimap_set_free(set);
2173 case MAILIMAP_NO_ERROR:
2179 if (clist_begin(fetch_result) == NULL) {
2180 mailimap_fetch_list_free(fetch_result);
2181 return MAILIMAP_ERROR_FETCH;
2184 msg_att = clist_begin(fetch_result)->data;
2189 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2190 cur = clist_next(cur)) {
2191 msg_att_item = clist_content(cur);
2193 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2194 if (msg_att_item->att_data.att_static->att_type ==
2195 MAILIMAP_MSG_ATT_BODY_SECTION) {
2196 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2198 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2200 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2205 mailimap_fetch_list_free(fetch_result);
2208 return MAILIMAP_ERROR_FETCH;
2211 * result_len = text_length;
2213 return MAILIMAP_NO_ERROR;
2216 mailimap_fetch_att_free(fetch_att);
2218 mailimap_set_free(set);
2223 static int imap_fetch_header(mailimap * imap,
2226 size_t * result_len)
2229 struct mailimap_set * set;
2230 struct mailimap_fetch_att * fetch_att;
2231 struct mailimap_fetch_type * fetch_type;
2232 clist * fetch_result;
2233 struct mailimap_msg_att * msg_att;
2234 struct mailimap_msg_att_item * msg_att_item;
2239 struct mailimap_section * section;
2241 set = mailimap_set_new_single(msg_index);
2243 res = MAILIMAP_ERROR_MEMORY;
2247 section = mailimap_section_new_header();
2248 if (section == NULL) {
2249 res = MAILIMAP_ERROR_MEMORY;
2253 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2254 if (fetch_att == NULL) {
2255 mailimap_section_free(section);
2256 res = MAILIMAP_ERROR_MEMORY;
2260 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2261 if (fetch_type == NULL) {
2262 res = MAILIMAP_ERROR_MEMORY;
2263 goto free_fetch_att;
2266 mailstream_logger = imap_logger_fetch;
2268 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2270 mailstream_logger = imap_logger_cmd;
2271 mailimap_fetch_type_free(fetch_type);
2272 mailimap_set_free(set);
2275 case MAILIMAP_NO_ERROR:
2281 if (clist_begin(fetch_result) == NULL) {
2282 mailimap_fetch_list_free(fetch_result);
2283 return MAILIMAP_ERROR_FETCH;
2286 msg_att = clist_begin(fetch_result)->data;
2291 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2292 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;
2299 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2301 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2306 mailimap_fetch_list_free(fetch_result);
2309 return MAILIMAP_ERROR_FETCH;
2312 * result_len = text_length;
2314 return MAILIMAP_NO_ERROR;
2317 mailimap_fetch_att_free(fetch_att);
2319 mailimap_set_free(set);
2326 struct fetch_content_param {
2329 const char * filename;
2333 struct fetch_content_result {
2337 static void fetch_content_run(struct etpan_thread_op * op)
2339 struct fetch_content_param * param;
2340 struct fetch_content_result * result;
2342 size_t content_size;
2348 result = op->result;
2354 if (param->with_body)
2355 r = imap_fetch(param->imap, param->msg_index,
2356 &content, &content_size);
2358 r = imap_fetch_header(param->imap, param->msg_index,
2359 &content, &content_size);
2363 if (r == MAILIMAP_NO_ERROR) {
2364 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2366 result->error = MAILIMAP_ERROR_FETCH;
2370 f = fdopen(fd, "wb");
2372 result->error = MAILIMAP_ERROR_FETCH;
2376 r = fwrite(content, 1, content_size, f);
2377 if (r < content_size) {
2378 result->error = MAILIMAP_ERROR_FETCH;
2384 result->error = MAILIMAP_ERROR_FETCH;
2395 claws_unlink(param->filename);
2398 /* mmap_string_unref is a simple free in libetpan
2399 * when it has MMAP_UNAVAILABLE defined */
2400 if (mmap_string_unref(content) != 0)
2404 debug_print("imap fetch_content run - end %i\n", result->error);
2407 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2409 const char * filename)
2411 struct fetch_content_param param;
2412 struct fetch_content_result result;
2415 debug_print("imap fetch_content - begin\n");
2417 imap = get_imap(folder);
2419 param.msg_index = msg_index;
2420 param.filename = filename;
2421 param.with_body = with_body;
2423 threaded_run(folder, ¶m, &result, fetch_content_run);
2425 if (result.error != MAILIMAP_NO_ERROR)
2426 return result.error;
2428 debug_print("imap fetch_content - end\n");
2430 return result.error;
2435 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2440 GSList *tags = NULL;
2444 flag_list = att_dyn->att_list;
2445 if (flag_list == NULL)
2448 for(cur = clist_begin(flag_list) ; cur != NULL ;
2449 cur = clist_next(cur)) {
2450 struct mailimap_flag_fetch * flag_fetch;
2452 flag_fetch = clist_content(cur);
2453 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2456 switch (flag_fetch->fl_flag->fl_type) {
2457 case MAILIMAP_FLAG_ANSWERED:
2458 flags |= MSG_REPLIED;
2460 case MAILIMAP_FLAG_FLAGGED:
2461 flags |= MSG_MARKED;
2463 case MAILIMAP_FLAG_DELETED:
2464 flags |= MSG_DELETED;
2466 case MAILIMAP_FLAG_SEEN:
2467 flags &= ~MSG_UNREAD;
2470 case MAILIMAP_FLAG_KEYWORD:
2471 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
2472 flags |= MSG_FORWARDED;
2474 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2484 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2488 struct mailimap_msg_att_dynamic ** patt_dyn)
2490 clistiter * item_cur;
2494 struct mailimap_msg_att_dynamic * att_dyn;
2501 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2502 item_cur = clist_next(item_cur)) {
2503 struct mailimap_msg_att_item * item;
2505 item = clist_content(item_cur);
2507 switch (item->att_type) {
2508 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2509 switch (item->att_data.att_static->att_type) {
2510 case MAILIMAP_MSG_ATT_UID:
2511 uid = item->att_data.att_static->att_data.att_uid;
2514 case MAILIMAP_MSG_ATT_BODY_SECTION:
2515 if (headers == NULL) {
2516 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2519 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2520 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2525 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2526 if (att_dyn == NULL) {
2527 att_dyn = item->att_data.att_dyn;
2535 if (pheaders != NULL)
2536 * pheaders = headers;
2537 if (pref_size != NULL)
2538 * pref_size = ref_size;
2539 if (patt_dyn != NULL)
2540 * patt_dyn = att_dyn;
2542 return MAIL_NO_ERROR;
2545 static struct imap_fetch_env_info *
2546 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2548 struct imap_fetch_env_info * info;
2552 struct mailimap_msg_att_dynamic * att_dyn;
2554 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2559 info = malloc(sizeof(* info));
2561 info->headers = strdup(headers);
2563 info->flags = imap_flags_to_flags(att_dyn, tags);
2569 imap_fetch_result_to_envelop_list(clist * fetch_result,
2570 carray ** p_env_list)
2577 env_list = carray_new(16);
2579 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2580 cur = clist_next(cur)) {
2581 struct mailimap_msg_att * msg_att;
2582 struct imap_fetch_env_info * env_info;
2583 GSList *tags = NULL;
2585 msg_att = clist_content(cur);
2587 env_info = fetch_to_env_info(msg_att, &tags);
2589 return MAILIMAP_ERROR_MEMORY;
2590 carray_add(env_list, env_info, NULL);
2591 carray_add(env_list, tags, NULL);
2594 * p_env_list = env_list;
2596 return MAIL_NO_ERROR;
2599 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2601 struct mailimap_fetch_att * fetch_att;
2605 struct mailimap_header_list * imap_hdrlist;
2606 struct mailimap_section * section;
2608 hdrlist = clist_new();
2610 header = strdup("Date");
2611 r = clist_append(hdrlist, header);
2612 header = strdup("From");
2613 r = clist_append(hdrlist, header);
2614 header = strdup("To");
2615 r = clist_append(hdrlist, header);
2616 header = strdup("Cc");
2617 r = clist_append(hdrlist, header);
2618 header = strdup("Subject");
2619 r = clist_append(hdrlist, header);
2620 header = strdup("Message-ID");
2621 r = clist_append(hdrlist, header);
2622 header = strdup("References");
2623 r = clist_append(hdrlist, header);
2624 header = strdup("In-Reply-To");
2625 r = clist_append(hdrlist, header);
2627 imap_hdrlist = mailimap_header_list_new(hdrlist);
2628 section = mailimap_section_new_header_fields(imap_hdrlist);
2629 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2630 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2632 return MAIL_NO_ERROR;
2635 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2637 struct mailimap_fetch_att * fetch_att;
2638 struct mailimap_section * section;
2640 section = mailimap_section_new_header();
2641 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2642 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2644 return MAIL_NO_ERROR;
2648 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2649 carray ** p_env_list)
2651 struct mailimap_fetch_att * fetch_att;
2652 struct mailimap_fetch_type * fetch_type;
2654 clist * fetch_result;
2656 carray * env_list = NULL;
2660 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2663 fetch_att = mailimap_fetch_att_new_uid();
2664 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2667 fetch_att = mailimap_fetch_att_new_flags();
2668 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2671 fetch_att = mailimap_fetch_att_new_rfc822_size();
2672 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2676 key.len = sizeof(imap);
2677 r = chash_get(courier_workaround_hash, &key, &value);
2679 r = imap_add_envelope_fetch_att(fetch_type);
2681 r = imap_add_header_fetch_att(fetch_type);
2683 mailstream_logger = imap_logger_fetch;
2685 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2687 mailstream_logger = imap_logger_cmd;
2689 case MAILIMAP_NO_ERROR:
2692 mailimap_fetch_type_free(fetch_type);
2693 debug_print("uid_fetch: %d\n", r);
2697 if (clist_begin(fetch_result) == NULL) {
2698 res = MAILIMAP_ERROR_FETCH;
2699 debug_print("clist_begin = NULL\n");
2703 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2704 mailimap_fetch_list_free(fetch_result);
2706 if (r != MAILIMAP_NO_ERROR) {
2707 mailimap_fetch_type_free(fetch_type);
2708 res = MAILIMAP_ERROR_MEMORY;
2709 debug_print("fetch_result_to_envelop_list: %d\n", res);
2713 mailimap_fetch_type_free(fetch_type);
2715 * p_env_list = env_list;
2717 return MAILIMAP_NO_ERROR;
2723 struct fetch_env_param {
2725 struct mailimap_set * set;
2728 struct fetch_env_result {
2729 carray * fetch_env_result;
2733 static void fetch_env_run(struct etpan_thread_op * op)
2735 struct fetch_env_param * param;
2736 struct fetch_env_result * result;
2741 result = op->result;
2746 r = imap_get_envelopes_list(param->imap, param->set,
2750 result->fetch_env_result = env_list;
2752 debug_print("imap fetch_env run - end %i\n", r);
2755 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2756 carray ** p_env_list)
2758 struct fetch_env_param param;
2759 struct fetch_env_result result;
2762 debug_print("imap fetch_env - begin\n");
2764 imap = get_imap(folder);
2768 threaded_run(folder, ¶m, &result, fetch_env_run);
2770 if (result.error != MAILIMAP_NO_ERROR) {
2776 key.len = sizeof(imap);
2777 r = chash_get(courier_workaround_hash, &key, &value);
2781 chash_set(courier_workaround_hash, &key, &value, NULL);
2783 threaded_run(folder, ¶m, &result, fetch_env_run);
2787 if (result.error != MAILIMAP_NO_ERROR)
2788 return result.error;
2790 debug_print("imap fetch_env - end\n");
2792 * p_env_list = result.fetch_env_result;
2794 return result.error;
2797 void imap_fetch_env_free(carray * env_list)
2801 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2802 struct imap_fetch_env_info * env_info;
2804 env_info = carray_get(env_list, i);
2805 free(env_info->headers);
2808 carray_free(env_list);
2815 struct append_param {
2817 const char * mailbox;
2818 const char * filename;
2819 struct mailimap_flag_list * flag_list;
2822 struct append_result {
2827 static void append_run(struct etpan_thread_op * op)
2829 struct append_param * param;
2830 struct append_result * result;
2835 struct stat stat_buf;
2838 guint32 uid = 0, val = 0;
2841 result = op->result;
2846 r = stat(param->filename, &stat_buf);
2848 result->error = MAILIMAP_ERROR_APPEND;
2851 size = stat_buf.st_size;
2853 fd = open(param->filename, O_RDONLY);
2855 result->error = MAILIMAP_ERROR_APPEND;
2859 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2860 if (data == (void *) MAP_FAILED) {
2862 result->error = MAILIMAP_ERROR_APPEND;
2866 data = file_read_to_str_no_recode(param->filename);
2868 result->error = MAILIMAP_ERROR_APPEND;
2871 size = strlen(data);
2873 mailstream_logger = imap_logger_append;
2875 r = mailimap_uidplus_append(param->imap, param->mailbox,
2876 param->flag_list, NULL,
2877 data, size, &val, &uid);
2879 mailstream_logger = imap_logger_cmd;
2890 debug_print("imap append run - end %i uid %d\n", r, uid);
2893 int imap_threaded_append(Folder * folder, const char * mailbox,
2894 const char * filename,
2895 struct mailimap_flag_list * flag_list,
2898 struct append_param param;
2899 struct append_result result;
2902 debug_print("imap append - begin\n");
2904 imap = get_imap(folder);
2906 param.mailbox = mailbox;
2907 param.filename = filename;
2908 param.flag_list = flag_list;
2910 threaded_run(folder, ¶m, &result, append_run);
2912 if (result.error != MAILIMAP_NO_ERROR)
2913 return result.error;
2915 debug_print("imap append - end\n");
2919 return result.error;
2925 struct expunge_param {
2929 struct expunge_result {
2933 static void expunge_run(struct etpan_thread_op * op)
2935 struct expunge_param * param;
2936 struct expunge_result * result;
2940 result = op->result;
2944 r = mailimap_expunge(param->imap);
2947 debug_print("imap expunge run - end %i\n", r);
2950 int imap_threaded_expunge(Folder * folder)
2952 struct expunge_param param;
2953 struct expunge_result result;
2955 debug_print("imap expunge - begin\n");
2957 param.imap = get_imap(folder);
2959 threaded_run(folder, ¶m, &result, expunge_run);
2961 debug_print("imap expunge - end\n");
2963 return result.error;
2969 struct mailimap_set * set;
2973 struct copy_result {
2975 struct mailimap_set *source;
2976 struct mailimap_set *dest;
2979 static void copy_run(struct etpan_thread_op * op)
2981 struct copy_param * param;
2982 struct copy_result * result;
2985 struct mailimap_set *source = NULL, *dest = NULL;
2988 result = op->result;
2992 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2993 &val, &source, &dest);
2997 result->source = source;
2998 result->dest = dest;
3000 result->source = NULL;
3001 result->dest = NULL;
3003 debug_print("imap copy run - end %i\n", r);
3006 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3007 const char * mb, struct mailimap_set **source,
3008 struct mailimap_set **dest)
3010 struct copy_param param;
3011 struct copy_result result;
3014 debug_print("imap copy - begin\n");
3016 imap = get_imap(folder);
3021 threaded_run(folder, ¶m, &result, copy_run);
3025 if (result.error != MAILIMAP_NO_ERROR)
3026 return result.error;
3028 *source = result.source;
3029 *dest = result.dest;
3031 debug_print("imap copy - end\n");
3033 return result.error;
3038 struct store_param {
3040 struct mailimap_set * set;
3041 struct mailimap_store_att_flags * store_att_flags;
3044 struct store_result {
3048 static void store_run(struct etpan_thread_op * op)
3050 struct store_param * param;
3051 struct store_result * result;
3055 result = op->result;
3059 r = mailimap_uid_store(param->imap, param->set,
3060 param->store_att_flags);
3064 debug_print("imap store run - end %i\n", r);
3067 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3068 struct mailimap_store_att_flags * store_att_flags)
3070 struct store_param param;
3071 struct store_result result;
3074 debug_print("imap store - begin\n");
3076 imap = get_imap(folder);
3079 param.store_att_flags = store_att_flags;
3081 threaded_run(folder, ¶m, &result, store_run);
3083 if (result.error != MAILIMAP_NO_ERROR)
3084 return result.error;
3086 debug_print("imap store - end\n");
3088 return result.error;
3092 #define ENV_BUFFER_SIZE 512
3094 static void do_exec_command(int fd, const char * command,
3095 const char * servername, uint16_t port)
3099 char env_buffer[ENV_BUFFER_SIZE];
3103 /* Fork again to become a child of init rather than
3104 the etpan client. */
3109 g_setenv("ETPANSERVER", servername, TRUE);
3111 g_unsetenv("ETPANSERVER");
3116 snprintf(porttext, sizeof(porttext), "%d", port);
3117 g_setenv("ETPANPORT", porttext, TRUE);
3120 g_unsetenv("ETPANPORT");
3123 /* Not a lot we can do if there's an error other than bail. */
3124 if (dup2(fd, 0) == -1)
3126 if (dup2(fd, 1) == -1)
3129 /* Should we close stderr and reopen /dev/null? */
3131 maxopen = sysconf(_SC_OPEN_MAX);
3132 for (i=3; i < maxopen; i++)
3136 /* Detach from the controlling tty if we have one. Otherwise,
3137 SSH might do something stupid like trying to use it instead
3138 of running $SSH_ASKPASS. Doh. */
3139 fd = open("/dev/tty", O_RDONLY);
3141 ioctl(fd, TIOCNOTTY, NULL);
3144 #endif /* TIOCNOTTY */
3146 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3148 /* Eep. Shouldn't reach this */
3152 static int subcommand_connect(const char *command,
3153 const char *servername, uint16_t port)
3155 /* SEB unsupported on Windows */
3159 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3164 do_exec_command(sockfds[1], command, servername, port);
3166 else if (childpid == -1) {
3174 /* Reap child, leaving grandchild process to run */
3175 waitpid(childpid, NULL, 0);
3180 static int socket_connect_cmd(mailimap * imap, const char * command,
3181 const char * server, int port)
3187 fd = subcommand_connect(command, server, port);
3189 return MAILIMAP_ERROR_STREAM;
3191 s = mailstream_socket_open(fd);
3194 return MAILIMAP_ERROR_STREAM;
3197 r = mailimap_connect(imap, s);
3198 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3199 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3200 mailstream_close(s);
3202 imap->imap_stream = NULL;
3211 struct connect_cmd_param {
3213 const char * command;
3214 const char * server;
3218 struct connect_cmd_result {
3222 static void connect_cmd_run(struct etpan_thread_op * op)
3225 struct connect_cmd_param * param;
3226 struct connect_cmd_result * result;
3229 result = op->result;
3233 r = socket_connect_cmd(param->imap, param->command,
3234 param->server, param->port);
3240 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3241 const char * server, int port)
3243 struct connect_cmd_param param;
3244 struct connect_cmd_result result;
3247 mailimap * imap, * oldimap;
3249 oldimap = get_imap(folder);
3251 imap = mailimap_new(0, NULL);
3254 debug_print("deleting old imap %p\n", oldimap);
3255 delete_imap(folder, oldimap);
3259 key.len = sizeof(folder);
3262 chash_set(session_hash, &key, &value, NULL);
3265 param.command = command;
3266 param.server = server;
3269 threaded_run(folder, ¶m, &result, connect_cmd_run);
3271 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3273 return result.error;
3275 #endif /* G_OS_WIN32 */
3277 void imap_threaded_cancel(Folder * folder)
3281 imap = get_imap(folder);
3282 if (imap->imap_stream != NULL)
3283 mailstream_cancel(imap->imap_stream);
3288 void imap_main_init(void)
3291 void imap_main_done(void)
3294 void imap_main_set_timeout(int sec)
3298 void imap_threaded_cancel(Folder * folder);