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 < 32) {
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;
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;
1214 else if (uid_key != NULL) {
1219 g_warning("no key!");
1220 result = op->result;
1222 result->search_result = NULL;
1224 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1226 /* free the key (with the imapset) */
1227 mailimap_search_key_free(key);
1229 result = op->result;
1231 result->search_result = search_result;
1233 debug_print("imap search run - end %i\n", r);
1236 int imap_threaded_search(Folder * folder, int search_type,
1237 struct mailimap_set * set, clist ** search_result)
1239 struct search_param param;
1240 struct search_result result;
1243 debug_print("imap search - begin\n");
1245 imap = get_imap(folder);
1248 param.type = search_type;
1250 threaded_run(folder, ¶m, &result, search_run);
1252 if (result.error != MAILIMAP_NO_ERROR)
1253 return result.error;
1255 debug_print("imap search - end\n");
1257 * search_result = result.search_result;
1259 return result.error;
1266 uid_list_to_env_list(clist * fetch_result, carray ** result)
1274 tab = carray_new(128);
1276 res = MAILIMAP_ERROR_MEMORY;
1280 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1281 cur = clist_next(cur)) {
1282 struct mailimap_msg_att * msg_att;
1283 clistiter * item_cur;
1288 msg_att = clist_content(cur);
1292 for(item_cur = clist_begin(msg_att->att_list) ;
1294 item_cur = clist_next(item_cur)) {
1295 struct mailimap_msg_att_item * item;
1297 item = clist_content(item_cur);
1299 switch (item->att_type) {
1300 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1301 switch (item->att_data.att_static->att_type) {
1302 case MAILIMAP_MSG_ATT_UID:
1303 uid = item->att_data.att_static->att_data.att_uid;
1309 puid = malloc(sizeof(* puid));
1311 res = MAILIMAP_ERROR_MEMORY;
1316 r = carray_add(tab, puid, NULL);
1319 res = MAILIMAP_ERROR_MEMORY;
1326 return MAILIMAP_NO_ERROR;
1329 for(i = 0 ; i < carray_count(tab) ; i++)
1330 mailmessage_free(carray_get(tab, i));
1335 static int imap_get_messages_list(mailimap * imap,
1336 uint32_t first_index,
1341 struct mailimap_fetch_att * fetch_att;
1342 struct mailimap_fetch_type * fetch_type;
1343 struct mailimap_set * set;
1344 clist * fetch_result;
1347 set = mailimap_set_new_interval(first_index, 0);
1349 res = MAILIMAP_ERROR_MEMORY;
1353 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1354 if (fetch_type == NULL) {
1355 res = MAILIMAP_ERROR_MEMORY;
1359 fetch_att = mailimap_fetch_att_new_uid();
1360 if (fetch_att == NULL) {
1361 res = MAILIMAP_ERROR_MEMORY;
1362 goto free_fetch_type;
1365 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1366 if (r != MAILIMAP_NO_ERROR) {
1367 mailimap_fetch_att_free(fetch_att);
1368 res = MAILIMAP_ERROR_MEMORY;
1369 goto free_fetch_type;
1372 r = mailimap_uid_fetch(imap, set,
1373 fetch_type, &fetch_result);
1375 mailimap_fetch_type_free(fetch_type);
1376 mailimap_set_free(set);
1378 if (r != MAILIMAP_NO_ERROR) {
1384 r = uid_list_to_env_list(fetch_result, &env_list);
1385 mailimap_fetch_list_free(fetch_result);
1387 * result = env_list;
1389 return MAILIMAP_NO_ERROR;
1392 mailimap_fetch_type_free(fetch_type);
1394 mailimap_set_free(set);
1402 struct fetch_uid_param {
1404 uint32_t first_index;
1407 struct fetch_uid_result {
1409 carray * fetch_result;
1412 static void fetch_uid_run(struct etpan_thread_op * op)
1414 struct fetch_uid_param * param;
1415 struct fetch_uid_result * result;
1416 carray * fetch_result;
1421 fetch_result = NULL;
1422 r = imap_get_messages_list(param->imap, param->first_index,
1425 result = op->result;
1427 result->fetch_result = fetch_result;
1428 debug_print("imap fetch_uid run - end %i\n", r);
1431 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1432 carray ** fetch_result)
1434 struct fetch_uid_param param;
1435 struct fetch_uid_result result;
1438 debug_print("imap fetch_uid - begin\n");
1440 imap = get_imap(folder);
1442 param.first_index = first_index;
1444 threaded_run(folder, ¶m, &result, fetch_uid_run);
1446 if (result.error != MAILIMAP_NO_ERROR)
1447 return result.error;
1449 debug_print("imap fetch_uid - end\n");
1451 * fetch_result = result.fetch_result;
1453 return result.error;
1457 void imap_fetch_uid_list_free(carray * uid_list)
1461 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1464 puid = carray_get(uid_list, i);
1467 carray_free(uid_list);
1472 static int imap_fetch(mailimap * imap,
1475 size_t * result_len)
1478 struct mailimap_set * set;
1479 struct mailimap_fetch_att * fetch_att;
1480 struct mailimap_fetch_type * fetch_type;
1481 clist * fetch_result;
1482 struct mailimap_msg_att * msg_att;
1483 struct mailimap_msg_att_item * msg_att_item;
1488 struct mailimap_section * section;
1490 set = mailimap_set_new_single(msg_index);
1492 res = MAILIMAP_ERROR_MEMORY;
1496 section = mailimap_section_new(NULL);
1497 if (section == NULL) {
1498 res = MAILIMAP_ERROR_MEMORY;
1502 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1503 if (fetch_att == NULL) {
1504 mailimap_section_free(section);
1505 res = MAILIMAP_ERROR_MEMORY;
1509 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1510 if (fetch_type == NULL) {
1511 res = MAILIMAP_ERROR_MEMORY;
1512 goto free_fetch_att;
1515 mailstream_logger = imap_logger_fetch;
1517 r = mailimap_uid_fetch(imap, set,
1518 fetch_type, &fetch_result);
1520 mailstream_logger = imap_logger_cmd;
1522 mailimap_fetch_type_free(fetch_type);
1523 mailimap_set_free(set);
1526 case MAILIMAP_NO_ERROR:
1532 if (clist_begin(fetch_result) == NULL) {
1533 mailimap_fetch_list_free(fetch_result);
1534 return MAILIMAP_ERROR_FETCH;
1537 msg_att = clist_begin(fetch_result)->data;
1542 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1543 cur = clist_next(cur)) {
1544 msg_att_item = clist_content(cur);
1546 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1547 if (msg_att_item->att_data.att_static->att_type ==
1548 MAILIMAP_MSG_ATT_BODY_SECTION) {
1549 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1551 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1553 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1558 mailimap_fetch_list_free(fetch_result);
1561 return MAILIMAP_ERROR_FETCH;
1564 * result_len = text_length;
1566 return MAILIMAP_NO_ERROR;
1569 mailimap_fetch_att_free(fetch_att);
1571 mailimap_set_free(set);
1576 static int imap_fetch_header(mailimap * imap,
1579 size_t * result_len)
1582 struct mailimap_set * set;
1583 struct mailimap_fetch_att * fetch_att;
1584 struct mailimap_fetch_type * fetch_type;
1585 clist * fetch_result;
1586 struct mailimap_msg_att * msg_att;
1587 struct mailimap_msg_att_item * msg_att_item;
1592 struct mailimap_section * section;
1594 set = mailimap_set_new_single(msg_index);
1596 res = MAILIMAP_ERROR_MEMORY;
1600 section = mailimap_section_new_header();
1601 if (section == NULL) {
1602 res = MAILIMAP_ERROR_MEMORY;
1606 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1607 if (fetch_att == NULL) {
1608 mailimap_section_free(section);
1609 res = MAILIMAP_ERROR_MEMORY;
1613 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1614 if (fetch_type == NULL) {
1615 res = MAILIMAP_ERROR_MEMORY;
1616 goto free_fetch_att;
1619 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1621 mailimap_fetch_type_free(fetch_type);
1622 mailimap_set_free(set);
1625 case MAILIMAP_NO_ERROR:
1631 if (clist_begin(fetch_result) == NULL) {
1632 mailimap_fetch_list_free(fetch_result);
1633 return MAILIMAP_ERROR_FETCH;
1636 msg_att = clist_begin(fetch_result)->data;
1641 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1642 cur = clist_next(cur)) {
1643 msg_att_item = clist_content(cur);
1645 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1646 if (msg_att_item->att_data.att_static->att_type ==
1647 MAILIMAP_MSG_ATT_BODY_SECTION) {
1648 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1649 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1651 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1656 mailimap_fetch_list_free(fetch_result);
1659 return MAILIMAP_ERROR_FETCH;
1662 * result_len = text_length;
1664 return MAILIMAP_NO_ERROR;
1667 mailimap_fetch_att_free(fetch_att);
1669 mailimap_set_free(set);
1676 struct fetch_content_param {
1679 const char * filename;
1683 struct fetch_content_result {
1687 static void fetch_content_run(struct etpan_thread_op * op)
1689 struct fetch_content_param * param;
1690 struct fetch_content_result * result;
1692 size_t content_size;
1701 if (param->with_body)
1702 r = imap_fetch(param->imap, param->msg_index,
1703 &content, &content_size);
1705 r = imap_fetch_header(param->imap, param->msg_index,
1706 &content, &content_size);
1708 result = op->result;
1711 if (r == MAILIMAP_NO_ERROR) {
1712 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1714 result->error = MAILIMAP_ERROR_FETCH;
1718 f = fdopen(fd, "wb");
1720 result->error = MAILIMAP_ERROR_FETCH;
1724 r = fwrite(content, 1, content_size, f);
1731 g_unlink(param->filename);
1742 g_unlink(param->filename);
1745 if (mmap_string_unref(content) != 0)
1749 debug_print("imap fetch_content run - end %i\n", r);
1752 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1754 const char * filename)
1756 struct fetch_content_param param;
1757 struct fetch_content_result result;
1760 debug_print("imap fetch_content - begin\n");
1762 imap = get_imap(folder);
1764 param.msg_index = msg_index;
1765 param.filename = filename;
1766 param.with_body = with_body;
1768 threaded_run(folder, ¶m, &result, fetch_content_run);
1770 if (result.error != MAILIMAP_NO_ERROR)
1771 return result.error;
1773 debug_print("imap fetch_content - end\n");
1775 return result.error;
1780 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1788 flag_list = att_dyn->att_list;
1789 if (flag_list == NULL)
1792 for(cur = clist_begin(flag_list) ; cur != NULL ;
1793 cur = clist_next(cur)) {
1794 struct mailimap_flag_fetch * flag_fetch;
1796 flag_fetch = clist_content(cur);
1797 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1800 switch (flag_fetch->fl_flag->fl_type) {
1801 case MAILIMAP_FLAG_ANSWERED:
1802 flags |= MSG_REPLIED;
1804 case MAILIMAP_FLAG_FLAGGED:
1805 flags |= MSG_MARKED;
1807 case MAILIMAP_FLAG_DELETED:
1808 flags |= MSG_DELETED;
1810 case MAILIMAP_FLAG_SEEN:
1811 flags &= ~MSG_UNREAD;
1821 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1825 struct mailimap_msg_att_dynamic ** patt_dyn)
1827 clistiter * item_cur;
1831 struct mailimap_msg_att_dynamic * att_dyn;
1838 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1839 item_cur = clist_next(item_cur)) {
1840 struct mailimap_msg_att_item * item;
1842 item = clist_content(item_cur);
1844 switch (item->att_type) {
1845 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1846 switch (item->att_data.att_static->att_type) {
1847 case MAILIMAP_MSG_ATT_UID:
1848 uid = item->att_data.att_static->att_data.att_uid;
1851 case MAILIMAP_MSG_ATT_BODY_SECTION:
1852 if (headers == NULL) {
1853 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1856 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1857 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1862 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1863 if (att_dyn == NULL) {
1864 att_dyn = item->att_data.att_dyn;
1872 if (pheaders != NULL)
1873 * pheaders = headers;
1874 if (pref_size != NULL)
1875 * pref_size = ref_size;
1876 if (patt_dyn != NULL)
1877 * patt_dyn = att_dyn;
1879 return MAIL_NO_ERROR;
1882 static struct imap_fetch_env_info *
1883 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1885 struct imap_fetch_env_info * info;
1889 struct mailimap_msg_att_dynamic * att_dyn;
1891 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1896 info = malloc(sizeof(* info));
1898 info->headers = strdup(headers);
1900 info->flags = imap_flags_to_flags(att_dyn);
1906 imap_fetch_result_to_envelop_list(clist * fetch_result,
1907 carray ** p_env_list)
1914 env_list = carray_new(16);
1916 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1917 cur = clist_next(cur)) {
1918 struct mailimap_msg_att * msg_att;
1919 struct imap_fetch_env_info * env_info;
1921 msg_att = clist_content(cur);
1923 env_info = fetch_to_env_info(msg_att);
1925 return MAILIMAP_ERROR_MEMORY;
1926 carray_add(env_list, env_info, NULL);
1929 * p_env_list = env_list;
1931 return MAIL_NO_ERROR;
1934 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1936 struct mailimap_fetch_att * fetch_att;
1940 struct mailimap_header_list * imap_hdrlist;
1941 struct mailimap_section * section;
1943 hdrlist = clist_new();
1945 header = strdup("Date");
1946 r = clist_append(hdrlist, header);
1947 header = strdup("From");
1948 r = clist_append(hdrlist, header);
1949 header = strdup("To");
1950 r = clist_append(hdrlist, header);
1951 header = strdup("Cc");
1952 r = clist_append(hdrlist, header);
1953 header = strdup("Subject");
1954 r = clist_append(hdrlist, header);
1955 header = strdup("Message-ID");
1956 r = clist_append(hdrlist, header);
1957 header = strdup("References");
1958 r = clist_append(hdrlist, header);
1959 header = strdup("In-Reply-To");
1960 r = clist_append(hdrlist, header);
1962 imap_hdrlist = mailimap_header_list_new(hdrlist);
1963 section = mailimap_section_new_header_fields(imap_hdrlist);
1964 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1965 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1967 return MAIL_NO_ERROR;
1970 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1972 struct mailimap_fetch_att * fetch_att;
1973 struct mailimap_section * section;
1975 section = mailimap_section_new_header();
1976 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1977 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1979 return MAIL_NO_ERROR;
1983 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1984 carray ** p_env_list)
1986 struct mailimap_fetch_att * fetch_att;
1987 struct mailimap_fetch_type * fetch_type;
1989 clist * fetch_result;
1991 carray * env_list = NULL;
1995 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1998 fetch_att = mailimap_fetch_att_new_uid();
1999 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2002 fetch_att = mailimap_fetch_att_new_flags();
2003 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2006 fetch_att = mailimap_fetch_att_new_rfc822_size();
2007 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2011 key.len = sizeof(imap);
2012 r = chash_get(courier_workaround_hash, &key, &value);
2014 r = imap_add_envelope_fetch_att(fetch_type);
2016 r = imap_add_header_fetch_att(fetch_type);
2018 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2021 case MAILIMAP_NO_ERROR:
2024 mailimap_fetch_type_free(fetch_type);
2025 debug_print("uid_fetch: %d\n", r);
2029 if (clist_begin(fetch_result) == NULL) {
2030 res = MAILIMAP_ERROR_FETCH;
2031 debug_print("clist_begin = NULL\n");
2035 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2036 mailimap_fetch_list_free(fetch_result);
2038 if (r != MAILIMAP_NO_ERROR) {
2039 mailimap_fetch_type_free(fetch_type);
2040 res = MAILIMAP_ERROR_MEMORY;
2041 debug_print("fetch_result_to_envelop_list: %d\n", res);
2045 mailimap_fetch_type_free(fetch_type);
2047 * p_env_list = env_list;
2049 return MAILIMAP_NO_ERROR;
2055 struct fetch_env_param {
2057 struct mailimap_set * set;
2060 struct fetch_env_result {
2061 carray * fetch_env_result;
2065 static void fetch_env_run(struct etpan_thread_op * op)
2067 struct fetch_env_param * param;
2068 struct fetch_env_result * result;
2075 r = imap_get_envelopes_list(param->imap, param->set,
2078 result = op->result;
2080 result->fetch_env_result = env_list;
2082 debug_print("imap fetch_env run - end %i\n", r);
2085 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2086 carray ** p_env_list)
2088 struct fetch_env_param param;
2089 struct fetch_env_result result;
2092 debug_print("imap fetch_env - begin\n");
2094 imap = get_imap(folder);
2098 threaded_run(folder, ¶m, &result, fetch_env_run);
2100 if (result.error != MAILIMAP_NO_ERROR) {
2106 key.len = sizeof(imap);
2107 r = chash_get(courier_workaround_hash, &key, &value);
2111 chash_set(courier_workaround_hash, &key, &value, NULL);
2113 threaded_run(folder, ¶m, &result, fetch_env_run);
2117 if (result.error != MAILIMAP_NO_ERROR)
2118 return result.error;
2120 debug_print("imap fetch_env - end\n");
2122 * p_env_list = result.fetch_env_result;
2124 return result.error;
2127 void imap_fetch_env_free(carray * env_list)
2131 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2132 struct imap_fetch_env_info * env_info;
2134 env_info = carray_get(env_list, i);
2135 free(env_info->headers);
2138 carray_free(env_list);
2145 struct append_param {
2147 const char * mailbox;
2148 const char * filename;
2149 struct mailimap_flag_list * flag_list;
2152 struct append_result {
2157 static void append_run(struct etpan_thread_op * op)
2159 struct append_param * param;
2160 struct append_result * result;
2164 struct stat stat_buf;
2168 result = op->result;
2170 r = stat(param->filename, &stat_buf);
2172 result->error = MAILIMAP_ERROR_APPEND;
2175 size = stat_buf.st_size;
2177 fd = open(param->filename, O_RDONLY);
2179 result->error = MAILIMAP_ERROR_APPEND;
2183 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2184 if (data == (void *) MAP_FAILED) {
2186 result->error = MAILIMAP_ERROR_APPEND;
2190 mailstream_logger = imap_logger_append;
2192 r = mailimap_append(param->imap, param->mailbox,
2193 param->flag_list, NULL,
2194 data, size/*, &uid */);
2196 mailstream_logger = imap_logger_cmd;
2203 debug_print("imap append run - end %i uid %d\n", r, uid);
2206 int imap_threaded_append(Folder * folder, const char * mailbox,
2207 const char * filename,
2208 struct mailimap_flag_list * flag_list,
2211 struct append_param param;
2212 struct append_result result;
2215 debug_print("imap append - begin\n");
2217 imap = get_imap(folder);
2219 param.mailbox = mailbox;
2220 param.filename = filename;
2221 param.flag_list = flag_list;
2223 threaded_run(folder, ¶m, &result, append_run);
2225 if (result.error != MAILIMAP_NO_ERROR)
2226 return result.error;
2228 debug_print("imap append - end\n");
2232 return result.error;
2238 struct expunge_param {
2242 struct expunge_result {
2246 static void expunge_run(struct etpan_thread_op * op)
2248 struct expunge_param * param;
2249 struct expunge_result * result;
2253 r = mailimap_expunge(param->imap);
2255 result = op->result;
2257 debug_print("imap expunge run - end %i\n", r);
2260 int imap_threaded_expunge(Folder * folder)
2262 struct expunge_param param;
2263 struct expunge_result result;
2265 debug_print("imap expunge - begin\n");
2267 param.imap = get_imap(folder);
2269 threaded_run(folder, ¶m, &result, expunge_run);
2271 debug_print("imap expunge - end\n");
2273 return result.error;
2279 struct mailimap_set * set;
2283 struct copy_result {
2287 static void copy_run(struct etpan_thread_op * op)
2289 struct copy_param * param;
2290 struct copy_result * result;
2295 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2297 result = op->result;
2300 debug_print("imap copy run - end %i\n", r);
2303 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2306 struct copy_param param;
2307 struct copy_result result;
2310 debug_print("imap copy - begin\n");
2312 imap = get_imap(folder);
2317 threaded_run(folder, ¶m, &result, copy_run);
2319 if (result.error != MAILIMAP_NO_ERROR)
2320 return result.error;
2322 debug_print("imap copy - end\n");
2324 return result.error;
2329 struct store_param {
2331 struct mailimap_set * set;
2332 struct mailimap_store_att_flags * store_att_flags;
2335 struct store_result {
2339 static void store_run(struct etpan_thread_op * op)
2341 struct store_param * param;
2342 struct store_result * result;
2347 r = mailimap_uid_store(param->imap, param->set,
2348 param->store_att_flags);
2350 result = op->result;
2353 debug_print("imap store run - end %i\n", r);
2356 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2357 struct mailimap_store_att_flags * store_att_flags)
2359 struct store_param param;
2360 struct store_result result;
2363 debug_print("imap store - begin\n");
2365 imap = get_imap(folder);
2368 param.store_att_flags = store_att_flags;
2370 threaded_run(folder, ¶m, &result, store_run);
2372 if (result.error != MAILIMAP_NO_ERROR)
2373 return result.error;
2375 debug_print("imap store - end\n");
2377 return result.error;
2381 #define ENV_BUFFER_SIZE 512
2383 static void do_exec_command(int fd, const char * command,
2384 const char * servername, uint16_t port)
2388 char env_buffer[ENV_BUFFER_SIZE];
2392 /* Fork again to become a child of init rather than
2393 the etpan client. */
2399 snprintf(env_buffer, ENV_BUFFER_SIZE,
2400 "ETPANSERVER=%s", servername);
2402 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2406 setenv("ETPANSERVER", servername, 1);
2408 unsetenv("ETPANSERVER");
2413 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2415 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2421 snprintf(porttext, sizeof(porttext), "%d", port);
2422 setenv("ETPANPORT", porttext, 1);
2425 unsetenv("ETPANPORT");
2429 /* Not a lot we can do if there's an error other than bail. */
2430 if (dup2(fd, 0) == -1)
2432 if (dup2(fd, 1) == -1)
2435 /* Should we close stderr and reopen /dev/null? */
2437 maxopen = sysconf(_SC_OPEN_MAX);
2438 for (i=3; i < maxopen; i++)
2442 /* Detach from the controlling tty if we have one. Otherwise,
2443 SSH might do something stupid like trying to use it instead
2444 of running $SSH_ASKPASS. Doh. */
2445 fd = open("/dev/tty", O_RDONLY);
2447 ioctl(fd, TIOCNOTTY, NULL);
2450 #endif /* TIOCNOTTY */
2452 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2454 /* Eep. Shouldn't reach this */
2458 static int subcommand_connect(const char *command,
2459 const char *servername, uint16_t port)
2461 /* SEB unsupported on Windows */
2465 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2470 do_exec_command(sockfds[1], command, servername, port);
2472 else if (childpid == -1) {
2480 /* Reap child, leaving grandchild process to run */
2481 waitpid(childpid, NULL, 0);
2486 int socket_connect_cmd(mailimap * imap, const char * command,
2487 const char * server, int port)
2493 fd = subcommand_connect(command, server, port);
2495 return MAILIMAP_ERROR_STREAM;
2497 s = mailstream_socket_open(fd);
2500 return MAILIMAP_ERROR_STREAM;
2503 r = mailimap_connect(imap, s);
2504 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2505 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2506 mailstream_close(s);
2515 struct connect_cmd_param {
2517 const char * command;
2518 const char * server;
2522 struct connect_cmd_result {
2526 static void connect_cmd_run(struct etpan_thread_op * op)
2529 struct connect_cmd_param * param;
2530 struct connect_cmd_result * result;
2533 result = op->result;
2535 r = socket_connect_cmd(param->imap, param->command,
2536 param->server, param->port);
2542 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2543 const char * server, int port)
2545 struct connect_cmd_param param;
2546 struct connect_cmd_result result;
2551 imap = mailimap_new(0, NULL);
2554 key.len = sizeof(folder);
2557 chash_set(session_hash, &key, &value, NULL);
2560 param.command = command;
2561 param.server = server;
2564 threaded_run(folder, ¶m, &result, connect_cmd_run);
2566 debug_print("connect_cmd ok %i\n", result.error);
2568 return result.error;
2572 void imap_main_init(void)
2575 void imap_main_done(void)
2578 void imap_main_set_timeout(int sec)