7 #include "imap-thread.h"
11 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
12 #include <sys/socket.h>
20 #include "etpan-thread-manager.h"
22 #include "ssl_certificate.h"
25 #define DISABLE_LOG_DURING_LOGIN
27 static struct etpan_thread_manager * thread_manager = NULL;
28 static chash * courier_workaround_hash = NULL;
29 static chash * imap_hash = NULL;
30 static chash * session_hash = NULL;
31 static guint thread_manager_signal = 0;
32 static GIOChannel * io_channel = NULL;
35 static gboolean thread_manager_event(GIOChannel * source,
36 GIOCondition condition,
39 etpan_thread_manager_loop(thread_manager);
44 static void imap_logger_cmd(int direction, const char * str, size_t size)
51 log_print("IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
55 memset(buf, 0, size+1);
56 strncpy(buf, str, size);
59 if (!strncmp(buf, "<<<<<<<", 7)
60 || !strncmp(buf, ">>>>>>>", 7)) {
64 while (strstr(buf, "\r"))
65 *strstr(buf, "\r") = ' ';
66 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
67 buf[strlen(buf)-1] = '\0';
69 lines = g_strsplit(buf, "\n", -1);
71 while (lines[i] && *lines[i]) {
72 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
79 static void imap_logger_fetch(int direction, const char * str, size_t size)
86 log_print("IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
91 memset(buf, 0, size+1);
92 strncpy(buf, str, size);
94 if (!strncmp(buf, "<<<<<<<", 7)
95 || !strncmp(buf, ">>>>>>>", 7)) {
99 while (strstr(buf, "\r"))
100 *strstr(buf, "\r") = ' ';
101 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
102 buf[strlen(buf)-1] = '\0';
104 lines = g_strsplit(buf, "\n", -1);
106 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
107 while (lines[i] && *lines[i]) {
108 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
112 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
118 static void imap_logger_uid(int direction, const char * str, size_t size)
125 log_print("IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
128 buf = malloc(size+1);
129 memset(buf, 0, size+1);
130 strncpy(buf, str, size);
132 if (!strncmp(buf, "<<<<<<<", 7)
133 || !strncmp(buf, ">>>>>>>", 7)) {
137 while (strstr(buf, "\r"))
138 *strstr(buf, "\r") = ' ';
139 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
140 buf[strlen(buf)-1] = '\0';
142 lines = g_strsplit(buf, "\n", -1);
144 while (lines[i] && *lines[i]) {
145 int llen = strlen(lines[i]);
147 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
150 strncpy2(tmp, lines[i], 63);
151 log_print("IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
160 void imap_logger_append(int direction, const char * str, size_t size)
167 log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
170 buf = malloc(size+1);
171 memset(buf, 0, size+1);
172 strncpy(buf, str, size);
174 if (!strncmp(buf, "<<<<<<<", 7)
175 || !strncmp(buf, ">>>>>>>", 7)) {
179 while (strstr(buf, "\r"))
180 *strstr(buf, "\r") = ' ';
181 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
182 buf[strlen(buf)-1] = '\0';
184 lines = g_strsplit(buf, "\n", -1);
186 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
187 while (lines[i] && *lines[i]) {
188 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
192 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
198 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
199 static gboolean etpan_skip_ssl_cert_check = FALSE;
200 extern void mailsasl_ref(void);
202 void imap_main_init(gboolean skip_ssl_cert_check)
204 int fd_thread_manager;
206 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
207 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
208 mailstream_network_delay.tv_usec = 0;
210 mailstream_debug = 1;
211 mailstream_logger = imap_logger_cmd;
214 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
215 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
216 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
218 thread_manager = etpan_thread_manager_new();
220 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
222 io_channel = g_io_channel_unix_new(fd_thread_manager);
224 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
225 thread_manager_event,
230 void imap_main_set_timeout(int sec)
232 mailstream_network_delay.tv_sec = sec;
233 mailstream_network_delay.tv_usec = 0;
236 void imap_main_done(void)
238 etpan_thread_manager_stop(thread_manager);
239 etpan_thread_manager_join(thread_manager);
241 g_source_remove(thread_manager_signal);
242 g_io_channel_unref(io_channel);
244 etpan_thread_manager_free(thread_manager);
246 chash_free(courier_workaround_hash);
247 chash_free(session_hash);
248 chash_free(imap_hash);
251 void imap_init(Folder * folder)
253 struct etpan_thread * thread;
257 thread = etpan_thread_manager_get_thread(thread_manager);
260 key.len = sizeof(folder);
264 chash_set(imap_hash, &key, &value, NULL);
267 void imap_done(Folder * folder)
269 struct etpan_thread * thread;
275 key.len = sizeof(folder);
277 r = chash_get(imap_hash, &key, &value);
283 etpan_thread_unbind(thread);
285 chash_delete(imap_hash, &key, NULL);
287 debug_print("remove thread");
290 static struct etpan_thread * get_thread(Folder * folder)
292 struct etpan_thread * thread;
297 key.len = sizeof(folder);
299 chash_get(imap_hash, &key, &value);
305 static mailimap * get_imap(Folder * folder)
313 key.len = sizeof(folder);
315 r = chash_get(session_hash, &key, &value);
325 static void generic_cb(int cancelled, void * result, void * callback_data)
329 p_finished = callback_data;
331 debug_print("generic_cb\n");
336 static void threaded_run(Folder * folder, void * param, void * result,
337 void (* func)(struct etpan_thread_op * ))
339 struct etpan_thread_op * op;
340 struct etpan_thread * thread;
343 imap_folder_ref(folder);
345 op = etpan_thread_op_new();
351 op->callback = generic_cb;
352 op->callback_data = &finished;
357 thread = get_thread(folder);
358 etpan_thread_op_schedule(thread, op);
361 gtk_main_iteration();
364 etpan_thread_op_free(op);
366 imap_folder_unref(folder);
372 struct connect_param {
378 struct connect_result {
382 static void connect_run(struct etpan_thread_op * op)
385 struct connect_param * param;
386 struct connect_result * result;
391 r = mailimap_socket_connect(param->imap,
392 param->server, param->port);
398 int imap_threaded_connect(Folder * folder, const char * server, int port)
400 struct connect_param param;
401 struct connect_result result;
406 imap = mailimap_new(0, NULL);
409 key.len = sizeof(folder);
412 chash_set(session_hash, &key, &value, NULL);
415 param.server = server;
419 threaded_run(folder, ¶m, &result, connect_run);
421 debug_print("connect ok %i\n", result.error);
426 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
429 struct connect_param *param = (struct connect_param *)data;
432 if (certificate == NULL || len < 0) {
433 g_warning("no cert presented.\n");
436 cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
438 g_warning("can't get cert\n");
440 } else if (ssl_certificate_check(cert, NULL,
441 (gchar *)param->server, (gushort)param->port) == TRUE) {
453 static void connect_ssl_run(struct etpan_thread_op * op)
456 struct connect_param * param;
457 struct connect_result * result;
462 r = mailimap_ssl_connect(param->imap,
463 param->server, param->port);
467 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
469 struct connect_param param;
470 struct connect_result result;
474 unsigned char *certificate = NULL;
477 imap = mailimap_new(0, NULL);
480 key.len = sizeof(folder);
483 chash_set(session_hash, &key, &value, NULL);
486 param.server = server;
490 threaded_run(folder, ¶m, &result, connect_ssl_run);
492 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
493 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
494 cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
495 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
500 debug_print("connect %d\n", result.error);
511 struct mailimap_capability_data *caps;
514 static void capability_run(struct etpan_thread_op * op)
517 struct capa_param * param;
518 struct capa_result * result;
519 struct mailimap_capability_data *caps;
524 r = mailimap_capability(param->imap, &caps);
527 result->caps = (r == 0 ? caps : NULL);
531 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
533 struct capa_param param;
534 struct capa_result result;
537 imap = get_imap(folder);
541 threaded_run(folder, ¶m, &result, capability_run);
543 debug_print("capa %d\n", result.error);
552 struct disconnect_param {
556 struct disconnect_result {
560 static void disconnect_run(struct etpan_thread_op * op)
563 struct disconnect_param * param;
564 struct disconnect_result * result;
569 r = mailimap_logout(param->imap);
574 void imap_threaded_disconnect(Folder * folder)
576 struct connect_param param;
577 struct connect_result result;
582 imap = get_imap(folder);
584 debug_print("was disconnected\n");
590 threaded_run(folder, ¶m, &result, disconnect_run);
593 key.len = sizeof(folder);
596 chash_delete(session_hash, &key, NULL);
599 key.len = sizeof(imap);
600 chash_delete(courier_workaround_hash, &key, NULL);
604 debug_print("disconnect ok\n");
611 const char * wildcard;
619 static void list_run(struct etpan_thread_op * op)
621 struct list_param * param;
622 struct list_result * result;
628 r = mailimap_list(param->imap, param->base,
629 param->wildcard, &list);
634 debug_print("imap list run - end\n");
637 int imap_threaded_list(Folder * folder, const char * base,
638 const char * wildcard,
641 struct list_param param;
642 struct list_result result;
644 debug_print("imap list - begin\n");
646 param.imap = get_imap(folder);
648 param.wildcard = wildcard;
650 threaded_run(folder, ¶m, &result, list_run);
652 * p_result = result.list;
654 debug_print("imap list - end %p\n", result.list);
664 const char * password;
669 struct login_result {
673 static void login_run(struct etpan_thread_op * op)
675 struct login_param * param;
676 struct login_result * result;
678 #ifdef DISABLE_LOG_DURING_LOGIN
684 #ifdef DISABLE_LOG_DURING_LOGIN
685 old_debug = mailstream_debug;
686 mailstream_debug = 0;
688 if (!strcmp(param->type, "LOGIN"))
689 r = mailimap_login(param->imap,
690 param->login, param->password);
691 else if (!strcmp(param->type, "GSSAPI"))
692 r = mailimap_authenticate(param->imap,
693 param->type, param->server, NULL, NULL,
694 param->login, param->login,
695 param->password, NULL);
697 r = mailimap_authenticate(param->imap,
698 param->type, NULL, NULL, NULL,
699 param->login, param->login,
700 param->password, NULL);
701 #ifdef DISABLE_LOG_DURING_LOGIN
702 mailstream_debug = old_debug;
707 debug_print("imap login run - end %i\n", r);
710 int imap_threaded_login(Folder * folder,
711 const char * login, const char * password,
714 struct login_param param;
715 struct login_result result;
717 debug_print("imap login - begin\n");
719 param.imap = get_imap(folder);
721 param.password = password;
723 if (folder && folder->account)
724 param.server = folder->account->recv_server;
728 threaded_run(folder, ¶m, &result, login_run);
730 debug_print("imap login - end\n");
736 struct status_param {
739 struct mailimap_status_att_list * status_att_list;
742 struct status_result {
744 struct mailimap_mailbox_data_status * data_status;
747 static void status_run(struct etpan_thread_op * op)
749 struct status_param * param;
750 struct status_result * result;
756 r = mailimap_status(param->imap, param->mb,
757 param->status_att_list,
758 &result->data_status);
761 debug_print("imap status run - end %i\n", r);
764 int imap_threaded_status(Folder * folder, const char * mb,
765 struct mailimap_mailbox_data_status ** data_status,
768 struct status_param param;
769 struct status_result result;
770 struct mailimap_status_att_list * status_att_list;
772 debug_print("imap status - begin\n");
774 status_att_list = mailimap_status_att_list_new_empty();
776 mailimap_status_att_list_add(status_att_list,
777 MAILIMAP_STATUS_ATT_MESSAGES);
780 mailimap_status_att_list_add(status_att_list,
781 MAILIMAP_STATUS_ATT_RECENT);
784 mailimap_status_att_list_add(status_att_list,
785 MAILIMAP_STATUS_ATT_UIDNEXT);
788 mailimap_status_att_list_add(status_att_list,
789 MAILIMAP_STATUS_ATT_UIDVALIDITY);
792 mailimap_status_att_list_add(status_att_list,
793 MAILIMAP_STATUS_ATT_UNSEEN);
795 param.imap = get_imap(folder);
797 param.status_att_list = status_att_list;
799 threaded_run(folder, ¶m, &result, status_run);
801 debug_print("imap status - end\n");
803 * data_status = result.data_status;
805 mailimap_status_att_list_free(status_att_list);
820 static void noop_run(struct etpan_thread_op * op)
822 struct noop_param * param;
823 struct noop_result * result;
827 r = mailimap_noop(param->imap);
831 debug_print("imap noop run - end %i\n", r);
834 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
836 struct noop_param param;
837 struct noop_result result;
840 debug_print("imap noop - begin\n");
842 imap = get_imap(folder);
845 threaded_run(folder, ¶m, &result, noop_run);
847 if (imap->imap_selection_info != NULL) {
848 * p_exists = imap->imap_selection_info->sel_exists;
854 debug_print("imap noop - end\n");
860 struct starttls_result {
864 static void starttls_run(struct etpan_thread_op * op)
866 struct connect_param * param;
867 struct starttls_result * result;
871 r = mailimap_starttls(param->imap);
875 debug_print("imap starttls run - end %i\n", r);
878 mailimap *imap = param->imap;
879 mailstream_low *plain_low = NULL;
880 mailstream_low *tls_low = NULL;
883 plain_low = mailstream_get_low(imap->imap_stream);
884 fd = mailstream_low_get_fd(plain_low);
886 debug_print("imap starttls run - can't get fd\n");
887 result->error = MAILIMAP_ERROR_STREAM;
891 tls_low = mailstream_low_tls_open(fd);
892 if (tls_low == NULL) {
893 debug_print("imap starttls run - can't tls_open\n");
894 result->error = MAILIMAP_ERROR_STREAM;
897 mailstream_low_free(plain_low);
898 mailstream_set_low(imap->imap_stream, tls_low);
902 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
904 struct connect_param param;
905 struct starttls_result result;
907 unsigned char *certificate;
909 debug_print("imap starttls - begin\n");
911 param.imap = get_imap(folder);
915 threaded_run(folder, ¶m, &result, starttls_run);
917 debug_print("imap starttls - end\n");
919 if (result.error == 0 && !etpan_skip_ssl_cert_check) {
920 cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
921 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
922 result.error = MAILIMAP_ERROR_STREAM;
931 struct create_param {
936 struct create_result {
940 static void create_run(struct etpan_thread_op * op)
942 struct create_param * param;
943 struct create_result * result;
947 r = mailimap_create(param->imap, param->mb);
951 debug_print("imap create run - end %i\n", r);
954 int imap_threaded_create(Folder * folder, const char * mb)
956 struct create_param param;
957 struct create_result result;
959 debug_print("imap create - begin\n");
961 param.imap = get_imap(folder);
964 threaded_run(folder, ¶m, &result, create_run);
966 debug_print("imap create - end\n");
974 struct rename_param {
977 const char * new_name;
980 struct rename_result {
984 static void rename_run(struct etpan_thread_op * op)
986 struct rename_param * param;
987 struct rename_result * result;
991 r = mailimap_rename(param->imap, param->mb, param->new_name);
995 debug_print("imap rename run - end %i\n", r);
998 int imap_threaded_rename(Folder * folder,
999 const char * mb, const char * new_name)
1001 struct rename_param param;
1002 struct rename_result result;
1004 debug_print("imap rename - begin\n");
1006 param.imap = get_imap(folder);
1008 param.new_name = new_name;
1010 threaded_run(folder, ¶m, &result, rename_run);
1012 debug_print("imap rename - end\n");
1014 return result.error;
1020 struct delete_param {
1025 struct delete_result {
1029 static void delete_run(struct etpan_thread_op * op)
1031 struct delete_param * param;
1032 struct delete_result * result;
1036 r = mailimap_delete(param->imap, param->mb);
1038 result = op->result;
1040 debug_print("imap delete run - end %i\n", r);
1043 int imap_threaded_delete(Folder * folder, const char * mb)
1045 struct delete_param param;
1046 struct delete_result result;
1048 debug_print("imap delete - begin\n");
1050 param.imap = get_imap(folder);
1053 threaded_run(folder, ¶m, &result, delete_run);
1055 debug_print("imap delete - end\n");
1057 return result.error;
1062 struct select_param {
1067 struct select_result {
1071 static void select_run(struct etpan_thread_op * op)
1073 struct select_param * param;
1074 struct select_result * result;
1078 r = mailimap_select(param->imap, param->mb);
1080 result = op->result;
1082 debug_print("imap select run - end %i\n", r);
1085 int imap_threaded_select(Folder * folder, const char * mb,
1086 gint * exists, gint * recent, gint * unseen,
1087 guint32 * uid_validity)
1089 struct select_param param;
1090 struct select_result result;
1093 debug_print("imap select - begin\n");
1095 imap = get_imap(folder);
1099 threaded_run(folder, ¶m, &result, select_run);
1101 if (result.error != MAILIMAP_NO_ERROR)
1102 return result.error;
1104 if (imap->imap_selection_info == NULL)
1105 return MAILIMAP_ERROR_PARSE;
1107 * exists = imap->imap_selection_info->sel_exists;
1108 * recent = imap->imap_selection_info->sel_recent;
1109 * unseen = imap->imap_selection_info->sel_unseen;
1110 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1112 debug_print("imap select - end\n");
1114 return result.error;
1119 struct examine_param {
1124 struct examine_result {
1128 static void examine_run(struct etpan_thread_op * op)
1130 struct examine_param * param;
1131 struct examine_result * result;
1135 r = mailimap_examine(param->imap, param->mb);
1137 result = op->result;
1139 debug_print("imap examine run - end %i\n", r);
1142 int imap_threaded_examine(Folder * folder, const char * mb,
1143 gint * exists, gint * recent, gint * unseen,
1144 guint32 * uid_validity)
1146 struct examine_param param;
1147 struct examine_result result;
1150 debug_print("imap examine - begin\n");
1152 imap = get_imap(folder);
1156 threaded_run(folder, ¶m, &result, examine_run);
1158 if (result.error != MAILIMAP_NO_ERROR)
1159 return result.error;
1161 if (imap->imap_selection_info == NULL)
1162 return MAILIMAP_ERROR_PARSE;
1164 * exists = imap->imap_selection_info->sel_exists;
1165 * recent = imap->imap_selection_info->sel_recent;
1166 * unseen = imap->imap_selection_info->sel_unseen;
1167 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1169 debug_print("imap examine - end\n");
1171 return result.error;
1177 struct search_param {
1180 struct mailimap_set * set;
1183 struct search_result {
1185 clist * search_result;
1188 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1190 return mailimap_set_item_new(orig->set_first, orig->set_last);
1193 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1195 clist *list = orig ? orig->set_list : NULL;
1196 clist *newlist = clist_new();
1201 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1202 clist_append(newlist,
1203 sc_mailimap_set_item_copy(
1204 (struct mailimap_set_item *)clist_content(cur)));
1205 return mailimap_set_new(newlist);
1208 static void search_run(struct etpan_thread_op * op)
1210 struct search_param * param;
1211 struct search_result * result;
1213 struct mailimap_search_key * key = NULL;
1214 struct mailimap_search_key * uid_key = NULL;
1215 struct mailimap_search_key * search_type_key;
1216 clist * search_result;
1220 /* we copy the mailimap_set because freeing the key is recursive */
1221 if (param->set != NULL) {
1222 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1223 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1224 uid_key = mailimap_search_key_new_all();
1226 search_type_key = NULL;
1227 switch (param->type) {
1228 case IMAP_SEARCH_TYPE_SIMPLE:
1229 search_type_key = NULL;
1232 case IMAP_SEARCH_TYPE_SEEN:
1233 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1234 NULL, NULL, NULL, NULL, NULL,
1235 NULL, NULL, NULL, NULL, NULL,
1236 NULL, NULL, NULL, NULL, 0,
1237 NULL, NULL, NULL, NULL, NULL,
1238 NULL, 0, NULL, NULL, NULL);
1241 case IMAP_SEARCH_TYPE_UNSEEN:
1242 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1243 NULL, NULL, NULL, NULL, NULL,
1244 NULL, NULL, NULL, NULL, NULL,
1245 NULL, NULL, NULL, NULL, 0,
1246 NULL, NULL, NULL, NULL, NULL,
1247 NULL, 0, NULL, NULL, NULL);
1250 case IMAP_SEARCH_TYPE_ANSWERED:
1251 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1252 NULL, NULL, NULL, NULL, NULL,
1253 NULL, NULL, NULL, NULL, NULL,
1254 NULL, NULL, NULL, NULL, 0,
1255 NULL, NULL, NULL, NULL, NULL,
1256 NULL, 0, NULL, NULL, NULL);
1259 case IMAP_SEARCH_TYPE_FLAGGED:
1260 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1261 NULL, NULL, NULL, NULL, NULL,
1262 NULL, NULL, NULL, NULL, NULL,
1263 NULL, NULL, NULL, NULL, 0,
1264 NULL, NULL, NULL, NULL, NULL,
1265 NULL, 0, NULL, NULL, NULL);
1267 case IMAP_SEARCH_TYPE_DELETED:
1268 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1269 NULL, NULL, NULL, NULL, NULL,
1270 NULL, NULL, NULL, NULL, NULL,
1271 NULL, NULL, NULL, NULL, 0,
1272 NULL, NULL, NULL, NULL, NULL,
1273 NULL, 0, NULL, NULL, NULL);
1277 if (search_type_key != NULL) {
1278 if (param->set != NULL) {
1279 key = mailimap_search_key_new_multiple_empty();
1280 mailimap_search_key_multiple_add(key, search_type_key);
1281 mailimap_search_key_multiple_add(key, uid_key);
1283 key = search_type_key;
1285 } else if (uid_key != NULL) {
1290 g_warning("no key!");
1291 result = op->result;
1293 result->search_result = NULL;
1295 mailstream_logger = imap_logger_uid;
1297 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1299 mailstream_logger = imap_logger_cmd;
1301 /* free the key (with the imapset) */
1302 mailimap_search_key_free(key);
1304 result = op->result;
1306 result->search_result = search_result;
1308 debug_print("imap search run - end %i\n", result->error);
1311 int imap_threaded_search(Folder * folder, int search_type,
1312 struct mailimap_set * set, clist ** search_result)
1314 struct search_param param;
1315 struct search_result result;
1318 debug_print("imap search - begin\n");
1320 imap = get_imap(folder);
1323 param.type = search_type;
1325 threaded_run(folder, ¶m, &result, search_run);
1327 if (result.error != MAILIMAP_NO_ERROR)
1328 return result.error;
1330 debug_print("imap search - end\n");
1332 * search_result = result.search_result;
1334 return result.error;
1341 uid_list_to_env_list(clist * fetch_result, carray ** result)
1349 tab = carray_new(128);
1351 res = MAILIMAP_ERROR_MEMORY;
1355 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1356 cur = clist_next(cur)) {
1357 struct mailimap_msg_att * msg_att;
1358 clistiter * item_cur;
1363 msg_att = clist_content(cur);
1367 for(item_cur = clist_begin(msg_att->att_list) ;
1369 item_cur = clist_next(item_cur)) {
1370 struct mailimap_msg_att_item * item;
1372 item = clist_content(item_cur);
1374 switch (item->att_type) {
1375 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1376 switch (item->att_data.att_static->att_type) {
1377 case MAILIMAP_MSG_ATT_UID:
1378 uid = item->att_data.att_static->att_data.att_uid;
1384 puid = malloc(sizeof(* puid));
1386 res = MAILIMAP_ERROR_MEMORY;
1391 r = carray_add(tab, puid, NULL);
1394 res = MAILIMAP_ERROR_MEMORY;
1401 return MAILIMAP_NO_ERROR;
1404 for(i = 0 ; i < carray_count(tab) ; i++)
1405 mailmessage_free(carray_get(tab, i));
1410 static int imap_get_messages_list(mailimap * imap,
1411 uint32_t first_index,
1416 struct mailimap_fetch_att * fetch_att;
1417 struct mailimap_fetch_type * fetch_type;
1418 struct mailimap_set * set;
1419 clist * fetch_result;
1422 set = mailimap_set_new_interval(first_index, 0);
1424 res = MAILIMAP_ERROR_MEMORY;
1428 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1429 if (fetch_type == NULL) {
1430 res = MAILIMAP_ERROR_MEMORY;
1434 fetch_att = mailimap_fetch_att_new_uid();
1435 if (fetch_att == NULL) {
1436 res = MAILIMAP_ERROR_MEMORY;
1437 goto free_fetch_type;
1440 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1441 if (r != MAILIMAP_NO_ERROR) {
1442 mailimap_fetch_att_free(fetch_att);
1443 res = MAILIMAP_ERROR_MEMORY;
1444 goto free_fetch_type;
1447 r = mailimap_uid_fetch(imap, set,
1448 fetch_type, &fetch_result);
1450 mailimap_fetch_type_free(fetch_type);
1451 mailimap_set_free(set);
1453 if (r != MAILIMAP_NO_ERROR) {
1459 r = uid_list_to_env_list(fetch_result, &env_list);
1460 mailimap_fetch_list_free(fetch_result);
1462 * result = env_list;
1464 return MAILIMAP_NO_ERROR;
1467 mailimap_fetch_type_free(fetch_type);
1469 mailimap_set_free(set);
1477 struct fetch_uid_param {
1479 uint32_t first_index;
1482 struct fetch_uid_result {
1484 carray * fetch_result;
1487 static void fetch_uid_run(struct etpan_thread_op * op)
1489 struct fetch_uid_param * param;
1490 struct fetch_uid_result * result;
1491 carray * fetch_result;
1496 fetch_result = NULL;
1497 r = imap_get_messages_list(param->imap, param->first_index,
1500 result = op->result;
1502 result->fetch_result = fetch_result;
1503 debug_print("imap fetch_uid run - end %i\n", r);
1506 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1507 carray ** fetch_result)
1509 struct fetch_uid_param param;
1510 struct fetch_uid_result result;
1513 debug_print("imap fetch_uid - begin\n");
1515 imap = get_imap(folder);
1517 param.first_index = first_index;
1519 threaded_run(folder, ¶m, &result, fetch_uid_run);
1521 if (result.error != MAILIMAP_NO_ERROR)
1522 return result.error;
1524 debug_print("imap fetch_uid - end\n");
1526 * fetch_result = result.fetch_result;
1528 return result.error;
1532 void imap_fetch_uid_list_free(carray * uid_list)
1536 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1539 puid = carray_get(uid_list, i);
1542 carray_free(uid_list);
1547 static int imap_fetch(mailimap * imap,
1550 size_t * result_len)
1553 struct mailimap_set * set;
1554 struct mailimap_fetch_att * fetch_att;
1555 struct mailimap_fetch_type * fetch_type;
1556 clist * fetch_result;
1557 struct mailimap_msg_att * msg_att;
1558 struct mailimap_msg_att_item * msg_att_item;
1563 struct mailimap_section * section;
1565 set = mailimap_set_new_single(msg_index);
1567 res = MAILIMAP_ERROR_MEMORY;
1571 section = mailimap_section_new(NULL);
1572 if (section == NULL) {
1573 res = MAILIMAP_ERROR_MEMORY;
1577 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1578 if (fetch_att == NULL) {
1579 mailimap_section_free(section);
1580 res = MAILIMAP_ERROR_MEMORY;
1584 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1585 if (fetch_type == NULL) {
1586 res = MAILIMAP_ERROR_MEMORY;
1587 goto free_fetch_att;
1590 mailstream_logger = imap_logger_fetch;
1592 r = mailimap_uid_fetch(imap, set,
1593 fetch_type, &fetch_result);
1595 mailstream_logger = imap_logger_cmd;
1597 mailimap_fetch_type_free(fetch_type);
1598 mailimap_set_free(set);
1601 case MAILIMAP_NO_ERROR:
1607 if (clist_begin(fetch_result) == NULL) {
1608 mailimap_fetch_list_free(fetch_result);
1609 return MAILIMAP_ERROR_FETCH;
1612 msg_att = clist_begin(fetch_result)->data;
1617 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1618 cur = clist_next(cur)) {
1619 msg_att_item = clist_content(cur);
1621 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1622 if (msg_att_item->att_data.att_static->att_type ==
1623 MAILIMAP_MSG_ATT_BODY_SECTION) {
1624 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1626 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1628 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1633 mailimap_fetch_list_free(fetch_result);
1636 return MAILIMAP_ERROR_FETCH;
1639 * result_len = text_length;
1641 return MAILIMAP_NO_ERROR;
1644 mailimap_fetch_att_free(fetch_att);
1646 mailimap_set_free(set);
1651 static int imap_fetch_header(mailimap * imap,
1654 size_t * result_len)
1657 struct mailimap_set * set;
1658 struct mailimap_fetch_att * fetch_att;
1659 struct mailimap_fetch_type * fetch_type;
1660 clist * fetch_result;
1661 struct mailimap_msg_att * msg_att;
1662 struct mailimap_msg_att_item * msg_att_item;
1667 struct mailimap_section * section;
1669 set = mailimap_set_new_single(msg_index);
1671 res = MAILIMAP_ERROR_MEMORY;
1675 section = mailimap_section_new_header();
1676 if (section == NULL) {
1677 res = MAILIMAP_ERROR_MEMORY;
1681 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1682 if (fetch_att == NULL) {
1683 mailimap_section_free(section);
1684 res = MAILIMAP_ERROR_MEMORY;
1688 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1689 if (fetch_type == NULL) {
1690 res = MAILIMAP_ERROR_MEMORY;
1691 goto free_fetch_att;
1694 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1696 mailimap_fetch_type_free(fetch_type);
1697 mailimap_set_free(set);
1700 case MAILIMAP_NO_ERROR:
1706 if (clist_begin(fetch_result) == NULL) {
1707 mailimap_fetch_list_free(fetch_result);
1708 return MAILIMAP_ERROR_FETCH;
1711 msg_att = clist_begin(fetch_result)->data;
1716 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1717 cur = clist_next(cur)) {
1718 msg_att_item = clist_content(cur);
1720 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1721 if (msg_att_item->att_data.att_static->att_type ==
1722 MAILIMAP_MSG_ATT_BODY_SECTION) {
1723 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1724 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1726 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1731 mailimap_fetch_list_free(fetch_result);
1734 return MAILIMAP_ERROR_FETCH;
1737 * result_len = text_length;
1739 return MAILIMAP_NO_ERROR;
1742 mailimap_fetch_att_free(fetch_att);
1744 mailimap_set_free(set);
1751 struct fetch_content_param {
1754 const char * filename;
1758 struct fetch_content_result {
1762 static void fetch_content_run(struct etpan_thread_op * op)
1764 struct fetch_content_param * param;
1765 struct fetch_content_result * result;
1767 size_t content_size;
1776 if (param->with_body)
1777 r = imap_fetch(param->imap, param->msg_index,
1778 &content, &content_size);
1780 r = imap_fetch_header(param->imap, param->msg_index,
1781 &content, &content_size);
1783 result = op->result;
1786 if (r == MAILIMAP_NO_ERROR) {
1787 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1789 result->error = MAILIMAP_ERROR_FETCH;
1793 f = fdopen(fd, "wb");
1795 result->error = MAILIMAP_ERROR_FETCH;
1799 r = fwrite(content, 1, content_size, f);
1806 g_unlink(param->filename);
1817 g_unlink(param->filename);
1820 if (mmap_string_unref(content) != 0)
1824 debug_print("imap fetch_content run - end %i\n", r);
1827 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1829 const char * filename)
1831 struct fetch_content_param param;
1832 struct fetch_content_result result;
1835 debug_print("imap fetch_content - begin\n");
1837 imap = get_imap(folder);
1839 param.msg_index = msg_index;
1840 param.filename = filename;
1841 param.with_body = with_body;
1843 threaded_run(folder, ¶m, &result, fetch_content_run);
1845 if (result.error != MAILIMAP_NO_ERROR)
1846 return result.error;
1848 debug_print("imap fetch_content - end\n");
1850 return result.error;
1855 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1863 flag_list = att_dyn->att_list;
1864 if (flag_list == NULL)
1867 for(cur = clist_begin(flag_list) ; cur != NULL ;
1868 cur = clist_next(cur)) {
1869 struct mailimap_flag_fetch * flag_fetch;
1871 flag_fetch = clist_content(cur);
1872 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1875 switch (flag_fetch->fl_flag->fl_type) {
1876 case MAILIMAP_FLAG_ANSWERED:
1877 flags |= MSG_REPLIED;
1879 case MAILIMAP_FLAG_FLAGGED:
1880 flags |= MSG_MARKED;
1882 case MAILIMAP_FLAG_DELETED:
1883 flags |= MSG_DELETED;
1885 case MAILIMAP_FLAG_SEEN:
1886 flags &= ~MSG_UNREAD;
1896 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1900 struct mailimap_msg_att_dynamic ** patt_dyn)
1902 clistiter * item_cur;
1906 struct mailimap_msg_att_dynamic * att_dyn;
1913 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1914 item_cur = clist_next(item_cur)) {
1915 struct mailimap_msg_att_item * item;
1917 item = clist_content(item_cur);
1919 switch (item->att_type) {
1920 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1921 switch (item->att_data.att_static->att_type) {
1922 case MAILIMAP_MSG_ATT_UID:
1923 uid = item->att_data.att_static->att_data.att_uid;
1926 case MAILIMAP_MSG_ATT_BODY_SECTION:
1927 if (headers == NULL) {
1928 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1931 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1932 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1937 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1938 if (att_dyn == NULL) {
1939 att_dyn = item->att_data.att_dyn;
1947 if (pheaders != NULL)
1948 * pheaders = headers;
1949 if (pref_size != NULL)
1950 * pref_size = ref_size;
1951 if (patt_dyn != NULL)
1952 * patt_dyn = att_dyn;
1954 return MAIL_NO_ERROR;
1957 static struct imap_fetch_env_info *
1958 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1960 struct imap_fetch_env_info * info;
1964 struct mailimap_msg_att_dynamic * att_dyn;
1966 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1971 info = malloc(sizeof(* info));
1973 info->headers = strdup(headers);
1975 info->flags = imap_flags_to_flags(att_dyn);
1981 imap_fetch_result_to_envelop_list(clist * fetch_result,
1982 carray ** p_env_list)
1989 env_list = carray_new(16);
1991 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1992 cur = clist_next(cur)) {
1993 struct mailimap_msg_att * msg_att;
1994 struct imap_fetch_env_info * env_info;
1996 msg_att = clist_content(cur);
1998 env_info = fetch_to_env_info(msg_att);
2000 return MAILIMAP_ERROR_MEMORY;
2001 carray_add(env_list, env_info, NULL);
2004 * p_env_list = env_list;
2006 return MAIL_NO_ERROR;
2009 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2011 struct mailimap_fetch_att * fetch_att;
2015 struct mailimap_header_list * imap_hdrlist;
2016 struct mailimap_section * section;
2018 hdrlist = clist_new();
2020 header = strdup("Date");
2021 r = clist_append(hdrlist, header);
2022 header = strdup("From");
2023 r = clist_append(hdrlist, header);
2024 header = strdup("To");
2025 r = clist_append(hdrlist, header);
2026 header = strdup("Cc");
2027 r = clist_append(hdrlist, header);
2028 header = strdup("Subject");
2029 r = clist_append(hdrlist, header);
2030 header = strdup("Message-ID");
2031 r = clist_append(hdrlist, header);
2032 header = strdup("References");
2033 r = clist_append(hdrlist, header);
2034 header = strdup("In-Reply-To");
2035 r = clist_append(hdrlist, header);
2037 imap_hdrlist = mailimap_header_list_new(hdrlist);
2038 section = mailimap_section_new_header_fields(imap_hdrlist);
2039 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2040 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2042 return MAIL_NO_ERROR;
2045 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2047 struct mailimap_fetch_att * fetch_att;
2048 struct mailimap_section * section;
2050 section = mailimap_section_new_header();
2051 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2052 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2054 return MAIL_NO_ERROR;
2058 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2059 carray ** p_env_list)
2061 struct mailimap_fetch_att * fetch_att;
2062 struct mailimap_fetch_type * fetch_type;
2064 clist * fetch_result;
2066 carray * env_list = NULL;
2070 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2073 fetch_att = mailimap_fetch_att_new_uid();
2074 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2077 fetch_att = mailimap_fetch_att_new_flags();
2078 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2081 fetch_att = mailimap_fetch_att_new_rfc822_size();
2082 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2086 key.len = sizeof(imap);
2087 r = chash_get(courier_workaround_hash, &key, &value);
2089 r = imap_add_envelope_fetch_att(fetch_type);
2091 r = imap_add_header_fetch_att(fetch_type);
2093 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2096 case MAILIMAP_NO_ERROR:
2099 mailimap_fetch_type_free(fetch_type);
2100 debug_print("uid_fetch: %d\n", r);
2104 if (clist_begin(fetch_result) == NULL) {
2105 res = MAILIMAP_ERROR_FETCH;
2106 debug_print("clist_begin = NULL\n");
2110 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2111 mailimap_fetch_list_free(fetch_result);
2113 if (r != MAILIMAP_NO_ERROR) {
2114 mailimap_fetch_type_free(fetch_type);
2115 res = MAILIMAP_ERROR_MEMORY;
2116 debug_print("fetch_result_to_envelop_list: %d\n", res);
2120 mailimap_fetch_type_free(fetch_type);
2122 * p_env_list = env_list;
2124 return MAILIMAP_NO_ERROR;
2130 struct fetch_env_param {
2132 struct mailimap_set * set;
2135 struct fetch_env_result {
2136 carray * fetch_env_result;
2140 static void fetch_env_run(struct etpan_thread_op * op)
2142 struct fetch_env_param * param;
2143 struct fetch_env_result * result;
2150 r = imap_get_envelopes_list(param->imap, param->set,
2153 result = op->result;
2155 result->fetch_env_result = env_list;
2157 debug_print("imap fetch_env run - end %i\n", r);
2160 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2161 carray ** p_env_list)
2163 struct fetch_env_param param;
2164 struct fetch_env_result result;
2167 debug_print("imap fetch_env - begin\n");
2169 imap = get_imap(folder);
2173 threaded_run(folder, ¶m, &result, fetch_env_run);
2175 if (result.error != MAILIMAP_NO_ERROR) {
2181 key.len = sizeof(imap);
2182 r = chash_get(courier_workaround_hash, &key, &value);
2186 chash_set(courier_workaround_hash, &key, &value, NULL);
2188 threaded_run(folder, ¶m, &result, fetch_env_run);
2192 if (result.error != MAILIMAP_NO_ERROR)
2193 return result.error;
2195 debug_print("imap fetch_env - end\n");
2197 * p_env_list = result.fetch_env_result;
2199 return result.error;
2202 void imap_fetch_env_free(carray * env_list)
2206 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2207 struct imap_fetch_env_info * env_info;
2209 env_info = carray_get(env_list, i);
2210 free(env_info->headers);
2213 carray_free(env_list);
2220 struct append_param {
2222 const char * mailbox;
2223 const char * filename;
2224 struct mailimap_flag_list * flag_list;
2227 struct append_result {
2232 static void append_run(struct etpan_thread_op * op)
2234 struct append_param * param;
2235 struct append_result * result;
2239 struct stat stat_buf;
2241 guint32 uid = 0, val = 0;
2244 result = op->result;
2246 r = stat(param->filename, &stat_buf);
2248 result->error = MAILIMAP_ERROR_APPEND;
2251 size = stat_buf.st_size;
2253 fd = open(param->filename, O_RDONLY);
2255 result->error = MAILIMAP_ERROR_APPEND;
2259 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2260 if (data == (void *) MAP_FAILED) {
2262 result->error = MAILIMAP_ERROR_APPEND;
2266 mailstream_logger = imap_logger_append;
2268 r = mailimap_uidplus_append(param->imap, param->mailbox,
2269 param->flag_list, NULL,
2270 data, size, &val, &uid);
2272 mailstream_logger = imap_logger_cmd;
2279 debug_print("imap append run - end %i uid %d\n", r, uid);
2282 int imap_threaded_append(Folder * folder, const char * mailbox,
2283 const char * filename,
2284 struct mailimap_flag_list * flag_list,
2287 struct append_param param;
2288 struct append_result result;
2291 debug_print("imap append - begin\n");
2293 imap = get_imap(folder);
2295 param.mailbox = mailbox;
2296 param.filename = filename;
2297 param.flag_list = flag_list;
2299 threaded_run(folder, ¶m, &result, append_run);
2301 if (result.error != MAILIMAP_NO_ERROR)
2302 return result.error;
2304 debug_print("imap append - end\n");
2308 return result.error;
2314 struct expunge_param {
2318 struct expunge_result {
2322 static void expunge_run(struct etpan_thread_op * op)
2324 struct expunge_param * param;
2325 struct expunge_result * result;
2329 r = mailimap_expunge(param->imap);
2331 result = op->result;
2333 debug_print("imap expunge run - end %i\n", r);
2336 int imap_threaded_expunge(Folder * folder)
2338 struct expunge_param param;
2339 struct expunge_result result;
2341 debug_print("imap expunge - begin\n");
2343 param.imap = get_imap(folder);
2345 threaded_run(folder, ¶m, &result, expunge_run);
2347 debug_print("imap expunge - end\n");
2349 return result.error;
2355 struct mailimap_set * set;
2359 struct copy_result {
2361 struct mailimap_set *source;
2362 struct mailimap_set *dest;
2365 static void copy_run(struct etpan_thread_op * op)
2367 struct copy_param * param;
2368 struct copy_result * result;
2372 struct mailimap_set *source = NULL, *dest = NULL;
2374 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2375 &val, &source, &dest);
2377 result = op->result;
2380 result->source = source;
2381 result->dest = dest;
2383 result->source = NULL;
2384 result->dest = NULL;
2386 debug_print("imap copy run - end %i\n", r);
2389 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2390 const char * mb, struct mailimap_set **source,
2391 struct mailimap_set **dest)
2393 struct copy_param param;
2394 struct copy_result result;
2397 debug_print("imap copy - begin\n");
2399 imap = get_imap(folder);
2404 threaded_run(folder, ¶m, &result, copy_run);
2408 if (result.error != MAILIMAP_NO_ERROR)
2409 return result.error;
2411 *source = result.source;
2412 *dest = result.dest;
2414 debug_print("imap copy - end\n");
2416 return result.error;
2421 struct store_param {
2423 struct mailimap_set * set;
2424 struct mailimap_store_att_flags * store_att_flags;
2427 struct store_result {
2431 static void store_run(struct etpan_thread_op * op)
2433 struct store_param * param;
2434 struct store_result * result;
2439 r = mailimap_uid_store(param->imap, param->set,
2440 param->store_att_flags);
2442 result = op->result;
2445 debug_print("imap store run - end %i\n", r);
2448 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2449 struct mailimap_store_att_flags * store_att_flags)
2451 struct store_param param;
2452 struct store_result result;
2455 debug_print("imap store - begin\n");
2457 imap = get_imap(folder);
2460 param.store_att_flags = store_att_flags;
2462 threaded_run(folder, ¶m, &result, store_run);
2464 if (result.error != MAILIMAP_NO_ERROR)
2465 return result.error;
2467 debug_print("imap store - end\n");
2469 return result.error;
2473 #define ENV_BUFFER_SIZE 512
2475 static void do_exec_command(int fd, const char * command,
2476 const char * servername, uint16_t port)
2480 char env_buffer[ENV_BUFFER_SIZE];
2484 /* Fork again to become a child of init rather than
2485 the etpan client. */
2491 snprintf(env_buffer, ENV_BUFFER_SIZE,
2492 "ETPANSERVER=%s", servername);
2494 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2498 setenv("ETPANSERVER", servername, 1);
2500 unsetenv("ETPANSERVER");
2505 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2507 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2513 snprintf(porttext, sizeof(porttext), "%d", port);
2514 setenv("ETPANPORT", porttext, 1);
2517 unsetenv("ETPANPORT");
2521 /* Not a lot we can do if there's an error other than bail. */
2522 if (dup2(fd, 0) == -1)
2524 if (dup2(fd, 1) == -1)
2527 /* Should we close stderr and reopen /dev/null? */
2529 maxopen = sysconf(_SC_OPEN_MAX);
2530 for (i=3; i < maxopen; i++)
2534 /* Detach from the controlling tty if we have one. Otherwise,
2535 SSH might do something stupid like trying to use it instead
2536 of running $SSH_ASKPASS. Doh. */
2537 fd = open("/dev/tty", O_RDONLY);
2539 ioctl(fd, TIOCNOTTY, NULL);
2542 #endif /* TIOCNOTTY */
2544 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2546 /* Eep. Shouldn't reach this */
2550 static int subcommand_connect(const char *command,
2551 const char *servername, uint16_t port)
2553 /* SEB unsupported on Windows */
2557 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2562 do_exec_command(sockfds[1], command, servername, port);
2564 else if (childpid == -1) {
2572 /* Reap child, leaving grandchild process to run */
2573 waitpid(childpid, NULL, 0);
2578 int socket_connect_cmd(mailimap * imap, const char * command,
2579 const char * server, int port)
2585 fd = subcommand_connect(command, server, port);
2587 return MAILIMAP_ERROR_STREAM;
2589 s = mailstream_socket_open(fd);
2592 return MAILIMAP_ERROR_STREAM;
2595 r = mailimap_connect(imap, s);
2596 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2597 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2598 mailstream_close(s);
2607 struct connect_cmd_param {
2609 const char * command;
2610 const char * server;
2614 struct connect_cmd_result {
2618 static void connect_cmd_run(struct etpan_thread_op * op)
2621 struct connect_cmd_param * param;
2622 struct connect_cmd_result * result;
2625 result = op->result;
2627 r = socket_connect_cmd(param->imap, param->command,
2628 param->server, param->port);
2634 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2635 const char * server, int port)
2637 struct connect_cmd_param param;
2638 struct connect_cmd_result result;
2643 imap = mailimap_new(0, NULL);
2646 key.len = sizeof(folder);
2649 chash_set(session_hash, &key, &value, NULL);
2652 param.command = command;
2653 param.server = server;
2656 threaded_run(folder, ¶m, &result, connect_cmd_run);
2658 debug_print("connect_cmd ok %i\n", result.error);
2660 return result.error;
2664 void imap_main_init(void)
2667 void imap_main_done(void)
2670 void imap_main_set_timeout(int sec)