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>
39 #include "etpan-thread-manager.h"
41 #include "mainwindow.h"
42 #include "ssl_certificate.h"
44 #include "remotefolder.h"
46 #define DISABLE_LOG_DURING_LOGIN
48 static struct etpan_thread_manager * thread_manager = NULL;
49 static chash * courier_workaround_hash = NULL;
50 static chash * imap_hash = NULL;
51 static chash * session_hash = NULL;
52 static guint thread_manager_signal = 0;
53 static GIOChannel * io_channel = NULL;
55 static void delete_imap(Folder *folder, mailimap *imap)
61 key.len = sizeof(folder);
64 chash_delete(session_hash, &key, NULL);
67 key.len = sizeof(imap);
68 chash_delete(courier_workaround_hash, &key, NULL);
69 if (imap && imap->imap_stream) {
70 /* we don't want libetpan to logout */
71 mailstream_close(imap->imap_stream);
72 imap->imap_stream = NULL;
74 debug_print("removing mailimap %p\n", imap);
78 static gboolean thread_manager_event(GIOChannel * source,
79 GIOCondition condition,
82 etpan_thread_manager_loop(thread_manager);
87 static void imap_logger_noop(int direction, const char * str, size_t size)
92 static void imap_logger_cmd(int direction, const char * str, size_t size)
99 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
102 buf = malloc(size+1);
103 memset(buf, 0, size+1);
104 strncpy(buf, str, size);
107 if (!strncmp(buf, "<<<<<<<", 7)
108 || !strncmp(buf, ">>>>>>>", 7)) {
112 while (strstr(buf, "\r"))
113 *strstr(buf, "\r") = ' ';
114 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
115 buf[strlen(buf)-1] = '\0';
117 lines = g_strsplit(buf, "\n", -1);
119 while (lines[i] && *lines[i]) {
120 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
127 static void imap_logger_fetch(int direction, const char * str, size_t size)
133 if (size > 128 && !direction) {
134 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
138 buf = malloc(size+1);
139 memset(buf, 0, size+1);
140 strncpy(buf, str, size);
142 if (!strncmp(buf, "<<<<<<<", 7)
143 || !strncmp(buf, ">>>>>>>", 7)) {
147 while (strstr(buf, "\r"))
148 *strstr(buf, "\r") = ' ';
149 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
150 buf[strlen(buf)-1] = '\0';
152 lines = g_strsplit(buf, "\n", -1);
154 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
155 while (lines[i] && *lines[i]) {
156 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
160 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
166 static void imap_logger_uid(int direction, const char * str, size_t size)
173 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
176 buf = malloc(size+1);
177 memset(buf, 0, size+1);
178 strncpy(buf, str, size);
180 if (!strncmp(buf, "<<<<<<<", 7)
181 || !strncmp(buf, ">>>>>>>", 7)) {
185 while (strstr(buf, "\r"))
186 *strstr(buf, "\r") = ' ';
187 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
188 buf[strlen(buf)-1] = '\0';
190 lines = g_strsplit(buf, "\n", -1);
192 while (lines[i] && *lines[i]) {
193 int llen = strlen(lines[i]);
195 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
198 strncpy2(tmp, lines[i], 63);
199 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
208 static void imap_logger_append(int direction, const char * str, size_t size)
215 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
217 } else if (direction == 0 && size > 64) {
218 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
221 buf = malloc(size+1);
222 memset(buf, 0, size+1);
223 strncpy(buf, str, size);
225 if (!strncmp(buf, "<<<<<<<", 7)
226 || !strncmp(buf, ">>>>>>>", 7)) {
230 while (strstr(buf, "\r"))
231 *strstr(buf, "\r") = ' ';
232 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
233 buf[strlen(buf)-1] = '\0';
235 lines = g_strsplit(buf, "\n", -1);
237 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
238 while (lines[i] && *lines[i]) {
239 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
243 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
249 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
250 gboolean etpan_skip_ssl_cert_check = FALSE;
251 extern void mailsasl_ref(void);
253 void imap_main_init(gboolean skip_ssl_cert_check)
255 int fd_thread_manager;
257 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
258 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
259 mailstream_network_delay.tv_usec = 0;
261 mailstream_debug = 1;
262 mailstream_logger = imap_logger_cmd;
265 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
266 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
267 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
269 thread_manager = etpan_thread_manager_new();
271 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
273 io_channel = g_io_channel_unix_new(fd_thread_manager);
275 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
276 thread_manager_event,
281 void imap_main_set_timeout(int sec)
283 mailstream_network_delay.tv_sec = sec;
284 mailstream_network_delay.tv_usec = 0;
287 void imap_main_done(void)
289 etpan_thread_manager_stop(thread_manager);
290 etpan_thread_manager_join(thread_manager);
292 g_source_remove(thread_manager_signal);
293 g_io_channel_unref(io_channel);
295 etpan_thread_manager_free(thread_manager);
297 chash_free(courier_workaround_hash);
298 chash_free(session_hash);
299 chash_free(imap_hash);
302 void imap_init(Folder * folder)
304 struct etpan_thread * thread;
308 thread = etpan_thread_manager_get_thread(thread_manager);
311 key.len = sizeof(folder);
315 chash_set(imap_hash, &key, &value, NULL);
318 void imap_done(Folder * folder)
320 struct etpan_thread * thread;
326 key.len = sizeof(folder);
328 r = chash_get(imap_hash, &key, &value);
334 etpan_thread_unbind(thread);
336 chash_delete(imap_hash, &key, NULL);
338 debug_print("remove thread");
341 static struct etpan_thread * get_thread(Folder * folder)
343 struct etpan_thread * thread;
348 key.len = sizeof(folder);
350 chash_get(imap_hash, &key, &value);
356 static mailimap * get_imap(Folder * folder)
364 key.len = sizeof(folder);
366 r = chash_get(session_hash, &key, &value);
371 debug_print("found imap %p\n", imap);
376 static void generic_cb(int cancelled, void * result, void * callback_data)
378 struct etpan_thread_op * op;
380 op = (struct etpan_thread_op *) callback_data;
382 debug_print("generic_cb\n");
383 if (op->imap && op->imap->imap_response_info &&
384 op->imap->imap_response_info->rsp_alert) {
385 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
386 op->imap->imap_response_info->rsp_alert);
387 mainwindow_show_error();
392 static void threaded_run(Folder * folder, void * param, void * result,
393 void (* func)(struct etpan_thread_op * ))
395 struct etpan_thread_op * op;
396 struct etpan_thread * thread;
398 imap_folder_ref(folder);
400 op = etpan_thread_op_new();
402 op->imap = get_imap(folder);
408 op->callback = generic_cb;
409 op->callback_data = op;
414 thread = get_thread(folder);
415 etpan_thread_op_schedule(thread, op);
417 while (!op->finished) {
418 gtk_main_iteration();
421 etpan_thread_op_free(op);
423 imap_folder_unref(folder);
429 struct connect_param {
435 struct connect_result {
439 #define CHECK_IMAP() { \
440 if (!param->imap) { \
441 result->error = MAILIMAP_ERROR_BAD_STATE; \
446 static void connect_run(struct etpan_thread_op * op)
449 struct connect_param * param;
450 struct connect_result * result;
457 r = mailimap_socket_connect(param->imap,
458 param->server, param->port);
464 int imap_threaded_connect(Folder * folder, const char * server, int port)
466 struct connect_param param;
467 struct connect_result result;
470 mailimap * imap, * oldimap;
472 oldimap = get_imap(folder);
474 imap = mailimap_new(0, NULL);
477 debug_print("deleting old imap %p\n", oldimap);
478 delete_imap(folder, oldimap);
482 key.len = sizeof(folder);
485 chash_set(session_hash, &key, &value, NULL);
488 param.server = server;
492 threaded_run(folder, ¶m, &result, connect_run);
494 debug_print("connect ok %i with imap %p\n", result.error, imap);
499 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
502 struct connect_param *param = (struct connect_param *)data;
505 if (certificate == NULL || len < 0) {
506 g_warning("no cert presented.\n");
509 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
511 g_warning("IMAP: can't get cert\n");
513 } else if (ssl_certificate_check(cert, NULL,
514 (gchar *)param->server, (gushort)param->port) == TRUE) {
522 struct connect_param *param = (struct connect_param *)data;
523 gnutls_x509_crt cert = NULL;
526 if (certificate == NULL || len < 0) {
527 g_warning("no cert presented.\n");
531 tmp.data = malloc(len);
532 memcpy(tmp.data, certificate, len);
534 gnutls_x509_crt_init(&cert);
535 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
536 g_warning("IMAP: can't get cert\n");
538 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
539 (gchar *)param->server, (gushort)param->port) == TRUE) {
540 gnutls_x509_crt_deinit(cert);
543 gnutls_x509_crt_deinit(cert);
550 static void connect_ssl_run(struct etpan_thread_op * op)
553 struct connect_param * param;
554 struct connect_result * result;
561 r = mailimap_ssl_connect(param->imap,
562 param->server, param->port);
566 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
568 struct connect_param param;
569 struct connect_result result;
572 mailimap * imap, * oldimap;
573 unsigned char *certificate = NULL;
576 oldimap = get_imap(folder);
578 imap = mailimap_new(0, NULL);
581 debug_print("deleting old imap %p\n", oldimap);
582 delete_imap(folder, oldimap);
586 key.len = sizeof(folder);
589 chash_set(session_hash, &key, &value, NULL);
592 param.server = server;
596 threaded_run(folder, ¶m, &result, connect_ssl_run);
598 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
599 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
600 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
601 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
606 debug_print("connect %d with imap %p\n", result.error, imap);
617 struct mailimap_capability_data *caps;
620 static void capability_run(struct etpan_thread_op * op)
623 struct capa_param * param;
624 struct capa_result * result;
625 struct mailimap_capability_data *caps;
632 r = mailimap_capability(param->imap, &caps);
635 result->caps = (r == 0 ? caps : NULL);
639 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
641 struct capa_param param;
642 struct capa_result result;
645 imap = get_imap(folder);
649 threaded_run(folder, ¶m, &result, capability_run);
651 debug_print("capa %d\n", result.error);
660 struct disconnect_param {
664 struct disconnect_result {
668 static void disconnect_run(struct etpan_thread_op * op)
671 struct disconnect_param * param;
672 struct disconnect_result * result;
679 r = mailimap_logout(param->imap);
684 void imap_threaded_disconnect(Folder * folder)
686 struct connect_param param;
687 struct connect_result result;
690 imap = get_imap(folder);
692 debug_print("was disconnected\n");
698 threaded_run(folder, ¶m, &result, disconnect_run);
700 if (imap == get_imap(folder)) {
701 debug_print("deleting old imap %p\n", imap);
702 delete_imap(folder, imap);
704 debug_print("imap already deleted %p\n", imap);
707 debug_print("disconnect ok\n");
714 const char * wildcard;
723 static void list_run(struct etpan_thread_op * op)
725 struct list_param * param;
726 struct list_result * result;
737 if (param->base == NULL || param->wildcard == NULL) {
740 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
744 r = mailimap_lsub(param->imap, param->base,
745 param->wildcard, &list);
747 r = mailimap_list(param->imap, param->base,
748 param->wildcard, &list);
751 debug_print("imap list run - end\n");
754 int imap_threaded_list(Folder * folder, const char * base,
755 const char * wildcard,
758 struct list_param param;
759 struct list_result result;
761 debug_print("imap list - begin\n");
763 param.imap = get_imap(folder);
765 param.wildcard = wildcard;
766 param.sub_only = FALSE;
768 threaded_run(folder, ¶m, &result, list_run);
770 * p_result = result.list;
772 debug_print("imap list - end %p\n", result.list);
777 int imap_threaded_lsub(Folder * folder, const char * base,
778 const char * wildcard,
781 struct list_param param;
782 struct list_result result;
784 debug_print("imap lsub - begin\n");
786 param.imap = get_imap(folder);
788 param.wildcard = wildcard;
789 param.sub_only = TRUE;
791 threaded_run(folder, ¶m, &result, list_run);
793 * p_result = result.list;
795 debug_print("imap lsub - end %p\n", result.list);
800 struct subscribe_param {
806 struct subscribe_result {
810 static void subscribe_run(struct etpan_thread_op * op)
812 struct subscribe_param * param;
813 struct subscribe_result * result;
821 if (param->mb == NULL) {
823 debug_print("no mb\n");
826 if (param->subscribe)
827 r = mailimap_subscribe(param->imap, param->mb);
829 r = mailimap_unsubscribe(param->imap, param->mb);
831 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
834 int imap_threaded_subscribe(Folder * folder, const char * mb,
837 struct subscribe_param param;
838 struct subscribe_result result;
840 debug_print("imap list - begin\n");
842 param.imap = get_imap(folder);
844 param.subscribe = subscribe;
846 threaded_run(folder, ¶m, &result, subscribe_run);
854 const char * password;
859 struct login_result {
863 static void login_run(struct etpan_thread_op * op)
865 struct login_param * param;
866 struct login_result * result;
868 #ifdef DISABLE_LOG_DURING_LOGIN
877 #ifdef DISABLE_LOG_DURING_LOGIN
878 old_debug = mailstream_debug;
879 mailstream_debug = 0;
881 if (!strcmp(param->type, "LOGIN"))
882 r = mailimap_login(param->imap,
883 param->login, param->password);
884 else if (!strcmp(param->type, "GSSAPI"))
885 r = mailimap_authenticate(param->imap,
886 param->type, param->server, NULL, NULL,
887 param->login, param->login,
888 param->password, NULL);
890 r = mailimap_authenticate(param->imap,
891 param->type, NULL, NULL, NULL,
892 param->login, param->login,
893 param->password, NULL);
894 #ifdef DISABLE_LOG_DURING_LOGIN
895 mailstream_debug = old_debug;
899 debug_print("imap login run - end %i\n", r);
902 int imap_threaded_login(Folder * folder,
903 const char * login, const char * password,
906 struct login_param param;
907 struct login_result result;
909 debug_print("imap login - begin\n");
911 param.imap = get_imap(folder);
913 param.password = password;
915 if (folder && folder->account)
916 param.server = folder->account->recv_server;
920 threaded_run(folder, ¶m, &result, login_run);
922 debug_print("imap login - end\n");
928 struct status_param {
931 struct mailimap_status_att_list * status_att_list;
934 struct status_result {
936 struct mailimap_mailbox_data_status * data_status;
939 static void status_run(struct etpan_thread_op * op)
941 struct status_param * param;
942 struct status_result * result;
950 r = mailimap_status(param->imap, param->mb,
951 param->status_att_list,
952 &result->data_status);
955 debug_print("imap status run - end %i\n", r);
958 int imap_threaded_status(Folder * folder, const char * mb,
959 struct mailimap_mailbox_data_status ** data_status,
962 struct status_param param;
963 struct status_result result;
964 struct mailimap_status_att_list * status_att_list;
966 debug_print("imap status - begin\n");
968 status_att_list = mailimap_status_att_list_new_empty();
970 mailimap_status_att_list_add(status_att_list,
971 MAILIMAP_STATUS_ATT_MESSAGES);
974 mailimap_status_att_list_add(status_att_list,
975 MAILIMAP_STATUS_ATT_RECENT);
978 mailimap_status_att_list_add(status_att_list,
979 MAILIMAP_STATUS_ATT_UIDNEXT);
982 mailimap_status_att_list_add(status_att_list,
983 MAILIMAP_STATUS_ATT_UIDVALIDITY);
986 mailimap_status_att_list_add(status_att_list,
987 MAILIMAP_STATUS_ATT_UNSEEN);
989 param.imap = get_imap(folder);
991 param.status_att_list = status_att_list;
993 threaded_run(folder, ¶m, &result, status_run);
995 debug_print("imap status - end\n");
997 * data_status = result.data_status;
999 mailimap_status_att_list_free(status_att_list);
1001 return result.error;
1010 struct noop_result {
1014 static void noop_run(struct etpan_thread_op * op)
1016 struct noop_param * param;
1017 struct noop_result * result;
1021 result = op->result;
1025 r = mailimap_noop(param->imap);
1028 debug_print("imap noop run - end %i\n", r);
1031 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1032 unsigned int *p_recent,
1033 unsigned int *p_expunge,
1034 unsigned int *p_unseen,
1035 unsigned int *p_uidnext,
1036 unsigned int *p_uidval)
1038 struct noop_param param;
1039 struct noop_result result;
1042 debug_print("imap noop - begin\n");
1044 imap = get_imap(folder);
1047 threaded_run(folder, ¶m, &result, noop_run);
1049 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1050 * p_exists = imap->imap_selection_info->sel_exists;
1051 * p_recent = imap->imap_selection_info->sel_recent;
1052 * p_unseen = imap->imap_selection_info->sel_unseen;
1053 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1054 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1062 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1063 imap->imap_response_info->rsp_expunged != NULL) {
1064 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1068 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1069 *p_exists, *p_recent, *p_expunge, *p_unseen,
1070 *p_uidnext, *p_uidval);
1072 return result.error;
1076 struct starttls_result {
1080 static void starttls_run(struct etpan_thread_op * op)
1082 struct connect_param * param;
1083 struct starttls_result * result;
1087 result = op->result;
1091 r = mailimap_starttls(param->imap);
1094 debug_print("imap starttls run - end %i\n", r);
1097 mailimap *imap = param->imap;
1098 mailstream_low *plain_low = NULL;
1099 mailstream_low *tls_low = NULL;
1102 plain_low = mailstream_get_low(imap->imap_stream);
1103 fd = mailstream_low_get_fd(plain_low);
1105 debug_print("imap starttls run - can't get fd\n");
1106 result->error = MAILIMAP_ERROR_STREAM;
1110 tls_low = mailstream_low_tls_open(fd);
1111 if (tls_low == NULL) {
1112 debug_print("imap starttls run - can't tls_open\n");
1113 result->error = MAILIMAP_ERROR_STREAM;
1116 mailstream_low_free(plain_low);
1117 mailstream_set_low(imap->imap_stream, tls_low);
1121 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1123 struct connect_param param;
1124 struct starttls_result result;
1126 unsigned char *certificate;
1128 debug_print("imap starttls - begin\n");
1130 param.imap = get_imap(folder);
1131 param.server = host;
1134 threaded_run(folder, ¶m, &result, starttls_run);
1136 debug_print("imap starttls - end\n");
1138 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1139 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1140 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1141 result.error = MAILIMAP_ERROR_STREAM;
1145 return result.error;
1150 struct create_param {
1155 struct create_result {
1159 static void create_run(struct etpan_thread_op * op)
1161 struct create_param * param;
1162 struct create_result * result;
1166 result = op->result;
1170 r = mailimap_create(param->imap, param->mb);
1173 debug_print("imap create run - end %i\n", r);
1176 int imap_threaded_create(Folder * folder, const char * mb)
1178 struct create_param param;
1179 struct create_result result;
1181 debug_print("imap create - begin\n");
1183 param.imap = get_imap(folder);
1186 threaded_run(folder, ¶m, &result, create_run);
1188 debug_print("imap create - end\n");
1190 return result.error;
1196 struct rename_param {
1199 const char * new_name;
1202 struct rename_result {
1206 static void rename_run(struct etpan_thread_op * op)
1208 struct rename_param * param;
1209 struct rename_result * result;
1213 result = op->result;
1217 r = mailimap_rename(param->imap, param->mb, param->new_name);
1220 debug_print("imap rename run - end %i\n", r);
1223 int imap_threaded_rename(Folder * folder,
1224 const char * mb, const char * new_name)
1226 struct rename_param param;
1227 struct rename_result result;
1229 debug_print("imap rename - begin\n");
1231 param.imap = get_imap(folder);
1233 param.new_name = new_name;
1235 threaded_run(folder, ¶m, &result, rename_run);
1237 debug_print("imap rename - end\n");
1239 return result.error;
1245 struct delete_param {
1250 struct delete_result {
1254 static void delete_run(struct etpan_thread_op * op)
1256 struct delete_param * param;
1257 struct delete_result * result;
1261 result = op->result;
1265 r = mailimap_delete(param->imap, param->mb);
1268 debug_print("imap delete run - end %i\n", r);
1271 int imap_threaded_delete(Folder * folder, const char * mb)
1273 struct delete_param param;
1274 struct delete_result result;
1276 debug_print("imap delete - begin\n");
1278 param.imap = get_imap(folder);
1281 threaded_run(folder, ¶m, &result, delete_run);
1283 debug_print("imap delete - end\n");
1285 return result.error;
1290 struct select_param {
1295 struct select_result {
1299 static void select_run(struct etpan_thread_op * op)
1301 struct select_param * param;
1302 struct select_result * result;
1306 result = op->result;
1310 r = mailimap_select(param->imap, param->mb);
1313 debug_print("imap select run - end %i\n", r);
1316 int imap_threaded_select(Folder * folder, const char * mb,
1317 gint * exists, gint * recent, gint * unseen,
1318 guint32 * uid_validity,gint *can_create_flags)
1320 struct select_param param;
1321 struct select_result result;
1324 debug_print("imap select - begin\n");
1326 imap = get_imap(folder);
1330 threaded_run(folder, ¶m, &result, select_run);
1332 if (result.error != MAILIMAP_NO_ERROR)
1333 return result.error;
1335 if (!imap || imap->imap_selection_info == NULL)
1336 return MAILIMAP_ERROR_PARSE;
1338 * exists = imap->imap_selection_info->sel_exists;
1339 * recent = imap->imap_selection_info->sel_recent;
1340 * unseen = imap->imap_selection_info->sel_unseen;
1341 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1342 * can_create_flags = FALSE;
1344 if (imap->imap_selection_info->sel_perm_flags) {
1346 clist_begin(imap->imap_selection_info->sel_perm_flags);
1347 for (; cur && !(*can_create_flags); cur = clist_next(cur)) {
1348 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1349 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1350 *can_create_flags = TRUE;
1351 else if (flag->fl_flag &&
1352 flag->fl_flag->fl_type == 6 &&
1353 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1354 *can_create_flags = TRUE;
1358 debug_print("imap select - end\n");
1360 return result.error;
1363 static void close_run(struct etpan_thread_op * op)
1365 struct select_param * param;
1366 struct select_result * result;
1370 result = op->result;
1374 r = mailimap_close(param->imap);
1377 debug_print("imap close run - end %i\n", r);
1380 int imap_threaded_close(Folder * folder)
1382 struct select_param param;
1383 struct select_result result;
1386 debug_print("imap close - begin\n");
1388 imap = get_imap(folder);
1391 threaded_run(folder, ¶m, &result, close_run);
1393 if (result.error != MAILIMAP_NO_ERROR)
1394 return result.error;
1396 debug_print("imap close - end\n");
1398 return result.error;
1401 struct examine_param {
1406 struct examine_result {
1410 static void examine_run(struct etpan_thread_op * op)
1412 struct examine_param * param;
1413 struct examine_result * result;
1417 result = op->result;
1421 r = mailimap_examine(param->imap, param->mb);
1424 debug_print("imap examine run - end %i\n", r);
1427 int imap_threaded_examine(Folder * folder, const char * mb,
1428 gint * exists, gint * recent, gint * unseen,
1429 guint32 * uid_validity)
1431 struct examine_param param;
1432 struct examine_result result;
1435 debug_print("imap examine - begin\n");
1437 imap = get_imap(folder);
1441 threaded_run(folder, ¶m, &result, examine_run);
1443 if (result.error != MAILIMAP_NO_ERROR)
1444 return result.error;
1446 if (!imap || imap->imap_selection_info == NULL)
1447 return MAILIMAP_ERROR_PARSE;
1449 * exists = imap->imap_selection_info->sel_exists;
1450 * recent = imap->imap_selection_info->sel_recent;
1451 * unseen = imap->imap_selection_info->sel_unseen;
1452 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1454 debug_print("imap examine - end\n");
1456 return result.error;
1462 struct search_param {
1465 struct mailimap_set * set;
1468 struct search_result {
1470 clist * search_result;
1473 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1475 return mailimap_set_item_new(orig->set_first, orig->set_last);
1478 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1480 clist *list = orig ? orig->set_list : NULL;
1481 clist *newlist = clist_new();
1486 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1487 clist_append(newlist,
1488 sc_mailimap_set_item_copy(
1489 (struct mailimap_set_item *)clist_content(cur)));
1490 return mailimap_set_new(newlist);
1493 static void search_run(struct etpan_thread_op * op)
1495 struct search_param * param;
1496 struct search_result * result;
1498 struct mailimap_search_key * key = NULL;
1499 struct mailimap_search_key * uid_key = NULL;
1500 struct mailimap_search_key * search_type_key;
1501 clist * search_result;
1504 result = op->result;
1508 /* we copy the mailimap_set because freeing the key is recursive */
1509 if (param->set != NULL) {
1510 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1511 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1512 uid_key = mailimap_search_key_new_all();
1514 search_type_key = NULL;
1515 switch (param->type) {
1516 case IMAP_SEARCH_TYPE_SIMPLE:
1517 search_type_key = NULL;
1520 case IMAP_SEARCH_TYPE_SEEN:
1521 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1522 NULL, NULL, NULL, NULL, NULL,
1523 NULL, NULL, NULL, NULL, NULL,
1524 NULL, NULL, NULL, NULL, 0,
1525 NULL, NULL, NULL, NULL, NULL,
1526 NULL, 0, NULL, NULL, NULL);
1529 case IMAP_SEARCH_TYPE_UNSEEN:
1530 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1531 NULL, NULL, NULL, NULL, NULL,
1532 NULL, NULL, NULL, NULL, NULL,
1533 NULL, NULL, NULL, NULL, 0,
1534 NULL, NULL, NULL, NULL, NULL,
1535 NULL, 0, NULL, NULL, NULL);
1538 case IMAP_SEARCH_TYPE_ANSWERED:
1539 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1540 NULL, NULL, NULL, NULL, NULL,
1541 NULL, NULL, NULL, NULL, NULL,
1542 NULL, NULL, NULL, NULL, 0,
1543 NULL, NULL, NULL, NULL, NULL,
1544 NULL, 0, NULL, NULL, NULL);
1547 case IMAP_SEARCH_TYPE_FLAGGED:
1548 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1549 NULL, NULL, NULL, NULL, NULL,
1550 NULL, NULL, NULL, NULL, NULL,
1551 NULL, NULL, NULL, NULL, 0,
1552 NULL, NULL, NULL, NULL, NULL,
1553 NULL, 0, NULL, NULL, NULL);
1555 case IMAP_SEARCH_TYPE_DELETED:
1556 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1557 NULL, NULL, NULL, NULL, NULL,
1558 NULL, NULL, NULL, NULL, NULL,
1559 NULL, NULL, NULL, NULL, 0,
1560 NULL, NULL, NULL, NULL, NULL,
1561 NULL, 0, NULL, NULL, NULL);
1565 if (search_type_key != NULL) {
1566 if (param->set != NULL) {
1567 key = mailimap_search_key_new_multiple_empty();
1568 mailimap_search_key_multiple_add(key, search_type_key);
1569 mailimap_search_key_multiple_add(key, uid_key);
1571 key = search_type_key;
1573 } else if (uid_key != NULL) {
1578 g_warning("no key!");
1579 result = op->result;
1581 result->search_result = NULL;
1583 mailstream_logger = imap_logger_uid;
1585 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1587 mailstream_logger = imap_logger_cmd;
1589 /* free the key (with the imapset) */
1590 mailimap_search_key_free(key);
1593 result->search_result = search_result;
1595 debug_print("imap search run - end %i\n", result->error);
1598 int imap_threaded_search(Folder * folder, int search_type,
1599 struct mailimap_set * set, clist ** search_result)
1601 struct search_param param;
1602 struct search_result result;
1605 debug_print("imap search - begin\n");
1607 imap = get_imap(folder);
1610 param.type = search_type;
1612 threaded_run(folder, ¶m, &result, search_run);
1614 if (result.error != MAILIMAP_NO_ERROR)
1615 return result.error;
1617 debug_print("imap search - end\n");
1619 * search_result = result.search_result;
1621 return result.error;
1626 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1630 struct mailimap_msg_att_dynamic ** patt_dyn);
1633 result_to_uid_list(clist * fetch_result, carray ** result)
1640 tab = carray_new(128);
1642 res = MAILIMAP_ERROR_MEMORY;
1646 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1647 cur = clist_next(cur)) {
1648 struct mailimap_msg_att * msg_att;
1652 msg_att = clist_content(cur);
1655 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1657 puid = malloc(sizeof(* puid));
1659 res = MAILIMAP_ERROR_MEMORY;
1664 r = carray_add(tab, puid, NULL);
1667 res = MAILIMAP_ERROR_MEMORY;
1674 return MAILIMAP_NO_ERROR;
1677 imap_fetch_uid_list_free(tab);
1682 static int imap_get_messages_list(mailimap * imap,
1683 uint32_t first_index,
1688 struct mailimap_fetch_att * fetch_att;
1689 struct mailimap_fetch_type * fetch_type;
1690 struct mailimap_set * set;
1691 clist * fetch_result;
1694 set = mailimap_set_new_interval(first_index, 0);
1696 res = MAILIMAP_ERROR_MEMORY;
1700 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1701 if (fetch_type == NULL) {
1702 res = MAILIMAP_ERROR_MEMORY;
1706 fetch_att = mailimap_fetch_att_new_uid();
1707 if (fetch_att == NULL) {
1708 res = MAILIMAP_ERROR_MEMORY;
1709 goto free_fetch_type;
1712 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1713 if (r != MAILIMAP_NO_ERROR) {
1714 mailimap_fetch_att_free(fetch_att);
1715 res = MAILIMAP_ERROR_MEMORY;
1716 goto free_fetch_type;
1719 mailstream_logger = imap_logger_fetch;
1721 r = mailimap_uid_fetch(imap, set,
1722 fetch_type, &fetch_result);
1724 mailstream_logger = imap_logger_cmd;
1725 mailimap_fetch_type_free(fetch_type);
1726 mailimap_set_free(set);
1728 if (r != MAILIMAP_NO_ERROR) {
1734 r = result_to_uid_list(fetch_result, &env_list);
1735 mailimap_fetch_list_free(fetch_result);
1737 * result = env_list;
1739 return MAILIMAP_NO_ERROR;
1742 mailimap_fetch_type_free(fetch_type);
1744 mailimap_set_free(set);
1752 struct fetch_uid_param {
1754 uint32_t first_index;
1757 struct fetch_uid_result {
1759 carray * fetch_result;
1762 static void fetch_uid_run(struct etpan_thread_op * op)
1764 struct fetch_uid_param * param;
1765 struct fetch_uid_result * result;
1766 carray * fetch_result;
1770 result = op->result;
1774 fetch_result = NULL;
1775 mailstream_logger = imap_logger_noop;
1776 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1778 r = imap_get_messages_list(param->imap, param->first_index,
1781 mailstream_logger = imap_logger_cmd;
1784 result->fetch_result = fetch_result;
1785 debug_print("imap fetch_uid run - end %i\n", r);
1788 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1789 carray ** fetch_result)
1791 struct fetch_uid_param param;
1792 struct fetch_uid_result result;
1795 debug_print("imap fetch_uid - begin\n");
1797 imap = get_imap(folder);
1799 param.first_index = first_index;
1801 threaded_run(folder, ¶m, &result, fetch_uid_run);
1803 if (result.error != MAILIMAP_NO_ERROR)
1804 return result.error;
1806 debug_print("imap fetch_uid - end\n");
1808 * fetch_result = result.fetch_result;
1810 return result.error;
1814 void imap_fetch_uid_list_free(carray * uid_list)
1818 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1821 puid = carray_get(uid_list, i);
1824 carray_free(uid_list);
1830 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1833 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1839 GSList *tags = NULL;
1841 tab = carray_new(128);
1843 res = MAILIMAP_ERROR_MEMORY;
1847 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1848 cur = clist_next(cur)) {
1849 struct mailimap_msg_att * msg_att;
1852 struct mailimap_msg_att_dynamic * att_dyn;
1858 msg_att = clist_content(cur);
1862 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1865 if (att_dyn == NULL)
1868 flags = imap_flags_to_flags(att_dyn, &tags);
1870 puid = malloc(sizeof(* puid));
1872 res = MAILIMAP_ERROR_MEMORY;
1877 r = carray_add(tab, puid, NULL);
1880 res = MAILIMAP_ERROR_MEMORY;
1883 pflags = malloc(sizeof(* pflags));
1884 if (pflags == NULL) {
1885 res = MAILIMAP_ERROR_MEMORY;
1889 r = carray_add(tab, pflags, NULL);
1892 res = MAILIMAP_ERROR_MEMORY;
1895 r = carray_add(tab, tags, NULL);
1898 res = MAILIMAP_ERROR_MEMORY;
1905 return MAILIMAP_NO_ERROR;
1908 imap_fetch_uid_flags_list_free(tab);
1909 slist_free_strings(tags);
1915 static int imap_get_messages_flags_list(mailimap * imap,
1916 uint32_t first_index,
1921 struct mailimap_fetch_att * fetch_att;
1922 struct mailimap_fetch_type * fetch_type;
1923 struct mailimap_set * set;
1924 clist * fetch_result;
1927 set = mailimap_set_new_interval(first_index, 0);
1929 res = MAILIMAP_ERROR_MEMORY;
1933 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1934 if (fetch_type == NULL) {
1935 res = MAILIMAP_ERROR_MEMORY;
1939 fetch_att = mailimap_fetch_att_new_flags();
1940 if (fetch_att == NULL) {
1941 res = MAILIMAP_ERROR_MEMORY;
1942 goto free_fetch_type;
1945 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1946 if (r != MAILIMAP_NO_ERROR) {
1947 mailimap_fetch_att_free(fetch_att);
1948 res = MAILIMAP_ERROR_MEMORY;
1949 goto free_fetch_type;
1952 fetch_att = mailimap_fetch_att_new_uid();
1953 if (fetch_att == NULL) {
1954 res = MAILIMAP_ERROR_MEMORY;
1955 goto free_fetch_type;
1958 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1959 if (r != MAILIMAP_NO_ERROR) {
1960 mailimap_fetch_att_free(fetch_att);
1961 res = MAILIMAP_ERROR_MEMORY;
1962 goto free_fetch_type;
1965 mailstream_logger = imap_logger_fetch;
1967 r = mailimap_uid_fetch(imap, set,
1968 fetch_type, &fetch_result);
1970 mailstream_logger = imap_logger_cmd;
1971 mailimap_fetch_type_free(fetch_type);
1972 mailimap_set_free(set);
1974 if (r != MAILIMAP_NO_ERROR) {
1980 r = result_to_uid_flags_list(fetch_result, &env_list);
1981 mailimap_fetch_list_free(fetch_result);
1983 * result = env_list;
1985 return MAILIMAP_NO_ERROR;
1988 mailimap_fetch_type_free(fetch_type);
1990 mailimap_set_free(set);
1997 static void fetch_uid_flags_run(struct etpan_thread_op * op)
1999 struct fetch_uid_param * param;
2000 struct fetch_uid_result * result;
2001 carray * fetch_result;
2005 result = op->result;
2009 fetch_result = NULL;
2010 r = imap_get_messages_flags_list(param->imap, param->first_index,
2014 result->fetch_result = fetch_result;
2015 debug_print("imap fetch_uid run - end %i\n", r);
2018 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2019 carray ** fetch_result)
2021 struct fetch_uid_param param;
2022 struct fetch_uid_result result;
2025 debug_print("imap fetch_uid - begin\n");
2027 imap = get_imap(folder);
2029 param.first_index = first_index;
2031 mailstream_logger = imap_logger_noop;
2032 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2034 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2036 mailstream_logger = imap_logger_cmd;
2039 if (result.error != MAILIMAP_NO_ERROR)
2040 return result.error;
2042 debug_print("imap fetch_uid - end\n");
2044 * fetch_result = result.fetch_result;
2046 return result.error;
2050 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2054 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2057 data = carray_get(uid_flags_list, i);
2059 data = carray_get(uid_flags_list, i + 1);
2062 carray_free(uid_flags_list);
2067 static int imap_fetch(mailimap * imap,
2070 size_t * result_len)
2073 struct mailimap_set * set;
2074 struct mailimap_fetch_att * fetch_att;
2075 struct mailimap_fetch_type * fetch_type;
2076 clist * fetch_result;
2077 struct mailimap_msg_att * msg_att;
2078 struct mailimap_msg_att_item * msg_att_item;
2083 struct mailimap_section * section;
2085 set = mailimap_set_new_single(msg_index);
2087 res = MAILIMAP_ERROR_MEMORY;
2091 section = mailimap_section_new(NULL);
2092 if (section == NULL) {
2093 res = MAILIMAP_ERROR_MEMORY;
2097 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2098 if (fetch_att == NULL) {
2099 mailimap_section_free(section);
2100 res = MAILIMAP_ERROR_MEMORY;
2104 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2105 if (fetch_type == NULL) {
2106 res = MAILIMAP_ERROR_MEMORY;
2107 goto free_fetch_att;
2110 mailstream_logger = imap_logger_fetch;
2112 r = mailimap_uid_fetch(imap, set,
2113 fetch_type, &fetch_result);
2115 mailstream_logger = imap_logger_cmd;
2117 mailimap_fetch_type_free(fetch_type);
2118 mailimap_set_free(set);
2121 case MAILIMAP_NO_ERROR:
2127 if (clist_begin(fetch_result) == NULL) {
2128 mailimap_fetch_list_free(fetch_result);
2129 return MAILIMAP_ERROR_FETCH;
2132 msg_att = clist_begin(fetch_result)->data;
2137 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2138 cur = clist_next(cur)) {
2139 msg_att_item = clist_content(cur);
2141 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2142 if (msg_att_item->att_data.att_static->att_type ==
2143 MAILIMAP_MSG_ATT_BODY_SECTION) {
2144 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2146 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2148 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2153 mailimap_fetch_list_free(fetch_result);
2156 return MAILIMAP_ERROR_FETCH;
2159 * result_len = text_length;
2161 return MAILIMAP_NO_ERROR;
2164 mailimap_fetch_att_free(fetch_att);
2166 mailimap_set_free(set);
2171 static int imap_fetch_header(mailimap * imap,
2174 size_t * result_len)
2177 struct mailimap_set * set;
2178 struct mailimap_fetch_att * fetch_att;
2179 struct mailimap_fetch_type * fetch_type;
2180 clist * fetch_result;
2181 struct mailimap_msg_att * msg_att;
2182 struct mailimap_msg_att_item * msg_att_item;
2187 struct mailimap_section * section;
2189 set = mailimap_set_new_single(msg_index);
2191 res = MAILIMAP_ERROR_MEMORY;
2195 section = mailimap_section_new_header();
2196 if (section == NULL) {
2197 res = MAILIMAP_ERROR_MEMORY;
2201 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2202 if (fetch_att == NULL) {
2203 mailimap_section_free(section);
2204 res = MAILIMAP_ERROR_MEMORY;
2208 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2209 if (fetch_type == NULL) {
2210 res = MAILIMAP_ERROR_MEMORY;
2211 goto free_fetch_att;
2214 mailstream_logger = imap_logger_fetch;
2216 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2218 mailstream_logger = imap_logger_cmd;
2219 mailimap_fetch_type_free(fetch_type);
2220 mailimap_set_free(set);
2223 case MAILIMAP_NO_ERROR:
2229 if (clist_begin(fetch_result) == NULL) {
2230 mailimap_fetch_list_free(fetch_result);
2231 return MAILIMAP_ERROR_FETCH;
2234 msg_att = clist_begin(fetch_result)->data;
2239 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2240 cur = clist_next(cur)) {
2241 msg_att_item = clist_content(cur);
2243 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2244 if (msg_att_item->att_data.att_static->att_type ==
2245 MAILIMAP_MSG_ATT_BODY_SECTION) {
2246 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2247 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2249 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2254 mailimap_fetch_list_free(fetch_result);
2257 return MAILIMAP_ERROR_FETCH;
2260 * result_len = text_length;
2262 return MAILIMAP_NO_ERROR;
2265 mailimap_fetch_att_free(fetch_att);
2267 mailimap_set_free(set);
2274 struct fetch_content_param {
2277 const char * filename;
2281 struct fetch_content_result {
2285 static void fetch_content_run(struct etpan_thread_op * op)
2287 struct fetch_content_param * param;
2288 struct fetch_content_result * result;
2290 size_t content_size;
2296 result = op->result;
2302 if (param->with_body)
2303 r = imap_fetch(param->imap, param->msg_index,
2304 &content, &content_size);
2306 r = imap_fetch_header(param->imap, param->msg_index,
2307 &content, &content_size);
2311 if (r == MAILIMAP_NO_ERROR) {
2312 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2314 result->error = MAILIMAP_ERROR_FETCH;
2318 f = fdopen(fd, "wb");
2320 result->error = MAILIMAP_ERROR_FETCH;
2324 r = fwrite(content, 1, content_size, f);
2325 if (r < content_size) {
2341 g_unlink(param->filename);
2344 if (mmap_string_unref(content) != 0)
2348 debug_print("imap fetch_content run - end %i\n", r);
2351 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2353 const char * filename)
2355 struct fetch_content_param param;
2356 struct fetch_content_result result;
2359 debug_print("imap fetch_content - begin\n");
2361 imap = get_imap(folder);
2363 param.msg_index = msg_index;
2364 param.filename = filename;
2365 param.with_body = with_body;
2367 threaded_run(folder, ¶m, &result, fetch_content_run);
2369 if (result.error != MAILIMAP_NO_ERROR)
2370 return result.error;
2372 debug_print("imap fetch_content - end\n");
2374 return result.error;
2379 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2384 GSList *tags = NULL;
2388 flag_list = att_dyn->att_list;
2389 if (flag_list == NULL)
2392 for(cur = clist_begin(flag_list) ; cur != NULL ;
2393 cur = clist_next(cur)) {
2394 struct mailimap_flag_fetch * flag_fetch;
2396 flag_fetch = clist_content(cur);
2397 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2400 switch (flag_fetch->fl_flag->fl_type) {
2401 case MAILIMAP_FLAG_ANSWERED:
2402 flags |= MSG_REPLIED;
2404 case MAILIMAP_FLAG_FLAGGED:
2405 flags |= MSG_MARKED;
2407 case MAILIMAP_FLAG_DELETED:
2408 flags |= MSG_DELETED;
2410 case MAILIMAP_FLAG_SEEN:
2411 flags &= ~MSG_UNREAD;
2414 case MAILIMAP_FLAG_KEYWORD:
2416 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2426 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2430 struct mailimap_msg_att_dynamic ** patt_dyn)
2432 clistiter * item_cur;
2436 struct mailimap_msg_att_dynamic * att_dyn;
2443 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2444 item_cur = clist_next(item_cur)) {
2445 struct mailimap_msg_att_item * item;
2447 item = clist_content(item_cur);
2449 switch (item->att_type) {
2450 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2451 switch (item->att_data.att_static->att_type) {
2452 case MAILIMAP_MSG_ATT_UID:
2453 uid = item->att_data.att_static->att_data.att_uid;
2456 case MAILIMAP_MSG_ATT_BODY_SECTION:
2457 if (headers == NULL) {
2458 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2461 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2462 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2467 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2468 if (att_dyn == NULL) {
2469 att_dyn = item->att_data.att_dyn;
2477 if (pheaders != NULL)
2478 * pheaders = headers;
2479 if (pref_size != NULL)
2480 * pref_size = ref_size;
2481 if (patt_dyn != NULL)
2482 * patt_dyn = att_dyn;
2484 return MAIL_NO_ERROR;
2487 static struct imap_fetch_env_info *
2488 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2490 struct imap_fetch_env_info * info;
2494 struct mailimap_msg_att_dynamic * att_dyn;
2496 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2501 info = malloc(sizeof(* info));
2503 info->headers = strdup(headers);
2505 info->flags = imap_flags_to_flags(att_dyn, tags);
2511 imap_fetch_result_to_envelop_list(clist * fetch_result,
2512 carray ** p_env_list)
2519 env_list = carray_new(16);
2521 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2522 cur = clist_next(cur)) {
2523 struct mailimap_msg_att * msg_att;
2524 struct imap_fetch_env_info * env_info;
2525 GSList *tags = NULL;
2527 msg_att = clist_content(cur);
2529 env_info = fetch_to_env_info(msg_att, &tags);
2531 return MAILIMAP_ERROR_MEMORY;
2532 carray_add(env_list, env_info, NULL);
2533 carray_add(env_list, tags, NULL);
2536 * p_env_list = env_list;
2538 return MAIL_NO_ERROR;
2541 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2543 struct mailimap_fetch_att * fetch_att;
2547 struct mailimap_header_list * imap_hdrlist;
2548 struct mailimap_section * section;
2550 hdrlist = clist_new();
2552 header = strdup("Date");
2553 r = clist_append(hdrlist, header);
2554 header = strdup("From");
2555 r = clist_append(hdrlist, header);
2556 header = strdup("To");
2557 r = clist_append(hdrlist, header);
2558 header = strdup("Cc");
2559 r = clist_append(hdrlist, header);
2560 header = strdup("Subject");
2561 r = clist_append(hdrlist, header);
2562 header = strdup("Message-ID");
2563 r = clist_append(hdrlist, header);
2564 header = strdup("References");
2565 r = clist_append(hdrlist, header);
2566 header = strdup("In-Reply-To");
2567 r = clist_append(hdrlist, header);
2569 imap_hdrlist = mailimap_header_list_new(hdrlist);
2570 section = mailimap_section_new_header_fields(imap_hdrlist);
2571 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2572 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2574 return MAIL_NO_ERROR;
2577 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2579 struct mailimap_fetch_att * fetch_att;
2580 struct mailimap_section * section;
2582 section = mailimap_section_new_header();
2583 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2584 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2586 return MAIL_NO_ERROR;
2590 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2591 carray ** p_env_list)
2593 struct mailimap_fetch_att * fetch_att;
2594 struct mailimap_fetch_type * fetch_type;
2596 clist * fetch_result;
2598 carray * env_list = NULL;
2602 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2605 fetch_att = mailimap_fetch_att_new_uid();
2606 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2609 fetch_att = mailimap_fetch_att_new_flags();
2610 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2613 fetch_att = mailimap_fetch_att_new_rfc822_size();
2614 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2618 key.len = sizeof(imap);
2619 r = chash_get(courier_workaround_hash, &key, &value);
2621 r = imap_add_envelope_fetch_att(fetch_type);
2623 r = imap_add_header_fetch_att(fetch_type);
2625 mailstream_logger = imap_logger_fetch;
2627 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2629 mailstream_logger = imap_logger_cmd;
2631 case MAILIMAP_NO_ERROR:
2634 mailimap_fetch_type_free(fetch_type);
2635 debug_print("uid_fetch: %d\n", r);
2639 if (clist_begin(fetch_result) == NULL) {
2640 res = MAILIMAP_ERROR_FETCH;
2641 debug_print("clist_begin = NULL\n");
2645 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2646 mailimap_fetch_list_free(fetch_result);
2648 if (r != MAILIMAP_NO_ERROR) {
2649 mailimap_fetch_type_free(fetch_type);
2650 res = MAILIMAP_ERROR_MEMORY;
2651 debug_print("fetch_result_to_envelop_list: %d\n", res);
2655 mailimap_fetch_type_free(fetch_type);
2657 * p_env_list = env_list;
2659 return MAILIMAP_NO_ERROR;
2665 struct fetch_env_param {
2667 struct mailimap_set * set;
2670 struct fetch_env_result {
2671 carray * fetch_env_result;
2675 static void fetch_env_run(struct etpan_thread_op * op)
2677 struct fetch_env_param * param;
2678 struct fetch_env_result * result;
2683 result = op->result;
2688 r = imap_get_envelopes_list(param->imap, param->set,
2692 result->fetch_env_result = env_list;
2694 debug_print("imap fetch_env run - end %i\n", r);
2697 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2698 carray ** p_env_list)
2700 struct fetch_env_param param;
2701 struct fetch_env_result result;
2704 debug_print("imap fetch_env - begin\n");
2706 imap = get_imap(folder);
2710 threaded_run(folder, ¶m, &result, fetch_env_run);
2712 if (result.error != MAILIMAP_NO_ERROR) {
2718 key.len = sizeof(imap);
2719 r = chash_get(courier_workaround_hash, &key, &value);
2723 chash_set(courier_workaround_hash, &key, &value, NULL);
2725 threaded_run(folder, ¶m, &result, fetch_env_run);
2729 if (result.error != MAILIMAP_NO_ERROR)
2730 return result.error;
2732 debug_print("imap fetch_env - end\n");
2734 * p_env_list = result.fetch_env_result;
2736 return result.error;
2739 void imap_fetch_env_free(carray * env_list)
2743 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2744 struct imap_fetch_env_info * env_info;
2746 env_info = carray_get(env_list, i);
2747 free(env_info->headers);
2750 carray_free(env_list);
2757 struct append_param {
2759 const char * mailbox;
2760 const char * filename;
2761 struct mailimap_flag_list * flag_list;
2764 struct append_result {
2769 static void append_run(struct etpan_thread_op * op)
2771 struct append_param * param;
2772 struct append_result * result;
2776 struct stat stat_buf;
2778 guint32 uid = 0, val = 0;
2781 result = op->result;
2785 r = stat(param->filename, &stat_buf);
2787 result->error = MAILIMAP_ERROR_APPEND;
2790 size = stat_buf.st_size;
2792 fd = open(param->filename, O_RDONLY);
2794 result->error = MAILIMAP_ERROR_APPEND;
2798 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2799 if (data == (void *) MAP_FAILED) {
2801 result->error = MAILIMAP_ERROR_APPEND;
2805 mailstream_logger = imap_logger_append;
2807 r = mailimap_uidplus_append(param->imap, param->mailbox,
2808 param->flag_list, NULL,
2809 data, size, &val, &uid);
2811 mailstream_logger = imap_logger_cmd;
2818 debug_print("imap append run - end %i uid %d\n", r, uid);
2821 int imap_threaded_append(Folder * folder, const char * mailbox,
2822 const char * filename,
2823 struct mailimap_flag_list * flag_list,
2826 struct append_param param;
2827 struct append_result result;
2830 debug_print("imap append - begin\n");
2832 imap = get_imap(folder);
2834 param.mailbox = mailbox;
2835 param.filename = filename;
2836 param.flag_list = flag_list;
2838 threaded_run(folder, ¶m, &result, append_run);
2840 if (result.error != MAILIMAP_NO_ERROR)
2841 return result.error;
2843 debug_print("imap append - end\n");
2847 return result.error;
2853 struct expunge_param {
2857 struct expunge_result {
2861 static void expunge_run(struct etpan_thread_op * op)
2863 struct expunge_param * param;
2864 struct expunge_result * result;
2868 result = op->result;
2872 r = mailimap_expunge(param->imap);
2875 debug_print("imap expunge run - end %i\n", r);
2878 int imap_threaded_expunge(Folder * folder)
2880 struct expunge_param param;
2881 struct expunge_result result;
2883 debug_print("imap expunge - begin\n");
2885 param.imap = get_imap(folder);
2887 threaded_run(folder, ¶m, &result, expunge_run);
2889 debug_print("imap expunge - end\n");
2891 return result.error;
2897 struct mailimap_set * set;
2901 struct copy_result {
2903 struct mailimap_set *source;
2904 struct mailimap_set *dest;
2907 static void copy_run(struct etpan_thread_op * op)
2909 struct copy_param * param;
2910 struct copy_result * result;
2913 struct mailimap_set *source = NULL, *dest = NULL;
2916 result = op->result;
2920 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2921 &val, &source, &dest);
2925 result->source = source;
2926 result->dest = dest;
2928 result->source = NULL;
2929 result->dest = NULL;
2931 debug_print("imap copy run - end %i\n", r);
2934 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2935 const char * mb, struct mailimap_set **source,
2936 struct mailimap_set **dest)
2938 struct copy_param param;
2939 struct copy_result result;
2942 debug_print("imap copy - begin\n");
2944 imap = get_imap(folder);
2949 threaded_run(folder, ¶m, &result, copy_run);
2953 if (result.error != MAILIMAP_NO_ERROR)
2954 return result.error;
2956 *source = result.source;
2957 *dest = result.dest;
2959 debug_print("imap copy - end\n");
2961 return result.error;
2966 struct store_param {
2968 struct mailimap_set * set;
2969 struct mailimap_store_att_flags * store_att_flags;
2972 struct store_result {
2976 static void store_run(struct etpan_thread_op * op)
2978 struct store_param * param;
2979 struct store_result * result;
2983 result = op->result;
2987 r = mailimap_uid_store(param->imap, param->set,
2988 param->store_att_flags);
2992 debug_print("imap store run - end %i\n", r);
2995 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2996 struct mailimap_store_att_flags * store_att_flags)
2998 struct store_param param;
2999 struct store_result result;
3002 debug_print("imap store - begin\n");
3004 imap = get_imap(folder);
3007 param.store_att_flags = store_att_flags;
3009 threaded_run(folder, ¶m, &result, store_run);
3011 if (result.error != MAILIMAP_NO_ERROR)
3012 return result.error;
3014 debug_print("imap store - end\n");
3016 return result.error;
3020 #define ENV_BUFFER_SIZE 512
3022 static void do_exec_command(int fd, const char * command,
3023 const char * servername, uint16_t port)
3027 char env_buffer[ENV_BUFFER_SIZE];
3031 /* Fork again to become a child of init rather than
3032 the etpan client. */
3038 snprintf(env_buffer, ENV_BUFFER_SIZE,
3039 "ETPANSERVER=%s", servername);
3041 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
3045 setenv("ETPANSERVER", servername, 1);
3047 unsetenv("ETPANSERVER");
3052 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
3054 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
3060 snprintf(porttext, sizeof(porttext), "%d", port);
3061 setenv("ETPANPORT", porttext, 1);
3064 unsetenv("ETPANPORT");
3068 /* Not a lot we can do if there's an error other than bail. */
3069 if (dup2(fd, 0) == -1)
3071 if (dup2(fd, 1) == -1)
3074 /* Should we close stderr and reopen /dev/null? */
3076 maxopen = sysconf(_SC_OPEN_MAX);
3077 for (i=3; i < maxopen; i++)
3081 /* Detach from the controlling tty if we have one. Otherwise,
3082 SSH might do something stupid like trying to use it instead
3083 of running $SSH_ASKPASS. Doh. */
3084 fd = open("/dev/tty", O_RDONLY);
3086 ioctl(fd, TIOCNOTTY, NULL);
3089 #endif /* TIOCNOTTY */
3091 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3093 /* Eep. Shouldn't reach this */
3097 static int subcommand_connect(const char *command,
3098 const char *servername, uint16_t port)
3100 /* SEB unsupported on Windows */
3104 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3109 do_exec_command(sockfds[1], command, servername, port);
3111 else if (childpid == -1) {
3119 /* Reap child, leaving grandchild process to run */
3120 waitpid(childpid, NULL, 0);
3125 static int socket_connect_cmd(mailimap * imap, const char * command,
3126 const char * server, int port)
3132 fd = subcommand_connect(command, server, port);
3134 return MAILIMAP_ERROR_STREAM;
3136 s = mailstream_socket_open(fd);
3139 return MAILIMAP_ERROR_STREAM;
3142 r = mailimap_connect(imap, s);
3143 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3144 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3145 mailstream_close(s);
3147 imap->imap_stream = NULL;
3156 struct connect_cmd_param {
3158 const char * command;
3159 const char * server;
3163 struct connect_cmd_result {
3167 static void connect_cmd_run(struct etpan_thread_op * op)
3170 struct connect_cmd_param * param;
3171 struct connect_cmd_result * result;
3174 result = op->result;
3178 r = socket_connect_cmd(param->imap, param->command,
3179 param->server, param->port);
3185 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3186 const char * server, int port)
3188 struct connect_cmd_param param;
3189 struct connect_cmd_result result;
3192 mailimap * imap, * oldimap;
3194 oldimap = get_imap(folder);
3196 imap = mailimap_new(0, NULL);
3199 debug_print("deleting old imap %p\n", oldimap);
3200 delete_imap(folder, oldimap);
3204 key.len = sizeof(folder);
3207 chash_set(session_hash, &key, &value, NULL);
3210 param.command = command;
3211 param.server = server;
3214 threaded_run(folder, ¶m, &result, connect_cmd_run);
3216 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3218 return result.error;
3221 void imap_threaded_cancel(Folder * folder)
3225 imap = get_imap(folder);
3226 if (imap->imap_stream != NULL)
3227 mailstream_cancel(imap->imap_stream);
3232 void imap_main_init(void)
3235 void imap_main_done(void)
3238 void imap_main_set_timeout(int sec)
3242 void imap_threaded_cancel(Folder * folder);