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)
45 strncpy(buf, str, size > 510 ? 510:size);
49 if (!strncmp(buf, "<<<<<<<", 7)
50 || !strncmp(buf, ">>>>>>>", 7)
51 || buf[0] == '\r' || buf[0] == '\n')
54 while (strstr(buf, "\r"))
55 *strstr(buf, "\r") = ' ';
56 while (strstr(buf, "\n"))
57 *strstr(buf, "\n") = ' ';
59 log_print("IMAP4%c %s\n", direction?'>':'<', buf);
62 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
64 void imap_main_init(void)
66 int fd_thread_manager;
68 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
69 mailstream_network_delay.tv_usec = 0;
72 mailstream_logger = imap_logger;
74 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
75 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
76 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
78 thread_manager = etpan_thread_manager_new();
80 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
82 io_channel = g_io_channel_unix_new(fd_thread_manager);
84 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
90 void imap_main_set_timeout(int sec)
92 mailstream_network_delay.tv_sec = sec;
93 mailstream_network_delay.tv_usec = 0;
96 void imap_main_done(void)
98 etpan_thread_manager_stop(thread_manager);
99 etpan_thread_manager_join(thread_manager);
101 g_source_remove(thread_manager_signal);
102 g_io_channel_unref(io_channel);
104 etpan_thread_manager_free(thread_manager);
106 chash_free(courier_workaround_hash);
107 chash_free(session_hash);
108 chash_free(imap_hash);
111 void imap_init(Folder * folder)
113 struct etpan_thread * thread;
117 thread = etpan_thread_manager_get_thread(thread_manager);
120 key.len = sizeof(folder);
124 chash_set(imap_hash, &key, &value, NULL);
127 void imap_done(Folder * folder)
129 struct etpan_thread * thread;
135 key.len = sizeof(folder);
137 r = chash_get(imap_hash, &key, &value);
143 etpan_thread_unbind(thread);
145 chash_delete(imap_hash, &key, NULL);
147 debug_print("remove thread");
150 static struct etpan_thread * get_thread(Folder * folder)
152 struct etpan_thread * thread;
157 key.len = sizeof(folder);
159 chash_get(imap_hash, &key, &value);
165 static mailimap * get_imap(Folder * folder)
173 key.len = sizeof(folder);
175 r = chash_get(session_hash, &key, &value);
185 static void generic_cb(int cancelled, void * result, void * callback_data)
189 p_finished = callback_data;
191 debug_print("generic_cb\n");
196 static void threaded_run(Folder * folder, void * param, void * result,
197 void (* func)(struct etpan_thread_op * ))
199 struct etpan_thread_op * op;
200 struct etpan_thread * thread;
203 imap_folder_ref(folder);
205 op = etpan_thread_op_new();
211 op->callback = generic_cb;
212 op->callback_data = &finished;
217 thread = get_thread(folder);
218 etpan_thread_op_schedule(thread, op);
221 gtk_main_iteration();
224 etpan_thread_op_free(op);
226 imap_folder_unref(folder);
232 struct connect_param {
238 struct connect_result {
242 static void connect_run(struct etpan_thread_op * op)
245 struct connect_param * param;
246 struct connect_result * result;
251 r = mailimap_socket_connect(param->imap,
252 param->server, param->port);
258 int imap_threaded_connect(Folder * folder, const char * server, int port)
260 struct connect_param param;
261 struct connect_result result;
266 imap = mailimap_new(0, NULL);
269 key.len = sizeof(folder);
272 chash_set(session_hash, &key, &value, NULL);
275 param.server = server;
278 threaded_run(folder, ¶m, &result, connect_run);
280 debug_print("connect ok %i\n", result.error);
286 static void connect_ssl_run(struct etpan_thread_op * op)
289 struct connect_param * param;
290 struct connect_result * result;
295 r = mailimap_ssl_connect(param->imap,
296 param->server, param->port);
301 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
303 struct connect_param param;
304 struct connect_result result;
309 imap = mailimap_new(0, NULL);
312 key.len = sizeof(folder);
315 chash_set(session_hash, &key, &value, NULL);
318 param.server = server;
321 threaded_run(folder, ¶m, &result, connect_ssl_run);
323 debug_print("connect %d\n", result.error);
334 struct mailimap_capability_data *caps;
337 static void capability_run(struct etpan_thread_op * op)
340 struct capa_param * param;
341 struct capa_result * result;
342 struct mailimap_capability_data *caps;
347 r = mailimap_capability(param->imap, &caps);
350 result->caps = (r == 0 ? caps : NULL);
354 struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
356 struct capa_param param;
357 struct capa_result result;
360 imap = get_imap(folder);
364 threaded_run(folder, ¶m, &result, capability_run);
366 debug_print("capa ok\n");
372 struct disconnect_param {
376 struct disconnect_result {
380 static void disconnect_run(struct etpan_thread_op * op)
383 struct disconnect_param * param;
384 struct disconnect_result * result;
389 r = mailimap_logout(param->imap);
394 void imap_threaded_disconnect(Folder * folder)
396 struct connect_param param;
397 struct connect_result result;
402 imap = get_imap(folder);
404 debug_print("was disconnected\n");
410 threaded_run(folder, ¶m, &result, disconnect_run);
413 key.len = sizeof(folder);
416 chash_delete(session_hash, &key, NULL);
419 key.len = sizeof(imap);
420 chash_delete(courier_workaround_hash, &key, NULL);
424 debug_print("disconnect ok\n");
431 const char * wildcard;
439 static void list_run(struct etpan_thread_op * op)
441 struct list_param * param;
442 struct list_result * result;
448 r = mailimap_list(param->imap, param->base,
449 param->wildcard, &list);
454 debug_print("imap list run - end\n");
457 int imap_threaded_list(Folder * folder, const char * base,
458 const char * wildcard,
461 struct list_param param;
462 struct list_result result;
464 debug_print("imap list - begin\n");
466 param.imap = get_imap(folder);
468 param.wildcard = wildcard;
470 threaded_run(folder, ¶m, &result, list_run);
472 * p_result = result.list;
474 debug_print("imap list - end %p\n", result.list);
484 const char * password;
488 struct login_result {
492 static void login_run(struct etpan_thread_op * op)
494 struct login_param * param;
495 struct login_result * result;
497 #ifdef DISABLE_LOG_DURING_LOGIN
503 #ifdef DISABLE_LOG_DURING_LOGIN
504 old_debug = mailstream_debug;
505 mailstream_debug = 0;
507 if (!strcmp(param->type, "LOGIN"))
508 r = mailimap_login(param->imap,
509 param->login, param->password);
511 r = mailimap_authenticate(param->imap,
512 param->type, NULL, NULL, NULL,
513 param->login, param->login,
514 param->password, NULL);
515 #ifdef DISABLE_LOG_DURING_LOGIN
516 mailstream_debug = old_debug;
521 debug_print("imap login run - end %i\n", r);
524 int imap_threaded_login(Folder * folder,
525 const char * login, const char * password,
528 struct login_param param;
529 struct login_result result;
531 debug_print("imap login - begin\n");
533 param.imap = get_imap(folder);
535 param.password = password;
538 threaded_run(folder, ¶m, &result, login_run);
540 debug_print("imap login - end\n");
546 struct status_param {
549 struct mailimap_status_att_list * status_att_list;
552 struct status_result {
554 struct mailimap_mailbox_data_status * data_status;
557 static void status_run(struct etpan_thread_op * op)
559 struct status_param * param;
560 struct status_result * result;
566 r = mailimap_status(param->imap, param->mb,
567 param->status_att_list,
568 &result->data_status);
571 debug_print("imap status run - end %i\n", r);
574 int imap_threaded_status(Folder * folder, const char * mb,
575 struct mailimap_mailbox_data_status ** data_status,
578 struct status_param param;
579 struct status_result result;
580 struct mailimap_status_att_list * status_att_list;
582 debug_print("imap status - begin\n");
584 status_att_list = mailimap_status_att_list_new_empty();
586 mailimap_status_att_list_add(status_att_list,
587 MAILIMAP_STATUS_ATT_MESSAGES);
590 mailimap_status_att_list_add(status_att_list,
591 MAILIMAP_STATUS_ATT_RECENT);
594 mailimap_status_att_list_add(status_att_list,
595 MAILIMAP_STATUS_ATT_UIDNEXT);
598 mailimap_status_att_list_add(status_att_list,
599 MAILIMAP_STATUS_ATT_UIDVALIDITY);
602 mailimap_status_att_list_add(status_att_list,
603 MAILIMAP_STATUS_ATT_UNSEEN);
605 param.imap = get_imap(folder);
607 param.status_att_list = status_att_list;
609 threaded_run(folder, ¶m, &result, status_run);
611 debug_print("imap status - end\n");
613 * data_status = result.data_status;
628 static void noop_run(struct etpan_thread_op * op)
630 struct noop_param * param;
631 struct noop_result * result;
635 r = mailimap_noop(param->imap);
639 debug_print("imap noop run - end %i\n", r);
642 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
644 struct noop_param param;
645 struct noop_result result;
648 debug_print("imap noop - begin\n");
650 imap = get_imap(folder);
653 threaded_run(folder, ¶m, &result, noop_run);
655 if (imap->imap_selection_info != NULL) {
656 * p_exists = imap->imap_selection_info->sel_exists;
662 debug_print("imap noop - end\n");
668 struct starttls_param {
672 struct starttls_result {
676 static void starttls_run(struct etpan_thread_op * op)
678 struct starttls_param * param;
679 struct starttls_result * result;
683 r = mailimap_starttls(param->imap);
687 debug_print("imap starttls run - end %i\n", r);
690 mailimap *imap = param->imap;
691 mailstream_low *plain_low = NULL;
692 mailstream_low *tls_low = NULL;
695 plain_low = mailstream_get_low(imap->imap_stream);
696 fd = mailstream_low_get_fd(plain_low);
698 debug_print("imap starttls run - can't get fd\n");
699 result->error = MAILIMAP_ERROR_STREAM;
702 tls_low = mailstream_low_ssl_open(fd);
703 if (tls_low == NULL) {
704 debug_print("imap starttls run - can't ssl_open\n");
705 result->error = MAILIMAP_ERROR_STREAM;
708 mailstream_low_free(plain_low);
709 mailstream_set_low(imap->imap_stream, tls_low);
713 int imap_threaded_starttls(Folder * folder)
715 struct starttls_param param;
716 struct starttls_result result;
718 debug_print("imap starttls - begin\n");
720 param.imap = get_imap(folder);
722 threaded_run(folder, ¶m, &result, starttls_run);
724 debug_print("imap starttls - end\n");
731 struct create_param {
736 struct create_result {
740 static void create_run(struct etpan_thread_op * op)
742 struct create_param * param;
743 struct create_result * result;
747 r = mailimap_create(param->imap, param->mb);
751 debug_print("imap create run - end %i\n", r);
754 int imap_threaded_create(Folder * folder, const char * mb)
756 struct create_param param;
757 struct create_result result;
759 debug_print("imap create - begin\n");
761 param.imap = get_imap(folder);
764 threaded_run(folder, ¶m, &result, create_run);
766 debug_print("imap create - end\n");
774 struct rename_param {
777 const char * new_name;
780 struct rename_result {
784 static void rename_run(struct etpan_thread_op * op)
786 struct rename_param * param;
787 struct rename_result * result;
791 r = mailimap_rename(param->imap, param->mb, param->new_name);
795 debug_print("imap rename run - end %i\n", r);
798 int imap_threaded_rename(Folder * folder,
799 const char * mb, const char * new_name)
801 struct rename_param param;
802 struct rename_result result;
804 debug_print("imap rename - begin\n");
806 param.imap = get_imap(folder);
808 param.new_name = new_name;
810 threaded_run(folder, ¶m, &result, rename_run);
812 debug_print("imap rename - end\n");
820 struct delete_param {
825 struct delete_result {
829 static void delete_run(struct etpan_thread_op * op)
831 struct delete_param * param;
832 struct delete_result * result;
836 r = mailimap_delete(param->imap, param->mb);
840 debug_print("imap delete run - end %i\n", r);
843 int imap_threaded_delete(Folder * folder, const char * mb)
845 struct delete_param param;
846 struct delete_result result;
848 debug_print("imap delete - begin\n");
850 param.imap = get_imap(folder);
853 threaded_run(folder, ¶m, &result, delete_run);
855 debug_print("imap delete - end\n");
862 struct select_param {
867 struct select_result {
871 static void select_run(struct etpan_thread_op * op)
873 struct select_param * param;
874 struct select_result * result;
878 r = mailimap_select(param->imap, param->mb);
882 debug_print("imap select run - end %i\n", r);
885 int imap_threaded_select(Folder * folder, const char * mb,
886 gint * exists, gint * recent, gint * unseen,
887 guint32 * uid_validity)
889 struct select_param param;
890 struct select_result result;
893 debug_print("imap select - begin\n");
895 imap = get_imap(folder);
899 threaded_run(folder, ¶m, &result, select_run);
901 if (result.error != MAILIMAP_NO_ERROR)
904 if (imap->imap_selection_info == NULL)
905 return MAILIMAP_ERROR_PARSE;
907 * exists = imap->imap_selection_info->sel_exists;
908 * recent = imap->imap_selection_info->sel_recent;
909 * unseen = imap->imap_selection_info->sel_unseen;
910 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
912 debug_print("imap select - end\n");
919 struct examine_param {
924 struct examine_result {
928 static void examine_run(struct etpan_thread_op * op)
930 struct examine_param * param;
931 struct examine_result * result;
935 r = mailimap_examine(param->imap, param->mb);
939 debug_print("imap examine run - end %i\n", r);
942 int imap_threaded_examine(Folder * folder, const char * mb,
943 gint * exists, gint * recent, gint * unseen,
944 guint32 * uid_validity)
946 struct examine_param param;
947 struct examine_result result;
950 debug_print("imap examine - begin\n");
952 imap = get_imap(folder);
956 threaded_run(folder, ¶m, &result, examine_run);
958 if (result.error != MAILIMAP_NO_ERROR)
961 if (imap->imap_selection_info == NULL)
962 return MAILIMAP_ERROR_PARSE;
964 * exists = imap->imap_selection_info->sel_exists;
965 * recent = imap->imap_selection_info->sel_recent;
966 * unseen = imap->imap_selection_info->sel_unseen;
967 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
969 debug_print("imap examine - end\n");
977 struct search_param {
980 struct mailimap_set * set;
983 struct search_result {
985 clist * search_result;
988 static void search_run(struct etpan_thread_op * op)
990 struct search_param * param;
991 struct search_result * result;
993 struct mailimap_search_key * key;
994 struct mailimap_search_key * uid_key;
995 struct mailimap_search_key * search_type_key;
996 clist * search_result;
1000 uid_key = mailimap_search_key_new_uid(param->set);
1002 search_type_key = NULL;
1003 switch (param->type) {
1004 case IMAP_SEARCH_TYPE_SIMPLE:
1005 search_type_key = NULL;
1008 case IMAP_SEARCH_TYPE_SEEN:
1009 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1010 NULL, NULL, NULL, NULL, NULL,
1011 NULL, NULL, NULL, NULL, NULL,
1012 NULL, NULL, NULL, NULL, 0,
1013 NULL, NULL, NULL, NULL, NULL,
1014 NULL, 0, NULL, NULL, NULL);
1017 case IMAP_SEARCH_TYPE_UNSEEN:
1018 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1019 NULL, NULL, NULL, NULL, NULL,
1020 NULL, NULL, NULL, NULL, NULL,
1021 NULL, NULL, NULL, NULL, 0,
1022 NULL, NULL, NULL, NULL, NULL,
1023 NULL, 0, NULL, NULL, NULL);
1026 case IMAP_SEARCH_TYPE_ANSWERED:
1027 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1028 NULL, NULL, NULL, NULL, NULL,
1029 NULL, NULL, NULL, NULL, NULL,
1030 NULL, NULL, NULL, NULL, 0,
1031 NULL, NULL, NULL, NULL, NULL,
1032 NULL, 0, NULL, NULL, NULL);
1035 case IMAP_SEARCH_TYPE_FLAGGED:
1036 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1037 NULL, NULL, NULL, NULL, NULL,
1038 NULL, NULL, NULL, NULL, NULL,
1039 NULL, NULL, NULL, NULL, 0,
1040 NULL, NULL, NULL, NULL, NULL,
1041 NULL, 0, NULL, NULL, NULL);
1043 case IMAP_SEARCH_TYPE_DELETED:
1044 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1045 NULL, NULL, NULL, NULL, NULL,
1046 NULL, NULL, NULL, NULL, NULL,
1047 NULL, NULL, NULL, NULL, 0,
1048 NULL, NULL, NULL, NULL, NULL,
1049 NULL, 0, NULL, NULL, NULL);
1053 if (search_type_key != NULL) {
1054 key = mailimap_search_key_new_multiple_empty();
1055 mailimap_search_key_multiple_add(key, search_type_key);
1056 mailimap_search_key_multiple_add(key, uid_key);
1062 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1064 result = op->result;
1066 result->search_result = search_result;
1067 debug_print("imap search run - end %i\n", r);
1070 int imap_threaded_search(Folder * folder, int search_type,
1071 struct mailimap_set * set, clist ** search_result)
1073 struct search_param param;
1074 struct search_result result;
1077 debug_print("imap search - begin\n");
1079 imap = get_imap(folder);
1082 param.type = search_type;
1084 threaded_run(folder, ¶m, &result, search_run);
1086 if (result.error != MAILIMAP_NO_ERROR)
1087 return result.error;
1089 debug_print("imap search - end\n");
1091 * search_result = result.search_result;
1093 return result.error;
1100 uid_list_to_env_list(clist * fetch_result, carray ** result)
1108 tab = carray_new(128);
1110 res = MAILIMAP_ERROR_MEMORY;
1114 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1115 cur = clist_next(cur)) {
1116 struct mailimap_msg_att * msg_att;
1117 clistiter * item_cur;
1122 msg_att = clist_content(cur);
1126 for(item_cur = clist_begin(msg_att->att_list) ;
1128 item_cur = clist_next(item_cur)) {
1129 struct mailimap_msg_att_item * item;
1131 item = clist_content(item_cur);
1133 switch (item->att_type) {
1134 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1135 switch (item->att_data.att_static->att_type) {
1136 case MAILIMAP_MSG_ATT_UID:
1137 uid = item->att_data.att_static->att_data.att_uid;
1143 puid = malloc(sizeof(* puid));
1145 res = MAILIMAP_ERROR_MEMORY;
1150 r = carray_add(tab, puid, NULL);
1153 res = MAILIMAP_ERROR_MEMORY;
1160 return MAILIMAP_NO_ERROR;
1163 for(i = 0 ; i < carray_count(tab) ; i++)
1164 mailmessage_free(carray_get(tab, i));
1169 static int imap_get_messages_list(mailimap * imap,
1170 uint32_t first_index,
1175 struct mailimap_fetch_att * fetch_att;
1176 struct mailimap_fetch_type * fetch_type;
1177 struct mailimap_set * set;
1178 clist * fetch_result;
1181 set = mailimap_set_new_interval(first_index, 0);
1183 res = MAILIMAP_ERROR_MEMORY;
1187 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1188 if (fetch_type == NULL) {
1189 res = MAILIMAP_ERROR_MEMORY;
1193 fetch_att = mailimap_fetch_att_new_uid();
1194 if (fetch_att == NULL) {
1195 res = MAILIMAP_ERROR_MEMORY;
1196 goto free_fetch_type;
1199 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1200 if (r != MAILIMAP_NO_ERROR) {
1201 mailimap_fetch_att_free(fetch_att);
1202 res = MAILIMAP_ERROR_MEMORY;
1203 goto free_fetch_type;
1206 r = mailimap_uid_fetch(imap, set,
1207 fetch_type, &fetch_result);
1209 mailimap_fetch_type_free(fetch_type);
1210 mailimap_set_free(set);
1212 if (r != MAILIMAP_NO_ERROR) {
1218 r = uid_list_to_env_list(fetch_result, &env_list);
1219 mailimap_fetch_list_free(fetch_result);
1221 * result = env_list;
1223 return MAILIMAP_NO_ERROR;
1226 mailimap_fetch_type_free(fetch_type);
1228 mailimap_set_free(set);
1236 struct fetch_uid_param {
1238 uint32_t first_index;
1241 struct fetch_uid_result {
1243 carray * fetch_result;
1246 static void fetch_uid_run(struct etpan_thread_op * op)
1248 struct fetch_uid_param * param;
1249 struct fetch_uid_result * result;
1250 carray * fetch_result;
1255 fetch_result = NULL;
1256 r = imap_get_messages_list(param->imap, param->first_index,
1259 result = op->result;
1261 result->fetch_result = fetch_result;
1262 debug_print("imap fetch_uid run - end %i\n", r);
1265 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1266 carray ** fetch_result)
1268 struct fetch_uid_param param;
1269 struct fetch_uid_result result;
1272 debug_print("imap fetch_uid - begin\n");
1274 imap = get_imap(folder);
1276 param.first_index = first_index;
1278 threaded_run(folder, ¶m, &result, fetch_uid_run);
1280 if (result.error != MAILIMAP_NO_ERROR)
1281 return result.error;
1283 debug_print("imap fetch_uid - end\n");
1285 * fetch_result = result.fetch_result;
1287 return result.error;
1291 void imap_fetch_uid_list_free(carray * uid_list)
1295 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1298 puid = carray_get(uid_list, i);
1301 carray_free(uid_list);
1306 static int imap_fetch(mailimap * imap,
1309 size_t * result_len)
1312 struct mailimap_set * set;
1313 struct mailimap_fetch_att * fetch_att;
1314 struct mailimap_fetch_type * fetch_type;
1315 clist * fetch_result;
1316 struct mailimap_msg_att * msg_att;
1317 struct mailimap_msg_att_item * msg_att_item;
1322 struct mailimap_section * section;
1324 set = mailimap_set_new_single(msg_index);
1326 res = MAILIMAP_ERROR_MEMORY;
1330 section = mailimap_section_new(NULL);
1331 if (section == NULL) {
1332 res = MAILIMAP_ERROR_MEMORY;
1336 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1337 if (fetch_att == NULL) {
1338 mailimap_section_free(section);
1339 res = MAILIMAP_ERROR_MEMORY;
1343 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1344 if (fetch_type == NULL) {
1345 res = MAILIMAP_ERROR_MEMORY;
1346 goto free_fetch_att;
1349 r = mailimap_uid_fetch(imap, set,
1350 fetch_type, &fetch_result);
1352 mailimap_fetch_type_free(fetch_type);
1353 mailimap_set_free(set);
1356 case MAILIMAP_NO_ERROR:
1362 if (clist_begin(fetch_result) == NULL) {
1363 mailimap_fetch_list_free(fetch_result);
1364 return MAILIMAP_ERROR_FETCH;
1367 msg_att = clist_begin(fetch_result)->data;
1372 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1373 cur = clist_next(cur)) {
1374 msg_att_item = clist_content(cur);
1376 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1377 if (msg_att_item->att_data.att_static->att_type ==
1378 MAILIMAP_MSG_ATT_BODY_SECTION) {
1379 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1381 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1383 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1388 mailimap_fetch_list_free(fetch_result);
1391 return MAILIMAP_ERROR_FETCH;
1394 * result_len = text_length;
1396 return MAILIMAP_NO_ERROR;
1399 mailimap_fetch_att_free(fetch_att);
1401 mailimap_set_free(set);
1406 static int imap_fetch_header(mailimap * imap,
1409 size_t * result_len)
1412 struct mailimap_set * set;
1413 struct mailimap_fetch_att * fetch_att;
1414 struct mailimap_fetch_type * fetch_type;
1415 clist * fetch_result;
1416 struct mailimap_msg_att * msg_att;
1417 struct mailimap_msg_att_item * msg_att_item;
1422 struct mailimap_section * section;
1424 set = mailimap_set_new_single(msg_index);
1426 res = MAILIMAP_ERROR_MEMORY;
1430 section = mailimap_section_new_header();
1431 if (section == NULL) {
1432 res = MAILIMAP_ERROR_MEMORY;
1436 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1437 if (fetch_att == NULL) {
1438 mailimap_section_free(section);
1439 res = MAILIMAP_ERROR_MEMORY;
1443 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1444 if (fetch_type == NULL) {
1445 res = MAILIMAP_ERROR_MEMORY;
1446 goto free_fetch_att;
1449 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1451 mailimap_fetch_type_free(fetch_type);
1452 mailimap_set_free(set);
1455 case MAILIMAP_NO_ERROR:
1461 if (clist_begin(fetch_result) == NULL) {
1462 mailimap_fetch_list_free(fetch_result);
1463 return MAILIMAP_ERROR_FETCH;
1466 msg_att = clist_begin(fetch_result)->data;
1471 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1472 cur = clist_next(cur)) {
1473 msg_att_item = clist_content(cur);
1475 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1476 if (msg_att_item->att_data.att_static->att_type ==
1477 MAILIMAP_MSG_ATT_BODY_SECTION) {
1478 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1479 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1481 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1486 mailimap_fetch_list_free(fetch_result);
1489 return MAILIMAP_ERROR_FETCH;
1492 * result_len = text_length;
1494 return MAILIMAP_NO_ERROR;
1497 mailimap_fetch_att_free(fetch_att);
1499 mailimap_set_free(set);
1506 struct fetch_content_param {
1509 const char * filename;
1513 struct fetch_content_result {
1517 static void fetch_content_run(struct etpan_thread_op * op)
1519 struct fetch_content_param * param;
1520 struct fetch_content_result * result;
1522 size_t content_size;
1531 if (param->with_body)
1532 r = imap_fetch(param->imap, param->msg_index,
1533 &content, &content_size);
1535 r = imap_fetch_header(param->imap, param->msg_index,
1536 &content, &content_size);
1538 result = op->result;
1541 if (r == MAILIMAP_NO_ERROR) {
1542 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1544 result->error = MAILIMAP_ERROR_FETCH;
1548 f = fdopen(fd, "wb");
1550 result->error = MAILIMAP_ERROR_FETCH;
1554 r = fwrite(content, 1, content_size, f);
1561 g_unlink(param->filename);
1572 g_unlink(param->filename);
1575 if (mmap_string_unref(content) != 0)
1579 debug_print("imap fetch_content run - end %i\n", r);
1582 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1584 const char * filename)
1586 struct fetch_content_param param;
1587 struct fetch_content_result result;
1590 debug_print("imap fetch_content - begin\n");
1592 imap = get_imap(folder);
1594 param.msg_index = msg_index;
1595 param.filename = filename;
1596 param.with_body = with_body;
1598 threaded_run(folder, ¶m, &result, fetch_content_run);
1600 if (result.error != MAILIMAP_NO_ERROR)
1601 return result.error;
1603 debug_print("imap fetch_content - end\n");
1605 return result.error;
1610 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1618 flag_list = att_dyn->att_list;
1619 if (flag_list == NULL)
1622 for(cur = clist_begin(flag_list) ; cur != NULL ;
1623 cur = clist_next(cur)) {
1624 struct mailimap_flag_fetch * flag_fetch;
1626 flag_fetch = clist_content(cur);
1627 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1630 switch (flag_fetch->fl_flag->fl_type) {
1631 case MAILIMAP_FLAG_ANSWERED:
1632 flags |= MSG_REPLIED;
1634 case MAILIMAP_FLAG_FLAGGED:
1635 flags |= MSG_MARKED;
1637 case MAILIMAP_FLAG_DELETED:
1638 flags |= MSG_DELETED;
1640 case MAILIMAP_FLAG_SEEN:
1641 flags &= ~MSG_UNREAD;
1651 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1655 struct mailimap_msg_att_dynamic ** patt_dyn)
1657 clistiter * item_cur;
1661 struct mailimap_msg_att_dynamic * att_dyn;
1668 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1669 item_cur = clist_next(item_cur)) {
1670 struct mailimap_msg_att_item * item;
1672 item = clist_content(item_cur);
1674 switch (item->att_type) {
1675 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1676 switch (item->att_data.att_static->att_type) {
1677 case MAILIMAP_MSG_ATT_UID:
1678 uid = item->att_data.att_static->att_data.att_uid;
1681 case MAILIMAP_MSG_ATT_BODY_SECTION:
1682 if (headers == NULL) {
1683 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1686 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1687 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1692 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1693 if (att_dyn == NULL) {
1694 att_dyn = item->att_data.att_dyn;
1702 if (pheaders != NULL)
1703 * pheaders = headers;
1704 if (pref_size != NULL)
1705 * pref_size = ref_size;
1706 if (patt_dyn != NULL)
1707 * patt_dyn = att_dyn;
1709 return MAIL_NO_ERROR;
1712 static struct imap_fetch_env_info *
1713 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1715 struct imap_fetch_env_info * info;
1719 struct mailimap_msg_att_dynamic * att_dyn;
1721 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1724 info = malloc(sizeof(* info));
1726 info->headers = strdup(headers);
1728 info->flags = imap_flags_to_flags(att_dyn);
1734 imap_fetch_result_to_envelop_list(clist * fetch_result,
1735 carray ** p_env_list)
1743 env_list = carray_new(16);
1745 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1746 cur = clist_next(cur)) {
1747 struct mailimap_msg_att * msg_att;
1748 struct imap_fetch_env_info * env_info;
1750 msg_att = clist_content(cur);
1752 env_info = fetch_to_env_info(msg_att);
1753 carray_add(env_list, env_info, NULL);
1756 * p_env_list = env_list;
1758 return MAIL_NO_ERROR;
1761 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1763 struct mailimap_fetch_att * fetch_att;
1767 struct mailimap_header_list * imap_hdrlist;
1768 struct mailimap_section * section;
1770 hdrlist = clist_new();
1772 header = strdup("Date");
1773 r = clist_append(hdrlist, header);
1774 header = strdup("From");
1775 r = clist_append(hdrlist, header);
1776 header = strdup("To");
1777 r = clist_append(hdrlist, header);
1778 header = strdup("Cc");
1779 r = clist_append(hdrlist, header);
1780 header = strdup("Subject");
1781 r = clist_append(hdrlist, header);
1782 header = strdup("Message-ID");
1783 r = clist_append(hdrlist, header);
1784 header = strdup("References");
1785 r = clist_append(hdrlist, header);
1786 header = strdup("In-Reply-To");
1787 r = clist_append(hdrlist, header);
1789 imap_hdrlist = mailimap_header_list_new(hdrlist);
1790 section = mailimap_section_new_header_fields(imap_hdrlist);
1791 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1792 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1794 return MAIL_NO_ERROR;
1797 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1799 struct mailimap_fetch_att * fetch_att;
1800 struct mailimap_section * section;
1802 section = mailimap_section_new_header();
1803 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1804 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1806 return MAIL_NO_ERROR;
1810 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1811 carray ** p_env_list)
1813 struct mailimap_fetch_att * fetch_att;
1814 struct mailimap_fetch_type * fetch_type;
1816 clist * fetch_result;
1822 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1825 fetch_att = mailimap_fetch_att_new_uid();
1826 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1829 fetch_att = mailimap_fetch_att_new_flags();
1830 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1833 fetch_att = mailimap_fetch_att_new_rfc822_size();
1834 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1838 key.len = sizeof(imap);
1839 r = chash_get(courier_workaround_hash, &key, &value);
1841 r = imap_add_envelope_fetch_att(fetch_type);
1843 r = imap_add_header_fetch_att(fetch_type);
1845 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1848 case MAILIMAP_NO_ERROR:
1851 mailimap_fetch_type_free(fetch_type);
1855 if (clist_begin(fetch_result) == NULL) {
1856 res = MAILIMAP_ERROR_FETCH;
1860 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1861 mailimap_fetch_list_free(fetch_result);
1863 if (r != MAILIMAP_NO_ERROR) {
1864 mailimap_fetch_type_free(fetch_type);
1865 res = MAILIMAP_ERROR_MEMORY;
1869 mailimap_fetch_type_free(fetch_type);
1871 * p_env_list = env_list;
1873 return MAILIMAP_NO_ERROR;
1879 struct fetch_env_param {
1881 struct mailimap_set * set;
1884 struct fetch_env_result {
1885 carray * fetch_env_result;
1889 static void fetch_env_run(struct etpan_thread_op * op)
1891 struct fetch_env_param * param;
1892 struct fetch_env_result * result;
1899 r = imap_get_envelopes_list(param->imap, param->set,
1902 result = op->result;
1904 result->fetch_env_result = env_list;
1906 debug_print("imap fetch_env run - end %i\n", r);
1909 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1910 carray ** p_env_list)
1912 struct fetch_env_param param;
1913 struct fetch_env_result result;
1916 debug_print("imap fetch_env - begin\n");
1918 imap = get_imap(folder);
1922 threaded_run(folder, ¶m, &result, fetch_env_run);
1924 if (result.error != MAILIMAP_NO_ERROR) {
1930 key.len = sizeof(imap);
1931 r = chash_get(courier_workaround_hash, &key, &value);
1935 chash_set(courier_workaround_hash, &key, &value, NULL);
1937 threaded_run(folder, ¶m, &result, fetch_env_run);
1941 if (result.error != MAILIMAP_NO_ERROR)
1942 return result.error;
1944 debug_print("imap fetch_env - end\n");
1946 * p_env_list = result.fetch_env_result;
1948 return result.error;
1951 void imap_fetch_env_free(carray * env_list)
1955 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1956 struct imap_fetch_env_info * env_info;
1958 env_info = carray_get(env_list, i);
1959 free(env_info->headers);
1962 carray_free(env_list);
1969 struct append_param {
1971 const char * mailbox;
1972 const char * filename;
1973 struct mailimap_flag_list * flag_list;
1976 struct append_result {
1980 static void append_run(struct etpan_thread_op * op)
1982 struct append_param * param;
1983 struct append_result * result;
1987 struct stat stat_buf;
1991 result = op->result;
1993 r = stat(param->filename, &stat_buf);
1995 result->error = MAILIMAP_ERROR_APPEND;
1998 size = stat_buf.st_size;
2000 fd = open(param->filename, O_RDONLY);
2002 result->error = MAILIMAP_ERROR_APPEND;
2006 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2007 if (data == (void *) MAP_FAILED) {
2009 result->error = MAILIMAP_ERROR_APPEND;
2013 r = mailimap_append(param->imap, param->mailbox,
2014 param->flag_list, NULL,
2022 debug_print("imap append run - end %i\n", r);
2025 int imap_threaded_append(Folder * folder, const char * mailbox,
2026 const char * filename,
2027 struct mailimap_flag_list * flag_list)
2029 struct append_param param;
2030 struct append_result result;
2033 debug_print("imap append - begin\n");
2035 imap = get_imap(folder);
2037 param.mailbox = mailbox;
2038 param.filename = filename;
2039 param.flag_list = flag_list;
2041 threaded_run(folder, ¶m, &result, append_run);
2043 if (result.error != MAILIMAP_NO_ERROR)
2044 return result.error;
2046 debug_print("imap append - end\n");
2048 return result.error;
2054 struct expunge_param {
2058 struct expunge_result {
2062 static void expunge_run(struct etpan_thread_op * op)
2064 struct expunge_param * param;
2065 struct expunge_result * result;
2069 r = mailimap_expunge(param->imap);
2071 result = op->result;
2073 debug_print("imap expunge run - end %i\n", r);
2076 int imap_threaded_expunge(Folder * folder)
2078 struct expunge_param param;
2079 struct expunge_result result;
2081 debug_print("imap expunge - begin\n");
2083 param.imap = get_imap(folder);
2085 threaded_run(folder, ¶m, &result, expunge_run);
2087 debug_print("imap expunge - end\n");
2089 return result.error;
2095 struct mailimap_set * set;
2099 struct copy_result {
2103 static void copy_run(struct etpan_thread_op * op)
2105 struct copy_param * param;
2106 struct copy_result * result;
2111 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2113 result = op->result;
2116 debug_print("imap copy run - end %i\n", r);
2119 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2122 struct copy_param param;
2123 struct copy_result result;
2126 debug_print("imap copy - begin\n");
2128 imap = get_imap(folder);
2133 threaded_run(folder, ¶m, &result, copy_run);
2135 if (result.error != MAILIMAP_NO_ERROR)
2136 return result.error;
2138 debug_print("imap copy - end\n");
2140 return result.error;
2145 struct store_param {
2147 struct mailimap_set * set;
2148 struct mailimap_store_att_flags * store_att_flags;
2151 struct store_result {
2155 static void store_run(struct etpan_thread_op * op)
2157 struct store_param * param;
2158 struct store_result * result;
2163 r = mailimap_uid_store(param->imap, param->set,
2164 param->store_att_flags);
2166 result = op->result;
2169 debug_print("imap store run - end %i\n", r);
2172 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2173 struct mailimap_store_att_flags * store_att_flags)
2175 struct store_param param;
2176 struct store_result result;
2179 debug_print("imap store - begin\n");
2181 imap = get_imap(folder);
2184 param.store_att_flags = store_att_flags;
2186 threaded_run(folder, ¶m, &result, store_run);
2188 if (result.error != MAILIMAP_NO_ERROR)
2189 return result.error;
2191 debug_print("imap store - end\n");
2193 return result.error;
2197 #define ENV_BUFFER_SIZE 512
2199 static void do_exec_command(int fd, const char * command,
2200 const char * servername, uint16_t port)
2204 char env_buffer[ENV_BUFFER_SIZE];
2208 /* Fork again to become a child of init rather than
2209 the etpan client. */
2215 snprintf(env_buffer, ENV_BUFFER_SIZE,
2216 "ETPANSERVER=%s", servername);
2218 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2222 setenv("ETPANSERVER", servername, 1);
2224 unsetenv("ETPANSERVER");
2229 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2231 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2237 snprintf(porttext, sizeof(porttext), "%d", port);
2238 setenv("ETPANPORT", porttext, 1);
2241 unsetenv("ETPANPORT");
2245 /* Not a lot we can do if there's an error other than bail. */
2246 if (dup2(fd, 0) == -1)
2248 if (dup2(fd, 1) == -1)
2251 /* Should we close stderr and reopen /dev/null? */
2253 maxopen = sysconf(_SC_OPEN_MAX);
2254 for (i=3; i < maxopen; i++)
2258 /* Detach from the controlling tty if we have one. Otherwise,
2259 SSH might do something stupid like trying to use it instead
2260 of running $SSH_ASKPASS. Doh. */
2261 fd = open("/dev/tty", O_RDONLY);
2263 ioctl(fd, TIOCNOTTY, NULL);
2266 #endif /* TIOCNOTTY */
2268 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2270 /* Eep. Shouldn't reach this */
2274 static int subcommand_connect(const char *command,
2275 const char *servername, uint16_t port)
2277 /* SEB unsupported on Windows */
2281 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2286 do_exec_command(sockfds[1], command, servername, port);
2288 else if (childpid == -1) {
2296 /* Reap child, leaving grandchild process to run */
2297 waitpid(childpid, NULL, 0);
2302 int socket_connect_cmd(mailimap * imap, const char * command,
2303 const char * server, int port)
2309 fd = subcommand_connect(command, server, port);
2311 return MAILIMAP_ERROR_STREAM;
2313 s = mailstream_socket_open(fd);
2316 return MAILIMAP_ERROR_STREAM;
2319 r = mailimap_connect(imap, s);
2320 if (r != MAILIMAP_NO_ERROR) {
2321 mailstream_close(s);
2325 return MAILIMAP_NO_ERROR;
2330 struct connect_cmd_param {
2332 const char * command;
2333 const char * server;
2337 struct connect_cmd_result {
2341 static void connect_cmd_run(struct etpan_thread_op * op)
2344 struct connect_cmd_param * param;
2345 struct connect_cmd_result * result;
2348 result = op->result;
2350 r = socket_connect_cmd(param->imap, param->command,
2351 param->server, param->port);
2357 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2358 const char * server, int port)
2360 struct connect_cmd_param param;
2361 struct connect_cmd_result result;
2366 imap = mailimap_new(0, NULL);
2369 key.len = sizeof(folder);
2372 chash_set(session_hash, &key, &value, NULL);
2375 param.command = command;
2376 param.server = server;
2379 threaded_run(folder, ¶m, &result, connect_cmd_run);
2381 debug_print("connect_cmd ok %i\n", result.error);
2383 return result.error;
2387 void imap_main_init(void)
2390 void imap_main_done(void)
2393 void imap_main_set_timeout(int sec)