7 #include "imap-thread.h"
11 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__CYGWIN__))
12 #include <sys/socket.h>
20 #include "etpan-thread-manager.h"
22 #include "ssl_certificate.h"
24 #define DISABLE_LOG_DURING_LOGIN
26 static struct etpan_thread_manager * thread_manager = NULL;
27 static chash * courier_workaround_hash = NULL;
28 static chash * imap_hash = NULL;
29 static chash * session_hash = NULL;
30 static guint thread_manager_signal = 0;
31 static GIOChannel * io_channel = NULL;
34 static gboolean thread_manager_event(GIOChannel * source,
35 GIOCondition condition,
38 etpan_thread_manager_loop(thread_manager);
43 void imap_logger_cmd(int direction, const char * str, size_t size)
50 memset(buf, 0, size+1);
51 strncpy(buf, str, size);
54 if (!strncmp(buf, "<<<<<<<", 7)
55 || !strncmp(buf, ">>>>>>>", 7)) {
59 while (strstr(buf, "\r"))
60 *strstr(buf, "\r") = ' ';
61 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
62 buf[strlen(buf)-1] = '\0';
64 lines = g_strsplit(buf, "\n", -1);
66 while (lines[i] && *lines[i]) {
67 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
74 void imap_logger_fetch(int direction, const char * str, size_t size)
81 memset(buf, 0, size+1);
82 strncpy(buf, str, size);
84 if (!strncmp(buf, "<<<<<<<", 7)
85 || !strncmp(buf, ">>>>>>>", 7)) {
89 while (strstr(buf, "\r"))
90 *strstr(buf, "\r") = ' ';
91 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
92 buf[strlen(buf)-1] = '\0';
94 lines = g_strsplit(buf, "\n", -1);
96 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
97 while (lines[i] && *lines[i]) {
98 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
102 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
108 void imap_logger_append(int direction, const char * str, size_t size)
114 buf = malloc(size+1);
115 memset(buf, 0, size+1);
116 strncpy(buf, str, size);
118 if (!strncmp(buf, "<<<<<<<", 7)
119 || !strncmp(buf, ">>>>>>>", 7)) {
123 while (strstr(buf, "\r"))
124 *strstr(buf, "\r") = ' ';
125 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
126 buf[strlen(buf)-1] = '\0';
128 lines = g_strsplit(buf, "\n", -1);
130 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
131 while (lines[i] && *lines[i]) {
132 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
136 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
142 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
143 static gboolean etpan_skip_ssl_cert_check = FALSE;
145 void imap_main_init(gboolean skip_ssl_cert_check)
147 int fd_thread_manager;
149 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
150 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
151 mailstream_network_delay.tv_usec = 0;
153 mailstream_debug = 1;
154 mailstream_logger = imap_logger_cmd;
156 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
157 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
158 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
160 thread_manager = etpan_thread_manager_new();
162 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
164 io_channel = g_io_channel_unix_new(fd_thread_manager);
166 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
167 thread_manager_event,
172 void imap_main_set_timeout(int sec)
174 mailstream_network_delay.tv_sec = sec;
175 mailstream_network_delay.tv_usec = 0;
178 void imap_main_done(void)
180 etpan_thread_manager_stop(thread_manager);
181 etpan_thread_manager_join(thread_manager);
183 g_source_remove(thread_manager_signal);
184 g_io_channel_unref(io_channel);
186 etpan_thread_manager_free(thread_manager);
188 chash_free(courier_workaround_hash);
189 chash_free(session_hash);
190 chash_free(imap_hash);
193 void imap_init(Folder * folder)
195 struct etpan_thread * thread;
199 thread = etpan_thread_manager_get_thread(thread_manager);
202 key.len = sizeof(folder);
206 chash_set(imap_hash, &key, &value, NULL);
209 void imap_done(Folder * folder)
211 struct etpan_thread * thread;
217 key.len = sizeof(folder);
219 r = chash_get(imap_hash, &key, &value);
225 etpan_thread_unbind(thread);
227 chash_delete(imap_hash, &key, NULL);
229 debug_print("remove thread");
232 static struct etpan_thread * get_thread(Folder * folder)
234 struct etpan_thread * thread;
239 key.len = sizeof(folder);
241 chash_get(imap_hash, &key, &value);
247 static mailimap * get_imap(Folder * folder)
255 key.len = sizeof(folder);
257 r = chash_get(session_hash, &key, &value);
267 static void generic_cb(int cancelled, void * result, void * callback_data)
271 p_finished = callback_data;
273 debug_print("generic_cb\n");
278 static void threaded_run(Folder * folder, void * param, void * result,
279 void (* func)(struct etpan_thread_op * ))
281 struct etpan_thread_op * op;
282 struct etpan_thread * thread;
285 imap_folder_ref(folder);
287 op = etpan_thread_op_new();
293 op->callback = generic_cb;
294 op->callback_data = &finished;
299 thread = get_thread(folder);
300 etpan_thread_op_schedule(thread, op);
303 gtk_main_iteration();
306 etpan_thread_op_free(op);
308 imap_folder_unref(folder);
314 struct connect_param {
320 struct connect_result {
324 static void connect_run(struct etpan_thread_op * op)
327 struct connect_param * param;
328 struct connect_result * result;
333 r = mailimap_socket_connect(param->imap,
334 param->server, param->port);
340 int imap_threaded_connect(Folder * folder, const char * server, int port)
342 struct connect_param param;
343 struct connect_result result;
348 imap = mailimap_new(0, NULL);
351 key.len = sizeof(folder);
354 chash_set(session_hash, &key, &value, NULL);
357 param.server = server;
360 threaded_run(folder, ¶m, &result, connect_run);
362 debug_print("connect ok %i\n", result.error);
367 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
370 struct connect_param *param = (struct connect_param *)data;
373 if (certificate == NULL || len < 0) {
374 g_warning("no cert presented.\n");
377 cert = d2i_X509(NULL, &certificate, len);
379 g_warning("can't get cert\n");
381 } else if (ssl_certificate_check(cert,
382 (gchar *)param->server, (gushort)param->port) == TRUE) {
394 static void connect_ssl_run(struct etpan_thread_op * op)
397 struct connect_param * param;
398 struct connect_result * result;
403 r = mailimap_ssl_connect(param->imap,
404 param->server, param->port);
408 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
410 struct connect_param param;
411 struct connect_result result;
415 unsigned char *certificate = NULL;
418 imap = mailimap_new(0, NULL);
421 key.len = sizeof(folder);
424 chash_set(session_hash, &key, &value, NULL);
427 param.server = server;
430 threaded_run(folder, ¶m, &result, connect_ssl_run);
432 if (result.error >= 0 && !etpan_skip_ssl_cert_check) {
433 cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
434 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
439 debug_print("connect %d\n", result.error);
450 struct mailimap_capability_data *caps;
453 static void capability_run(struct etpan_thread_op * op)
456 struct capa_param * param;
457 struct capa_result * result;
458 struct mailimap_capability_data *caps;
463 r = mailimap_capability(param->imap, &caps);
466 result->caps = (r == 0 ? caps : NULL);
470 struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
472 struct capa_param param;
473 struct capa_result result;
476 imap = get_imap(folder);
480 threaded_run(folder, ¶m, &result, capability_run);
482 debug_print("capa ok\n");
488 struct disconnect_param {
492 struct disconnect_result {
496 static void disconnect_run(struct etpan_thread_op * op)
499 struct disconnect_param * param;
500 struct disconnect_result * result;
505 r = mailimap_logout(param->imap);
510 void imap_threaded_disconnect(Folder * folder)
512 struct connect_param param;
513 struct connect_result result;
518 imap = get_imap(folder);
520 debug_print("was disconnected\n");
526 threaded_run(folder, ¶m, &result, disconnect_run);
529 key.len = sizeof(folder);
532 chash_delete(session_hash, &key, NULL);
535 key.len = sizeof(imap);
536 chash_delete(courier_workaround_hash, &key, NULL);
540 debug_print("disconnect ok\n");
547 const char * wildcard;
555 static void list_run(struct etpan_thread_op * op)
557 struct list_param * param;
558 struct list_result * result;
564 r = mailimap_list(param->imap, param->base,
565 param->wildcard, &list);
570 debug_print("imap list run - end\n");
573 int imap_threaded_list(Folder * folder, const char * base,
574 const char * wildcard,
577 struct list_param param;
578 struct list_result result;
580 debug_print("imap list - begin\n");
582 param.imap = get_imap(folder);
584 param.wildcard = wildcard;
586 threaded_run(folder, ¶m, &result, list_run);
588 * p_result = result.list;
590 debug_print("imap list - end %p\n", result.list);
600 const char * password;
604 struct login_result {
608 static void login_run(struct etpan_thread_op * op)
610 struct login_param * param;
611 struct login_result * result;
613 #ifdef DISABLE_LOG_DURING_LOGIN
619 #ifdef DISABLE_LOG_DURING_LOGIN
620 old_debug = mailstream_debug;
621 mailstream_debug = 0;
623 if (!strcmp(param->type, "LOGIN"))
624 r = mailimap_login(param->imap,
625 param->login, param->password);
627 r = mailimap_authenticate(param->imap,
628 param->type, NULL, NULL, NULL,
629 param->login, param->login,
630 param->password, NULL);
631 #ifdef DISABLE_LOG_DURING_LOGIN
632 mailstream_debug = old_debug;
637 debug_print("imap login run - end %i\n", r);
640 int imap_threaded_login(Folder * folder,
641 const char * login, const char * password,
644 struct login_param param;
645 struct login_result result;
647 debug_print("imap login - begin\n");
649 param.imap = get_imap(folder);
651 param.password = password;
654 threaded_run(folder, ¶m, &result, login_run);
656 debug_print("imap login - end\n");
662 struct status_param {
665 struct mailimap_status_att_list * status_att_list;
668 struct status_result {
670 struct mailimap_mailbox_data_status * data_status;
673 static void status_run(struct etpan_thread_op * op)
675 struct status_param * param;
676 struct status_result * result;
682 r = mailimap_status(param->imap, param->mb,
683 param->status_att_list,
684 &result->data_status);
687 debug_print("imap status run - end %i\n", r);
690 int imap_threaded_status(Folder * folder, const char * mb,
691 struct mailimap_mailbox_data_status ** data_status,
694 struct status_param param;
695 struct status_result result;
696 struct mailimap_status_att_list * status_att_list;
698 debug_print("imap status - begin\n");
700 status_att_list = mailimap_status_att_list_new_empty();
702 mailimap_status_att_list_add(status_att_list,
703 MAILIMAP_STATUS_ATT_MESSAGES);
706 mailimap_status_att_list_add(status_att_list,
707 MAILIMAP_STATUS_ATT_RECENT);
710 mailimap_status_att_list_add(status_att_list,
711 MAILIMAP_STATUS_ATT_UIDNEXT);
714 mailimap_status_att_list_add(status_att_list,
715 MAILIMAP_STATUS_ATT_UIDVALIDITY);
718 mailimap_status_att_list_add(status_att_list,
719 MAILIMAP_STATUS_ATT_UNSEEN);
721 param.imap = get_imap(folder);
723 param.status_att_list = status_att_list;
725 threaded_run(folder, ¶m, &result, status_run);
727 debug_print("imap status - end\n");
729 * data_status = result.data_status;
731 mailimap_status_att_list_free(status_att_list);
746 static void noop_run(struct etpan_thread_op * op)
748 struct noop_param * param;
749 struct noop_result * result;
753 r = mailimap_noop(param->imap);
757 debug_print("imap noop run - end %i\n", r);
760 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
762 struct noop_param param;
763 struct noop_result result;
766 debug_print("imap noop - begin\n");
768 imap = get_imap(folder);
771 threaded_run(folder, ¶m, &result, noop_run);
773 if (imap->imap_selection_info != NULL) {
774 * p_exists = imap->imap_selection_info->sel_exists;
780 debug_print("imap noop - end\n");
786 struct starttls_result {
790 static void starttls_run(struct etpan_thread_op * op)
792 struct connect_param * param;
793 struct starttls_result * result;
797 r = mailimap_starttls(param->imap);
801 debug_print("imap starttls run - end %i\n", r);
804 mailimap *imap = param->imap;
805 mailstream_low *plain_low = NULL;
806 mailstream_low *tls_low = NULL;
809 plain_low = mailstream_get_low(imap->imap_stream);
810 fd = mailstream_low_get_fd(plain_low);
812 debug_print("imap starttls run - can't get fd\n");
813 result->error = MAILIMAP_ERROR_STREAM;
817 tls_low = mailstream_low_tls_open(fd);
818 if (tls_low == NULL) {
819 debug_print("imap starttls run - can't tls_open\n");
820 result->error = MAILIMAP_ERROR_STREAM;
823 mailstream_low_free(plain_low);
824 mailstream_set_low(imap->imap_stream, tls_low);
828 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
830 struct connect_param param;
831 struct starttls_result result;
833 unsigned char *certificate;
835 debug_print("imap starttls - begin\n");
837 param.imap = get_imap(folder);
841 threaded_run(folder, ¶m, &result, starttls_run);
843 debug_print("imap starttls - end\n");
845 if (result.error == 0 && !etpan_skip_ssl_cert_check) {
846 cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
847 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
848 result.error = MAILIMAP_ERROR_STREAM;
857 struct create_param {
862 struct create_result {
866 static void create_run(struct etpan_thread_op * op)
868 struct create_param * param;
869 struct create_result * result;
873 r = mailimap_create(param->imap, param->mb);
877 debug_print("imap create run - end %i\n", r);
880 int imap_threaded_create(Folder * folder, const char * mb)
882 struct create_param param;
883 struct create_result result;
885 debug_print("imap create - begin\n");
887 param.imap = get_imap(folder);
890 threaded_run(folder, ¶m, &result, create_run);
892 debug_print("imap create - end\n");
900 struct rename_param {
903 const char * new_name;
906 struct rename_result {
910 static void rename_run(struct etpan_thread_op * op)
912 struct rename_param * param;
913 struct rename_result * result;
917 r = mailimap_rename(param->imap, param->mb, param->new_name);
921 debug_print("imap rename run - end %i\n", r);
924 int imap_threaded_rename(Folder * folder,
925 const char * mb, const char * new_name)
927 struct rename_param param;
928 struct rename_result result;
930 debug_print("imap rename - begin\n");
932 param.imap = get_imap(folder);
934 param.new_name = new_name;
936 threaded_run(folder, ¶m, &result, rename_run);
938 debug_print("imap rename - end\n");
946 struct delete_param {
951 struct delete_result {
955 static void delete_run(struct etpan_thread_op * op)
957 struct delete_param * param;
958 struct delete_result * result;
962 r = mailimap_delete(param->imap, param->mb);
966 debug_print("imap delete run - end %i\n", r);
969 int imap_threaded_delete(Folder * folder, const char * mb)
971 struct delete_param param;
972 struct delete_result result;
974 debug_print("imap delete - begin\n");
976 param.imap = get_imap(folder);
979 threaded_run(folder, ¶m, &result, delete_run);
981 debug_print("imap delete - end\n");
988 struct select_param {
993 struct select_result {
997 static void select_run(struct etpan_thread_op * op)
999 struct select_param * param;
1000 struct select_result * result;
1004 r = mailimap_select(param->imap, param->mb);
1006 result = op->result;
1008 debug_print("imap select run - end %i\n", r);
1011 int imap_threaded_select(Folder * folder, const char * mb,
1012 gint * exists, gint * recent, gint * unseen,
1013 guint32 * uid_validity)
1015 struct select_param param;
1016 struct select_result result;
1019 debug_print("imap select - begin\n");
1021 imap = get_imap(folder);
1025 threaded_run(folder, ¶m, &result, select_run);
1027 if (result.error != MAILIMAP_NO_ERROR)
1028 return result.error;
1030 if (imap->imap_selection_info == NULL)
1031 return MAILIMAP_ERROR_PARSE;
1033 * exists = imap->imap_selection_info->sel_exists;
1034 * recent = imap->imap_selection_info->sel_recent;
1035 * unseen = imap->imap_selection_info->sel_unseen;
1036 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1038 debug_print("imap select - end\n");
1040 return result.error;
1045 struct examine_param {
1050 struct examine_result {
1054 static void examine_run(struct etpan_thread_op * op)
1056 struct examine_param * param;
1057 struct examine_result * result;
1061 r = mailimap_examine(param->imap, param->mb);
1063 result = op->result;
1065 debug_print("imap examine run - end %i\n", r);
1068 int imap_threaded_examine(Folder * folder, const char * mb,
1069 gint * exists, gint * recent, gint * unseen,
1070 guint32 * uid_validity)
1072 struct examine_param param;
1073 struct examine_result result;
1076 debug_print("imap examine - begin\n");
1078 imap = get_imap(folder);
1082 threaded_run(folder, ¶m, &result, examine_run);
1084 if (result.error != MAILIMAP_NO_ERROR)
1085 return result.error;
1087 if (imap->imap_selection_info == NULL)
1088 return MAILIMAP_ERROR_PARSE;
1090 * exists = imap->imap_selection_info->sel_exists;
1091 * recent = imap->imap_selection_info->sel_recent;
1092 * unseen = imap->imap_selection_info->sel_unseen;
1093 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1095 debug_print("imap examine - end\n");
1097 return result.error;
1103 struct search_param {
1106 struct mailimap_set * set;
1109 struct search_result {
1111 clist * search_result;
1114 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1116 return mailimap_set_item_new(orig->set_first, orig->set_last);
1119 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1121 clist *list = orig ? orig->set_list : NULL;
1122 clist *newlist = clist_new();
1127 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1128 clist_append(newlist,
1129 sc_mailimap_set_item_copy(
1130 (struct mailimap_set_item *)clist_content(cur)));
1131 return mailimap_set_new(newlist);
1134 static void search_run(struct etpan_thread_op * op)
1136 struct search_param * param;
1137 struct search_result * result;
1139 struct mailimap_search_key * key = NULL;
1140 struct mailimap_search_key * uid_key = NULL;
1141 struct mailimap_search_key * search_type_key;
1142 clist * search_result;
1146 if (param->set == NULL && param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1147 g_warning("broken search");
1150 /* we copy the mailimap_set because freeing the key is recursive */
1151 if (param->set != NULL)
1152 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1154 search_type_key = NULL;
1155 switch (param->type) {
1156 case IMAP_SEARCH_TYPE_SIMPLE:
1157 search_type_key = NULL;
1160 case IMAP_SEARCH_TYPE_SEEN:
1161 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1162 NULL, NULL, NULL, NULL, NULL,
1163 NULL, NULL, NULL, NULL, NULL,
1164 NULL, NULL, NULL, NULL, 0,
1165 NULL, NULL, NULL, NULL, NULL,
1166 NULL, 0, NULL, NULL, NULL);
1169 case IMAP_SEARCH_TYPE_UNSEEN:
1170 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1171 NULL, NULL, NULL, NULL, NULL,
1172 NULL, NULL, NULL, NULL, NULL,
1173 NULL, NULL, NULL, NULL, 0,
1174 NULL, NULL, NULL, NULL, NULL,
1175 NULL, 0, NULL, NULL, NULL);
1178 case IMAP_SEARCH_TYPE_ANSWERED:
1179 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1180 NULL, NULL, NULL, NULL, NULL,
1181 NULL, NULL, NULL, NULL, NULL,
1182 NULL, NULL, NULL, NULL, 0,
1183 NULL, NULL, NULL, NULL, NULL,
1184 NULL, 0, NULL, NULL, NULL);
1187 case IMAP_SEARCH_TYPE_FLAGGED:
1188 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1189 NULL, NULL, NULL, NULL, NULL,
1190 NULL, NULL, NULL, NULL, NULL,
1191 NULL, NULL, NULL, NULL, 0,
1192 NULL, NULL, NULL, NULL, NULL,
1193 NULL, 0, NULL, NULL, NULL);
1195 case IMAP_SEARCH_TYPE_DELETED:
1196 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1197 NULL, NULL, NULL, NULL, NULL,
1198 NULL, NULL, NULL, NULL, NULL,
1199 NULL, NULL, NULL, NULL, 0,
1200 NULL, NULL, NULL, NULL, NULL,
1201 NULL, 0, NULL, NULL, NULL);
1205 if (search_type_key != NULL) {
1206 if (param->set != NULL) {
1207 key = mailimap_search_key_new_multiple_empty();
1208 mailimap_search_key_multiple_add(key, search_type_key);
1209 mailimap_search_key_multiple_add(key, uid_key);
1211 key = search_type_key;
1213 } else if (uid_key != NULL) {
1218 g_warning("no key!");
1219 result = op->result;
1221 result->search_result = NULL;
1223 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1225 /* free the key (with the imapset) */
1226 mailimap_search_key_free(key);
1228 result = op->result;
1230 result->search_result = search_result;
1232 debug_print("imap search run - end %i\n", result->error);
1235 int imap_threaded_search(Folder * folder, int search_type,
1236 struct mailimap_set * set, clist ** search_result)
1238 struct search_param param;
1239 struct search_result result;
1242 debug_print("imap search - begin\n");
1244 imap = get_imap(folder);
1247 param.type = search_type;
1249 threaded_run(folder, ¶m, &result, search_run);
1251 if (result.error != MAILIMAP_NO_ERROR)
1252 return result.error;
1254 debug_print("imap search - end\n");
1256 * search_result = result.search_result;
1258 return result.error;
1265 uid_list_to_env_list(clist * fetch_result, carray ** result)
1273 tab = carray_new(128);
1275 res = MAILIMAP_ERROR_MEMORY;
1279 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1280 cur = clist_next(cur)) {
1281 struct mailimap_msg_att * msg_att;
1282 clistiter * item_cur;
1287 msg_att = clist_content(cur);
1291 for(item_cur = clist_begin(msg_att->att_list) ;
1293 item_cur = clist_next(item_cur)) {
1294 struct mailimap_msg_att_item * item;
1296 item = clist_content(item_cur);
1298 switch (item->att_type) {
1299 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1300 switch (item->att_data.att_static->att_type) {
1301 case MAILIMAP_MSG_ATT_UID:
1302 uid = item->att_data.att_static->att_data.att_uid;
1308 puid = malloc(sizeof(* puid));
1310 res = MAILIMAP_ERROR_MEMORY;
1315 r = carray_add(tab, puid, NULL);
1318 res = MAILIMAP_ERROR_MEMORY;
1325 return MAILIMAP_NO_ERROR;
1328 for(i = 0 ; i < carray_count(tab) ; i++)
1329 mailmessage_free(carray_get(tab, i));
1334 static int imap_get_messages_list(mailimap * imap,
1335 uint32_t first_index,
1340 struct mailimap_fetch_att * fetch_att;
1341 struct mailimap_fetch_type * fetch_type;
1342 struct mailimap_set * set;
1343 clist * fetch_result;
1346 set = mailimap_set_new_interval(first_index, 0);
1348 res = MAILIMAP_ERROR_MEMORY;
1352 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1353 if (fetch_type == NULL) {
1354 res = MAILIMAP_ERROR_MEMORY;
1358 fetch_att = mailimap_fetch_att_new_uid();
1359 if (fetch_att == NULL) {
1360 res = MAILIMAP_ERROR_MEMORY;
1361 goto free_fetch_type;
1364 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1365 if (r != MAILIMAP_NO_ERROR) {
1366 mailimap_fetch_att_free(fetch_att);
1367 res = MAILIMAP_ERROR_MEMORY;
1368 goto free_fetch_type;
1371 r = mailimap_uid_fetch(imap, set,
1372 fetch_type, &fetch_result);
1374 mailimap_fetch_type_free(fetch_type);
1375 mailimap_set_free(set);
1377 if (r != MAILIMAP_NO_ERROR) {
1383 r = uid_list_to_env_list(fetch_result, &env_list);
1384 mailimap_fetch_list_free(fetch_result);
1386 * result = env_list;
1388 return MAILIMAP_NO_ERROR;
1391 mailimap_fetch_type_free(fetch_type);
1393 mailimap_set_free(set);
1401 struct fetch_uid_param {
1403 uint32_t first_index;
1406 struct fetch_uid_result {
1408 carray * fetch_result;
1411 static void fetch_uid_run(struct etpan_thread_op * op)
1413 struct fetch_uid_param * param;
1414 struct fetch_uid_result * result;
1415 carray * fetch_result;
1420 fetch_result = NULL;
1421 r = imap_get_messages_list(param->imap, param->first_index,
1424 result = op->result;
1426 result->fetch_result = fetch_result;
1427 debug_print("imap fetch_uid run - end %i\n", r);
1430 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1431 carray ** fetch_result)
1433 struct fetch_uid_param param;
1434 struct fetch_uid_result result;
1437 debug_print("imap fetch_uid - begin\n");
1439 imap = get_imap(folder);
1441 param.first_index = first_index;
1443 threaded_run(folder, ¶m, &result, fetch_uid_run);
1445 if (result.error != MAILIMAP_NO_ERROR)
1446 return result.error;
1448 debug_print("imap fetch_uid - end\n");
1450 * fetch_result = result.fetch_result;
1452 return result.error;
1456 void imap_fetch_uid_list_free(carray * uid_list)
1460 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1463 puid = carray_get(uid_list, i);
1466 carray_free(uid_list);
1471 static int imap_fetch(mailimap * imap,
1474 size_t * result_len)
1477 struct mailimap_set * set;
1478 struct mailimap_fetch_att * fetch_att;
1479 struct mailimap_fetch_type * fetch_type;
1480 clist * fetch_result;
1481 struct mailimap_msg_att * msg_att;
1482 struct mailimap_msg_att_item * msg_att_item;
1487 struct mailimap_section * section;
1489 set = mailimap_set_new_single(msg_index);
1491 res = MAILIMAP_ERROR_MEMORY;
1495 section = mailimap_section_new(NULL);
1496 if (section == NULL) {
1497 res = MAILIMAP_ERROR_MEMORY;
1501 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1502 if (fetch_att == NULL) {
1503 mailimap_section_free(section);
1504 res = MAILIMAP_ERROR_MEMORY;
1508 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1509 if (fetch_type == NULL) {
1510 res = MAILIMAP_ERROR_MEMORY;
1511 goto free_fetch_att;
1514 mailstream_logger = imap_logger_fetch;
1516 r = mailimap_uid_fetch(imap, set,
1517 fetch_type, &fetch_result);
1519 mailstream_logger = imap_logger_cmd;
1521 mailimap_fetch_type_free(fetch_type);
1522 mailimap_set_free(set);
1525 case MAILIMAP_NO_ERROR:
1531 if (clist_begin(fetch_result) == NULL) {
1532 mailimap_fetch_list_free(fetch_result);
1533 return MAILIMAP_ERROR_FETCH;
1536 msg_att = clist_begin(fetch_result)->data;
1541 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1542 cur = clist_next(cur)) {
1543 msg_att_item = clist_content(cur);
1545 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1546 if (msg_att_item->att_data.att_static->att_type ==
1547 MAILIMAP_MSG_ATT_BODY_SECTION) {
1548 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1550 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1552 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1557 mailimap_fetch_list_free(fetch_result);
1560 return MAILIMAP_ERROR_FETCH;
1563 * result_len = text_length;
1565 return MAILIMAP_NO_ERROR;
1568 mailimap_fetch_att_free(fetch_att);
1570 mailimap_set_free(set);
1575 static int imap_fetch_header(mailimap * imap,
1578 size_t * result_len)
1581 struct mailimap_set * set;
1582 struct mailimap_fetch_att * fetch_att;
1583 struct mailimap_fetch_type * fetch_type;
1584 clist * fetch_result;
1585 struct mailimap_msg_att * msg_att;
1586 struct mailimap_msg_att_item * msg_att_item;
1591 struct mailimap_section * section;
1593 set = mailimap_set_new_single(msg_index);
1595 res = MAILIMAP_ERROR_MEMORY;
1599 section = mailimap_section_new_header();
1600 if (section == NULL) {
1601 res = MAILIMAP_ERROR_MEMORY;
1605 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1606 if (fetch_att == NULL) {
1607 mailimap_section_free(section);
1608 res = MAILIMAP_ERROR_MEMORY;
1612 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1613 if (fetch_type == NULL) {
1614 res = MAILIMAP_ERROR_MEMORY;
1615 goto free_fetch_att;
1618 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1620 mailimap_fetch_type_free(fetch_type);
1621 mailimap_set_free(set);
1624 case MAILIMAP_NO_ERROR:
1630 if (clist_begin(fetch_result) == NULL) {
1631 mailimap_fetch_list_free(fetch_result);
1632 return MAILIMAP_ERROR_FETCH;
1635 msg_att = clist_begin(fetch_result)->data;
1640 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1641 cur = clist_next(cur)) {
1642 msg_att_item = clist_content(cur);
1644 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1645 if (msg_att_item->att_data.att_static->att_type ==
1646 MAILIMAP_MSG_ATT_BODY_SECTION) {
1647 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1648 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1650 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1655 mailimap_fetch_list_free(fetch_result);
1658 return MAILIMAP_ERROR_FETCH;
1661 * result_len = text_length;
1663 return MAILIMAP_NO_ERROR;
1666 mailimap_fetch_att_free(fetch_att);
1668 mailimap_set_free(set);
1675 struct fetch_content_param {
1678 const char * filename;
1682 struct fetch_content_result {
1686 static void fetch_content_run(struct etpan_thread_op * op)
1688 struct fetch_content_param * param;
1689 struct fetch_content_result * result;
1691 size_t content_size;
1700 if (param->with_body)
1701 r = imap_fetch(param->imap, param->msg_index,
1702 &content, &content_size);
1704 r = imap_fetch_header(param->imap, param->msg_index,
1705 &content, &content_size);
1707 result = op->result;
1710 if (r == MAILIMAP_NO_ERROR) {
1711 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1713 result->error = MAILIMAP_ERROR_FETCH;
1717 f = fdopen(fd, "wb");
1719 result->error = MAILIMAP_ERROR_FETCH;
1723 r = fwrite(content, 1, content_size, f);
1730 g_unlink(param->filename);
1741 g_unlink(param->filename);
1744 if (mmap_string_unref(content) != 0)
1748 debug_print("imap fetch_content run - end %i\n", r);
1751 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1753 const char * filename)
1755 struct fetch_content_param param;
1756 struct fetch_content_result result;
1759 debug_print("imap fetch_content - begin\n");
1761 imap = get_imap(folder);
1763 param.msg_index = msg_index;
1764 param.filename = filename;
1765 param.with_body = with_body;
1767 threaded_run(folder, ¶m, &result, fetch_content_run);
1769 if (result.error != MAILIMAP_NO_ERROR)
1770 return result.error;
1772 debug_print("imap fetch_content - end\n");
1774 return result.error;
1779 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1787 flag_list = att_dyn->att_list;
1788 if (flag_list == NULL)
1791 for(cur = clist_begin(flag_list) ; cur != NULL ;
1792 cur = clist_next(cur)) {
1793 struct mailimap_flag_fetch * flag_fetch;
1795 flag_fetch = clist_content(cur);
1796 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1799 switch (flag_fetch->fl_flag->fl_type) {
1800 case MAILIMAP_FLAG_ANSWERED:
1801 flags |= MSG_REPLIED;
1803 case MAILIMAP_FLAG_FLAGGED:
1804 flags |= MSG_MARKED;
1806 case MAILIMAP_FLAG_DELETED:
1807 flags |= MSG_DELETED;
1809 case MAILIMAP_FLAG_SEEN:
1810 flags &= ~MSG_UNREAD;
1820 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1824 struct mailimap_msg_att_dynamic ** patt_dyn)
1826 clistiter * item_cur;
1830 struct mailimap_msg_att_dynamic * att_dyn;
1837 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1838 item_cur = clist_next(item_cur)) {
1839 struct mailimap_msg_att_item * item;
1841 item = clist_content(item_cur);
1843 switch (item->att_type) {
1844 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1845 switch (item->att_data.att_static->att_type) {
1846 case MAILIMAP_MSG_ATT_UID:
1847 uid = item->att_data.att_static->att_data.att_uid;
1850 case MAILIMAP_MSG_ATT_BODY_SECTION:
1851 if (headers == NULL) {
1852 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1855 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1856 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1861 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1862 if (att_dyn == NULL) {
1863 att_dyn = item->att_data.att_dyn;
1871 if (pheaders != NULL)
1872 * pheaders = headers;
1873 if (pref_size != NULL)
1874 * pref_size = ref_size;
1875 if (patt_dyn != NULL)
1876 * patt_dyn = att_dyn;
1878 return MAIL_NO_ERROR;
1881 static struct imap_fetch_env_info *
1882 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1884 struct imap_fetch_env_info * info;
1888 struct mailimap_msg_att_dynamic * att_dyn;
1890 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1895 info = malloc(sizeof(* info));
1897 info->headers = strdup(headers);
1899 info->flags = imap_flags_to_flags(att_dyn);
1905 imap_fetch_result_to_envelop_list(clist * fetch_result,
1906 carray ** p_env_list)
1913 env_list = carray_new(16);
1915 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1916 cur = clist_next(cur)) {
1917 struct mailimap_msg_att * msg_att;
1918 struct imap_fetch_env_info * env_info;
1920 msg_att = clist_content(cur);
1922 env_info = fetch_to_env_info(msg_att);
1924 return MAILIMAP_ERROR_MEMORY;
1925 carray_add(env_list, env_info, NULL);
1928 * p_env_list = env_list;
1930 return MAIL_NO_ERROR;
1933 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1935 struct mailimap_fetch_att * fetch_att;
1939 struct mailimap_header_list * imap_hdrlist;
1940 struct mailimap_section * section;
1942 hdrlist = clist_new();
1944 header = strdup("Date");
1945 r = clist_append(hdrlist, header);
1946 header = strdup("From");
1947 r = clist_append(hdrlist, header);
1948 header = strdup("To");
1949 r = clist_append(hdrlist, header);
1950 header = strdup("Cc");
1951 r = clist_append(hdrlist, header);
1952 header = strdup("Subject");
1953 r = clist_append(hdrlist, header);
1954 header = strdup("Message-ID");
1955 r = clist_append(hdrlist, header);
1956 header = strdup("References");
1957 r = clist_append(hdrlist, header);
1958 header = strdup("In-Reply-To");
1959 r = clist_append(hdrlist, header);
1961 imap_hdrlist = mailimap_header_list_new(hdrlist);
1962 section = mailimap_section_new_header_fields(imap_hdrlist);
1963 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1964 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1966 return MAIL_NO_ERROR;
1969 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1971 struct mailimap_fetch_att * fetch_att;
1972 struct mailimap_section * section;
1974 section = mailimap_section_new_header();
1975 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1976 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1978 return MAIL_NO_ERROR;
1982 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1983 carray ** p_env_list)
1985 struct mailimap_fetch_att * fetch_att;
1986 struct mailimap_fetch_type * fetch_type;
1988 clist * fetch_result;
1990 carray * env_list = NULL;
1994 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1997 fetch_att = mailimap_fetch_att_new_uid();
1998 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2001 fetch_att = mailimap_fetch_att_new_flags();
2002 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2005 fetch_att = mailimap_fetch_att_new_rfc822_size();
2006 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2010 key.len = sizeof(imap);
2011 r = chash_get(courier_workaround_hash, &key, &value);
2013 r = imap_add_envelope_fetch_att(fetch_type);
2015 r = imap_add_header_fetch_att(fetch_type);
2017 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2020 case MAILIMAP_NO_ERROR:
2023 mailimap_fetch_type_free(fetch_type);
2024 debug_print("uid_fetch: %d\n", r);
2028 if (clist_begin(fetch_result) == NULL) {
2029 res = MAILIMAP_ERROR_FETCH;
2030 debug_print("clist_begin = NULL\n");
2034 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2035 mailimap_fetch_list_free(fetch_result);
2037 if (r != MAILIMAP_NO_ERROR) {
2038 mailimap_fetch_type_free(fetch_type);
2039 res = MAILIMAP_ERROR_MEMORY;
2040 debug_print("fetch_result_to_envelop_list: %d\n", res);
2044 mailimap_fetch_type_free(fetch_type);
2046 * p_env_list = env_list;
2048 return MAILIMAP_NO_ERROR;
2054 struct fetch_env_param {
2056 struct mailimap_set * set;
2059 struct fetch_env_result {
2060 carray * fetch_env_result;
2064 static void fetch_env_run(struct etpan_thread_op * op)
2066 struct fetch_env_param * param;
2067 struct fetch_env_result * result;
2074 r = imap_get_envelopes_list(param->imap, param->set,
2077 result = op->result;
2079 result->fetch_env_result = env_list;
2081 debug_print("imap fetch_env run - end %i\n", r);
2084 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2085 carray ** p_env_list)
2087 struct fetch_env_param param;
2088 struct fetch_env_result result;
2091 debug_print("imap fetch_env - begin\n");
2093 imap = get_imap(folder);
2097 threaded_run(folder, ¶m, &result, fetch_env_run);
2099 if (result.error != MAILIMAP_NO_ERROR) {
2105 key.len = sizeof(imap);
2106 r = chash_get(courier_workaround_hash, &key, &value);
2110 chash_set(courier_workaround_hash, &key, &value, NULL);
2112 threaded_run(folder, ¶m, &result, fetch_env_run);
2116 if (result.error != MAILIMAP_NO_ERROR)
2117 return result.error;
2119 debug_print("imap fetch_env - end\n");
2121 * p_env_list = result.fetch_env_result;
2123 return result.error;
2126 void imap_fetch_env_free(carray * env_list)
2130 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2131 struct imap_fetch_env_info * env_info;
2133 env_info = carray_get(env_list, i);
2134 free(env_info->headers);
2137 carray_free(env_list);
2144 struct append_param {
2146 const char * mailbox;
2147 const char * filename;
2148 struct mailimap_flag_list * flag_list;
2151 struct append_result {
2156 static void append_run(struct etpan_thread_op * op)
2158 struct append_param * param;
2159 struct append_result * result;
2163 struct stat stat_buf;
2167 result = op->result;
2169 r = stat(param->filename, &stat_buf);
2171 result->error = MAILIMAP_ERROR_APPEND;
2174 size = stat_buf.st_size;
2176 fd = open(param->filename, O_RDONLY);
2178 result->error = MAILIMAP_ERROR_APPEND;
2182 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2183 if (data == (void *) MAP_FAILED) {
2185 result->error = MAILIMAP_ERROR_APPEND;
2189 mailstream_logger = imap_logger_append;
2191 r = mailimap_append(param->imap, param->mailbox,
2192 param->flag_list, NULL,
2193 data, size/*, &uid */);
2195 mailstream_logger = imap_logger_cmd;
2202 debug_print("imap append run - end %i uid %d\n", r, uid);
2205 int imap_threaded_append(Folder * folder, const char * mailbox,
2206 const char * filename,
2207 struct mailimap_flag_list * flag_list,
2210 struct append_param param;
2211 struct append_result result;
2214 debug_print("imap append - begin\n");
2216 imap = get_imap(folder);
2218 param.mailbox = mailbox;
2219 param.filename = filename;
2220 param.flag_list = flag_list;
2222 threaded_run(folder, ¶m, &result, append_run);
2224 if (result.error != MAILIMAP_NO_ERROR)
2225 return result.error;
2227 debug_print("imap append - end\n");
2231 return result.error;
2237 struct expunge_param {
2241 struct expunge_result {
2245 static void expunge_run(struct etpan_thread_op * op)
2247 struct expunge_param * param;
2248 struct expunge_result * result;
2252 r = mailimap_expunge(param->imap);
2254 result = op->result;
2256 debug_print("imap expunge run - end %i\n", r);
2259 int imap_threaded_expunge(Folder * folder)
2261 struct expunge_param param;
2262 struct expunge_result result;
2264 debug_print("imap expunge - begin\n");
2266 param.imap = get_imap(folder);
2268 threaded_run(folder, ¶m, &result, expunge_run);
2270 debug_print("imap expunge - end\n");
2272 return result.error;
2278 struct mailimap_set * set;
2282 struct copy_result {
2286 static void copy_run(struct etpan_thread_op * op)
2288 struct copy_param * param;
2289 struct copy_result * result;
2294 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2296 result = op->result;
2299 debug_print("imap copy run - end %i\n", r);
2302 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2305 struct copy_param param;
2306 struct copy_result result;
2309 debug_print("imap copy - begin\n");
2311 imap = get_imap(folder);
2316 threaded_run(folder, ¶m, &result, copy_run);
2318 if (result.error != MAILIMAP_NO_ERROR)
2319 return result.error;
2321 debug_print("imap copy - end\n");
2323 return result.error;
2328 struct store_param {
2330 struct mailimap_set * set;
2331 struct mailimap_store_att_flags * store_att_flags;
2334 struct store_result {
2338 static void store_run(struct etpan_thread_op * op)
2340 struct store_param * param;
2341 struct store_result * result;
2346 r = mailimap_uid_store(param->imap, param->set,
2347 param->store_att_flags);
2349 result = op->result;
2352 debug_print("imap store run - end %i\n", r);
2355 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2356 struct mailimap_store_att_flags * store_att_flags)
2358 struct store_param param;
2359 struct store_result result;
2362 debug_print("imap store - begin\n");
2364 imap = get_imap(folder);
2367 param.store_att_flags = store_att_flags;
2369 threaded_run(folder, ¶m, &result, store_run);
2371 if (result.error != MAILIMAP_NO_ERROR)
2372 return result.error;
2374 debug_print("imap store - end\n");
2376 return result.error;
2380 #define ENV_BUFFER_SIZE 512
2382 static void do_exec_command(int fd, const char * command,
2383 const char * servername, uint16_t port)
2387 char env_buffer[ENV_BUFFER_SIZE];
2391 /* Fork again to become a child of init rather than
2392 the etpan client. */
2398 snprintf(env_buffer, ENV_BUFFER_SIZE,
2399 "ETPANSERVER=%s", servername);
2401 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2405 setenv("ETPANSERVER", servername, 1);
2407 unsetenv("ETPANSERVER");
2412 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2414 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2420 snprintf(porttext, sizeof(porttext), "%d", port);
2421 setenv("ETPANPORT", porttext, 1);
2424 unsetenv("ETPANPORT");
2428 /* Not a lot we can do if there's an error other than bail. */
2429 if (dup2(fd, 0) == -1)
2431 if (dup2(fd, 1) == -1)
2434 /* Should we close stderr and reopen /dev/null? */
2436 maxopen = sysconf(_SC_OPEN_MAX);
2437 for (i=3; i < maxopen; i++)
2441 /* Detach from the controlling tty if we have one. Otherwise,
2442 SSH might do something stupid like trying to use it instead
2443 of running $SSH_ASKPASS. Doh. */
2444 fd = open("/dev/tty", O_RDONLY);
2446 ioctl(fd, TIOCNOTTY, NULL);
2449 #endif /* TIOCNOTTY */
2451 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2453 /* Eep. Shouldn't reach this */
2457 static int subcommand_connect(const char *command,
2458 const char *servername, uint16_t port)
2460 /* SEB unsupported on Windows */
2464 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2469 do_exec_command(sockfds[1], command, servername, port);
2471 else if (childpid == -1) {
2479 /* Reap child, leaving grandchild process to run */
2480 waitpid(childpid, NULL, 0);
2485 int socket_connect_cmd(mailimap * imap, const char * command,
2486 const char * server, int port)
2492 fd = subcommand_connect(command, server, port);
2494 return MAILIMAP_ERROR_STREAM;
2496 s = mailstream_socket_open(fd);
2499 return MAILIMAP_ERROR_STREAM;
2502 r = mailimap_connect(imap, s);
2503 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2504 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2505 mailstream_close(s);
2514 struct connect_cmd_param {
2516 const char * command;
2517 const char * server;
2521 struct connect_cmd_result {
2525 static void connect_cmd_run(struct etpan_thread_op * op)
2528 struct connect_cmd_param * param;
2529 struct connect_cmd_result * result;
2532 result = op->result;
2534 r = socket_connect_cmd(param->imap, param->command,
2535 param->server, param->port);
2541 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2542 const char * server, int port)
2544 struct connect_cmd_param param;
2545 struct connect_cmd_result result;
2550 imap = mailimap_new(0, NULL);
2553 key.len = sizeof(folder);
2556 chash_set(session_hash, &key, &value, NULL);
2559 param.command = command;
2560 param.server = server;
2563 threaded_run(folder, ¶m, &result, connect_cmd_run);
2565 debug_print("connect_cmd ok %i\n", result.error);
2567 return result.error;
2571 void imap_main_init(void)
2574 void imap_main_done(void)
2577 void imap_main_set_timeout(int sec)