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;
617 mailimap_status_att_list_free(status_att_list);
632 static void noop_run(struct etpan_thread_op * op)
634 struct noop_param * param;
635 struct noop_result * result;
639 r = mailimap_noop(param->imap);
643 debug_print("imap noop run - end %i\n", r);
646 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
648 struct noop_param param;
649 struct noop_result result;
652 debug_print("imap noop - begin\n");
654 imap = get_imap(folder);
657 threaded_run(folder, ¶m, &result, noop_run);
659 if (imap->imap_selection_info != NULL) {
660 * p_exists = imap->imap_selection_info->sel_exists;
666 debug_print("imap noop - end\n");
672 struct starttls_param {
676 struct starttls_result {
680 static void starttls_run(struct etpan_thread_op * op)
682 struct starttls_param * param;
683 struct starttls_result * result;
687 r = mailimap_starttls(param->imap);
691 debug_print("imap starttls run - end %i\n", r);
694 mailimap *imap = param->imap;
695 mailstream_low *plain_low = NULL;
696 mailstream_low *tls_low = NULL;
699 plain_low = mailstream_get_low(imap->imap_stream);
700 fd = mailstream_low_get_fd(plain_low);
702 debug_print("imap starttls run - can't get fd\n");
703 result->error = MAILIMAP_ERROR_STREAM;
706 tls_low = mailstream_low_tls_open(fd);
707 if (tls_low == NULL) {
708 debug_print("imap starttls run - can't tls_open\n");
709 result->error = MAILIMAP_ERROR_STREAM;
712 mailstream_low_free(plain_low);
713 mailstream_set_low(imap->imap_stream, tls_low);
717 int imap_threaded_starttls(Folder * folder)
719 struct starttls_param param;
720 struct starttls_result result;
722 debug_print("imap starttls - begin\n");
724 param.imap = get_imap(folder);
726 threaded_run(folder, ¶m, &result, starttls_run);
728 debug_print("imap starttls - end\n");
735 struct create_param {
740 struct create_result {
744 static void create_run(struct etpan_thread_op * op)
746 struct create_param * param;
747 struct create_result * result;
751 r = mailimap_create(param->imap, param->mb);
755 debug_print("imap create run - end %i\n", r);
758 int imap_threaded_create(Folder * folder, const char * mb)
760 struct create_param param;
761 struct create_result result;
763 debug_print("imap create - begin\n");
765 param.imap = get_imap(folder);
768 threaded_run(folder, ¶m, &result, create_run);
770 debug_print("imap create - end\n");
778 struct rename_param {
781 const char * new_name;
784 struct rename_result {
788 static void rename_run(struct etpan_thread_op * op)
790 struct rename_param * param;
791 struct rename_result * result;
795 r = mailimap_rename(param->imap, param->mb, param->new_name);
799 debug_print("imap rename run - end %i\n", r);
802 int imap_threaded_rename(Folder * folder,
803 const char * mb, const char * new_name)
805 struct rename_param param;
806 struct rename_result result;
808 debug_print("imap rename - begin\n");
810 param.imap = get_imap(folder);
812 param.new_name = new_name;
814 threaded_run(folder, ¶m, &result, rename_run);
816 debug_print("imap rename - end\n");
824 struct delete_param {
829 struct delete_result {
833 static void delete_run(struct etpan_thread_op * op)
835 struct delete_param * param;
836 struct delete_result * result;
840 r = mailimap_delete(param->imap, param->mb);
844 debug_print("imap delete run - end %i\n", r);
847 int imap_threaded_delete(Folder * folder, const char * mb)
849 struct delete_param param;
850 struct delete_result result;
852 debug_print("imap delete - begin\n");
854 param.imap = get_imap(folder);
857 threaded_run(folder, ¶m, &result, delete_run);
859 debug_print("imap delete - end\n");
866 struct select_param {
871 struct select_result {
875 static void select_run(struct etpan_thread_op * op)
877 struct select_param * param;
878 struct select_result * result;
882 r = mailimap_select(param->imap, param->mb);
886 debug_print("imap select run - end %i\n", r);
889 int imap_threaded_select(Folder * folder, const char * mb,
890 gint * exists, gint * recent, gint * unseen,
891 guint32 * uid_validity)
893 struct select_param param;
894 struct select_result result;
897 debug_print("imap select - begin\n");
899 imap = get_imap(folder);
903 threaded_run(folder, ¶m, &result, select_run);
905 if (result.error != MAILIMAP_NO_ERROR)
908 if (imap->imap_selection_info == NULL)
909 return MAILIMAP_ERROR_PARSE;
911 * exists = imap->imap_selection_info->sel_exists;
912 * recent = imap->imap_selection_info->sel_recent;
913 * unseen = imap->imap_selection_info->sel_unseen;
914 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
916 debug_print("imap select - end\n");
923 struct examine_param {
928 struct examine_result {
932 static void examine_run(struct etpan_thread_op * op)
934 struct examine_param * param;
935 struct examine_result * result;
939 r = mailimap_examine(param->imap, param->mb);
943 debug_print("imap examine run - end %i\n", r);
946 int imap_threaded_examine(Folder * folder, const char * mb,
947 gint * exists, gint * recent, gint * unseen,
948 guint32 * uid_validity)
950 struct examine_param param;
951 struct examine_result result;
954 debug_print("imap examine - begin\n");
956 imap = get_imap(folder);
960 threaded_run(folder, ¶m, &result, examine_run);
962 if (result.error != MAILIMAP_NO_ERROR)
965 if (imap->imap_selection_info == NULL)
966 return MAILIMAP_ERROR_PARSE;
968 * exists = imap->imap_selection_info->sel_exists;
969 * recent = imap->imap_selection_info->sel_recent;
970 * unseen = imap->imap_selection_info->sel_unseen;
971 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
973 debug_print("imap examine - end\n");
981 struct search_param {
984 struct mailimap_set * set;
987 struct search_result {
989 clist * search_result;
992 static void search_run(struct etpan_thread_op * op)
994 struct search_param * param;
995 struct search_result * result;
997 struct mailimap_search_key * key;
998 struct mailimap_search_key * uid_key;
999 struct mailimap_search_key * search_type_key;
1000 clist * search_result;
1004 uid_key = mailimap_search_key_new_uid(param->set);
1006 search_type_key = NULL;
1007 switch (param->type) {
1008 case IMAP_SEARCH_TYPE_SIMPLE:
1009 search_type_key = NULL;
1012 case IMAP_SEARCH_TYPE_SEEN:
1013 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1014 NULL, NULL, NULL, NULL, NULL,
1015 NULL, NULL, NULL, NULL, NULL,
1016 NULL, NULL, NULL, NULL, 0,
1017 NULL, NULL, NULL, NULL, NULL,
1018 NULL, 0, NULL, NULL, NULL);
1021 case IMAP_SEARCH_TYPE_UNSEEN:
1022 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1023 NULL, NULL, NULL, NULL, NULL,
1024 NULL, NULL, NULL, NULL, NULL,
1025 NULL, NULL, NULL, NULL, 0,
1026 NULL, NULL, NULL, NULL, NULL,
1027 NULL, 0, NULL, NULL, NULL);
1030 case IMAP_SEARCH_TYPE_ANSWERED:
1031 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1032 NULL, NULL, NULL, NULL, NULL,
1033 NULL, NULL, NULL, NULL, NULL,
1034 NULL, NULL, NULL, NULL, 0,
1035 NULL, NULL, NULL, NULL, NULL,
1036 NULL, 0, NULL, NULL, NULL);
1039 case IMAP_SEARCH_TYPE_FLAGGED:
1040 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1041 NULL, NULL, NULL, NULL, NULL,
1042 NULL, NULL, NULL, NULL, NULL,
1043 NULL, NULL, NULL, NULL, 0,
1044 NULL, NULL, NULL, NULL, NULL,
1045 NULL, 0, NULL, NULL, NULL);
1047 case IMAP_SEARCH_TYPE_DELETED:
1048 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1049 NULL, NULL, NULL, NULL, NULL,
1050 NULL, NULL, NULL, NULL, NULL,
1051 NULL, NULL, NULL, NULL, 0,
1052 NULL, NULL, NULL, NULL, NULL,
1053 NULL, 0, NULL, NULL, NULL);
1057 if (search_type_key != NULL) {
1058 key = mailimap_search_key_new_multiple_empty();
1059 mailimap_search_key_multiple_add(key, search_type_key);
1060 mailimap_search_key_multiple_add(key, uid_key);
1066 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1068 result = op->result;
1070 result->search_result = search_result;
1071 debug_print("imap search run - end %i\n", r);
1074 int imap_threaded_search(Folder * folder, int search_type,
1075 struct mailimap_set * set, clist ** search_result)
1077 struct search_param param;
1078 struct search_result result;
1081 debug_print("imap search - begin\n");
1083 imap = get_imap(folder);
1086 param.type = search_type;
1088 threaded_run(folder, ¶m, &result, search_run);
1090 if (result.error != MAILIMAP_NO_ERROR)
1091 return result.error;
1093 debug_print("imap search - end\n");
1095 * search_result = result.search_result;
1097 return result.error;
1104 uid_list_to_env_list(clist * fetch_result, carray ** result)
1112 tab = carray_new(128);
1114 res = MAILIMAP_ERROR_MEMORY;
1118 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1119 cur = clist_next(cur)) {
1120 struct mailimap_msg_att * msg_att;
1121 clistiter * item_cur;
1126 msg_att = clist_content(cur);
1130 for(item_cur = clist_begin(msg_att->att_list) ;
1132 item_cur = clist_next(item_cur)) {
1133 struct mailimap_msg_att_item * item;
1135 item = clist_content(item_cur);
1137 switch (item->att_type) {
1138 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1139 switch (item->att_data.att_static->att_type) {
1140 case MAILIMAP_MSG_ATT_UID:
1141 uid = item->att_data.att_static->att_data.att_uid;
1147 puid = malloc(sizeof(* puid));
1149 res = MAILIMAP_ERROR_MEMORY;
1154 r = carray_add(tab, puid, NULL);
1157 res = MAILIMAP_ERROR_MEMORY;
1164 return MAILIMAP_NO_ERROR;
1167 for(i = 0 ; i < carray_count(tab) ; i++)
1168 mailmessage_free(carray_get(tab, i));
1173 static int imap_get_messages_list(mailimap * imap,
1174 uint32_t first_index,
1179 struct mailimap_fetch_att * fetch_att;
1180 struct mailimap_fetch_type * fetch_type;
1181 struct mailimap_set * set;
1182 clist * fetch_result;
1185 set = mailimap_set_new_interval(first_index, 0);
1187 res = MAILIMAP_ERROR_MEMORY;
1191 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1192 if (fetch_type == NULL) {
1193 res = MAILIMAP_ERROR_MEMORY;
1197 fetch_att = mailimap_fetch_att_new_uid();
1198 if (fetch_att == NULL) {
1199 res = MAILIMAP_ERROR_MEMORY;
1200 goto free_fetch_type;
1203 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1204 if (r != MAILIMAP_NO_ERROR) {
1205 mailimap_fetch_att_free(fetch_att);
1206 res = MAILIMAP_ERROR_MEMORY;
1207 goto free_fetch_type;
1210 r = mailimap_uid_fetch(imap, set,
1211 fetch_type, &fetch_result);
1213 mailimap_fetch_type_free(fetch_type);
1214 mailimap_set_free(set);
1216 if (r != MAILIMAP_NO_ERROR) {
1222 r = uid_list_to_env_list(fetch_result, &env_list);
1223 mailimap_fetch_list_free(fetch_result);
1225 * result = env_list;
1227 return MAILIMAP_NO_ERROR;
1230 mailimap_fetch_type_free(fetch_type);
1232 mailimap_set_free(set);
1240 struct fetch_uid_param {
1242 uint32_t first_index;
1245 struct fetch_uid_result {
1247 carray * fetch_result;
1250 static void fetch_uid_run(struct etpan_thread_op * op)
1252 struct fetch_uid_param * param;
1253 struct fetch_uid_result * result;
1254 carray * fetch_result;
1259 fetch_result = NULL;
1260 r = imap_get_messages_list(param->imap, param->first_index,
1263 result = op->result;
1265 result->fetch_result = fetch_result;
1266 debug_print("imap fetch_uid run - end %i\n", r);
1269 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1270 carray ** fetch_result)
1272 struct fetch_uid_param param;
1273 struct fetch_uid_result result;
1276 debug_print("imap fetch_uid - begin\n");
1278 imap = get_imap(folder);
1280 param.first_index = first_index;
1282 threaded_run(folder, ¶m, &result, fetch_uid_run);
1284 if (result.error != MAILIMAP_NO_ERROR)
1285 return result.error;
1287 debug_print("imap fetch_uid - end\n");
1289 * fetch_result = result.fetch_result;
1291 return result.error;
1295 void imap_fetch_uid_list_free(carray * uid_list)
1299 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1302 puid = carray_get(uid_list, i);
1305 carray_free(uid_list);
1310 static int imap_fetch(mailimap * imap,
1313 size_t * result_len)
1316 struct mailimap_set * set;
1317 struct mailimap_fetch_att * fetch_att;
1318 struct mailimap_fetch_type * fetch_type;
1319 clist * fetch_result;
1320 struct mailimap_msg_att * msg_att;
1321 struct mailimap_msg_att_item * msg_att_item;
1326 struct mailimap_section * section;
1328 set = mailimap_set_new_single(msg_index);
1330 res = MAILIMAP_ERROR_MEMORY;
1334 section = mailimap_section_new(NULL);
1335 if (section == NULL) {
1336 res = MAILIMAP_ERROR_MEMORY;
1340 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1341 if (fetch_att == NULL) {
1342 mailimap_section_free(section);
1343 res = MAILIMAP_ERROR_MEMORY;
1347 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1348 if (fetch_type == NULL) {
1349 res = MAILIMAP_ERROR_MEMORY;
1350 goto free_fetch_att;
1353 r = mailimap_uid_fetch(imap, set,
1354 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;
1385 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1387 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1392 mailimap_fetch_list_free(fetch_result);
1395 return MAILIMAP_ERROR_FETCH;
1398 * result_len = text_length;
1400 return MAILIMAP_NO_ERROR;
1403 mailimap_fetch_att_free(fetch_att);
1405 mailimap_set_free(set);
1410 static int imap_fetch_header(mailimap * imap,
1413 size_t * result_len)
1416 struct mailimap_set * set;
1417 struct mailimap_fetch_att * fetch_att;
1418 struct mailimap_fetch_type * fetch_type;
1419 clist * fetch_result;
1420 struct mailimap_msg_att * msg_att;
1421 struct mailimap_msg_att_item * msg_att_item;
1426 struct mailimap_section * section;
1428 set = mailimap_set_new_single(msg_index);
1430 res = MAILIMAP_ERROR_MEMORY;
1434 section = mailimap_section_new_header();
1435 if (section == NULL) {
1436 res = MAILIMAP_ERROR_MEMORY;
1440 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1441 if (fetch_att == NULL) {
1442 mailimap_section_free(section);
1443 res = MAILIMAP_ERROR_MEMORY;
1447 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1448 if (fetch_type == NULL) {
1449 res = MAILIMAP_ERROR_MEMORY;
1450 goto free_fetch_att;
1453 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1455 mailimap_fetch_type_free(fetch_type);
1456 mailimap_set_free(set);
1459 case MAILIMAP_NO_ERROR:
1465 if (clist_begin(fetch_result) == NULL) {
1466 mailimap_fetch_list_free(fetch_result);
1467 return MAILIMAP_ERROR_FETCH;
1470 msg_att = clist_begin(fetch_result)->data;
1475 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1476 cur = clist_next(cur)) {
1477 msg_att_item = clist_content(cur);
1479 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1480 if (msg_att_item->att_data.att_static->att_type ==
1481 MAILIMAP_MSG_ATT_BODY_SECTION) {
1482 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1483 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1485 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1490 mailimap_fetch_list_free(fetch_result);
1493 return MAILIMAP_ERROR_FETCH;
1496 * result_len = text_length;
1498 return MAILIMAP_NO_ERROR;
1501 mailimap_fetch_att_free(fetch_att);
1503 mailimap_set_free(set);
1510 struct fetch_content_param {
1513 const char * filename;
1517 struct fetch_content_result {
1521 static void fetch_content_run(struct etpan_thread_op * op)
1523 struct fetch_content_param * param;
1524 struct fetch_content_result * result;
1526 size_t content_size;
1535 if (param->with_body)
1536 r = imap_fetch(param->imap, param->msg_index,
1537 &content, &content_size);
1539 r = imap_fetch_header(param->imap, param->msg_index,
1540 &content, &content_size);
1542 result = op->result;
1545 if (r == MAILIMAP_NO_ERROR) {
1546 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1548 result->error = MAILIMAP_ERROR_FETCH;
1552 f = fdopen(fd, "wb");
1554 result->error = MAILIMAP_ERROR_FETCH;
1558 r = fwrite(content, 1, content_size, f);
1565 g_unlink(param->filename);
1576 g_unlink(param->filename);
1579 if (mmap_string_unref(content) != 0)
1583 debug_print("imap fetch_content run - end %i\n", r);
1586 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1588 const char * filename)
1590 struct fetch_content_param param;
1591 struct fetch_content_result result;
1594 debug_print("imap fetch_content - begin\n");
1596 imap = get_imap(folder);
1598 param.msg_index = msg_index;
1599 param.filename = filename;
1600 param.with_body = with_body;
1602 threaded_run(folder, ¶m, &result, fetch_content_run);
1604 if (result.error != MAILIMAP_NO_ERROR)
1605 return result.error;
1607 debug_print("imap fetch_content - end\n");
1609 return result.error;
1614 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1622 flag_list = att_dyn->att_list;
1623 if (flag_list == NULL)
1626 for(cur = clist_begin(flag_list) ; cur != NULL ;
1627 cur = clist_next(cur)) {
1628 struct mailimap_flag_fetch * flag_fetch;
1630 flag_fetch = clist_content(cur);
1631 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1634 switch (flag_fetch->fl_flag->fl_type) {
1635 case MAILIMAP_FLAG_ANSWERED:
1636 flags |= MSG_REPLIED;
1638 case MAILIMAP_FLAG_FLAGGED:
1639 flags |= MSG_MARKED;
1641 case MAILIMAP_FLAG_DELETED:
1642 flags |= MSG_DELETED;
1644 case MAILIMAP_FLAG_SEEN:
1645 flags &= ~MSG_UNREAD;
1655 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1659 struct mailimap_msg_att_dynamic ** patt_dyn)
1661 clistiter * item_cur;
1665 struct mailimap_msg_att_dynamic * att_dyn;
1672 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1673 item_cur = clist_next(item_cur)) {
1674 struct mailimap_msg_att_item * item;
1676 item = clist_content(item_cur);
1678 switch (item->att_type) {
1679 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1680 switch (item->att_data.att_static->att_type) {
1681 case MAILIMAP_MSG_ATT_UID:
1682 uid = item->att_data.att_static->att_data.att_uid;
1685 case MAILIMAP_MSG_ATT_BODY_SECTION:
1686 if (headers == NULL) {
1687 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1690 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1691 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1696 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1697 if (att_dyn == NULL) {
1698 att_dyn = item->att_data.att_dyn;
1706 if (pheaders != NULL)
1707 * pheaders = headers;
1708 if (pref_size != NULL)
1709 * pref_size = ref_size;
1710 if (patt_dyn != NULL)
1711 * patt_dyn = att_dyn;
1713 return MAIL_NO_ERROR;
1716 static struct imap_fetch_env_info *
1717 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1719 struct imap_fetch_env_info * info;
1723 struct mailimap_msg_att_dynamic * att_dyn;
1725 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1730 info = malloc(sizeof(* info));
1732 info->headers = strdup(headers);
1734 info->flags = imap_flags_to_flags(att_dyn);
1740 imap_fetch_result_to_envelop_list(clist * fetch_result,
1741 carray ** p_env_list)
1748 env_list = carray_new(16);
1750 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1751 cur = clist_next(cur)) {
1752 struct mailimap_msg_att * msg_att;
1753 struct imap_fetch_env_info * env_info;
1755 msg_att = clist_content(cur);
1757 env_info = fetch_to_env_info(msg_att);
1759 return MAILIMAP_ERROR_MEMORY;
1760 carray_add(env_list, env_info, NULL);
1763 * p_env_list = env_list;
1765 return MAIL_NO_ERROR;
1768 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1770 struct mailimap_fetch_att * fetch_att;
1774 struct mailimap_header_list * imap_hdrlist;
1775 struct mailimap_section * section;
1777 hdrlist = clist_new();
1779 header = strdup("Date");
1780 r = clist_append(hdrlist, header);
1781 header = strdup("From");
1782 r = clist_append(hdrlist, header);
1783 header = strdup("To");
1784 r = clist_append(hdrlist, header);
1785 header = strdup("Cc");
1786 r = clist_append(hdrlist, header);
1787 header = strdup("Subject");
1788 r = clist_append(hdrlist, header);
1789 header = strdup("Message-ID");
1790 r = clist_append(hdrlist, header);
1791 header = strdup("References");
1792 r = clist_append(hdrlist, header);
1793 header = strdup("In-Reply-To");
1794 r = clist_append(hdrlist, header);
1796 imap_hdrlist = mailimap_header_list_new(hdrlist);
1797 section = mailimap_section_new_header_fields(imap_hdrlist);
1798 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1799 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1801 return MAIL_NO_ERROR;
1804 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1806 struct mailimap_fetch_att * fetch_att;
1807 struct mailimap_section * section;
1809 section = mailimap_section_new_header();
1810 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1811 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1813 return MAIL_NO_ERROR;
1817 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1818 carray ** p_env_list)
1820 struct mailimap_fetch_att * fetch_att;
1821 struct mailimap_fetch_type * fetch_type;
1823 clist * fetch_result;
1825 carray * env_list = NULL;
1829 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1832 fetch_att = mailimap_fetch_att_new_uid();
1833 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1836 fetch_att = mailimap_fetch_att_new_flags();
1837 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1840 fetch_att = mailimap_fetch_att_new_rfc822_size();
1841 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1845 key.len = sizeof(imap);
1846 r = chash_get(courier_workaround_hash, &key, &value);
1848 r = imap_add_envelope_fetch_att(fetch_type);
1850 r = imap_add_header_fetch_att(fetch_type);
1852 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1855 case MAILIMAP_NO_ERROR:
1858 mailimap_fetch_type_free(fetch_type);
1859 debug_print("uid_fetch: %d\n", r);
1863 if (clist_begin(fetch_result) == NULL) {
1864 res = MAILIMAP_ERROR_FETCH;
1865 debug_print("clist_begin = NULL\n");
1869 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1870 mailimap_fetch_list_free(fetch_result);
1872 if (r != MAILIMAP_NO_ERROR) {
1873 mailimap_fetch_type_free(fetch_type);
1874 res = MAILIMAP_ERROR_MEMORY;
1875 debug_print("fetch_result_to_envelop_list: %d\n", res);
1879 mailimap_fetch_type_free(fetch_type);
1881 * p_env_list = env_list;
1883 return MAILIMAP_NO_ERROR;
1889 struct fetch_env_param {
1891 struct mailimap_set * set;
1894 struct fetch_env_result {
1895 carray * fetch_env_result;
1899 static void fetch_env_run(struct etpan_thread_op * op)
1901 struct fetch_env_param * param;
1902 struct fetch_env_result * result;
1909 r = imap_get_envelopes_list(param->imap, param->set,
1912 result = op->result;
1914 result->fetch_env_result = env_list;
1916 debug_print("imap fetch_env run - end %i\n", r);
1919 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1920 carray ** p_env_list)
1922 struct fetch_env_param param;
1923 struct fetch_env_result result;
1926 debug_print("imap fetch_env - begin\n");
1928 imap = get_imap(folder);
1932 threaded_run(folder, ¶m, &result, fetch_env_run);
1934 if (result.error != MAILIMAP_NO_ERROR) {
1940 key.len = sizeof(imap);
1941 r = chash_get(courier_workaround_hash, &key, &value);
1945 chash_set(courier_workaround_hash, &key, &value, NULL);
1947 threaded_run(folder, ¶m, &result, fetch_env_run);
1951 if (result.error != MAILIMAP_NO_ERROR)
1952 return result.error;
1954 debug_print("imap fetch_env - end\n");
1956 * p_env_list = result.fetch_env_result;
1958 return result.error;
1961 void imap_fetch_env_free(carray * env_list)
1965 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1966 struct imap_fetch_env_info * env_info;
1968 env_info = carray_get(env_list, i);
1969 free(env_info->headers);
1972 carray_free(env_list);
1979 struct append_param {
1981 const char * mailbox;
1982 const char * filename;
1983 struct mailimap_flag_list * flag_list;
1986 struct append_result {
1990 static void append_run(struct etpan_thread_op * op)
1992 struct append_param * param;
1993 struct append_result * result;
1997 struct stat stat_buf;
2001 result = op->result;
2003 r = stat(param->filename, &stat_buf);
2005 result->error = MAILIMAP_ERROR_APPEND;
2008 size = stat_buf.st_size;
2010 fd = open(param->filename, O_RDONLY);
2012 result->error = MAILIMAP_ERROR_APPEND;
2016 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2017 if (data == (void *) MAP_FAILED) {
2019 result->error = MAILIMAP_ERROR_APPEND;
2023 r = mailimap_append(param->imap, param->mailbox,
2024 param->flag_list, NULL,
2032 debug_print("imap append run - end %i\n", r);
2035 int imap_threaded_append(Folder * folder, const char * mailbox,
2036 const char * filename,
2037 struct mailimap_flag_list * flag_list)
2039 struct append_param param;
2040 struct append_result result;
2043 debug_print("imap append - begin\n");
2045 imap = get_imap(folder);
2047 param.mailbox = mailbox;
2048 param.filename = filename;
2049 param.flag_list = flag_list;
2051 threaded_run(folder, ¶m, &result, append_run);
2053 if (result.error != MAILIMAP_NO_ERROR)
2054 return result.error;
2056 debug_print("imap append - end\n");
2058 return result.error;
2064 struct expunge_param {
2068 struct expunge_result {
2072 static void expunge_run(struct etpan_thread_op * op)
2074 struct expunge_param * param;
2075 struct expunge_result * result;
2079 r = mailimap_expunge(param->imap);
2081 result = op->result;
2083 debug_print("imap expunge run - end %i\n", r);
2086 int imap_threaded_expunge(Folder * folder)
2088 struct expunge_param param;
2089 struct expunge_result result;
2091 debug_print("imap expunge - begin\n");
2093 param.imap = get_imap(folder);
2095 threaded_run(folder, ¶m, &result, expunge_run);
2097 debug_print("imap expunge - end\n");
2099 return result.error;
2105 struct mailimap_set * set;
2109 struct copy_result {
2113 static void copy_run(struct etpan_thread_op * op)
2115 struct copy_param * param;
2116 struct copy_result * result;
2121 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2123 result = op->result;
2126 debug_print("imap copy run - end %i\n", r);
2129 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2132 struct copy_param param;
2133 struct copy_result result;
2136 debug_print("imap copy - begin\n");
2138 imap = get_imap(folder);
2143 threaded_run(folder, ¶m, &result, copy_run);
2145 if (result.error != MAILIMAP_NO_ERROR)
2146 return result.error;
2148 debug_print("imap copy - end\n");
2150 return result.error;
2155 struct store_param {
2157 struct mailimap_set * set;
2158 struct mailimap_store_att_flags * store_att_flags;
2161 struct store_result {
2165 static void store_run(struct etpan_thread_op * op)
2167 struct store_param * param;
2168 struct store_result * result;
2173 r = mailimap_uid_store(param->imap, param->set,
2174 param->store_att_flags);
2176 result = op->result;
2179 debug_print("imap store run - end %i\n", r);
2182 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2183 struct mailimap_store_att_flags * store_att_flags)
2185 struct store_param param;
2186 struct store_result result;
2189 debug_print("imap store - begin\n");
2191 imap = get_imap(folder);
2194 param.store_att_flags = store_att_flags;
2196 threaded_run(folder, ¶m, &result, store_run);
2198 if (result.error != MAILIMAP_NO_ERROR)
2199 return result.error;
2201 debug_print("imap store - end\n");
2203 return result.error;
2207 #define ENV_BUFFER_SIZE 512
2209 static void do_exec_command(int fd, const char * command,
2210 const char * servername, uint16_t port)
2214 char env_buffer[ENV_BUFFER_SIZE];
2218 /* Fork again to become a child of init rather than
2219 the etpan client. */
2225 snprintf(env_buffer, ENV_BUFFER_SIZE,
2226 "ETPANSERVER=%s", servername);
2228 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2232 setenv("ETPANSERVER", servername, 1);
2234 unsetenv("ETPANSERVER");
2239 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2241 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2247 snprintf(porttext, sizeof(porttext), "%d", port);
2248 setenv("ETPANPORT", porttext, 1);
2251 unsetenv("ETPANPORT");
2255 /* Not a lot we can do if there's an error other than bail. */
2256 if (dup2(fd, 0) == -1)
2258 if (dup2(fd, 1) == -1)
2261 /* Should we close stderr and reopen /dev/null? */
2263 maxopen = sysconf(_SC_OPEN_MAX);
2264 for (i=3; i < maxopen; i++)
2268 /* Detach from the controlling tty if we have one. Otherwise,
2269 SSH might do something stupid like trying to use it instead
2270 of running $SSH_ASKPASS. Doh. */
2271 fd = open("/dev/tty", O_RDONLY);
2273 ioctl(fd, TIOCNOTTY, NULL);
2276 #endif /* TIOCNOTTY */
2278 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2280 /* Eep. Shouldn't reach this */
2284 static int subcommand_connect(const char *command,
2285 const char *servername, uint16_t port)
2287 /* SEB unsupported on Windows */
2291 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2296 do_exec_command(sockfds[1], command, servername, port);
2298 else if (childpid == -1) {
2306 /* Reap child, leaving grandchild process to run */
2307 waitpid(childpid, NULL, 0);
2312 int socket_connect_cmd(mailimap * imap, const char * command,
2313 const char * server, int port)
2319 fd = subcommand_connect(command, server, port);
2321 return MAILIMAP_ERROR_STREAM;
2323 s = mailstream_socket_open(fd);
2326 return MAILIMAP_ERROR_STREAM;
2329 r = mailimap_connect(imap, s);
2330 if (r != MAILIMAP_NO_ERROR) {
2331 mailstream_close(s);
2335 return MAILIMAP_NO_ERROR;
2340 struct connect_cmd_param {
2342 const char * command;
2343 const char * server;
2347 struct connect_cmd_result {
2351 static void connect_cmd_run(struct etpan_thread_op * op)
2354 struct connect_cmd_param * param;
2355 struct connect_cmd_result * result;
2358 result = op->result;
2360 r = socket_connect_cmd(param->imap, param->command,
2361 param->server, param->port);
2367 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2368 const char * server, int port)
2370 struct connect_cmd_param param;
2371 struct connect_cmd_result result;
2376 imap = mailimap_new(0, NULL);
2379 key.len = sizeof(folder);
2382 chash_set(session_hash, &key, &value, NULL);
2385 param.command = command;
2386 param.server = server;
2389 threaded_run(folder, ¶m, &result, connect_cmd_run);
2391 debug_print("connect_cmd ok %i\n", result.error);
2393 return result.error;
2397 void imap_main_init(void)
2400 void imap_main_done(void)
2403 void imap_main_set_timeout(int sec)