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);
169 } else if (direction == 0 && size > 64) {
170 log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
173 buf = malloc(size+1);
174 memset(buf, 0, size+1);
175 strncpy(buf, str, size);
177 if (!strncmp(buf, "<<<<<<<", 7)
178 || !strncmp(buf, ">>>>>>>", 7)) {
182 while (strstr(buf, "\r"))
183 *strstr(buf, "\r") = ' ';
184 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
185 buf[strlen(buf)-1] = '\0';
187 lines = g_strsplit(buf, "\n", -1);
189 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
190 while (lines[i] && *lines[i]) {
191 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
195 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
201 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
202 static gboolean etpan_skip_ssl_cert_check = FALSE;
203 extern void mailsasl_ref(void);
205 void imap_main_init(gboolean skip_ssl_cert_check)
207 int fd_thread_manager;
209 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
210 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
211 mailstream_network_delay.tv_usec = 0;
213 mailstream_debug = 1;
214 mailstream_logger = imap_logger_cmd;
217 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
218 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
219 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
221 thread_manager = etpan_thread_manager_new();
223 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
225 io_channel = g_io_channel_unix_new(fd_thread_manager);
227 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
228 thread_manager_event,
233 void imap_main_set_timeout(int sec)
235 mailstream_network_delay.tv_sec = sec;
236 mailstream_network_delay.tv_usec = 0;
239 void imap_main_done(void)
241 etpan_thread_manager_stop(thread_manager);
242 etpan_thread_manager_join(thread_manager);
244 g_source_remove(thread_manager_signal);
245 g_io_channel_unref(io_channel);
247 etpan_thread_manager_free(thread_manager);
249 chash_free(courier_workaround_hash);
250 chash_free(session_hash);
251 chash_free(imap_hash);
254 void imap_init(Folder * folder)
256 struct etpan_thread * thread;
260 thread = etpan_thread_manager_get_thread(thread_manager);
263 key.len = sizeof(folder);
267 chash_set(imap_hash, &key, &value, NULL);
270 void imap_done(Folder * folder)
272 struct etpan_thread * thread;
278 key.len = sizeof(folder);
280 r = chash_get(imap_hash, &key, &value);
286 etpan_thread_unbind(thread);
288 chash_delete(imap_hash, &key, NULL);
290 debug_print("remove thread");
293 static struct etpan_thread * get_thread(Folder * folder)
295 struct etpan_thread * thread;
300 key.len = sizeof(folder);
302 chash_get(imap_hash, &key, &value);
308 static mailimap * get_imap(Folder * folder)
316 key.len = sizeof(folder);
318 r = chash_get(session_hash, &key, &value);
328 static void generic_cb(int cancelled, void * result, void * callback_data)
332 p_finished = callback_data;
334 debug_print("generic_cb\n");
339 static void threaded_run(Folder * folder, void * param, void * result,
340 void (* func)(struct etpan_thread_op * ))
342 struct etpan_thread_op * op;
343 struct etpan_thread * thread;
346 imap_folder_ref(folder);
348 op = etpan_thread_op_new();
354 op->callback = generic_cb;
355 op->callback_data = &finished;
360 thread = get_thread(folder);
361 etpan_thread_op_schedule(thread, op);
364 gtk_main_iteration();
367 etpan_thread_op_free(op);
369 imap_folder_unref(folder);
375 struct connect_param {
381 struct connect_result {
385 #define CHECK_IMAP() { \
386 if (!param->imap) { \
387 result->error = MAILIMAP_ERROR_BAD_STATE; \
392 static void connect_run(struct etpan_thread_op * op)
395 struct connect_param * param;
396 struct connect_result * result;
403 r = mailimap_socket_connect(param->imap,
404 param->server, param->port);
410 int imap_threaded_connect(Folder * folder, const char * server, int port)
412 struct connect_param param;
413 struct connect_result result;
418 imap = get_imap(folder);
421 key.len = sizeof(folder);
424 chash_delete(session_hash, &key, NULL);
426 debug_print("deleted old imap\n");
429 imap = mailimap_new(0, NULL);
432 key.len = sizeof(folder);
435 chash_set(session_hash, &key, &value, NULL);
438 param.server = server;
442 threaded_run(folder, ¶m, &result, connect_run);
444 debug_print("connect ok %i\n", result.error);
449 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
452 struct connect_param *param = (struct connect_param *)data;
455 if (certificate == NULL || len < 0) {
456 g_warning("no cert presented.\n");
459 cert = d2i_X509(NULL, &certificate, len);
461 g_warning("can't get cert\n");
463 } else if (ssl_certificate_check(cert, NULL,
464 (gchar *)param->server, (gushort)param->port) == TRUE) {
476 static void connect_ssl_run(struct etpan_thread_op * op)
479 struct connect_param * param;
480 struct connect_result * result;
487 r = mailimap_ssl_connect(param->imap,
488 param->server, param->port);
492 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
494 struct connect_param param;
495 struct connect_result result;
499 unsigned char *certificate = NULL;
502 imap = get_imap(folder);
505 key.len = sizeof(folder);
508 chash_delete(session_hash, &key, NULL);
510 debug_print("deleted old imap\n");
513 imap = mailimap_new(0, NULL);
516 key.len = sizeof(folder);
519 chash_set(session_hash, &key, &value, NULL);
522 param.server = server;
526 threaded_run(folder, ¶m, &result, connect_ssl_run);
528 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
529 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
530 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
531 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
536 debug_print("connect %d\n", result.error);
547 struct mailimap_capability_data *caps;
550 static void capability_run(struct etpan_thread_op * op)
553 struct capa_param * param;
554 struct capa_result * result;
555 struct mailimap_capability_data *caps;
562 r = mailimap_capability(param->imap, &caps);
565 result->caps = (r == 0 ? caps : NULL);
569 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
571 struct capa_param param;
572 struct capa_result result;
575 imap = get_imap(folder);
579 threaded_run(folder, ¶m, &result, capability_run);
581 debug_print("capa %d\n", result.error);
590 struct disconnect_param {
594 struct disconnect_result {
598 static void disconnect_run(struct etpan_thread_op * op)
601 struct disconnect_param * param;
602 struct disconnect_result * result;
609 r = mailimap_logout(param->imap);
614 void imap_threaded_disconnect(Folder * folder)
616 struct connect_param param;
617 struct connect_result result;
622 imap = get_imap(folder);
624 debug_print("was disconnected\n");
630 threaded_run(folder, ¶m, &result, disconnect_run);
633 key.len = sizeof(folder);
636 chash_delete(session_hash, &key, NULL);
639 key.len = sizeof(imap);
640 chash_delete(courier_workaround_hash, &key, NULL);
644 debug_print("disconnect ok\n");
651 const char * wildcard;
659 static void list_run(struct etpan_thread_op * op)
661 struct list_param * param;
662 struct list_result * result;
672 r = mailimap_list(param->imap, param->base,
673 param->wildcard, &list);
677 debug_print("imap list run - end\n");
680 int imap_threaded_list(Folder * folder, const char * base,
681 const char * wildcard,
684 struct list_param param;
685 struct list_result result;
687 debug_print("imap list - begin\n");
689 param.imap = get_imap(folder);
691 param.wildcard = wildcard;
693 threaded_run(folder, ¶m, &result, list_run);
695 * p_result = result.list;
697 debug_print("imap list - end %p\n", result.list);
707 const char * password;
712 struct login_result {
716 static void login_run(struct etpan_thread_op * op)
718 struct login_param * param;
719 struct login_result * result;
721 #ifdef DISABLE_LOG_DURING_LOGIN
730 #ifdef DISABLE_LOG_DURING_LOGIN
731 old_debug = mailstream_debug;
732 mailstream_debug = 0;
734 if (!strcmp(param->type, "LOGIN"))
735 r = mailimap_login(param->imap,
736 param->login, param->password);
737 else if (!strcmp(param->type, "GSSAPI"))
738 r = mailimap_authenticate(param->imap,
739 param->type, param->server, NULL, NULL,
740 param->login, param->login,
741 param->password, NULL);
743 r = mailimap_authenticate(param->imap,
744 param->type, NULL, NULL, NULL,
745 param->login, param->login,
746 param->password, NULL);
747 #ifdef DISABLE_LOG_DURING_LOGIN
748 mailstream_debug = old_debug;
752 debug_print("imap login run - end %i\n", r);
755 int imap_threaded_login(Folder * folder,
756 const char * login, const char * password,
759 struct login_param param;
760 struct login_result result;
762 debug_print("imap login - begin\n");
764 param.imap = get_imap(folder);
766 param.password = password;
768 if (folder && folder->account)
769 param.server = folder->account->recv_server;
773 threaded_run(folder, ¶m, &result, login_run);
775 debug_print("imap login - end\n");
781 struct status_param {
784 struct mailimap_status_att_list * status_att_list;
787 struct status_result {
789 struct mailimap_mailbox_data_status * data_status;
792 static void status_run(struct etpan_thread_op * op)
794 struct status_param * param;
795 struct status_result * result;
803 r = mailimap_status(param->imap, param->mb,
804 param->status_att_list,
805 &result->data_status);
808 debug_print("imap status run - end %i\n", r);
811 int imap_threaded_status(Folder * folder, const char * mb,
812 struct mailimap_mailbox_data_status ** data_status,
815 struct status_param param;
816 struct status_result result;
817 struct mailimap_status_att_list * status_att_list;
819 debug_print("imap status - begin\n");
821 status_att_list = mailimap_status_att_list_new_empty();
823 mailimap_status_att_list_add(status_att_list,
824 MAILIMAP_STATUS_ATT_MESSAGES);
827 mailimap_status_att_list_add(status_att_list,
828 MAILIMAP_STATUS_ATT_RECENT);
831 mailimap_status_att_list_add(status_att_list,
832 MAILIMAP_STATUS_ATT_UIDNEXT);
835 mailimap_status_att_list_add(status_att_list,
836 MAILIMAP_STATUS_ATT_UIDVALIDITY);
839 mailimap_status_att_list_add(status_att_list,
840 MAILIMAP_STATUS_ATT_UNSEEN);
842 param.imap = get_imap(folder);
844 param.status_att_list = status_att_list;
846 threaded_run(folder, ¶m, &result, status_run);
848 debug_print("imap status - end\n");
850 * data_status = result.data_status;
852 mailimap_status_att_list_free(status_att_list);
867 static void noop_run(struct etpan_thread_op * op)
869 struct noop_param * param;
870 struct noop_result * result;
878 r = mailimap_noop(param->imap);
881 debug_print("imap noop run - end %i\n", r);
884 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
886 struct noop_param param;
887 struct noop_result result;
890 debug_print("imap noop - begin\n");
892 imap = get_imap(folder);
895 threaded_run(folder, ¶m, &result, noop_run);
897 if (imap->imap_selection_info != NULL) {
898 * p_exists = imap->imap_selection_info->sel_exists;
904 debug_print("imap noop - end\n");
910 struct starttls_result {
914 static void starttls_run(struct etpan_thread_op * op)
916 struct connect_param * param;
917 struct starttls_result * result;
925 r = mailimap_starttls(param->imap);
928 debug_print("imap starttls run - end %i\n", r);
931 mailimap *imap = param->imap;
932 mailstream_low *plain_low = NULL;
933 mailstream_low *tls_low = NULL;
936 plain_low = mailstream_get_low(imap->imap_stream);
937 fd = mailstream_low_get_fd(plain_low);
939 debug_print("imap starttls run - can't get fd\n");
940 result->error = MAILIMAP_ERROR_STREAM;
944 tls_low = mailstream_low_tls_open(fd);
945 if (tls_low == NULL) {
946 debug_print("imap starttls run - can't tls_open\n");
947 result->error = MAILIMAP_ERROR_STREAM;
950 mailstream_low_free(plain_low);
951 mailstream_set_low(imap->imap_stream, tls_low);
955 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
957 struct connect_param param;
958 struct starttls_result result;
960 unsigned char *certificate;
962 debug_print("imap starttls - begin\n");
964 param.imap = get_imap(folder);
968 threaded_run(folder, ¶m, &result, starttls_run);
970 debug_print("imap starttls - end\n");
972 if (result.error == 0 && !etpan_skip_ssl_cert_check) {
973 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
974 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
975 result.error = MAILIMAP_ERROR_STREAM;
984 struct create_param {
989 struct create_result {
993 static void create_run(struct etpan_thread_op * op)
995 struct create_param * param;
996 struct create_result * result;
1000 result = op->result;
1004 r = mailimap_create(param->imap, param->mb);
1007 debug_print("imap create run - end %i\n", r);
1010 int imap_threaded_create(Folder * folder, const char * mb)
1012 struct create_param param;
1013 struct create_result result;
1015 debug_print("imap create - begin\n");
1017 param.imap = get_imap(folder);
1020 threaded_run(folder, ¶m, &result, create_run);
1022 debug_print("imap create - end\n");
1024 return result.error;
1030 struct rename_param {
1033 const char * new_name;
1036 struct rename_result {
1040 static void rename_run(struct etpan_thread_op * op)
1042 struct rename_param * param;
1043 struct rename_result * result;
1047 result = op->result;
1051 r = mailimap_rename(param->imap, param->mb, param->new_name);
1054 debug_print("imap rename run - end %i\n", r);
1057 int imap_threaded_rename(Folder * folder,
1058 const char * mb, const char * new_name)
1060 struct rename_param param;
1061 struct rename_result result;
1063 debug_print("imap rename - begin\n");
1065 param.imap = get_imap(folder);
1067 param.new_name = new_name;
1069 threaded_run(folder, ¶m, &result, rename_run);
1071 debug_print("imap rename - end\n");
1073 return result.error;
1079 struct delete_param {
1084 struct delete_result {
1088 static void delete_run(struct etpan_thread_op * op)
1090 struct delete_param * param;
1091 struct delete_result * result;
1095 result = op->result;
1099 r = mailimap_delete(param->imap, param->mb);
1102 debug_print("imap delete run - end %i\n", r);
1105 int imap_threaded_delete(Folder * folder, const char * mb)
1107 struct delete_param param;
1108 struct delete_result result;
1110 debug_print("imap delete - begin\n");
1112 param.imap = get_imap(folder);
1115 threaded_run(folder, ¶m, &result, delete_run);
1117 debug_print("imap delete - end\n");
1119 return result.error;
1124 struct select_param {
1129 struct select_result {
1133 static void select_run(struct etpan_thread_op * op)
1135 struct select_param * param;
1136 struct select_result * result;
1140 result = op->result;
1144 r = mailimap_select(param->imap, param->mb);
1147 debug_print("imap select run - end %i\n", r);
1150 int imap_threaded_select(Folder * folder, const char * mb,
1151 gint * exists, gint * recent, gint * unseen,
1152 guint32 * uid_validity)
1154 struct select_param param;
1155 struct select_result result;
1158 debug_print("imap select - begin\n");
1160 imap = get_imap(folder);
1164 threaded_run(folder, ¶m, &result, select_run);
1166 if (result.error != MAILIMAP_NO_ERROR)
1167 return result.error;
1169 if (imap->imap_selection_info == NULL)
1170 return MAILIMAP_ERROR_PARSE;
1172 * exists = imap->imap_selection_info->sel_exists;
1173 * recent = imap->imap_selection_info->sel_recent;
1174 * unseen = imap->imap_selection_info->sel_unseen;
1175 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1177 debug_print("imap select - end\n");
1179 return result.error;
1184 struct examine_param {
1189 struct examine_result {
1193 static void examine_run(struct etpan_thread_op * op)
1195 struct examine_param * param;
1196 struct examine_result * result;
1200 result = op->result;
1204 r = mailimap_examine(param->imap, param->mb);
1207 debug_print("imap examine run - end %i\n", r);
1210 int imap_threaded_examine(Folder * folder, const char * mb,
1211 gint * exists, gint * recent, gint * unseen,
1212 guint32 * uid_validity)
1214 struct examine_param param;
1215 struct examine_result result;
1218 debug_print("imap examine - begin\n");
1220 imap = get_imap(folder);
1224 threaded_run(folder, ¶m, &result, examine_run);
1226 if (result.error != MAILIMAP_NO_ERROR)
1227 return result.error;
1229 if (imap->imap_selection_info == NULL)
1230 return MAILIMAP_ERROR_PARSE;
1232 * exists = imap->imap_selection_info->sel_exists;
1233 * recent = imap->imap_selection_info->sel_recent;
1234 * unseen = imap->imap_selection_info->sel_unseen;
1235 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1237 debug_print("imap examine - end\n");
1239 return result.error;
1245 struct search_param {
1248 struct mailimap_set * set;
1251 struct search_result {
1253 clist * search_result;
1256 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1258 return mailimap_set_item_new(orig->set_first, orig->set_last);
1261 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1263 clist *list = orig ? orig->set_list : NULL;
1264 clist *newlist = clist_new();
1269 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1270 clist_append(newlist,
1271 sc_mailimap_set_item_copy(
1272 (struct mailimap_set_item *)clist_content(cur)));
1273 return mailimap_set_new(newlist);
1276 static void search_run(struct etpan_thread_op * op)
1278 struct search_param * param;
1279 struct search_result * result;
1281 struct mailimap_search_key * key = NULL;
1282 struct mailimap_search_key * uid_key = NULL;
1283 struct mailimap_search_key * search_type_key;
1284 clist * search_result;
1287 result = op->result;
1291 /* we copy the mailimap_set because freeing the key is recursive */
1292 if (param->set != NULL) {
1293 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1294 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1295 uid_key = mailimap_search_key_new_all();
1297 search_type_key = NULL;
1298 switch (param->type) {
1299 case IMAP_SEARCH_TYPE_SIMPLE:
1300 search_type_key = NULL;
1303 case IMAP_SEARCH_TYPE_SEEN:
1304 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1305 NULL, NULL, NULL, NULL, NULL,
1306 NULL, NULL, NULL, NULL, NULL,
1307 NULL, NULL, NULL, NULL, 0,
1308 NULL, NULL, NULL, NULL, NULL,
1309 NULL, 0, NULL, NULL, NULL);
1312 case IMAP_SEARCH_TYPE_UNSEEN:
1313 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1314 NULL, NULL, NULL, NULL, NULL,
1315 NULL, NULL, NULL, NULL, NULL,
1316 NULL, NULL, NULL, NULL, 0,
1317 NULL, NULL, NULL, NULL, NULL,
1318 NULL, 0, NULL, NULL, NULL);
1321 case IMAP_SEARCH_TYPE_ANSWERED:
1322 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1323 NULL, NULL, NULL, NULL, NULL,
1324 NULL, NULL, NULL, NULL, NULL,
1325 NULL, NULL, NULL, NULL, 0,
1326 NULL, NULL, NULL, NULL, NULL,
1327 NULL, 0, NULL, NULL, NULL);
1330 case IMAP_SEARCH_TYPE_FLAGGED:
1331 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1332 NULL, NULL, NULL, NULL, NULL,
1333 NULL, NULL, NULL, NULL, NULL,
1334 NULL, NULL, NULL, NULL, 0,
1335 NULL, NULL, NULL, NULL, NULL,
1336 NULL, 0, NULL, NULL, NULL);
1338 case IMAP_SEARCH_TYPE_DELETED:
1339 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1340 NULL, NULL, NULL, NULL, NULL,
1341 NULL, NULL, NULL, NULL, NULL,
1342 NULL, NULL, NULL, NULL, 0,
1343 NULL, NULL, NULL, NULL, NULL,
1344 NULL, 0, NULL, NULL, NULL);
1348 if (search_type_key != NULL) {
1349 if (param->set != NULL) {
1350 key = mailimap_search_key_new_multiple_empty();
1351 mailimap_search_key_multiple_add(key, search_type_key);
1352 mailimap_search_key_multiple_add(key, uid_key);
1354 key = search_type_key;
1356 } else if (uid_key != NULL) {
1361 g_warning("no key!");
1362 result = op->result;
1364 result->search_result = NULL;
1366 mailstream_logger = imap_logger_uid;
1368 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1370 mailstream_logger = imap_logger_cmd;
1372 /* free the key (with the imapset) */
1373 mailimap_search_key_free(key);
1376 result->search_result = search_result;
1378 debug_print("imap search run - end %i\n", result->error);
1381 int imap_threaded_search(Folder * folder, int search_type,
1382 struct mailimap_set * set, clist ** search_result)
1384 struct search_param param;
1385 struct search_result result;
1388 debug_print("imap search - begin\n");
1390 imap = get_imap(folder);
1393 param.type = search_type;
1395 threaded_run(folder, ¶m, &result, search_run);
1397 if (result.error != MAILIMAP_NO_ERROR)
1398 return result.error;
1400 debug_print("imap search - end\n");
1402 * search_result = result.search_result;
1404 return result.error;
1411 uid_list_to_env_list(clist * fetch_result, carray ** result)
1419 tab = carray_new(128);
1421 res = MAILIMAP_ERROR_MEMORY;
1425 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1426 cur = clist_next(cur)) {
1427 struct mailimap_msg_att * msg_att;
1428 clistiter * item_cur;
1433 msg_att = clist_content(cur);
1437 for(item_cur = clist_begin(msg_att->att_list) ;
1439 item_cur = clist_next(item_cur)) {
1440 struct mailimap_msg_att_item * item;
1442 item = clist_content(item_cur);
1444 switch (item->att_type) {
1445 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1446 switch (item->att_data.att_static->att_type) {
1447 case MAILIMAP_MSG_ATT_UID:
1448 uid = item->att_data.att_static->att_data.att_uid;
1454 puid = malloc(sizeof(* puid));
1456 res = MAILIMAP_ERROR_MEMORY;
1461 r = carray_add(tab, puid, NULL);
1464 res = MAILIMAP_ERROR_MEMORY;
1471 return MAILIMAP_NO_ERROR;
1474 for(i = 0 ; i < carray_count(tab) ; i++)
1475 mailmessage_free(carray_get(tab, i));
1480 static int imap_get_messages_list(mailimap * imap,
1481 uint32_t first_index,
1486 struct mailimap_fetch_att * fetch_att;
1487 struct mailimap_fetch_type * fetch_type;
1488 struct mailimap_set * set;
1489 clist * fetch_result;
1492 set = mailimap_set_new_interval(first_index, 0);
1494 res = MAILIMAP_ERROR_MEMORY;
1498 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1499 if (fetch_type == NULL) {
1500 res = MAILIMAP_ERROR_MEMORY;
1504 fetch_att = mailimap_fetch_att_new_uid();
1505 if (fetch_att == NULL) {
1506 res = MAILIMAP_ERROR_MEMORY;
1507 goto free_fetch_type;
1510 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1511 if (r != MAILIMAP_NO_ERROR) {
1512 mailimap_fetch_att_free(fetch_att);
1513 res = MAILIMAP_ERROR_MEMORY;
1514 goto free_fetch_type;
1517 r = mailimap_uid_fetch(imap, set,
1518 fetch_type, &fetch_result);
1520 mailimap_fetch_type_free(fetch_type);
1521 mailimap_set_free(set);
1523 if (r != MAILIMAP_NO_ERROR) {
1529 r = uid_list_to_env_list(fetch_result, &env_list);
1530 mailimap_fetch_list_free(fetch_result);
1532 * result = env_list;
1534 return MAILIMAP_NO_ERROR;
1537 mailimap_fetch_type_free(fetch_type);
1539 mailimap_set_free(set);
1547 struct fetch_uid_param {
1549 uint32_t first_index;
1552 struct fetch_uid_result {
1554 carray * fetch_result;
1557 static void fetch_uid_run(struct etpan_thread_op * op)
1559 struct fetch_uid_param * param;
1560 struct fetch_uid_result * result;
1561 carray * fetch_result;
1565 result = op->result;
1569 fetch_result = NULL;
1570 r = imap_get_messages_list(param->imap, param->first_index,
1574 result->fetch_result = fetch_result;
1575 debug_print("imap fetch_uid run - end %i\n", r);
1578 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1579 carray ** fetch_result)
1581 struct fetch_uid_param param;
1582 struct fetch_uid_result result;
1585 debug_print("imap fetch_uid - begin\n");
1587 imap = get_imap(folder);
1589 param.first_index = first_index;
1591 threaded_run(folder, ¶m, &result, fetch_uid_run);
1593 if (result.error != MAILIMAP_NO_ERROR)
1594 return result.error;
1596 debug_print("imap fetch_uid - end\n");
1598 * fetch_result = result.fetch_result;
1600 return result.error;
1604 void imap_fetch_uid_list_free(carray * uid_list)
1608 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1611 puid = carray_get(uid_list, i);
1614 carray_free(uid_list);
1619 static int imap_fetch(mailimap * imap,
1622 size_t * result_len)
1625 struct mailimap_set * set;
1626 struct mailimap_fetch_att * fetch_att;
1627 struct mailimap_fetch_type * fetch_type;
1628 clist * fetch_result;
1629 struct mailimap_msg_att * msg_att;
1630 struct mailimap_msg_att_item * msg_att_item;
1635 struct mailimap_section * section;
1637 set = mailimap_set_new_single(msg_index);
1639 res = MAILIMAP_ERROR_MEMORY;
1643 section = mailimap_section_new(NULL);
1644 if (section == NULL) {
1645 res = MAILIMAP_ERROR_MEMORY;
1649 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1650 if (fetch_att == NULL) {
1651 mailimap_section_free(section);
1652 res = MAILIMAP_ERROR_MEMORY;
1656 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1657 if (fetch_type == NULL) {
1658 res = MAILIMAP_ERROR_MEMORY;
1659 goto free_fetch_att;
1662 mailstream_logger = imap_logger_fetch;
1664 r = mailimap_uid_fetch(imap, set,
1665 fetch_type, &fetch_result);
1667 mailstream_logger = imap_logger_cmd;
1669 mailimap_fetch_type_free(fetch_type);
1670 mailimap_set_free(set);
1673 case MAILIMAP_NO_ERROR:
1679 if (clist_begin(fetch_result) == NULL) {
1680 mailimap_fetch_list_free(fetch_result);
1681 return MAILIMAP_ERROR_FETCH;
1684 msg_att = clist_begin(fetch_result)->data;
1689 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1690 cur = clist_next(cur)) {
1691 msg_att_item = clist_content(cur);
1693 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1694 if (msg_att_item->att_data.att_static->att_type ==
1695 MAILIMAP_MSG_ATT_BODY_SECTION) {
1696 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1698 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1700 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1705 mailimap_fetch_list_free(fetch_result);
1708 return MAILIMAP_ERROR_FETCH;
1711 * result_len = text_length;
1713 return MAILIMAP_NO_ERROR;
1716 mailimap_fetch_att_free(fetch_att);
1718 mailimap_set_free(set);
1723 static int imap_fetch_header(mailimap * imap,
1726 size_t * result_len)
1729 struct mailimap_set * set;
1730 struct mailimap_fetch_att * fetch_att;
1731 struct mailimap_fetch_type * fetch_type;
1732 clist * fetch_result;
1733 struct mailimap_msg_att * msg_att;
1734 struct mailimap_msg_att_item * msg_att_item;
1739 struct mailimap_section * section;
1741 set = mailimap_set_new_single(msg_index);
1743 res = MAILIMAP_ERROR_MEMORY;
1747 section = mailimap_section_new_header();
1748 if (section == NULL) {
1749 res = MAILIMAP_ERROR_MEMORY;
1753 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1754 if (fetch_att == NULL) {
1755 mailimap_section_free(section);
1756 res = MAILIMAP_ERROR_MEMORY;
1760 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1761 if (fetch_type == NULL) {
1762 res = MAILIMAP_ERROR_MEMORY;
1763 goto free_fetch_att;
1766 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1768 mailimap_fetch_type_free(fetch_type);
1769 mailimap_set_free(set);
1772 case MAILIMAP_NO_ERROR:
1778 if (clist_begin(fetch_result) == NULL) {
1779 mailimap_fetch_list_free(fetch_result);
1780 return MAILIMAP_ERROR_FETCH;
1783 msg_att = clist_begin(fetch_result)->data;
1788 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1789 cur = clist_next(cur)) {
1790 msg_att_item = clist_content(cur);
1792 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1793 if (msg_att_item->att_data.att_static->att_type ==
1794 MAILIMAP_MSG_ATT_BODY_SECTION) {
1795 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1796 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1798 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1803 mailimap_fetch_list_free(fetch_result);
1806 return MAILIMAP_ERROR_FETCH;
1809 * result_len = text_length;
1811 return MAILIMAP_NO_ERROR;
1814 mailimap_fetch_att_free(fetch_att);
1816 mailimap_set_free(set);
1823 struct fetch_content_param {
1826 const char * filename;
1830 struct fetch_content_result {
1834 static void fetch_content_run(struct etpan_thread_op * op)
1836 struct fetch_content_param * param;
1837 struct fetch_content_result * result;
1839 size_t content_size;
1845 result = op->result;
1851 if (param->with_body)
1852 r = imap_fetch(param->imap, param->msg_index,
1853 &content, &content_size);
1855 r = imap_fetch_header(param->imap, param->msg_index,
1856 &content, &content_size);
1860 if (r == MAILIMAP_NO_ERROR) {
1861 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1863 result->error = MAILIMAP_ERROR_FETCH;
1867 f = fdopen(fd, "wb");
1869 result->error = MAILIMAP_ERROR_FETCH;
1873 r = fwrite(content, 1, content_size, f);
1880 g_unlink(param->filename);
1891 g_unlink(param->filename);
1894 if (mmap_string_unref(content) != 0)
1898 debug_print("imap fetch_content run - end %i\n", r);
1901 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1903 const char * filename)
1905 struct fetch_content_param param;
1906 struct fetch_content_result result;
1909 debug_print("imap fetch_content - begin\n");
1911 imap = get_imap(folder);
1913 param.msg_index = msg_index;
1914 param.filename = filename;
1915 param.with_body = with_body;
1917 threaded_run(folder, ¶m, &result, fetch_content_run);
1919 if (result.error != MAILIMAP_NO_ERROR)
1920 return result.error;
1922 debug_print("imap fetch_content - end\n");
1924 return result.error;
1929 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1937 flag_list = att_dyn->att_list;
1938 if (flag_list == NULL)
1941 for(cur = clist_begin(flag_list) ; cur != NULL ;
1942 cur = clist_next(cur)) {
1943 struct mailimap_flag_fetch * flag_fetch;
1945 flag_fetch = clist_content(cur);
1946 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1949 switch (flag_fetch->fl_flag->fl_type) {
1950 case MAILIMAP_FLAG_ANSWERED:
1951 flags |= MSG_REPLIED;
1953 case MAILIMAP_FLAG_FLAGGED:
1954 flags |= MSG_MARKED;
1956 case MAILIMAP_FLAG_DELETED:
1957 flags |= MSG_DELETED;
1959 case MAILIMAP_FLAG_SEEN:
1960 flags &= ~MSG_UNREAD;
1970 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1974 struct mailimap_msg_att_dynamic ** patt_dyn)
1976 clistiter * item_cur;
1980 struct mailimap_msg_att_dynamic * att_dyn;
1987 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1988 item_cur = clist_next(item_cur)) {
1989 struct mailimap_msg_att_item * item;
1991 item = clist_content(item_cur);
1993 switch (item->att_type) {
1994 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1995 switch (item->att_data.att_static->att_type) {
1996 case MAILIMAP_MSG_ATT_UID:
1997 uid = item->att_data.att_static->att_data.att_uid;
2000 case MAILIMAP_MSG_ATT_BODY_SECTION:
2001 if (headers == NULL) {
2002 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2005 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2006 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2011 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2012 if (att_dyn == NULL) {
2013 att_dyn = item->att_data.att_dyn;
2021 if (pheaders != NULL)
2022 * pheaders = headers;
2023 if (pref_size != NULL)
2024 * pref_size = ref_size;
2025 if (patt_dyn != NULL)
2026 * patt_dyn = att_dyn;
2028 return MAIL_NO_ERROR;
2031 static struct imap_fetch_env_info *
2032 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2034 struct imap_fetch_env_info * info;
2038 struct mailimap_msg_att_dynamic * att_dyn;
2040 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2045 info = malloc(sizeof(* info));
2047 info->headers = strdup(headers);
2049 info->flags = imap_flags_to_flags(att_dyn);
2055 imap_fetch_result_to_envelop_list(clist * fetch_result,
2056 carray ** p_env_list)
2063 env_list = carray_new(16);
2065 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2066 cur = clist_next(cur)) {
2067 struct mailimap_msg_att * msg_att;
2068 struct imap_fetch_env_info * env_info;
2070 msg_att = clist_content(cur);
2072 env_info = fetch_to_env_info(msg_att);
2074 return MAILIMAP_ERROR_MEMORY;
2075 carray_add(env_list, env_info, NULL);
2078 * p_env_list = env_list;
2080 return MAIL_NO_ERROR;
2083 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2085 struct mailimap_fetch_att * fetch_att;
2089 struct mailimap_header_list * imap_hdrlist;
2090 struct mailimap_section * section;
2092 hdrlist = clist_new();
2094 header = strdup("Date");
2095 r = clist_append(hdrlist, header);
2096 header = strdup("From");
2097 r = clist_append(hdrlist, header);
2098 header = strdup("To");
2099 r = clist_append(hdrlist, header);
2100 header = strdup("Cc");
2101 r = clist_append(hdrlist, header);
2102 header = strdup("Subject");
2103 r = clist_append(hdrlist, header);
2104 header = strdup("Message-ID");
2105 r = clist_append(hdrlist, header);
2106 header = strdup("References");
2107 r = clist_append(hdrlist, header);
2108 header = strdup("In-Reply-To");
2109 r = clist_append(hdrlist, header);
2111 imap_hdrlist = mailimap_header_list_new(hdrlist);
2112 section = mailimap_section_new_header_fields(imap_hdrlist);
2113 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2114 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2116 return MAIL_NO_ERROR;
2119 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2121 struct mailimap_fetch_att * fetch_att;
2122 struct mailimap_section * section;
2124 section = mailimap_section_new_header();
2125 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2126 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2128 return MAIL_NO_ERROR;
2132 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2133 carray ** p_env_list)
2135 struct mailimap_fetch_att * fetch_att;
2136 struct mailimap_fetch_type * fetch_type;
2138 clist * fetch_result;
2140 carray * env_list = NULL;
2144 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2147 fetch_att = mailimap_fetch_att_new_uid();
2148 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2151 fetch_att = mailimap_fetch_att_new_flags();
2152 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2155 fetch_att = mailimap_fetch_att_new_rfc822_size();
2156 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2160 key.len = sizeof(imap);
2161 r = chash_get(courier_workaround_hash, &key, &value);
2163 r = imap_add_envelope_fetch_att(fetch_type);
2165 r = imap_add_header_fetch_att(fetch_type);
2167 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2170 case MAILIMAP_NO_ERROR:
2173 mailimap_fetch_type_free(fetch_type);
2174 debug_print("uid_fetch: %d\n", r);
2178 if (clist_begin(fetch_result) == NULL) {
2179 res = MAILIMAP_ERROR_FETCH;
2180 debug_print("clist_begin = NULL\n");
2184 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2185 mailimap_fetch_list_free(fetch_result);
2187 if (r != MAILIMAP_NO_ERROR) {
2188 mailimap_fetch_type_free(fetch_type);
2189 res = MAILIMAP_ERROR_MEMORY;
2190 debug_print("fetch_result_to_envelop_list: %d\n", res);
2194 mailimap_fetch_type_free(fetch_type);
2196 * p_env_list = env_list;
2198 return MAILIMAP_NO_ERROR;
2204 struct fetch_env_param {
2206 struct mailimap_set * set;
2209 struct fetch_env_result {
2210 carray * fetch_env_result;
2214 static void fetch_env_run(struct etpan_thread_op * op)
2216 struct fetch_env_param * param;
2217 struct fetch_env_result * result;
2222 result = op->result;
2227 r = imap_get_envelopes_list(param->imap, param->set,
2231 result->fetch_env_result = env_list;
2233 debug_print("imap fetch_env run - end %i\n", r);
2236 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2237 carray ** p_env_list)
2239 struct fetch_env_param param;
2240 struct fetch_env_result result;
2243 debug_print("imap fetch_env - begin\n");
2245 imap = get_imap(folder);
2249 threaded_run(folder, ¶m, &result, fetch_env_run);
2251 if (result.error != MAILIMAP_NO_ERROR) {
2257 key.len = sizeof(imap);
2258 r = chash_get(courier_workaround_hash, &key, &value);
2262 chash_set(courier_workaround_hash, &key, &value, NULL);
2264 threaded_run(folder, ¶m, &result, fetch_env_run);
2268 if (result.error != MAILIMAP_NO_ERROR)
2269 return result.error;
2271 debug_print("imap fetch_env - end\n");
2273 * p_env_list = result.fetch_env_result;
2275 return result.error;
2278 void imap_fetch_env_free(carray * env_list)
2282 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2283 struct imap_fetch_env_info * env_info;
2285 env_info = carray_get(env_list, i);
2286 free(env_info->headers);
2289 carray_free(env_list);
2296 struct append_param {
2298 const char * mailbox;
2299 const char * filename;
2300 struct mailimap_flag_list * flag_list;
2303 struct append_result {
2308 static void append_run(struct etpan_thread_op * op)
2310 struct append_param * param;
2311 struct append_result * result;
2315 struct stat stat_buf;
2317 guint32 uid = 0, val = 0;
2320 result = op->result;
2324 r = stat(param->filename, &stat_buf);
2326 result->error = MAILIMAP_ERROR_APPEND;
2329 size = stat_buf.st_size;
2331 fd = open(param->filename, O_RDONLY);
2333 result->error = MAILIMAP_ERROR_APPEND;
2337 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2338 if (data == (void *) MAP_FAILED) {
2340 result->error = MAILIMAP_ERROR_APPEND;
2344 mailstream_logger = imap_logger_append;
2346 r = mailimap_uidplus_append(param->imap, param->mailbox,
2347 param->flag_list, NULL,
2348 data, size, &val, &uid);
2350 mailstream_logger = imap_logger_cmd;
2357 debug_print("imap append run - end %i uid %d\n", r, uid);
2360 int imap_threaded_append(Folder * folder, const char * mailbox,
2361 const char * filename,
2362 struct mailimap_flag_list * flag_list,
2365 struct append_param param;
2366 struct append_result result;
2369 debug_print("imap append - begin\n");
2371 imap = get_imap(folder);
2373 param.mailbox = mailbox;
2374 param.filename = filename;
2375 param.flag_list = flag_list;
2377 threaded_run(folder, ¶m, &result, append_run);
2379 if (result.error != MAILIMAP_NO_ERROR)
2380 return result.error;
2382 debug_print("imap append - end\n");
2386 return result.error;
2392 struct expunge_param {
2396 struct expunge_result {
2400 static void expunge_run(struct etpan_thread_op * op)
2402 struct expunge_param * param;
2403 struct expunge_result * result;
2407 result = op->result;
2411 r = mailimap_expunge(param->imap);
2414 debug_print("imap expunge run - end %i\n", r);
2417 int imap_threaded_expunge(Folder * folder)
2419 struct expunge_param param;
2420 struct expunge_result result;
2422 debug_print("imap expunge - begin\n");
2424 param.imap = get_imap(folder);
2426 threaded_run(folder, ¶m, &result, expunge_run);
2428 debug_print("imap expunge - end\n");
2430 return result.error;
2436 struct mailimap_set * set;
2440 struct copy_result {
2442 struct mailimap_set *source;
2443 struct mailimap_set *dest;
2446 static void copy_run(struct etpan_thread_op * op)
2448 struct copy_param * param;
2449 struct copy_result * result;
2452 struct mailimap_set *source = NULL, *dest = NULL;
2455 result = op->result;
2459 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2460 &val, &source, &dest);
2464 result->source = source;
2465 result->dest = dest;
2467 result->source = NULL;
2468 result->dest = NULL;
2470 debug_print("imap copy run - end %i\n", r);
2473 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2474 const char * mb, struct mailimap_set **source,
2475 struct mailimap_set **dest)
2477 struct copy_param param;
2478 struct copy_result result;
2481 debug_print("imap copy - begin\n");
2483 imap = get_imap(folder);
2488 threaded_run(folder, ¶m, &result, copy_run);
2492 if (result.error != MAILIMAP_NO_ERROR)
2493 return result.error;
2495 *source = result.source;
2496 *dest = result.dest;
2498 debug_print("imap copy - end\n");
2500 return result.error;
2505 struct store_param {
2507 struct mailimap_set * set;
2508 struct mailimap_store_att_flags * store_att_flags;
2511 struct store_result {
2515 static void store_run(struct etpan_thread_op * op)
2517 struct store_param * param;
2518 struct store_result * result;
2522 result = op->result;
2526 r = mailimap_uid_store(param->imap, param->set,
2527 param->store_att_flags);
2531 debug_print("imap store run - end %i\n", r);
2534 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2535 struct mailimap_store_att_flags * store_att_flags)
2537 struct store_param param;
2538 struct store_result result;
2541 debug_print("imap store - begin\n");
2543 imap = get_imap(folder);
2546 param.store_att_flags = store_att_flags;
2548 threaded_run(folder, ¶m, &result, store_run);
2550 if (result.error != MAILIMAP_NO_ERROR)
2551 return result.error;
2553 debug_print("imap store - end\n");
2555 return result.error;
2559 #define ENV_BUFFER_SIZE 512
2561 static void do_exec_command(int fd, const char * command,
2562 const char * servername, uint16_t port)
2566 char env_buffer[ENV_BUFFER_SIZE];
2570 /* Fork again to become a child of init rather than
2571 the etpan client. */
2577 snprintf(env_buffer, ENV_BUFFER_SIZE,
2578 "ETPANSERVER=%s", servername);
2580 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2584 setenv("ETPANSERVER", servername, 1);
2586 unsetenv("ETPANSERVER");
2591 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2593 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2599 snprintf(porttext, sizeof(porttext), "%d", port);
2600 setenv("ETPANPORT", porttext, 1);
2603 unsetenv("ETPANPORT");
2607 /* Not a lot we can do if there's an error other than bail. */
2608 if (dup2(fd, 0) == -1)
2610 if (dup2(fd, 1) == -1)
2613 /* Should we close stderr and reopen /dev/null? */
2615 maxopen = sysconf(_SC_OPEN_MAX);
2616 for (i=3; i < maxopen; i++)
2620 /* Detach from the controlling tty if we have one. Otherwise,
2621 SSH might do something stupid like trying to use it instead
2622 of running $SSH_ASKPASS. Doh. */
2623 fd = open("/dev/tty", O_RDONLY);
2625 ioctl(fd, TIOCNOTTY, NULL);
2628 #endif /* TIOCNOTTY */
2630 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2632 /* Eep. Shouldn't reach this */
2636 static int subcommand_connect(const char *command,
2637 const char *servername, uint16_t port)
2639 /* SEB unsupported on Windows */
2643 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2648 do_exec_command(sockfds[1], command, servername, port);
2650 else if (childpid == -1) {
2658 /* Reap child, leaving grandchild process to run */
2659 waitpid(childpid, NULL, 0);
2664 int socket_connect_cmd(mailimap * imap, const char * command,
2665 const char * server, int port)
2671 fd = subcommand_connect(command, server, port);
2673 return MAILIMAP_ERROR_STREAM;
2675 s = mailstream_socket_open(fd);
2678 return MAILIMAP_ERROR_STREAM;
2681 r = mailimap_connect(imap, s);
2682 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2683 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2684 mailstream_close(s);
2693 struct connect_cmd_param {
2695 const char * command;
2696 const char * server;
2700 struct connect_cmd_result {
2704 static void connect_cmd_run(struct etpan_thread_op * op)
2707 struct connect_cmd_param * param;
2708 struct connect_cmd_result * result;
2711 result = op->result;
2715 r = socket_connect_cmd(param->imap, param->command,
2716 param->server, param->port);
2722 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2723 const char * server, int port)
2725 struct connect_cmd_param param;
2726 struct connect_cmd_result result;
2731 imap = get_imap(folder);
2734 key.len = sizeof(folder);
2737 chash_delete(session_hash, &key, NULL);
2738 mailimap_free(imap);
2739 debug_print("deleted old imap\n");
2742 imap = mailimap_new(0, NULL);
2745 key.len = sizeof(folder);
2748 chash_set(session_hash, &key, &value, NULL);
2751 param.command = command;
2752 param.server = server;
2755 threaded_run(folder, ¶m, &result, connect_cmd_run);
2757 debug_print("connect_cmd ok %i\n", result.error);
2759 return result.error;
2763 void imap_main_init(void)
2766 void imap_main_done(void)
2769 void imap_main_set_timeout(int sec)