7 #include "imap-thread.h"
16 #include "etpan-thread-manager.h"
19 #define DISABLE_LOG_DURING_LOGIN
21 static struct etpan_thread_manager * thread_manager = NULL;
22 static chash * courier_workaround_hash = NULL;
23 static chash * imap_hash = NULL;
24 static chash * session_hash = NULL;
25 static guint thread_manager_signal = 0;
26 static GIOChannel * io_channel = NULL;
29 static gboolean thread_manager_event(GIOChannel * source,
30 GIOCondition condition,
33 etpan_thread_manager_loop(thread_manager);
39 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
41 void imap_main_init(void)
43 int fd_thread_manager;
45 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
46 mailstream_network_delay.tv_usec = 0;
51 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
52 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
53 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
55 thread_manager = etpan_thread_manager_new();
57 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
59 io_channel = g_io_channel_unix_new(fd_thread_manager);
61 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
67 void imap_main_set_timeout(int sec)
69 mailstream_network_delay.tv_sec = sec;
70 mailstream_network_delay.tv_usec = 0;
73 void imap_main_done(void)
75 etpan_thread_manager_stop(thread_manager);
76 etpan_thread_manager_join(thread_manager);
78 g_source_remove(thread_manager_signal);
79 g_io_channel_unref(io_channel);
81 etpan_thread_manager_free(thread_manager);
83 chash_free(courier_workaround_hash);
84 chash_free(session_hash);
85 chash_free(imap_hash);
88 void imap_init(Folder * folder)
90 struct etpan_thread * thread;
94 thread = etpan_thread_manager_get_thread(thread_manager);
97 key.len = sizeof(folder);
101 chash_set(imap_hash, &key, &value, NULL);
104 void imap_done(Folder * folder)
106 struct etpan_thread * thread;
112 key.len = sizeof(folder);
114 r = chash_get(imap_hash, &key, &value);
120 etpan_thread_unbind(thread);
122 chash_delete(imap_hash, &key, NULL);
124 debug_print("remove thread");
127 static struct etpan_thread * get_thread(Folder * folder)
129 struct etpan_thread * thread;
134 key.len = sizeof(folder);
136 chash_get(imap_hash, &key, &value);
142 static mailimap * get_imap(Folder * folder)
150 key.len = sizeof(folder);
152 r = chash_get(session_hash, &key, &value);
162 static void generic_cb(int cancelled, void * result, void * callback_data)
166 p_finished = callback_data;
168 debug_print("generic_cb\n");
173 static void threaded_run(Folder * folder, void * param, void * result,
174 void (* func)(struct etpan_thread_op * ))
176 struct etpan_thread_op * op;
177 struct etpan_thread * thread;
180 imap_folder_ref(folder);
182 op = etpan_thread_op_new();
188 op->callback = generic_cb;
189 op->callback_data = &finished;
194 thread = get_thread(folder);
195 etpan_thread_op_schedule(thread, op);
198 gtk_main_iteration();
201 etpan_thread_op_free(op);
203 imap_folder_unref(folder);
209 struct connect_param {
215 struct connect_result {
219 static void connect_run(struct etpan_thread_op * op)
222 struct connect_param * param;
223 struct connect_result * result;
228 r = mailimap_socket_connect(param->imap,
229 param->server, param->port);
235 int imap_threaded_connect(Folder * folder, const char * server, int port)
237 struct connect_param param;
238 struct connect_result result;
243 imap = mailimap_new(0, NULL);
246 key.len = sizeof(folder);
249 chash_set(session_hash, &key, &value, NULL);
252 param.server = server;
255 threaded_run(folder, ¶m, &result, connect_run);
257 debug_print("connect ok %i\n", result.error);
263 static void connect_ssl_run(struct etpan_thread_op * op)
266 struct connect_param * param;
267 struct connect_result * result;
272 r = mailimap_ssl_connect(param->imap,
273 param->server, param->port);
278 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
280 struct connect_param param;
281 struct connect_result result;
286 imap = mailimap_new(0, NULL);
289 key.len = sizeof(folder);
292 chash_set(session_hash, &key, &value, NULL);
295 param.server = server;
298 threaded_run(folder, ¶m, &result, connect_ssl_run);
300 debug_print("connect ok\n");
305 struct disconnect_param {
309 struct disconnect_result {
313 static void disconnect_run(struct etpan_thread_op * op)
316 struct disconnect_param * param;
317 struct disconnect_result * result;
322 r = mailimap_logout(param->imap);
327 void imap_threaded_disconnect(Folder * folder)
329 struct connect_param param;
330 struct connect_result result;
335 imap = get_imap(folder);
337 debug_print("was disconnected\n");
343 threaded_run(folder, ¶m, &result, disconnect_run);
346 key.len = sizeof(folder);
349 chash_delete(session_hash, &key, NULL);
352 key.len = sizeof(imap);
353 chash_delete(courier_workaround_hash, &key, NULL);
357 debug_print("disconnect ok\n");
364 const char * wildcard;
372 static void list_run(struct etpan_thread_op * op)
374 struct list_param * param;
375 struct list_result * result;
381 r = mailimap_list(param->imap, param->base,
382 param->wildcard, &list);
387 debug_print("imap list run - end\n");
390 int imap_threaded_list(Folder * folder, const char * base,
391 const char * wildcard,
394 struct list_param param;
395 struct list_result result;
397 debug_print("imap list - begin\n");
399 param.imap = get_imap(folder);
401 param.wildcard = wildcard;
403 threaded_run(folder, ¶m, &result, list_run);
405 * p_result = result.list;
407 debug_print("imap list - end %p\n", result.list);
417 const char * password;
420 struct login_result {
424 static void login_run(struct etpan_thread_op * op)
426 struct login_param * param;
427 struct login_result * result;
429 #ifdef DISABLE_LOG_DURING_LOGIN
435 #ifdef DISABLE_LOG_DURING_LOGIN
436 old_debug = mailstream_debug;
439 r = mailimap_login(param->imap,
440 param->login, param->password);
442 #ifdef DISABLE_LOG_DURING_LOGIN
443 mailstream_debug = old_debug;
448 debug_print("imap login run - end %i\n", r);
451 int imap_threaded_login(Folder * folder,
452 const char * login, const char * password)
454 struct login_param param;
455 struct login_result result;
457 debug_print("imap login - begin\n");
459 param.imap = get_imap(folder);
461 param.password = password;
463 threaded_run(folder, ¶m, &result, login_run);
465 debug_print("imap login - end\n");
471 struct status_param {
474 struct mailimap_status_att_list * status_att_list;
477 struct status_result {
479 struct mailimap_mailbox_data_status * data_status;
482 static void status_run(struct etpan_thread_op * op)
484 struct status_param * param;
485 struct status_result * result;
491 r = mailimap_status(param->imap, param->mb,
492 param->status_att_list,
493 &result->data_status);
496 debug_print("imap status run - end %i\n", r);
499 int imap_threaded_status(Folder * folder, const char * mb,
500 struct mailimap_mailbox_data_status ** data_status)
502 struct status_param param;
503 struct status_result result;
504 struct mailimap_status_att_list * status_att_list;
506 debug_print("imap status - begin\n");
508 status_att_list = mailimap_status_att_list_new_empty();
509 mailimap_status_att_list_add(status_att_list,
510 MAILIMAP_STATUS_ATT_MESSAGES);
511 mailimap_status_att_list_add(status_att_list,
512 MAILIMAP_STATUS_ATT_RECENT);
513 mailimap_status_att_list_add(status_att_list,
514 MAILIMAP_STATUS_ATT_UIDNEXT);
515 mailimap_status_att_list_add(status_att_list,
516 MAILIMAP_STATUS_ATT_UIDVALIDITY);
517 mailimap_status_att_list_add(status_att_list,
518 MAILIMAP_STATUS_ATT_UNSEEN);
520 param.imap = get_imap(folder);
522 param.status_att_list = status_att_list;
524 threaded_run(folder, ¶m, &result, status_run);
526 debug_print("imap status - end\n");
528 * data_status = result.data_status;
543 static void noop_run(struct etpan_thread_op * op)
545 struct noop_param * param;
546 struct noop_result * result;
550 r = mailimap_noop(param->imap);
554 debug_print("imap noop run - end %i\n", r);
557 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
559 struct noop_param param;
560 struct noop_result result;
563 debug_print("imap noop - begin\n");
565 imap = get_imap(folder);
568 threaded_run(folder, ¶m, &result, noop_run);
570 if (imap->imap_selection_info != NULL) {
571 * p_exists = imap->imap_selection_info->sel_exists;
577 debug_print("imap noop - end\n");
583 struct starttls_param {
587 struct starttls_result {
591 static void starttls_run(struct etpan_thread_op * op)
593 struct starttls_param * param;
594 struct starttls_result * result;
598 r = mailimap_starttls(param->imap);
602 debug_print("imap starttls run - end %i\n", r);
605 int imap_threaded_starttls(Folder * folder)
607 struct starttls_param param;
608 struct starttls_result result;
610 debug_print("imap starttls - begin\n");
612 param.imap = get_imap(folder);
614 threaded_run(folder, ¶m, &result, starttls_run);
616 debug_print("imap starttls - end\n");
623 struct create_param {
628 struct create_result {
632 static void create_run(struct etpan_thread_op * op)
634 struct create_param * param;
635 struct create_result * result;
639 r = mailimap_create(param->imap, param->mb);
643 debug_print("imap create run - end %i\n", r);
646 int imap_threaded_create(Folder * folder, const char * mb)
648 struct create_param param;
649 struct create_result result;
651 debug_print("imap create - begin\n");
653 param.imap = get_imap(folder);
656 threaded_run(folder, ¶m, &result, create_run);
658 debug_print("imap create - end\n");
666 struct rename_param {
669 const char * new_name;
672 struct rename_result {
676 static void rename_run(struct etpan_thread_op * op)
678 struct rename_param * param;
679 struct rename_result * result;
683 r = mailimap_rename(param->imap, param->mb, param->new_name);
687 debug_print("imap rename run - end %i\n", r);
690 int imap_threaded_rename(Folder * folder,
691 const char * mb, const char * new_name)
693 struct rename_param param;
694 struct rename_result result;
696 debug_print("imap rename - begin\n");
698 param.imap = get_imap(folder);
700 param.new_name = new_name;
702 threaded_run(folder, ¶m, &result, rename_run);
704 debug_print("imap rename - end\n");
712 struct delete_param {
717 struct delete_result {
721 static void delete_run(struct etpan_thread_op * op)
723 struct delete_param * param;
724 struct delete_result * result;
728 r = mailimap_delete(param->imap, param->mb);
732 debug_print("imap delete run - end %i\n", r);
735 int imap_threaded_delete(Folder * folder, const char * mb)
737 struct delete_param param;
738 struct delete_result result;
740 debug_print("imap delete - begin\n");
742 param.imap = get_imap(folder);
745 threaded_run(folder, ¶m, &result, delete_run);
747 debug_print("imap delete - end\n");
754 struct select_param {
759 struct select_result {
763 static void select_run(struct etpan_thread_op * op)
765 struct select_param * param;
766 struct select_result * result;
770 r = mailimap_select(param->imap, param->mb);
774 debug_print("imap select run - end %i\n", r);
777 int imap_threaded_select(Folder * folder, const char * mb,
778 gint * exists, gint * recent, gint * unseen,
779 guint32 * uid_validity)
781 struct select_param param;
782 struct select_result result;
785 debug_print("imap select - begin\n");
787 imap = get_imap(folder);
791 threaded_run(folder, ¶m, &result, select_run);
793 if (result.error != MAILIMAP_NO_ERROR)
796 if (imap->imap_selection_info == NULL)
797 return MAILIMAP_ERROR_PARSE;
799 * exists = imap->imap_selection_info->sel_exists;
800 * recent = imap->imap_selection_info->sel_recent;
801 * unseen = imap->imap_selection_info->sel_unseen;
802 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
804 debug_print("imap select - end\n");
811 struct examine_param {
816 struct examine_result {
820 static void examine_run(struct etpan_thread_op * op)
822 struct examine_param * param;
823 struct examine_result * result;
827 r = mailimap_examine(param->imap, param->mb);
831 debug_print("imap examine run - end %i\n", r);
834 int imap_threaded_examine(Folder * folder, const char * mb,
835 gint * exists, gint * recent, gint * unseen,
836 guint32 * uid_validity)
838 struct examine_param param;
839 struct examine_result result;
842 debug_print("imap examine - begin\n");
844 imap = get_imap(folder);
848 threaded_run(folder, ¶m, &result, examine_run);
850 if (result.error != MAILIMAP_NO_ERROR)
853 if (imap->imap_selection_info == NULL)
854 return MAILIMAP_ERROR_PARSE;
856 * exists = imap->imap_selection_info->sel_exists;
857 * recent = imap->imap_selection_info->sel_recent;
858 * unseen = imap->imap_selection_info->sel_unseen;
859 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
861 debug_print("imap examine - end\n");
869 struct search_param {
872 struct mailimap_set * set;
875 struct search_result {
877 clist * search_result;
880 static void search_run(struct etpan_thread_op * op)
882 struct search_param * param;
883 struct search_result * result;
885 struct mailimap_search_key * key;
886 struct mailimap_search_key * uid_key;
887 struct mailimap_search_key * search_type_key;
888 clist * search_result;
892 uid_key = mailimap_search_key_new_uid(param->set);
894 search_type_key = NULL;
895 switch (param->type) {
896 case IMAP_SEARCH_TYPE_SIMPLE:
897 search_type_key = NULL;
900 case IMAP_SEARCH_TYPE_SEEN:
901 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
902 NULL, NULL, NULL, NULL, NULL,
903 NULL, NULL, NULL, NULL, NULL,
904 NULL, NULL, NULL, NULL, 0,
905 NULL, NULL, NULL, NULL, NULL,
906 NULL, 0, NULL, NULL, NULL);
909 case IMAP_SEARCH_TYPE_UNSEEN:
910 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
911 NULL, NULL, NULL, NULL, NULL,
912 NULL, NULL, NULL, NULL, NULL,
913 NULL, NULL, NULL, NULL, 0,
914 NULL, NULL, NULL, NULL, NULL,
915 NULL, 0, NULL, NULL, NULL);
918 case IMAP_SEARCH_TYPE_ANSWERED:
919 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
920 NULL, NULL, NULL, NULL, NULL,
921 NULL, NULL, NULL, NULL, NULL,
922 NULL, NULL, NULL, NULL, 0,
923 NULL, NULL, NULL, NULL, NULL,
924 NULL, 0, NULL, NULL, NULL);
927 case IMAP_SEARCH_TYPE_FLAGGED:
928 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
929 NULL, NULL, NULL, NULL, NULL,
930 NULL, NULL, NULL, NULL, NULL,
931 NULL, NULL, NULL, NULL, 0,
932 NULL, NULL, NULL, NULL, NULL,
933 NULL, 0, NULL, NULL, NULL);
937 if (search_type_key != NULL) {
938 key = mailimap_search_key_new_multiple_empty();
939 mailimap_search_key_multiple_add(key, search_type_key);
940 mailimap_search_key_multiple_add(key, uid_key);
946 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
950 result->search_result = search_result;
951 debug_print("imap search run - end %i\n", r);
954 int imap_threaded_search(Folder * folder, int search_type,
955 struct mailimap_set * set, clist ** search_result)
957 struct search_param param;
958 struct search_result result;
961 debug_print("imap search - begin\n");
963 imap = get_imap(folder);
966 param.type = search_type;
968 threaded_run(folder, ¶m, &result, search_run);
970 if (result.error != MAILIMAP_NO_ERROR)
973 debug_print("imap search - end\n");
975 * search_result = result.search_result;
984 uid_list_to_env_list(clist * fetch_result, carray ** result)
992 tab = carray_new(128);
994 res = MAILIMAP_ERROR_MEMORY;
998 for(cur = clist_begin(fetch_result) ; cur != NULL ;
999 cur = clist_next(cur)) {
1000 struct mailimap_msg_att * msg_att;
1001 clistiter * item_cur;
1006 msg_att = clist_content(cur);
1010 for(item_cur = clist_begin(msg_att->att_list) ;
1012 item_cur = clist_next(item_cur)) {
1013 struct mailimap_msg_att_item * item;
1015 item = clist_content(item_cur);
1017 switch (item->att_type) {
1018 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1019 switch (item->att_data.att_static->att_type) {
1020 case MAILIMAP_MSG_ATT_UID:
1021 uid = item->att_data.att_static->att_data.att_uid;
1027 puid = malloc(sizeof(* puid));
1029 res = MAILIMAP_ERROR_MEMORY;
1034 r = carray_add(tab, puid, NULL);
1037 res = MAILIMAP_ERROR_MEMORY;
1044 return MAILIMAP_NO_ERROR;
1047 for(i = 0 ; i < carray_count(tab) ; i++)
1048 mailmessage_free(carray_get(tab, i));
1053 static int imap_get_messages_list(mailimap * imap,
1054 uint32_t first_index,
1059 struct mailimap_fetch_att * fetch_att;
1060 struct mailimap_fetch_type * fetch_type;
1061 struct mailimap_set * set;
1062 clist * fetch_result;
1065 set = mailimap_set_new_interval(first_index, 0);
1067 res = MAILIMAP_ERROR_MEMORY;
1071 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1072 if (fetch_type == NULL) {
1073 res = MAILIMAP_ERROR_MEMORY;
1077 fetch_att = mailimap_fetch_att_new_uid();
1078 if (fetch_att == NULL) {
1079 res = MAILIMAP_ERROR_MEMORY;
1080 goto free_fetch_type;
1083 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1084 if (r != MAILIMAP_NO_ERROR) {
1085 mailimap_fetch_att_free(fetch_att);
1086 res = MAILIMAP_ERROR_MEMORY;
1087 goto free_fetch_type;
1090 r = mailimap_uid_fetch(imap, set,
1091 fetch_type, &fetch_result);
1093 mailimap_fetch_type_free(fetch_type);
1094 mailimap_set_free(set);
1096 if (r != MAILIMAP_NO_ERROR) {
1102 r = uid_list_to_env_list(fetch_result, &env_list);
1103 mailimap_fetch_list_free(fetch_result);
1105 * result = env_list;
1107 return MAILIMAP_NO_ERROR;
1110 mailimap_fetch_type_free(fetch_type);
1112 mailimap_set_free(set);
1120 struct fetch_uid_param {
1122 uint32_t first_index;
1125 struct fetch_uid_result {
1127 carray * fetch_result;
1130 static void fetch_uid_run(struct etpan_thread_op * op)
1132 struct fetch_uid_param * param;
1133 struct fetch_uid_result * result;
1134 carray * fetch_result;
1139 fetch_result = NULL;
1140 r = imap_get_messages_list(param->imap, param->first_index,
1143 result = op->result;
1145 result->fetch_result = fetch_result;
1146 debug_print("imap fetch_uid run - end %i\n", r);
1149 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1150 carray ** fetch_result)
1152 struct fetch_uid_param param;
1153 struct fetch_uid_result result;
1156 debug_print("imap fetch_uid - begin\n");
1158 imap = get_imap(folder);
1160 param.first_index = first_index;
1162 threaded_run(folder, ¶m, &result, fetch_uid_run);
1164 if (result.error != MAILIMAP_NO_ERROR)
1165 return result.error;
1167 debug_print("imap fetch_uid - end\n");
1169 * fetch_result = result.fetch_result;
1171 return result.error;
1175 void imap_fetch_uid_list_free(carray * uid_list)
1179 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1182 puid = carray_get(uid_list, i);
1185 carray_free(uid_list);
1190 static int imap_fetch(mailimap * imap,
1193 size_t * result_len)
1196 struct mailimap_set * set;
1197 struct mailimap_fetch_att * fetch_att;
1198 struct mailimap_fetch_type * fetch_type;
1199 clist * fetch_result;
1200 struct mailimap_msg_att * msg_att;
1201 struct mailimap_msg_att_item * msg_att_item;
1206 struct mailimap_section * section;
1208 set = mailimap_set_new_single(msg_index);
1210 res = MAILIMAP_ERROR_MEMORY;
1214 section = mailimap_section_new(NULL);
1215 if (section == NULL) {
1216 res = MAILIMAP_ERROR_MEMORY;
1220 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1221 if (fetch_att == NULL) {
1222 mailimap_section_free(section);
1223 res = MAILIMAP_ERROR_MEMORY;
1227 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1228 if (fetch_type == NULL) {
1229 res = MAILIMAP_ERROR_MEMORY;
1230 goto free_fetch_att;
1233 r = mailimap_uid_fetch(imap, set,
1234 fetch_type, &fetch_result);
1236 mailimap_fetch_type_free(fetch_type);
1237 mailimap_set_free(set);
1240 case MAILIMAP_NO_ERROR:
1246 if (clist_begin(fetch_result) == NULL) {
1247 mailimap_fetch_list_free(fetch_result);
1248 return MAILIMAP_ERROR_FETCH;
1251 msg_att = clist_begin(fetch_result)->data;
1256 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1257 cur = clist_next(cur)) {
1258 msg_att_item = clist_content(cur);
1260 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1261 if (msg_att_item->att_data.att_static->att_type ==
1262 MAILIMAP_MSG_ATT_BODY_SECTION) {
1263 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1265 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1267 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1272 mailimap_fetch_list_free(fetch_result);
1275 return MAILIMAP_ERROR_FETCH;
1278 * result_len = text_length;
1280 return MAILIMAP_NO_ERROR;
1283 mailimap_fetch_att_free(fetch_att);
1285 mailimap_set_free(set);
1290 static int imap_fetch_header(mailimap * imap,
1293 size_t * result_len)
1296 struct mailimap_set * set;
1297 struct mailimap_fetch_att * fetch_att;
1298 struct mailimap_fetch_type * fetch_type;
1299 clist * fetch_result;
1300 struct mailimap_msg_att * msg_att;
1301 struct mailimap_msg_att_item * msg_att_item;
1306 struct mailimap_section * section;
1308 set = mailimap_set_new_single(msg_index);
1310 res = MAILIMAP_ERROR_MEMORY;
1314 section = mailimap_section_new_header();
1315 if (section == NULL) {
1316 res = MAILIMAP_ERROR_MEMORY;
1320 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1321 if (fetch_att == NULL) {
1322 mailimap_section_free(section);
1323 res = MAILIMAP_ERROR_MEMORY;
1327 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1328 if (fetch_type == NULL) {
1329 res = MAILIMAP_ERROR_MEMORY;
1330 goto free_fetch_att;
1333 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1335 mailimap_fetch_type_free(fetch_type);
1336 mailimap_set_free(set);
1339 case MAILIMAP_NO_ERROR:
1345 if (clist_begin(fetch_result) == NULL) {
1346 mailimap_fetch_list_free(fetch_result);
1347 return MAILIMAP_ERROR_FETCH;
1350 msg_att = clist_begin(fetch_result)->data;
1355 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1356 cur = clist_next(cur)) {
1357 msg_att_item = clist_content(cur);
1359 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1360 if (msg_att_item->att_data.att_static->att_type ==
1361 MAILIMAP_MSG_ATT_BODY_SECTION) {
1362 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1363 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1365 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1370 mailimap_fetch_list_free(fetch_result);
1373 return MAILIMAP_ERROR_FETCH;
1376 * result_len = text_length;
1378 return MAILIMAP_NO_ERROR;
1381 mailimap_fetch_att_free(fetch_att);
1383 mailimap_set_free(set);
1390 struct fetch_content_param {
1393 const char * filename;
1397 struct fetch_content_result {
1401 static void fetch_content_run(struct etpan_thread_op * op)
1403 struct fetch_content_param * param;
1404 struct fetch_content_result * result;
1406 size_t content_size;
1415 if (param->with_body)
1416 r = imap_fetch(param->imap, param->msg_index,
1417 &content, &content_size);
1419 r = imap_fetch_header(param->imap, param->msg_index,
1420 &content, &content_size);
1422 result = op->result;
1425 if (r == MAILIMAP_NO_ERROR) {
1426 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1428 result->error = MAILIMAP_ERROR_FETCH;
1432 f = fdopen(fd, "wb");
1434 result->error = MAILIMAP_ERROR_FETCH;
1438 r = fwrite(content, 1, content_size, f);
1445 unlink(param->filename);
1456 unlink(param->filename);
1459 if (mmap_string_unref(content) != 0)
1463 debug_print("imap fetch_content run - end %i\n", r);
1466 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1468 const char * filename)
1470 struct fetch_content_param param;
1471 struct fetch_content_result result;
1474 debug_print("imap fetch_content - begin\n");
1476 imap = get_imap(folder);
1478 param.msg_index = msg_index;
1479 param.filename = filename;
1480 param.with_body = with_body;
1482 threaded_run(folder, ¶m, &result, fetch_content_run);
1484 if (result.error != MAILIMAP_NO_ERROR)
1485 return result.error;
1487 debug_print("imap fetch_content - end\n");
1489 return result.error;
1494 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1502 flag_list = att_dyn->att_list;
1503 if (flag_list == NULL)
1506 for(cur = clist_begin(flag_list) ; cur != NULL ;
1507 cur = clist_next(cur)) {
1508 struct mailimap_flag_fetch * flag_fetch;
1510 flag_fetch = clist_content(cur);
1511 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1514 switch (flag_fetch->fl_flag->fl_type) {
1515 case MAILIMAP_FLAG_ANSWERED:
1516 flags |= MSG_REPLIED;
1518 case MAILIMAP_FLAG_FLAGGED:
1519 flags |= MSG_MARKED;
1521 case MAILIMAP_FLAG_DELETED:
1522 flags |= MSG_DELETED;
1524 case MAILIMAP_FLAG_SEEN:
1525 flags &= ~MSG_UNREAD;
1535 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1539 struct mailimap_msg_att_dynamic ** patt_dyn)
1541 clistiter * item_cur;
1545 struct mailimap_msg_att_dynamic * att_dyn;
1552 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1553 item_cur = clist_next(item_cur)) {
1554 struct mailimap_msg_att_item * item;
1556 item = clist_content(item_cur);
1558 switch (item->att_type) {
1559 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1560 switch (item->att_data.att_static->att_type) {
1561 case MAILIMAP_MSG_ATT_UID:
1562 uid = item->att_data.att_static->att_data.att_uid;
1565 case MAILIMAP_MSG_ATT_BODY_SECTION:
1566 if (headers == NULL) {
1567 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1570 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1571 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1576 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1577 if (att_dyn == NULL) {
1578 att_dyn = item->att_data.att_dyn;
1586 if (pheaders != NULL)
1587 * pheaders = headers;
1588 if (pref_size != NULL)
1589 * pref_size = ref_size;
1590 if (patt_dyn != NULL)
1591 * patt_dyn = att_dyn;
1593 return MAIL_NO_ERROR;
1596 static struct imap_fetch_env_info *
1597 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1599 struct imap_fetch_env_info * info;
1603 struct mailimap_msg_att_dynamic * att_dyn;
1605 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1608 info = malloc(sizeof(* info));
1610 info->headers = strdup(headers);
1612 info->flags = imap_flags_to_flags(att_dyn);
1618 imap_fetch_result_to_envelop_list(clist * fetch_result,
1619 carray ** p_env_list)
1627 env_list = carray_new(16);
1629 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1630 cur = clist_next(cur)) {
1631 struct mailimap_msg_att * msg_att;
1632 struct imap_fetch_env_info * env_info;
1634 msg_att = clist_content(cur);
1636 env_info = fetch_to_env_info(msg_att);
1637 carray_add(env_list, env_info, NULL);
1640 * p_env_list = env_list;
1642 return MAIL_NO_ERROR;
1645 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1647 struct mailimap_fetch_att * fetch_att;
1651 struct mailimap_header_list * imap_hdrlist;
1652 struct mailimap_section * section;
1654 hdrlist = clist_new();
1656 header = strdup("Date");
1657 r = clist_append(hdrlist, header);
1658 header = strdup("From");
1659 r = clist_append(hdrlist, header);
1660 header = strdup("To");
1661 r = clist_append(hdrlist, header);
1662 header = strdup("Cc");
1663 r = clist_append(hdrlist, header);
1664 header = strdup("Subject");
1665 r = clist_append(hdrlist, header);
1666 header = strdup("Message-ID");
1667 r = clist_append(hdrlist, header);
1668 header = strdup("References");
1669 r = clist_append(hdrlist, header);
1670 header = strdup("In-Reply-To");
1671 r = clist_append(hdrlist, header);
1673 imap_hdrlist = mailimap_header_list_new(hdrlist);
1674 section = mailimap_section_new_header_fields(imap_hdrlist);
1675 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1676 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1678 return MAIL_NO_ERROR;
1681 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1683 struct mailimap_fetch_att * fetch_att;
1684 struct mailimap_section * section;
1686 section = mailimap_section_new_header();
1687 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1688 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1690 return MAIL_NO_ERROR;
1694 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1695 carray ** p_env_list)
1697 struct mailimap_fetch_att * fetch_att;
1698 struct mailimap_fetch_type * fetch_type;
1700 clist * fetch_result;
1706 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1709 fetch_att = mailimap_fetch_att_new_uid();
1710 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1713 fetch_att = mailimap_fetch_att_new_flags();
1714 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1717 fetch_att = mailimap_fetch_att_new_rfc822_size();
1718 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1722 key.len = sizeof(imap);
1723 r = chash_get(courier_workaround_hash, &key, &value);
1725 r = imap_add_envelope_fetch_att(fetch_type);
1727 r = imap_add_header_fetch_att(fetch_type);
1729 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1732 case MAILIMAP_NO_ERROR:
1735 mailimap_fetch_type_free(fetch_type);
1739 if (clist_begin(fetch_result) == NULL) {
1740 res = MAILIMAP_ERROR_FETCH;
1744 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1745 mailimap_fetch_list_free(fetch_result);
1747 if (r != MAILIMAP_NO_ERROR) {
1748 mailimap_fetch_type_free(fetch_type);
1749 res = MAILIMAP_ERROR_MEMORY;
1753 mailimap_fetch_type_free(fetch_type);
1755 * p_env_list = env_list;
1757 return MAILIMAP_NO_ERROR;
1763 struct fetch_env_param {
1765 struct mailimap_set * set;
1768 struct fetch_env_result {
1769 carray * fetch_env_result;
1773 static void fetch_env_run(struct etpan_thread_op * op)
1775 struct fetch_env_param * param;
1776 struct fetch_env_result * result;
1783 r = imap_get_envelopes_list(param->imap, param->set,
1786 result = op->result;
1788 result->fetch_env_result = env_list;
1790 debug_print("imap fetch_env run - end %i\n", r);
1793 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1794 carray ** p_env_list)
1796 struct fetch_env_param param;
1797 struct fetch_env_result result;
1800 debug_print("imap fetch_env - begin\n");
1802 imap = get_imap(folder);
1806 threaded_run(folder, ¶m, &result, fetch_env_run);
1808 if (result.error != MAILIMAP_NO_ERROR) {
1814 key.len = sizeof(imap);
1815 r = chash_get(courier_workaround_hash, &key, &value);
1819 chash_set(courier_workaround_hash, &key, &value, NULL);
1821 threaded_run(folder, ¶m, &result, fetch_env_run);
1825 if (result.error != MAILIMAP_NO_ERROR)
1826 return result.error;
1828 debug_print("imap fetch_env - end\n");
1830 * p_env_list = result.fetch_env_result;
1832 return result.error;
1835 void imap_fetch_env_free(carray * env_list)
1839 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1840 struct imap_fetch_env_info * env_info;
1842 env_info = carray_get(env_list, i);
1843 free(env_info->headers);
1846 carray_free(env_list);
1853 struct append_param {
1855 const char * mailbox;
1856 const char * filename;
1857 struct mailimap_flag_list * flag_list;
1860 struct append_result {
1864 static void append_run(struct etpan_thread_op * op)
1866 struct append_param * param;
1867 struct append_result * result;
1871 struct stat stat_buf;
1875 result = op->result;
1877 r = stat(param->filename, &stat_buf);
1879 result->error = MAILIMAP_ERROR_APPEND;
1882 size = stat_buf.st_size;
1884 fd = open(param->filename, O_RDONLY);
1886 result->error = MAILIMAP_ERROR_APPEND;
1890 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1891 if (data == (void *) MAP_FAILED) {
1893 result->error = MAILIMAP_ERROR_APPEND;
1897 r = mailimap_append(param->imap, param->mailbox,
1898 param->flag_list, NULL,
1906 debug_print("imap append run - end %i\n", r);
1909 int imap_threaded_append(Folder * folder, const char * mailbox,
1910 const char * filename,
1911 struct mailimap_flag_list * flag_list)
1913 struct append_param param;
1914 struct append_result result;
1917 debug_print("imap append - begin\n");
1919 imap = get_imap(folder);
1921 param.mailbox = mailbox;
1922 param.filename = filename;
1923 param.flag_list = flag_list;
1925 threaded_run(folder, ¶m, &result, append_run);
1927 if (result.error != MAILIMAP_NO_ERROR)
1928 return result.error;
1930 debug_print("imap append - end\n");
1932 return result.error;
1938 struct expunge_param {
1942 struct expunge_result {
1946 static void expunge_run(struct etpan_thread_op * op)
1948 struct expunge_param * param;
1949 struct expunge_result * result;
1953 r = mailimap_expunge(param->imap);
1955 result = op->result;
1957 debug_print("imap expunge run - end %i\n", r);
1960 int imap_threaded_expunge(Folder * folder)
1962 struct expunge_param param;
1963 struct expunge_result result;
1965 debug_print("imap expunge - begin\n");
1967 param.imap = get_imap(folder);
1969 threaded_run(folder, ¶m, &result, expunge_run);
1971 debug_print("imap expunge - end\n");
1973 return result.error;
1979 struct mailimap_set * set;
1983 struct copy_result {
1987 static void copy_run(struct etpan_thread_op * op)
1989 struct copy_param * param;
1990 struct copy_result * result;
1995 r = mailimap_uid_copy(param->imap, param->set, param->mb);
1997 result = op->result;
2000 debug_print("imap copy run - end %i\n", r);
2003 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2006 struct copy_param param;
2007 struct copy_result result;
2010 debug_print("imap copy - begin\n");
2012 imap = get_imap(folder);
2017 threaded_run(folder, ¶m, &result, copy_run);
2019 if (result.error != MAILIMAP_NO_ERROR)
2020 return result.error;
2022 debug_print("imap copy - end\n");
2024 return result.error;
2029 struct store_param {
2031 struct mailimap_set * set;
2032 struct mailimap_store_att_flags * store_att_flags;
2035 struct store_result {
2039 static void store_run(struct etpan_thread_op * op)
2041 struct store_param * param;
2042 struct store_result * result;
2047 r = mailimap_uid_store(param->imap, param->set,
2048 param->store_att_flags);
2050 result = op->result;
2053 debug_print("imap store run - end %i\n", r);
2056 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2057 struct mailimap_store_att_flags * store_att_flags)
2059 struct store_param param;
2060 struct store_result result;
2063 debug_print("imap store - begin\n");
2065 imap = get_imap(folder);
2068 param.store_att_flags = store_att_flags;
2070 threaded_run(folder, ¶m, &result, store_run);
2072 if (result.error != MAILIMAP_NO_ERROR)
2073 return result.error;
2075 debug_print("imap store - end\n");
2077 return result.error;
2082 static void do_exec_command(int fd, const char * command,
2083 const char * servername, uint16_t port)
2088 /* Fork again to become a child of init rather than
2089 the etpan client. */
2094 setenv("ETPANSERVER", servername, 1);
2096 unsetenv("ETPANSERVER");
2101 snprintf(porttext, sizeof(porttext), "%d", port);
2102 setenv("ETPANPORT", porttext, 1);
2105 unsetenv("ETPANPORT");
2108 /* Not a lot we can do if there's an error other than bail. */
2109 if (dup2(fd, 0) == -1)
2111 if (dup2(fd, 1) == -1)
2114 /* Should we close stderr and reopen /dev/null? */
2116 maxopen = sysconf(_SC_OPEN_MAX);
2117 for (i=3; i < maxopen; i++)
2121 /* Detach from the controlling tty if we have one. Otherwise,
2122 SSH might do something stupid like trying to use it instead
2123 of running $SSH_ASKPASS. Doh. */
2124 fd = open("/dev/tty", O_RDONLY);
2126 ioctl(fd, TIOCNOTTY, NULL);
2129 #endif /* TIOCNOTTY */
2131 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2133 /* Eep. Shouldn't reach this */
2137 static int subcommand_connect(const char *command,
2138 const char *servername, uint16_t port)
2140 /* SEB unsupported on Windows */
2144 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2149 do_exec_command(sockfds[1], command, servername, port);
2151 else if (childpid == -1) {
2159 /* Reap child, leaving grandchild process to run */
2160 waitpid(childpid, NULL, 0);
2165 int socket_connect_cmd(mailimap * imap, const char * command,
2166 const char * server, int port)
2172 fd = subcommand_connect(command, server, port);
2174 return MAILIMAP_ERROR_STREAM;
2176 s = mailstream_socket_open(fd);
2179 return MAILIMAP_ERROR_STREAM;
2182 r = mailimap_connect(imap, s);
2183 if (r != MAILIMAP_NO_ERROR) {
2184 mailstream_close(s);
2188 return MAILIMAP_NO_ERROR;
2193 struct connect_cmd_param {
2195 const char * command;
2196 const char * server;
2200 struct connect_cmd_result {
2204 static void connect_cmd_run(struct etpan_thread_op * op)
2207 struct connect_cmd_param * param;
2208 struct connect_cmd_result * result;
2211 result = op->result;
2213 r = socket_connect_cmd(param->imap, param->command,
2214 param->server, param->port);
2220 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2221 const char * server, int port)
2223 struct connect_cmd_param param;
2224 struct connect_cmd_result result;
2229 imap = mailimap_new(0, NULL);
2232 key.len = sizeof(folder);
2235 chash_set(session_hash, &key, &value, NULL);
2238 param.command = command;
2239 param.server = server;
2242 threaded_run(folder, ¶m, &result, connect_cmd_run);
2244 debug_print("connect_cmd ok %i\n", result.error);
2246 return result.error;
2250 void imap_main_init(void)
2253 void imap_main_done(void)
2256 void imap_main_set_timeout(int sec)