7 #include "imap-thread.h"
11 #if (defined (__NetBSD__) || defined (__FreeBSD__))
12 #include <sys/socket.h>
20 #include "etpan-thread-manager.h"
23 #define DISABLE_LOG_DURING_LOGIN
25 static struct etpan_thread_manager * thread_manager = NULL;
26 static chash * courier_workaround_hash = NULL;
27 static chash * imap_hash = NULL;
28 static chash * session_hash = NULL;
29 static guint thread_manager_signal = 0;
30 static GIOChannel * io_channel = NULL;
33 static gboolean thread_manager_event(GIOChannel * source,
34 GIOCondition condition,
37 etpan_thread_manager_loop(thread_manager);
42 void imap_logger(int direction, const char * str, size_t size)
47 strncpy(buf, str, size > 510 ? 510:size);
51 if (!strncmp(buf, "<<<<<<<", 7)
52 || !strncmp(buf, ">>>>>>>", 7)
53 || buf[0] == '\r' || buf[0] == '\n')
56 while (strstr(buf, "\r"))
57 *strstr(buf, "\r") = ' ';
58 while (strstr(buf, "\n"))
59 *strstr(buf, "\n") = ' ';
61 log_print("IMAP4%c %s\n", direction?'>':'<', buf);
64 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
66 void imap_main_init(void)
68 int fd_thread_manager;
70 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
71 mailstream_network_delay.tv_usec = 0;
74 mailstream_logger = imap_logger;
76 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
77 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
78 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
80 thread_manager = etpan_thread_manager_new();
82 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
84 io_channel = g_io_channel_unix_new(fd_thread_manager);
86 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
92 void imap_main_set_timeout(int sec)
94 mailstream_network_delay.tv_sec = sec;
95 mailstream_network_delay.tv_usec = 0;
98 void imap_main_done(void)
100 etpan_thread_manager_stop(thread_manager);
101 etpan_thread_manager_join(thread_manager);
103 g_source_remove(thread_manager_signal);
104 g_io_channel_unref(io_channel);
106 etpan_thread_manager_free(thread_manager);
108 chash_free(courier_workaround_hash);
109 chash_free(session_hash);
110 chash_free(imap_hash);
113 void imap_init(Folder * folder)
115 struct etpan_thread * thread;
119 thread = etpan_thread_manager_get_thread(thread_manager);
122 key.len = sizeof(folder);
126 chash_set(imap_hash, &key, &value, NULL);
129 void imap_done(Folder * folder)
131 struct etpan_thread * thread;
137 key.len = sizeof(folder);
139 r = chash_get(imap_hash, &key, &value);
145 etpan_thread_unbind(thread);
147 chash_delete(imap_hash, &key, NULL);
149 debug_print("remove thread");
152 static struct etpan_thread * get_thread(Folder * folder)
154 struct etpan_thread * thread;
159 key.len = sizeof(folder);
161 chash_get(imap_hash, &key, &value);
167 static mailimap * get_imap(Folder * folder)
175 key.len = sizeof(folder);
177 r = chash_get(session_hash, &key, &value);
187 static void generic_cb(int cancelled, void * result, void * callback_data)
191 p_finished = callback_data;
193 debug_print("generic_cb\n");
198 static void threaded_run(Folder * folder, void * param, void * result,
199 void (* func)(struct etpan_thread_op * ))
201 struct etpan_thread_op * op;
202 struct etpan_thread * thread;
205 imap_folder_ref(folder);
207 op = etpan_thread_op_new();
213 op->callback = generic_cb;
214 op->callback_data = &finished;
219 thread = get_thread(folder);
220 etpan_thread_op_schedule(thread, op);
223 gtk_main_iteration();
226 etpan_thread_op_free(op);
228 imap_folder_unref(folder);
234 struct connect_param {
240 struct connect_result {
244 static void connect_run(struct etpan_thread_op * op)
247 struct connect_param * param;
248 struct connect_result * result;
253 r = mailimap_socket_connect(param->imap,
254 param->server, param->port);
260 int imap_threaded_connect(Folder * folder, const char * server, int port)
262 struct connect_param param;
263 struct connect_result result;
268 imap = mailimap_new(0, NULL);
271 key.len = sizeof(folder);
274 chash_set(session_hash, &key, &value, NULL);
277 param.server = server;
280 threaded_run(folder, ¶m, &result, connect_run);
282 debug_print("connect ok %i\n", result.error);
288 static void connect_ssl_run(struct etpan_thread_op * op)
291 struct connect_param * param;
292 struct connect_result * result;
297 r = mailimap_ssl_connect(param->imap,
298 param->server, param->port);
303 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
305 struct connect_param param;
306 struct connect_result result;
311 imap = mailimap_new(0, NULL);
314 key.len = sizeof(folder);
317 chash_set(session_hash, &key, &value, NULL);
320 param.server = server;
323 threaded_run(folder, ¶m, &result, connect_ssl_run);
325 debug_print("connect %d\n", result.error);
336 struct mailimap_capability_data *caps;
339 static void capability_run(struct etpan_thread_op * op)
342 struct capa_param * param;
343 struct capa_result * result;
344 struct mailimap_capability_data *caps;
349 r = mailimap_capability(param->imap, &caps);
352 result->caps = (r == 0 ? caps : NULL);
356 struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
358 struct capa_param param;
359 struct capa_result result;
362 imap = get_imap(folder);
366 threaded_run(folder, ¶m, &result, capability_run);
368 debug_print("capa ok\n");
374 struct disconnect_param {
378 struct disconnect_result {
382 static void disconnect_run(struct etpan_thread_op * op)
385 struct disconnect_param * param;
386 struct disconnect_result * result;
391 r = mailimap_logout(param->imap);
396 void imap_threaded_disconnect(Folder * folder)
398 struct connect_param param;
399 struct connect_result result;
404 imap = get_imap(folder);
406 debug_print("was disconnected\n");
412 threaded_run(folder, ¶m, &result, disconnect_run);
415 key.len = sizeof(folder);
418 chash_delete(session_hash, &key, NULL);
421 key.len = sizeof(imap);
422 chash_delete(courier_workaround_hash, &key, NULL);
426 debug_print("disconnect ok\n");
433 const char * wildcard;
441 static void list_run(struct etpan_thread_op * op)
443 struct list_param * param;
444 struct list_result * result;
450 r = mailimap_list(param->imap, param->base,
451 param->wildcard, &list);
456 debug_print("imap list run - end\n");
459 int imap_threaded_list(Folder * folder, const char * base,
460 const char * wildcard,
463 struct list_param param;
464 struct list_result result;
466 debug_print("imap list - begin\n");
468 param.imap = get_imap(folder);
470 param.wildcard = wildcard;
472 threaded_run(folder, ¶m, &result, list_run);
474 * p_result = result.list;
476 debug_print("imap list - end %p\n", result.list);
486 const char * password;
490 struct login_result {
494 static void login_run(struct etpan_thread_op * op)
496 struct login_param * param;
497 struct login_result * result;
499 #ifdef DISABLE_LOG_DURING_LOGIN
505 #ifdef DISABLE_LOG_DURING_LOGIN
506 old_debug = mailstream_debug;
507 mailstream_debug = 0;
509 if (!strcmp(param->type, "LOGIN"))
510 r = mailimap_login(param->imap,
511 param->login, param->password);
513 r = mailimap_authenticate(param->imap,
514 param->type, NULL, NULL, NULL,
515 param->login, param->login,
516 param->password, NULL);
517 #ifdef DISABLE_LOG_DURING_LOGIN
518 mailstream_debug = old_debug;
523 debug_print("imap login run - end %i\n", r);
526 int imap_threaded_login(Folder * folder,
527 const char * login, const char * password,
530 struct login_param param;
531 struct login_result result;
533 debug_print("imap login - begin\n");
535 param.imap = get_imap(folder);
537 param.password = password;
540 threaded_run(folder, ¶m, &result, login_run);
542 debug_print("imap login - end\n");
548 struct status_param {
551 struct mailimap_status_att_list * status_att_list;
554 struct status_result {
556 struct mailimap_mailbox_data_status * data_status;
559 static void status_run(struct etpan_thread_op * op)
561 struct status_param * param;
562 struct status_result * result;
568 r = mailimap_status(param->imap, param->mb,
569 param->status_att_list,
570 &result->data_status);
573 debug_print("imap status run - end %i\n", r);
576 int imap_threaded_status(Folder * folder, const char * mb,
577 struct mailimap_mailbox_data_status ** data_status,
580 struct status_param param;
581 struct status_result result;
582 struct mailimap_status_att_list * status_att_list;
584 debug_print("imap status - begin\n");
586 status_att_list = mailimap_status_att_list_new_empty();
588 mailimap_status_att_list_add(status_att_list,
589 MAILIMAP_STATUS_ATT_MESSAGES);
592 mailimap_status_att_list_add(status_att_list,
593 MAILIMAP_STATUS_ATT_RECENT);
596 mailimap_status_att_list_add(status_att_list,
597 MAILIMAP_STATUS_ATT_UIDNEXT);
600 mailimap_status_att_list_add(status_att_list,
601 MAILIMAP_STATUS_ATT_UIDVALIDITY);
604 mailimap_status_att_list_add(status_att_list,
605 MAILIMAP_STATUS_ATT_UNSEEN);
607 param.imap = get_imap(folder);
609 param.status_att_list = status_att_list;
611 threaded_run(folder, ¶m, &result, status_run);
613 debug_print("imap status - end\n");
615 * data_status = result.data_status;
630 static void noop_run(struct etpan_thread_op * op)
632 struct noop_param * param;
633 struct noop_result * result;
637 r = mailimap_noop(param->imap);
641 debug_print("imap noop run - end %i\n", r);
644 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
646 struct noop_param param;
647 struct noop_result result;
650 debug_print("imap noop - begin\n");
652 imap = get_imap(folder);
655 threaded_run(folder, ¶m, &result, noop_run);
657 if (imap->imap_selection_info != NULL) {
658 * p_exists = imap->imap_selection_info->sel_exists;
664 debug_print("imap noop - end\n");
670 struct starttls_param {
674 struct starttls_result {
678 static void starttls_run(struct etpan_thread_op * op)
680 struct starttls_param * param;
681 struct starttls_result * result;
685 r = mailimap_starttls(param->imap);
689 debug_print("imap starttls run - end %i\n", r);
692 mailimap *imap = param->imap;
693 mailstream_low *plain_low = NULL;
694 mailstream_low *tls_low = NULL;
697 plain_low = mailstream_get_low(imap->imap_stream);
698 fd = mailstream_low_get_fd(plain_low);
700 debug_print("imap starttls run - can't get fd\n");
701 result->error = MAILIMAP_ERROR_STREAM;
704 tls_low = mailstream_low_ssl_open(fd);
705 if (tls_low == NULL) {
706 debug_print("imap starttls run - can't ssl_open\n");
707 result->error = MAILIMAP_ERROR_STREAM;
710 mailstream_low_free(plain_low);
711 mailstream_set_low(imap->imap_stream, tls_low);
715 int imap_threaded_starttls(Folder * folder)
717 struct starttls_param param;
718 struct starttls_result result;
720 debug_print("imap starttls - begin\n");
722 param.imap = get_imap(folder);
724 threaded_run(folder, ¶m, &result, starttls_run);
726 debug_print("imap starttls - end\n");
733 struct create_param {
738 struct create_result {
742 static void create_run(struct etpan_thread_op * op)
744 struct create_param * param;
745 struct create_result * result;
749 r = mailimap_create(param->imap, param->mb);
753 debug_print("imap create run - end %i\n", r);
756 int imap_threaded_create(Folder * folder, const char * mb)
758 struct create_param param;
759 struct create_result result;
761 debug_print("imap create - begin\n");
763 param.imap = get_imap(folder);
766 threaded_run(folder, ¶m, &result, create_run);
768 debug_print("imap create - end\n");
776 struct rename_param {
779 const char * new_name;
782 struct rename_result {
786 static void rename_run(struct etpan_thread_op * op)
788 struct rename_param * param;
789 struct rename_result * result;
793 r = mailimap_rename(param->imap, param->mb, param->new_name);
797 debug_print("imap rename run - end %i\n", r);
800 int imap_threaded_rename(Folder * folder,
801 const char * mb, const char * new_name)
803 struct rename_param param;
804 struct rename_result result;
806 debug_print("imap rename - begin\n");
808 param.imap = get_imap(folder);
810 param.new_name = new_name;
812 threaded_run(folder, ¶m, &result, rename_run);
814 debug_print("imap rename - end\n");
822 struct delete_param {
827 struct delete_result {
831 static void delete_run(struct etpan_thread_op * op)
833 struct delete_param * param;
834 struct delete_result * result;
838 r = mailimap_delete(param->imap, param->mb);
842 debug_print("imap delete run - end %i\n", r);
845 int imap_threaded_delete(Folder * folder, const char * mb)
847 struct delete_param param;
848 struct delete_result result;
850 debug_print("imap delete - begin\n");
852 param.imap = get_imap(folder);
855 threaded_run(folder, ¶m, &result, delete_run);
857 debug_print("imap delete - end\n");
864 struct select_param {
869 struct select_result {
873 static void select_run(struct etpan_thread_op * op)
875 struct select_param * param;
876 struct select_result * result;
880 r = mailimap_select(param->imap, param->mb);
884 debug_print("imap select run - end %i\n", r);
887 int imap_threaded_select(Folder * folder, const char * mb,
888 gint * exists, gint * recent, gint * unseen,
889 guint32 * uid_validity)
891 struct select_param param;
892 struct select_result result;
895 debug_print("imap select - begin\n");
897 imap = get_imap(folder);
901 threaded_run(folder, ¶m, &result, select_run);
903 if (result.error != MAILIMAP_NO_ERROR)
906 if (imap->imap_selection_info == NULL)
907 return MAILIMAP_ERROR_PARSE;
909 * exists = imap->imap_selection_info->sel_exists;
910 * recent = imap->imap_selection_info->sel_recent;
911 * unseen = imap->imap_selection_info->sel_unseen;
912 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
914 debug_print("imap select - end\n");
921 struct examine_param {
926 struct examine_result {
930 static void examine_run(struct etpan_thread_op * op)
932 struct examine_param * param;
933 struct examine_result * result;
937 r = mailimap_examine(param->imap, param->mb);
941 debug_print("imap examine run - end %i\n", r);
944 int imap_threaded_examine(Folder * folder, const char * mb,
945 gint * exists, gint * recent, gint * unseen,
946 guint32 * uid_validity)
948 struct examine_param param;
949 struct examine_result result;
952 debug_print("imap examine - begin\n");
954 imap = get_imap(folder);
958 threaded_run(folder, ¶m, &result, examine_run);
960 if (result.error != MAILIMAP_NO_ERROR)
963 if (imap->imap_selection_info == NULL)
964 return MAILIMAP_ERROR_PARSE;
966 * exists = imap->imap_selection_info->sel_exists;
967 * recent = imap->imap_selection_info->sel_recent;
968 * unseen = imap->imap_selection_info->sel_unseen;
969 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
971 debug_print("imap examine - end\n");
979 struct search_param {
982 struct mailimap_set * set;
985 struct search_result {
987 clist * search_result;
990 static void search_run(struct etpan_thread_op * op)
992 struct search_param * param;
993 struct search_result * result;
995 struct mailimap_search_key * key;
996 struct mailimap_search_key * uid_key;
997 struct mailimap_search_key * search_type_key;
998 clist * search_result;
1002 uid_key = mailimap_search_key_new_uid(param->set);
1004 search_type_key = NULL;
1005 switch (param->type) {
1006 case IMAP_SEARCH_TYPE_SIMPLE:
1007 search_type_key = NULL;
1010 case IMAP_SEARCH_TYPE_SEEN:
1011 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1012 NULL, NULL, NULL, NULL, NULL,
1013 NULL, NULL, NULL, NULL, NULL,
1014 NULL, NULL, NULL, NULL, 0,
1015 NULL, NULL, NULL, NULL, NULL,
1016 NULL, 0, NULL, NULL, NULL);
1019 case IMAP_SEARCH_TYPE_UNSEEN:
1020 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1021 NULL, NULL, NULL, NULL, NULL,
1022 NULL, NULL, NULL, NULL, NULL,
1023 NULL, NULL, NULL, NULL, 0,
1024 NULL, NULL, NULL, NULL, NULL,
1025 NULL, 0, NULL, NULL, NULL);
1028 case IMAP_SEARCH_TYPE_ANSWERED:
1029 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1030 NULL, NULL, NULL, NULL, NULL,
1031 NULL, NULL, NULL, NULL, NULL,
1032 NULL, NULL, NULL, NULL, 0,
1033 NULL, NULL, NULL, NULL, NULL,
1034 NULL, 0, NULL, NULL, NULL);
1037 case IMAP_SEARCH_TYPE_FLAGGED:
1038 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1039 NULL, NULL, NULL, NULL, NULL,
1040 NULL, NULL, NULL, NULL, NULL,
1041 NULL, NULL, NULL, NULL, 0,
1042 NULL, NULL, NULL, NULL, NULL,
1043 NULL, 0, NULL, NULL, NULL);
1045 case IMAP_SEARCH_TYPE_DELETED:
1046 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1047 NULL, NULL, NULL, NULL, NULL,
1048 NULL, NULL, NULL, NULL, NULL,
1049 NULL, NULL, NULL, NULL, 0,
1050 NULL, NULL, NULL, NULL, NULL,
1051 NULL, 0, NULL, NULL, NULL);
1055 if (search_type_key != NULL) {
1056 key = mailimap_search_key_new_multiple_empty();
1057 mailimap_search_key_multiple_add(key, search_type_key);
1058 mailimap_search_key_multiple_add(key, uid_key);
1064 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1066 result = op->result;
1068 result->search_result = search_result;
1069 debug_print("imap search run - end %i\n", r);
1072 int imap_threaded_search(Folder * folder, int search_type,
1073 struct mailimap_set * set, clist ** search_result)
1075 struct search_param param;
1076 struct search_result result;
1079 debug_print("imap search - begin\n");
1081 imap = get_imap(folder);
1084 param.type = search_type;
1086 threaded_run(folder, ¶m, &result, search_run);
1088 if (result.error != MAILIMAP_NO_ERROR)
1089 return result.error;
1091 debug_print("imap search - end\n");
1093 * search_result = result.search_result;
1095 return result.error;
1102 uid_list_to_env_list(clist * fetch_result, carray ** result)
1110 tab = carray_new(128);
1112 res = MAILIMAP_ERROR_MEMORY;
1116 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1117 cur = clist_next(cur)) {
1118 struct mailimap_msg_att * msg_att;
1119 clistiter * item_cur;
1124 msg_att = clist_content(cur);
1128 for(item_cur = clist_begin(msg_att->att_list) ;
1130 item_cur = clist_next(item_cur)) {
1131 struct mailimap_msg_att_item * item;
1133 item = clist_content(item_cur);
1135 switch (item->att_type) {
1136 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1137 switch (item->att_data.att_static->att_type) {
1138 case MAILIMAP_MSG_ATT_UID:
1139 uid = item->att_data.att_static->att_data.att_uid;
1145 puid = malloc(sizeof(* puid));
1147 res = MAILIMAP_ERROR_MEMORY;
1152 r = carray_add(tab, puid, NULL);
1155 res = MAILIMAP_ERROR_MEMORY;
1162 return MAILIMAP_NO_ERROR;
1165 for(i = 0 ; i < carray_count(tab) ; i++)
1166 mailmessage_free(carray_get(tab, i));
1171 static int imap_get_messages_list(mailimap * imap,
1172 uint32_t first_index,
1177 struct mailimap_fetch_att * fetch_att;
1178 struct mailimap_fetch_type * fetch_type;
1179 struct mailimap_set * set;
1180 clist * fetch_result;
1183 set = mailimap_set_new_interval(first_index, 0);
1185 res = MAILIMAP_ERROR_MEMORY;
1189 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1190 if (fetch_type == NULL) {
1191 res = MAILIMAP_ERROR_MEMORY;
1195 fetch_att = mailimap_fetch_att_new_uid();
1196 if (fetch_att == NULL) {
1197 res = MAILIMAP_ERROR_MEMORY;
1198 goto free_fetch_type;
1201 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1202 if (r != MAILIMAP_NO_ERROR) {
1203 mailimap_fetch_att_free(fetch_att);
1204 res = MAILIMAP_ERROR_MEMORY;
1205 goto free_fetch_type;
1208 r = mailimap_uid_fetch(imap, set,
1209 fetch_type, &fetch_result);
1211 mailimap_fetch_type_free(fetch_type);
1212 mailimap_set_free(set);
1214 if (r != MAILIMAP_NO_ERROR) {
1220 r = uid_list_to_env_list(fetch_result, &env_list);
1221 mailimap_fetch_list_free(fetch_result);
1223 * result = env_list;
1225 return MAILIMAP_NO_ERROR;
1228 mailimap_fetch_type_free(fetch_type);
1230 mailimap_set_free(set);
1238 struct fetch_uid_param {
1240 uint32_t first_index;
1243 struct fetch_uid_result {
1245 carray * fetch_result;
1248 static void fetch_uid_run(struct etpan_thread_op * op)
1250 struct fetch_uid_param * param;
1251 struct fetch_uid_result * result;
1252 carray * fetch_result;
1257 fetch_result = NULL;
1258 r = imap_get_messages_list(param->imap, param->first_index,
1261 result = op->result;
1263 result->fetch_result = fetch_result;
1264 debug_print("imap fetch_uid run - end %i\n", r);
1267 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1268 carray ** fetch_result)
1270 struct fetch_uid_param param;
1271 struct fetch_uid_result result;
1274 debug_print("imap fetch_uid - begin\n");
1276 imap = get_imap(folder);
1278 param.first_index = first_index;
1280 threaded_run(folder, ¶m, &result, fetch_uid_run);
1282 if (result.error != MAILIMAP_NO_ERROR)
1283 return result.error;
1285 debug_print("imap fetch_uid - end\n");
1287 * fetch_result = result.fetch_result;
1289 return result.error;
1293 void imap_fetch_uid_list_free(carray * uid_list)
1297 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1300 puid = carray_get(uid_list, i);
1303 carray_free(uid_list);
1308 static int imap_fetch(mailimap * imap,
1311 size_t * result_len)
1314 struct mailimap_set * set;
1315 struct mailimap_fetch_att * fetch_att;
1316 struct mailimap_fetch_type * fetch_type;
1317 clist * fetch_result;
1318 struct mailimap_msg_att * msg_att;
1319 struct mailimap_msg_att_item * msg_att_item;
1324 struct mailimap_section * section;
1326 set = mailimap_set_new_single(msg_index);
1328 res = MAILIMAP_ERROR_MEMORY;
1332 section = mailimap_section_new(NULL);
1333 if (section == NULL) {
1334 res = MAILIMAP_ERROR_MEMORY;
1338 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1339 if (fetch_att == NULL) {
1340 mailimap_section_free(section);
1341 res = MAILIMAP_ERROR_MEMORY;
1345 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1346 if (fetch_type == NULL) {
1347 res = MAILIMAP_ERROR_MEMORY;
1348 goto free_fetch_att;
1351 r = mailimap_uid_fetch(imap, set,
1352 fetch_type, &fetch_result);
1354 mailimap_fetch_type_free(fetch_type);
1355 mailimap_set_free(set);
1358 case MAILIMAP_NO_ERROR:
1364 if (clist_begin(fetch_result) == NULL) {
1365 mailimap_fetch_list_free(fetch_result);
1366 return MAILIMAP_ERROR_FETCH;
1369 msg_att = clist_begin(fetch_result)->data;
1374 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1375 cur = clist_next(cur)) {
1376 msg_att_item = clist_content(cur);
1378 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1379 if (msg_att_item->att_data.att_static->att_type ==
1380 MAILIMAP_MSG_ATT_BODY_SECTION) {
1381 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1383 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1385 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1390 mailimap_fetch_list_free(fetch_result);
1393 return MAILIMAP_ERROR_FETCH;
1396 * result_len = text_length;
1398 return MAILIMAP_NO_ERROR;
1401 mailimap_fetch_att_free(fetch_att);
1403 mailimap_set_free(set);
1408 static int imap_fetch_header(mailimap * imap,
1411 size_t * result_len)
1414 struct mailimap_set * set;
1415 struct mailimap_fetch_att * fetch_att;
1416 struct mailimap_fetch_type * fetch_type;
1417 clist * fetch_result;
1418 struct mailimap_msg_att * msg_att;
1419 struct mailimap_msg_att_item * msg_att_item;
1424 struct mailimap_section * section;
1426 set = mailimap_set_new_single(msg_index);
1428 res = MAILIMAP_ERROR_MEMORY;
1432 section = mailimap_section_new_header();
1433 if (section == NULL) {
1434 res = MAILIMAP_ERROR_MEMORY;
1438 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1439 if (fetch_att == NULL) {
1440 mailimap_section_free(section);
1441 res = MAILIMAP_ERROR_MEMORY;
1445 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1446 if (fetch_type == NULL) {
1447 res = MAILIMAP_ERROR_MEMORY;
1448 goto free_fetch_att;
1451 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1453 mailimap_fetch_type_free(fetch_type);
1454 mailimap_set_free(set);
1457 case MAILIMAP_NO_ERROR:
1463 if (clist_begin(fetch_result) == NULL) {
1464 mailimap_fetch_list_free(fetch_result);
1465 return MAILIMAP_ERROR_FETCH;
1468 msg_att = clist_begin(fetch_result)->data;
1473 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1474 cur = clist_next(cur)) {
1475 msg_att_item = clist_content(cur);
1477 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1478 if (msg_att_item->att_data.att_static->att_type ==
1479 MAILIMAP_MSG_ATT_BODY_SECTION) {
1480 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1481 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1483 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1488 mailimap_fetch_list_free(fetch_result);
1491 return MAILIMAP_ERROR_FETCH;
1494 * result_len = text_length;
1496 return MAILIMAP_NO_ERROR;
1499 mailimap_fetch_att_free(fetch_att);
1501 mailimap_set_free(set);
1508 struct fetch_content_param {
1511 const char * filename;
1515 struct fetch_content_result {
1519 static void fetch_content_run(struct etpan_thread_op * op)
1521 struct fetch_content_param * param;
1522 struct fetch_content_result * result;
1524 size_t content_size;
1533 if (param->with_body)
1534 r = imap_fetch(param->imap, param->msg_index,
1535 &content, &content_size);
1537 r = imap_fetch_header(param->imap, param->msg_index,
1538 &content, &content_size);
1540 result = op->result;
1543 if (r == MAILIMAP_NO_ERROR) {
1544 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1546 result->error = MAILIMAP_ERROR_FETCH;
1550 f = fdopen(fd, "wb");
1552 result->error = MAILIMAP_ERROR_FETCH;
1556 r = fwrite(content, 1, content_size, f);
1563 g_unlink(param->filename);
1574 g_unlink(param->filename);
1577 if (mmap_string_unref(content) != 0)
1581 debug_print("imap fetch_content run - end %i\n", r);
1584 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1586 const char * filename)
1588 struct fetch_content_param param;
1589 struct fetch_content_result result;
1592 debug_print("imap fetch_content - begin\n");
1594 imap = get_imap(folder);
1596 param.msg_index = msg_index;
1597 param.filename = filename;
1598 param.with_body = with_body;
1600 threaded_run(folder, ¶m, &result, fetch_content_run);
1602 if (result.error != MAILIMAP_NO_ERROR)
1603 return result.error;
1605 debug_print("imap fetch_content - end\n");
1607 return result.error;
1612 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1620 flag_list = att_dyn->att_list;
1621 if (flag_list == NULL)
1624 for(cur = clist_begin(flag_list) ; cur != NULL ;
1625 cur = clist_next(cur)) {
1626 struct mailimap_flag_fetch * flag_fetch;
1628 flag_fetch = clist_content(cur);
1629 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1632 switch (flag_fetch->fl_flag->fl_type) {
1633 case MAILIMAP_FLAG_ANSWERED:
1634 flags |= MSG_REPLIED;
1636 case MAILIMAP_FLAG_FLAGGED:
1637 flags |= MSG_MARKED;
1639 case MAILIMAP_FLAG_DELETED:
1640 flags |= MSG_DELETED;
1642 case MAILIMAP_FLAG_SEEN:
1643 flags &= ~MSG_UNREAD;
1653 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1657 struct mailimap_msg_att_dynamic ** patt_dyn)
1659 clistiter * item_cur;
1663 struct mailimap_msg_att_dynamic * att_dyn;
1670 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1671 item_cur = clist_next(item_cur)) {
1672 struct mailimap_msg_att_item * item;
1674 item = clist_content(item_cur);
1676 switch (item->att_type) {
1677 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1678 switch (item->att_data.att_static->att_type) {
1679 case MAILIMAP_MSG_ATT_UID:
1680 uid = item->att_data.att_static->att_data.att_uid;
1683 case MAILIMAP_MSG_ATT_BODY_SECTION:
1684 if (headers == NULL) {
1685 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1688 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1689 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1694 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1695 if (att_dyn == NULL) {
1696 att_dyn = item->att_data.att_dyn;
1704 if (pheaders != NULL)
1705 * pheaders = headers;
1706 if (pref_size != NULL)
1707 * pref_size = ref_size;
1708 if (patt_dyn != NULL)
1709 * patt_dyn = att_dyn;
1711 return MAIL_NO_ERROR;
1714 static struct imap_fetch_env_info *
1715 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1717 struct imap_fetch_env_info * info;
1721 struct mailimap_msg_att_dynamic * att_dyn;
1723 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1726 info = malloc(sizeof(* info));
1728 info->headers = strdup(headers);
1730 info->flags = imap_flags_to_flags(att_dyn);
1736 imap_fetch_result_to_envelop_list(clist * fetch_result,
1737 carray ** p_env_list)
1745 env_list = carray_new(16);
1747 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1748 cur = clist_next(cur)) {
1749 struct mailimap_msg_att * msg_att;
1750 struct imap_fetch_env_info * env_info;
1752 msg_att = clist_content(cur);
1754 env_info = fetch_to_env_info(msg_att);
1755 carray_add(env_list, env_info, NULL);
1758 * p_env_list = env_list;
1760 return MAIL_NO_ERROR;
1763 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1765 struct mailimap_fetch_att * fetch_att;
1769 struct mailimap_header_list * imap_hdrlist;
1770 struct mailimap_section * section;
1772 hdrlist = clist_new();
1774 header = strdup("Date");
1775 r = clist_append(hdrlist, header);
1776 header = strdup("From");
1777 r = clist_append(hdrlist, header);
1778 header = strdup("To");
1779 r = clist_append(hdrlist, header);
1780 header = strdup("Cc");
1781 r = clist_append(hdrlist, header);
1782 header = strdup("Subject");
1783 r = clist_append(hdrlist, header);
1784 header = strdup("Message-ID");
1785 r = clist_append(hdrlist, header);
1786 header = strdup("References");
1787 r = clist_append(hdrlist, header);
1788 header = strdup("In-Reply-To");
1789 r = clist_append(hdrlist, header);
1791 imap_hdrlist = mailimap_header_list_new(hdrlist);
1792 section = mailimap_section_new_header_fields(imap_hdrlist);
1793 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1794 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1796 return MAIL_NO_ERROR;
1799 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1801 struct mailimap_fetch_att * fetch_att;
1802 struct mailimap_section * section;
1804 section = mailimap_section_new_header();
1805 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1806 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1808 return MAIL_NO_ERROR;
1812 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1813 carray ** p_env_list)
1815 struct mailimap_fetch_att * fetch_att;
1816 struct mailimap_fetch_type * fetch_type;
1818 clist * fetch_result;
1824 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1827 fetch_att = mailimap_fetch_att_new_uid();
1828 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1831 fetch_att = mailimap_fetch_att_new_flags();
1832 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1835 fetch_att = mailimap_fetch_att_new_rfc822_size();
1836 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1840 key.len = sizeof(imap);
1841 r = chash_get(courier_workaround_hash, &key, &value);
1843 r = imap_add_envelope_fetch_att(fetch_type);
1845 r = imap_add_header_fetch_att(fetch_type);
1847 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1850 case MAILIMAP_NO_ERROR:
1853 mailimap_fetch_type_free(fetch_type);
1857 if (clist_begin(fetch_result) == NULL) {
1858 res = MAILIMAP_ERROR_FETCH;
1862 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1863 mailimap_fetch_list_free(fetch_result);
1865 if (r != MAILIMAP_NO_ERROR) {
1866 mailimap_fetch_type_free(fetch_type);
1867 res = MAILIMAP_ERROR_MEMORY;
1871 mailimap_fetch_type_free(fetch_type);
1873 * p_env_list = env_list;
1875 return MAILIMAP_NO_ERROR;
1881 struct fetch_env_param {
1883 struct mailimap_set * set;
1886 struct fetch_env_result {
1887 carray * fetch_env_result;
1891 static void fetch_env_run(struct etpan_thread_op * op)
1893 struct fetch_env_param * param;
1894 struct fetch_env_result * result;
1901 r = imap_get_envelopes_list(param->imap, param->set,
1904 result = op->result;
1906 result->fetch_env_result = env_list;
1908 debug_print("imap fetch_env run - end %i\n", r);
1911 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1912 carray ** p_env_list)
1914 struct fetch_env_param param;
1915 struct fetch_env_result result;
1918 debug_print("imap fetch_env - begin\n");
1920 imap = get_imap(folder);
1924 threaded_run(folder, ¶m, &result, fetch_env_run);
1926 if (result.error != MAILIMAP_NO_ERROR) {
1932 key.len = sizeof(imap);
1933 r = chash_get(courier_workaround_hash, &key, &value);
1937 chash_set(courier_workaround_hash, &key, &value, NULL);
1939 threaded_run(folder, ¶m, &result, fetch_env_run);
1943 if (result.error != MAILIMAP_NO_ERROR)
1944 return result.error;
1946 debug_print("imap fetch_env - end\n");
1948 * p_env_list = result.fetch_env_result;
1950 return result.error;
1953 void imap_fetch_env_free(carray * env_list)
1957 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1958 struct imap_fetch_env_info * env_info;
1960 env_info = carray_get(env_list, i);
1961 free(env_info->headers);
1964 carray_free(env_list);
1971 struct append_param {
1973 const char * mailbox;
1974 const char * filename;
1975 struct mailimap_flag_list * flag_list;
1978 struct append_result {
1982 static void append_run(struct etpan_thread_op * op)
1984 struct append_param * param;
1985 struct append_result * result;
1989 struct stat stat_buf;
1993 result = op->result;
1995 r = stat(param->filename, &stat_buf);
1997 result->error = MAILIMAP_ERROR_APPEND;
2000 size = stat_buf.st_size;
2002 fd = open(param->filename, O_RDONLY);
2004 result->error = MAILIMAP_ERROR_APPEND;
2008 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2009 if (data == (void *) MAP_FAILED) {
2011 result->error = MAILIMAP_ERROR_APPEND;
2015 r = mailimap_append(param->imap, param->mailbox,
2016 param->flag_list, NULL,
2024 debug_print("imap append run - end %i\n", r);
2027 int imap_threaded_append(Folder * folder, const char * mailbox,
2028 const char * filename,
2029 struct mailimap_flag_list * flag_list)
2031 struct append_param param;
2032 struct append_result result;
2035 debug_print("imap append - begin\n");
2037 imap = get_imap(folder);
2039 param.mailbox = mailbox;
2040 param.filename = filename;
2041 param.flag_list = flag_list;
2043 threaded_run(folder, ¶m, &result, append_run);
2045 if (result.error != MAILIMAP_NO_ERROR)
2046 return result.error;
2048 debug_print("imap append - end\n");
2050 return result.error;
2056 struct expunge_param {
2060 struct expunge_result {
2064 static void expunge_run(struct etpan_thread_op * op)
2066 struct expunge_param * param;
2067 struct expunge_result * result;
2071 r = mailimap_expunge(param->imap);
2073 result = op->result;
2075 debug_print("imap expunge run - end %i\n", r);
2078 int imap_threaded_expunge(Folder * folder)
2080 struct expunge_param param;
2081 struct expunge_result result;
2083 debug_print("imap expunge - begin\n");
2085 param.imap = get_imap(folder);
2087 threaded_run(folder, ¶m, &result, expunge_run);
2089 debug_print("imap expunge - end\n");
2091 return result.error;
2097 struct mailimap_set * set;
2101 struct copy_result {
2105 static void copy_run(struct etpan_thread_op * op)
2107 struct copy_param * param;
2108 struct copy_result * result;
2113 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2115 result = op->result;
2118 debug_print("imap copy run - end %i\n", r);
2121 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2124 struct copy_param param;
2125 struct copy_result result;
2128 debug_print("imap copy - begin\n");
2130 imap = get_imap(folder);
2135 threaded_run(folder, ¶m, &result, copy_run);
2137 if (result.error != MAILIMAP_NO_ERROR)
2138 return result.error;
2140 debug_print("imap copy - end\n");
2142 return result.error;
2147 struct store_param {
2149 struct mailimap_set * set;
2150 struct mailimap_store_att_flags * store_att_flags;
2153 struct store_result {
2157 static void store_run(struct etpan_thread_op * op)
2159 struct store_param * param;
2160 struct store_result * result;
2165 r = mailimap_uid_store(param->imap, param->set,
2166 param->store_att_flags);
2168 result = op->result;
2171 debug_print("imap store run - end %i\n", r);
2174 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2175 struct mailimap_store_att_flags * store_att_flags)
2177 struct store_param param;
2178 struct store_result result;
2181 debug_print("imap store - begin\n");
2183 imap = get_imap(folder);
2186 param.store_att_flags = store_att_flags;
2188 threaded_run(folder, ¶m, &result, store_run);
2190 if (result.error != MAILIMAP_NO_ERROR)
2191 return result.error;
2193 debug_print("imap store - end\n");
2195 return result.error;
2199 #define ENV_BUFFER_SIZE 512
2201 static void do_exec_command(int fd, const char * command,
2202 const char * servername, uint16_t port)
2206 char env_buffer[ENV_BUFFER_SIZE];
2210 /* Fork again to become a child of init rather than
2211 the etpan client. */
2217 snprintf(env_buffer, ENV_BUFFER_SIZE,
2218 "ETPANSERVER=%s", servername);
2220 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2224 setenv("ETPANSERVER", servername, 1);
2226 unsetenv("ETPANSERVER");
2231 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2233 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2239 snprintf(porttext, sizeof(porttext), "%d", port);
2240 setenv("ETPANPORT", porttext, 1);
2243 unsetenv("ETPANPORT");
2247 /* Not a lot we can do if there's an error other than bail. */
2248 if (dup2(fd, 0) == -1)
2250 if (dup2(fd, 1) == -1)
2253 /* Should we close stderr and reopen /dev/null? */
2255 maxopen = sysconf(_SC_OPEN_MAX);
2256 for (i=3; i < maxopen; i++)
2260 /* Detach from the controlling tty if we have one. Otherwise,
2261 SSH might do something stupid like trying to use it instead
2262 of running $SSH_ASKPASS. Doh. */
2263 fd = open("/dev/tty", O_RDONLY);
2265 ioctl(fd, TIOCNOTTY, NULL);
2268 #endif /* TIOCNOTTY */
2270 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2272 /* Eep. Shouldn't reach this */
2276 static int subcommand_connect(const char *command,
2277 const char *servername, uint16_t port)
2279 /* SEB unsupported on Windows */
2283 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2288 do_exec_command(sockfds[1], command, servername, port);
2290 else if (childpid == -1) {
2298 /* Reap child, leaving grandchild process to run */
2299 waitpid(childpid, NULL, 0);
2304 int socket_connect_cmd(mailimap * imap, const char * command,
2305 const char * server, int port)
2311 fd = subcommand_connect(command, server, port);
2313 return MAILIMAP_ERROR_STREAM;
2315 s = mailstream_socket_open(fd);
2318 return MAILIMAP_ERROR_STREAM;
2321 r = mailimap_connect(imap, s);
2322 if (r != MAILIMAP_NO_ERROR) {
2323 mailstream_close(s);
2327 return MAILIMAP_NO_ERROR;
2332 struct connect_cmd_param {
2334 const char * command;
2335 const char * server;
2339 struct connect_cmd_result {
2343 static void connect_cmd_run(struct etpan_thread_op * op)
2346 struct connect_cmd_param * param;
2347 struct connect_cmd_result * result;
2350 result = op->result;
2352 r = socket_connect_cmd(param->imap, param->command,
2353 param->server, param->port);
2359 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2360 const char * server, int port)
2362 struct connect_cmd_param param;
2363 struct connect_cmd_result result;
2368 imap = mailimap_new(0, NULL);
2371 key.len = sizeof(folder);
2374 chash_set(session_hash, &key, &value, NULL);
2377 param.command = command;
2378 param.server = server;
2381 threaded_run(folder, ¶m, &result, connect_cmd_run);
2383 debug_print("connect_cmd ok %i\n", result.error);
2385 return result.error;
2389 void imap_main_init(void)
2392 void imap_main_done(void)
2395 void imap_main_set_timeout(int sec)