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 = mailimap_new(0, NULL);
421 key.len = sizeof(folder);
424 chash_set(session_hash, &key, &value, NULL);
427 param.server = server;
431 threaded_run(folder, ¶m, &result, connect_run);
433 debug_print("connect ok %i\n", result.error);
438 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
441 struct connect_param *param = (struct connect_param *)data;
444 if (certificate == NULL || len < 0) {
445 g_warning("no cert presented.\n");
448 cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
450 g_warning("can't get cert\n");
452 } else if (ssl_certificate_check(cert, NULL,
453 (gchar *)param->server, (gushort)param->port) == TRUE) {
465 static void connect_ssl_run(struct etpan_thread_op * op)
468 struct connect_param * param;
469 struct connect_result * result;
476 r = mailimap_ssl_connect(param->imap,
477 param->server, param->port);
481 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
483 struct connect_param param;
484 struct connect_result result;
488 unsigned char *certificate = NULL;
491 imap = mailimap_new(0, NULL);
494 key.len = sizeof(folder);
497 chash_set(session_hash, &key, &value, NULL);
500 param.server = server;
504 threaded_run(folder, ¶m, &result, connect_ssl_run);
506 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
507 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
508 cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
509 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
514 debug_print("connect %d\n", result.error);
525 struct mailimap_capability_data *caps;
528 static void capability_run(struct etpan_thread_op * op)
531 struct capa_param * param;
532 struct capa_result * result;
533 struct mailimap_capability_data *caps;
540 r = mailimap_capability(param->imap, &caps);
543 result->caps = (r == 0 ? caps : NULL);
547 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
549 struct capa_param param;
550 struct capa_result result;
553 imap = get_imap(folder);
557 threaded_run(folder, ¶m, &result, capability_run);
559 debug_print("capa %d\n", result.error);
568 struct disconnect_param {
572 struct disconnect_result {
576 static void disconnect_run(struct etpan_thread_op * op)
579 struct disconnect_param * param;
580 struct disconnect_result * result;
587 r = mailimap_logout(param->imap);
592 void imap_threaded_disconnect(Folder * folder)
594 struct connect_param param;
595 struct connect_result result;
600 imap = get_imap(folder);
602 debug_print("was disconnected\n");
608 threaded_run(folder, ¶m, &result, disconnect_run);
611 key.len = sizeof(folder);
614 chash_delete(session_hash, &key, NULL);
617 key.len = sizeof(imap);
618 chash_delete(courier_workaround_hash, &key, NULL);
622 debug_print("disconnect ok\n");
629 const char * wildcard;
637 static void list_run(struct etpan_thread_op * op)
639 struct list_param * param;
640 struct list_result * result;
650 r = mailimap_list(param->imap, param->base,
651 param->wildcard, &list);
655 debug_print("imap list run - end\n");
658 int imap_threaded_list(Folder * folder, const char * base,
659 const char * wildcard,
662 struct list_param param;
663 struct list_result result;
665 debug_print("imap list - begin\n");
667 param.imap = get_imap(folder);
669 param.wildcard = wildcard;
671 threaded_run(folder, ¶m, &result, list_run);
673 * p_result = result.list;
675 debug_print("imap list - end %p\n", result.list);
685 const char * password;
690 struct login_result {
694 static void login_run(struct etpan_thread_op * op)
696 struct login_param * param;
697 struct login_result * result;
699 #ifdef DISABLE_LOG_DURING_LOGIN
708 #ifdef DISABLE_LOG_DURING_LOGIN
709 old_debug = mailstream_debug;
710 mailstream_debug = 0;
712 if (!strcmp(param->type, "LOGIN"))
713 r = mailimap_login(param->imap,
714 param->login, param->password);
715 else if (!strcmp(param->type, "GSSAPI"))
716 r = mailimap_authenticate(param->imap,
717 param->type, param->server, NULL, NULL,
718 param->login, param->login,
719 param->password, NULL);
721 r = mailimap_authenticate(param->imap,
722 param->type, NULL, NULL, NULL,
723 param->login, param->login,
724 param->password, NULL);
725 #ifdef DISABLE_LOG_DURING_LOGIN
726 mailstream_debug = old_debug;
730 debug_print("imap login run - end %i\n", r);
733 int imap_threaded_login(Folder * folder,
734 const char * login, const char * password,
737 struct login_param param;
738 struct login_result result;
740 debug_print("imap login - begin\n");
742 param.imap = get_imap(folder);
744 param.password = password;
746 if (folder && folder->account)
747 param.server = folder->account->recv_server;
751 threaded_run(folder, ¶m, &result, login_run);
753 debug_print("imap login - end\n");
759 struct status_param {
762 struct mailimap_status_att_list * status_att_list;
765 struct status_result {
767 struct mailimap_mailbox_data_status * data_status;
770 static void status_run(struct etpan_thread_op * op)
772 struct status_param * param;
773 struct status_result * result;
781 r = mailimap_status(param->imap, param->mb,
782 param->status_att_list,
783 &result->data_status);
786 debug_print("imap status run - end %i\n", r);
789 int imap_threaded_status(Folder * folder, const char * mb,
790 struct mailimap_mailbox_data_status ** data_status,
793 struct status_param param;
794 struct status_result result;
795 struct mailimap_status_att_list * status_att_list;
797 debug_print("imap status - begin\n");
799 status_att_list = mailimap_status_att_list_new_empty();
801 mailimap_status_att_list_add(status_att_list,
802 MAILIMAP_STATUS_ATT_MESSAGES);
805 mailimap_status_att_list_add(status_att_list,
806 MAILIMAP_STATUS_ATT_RECENT);
809 mailimap_status_att_list_add(status_att_list,
810 MAILIMAP_STATUS_ATT_UIDNEXT);
813 mailimap_status_att_list_add(status_att_list,
814 MAILIMAP_STATUS_ATT_UIDVALIDITY);
817 mailimap_status_att_list_add(status_att_list,
818 MAILIMAP_STATUS_ATT_UNSEEN);
820 param.imap = get_imap(folder);
822 param.status_att_list = status_att_list;
824 threaded_run(folder, ¶m, &result, status_run);
826 debug_print("imap status - end\n");
828 * data_status = result.data_status;
830 mailimap_status_att_list_free(status_att_list);
845 static void noop_run(struct etpan_thread_op * op)
847 struct noop_param * param;
848 struct noop_result * result;
856 r = mailimap_noop(param->imap);
859 debug_print("imap noop run - end %i\n", r);
862 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
864 struct noop_param param;
865 struct noop_result result;
868 debug_print("imap noop - begin\n");
870 imap = get_imap(folder);
873 threaded_run(folder, ¶m, &result, noop_run);
875 if (imap->imap_selection_info != NULL) {
876 * p_exists = imap->imap_selection_info->sel_exists;
882 debug_print("imap noop - end\n");
888 struct starttls_result {
892 static void starttls_run(struct etpan_thread_op * op)
894 struct connect_param * param;
895 struct starttls_result * result;
903 r = mailimap_starttls(param->imap);
906 debug_print("imap starttls run - end %i\n", r);
909 mailimap *imap = param->imap;
910 mailstream_low *plain_low = NULL;
911 mailstream_low *tls_low = NULL;
914 plain_low = mailstream_get_low(imap->imap_stream);
915 fd = mailstream_low_get_fd(plain_low);
917 debug_print("imap starttls run - can't get fd\n");
918 result->error = MAILIMAP_ERROR_STREAM;
922 tls_low = mailstream_low_tls_open(fd);
923 if (tls_low == NULL) {
924 debug_print("imap starttls run - can't tls_open\n");
925 result->error = MAILIMAP_ERROR_STREAM;
928 mailstream_low_free(plain_low);
929 mailstream_set_low(imap->imap_stream, tls_low);
933 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
935 struct connect_param param;
936 struct starttls_result result;
938 unsigned char *certificate;
940 debug_print("imap starttls - begin\n");
942 param.imap = get_imap(folder);
946 threaded_run(folder, ¶m, &result, starttls_run);
948 debug_print("imap starttls - end\n");
950 if (result.error == 0 && !etpan_skip_ssl_cert_check) {
951 cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
952 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
953 result.error = MAILIMAP_ERROR_STREAM;
962 struct create_param {
967 struct create_result {
971 static void create_run(struct etpan_thread_op * op)
973 struct create_param * param;
974 struct create_result * result;
982 r = mailimap_create(param->imap, param->mb);
985 debug_print("imap create run - end %i\n", r);
988 int imap_threaded_create(Folder * folder, const char * mb)
990 struct create_param param;
991 struct create_result result;
993 debug_print("imap create - begin\n");
995 param.imap = get_imap(folder);
998 threaded_run(folder, ¶m, &result, create_run);
1000 debug_print("imap create - end\n");
1002 return result.error;
1008 struct rename_param {
1011 const char * new_name;
1014 struct rename_result {
1018 static void rename_run(struct etpan_thread_op * op)
1020 struct rename_param * param;
1021 struct rename_result * result;
1025 result = op->result;
1029 r = mailimap_rename(param->imap, param->mb, param->new_name);
1032 debug_print("imap rename run - end %i\n", r);
1035 int imap_threaded_rename(Folder * folder,
1036 const char * mb, const char * new_name)
1038 struct rename_param param;
1039 struct rename_result result;
1041 debug_print("imap rename - begin\n");
1043 param.imap = get_imap(folder);
1045 param.new_name = new_name;
1047 threaded_run(folder, ¶m, &result, rename_run);
1049 debug_print("imap rename - end\n");
1051 return result.error;
1057 struct delete_param {
1062 struct delete_result {
1066 static void delete_run(struct etpan_thread_op * op)
1068 struct delete_param * param;
1069 struct delete_result * result;
1073 result = op->result;
1077 r = mailimap_delete(param->imap, param->mb);
1080 debug_print("imap delete run - end %i\n", r);
1083 int imap_threaded_delete(Folder * folder, const char * mb)
1085 struct delete_param param;
1086 struct delete_result result;
1088 debug_print("imap delete - begin\n");
1090 param.imap = get_imap(folder);
1093 threaded_run(folder, ¶m, &result, delete_run);
1095 debug_print("imap delete - end\n");
1097 return result.error;
1102 struct select_param {
1107 struct select_result {
1111 static void select_run(struct etpan_thread_op * op)
1113 struct select_param * param;
1114 struct select_result * result;
1118 result = op->result;
1122 r = mailimap_select(param->imap, param->mb);
1125 debug_print("imap select run - end %i\n", r);
1128 int imap_threaded_select(Folder * folder, const char * mb,
1129 gint * exists, gint * recent, gint * unseen,
1130 guint32 * uid_validity)
1132 struct select_param param;
1133 struct select_result result;
1136 debug_print("imap select - begin\n");
1138 imap = get_imap(folder);
1142 threaded_run(folder, ¶m, &result, select_run);
1144 if (result.error != MAILIMAP_NO_ERROR)
1145 return result.error;
1147 if (imap->imap_selection_info == NULL)
1148 return MAILIMAP_ERROR_PARSE;
1150 * exists = imap->imap_selection_info->sel_exists;
1151 * recent = imap->imap_selection_info->sel_recent;
1152 * unseen = imap->imap_selection_info->sel_unseen;
1153 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1155 debug_print("imap select - end\n");
1157 return result.error;
1162 struct examine_param {
1167 struct examine_result {
1171 static void examine_run(struct etpan_thread_op * op)
1173 struct examine_param * param;
1174 struct examine_result * result;
1178 result = op->result;
1182 r = mailimap_examine(param->imap, param->mb);
1185 debug_print("imap examine run - end %i\n", r);
1188 int imap_threaded_examine(Folder * folder, const char * mb,
1189 gint * exists, gint * recent, gint * unseen,
1190 guint32 * uid_validity)
1192 struct examine_param param;
1193 struct examine_result result;
1196 debug_print("imap examine - begin\n");
1198 imap = get_imap(folder);
1202 threaded_run(folder, ¶m, &result, examine_run);
1204 if (result.error != MAILIMAP_NO_ERROR)
1205 return result.error;
1207 if (imap->imap_selection_info == NULL)
1208 return MAILIMAP_ERROR_PARSE;
1210 * exists = imap->imap_selection_info->sel_exists;
1211 * recent = imap->imap_selection_info->sel_recent;
1212 * unseen = imap->imap_selection_info->sel_unseen;
1213 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1215 debug_print("imap examine - end\n");
1217 return result.error;
1223 struct search_param {
1226 struct mailimap_set * set;
1229 struct search_result {
1231 clist * search_result;
1234 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1236 return mailimap_set_item_new(orig->set_first, orig->set_last);
1239 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1241 clist *list = orig ? orig->set_list : NULL;
1242 clist *newlist = clist_new();
1247 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1248 clist_append(newlist,
1249 sc_mailimap_set_item_copy(
1250 (struct mailimap_set_item *)clist_content(cur)));
1251 return mailimap_set_new(newlist);
1254 static void search_run(struct etpan_thread_op * op)
1256 struct search_param * param;
1257 struct search_result * result;
1259 struct mailimap_search_key * key = NULL;
1260 struct mailimap_search_key * uid_key = NULL;
1261 struct mailimap_search_key * search_type_key;
1262 clist * search_result;
1265 result = op->result;
1269 /* we copy the mailimap_set because freeing the key is recursive */
1270 if (param->set != NULL) {
1271 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1272 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1273 uid_key = mailimap_search_key_new_all();
1275 search_type_key = NULL;
1276 switch (param->type) {
1277 case IMAP_SEARCH_TYPE_SIMPLE:
1278 search_type_key = NULL;
1281 case IMAP_SEARCH_TYPE_SEEN:
1282 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1283 NULL, NULL, NULL, NULL, NULL,
1284 NULL, NULL, NULL, NULL, NULL,
1285 NULL, NULL, NULL, NULL, 0,
1286 NULL, NULL, NULL, NULL, NULL,
1287 NULL, 0, NULL, NULL, NULL);
1290 case IMAP_SEARCH_TYPE_UNSEEN:
1291 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1292 NULL, NULL, NULL, NULL, NULL,
1293 NULL, NULL, NULL, NULL, NULL,
1294 NULL, NULL, NULL, NULL, 0,
1295 NULL, NULL, NULL, NULL, NULL,
1296 NULL, 0, NULL, NULL, NULL);
1299 case IMAP_SEARCH_TYPE_ANSWERED:
1300 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1301 NULL, NULL, NULL, NULL, NULL,
1302 NULL, NULL, NULL, NULL, NULL,
1303 NULL, NULL, NULL, NULL, 0,
1304 NULL, NULL, NULL, NULL, NULL,
1305 NULL, 0, NULL, NULL, NULL);
1308 case IMAP_SEARCH_TYPE_FLAGGED:
1309 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1310 NULL, NULL, NULL, NULL, NULL,
1311 NULL, NULL, NULL, NULL, NULL,
1312 NULL, NULL, NULL, NULL, 0,
1313 NULL, NULL, NULL, NULL, NULL,
1314 NULL, 0, NULL, NULL, NULL);
1316 case IMAP_SEARCH_TYPE_DELETED:
1317 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1318 NULL, NULL, NULL, NULL, NULL,
1319 NULL, NULL, NULL, NULL, NULL,
1320 NULL, NULL, NULL, NULL, 0,
1321 NULL, NULL, NULL, NULL, NULL,
1322 NULL, 0, NULL, NULL, NULL);
1326 if (search_type_key != NULL) {
1327 if (param->set != NULL) {
1328 key = mailimap_search_key_new_multiple_empty();
1329 mailimap_search_key_multiple_add(key, search_type_key);
1330 mailimap_search_key_multiple_add(key, uid_key);
1332 key = search_type_key;
1334 } else if (uid_key != NULL) {
1339 g_warning("no key!");
1340 result = op->result;
1342 result->search_result = NULL;
1344 mailstream_logger = imap_logger_uid;
1346 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1348 mailstream_logger = imap_logger_cmd;
1350 /* free the key (with the imapset) */
1351 mailimap_search_key_free(key);
1354 result->search_result = search_result;
1356 debug_print("imap search run - end %i\n", result->error);
1359 int imap_threaded_search(Folder * folder, int search_type,
1360 struct mailimap_set * set, clist ** search_result)
1362 struct search_param param;
1363 struct search_result result;
1366 debug_print("imap search - begin\n");
1368 imap = get_imap(folder);
1371 param.type = search_type;
1373 threaded_run(folder, ¶m, &result, search_run);
1375 if (result.error != MAILIMAP_NO_ERROR)
1376 return result.error;
1378 debug_print("imap search - end\n");
1380 * search_result = result.search_result;
1382 return result.error;
1389 uid_list_to_env_list(clist * fetch_result, carray ** result)
1397 tab = carray_new(128);
1399 res = MAILIMAP_ERROR_MEMORY;
1403 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1404 cur = clist_next(cur)) {
1405 struct mailimap_msg_att * msg_att;
1406 clistiter * item_cur;
1411 msg_att = clist_content(cur);
1415 for(item_cur = clist_begin(msg_att->att_list) ;
1417 item_cur = clist_next(item_cur)) {
1418 struct mailimap_msg_att_item * item;
1420 item = clist_content(item_cur);
1422 switch (item->att_type) {
1423 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1424 switch (item->att_data.att_static->att_type) {
1425 case MAILIMAP_MSG_ATT_UID:
1426 uid = item->att_data.att_static->att_data.att_uid;
1432 puid = malloc(sizeof(* puid));
1434 res = MAILIMAP_ERROR_MEMORY;
1439 r = carray_add(tab, puid, NULL);
1442 res = MAILIMAP_ERROR_MEMORY;
1449 return MAILIMAP_NO_ERROR;
1452 for(i = 0 ; i < carray_count(tab) ; i++)
1453 mailmessage_free(carray_get(tab, i));
1458 static int imap_get_messages_list(mailimap * imap,
1459 uint32_t first_index,
1464 struct mailimap_fetch_att * fetch_att;
1465 struct mailimap_fetch_type * fetch_type;
1466 struct mailimap_set * set;
1467 clist * fetch_result;
1470 set = mailimap_set_new_interval(first_index, 0);
1472 res = MAILIMAP_ERROR_MEMORY;
1476 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1477 if (fetch_type == NULL) {
1478 res = MAILIMAP_ERROR_MEMORY;
1482 fetch_att = mailimap_fetch_att_new_uid();
1483 if (fetch_att == NULL) {
1484 res = MAILIMAP_ERROR_MEMORY;
1485 goto free_fetch_type;
1488 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1489 if (r != MAILIMAP_NO_ERROR) {
1490 mailimap_fetch_att_free(fetch_att);
1491 res = MAILIMAP_ERROR_MEMORY;
1492 goto free_fetch_type;
1495 r = mailimap_uid_fetch(imap, set,
1496 fetch_type, &fetch_result);
1498 mailimap_fetch_type_free(fetch_type);
1499 mailimap_set_free(set);
1501 if (r != MAILIMAP_NO_ERROR) {
1507 r = uid_list_to_env_list(fetch_result, &env_list);
1508 mailimap_fetch_list_free(fetch_result);
1510 * result = env_list;
1512 return MAILIMAP_NO_ERROR;
1515 mailimap_fetch_type_free(fetch_type);
1517 mailimap_set_free(set);
1525 struct fetch_uid_param {
1527 uint32_t first_index;
1530 struct fetch_uid_result {
1532 carray * fetch_result;
1535 static void fetch_uid_run(struct etpan_thread_op * op)
1537 struct fetch_uid_param * param;
1538 struct fetch_uid_result * result;
1539 carray * fetch_result;
1543 result = op->result;
1547 fetch_result = NULL;
1548 r = imap_get_messages_list(param->imap, param->first_index,
1552 result->fetch_result = fetch_result;
1553 debug_print("imap fetch_uid run - end %i\n", r);
1556 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1557 carray ** fetch_result)
1559 struct fetch_uid_param param;
1560 struct fetch_uid_result result;
1563 debug_print("imap fetch_uid - begin\n");
1565 imap = get_imap(folder);
1567 param.first_index = first_index;
1569 threaded_run(folder, ¶m, &result, fetch_uid_run);
1571 if (result.error != MAILIMAP_NO_ERROR)
1572 return result.error;
1574 debug_print("imap fetch_uid - end\n");
1576 * fetch_result = result.fetch_result;
1578 return result.error;
1582 void imap_fetch_uid_list_free(carray * uid_list)
1586 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1589 puid = carray_get(uid_list, i);
1592 carray_free(uid_list);
1597 static int imap_fetch(mailimap * imap,
1600 size_t * result_len)
1603 struct mailimap_set * set;
1604 struct mailimap_fetch_att * fetch_att;
1605 struct mailimap_fetch_type * fetch_type;
1606 clist * fetch_result;
1607 struct mailimap_msg_att * msg_att;
1608 struct mailimap_msg_att_item * msg_att_item;
1613 struct mailimap_section * section;
1615 set = mailimap_set_new_single(msg_index);
1617 res = MAILIMAP_ERROR_MEMORY;
1621 section = mailimap_section_new(NULL);
1622 if (section == NULL) {
1623 res = MAILIMAP_ERROR_MEMORY;
1627 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1628 if (fetch_att == NULL) {
1629 mailimap_section_free(section);
1630 res = MAILIMAP_ERROR_MEMORY;
1634 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1635 if (fetch_type == NULL) {
1636 res = MAILIMAP_ERROR_MEMORY;
1637 goto free_fetch_att;
1640 mailstream_logger = imap_logger_fetch;
1642 r = mailimap_uid_fetch(imap, set,
1643 fetch_type, &fetch_result);
1645 mailstream_logger = imap_logger_cmd;
1647 mailimap_fetch_type_free(fetch_type);
1648 mailimap_set_free(set);
1651 case MAILIMAP_NO_ERROR:
1657 if (clist_begin(fetch_result) == NULL) {
1658 mailimap_fetch_list_free(fetch_result);
1659 return MAILIMAP_ERROR_FETCH;
1662 msg_att = clist_begin(fetch_result)->data;
1667 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1668 cur = clist_next(cur)) {
1669 msg_att_item = clist_content(cur);
1671 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1672 if (msg_att_item->att_data.att_static->att_type ==
1673 MAILIMAP_MSG_ATT_BODY_SECTION) {
1674 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1676 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1678 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1683 mailimap_fetch_list_free(fetch_result);
1686 return MAILIMAP_ERROR_FETCH;
1689 * result_len = text_length;
1691 return MAILIMAP_NO_ERROR;
1694 mailimap_fetch_att_free(fetch_att);
1696 mailimap_set_free(set);
1701 static int imap_fetch_header(mailimap * imap,
1704 size_t * result_len)
1707 struct mailimap_set * set;
1708 struct mailimap_fetch_att * fetch_att;
1709 struct mailimap_fetch_type * fetch_type;
1710 clist * fetch_result;
1711 struct mailimap_msg_att * msg_att;
1712 struct mailimap_msg_att_item * msg_att_item;
1717 struct mailimap_section * section;
1719 set = mailimap_set_new_single(msg_index);
1721 res = MAILIMAP_ERROR_MEMORY;
1725 section = mailimap_section_new_header();
1726 if (section == NULL) {
1727 res = MAILIMAP_ERROR_MEMORY;
1731 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1732 if (fetch_att == NULL) {
1733 mailimap_section_free(section);
1734 res = MAILIMAP_ERROR_MEMORY;
1738 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1739 if (fetch_type == NULL) {
1740 res = MAILIMAP_ERROR_MEMORY;
1741 goto free_fetch_att;
1744 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1746 mailimap_fetch_type_free(fetch_type);
1747 mailimap_set_free(set);
1750 case MAILIMAP_NO_ERROR:
1756 if (clist_begin(fetch_result) == NULL) {
1757 mailimap_fetch_list_free(fetch_result);
1758 return MAILIMAP_ERROR_FETCH;
1761 msg_att = clist_begin(fetch_result)->data;
1766 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1767 cur = clist_next(cur)) {
1768 msg_att_item = clist_content(cur);
1770 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1771 if (msg_att_item->att_data.att_static->att_type ==
1772 MAILIMAP_MSG_ATT_BODY_SECTION) {
1773 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1774 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1776 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1781 mailimap_fetch_list_free(fetch_result);
1784 return MAILIMAP_ERROR_FETCH;
1787 * result_len = text_length;
1789 return MAILIMAP_NO_ERROR;
1792 mailimap_fetch_att_free(fetch_att);
1794 mailimap_set_free(set);
1801 struct fetch_content_param {
1804 const char * filename;
1808 struct fetch_content_result {
1812 static void fetch_content_run(struct etpan_thread_op * op)
1814 struct fetch_content_param * param;
1815 struct fetch_content_result * result;
1817 size_t content_size;
1823 result = op->result;
1829 if (param->with_body)
1830 r = imap_fetch(param->imap, param->msg_index,
1831 &content, &content_size);
1833 r = imap_fetch_header(param->imap, param->msg_index,
1834 &content, &content_size);
1838 if (r == MAILIMAP_NO_ERROR) {
1839 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1841 result->error = MAILIMAP_ERROR_FETCH;
1845 f = fdopen(fd, "wb");
1847 result->error = MAILIMAP_ERROR_FETCH;
1851 r = fwrite(content, 1, content_size, f);
1858 g_unlink(param->filename);
1869 g_unlink(param->filename);
1872 if (mmap_string_unref(content) != 0)
1876 debug_print("imap fetch_content run - end %i\n", r);
1879 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1881 const char * filename)
1883 struct fetch_content_param param;
1884 struct fetch_content_result result;
1887 debug_print("imap fetch_content - begin\n");
1889 imap = get_imap(folder);
1891 param.msg_index = msg_index;
1892 param.filename = filename;
1893 param.with_body = with_body;
1895 threaded_run(folder, ¶m, &result, fetch_content_run);
1897 if (result.error != MAILIMAP_NO_ERROR)
1898 return result.error;
1900 debug_print("imap fetch_content - end\n");
1902 return result.error;
1907 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1915 flag_list = att_dyn->att_list;
1916 if (flag_list == NULL)
1919 for(cur = clist_begin(flag_list) ; cur != NULL ;
1920 cur = clist_next(cur)) {
1921 struct mailimap_flag_fetch * flag_fetch;
1923 flag_fetch = clist_content(cur);
1924 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1927 switch (flag_fetch->fl_flag->fl_type) {
1928 case MAILIMAP_FLAG_ANSWERED:
1929 flags |= MSG_REPLIED;
1931 case MAILIMAP_FLAG_FLAGGED:
1932 flags |= MSG_MARKED;
1934 case MAILIMAP_FLAG_DELETED:
1935 flags |= MSG_DELETED;
1937 case MAILIMAP_FLAG_SEEN:
1938 flags &= ~MSG_UNREAD;
1948 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1952 struct mailimap_msg_att_dynamic ** patt_dyn)
1954 clistiter * item_cur;
1958 struct mailimap_msg_att_dynamic * att_dyn;
1965 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1966 item_cur = clist_next(item_cur)) {
1967 struct mailimap_msg_att_item * item;
1969 item = clist_content(item_cur);
1971 switch (item->att_type) {
1972 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1973 switch (item->att_data.att_static->att_type) {
1974 case MAILIMAP_MSG_ATT_UID:
1975 uid = item->att_data.att_static->att_data.att_uid;
1978 case MAILIMAP_MSG_ATT_BODY_SECTION:
1979 if (headers == NULL) {
1980 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1983 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1984 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1989 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1990 if (att_dyn == NULL) {
1991 att_dyn = item->att_data.att_dyn;
1999 if (pheaders != NULL)
2000 * pheaders = headers;
2001 if (pref_size != NULL)
2002 * pref_size = ref_size;
2003 if (patt_dyn != NULL)
2004 * patt_dyn = att_dyn;
2006 return MAIL_NO_ERROR;
2009 static struct imap_fetch_env_info *
2010 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2012 struct imap_fetch_env_info * info;
2016 struct mailimap_msg_att_dynamic * att_dyn;
2018 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2023 info = malloc(sizeof(* info));
2025 info->headers = strdup(headers);
2027 info->flags = imap_flags_to_flags(att_dyn);
2033 imap_fetch_result_to_envelop_list(clist * fetch_result,
2034 carray ** p_env_list)
2041 env_list = carray_new(16);
2043 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2044 cur = clist_next(cur)) {
2045 struct mailimap_msg_att * msg_att;
2046 struct imap_fetch_env_info * env_info;
2048 msg_att = clist_content(cur);
2050 env_info = fetch_to_env_info(msg_att);
2052 return MAILIMAP_ERROR_MEMORY;
2053 carray_add(env_list, env_info, NULL);
2056 * p_env_list = env_list;
2058 return MAIL_NO_ERROR;
2061 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2063 struct mailimap_fetch_att * fetch_att;
2067 struct mailimap_header_list * imap_hdrlist;
2068 struct mailimap_section * section;
2070 hdrlist = clist_new();
2072 header = strdup("Date");
2073 r = clist_append(hdrlist, header);
2074 header = strdup("From");
2075 r = clist_append(hdrlist, header);
2076 header = strdup("To");
2077 r = clist_append(hdrlist, header);
2078 header = strdup("Cc");
2079 r = clist_append(hdrlist, header);
2080 header = strdup("Subject");
2081 r = clist_append(hdrlist, header);
2082 header = strdup("Message-ID");
2083 r = clist_append(hdrlist, header);
2084 header = strdup("References");
2085 r = clist_append(hdrlist, header);
2086 header = strdup("In-Reply-To");
2087 r = clist_append(hdrlist, header);
2089 imap_hdrlist = mailimap_header_list_new(hdrlist);
2090 section = mailimap_section_new_header_fields(imap_hdrlist);
2091 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2092 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2094 return MAIL_NO_ERROR;
2097 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2099 struct mailimap_fetch_att * fetch_att;
2100 struct mailimap_section * section;
2102 section = mailimap_section_new_header();
2103 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2104 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2106 return MAIL_NO_ERROR;
2110 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2111 carray ** p_env_list)
2113 struct mailimap_fetch_att * fetch_att;
2114 struct mailimap_fetch_type * fetch_type;
2116 clist * fetch_result;
2118 carray * env_list = NULL;
2122 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2125 fetch_att = mailimap_fetch_att_new_uid();
2126 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2129 fetch_att = mailimap_fetch_att_new_flags();
2130 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2133 fetch_att = mailimap_fetch_att_new_rfc822_size();
2134 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2138 key.len = sizeof(imap);
2139 r = chash_get(courier_workaround_hash, &key, &value);
2141 r = imap_add_envelope_fetch_att(fetch_type);
2143 r = imap_add_header_fetch_att(fetch_type);
2145 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2148 case MAILIMAP_NO_ERROR:
2151 mailimap_fetch_type_free(fetch_type);
2152 debug_print("uid_fetch: %d\n", r);
2156 if (clist_begin(fetch_result) == NULL) {
2157 res = MAILIMAP_ERROR_FETCH;
2158 debug_print("clist_begin = NULL\n");
2162 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2163 mailimap_fetch_list_free(fetch_result);
2165 if (r != MAILIMAP_NO_ERROR) {
2166 mailimap_fetch_type_free(fetch_type);
2167 res = MAILIMAP_ERROR_MEMORY;
2168 debug_print("fetch_result_to_envelop_list: %d\n", res);
2172 mailimap_fetch_type_free(fetch_type);
2174 * p_env_list = env_list;
2176 return MAILIMAP_NO_ERROR;
2182 struct fetch_env_param {
2184 struct mailimap_set * set;
2187 struct fetch_env_result {
2188 carray * fetch_env_result;
2192 static void fetch_env_run(struct etpan_thread_op * op)
2194 struct fetch_env_param * param;
2195 struct fetch_env_result * result;
2200 result = op->result;
2205 r = imap_get_envelopes_list(param->imap, param->set,
2209 result->fetch_env_result = env_list;
2211 debug_print("imap fetch_env run - end %i\n", r);
2214 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2215 carray ** p_env_list)
2217 struct fetch_env_param param;
2218 struct fetch_env_result result;
2221 debug_print("imap fetch_env - begin\n");
2223 imap = get_imap(folder);
2227 threaded_run(folder, ¶m, &result, fetch_env_run);
2229 if (result.error != MAILIMAP_NO_ERROR) {
2235 key.len = sizeof(imap);
2236 r = chash_get(courier_workaround_hash, &key, &value);
2240 chash_set(courier_workaround_hash, &key, &value, NULL);
2242 threaded_run(folder, ¶m, &result, fetch_env_run);
2246 if (result.error != MAILIMAP_NO_ERROR)
2247 return result.error;
2249 debug_print("imap fetch_env - end\n");
2251 * p_env_list = result.fetch_env_result;
2253 return result.error;
2256 void imap_fetch_env_free(carray * env_list)
2260 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2261 struct imap_fetch_env_info * env_info;
2263 env_info = carray_get(env_list, i);
2264 free(env_info->headers);
2267 carray_free(env_list);
2274 struct append_param {
2276 const char * mailbox;
2277 const char * filename;
2278 struct mailimap_flag_list * flag_list;
2281 struct append_result {
2286 static void append_run(struct etpan_thread_op * op)
2288 struct append_param * param;
2289 struct append_result * result;
2293 struct stat stat_buf;
2295 guint32 uid = 0, val = 0;
2298 result = op->result;
2302 r = stat(param->filename, &stat_buf);
2304 result->error = MAILIMAP_ERROR_APPEND;
2307 size = stat_buf.st_size;
2309 fd = open(param->filename, O_RDONLY);
2311 result->error = MAILIMAP_ERROR_APPEND;
2315 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2316 if (data == (void *) MAP_FAILED) {
2318 result->error = MAILIMAP_ERROR_APPEND;
2322 mailstream_logger = imap_logger_append;
2324 r = mailimap_uidplus_append(param->imap, param->mailbox,
2325 param->flag_list, NULL,
2326 data, size, &val, &uid);
2328 mailstream_logger = imap_logger_cmd;
2335 debug_print("imap append run - end %i uid %d\n", r, uid);
2338 int imap_threaded_append(Folder * folder, const char * mailbox,
2339 const char * filename,
2340 struct mailimap_flag_list * flag_list,
2343 struct append_param param;
2344 struct append_result result;
2347 debug_print("imap append - begin\n");
2349 imap = get_imap(folder);
2351 param.mailbox = mailbox;
2352 param.filename = filename;
2353 param.flag_list = flag_list;
2355 threaded_run(folder, ¶m, &result, append_run);
2357 if (result.error != MAILIMAP_NO_ERROR)
2358 return result.error;
2360 debug_print("imap append - end\n");
2364 return result.error;
2370 struct expunge_param {
2374 struct expunge_result {
2378 static void expunge_run(struct etpan_thread_op * op)
2380 struct expunge_param * param;
2381 struct expunge_result * result;
2385 result = op->result;
2389 r = mailimap_expunge(param->imap);
2392 debug_print("imap expunge run - end %i\n", r);
2395 int imap_threaded_expunge(Folder * folder)
2397 struct expunge_param param;
2398 struct expunge_result result;
2400 debug_print("imap expunge - begin\n");
2402 param.imap = get_imap(folder);
2404 threaded_run(folder, ¶m, &result, expunge_run);
2406 debug_print("imap expunge - end\n");
2408 return result.error;
2414 struct mailimap_set * set;
2418 struct copy_result {
2420 struct mailimap_set *source;
2421 struct mailimap_set *dest;
2424 static void copy_run(struct etpan_thread_op * op)
2426 struct copy_param * param;
2427 struct copy_result * result;
2430 struct mailimap_set *source = NULL, *dest = NULL;
2433 result = op->result;
2437 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2438 &val, &source, &dest);
2442 result->source = source;
2443 result->dest = dest;
2445 result->source = NULL;
2446 result->dest = NULL;
2448 debug_print("imap copy run - end %i\n", r);
2451 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2452 const char * mb, struct mailimap_set **source,
2453 struct mailimap_set **dest)
2455 struct copy_param param;
2456 struct copy_result result;
2459 debug_print("imap copy - begin\n");
2461 imap = get_imap(folder);
2466 threaded_run(folder, ¶m, &result, copy_run);
2470 if (result.error != MAILIMAP_NO_ERROR)
2471 return result.error;
2473 *source = result.source;
2474 *dest = result.dest;
2476 debug_print("imap copy - end\n");
2478 return result.error;
2483 struct store_param {
2485 struct mailimap_set * set;
2486 struct mailimap_store_att_flags * store_att_flags;
2489 struct store_result {
2493 static void store_run(struct etpan_thread_op * op)
2495 struct store_param * param;
2496 struct store_result * result;
2500 result = op->result;
2504 r = mailimap_uid_store(param->imap, param->set,
2505 param->store_att_flags);
2509 debug_print("imap store run - end %i\n", r);
2512 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2513 struct mailimap_store_att_flags * store_att_flags)
2515 struct store_param param;
2516 struct store_result result;
2519 debug_print("imap store - begin\n");
2521 imap = get_imap(folder);
2524 param.store_att_flags = store_att_flags;
2526 threaded_run(folder, ¶m, &result, store_run);
2528 if (result.error != MAILIMAP_NO_ERROR)
2529 return result.error;
2531 debug_print("imap store - end\n");
2533 return result.error;
2537 #define ENV_BUFFER_SIZE 512
2539 static void do_exec_command(int fd, const char * command,
2540 const char * servername, uint16_t port)
2544 char env_buffer[ENV_BUFFER_SIZE];
2548 /* Fork again to become a child of init rather than
2549 the etpan client. */
2555 snprintf(env_buffer, ENV_BUFFER_SIZE,
2556 "ETPANSERVER=%s", servername);
2558 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2562 setenv("ETPANSERVER", servername, 1);
2564 unsetenv("ETPANSERVER");
2569 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2571 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2577 snprintf(porttext, sizeof(porttext), "%d", port);
2578 setenv("ETPANPORT", porttext, 1);
2581 unsetenv("ETPANPORT");
2585 /* Not a lot we can do if there's an error other than bail. */
2586 if (dup2(fd, 0) == -1)
2588 if (dup2(fd, 1) == -1)
2591 /* Should we close stderr and reopen /dev/null? */
2593 maxopen = sysconf(_SC_OPEN_MAX);
2594 for (i=3; i < maxopen; i++)
2598 /* Detach from the controlling tty if we have one. Otherwise,
2599 SSH might do something stupid like trying to use it instead
2600 of running $SSH_ASKPASS. Doh. */
2601 fd = open("/dev/tty", O_RDONLY);
2603 ioctl(fd, TIOCNOTTY, NULL);
2606 #endif /* TIOCNOTTY */
2608 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2610 /* Eep. Shouldn't reach this */
2614 static int subcommand_connect(const char *command,
2615 const char *servername, uint16_t port)
2617 /* SEB unsupported on Windows */
2621 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2626 do_exec_command(sockfds[1], command, servername, port);
2628 else if (childpid == -1) {
2636 /* Reap child, leaving grandchild process to run */
2637 waitpid(childpid, NULL, 0);
2642 int socket_connect_cmd(mailimap * imap, const char * command,
2643 const char * server, int port)
2649 fd = subcommand_connect(command, server, port);
2651 return MAILIMAP_ERROR_STREAM;
2653 s = mailstream_socket_open(fd);
2656 return MAILIMAP_ERROR_STREAM;
2659 r = mailimap_connect(imap, s);
2660 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2661 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2662 mailstream_close(s);
2671 struct connect_cmd_param {
2673 const char * command;
2674 const char * server;
2678 struct connect_cmd_result {
2682 static void connect_cmd_run(struct etpan_thread_op * op)
2685 struct connect_cmd_param * param;
2686 struct connect_cmd_result * result;
2689 result = op->result;
2693 r = socket_connect_cmd(param->imap, param->command,
2694 param->server, param->port);
2700 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2701 const char * server, int port)
2703 struct connect_cmd_param param;
2704 struct connect_cmd_result result;
2709 imap = mailimap_new(0, NULL);
2712 key.len = sizeof(folder);
2715 chash_set(session_hash, &key, &value, NULL);
2718 param.command = command;
2719 param.server = server;
2722 threaded_run(folder, ¶m, &result, connect_cmd_run);
2724 debug_print("connect_cmd ok %i\n", result.error);
2726 return result.error;
2730 void imap_main_init(void)
2733 void imap_main_done(void)
2736 void imap_main_set_timeout(int sec)