2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "imap-thread.h"
28 #include <sys/types.h>
30 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
31 #include <sys/socket.h>
40 #include "etpan-thread-manager.h"
42 #include "mainwindow.h"
43 #include "ssl_certificate.h"
45 #include "remotefolder.h"
47 #define DISABLE_LOG_DURING_LOGIN
49 static struct etpan_thread_manager * thread_manager = NULL;
50 static chash * courier_workaround_hash = NULL;
51 static chash * imap_hash = NULL;
52 static chash * session_hash = NULL;
53 static guint thread_manager_signal = 0;
54 static GIOChannel * io_channel = NULL;
56 static void delete_imap(Folder *folder, mailimap *imap)
62 key.len = sizeof(folder);
65 chash_delete(session_hash, &key, NULL);
68 key.len = sizeof(imap);
69 chash_delete(courier_workaround_hash, &key, NULL);
70 if (imap && imap->imap_stream) {
71 /* we don't want libetpan to logout */
72 mailstream_close(imap->imap_stream);
73 imap->imap_stream = NULL;
75 debug_print("removing mailimap %p\n", imap);
79 static gboolean thread_manager_event(GIOChannel * source,
80 GIOCondition condition,
83 etpan_thread_manager_loop(thread_manager);
88 static void imap_logger_noop(int direction, const char * str, size_t size)
93 static void imap_logger_cmd(int direction, const char * str, size_t size)
100 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
103 buf = malloc(size+1);
104 memset(buf, 0, size+1);
105 strncpy(buf, str, size);
108 if (!strncmp(buf, "<<<<<<<", 7)
109 || !strncmp(buf, ">>>>>>>", 7)) {
113 while (strstr(buf, "\r"))
114 *strstr(buf, "\r") = ' ';
115 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
116 buf[strlen(buf)-1] = '\0';
118 lines = g_strsplit(buf, "\n", -1);
120 while (lines[i] && *lines[i]) {
121 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
128 static void imap_logger_fetch(int direction, const char * str, size_t size)
134 if (size > 128 && !direction) {
135 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
139 buf = malloc(size+1);
140 memset(buf, 0, size+1);
141 strncpy(buf, str, size);
143 if (!strncmp(buf, "<<<<<<<", 7)
144 || !strncmp(buf, ">>>>>>>", 7)) {
148 while (strstr(buf, "\r"))
149 *strstr(buf, "\r") = ' ';
150 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
151 buf[strlen(buf)-1] = '\0';
153 lines = g_strsplit(buf, "\n", -1);
155 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
156 while (lines[i] && *lines[i]) {
157 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
161 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
167 static void imap_logger_uid(int direction, const char * str, size_t size)
174 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
177 buf = malloc(size+1);
178 memset(buf, 0, size+1);
179 strncpy(buf, str, size);
181 if (!strncmp(buf, "<<<<<<<", 7)
182 || !strncmp(buf, ">>>>>>>", 7)) {
186 while (strstr(buf, "\r"))
187 *strstr(buf, "\r") = ' ';
188 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
189 buf[strlen(buf)-1] = '\0';
191 lines = g_strsplit(buf, "\n", -1);
193 while (lines[i] && *lines[i]) {
194 int llen = strlen(lines[i]);
196 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
199 strncpy2(tmp, lines[i], 63);
200 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
209 static void imap_logger_append(int direction, const char * str, size_t size)
216 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
218 } else if (direction == 0 && size > 64) {
219 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
222 buf = malloc(size+1);
223 memset(buf, 0, size+1);
224 strncpy(buf, str, size);
226 if (!strncmp(buf, "<<<<<<<", 7)
227 || !strncmp(buf, ">>>>>>>", 7)) {
231 while (strstr(buf, "\r"))
232 *strstr(buf, "\r") = ' ';
233 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
234 buf[strlen(buf)-1] = '\0';
236 lines = g_strsplit(buf, "\n", -1);
238 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
239 while (lines[i] && *lines[i]) {
240 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
244 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
250 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
251 gboolean etpan_skip_ssl_cert_check = FALSE;
252 extern void mailsasl_ref(void);
254 void imap_main_init(gboolean skip_ssl_cert_check)
256 int fd_thread_manager;
258 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
259 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
260 mailstream_network_delay.tv_usec = 0;
262 mailstream_debug = 1;
263 mailstream_logger = imap_logger_cmd;
266 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
267 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
268 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
270 thread_manager = etpan_thread_manager_new();
272 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
274 io_channel = g_io_channel_unix_new(fd_thread_manager);
276 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
277 thread_manager_event,
282 void imap_main_set_timeout(int sec)
284 mailstream_network_delay.tv_sec = sec;
285 mailstream_network_delay.tv_usec = 0;
288 void imap_main_done(void)
290 etpan_thread_manager_stop(thread_manager);
291 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
294 etpan_thread_manager_join(thread_manager);
296 g_source_remove(thread_manager_signal);
297 g_io_channel_unref(io_channel);
299 etpan_thread_manager_free(thread_manager);
301 chash_free(courier_workaround_hash);
302 chash_free(session_hash);
303 chash_free(imap_hash);
306 void imap_init(Folder * folder)
308 struct etpan_thread * thread;
312 thread = etpan_thread_manager_get_thread(thread_manager);
315 key.len = sizeof(folder);
319 chash_set(imap_hash, &key, &value, NULL);
322 void imap_done(Folder * folder)
324 struct etpan_thread * thread;
330 key.len = sizeof(folder);
332 r = chash_get(imap_hash, &key, &value);
338 etpan_thread_unbind(thread);
340 chash_delete(imap_hash, &key, NULL);
342 debug_print("remove thread");
345 static struct etpan_thread * get_thread(Folder * folder)
347 struct etpan_thread * thread;
352 key.len = sizeof(folder);
354 chash_get(imap_hash, &key, &value);
360 static mailimap * get_imap(Folder * folder)
368 key.len = sizeof(folder);
370 r = chash_get(session_hash, &key, &value);
375 debug_print("found imap %p\n", imap);
379 static gboolean cb_show_error(gpointer data)
381 mainwindow_show_error();
385 static void generic_cb(int cancelled, void * result, void * callback_data)
387 struct etpan_thread_op * op;
389 op = (struct etpan_thread_op *) callback_data;
391 debug_print("generic_cb\n");
392 if (op->imap && op->imap->imap_response_info &&
393 op->imap->imap_response_info->rsp_alert) {
394 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
395 op->imap->imap_response_info->rsp_alert);
396 g_timeout_add(10, cb_show_error, NULL);
401 static void threaded_run(Folder * folder, void * param, void * result,
402 void (* func)(struct etpan_thread_op * ))
404 struct etpan_thread_op * op;
405 struct etpan_thread * thread;
407 imap_folder_ref(folder);
409 op = etpan_thread_op_new();
411 op->imap = get_imap(folder);
417 op->callback = generic_cb;
418 op->callback_data = op;
423 thread = get_thread(folder);
424 etpan_thread_op_schedule(thread, op);
426 while (!op->finished) {
427 gtk_main_iteration();
430 etpan_thread_op_free(op);
432 imap_folder_unref(folder);
438 struct connect_param {
444 struct connect_result {
448 #define CHECK_IMAP() { \
449 if (!param->imap) { \
450 result->error = MAILIMAP_ERROR_BAD_STATE; \
455 static void connect_run(struct etpan_thread_op * op)
458 struct connect_param * param;
459 struct connect_result * result;
466 r = mailimap_socket_connect(param->imap,
467 param->server, param->port);
473 int imap_threaded_connect(Folder * folder, const char * server, int port)
475 struct connect_param param;
476 struct connect_result result;
479 mailimap * imap, * oldimap;
481 oldimap = get_imap(folder);
483 imap = mailimap_new(0, NULL);
486 debug_print("deleting old imap %p\n", oldimap);
487 delete_imap(folder, oldimap);
491 key.len = sizeof(folder);
494 chash_set(session_hash, &key, &value, NULL);
497 param.server = server;
501 threaded_run(folder, ¶m, &result, connect_run);
503 debug_print("connect ok %i with imap %p\n", result.error, imap);
508 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
511 struct connect_param *param = (struct connect_param *)data;
514 if (certificate == NULL || len < 0) {
515 g_warning("no cert presented.\n");
518 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
520 g_warning("IMAP: can't get cert\n");
522 } else if (ssl_certificate_check(cert, NULL,
523 (gchar *)param->server, (gushort)param->port) == TRUE) {
531 struct connect_param *param = (struct connect_param *)data;
532 gnutls_x509_crt cert = NULL;
535 if (certificate == NULL || len < 0) {
536 g_warning("no cert presented.\n");
540 tmp.data = malloc(len);
541 memcpy(tmp.data, certificate, len);
543 gnutls_x509_crt_init(&cert);
544 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
545 g_warning("IMAP: can't get cert\n");
547 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
548 (gchar *)param->server, (gushort)param->port) == TRUE) {
549 gnutls_x509_crt_deinit(cert);
552 gnutls_x509_crt_deinit(cert);
559 static void connect_ssl_run(struct etpan_thread_op * op)
562 struct connect_param * param;
563 struct connect_result * result;
570 r = mailimap_ssl_connect(param->imap,
571 param->server, param->port);
575 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
577 struct connect_param param;
578 struct connect_result result;
581 mailimap * imap, * oldimap;
582 unsigned char *certificate = NULL;
585 oldimap = get_imap(folder);
587 imap = mailimap_new(0, NULL);
590 debug_print("deleting old imap %p\n", oldimap);
591 delete_imap(folder, oldimap);
595 key.len = sizeof(folder);
598 chash_set(session_hash, &key, &value, NULL);
601 param.server = server;
605 threaded_run(folder, ¶m, &result, connect_ssl_run);
607 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
608 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
609 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
610 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
615 debug_print("connect %d with imap %p\n", result.error, imap);
626 struct mailimap_capability_data *caps;
629 static void capability_run(struct etpan_thread_op * op)
632 struct capa_param * param;
633 struct capa_result * result;
634 struct mailimap_capability_data *caps;
641 r = mailimap_capability(param->imap, &caps);
644 result->caps = (r == 0 ? caps : NULL);
648 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
650 struct capa_param param;
651 struct capa_result result;
654 imap = get_imap(folder);
658 threaded_run(folder, ¶m, &result, capability_run);
660 debug_print("capa %d\n", result.error);
669 struct disconnect_param {
673 struct disconnect_result {
677 static void disconnect_run(struct etpan_thread_op * op)
680 struct disconnect_param * param;
681 struct disconnect_result * result;
688 r = mailimap_logout(param->imap);
693 void imap_threaded_disconnect(Folder * folder)
695 struct connect_param param;
696 struct connect_result result;
699 imap = get_imap(folder);
701 debug_print("was disconnected\n");
707 threaded_run(folder, ¶m, &result, disconnect_run);
709 if (imap == get_imap(folder)) {
710 debug_print("deleting old imap %p\n", imap);
711 delete_imap(folder, imap);
713 debug_print("imap already deleted %p\n", imap);
716 debug_print("disconnect ok\n");
723 const char * wildcard;
732 static void list_run(struct etpan_thread_op * op)
734 struct list_param * param;
735 struct list_result * result;
746 if (param->base == NULL || param->wildcard == NULL) {
749 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
753 r = mailimap_lsub(param->imap, param->base,
754 param->wildcard, &list);
756 r = mailimap_list(param->imap, param->base,
757 param->wildcard, &list);
760 debug_print("imap list run - end\n");
763 int imap_threaded_list(Folder * folder, const char * base,
764 const char * wildcard,
767 struct list_param param;
768 struct list_result result;
770 debug_print("imap list - begin\n");
772 param.imap = get_imap(folder);
774 param.wildcard = wildcard;
775 param.sub_only = FALSE;
777 threaded_run(folder, ¶m, &result, list_run);
779 * p_result = result.list;
781 debug_print("imap list - end %p\n", result.list);
786 int imap_threaded_lsub(Folder * folder, const char * base,
787 const char * wildcard,
790 struct list_param param;
791 struct list_result result;
793 debug_print("imap lsub - begin\n");
795 param.imap = get_imap(folder);
797 param.wildcard = wildcard;
798 param.sub_only = TRUE;
800 threaded_run(folder, ¶m, &result, list_run);
802 * p_result = result.list;
804 debug_print("imap lsub - end %p\n", result.list);
809 struct subscribe_param {
815 struct subscribe_result {
819 static void subscribe_run(struct etpan_thread_op * op)
821 struct subscribe_param * param;
822 struct subscribe_result * result;
830 if (param->mb == NULL) {
832 debug_print("no mb\n");
835 if (param->subscribe)
836 r = mailimap_subscribe(param->imap, param->mb);
838 r = mailimap_unsubscribe(param->imap, param->mb);
840 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
843 int imap_threaded_subscribe(Folder * folder, const char * mb,
846 struct subscribe_param param;
847 struct subscribe_result result;
849 debug_print("imap list - begin\n");
851 param.imap = get_imap(folder);
853 param.subscribe = subscribe;
855 threaded_run(folder, ¶m, &result, subscribe_run);
863 const char * password;
868 struct login_result {
872 static void login_run(struct etpan_thread_op * op)
874 struct login_param * param;
875 struct login_result * result;
877 #ifdef DISABLE_LOG_DURING_LOGIN
886 #ifdef DISABLE_LOG_DURING_LOGIN
887 old_debug = mailstream_debug;
888 mailstream_debug = 0;
890 if (!strcmp(param->type, "LOGIN"))
891 r = mailimap_login(param->imap,
892 param->login, param->password);
893 else if (!strcmp(param->type, "GSSAPI"))
894 r = mailimap_authenticate(param->imap,
895 param->type, param->server, NULL, NULL,
896 param->login, param->login,
897 param->password, NULL);
899 r = mailimap_authenticate(param->imap,
900 param->type, NULL, NULL, NULL,
901 param->login, param->login,
902 param->password, NULL);
903 #ifdef DISABLE_LOG_DURING_LOGIN
904 mailstream_debug = old_debug;
908 if (param->imap->imap_response)
909 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
910 debug_print("imap login run - end %i\n", r);
913 int imap_threaded_login(Folder * folder,
914 const char * login, const char * password,
917 struct login_param param;
918 struct login_result result;
920 debug_print("imap login - begin\n");
922 param.imap = get_imap(folder);
924 param.password = password;
926 if (folder && folder->account)
927 param.server = folder->account->recv_server;
931 threaded_run(folder, ¶m, &result, login_run);
933 debug_print("imap login - end\n");
939 struct status_param {
942 struct mailimap_status_att_list * status_att_list;
945 struct status_result {
947 struct mailimap_mailbox_data_status * data_status;
950 static void status_run(struct etpan_thread_op * op)
952 struct status_param * param;
953 struct status_result * result;
961 r = mailimap_status(param->imap, param->mb,
962 param->status_att_list,
963 &result->data_status);
966 debug_print("imap status run - end %i\n", r);
969 int imap_threaded_status(Folder * folder, const char * mb,
970 struct mailimap_mailbox_data_status ** data_status,
973 struct status_param param;
974 struct status_result result;
975 struct mailimap_status_att_list * status_att_list;
977 debug_print("imap status - begin\n");
979 status_att_list = mailimap_status_att_list_new_empty();
981 mailimap_status_att_list_add(status_att_list,
982 MAILIMAP_STATUS_ATT_MESSAGES);
985 mailimap_status_att_list_add(status_att_list,
986 MAILIMAP_STATUS_ATT_RECENT);
989 mailimap_status_att_list_add(status_att_list,
990 MAILIMAP_STATUS_ATT_UIDNEXT);
993 mailimap_status_att_list_add(status_att_list,
994 MAILIMAP_STATUS_ATT_UIDVALIDITY);
997 mailimap_status_att_list_add(status_att_list,
998 MAILIMAP_STATUS_ATT_UNSEEN);
1000 param.imap = get_imap(folder);
1002 param.status_att_list = status_att_list;
1004 threaded_run(folder, ¶m, &result, status_run);
1006 debug_print("imap status - end\n");
1008 * data_status = result.data_status;
1010 mailimap_status_att_list_free(status_att_list);
1012 return result.error;
1021 struct noop_result {
1025 static void noop_run(struct etpan_thread_op * op)
1027 struct noop_param * param;
1028 struct noop_result * result;
1032 result = op->result;
1036 r = mailimap_noop(param->imap);
1039 debug_print("imap noop run - end %i\n", r);
1042 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1043 unsigned int *p_recent,
1044 unsigned int *p_expunge,
1045 unsigned int *p_unseen,
1046 unsigned int *p_uidnext,
1047 unsigned int *p_uidval)
1049 struct noop_param param;
1050 struct noop_result result;
1053 debug_print("imap noop - begin\n");
1055 imap = get_imap(folder);
1058 threaded_run(folder, ¶m, &result, noop_run);
1060 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1061 * p_exists = imap->imap_selection_info->sel_exists;
1062 * p_recent = imap->imap_selection_info->sel_recent;
1063 * p_unseen = imap->imap_selection_info->sel_unseen;
1064 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1065 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1073 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1074 imap->imap_response_info->rsp_expunged != NULL) {
1075 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1079 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1080 *p_exists, *p_recent, *p_expunge, *p_unseen,
1081 *p_uidnext, *p_uidval);
1083 return result.error;
1087 struct starttls_result {
1091 static void starttls_run(struct etpan_thread_op * op)
1093 struct connect_param * param;
1094 struct starttls_result * result;
1098 result = op->result;
1102 r = mailimap_starttls(param->imap);
1105 debug_print("imap starttls run - end %i\n", r);
1108 mailimap *imap = param->imap;
1109 mailstream_low *plain_low = NULL;
1110 mailstream_low *tls_low = NULL;
1113 plain_low = mailstream_get_low(imap->imap_stream);
1114 fd = mailstream_low_get_fd(plain_low);
1116 debug_print("imap starttls run - can't get fd\n");
1117 result->error = MAILIMAP_ERROR_STREAM;
1121 tls_low = mailstream_low_tls_open(fd);
1122 if (tls_low == NULL) {
1123 debug_print("imap starttls run - can't tls_open\n");
1124 result->error = MAILIMAP_ERROR_STREAM;
1127 mailstream_low_free(plain_low);
1128 mailstream_set_low(imap->imap_stream, tls_low);
1132 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1134 struct connect_param param;
1135 struct starttls_result result;
1137 unsigned char *certificate;
1139 debug_print("imap starttls - begin\n");
1141 param.imap = get_imap(folder);
1142 param.server = host;
1145 threaded_run(folder, ¶m, &result, starttls_run);
1147 debug_print("imap starttls - end\n");
1149 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1150 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1151 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1152 result.error = MAILIMAP_ERROR_STREAM;
1156 return result.error;
1161 struct create_param {
1166 struct create_result {
1170 static void create_run(struct etpan_thread_op * op)
1172 struct create_param * param;
1173 struct create_result * result;
1177 result = op->result;
1181 r = mailimap_create(param->imap, param->mb);
1184 debug_print("imap create run - end %i\n", r);
1187 int imap_threaded_create(Folder * folder, const char * mb)
1189 struct create_param param;
1190 struct create_result result;
1192 debug_print("imap create - begin\n");
1194 param.imap = get_imap(folder);
1197 threaded_run(folder, ¶m, &result, create_run);
1199 debug_print("imap create - end\n");
1201 return result.error;
1207 struct rename_param {
1210 const char * new_name;
1213 struct rename_result {
1217 static void rename_run(struct etpan_thread_op * op)
1219 struct rename_param * param;
1220 struct rename_result * result;
1224 result = op->result;
1228 r = mailimap_rename(param->imap, param->mb, param->new_name);
1231 debug_print("imap rename run - end %i\n", r);
1234 int imap_threaded_rename(Folder * folder,
1235 const char * mb, const char * new_name)
1237 struct rename_param param;
1238 struct rename_result result;
1240 debug_print("imap rename - begin\n");
1242 param.imap = get_imap(folder);
1244 param.new_name = new_name;
1246 threaded_run(folder, ¶m, &result, rename_run);
1248 debug_print("imap rename - end\n");
1250 return result.error;
1256 struct delete_param {
1261 struct delete_result {
1265 static void delete_run(struct etpan_thread_op * op)
1267 struct delete_param * param;
1268 struct delete_result * result;
1272 result = op->result;
1276 r = mailimap_delete(param->imap, param->mb);
1279 debug_print("imap delete run - end %i\n", r);
1282 int imap_threaded_delete(Folder * folder, const char * mb)
1284 struct delete_param param;
1285 struct delete_result result;
1287 debug_print("imap delete - begin\n");
1289 param.imap = get_imap(folder);
1292 threaded_run(folder, ¶m, &result, delete_run);
1294 debug_print("imap delete - end\n");
1296 return result.error;
1301 struct select_param {
1306 struct select_result {
1310 static void select_run(struct etpan_thread_op * op)
1312 struct select_param * param;
1313 struct select_result * result;
1317 result = op->result;
1321 r = mailimap_select(param->imap, param->mb);
1324 debug_print("imap select run - end %i\n", r);
1327 int imap_threaded_select(Folder * folder, const char * mb,
1328 gint * exists, gint * recent, gint * unseen,
1329 guint32 * uid_validity,gint *can_create_flags)
1331 struct select_param param;
1332 struct select_result result;
1335 debug_print("imap select - begin\n");
1337 imap = get_imap(folder);
1341 threaded_run(folder, ¶m, &result, select_run);
1343 if (result.error != MAILIMAP_NO_ERROR)
1344 return result.error;
1346 if (!imap || imap->imap_selection_info == NULL)
1347 return MAILIMAP_ERROR_PARSE;
1349 * exists = imap->imap_selection_info->sel_exists;
1350 * recent = imap->imap_selection_info->sel_recent;
1351 * unseen = imap->imap_selection_info->sel_unseen;
1352 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1353 * can_create_flags = FALSE;
1355 if (imap->imap_selection_info->sel_perm_flags) {
1357 clist_begin(imap->imap_selection_info->sel_perm_flags);
1358 for (; cur && !(*can_create_flags); cur = clist_next(cur)) {
1359 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1360 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1361 *can_create_flags = TRUE;
1362 else if (flag->fl_flag &&
1363 flag->fl_flag->fl_type == 6 &&
1364 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1365 *can_create_flags = TRUE;
1369 debug_print("imap select - end\n");
1371 return result.error;
1374 static void close_run(struct etpan_thread_op * op)
1376 struct select_param * param;
1377 struct select_result * result;
1381 result = op->result;
1385 r = mailimap_close(param->imap);
1388 debug_print("imap close run - end %i\n", r);
1391 int imap_threaded_close(Folder * folder)
1393 struct select_param param;
1394 struct select_result result;
1397 debug_print("imap close - begin\n");
1399 imap = get_imap(folder);
1402 threaded_run(folder, ¶m, &result, close_run);
1404 if (result.error != MAILIMAP_NO_ERROR)
1405 return result.error;
1407 debug_print("imap close - end\n");
1409 return result.error;
1412 struct examine_param {
1417 struct examine_result {
1421 static void examine_run(struct etpan_thread_op * op)
1423 struct examine_param * param;
1424 struct examine_result * result;
1428 result = op->result;
1432 r = mailimap_examine(param->imap, param->mb);
1435 debug_print("imap examine run - end %i\n", r);
1438 int imap_threaded_examine(Folder * folder, const char * mb,
1439 gint * exists, gint * recent, gint * unseen,
1440 guint32 * uid_validity)
1442 struct examine_param param;
1443 struct examine_result result;
1446 debug_print("imap examine - begin\n");
1448 imap = get_imap(folder);
1452 threaded_run(folder, ¶m, &result, examine_run);
1454 if (result.error != MAILIMAP_NO_ERROR)
1455 return result.error;
1457 if (!imap || imap->imap_selection_info == NULL)
1458 return MAILIMAP_ERROR_PARSE;
1460 * exists = imap->imap_selection_info->sel_exists;
1461 * recent = imap->imap_selection_info->sel_recent;
1462 * unseen = imap->imap_selection_info->sel_unseen;
1463 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1465 debug_print("imap examine - end\n");
1467 return result.error;
1473 struct search_param {
1476 struct mailimap_set * set;
1479 struct search_result {
1481 clist * search_result;
1484 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1486 return mailimap_set_item_new(orig->set_first, orig->set_last);
1489 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1491 clist *list = orig ? orig->set_list : NULL;
1492 clist *newlist = clist_new();
1497 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1498 clist_append(newlist,
1499 sc_mailimap_set_item_copy(
1500 (struct mailimap_set_item *)clist_content(cur)));
1501 return mailimap_set_new(newlist);
1504 static void search_run(struct etpan_thread_op * op)
1506 struct search_param * param;
1507 struct search_result * result;
1509 struct mailimap_search_key * key = NULL;
1510 struct mailimap_search_key * uid_key = NULL;
1511 struct mailimap_search_key * search_type_key;
1512 clist * search_result;
1515 result = op->result;
1519 /* we copy the mailimap_set because freeing the key is recursive */
1520 if (param->set != NULL) {
1521 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1522 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1523 uid_key = mailimap_search_key_new_all();
1525 search_type_key = NULL;
1526 switch (param->type) {
1527 case IMAP_SEARCH_TYPE_SIMPLE:
1528 search_type_key = NULL;
1531 case IMAP_SEARCH_TYPE_SEEN:
1532 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1533 NULL, NULL, NULL, NULL, NULL,
1534 NULL, NULL, NULL, NULL, NULL,
1535 NULL, NULL, NULL, NULL, 0,
1536 NULL, NULL, NULL, NULL, NULL,
1537 NULL, 0, NULL, NULL, NULL);
1540 case IMAP_SEARCH_TYPE_UNSEEN:
1541 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1542 NULL, NULL, NULL, NULL, NULL,
1543 NULL, NULL, NULL, NULL, NULL,
1544 NULL, NULL, NULL, NULL, 0,
1545 NULL, NULL, NULL, NULL, NULL,
1546 NULL, 0, NULL, NULL, NULL);
1549 case IMAP_SEARCH_TYPE_ANSWERED:
1550 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1551 NULL, NULL, NULL, NULL, NULL,
1552 NULL, NULL, NULL, NULL, NULL,
1553 NULL, NULL, NULL, NULL, 0,
1554 NULL, NULL, NULL, NULL, NULL,
1555 NULL, 0, NULL, NULL, NULL);
1558 case IMAP_SEARCH_TYPE_FLAGGED:
1559 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1560 NULL, NULL, NULL, NULL, NULL,
1561 NULL, NULL, NULL, NULL, NULL,
1562 NULL, NULL, NULL, NULL, 0,
1563 NULL, NULL, NULL, NULL, NULL,
1564 NULL, 0, NULL, NULL, NULL);
1566 case IMAP_SEARCH_TYPE_DELETED:
1567 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1568 NULL, NULL, NULL, NULL, NULL,
1569 NULL, NULL, NULL, NULL, NULL,
1570 NULL, NULL, NULL, NULL, 0,
1571 NULL, NULL, NULL, NULL, NULL,
1572 NULL, 0, NULL, NULL, NULL);
1574 case IMAP_SEARCH_TYPE_FORWARDED:
1575 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1576 NULL, NULL, NULL, NULL, NULL,
1577 strdup("$Forwarded"), NULL, NULL, NULL, NULL,
1578 NULL, NULL, NULL, NULL, 0,
1579 NULL, NULL, NULL, NULL, NULL,
1580 NULL, 0, NULL, NULL, NULL);
1584 if (search_type_key != NULL) {
1585 if (param->set != NULL) {
1586 key = mailimap_search_key_new_multiple_empty();
1587 mailimap_search_key_multiple_add(key, search_type_key);
1588 mailimap_search_key_multiple_add(key, uid_key);
1590 key = search_type_key;
1592 } else if (uid_key != NULL) {
1597 g_warning("no key!");
1598 result = op->result;
1600 result->search_result = NULL;
1602 mailstream_logger = imap_logger_uid;
1604 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1606 mailstream_logger = imap_logger_cmd;
1608 /* free the key (with the imapset) */
1609 mailimap_search_key_free(key);
1612 result->search_result = search_result;
1614 debug_print("imap search run - end %i\n", result->error);
1617 int imap_threaded_search(Folder * folder, int search_type,
1618 struct mailimap_set * set, clist ** search_result)
1620 struct search_param param;
1621 struct search_result result;
1624 debug_print("imap search - begin\n");
1626 imap = get_imap(folder);
1629 param.type = search_type;
1631 threaded_run(folder, ¶m, &result, search_run);
1633 if (result.error != MAILIMAP_NO_ERROR)
1634 return result.error;
1636 debug_print("imap search - end\n");
1638 * search_result = result.search_result;
1640 return result.error;
1645 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1649 struct mailimap_msg_att_dynamic ** patt_dyn);
1652 result_to_uid_list(clist * fetch_result, carray ** result)
1659 tab = carray_new(128);
1661 res = MAILIMAP_ERROR_MEMORY;
1665 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1666 cur = clist_next(cur)) {
1667 struct mailimap_msg_att * msg_att;
1671 msg_att = clist_content(cur);
1674 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1676 puid = malloc(sizeof(* puid));
1678 res = MAILIMAP_ERROR_MEMORY;
1683 r = carray_add(tab, puid, NULL);
1686 res = MAILIMAP_ERROR_MEMORY;
1693 return MAILIMAP_NO_ERROR;
1696 imap_fetch_uid_list_free(tab);
1701 static int imap_get_messages_list(mailimap * imap,
1702 uint32_t first_index,
1707 struct mailimap_fetch_att * fetch_att;
1708 struct mailimap_fetch_type * fetch_type;
1709 struct mailimap_set * set;
1710 clist * fetch_result;
1713 set = mailimap_set_new_interval(first_index, 0);
1715 res = MAILIMAP_ERROR_MEMORY;
1719 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1720 if (fetch_type == NULL) {
1721 res = MAILIMAP_ERROR_MEMORY;
1725 fetch_att = mailimap_fetch_att_new_uid();
1726 if (fetch_att == NULL) {
1727 res = MAILIMAP_ERROR_MEMORY;
1728 goto free_fetch_type;
1731 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1732 if (r != MAILIMAP_NO_ERROR) {
1733 mailimap_fetch_att_free(fetch_att);
1734 res = MAILIMAP_ERROR_MEMORY;
1735 goto free_fetch_type;
1738 mailstream_logger = imap_logger_fetch;
1740 r = mailimap_uid_fetch(imap, set,
1741 fetch_type, &fetch_result);
1743 mailstream_logger = imap_logger_cmd;
1744 mailimap_fetch_type_free(fetch_type);
1745 mailimap_set_free(set);
1747 if (r != MAILIMAP_NO_ERROR) {
1753 r = result_to_uid_list(fetch_result, &env_list);
1754 mailimap_fetch_list_free(fetch_result);
1756 * result = env_list;
1758 return MAILIMAP_NO_ERROR;
1761 mailimap_fetch_type_free(fetch_type);
1763 mailimap_set_free(set);
1771 struct fetch_uid_param {
1773 uint32_t first_index;
1776 struct fetch_uid_result {
1778 carray * fetch_result;
1781 static void fetch_uid_run(struct etpan_thread_op * op)
1783 struct fetch_uid_param * param;
1784 struct fetch_uid_result * result;
1785 carray * fetch_result;
1789 result = op->result;
1793 fetch_result = NULL;
1794 mailstream_logger = imap_logger_noop;
1795 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1797 r = imap_get_messages_list(param->imap, param->first_index,
1800 mailstream_logger = imap_logger_cmd;
1803 result->fetch_result = fetch_result;
1804 debug_print("imap fetch_uid run - end %i\n", r);
1807 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1808 carray ** fetch_result)
1810 struct fetch_uid_param param;
1811 struct fetch_uid_result result;
1814 debug_print("imap fetch_uid - begin\n");
1816 imap = get_imap(folder);
1818 param.first_index = first_index;
1820 threaded_run(folder, ¶m, &result, fetch_uid_run);
1822 if (result.error != MAILIMAP_NO_ERROR)
1823 return result.error;
1825 debug_print("imap fetch_uid - end\n");
1827 * fetch_result = result.fetch_result;
1829 return result.error;
1833 void imap_fetch_uid_list_free(carray * uid_list)
1837 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1840 puid = carray_get(uid_list, i);
1843 carray_free(uid_list);
1849 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1852 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1858 GSList *tags = NULL;
1860 tab = carray_new(128);
1862 res = MAILIMAP_ERROR_MEMORY;
1866 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1867 cur = clist_next(cur)) {
1868 struct mailimap_msg_att * msg_att;
1871 struct mailimap_msg_att_dynamic * att_dyn;
1877 msg_att = clist_content(cur);
1881 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1884 if (att_dyn == NULL)
1887 flags = imap_flags_to_flags(att_dyn, &tags);
1889 puid = malloc(sizeof(* puid));
1891 res = MAILIMAP_ERROR_MEMORY;
1896 r = carray_add(tab, puid, NULL);
1899 res = MAILIMAP_ERROR_MEMORY;
1902 pflags = malloc(sizeof(* pflags));
1903 if (pflags == NULL) {
1904 res = MAILIMAP_ERROR_MEMORY;
1908 r = carray_add(tab, pflags, NULL);
1911 res = MAILIMAP_ERROR_MEMORY;
1914 r = carray_add(tab, tags, NULL);
1917 res = MAILIMAP_ERROR_MEMORY;
1924 return MAILIMAP_NO_ERROR;
1927 imap_fetch_uid_flags_list_free(tab);
1928 slist_free_strings(tags);
1934 static int imap_get_messages_flags_list(mailimap * imap,
1935 uint32_t first_index,
1940 struct mailimap_fetch_att * fetch_att;
1941 struct mailimap_fetch_type * fetch_type;
1942 struct mailimap_set * set;
1943 clist * fetch_result;
1946 set = mailimap_set_new_interval(first_index, 0);
1948 res = MAILIMAP_ERROR_MEMORY;
1952 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1953 if (fetch_type == NULL) {
1954 res = MAILIMAP_ERROR_MEMORY;
1958 fetch_att = mailimap_fetch_att_new_flags();
1959 if (fetch_att == NULL) {
1960 res = MAILIMAP_ERROR_MEMORY;
1961 goto free_fetch_type;
1964 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1965 if (r != MAILIMAP_NO_ERROR) {
1966 mailimap_fetch_att_free(fetch_att);
1967 res = MAILIMAP_ERROR_MEMORY;
1968 goto free_fetch_type;
1971 fetch_att = mailimap_fetch_att_new_uid();
1972 if (fetch_att == NULL) {
1973 res = MAILIMAP_ERROR_MEMORY;
1974 goto free_fetch_type;
1977 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1978 if (r != MAILIMAP_NO_ERROR) {
1979 mailimap_fetch_att_free(fetch_att);
1980 res = MAILIMAP_ERROR_MEMORY;
1981 goto free_fetch_type;
1984 mailstream_logger = imap_logger_fetch;
1986 r = mailimap_uid_fetch(imap, set,
1987 fetch_type, &fetch_result);
1989 mailstream_logger = imap_logger_cmd;
1990 mailimap_fetch_type_free(fetch_type);
1991 mailimap_set_free(set);
1993 if (r != MAILIMAP_NO_ERROR) {
1999 r = result_to_uid_flags_list(fetch_result, &env_list);
2000 mailimap_fetch_list_free(fetch_result);
2002 * result = env_list;
2004 return MAILIMAP_NO_ERROR;
2007 mailimap_fetch_type_free(fetch_type);
2009 mailimap_set_free(set);
2016 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2018 struct fetch_uid_param * param;
2019 struct fetch_uid_result * result;
2020 carray * fetch_result;
2024 result = op->result;
2028 fetch_result = NULL;
2029 r = imap_get_messages_flags_list(param->imap, param->first_index,
2033 result->fetch_result = fetch_result;
2034 debug_print("imap fetch_uid run - end %i\n", r);
2037 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2038 carray ** fetch_result)
2040 struct fetch_uid_param param;
2041 struct fetch_uid_result result;
2044 debug_print("imap fetch_uid - begin\n");
2046 imap = get_imap(folder);
2048 param.first_index = first_index;
2050 mailstream_logger = imap_logger_noop;
2051 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2053 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2055 mailstream_logger = imap_logger_cmd;
2058 if (result.error != MAILIMAP_NO_ERROR)
2059 return result.error;
2061 debug_print("imap fetch_uid - end\n");
2063 * fetch_result = result.fetch_result;
2065 return result.error;
2069 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2073 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2076 data = carray_get(uid_flags_list, i);
2078 data = carray_get(uid_flags_list, i + 1);
2081 carray_free(uid_flags_list);
2086 static int imap_fetch(mailimap * imap,
2089 size_t * result_len)
2092 struct mailimap_set * set;
2093 struct mailimap_fetch_att * fetch_att;
2094 struct mailimap_fetch_type * fetch_type;
2095 clist * fetch_result;
2096 struct mailimap_msg_att * msg_att;
2097 struct mailimap_msg_att_item * msg_att_item;
2102 struct mailimap_section * section;
2104 set = mailimap_set_new_single(msg_index);
2106 res = MAILIMAP_ERROR_MEMORY;
2110 section = mailimap_section_new(NULL);
2111 if (section == NULL) {
2112 res = MAILIMAP_ERROR_MEMORY;
2116 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2117 if (fetch_att == NULL) {
2118 mailimap_section_free(section);
2119 res = MAILIMAP_ERROR_MEMORY;
2123 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2124 if (fetch_type == NULL) {
2125 res = MAILIMAP_ERROR_MEMORY;
2126 goto free_fetch_att;
2129 mailstream_logger = imap_logger_fetch;
2131 r = mailimap_uid_fetch(imap, set,
2132 fetch_type, &fetch_result);
2134 mailstream_logger = imap_logger_cmd;
2136 mailimap_fetch_type_free(fetch_type);
2137 mailimap_set_free(set);
2140 case MAILIMAP_NO_ERROR:
2146 if (clist_begin(fetch_result) == NULL) {
2147 mailimap_fetch_list_free(fetch_result);
2148 return MAILIMAP_ERROR_FETCH;
2151 msg_att = clist_begin(fetch_result)->data;
2156 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2157 cur = clist_next(cur)) {
2158 msg_att_item = clist_content(cur);
2160 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2161 if (msg_att_item->att_data.att_static->att_type ==
2162 MAILIMAP_MSG_ATT_BODY_SECTION) {
2163 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2165 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2167 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2172 mailimap_fetch_list_free(fetch_result);
2175 return MAILIMAP_ERROR_FETCH;
2178 * result_len = text_length;
2180 return MAILIMAP_NO_ERROR;
2183 mailimap_fetch_att_free(fetch_att);
2185 mailimap_set_free(set);
2190 static int imap_fetch_header(mailimap * imap,
2193 size_t * result_len)
2196 struct mailimap_set * set;
2197 struct mailimap_fetch_att * fetch_att;
2198 struct mailimap_fetch_type * fetch_type;
2199 clist * fetch_result;
2200 struct mailimap_msg_att * msg_att;
2201 struct mailimap_msg_att_item * msg_att_item;
2206 struct mailimap_section * section;
2208 set = mailimap_set_new_single(msg_index);
2210 res = MAILIMAP_ERROR_MEMORY;
2214 section = mailimap_section_new_header();
2215 if (section == NULL) {
2216 res = MAILIMAP_ERROR_MEMORY;
2220 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2221 if (fetch_att == NULL) {
2222 mailimap_section_free(section);
2223 res = MAILIMAP_ERROR_MEMORY;
2227 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2228 if (fetch_type == NULL) {
2229 res = MAILIMAP_ERROR_MEMORY;
2230 goto free_fetch_att;
2233 mailstream_logger = imap_logger_fetch;
2235 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2237 mailstream_logger = imap_logger_cmd;
2238 mailimap_fetch_type_free(fetch_type);
2239 mailimap_set_free(set);
2242 case MAILIMAP_NO_ERROR:
2248 if (clist_begin(fetch_result) == NULL) {
2249 mailimap_fetch_list_free(fetch_result);
2250 return MAILIMAP_ERROR_FETCH;
2253 msg_att = clist_begin(fetch_result)->data;
2258 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2259 cur = clist_next(cur)) {
2260 msg_att_item = clist_content(cur);
2262 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2263 if (msg_att_item->att_data.att_static->att_type ==
2264 MAILIMAP_MSG_ATT_BODY_SECTION) {
2265 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2266 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2268 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2273 mailimap_fetch_list_free(fetch_result);
2276 return MAILIMAP_ERROR_FETCH;
2279 * result_len = text_length;
2281 return MAILIMAP_NO_ERROR;
2284 mailimap_fetch_att_free(fetch_att);
2286 mailimap_set_free(set);
2293 struct fetch_content_param {
2296 const char * filename;
2300 struct fetch_content_result {
2304 static void fetch_content_run(struct etpan_thread_op * op)
2306 struct fetch_content_param * param;
2307 struct fetch_content_result * result;
2309 size_t content_size;
2315 result = op->result;
2321 if (param->with_body)
2322 r = imap_fetch(param->imap, param->msg_index,
2323 &content, &content_size);
2325 r = imap_fetch_header(param->imap, param->msg_index,
2326 &content, &content_size);
2330 if (r == MAILIMAP_NO_ERROR) {
2331 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2333 result->error = MAILIMAP_ERROR_FETCH;
2337 f = fdopen(fd, "wb");
2339 result->error = MAILIMAP_ERROR_FETCH;
2343 r = fwrite(content, 1, content_size, f);
2344 if (r < content_size) {
2360 g_unlink(param->filename);
2363 /* mmap_string_unref is a simple free in libetpan
2364 * when it has MMAP_UNAVAILABLE defined */
2365 if (mmap_string_unref(content) != 0)
2369 debug_print("imap fetch_content run - end %i\n", r);
2372 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2374 const char * filename)
2376 struct fetch_content_param param;
2377 struct fetch_content_result result;
2380 debug_print("imap fetch_content - begin\n");
2382 imap = get_imap(folder);
2384 param.msg_index = msg_index;
2385 param.filename = filename;
2386 param.with_body = with_body;
2388 threaded_run(folder, ¶m, &result, fetch_content_run);
2390 if (result.error != MAILIMAP_NO_ERROR)
2391 return result.error;
2393 debug_print("imap fetch_content - end\n");
2395 return result.error;
2400 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2405 GSList *tags = NULL;
2409 flag_list = att_dyn->att_list;
2410 if (flag_list == NULL)
2413 for(cur = clist_begin(flag_list) ; cur != NULL ;
2414 cur = clist_next(cur)) {
2415 struct mailimap_flag_fetch * flag_fetch;
2417 flag_fetch = clist_content(cur);
2418 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2421 switch (flag_fetch->fl_flag->fl_type) {
2422 case MAILIMAP_FLAG_ANSWERED:
2423 flags |= MSG_REPLIED;
2425 case MAILIMAP_FLAG_FLAGGED:
2426 flags |= MSG_MARKED;
2428 case MAILIMAP_FLAG_DELETED:
2429 flags |= MSG_DELETED;
2431 case MAILIMAP_FLAG_SEEN:
2432 flags &= ~MSG_UNREAD;
2435 case MAILIMAP_FLAG_KEYWORD:
2436 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
2437 flags |= MSG_FORWARDED;
2439 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2449 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2453 struct mailimap_msg_att_dynamic ** patt_dyn)
2455 clistiter * item_cur;
2459 struct mailimap_msg_att_dynamic * att_dyn;
2466 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2467 item_cur = clist_next(item_cur)) {
2468 struct mailimap_msg_att_item * item;
2470 item = clist_content(item_cur);
2472 switch (item->att_type) {
2473 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2474 switch (item->att_data.att_static->att_type) {
2475 case MAILIMAP_MSG_ATT_UID:
2476 uid = item->att_data.att_static->att_data.att_uid;
2479 case MAILIMAP_MSG_ATT_BODY_SECTION:
2480 if (headers == NULL) {
2481 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2484 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2485 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2490 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2491 if (att_dyn == NULL) {
2492 att_dyn = item->att_data.att_dyn;
2500 if (pheaders != NULL)
2501 * pheaders = headers;
2502 if (pref_size != NULL)
2503 * pref_size = ref_size;
2504 if (patt_dyn != NULL)
2505 * patt_dyn = att_dyn;
2507 return MAIL_NO_ERROR;
2510 static struct imap_fetch_env_info *
2511 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2513 struct imap_fetch_env_info * info;
2517 struct mailimap_msg_att_dynamic * att_dyn;
2519 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2524 info = malloc(sizeof(* info));
2526 info->headers = strdup(headers);
2528 info->flags = imap_flags_to_flags(att_dyn, tags);
2534 imap_fetch_result_to_envelop_list(clist * fetch_result,
2535 carray ** p_env_list)
2542 env_list = carray_new(16);
2544 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2545 cur = clist_next(cur)) {
2546 struct mailimap_msg_att * msg_att;
2547 struct imap_fetch_env_info * env_info;
2548 GSList *tags = NULL;
2550 msg_att = clist_content(cur);
2552 env_info = fetch_to_env_info(msg_att, &tags);
2554 return MAILIMAP_ERROR_MEMORY;
2555 carray_add(env_list, env_info, NULL);
2556 carray_add(env_list, tags, NULL);
2559 * p_env_list = env_list;
2561 return MAIL_NO_ERROR;
2564 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2566 struct mailimap_fetch_att * fetch_att;
2570 struct mailimap_header_list * imap_hdrlist;
2571 struct mailimap_section * section;
2573 hdrlist = clist_new();
2575 header = strdup("Date");
2576 r = clist_append(hdrlist, header);
2577 header = strdup("From");
2578 r = clist_append(hdrlist, header);
2579 header = strdup("To");
2580 r = clist_append(hdrlist, header);
2581 header = strdup("Cc");
2582 r = clist_append(hdrlist, header);
2583 header = strdup("Subject");
2584 r = clist_append(hdrlist, header);
2585 header = strdup("Message-ID");
2586 r = clist_append(hdrlist, header);
2587 header = strdup("References");
2588 r = clist_append(hdrlist, header);
2589 header = strdup("In-Reply-To");
2590 r = clist_append(hdrlist, header);
2592 imap_hdrlist = mailimap_header_list_new(hdrlist);
2593 section = mailimap_section_new_header_fields(imap_hdrlist);
2594 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2595 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2597 return MAIL_NO_ERROR;
2600 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2602 struct mailimap_fetch_att * fetch_att;
2603 struct mailimap_section * section;
2605 section = mailimap_section_new_header();
2606 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2607 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2609 return MAIL_NO_ERROR;
2613 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2614 carray ** p_env_list)
2616 struct mailimap_fetch_att * fetch_att;
2617 struct mailimap_fetch_type * fetch_type;
2619 clist * fetch_result;
2621 carray * env_list = NULL;
2625 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2628 fetch_att = mailimap_fetch_att_new_uid();
2629 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2632 fetch_att = mailimap_fetch_att_new_flags();
2633 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2636 fetch_att = mailimap_fetch_att_new_rfc822_size();
2637 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2641 key.len = sizeof(imap);
2642 r = chash_get(courier_workaround_hash, &key, &value);
2644 r = imap_add_envelope_fetch_att(fetch_type);
2646 r = imap_add_header_fetch_att(fetch_type);
2648 mailstream_logger = imap_logger_fetch;
2650 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2652 mailstream_logger = imap_logger_cmd;
2654 case MAILIMAP_NO_ERROR:
2657 mailimap_fetch_type_free(fetch_type);
2658 debug_print("uid_fetch: %d\n", r);
2662 if (clist_begin(fetch_result) == NULL) {
2663 res = MAILIMAP_ERROR_FETCH;
2664 debug_print("clist_begin = NULL\n");
2668 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2669 mailimap_fetch_list_free(fetch_result);
2671 if (r != MAILIMAP_NO_ERROR) {
2672 mailimap_fetch_type_free(fetch_type);
2673 res = MAILIMAP_ERROR_MEMORY;
2674 debug_print("fetch_result_to_envelop_list: %d\n", res);
2678 mailimap_fetch_type_free(fetch_type);
2680 * p_env_list = env_list;
2682 return MAILIMAP_NO_ERROR;
2688 struct fetch_env_param {
2690 struct mailimap_set * set;
2693 struct fetch_env_result {
2694 carray * fetch_env_result;
2698 static void fetch_env_run(struct etpan_thread_op * op)
2700 struct fetch_env_param * param;
2701 struct fetch_env_result * result;
2706 result = op->result;
2711 r = imap_get_envelopes_list(param->imap, param->set,
2715 result->fetch_env_result = env_list;
2717 debug_print("imap fetch_env run - end %i\n", r);
2720 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2721 carray ** p_env_list)
2723 struct fetch_env_param param;
2724 struct fetch_env_result result;
2727 debug_print("imap fetch_env - begin\n");
2729 imap = get_imap(folder);
2733 threaded_run(folder, ¶m, &result, fetch_env_run);
2735 if (result.error != MAILIMAP_NO_ERROR) {
2741 key.len = sizeof(imap);
2742 r = chash_get(courier_workaround_hash, &key, &value);
2746 chash_set(courier_workaround_hash, &key, &value, NULL);
2748 threaded_run(folder, ¶m, &result, fetch_env_run);
2752 if (result.error != MAILIMAP_NO_ERROR)
2753 return result.error;
2755 debug_print("imap fetch_env - end\n");
2757 * p_env_list = result.fetch_env_result;
2759 return result.error;
2762 void imap_fetch_env_free(carray * env_list)
2766 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2767 struct imap_fetch_env_info * env_info;
2769 env_info = carray_get(env_list, i);
2770 free(env_info->headers);
2773 carray_free(env_list);
2780 struct append_param {
2782 const char * mailbox;
2783 const char * filename;
2784 struct mailimap_flag_list * flag_list;
2787 struct append_result {
2792 static void append_run(struct etpan_thread_op * op)
2794 struct append_param * param;
2795 struct append_result * result;
2800 struct stat stat_buf;
2803 guint32 uid = 0, val = 0;
2806 result = op->result;
2811 r = stat(param->filename, &stat_buf);
2813 result->error = MAILIMAP_ERROR_APPEND;
2816 size = stat_buf.st_size;
2818 fd = open(param->filename, O_RDONLY);
2820 result->error = MAILIMAP_ERROR_APPEND;
2824 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2825 if (data == (void *) MAP_FAILED) {
2827 result->error = MAILIMAP_ERROR_APPEND;
2831 data = file_read_to_str_no_recode(param->filename);
2833 result->error = MAILIMAP_ERROR_APPEND;
2836 size = strlen(data);
2838 mailstream_logger = imap_logger_append;
2840 r = mailimap_uidplus_append(param->imap, param->mailbox,
2841 param->flag_list, NULL,
2842 data, size, &val, &uid);
2844 mailstream_logger = imap_logger_cmd;
2855 debug_print("imap append run - end %i uid %d\n", r, uid);
2858 int imap_threaded_append(Folder * folder, const char * mailbox,
2859 const char * filename,
2860 struct mailimap_flag_list * flag_list,
2863 struct append_param param;
2864 struct append_result result;
2867 debug_print("imap append - begin\n");
2869 imap = get_imap(folder);
2871 param.mailbox = mailbox;
2872 param.filename = filename;
2873 param.flag_list = flag_list;
2875 threaded_run(folder, ¶m, &result, append_run);
2877 if (result.error != MAILIMAP_NO_ERROR)
2878 return result.error;
2880 debug_print("imap append - end\n");
2884 return result.error;
2890 struct expunge_param {
2894 struct expunge_result {
2898 static void expunge_run(struct etpan_thread_op * op)
2900 struct expunge_param * param;
2901 struct expunge_result * result;
2905 result = op->result;
2909 r = mailimap_expunge(param->imap);
2912 debug_print("imap expunge run - end %i\n", r);
2915 int imap_threaded_expunge(Folder * folder)
2917 struct expunge_param param;
2918 struct expunge_result result;
2920 debug_print("imap expunge - begin\n");
2922 param.imap = get_imap(folder);
2924 threaded_run(folder, ¶m, &result, expunge_run);
2926 debug_print("imap expunge - end\n");
2928 return result.error;
2934 struct mailimap_set * set;
2938 struct copy_result {
2940 struct mailimap_set *source;
2941 struct mailimap_set *dest;
2944 static void copy_run(struct etpan_thread_op * op)
2946 struct copy_param * param;
2947 struct copy_result * result;
2950 struct mailimap_set *source = NULL, *dest = NULL;
2953 result = op->result;
2957 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2958 &val, &source, &dest);
2962 result->source = source;
2963 result->dest = dest;
2965 result->source = NULL;
2966 result->dest = NULL;
2968 debug_print("imap copy run - end %i\n", r);
2971 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2972 const char * mb, struct mailimap_set **source,
2973 struct mailimap_set **dest)
2975 struct copy_param param;
2976 struct copy_result result;
2979 debug_print("imap copy - begin\n");
2981 imap = get_imap(folder);
2986 threaded_run(folder, ¶m, &result, copy_run);
2990 if (result.error != MAILIMAP_NO_ERROR)
2991 return result.error;
2993 *source = result.source;
2994 *dest = result.dest;
2996 debug_print("imap copy - end\n");
2998 return result.error;
3003 struct store_param {
3005 struct mailimap_set * set;
3006 struct mailimap_store_att_flags * store_att_flags;
3009 struct store_result {
3013 static void store_run(struct etpan_thread_op * op)
3015 struct store_param * param;
3016 struct store_result * result;
3020 result = op->result;
3024 r = mailimap_uid_store(param->imap, param->set,
3025 param->store_att_flags);
3029 debug_print("imap store run - end %i\n", r);
3032 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3033 struct mailimap_store_att_flags * store_att_flags)
3035 struct store_param param;
3036 struct store_result result;
3039 debug_print("imap store - begin\n");
3041 imap = get_imap(folder);
3044 param.store_att_flags = store_att_flags;
3046 threaded_run(folder, ¶m, &result, store_run);
3048 if (result.error != MAILIMAP_NO_ERROR)
3049 return result.error;
3051 debug_print("imap store - end\n");
3053 return result.error;
3057 #define ENV_BUFFER_SIZE 512
3059 static void do_exec_command(int fd, const char * command,
3060 const char * servername, uint16_t port)
3064 char env_buffer[ENV_BUFFER_SIZE];
3068 /* Fork again to become a child of init rather than
3069 the etpan client. */
3075 snprintf(env_buffer, ENV_BUFFER_SIZE,
3076 "ETPANSERVER=%s", servername);
3078 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
3082 setenv("ETPANSERVER", servername, 1);
3084 unsetenv("ETPANSERVER");
3089 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
3091 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
3097 snprintf(porttext, sizeof(porttext), "%d", port);
3098 setenv("ETPANPORT", porttext, 1);
3101 unsetenv("ETPANPORT");
3105 /* Not a lot we can do if there's an error other than bail. */
3106 if (dup2(fd, 0) == -1)
3108 if (dup2(fd, 1) == -1)
3111 /* Should we close stderr and reopen /dev/null? */
3113 maxopen = sysconf(_SC_OPEN_MAX);
3114 for (i=3; i < maxopen; i++)
3118 /* Detach from the controlling tty if we have one. Otherwise,
3119 SSH might do something stupid like trying to use it instead
3120 of running $SSH_ASKPASS. Doh. */
3121 fd = open("/dev/tty", O_RDONLY);
3123 ioctl(fd, TIOCNOTTY, NULL);
3126 #endif /* TIOCNOTTY */
3128 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3130 /* Eep. Shouldn't reach this */
3134 static int subcommand_connect(const char *command,
3135 const char *servername, uint16_t port)
3137 /* SEB unsupported on Windows */
3141 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3146 do_exec_command(sockfds[1], command, servername, port);
3148 else if (childpid == -1) {
3156 /* Reap child, leaving grandchild process to run */
3157 waitpid(childpid, NULL, 0);
3162 static int socket_connect_cmd(mailimap * imap, const char * command,
3163 const char * server, int port)
3169 fd = subcommand_connect(command, server, port);
3171 return MAILIMAP_ERROR_STREAM;
3173 s = mailstream_socket_open(fd);
3176 return MAILIMAP_ERROR_STREAM;
3179 r = mailimap_connect(imap, s);
3180 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3181 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3182 mailstream_close(s);
3184 imap->imap_stream = NULL;
3193 struct connect_cmd_param {
3195 const char * command;
3196 const char * server;
3200 struct connect_cmd_result {
3204 static void connect_cmd_run(struct etpan_thread_op * op)
3207 struct connect_cmd_param * param;
3208 struct connect_cmd_result * result;
3211 result = op->result;
3215 r = socket_connect_cmd(param->imap, param->command,
3216 param->server, param->port);
3222 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3223 const char * server, int port)
3225 struct connect_cmd_param param;
3226 struct connect_cmd_result result;
3229 mailimap * imap, * oldimap;
3231 oldimap = get_imap(folder);
3233 imap = mailimap_new(0, NULL);
3236 debug_print("deleting old imap %p\n", oldimap);
3237 delete_imap(folder, oldimap);
3241 key.len = sizeof(folder);
3244 chash_set(session_hash, &key, &value, NULL);
3247 param.command = command;
3248 param.server = server;
3251 threaded_run(folder, ¶m, &result, connect_cmd_run);
3253 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3255 return result.error;
3257 #endif /* G_OS_WIN32 */
3259 void imap_threaded_cancel(Folder * folder)
3263 imap = get_imap(folder);
3264 if (imap->imap_stream != NULL)
3265 mailstream_cancel(imap->imap_stream);
3270 void imap_main_init(void)
3273 void imap_main_done(void)
3276 void imap_main_set_timeout(int sec)
3280 void imap_threaded_cancel(Folder * folder);