7 #include "imap-thread.h"
11 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
12 #include <sys/socket.h>
20 #include "etpan-thread-manager.h"
22 #include "ssl_certificate.h"
25 #define DISABLE_LOG_DURING_LOGIN
27 static struct etpan_thread_manager * thread_manager = NULL;
28 static chash * courier_workaround_hash = NULL;
29 static chash * imap_hash = NULL;
30 static chash * session_hash = NULL;
31 static guint thread_manager_signal = 0;
32 static GIOChannel * io_channel = NULL;
35 static gboolean thread_manager_event(GIOChannel * source,
36 GIOCondition condition,
39 etpan_thread_manager_loop(thread_manager);
44 static void imap_logger_cmd(int direction, const char * str, size_t size)
51 log_print("IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
55 memset(buf, 0, size+1);
56 strncpy(buf, str, size);
59 if (!strncmp(buf, "<<<<<<<", 7)
60 || !strncmp(buf, ">>>>>>>", 7)) {
64 while (strstr(buf, "\r"))
65 *strstr(buf, "\r") = ' ';
66 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
67 buf[strlen(buf)-1] = '\0';
69 lines = g_strsplit(buf, "\n", -1);
71 while (lines[i] && *lines[i]) {
72 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
79 static void imap_logger_fetch(int direction, const char * str, size_t size)
86 log_print("IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
91 memset(buf, 0, size+1);
92 strncpy(buf, str, size);
94 if (!strncmp(buf, "<<<<<<<", 7)
95 || !strncmp(buf, ">>>>>>>", 7)) {
99 while (strstr(buf, "\r"))
100 *strstr(buf, "\r") = ' ';
101 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
102 buf[strlen(buf)-1] = '\0';
104 lines = g_strsplit(buf, "\n", -1);
106 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
107 while (lines[i] && *lines[i]) {
108 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
112 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
118 static void imap_logger_uid(int direction, const char * str, size_t size)
125 log_print("IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
128 buf = malloc(size+1);
129 memset(buf, 0, size+1);
130 strncpy(buf, str, size);
132 if (!strncmp(buf, "<<<<<<<", 7)
133 || !strncmp(buf, ">>>>>>>", 7)) {
137 while (strstr(buf, "\r"))
138 *strstr(buf, "\r") = ' ';
139 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
140 buf[strlen(buf)-1] = '\0';
142 lines = g_strsplit(buf, "\n", -1);
144 while (lines[i] && *lines[i]) {
145 int llen = strlen(lines[i]);
147 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
150 strncpy2(tmp, lines[i], 63);
151 log_print("IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
160 void imap_logger_append(int direction, const char * str, size_t size)
167 log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
170 buf = malloc(size+1);
171 memset(buf, 0, size+1);
172 strncpy(buf, str, size);
174 if (!strncmp(buf, "<<<<<<<", 7)
175 || !strncmp(buf, ">>>>>>>", 7)) {
179 while (strstr(buf, "\r"))
180 *strstr(buf, "\r") = ' ';
181 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
182 buf[strlen(buf)-1] = '\0';
184 lines = g_strsplit(buf, "\n", -1);
186 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
187 while (lines[i] && *lines[i]) {
188 log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
192 log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
198 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
199 static gboolean etpan_skip_ssl_cert_check = FALSE;
200 extern void mailsasl_ref(void);
202 void imap_main_init(gboolean skip_ssl_cert_check)
204 int fd_thread_manager;
206 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
207 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
208 mailstream_network_delay.tv_usec = 0;
210 mailstream_debug = 1;
211 mailstream_logger = imap_logger_cmd;
214 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
215 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
216 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
218 thread_manager = etpan_thread_manager_new();
220 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
222 io_channel = g_io_channel_unix_new(fd_thread_manager);
224 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
225 thread_manager_event,
230 void imap_main_set_timeout(int sec)
232 mailstream_network_delay.tv_sec = sec;
233 mailstream_network_delay.tv_usec = 0;
236 void imap_main_done(void)
238 etpan_thread_manager_stop(thread_manager);
239 etpan_thread_manager_join(thread_manager);
241 g_source_remove(thread_manager_signal);
242 g_io_channel_unref(io_channel);
244 etpan_thread_manager_free(thread_manager);
246 chash_free(courier_workaround_hash);
247 chash_free(session_hash);
248 chash_free(imap_hash);
251 void imap_init(Folder * folder)
253 struct etpan_thread * thread;
257 thread = etpan_thread_manager_get_thread(thread_manager);
260 key.len = sizeof(folder);
264 chash_set(imap_hash, &key, &value, NULL);
267 void imap_done(Folder * folder)
269 struct etpan_thread * thread;
275 key.len = sizeof(folder);
277 r = chash_get(imap_hash, &key, &value);
283 etpan_thread_unbind(thread);
285 chash_delete(imap_hash, &key, NULL);
287 debug_print("remove thread");
290 static struct etpan_thread * get_thread(Folder * folder)
292 struct etpan_thread * thread;
297 key.len = sizeof(folder);
299 chash_get(imap_hash, &key, &value);
305 static mailimap * get_imap(Folder * folder)
313 key.len = sizeof(folder);
315 r = chash_get(session_hash, &key, &value);
325 static void generic_cb(int cancelled, void * result, void * callback_data)
329 p_finished = callback_data;
331 debug_print("generic_cb\n");
336 static void threaded_run(Folder * folder, void * param, void * result,
337 void (* func)(struct etpan_thread_op * ))
339 struct etpan_thread_op * op;
340 struct etpan_thread * thread;
343 imap_folder_ref(folder);
345 op = etpan_thread_op_new();
351 op->callback = generic_cb;
352 op->callback_data = &finished;
357 thread = get_thread(folder);
358 etpan_thread_op_schedule(thread, op);
361 gtk_main_iteration();
364 etpan_thread_op_free(op);
366 imap_folder_unref(folder);
372 struct connect_param {
378 struct connect_result {
382 #define CHECK_IMAP() { \
383 if (!param->imap) { \
384 result->error = MAILIMAP_ERROR_BAD_STATE; \
389 static void connect_run(struct etpan_thread_op * op)
392 struct connect_param * param;
393 struct connect_result * result;
400 r = mailimap_socket_connect(param->imap,
401 param->server, param->port);
407 int imap_threaded_connect(Folder * folder, const char * server, int port)
409 struct connect_param param;
410 struct connect_result result;
415 imap = mailimap_new(0, NULL);
418 key.len = sizeof(folder);
421 chash_set(session_hash, &key, &value, NULL);
424 param.server = server;
428 threaded_run(folder, ¶m, &result, connect_run);
430 debug_print("connect ok %i\n", result.error);
435 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
438 struct connect_param *param = (struct connect_param *)data;
441 if (certificate == NULL || len < 0) {
442 g_warning("no cert presented.\n");
445 cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
447 g_warning("can't get cert\n");
449 } else if (ssl_certificate_check(cert, NULL,
450 (gchar *)param->server, (gushort)param->port) == TRUE) {
462 static void connect_ssl_run(struct etpan_thread_op * op)
465 struct connect_param * param;
466 struct connect_result * result;
473 r = mailimap_ssl_connect(param->imap,
474 param->server, param->port);
478 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
480 struct connect_param param;
481 struct connect_result result;
485 unsigned char *certificate = NULL;
488 imap = mailimap_new(0, NULL);
491 key.len = sizeof(folder);
494 chash_set(session_hash, &key, &value, NULL);
497 param.server = server;
501 threaded_run(folder, ¶m, &result, connect_ssl_run);
503 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
504 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
505 cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
506 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
511 debug_print("connect %d\n", result.error);
522 struct mailimap_capability_data *caps;
525 static void capability_run(struct etpan_thread_op * op)
528 struct capa_param * param;
529 struct capa_result * result;
530 struct mailimap_capability_data *caps;
537 r = mailimap_capability(param->imap, &caps);
540 result->caps = (r == 0 ? caps : NULL);
544 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
546 struct capa_param param;
547 struct capa_result result;
550 imap = get_imap(folder);
554 threaded_run(folder, ¶m, &result, capability_run);
556 debug_print("capa %d\n", result.error);
565 struct disconnect_param {
569 struct disconnect_result {
573 static void disconnect_run(struct etpan_thread_op * op)
576 struct disconnect_param * param;
577 struct disconnect_result * result;
584 r = mailimap_logout(param->imap);
589 void imap_threaded_disconnect(Folder * folder)
591 struct connect_param param;
592 struct connect_result result;
597 imap = get_imap(folder);
599 debug_print("was disconnected\n");
605 threaded_run(folder, ¶m, &result, disconnect_run);
608 key.len = sizeof(folder);
611 chash_delete(session_hash, &key, NULL);
614 key.len = sizeof(imap);
615 chash_delete(courier_workaround_hash, &key, NULL);
619 debug_print("disconnect ok\n");
626 const char * wildcard;
634 static void list_run(struct etpan_thread_op * op)
636 struct list_param * param;
637 struct list_result * result;
646 r = mailimap_list(param->imap, param->base,
647 param->wildcard, &list);
652 debug_print("imap list run - end\n");
655 int imap_threaded_list(Folder * folder, const char * base,
656 const char * wildcard,
659 struct list_param param;
660 struct list_result result;
662 debug_print("imap list - begin\n");
664 param.imap = get_imap(folder);
666 param.wildcard = wildcard;
668 threaded_run(folder, ¶m, &result, list_run);
670 * p_result = result.list;
672 debug_print("imap list - end %p\n", result.list);
682 const char * password;
687 struct login_result {
691 static void login_run(struct etpan_thread_op * op)
693 struct login_param * param;
694 struct login_result * result;
696 #ifdef DISABLE_LOG_DURING_LOGIN
704 #ifdef DISABLE_LOG_DURING_LOGIN
705 old_debug = mailstream_debug;
706 mailstream_debug = 0;
708 if (!strcmp(param->type, "LOGIN"))
709 r = mailimap_login(param->imap,
710 param->login, param->password);
711 else if (!strcmp(param->type, "GSSAPI"))
712 r = mailimap_authenticate(param->imap,
713 param->type, param->server, NULL, NULL,
714 param->login, param->login,
715 param->password, NULL);
717 r = mailimap_authenticate(param->imap,
718 param->type, NULL, NULL, NULL,
719 param->login, param->login,
720 param->password, NULL);
721 #ifdef DISABLE_LOG_DURING_LOGIN
722 mailstream_debug = old_debug;
727 debug_print("imap login run - end %i\n", r);
730 int imap_threaded_login(Folder * folder,
731 const char * login, const char * password,
734 struct login_param param;
735 struct login_result result;
737 debug_print("imap login - begin\n");
739 param.imap = get_imap(folder);
741 param.password = password;
743 if (folder && folder->account)
744 param.server = folder->account->recv_server;
748 threaded_run(folder, ¶m, &result, login_run);
750 debug_print("imap login - end\n");
756 struct status_param {
759 struct mailimap_status_att_list * status_att_list;
762 struct status_result {
764 struct mailimap_mailbox_data_status * data_status;
767 static void status_run(struct etpan_thread_op * op)
769 struct status_param * param;
770 struct status_result * result;
778 r = mailimap_status(param->imap, param->mb,
779 param->status_att_list,
780 &result->data_status);
783 debug_print("imap status run - end %i\n", r);
786 int imap_threaded_status(Folder * folder, const char * mb,
787 struct mailimap_mailbox_data_status ** data_status,
790 struct status_param param;
791 struct status_result result;
792 struct mailimap_status_att_list * status_att_list;
794 debug_print("imap status - begin\n");
796 status_att_list = mailimap_status_att_list_new_empty();
798 mailimap_status_att_list_add(status_att_list,
799 MAILIMAP_STATUS_ATT_MESSAGES);
802 mailimap_status_att_list_add(status_att_list,
803 MAILIMAP_STATUS_ATT_RECENT);
806 mailimap_status_att_list_add(status_att_list,
807 MAILIMAP_STATUS_ATT_UIDNEXT);
810 mailimap_status_att_list_add(status_att_list,
811 MAILIMAP_STATUS_ATT_UIDVALIDITY);
814 mailimap_status_att_list_add(status_att_list,
815 MAILIMAP_STATUS_ATT_UNSEEN);
817 param.imap = get_imap(folder);
819 param.status_att_list = status_att_list;
821 threaded_run(folder, ¶m, &result, status_run);
823 debug_print("imap status - end\n");
825 * data_status = result.data_status;
827 mailimap_status_att_list_free(status_att_list);
842 static void noop_run(struct etpan_thread_op * op)
844 struct noop_param * param;
845 struct noop_result * result;
852 r = mailimap_noop(param->imap);
856 debug_print("imap noop run - end %i\n", r);
859 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
861 struct noop_param param;
862 struct noop_result result;
865 debug_print("imap noop - begin\n");
867 imap = get_imap(folder);
870 threaded_run(folder, ¶m, &result, noop_run);
872 if (imap->imap_selection_info != NULL) {
873 * p_exists = imap->imap_selection_info->sel_exists;
879 debug_print("imap noop - end\n");
885 struct starttls_result {
889 static void starttls_run(struct etpan_thread_op * op)
891 struct connect_param * param;
892 struct starttls_result * result;
899 r = mailimap_starttls(param->imap);
903 debug_print("imap starttls run - end %i\n", r);
906 mailimap *imap = param->imap;
907 mailstream_low *plain_low = NULL;
908 mailstream_low *tls_low = NULL;
911 plain_low = mailstream_get_low(imap->imap_stream);
912 fd = mailstream_low_get_fd(plain_low);
914 debug_print("imap starttls run - can't get fd\n");
915 result->error = MAILIMAP_ERROR_STREAM;
919 tls_low = mailstream_low_tls_open(fd);
920 if (tls_low == NULL) {
921 debug_print("imap starttls run - can't tls_open\n");
922 result->error = MAILIMAP_ERROR_STREAM;
925 mailstream_low_free(plain_low);
926 mailstream_set_low(imap->imap_stream, tls_low);
930 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
932 struct connect_param param;
933 struct starttls_result result;
935 unsigned char *certificate;
937 debug_print("imap starttls - begin\n");
939 param.imap = get_imap(folder);
943 threaded_run(folder, ¶m, &result, starttls_run);
945 debug_print("imap starttls - end\n");
947 if (result.error == 0 && !etpan_skip_ssl_cert_check) {
948 cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
949 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
950 result.error = MAILIMAP_ERROR_STREAM;
959 struct create_param {
964 struct create_result {
968 static void create_run(struct etpan_thread_op * op)
970 struct create_param * param;
971 struct create_result * result;
978 r = mailimap_create(param->imap, param->mb);
982 debug_print("imap create run - end %i\n", r);
985 int imap_threaded_create(Folder * folder, const char * mb)
987 struct create_param param;
988 struct create_result result;
990 debug_print("imap create - begin\n");
992 param.imap = get_imap(folder);
995 threaded_run(folder, ¶m, &result, create_run);
997 debug_print("imap create - end\n");
1005 struct rename_param {
1008 const char * new_name;
1011 struct rename_result {
1015 static void rename_run(struct etpan_thread_op * op)
1017 struct rename_param * param;
1018 struct rename_result * result;
1025 r = mailimap_rename(param->imap, param->mb, param->new_name);
1027 result = op->result;
1029 debug_print("imap rename run - end %i\n", r);
1032 int imap_threaded_rename(Folder * folder,
1033 const char * mb, const char * new_name)
1035 struct rename_param param;
1036 struct rename_result result;
1038 debug_print("imap rename - begin\n");
1040 param.imap = get_imap(folder);
1042 param.new_name = new_name;
1044 threaded_run(folder, ¶m, &result, rename_run);
1046 debug_print("imap rename - end\n");
1048 return result.error;
1054 struct delete_param {
1059 struct delete_result {
1063 static void delete_run(struct etpan_thread_op * op)
1065 struct delete_param * param;
1066 struct delete_result * result;
1073 r = mailimap_delete(param->imap, param->mb);
1075 result = op->result;
1077 debug_print("imap delete run - end %i\n", r);
1080 int imap_threaded_delete(Folder * folder, const char * mb)
1082 struct delete_param param;
1083 struct delete_result result;
1085 debug_print("imap delete - begin\n");
1087 param.imap = get_imap(folder);
1090 threaded_run(folder, ¶m, &result, delete_run);
1092 debug_print("imap delete - end\n");
1094 return result.error;
1099 struct select_param {
1104 struct select_result {
1108 static void select_run(struct etpan_thread_op * op)
1110 struct select_param * param;
1111 struct select_result * result;
1118 r = mailimap_select(param->imap, param->mb);
1120 result = op->result;
1122 debug_print("imap select run - end %i\n", r);
1125 int imap_threaded_select(Folder * folder, const char * mb,
1126 gint * exists, gint * recent, gint * unseen,
1127 guint32 * uid_validity)
1129 struct select_param param;
1130 struct select_result result;
1133 debug_print("imap select - begin\n");
1135 imap = get_imap(folder);
1139 threaded_run(folder, ¶m, &result, select_run);
1141 if (result.error != MAILIMAP_NO_ERROR)
1142 return result.error;
1144 if (imap->imap_selection_info == NULL)
1145 return MAILIMAP_ERROR_PARSE;
1147 * exists = imap->imap_selection_info->sel_exists;
1148 * recent = imap->imap_selection_info->sel_recent;
1149 * unseen = imap->imap_selection_info->sel_unseen;
1150 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1152 debug_print("imap select - end\n");
1154 return result.error;
1159 struct examine_param {
1164 struct examine_result {
1168 static void examine_run(struct etpan_thread_op * op)
1170 struct examine_param * param;
1171 struct examine_result * result;
1178 r = mailimap_examine(param->imap, param->mb);
1180 result = op->result;
1182 debug_print("imap examine run - end %i\n", r);
1185 int imap_threaded_examine(Folder * folder, const char * mb,
1186 gint * exists, gint * recent, gint * unseen,
1187 guint32 * uid_validity)
1189 struct examine_param param;
1190 struct examine_result result;
1193 debug_print("imap examine - begin\n");
1195 imap = get_imap(folder);
1199 threaded_run(folder, ¶m, &result, examine_run);
1201 if (result.error != MAILIMAP_NO_ERROR)
1202 return result.error;
1204 if (imap->imap_selection_info == NULL)
1205 return MAILIMAP_ERROR_PARSE;
1207 * exists = imap->imap_selection_info->sel_exists;
1208 * recent = imap->imap_selection_info->sel_recent;
1209 * unseen = imap->imap_selection_info->sel_unseen;
1210 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1212 debug_print("imap examine - end\n");
1214 return result.error;
1220 struct search_param {
1223 struct mailimap_set * set;
1226 struct search_result {
1228 clist * search_result;
1231 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1233 return mailimap_set_item_new(orig->set_first, orig->set_last);
1236 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1238 clist *list = orig ? orig->set_list : NULL;
1239 clist *newlist = clist_new();
1244 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1245 clist_append(newlist,
1246 sc_mailimap_set_item_copy(
1247 (struct mailimap_set_item *)clist_content(cur)));
1248 return mailimap_set_new(newlist);
1251 static void search_run(struct etpan_thread_op * op)
1253 struct search_param * param;
1254 struct search_result * result;
1256 struct mailimap_search_key * key = NULL;
1257 struct mailimap_search_key * uid_key = NULL;
1258 struct mailimap_search_key * search_type_key;
1259 clist * search_result;
1265 /* we copy the mailimap_set because freeing the key is recursive */
1266 if (param->set != NULL) {
1267 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1268 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1269 uid_key = mailimap_search_key_new_all();
1271 search_type_key = NULL;
1272 switch (param->type) {
1273 case IMAP_SEARCH_TYPE_SIMPLE:
1274 search_type_key = NULL;
1277 case IMAP_SEARCH_TYPE_SEEN:
1278 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1279 NULL, NULL, NULL, NULL, NULL,
1280 NULL, NULL, NULL, NULL, NULL,
1281 NULL, NULL, NULL, NULL, 0,
1282 NULL, NULL, NULL, NULL, NULL,
1283 NULL, 0, NULL, NULL, NULL);
1286 case IMAP_SEARCH_TYPE_UNSEEN:
1287 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1288 NULL, NULL, NULL, NULL, NULL,
1289 NULL, NULL, NULL, NULL, NULL,
1290 NULL, NULL, NULL, NULL, 0,
1291 NULL, NULL, NULL, NULL, NULL,
1292 NULL, 0, NULL, NULL, NULL);
1295 case IMAP_SEARCH_TYPE_ANSWERED:
1296 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1297 NULL, NULL, NULL, NULL, NULL,
1298 NULL, NULL, NULL, NULL, NULL,
1299 NULL, NULL, NULL, NULL, 0,
1300 NULL, NULL, NULL, NULL, NULL,
1301 NULL, 0, NULL, NULL, NULL);
1304 case IMAP_SEARCH_TYPE_FLAGGED:
1305 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1306 NULL, NULL, NULL, NULL, NULL,
1307 NULL, NULL, NULL, NULL, NULL,
1308 NULL, NULL, NULL, NULL, 0,
1309 NULL, NULL, NULL, NULL, NULL,
1310 NULL, 0, NULL, NULL, NULL);
1312 case IMAP_SEARCH_TYPE_DELETED:
1313 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1314 NULL, NULL, NULL, NULL, NULL,
1315 NULL, NULL, NULL, NULL, NULL,
1316 NULL, NULL, NULL, NULL, 0,
1317 NULL, NULL, NULL, NULL, NULL,
1318 NULL, 0, NULL, NULL, NULL);
1322 if (search_type_key != NULL) {
1323 if (param->set != NULL) {
1324 key = mailimap_search_key_new_multiple_empty();
1325 mailimap_search_key_multiple_add(key, search_type_key);
1326 mailimap_search_key_multiple_add(key, uid_key);
1328 key = search_type_key;
1330 } else if (uid_key != NULL) {
1335 g_warning("no key!");
1336 result = op->result;
1338 result->search_result = NULL;
1340 mailstream_logger = imap_logger_uid;
1342 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1344 mailstream_logger = imap_logger_cmd;
1346 /* free the key (with the imapset) */
1347 mailimap_search_key_free(key);
1349 result = op->result;
1351 result->search_result = search_result;
1353 debug_print("imap search run - end %i\n", result->error);
1356 int imap_threaded_search(Folder * folder, int search_type,
1357 struct mailimap_set * set, clist ** search_result)
1359 struct search_param param;
1360 struct search_result result;
1363 debug_print("imap search - begin\n");
1365 imap = get_imap(folder);
1368 param.type = search_type;
1370 threaded_run(folder, ¶m, &result, search_run);
1372 if (result.error != MAILIMAP_NO_ERROR)
1373 return result.error;
1375 debug_print("imap search - end\n");
1377 * search_result = result.search_result;
1379 return result.error;
1386 uid_list_to_env_list(clist * fetch_result, carray ** result)
1394 tab = carray_new(128);
1396 res = MAILIMAP_ERROR_MEMORY;
1400 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1401 cur = clist_next(cur)) {
1402 struct mailimap_msg_att * msg_att;
1403 clistiter * item_cur;
1408 msg_att = clist_content(cur);
1412 for(item_cur = clist_begin(msg_att->att_list) ;
1414 item_cur = clist_next(item_cur)) {
1415 struct mailimap_msg_att_item * item;
1417 item = clist_content(item_cur);
1419 switch (item->att_type) {
1420 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1421 switch (item->att_data.att_static->att_type) {
1422 case MAILIMAP_MSG_ATT_UID:
1423 uid = item->att_data.att_static->att_data.att_uid;
1429 puid = malloc(sizeof(* puid));
1431 res = MAILIMAP_ERROR_MEMORY;
1436 r = carray_add(tab, puid, NULL);
1439 res = MAILIMAP_ERROR_MEMORY;
1446 return MAILIMAP_NO_ERROR;
1449 for(i = 0 ; i < carray_count(tab) ; i++)
1450 mailmessage_free(carray_get(tab, i));
1455 static int imap_get_messages_list(mailimap * imap,
1456 uint32_t first_index,
1461 struct mailimap_fetch_att * fetch_att;
1462 struct mailimap_fetch_type * fetch_type;
1463 struct mailimap_set * set;
1464 clist * fetch_result;
1467 set = mailimap_set_new_interval(first_index, 0);
1469 res = MAILIMAP_ERROR_MEMORY;
1473 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1474 if (fetch_type == NULL) {
1475 res = MAILIMAP_ERROR_MEMORY;
1479 fetch_att = mailimap_fetch_att_new_uid();
1480 if (fetch_att == NULL) {
1481 res = MAILIMAP_ERROR_MEMORY;
1482 goto free_fetch_type;
1485 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1486 if (r != MAILIMAP_NO_ERROR) {
1487 mailimap_fetch_att_free(fetch_att);
1488 res = MAILIMAP_ERROR_MEMORY;
1489 goto free_fetch_type;
1492 r = mailimap_uid_fetch(imap, set,
1493 fetch_type, &fetch_result);
1495 mailimap_fetch_type_free(fetch_type);
1496 mailimap_set_free(set);
1498 if (r != MAILIMAP_NO_ERROR) {
1504 r = uid_list_to_env_list(fetch_result, &env_list);
1505 mailimap_fetch_list_free(fetch_result);
1507 * result = env_list;
1509 return MAILIMAP_NO_ERROR;
1512 mailimap_fetch_type_free(fetch_type);
1514 mailimap_set_free(set);
1522 struct fetch_uid_param {
1524 uint32_t first_index;
1527 struct fetch_uid_result {
1529 carray * fetch_result;
1532 static void fetch_uid_run(struct etpan_thread_op * op)
1534 struct fetch_uid_param * param;
1535 struct fetch_uid_result * result;
1536 carray * fetch_result;
1543 fetch_result = NULL;
1544 r = imap_get_messages_list(param->imap, param->first_index,
1547 result = op->result;
1549 result->fetch_result = fetch_result;
1550 debug_print("imap fetch_uid run - end %i\n", r);
1553 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1554 carray ** fetch_result)
1556 struct fetch_uid_param param;
1557 struct fetch_uid_result result;
1560 debug_print("imap fetch_uid - begin\n");
1562 imap = get_imap(folder);
1564 param.first_index = first_index;
1566 threaded_run(folder, ¶m, &result, fetch_uid_run);
1568 if (result.error != MAILIMAP_NO_ERROR)
1569 return result.error;
1571 debug_print("imap fetch_uid - end\n");
1573 * fetch_result = result.fetch_result;
1575 return result.error;
1579 void imap_fetch_uid_list_free(carray * uid_list)
1583 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1586 puid = carray_get(uid_list, i);
1589 carray_free(uid_list);
1594 static int imap_fetch(mailimap * imap,
1597 size_t * result_len)
1600 struct mailimap_set * set;
1601 struct mailimap_fetch_att * fetch_att;
1602 struct mailimap_fetch_type * fetch_type;
1603 clist * fetch_result;
1604 struct mailimap_msg_att * msg_att;
1605 struct mailimap_msg_att_item * msg_att_item;
1610 struct mailimap_section * section;
1612 set = mailimap_set_new_single(msg_index);
1614 res = MAILIMAP_ERROR_MEMORY;
1618 section = mailimap_section_new(NULL);
1619 if (section == NULL) {
1620 res = MAILIMAP_ERROR_MEMORY;
1624 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1625 if (fetch_att == NULL) {
1626 mailimap_section_free(section);
1627 res = MAILIMAP_ERROR_MEMORY;
1631 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1632 if (fetch_type == NULL) {
1633 res = MAILIMAP_ERROR_MEMORY;
1634 goto free_fetch_att;
1637 mailstream_logger = imap_logger_fetch;
1639 r = mailimap_uid_fetch(imap, set,
1640 fetch_type, &fetch_result);
1642 mailstream_logger = imap_logger_cmd;
1644 mailimap_fetch_type_free(fetch_type);
1645 mailimap_set_free(set);
1648 case MAILIMAP_NO_ERROR:
1654 if (clist_begin(fetch_result) == NULL) {
1655 mailimap_fetch_list_free(fetch_result);
1656 return MAILIMAP_ERROR_FETCH;
1659 msg_att = clist_begin(fetch_result)->data;
1664 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1665 cur = clist_next(cur)) {
1666 msg_att_item = clist_content(cur);
1668 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1669 if (msg_att_item->att_data.att_static->att_type ==
1670 MAILIMAP_MSG_ATT_BODY_SECTION) {
1671 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1673 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1675 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1680 mailimap_fetch_list_free(fetch_result);
1683 return MAILIMAP_ERROR_FETCH;
1686 * result_len = text_length;
1688 return MAILIMAP_NO_ERROR;
1691 mailimap_fetch_att_free(fetch_att);
1693 mailimap_set_free(set);
1698 static int imap_fetch_header(mailimap * imap,
1701 size_t * result_len)
1704 struct mailimap_set * set;
1705 struct mailimap_fetch_att * fetch_att;
1706 struct mailimap_fetch_type * fetch_type;
1707 clist * fetch_result;
1708 struct mailimap_msg_att * msg_att;
1709 struct mailimap_msg_att_item * msg_att_item;
1714 struct mailimap_section * section;
1716 set = mailimap_set_new_single(msg_index);
1718 res = MAILIMAP_ERROR_MEMORY;
1722 section = mailimap_section_new_header();
1723 if (section == NULL) {
1724 res = MAILIMAP_ERROR_MEMORY;
1728 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1729 if (fetch_att == NULL) {
1730 mailimap_section_free(section);
1731 res = MAILIMAP_ERROR_MEMORY;
1735 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1736 if (fetch_type == NULL) {
1737 res = MAILIMAP_ERROR_MEMORY;
1738 goto free_fetch_att;
1741 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1743 mailimap_fetch_type_free(fetch_type);
1744 mailimap_set_free(set);
1747 case MAILIMAP_NO_ERROR:
1753 if (clist_begin(fetch_result) == NULL) {
1754 mailimap_fetch_list_free(fetch_result);
1755 return MAILIMAP_ERROR_FETCH;
1758 msg_att = clist_begin(fetch_result)->data;
1763 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1764 cur = clist_next(cur)) {
1765 msg_att_item = clist_content(cur);
1767 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1768 if (msg_att_item->att_data.att_static->att_type ==
1769 MAILIMAP_MSG_ATT_BODY_SECTION) {
1770 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1771 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1773 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1778 mailimap_fetch_list_free(fetch_result);
1781 return MAILIMAP_ERROR_FETCH;
1784 * result_len = text_length;
1786 return MAILIMAP_NO_ERROR;
1789 mailimap_fetch_att_free(fetch_att);
1791 mailimap_set_free(set);
1798 struct fetch_content_param {
1801 const char * filename;
1805 struct fetch_content_result {
1809 static void fetch_content_run(struct etpan_thread_op * op)
1811 struct fetch_content_param * param;
1812 struct fetch_content_result * result;
1814 size_t content_size;
1825 if (param->with_body)
1826 r = imap_fetch(param->imap, param->msg_index,
1827 &content, &content_size);
1829 r = imap_fetch_header(param->imap, param->msg_index,
1830 &content, &content_size);
1832 result = op->result;
1835 if (r == MAILIMAP_NO_ERROR) {
1836 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1838 result->error = MAILIMAP_ERROR_FETCH;
1842 f = fdopen(fd, "wb");
1844 result->error = MAILIMAP_ERROR_FETCH;
1848 r = fwrite(content, 1, content_size, f);
1855 g_unlink(param->filename);
1866 g_unlink(param->filename);
1869 if (mmap_string_unref(content) != 0)
1873 debug_print("imap fetch_content run - end %i\n", r);
1876 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1878 const char * filename)
1880 struct fetch_content_param param;
1881 struct fetch_content_result result;
1884 debug_print("imap fetch_content - begin\n");
1886 imap = get_imap(folder);
1888 param.msg_index = msg_index;
1889 param.filename = filename;
1890 param.with_body = with_body;
1892 threaded_run(folder, ¶m, &result, fetch_content_run);
1894 if (result.error != MAILIMAP_NO_ERROR)
1895 return result.error;
1897 debug_print("imap fetch_content - end\n");
1899 return result.error;
1904 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1912 flag_list = att_dyn->att_list;
1913 if (flag_list == NULL)
1916 for(cur = clist_begin(flag_list) ; cur != NULL ;
1917 cur = clist_next(cur)) {
1918 struct mailimap_flag_fetch * flag_fetch;
1920 flag_fetch = clist_content(cur);
1921 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1924 switch (flag_fetch->fl_flag->fl_type) {
1925 case MAILIMAP_FLAG_ANSWERED:
1926 flags |= MSG_REPLIED;
1928 case MAILIMAP_FLAG_FLAGGED:
1929 flags |= MSG_MARKED;
1931 case MAILIMAP_FLAG_DELETED:
1932 flags |= MSG_DELETED;
1934 case MAILIMAP_FLAG_SEEN:
1935 flags &= ~MSG_UNREAD;
1945 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1949 struct mailimap_msg_att_dynamic ** patt_dyn)
1951 clistiter * item_cur;
1955 struct mailimap_msg_att_dynamic * att_dyn;
1962 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1963 item_cur = clist_next(item_cur)) {
1964 struct mailimap_msg_att_item * item;
1966 item = clist_content(item_cur);
1968 switch (item->att_type) {
1969 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1970 switch (item->att_data.att_static->att_type) {
1971 case MAILIMAP_MSG_ATT_UID:
1972 uid = item->att_data.att_static->att_data.att_uid;
1975 case MAILIMAP_MSG_ATT_BODY_SECTION:
1976 if (headers == NULL) {
1977 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1980 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1981 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1986 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1987 if (att_dyn == NULL) {
1988 att_dyn = item->att_data.att_dyn;
1996 if (pheaders != NULL)
1997 * pheaders = headers;
1998 if (pref_size != NULL)
1999 * pref_size = ref_size;
2000 if (patt_dyn != NULL)
2001 * patt_dyn = att_dyn;
2003 return MAIL_NO_ERROR;
2006 static struct imap_fetch_env_info *
2007 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2009 struct imap_fetch_env_info * info;
2013 struct mailimap_msg_att_dynamic * att_dyn;
2015 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2020 info = malloc(sizeof(* info));
2022 info->headers = strdup(headers);
2024 info->flags = imap_flags_to_flags(att_dyn);
2030 imap_fetch_result_to_envelop_list(clist * fetch_result,
2031 carray ** p_env_list)
2038 env_list = carray_new(16);
2040 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2041 cur = clist_next(cur)) {
2042 struct mailimap_msg_att * msg_att;
2043 struct imap_fetch_env_info * env_info;
2045 msg_att = clist_content(cur);
2047 env_info = fetch_to_env_info(msg_att);
2049 return MAILIMAP_ERROR_MEMORY;
2050 carray_add(env_list, env_info, NULL);
2053 * p_env_list = env_list;
2055 return MAIL_NO_ERROR;
2058 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2060 struct mailimap_fetch_att * fetch_att;
2064 struct mailimap_header_list * imap_hdrlist;
2065 struct mailimap_section * section;
2067 hdrlist = clist_new();
2069 header = strdup("Date");
2070 r = clist_append(hdrlist, header);
2071 header = strdup("From");
2072 r = clist_append(hdrlist, header);
2073 header = strdup("To");
2074 r = clist_append(hdrlist, header);
2075 header = strdup("Cc");
2076 r = clist_append(hdrlist, header);
2077 header = strdup("Subject");
2078 r = clist_append(hdrlist, header);
2079 header = strdup("Message-ID");
2080 r = clist_append(hdrlist, header);
2081 header = strdup("References");
2082 r = clist_append(hdrlist, header);
2083 header = strdup("In-Reply-To");
2084 r = clist_append(hdrlist, header);
2086 imap_hdrlist = mailimap_header_list_new(hdrlist);
2087 section = mailimap_section_new_header_fields(imap_hdrlist);
2088 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2089 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2091 return MAIL_NO_ERROR;
2094 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2096 struct mailimap_fetch_att * fetch_att;
2097 struct mailimap_section * section;
2099 section = mailimap_section_new_header();
2100 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2101 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2103 return MAIL_NO_ERROR;
2107 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2108 carray ** p_env_list)
2110 struct mailimap_fetch_att * fetch_att;
2111 struct mailimap_fetch_type * fetch_type;
2113 clist * fetch_result;
2115 carray * env_list = NULL;
2119 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2122 fetch_att = mailimap_fetch_att_new_uid();
2123 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2126 fetch_att = mailimap_fetch_att_new_flags();
2127 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2130 fetch_att = mailimap_fetch_att_new_rfc822_size();
2131 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2135 key.len = sizeof(imap);
2136 r = chash_get(courier_workaround_hash, &key, &value);
2138 r = imap_add_envelope_fetch_att(fetch_type);
2140 r = imap_add_header_fetch_att(fetch_type);
2142 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2145 case MAILIMAP_NO_ERROR:
2148 mailimap_fetch_type_free(fetch_type);
2149 debug_print("uid_fetch: %d\n", r);
2153 if (clist_begin(fetch_result) == NULL) {
2154 res = MAILIMAP_ERROR_FETCH;
2155 debug_print("clist_begin = NULL\n");
2159 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2160 mailimap_fetch_list_free(fetch_result);
2162 if (r != MAILIMAP_NO_ERROR) {
2163 mailimap_fetch_type_free(fetch_type);
2164 res = MAILIMAP_ERROR_MEMORY;
2165 debug_print("fetch_result_to_envelop_list: %d\n", res);
2169 mailimap_fetch_type_free(fetch_type);
2171 * p_env_list = env_list;
2173 return MAILIMAP_NO_ERROR;
2179 struct fetch_env_param {
2181 struct mailimap_set * set;
2184 struct fetch_env_result {
2185 carray * fetch_env_result;
2189 static void fetch_env_run(struct etpan_thread_op * op)
2191 struct fetch_env_param * param;
2192 struct fetch_env_result * result;
2201 r = imap_get_envelopes_list(param->imap, param->set,
2204 result = op->result;
2206 result->fetch_env_result = env_list;
2208 debug_print("imap fetch_env run - end %i\n", r);
2211 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2212 carray ** p_env_list)
2214 struct fetch_env_param param;
2215 struct fetch_env_result result;
2218 debug_print("imap fetch_env - begin\n");
2220 imap = get_imap(folder);
2224 threaded_run(folder, ¶m, &result, fetch_env_run);
2226 if (result.error != MAILIMAP_NO_ERROR) {
2232 key.len = sizeof(imap);
2233 r = chash_get(courier_workaround_hash, &key, &value);
2237 chash_set(courier_workaround_hash, &key, &value, NULL);
2239 threaded_run(folder, ¶m, &result, fetch_env_run);
2243 if (result.error != MAILIMAP_NO_ERROR)
2244 return result.error;
2246 debug_print("imap fetch_env - end\n");
2248 * p_env_list = result.fetch_env_result;
2250 return result.error;
2253 void imap_fetch_env_free(carray * env_list)
2257 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2258 struct imap_fetch_env_info * env_info;
2260 env_info = carray_get(env_list, i);
2261 free(env_info->headers);
2264 carray_free(env_list);
2271 struct append_param {
2273 const char * mailbox;
2274 const char * filename;
2275 struct mailimap_flag_list * flag_list;
2278 struct append_result {
2283 static void append_run(struct etpan_thread_op * op)
2285 struct append_param * param;
2286 struct append_result * result;
2290 struct stat stat_buf;
2292 guint32 uid = 0, val = 0;
2295 result = op->result;
2299 r = stat(param->filename, &stat_buf);
2301 result->error = MAILIMAP_ERROR_APPEND;
2304 size = stat_buf.st_size;
2306 fd = open(param->filename, O_RDONLY);
2308 result->error = MAILIMAP_ERROR_APPEND;
2312 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2313 if (data == (void *) MAP_FAILED) {
2315 result->error = MAILIMAP_ERROR_APPEND;
2319 mailstream_logger = imap_logger_append;
2321 r = mailimap_uidplus_append(param->imap, param->mailbox,
2322 param->flag_list, NULL,
2323 data, size, &val, &uid);
2325 mailstream_logger = imap_logger_cmd;
2332 debug_print("imap append run - end %i uid %d\n", r, uid);
2335 int imap_threaded_append(Folder * folder, const char * mailbox,
2336 const char * filename,
2337 struct mailimap_flag_list * flag_list,
2340 struct append_param param;
2341 struct append_result result;
2344 debug_print("imap append - begin\n");
2346 imap = get_imap(folder);
2348 param.mailbox = mailbox;
2349 param.filename = filename;
2350 param.flag_list = flag_list;
2352 threaded_run(folder, ¶m, &result, append_run);
2354 if (result.error != MAILIMAP_NO_ERROR)
2355 return result.error;
2357 debug_print("imap append - end\n");
2361 return result.error;
2367 struct expunge_param {
2371 struct expunge_result {
2375 static void expunge_run(struct etpan_thread_op * op)
2377 struct expunge_param * param;
2378 struct expunge_result * result;
2385 r = mailimap_expunge(param->imap);
2387 result = op->result;
2389 debug_print("imap expunge run - end %i\n", r);
2392 int imap_threaded_expunge(Folder * folder)
2394 struct expunge_param param;
2395 struct expunge_result result;
2397 debug_print("imap expunge - begin\n");
2399 param.imap = get_imap(folder);
2401 threaded_run(folder, ¶m, &result, expunge_run);
2403 debug_print("imap expunge - end\n");
2405 return result.error;
2411 struct mailimap_set * set;
2415 struct copy_result {
2417 struct mailimap_set *source;
2418 struct mailimap_set *dest;
2421 static void copy_run(struct etpan_thread_op * op)
2423 struct copy_param * param;
2424 struct copy_result * result;
2427 struct mailimap_set *source = NULL, *dest = NULL;
2433 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2434 &val, &source, &dest);
2436 result = op->result;
2439 result->source = source;
2440 result->dest = dest;
2442 result->source = NULL;
2443 result->dest = NULL;
2445 debug_print("imap copy run - end %i\n", r);
2448 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2449 const char * mb, struct mailimap_set **source,
2450 struct mailimap_set **dest)
2452 struct copy_param param;
2453 struct copy_result result;
2456 debug_print("imap copy - begin\n");
2458 imap = get_imap(folder);
2463 threaded_run(folder, ¶m, &result, copy_run);
2467 if (result.error != MAILIMAP_NO_ERROR)
2468 return result.error;
2470 *source = result.source;
2471 *dest = result.dest;
2473 debug_print("imap copy - end\n");
2475 return result.error;
2480 struct store_param {
2482 struct mailimap_set * set;
2483 struct mailimap_store_att_flags * store_att_flags;
2486 struct store_result {
2490 static void store_run(struct etpan_thread_op * op)
2492 struct store_param * param;
2493 struct store_result * result;
2500 r = mailimap_uid_store(param->imap, param->set,
2501 param->store_att_flags);
2503 result = op->result;
2506 debug_print("imap store run - end %i\n", r);
2509 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2510 struct mailimap_store_att_flags * store_att_flags)
2512 struct store_param param;
2513 struct store_result result;
2516 debug_print("imap store - begin\n");
2518 imap = get_imap(folder);
2521 param.store_att_flags = store_att_flags;
2523 threaded_run(folder, ¶m, &result, store_run);
2525 if (result.error != MAILIMAP_NO_ERROR)
2526 return result.error;
2528 debug_print("imap store - end\n");
2530 return result.error;
2534 #define ENV_BUFFER_SIZE 512
2536 static void do_exec_command(int fd, const char * command,
2537 const char * servername, uint16_t port)
2541 char env_buffer[ENV_BUFFER_SIZE];
2545 /* Fork again to become a child of init rather than
2546 the etpan client. */
2552 snprintf(env_buffer, ENV_BUFFER_SIZE,
2553 "ETPANSERVER=%s", servername);
2555 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2559 setenv("ETPANSERVER", servername, 1);
2561 unsetenv("ETPANSERVER");
2566 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2568 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2574 snprintf(porttext, sizeof(porttext), "%d", port);
2575 setenv("ETPANPORT", porttext, 1);
2578 unsetenv("ETPANPORT");
2582 /* Not a lot we can do if there's an error other than bail. */
2583 if (dup2(fd, 0) == -1)
2585 if (dup2(fd, 1) == -1)
2588 /* Should we close stderr and reopen /dev/null? */
2590 maxopen = sysconf(_SC_OPEN_MAX);
2591 for (i=3; i < maxopen; i++)
2595 /* Detach from the controlling tty if we have one. Otherwise,
2596 SSH might do something stupid like trying to use it instead
2597 of running $SSH_ASKPASS. Doh. */
2598 fd = open("/dev/tty", O_RDONLY);
2600 ioctl(fd, TIOCNOTTY, NULL);
2603 #endif /* TIOCNOTTY */
2605 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2607 /* Eep. Shouldn't reach this */
2611 static int subcommand_connect(const char *command,
2612 const char *servername, uint16_t port)
2614 /* SEB unsupported on Windows */
2618 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2623 do_exec_command(sockfds[1], command, servername, port);
2625 else if (childpid == -1) {
2633 /* Reap child, leaving grandchild process to run */
2634 waitpid(childpid, NULL, 0);
2639 int socket_connect_cmd(mailimap * imap, const char * command,
2640 const char * server, int port)
2646 fd = subcommand_connect(command, server, port);
2648 return MAILIMAP_ERROR_STREAM;
2650 s = mailstream_socket_open(fd);
2653 return MAILIMAP_ERROR_STREAM;
2656 r = mailimap_connect(imap, s);
2657 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2658 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2659 mailstream_close(s);
2668 struct connect_cmd_param {
2670 const char * command;
2671 const char * server;
2675 struct connect_cmd_result {
2679 static void connect_cmd_run(struct etpan_thread_op * op)
2682 struct connect_cmd_param * param;
2683 struct connect_cmd_result * result;
2686 result = op->result;
2690 r = socket_connect_cmd(param->imap, param->command,
2691 param->server, param->port);
2697 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2698 const char * server, int port)
2700 struct connect_cmd_param param;
2701 struct connect_cmd_result result;
2706 imap = mailimap_new(0, NULL);
2709 key.len = sizeof(folder);
2712 chash_set(session_hash, &key, &value, NULL);
2715 param.command = command;
2716 param.server = server;
2719 threaded_run(folder, ¶m, &result, connect_cmd_run);
2721 debug_print("connect_cmd ok %i\n", result.error);
2723 return result.error;
2727 void imap_main_init(void)
2730 void imap_main_done(void)
2733 void imap_main_set_timeout(int sec)