7 #include "imap-thread.h"
17 #include "etpan-thread-manager.h"
20 #define DISABLE_LOG_DURING_LOGIN
22 static struct etpan_thread_manager * thread_manager = NULL;
23 static chash * courier_workaround_hash = NULL;
24 static chash * imap_hash = NULL;
25 static chash * session_hash = NULL;
26 static guint thread_manager_signal = 0;
27 static GIOChannel * io_channel = NULL;
30 static gboolean thread_manager_event(GIOChannel * source,
31 GIOCondition condition,
34 etpan_thread_manager_loop(thread_manager);
39 void imap_logger(int direction, const char * str, size_t size)
42 strncpy(buf, str, 511);
46 if (!strncmp(buf, "<<<<<<<", 7)
47 || !strncmp(buf, ">>>>>>>", 7)
48 || buf[0] == '\r' || buf[0] == '\n')
51 while (strstr(buf, "\r"))
52 *strstr(buf, "\r") = ' ';
53 while (strstr(buf, "\n"))
54 *strstr(buf, "\n") = ' ';
56 log_print("IMAP4%c %s\n", direction?'>':'<', buf);
59 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
61 void imap_main_init(void)
63 int fd_thread_manager;
65 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
66 mailstream_network_delay.tv_usec = 0;
69 mailstream_logger = imap_logger;
71 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
72 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
73 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
75 thread_manager = etpan_thread_manager_new();
77 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
79 io_channel = g_io_channel_unix_new(fd_thread_manager);
81 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
87 void imap_main_set_timeout(int sec)
89 mailstream_network_delay.tv_sec = sec;
90 mailstream_network_delay.tv_usec = 0;
93 void imap_main_done(void)
95 etpan_thread_manager_stop(thread_manager);
96 etpan_thread_manager_join(thread_manager);
98 g_source_remove(thread_manager_signal);
99 g_io_channel_unref(io_channel);
101 etpan_thread_manager_free(thread_manager);
103 chash_free(courier_workaround_hash);
104 chash_free(session_hash);
105 chash_free(imap_hash);
108 void imap_init(Folder * folder)
110 struct etpan_thread * thread;
114 thread = etpan_thread_manager_get_thread(thread_manager);
117 key.len = sizeof(folder);
121 chash_set(imap_hash, &key, &value, NULL);
124 void imap_done(Folder * folder)
126 struct etpan_thread * thread;
132 key.len = sizeof(folder);
134 r = chash_get(imap_hash, &key, &value);
140 etpan_thread_unbind(thread);
142 chash_delete(imap_hash, &key, NULL);
144 debug_print("remove thread");
147 static struct etpan_thread * get_thread(Folder * folder)
149 struct etpan_thread * thread;
154 key.len = sizeof(folder);
156 chash_get(imap_hash, &key, &value);
162 static mailimap * get_imap(Folder * folder)
170 key.len = sizeof(folder);
172 r = chash_get(session_hash, &key, &value);
182 static void generic_cb(int cancelled, void * result, void * callback_data)
186 p_finished = callback_data;
188 debug_print("generic_cb\n");
193 static void threaded_run(Folder * folder, void * param, void * result,
194 void (* func)(struct etpan_thread_op * ))
196 struct etpan_thread_op * op;
197 struct etpan_thread * thread;
200 imap_folder_ref(folder);
202 op = etpan_thread_op_new();
208 op->callback = generic_cb;
209 op->callback_data = &finished;
214 thread = get_thread(folder);
215 etpan_thread_op_schedule(thread, op);
218 gtk_main_iteration();
221 etpan_thread_op_free(op);
223 imap_folder_unref(folder);
229 struct connect_param {
235 struct connect_result {
239 static void connect_run(struct etpan_thread_op * op)
242 struct connect_param * param;
243 struct connect_result * result;
248 r = mailimap_socket_connect(param->imap,
249 param->server, param->port);
255 int imap_threaded_connect(Folder * folder, const char * server, int port)
257 struct connect_param param;
258 struct connect_result result;
263 imap = mailimap_new(0, NULL);
266 key.len = sizeof(folder);
269 chash_set(session_hash, &key, &value, NULL);
272 param.server = server;
275 threaded_run(folder, ¶m, &result, connect_run);
277 debug_print("connect ok %i\n", result.error);
283 static void connect_ssl_run(struct etpan_thread_op * op)
286 struct connect_param * param;
287 struct connect_result * result;
292 r = mailimap_ssl_connect(param->imap,
293 param->server, param->port);
298 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
300 struct connect_param param;
301 struct connect_result result;
306 imap = mailimap_new(0, NULL);
309 key.len = sizeof(folder);
312 chash_set(session_hash, &key, &value, NULL);
315 param.server = server;
318 threaded_run(folder, ¶m, &result, connect_ssl_run);
320 debug_print("connect ok\n");
325 struct disconnect_param {
329 struct disconnect_result {
333 static void disconnect_run(struct etpan_thread_op * op)
336 struct disconnect_param * param;
337 struct disconnect_result * result;
342 r = mailimap_logout(param->imap);
347 void imap_threaded_disconnect(Folder * folder)
349 struct connect_param param;
350 struct connect_result result;
355 imap = get_imap(folder);
357 debug_print("was disconnected\n");
363 threaded_run(folder, ¶m, &result, disconnect_run);
366 key.len = sizeof(folder);
369 chash_delete(session_hash, &key, NULL);
372 key.len = sizeof(imap);
373 chash_delete(courier_workaround_hash, &key, NULL);
377 debug_print("disconnect ok\n");
384 const char * wildcard;
392 static void list_run(struct etpan_thread_op * op)
394 struct list_param * param;
395 struct list_result * result;
401 r = mailimap_list(param->imap, param->base,
402 param->wildcard, &list);
407 debug_print("imap list run - end\n");
410 int imap_threaded_list(Folder * folder, const char * base,
411 const char * wildcard,
414 struct list_param param;
415 struct list_result result;
417 debug_print("imap list - begin\n");
419 param.imap = get_imap(folder);
421 param.wildcard = wildcard;
423 threaded_run(folder, ¶m, &result, list_run);
425 * p_result = result.list;
427 debug_print("imap list - end %p\n", result.list);
437 const char * password;
440 struct login_result {
444 static void login_run(struct etpan_thread_op * op)
446 struct login_param * param;
447 struct login_result * result;
449 #ifdef DISABLE_LOG_DURING_LOGIN
455 #ifdef DISABLE_LOG_DURING_LOGIN
456 old_debug = mailstream_debug;
457 mailstream_debug = 0;
460 r = mailimap_login(param->imap,
461 param->login, param->password);
463 #ifdef DISABLE_LOG_DURING_LOGIN
464 mailstream_debug = old_debug;
469 debug_print("imap login run - end %i\n", r);
472 int imap_threaded_login(Folder * folder,
473 const char * login, const char * password)
475 struct login_param param;
476 struct login_result result;
478 debug_print("imap login - begin\n");
480 param.imap = get_imap(folder);
482 param.password = password;
484 threaded_run(folder, ¶m, &result, login_run);
486 debug_print("imap login - end\n");
492 struct status_param {
495 struct mailimap_status_att_list * status_att_list;
498 struct status_result {
500 struct mailimap_mailbox_data_status * data_status;
503 static void status_run(struct etpan_thread_op * op)
505 struct status_param * param;
506 struct status_result * result;
512 r = mailimap_status(param->imap, param->mb,
513 param->status_att_list,
514 &result->data_status);
517 debug_print("imap status run - end %i\n", r);
520 int imap_threaded_status(Folder * folder, const char * mb,
521 struct mailimap_mailbox_data_status ** data_status)
523 struct status_param param;
524 struct status_result result;
525 struct mailimap_status_att_list * status_att_list;
527 debug_print("imap status - begin\n");
529 status_att_list = mailimap_status_att_list_new_empty();
530 mailimap_status_att_list_add(status_att_list,
531 MAILIMAP_STATUS_ATT_MESSAGES);
532 mailimap_status_att_list_add(status_att_list,
533 MAILIMAP_STATUS_ATT_RECENT);
534 mailimap_status_att_list_add(status_att_list,
535 MAILIMAP_STATUS_ATT_UIDNEXT);
536 mailimap_status_att_list_add(status_att_list,
537 MAILIMAP_STATUS_ATT_UIDVALIDITY);
538 mailimap_status_att_list_add(status_att_list,
539 MAILIMAP_STATUS_ATT_UNSEEN);
541 param.imap = get_imap(folder);
543 param.status_att_list = status_att_list;
545 threaded_run(folder, ¶m, &result, status_run);
547 debug_print("imap status - end\n");
549 * data_status = result.data_status;
564 static void noop_run(struct etpan_thread_op * op)
566 struct noop_param * param;
567 struct noop_result * result;
571 r = mailimap_noop(param->imap);
575 debug_print("imap noop run - end %i\n", r);
578 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
580 struct noop_param param;
581 struct noop_result result;
584 debug_print("imap noop - begin\n");
586 imap = get_imap(folder);
589 threaded_run(folder, ¶m, &result, noop_run);
591 if (imap->imap_selection_info != NULL) {
592 * p_exists = imap->imap_selection_info->sel_exists;
598 debug_print("imap noop - end\n");
604 struct starttls_param {
608 struct starttls_result {
612 static void starttls_run(struct etpan_thread_op * op)
614 struct starttls_param * param;
615 struct starttls_result * result;
619 r = mailimap_starttls(param->imap);
623 debug_print("imap starttls run - end %i\n", r);
626 mailimap *imap = param->imap;
627 mailstream_low *plain_low = NULL;
628 mailstream_low *tls_low = NULL;
631 plain_low = mailstream_get_low(imap->imap_stream);
632 fd = mailstream_low_get_fd(plain_low);
634 debug_print("imap starttls run - can't get fd\n");
635 result->error = MAILIMAP_ERROR_STREAM;
638 tls_low = mailstream_low_ssl_open(fd);
639 if (tls_low == NULL) {
640 debug_print("imap starttls run - can't ssl_open\n");
641 result->error = MAILIMAP_ERROR_STREAM;
644 mailstream_low_free(plain_low);
645 mailstream_set_low(imap->imap_stream, tls_low);
649 int imap_threaded_starttls(Folder * folder)
651 struct starttls_param param;
652 struct starttls_result result;
654 debug_print("imap starttls - begin\n");
656 param.imap = get_imap(folder);
658 threaded_run(folder, ¶m, &result, starttls_run);
660 debug_print("imap starttls - end\n");
667 struct create_param {
672 struct create_result {
676 static void create_run(struct etpan_thread_op * op)
678 struct create_param * param;
679 struct create_result * result;
683 r = mailimap_create(param->imap, param->mb);
687 debug_print("imap create run - end %i\n", r);
690 int imap_threaded_create(Folder * folder, const char * mb)
692 struct create_param param;
693 struct create_result result;
695 debug_print("imap create - begin\n");
697 param.imap = get_imap(folder);
700 threaded_run(folder, ¶m, &result, create_run);
702 debug_print("imap create - end\n");
710 struct rename_param {
713 const char * new_name;
716 struct rename_result {
720 static void rename_run(struct etpan_thread_op * op)
722 struct rename_param * param;
723 struct rename_result * result;
727 r = mailimap_rename(param->imap, param->mb, param->new_name);
731 debug_print("imap rename run - end %i\n", r);
734 int imap_threaded_rename(Folder * folder,
735 const char * mb, const char * new_name)
737 struct rename_param param;
738 struct rename_result result;
740 debug_print("imap rename - begin\n");
742 param.imap = get_imap(folder);
744 param.new_name = new_name;
746 threaded_run(folder, ¶m, &result, rename_run);
748 debug_print("imap rename - end\n");
756 struct delete_param {
761 struct delete_result {
765 static void delete_run(struct etpan_thread_op * op)
767 struct delete_param * param;
768 struct delete_result * result;
772 r = mailimap_delete(param->imap, param->mb);
776 debug_print("imap delete run - end %i\n", r);
779 int imap_threaded_delete(Folder * folder, const char * mb)
781 struct delete_param param;
782 struct delete_result result;
784 debug_print("imap delete - begin\n");
786 param.imap = get_imap(folder);
789 threaded_run(folder, ¶m, &result, delete_run);
791 debug_print("imap delete - end\n");
798 struct select_param {
803 struct select_result {
807 static void select_run(struct etpan_thread_op * op)
809 struct select_param * param;
810 struct select_result * result;
814 r = mailimap_select(param->imap, param->mb);
818 debug_print("imap select run - end %i\n", r);
821 int imap_threaded_select(Folder * folder, const char * mb,
822 gint * exists, gint * recent, gint * unseen,
823 guint32 * uid_validity)
825 struct select_param param;
826 struct select_result result;
829 debug_print("imap select - begin\n");
831 imap = get_imap(folder);
835 threaded_run(folder, ¶m, &result, select_run);
837 if (result.error != MAILIMAP_NO_ERROR)
840 if (imap->imap_selection_info == NULL)
841 return MAILIMAP_ERROR_PARSE;
843 * exists = imap->imap_selection_info->sel_exists;
844 * recent = imap->imap_selection_info->sel_recent;
845 * unseen = imap->imap_selection_info->sel_unseen;
846 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
848 debug_print("imap select - end\n");
855 struct examine_param {
860 struct examine_result {
864 static void examine_run(struct etpan_thread_op * op)
866 struct examine_param * param;
867 struct examine_result * result;
871 r = mailimap_examine(param->imap, param->mb);
875 debug_print("imap examine run - end %i\n", r);
878 int imap_threaded_examine(Folder * folder, const char * mb,
879 gint * exists, gint * recent, gint * unseen,
880 guint32 * uid_validity)
882 struct examine_param param;
883 struct examine_result result;
886 debug_print("imap examine - begin\n");
888 imap = get_imap(folder);
892 threaded_run(folder, ¶m, &result, examine_run);
894 if (result.error != MAILIMAP_NO_ERROR)
897 if (imap->imap_selection_info == NULL)
898 return MAILIMAP_ERROR_PARSE;
900 * exists = imap->imap_selection_info->sel_exists;
901 * recent = imap->imap_selection_info->sel_recent;
902 * unseen = imap->imap_selection_info->sel_unseen;
903 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
905 debug_print("imap examine - end\n");
913 struct search_param {
916 struct mailimap_set * set;
919 struct search_result {
921 clist * search_result;
924 static void search_run(struct etpan_thread_op * op)
926 struct search_param * param;
927 struct search_result * result;
929 struct mailimap_search_key * key;
930 struct mailimap_search_key * uid_key;
931 struct mailimap_search_key * search_type_key;
932 clist * search_result;
936 uid_key = mailimap_search_key_new_uid(param->set);
938 search_type_key = NULL;
939 switch (param->type) {
940 case IMAP_SEARCH_TYPE_SIMPLE:
941 search_type_key = NULL;
944 case IMAP_SEARCH_TYPE_SEEN:
945 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
946 NULL, NULL, NULL, NULL, NULL,
947 NULL, NULL, NULL, NULL, NULL,
948 NULL, NULL, NULL, NULL, 0,
949 NULL, NULL, NULL, NULL, NULL,
950 NULL, 0, NULL, NULL, NULL);
953 case IMAP_SEARCH_TYPE_UNSEEN:
954 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
955 NULL, NULL, NULL, NULL, NULL,
956 NULL, NULL, NULL, NULL, NULL,
957 NULL, NULL, NULL, NULL, 0,
958 NULL, NULL, NULL, NULL, NULL,
959 NULL, 0, NULL, NULL, NULL);
962 case IMAP_SEARCH_TYPE_ANSWERED:
963 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
964 NULL, NULL, NULL, NULL, NULL,
965 NULL, NULL, NULL, NULL, NULL,
966 NULL, NULL, NULL, NULL, 0,
967 NULL, NULL, NULL, NULL, NULL,
968 NULL, 0, NULL, NULL, NULL);
971 case IMAP_SEARCH_TYPE_FLAGGED:
972 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
973 NULL, NULL, NULL, NULL, NULL,
974 NULL, NULL, NULL, NULL, NULL,
975 NULL, NULL, NULL, NULL, 0,
976 NULL, NULL, NULL, NULL, NULL,
977 NULL, 0, NULL, NULL, NULL);
981 if (search_type_key != NULL) {
982 key = mailimap_search_key_new_multiple_empty();
983 mailimap_search_key_multiple_add(key, search_type_key);
984 mailimap_search_key_multiple_add(key, uid_key);
990 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
994 result->search_result = search_result;
995 debug_print("imap search run - end %i\n", r);
998 int imap_threaded_search(Folder * folder, int search_type,
999 struct mailimap_set * set, clist ** search_result)
1001 struct search_param param;
1002 struct search_result result;
1005 debug_print("imap search - begin\n");
1007 imap = get_imap(folder);
1010 param.type = search_type;
1012 threaded_run(folder, ¶m, &result, search_run);
1014 if (result.error != MAILIMAP_NO_ERROR)
1015 return result.error;
1017 debug_print("imap search - end\n");
1019 * search_result = result.search_result;
1021 return result.error;
1028 uid_list_to_env_list(clist * fetch_result, carray ** result)
1036 tab = carray_new(128);
1038 res = MAILIMAP_ERROR_MEMORY;
1042 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1043 cur = clist_next(cur)) {
1044 struct mailimap_msg_att * msg_att;
1045 clistiter * item_cur;
1050 msg_att = clist_content(cur);
1054 for(item_cur = clist_begin(msg_att->att_list) ;
1056 item_cur = clist_next(item_cur)) {
1057 struct mailimap_msg_att_item * item;
1059 item = clist_content(item_cur);
1061 switch (item->att_type) {
1062 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1063 switch (item->att_data.att_static->att_type) {
1064 case MAILIMAP_MSG_ATT_UID:
1065 uid = item->att_data.att_static->att_data.att_uid;
1071 puid = malloc(sizeof(* puid));
1073 res = MAILIMAP_ERROR_MEMORY;
1078 r = carray_add(tab, puid, NULL);
1081 res = MAILIMAP_ERROR_MEMORY;
1088 return MAILIMAP_NO_ERROR;
1091 for(i = 0 ; i < carray_count(tab) ; i++)
1092 mailmessage_free(carray_get(tab, i));
1097 static int imap_get_messages_list(mailimap * imap,
1098 uint32_t first_index,
1103 struct mailimap_fetch_att * fetch_att;
1104 struct mailimap_fetch_type * fetch_type;
1105 struct mailimap_set * set;
1106 clist * fetch_result;
1109 set = mailimap_set_new_interval(first_index, 0);
1111 res = MAILIMAP_ERROR_MEMORY;
1115 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1116 if (fetch_type == NULL) {
1117 res = MAILIMAP_ERROR_MEMORY;
1121 fetch_att = mailimap_fetch_att_new_uid();
1122 if (fetch_att == NULL) {
1123 res = MAILIMAP_ERROR_MEMORY;
1124 goto free_fetch_type;
1127 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1128 if (r != MAILIMAP_NO_ERROR) {
1129 mailimap_fetch_att_free(fetch_att);
1130 res = MAILIMAP_ERROR_MEMORY;
1131 goto free_fetch_type;
1134 r = mailimap_uid_fetch(imap, set,
1135 fetch_type, &fetch_result);
1137 mailimap_fetch_type_free(fetch_type);
1138 mailimap_set_free(set);
1140 if (r != MAILIMAP_NO_ERROR) {
1146 r = uid_list_to_env_list(fetch_result, &env_list);
1147 mailimap_fetch_list_free(fetch_result);
1149 * result = env_list;
1151 return MAILIMAP_NO_ERROR;
1154 mailimap_fetch_type_free(fetch_type);
1156 mailimap_set_free(set);
1164 struct fetch_uid_param {
1166 uint32_t first_index;
1169 struct fetch_uid_result {
1171 carray * fetch_result;
1174 static void fetch_uid_run(struct etpan_thread_op * op)
1176 struct fetch_uid_param * param;
1177 struct fetch_uid_result * result;
1178 carray * fetch_result;
1183 fetch_result = NULL;
1184 r = imap_get_messages_list(param->imap, param->first_index,
1187 result = op->result;
1189 result->fetch_result = fetch_result;
1190 debug_print("imap fetch_uid run - end %i\n", r);
1193 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1194 carray ** fetch_result)
1196 struct fetch_uid_param param;
1197 struct fetch_uid_result result;
1200 debug_print("imap fetch_uid - begin\n");
1202 imap = get_imap(folder);
1204 param.first_index = first_index;
1206 threaded_run(folder, ¶m, &result, fetch_uid_run);
1208 if (result.error != MAILIMAP_NO_ERROR)
1209 return result.error;
1211 debug_print("imap fetch_uid - end\n");
1213 * fetch_result = result.fetch_result;
1215 return result.error;
1219 void imap_fetch_uid_list_free(carray * uid_list)
1223 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1226 puid = carray_get(uid_list, i);
1229 carray_free(uid_list);
1234 static int imap_fetch(mailimap * imap,
1237 size_t * result_len)
1240 struct mailimap_set * set;
1241 struct mailimap_fetch_att * fetch_att;
1242 struct mailimap_fetch_type * fetch_type;
1243 clist * fetch_result;
1244 struct mailimap_msg_att * msg_att;
1245 struct mailimap_msg_att_item * msg_att_item;
1250 struct mailimap_section * section;
1252 set = mailimap_set_new_single(msg_index);
1254 res = MAILIMAP_ERROR_MEMORY;
1258 section = mailimap_section_new(NULL);
1259 if (section == NULL) {
1260 res = MAILIMAP_ERROR_MEMORY;
1264 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1265 if (fetch_att == NULL) {
1266 mailimap_section_free(section);
1267 res = MAILIMAP_ERROR_MEMORY;
1271 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1272 if (fetch_type == NULL) {
1273 res = MAILIMAP_ERROR_MEMORY;
1274 goto free_fetch_att;
1277 r = mailimap_uid_fetch(imap, set,
1278 fetch_type, &fetch_result);
1280 mailimap_fetch_type_free(fetch_type);
1281 mailimap_set_free(set);
1284 case MAILIMAP_NO_ERROR:
1290 if (clist_begin(fetch_result) == NULL) {
1291 mailimap_fetch_list_free(fetch_result);
1292 return MAILIMAP_ERROR_FETCH;
1295 msg_att = clist_begin(fetch_result)->data;
1300 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1301 cur = clist_next(cur)) {
1302 msg_att_item = clist_content(cur);
1304 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1305 if (msg_att_item->att_data.att_static->att_type ==
1306 MAILIMAP_MSG_ATT_BODY_SECTION) {
1307 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1309 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1311 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1316 mailimap_fetch_list_free(fetch_result);
1319 return MAILIMAP_ERROR_FETCH;
1322 * result_len = text_length;
1324 return MAILIMAP_NO_ERROR;
1327 mailimap_fetch_att_free(fetch_att);
1329 mailimap_set_free(set);
1334 static int imap_fetch_header(mailimap * imap,
1337 size_t * result_len)
1340 struct mailimap_set * set;
1341 struct mailimap_fetch_att * fetch_att;
1342 struct mailimap_fetch_type * fetch_type;
1343 clist * fetch_result;
1344 struct mailimap_msg_att * msg_att;
1345 struct mailimap_msg_att_item * msg_att_item;
1350 struct mailimap_section * section;
1352 set = mailimap_set_new_single(msg_index);
1354 res = MAILIMAP_ERROR_MEMORY;
1358 section = mailimap_section_new_header();
1359 if (section == NULL) {
1360 res = MAILIMAP_ERROR_MEMORY;
1364 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1365 if (fetch_att == NULL) {
1366 mailimap_section_free(section);
1367 res = MAILIMAP_ERROR_MEMORY;
1371 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1372 if (fetch_type == NULL) {
1373 res = MAILIMAP_ERROR_MEMORY;
1374 goto free_fetch_att;
1377 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1379 mailimap_fetch_type_free(fetch_type);
1380 mailimap_set_free(set);
1383 case MAILIMAP_NO_ERROR:
1389 if (clist_begin(fetch_result) == NULL) {
1390 mailimap_fetch_list_free(fetch_result);
1391 return MAILIMAP_ERROR_FETCH;
1394 msg_att = clist_begin(fetch_result)->data;
1399 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1400 cur = clist_next(cur)) {
1401 msg_att_item = clist_content(cur);
1403 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1404 if (msg_att_item->att_data.att_static->att_type ==
1405 MAILIMAP_MSG_ATT_BODY_SECTION) {
1406 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1407 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1409 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1414 mailimap_fetch_list_free(fetch_result);
1417 return MAILIMAP_ERROR_FETCH;
1420 * result_len = text_length;
1422 return MAILIMAP_NO_ERROR;
1425 mailimap_fetch_att_free(fetch_att);
1427 mailimap_set_free(set);
1434 struct fetch_content_param {
1437 const char * filename;
1441 struct fetch_content_result {
1445 static void fetch_content_run(struct etpan_thread_op * op)
1447 struct fetch_content_param * param;
1448 struct fetch_content_result * result;
1450 size_t content_size;
1459 if (param->with_body)
1460 r = imap_fetch(param->imap, param->msg_index,
1461 &content, &content_size);
1463 r = imap_fetch_header(param->imap, param->msg_index,
1464 &content, &content_size);
1466 result = op->result;
1469 if (r == MAILIMAP_NO_ERROR) {
1470 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1472 result->error = MAILIMAP_ERROR_FETCH;
1476 f = fdopen(fd, "wb");
1478 result->error = MAILIMAP_ERROR_FETCH;
1482 r = fwrite(content, 1, content_size, f);
1489 unlink(param->filename);
1500 unlink(param->filename);
1503 if (mmap_string_unref(content) != 0)
1507 debug_print("imap fetch_content run - end %i\n", r);
1510 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1512 const char * filename)
1514 struct fetch_content_param param;
1515 struct fetch_content_result result;
1518 debug_print("imap fetch_content - begin\n");
1520 imap = get_imap(folder);
1522 param.msg_index = msg_index;
1523 param.filename = filename;
1524 param.with_body = with_body;
1526 threaded_run(folder, ¶m, &result, fetch_content_run);
1528 if (result.error != MAILIMAP_NO_ERROR)
1529 return result.error;
1531 debug_print("imap fetch_content - end\n");
1533 return result.error;
1538 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1546 flag_list = att_dyn->att_list;
1547 if (flag_list == NULL)
1550 for(cur = clist_begin(flag_list) ; cur != NULL ;
1551 cur = clist_next(cur)) {
1552 struct mailimap_flag_fetch * flag_fetch;
1554 flag_fetch = clist_content(cur);
1555 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1558 switch (flag_fetch->fl_flag->fl_type) {
1559 case MAILIMAP_FLAG_ANSWERED:
1560 flags |= MSG_REPLIED;
1562 case MAILIMAP_FLAG_FLAGGED:
1563 flags |= MSG_MARKED;
1565 case MAILIMAP_FLAG_DELETED:
1566 flags |= MSG_DELETED;
1568 case MAILIMAP_FLAG_SEEN:
1569 flags &= ~MSG_UNREAD;
1579 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1583 struct mailimap_msg_att_dynamic ** patt_dyn)
1585 clistiter * item_cur;
1589 struct mailimap_msg_att_dynamic * att_dyn;
1596 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1597 item_cur = clist_next(item_cur)) {
1598 struct mailimap_msg_att_item * item;
1600 item = clist_content(item_cur);
1602 switch (item->att_type) {
1603 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1604 switch (item->att_data.att_static->att_type) {
1605 case MAILIMAP_MSG_ATT_UID:
1606 uid = item->att_data.att_static->att_data.att_uid;
1609 case MAILIMAP_MSG_ATT_BODY_SECTION:
1610 if (headers == NULL) {
1611 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1614 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1615 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1620 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1621 if (att_dyn == NULL) {
1622 att_dyn = item->att_data.att_dyn;
1630 if (pheaders != NULL)
1631 * pheaders = headers;
1632 if (pref_size != NULL)
1633 * pref_size = ref_size;
1634 if (patt_dyn != NULL)
1635 * patt_dyn = att_dyn;
1637 return MAIL_NO_ERROR;
1640 static struct imap_fetch_env_info *
1641 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1643 struct imap_fetch_env_info * info;
1647 struct mailimap_msg_att_dynamic * att_dyn;
1649 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1652 info = malloc(sizeof(* info));
1654 info->headers = strdup(headers);
1656 info->flags = imap_flags_to_flags(att_dyn);
1662 imap_fetch_result_to_envelop_list(clist * fetch_result,
1663 carray ** p_env_list)
1671 env_list = carray_new(16);
1673 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1674 cur = clist_next(cur)) {
1675 struct mailimap_msg_att * msg_att;
1676 struct imap_fetch_env_info * env_info;
1678 msg_att = clist_content(cur);
1680 env_info = fetch_to_env_info(msg_att);
1681 carray_add(env_list, env_info, NULL);
1684 * p_env_list = env_list;
1686 return MAIL_NO_ERROR;
1689 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1691 struct mailimap_fetch_att * fetch_att;
1695 struct mailimap_header_list * imap_hdrlist;
1696 struct mailimap_section * section;
1698 hdrlist = clist_new();
1700 header = strdup("Date");
1701 r = clist_append(hdrlist, header);
1702 header = strdup("From");
1703 r = clist_append(hdrlist, header);
1704 header = strdup("To");
1705 r = clist_append(hdrlist, header);
1706 header = strdup("Cc");
1707 r = clist_append(hdrlist, header);
1708 header = strdup("Subject");
1709 r = clist_append(hdrlist, header);
1710 header = strdup("Message-ID");
1711 r = clist_append(hdrlist, header);
1712 header = strdup("References");
1713 r = clist_append(hdrlist, header);
1714 header = strdup("In-Reply-To");
1715 r = clist_append(hdrlist, header);
1717 imap_hdrlist = mailimap_header_list_new(hdrlist);
1718 section = mailimap_section_new_header_fields(imap_hdrlist);
1719 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1720 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1722 return MAIL_NO_ERROR;
1725 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1727 struct mailimap_fetch_att * fetch_att;
1728 struct mailimap_section * section;
1730 section = mailimap_section_new_header();
1731 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1732 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1734 return MAIL_NO_ERROR;
1738 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1739 carray ** p_env_list)
1741 struct mailimap_fetch_att * fetch_att;
1742 struct mailimap_fetch_type * fetch_type;
1744 clist * fetch_result;
1750 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1753 fetch_att = mailimap_fetch_att_new_uid();
1754 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1757 fetch_att = mailimap_fetch_att_new_flags();
1758 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1761 fetch_att = mailimap_fetch_att_new_rfc822_size();
1762 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1766 key.len = sizeof(imap);
1767 r = chash_get(courier_workaround_hash, &key, &value);
1769 r = imap_add_envelope_fetch_att(fetch_type);
1771 r = imap_add_header_fetch_att(fetch_type);
1773 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1776 case MAILIMAP_NO_ERROR:
1779 mailimap_fetch_type_free(fetch_type);
1783 if (clist_begin(fetch_result) == NULL) {
1784 res = MAILIMAP_ERROR_FETCH;
1788 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1789 mailimap_fetch_list_free(fetch_result);
1791 if (r != MAILIMAP_NO_ERROR) {
1792 mailimap_fetch_type_free(fetch_type);
1793 res = MAILIMAP_ERROR_MEMORY;
1797 mailimap_fetch_type_free(fetch_type);
1799 * p_env_list = env_list;
1801 return MAILIMAP_NO_ERROR;
1807 struct fetch_env_param {
1809 struct mailimap_set * set;
1812 struct fetch_env_result {
1813 carray * fetch_env_result;
1817 static void fetch_env_run(struct etpan_thread_op * op)
1819 struct fetch_env_param * param;
1820 struct fetch_env_result * result;
1827 r = imap_get_envelopes_list(param->imap, param->set,
1830 result = op->result;
1832 result->fetch_env_result = env_list;
1834 debug_print("imap fetch_env run - end %i\n", r);
1837 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1838 carray ** p_env_list)
1840 struct fetch_env_param param;
1841 struct fetch_env_result result;
1844 debug_print("imap fetch_env - begin\n");
1846 imap = get_imap(folder);
1850 threaded_run(folder, ¶m, &result, fetch_env_run);
1852 if (result.error != MAILIMAP_NO_ERROR) {
1858 key.len = sizeof(imap);
1859 r = chash_get(courier_workaround_hash, &key, &value);
1863 chash_set(courier_workaround_hash, &key, &value, NULL);
1865 threaded_run(folder, ¶m, &result, fetch_env_run);
1869 if (result.error != MAILIMAP_NO_ERROR)
1870 return result.error;
1872 debug_print("imap fetch_env - end\n");
1874 * p_env_list = result.fetch_env_result;
1876 return result.error;
1879 void imap_fetch_env_free(carray * env_list)
1883 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1884 struct imap_fetch_env_info * env_info;
1886 env_info = carray_get(env_list, i);
1887 free(env_info->headers);
1890 carray_free(env_list);
1897 struct append_param {
1899 const char * mailbox;
1900 const char * filename;
1901 struct mailimap_flag_list * flag_list;
1904 struct append_result {
1908 static void append_run(struct etpan_thread_op * op)
1910 struct append_param * param;
1911 struct append_result * result;
1915 struct stat stat_buf;
1919 result = op->result;
1921 r = stat(param->filename, &stat_buf);
1923 result->error = MAILIMAP_ERROR_APPEND;
1926 size = stat_buf.st_size;
1928 fd = open(param->filename, O_RDONLY);
1930 result->error = MAILIMAP_ERROR_APPEND;
1934 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1935 if (data == (void *) MAP_FAILED) {
1937 result->error = MAILIMAP_ERROR_APPEND;
1941 r = mailimap_append(param->imap, param->mailbox,
1942 param->flag_list, NULL,
1950 debug_print("imap append run - end %i\n", r);
1953 int imap_threaded_append(Folder * folder, const char * mailbox,
1954 const char * filename,
1955 struct mailimap_flag_list * flag_list)
1957 struct append_param param;
1958 struct append_result result;
1961 debug_print("imap append - begin\n");
1963 imap = get_imap(folder);
1965 param.mailbox = mailbox;
1966 param.filename = filename;
1967 param.flag_list = flag_list;
1969 threaded_run(folder, ¶m, &result, append_run);
1971 if (result.error != MAILIMAP_NO_ERROR)
1972 return result.error;
1974 debug_print("imap append - end\n");
1976 return result.error;
1982 struct expunge_param {
1986 struct expunge_result {
1990 static void expunge_run(struct etpan_thread_op * op)
1992 struct expunge_param * param;
1993 struct expunge_result * result;
1997 r = mailimap_expunge(param->imap);
1999 result = op->result;
2001 debug_print("imap expunge run - end %i\n", r);
2004 int imap_threaded_expunge(Folder * folder)
2006 struct expunge_param param;
2007 struct expunge_result result;
2009 debug_print("imap expunge - begin\n");
2011 param.imap = get_imap(folder);
2013 threaded_run(folder, ¶m, &result, expunge_run);
2015 debug_print("imap expunge - end\n");
2017 return result.error;
2023 struct mailimap_set * set;
2027 struct copy_result {
2031 static void copy_run(struct etpan_thread_op * op)
2033 struct copy_param * param;
2034 struct copy_result * result;
2039 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2041 result = op->result;
2044 debug_print("imap copy run - end %i\n", r);
2047 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2050 struct copy_param param;
2051 struct copy_result result;
2054 debug_print("imap copy - begin\n");
2056 imap = get_imap(folder);
2061 threaded_run(folder, ¶m, &result, copy_run);
2063 if (result.error != MAILIMAP_NO_ERROR)
2064 return result.error;
2066 debug_print("imap copy - end\n");
2068 return result.error;
2073 struct store_param {
2075 struct mailimap_set * set;
2076 struct mailimap_store_att_flags * store_att_flags;
2079 struct store_result {
2083 static void store_run(struct etpan_thread_op * op)
2085 struct store_param * param;
2086 struct store_result * result;
2091 r = mailimap_uid_store(param->imap, param->set,
2092 param->store_att_flags);
2094 result = op->result;
2097 debug_print("imap store run - end %i\n", r);
2100 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2101 struct mailimap_store_att_flags * store_att_flags)
2103 struct store_param param;
2104 struct store_result result;
2107 debug_print("imap store - begin\n");
2109 imap = get_imap(folder);
2112 param.store_att_flags = store_att_flags;
2114 threaded_run(folder, ¶m, &result, store_run);
2116 if (result.error != MAILIMAP_NO_ERROR)
2117 return result.error;
2119 debug_print("imap store - end\n");
2121 return result.error;
2126 static void do_exec_command(int fd, const char * command,
2127 const char * servername, uint16_t port)
2132 /* Fork again to become a child of init rather than
2133 the etpan client. */
2138 setenv("ETPANSERVER", servername, 1);
2140 unsetenv("ETPANSERVER");
2145 snprintf(porttext, sizeof(porttext), "%d", port);
2146 setenv("ETPANPORT", porttext, 1);
2149 unsetenv("ETPANPORT");
2152 /* Not a lot we can do if there's an error other than bail. */
2153 if (dup2(fd, 0) == -1)
2155 if (dup2(fd, 1) == -1)
2158 /* Should we close stderr and reopen /dev/null? */
2160 maxopen = sysconf(_SC_OPEN_MAX);
2161 for (i=3; i < maxopen; i++)
2165 /* Detach from the controlling tty if we have one. Otherwise,
2166 SSH might do something stupid like trying to use it instead
2167 of running $SSH_ASKPASS. Doh. */
2168 fd = open("/dev/tty", O_RDONLY);
2170 ioctl(fd, TIOCNOTTY, NULL);
2173 #endif /* TIOCNOTTY */
2175 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2177 /* Eep. Shouldn't reach this */
2181 static int subcommand_connect(const char *command,
2182 const char *servername, uint16_t port)
2184 /* SEB unsupported on Windows */
2188 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2193 do_exec_command(sockfds[1], command, servername, port);
2195 else if (childpid == -1) {
2203 /* Reap child, leaving grandchild process to run */
2204 waitpid(childpid, NULL, 0);
2209 int socket_connect_cmd(mailimap * imap, const char * command,
2210 const char * server, int port)
2216 fd = subcommand_connect(command, server, port);
2218 return MAILIMAP_ERROR_STREAM;
2220 s = mailstream_socket_open(fd);
2223 return MAILIMAP_ERROR_STREAM;
2226 r = mailimap_connect(imap, s);
2227 if (r != MAILIMAP_NO_ERROR) {
2228 mailstream_close(s);
2232 return MAILIMAP_NO_ERROR;
2237 struct connect_cmd_param {
2239 const char * command;
2240 const char * server;
2244 struct connect_cmd_result {
2248 static void connect_cmd_run(struct etpan_thread_op * op)
2251 struct connect_cmd_param * param;
2252 struct connect_cmd_result * result;
2255 result = op->result;
2257 r = socket_connect_cmd(param->imap, param->command,
2258 param->server, param->port);
2264 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2265 const char * server, int port)
2267 struct connect_cmd_param param;
2268 struct connect_cmd_result result;
2273 imap = mailimap_new(0, NULL);
2276 key.len = sizeof(folder);
2279 chash_set(session_hash, &key, &value, NULL);
2282 param.command = command;
2283 param.server = server;
2286 threaded_run(folder, ¶m, &result, connect_cmd_run);
2288 debug_print("connect_cmd ok %i\n", result.error);
2290 return result.error;
2294 void imap_main_init(void)
2297 void imap_main_done(void)
2300 void imap_main_set_timeout(int sec)