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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 "ssl_certificate.h"
44 #define DISABLE_LOG_DURING_LOGIN
46 static struct etpan_thread_manager * thread_manager = NULL;
47 static chash * courier_workaround_hash = NULL;
48 static chash * imap_hash = NULL;
49 static chash * session_hash = NULL;
50 static guint thread_manager_signal = 0;
51 static GIOChannel * io_channel = NULL;
53 static void delete_imap(Folder *folder, mailimap *imap)
59 key.len = sizeof(folder);
62 chash_delete(session_hash, &key, NULL);
65 key.len = sizeof(imap);
66 chash_delete(courier_workaround_hash, &key, NULL);
67 if (imap->imap_stream) {
68 /* we don't want libetpan to logout */
69 mailstream_close(imap->imap_stream);
70 imap->imap_stream = NULL;
72 debug_print("removing mailimap %p\n", imap);
76 static gboolean thread_manager_event(GIOChannel * source,
77 GIOCondition condition,
80 etpan_thread_manager_loop(thread_manager);
85 static void imap_logger_cmd(int direction, const char * str, size_t size)
92 log_print("IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
96 memset(buf, 0, size+1);
97 strncpy(buf, str, size);
100 if (!strncmp(buf, "<<<<<<<", 7)
101 || !strncmp(buf, ">>>>>>>", 7)) {
105 while (strstr(buf, "\r"))
106 *strstr(buf, "\r") = ' ';
107 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
108 buf[strlen(buf)-1] = '\0';
110 lines = g_strsplit(buf, "\n", -1);
112 while (lines[i] && *lines[i]) {
113 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
120 static void imap_logger_fetch(int direction, const char * str, size_t size)
127 log_print("IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
131 buf = malloc(size+1);
132 memset(buf, 0, size+1);
133 strncpy(buf, str, size);
135 if (!strncmp(buf, "<<<<<<<", 7)
136 || !strncmp(buf, ">>>>>>>", 7)) {
140 while (strstr(buf, "\r"))
141 *strstr(buf, "\r") = ' ';
142 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
143 buf[strlen(buf)-1] = '\0';
145 lines = g_strsplit(buf, "\n", -1);
147 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
148 while (lines[i] && *lines[i]) {
149 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
153 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
159 static void imap_logger_uid(int direction, const char * str, size_t size)
166 log_print("IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
169 buf = malloc(size+1);
170 memset(buf, 0, size+1);
171 strncpy(buf, str, size);
173 if (!strncmp(buf, "<<<<<<<", 7)
174 || !strncmp(buf, ">>>>>>>", 7)) {
178 while (strstr(buf, "\r"))
179 *strstr(buf, "\r") = ' ';
180 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
181 buf[strlen(buf)-1] = '\0';
183 lines = g_strsplit(buf, "\n", -1);
185 while (lines[i] && *lines[i]) {
186 int llen = strlen(lines[i]);
188 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
191 strncpy2(tmp, lines[i], 63);
192 log_print("IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
201 static void imap_logger_append(int direction, const char * str, size_t size)
208 log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
210 } else if (direction == 0 && size > 64) {
211 log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
214 buf = malloc(size+1);
215 memset(buf, 0, size+1);
216 strncpy(buf, str, size);
218 if (!strncmp(buf, "<<<<<<<", 7)
219 || !strncmp(buf, ">>>>>>>", 7)) {
223 while (strstr(buf, "\r"))
224 *strstr(buf, "\r") = ' ';
225 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
226 buf[strlen(buf)-1] = '\0';
228 lines = g_strsplit(buf, "\n", -1);
230 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
231 while (lines[i] && *lines[i]) {
232 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
236 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
242 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
243 static gboolean etpan_skip_ssl_cert_check = FALSE;
244 extern void mailsasl_ref(void);
246 void imap_main_init(gboolean skip_ssl_cert_check)
248 int fd_thread_manager;
250 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
251 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
252 mailstream_network_delay.tv_usec = 0;
254 mailstream_debug = 1;
255 mailstream_logger = imap_logger_cmd;
258 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
259 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
260 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
262 thread_manager = etpan_thread_manager_new();
264 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
266 io_channel = g_io_channel_unix_new(fd_thread_manager);
268 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
269 thread_manager_event,
274 void imap_main_set_timeout(int sec)
276 mailstream_network_delay.tv_sec = sec;
277 mailstream_network_delay.tv_usec = 0;
280 void imap_main_done(void)
282 etpan_thread_manager_stop(thread_manager);
283 etpan_thread_manager_join(thread_manager);
285 g_source_remove(thread_manager_signal);
286 g_io_channel_unref(io_channel);
288 etpan_thread_manager_free(thread_manager);
290 chash_free(courier_workaround_hash);
291 chash_free(session_hash);
292 chash_free(imap_hash);
295 void imap_init(Folder * folder)
297 struct etpan_thread * thread;
301 thread = etpan_thread_manager_get_thread(thread_manager);
304 key.len = sizeof(folder);
308 chash_set(imap_hash, &key, &value, NULL);
311 void imap_done(Folder * folder)
313 struct etpan_thread * thread;
319 key.len = sizeof(folder);
321 r = chash_get(imap_hash, &key, &value);
327 etpan_thread_unbind(thread);
329 chash_delete(imap_hash, &key, NULL);
331 debug_print("remove thread");
334 static struct etpan_thread * get_thread(Folder * folder)
336 struct etpan_thread * thread;
341 key.len = sizeof(folder);
343 chash_get(imap_hash, &key, &value);
349 static mailimap * get_imap(Folder * folder)
357 key.len = sizeof(folder);
359 r = chash_get(session_hash, &key, &value);
364 debug_print("found imap %p\n", imap);
369 static void generic_cb(int cancelled, void * result, void * callback_data)
373 p_finished = callback_data;
375 debug_print("generic_cb\n");
380 static void threaded_run(Folder * folder, void * param, void * result,
381 void (* func)(struct etpan_thread_op * ))
383 struct etpan_thread_op * op;
384 struct etpan_thread * thread;
387 imap_folder_ref(folder);
389 op = etpan_thread_op_new();
395 op->callback = generic_cb;
396 op->callback_data = &finished;
401 thread = get_thread(folder);
402 etpan_thread_op_schedule(thread, op);
405 gtk_main_iteration();
408 etpan_thread_op_free(op);
410 imap_folder_unref(folder);
416 struct connect_param {
422 struct connect_result {
426 #define CHECK_IMAP() { \
427 if (!param->imap) { \
428 result->error = MAILIMAP_ERROR_BAD_STATE; \
433 static void connect_run(struct etpan_thread_op * op)
436 struct connect_param * param;
437 struct connect_result * result;
444 r = mailimap_socket_connect(param->imap,
445 param->server, param->port);
451 int imap_threaded_connect(Folder * folder, const char * server, int port)
453 struct connect_param param;
454 struct connect_result result;
457 mailimap * imap, * oldimap;
459 oldimap = get_imap(folder);
461 imap = mailimap_new(0, NULL);
464 debug_print("deleting old imap %p\n", oldimap);
465 delete_imap(folder, oldimap);
469 key.len = sizeof(folder);
472 chash_set(session_hash, &key, &value, NULL);
475 param.server = server;
479 threaded_run(folder, ¶m, &result, connect_run);
481 debug_print("connect ok %i with imap %p\n", result.error, imap);
486 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
489 struct connect_param *param = (struct connect_param *)data;
492 if (certificate == NULL || len < 0) {
493 g_warning("no cert presented.\n");
496 cert = d2i_X509(NULL, &certificate, len);
498 g_warning("can't get cert\n");
500 } else if (ssl_certificate_check(cert, NULL,
501 (gchar *)param->server, (gushort)param->port) == TRUE) {
513 static void connect_ssl_run(struct etpan_thread_op * op)
516 struct connect_param * param;
517 struct connect_result * result;
524 r = mailimap_ssl_connect(param->imap,
525 param->server, param->port);
529 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
531 struct connect_param param;
532 struct connect_result result;
535 mailimap * imap, * oldimap;
536 unsigned char *certificate = NULL;
539 oldimap = get_imap(folder);
541 imap = mailimap_new(0, NULL);
544 debug_print("deleting old imap %p\n", oldimap);
545 delete_imap(folder, oldimap);
549 key.len = sizeof(folder);
552 chash_set(session_hash, &key, &value, NULL);
555 param.server = server;
559 threaded_run(folder, ¶m, &result, connect_ssl_run);
561 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
562 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
563 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
564 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
569 debug_print("connect %d with imap %p\n", result.error, imap);
580 struct mailimap_capability_data *caps;
583 static void capability_run(struct etpan_thread_op * op)
586 struct capa_param * param;
587 struct capa_result * result;
588 struct mailimap_capability_data *caps;
595 r = mailimap_capability(param->imap, &caps);
598 result->caps = (r == 0 ? caps : NULL);
602 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
604 struct capa_param param;
605 struct capa_result result;
608 imap = get_imap(folder);
612 threaded_run(folder, ¶m, &result, capability_run);
614 debug_print("capa %d\n", result.error);
623 struct disconnect_param {
627 struct disconnect_result {
631 static void disconnect_run(struct etpan_thread_op * op)
634 struct disconnect_param * param;
635 struct disconnect_result * result;
642 r = mailimap_logout(param->imap);
647 void imap_threaded_disconnect(Folder * folder)
649 struct connect_param param;
650 struct connect_result result;
653 imap = get_imap(folder);
655 debug_print("was disconnected\n");
661 threaded_run(folder, ¶m, &result, disconnect_run);
663 if (imap == get_imap(folder)) {
664 debug_print("deleting old imap %p\n", imap);
665 delete_imap(folder, imap);
667 debug_print("imap already deleted %p\n", imap);
670 debug_print("disconnect ok\n");
677 const char * wildcard;
686 static void list_run(struct etpan_thread_op * op)
688 struct list_param * param;
689 struct list_result * result;
700 if (param->base == NULL || param->wildcard == NULL) {
703 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
707 r = mailimap_lsub(param->imap, param->base,
708 param->wildcard, &list);
710 r = mailimap_list(param->imap, param->base,
711 param->wildcard, &list);
714 debug_print("imap list run - end\n");
717 int imap_threaded_list(Folder * folder, const char * base,
718 const char * wildcard,
721 struct list_param param;
722 struct list_result result;
724 debug_print("imap list - begin\n");
726 param.imap = get_imap(folder);
728 param.wildcard = wildcard;
729 param.sub_only = FALSE;
731 threaded_run(folder, ¶m, &result, list_run);
733 * p_result = result.list;
735 debug_print("imap list - end %p\n", result.list);
740 int imap_threaded_lsub(Folder * folder, const char * base,
741 const char * wildcard,
744 struct list_param param;
745 struct list_result result;
747 debug_print("imap lsub - begin\n");
749 param.imap = get_imap(folder);
751 param.wildcard = wildcard;
752 param.sub_only = TRUE;
754 threaded_run(folder, ¶m, &result, list_run);
756 * p_result = result.list;
758 debug_print("imap lsub - end %p\n", result.list);
763 struct subscribe_param {
769 struct subscribe_result {
773 static void subscribe_run(struct etpan_thread_op * op)
775 struct subscribe_param * param;
776 struct subscribe_result * result;
784 if (param->mb == NULL) {
786 debug_print("no mb\n");
789 if (param->subscribe)
790 r = mailimap_subscribe(param->imap, param->mb);
792 r = mailimap_unsubscribe(param->imap, param->mb);
794 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
797 int imap_threaded_subscribe(Folder * folder, const char * mb,
800 struct subscribe_param param;
801 struct subscribe_result result;
803 debug_print("imap list - begin\n");
805 param.imap = get_imap(folder);
807 param.subscribe = subscribe;
809 threaded_run(folder, ¶m, &result, subscribe_run);
817 const char * password;
822 struct login_result {
826 static void login_run(struct etpan_thread_op * op)
828 struct login_param * param;
829 struct login_result * result;
831 #ifdef DISABLE_LOG_DURING_LOGIN
840 #ifdef DISABLE_LOG_DURING_LOGIN
841 old_debug = mailstream_debug;
842 mailstream_debug = 0;
844 if (!strcmp(param->type, "LOGIN"))
845 r = mailimap_login(param->imap,
846 param->login, param->password);
847 else if (!strcmp(param->type, "GSSAPI"))
848 r = mailimap_authenticate(param->imap,
849 param->type, param->server, NULL, NULL,
850 param->login, param->login,
851 param->password, NULL);
853 r = mailimap_authenticate(param->imap,
854 param->type, NULL, NULL, NULL,
855 param->login, param->login,
856 param->password, NULL);
857 #ifdef DISABLE_LOG_DURING_LOGIN
858 mailstream_debug = old_debug;
862 debug_print("imap login run - end %i\n", r);
865 int imap_threaded_login(Folder * folder,
866 const char * login, const char * password,
869 struct login_param param;
870 struct login_result result;
872 debug_print("imap login - begin\n");
874 param.imap = get_imap(folder);
876 param.password = password;
878 if (folder && folder->account)
879 param.server = folder->account->recv_server;
883 threaded_run(folder, ¶m, &result, login_run);
885 debug_print("imap login - end\n");
891 struct status_param {
894 struct mailimap_status_att_list * status_att_list;
897 struct status_result {
899 struct mailimap_mailbox_data_status * data_status;
902 static void status_run(struct etpan_thread_op * op)
904 struct status_param * param;
905 struct status_result * result;
913 r = mailimap_status(param->imap, param->mb,
914 param->status_att_list,
915 &result->data_status);
918 debug_print("imap status run - end %i\n", r);
921 int imap_threaded_status(Folder * folder, const char * mb,
922 struct mailimap_mailbox_data_status ** data_status,
925 struct status_param param;
926 struct status_result result;
927 struct mailimap_status_att_list * status_att_list;
929 debug_print("imap status - begin\n");
931 status_att_list = mailimap_status_att_list_new_empty();
933 mailimap_status_att_list_add(status_att_list,
934 MAILIMAP_STATUS_ATT_MESSAGES);
937 mailimap_status_att_list_add(status_att_list,
938 MAILIMAP_STATUS_ATT_RECENT);
941 mailimap_status_att_list_add(status_att_list,
942 MAILIMAP_STATUS_ATT_UIDNEXT);
945 mailimap_status_att_list_add(status_att_list,
946 MAILIMAP_STATUS_ATT_UIDVALIDITY);
949 mailimap_status_att_list_add(status_att_list,
950 MAILIMAP_STATUS_ATT_UNSEEN);
952 param.imap = get_imap(folder);
954 param.status_att_list = status_att_list;
956 threaded_run(folder, ¶m, &result, status_run);
958 debug_print("imap status - end\n");
960 * data_status = result.data_status;
962 mailimap_status_att_list_free(status_att_list);
977 static void noop_run(struct etpan_thread_op * op)
979 struct noop_param * param;
980 struct noop_result * result;
988 r = mailimap_noop(param->imap);
991 debug_print("imap noop run - end %i\n", r);
994 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
996 struct noop_param param;
997 struct noop_result result;
1000 debug_print("imap noop - begin\n");
1002 imap = get_imap(folder);
1005 threaded_run(folder, ¶m, &result, noop_run);
1007 if (imap->imap_selection_info != NULL) {
1008 * p_exists = imap->imap_selection_info->sel_exists;
1014 debug_print("imap noop - end\n");
1016 return result.error;
1020 struct starttls_result {
1024 static void starttls_run(struct etpan_thread_op * op)
1026 struct connect_param * param;
1027 struct starttls_result * result;
1031 result = op->result;
1035 r = mailimap_starttls(param->imap);
1038 debug_print("imap starttls run - end %i\n", r);
1041 mailimap *imap = param->imap;
1042 mailstream_low *plain_low = NULL;
1043 mailstream_low *tls_low = NULL;
1046 plain_low = mailstream_get_low(imap->imap_stream);
1047 fd = mailstream_low_get_fd(plain_low);
1049 debug_print("imap starttls run - can't get fd\n");
1050 result->error = MAILIMAP_ERROR_STREAM;
1054 tls_low = mailstream_low_tls_open(fd);
1055 if (tls_low == NULL) {
1056 debug_print("imap starttls run - can't tls_open\n");
1057 result->error = MAILIMAP_ERROR_STREAM;
1060 mailstream_low_free(plain_low);
1061 mailstream_set_low(imap->imap_stream, tls_low);
1065 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1067 struct connect_param param;
1068 struct starttls_result result;
1070 unsigned char *certificate;
1072 debug_print("imap starttls - begin\n");
1074 param.imap = get_imap(folder);
1075 param.server = host;
1078 threaded_run(folder, ¶m, &result, starttls_run);
1080 debug_print("imap starttls - end\n");
1082 if (result.error == 0 && !etpan_skip_ssl_cert_check) {
1083 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1084 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1085 result.error = MAILIMAP_ERROR_STREAM;
1089 return result.error;
1094 struct create_param {
1099 struct create_result {
1103 static void create_run(struct etpan_thread_op * op)
1105 struct create_param * param;
1106 struct create_result * result;
1110 result = op->result;
1114 r = mailimap_create(param->imap, param->mb);
1117 debug_print("imap create run - end %i\n", r);
1120 int imap_threaded_create(Folder * folder, const char * mb)
1122 struct create_param param;
1123 struct create_result result;
1125 debug_print("imap create - begin\n");
1127 param.imap = get_imap(folder);
1130 threaded_run(folder, ¶m, &result, create_run);
1132 debug_print("imap create - end\n");
1134 return result.error;
1140 struct rename_param {
1143 const char * new_name;
1146 struct rename_result {
1150 static void rename_run(struct etpan_thread_op * op)
1152 struct rename_param * param;
1153 struct rename_result * result;
1157 result = op->result;
1161 r = mailimap_rename(param->imap, param->mb, param->new_name);
1164 debug_print("imap rename run - end %i\n", r);
1167 int imap_threaded_rename(Folder * folder,
1168 const char * mb, const char * new_name)
1170 struct rename_param param;
1171 struct rename_result result;
1173 debug_print("imap rename - begin\n");
1175 param.imap = get_imap(folder);
1177 param.new_name = new_name;
1179 threaded_run(folder, ¶m, &result, rename_run);
1181 debug_print("imap rename - end\n");
1183 return result.error;
1189 struct delete_param {
1194 struct delete_result {
1198 static void delete_run(struct etpan_thread_op * op)
1200 struct delete_param * param;
1201 struct delete_result * result;
1205 result = op->result;
1209 r = mailimap_delete(param->imap, param->mb);
1212 debug_print("imap delete run - end %i\n", r);
1215 int imap_threaded_delete(Folder * folder, const char * mb)
1217 struct delete_param param;
1218 struct delete_result result;
1220 debug_print("imap delete - begin\n");
1222 param.imap = get_imap(folder);
1225 threaded_run(folder, ¶m, &result, delete_run);
1227 debug_print("imap delete - end\n");
1229 return result.error;
1234 struct select_param {
1239 struct select_result {
1243 static void select_run(struct etpan_thread_op * op)
1245 struct select_param * param;
1246 struct select_result * result;
1250 result = op->result;
1254 r = mailimap_select(param->imap, param->mb);
1257 debug_print("imap select run - end %i\n", r);
1260 int imap_threaded_select(Folder * folder, const char * mb,
1261 gint * exists, gint * recent, gint * unseen,
1262 guint32 * uid_validity)
1264 struct select_param param;
1265 struct select_result result;
1268 debug_print("imap select - begin\n");
1270 imap = get_imap(folder);
1274 threaded_run(folder, ¶m, &result, select_run);
1276 if (result.error != MAILIMAP_NO_ERROR)
1277 return result.error;
1279 if (imap->imap_selection_info == NULL)
1280 return MAILIMAP_ERROR_PARSE;
1282 * exists = imap->imap_selection_info->sel_exists;
1283 * recent = imap->imap_selection_info->sel_recent;
1284 * unseen = imap->imap_selection_info->sel_unseen;
1285 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1287 debug_print("imap select - end\n");
1289 return result.error;
1294 struct examine_param {
1299 struct examine_result {
1303 static void examine_run(struct etpan_thread_op * op)
1305 struct examine_param * param;
1306 struct examine_result * result;
1310 result = op->result;
1314 r = mailimap_examine(param->imap, param->mb);
1317 debug_print("imap examine run - end %i\n", r);
1320 int imap_threaded_examine(Folder * folder, const char * mb,
1321 gint * exists, gint * recent, gint * unseen,
1322 guint32 * uid_validity)
1324 struct examine_param param;
1325 struct examine_result result;
1328 debug_print("imap examine - begin\n");
1330 imap = get_imap(folder);
1334 threaded_run(folder, ¶m, &result, examine_run);
1336 if (result.error != MAILIMAP_NO_ERROR)
1337 return result.error;
1339 if (imap->imap_selection_info == NULL)
1340 return MAILIMAP_ERROR_PARSE;
1342 * exists = imap->imap_selection_info->sel_exists;
1343 * recent = imap->imap_selection_info->sel_recent;
1344 * unseen = imap->imap_selection_info->sel_unseen;
1345 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1347 debug_print("imap examine - end\n");
1349 return result.error;
1355 struct search_param {
1358 struct mailimap_set * set;
1361 struct search_result {
1363 clist * search_result;
1366 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1368 return mailimap_set_item_new(orig->set_first, orig->set_last);
1371 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1373 clist *list = orig ? orig->set_list : NULL;
1374 clist *newlist = clist_new();
1379 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1380 clist_append(newlist,
1381 sc_mailimap_set_item_copy(
1382 (struct mailimap_set_item *)clist_content(cur)));
1383 return mailimap_set_new(newlist);
1386 static void search_run(struct etpan_thread_op * op)
1388 struct search_param * param;
1389 struct search_result * result;
1391 struct mailimap_search_key * key = NULL;
1392 struct mailimap_search_key * uid_key = NULL;
1393 struct mailimap_search_key * search_type_key;
1394 clist * search_result;
1397 result = op->result;
1401 /* we copy the mailimap_set because freeing the key is recursive */
1402 if (param->set != NULL) {
1403 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1404 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1405 uid_key = mailimap_search_key_new_all();
1407 search_type_key = NULL;
1408 switch (param->type) {
1409 case IMAP_SEARCH_TYPE_SIMPLE:
1410 search_type_key = NULL;
1413 case IMAP_SEARCH_TYPE_SEEN:
1414 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1415 NULL, NULL, NULL, NULL, NULL,
1416 NULL, NULL, NULL, NULL, NULL,
1417 NULL, NULL, NULL, NULL, 0,
1418 NULL, NULL, NULL, NULL, NULL,
1419 NULL, 0, NULL, NULL, NULL);
1422 case IMAP_SEARCH_TYPE_UNSEEN:
1423 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1424 NULL, NULL, NULL, NULL, NULL,
1425 NULL, NULL, NULL, NULL, NULL,
1426 NULL, NULL, NULL, NULL, 0,
1427 NULL, NULL, NULL, NULL, NULL,
1428 NULL, 0, NULL, NULL, NULL);
1431 case IMAP_SEARCH_TYPE_ANSWERED:
1432 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1433 NULL, NULL, NULL, NULL, NULL,
1434 NULL, NULL, NULL, NULL, NULL,
1435 NULL, NULL, NULL, NULL, 0,
1436 NULL, NULL, NULL, NULL, NULL,
1437 NULL, 0, NULL, NULL, NULL);
1440 case IMAP_SEARCH_TYPE_FLAGGED:
1441 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1442 NULL, NULL, NULL, NULL, NULL,
1443 NULL, NULL, NULL, NULL, NULL,
1444 NULL, NULL, NULL, NULL, 0,
1445 NULL, NULL, NULL, NULL, NULL,
1446 NULL, 0, NULL, NULL, NULL);
1448 case IMAP_SEARCH_TYPE_DELETED:
1449 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1450 NULL, NULL, NULL, NULL, NULL,
1451 NULL, NULL, NULL, NULL, NULL,
1452 NULL, NULL, NULL, NULL, 0,
1453 NULL, NULL, NULL, NULL, NULL,
1454 NULL, 0, NULL, NULL, NULL);
1458 if (search_type_key != NULL) {
1459 if (param->set != NULL) {
1460 key = mailimap_search_key_new_multiple_empty();
1461 mailimap_search_key_multiple_add(key, search_type_key);
1462 mailimap_search_key_multiple_add(key, uid_key);
1464 key = search_type_key;
1466 } else if (uid_key != NULL) {
1471 g_warning("no key!");
1472 result = op->result;
1474 result->search_result = NULL;
1476 mailstream_logger = imap_logger_uid;
1478 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1480 mailstream_logger = imap_logger_cmd;
1482 /* free the key (with the imapset) */
1483 mailimap_search_key_free(key);
1486 result->search_result = search_result;
1488 debug_print("imap search run - end %i\n", result->error);
1491 int imap_threaded_search(Folder * folder, int search_type,
1492 struct mailimap_set * set, clist ** search_result)
1494 struct search_param param;
1495 struct search_result result;
1498 debug_print("imap search - begin\n");
1500 imap = get_imap(folder);
1503 param.type = search_type;
1505 threaded_run(folder, ¶m, &result, search_run);
1507 if (result.error != MAILIMAP_NO_ERROR)
1508 return result.error;
1510 debug_print("imap search - end\n");
1512 * search_result = result.search_result;
1514 return result.error;
1521 uid_list_to_env_list(clist * fetch_result, carray ** result)
1529 tab = carray_new(128);
1531 res = MAILIMAP_ERROR_MEMORY;
1535 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1536 cur = clist_next(cur)) {
1537 struct mailimap_msg_att * msg_att;
1538 clistiter * item_cur;
1543 msg_att = clist_content(cur);
1547 for(item_cur = clist_begin(msg_att->att_list) ;
1549 item_cur = clist_next(item_cur)) {
1550 struct mailimap_msg_att_item * item;
1552 item = clist_content(item_cur);
1554 switch (item->att_type) {
1555 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1556 switch (item->att_data.att_static->att_type) {
1557 case MAILIMAP_MSG_ATT_UID:
1558 uid = item->att_data.att_static->att_data.att_uid;
1564 puid = malloc(sizeof(* puid));
1566 res = MAILIMAP_ERROR_MEMORY;
1571 r = carray_add(tab, puid, NULL);
1574 res = MAILIMAP_ERROR_MEMORY;
1581 return MAILIMAP_NO_ERROR;
1584 for(i = 0 ; i < carray_count(tab) ; i++)
1585 mailmessage_free(carray_get(tab, i));
1590 static int imap_get_messages_list(mailimap * imap,
1591 uint32_t first_index,
1596 struct mailimap_fetch_att * fetch_att;
1597 struct mailimap_fetch_type * fetch_type;
1598 struct mailimap_set * set;
1599 clist * fetch_result;
1602 set = mailimap_set_new_interval(first_index, 0);
1604 res = MAILIMAP_ERROR_MEMORY;
1608 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1609 if (fetch_type == NULL) {
1610 res = MAILIMAP_ERROR_MEMORY;
1614 fetch_att = mailimap_fetch_att_new_uid();
1615 if (fetch_att == NULL) {
1616 res = MAILIMAP_ERROR_MEMORY;
1617 goto free_fetch_type;
1620 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1621 if (r != MAILIMAP_NO_ERROR) {
1622 mailimap_fetch_att_free(fetch_att);
1623 res = MAILIMAP_ERROR_MEMORY;
1624 goto free_fetch_type;
1627 r = mailimap_uid_fetch(imap, set,
1628 fetch_type, &fetch_result);
1630 mailimap_fetch_type_free(fetch_type);
1631 mailimap_set_free(set);
1633 if (r != MAILIMAP_NO_ERROR) {
1639 r = uid_list_to_env_list(fetch_result, &env_list);
1640 mailimap_fetch_list_free(fetch_result);
1642 * result = env_list;
1644 return MAILIMAP_NO_ERROR;
1647 mailimap_fetch_type_free(fetch_type);
1649 mailimap_set_free(set);
1657 struct fetch_uid_param {
1659 uint32_t first_index;
1662 struct fetch_uid_result {
1664 carray * fetch_result;
1667 static void fetch_uid_run(struct etpan_thread_op * op)
1669 struct fetch_uid_param * param;
1670 struct fetch_uid_result * result;
1671 carray * fetch_result;
1675 result = op->result;
1679 fetch_result = NULL;
1680 r = imap_get_messages_list(param->imap, param->first_index,
1684 result->fetch_result = fetch_result;
1685 debug_print("imap fetch_uid run - end %i\n", r);
1688 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1689 carray ** fetch_result)
1691 struct fetch_uid_param param;
1692 struct fetch_uid_result result;
1695 debug_print("imap fetch_uid - begin\n");
1697 imap = get_imap(folder);
1699 param.first_index = first_index;
1701 threaded_run(folder, ¶m, &result, fetch_uid_run);
1703 if (result.error != MAILIMAP_NO_ERROR)
1704 return result.error;
1706 debug_print("imap fetch_uid - end\n");
1708 * fetch_result = result.fetch_result;
1710 return result.error;
1714 void imap_fetch_uid_list_free(carray * uid_list)
1718 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1721 puid = carray_get(uid_list, i);
1724 carray_free(uid_list);
1729 static int imap_fetch(mailimap * imap,
1732 size_t * result_len)
1735 struct mailimap_set * set;
1736 struct mailimap_fetch_att * fetch_att;
1737 struct mailimap_fetch_type * fetch_type;
1738 clist * fetch_result;
1739 struct mailimap_msg_att * msg_att;
1740 struct mailimap_msg_att_item * msg_att_item;
1745 struct mailimap_section * section;
1747 set = mailimap_set_new_single(msg_index);
1749 res = MAILIMAP_ERROR_MEMORY;
1753 section = mailimap_section_new(NULL);
1754 if (section == NULL) {
1755 res = MAILIMAP_ERROR_MEMORY;
1759 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1760 if (fetch_att == NULL) {
1761 mailimap_section_free(section);
1762 res = MAILIMAP_ERROR_MEMORY;
1766 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1767 if (fetch_type == NULL) {
1768 res = MAILIMAP_ERROR_MEMORY;
1769 goto free_fetch_att;
1772 mailstream_logger = imap_logger_fetch;
1774 r = mailimap_uid_fetch(imap, set,
1775 fetch_type, &fetch_result);
1777 mailstream_logger = imap_logger_cmd;
1779 mailimap_fetch_type_free(fetch_type);
1780 mailimap_set_free(set);
1783 case MAILIMAP_NO_ERROR:
1789 if (clist_begin(fetch_result) == NULL) {
1790 mailimap_fetch_list_free(fetch_result);
1791 return MAILIMAP_ERROR_FETCH;
1794 msg_att = clist_begin(fetch_result)->data;
1799 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1800 cur = clist_next(cur)) {
1801 msg_att_item = clist_content(cur);
1803 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1804 if (msg_att_item->att_data.att_static->att_type ==
1805 MAILIMAP_MSG_ATT_BODY_SECTION) {
1806 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1808 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1810 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1815 mailimap_fetch_list_free(fetch_result);
1818 return MAILIMAP_ERROR_FETCH;
1821 * result_len = text_length;
1823 return MAILIMAP_NO_ERROR;
1826 mailimap_fetch_att_free(fetch_att);
1828 mailimap_set_free(set);
1833 static int imap_fetch_header(mailimap * imap,
1836 size_t * result_len)
1839 struct mailimap_set * set;
1840 struct mailimap_fetch_att * fetch_att;
1841 struct mailimap_fetch_type * fetch_type;
1842 clist * fetch_result;
1843 struct mailimap_msg_att * msg_att;
1844 struct mailimap_msg_att_item * msg_att_item;
1849 struct mailimap_section * section;
1851 set = mailimap_set_new_single(msg_index);
1853 res = MAILIMAP_ERROR_MEMORY;
1857 section = mailimap_section_new_header();
1858 if (section == NULL) {
1859 res = MAILIMAP_ERROR_MEMORY;
1863 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1864 if (fetch_att == NULL) {
1865 mailimap_section_free(section);
1866 res = MAILIMAP_ERROR_MEMORY;
1870 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1871 if (fetch_type == NULL) {
1872 res = MAILIMAP_ERROR_MEMORY;
1873 goto free_fetch_att;
1876 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1878 mailimap_fetch_type_free(fetch_type);
1879 mailimap_set_free(set);
1882 case MAILIMAP_NO_ERROR:
1888 if (clist_begin(fetch_result) == NULL) {
1889 mailimap_fetch_list_free(fetch_result);
1890 return MAILIMAP_ERROR_FETCH;
1893 msg_att = clist_begin(fetch_result)->data;
1898 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1899 cur = clist_next(cur)) {
1900 msg_att_item = clist_content(cur);
1902 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1903 if (msg_att_item->att_data.att_static->att_type ==
1904 MAILIMAP_MSG_ATT_BODY_SECTION) {
1905 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1906 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1908 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1913 mailimap_fetch_list_free(fetch_result);
1916 return MAILIMAP_ERROR_FETCH;
1919 * result_len = text_length;
1921 return MAILIMAP_NO_ERROR;
1924 mailimap_fetch_att_free(fetch_att);
1926 mailimap_set_free(set);
1933 struct fetch_content_param {
1936 const char * filename;
1940 struct fetch_content_result {
1944 static void fetch_content_run(struct etpan_thread_op * op)
1946 struct fetch_content_param * param;
1947 struct fetch_content_result * result;
1949 size_t content_size;
1955 result = op->result;
1961 if (param->with_body)
1962 r = imap_fetch(param->imap, param->msg_index,
1963 &content, &content_size);
1965 r = imap_fetch_header(param->imap, param->msg_index,
1966 &content, &content_size);
1970 if (r == MAILIMAP_NO_ERROR) {
1971 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1973 result->error = MAILIMAP_ERROR_FETCH;
1977 f = fdopen(fd, "wb");
1979 result->error = MAILIMAP_ERROR_FETCH;
1983 r = fwrite(content, 1, content_size, f);
1990 g_unlink(param->filename);
2001 g_unlink(param->filename);
2004 if (mmap_string_unref(content) != 0)
2008 debug_print("imap fetch_content run - end %i\n", r);
2011 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2013 const char * filename)
2015 struct fetch_content_param param;
2016 struct fetch_content_result result;
2019 debug_print("imap fetch_content - begin\n");
2021 imap = get_imap(folder);
2023 param.msg_index = msg_index;
2024 param.filename = filename;
2025 param.with_body = with_body;
2027 threaded_run(folder, ¶m, &result, fetch_content_run);
2029 if (result.error != MAILIMAP_NO_ERROR)
2030 return result.error;
2032 debug_print("imap fetch_content - end\n");
2034 return result.error;
2039 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
2047 flag_list = att_dyn->att_list;
2048 if (flag_list == NULL)
2051 for(cur = clist_begin(flag_list) ; cur != NULL ;
2052 cur = clist_next(cur)) {
2053 struct mailimap_flag_fetch * flag_fetch;
2055 flag_fetch = clist_content(cur);
2056 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2059 switch (flag_fetch->fl_flag->fl_type) {
2060 case MAILIMAP_FLAG_ANSWERED:
2061 flags |= MSG_REPLIED;
2063 case MAILIMAP_FLAG_FLAGGED:
2064 flags |= MSG_MARKED;
2066 case MAILIMAP_FLAG_DELETED:
2067 flags |= MSG_DELETED;
2069 case MAILIMAP_FLAG_SEEN:
2070 flags &= ~MSG_UNREAD;
2080 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2084 struct mailimap_msg_att_dynamic ** patt_dyn)
2086 clistiter * item_cur;
2090 struct mailimap_msg_att_dynamic * att_dyn;
2097 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2098 item_cur = clist_next(item_cur)) {
2099 struct mailimap_msg_att_item * item;
2101 item = clist_content(item_cur);
2103 switch (item->att_type) {
2104 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2105 switch (item->att_data.att_static->att_type) {
2106 case MAILIMAP_MSG_ATT_UID:
2107 uid = item->att_data.att_static->att_data.att_uid;
2110 case MAILIMAP_MSG_ATT_BODY_SECTION:
2111 if (headers == NULL) {
2112 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2115 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2116 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2121 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2122 if (att_dyn == NULL) {
2123 att_dyn = item->att_data.att_dyn;
2131 if (pheaders != NULL)
2132 * pheaders = headers;
2133 if (pref_size != NULL)
2134 * pref_size = ref_size;
2135 if (patt_dyn != NULL)
2136 * patt_dyn = att_dyn;
2138 return MAIL_NO_ERROR;
2141 static struct imap_fetch_env_info *
2142 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2144 struct imap_fetch_env_info * info;
2148 struct mailimap_msg_att_dynamic * att_dyn;
2150 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2155 info = malloc(sizeof(* info));
2157 info->headers = strdup(headers);
2159 info->flags = imap_flags_to_flags(att_dyn);
2165 imap_fetch_result_to_envelop_list(clist * fetch_result,
2166 carray ** p_env_list)
2173 env_list = carray_new(16);
2175 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2176 cur = clist_next(cur)) {
2177 struct mailimap_msg_att * msg_att;
2178 struct imap_fetch_env_info * env_info;
2180 msg_att = clist_content(cur);
2182 env_info = fetch_to_env_info(msg_att);
2184 return MAILIMAP_ERROR_MEMORY;
2185 carray_add(env_list, env_info, NULL);
2188 * p_env_list = env_list;
2190 return MAIL_NO_ERROR;
2193 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2195 struct mailimap_fetch_att * fetch_att;
2199 struct mailimap_header_list * imap_hdrlist;
2200 struct mailimap_section * section;
2202 hdrlist = clist_new();
2204 header = strdup("Date");
2205 r = clist_append(hdrlist, header);
2206 header = strdup("From");
2207 r = clist_append(hdrlist, header);
2208 header = strdup("To");
2209 r = clist_append(hdrlist, header);
2210 header = strdup("Cc");
2211 r = clist_append(hdrlist, header);
2212 header = strdup("Subject");
2213 r = clist_append(hdrlist, header);
2214 header = strdup("Message-ID");
2215 r = clist_append(hdrlist, header);
2216 header = strdup("References");
2217 r = clist_append(hdrlist, header);
2218 header = strdup("In-Reply-To");
2219 r = clist_append(hdrlist, header);
2221 imap_hdrlist = mailimap_header_list_new(hdrlist);
2222 section = mailimap_section_new_header_fields(imap_hdrlist);
2223 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2224 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2226 return MAIL_NO_ERROR;
2229 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2231 struct mailimap_fetch_att * fetch_att;
2232 struct mailimap_section * section;
2234 section = mailimap_section_new_header();
2235 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2236 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2238 return MAIL_NO_ERROR;
2242 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2243 carray ** p_env_list)
2245 struct mailimap_fetch_att * fetch_att;
2246 struct mailimap_fetch_type * fetch_type;
2248 clist * fetch_result;
2250 carray * env_list = NULL;
2254 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2257 fetch_att = mailimap_fetch_att_new_uid();
2258 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2261 fetch_att = mailimap_fetch_att_new_flags();
2262 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2265 fetch_att = mailimap_fetch_att_new_rfc822_size();
2266 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2270 key.len = sizeof(imap);
2271 r = chash_get(courier_workaround_hash, &key, &value);
2273 r = imap_add_envelope_fetch_att(fetch_type);
2275 r = imap_add_header_fetch_att(fetch_type);
2277 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2280 case MAILIMAP_NO_ERROR:
2283 mailimap_fetch_type_free(fetch_type);
2284 debug_print("uid_fetch: %d\n", r);
2288 if (clist_begin(fetch_result) == NULL) {
2289 res = MAILIMAP_ERROR_FETCH;
2290 debug_print("clist_begin = NULL\n");
2294 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2295 mailimap_fetch_list_free(fetch_result);
2297 if (r != MAILIMAP_NO_ERROR) {
2298 mailimap_fetch_type_free(fetch_type);
2299 res = MAILIMAP_ERROR_MEMORY;
2300 debug_print("fetch_result_to_envelop_list: %d\n", res);
2304 mailimap_fetch_type_free(fetch_type);
2306 * p_env_list = env_list;
2308 return MAILIMAP_NO_ERROR;
2314 struct fetch_env_param {
2316 struct mailimap_set * set;
2319 struct fetch_env_result {
2320 carray * fetch_env_result;
2324 static void fetch_env_run(struct etpan_thread_op * op)
2326 struct fetch_env_param * param;
2327 struct fetch_env_result * result;
2332 result = op->result;
2337 r = imap_get_envelopes_list(param->imap, param->set,
2341 result->fetch_env_result = env_list;
2343 debug_print("imap fetch_env run - end %i\n", r);
2346 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2347 carray ** p_env_list)
2349 struct fetch_env_param param;
2350 struct fetch_env_result result;
2353 debug_print("imap fetch_env - begin\n");
2355 imap = get_imap(folder);
2359 threaded_run(folder, ¶m, &result, fetch_env_run);
2361 if (result.error != MAILIMAP_NO_ERROR) {
2367 key.len = sizeof(imap);
2368 r = chash_get(courier_workaround_hash, &key, &value);
2372 chash_set(courier_workaround_hash, &key, &value, NULL);
2374 threaded_run(folder, ¶m, &result, fetch_env_run);
2378 if (result.error != MAILIMAP_NO_ERROR)
2379 return result.error;
2381 debug_print("imap fetch_env - end\n");
2383 * p_env_list = result.fetch_env_result;
2385 return result.error;
2388 void imap_fetch_env_free(carray * env_list)
2392 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2393 struct imap_fetch_env_info * env_info;
2395 env_info = carray_get(env_list, i);
2396 free(env_info->headers);
2399 carray_free(env_list);
2406 struct append_param {
2408 const char * mailbox;
2409 const char * filename;
2410 struct mailimap_flag_list * flag_list;
2413 struct append_result {
2418 static void append_run(struct etpan_thread_op * op)
2420 struct append_param * param;
2421 struct append_result * result;
2425 struct stat stat_buf;
2427 guint32 uid = 0, val = 0;
2430 result = op->result;
2434 r = stat(param->filename, &stat_buf);
2436 result->error = MAILIMAP_ERROR_APPEND;
2439 size = stat_buf.st_size;
2441 fd = open(param->filename, O_RDONLY);
2443 result->error = MAILIMAP_ERROR_APPEND;
2447 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2448 if (data == (void *) MAP_FAILED) {
2450 result->error = MAILIMAP_ERROR_APPEND;
2454 mailstream_logger = imap_logger_append;
2456 r = mailimap_uidplus_append(param->imap, param->mailbox,
2457 param->flag_list, NULL,
2458 data, size, &val, &uid);
2460 mailstream_logger = imap_logger_cmd;
2467 debug_print("imap append run - end %i uid %d\n", r, uid);
2470 int imap_threaded_append(Folder * folder, const char * mailbox,
2471 const char * filename,
2472 struct mailimap_flag_list * flag_list,
2475 struct append_param param;
2476 struct append_result result;
2479 debug_print("imap append - begin\n");
2481 imap = get_imap(folder);
2483 param.mailbox = mailbox;
2484 param.filename = filename;
2485 param.flag_list = flag_list;
2487 threaded_run(folder, ¶m, &result, append_run);
2489 if (result.error != MAILIMAP_NO_ERROR)
2490 return result.error;
2492 debug_print("imap append - end\n");
2496 return result.error;
2502 struct expunge_param {
2506 struct expunge_result {
2510 static void expunge_run(struct etpan_thread_op * op)
2512 struct expunge_param * param;
2513 struct expunge_result * result;
2517 result = op->result;
2521 r = mailimap_expunge(param->imap);
2524 debug_print("imap expunge run - end %i\n", r);
2527 int imap_threaded_expunge(Folder * folder)
2529 struct expunge_param param;
2530 struct expunge_result result;
2532 debug_print("imap expunge - begin\n");
2534 param.imap = get_imap(folder);
2536 threaded_run(folder, ¶m, &result, expunge_run);
2538 debug_print("imap expunge - end\n");
2540 return result.error;
2546 struct mailimap_set * set;
2550 struct copy_result {
2552 struct mailimap_set *source;
2553 struct mailimap_set *dest;
2556 static void copy_run(struct etpan_thread_op * op)
2558 struct copy_param * param;
2559 struct copy_result * result;
2562 struct mailimap_set *source = NULL, *dest = NULL;
2565 result = op->result;
2569 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2570 &val, &source, &dest);
2574 result->source = source;
2575 result->dest = dest;
2577 result->source = NULL;
2578 result->dest = NULL;
2580 debug_print("imap copy run - end %i\n", r);
2583 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2584 const char * mb, struct mailimap_set **source,
2585 struct mailimap_set **dest)
2587 struct copy_param param;
2588 struct copy_result result;
2591 debug_print("imap copy - begin\n");
2593 imap = get_imap(folder);
2598 threaded_run(folder, ¶m, &result, copy_run);
2602 if (result.error != MAILIMAP_NO_ERROR)
2603 return result.error;
2605 *source = result.source;
2606 *dest = result.dest;
2608 debug_print("imap copy - end\n");
2610 return result.error;
2615 struct store_param {
2617 struct mailimap_set * set;
2618 struct mailimap_store_att_flags * store_att_flags;
2621 struct store_result {
2625 static void store_run(struct etpan_thread_op * op)
2627 struct store_param * param;
2628 struct store_result * result;
2632 result = op->result;
2636 r = mailimap_uid_store(param->imap, param->set,
2637 param->store_att_flags);
2641 debug_print("imap store run - end %i\n", r);
2644 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2645 struct mailimap_store_att_flags * store_att_flags)
2647 struct store_param param;
2648 struct store_result result;
2651 debug_print("imap store - begin\n");
2653 imap = get_imap(folder);
2656 param.store_att_flags = store_att_flags;
2658 threaded_run(folder, ¶m, &result, store_run);
2660 if (result.error != MAILIMAP_NO_ERROR)
2661 return result.error;
2663 debug_print("imap store - end\n");
2665 return result.error;
2669 #define ENV_BUFFER_SIZE 512
2671 static void do_exec_command(int fd, const char * command,
2672 const char * servername, uint16_t port)
2676 char env_buffer[ENV_BUFFER_SIZE];
2680 /* Fork again to become a child of init rather than
2681 the etpan client. */
2687 snprintf(env_buffer, ENV_BUFFER_SIZE,
2688 "ETPANSERVER=%s", servername);
2690 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2694 setenv("ETPANSERVER", servername, 1);
2696 unsetenv("ETPANSERVER");
2701 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2703 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2709 snprintf(porttext, sizeof(porttext), "%d", port);
2710 setenv("ETPANPORT", porttext, 1);
2713 unsetenv("ETPANPORT");
2717 /* Not a lot we can do if there's an error other than bail. */
2718 if (dup2(fd, 0) == -1)
2720 if (dup2(fd, 1) == -1)
2723 /* Should we close stderr and reopen /dev/null? */
2725 maxopen = sysconf(_SC_OPEN_MAX);
2726 for (i=3; i < maxopen; i++)
2730 /* Detach from the controlling tty if we have one. Otherwise,
2731 SSH might do something stupid like trying to use it instead
2732 of running $SSH_ASKPASS. Doh. */
2733 fd = open("/dev/tty", O_RDONLY);
2735 ioctl(fd, TIOCNOTTY, NULL);
2738 #endif /* TIOCNOTTY */
2740 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2742 /* Eep. Shouldn't reach this */
2746 static int subcommand_connect(const char *command,
2747 const char *servername, uint16_t port)
2749 /* SEB unsupported on Windows */
2753 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2758 do_exec_command(sockfds[1], command, servername, port);
2760 else if (childpid == -1) {
2768 /* Reap child, leaving grandchild process to run */
2769 waitpid(childpid, NULL, 0);
2774 static int socket_connect_cmd(mailimap * imap, const char * command,
2775 const char * server, int port)
2781 fd = subcommand_connect(command, server, port);
2783 return MAILIMAP_ERROR_STREAM;
2785 s = mailstream_socket_open(fd);
2788 return MAILIMAP_ERROR_STREAM;
2791 r = mailimap_connect(imap, s);
2792 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2793 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2794 mailstream_close(s);
2795 imap->imap_stream = NULL;
2804 struct connect_cmd_param {
2806 const char * command;
2807 const char * server;
2811 struct connect_cmd_result {
2815 static void connect_cmd_run(struct etpan_thread_op * op)
2818 struct connect_cmd_param * param;
2819 struct connect_cmd_result * result;
2822 result = op->result;
2826 r = socket_connect_cmd(param->imap, param->command,
2827 param->server, param->port);
2833 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2834 const char * server, int port)
2836 struct connect_cmd_param param;
2837 struct connect_cmd_result result;
2840 mailimap * imap, * oldimap;
2842 oldimap = get_imap(folder);
2844 imap = mailimap_new(0, NULL);
2847 debug_print("deleting old imap %p\n", oldimap);
2848 delete_imap(folder, oldimap);
2852 key.len = sizeof(folder);
2855 chash_set(session_hash, &key, &value, NULL);
2858 param.command = command;
2859 param.server = server;
2862 threaded_run(folder, ¶m, &result, connect_cmd_run);
2864 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
2866 return result.error;
2870 void imap_main_init(void)
2873 void imap_main_done(void)
2876 void imap_main_set_timeout(int sec)