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 imap_disconnect_all();
291 etpan_thread_manager_stop(thread_manager);
292 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
295 etpan_thread_manager_join(thread_manager);
297 g_source_remove(thread_manager_signal);
298 g_io_channel_unref(io_channel);
300 etpan_thread_manager_free(thread_manager);
302 chash_free(courier_workaround_hash);
303 chash_free(session_hash);
304 chash_free(imap_hash);
307 void imap_init(Folder * folder)
309 struct etpan_thread * thread;
313 thread = etpan_thread_manager_get_thread(thread_manager);
316 key.len = sizeof(folder);
320 chash_set(imap_hash, &key, &value, NULL);
323 void imap_done(Folder * folder)
325 struct etpan_thread * thread;
331 key.len = sizeof(folder);
333 r = chash_get(imap_hash, &key, &value);
339 etpan_thread_unbind(thread);
341 chash_delete(imap_hash, &key, NULL);
343 debug_print("remove thread");
346 static struct etpan_thread * get_thread(Folder * folder)
348 struct etpan_thread * thread;
353 key.len = sizeof(folder);
355 chash_get(imap_hash, &key, &value);
361 static mailimap * get_imap(Folder * folder)
369 key.len = sizeof(folder);
371 r = chash_get(session_hash, &key, &value);
376 debug_print("found imap %p\n", imap);
380 static gboolean cb_show_error(gpointer data)
382 mainwindow_show_error();
386 static void generic_cb(int cancelled, void * result, void * callback_data)
388 struct etpan_thread_op * op;
390 op = (struct etpan_thread_op *) callback_data;
392 debug_print("generic_cb\n");
393 if (op->imap && op->imap->imap_response_info &&
394 op->imap->imap_response_info->rsp_alert) {
395 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
396 op->imap->imap_response_info->rsp_alert);
397 g_timeout_add(10, cb_show_error, NULL);
402 static void threaded_run(Folder * folder, void * param, void * result,
403 void (* func)(struct etpan_thread_op * ))
405 struct etpan_thread_op * op;
406 struct etpan_thread * thread;
408 imap_folder_ref(folder);
410 op = etpan_thread_op_new();
412 op->imap = get_imap(folder);
418 op->callback = generic_cb;
419 op->callback_data = op;
424 thread = get_thread(folder);
425 etpan_thread_op_schedule(thread, op);
427 while (!op->finished) {
428 gtk_main_iteration();
431 etpan_thread_op_free(op);
433 imap_folder_unref(folder);
439 struct connect_param {
445 struct connect_result {
449 #define CHECK_IMAP() { \
450 if (!param->imap) { \
451 result->error = MAILIMAP_ERROR_BAD_STATE; \
456 static void connect_run(struct etpan_thread_op * op)
459 struct connect_param * param;
460 struct connect_result * result;
467 r = mailimap_socket_connect(param->imap,
468 param->server, param->port);
474 int imap_threaded_connect(Folder * folder, const char * server, int port)
476 struct connect_param param;
477 struct connect_result result;
480 mailimap * imap, * oldimap;
482 oldimap = get_imap(folder);
484 imap = mailimap_new(0, NULL);
487 debug_print("deleting old imap %p\n", oldimap);
488 delete_imap(folder, oldimap);
492 key.len = sizeof(folder);
495 chash_set(session_hash, &key, &value, NULL);
498 param.server = server;
502 threaded_run(folder, ¶m, &result, connect_run);
504 debug_print("connect ok %i with imap %p\n", result.error, imap);
509 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
512 struct connect_param *param = (struct connect_param *)data;
515 if (certificate == NULL || len < 0) {
516 g_warning("no cert presented.\n");
519 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
521 g_warning("IMAP: can't get cert\n");
523 } else if (ssl_certificate_check(cert, NULL,
524 (gchar *)param->server, (gushort)param->port) == TRUE) {
532 struct connect_param *param = (struct connect_param *)data;
533 gnutls_x509_crt cert = NULL;
536 if (certificate == NULL || len < 0) {
537 g_warning("no cert presented.\n");
541 tmp.data = malloc(len);
542 memcpy(tmp.data, certificate, len);
544 gnutls_x509_crt_init(&cert);
545 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
546 g_warning("IMAP: can't get cert\n");
548 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
549 (gchar *)param->server, (gushort)param->port) == TRUE) {
550 gnutls_x509_crt_deinit(cert);
553 gnutls_x509_crt_deinit(cert);
560 static void connect_ssl_run(struct etpan_thread_op * op)
563 struct connect_param * param;
564 struct connect_result * result;
571 r = mailimap_ssl_connect(param->imap,
572 param->server, param->port);
576 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
578 struct connect_param param;
579 struct connect_result result;
582 mailimap * imap, * oldimap;
583 unsigned char *certificate = NULL;
586 oldimap = get_imap(folder);
588 imap = mailimap_new(0, NULL);
591 debug_print("deleting old imap %p\n", oldimap);
592 delete_imap(folder, oldimap);
596 key.len = sizeof(folder);
599 chash_set(session_hash, &key, &value, NULL);
602 param.server = server;
606 threaded_run(folder, ¶m, &result, connect_ssl_run);
608 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
609 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
610 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
611 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
616 debug_print("connect %d with imap %p\n", result.error, imap);
627 struct mailimap_capability_data *caps;
630 static void capability_run(struct etpan_thread_op * op)
633 struct capa_param * param;
634 struct capa_result * result;
635 struct mailimap_capability_data *caps;
642 r = mailimap_capability(param->imap, &caps);
645 result->caps = (r == 0 ? caps : NULL);
649 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
651 struct capa_param param;
652 struct capa_result result;
655 imap = get_imap(folder);
659 threaded_run(folder, ¶m, &result, capability_run);
661 debug_print("capa %d\n", result.error);
670 struct disconnect_param {
674 struct disconnect_result {
678 static void disconnect_run(struct etpan_thread_op * op)
681 struct disconnect_param * param;
682 struct disconnect_result * result;
689 r = mailimap_logout(param->imap);
694 void imap_threaded_disconnect(Folder * folder)
696 struct connect_param param;
697 struct connect_result result;
700 imap = get_imap(folder);
702 debug_print("was disconnected\n");
708 threaded_run(folder, ¶m, &result, disconnect_run);
710 if (imap == get_imap(folder)) {
711 debug_print("deleting old imap %p\n", imap);
712 delete_imap(folder, imap);
714 debug_print("imap already deleted %p\n", imap);
717 debug_print("disconnect ok\n");
724 const char * wildcard;
733 static void list_run(struct etpan_thread_op * op)
735 struct list_param * param;
736 struct list_result * result;
747 if (param->base == NULL || param->wildcard == NULL) {
750 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
754 r = mailimap_lsub(param->imap, param->base,
755 param->wildcard, &list);
757 r = mailimap_list(param->imap, param->base,
758 param->wildcard, &list);
761 debug_print("imap list run - end\n");
764 int imap_threaded_list(Folder * folder, const char * base,
765 const char * wildcard,
768 struct list_param param;
769 struct list_result result;
771 debug_print("imap list - begin\n");
773 param.imap = get_imap(folder);
775 param.wildcard = wildcard;
776 param.sub_only = FALSE;
778 threaded_run(folder, ¶m, &result, list_run);
780 * p_result = result.list;
782 debug_print("imap list - end %p\n", result.list);
787 int imap_threaded_lsub(Folder * folder, const char * base,
788 const char * wildcard,
791 struct list_param param;
792 struct list_result result;
794 debug_print("imap lsub - begin\n");
796 param.imap = get_imap(folder);
798 param.wildcard = wildcard;
799 param.sub_only = TRUE;
801 threaded_run(folder, ¶m, &result, list_run);
803 * p_result = result.list;
805 debug_print("imap lsub - end %p\n", result.list);
810 struct subscribe_param {
816 struct subscribe_result {
820 static void subscribe_run(struct etpan_thread_op * op)
822 struct subscribe_param * param;
823 struct subscribe_result * result;
831 if (param->mb == NULL) {
833 debug_print("no mb\n");
836 if (param->subscribe)
837 r = mailimap_subscribe(param->imap, param->mb);
839 r = mailimap_unsubscribe(param->imap, param->mb);
841 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
844 int imap_threaded_subscribe(Folder * folder, const char * mb,
847 struct subscribe_param param;
848 struct subscribe_result result;
850 debug_print("imap list - begin\n");
852 param.imap = get_imap(folder);
854 param.subscribe = subscribe;
856 threaded_run(folder, ¶m, &result, subscribe_run);
864 const char * password;
869 struct login_result {
873 static void login_run(struct etpan_thread_op * op)
875 struct login_param * param;
876 struct login_result * result;
878 #ifdef DISABLE_LOG_DURING_LOGIN
887 #ifdef DISABLE_LOG_DURING_LOGIN
888 old_debug = mailstream_debug;
889 mailstream_debug = 0;
891 if (!strcmp(param->type, "LOGIN"))
892 r = mailimap_login(param->imap,
893 param->login, param->password);
894 else if (!strcmp(param->type, "GSSAPI"))
895 r = mailimap_authenticate(param->imap,
896 param->type, param->server, NULL, NULL,
897 param->login, param->login,
898 param->password, NULL);
900 r = mailimap_authenticate(param->imap,
901 param->type, NULL, NULL, NULL,
902 param->login, param->login,
903 param->password, NULL);
904 #ifdef DISABLE_LOG_DURING_LOGIN
905 mailstream_debug = old_debug;
909 if (param->imap->imap_response)
910 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
911 debug_print("imap login run - end %i\n", r);
914 int imap_threaded_login(Folder * folder,
915 const char * login, const char * password,
918 struct login_param param;
919 struct login_result result;
921 debug_print("imap login - begin\n");
923 param.imap = get_imap(folder);
925 param.password = password;
927 if (folder && folder->account)
928 param.server = folder->account->recv_server;
932 threaded_run(folder, ¶m, &result, login_run);
934 debug_print("imap login - end\n");
940 struct status_param {
943 struct mailimap_status_att_list * status_att_list;
946 struct status_result {
948 struct mailimap_mailbox_data_status * data_status;
951 static void status_run(struct etpan_thread_op * op)
953 struct status_param * param;
954 struct status_result * result;
962 r = mailimap_status(param->imap, param->mb,
963 param->status_att_list,
964 &result->data_status);
967 debug_print("imap status run - end %i\n", r);
970 int imap_threaded_status(Folder * folder, const char * mb,
971 struct mailimap_mailbox_data_status ** data_status,
974 struct status_param param;
975 struct status_result result;
976 struct mailimap_status_att_list * status_att_list;
978 debug_print("imap status - begin\n");
980 status_att_list = mailimap_status_att_list_new_empty();
982 mailimap_status_att_list_add(status_att_list,
983 MAILIMAP_STATUS_ATT_MESSAGES);
986 mailimap_status_att_list_add(status_att_list,
987 MAILIMAP_STATUS_ATT_RECENT);
990 mailimap_status_att_list_add(status_att_list,
991 MAILIMAP_STATUS_ATT_UIDNEXT);
994 mailimap_status_att_list_add(status_att_list,
995 MAILIMAP_STATUS_ATT_UIDVALIDITY);
998 mailimap_status_att_list_add(status_att_list,
999 MAILIMAP_STATUS_ATT_UNSEEN);
1001 param.imap = get_imap(folder);
1003 param.status_att_list = status_att_list;
1005 threaded_run(folder, ¶m, &result, status_run);
1007 debug_print("imap status - end\n");
1009 * data_status = result.data_status;
1011 mailimap_status_att_list_free(status_att_list);
1013 return result.error;
1022 struct noop_result {
1026 static void noop_run(struct etpan_thread_op * op)
1028 struct noop_param * param;
1029 struct noop_result * result;
1033 result = op->result;
1037 r = mailimap_noop(param->imap);
1040 debug_print("imap noop run - end %i\n", r);
1043 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1044 unsigned int *p_recent,
1045 unsigned int *p_expunge,
1046 unsigned int *p_unseen,
1047 unsigned int *p_uidnext,
1048 unsigned int *p_uidval)
1050 struct noop_param param;
1051 struct noop_result result;
1054 debug_print("imap noop - begin\n");
1056 imap = get_imap(folder);
1059 threaded_run(folder, ¶m, &result, noop_run);
1061 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1062 * p_exists = imap->imap_selection_info->sel_exists;
1063 * p_recent = imap->imap_selection_info->sel_recent;
1064 * p_unseen = imap->imap_selection_info->sel_unseen;
1065 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1066 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1074 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1075 imap->imap_response_info->rsp_expunged != NULL) {
1076 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1080 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1081 *p_exists, *p_recent, *p_expunge, *p_unseen,
1082 *p_uidnext, *p_uidval);
1084 return result.error;
1088 struct starttls_result {
1092 static void starttls_run(struct etpan_thread_op * op)
1094 struct connect_param * param;
1095 struct starttls_result * result;
1099 result = op->result;
1103 r = mailimap_starttls(param->imap);
1106 debug_print("imap starttls run - end %i\n", r);
1109 mailimap *imap = param->imap;
1110 mailstream_low *plain_low = NULL;
1111 mailstream_low *tls_low = NULL;
1114 plain_low = mailstream_get_low(imap->imap_stream);
1115 fd = mailstream_low_get_fd(plain_low);
1117 debug_print("imap starttls run - can't get fd\n");
1118 result->error = MAILIMAP_ERROR_STREAM;
1122 tls_low = mailstream_low_tls_open(fd);
1123 if (tls_low == NULL) {
1124 debug_print("imap starttls run - can't tls_open\n");
1125 result->error = MAILIMAP_ERROR_STREAM;
1128 mailstream_low_free(plain_low);
1129 mailstream_set_low(imap->imap_stream, tls_low);
1133 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1135 struct connect_param param;
1136 struct starttls_result result;
1138 unsigned char *certificate;
1140 debug_print("imap starttls - begin\n");
1142 param.imap = get_imap(folder);
1143 param.server = host;
1146 threaded_run(folder, ¶m, &result, starttls_run);
1148 debug_print("imap starttls - end\n");
1150 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1151 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1152 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1153 result.error = MAILIMAP_ERROR_STREAM;
1157 return result.error;
1162 struct create_param {
1167 struct create_result {
1171 static void create_run(struct etpan_thread_op * op)
1173 struct create_param * param;
1174 struct create_result * result;
1178 result = op->result;
1182 r = mailimap_create(param->imap, param->mb);
1185 debug_print("imap create run - end %i\n", r);
1188 int imap_threaded_create(Folder * folder, const char * mb)
1190 struct create_param param;
1191 struct create_result result;
1193 debug_print("imap create - begin\n");
1195 param.imap = get_imap(folder);
1198 threaded_run(folder, ¶m, &result, create_run);
1200 debug_print("imap create - end\n");
1202 return result.error;
1208 struct rename_param {
1211 const char * new_name;
1214 struct rename_result {
1218 static void rename_run(struct etpan_thread_op * op)
1220 struct rename_param * param;
1221 struct rename_result * result;
1225 result = op->result;
1229 r = mailimap_rename(param->imap, param->mb, param->new_name);
1232 debug_print("imap rename run - end %i\n", r);
1235 int imap_threaded_rename(Folder * folder,
1236 const char * mb, const char * new_name)
1238 struct rename_param param;
1239 struct rename_result result;
1241 debug_print("imap rename - begin\n");
1243 param.imap = get_imap(folder);
1245 param.new_name = new_name;
1247 threaded_run(folder, ¶m, &result, rename_run);
1249 debug_print("imap rename - end\n");
1251 return result.error;
1257 struct delete_param {
1262 struct delete_result {
1266 static void delete_run(struct etpan_thread_op * op)
1268 struct delete_param * param;
1269 struct delete_result * result;
1273 result = op->result;
1277 r = mailimap_delete(param->imap, param->mb);
1280 debug_print("imap delete run - end %i\n", r);
1283 int imap_threaded_delete(Folder * folder, const char * mb)
1285 struct delete_param param;
1286 struct delete_result result;
1288 debug_print("imap delete - begin\n");
1290 param.imap = get_imap(folder);
1293 threaded_run(folder, ¶m, &result, delete_run);
1295 debug_print("imap delete - end\n");
1297 return result.error;
1302 struct select_param {
1307 struct select_result {
1311 static void select_run(struct etpan_thread_op * op)
1313 struct select_param * param;
1314 struct select_result * result;
1318 result = op->result;
1322 r = mailimap_select(param->imap, param->mb);
1325 debug_print("imap select run - end %i\n", r);
1328 int imap_threaded_select(Folder * folder, const char * mb,
1329 gint * exists, gint * recent, gint * unseen,
1330 guint32 * uid_validity,gint *can_create_flags,
1333 struct select_param param;
1334 struct select_result result;
1337 debug_print("imap select - begin\n");
1339 imap = get_imap(folder);
1343 threaded_run(folder, ¶m, &result, select_run);
1345 if (result.error != MAILIMAP_NO_ERROR)
1346 return result.error;
1348 if (!imap || imap->imap_selection_info == NULL)
1349 return MAILIMAP_ERROR_PARSE;
1351 * exists = imap->imap_selection_info->sel_exists;
1352 * recent = imap->imap_selection_info->sel_recent;
1353 * unseen = imap->imap_selection_info->sel_unseen;
1354 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1355 * can_create_flags = FALSE;
1357 if (imap->imap_selection_info->sel_perm_flags) {
1358 GSList *t_flags = NULL;
1360 clist_begin(imap->imap_selection_info->sel_perm_flags);
1361 for (; cur; cur = clist_next(cur)) {
1362 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1363 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1364 *can_create_flags = TRUE;
1365 else if (flag->fl_flag &&
1366 flag->fl_flag->fl_type == 6 &&
1367 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1368 *can_create_flags = TRUE;
1370 MsgPermFlags c_flag = 0;
1371 switch (flag->fl_flag->fl_type) {
1372 case MAILIMAP_FLAG_ANSWERED:
1373 c_flag = IMAP_FLAG_ANSWERED;
1375 case MAILIMAP_FLAG_FLAGGED:
1376 c_flag = IMAP_FLAG_FLAGGED;
1378 case MAILIMAP_FLAG_DELETED:
1379 c_flag = IMAP_FLAG_DELETED;
1381 case MAILIMAP_FLAG_DRAFT:
1382 c_flag = IMAP_FLAG_DRAFT;
1384 case MAILIMAP_FLAG_SEEN:
1385 c_flag = IMAP_FLAG_SEEN;
1387 case MAILIMAP_FLAG_KEYWORD:
1388 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
1389 c_flag = IMAP_FLAG_FORWARDED;
1395 t_flags = g_slist_prepend(t_flags,
1396 GUINT_TO_POINTER(c_flag));
1401 *ok_flags = t_flags;
1403 debug_print("imap select - end\n");
1405 return result.error;
1408 static void close_run(struct etpan_thread_op * op)
1410 struct select_param * param;
1411 struct select_result * result;
1415 result = op->result;
1419 r = mailimap_close(param->imap);
1422 debug_print("imap close run - end %i\n", r);
1425 int imap_threaded_close(Folder * folder)
1427 struct select_param param;
1428 struct select_result result;
1431 debug_print("imap close - begin\n");
1433 imap = get_imap(folder);
1436 threaded_run(folder, ¶m, &result, close_run);
1438 if (result.error != MAILIMAP_NO_ERROR)
1439 return result.error;
1441 debug_print("imap close - end\n");
1443 return result.error;
1446 struct examine_param {
1451 struct examine_result {
1455 static void examine_run(struct etpan_thread_op * op)
1457 struct examine_param * param;
1458 struct examine_result * result;
1462 result = op->result;
1466 r = mailimap_examine(param->imap, param->mb);
1469 debug_print("imap examine run - end %i\n", r);
1472 int imap_threaded_examine(Folder * folder, const char * mb,
1473 gint * exists, gint * recent, gint * unseen,
1474 guint32 * uid_validity)
1476 struct examine_param param;
1477 struct examine_result result;
1480 debug_print("imap examine - begin\n");
1482 imap = get_imap(folder);
1486 threaded_run(folder, ¶m, &result, examine_run);
1488 if (result.error != MAILIMAP_NO_ERROR)
1489 return result.error;
1491 if (!imap || imap->imap_selection_info == NULL)
1492 return MAILIMAP_ERROR_PARSE;
1494 * exists = imap->imap_selection_info->sel_exists;
1495 * recent = imap->imap_selection_info->sel_recent;
1496 * unseen = imap->imap_selection_info->sel_unseen;
1497 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1499 debug_print("imap examine - end\n");
1501 return result.error;
1507 struct search_param {
1510 struct mailimap_set * set;
1513 struct search_result {
1515 clist * search_result;
1518 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1520 return mailimap_set_item_new(orig->set_first, orig->set_last);
1523 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1525 clist *list = orig ? orig->set_list : NULL;
1526 clist *newlist = clist_new();
1531 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1532 clist_append(newlist,
1533 sc_mailimap_set_item_copy(
1534 (struct mailimap_set_item *)clist_content(cur)));
1535 return mailimap_set_new(newlist);
1538 static void search_run(struct etpan_thread_op * op)
1540 struct search_param * param;
1541 struct search_result * result;
1543 struct mailimap_search_key * key = NULL;
1544 struct mailimap_search_key * uid_key = NULL;
1545 struct mailimap_search_key * search_type_key;
1546 clist * search_result;
1549 result = op->result;
1553 /* we copy the mailimap_set because freeing the key is recursive */
1554 if (param->set != NULL) {
1555 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1556 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1557 uid_key = mailimap_search_key_new_all();
1559 search_type_key = NULL;
1560 switch (param->type) {
1561 case IMAP_SEARCH_TYPE_SIMPLE:
1562 search_type_key = NULL;
1565 case IMAP_SEARCH_TYPE_SEEN:
1566 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1567 NULL, NULL, NULL, NULL, NULL,
1568 NULL, NULL, NULL, NULL, NULL,
1569 NULL, NULL, NULL, NULL, 0,
1570 NULL, NULL, NULL, NULL, NULL,
1571 NULL, 0, NULL, NULL, NULL);
1574 case IMAP_SEARCH_TYPE_UNSEEN:
1575 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1576 NULL, NULL, NULL, NULL, NULL,
1577 NULL, NULL, NULL, NULL, NULL,
1578 NULL, NULL, NULL, NULL, 0,
1579 NULL, NULL, NULL, NULL, NULL,
1580 NULL, 0, NULL, NULL, NULL);
1583 case IMAP_SEARCH_TYPE_ANSWERED:
1584 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1585 NULL, NULL, NULL, NULL, NULL,
1586 NULL, NULL, NULL, NULL, NULL,
1587 NULL, NULL, NULL, NULL, 0,
1588 NULL, NULL, NULL, NULL, NULL,
1589 NULL, 0, NULL, NULL, NULL);
1592 case IMAP_SEARCH_TYPE_FLAGGED:
1593 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1594 NULL, NULL, NULL, NULL, NULL,
1595 NULL, NULL, NULL, NULL, NULL,
1596 NULL, NULL, NULL, NULL, 0,
1597 NULL, NULL, NULL, NULL, NULL,
1598 NULL, 0, NULL, NULL, NULL);
1600 case IMAP_SEARCH_TYPE_DELETED:
1601 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1602 NULL, NULL, NULL, NULL, NULL,
1603 NULL, NULL, NULL, NULL, NULL,
1604 NULL, NULL, NULL, NULL, 0,
1605 NULL, NULL, NULL, NULL, NULL,
1606 NULL, 0, NULL, NULL, NULL);
1608 case IMAP_SEARCH_TYPE_FORWARDED:
1609 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1610 NULL, NULL, NULL, NULL, NULL,
1611 strdup("$Forwarded"), NULL, NULL, NULL, NULL,
1612 NULL, NULL, NULL, NULL, 0,
1613 NULL, NULL, NULL, NULL, NULL,
1614 NULL, 0, NULL, NULL, NULL);
1618 if (search_type_key != NULL) {
1619 if (param->set != NULL) {
1620 key = mailimap_search_key_new_multiple_empty();
1621 mailimap_search_key_multiple_add(key, search_type_key);
1622 mailimap_search_key_multiple_add(key, uid_key);
1624 key = search_type_key;
1626 } else if (uid_key != NULL) {
1631 g_warning("no key!");
1632 result = op->result;
1634 result->search_result = NULL;
1636 mailstream_logger = imap_logger_uid;
1638 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1640 mailstream_logger = imap_logger_cmd;
1642 /* free the key (with the imapset) */
1643 mailimap_search_key_free(key);
1646 result->search_result = search_result;
1648 debug_print("imap search run - end %i\n", result->error);
1651 int imap_threaded_search(Folder * folder, int search_type,
1652 struct mailimap_set * set, clist ** search_result)
1654 struct search_param param;
1655 struct search_result result;
1658 debug_print("imap search - begin\n");
1660 imap = get_imap(folder);
1663 param.type = search_type;
1665 threaded_run(folder, ¶m, &result, search_run);
1667 if (result.error != MAILIMAP_NO_ERROR)
1668 return result.error;
1670 debug_print("imap search - end\n");
1672 * search_result = result.search_result;
1674 return result.error;
1679 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1683 struct mailimap_msg_att_dynamic ** patt_dyn);
1686 result_to_uid_list(clist * fetch_result, carray ** result)
1693 tab = carray_new(128);
1695 res = MAILIMAP_ERROR_MEMORY;
1699 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1700 cur = clist_next(cur)) {
1701 struct mailimap_msg_att * msg_att;
1705 msg_att = clist_content(cur);
1708 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1710 puid = malloc(sizeof(* puid));
1712 res = MAILIMAP_ERROR_MEMORY;
1717 r = carray_add(tab, puid, NULL);
1720 res = MAILIMAP_ERROR_MEMORY;
1727 return MAILIMAP_NO_ERROR;
1730 imap_fetch_uid_list_free(tab);
1735 static int imap_get_messages_list(mailimap * imap,
1736 uint32_t first_index,
1741 struct mailimap_fetch_att * fetch_att;
1742 struct mailimap_fetch_type * fetch_type;
1743 struct mailimap_set * set;
1744 clist * fetch_result;
1747 set = mailimap_set_new_interval(first_index, 0);
1749 res = MAILIMAP_ERROR_MEMORY;
1753 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1754 if (fetch_type == NULL) {
1755 res = MAILIMAP_ERROR_MEMORY;
1759 fetch_att = mailimap_fetch_att_new_uid();
1760 if (fetch_att == NULL) {
1761 res = MAILIMAP_ERROR_MEMORY;
1762 goto free_fetch_type;
1765 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1766 if (r != MAILIMAP_NO_ERROR) {
1767 mailimap_fetch_att_free(fetch_att);
1768 res = MAILIMAP_ERROR_MEMORY;
1769 goto free_fetch_type;
1772 mailstream_logger = imap_logger_fetch;
1774 r = mailimap_uid_fetch(imap, set,
1775 fetch_type, &fetch_result);
1777 mailstream_logger = imap_logger_cmd;
1778 mailimap_fetch_type_free(fetch_type);
1779 mailimap_set_free(set);
1781 if (r != MAILIMAP_NO_ERROR) {
1787 r = result_to_uid_list(fetch_result, &env_list);
1788 mailimap_fetch_list_free(fetch_result);
1790 * result = env_list;
1792 return MAILIMAP_NO_ERROR;
1795 mailimap_fetch_type_free(fetch_type);
1797 mailimap_set_free(set);
1805 struct fetch_uid_param {
1807 uint32_t first_index;
1810 struct fetch_uid_result {
1812 carray * fetch_result;
1815 static void fetch_uid_run(struct etpan_thread_op * op)
1817 struct fetch_uid_param * param;
1818 struct fetch_uid_result * result;
1819 carray * fetch_result;
1823 result = op->result;
1827 fetch_result = NULL;
1828 mailstream_logger = imap_logger_noop;
1829 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1831 r = imap_get_messages_list(param->imap, param->first_index,
1834 mailstream_logger = imap_logger_cmd;
1837 result->fetch_result = fetch_result;
1838 debug_print("imap fetch_uid run - end %i\n", r);
1841 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1842 carray ** fetch_result)
1844 struct fetch_uid_param param;
1845 struct fetch_uid_result result;
1848 debug_print("imap fetch_uid - begin\n");
1850 imap = get_imap(folder);
1852 param.first_index = first_index;
1854 threaded_run(folder, ¶m, &result, fetch_uid_run);
1856 if (result.error != MAILIMAP_NO_ERROR)
1857 return result.error;
1859 debug_print("imap fetch_uid - end\n");
1861 * fetch_result = result.fetch_result;
1863 return result.error;
1867 void imap_fetch_uid_list_free(carray * uid_list)
1871 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1874 puid = carray_get(uid_list, i);
1877 carray_free(uid_list);
1883 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1886 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1892 GSList *tags = NULL;
1894 tab = carray_new(128);
1896 res = MAILIMAP_ERROR_MEMORY;
1900 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1901 cur = clist_next(cur)) {
1902 struct mailimap_msg_att * msg_att;
1905 struct mailimap_msg_att_dynamic * att_dyn;
1911 msg_att = clist_content(cur);
1915 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1918 if (att_dyn == NULL)
1921 flags = imap_flags_to_flags(att_dyn, &tags);
1923 puid = malloc(sizeof(* puid));
1925 res = MAILIMAP_ERROR_MEMORY;
1930 r = carray_add(tab, puid, NULL);
1933 res = MAILIMAP_ERROR_MEMORY;
1936 pflags = malloc(sizeof(* pflags));
1937 if (pflags == NULL) {
1938 res = MAILIMAP_ERROR_MEMORY;
1942 r = carray_add(tab, pflags, NULL);
1945 res = MAILIMAP_ERROR_MEMORY;
1948 r = carray_add(tab, tags, NULL);
1951 res = MAILIMAP_ERROR_MEMORY;
1958 return MAILIMAP_NO_ERROR;
1961 imap_fetch_uid_flags_list_free(tab);
1962 slist_free_strings(tags);
1968 static int imap_get_messages_flags_list(mailimap * imap,
1969 uint32_t first_index,
1974 struct mailimap_fetch_att * fetch_att;
1975 struct mailimap_fetch_type * fetch_type;
1976 struct mailimap_set * set;
1977 clist * fetch_result;
1980 set = mailimap_set_new_interval(first_index, 0);
1982 res = MAILIMAP_ERROR_MEMORY;
1986 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1987 if (fetch_type == NULL) {
1988 res = MAILIMAP_ERROR_MEMORY;
1992 fetch_att = mailimap_fetch_att_new_flags();
1993 if (fetch_att == NULL) {
1994 res = MAILIMAP_ERROR_MEMORY;
1995 goto free_fetch_type;
1998 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1999 if (r != MAILIMAP_NO_ERROR) {
2000 mailimap_fetch_att_free(fetch_att);
2001 res = MAILIMAP_ERROR_MEMORY;
2002 goto free_fetch_type;
2005 fetch_att = mailimap_fetch_att_new_uid();
2006 if (fetch_att == NULL) {
2007 res = MAILIMAP_ERROR_MEMORY;
2008 goto free_fetch_type;
2011 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2012 if (r != MAILIMAP_NO_ERROR) {
2013 mailimap_fetch_att_free(fetch_att);
2014 res = MAILIMAP_ERROR_MEMORY;
2015 goto free_fetch_type;
2018 mailstream_logger = imap_logger_fetch;
2020 r = mailimap_uid_fetch(imap, set,
2021 fetch_type, &fetch_result);
2023 mailstream_logger = imap_logger_cmd;
2024 mailimap_fetch_type_free(fetch_type);
2025 mailimap_set_free(set);
2027 if (r != MAILIMAP_NO_ERROR) {
2033 r = result_to_uid_flags_list(fetch_result, &env_list);
2034 mailimap_fetch_list_free(fetch_result);
2036 * result = env_list;
2038 return MAILIMAP_NO_ERROR;
2041 mailimap_fetch_type_free(fetch_type);
2043 mailimap_set_free(set);
2050 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2052 struct fetch_uid_param * param;
2053 struct fetch_uid_result * result;
2054 carray * fetch_result;
2058 result = op->result;
2062 fetch_result = NULL;
2063 r = imap_get_messages_flags_list(param->imap, param->first_index,
2067 result->fetch_result = fetch_result;
2068 debug_print("imap fetch_uid run - end %i\n", r);
2071 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2072 carray ** fetch_result)
2074 struct fetch_uid_param param;
2075 struct fetch_uid_result result;
2078 debug_print("imap fetch_uid - begin\n");
2080 imap = get_imap(folder);
2082 param.first_index = first_index;
2084 mailstream_logger = imap_logger_noop;
2085 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2087 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2089 mailstream_logger = imap_logger_cmd;
2092 if (result.error != MAILIMAP_NO_ERROR)
2093 return result.error;
2095 debug_print("imap fetch_uid - end\n");
2097 * fetch_result = result.fetch_result;
2099 return result.error;
2103 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2107 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2110 data = carray_get(uid_flags_list, i);
2112 data = carray_get(uid_flags_list, i + 1);
2115 carray_free(uid_flags_list);
2120 static int imap_fetch(mailimap * imap,
2123 size_t * result_len)
2126 struct mailimap_set * set;
2127 struct mailimap_fetch_att * fetch_att;
2128 struct mailimap_fetch_type * fetch_type;
2129 clist * fetch_result;
2130 struct mailimap_msg_att * msg_att;
2131 struct mailimap_msg_att_item * msg_att_item;
2136 struct mailimap_section * section;
2138 set = mailimap_set_new_single(msg_index);
2140 res = MAILIMAP_ERROR_MEMORY;
2144 section = mailimap_section_new(NULL);
2145 if (section == NULL) {
2146 res = MAILIMAP_ERROR_MEMORY;
2150 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2151 if (fetch_att == NULL) {
2152 mailimap_section_free(section);
2153 res = MAILIMAP_ERROR_MEMORY;
2157 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2158 if (fetch_type == NULL) {
2159 res = MAILIMAP_ERROR_MEMORY;
2160 goto free_fetch_att;
2163 mailstream_logger = imap_logger_fetch;
2165 r = mailimap_uid_fetch(imap, set,
2166 fetch_type, &fetch_result);
2168 mailstream_logger = imap_logger_cmd;
2170 mailimap_fetch_type_free(fetch_type);
2171 mailimap_set_free(set);
2174 case MAILIMAP_NO_ERROR:
2180 if (clist_begin(fetch_result) == NULL) {
2181 mailimap_fetch_list_free(fetch_result);
2182 return MAILIMAP_ERROR_FETCH;
2185 msg_att = clist_begin(fetch_result)->data;
2190 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2191 cur = clist_next(cur)) {
2192 msg_att_item = clist_content(cur);
2194 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2195 if (msg_att_item->att_data.att_static->att_type ==
2196 MAILIMAP_MSG_ATT_BODY_SECTION) {
2197 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2199 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2201 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2206 mailimap_fetch_list_free(fetch_result);
2209 return MAILIMAP_ERROR_FETCH;
2212 * result_len = text_length;
2214 return MAILIMAP_NO_ERROR;
2217 mailimap_fetch_att_free(fetch_att);
2219 mailimap_set_free(set);
2224 static int imap_fetch_header(mailimap * imap,
2227 size_t * result_len)
2230 struct mailimap_set * set;
2231 struct mailimap_fetch_att * fetch_att;
2232 struct mailimap_fetch_type * fetch_type;
2233 clist * fetch_result;
2234 struct mailimap_msg_att * msg_att;
2235 struct mailimap_msg_att_item * msg_att_item;
2240 struct mailimap_section * section;
2242 set = mailimap_set_new_single(msg_index);
2244 res = MAILIMAP_ERROR_MEMORY;
2248 section = mailimap_section_new_header();
2249 if (section == NULL) {
2250 res = MAILIMAP_ERROR_MEMORY;
2254 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2255 if (fetch_att == NULL) {
2256 mailimap_section_free(section);
2257 res = MAILIMAP_ERROR_MEMORY;
2261 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2262 if (fetch_type == NULL) {
2263 res = MAILIMAP_ERROR_MEMORY;
2264 goto free_fetch_att;
2267 mailstream_logger = imap_logger_fetch;
2269 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2271 mailstream_logger = imap_logger_cmd;
2272 mailimap_fetch_type_free(fetch_type);
2273 mailimap_set_free(set);
2276 case MAILIMAP_NO_ERROR:
2282 if (clist_begin(fetch_result) == NULL) {
2283 mailimap_fetch_list_free(fetch_result);
2284 return MAILIMAP_ERROR_FETCH;
2287 msg_att = clist_begin(fetch_result)->data;
2292 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2293 cur = clist_next(cur)) {
2294 msg_att_item = clist_content(cur);
2296 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2297 if (msg_att_item->att_data.att_static->att_type ==
2298 MAILIMAP_MSG_ATT_BODY_SECTION) {
2299 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2300 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2302 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2307 mailimap_fetch_list_free(fetch_result);
2310 return MAILIMAP_ERROR_FETCH;
2313 * result_len = text_length;
2315 return MAILIMAP_NO_ERROR;
2318 mailimap_fetch_att_free(fetch_att);
2320 mailimap_set_free(set);
2327 struct fetch_content_param {
2330 const char * filename;
2334 struct fetch_content_result {
2338 static void fetch_content_run(struct etpan_thread_op * op)
2340 struct fetch_content_param * param;
2341 struct fetch_content_result * result;
2343 size_t content_size;
2349 result = op->result;
2355 if (param->with_body)
2356 r = imap_fetch(param->imap, param->msg_index,
2357 &content, &content_size);
2359 r = imap_fetch_header(param->imap, param->msg_index,
2360 &content, &content_size);
2364 if (r == MAILIMAP_NO_ERROR) {
2365 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2367 result->error = MAILIMAP_ERROR_FETCH;
2371 f = fdopen(fd, "wb");
2373 result->error = MAILIMAP_ERROR_FETCH;
2377 r = fwrite(content, 1, content_size, f);
2378 if (r < content_size) {
2379 result->error = MAILIMAP_ERROR_FETCH;
2385 result->error = MAILIMAP_ERROR_FETCH;
2396 claws_unlink(param->filename);
2399 /* mmap_string_unref is a simple free in libetpan
2400 * when it has MMAP_UNAVAILABLE defined */
2401 if (mmap_string_unref(content) != 0)
2405 debug_print("imap fetch_content run - end %i\n", result->error);
2408 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2410 const char * filename)
2412 struct fetch_content_param param;
2413 struct fetch_content_result result;
2416 debug_print("imap fetch_content - begin\n");
2418 imap = get_imap(folder);
2420 param.msg_index = msg_index;
2421 param.filename = filename;
2422 param.with_body = with_body;
2424 threaded_run(folder, ¶m, &result, fetch_content_run);
2426 if (result.error != MAILIMAP_NO_ERROR)
2427 return result.error;
2429 debug_print("imap fetch_content - end\n");
2431 return result.error;
2436 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2441 GSList *tags = NULL;
2445 flag_list = att_dyn->att_list;
2446 if (flag_list == NULL)
2449 for(cur = clist_begin(flag_list) ; cur != NULL ;
2450 cur = clist_next(cur)) {
2451 struct mailimap_flag_fetch * flag_fetch;
2453 flag_fetch = clist_content(cur);
2454 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2457 switch (flag_fetch->fl_flag->fl_type) {
2458 case MAILIMAP_FLAG_ANSWERED:
2459 flags |= MSG_REPLIED;
2461 case MAILIMAP_FLAG_FLAGGED:
2462 flags |= MSG_MARKED;
2464 case MAILIMAP_FLAG_DELETED:
2465 flags |= MSG_DELETED;
2467 case MAILIMAP_FLAG_SEEN:
2468 flags &= ~MSG_UNREAD;
2471 case MAILIMAP_FLAG_KEYWORD:
2472 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
2473 flags |= MSG_FORWARDED;
2475 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2485 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2489 struct mailimap_msg_att_dynamic ** patt_dyn)
2491 clistiter * item_cur;
2495 struct mailimap_msg_att_dynamic * att_dyn;
2502 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2503 item_cur = clist_next(item_cur)) {
2504 struct mailimap_msg_att_item * item;
2506 item = clist_content(item_cur);
2508 switch (item->att_type) {
2509 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2510 switch (item->att_data.att_static->att_type) {
2511 case MAILIMAP_MSG_ATT_UID:
2512 uid = item->att_data.att_static->att_data.att_uid;
2515 case MAILIMAP_MSG_ATT_BODY_SECTION:
2516 if (headers == NULL) {
2517 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2520 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2521 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2526 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2527 if (att_dyn == NULL) {
2528 att_dyn = item->att_data.att_dyn;
2536 if (pheaders != NULL)
2537 * pheaders = headers;
2538 if (pref_size != NULL)
2539 * pref_size = ref_size;
2540 if (patt_dyn != NULL)
2541 * patt_dyn = att_dyn;
2543 return MAIL_NO_ERROR;
2546 static struct imap_fetch_env_info *
2547 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2549 struct imap_fetch_env_info * info;
2553 struct mailimap_msg_att_dynamic * att_dyn;
2555 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2560 info = malloc(sizeof(* info));
2562 info->headers = strdup(headers);
2564 info->flags = imap_flags_to_flags(att_dyn, tags);
2570 imap_fetch_result_to_envelop_list(clist * fetch_result,
2571 carray ** p_env_list)
2578 env_list = carray_new(16);
2580 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2581 cur = clist_next(cur)) {
2582 struct mailimap_msg_att * msg_att;
2583 struct imap_fetch_env_info * env_info;
2584 GSList *tags = NULL;
2586 msg_att = clist_content(cur);
2588 env_info = fetch_to_env_info(msg_att, &tags);
2590 return MAILIMAP_ERROR_MEMORY;
2591 carray_add(env_list, env_info, NULL);
2592 carray_add(env_list, tags, NULL);
2595 * p_env_list = env_list;
2597 return MAIL_NO_ERROR;
2600 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2602 struct mailimap_fetch_att * fetch_att;
2606 struct mailimap_header_list * imap_hdrlist;
2607 struct mailimap_section * section;
2609 hdrlist = clist_new();
2611 header = strdup("Date");
2612 r = clist_append(hdrlist, header);
2613 header = strdup("From");
2614 r = clist_append(hdrlist, header);
2615 header = strdup("To");
2616 r = clist_append(hdrlist, header);
2617 header = strdup("Cc");
2618 r = clist_append(hdrlist, header);
2619 header = strdup("Subject");
2620 r = clist_append(hdrlist, header);
2621 header = strdup("Message-ID");
2622 r = clist_append(hdrlist, header);
2623 header = strdup("References");
2624 r = clist_append(hdrlist, header);
2625 header = strdup("In-Reply-To");
2626 r = clist_append(hdrlist, header);
2628 imap_hdrlist = mailimap_header_list_new(hdrlist);
2629 section = mailimap_section_new_header_fields(imap_hdrlist);
2630 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2631 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2633 return MAIL_NO_ERROR;
2636 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2638 struct mailimap_fetch_att * fetch_att;
2639 struct mailimap_section * section;
2641 section = mailimap_section_new_header();
2642 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2643 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2645 return MAIL_NO_ERROR;
2649 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2650 carray ** p_env_list)
2652 struct mailimap_fetch_att * fetch_att;
2653 struct mailimap_fetch_type * fetch_type;
2655 clist * fetch_result;
2657 carray * env_list = NULL;
2661 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2664 fetch_att = mailimap_fetch_att_new_uid();
2665 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2668 fetch_att = mailimap_fetch_att_new_flags();
2669 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2672 fetch_att = mailimap_fetch_att_new_rfc822_size();
2673 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2677 key.len = sizeof(imap);
2678 r = chash_get(courier_workaround_hash, &key, &value);
2680 r = imap_add_envelope_fetch_att(fetch_type);
2682 r = imap_add_header_fetch_att(fetch_type);
2684 mailstream_logger = imap_logger_fetch;
2686 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2688 mailstream_logger = imap_logger_cmd;
2690 case MAILIMAP_NO_ERROR:
2693 mailimap_fetch_type_free(fetch_type);
2694 debug_print("uid_fetch: %d\n", r);
2698 if (clist_begin(fetch_result) == NULL) {
2699 res = MAILIMAP_ERROR_FETCH;
2700 debug_print("clist_begin = NULL\n");
2704 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2705 mailimap_fetch_list_free(fetch_result);
2707 if (r != MAILIMAP_NO_ERROR) {
2708 mailimap_fetch_type_free(fetch_type);
2709 res = MAILIMAP_ERROR_MEMORY;
2710 debug_print("fetch_result_to_envelop_list: %d\n", res);
2714 mailimap_fetch_type_free(fetch_type);
2716 * p_env_list = env_list;
2718 return MAILIMAP_NO_ERROR;
2724 struct fetch_env_param {
2726 struct mailimap_set * set;
2729 struct fetch_env_result {
2730 carray * fetch_env_result;
2734 static void fetch_env_run(struct etpan_thread_op * op)
2736 struct fetch_env_param * param;
2737 struct fetch_env_result * result;
2742 result = op->result;
2747 r = imap_get_envelopes_list(param->imap, param->set,
2751 result->fetch_env_result = env_list;
2753 debug_print("imap fetch_env run - end %i\n", r);
2756 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2757 carray ** p_env_list)
2759 struct fetch_env_param param;
2760 struct fetch_env_result result;
2763 debug_print("imap fetch_env - begin\n");
2765 imap = get_imap(folder);
2769 threaded_run(folder, ¶m, &result, fetch_env_run);
2771 if (result.error != MAILIMAP_NO_ERROR) {
2777 key.len = sizeof(imap);
2778 r = chash_get(courier_workaround_hash, &key, &value);
2782 chash_set(courier_workaround_hash, &key, &value, NULL);
2784 threaded_run(folder, ¶m, &result, fetch_env_run);
2788 if (result.error != MAILIMAP_NO_ERROR)
2789 return result.error;
2791 debug_print("imap fetch_env - end\n");
2793 * p_env_list = result.fetch_env_result;
2795 return result.error;
2798 void imap_fetch_env_free(carray * env_list)
2802 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2803 struct imap_fetch_env_info * env_info;
2805 env_info = carray_get(env_list, i);
2806 free(env_info->headers);
2809 carray_free(env_list);
2816 struct append_param {
2818 const char * mailbox;
2819 const char * filename;
2820 struct mailimap_flag_list * flag_list;
2823 struct append_result {
2828 static void append_run(struct etpan_thread_op * op)
2830 struct append_param * param;
2831 struct append_result * result;
2836 struct stat stat_buf;
2839 guint32 uid = 0, val = 0;
2842 result = op->result;
2847 r = stat(param->filename, &stat_buf);
2849 result->error = MAILIMAP_ERROR_APPEND;
2852 size = stat_buf.st_size;
2854 fd = open(param->filename, O_RDONLY);
2856 result->error = MAILIMAP_ERROR_APPEND;
2860 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2861 if (data == (void *) MAP_FAILED) {
2863 result->error = MAILIMAP_ERROR_APPEND;
2867 data = file_read_to_str_no_recode(param->filename);
2869 result->error = MAILIMAP_ERROR_APPEND;
2872 size = strlen(data);
2874 mailstream_logger = imap_logger_append;
2876 r = mailimap_uidplus_append(param->imap, param->mailbox,
2877 param->flag_list, NULL,
2878 data, size, &val, &uid);
2880 mailstream_logger = imap_logger_cmd;
2891 debug_print("imap append run - end %i uid %d\n", r, uid);
2894 int imap_threaded_append(Folder * folder, const char * mailbox,
2895 const char * filename,
2896 struct mailimap_flag_list * flag_list,
2899 struct append_param param;
2900 struct append_result result;
2903 debug_print("imap append - begin\n");
2905 imap = get_imap(folder);
2907 param.mailbox = mailbox;
2908 param.filename = filename;
2909 param.flag_list = flag_list;
2911 threaded_run(folder, ¶m, &result, append_run);
2913 if (result.error != MAILIMAP_NO_ERROR)
2914 return result.error;
2916 debug_print("imap append - end\n");
2920 return result.error;
2926 struct expunge_param {
2930 struct expunge_result {
2934 static void expunge_run(struct etpan_thread_op * op)
2936 struct expunge_param * param;
2937 struct expunge_result * result;
2941 result = op->result;
2945 r = mailimap_expunge(param->imap);
2948 debug_print("imap expunge run - end %i\n", r);
2951 int imap_threaded_expunge(Folder * folder)
2953 struct expunge_param param;
2954 struct expunge_result result;
2956 debug_print("imap expunge - begin\n");
2958 param.imap = get_imap(folder);
2960 threaded_run(folder, ¶m, &result, expunge_run);
2962 debug_print("imap expunge - end\n");
2964 return result.error;
2970 struct mailimap_set * set;
2974 struct copy_result {
2976 struct mailimap_set *source;
2977 struct mailimap_set *dest;
2980 static void copy_run(struct etpan_thread_op * op)
2982 struct copy_param * param;
2983 struct copy_result * result;
2986 struct mailimap_set *source = NULL, *dest = NULL;
2989 result = op->result;
2993 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2994 &val, &source, &dest);
2998 result->source = source;
2999 result->dest = dest;
3001 result->source = NULL;
3002 result->dest = NULL;
3004 debug_print("imap copy run - end %i\n", r);
3007 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3008 const char * mb, struct mailimap_set **source,
3009 struct mailimap_set **dest)
3011 struct copy_param param;
3012 struct copy_result result;
3015 debug_print("imap copy - begin\n");
3017 imap = get_imap(folder);
3022 threaded_run(folder, ¶m, &result, copy_run);
3026 if (result.error != MAILIMAP_NO_ERROR)
3027 return result.error;
3029 *source = result.source;
3030 *dest = result.dest;
3032 debug_print("imap copy - end\n");
3034 return result.error;
3039 struct store_param {
3041 struct mailimap_set * set;
3042 struct mailimap_store_att_flags * store_att_flags;
3045 struct store_result {
3049 static void store_run(struct etpan_thread_op * op)
3051 struct store_param * param;
3052 struct store_result * result;
3056 result = op->result;
3060 r = mailimap_uid_store(param->imap, param->set,
3061 param->store_att_flags);
3065 debug_print("imap store run - end %i\n", r);
3068 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3069 struct mailimap_store_att_flags * store_att_flags)
3071 struct store_param param;
3072 struct store_result result;
3075 debug_print("imap store - begin\n");
3077 imap = get_imap(folder);
3080 param.store_att_flags = store_att_flags;
3082 threaded_run(folder, ¶m, &result, store_run);
3084 if (result.error != MAILIMAP_NO_ERROR)
3085 return result.error;
3087 debug_print("imap store - end\n");
3089 return result.error;
3093 #define ENV_BUFFER_SIZE 512
3095 static void do_exec_command(int fd, const char * command,
3096 const char * servername, uint16_t port)
3100 char env_buffer[ENV_BUFFER_SIZE];
3104 /* Fork again to become a child of init rather than
3105 the etpan client. */
3110 g_setenv("ETPANSERVER", servername, TRUE);
3112 g_unsetenv("ETPANSERVER");
3117 snprintf(porttext, sizeof(porttext), "%d", port);
3118 g_setenv("ETPANPORT", porttext, TRUE);
3121 g_unsetenv("ETPANPORT");
3124 /* Not a lot we can do if there's an error other than bail. */
3125 if (dup2(fd, 0) == -1)
3127 if (dup2(fd, 1) == -1)
3130 /* Should we close stderr and reopen /dev/null? */
3132 maxopen = sysconf(_SC_OPEN_MAX);
3133 for (i=3; i < maxopen; i++)
3137 /* Detach from the controlling tty if we have one. Otherwise,
3138 SSH might do something stupid like trying to use it instead
3139 of running $SSH_ASKPASS. Doh. */
3140 fd = open("/dev/tty", O_RDONLY);
3142 ioctl(fd, TIOCNOTTY, NULL);
3145 #endif /* TIOCNOTTY */
3147 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3149 /* Eep. Shouldn't reach this */
3153 static int subcommand_connect(const char *command,
3154 const char *servername, uint16_t port)
3156 /* SEB unsupported on Windows */
3160 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3165 do_exec_command(sockfds[1], command, servername, port);
3167 else if (childpid == -1) {
3175 /* Reap child, leaving grandchild process to run */
3176 waitpid(childpid, NULL, 0);
3181 static int socket_connect_cmd(mailimap * imap, const char * command,
3182 const char * server, int port)
3188 fd = subcommand_connect(command, server, port);
3190 return MAILIMAP_ERROR_STREAM;
3192 s = mailstream_socket_open(fd);
3195 return MAILIMAP_ERROR_STREAM;
3198 r = mailimap_connect(imap, s);
3199 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3200 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3201 mailstream_close(s);
3203 imap->imap_stream = NULL;
3212 struct connect_cmd_param {
3214 const char * command;
3215 const char * server;
3219 struct connect_cmd_result {
3223 static void connect_cmd_run(struct etpan_thread_op * op)
3226 struct connect_cmd_param * param;
3227 struct connect_cmd_result * result;
3230 result = op->result;
3234 r = socket_connect_cmd(param->imap, param->command,
3235 param->server, param->port);
3241 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3242 const char * server, int port)
3244 struct connect_cmd_param param;
3245 struct connect_cmd_result result;
3248 mailimap * imap, * oldimap;
3250 oldimap = get_imap(folder);
3252 imap = mailimap_new(0, NULL);
3255 debug_print("deleting old imap %p\n", oldimap);
3256 delete_imap(folder, oldimap);
3260 key.len = sizeof(folder);
3263 chash_set(session_hash, &key, &value, NULL);
3266 param.command = command;
3267 param.server = server;
3270 threaded_run(folder, ¶m, &result, connect_cmd_run);
3272 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3274 return result.error;
3276 #endif /* G_OS_WIN32 */
3278 void imap_threaded_cancel(Folder * folder)
3282 imap = get_imap(folder);
3283 if (imap->imap_stream != NULL)
3284 mailstream_cancel(imap->imap_stream);
3289 void imap_main_init(void)
3292 void imap_main_done(void)
3295 void imap_main_set_timeout(int sec)
3299 void imap_threaded_cancel(Folder * folder);