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 int imap_threaded_starttls(Folder * folder)
628 struct starttls_param param;
629 struct starttls_result result;
631 debug_print("imap starttls - begin\n");
633 param.imap = get_imap(folder);
635 threaded_run(folder, ¶m, &result, starttls_run);
637 debug_print("imap starttls - end\n");
644 struct create_param {
649 struct create_result {
653 static void create_run(struct etpan_thread_op * op)
655 struct create_param * param;
656 struct create_result * result;
660 r = mailimap_create(param->imap, param->mb);
664 debug_print("imap create run - end %i\n", r);
667 int imap_threaded_create(Folder * folder, const char * mb)
669 struct create_param param;
670 struct create_result result;
672 debug_print("imap create - begin\n");
674 param.imap = get_imap(folder);
677 threaded_run(folder, ¶m, &result, create_run);
679 debug_print("imap create - end\n");
687 struct rename_param {
690 const char * new_name;
693 struct rename_result {
697 static void rename_run(struct etpan_thread_op * op)
699 struct rename_param * param;
700 struct rename_result * result;
704 r = mailimap_rename(param->imap, param->mb, param->new_name);
708 debug_print("imap rename run - end %i\n", r);
711 int imap_threaded_rename(Folder * folder,
712 const char * mb, const char * new_name)
714 struct rename_param param;
715 struct rename_result result;
717 debug_print("imap rename - begin\n");
719 param.imap = get_imap(folder);
721 param.new_name = new_name;
723 threaded_run(folder, ¶m, &result, rename_run);
725 debug_print("imap rename - end\n");
733 struct delete_param {
738 struct delete_result {
742 static void delete_run(struct etpan_thread_op * op)
744 struct delete_param * param;
745 struct delete_result * result;
749 r = mailimap_delete(param->imap, param->mb);
753 debug_print("imap delete run - end %i\n", r);
756 int imap_threaded_delete(Folder * folder, const char * mb)
758 struct delete_param param;
759 struct delete_result result;
761 debug_print("imap delete - begin\n");
763 param.imap = get_imap(folder);
766 threaded_run(folder, ¶m, &result, delete_run);
768 debug_print("imap delete - end\n");
775 struct select_param {
780 struct select_result {
784 static void select_run(struct etpan_thread_op * op)
786 struct select_param * param;
787 struct select_result * result;
791 r = mailimap_select(param->imap, param->mb);
795 debug_print("imap select run - end %i\n", r);
798 int imap_threaded_select(Folder * folder, const char * mb,
799 gint * exists, gint * recent, gint * unseen,
800 guint32 * uid_validity)
802 struct select_param param;
803 struct select_result result;
806 debug_print("imap select - begin\n");
808 imap = get_imap(folder);
812 threaded_run(folder, ¶m, &result, select_run);
814 if (result.error != MAILIMAP_NO_ERROR)
817 if (imap->imap_selection_info == NULL)
818 return MAILIMAP_ERROR_PARSE;
820 * exists = imap->imap_selection_info->sel_exists;
821 * recent = imap->imap_selection_info->sel_recent;
822 * unseen = imap->imap_selection_info->sel_unseen;
823 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
825 debug_print("imap select - end\n");
832 struct examine_param {
837 struct examine_result {
841 static void examine_run(struct etpan_thread_op * op)
843 struct examine_param * param;
844 struct examine_result * result;
848 r = mailimap_examine(param->imap, param->mb);
852 debug_print("imap examine run - end %i\n", r);
855 int imap_threaded_examine(Folder * folder, const char * mb,
856 gint * exists, gint * recent, gint * unseen,
857 guint32 * uid_validity)
859 struct examine_param param;
860 struct examine_result result;
863 debug_print("imap examine - begin\n");
865 imap = get_imap(folder);
869 threaded_run(folder, ¶m, &result, examine_run);
871 if (result.error != MAILIMAP_NO_ERROR)
874 if (imap->imap_selection_info == NULL)
875 return MAILIMAP_ERROR_PARSE;
877 * exists = imap->imap_selection_info->sel_exists;
878 * recent = imap->imap_selection_info->sel_recent;
879 * unseen = imap->imap_selection_info->sel_unseen;
880 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
882 debug_print("imap examine - end\n");
890 struct search_param {
893 struct mailimap_set * set;
896 struct search_result {
898 clist * search_result;
901 static void search_run(struct etpan_thread_op * op)
903 struct search_param * param;
904 struct search_result * result;
906 struct mailimap_search_key * key;
907 struct mailimap_search_key * uid_key;
908 struct mailimap_search_key * search_type_key;
909 clist * search_result;
913 uid_key = mailimap_search_key_new_uid(param->set);
915 search_type_key = NULL;
916 switch (param->type) {
917 case IMAP_SEARCH_TYPE_SIMPLE:
918 search_type_key = NULL;
921 case IMAP_SEARCH_TYPE_SEEN:
922 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
923 NULL, NULL, NULL, NULL, NULL,
924 NULL, NULL, NULL, NULL, NULL,
925 NULL, NULL, NULL, NULL, 0,
926 NULL, NULL, NULL, NULL, NULL,
927 NULL, 0, NULL, NULL, NULL);
930 case IMAP_SEARCH_TYPE_UNSEEN:
931 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
932 NULL, NULL, NULL, NULL, NULL,
933 NULL, NULL, NULL, NULL, NULL,
934 NULL, NULL, NULL, NULL, 0,
935 NULL, NULL, NULL, NULL, NULL,
936 NULL, 0, NULL, NULL, NULL);
939 case IMAP_SEARCH_TYPE_ANSWERED:
940 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
941 NULL, NULL, NULL, NULL, NULL,
942 NULL, NULL, NULL, NULL, NULL,
943 NULL, NULL, NULL, NULL, 0,
944 NULL, NULL, NULL, NULL, NULL,
945 NULL, 0, NULL, NULL, NULL);
948 case IMAP_SEARCH_TYPE_FLAGGED:
949 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
950 NULL, NULL, NULL, NULL, NULL,
951 NULL, NULL, NULL, NULL, NULL,
952 NULL, NULL, NULL, NULL, 0,
953 NULL, NULL, NULL, NULL, NULL,
954 NULL, 0, NULL, NULL, NULL);
958 if (search_type_key != NULL) {
959 key = mailimap_search_key_new_multiple_empty();
960 mailimap_search_key_multiple_add(key, search_type_key);
961 mailimap_search_key_multiple_add(key, uid_key);
967 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
971 result->search_result = search_result;
972 debug_print("imap search run - end %i\n", r);
975 int imap_threaded_search(Folder * folder, int search_type,
976 struct mailimap_set * set, clist ** search_result)
978 struct search_param param;
979 struct search_result result;
982 debug_print("imap search - begin\n");
984 imap = get_imap(folder);
987 param.type = search_type;
989 threaded_run(folder, ¶m, &result, search_run);
991 if (result.error != MAILIMAP_NO_ERROR)
994 debug_print("imap search - end\n");
996 * search_result = result.search_result;
1005 uid_list_to_env_list(clist * fetch_result, carray ** result)
1013 tab = carray_new(128);
1015 res = MAILIMAP_ERROR_MEMORY;
1019 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1020 cur = clist_next(cur)) {
1021 struct mailimap_msg_att * msg_att;
1022 clistiter * item_cur;
1027 msg_att = clist_content(cur);
1031 for(item_cur = clist_begin(msg_att->att_list) ;
1033 item_cur = clist_next(item_cur)) {
1034 struct mailimap_msg_att_item * item;
1036 item = clist_content(item_cur);
1038 switch (item->att_type) {
1039 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1040 switch (item->att_data.att_static->att_type) {
1041 case MAILIMAP_MSG_ATT_UID:
1042 uid = item->att_data.att_static->att_data.att_uid;
1048 puid = malloc(sizeof(* puid));
1050 res = MAILIMAP_ERROR_MEMORY;
1055 r = carray_add(tab, puid, NULL);
1058 res = MAILIMAP_ERROR_MEMORY;
1065 return MAILIMAP_NO_ERROR;
1068 for(i = 0 ; i < carray_count(tab) ; i++)
1069 mailmessage_free(carray_get(tab, i));
1074 static int imap_get_messages_list(mailimap * imap,
1075 uint32_t first_index,
1080 struct mailimap_fetch_att * fetch_att;
1081 struct mailimap_fetch_type * fetch_type;
1082 struct mailimap_set * set;
1083 clist * fetch_result;
1086 set = mailimap_set_new_interval(first_index, 0);
1088 res = MAILIMAP_ERROR_MEMORY;
1092 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1093 if (fetch_type == NULL) {
1094 res = MAILIMAP_ERROR_MEMORY;
1098 fetch_att = mailimap_fetch_att_new_uid();
1099 if (fetch_att == NULL) {
1100 res = MAILIMAP_ERROR_MEMORY;
1101 goto free_fetch_type;
1104 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1105 if (r != MAILIMAP_NO_ERROR) {
1106 mailimap_fetch_att_free(fetch_att);
1107 res = MAILIMAP_ERROR_MEMORY;
1108 goto free_fetch_type;
1111 r = mailimap_uid_fetch(imap, set,
1112 fetch_type, &fetch_result);
1114 mailimap_fetch_type_free(fetch_type);
1115 mailimap_set_free(set);
1117 if (r != MAILIMAP_NO_ERROR) {
1123 r = uid_list_to_env_list(fetch_result, &env_list);
1124 mailimap_fetch_list_free(fetch_result);
1126 * result = env_list;
1128 return MAILIMAP_NO_ERROR;
1131 mailimap_fetch_type_free(fetch_type);
1133 mailimap_set_free(set);
1141 struct fetch_uid_param {
1143 uint32_t first_index;
1146 struct fetch_uid_result {
1148 carray * fetch_result;
1151 static void fetch_uid_run(struct etpan_thread_op * op)
1153 struct fetch_uid_param * param;
1154 struct fetch_uid_result * result;
1155 carray * fetch_result;
1160 fetch_result = NULL;
1161 r = imap_get_messages_list(param->imap, param->first_index,
1164 result = op->result;
1166 result->fetch_result = fetch_result;
1167 debug_print("imap fetch_uid run - end %i\n", r);
1170 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1171 carray ** fetch_result)
1173 struct fetch_uid_param param;
1174 struct fetch_uid_result result;
1177 debug_print("imap fetch_uid - begin\n");
1179 imap = get_imap(folder);
1181 param.first_index = first_index;
1183 threaded_run(folder, ¶m, &result, fetch_uid_run);
1185 if (result.error != MAILIMAP_NO_ERROR)
1186 return result.error;
1188 debug_print("imap fetch_uid - end\n");
1190 * fetch_result = result.fetch_result;
1192 return result.error;
1196 void imap_fetch_uid_list_free(carray * uid_list)
1200 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1203 puid = carray_get(uid_list, i);
1206 carray_free(uid_list);
1211 static int imap_fetch(mailimap * imap,
1214 size_t * result_len)
1217 struct mailimap_set * set;
1218 struct mailimap_fetch_att * fetch_att;
1219 struct mailimap_fetch_type * fetch_type;
1220 clist * fetch_result;
1221 struct mailimap_msg_att * msg_att;
1222 struct mailimap_msg_att_item * msg_att_item;
1227 struct mailimap_section * section;
1229 set = mailimap_set_new_single(msg_index);
1231 res = MAILIMAP_ERROR_MEMORY;
1235 section = mailimap_section_new(NULL);
1236 if (section == NULL) {
1237 res = MAILIMAP_ERROR_MEMORY;
1241 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1242 if (fetch_att == NULL) {
1243 mailimap_section_free(section);
1244 res = MAILIMAP_ERROR_MEMORY;
1248 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1249 if (fetch_type == NULL) {
1250 res = MAILIMAP_ERROR_MEMORY;
1251 goto free_fetch_att;
1254 r = mailimap_uid_fetch(imap, set,
1255 fetch_type, &fetch_result);
1257 mailimap_fetch_type_free(fetch_type);
1258 mailimap_set_free(set);
1261 case MAILIMAP_NO_ERROR:
1267 if (clist_begin(fetch_result) == NULL) {
1268 mailimap_fetch_list_free(fetch_result);
1269 return MAILIMAP_ERROR_FETCH;
1272 msg_att = clist_begin(fetch_result)->data;
1277 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1278 cur = clist_next(cur)) {
1279 msg_att_item = clist_content(cur);
1281 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1282 if (msg_att_item->att_data.att_static->att_type ==
1283 MAILIMAP_MSG_ATT_BODY_SECTION) {
1284 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1286 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1288 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1293 mailimap_fetch_list_free(fetch_result);
1296 return MAILIMAP_ERROR_FETCH;
1299 * result_len = text_length;
1301 return MAILIMAP_NO_ERROR;
1304 mailimap_fetch_att_free(fetch_att);
1306 mailimap_set_free(set);
1311 static int imap_fetch_header(mailimap * imap,
1314 size_t * result_len)
1317 struct mailimap_set * set;
1318 struct mailimap_fetch_att * fetch_att;
1319 struct mailimap_fetch_type * fetch_type;
1320 clist * fetch_result;
1321 struct mailimap_msg_att * msg_att;
1322 struct mailimap_msg_att_item * msg_att_item;
1327 struct mailimap_section * section;
1329 set = mailimap_set_new_single(msg_index);
1331 res = MAILIMAP_ERROR_MEMORY;
1335 section = mailimap_section_new_header();
1336 if (section == NULL) {
1337 res = MAILIMAP_ERROR_MEMORY;
1341 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1342 if (fetch_att == NULL) {
1343 mailimap_section_free(section);
1344 res = MAILIMAP_ERROR_MEMORY;
1348 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1349 if (fetch_type == NULL) {
1350 res = MAILIMAP_ERROR_MEMORY;
1351 goto free_fetch_att;
1354 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1356 mailimap_fetch_type_free(fetch_type);
1357 mailimap_set_free(set);
1360 case MAILIMAP_NO_ERROR:
1366 if (clist_begin(fetch_result) == NULL) {
1367 mailimap_fetch_list_free(fetch_result);
1368 return MAILIMAP_ERROR_FETCH;
1371 msg_att = clist_begin(fetch_result)->data;
1376 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1377 cur = clist_next(cur)) {
1378 msg_att_item = clist_content(cur);
1380 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1381 if (msg_att_item->att_data.att_static->att_type ==
1382 MAILIMAP_MSG_ATT_BODY_SECTION) {
1383 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1384 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1386 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1391 mailimap_fetch_list_free(fetch_result);
1394 return MAILIMAP_ERROR_FETCH;
1397 * result_len = text_length;
1399 return MAILIMAP_NO_ERROR;
1402 mailimap_fetch_att_free(fetch_att);
1404 mailimap_set_free(set);
1411 struct fetch_content_param {
1414 const char * filename;
1418 struct fetch_content_result {
1422 static void fetch_content_run(struct etpan_thread_op * op)
1424 struct fetch_content_param * param;
1425 struct fetch_content_result * result;
1427 size_t content_size;
1436 if (param->with_body)
1437 r = imap_fetch(param->imap, param->msg_index,
1438 &content, &content_size);
1440 r = imap_fetch_header(param->imap, param->msg_index,
1441 &content, &content_size);
1443 result = op->result;
1446 if (r == MAILIMAP_NO_ERROR) {
1447 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1449 result->error = MAILIMAP_ERROR_FETCH;
1453 f = fdopen(fd, "wb");
1455 result->error = MAILIMAP_ERROR_FETCH;
1459 r = fwrite(content, 1, content_size, f);
1466 unlink(param->filename);
1477 unlink(param->filename);
1480 if (mmap_string_unref(content) != 0)
1484 debug_print("imap fetch_content run - end %i\n", r);
1487 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1489 const char * filename)
1491 struct fetch_content_param param;
1492 struct fetch_content_result result;
1495 debug_print("imap fetch_content - begin\n");
1497 imap = get_imap(folder);
1499 param.msg_index = msg_index;
1500 param.filename = filename;
1501 param.with_body = with_body;
1503 threaded_run(folder, ¶m, &result, fetch_content_run);
1505 if (result.error != MAILIMAP_NO_ERROR)
1506 return result.error;
1508 debug_print("imap fetch_content - end\n");
1510 return result.error;
1515 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1523 flag_list = att_dyn->att_list;
1524 if (flag_list == NULL)
1527 for(cur = clist_begin(flag_list) ; cur != NULL ;
1528 cur = clist_next(cur)) {
1529 struct mailimap_flag_fetch * flag_fetch;
1531 flag_fetch = clist_content(cur);
1532 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1535 switch (flag_fetch->fl_flag->fl_type) {
1536 case MAILIMAP_FLAG_ANSWERED:
1537 flags |= MSG_REPLIED;
1539 case MAILIMAP_FLAG_FLAGGED:
1540 flags |= MSG_MARKED;
1542 case MAILIMAP_FLAG_DELETED:
1543 flags |= MSG_DELETED;
1545 case MAILIMAP_FLAG_SEEN:
1546 flags &= ~MSG_UNREAD;
1556 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1560 struct mailimap_msg_att_dynamic ** patt_dyn)
1562 clistiter * item_cur;
1566 struct mailimap_msg_att_dynamic * att_dyn;
1573 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1574 item_cur = clist_next(item_cur)) {
1575 struct mailimap_msg_att_item * item;
1577 item = clist_content(item_cur);
1579 switch (item->att_type) {
1580 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1581 switch (item->att_data.att_static->att_type) {
1582 case MAILIMAP_MSG_ATT_UID:
1583 uid = item->att_data.att_static->att_data.att_uid;
1586 case MAILIMAP_MSG_ATT_BODY_SECTION:
1587 if (headers == NULL) {
1588 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1591 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1592 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1597 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1598 if (att_dyn == NULL) {
1599 att_dyn = item->att_data.att_dyn;
1607 if (pheaders != NULL)
1608 * pheaders = headers;
1609 if (pref_size != NULL)
1610 * pref_size = ref_size;
1611 if (patt_dyn != NULL)
1612 * patt_dyn = att_dyn;
1614 return MAIL_NO_ERROR;
1617 static struct imap_fetch_env_info *
1618 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1620 struct imap_fetch_env_info * info;
1624 struct mailimap_msg_att_dynamic * att_dyn;
1626 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1629 info = malloc(sizeof(* info));
1631 info->headers = strdup(headers);
1633 info->flags = imap_flags_to_flags(att_dyn);
1639 imap_fetch_result_to_envelop_list(clist * fetch_result,
1640 carray ** p_env_list)
1648 env_list = carray_new(16);
1650 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1651 cur = clist_next(cur)) {
1652 struct mailimap_msg_att * msg_att;
1653 struct imap_fetch_env_info * env_info;
1655 msg_att = clist_content(cur);
1657 env_info = fetch_to_env_info(msg_att);
1658 carray_add(env_list, env_info, NULL);
1661 * p_env_list = env_list;
1663 return MAIL_NO_ERROR;
1666 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1668 struct mailimap_fetch_att * fetch_att;
1672 struct mailimap_header_list * imap_hdrlist;
1673 struct mailimap_section * section;
1675 hdrlist = clist_new();
1677 header = strdup("Date");
1678 r = clist_append(hdrlist, header);
1679 header = strdup("From");
1680 r = clist_append(hdrlist, header);
1681 header = strdup("To");
1682 r = clist_append(hdrlist, header);
1683 header = strdup("Cc");
1684 r = clist_append(hdrlist, header);
1685 header = strdup("Subject");
1686 r = clist_append(hdrlist, header);
1687 header = strdup("Message-ID");
1688 r = clist_append(hdrlist, header);
1689 header = strdup("References");
1690 r = clist_append(hdrlist, header);
1691 header = strdup("In-Reply-To");
1692 r = clist_append(hdrlist, header);
1694 imap_hdrlist = mailimap_header_list_new(hdrlist);
1695 section = mailimap_section_new_header_fields(imap_hdrlist);
1696 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1697 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1699 return MAIL_NO_ERROR;
1702 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1704 struct mailimap_fetch_att * fetch_att;
1705 struct mailimap_section * section;
1707 section = mailimap_section_new_header();
1708 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1709 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1711 return MAIL_NO_ERROR;
1715 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1716 carray ** p_env_list)
1718 struct mailimap_fetch_att * fetch_att;
1719 struct mailimap_fetch_type * fetch_type;
1721 clist * fetch_result;
1727 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1730 fetch_att = mailimap_fetch_att_new_uid();
1731 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1734 fetch_att = mailimap_fetch_att_new_flags();
1735 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1738 fetch_att = mailimap_fetch_att_new_rfc822_size();
1739 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1743 key.len = sizeof(imap);
1744 r = chash_get(courier_workaround_hash, &key, &value);
1746 r = imap_add_envelope_fetch_att(fetch_type);
1748 r = imap_add_header_fetch_att(fetch_type);
1750 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1753 case MAILIMAP_NO_ERROR:
1756 mailimap_fetch_type_free(fetch_type);
1760 if (clist_begin(fetch_result) == NULL) {
1761 res = MAILIMAP_ERROR_FETCH;
1765 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1766 mailimap_fetch_list_free(fetch_result);
1768 if (r != MAILIMAP_NO_ERROR) {
1769 mailimap_fetch_type_free(fetch_type);
1770 res = MAILIMAP_ERROR_MEMORY;
1774 mailimap_fetch_type_free(fetch_type);
1776 * p_env_list = env_list;
1778 return MAILIMAP_NO_ERROR;
1784 struct fetch_env_param {
1786 struct mailimap_set * set;
1789 struct fetch_env_result {
1790 carray * fetch_env_result;
1794 static void fetch_env_run(struct etpan_thread_op * op)
1796 struct fetch_env_param * param;
1797 struct fetch_env_result * result;
1804 r = imap_get_envelopes_list(param->imap, param->set,
1807 result = op->result;
1809 result->fetch_env_result = env_list;
1811 debug_print("imap fetch_env run - end %i\n", r);
1814 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1815 carray ** p_env_list)
1817 struct fetch_env_param param;
1818 struct fetch_env_result result;
1821 debug_print("imap fetch_env - begin\n");
1823 imap = get_imap(folder);
1827 threaded_run(folder, ¶m, &result, fetch_env_run);
1829 if (result.error != MAILIMAP_NO_ERROR) {
1835 key.len = sizeof(imap);
1836 r = chash_get(courier_workaround_hash, &key, &value);
1840 chash_set(courier_workaround_hash, &key, &value, NULL);
1842 threaded_run(folder, ¶m, &result, fetch_env_run);
1846 if (result.error != MAILIMAP_NO_ERROR)
1847 return result.error;
1849 debug_print("imap fetch_env - end\n");
1851 * p_env_list = result.fetch_env_result;
1853 return result.error;
1856 void imap_fetch_env_free(carray * env_list)
1860 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1861 struct imap_fetch_env_info * env_info;
1863 env_info = carray_get(env_list, i);
1864 free(env_info->headers);
1867 carray_free(env_list);
1874 struct append_param {
1876 const char * mailbox;
1877 const char * filename;
1878 struct mailimap_flag_list * flag_list;
1881 struct append_result {
1885 static void append_run(struct etpan_thread_op * op)
1887 struct append_param * param;
1888 struct append_result * result;
1892 struct stat stat_buf;
1896 result = op->result;
1898 r = stat(param->filename, &stat_buf);
1900 result->error = MAILIMAP_ERROR_APPEND;
1903 size = stat_buf.st_size;
1905 fd = open(param->filename, O_RDONLY);
1907 result->error = MAILIMAP_ERROR_APPEND;
1911 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1912 if (data == (void *) MAP_FAILED) {
1914 result->error = MAILIMAP_ERROR_APPEND;
1918 r = mailimap_append(param->imap, param->mailbox,
1919 param->flag_list, NULL,
1927 debug_print("imap append run - end %i\n", r);
1930 int imap_threaded_append(Folder * folder, const char * mailbox,
1931 const char * filename,
1932 struct mailimap_flag_list * flag_list)
1934 struct append_param param;
1935 struct append_result result;
1938 debug_print("imap append - begin\n");
1940 imap = get_imap(folder);
1942 param.mailbox = mailbox;
1943 param.filename = filename;
1944 param.flag_list = flag_list;
1946 threaded_run(folder, ¶m, &result, append_run);
1948 if (result.error != MAILIMAP_NO_ERROR)
1949 return result.error;
1951 debug_print("imap append - end\n");
1953 return result.error;
1959 struct expunge_param {
1963 struct expunge_result {
1967 static void expunge_run(struct etpan_thread_op * op)
1969 struct expunge_param * param;
1970 struct expunge_result * result;
1974 r = mailimap_expunge(param->imap);
1976 result = op->result;
1978 debug_print("imap expunge run - end %i\n", r);
1981 int imap_threaded_expunge(Folder * folder)
1983 struct expunge_param param;
1984 struct expunge_result result;
1986 debug_print("imap expunge - begin\n");
1988 param.imap = get_imap(folder);
1990 threaded_run(folder, ¶m, &result, expunge_run);
1992 debug_print("imap expunge - end\n");
1994 return result.error;
2000 struct mailimap_set * set;
2004 struct copy_result {
2008 static void copy_run(struct etpan_thread_op * op)
2010 struct copy_param * param;
2011 struct copy_result * result;
2016 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2018 result = op->result;
2021 debug_print("imap copy run - end %i\n", r);
2024 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2027 struct copy_param param;
2028 struct copy_result result;
2031 debug_print("imap copy - begin\n");
2033 imap = get_imap(folder);
2038 threaded_run(folder, ¶m, &result, copy_run);
2040 if (result.error != MAILIMAP_NO_ERROR)
2041 return result.error;
2043 debug_print("imap copy - end\n");
2045 return result.error;
2050 struct store_param {
2052 struct mailimap_set * set;
2053 struct mailimap_store_att_flags * store_att_flags;
2056 struct store_result {
2060 static void store_run(struct etpan_thread_op * op)
2062 struct store_param * param;
2063 struct store_result * result;
2068 r = mailimap_uid_store(param->imap, param->set,
2069 param->store_att_flags);
2071 result = op->result;
2074 debug_print("imap store run - end %i\n", r);
2077 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2078 struct mailimap_store_att_flags * store_att_flags)
2080 struct store_param param;
2081 struct store_result result;
2084 debug_print("imap store - begin\n");
2086 imap = get_imap(folder);
2089 param.store_att_flags = store_att_flags;
2091 threaded_run(folder, ¶m, &result, store_run);
2093 if (result.error != MAILIMAP_NO_ERROR)
2094 return result.error;
2096 debug_print("imap store - end\n");
2098 return result.error;
2103 static void do_exec_command(int fd, const char * command,
2104 const char * servername, uint16_t port)
2109 /* Fork again to become a child of init rather than
2110 the etpan client. */
2115 setenv("ETPANSERVER", servername, 1);
2117 unsetenv("ETPANSERVER");
2122 snprintf(porttext, sizeof(porttext), "%d", port);
2123 setenv("ETPANPORT", porttext, 1);
2126 unsetenv("ETPANPORT");
2129 /* Not a lot we can do if there's an error other than bail. */
2130 if (dup2(fd, 0) == -1)
2132 if (dup2(fd, 1) == -1)
2135 /* Should we close stderr and reopen /dev/null? */
2137 maxopen = sysconf(_SC_OPEN_MAX);
2138 for (i=3; i < maxopen; i++)
2142 /* Detach from the controlling tty if we have one. Otherwise,
2143 SSH might do something stupid like trying to use it instead
2144 of running $SSH_ASKPASS. Doh. */
2145 fd = open("/dev/tty", O_RDONLY);
2147 ioctl(fd, TIOCNOTTY, NULL);
2150 #endif /* TIOCNOTTY */
2152 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2154 /* Eep. Shouldn't reach this */
2158 static int subcommand_connect(const char *command,
2159 const char *servername, uint16_t port)
2161 /* SEB unsupported on Windows */
2165 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2170 do_exec_command(sockfds[1], command, servername, port);
2172 else if (childpid == -1) {
2180 /* Reap child, leaving grandchild process to run */
2181 waitpid(childpid, NULL, 0);
2186 int socket_connect_cmd(mailimap * imap, const char * command,
2187 const char * server, int port)
2193 fd = subcommand_connect(command, server, port);
2195 return MAILIMAP_ERROR_STREAM;
2197 s = mailstream_socket_open(fd);
2200 return MAILIMAP_ERROR_STREAM;
2203 r = mailimap_connect(imap, s);
2204 if (r != MAILIMAP_NO_ERROR) {
2205 mailstream_close(s);
2209 return MAILIMAP_NO_ERROR;
2214 struct connect_cmd_param {
2216 const char * command;
2217 const char * server;
2221 struct connect_cmd_result {
2225 static void connect_cmd_run(struct etpan_thread_op * op)
2228 struct connect_cmd_param * param;
2229 struct connect_cmd_result * result;
2232 result = op->result;
2234 r = socket_connect_cmd(param->imap, param->command,
2235 param->server, param->port);
2241 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2242 const char * server, int port)
2244 struct connect_cmd_param param;
2245 struct connect_cmd_result result;
2250 imap = mailimap_new(0, NULL);
2253 key.len = sizeof(folder);
2256 chash_set(session_hash, &key, &value, NULL);
2259 param.command = command;
2260 param.server = server;
2263 threaded_run(folder, ¶m, &result, connect_cmd_run);
2265 debug_print("connect_cmd ok %i\n", result.error);
2267 return result.error;
2271 void imap_main_init(void)
2274 void imap_main_done(void)
2277 void imap_main_set_timeout(int sec)