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);
375 static gboolean cb_show_error(gpointer data)
377 mainwindow_show_error();
381 static void generic_cb(int cancelled, void * result, void * callback_data)
383 struct etpan_thread_op * op;
385 op = (struct etpan_thread_op *) callback_data;
387 debug_print("generic_cb\n");
388 if (op->imap && op->imap->imap_response_info &&
389 op->imap->imap_response_info->rsp_alert) {
390 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
391 op->imap->imap_response_info->rsp_alert);
392 g_timeout_add(10, cb_show_error, NULL);
397 static void threaded_run(Folder * folder, void * param, void * result,
398 void (* func)(struct etpan_thread_op * ))
400 struct etpan_thread_op * op;
401 struct etpan_thread * thread;
403 imap_folder_ref(folder);
405 op = etpan_thread_op_new();
407 op->imap = get_imap(folder);
413 op->callback = generic_cb;
414 op->callback_data = op;
419 thread = get_thread(folder);
420 etpan_thread_op_schedule(thread, op);
422 while (!op->finished) {
423 gtk_main_iteration();
426 etpan_thread_op_free(op);
428 imap_folder_unref(folder);
434 struct connect_param {
440 struct connect_result {
444 #define CHECK_IMAP() { \
445 if (!param->imap) { \
446 result->error = MAILIMAP_ERROR_BAD_STATE; \
451 static void connect_run(struct etpan_thread_op * op)
454 struct connect_param * param;
455 struct connect_result * result;
462 r = mailimap_socket_connect(param->imap,
463 param->server, param->port);
469 int imap_threaded_connect(Folder * folder, const char * server, int port)
471 struct connect_param param;
472 struct connect_result result;
475 mailimap * imap, * oldimap;
477 oldimap = get_imap(folder);
479 imap = mailimap_new(0, NULL);
482 debug_print("deleting old imap %p\n", oldimap);
483 delete_imap(folder, oldimap);
487 key.len = sizeof(folder);
490 chash_set(session_hash, &key, &value, NULL);
493 param.server = server;
497 threaded_run(folder, ¶m, &result, connect_run);
499 debug_print("connect ok %i with imap %p\n", result.error, imap);
504 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
507 struct connect_param *param = (struct connect_param *)data;
510 if (certificate == NULL || len < 0) {
511 g_warning("no cert presented.\n");
514 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
516 g_warning("IMAP: can't get cert\n");
518 } else if (ssl_certificate_check(cert, NULL,
519 (gchar *)param->server, (gushort)param->port) == TRUE) {
527 struct connect_param *param = (struct connect_param *)data;
528 gnutls_x509_crt cert = NULL;
531 if (certificate == NULL || len < 0) {
532 g_warning("no cert presented.\n");
536 tmp.data = malloc(len);
537 memcpy(tmp.data, certificate, len);
539 gnutls_x509_crt_init(&cert);
540 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
541 g_warning("IMAP: can't get cert\n");
543 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
544 (gchar *)param->server, (gushort)param->port) == TRUE) {
545 gnutls_x509_crt_deinit(cert);
548 gnutls_x509_crt_deinit(cert);
555 static void connect_ssl_run(struct etpan_thread_op * op)
558 struct connect_param * param;
559 struct connect_result * result;
566 r = mailimap_ssl_connect(param->imap,
567 param->server, param->port);
571 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
573 struct connect_param param;
574 struct connect_result result;
577 mailimap * imap, * oldimap;
578 unsigned char *certificate = NULL;
581 oldimap = get_imap(folder);
583 imap = mailimap_new(0, NULL);
586 debug_print("deleting old imap %p\n", oldimap);
587 delete_imap(folder, oldimap);
591 key.len = sizeof(folder);
594 chash_set(session_hash, &key, &value, NULL);
597 param.server = server;
601 threaded_run(folder, ¶m, &result, connect_ssl_run);
603 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
604 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
605 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
606 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
611 debug_print("connect %d with imap %p\n", result.error, imap);
622 struct mailimap_capability_data *caps;
625 static void capability_run(struct etpan_thread_op * op)
628 struct capa_param * param;
629 struct capa_result * result;
630 struct mailimap_capability_data *caps;
637 r = mailimap_capability(param->imap, &caps);
640 result->caps = (r == 0 ? caps : NULL);
644 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
646 struct capa_param param;
647 struct capa_result result;
650 imap = get_imap(folder);
654 threaded_run(folder, ¶m, &result, capability_run);
656 debug_print("capa %d\n", result.error);
665 struct disconnect_param {
669 struct disconnect_result {
673 static void disconnect_run(struct etpan_thread_op * op)
676 struct disconnect_param * param;
677 struct disconnect_result * result;
684 r = mailimap_logout(param->imap);
689 void imap_threaded_disconnect(Folder * folder)
691 struct connect_param param;
692 struct connect_result result;
695 imap = get_imap(folder);
697 debug_print("was disconnected\n");
703 threaded_run(folder, ¶m, &result, disconnect_run);
705 if (imap == get_imap(folder)) {
706 debug_print("deleting old imap %p\n", imap);
707 delete_imap(folder, imap);
709 debug_print("imap already deleted %p\n", imap);
712 debug_print("disconnect ok\n");
719 const char * wildcard;
728 static void list_run(struct etpan_thread_op * op)
730 struct list_param * param;
731 struct list_result * result;
742 if (param->base == NULL || param->wildcard == NULL) {
745 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
749 r = mailimap_lsub(param->imap, param->base,
750 param->wildcard, &list);
752 r = mailimap_list(param->imap, param->base,
753 param->wildcard, &list);
756 debug_print("imap list run - end\n");
759 int imap_threaded_list(Folder * folder, const char * base,
760 const char * wildcard,
763 struct list_param param;
764 struct list_result result;
766 debug_print("imap list - begin\n");
768 param.imap = get_imap(folder);
770 param.wildcard = wildcard;
771 param.sub_only = FALSE;
773 threaded_run(folder, ¶m, &result, list_run);
775 * p_result = result.list;
777 debug_print("imap list - end %p\n", result.list);
782 int imap_threaded_lsub(Folder * folder, const char * base,
783 const char * wildcard,
786 struct list_param param;
787 struct list_result result;
789 debug_print("imap lsub - begin\n");
791 param.imap = get_imap(folder);
793 param.wildcard = wildcard;
794 param.sub_only = TRUE;
796 threaded_run(folder, ¶m, &result, list_run);
798 * p_result = result.list;
800 debug_print("imap lsub - end %p\n", result.list);
805 struct subscribe_param {
811 struct subscribe_result {
815 static void subscribe_run(struct etpan_thread_op * op)
817 struct subscribe_param * param;
818 struct subscribe_result * result;
826 if (param->mb == NULL) {
828 debug_print("no mb\n");
831 if (param->subscribe)
832 r = mailimap_subscribe(param->imap, param->mb);
834 r = mailimap_unsubscribe(param->imap, param->mb);
836 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
839 int imap_threaded_subscribe(Folder * folder, const char * mb,
842 struct subscribe_param param;
843 struct subscribe_result result;
845 debug_print("imap list - begin\n");
847 param.imap = get_imap(folder);
849 param.subscribe = subscribe;
851 threaded_run(folder, ¶m, &result, subscribe_run);
859 const char * password;
864 struct login_result {
868 static void login_run(struct etpan_thread_op * op)
870 struct login_param * param;
871 struct login_result * result;
873 #ifdef DISABLE_LOG_DURING_LOGIN
882 #ifdef DISABLE_LOG_DURING_LOGIN
883 old_debug = mailstream_debug;
884 mailstream_debug = 0;
886 if (!strcmp(param->type, "LOGIN"))
887 r = mailimap_login(param->imap,
888 param->login, param->password);
889 else if (!strcmp(param->type, "GSSAPI"))
890 r = mailimap_authenticate(param->imap,
891 param->type, param->server, NULL, NULL,
892 param->login, param->login,
893 param->password, NULL);
895 r = mailimap_authenticate(param->imap,
896 param->type, NULL, NULL, NULL,
897 param->login, param->login,
898 param->password, NULL);
899 #ifdef DISABLE_LOG_DURING_LOGIN
900 mailstream_debug = old_debug;
904 if (param->imap->imap_response)
905 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
906 debug_print("imap login run - end %i\n", r);
909 int imap_threaded_login(Folder * folder,
910 const char * login, const char * password,
913 struct login_param param;
914 struct login_result result;
916 debug_print("imap login - begin\n");
918 param.imap = get_imap(folder);
920 param.password = password;
922 if (folder && folder->account)
923 param.server = folder->account->recv_server;
927 threaded_run(folder, ¶m, &result, login_run);
929 debug_print("imap login - end\n");
935 struct status_param {
938 struct mailimap_status_att_list * status_att_list;
941 struct status_result {
943 struct mailimap_mailbox_data_status * data_status;
946 static void status_run(struct etpan_thread_op * op)
948 struct status_param * param;
949 struct status_result * result;
957 r = mailimap_status(param->imap, param->mb,
958 param->status_att_list,
959 &result->data_status);
962 debug_print("imap status run - end %i\n", r);
965 int imap_threaded_status(Folder * folder, const char * mb,
966 struct mailimap_mailbox_data_status ** data_status,
969 struct status_param param;
970 struct status_result result;
971 struct mailimap_status_att_list * status_att_list;
973 debug_print("imap status - begin\n");
975 status_att_list = mailimap_status_att_list_new_empty();
977 mailimap_status_att_list_add(status_att_list,
978 MAILIMAP_STATUS_ATT_MESSAGES);
981 mailimap_status_att_list_add(status_att_list,
982 MAILIMAP_STATUS_ATT_RECENT);
985 mailimap_status_att_list_add(status_att_list,
986 MAILIMAP_STATUS_ATT_UIDNEXT);
989 mailimap_status_att_list_add(status_att_list,
990 MAILIMAP_STATUS_ATT_UIDVALIDITY);
993 mailimap_status_att_list_add(status_att_list,
994 MAILIMAP_STATUS_ATT_UNSEEN);
996 param.imap = get_imap(folder);
998 param.status_att_list = status_att_list;
1000 threaded_run(folder, ¶m, &result, status_run);
1002 debug_print("imap status - end\n");
1004 * data_status = result.data_status;
1006 mailimap_status_att_list_free(status_att_list);
1008 return result.error;
1017 struct noop_result {
1021 static void noop_run(struct etpan_thread_op * op)
1023 struct noop_param * param;
1024 struct noop_result * result;
1028 result = op->result;
1032 r = mailimap_noop(param->imap);
1035 debug_print("imap noop run - end %i\n", r);
1038 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1039 unsigned int *p_recent,
1040 unsigned int *p_expunge,
1041 unsigned int *p_unseen,
1042 unsigned int *p_uidnext,
1043 unsigned int *p_uidval)
1045 struct noop_param param;
1046 struct noop_result result;
1049 debug_print("imap noop - begin\n");
1051 imap = get_imap(folder);
1054 threaded_run(folder, ¶m, &result, noop_run);
1056 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1057 * p_exists = imap->imap_selection_info->sel_exists;
1058 * p_recent = imap->imap_selection_info->sel_recent;
1059 * p_unseen = imap->imap_selection_info->sel_unseen;
1060 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1061 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1069 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1070 imap->imap_response_info->rsp_expunged != NULL) {
1071 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1075 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1076 *p_exists, *p_recent, *p_expunge, *p_unseen,
1077 *p_uidnext, *p_uidval);
1079 return result.error;
1083 struct starttls_result {
1087 static void starttls_run(struct etpan_thread_op * op)
1089 struct connect_param * param;
1090 struct starttls_result * result;
1094 result = op->result;
1098 r = mailimap_starttls(param->imap);
1101 debug_print("imap starttls run - end %i\n", r);
1104 mailimap *imap = param->imap;
1105 mailstream_low *plain_low = NULL;
1106 mailstream_low *tls_low = NULL;
1109 plain_low = mailstream_get_low(imap->imap_stream);
1110 fd = mailstream_low_get_fd(plain_low);
1112 debug_print("imap starttls run - can't get fd\n");
1113 result->error = MAILIMAP_ERROR_STREAM;
1117 tls_low = mailstream_low_tls_open(fd);
1118 if (tls_low == NULL) {
1119 debug_print("imap starttls run - can't tls_open\n");
1120 result->error = MAILIMAP_ERROR_STREAM;
1123 mailstream_low_free(plain_low);
1124 mailstream_set_low(imap->imap_stream, tls_low);
1128 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1130 struct connect_param param;
1131 struct starttls_result result;
1133 unsigned char *certificate;
1135 debug_print("imap starttls - begin\n");
1137 param.imap = get_imap(folder);
1138 param.server = host;
1141 threaded_run(folder, ¶m, &result, starttls_run);
1143 debug_print("imap starttls - end\n");
1145 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1146 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1147 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1148 result.error = MAILIMAP_ERROR_STREAM;
1152 return result.error;
1157 struct create_param {
1162 struct create_result {
1166 static void create_run(struct etpan_thread_op * op)
1168 struct create_param * param;
1169 struct create_result * result;
1173 result = op->result;
1177 r = mailimap_create(param->imap, param->mb);
1180 debug_print("imap create run - end %i\n", r);
1183 int imap_threaded_create(Folder * folder, const char * mb)
1185 struct create_param param;
1186 struct create_result result;
1188 debug_print("imap create - begin\n");
1190 param.imap = get_imap(folder);
1193 threaded_run(folder, ¶m, &result, create_run);
1195 debug_print("imap create - end\n");
1197 return result.error;
1203 struct rename_param {
1206 const char * new_name;
1209 struct rename_result {
1213 static void rename_run(struct etpan_thread_op * op)
1215 struct rename_param * param;
1216 struct rename_result * result;
1220 result = op->result;
1224 r = mailimap_rename(param->imap, param->mb, param->new_name);
1227 debug_print("imap rename run - end %i\n", r);
1230 int imap_threaded_rename(Folder * folder,
1231 const char * mb, const char * new_name)
1233 struct rename_param param;
1234 struct rename_result result;
1236 debug_print("imap rename - begin\n");
1238 param.imap = get_imap(folder);
1240 param.new_name = new_name;
1242 threaded_run(folder, ¶m, &result, rename_run);
1244 debug_print("imap rename - end\n");
1246 return result.error;
1252 struct delete_param {
1257 struct delete_result {
1261 static void delete_run(struct etpan_thread_op * op)
1263 struct delete_param * param;
1264 struct delete_result * result;
1268 result = op->result;
1272 r = mailimap_delete(param->imap, param->mb);
1275 debug_print("imap delete run - end %i\n", r);
1278 int imap_threaded_delete(Folder * folder, const char * mb)
1280 struct delete_param param;
1281 struct delete_result result;
1283 debug_print("imap delete - begin\n");
1285 param.imap = get_imap(folder);
1288 threaded_run(folder, ¶m, &result, delete_run);
1290 debug_print("imap delete - end\n");
1292 return result.error;
1297 struct select_param {
1302 struct select_result {
1306 static void select_run(struct etpan_thread_op * op)
1308 struct select_param * param;
1309 struct select_result * result;
1313 result = op->result;
1317 r = mailimap_select(param->imap, param->mb);
1320 debug_print("imap select run - end %i\n", r);
1323 int imap_threaded_select(Folder * folder, const char * mb,
1324 gint * exists, gint * recent, gint * unseen,
1325 guint32 * uid_validity,gint *can_create_flags)
1327 struct select_param param;
1328 struct select_result result;
1331 debug_print("imap select - begin\n");
1333 imap = get_imap(folder);
1337 threaded_run(folder, ¶m, &result, select_run);
1339 if (result.error != MAILIMAP_NO_ERROR)
1340 return result.error;
1342 if (!imap || imap->imap_selection_info == NULL)
1343 return MAILIMAP_ERROR_PARSE;
1345 * exists = imap->imap_selection_info->sel_exists;
1346 * recent = imap->imap_selection_info->sel_recent;
1347 * unseen = imap->imap_selection_info->sel_unseen;
1348 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1349 * can_create_flags = FALSE;
1351 if (imap->imap_selection_info->sel_perm_flags) {
1353 clist_begin(imap->imap_selection_info->sel_perm_flags);
1354 for (; cur && !(*can_create_flags); cur = clist_next(cur)) {
1355 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1356 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1357 *can_create_flags = TRUE;
1358 else if (flag->fl_flag &&
1359 flag->fl_flag->fl_type == 6 &&
1360 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1361 *can_create_flags = TRUE;
1365 debug_print("imap select - end\n");
1367 return result.error;
1370 static void close_run(struct etpan_thread_op * op)
1372 struct select_param * param;
1373 struct select_result * result;
1377 result = op->result;
1381 r = mailimap_close(param->imap);
1384 debug_print("imap close run - end %i\n", r);
1387 int imap_threaded_close(Folder * folder)
1389 struct select_param param;
1390 struct select_result result;
1393 debug_print("imap close - begin\n");
1395 imap = get_imap(folder);
1398 threaded_run(folder, ¶m, &result, close_run);
1400 if (result.error != MAILIMAP_NO_ERROR)
1401 return result.error;
1403 debug_print("imap close - end\n");
1405 return result.error;
1408 struct examine_param {
1413 struct examine_result {
1417 static void examine_run(struct etpan_thread_op * op)
1419 struct examine_param * param;
1420 struct examine_result * result;
1424 result = op->result;
1428 r = mailimap_examine(param->imap, param->mb);
1431 debug_print("imap examine run - end %i\n", r);
1434 int imap_threaded_examine(Folder * folder, const char * mb,
1435 gint * exists, gint * recent, gint * unseen,
1436 guint32 * uid_validity)
1438 struct examine_param param;
1439 struct examine_result result;
1442 debug_print("imap examine - begin\n");
1444 imap = get_imap(folder);
1448 threaded_run(folder, ¶m, &result, examine_run);
1450 if (result.error != MAILIMAP_NO_ERROR)
1451 return result.error;
1453 if (!imap || imap->imap_selection_info == NULL)
1454 return MAILIMAP_ERROR_PARSE;
1456 * exists = imap->imap_selection_info->sel_exists;
1457 * recent = imap->imap_selection_info->sel_recent;
1458 * unseen = imap->imap_selection_info->sel_unseen;
1459 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1461 debug_print("imap examine - end\n");
1463 return result.error;
1469 struct search_param {
1472 struct mailimap_set * set;
1475 struct search_result {
1477 clist * search_result;
1480 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1482 return mailimap_set_item_new(orig->set_first, orig->set_last);
1485 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1487 clist *list = orig ? orig->set_list : NULL;
1488 clist *newlist = clist_new();
1493 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1494 clist_append(newlist,
1495 sc_mailimap_set_item_copy(
1496 (struct mailimap_set_item *)clist_content(cur)));
1497 return mailimap_set_new(newlist);
1500 static void search_run(struct etpan_thread_op * op)
1502 struct search_param * param;
1503 struct search_result * result;
1505 struct mailimap_search_key * key = NULL;
1506 struct mailimap_search_key * uid_key = NULL;
1507 struct mailimap_search_key * search_type_key;
1508 clist * search_result;
1511 result = op->result;
1515 /* we copy the mailimap_set because freeing the key is recursive */
1516 if (param->set != NULL) {
1517 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1518 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1519 uid_key = mailimap_search_key_new_all();
1521 search_type_key = NULL;
1522 switch (param->type) {
1523 case IMAP_SEARCH_TYPE_SIMPLE:
1524 search_type_key = NULL;
1527 case IMAP_SEARCH_TYPE_SEEN:
1528 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1529 NULL, NULL, NULL, NULL, NULL,
1530 NULL, NULL, NULL, NULL, NULL,
1531 NULL, NULL, NULL, NULL, 0,
1532 NULL, NULL, NULL, NULL, NULL,
1533 NULL, 0, NULL, NULL, NULL);
1536 case IMAP_SEARCH_TYPE_UNSEEN:
1537 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1538 NULL, NULL, NULL, NULL, NULL,
1539 NULL, NULL, NULL, NULL, NULL,
1540 NULL, NULL, NULL, NULL, 0,
1541 NULL, NULL, NULL, NULL, NULL,
1542 NULL, 0, NULL, NULL, NULL);
1545 case IMAP_SEARCH_TYPE_ANSWERED:
1546 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1547 NULL, NULL, NULL, NULL, NULL,
1548 NULL, NULL, NULL, NULL, NULL,
1549 NULL, NULL, NULL, NULL, 0,
1550 NULL, NULL, NULL, NULL, NULL,
1551 NULL, 0, NULL, NULL, NULL);
1554 case IMAP_SEARCH_TYPE_FLAGGED:
1555 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1556 NULL, NULL, NULL, NULL, NULL,
1557 NULL, NULL, NULL, NULL, NULL,
1558 NULL, NULL, NULL, NULL, 0,
1559 NULL, NULL, NULL, NULL, NULL,
1560 NULL, 0, NULL, NULL, NULL);
1562 case IMAP_SEARCH_TYPE_DELETED:
1563 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1564 NULL, NULL, NULL, NULL, NULL,
1565 NULL, NULL, NULL, NULL, NULL,
1566 NULL, NULL, NULL, NULL, 0,
1567 NULL, NULL, NULL, NULL, NULL,
1568 NULL, 0, NULL, NULL, NULL);
1572 if (search_type_key != NULL) {
1573 if (param->set != NULL) {
1574 key = mailimap_search_key_new_multiple_empty();
1575 mailimap_search_key_multiple_add(key, search_type_key);
1576 mailimap_search_key_multiple_add(key, uid_key);
1578 key = search_type_key;
1580 } else if (uid_key != NULL) {
1585 g_warning("no key!");
1586 result = op->result;
1588 result->search_result = NULL;
1590 mailstream_logger = imap_logger_uid;
1592 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1594 mailstream_logger = imap_logger_cmd;
1596 /* free the key (with the imapset) */
1597 mailimap_search_key_free(key);
1600 result->search_result = search_result;
1602 debug_print("imap search run - end %i\n", result->error);
1605 int imap_threaded_search(Folder * folder, int search_type,
1606 struct mailimap_set * set, clist ** search_result)
1608 struct search_param param;
1609 struct search_result result;
1612 debug_print("imap search - begin\n");
1614 imap = get_imap(folder);
1617 param.type = search_type;
1619 threaded_run(folder, ¶m, &result, search_run);
1621 if (result.error != MAILIMAP_NO_ERROR)
1622 return result.error;
1624 debug_print("imap search - end\n");
1626 * search_result = result.search_result;
1628 return result.error;
1633 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1637 struct mailimap_msg_att_dynamic ** patt_dyn);
1640 result_to_uid_list(clist * fetch_result, carray ** result)
1647 tab = carray_new(128);
1649 res = MAILIMAP_ERROR_MEMORY;
1653 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1654 cur = clist_next(cur)) {
1655 struct mailimap_msg_att * msg_att;
1659 msg_att = clist_content(cur);
1662 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1664 puid = malloc(sizeof(* puid));
1666 res = MAILIMAP_ERROR_MEMORY;
1671 r = carray_add(tab, puid, NULL);
1674 res = MAILIMAP_ERROR_MEMORY;
1681 return MAILIMAP_NO_ERROR;
1684 imap_fetch_uid_list_free(tab);
1689 static int imap_get_messages_list(mailimap * imap,
1690 uint32_t first_index,
1695 struct mailimap_fetch_att * fetch_att;
1696 struct mailimap_fetch_type * fetch_type;
1697 struct mailimap_set * set;
1698 clist * fetch_result;
1701 set = mailimap_set_new_interval(first_index, 0);
1703 res = MAILIMAP_ERROR_MEMORY;
1707 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1708 if (fetch_type == NULL) {
1709 res = MAILIMAP_ERROR_MEMORY;
1713 fetch_att = mailimap_fetch_att_new_uid();
1714 if (fetch_att == NULL) {
1715 res = MAILIMAP_ERROR_MEMORY;
1716 goto free_fetch_type;
1719 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1720 if (r != MAILIMAP_NO_ERROR) {
1721 mailimap_fetch_att_free(fetch_att);
1722 res = MAILIMAP_ERROR_MEMORY;
1723 goto free_fetch_type;
1726 mailstream_logger = imap_logger_fetch;
1728 r = mailimap_uid_fetch(imap, set,
1729 fetch_type, &fetch_result);
1731 mailstream_logger = imap_logger_cmd;
1732 mailimap_fetch_type_free(fetch_type);
1733 mailimap_set_free(set);
1735 if (r != MAILIMAP_NO_ERROR) {
1741 r = result_to_uid_list(fetch_result, &env_list);
1742 mailimap_fetch_list_free(fetch_result);
1744 * result = env_list;
1746 return MAILIMAP_NO_ERROR;
1749 mailimap_fetch_type_free(fetch_type);
1751 mailimap_set_free(set);
1759 struct fetch_uid_param {
1761 uint32_t first_index;
1764 struct fetch_uid_result {
1766 carray * fetch_result;
1769 static void fetch_uid_run(struct etpan_thread_op * op)
1771 struct fetch_uid_param * param;
1772 struct fetch_uid_result * result;
1773 carray * fetch_result;
1777 result = op->result;
1781 fetch_result = NULL;
1782 mailstream_logger = imap_logger_noop;
1783 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1785 r = imap_get_messages_list(param->imap, param->first_index,
1788 mailstream_logger = imap_logger_cmd;
1791 result->fetch_result = fetch_result;
1792 debug_print("imap fetch_uid run - end %i\n", r);
1795 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1796 carray ** fetch_result)
1798 struct fetch_uid_param param;
1799 struct fetch_uid_result result;
1802 debug_print("imap fetch_uid - begin\n");
1804 imap = get_imap(folder);
1806 param.first_index = first_index;
1808 threaded_run(folder, ¶m, &result, fetch_uid_run);
1810 if (result.error != MAILIMAP_NO_ERROR)
1811 return result.error;
1813 debug_print("imap fetch_uid - end\n");
1815 * fetch_result = result.fetch_result;
1817 return result.error;
1821 void imap_fetch_uid_list_free(carray * uid_list)
1825 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1828 puid = carray_get(uid_list, i);
1831 carray_free(uid_list);
1837 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1840 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1846 GSList *tags = NULL;
1848 tab = carray_new(128);
1850 res = MAILIMAP_ERROR_MEMORY;
1854 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1855 cur = clist_next(cur)) {
1856 struct mailimap_msg_att * msg_att;
1859 struct mailimap_msg_att_dynamic * att_dyn;
1865 msg_att = clist_content(cur);
1869 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1872 if (att_dyn == NULL)
1875 flags = imap_flags_to_flags(att_dyn, &tags);
1877 puid = malloc(sizeof(* puid));
1879 res = MAILIMAP_ERROR_MEMORY;
1884 r = carray_add(tab, puid, NULL);
1887 res = MAILIMAP_ERROR_MEMORY;
1890 pflags = malloc(sizeof(* pflags));
1891 if (pflags == NULL) {
1892 res = MAILIMAP_ERROR_MEMORY;
1896 r = carray_add(tab, pflags, NULL);
1899 res = MAILIMAP_ERROR_MEMORY;
1902 r = carray_add(tab, tags, NULL);
1905 res = MAILIMAP_ERROR_MEMORY;
1912 return MAILIMAP_NO_ERROR;
1915 imap_fetch_uid_flags_list_free(tab);
1916 slist_free_strings(tags);
1922 static int imap_get_messages_flags_list(mailimap * imap,
1923 uint32_t first_index,
1928 struct mailimap_fetch_att * fetch_att;
1929 struct mailimap_fetch_type * fetch_type;
1930 struct mailimap_set * set;
1931 clist * fetch_result;
1934 set = mailimap_set_new_interval(first_index, 0);
1936 res = MAILIMAP_ERROR_MEMORY;
1940 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1941 if (fetch_type == NULL) {
1942 res = MAILIMAP_ERROR_MEMORY;
1946 fetch_att = mailimap_fetch_att_new_flags();
1947 if (fetch_att == NULL) {
1948 res = MAILIMAP_ERROR_MEMORY;
1949 goto free_fetch_type;
1952 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1953 if (r != MAILIMAP_NO_ERROR) {
1954 mailimap_fetch_att_free(fetch_att);
1955 res = MAILIMAP_ERROR_MEMORY;
1956 goto free_fetch_type;
1959 fetch_att = mailimap_fetch_att_new_uid();
1960 if (fetch_att == NULL) {
1961 res = MAILIMAP_ERROR_MEMORY;
1962 goto free_fetch_type;
1965 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1966 if (r != MAILIMAP_NO_ERROR) {
1967 mailimap_fetch_att_free(fetch_att);
1968 res = MAILIMAP_ERROR_MEMORY;
1969 goto free_fetch_type;
1972 mailstream_logger = imap_logger_fetch;
1974 r = mailimap_uid_fetch(imap, set,
1975 fetch_type, &fetch_result);
1977 mailstream_logger = imap_logger_cmd;
1978 mailimap_fetch_type_free(fetch_type);
1979 mailimap_set_free(set);
1981 if (r != MAILIMAP_NO_ERROR) {
1987 r = result_to_uid_flags_list(fetch_result, &env_list);
1988 mailimap_fetch_list_free(fetch_result);
1990 * result = env_list;
1992 return MAILIMAP_NO_ERROR;
1995 mailimap_fetch_type_free(fetch_type);
1997 mailimap_set_free(set);
2004 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2006 struct fetch_uid_param * param;
2007 struct fetch_uid_result * result;
2008 carray * fetch_result;
2012 result = op->result;
2016 fetch_result = NULL;
2017 r = imap_get_messages_flags_list(param->imap, param->first_index,
2021 result->fetch_result = fetch_result;
2022 debug_print("imap fetch_uid run - end %i\n", r);
2025 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2026 carray ** fetch_result)
2028 struct fetch_uid_param param;
2029 struct fetch_uid_result result;
2032 debug_print("imap fetch_uid - begin\n");
2034 imap = get_imap(folder);
2036 param.first_index = first_index;
2038 mailstream_logger = imap_logger_noop;
2039 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2041 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2043 mailstream_logger = imap_logger_cmd;
2046 if (result.error != MAILIMAP_NO_ERROR)
2047 return result.error;
2049 debug_print("imap fetch_uid - end\n");
2051 * fetch_result = result.fetch_result;
2053 return result.error;
2057 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2061 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2064 data = carray_get(uid_flags_list, i);
2066 data = carray_get(uid_flags_list, i + 1);
2069 carray_free(uid_flags_list);
2074 static int imap_fetch(mailimap * imap,
2077 size_t * result_len)
2080 struct mailimap_set * set;
2081 struct mailimap_fetch_att * fetch_att;
2082 struct mailimap_fetch_type * fetch_type;
2083 clist * fetch_result;
2084 struct mailimap_msg_att * msg_att;
2085 struct mailimap_msg_att_item * msg_att_item;
2090 struct mailimap_section * section;
2092 set = mailimap_set_new_single(msg_index);
2094 res = MAILIMAP_ERROR_MEMORY;
2098 section = mailimap_section_new(NULL);
2099 if (section == NULL) {
2100 res = MAILIMAP_ERROR_MEMORY;
2104 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2105 if (fetch_att == NULL) {
2106 mailimap_section_free(section);
2107 res = MAILIMAP_ERROR_MEMORY;
2111 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2112 if (fetch_type == NULL) {
2113 res = MAILIMAP_ERROR_MEMORY;
2114 goto free_fetch_att;
2117 mailstream_logger = imap_logger_fetch;
2119 r = mailimap_uid_fetch(imap, set,
2120 fetch_type, &fetch_result);
2122 mailstream_logger = imap_logger_cmd;
2124 mailimap_fetch_type_free(fetch_type);
2125 mailimap_set_free(set);
2128 case MAILIMAP_NO_ERROR:
2134 if (clist_begin(fetch_result) == NULL) {
2135 mailimap_fetch_list_free(fetch_result);
2136 return MAILIMAP_ERROR_FETCH;
2139 msg_att = clist_begin(fetch_result)->data;
2144 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2145 cur = clist_next(cur)) {
2146 msg_att_item = clist_content(cur);
2148 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2149 if (msg_att_item->att_data.att_static->att_type ==
2150 MAILIMAP_MSG_ATT_BODY_SECTION) {
2151 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2153 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2155 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2160 mailimap_fetch_list_free(fetch_result);
2163 return MAILIMAP_ERROR_FETCH;
2166 * result_len = text_length;
2168 return MAILIMAP_NO_ERROR;
2171 mailimap_fetch_att_free(fetch_att);
2173 mailimap_set_free(set);
2178 static int imap_fetch_header(mailimap * imap,
2181 size_t * result_len)
2184 struct mailimap_set * set;
2185 struct mailimap_fetch_att * fetch_att;
2186 struct mailimap_fetch_type * fetch_type;
2187 clist * fetch_result;
2188 struct mailimap_msg_att * msg_att;
2189 struct mailimap_msg_att_item * msg_att_item;
2194 struct mailimap_section * section;
2196 set = mailimap_set_new_single(msg_index);
2198 res = MAILIMAP_ERROR_MEMORY;
2202 section = mailimap_section_new_header();
2203 if (section == NULL) {
2204 res = MAILIMAP_ERROR_MEMORY;
2208 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2209 if (fetch_att == NULL) {
2210 mailimap_section_free(section);
2211 res = MAILIMAP_ERROR_MEMORY;
2215 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2216 if (fetch_type == NULL) {
2217 res = MAILIMAP_ERROR_MEMORY;
2218 goto free_fetch_att;
2221 mailstream_logger = imap_logger_fetch;
2223 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2225 mailstream_logger = imap_logger_cmd;
2226 mailimap_fetch_type_free(fetch_type);
2227 mailimap_set_free(set);
2230 case MAILIMAP_NO_ERROR:
2236 if (clist_begin(fetch_result) == NULL) {
2237 mailimap_fetch_list_free(fetch_result);
2238 return MAILIMAP_ERROR_FETCH;
2241 msg_att = clist_begin(fetch_result)->data;
2246 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2247 cur = clist_next(cur)) {
2248 msg_att_item = clist_content(cur);
2250 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2251 if (msg_att_item->att_data.att_static->att_type ==
2252 MAILIMAP_MSG_ATT_BODY_SECTION) {
2253 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2254 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2256 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2261 mailimap_fetch_list_free(fetch_result);
2264 return MAILIMAP_ERROR_FETCH;
2267 * result_len = text_length;
2269 return MAILIMAP_NO_ERROR;
2272 mailimap_fetch_att_free(fetch_att);
2274 mailimap_set_free(set);
2281 struct fetch_content_param {
2284 const char * filename;
2288 struct fetch_content_result {
2292 static void fetch_content_run(struct etpan_thread_op * op)
2294 struct fetch_content_param * param;
2295 struct fetch_content_result * result;
2297 size_t content_size;
2303 result = op->result;
2309 if (param->with_body)
2310 r = imap_fetch(param->imap, param->msg_index,
2311 &content, &content_size);
2313 r = imap_fetch_header(param->imap, param->msg_index,
2314 &content, &content_size);
2318 if (r == MAILIMAP_NO_ERROR) {
2319 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2321 result->error = MAILIMAP_ERROR_FETCH;
2325 f = fdopen(fd, "wb");
2327 result->error = MAILIMAP_ERROR_FETCH;
2331 r = fwrite(content, 1, content_size, f);
2332 if (r < content_size) {
2348 g_unlink(param->filename);
2351 /* mmap_string_unref is a simple free in libetpan
2352 * when it has MMAP_UNAVAILABLE defined */
2353 if (mmap_string_unref(content) != 0)
2357 debug_print("imap fetch_content run - end %i\n", r);
2360 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2362 const char * filename)
2364 struct fetch_content_param param;
2365 struct fetch_content_result result;
2368 debug_print("imap fetch_content - begin\n");
2370 imap = get_imap(folder);
2372 param.msg_index = msg_index;
2373 param.filename = filename;
2374 param.with_body = with_body;
2376 threaded_run(folder, ¶m, &result, fetch_content_run);
2378 if (result.error != MAILIMAP_NO_ERROR)
2379 return result.error;
2381 debug_print("imap fetch_content - end\n");
2383 return result.error;
2388 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2393 GSList *tags = NULL;
2397 flag_list = att_dyn->att_list;
2398 if (flag_list == NULL)
2401 for(cur = clist_begin(flag_list) ; cur != NULL ;
2402 cur = clist_next(cur)) {
2403 struct mailimap_flag_fetch * flag_fetch;
2405 flag_fetch = clist_content(cur);
2406 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2409 switch (flag_fetch->fl_flag->fl_type) {
2410 case MAILIMAP_FLAG_ANSWERED:
2411 flags |= MSG_REPLIED;
2413 case MAILIMAP_FLAG_FLAGGED:
2414 flags |= MSG_MARKED;
2416 case MAILIMAP_FLAG_DELETED:
2417 flags |= MSG_DELETED;
2419 case MAILIMAP_FLAG_SEEN:
2420 flags &= ~MSG_UNREAD;
2423 case MAILIMAP_FLAG_KEYWORD:
2425 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2435 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2439 struct mailimap_msg_att_dynamic ** patt_dyn)
2441 clistiter * item_cur;
2445 struct mailimap_msg_att_dynamic * att_dyn;
2452 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2453 item_cur = clist_next(item_cur)) {
2454 struct mailimap_msg_att_item * item;
2456 item = clist_content(item_cur);
2458 switch (item->att_type) {
2459 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2460 switch (item->att_data.att_static->att_type) {
2461 case MAILIMAP_MSG_ATT_UID:
2462 uid = item->att_data.att_static->att_data.att_uid;
2465 case MAILIMAP_MSG_ATT_BODY_SECTION:
2466 if (headers == NULL) {
2467 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2470 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2471 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2476 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2477 if (att_dyn == NULL) {
2478 att_dyn = item->att_data.att_dyn;
2486 if (pheaders != NULL)
2487 * pheaders = headers;
2488 if (pref_size != NULL)
2489 * pref_size = ref_size;
2490 if (patt_dyn != NULL)
2491 * patt_dyn = att_dyn;
2493 return MAIL_NO_ERROR;
2496 static struct imap_fetch_env_info *
2497 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2499 struct imap_fetch_env_info * info;
2503 struct mailimap_msg_att_dynamic * att_dyn;
2505 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2510 info = malloc(sizeof(* info));
2512 info->headers = strdup(headers);
2514 info->flags = imap_flags_to_flags(att_dyn, tags);
2520 imap_fetch_result_to_envelop_list(clist * fetch_result,
2521 carray ** p_env_list)
2528 env_list = carray_new(16);
2530 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2531 cur = clist_next(cur)) {
2532 struct mailimap_msg_att * msg_att;
2533 struct imap_fetch_env_info * env_info;
2534 GSList *tags = NULL;
2536 msg_att = clist_content(cur);
2538 env_info = fetch_to_env_info(msg_att, &tags);
2540 return MAILIMAP_ERROR_MEMORY;
2541 carray_add(env_list, env_info, NULL);
2542 carray_add(env_list, tags, NULL);
2545 * p_env_list = env_list;
2547 return MAIL_NO_ERROR;
2550 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2552 struct mailimap_fetch_att * fetch_att;
2556 struct mailimap_header_list * imap_hdrlist;
2557 struct mailimap_section * section;
2559 hdrlist = clist_new();
2561 header = strdup("Date");
2562 r = clist_append(hdrlist, header);
2563 header = strdup("From");
2564 r = clist_append(hdrlist, header);
2565 header = strdup("To");
2566 r = clist_append(hdrlist, header);
2567 header = strdup("Cc");
2568 r = clist_append(hdrlist, header);
2569 header = strdup("Subject");
2570 r = clist_append(hdrlist, header);
2571 header = strdup("Message-ID");
2572 r = clist_append(hdrlist, header);
2573 header = strdup("References");
2574 r = clist_append(hdrlist, header);
2575 header = strdup("In-Reply-To");
2576 r = clist_append(hdrlist, header);
2578 imap_hdrlist = mailimap_header_list_new(hdrlist);
2579 section = mailimap_section_new_header_fields(imap_hdrlist);
2580 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2581 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2583 return MAIL_NO_ERROR;
2586 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2588 struct mailimap_fetch_att * fetch_att;
2589 struct mailimap_section * section;
2591 section = mailimap_section_new_header();
2592 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2593 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2595 return MAIL_NO_ERROR;
2599 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2600 carray ** p_env_list)
2602 struct mailimap_fetch_att * fetch_att;
2603 struct mailimap_fetch_type * fetch_type;
2605 clist * fetch_result;
2607 carray * env_list = NULL;
2611 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2614 fetch_att = mailimap_fetch_att_new_uid();
2615 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2618 fetch_att = mailimap_fetch_att_new_flags();
2619 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2622 fetch_att = mailimap_fetch_att_new_rfc822_size();
2623 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2627 key.len = sizeof(imap);
2628 r = chash_get(courier_workaround_hash, &key, &value);
2630 r = imap_add_envelope_fetch_att(fetch_type);
2632 r = imap_add_header_fetch_att(fetch_type);
2634 mailstream_logger = imap_logger_fetch;
2636 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2638 mailstream_logger = imap_logger_cmd;
2640 case MAILIMAP_NO_ERROR:
2643 mailimap_fetch_type_free(fetch_type);
2644 debug_print("uid_fetch: %d\n", r);
2648 if (clist_begin(fetch_result) == NULL) {
2649 res = MAILIMAP_ERROR_FETCH;
2650 debug_print("clist_begin = NULL\n");
2654 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2655 mailimap_fetch_list_free(fetch_result);
2657 if (r != MAILIMAP_NO_ERROR) {
2658 mailimap_fetch_type_free(fetch_type);
2659 res = MAILIMAP_ERROR_MEMORY;
2660 debug_print("fetch_result_to_envelop_list: %d\n", res);
2664 mailimap_fetch_type_free(fetch_type);
2666 * p_env_list = env_list;
2668 return MAILIMAP_NO_ERROR;
2674 struct fetch_env_param {
2676 struct mailimap_set * set;
2679 struct fetch_env_result {
2680 carray * fetch_env_result;
2684 static void fetch_env_run(struct etpan_thread_op * op)
2686 struct fetch_env_param * param;
2687 struct fetch_env_result * result;
2692 result = op->result;
2697 r = imap_get_envelopes_list(param->imap, param->set,
2701 result->fetch_env_result = env_list;
2703 debug_print("imap fetch_env run - end %i\n", r);
2706 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2707 carray ** p_env_list)
2709 struct fetch_env_param param;
2710 struct fetch_env_result result;
2713 debug_print("imap fetch_env - begin\n");
2715 imap = get_imap(folder);
2719 threaded_run(folder, ¶m, &result, fetch_env_run);
2721 if (result.error != MAILIMAP_NO_ERROR) {
2727 key.len = sizeof(imap);
2728 r = chash_get(courier_workaround_hash, &key, &value);
2732 chash_set(courier_workaround_hash, &key, &value, NULL);
2734 threaded_run(folder, ¶m, &result, fetch_env_run);
2738 if (result.error != MAILIMAP_NO_ERROR)
2739 return result.error;
2741 debug_print("imap fetch_env - end\n");
2743 * p_env_list = result.fetch_env_result;
2745 return result.error;
2748 void imap_fetch_env_free(carray * env_list)
2752 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2753 struct imap_fetch_env_info * env_info;
2755 env_info = carray_get(env_list, i);
2756 free(env_info->headers);
2759 carray_free(env_list);
2766 struct append_param {
2768 const char * mailbox;
2769 const char * filename;
2770 struct mailimap_flag_list * flag_list;
2773 struct append_result {
2778 static void append_run(struct etpan_thread_op * op)
2780 struct append_param * param;
2781 struct append_result * result;
2786 struct stat stat_buf;
2789 guint32 uid = 0, val = 0;
2792 result = op->result;
2797 r = stat(param->filename, &stat_buf);
2799 result->error = MAILIMAP_ERROR_APPEND;
2802 size = stat_buf.st_size;
2804 fd = open(param->filename, O_RDONLY);
2806 result->error = MAILIMAP_ERROR_APPEND;
2810 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2811 if (data == (void *) MAP_FAILED) {
2813 result->error = MAILIMAP_ERROR_APPEND;
2817 data = file_read_to_str_no_recode(param->filename);
2819 result->error = MAILIMAP_ERROR_APPEND;
2822 size = strlen(data);
2824 mailstream_logger = imap_logger_append;
2826 r = mailimap_uidplus_append(param->imap, param->mailbox,
2827 param->flag_list, NULL,
2828 data, size, &val, &uid);
2830 mailstream_logger = imap_logger_cmd;
2841 debug_print("imap append run - end %i uid %d\n", r, uid);
2844 int imap_threaded_append(Folder * folder, const char * mailbox,
2845 const char * filename,
2846 struct mailimap_flag_list * flag_list,
2849 struct append_param param;
2850 struct append_result result;
2853 debug_print("imap append - begin\n");
2855 imap = get_imap(folder);
2857 param.mailbox = mailbox;
2858 param.filename = filename;
2859 param.flag_list = flag_list;
2861 threaded_run(folder, ¶m, &result, append_run);
2863 if (result.error != MAILIMAP_NO_ERROR)
2864 return result.error;
2866 debug_print("imap append - end\n");
2870 return result.error;
2876 struct expunge_param {
2880 struct expunge_result {
2884 static void expunge_run(struct etpan_thread_op * op)
2886 struct expunge_param * param;
2887 struct expunge_result * result;
2891 result = op->result;
2895 r = mailimap_expunge(param->imap);
2898 debug_print("imap expunge run - end %i\n", r);
2901 int imap_threaded_expunge(Folder * folder)
2903 struct expunge_param param;
2904 struct expunge_result result;
2906 debug_print("imap expunge - begin\n");
2908 param.imap = get_imap(folder);
2910 threaded_run(folder, ¶m, &result, expunge_run);
2912 debug_print("imap expunge - end\n");
2914 return result.error;
2920 struct mailimap_set * set;
2924 struct copy_result {
2926 struct mailimap_set *source;
2927 struct mailimap_set *dest;
2930 static void copy_run(struct etpan_thread_op * op)
2932 struct copy_param * param;
2933 struct copy_result * result;
2936 struct mailimap_set *source = NULL, *dest = NULL;
2939 result = op->result;
2943 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2944 &val, &source, &dest);
2948 result->source = source;
2949 result->dest = dest;
2951 result->source = NULL;
2952 result->dest = NULL;
2954 debug_print("imap copy run - end %i\n", r);
2957 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2958 const char * mb, struct mailimap_set **source,
2959 struct mailimap_set **dest)
2961 struct copy_param param;
2962 struct copy_result result;
2965 debug_print("imap copy - begin\n");
2967 imap = get_imap(folder);
2972 threaded_run(folder, ¶m, &result, copy_run);
2976 if (result.error != MAILIMAP_NO_ERROR)
2977 return result.error;
2979 *source = result.source;
2980 *dest = result.dest;
2982 debug_print("imap copy - end\n");
2984 return result.error;
2989 struct store_param {
2991 struct mailimap_set * set;
2992 struct mailimap_store_att_flags * store_att_flags;
2995 struct store_result {
2999 static void store_run(struct etpan_thread_op * op)
3001 struct store_param * param;
3002 struct store_result * result;
3006 result = op->result;
3010 r = mailimap_uid_store(param->imap, param->set,
3011 param->store_att_flags);
3015 debug_print("imap store run - end %i\n", r);
3018 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3019 struct mailimap_store_att_flags * store_att_flags)
3021 struct store_param param;
3022 struct store_result result;
3025 debug_print("imap store - begin\n");
3027 imap = get_imap(folder);
3030 param.store_att_flags = store_att_flags;
3032 threaded_run(folder, ¶m, &result, store_run);
3034 if (result.error != MAILIMAP_NO_ERROR)
3035 return result.error;
3037 debug_print("imap store - end\n");
3039 return result.error;
3043 #define ENV_BUFFER_SIZE 512
3045 static void do_exec_command(int fd, const char * command,
3046 const char * servername, uint16_t port)
3050 char env_buffer[ENV_BUFFER_SIZE];
3054 /* Fork again to become a child of init rather than
3055 the etpan client. */
3061 snprintf(env_buffer, ENV_BUFFER_SIZE,
3062 "ETPANSERVER=%s", servername);
3064 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
3068 setenv("ETPANSERVER", servername, 1);
3070 unsetenv("ETPANSERVER");
3075 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
3077 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
3083 snprintf(porttext, sizeof(porttext), "%d", port);
3084 setenv("ETPANPORT", porttext, 1);
3087 unsetenv("ETPANPORT");
3091 /* Not a lot we can do if there's an error other than bail. */
3092 if (dup2(fd, 0) == -1)
3094 if (dup2(fd, 1) == -1)
3097 /* Should we close stderr and reopen /dev/null? */
3099 maxopen = sysconf(_SC_OPEN_MAX);
3100 for (i=3; i < maxopen; i++)
3104 /* Detach from the controlling tty if we have one. Otherwise,
3105 SSH might do something stupid like trying to use it instead
3106 of running $SSH_ASKPASS. Doh. */
3107 fd = open("/dev/tty", O_RDONLY);
3109 ioctl(fd, TIOCNOTTY, NULL);
3112 #endif /* TIOCNOTTY */
3114 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3116 /* Eep. Shouldn't reach this */
3120 static int subcommand_connect(const char *command,
3121 const char *servername, uint16_t port)
3123 /* SEB unsupported on Windows */
3127 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3132 do_exec_command(sockfds[1], command, servername, port);
3134 else if (childpid == -1) {
3142 /* Reap child, leaving grandchild process to run */
3143 waitpid(childpid, NULL, 0);
3148 static int socket_connect_cmd(mailimap * imap, const char * command,
3149 const char * server, int port)
3155 fd = subcommand_connect(command, server, port);
3157 return MAILIMAP_ERROR_STREAM;
3159 s = mailstream_socket_open(fd);
3162 return MAILIMAP_ERROR_STREAM;
3165 r = mailimap_connect(imap, s);
3166 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3167 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3168 mailstream_close(s);
3170 imap->imap_stream = NULL;
3179 struct connect_cmd_param {
3181 const char * command;
3182 const char * server;
3186 struct connect_cmd_result {
3190 static void connect_cmd_run(struct etpan_thread_op * op)
3193 struct connect_cmd_param * param;
3194 struct connect_cmd_result * result;
3197 result = op->result;
3201 r = socket_connect_cmd(param->imap, param->command,
3202 param->server, param->port);
3208 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3209 const char * server, int port)
3211 struct connect_cmd_param param;
3212 struct connect_cmd_result result;
3215 mailimap * imap, * oldimap;
3217 oldimap = get_imap(folder);
3219 imap = mailimap_new(0, NULL);
3222 debug_print("deleting old imap %p\n", oldimap);
3223 delete_imap(folder, oldimap);
3227 key.len = sizeof(folder);
3230 chash_set(session_hash, &key, &value, NULL);
3233 param.command = command;
3234 param.server = server;
3237 threaded_run(folder, ¶m, &result, connect_cmd_run);
3239 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3241 return result.error;
3243 #endif /* G_OS_WIN32 */
3245 void imap_threaded_cancel(Folder * folder)
3249 imap = get_imap(folder);
3250 if (imap->imap_stream != NULL)
3251 mailstream_cancel(imap->imap_stream);
3256 void imap_main_init(void)
3259 void imap_main_done(void)
3262 void imap_main_set_timeout(int sec)
3266 void imap_threaded_cancel(Folder * folder);