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