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, 511);
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 ok\n");
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);
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)
577 struct status_param param;
578 struct status_result result;
579 struct mailimap_status_att_list * status_att_list;
581 debug_print("imap status - begin\n");
583 status_att_list = mailimap_status_att_list_new_empty();
584 mailimap_status_att_list_add(status_att_list,
585 MAILIMAP_STATUS_ATT_MESSAGES);
586 mailimap_status_att_list_add(status_att_list,
587 MAILIMAP_STATUS_ATT_RECENT);
588 mailimap_status_att_list_add(status_att_list,
589 MAILIMAP_STATUS_ATT_UIDNEXT);
590 mailimap_status_att_list_add(status_att_list,
591 MAILIMAP_STATUS_ATT_UIDVALIDITY);
592 mailimap_status_att_list_add(status_att_list,
593 MAILIMAP_STATUS_ATT_UNSEEN);
595 param.imap = get_imap(folder);
597 param.status_att_list = status_att_list;
599 threaded_run(folder, ¶m, &result, status_run);
601 debug_print("imap status - end\n");
603 * data_status = result.data_status;
618 static void noop_run(struct etpan_thread_op * op)
620 struct noop_param * param;
621 struct noop_result * result;
625 r = mailimap_noop(param->imap);
629 debug_print("imap noop run - end %i\n", r);
632 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
634 struct noop_param param;
635 struct noop_result result;
638 debug_print("imap noop - begin\n");
640 imap = get_imap(folder);
643 threaded_run(folder, ¶m, &result, noop_run);
645 if (imap->imap_selection_info != NULL) {
646 * p_exists = imap->imap_selection_info->sel_exists;
652 debug_print("imap noop - end\n");
658 struct starttls_param {
662 struct starttls_result {
666 static void starttls_run(struct etpan_thread_op * op)
668 struct starttls_param * param;
669 struct starttls_result * result;
673 r = mailimap_starttls(param->imap);
677 debug_print("imap starttls run - end %i\n", r);
680 mailimap *imap = param->imap;
681 mailstream_low *plain_low = NULL;
682 mailstream_low *tls_low = NULL;
685 plain_low = mailstream_get_low(imap->imap_stream);
686 fd = mailstream_low_get_fd(plain_low);
688 debug_print("imap starttls run - can't get fd\n");
689 result->error = MAILIMAP_ERROR_STREAM;
692 tls_low = mailstream_low_ssl_open(fd);
693 if (tls_low == NULL) {
694 debug_print("imap starttls run - can't ssl_open\n");
695 result->error = MAILIMAP_ERROR_STREAM;
698 mailstream_low_free(plain_low);
699 mailstream_set_low(imap->imap_stream, tls_low);
703 int imap_threaded_starttls(Folder * folder)
705 struct starttls_param param;
706 struct starttls_result result;
708 debug_print("imap starttls - begin\n");
710 param.imap = get_imap(folder);
712 threaded_run(folder, ¶m, &result, starttls_run);
714 debug_print("imap starttls - end\n");
721 struct create_param {
726 struct create_result {
730 static void create_run(struct etpan_thread_op * op)
732 struct create_param * param;
733 struct create_result * result;
737 r = mailimap_create(param->imap, param->mb);
741 debug_print("imap create run - end %i\n", r);
744 int imap_threaded_create(Folder * folder, const char * mb)
746 struct create_param param;
747 struct create_result result;
749 debug_print("imap create - begin\n");
751 param.imap = get_imap(folder);
754 threaded_run(folder, ¶m, &result, create_run);
756 debug_print("imap create - end\n");
764 struct rename_param {
767 const char * new_name;
770 struct rename_result {
774 static void rename_run(struct etpan_thread_op * op)
776 struct rename_param * param;
777 struct rename_result * result;
781 r = mailimap_rename(param->imap, param->mb, param->new_name);
785 debug_print("imap rename run - end %i\n", r);
788 int imap_threaded_rename(Folder * folder,
789 const char * mb, const char * new_name)
791 struct rename_param param;
792 struct rename_result result;
794 debug_print("imap rename - begin\n");
796 param.imap = get_imap(folder);
798 param.new_name = new_name;
800 threaded_run(folder, ¶m, &result, rename_run);
802 debug_print("imap rename - end\n");
810 struct delete_param {
815 struct delete_result {
819 static void delete_run(struct etpan_thread_op * op)
821 struct delete_param * param;
822 struct delete_result * result;
826 r = mailimap_delete(param->imap, param->mb);
830 debug_print("imap delete run - end %i\n", r);
833 int imap_threaded_delete(Folder * folder, const char * mb)
835 struct delete_param param;
836 struct delete_result result;
838 debug_print("imap delete - begin\n");
840 param.imap = get_imap(folder);
843 threaded_run(folder, ¶m, &result, delete_run);
845 debug_print("imap delete - end\n");
852 struct select_param {
857 struct select_result {
861 static void select_run(struct etpan_thread_op * op)
863 struct select_param * param;
864 struct select_result * result;
868 r = mailimap_select(param->imap, param->mb);
872 debug_print("imap select run - end %i\n", r);
875 int imap_threaded_select(Folder * folder, const char * mb,
876 gint * exists, gint * recent, gint * unseen,
877 guint32 * uid_validity)
879 struct select_param param;
880 struct select_result result;
883 debug_print("imap select - begin\n");
885 imap = get_imap(folder);
889 threaded_run(folder, ¶m, &result, select_run);
891 if (result.error != MAILIMAP_NO_ERROR)
894 if (imap->imap_selection_info == NULL)
895 return MAILIMAP_ERROR_PARSE;
897 * exists = imap->imap_selection_info->sel_exists;
898 * recent = imap->imap_selection_info->sel_recent;
899 * unseen = imap->imap_selection_info->sel_unseen;
900 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
902 debug_print("imap select - end\n");
909 struct examine_param {
914 struct examine_result {
918 static void examine_run(struct etpan_thread_op * op)
920 struct examine_param * param;
921 struct examine_result * result;
925 r = mailimap_examine(param->imap, param->mb);
929 debug_print("imap examine run - end %i\n", r);
932 int imap_threaded_examine(Folder * folder, const char * mb,
933 gint * exists, gint * recent, gint * unseen,
934 guint32 * uid_validity)
936 struct examine_param param;
937 struct examine_result result;
940 debug_print("imap examine - begin\n");
942 imap = get_imap(folder);
946 threaded_run(folder, ¶m, &result, examine_run);
948 if (result.error != MAILIMAP_NO_ERROR)
951 if (imap->imap_selection_info == NULL)
952 return MAILIMAP_ERROR_PARSE;
954 * exists = imap->imap_selection_info->sel_exists;
955 * recent = imap->imap_selection_info->sel_recent;
956 * unseen = imap->imap_selection_info->sel_unseen;
957 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
959 debug_print("imap examine - end\n");
967 struct search_param {
970 struct mailimap_set * set;
973 struct search_result {
975 clist * search_result;
978 static void search_run(struct etpan_thread_op * op)
980 struct search_param * param;
981 struct search_result * result;
983 struct mailimap_search_key * key;
984 struct mailimap_search_key * uid_key;
985 struct mailimap_search_key * search_type_key;
986 clist * search_result;
990 uid_key = mailimap_search_key_new_uid(param->set);
992 search_type_key = NULL;
993 switch (param->type) {
994 case IMAP_SEARCH_TYPE_SIMPLE:
995 search_type_key = NULL;
998 case IMAP_SEARCH_TYPE_SEEN:
999 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1000 NULL, NULL, NULL, NULL, NULL,
1001 NULL, NULL, NULL, NULL, NULL,
1002 NULL, NULL, NULL, NULL, 0,
1003 NULL, NULL, NULL, NULL, NULL,
1004 NULL, 0, NULL, NULL, NULL);
1007 case IMAP_SEARCH_TYPE_UNSEEN:
1008 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1009 NULL, NULL, NULL, NULL, NULL,
1010 NULL, NULL, NULL, NULL, NULL,
1011 NULL, NULL, NULL, NULL, 0,
1012 NULL, NULL, NULL, NULL, NULL,
1013 NULL, 0, NULL, NULL, NULL);
1016 case IMAP_SEARCH_TYPE_ANSWERED:
1017 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1018 NULL, NULL, NULL, NULL, NULL,
1019 NULL, NULL, NULL, NULL, NULL,
1020 NULL, NULL, NULL, NULL, 0,
1021 NULL, NULL, NULL, NULL, NULL,
1022 NULL, 0, NULL, NULL, NULL);
1025 case IMAP_SEARCH_TYPE_FLAGGED:
1026 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1027 NULL, NULL, NULL, NULL, NULL,
1028 NULL, NULL, NULL, NULL, NULL,
1029 NULL, NULL, NULL, NULL, 0,
1030 NULL, NULL, NULL, NULL, NULL,
1031 NULL, 0, NULL, NULL, NULL);
1035 if (search_type_key != NULL) {
1036 key = mailimap_search_key_new_multiple_empty();
1037 mailimap_search_key_multiple_add(key, search_type_key);
1038 mailimap_search_key_multiple_add(key, uid_key);
1044 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1046 result = op->result;
1048 result->search_result = search_result;
1049 debug_print("imap search run - end %i\n", r);
1052 int imap_threaded_search(Folder * folder, int search_type,
1053 struct mailimap_set * set, clist ** search_result)
1055 struct search_param param;
1056 struct search_result result;
1059 debug_print("imap search - begin\n");
1061 imap = get_imap(folder);
1064 param.type = search_type;
1066 threaded_run(folder, ¶m, &result, search_run);
1068 if (result.error != MAILIMAP_NO_ERROR)
1069 return result.error;
1071 debug_print("imap search - end\n");
1073 * search_result = result.search_result;
1075 return result.error;
1082 uid_list_to_env_list(clist * fetch_result, carray ** result)
1090 tab = carray_new(128);
1092 res = MAILIMAP_ERROR_MEMORY;
1096 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1097 cur = clist_next(cur)) {
1098 struct mailimap_msg_att * msg_att;
1099 clistiter * item_cur;
1104 msg_att = clist_content(cur);
1108 for(item_cur = clist_begin(msg_att->att_list) ;
1110 item_cur = clist_next(item_cur)) {
1111 struct mailimap_msg_att_item * item;
1113 item = clist_content(item_cur);
1115 switch (item->att_type) {
1116 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1117 switch (item->att_data.att_static->att_type) {
1118 case MAILIMAP_MSG_ATT_UID:
1119 uid = item->att_data.att_static->att_data.att_uid;
1125 puid = malloc(sizeof(* puid));
1127 res = MAILIMAP_ERROR_MEMORY;
1132 r = carray_add(tab, puid, NULL);
1135 res = MAILIMAP_ERROR_MEMORY;
1142 return MAILIMAP_NO_ERROR;
1145 for(i = 0 ; i < carray_count(tab) ; i++)
1146 mailmessage_free(carray_get(tab, i));
1151 static int imap_get_messages_list(mailimap * imap,
1152 uint32_t first_index,
1157 struct mailimap_fetch_att * fetch_att;
1158 struct mailimap_fetch_type * fetch_type;
1159 struct mailimap_set * set;
1160 clist * fetch_result;
1163 set = mailimap_set_new_interval(first_index, 0);
1165 res = MAILIMAP_ERROR_MEMORY;
1169 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1170 if (fetch_type == NULL) {
1171 res = MAILIMAP_ERROR_MEMORY;
1175 fetch_att = mailimap_fetch_att_new_uid();
1176 if (fetch_att == NULL) {
1177 res = MAILIMAP_ERROR_MEMORY;
1178 goto free_fetch_type;
1181 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1182 if (r != MAILIMAP_NO_ERROR) {
1183 mailimap_fetch_att_free(fetch_att);
1184 res = MAILIMAP_ERROR_MEMORY;
1185 goto free_fetch_type;
1188 r = mailimap_uid_fetch(imap, set,
1189 fetch_type, &fetch_result);
1191 mailimap_fetch_type_free(fetch_type);
1192 mailimap_set_free(set);
1194 if (r != MAILIMAP_NO_ERROR) {
1200 r = uid_list_to_env_list(fetch_result, &env_list);
1201 mailimap_fetch_list_free(fetch_result);
1203 * result = env_list;
1205 return MAILIMAP_NO_ERROR;
1208 mailimap_fetch_type_free(fetch_type);
1210 mailimap_set_free(set);
1218 struct fetch_uid_param {
1220 uint32_t first_index;
1223 struct fetch_uid_result {
1225 carray * fetch_result;
1228 static void fetch_uid_run(struct etpan_thread_op * op)
1230 struct fetch_uid_param * param;
1231 struct fetch_uid_result * result;
1232 carray * fetch_result;
1237 fetch_result = NULL;
1238 r = imap_get_messages_list(param->imap, param->first_index,
1241 result = op->result;
1243 result->fetch_result = fetch_result;
1244 debug_print("imap fetch_uid run - end %i\n", r);
1247 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1248 carray ** fetch_result)
1250 struct fetch_uid_param param;
1251 struct fetch_uid_result result;
1254 debug_print("imap fetch_uid - begin\n");
1256 imap = get_imap(folder);
1258 param.first_index = first_index;
1260 threaded_run(folder, ¶m, &result, fetch_uid_run);
1262 if (result.error != MAILIMAP_NO_ERROR)
1263 return result.error;
1265 debug_print("imap fetch_uid - end\n");
1267 * fetch_result = result.fetch_result;
1269 return result.error;
1273 void imap_fetch_uid_list_free(carray * uid_list)
1277 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1280 puid = carray_get(uid_list, i);
1283 carray_free(uid_list);
1288 static int imap_fetch(mailimap * imap,
1291 size_t * result_len)
1294 struct mailimap_set * set;
1295 struct mailimap_fetch_att * fetch_att;
1296 struct mailimap_fetch_type * fetch_type;
1297 clist * fetch_result;
1298 struct mailimap_msg_att * msg_att;
1299 struct mailimap_msg_att_item * msg_att_item;
1304 struct mailimap_section * section;
1306 set = mailimap_set_new_single(msg_index);
1308 res = MAILIMAP_ERROR_MEMORY;
1312 section = mailimap_section_new(NULL);
1313 if (section == NULL) {
1314 res = MAILIMAP_ERROR_MEMORY;
1318 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1319 if (fetch_att == NULL) {
1320 mailimap_section_free(section);
1321 res = MAILIMAP_ERROR_MEMORY;
1325 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1326 if (fetch_type == NULL) {
1327 res = MAILIMAP_ERROR_MEMORY;
1328 goto free_fetch_att;
1331 r = mailimap_uid_fetch(imap, set,
1332 fetch_type, &fetch_result);
1334 mailimap_fetch_type_free(fetch_type);
1335 mailimap_set_free(set);
1338 case MAILIMAP_NO_ERROR:
1344 if (clist_begin(fetch_result) == NULL) {
1345 mailimap_fetch_list_free(fetch_result);
1346 return MAILIMAP_ERROR_FETCH;
1349 msg_att = clist_begin(fetch_result)->data;
1354 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1355 cur = clist_next(cur)) {
1356 msg_att_item = clist_content(cur);
1358 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1359 if (msg_att_item->att_data.att_static->att_type ==
1360 MAILIMAP_MSG_ATT_BODY_SECTION) {
1361 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1363 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1365 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1370 mailimap_fetch_list_free(fetch_result);
1373 return MAILIMAP_ERROR_FETCH;
1376 * result_len = text_length;
1378 return MAILIMAP_NO_ERROR;
1381 mailimap_fetch_att_free(fetch_att);
1383 mailimap_set_free(set);
1388 static int imap_fetch_header(mailimap * imap,
1391 size_t * result_len)
1394 struct mailimap_set * set;
1395 struct mailimap_fetch_att * fetch_att;
1396 struct mailimap_fetch_type * fetch_type;
1397 clist * fetch_result;
1398 struct mailimap_msg_att * msg_att;
1399 struct mailimap_msg_att_item * msg_att_item;
1404 struct mailimap_section * section;
1406 set = mailimap_set_new_single(msg_index);
1408 res = MAILIMAP_ERROR_MEMORY;
1412 section = mailimap_section_new_header();
1413 if (section == NULL) {
1414 res = MAILIMAP_ERROR_MEMORY;
1418 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1419 if (fetch_att == NULL) {
1420 mailimap_section_free(section);
1421 res = MAILIMAP_ERROR_MEMORY;
1425 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1426 if (fetch_type == NULL) {
1427 res = MAILIMAP_ERROR_MEMORY;
1428 goto free_fetch_att;
1431 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1433 mailimap_fetch_type_free(fetch_type);
1434 mailimap_set_free(set);
1437 case MAILIMAP_NO_ERROR:
1443 if (clist_begin(fetch_result) == NULL) {
1444 mailimap_fetch_list_free(fetch_result);
1445 return MAILIMAP_ERROR_FETCH;
1448 msg_att = clist_begin(fetch_result)->data;
1453 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1454 cur = clist_next(cur)) {
1455 msg_att_item = clist_content(cur);
1457 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1458 if (msg_att_item->att_data.att_static->att_type ==
1459 MAILIMAP_MSG_ATT_BODY_SECTION) {
1460 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1461 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1463 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1468 mailimap_fetch_list_free(fetch_result);
1471 return MAILIMAP_ERROR_FETCH;
1474 * result_len = text_length;
1476 return MAILIMAP_NO_ERROR;
1479 mailimap_fetch_att_free(fetch_att);
1481 mailimap_set_free(set);
1488 struct fetch_content_param {
1491 const char * filename;
1495 struct fetch_content_result {
1499 static void fetch_content_run(struct etpan_thread_op * op)
1501 struct fetch_content_param * param;
1502 struct fetch_content_result * result;
1504 size_t content_size;
1513 if (param->with_body)
1514 r = imap_fetch(param->imap, param->msg_index,
1515 &content, &content_size);
1517 r = imap_fetch_header(param->imap, param->msg_index,
1518 &content, &content_size);
1520 result = op->result;
1523 if (r == MAILIMAP_NO_ERROR) {
1524 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1526 result->error = MAILIMAP_ERROR_FETCH;
1530 f = fdopen(fd, "wb");
1532 result->error = MAILIMAP_ERROR_FETCH;
1536 r = fwrite(content, 1, content_size, f);
1543 g_unlink(param->filename);
1554 g_unlink(param->filename);
1557 if (mmap_string_unref(content) != 0)
1561 debug_print("imap fetch_content run - end %i\n", r);
1564 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1566 const char * filename)
1568 struct fetch_content_param param;
1569 struct fetch_content_result result;
1572 debug_print("imap fetch_content - begin\n");
1574 imap = get_imap(folder);
1576 param.msg_index = msg_index;
1577 param.filename = filename;
1578 param.with_body = with_body;
1580 threaded_run(folder, ¶m, &result, fetch_content_run);
1582 if (result.error != MAILIMAP_NO_ERROR)
1583 return result.error;
1585 debug_print("imap fetch_content - end\n");
1587 return result.error;
1592 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1600 flag_list = att_dyn->att_list;
1601 if (flag_list == NULL)
1604 for(cur = clist_begin(flag_list) ; cur != NULL ;
1605 cur = clist_next(cur)) {
1606 struct mailimap_flag_fetch * flag_fetch;
1608 flag_fetch = clist_content(cur);
1609 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1612 switch (flag_fetch->fl_flag->fl_type) {
1613 case MAILIMAP_FLAG_ANSWERED:
1614 flags |= MSG_REPLIED;
1616 case MAILIMAP_FLAG_FLAGGED:
1617 flags |= MSG_MARKED;
1619 case MAILIMAP_FLAG_DELETED:
1620 flags |= MSG_DELETED;
1622 case MAILIMAP_FLAG_SEEN:
1623 flags &= ~MSG_UNREAD;
1633 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1637 struct mailimap_msg_att_dynamic ** patt_dyn)
1639 clistiter * item_cur;
1643 struct mailimap_msg_att_dynamic * att_dyn;
1650 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1651 item_cur = clist_next(item_cur)) {
1652 struct mailimap_msg_att_item * item;
1654 item = clist_content(item_cur);
1656 switch (item->att_type) {
1657 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1658 switch (item->att_data.att_static->att_type) {
1659 case MAILIMAP_MSG_ATT_UID:
1660 uid = item->att_data.att_static->att_data.att_uid;
1663 case MAILIMAP_MSG_ATT_BODY_SECTION:
1664 if (headers == NULL) {
1665 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1668 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1669 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1674 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1675 if (att_dyn == NULL) {
1676 att_dyn = item->att_data.att_dyn;
1684 if (pheaders != NULL)
1685 * pheaders = headers;
1686 if (pref_size != NULL)
1687 * pref_size = ref_size;
1688 if (patt_dyn != NULL)
1689 * patt_dyn = att_dyn;
1691 return MAIL_NO_ERROR;
1694 static struct imap_fetch_env_info *
1695 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1697 struct imap_fetch_env_info * info;
1701 struct mailimap_msg_att_dynamic * att_dyn;
1703 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1706 info = malloc(sizeof(* info));
1708 info->headers = strdup(headers);
1710 info->flags = imap_flags_to_flags(att_dyn);
1716 imap_fetch_result_to_envelop_list(clist * fetch_result,
1717 carray ** p_env_list)
1725 env_list = carray_new(16);
1727 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1728 cur = clist_next(cur)) {
1729 struct mailimap_msg_att * msg_att;
1730 struct imap_fetch_env_info * env_info;
1732 msg_att = clist_content(cur);
1734 env_info = fetch_to_env_info(msg_att);
1735 carray_add(env_list, env_info, NULL);
1738 * p_env_list = env_list;
1740 return MAIL_NO_ERROR;
1743 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1745 struct mailimap_fetch_att * fetch_att;
1749 struct mailimap_header_list * imap_hdrlist;
1750 struct mailimap_section * section;
1752 hdrlist = clist_new();
1754 header = strdup("Date");
1755 r = clist_append(hdrlist, header);
1756 header = strdup("From");
1757 r = clist_append(hdrlist, header);
1758 header = strdup("To");
1759 r = clist_append(hdrlist, header);
1760 header = strdup("Cc");
1761 r = clist_append(hdrlist, header);
1762 header = strdup("Subject");
1763 r = clist_append(hdrlist, header);
1764 header = strdup("Message-ID");
1765 r = clist_append(hdrlist, header);
1766 header = strdup("References");
1767 r = clist_append(hdrlist, header);
1768 header = strdup("In-Reply-To");
1769 r = clist_append(hdrlist, header);
1771 imap_hdrlist = mailimap_header_list_new(hdrlist);
1772 section = mailimap_section_new_header_fields(imap_hdrlist);
1773 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1774 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1776 return MAIL_NO_ERROR;
1779 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1781 struct mailimap_fetch_att * fetch_att;
1782 struct mailimap_section * section;
1784 section = mailimap_section_new_header();
1785 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1786 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1788 return MAIL_NO_ERROR;
1792 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1793 carray ** p_env_list)
1795 struct mailimap_fetch_att * fetch_att;
1796 struct mailimap_fetch_type * fetch_type;
1798 clist * fetch_result;
1804 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1807 fetch_att = mailimap_fetch_att_new_uid();
1808 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1811 fetch_att = mailimap_fetch_att_new_flags();
1812 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1815 fetch_att = mailimap_fetch_att_new_rfc822_size();
1816 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1820 key.len = sizeof(imap);
1821 r = chash_get(courier_workaround_hash, &key, &value);
1823 r = imap_add_envelope_fetch_att(fetch_type);
1825 r = imap_add_header_fetch_att(fetch_type);
1827 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1830 case MAILIMAP_NO_ERROR:
1833 mailimap_fetch_type_free(fetch_type);
1837 if (clist_begin(fetch_result) == NULL) {
1838 res = MAILIMAP_ERROR_FETCH;
1842 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1843 mailimap_fetch_list_free(fetch_result);
1845 if (r != MAILIMAP_NO_ERROR) {
1846 mailimap_fetch_type_free(fetch_type);
1847 res = MAILIMAP_ERROR_MEMORY;
1851 mailimap_fetch_type_free(fetch_type);
1853 * p_env_list = env_list;
1855 return MAILIMAP_NO_ERROR;
1861 struct fetch_env_param {
1863 struct mailimap_set * set;
1866 struct fetch_env_result {
1867 carray * fetch_env_result;
1871 static void fetch_env_run(struct etpan_thread_op * op)
1873 struct fetch_env_param * param;
1874 struct fetch_env_result * result;
1881 r = imap_get_envelopes_list(param->imap, param->set,
1884 result = op->result;
1886 result->fetch_env_result = env_list;
1888 debug_print("imap fetch_env run - end %i\n", r);
1891 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1892 carray ** p_env_list)
1894 struct fetch_env_param param;
1895 struct fetch_env_result result;
1898 debug_print("imap fetch_env - begin\n");
1900 imap = get_imap(folder);
1904 threaded_run(folder, ¶m, &result, fetch_env_run);
1906 if (result.error != MAILIMAP_NO_ERROR) {
1912 key.len = sizeof(imap);
1913 r = chash_get(courier_workaround_hash, &key, &value);
1917 chash_set(courier_workaround_hash, &key, &value, NULL);
1919 threaded_run(folder, ¶m, &result, fetch_env_run);
1923 if (result.error != MAILIMAP_NO_ERROR)
1924 return result.error;
1926 debug_print("imap fetch_env - end\n");
1928 * p_env_list = result.fetch_env_result;
1930 return result.error;
1933 void imap_fetch_env_free(carray * env_list)
1937 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1938 struct imap_fetch_env_info * env_info;
1940 env_info = carray_get(env_list, i);
1941 free(env_info->headers);
1944 carray_free(env_list);
1951 struct append_param {
1953 const char * mailbox;
1954 const char * filename;
1955 struct mailimap_flag_list * flag_list;
1958 struct append_result {
1962 static void append_run(struct etpan_thread_op * op)
1964 struct append_param * param;
1965 struct append_result * result;
1969 struct stat stat_buf;
1973 result = op->result;
1975 r = stat(param->filename, &stat_buf);
1977 result->error = MAILIMAP_ERROR_APPEND;
1980 size = stat_buf.st_size;
1982 fd = open(param->filename, O_RDONLY);
1984 result->error = MAILIMAP_ERROR_APPEND;
1988 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1989 if (data == (void *) MAP_FAILED) {
1991 result->error = MAILIMAP_ERROR_APPEND;
1995 r = mailimap_append(param->imap, param->mailbox,
1996 param->flag_list, NULL,
2004 debug_print("imap append run - end %i\n", r);
2007 int imap_threaded_append(Folder * folder, const char * mailbox,
2008 const char * filename,
2009 struct mailimap_flag_list * flag_list)
2011 struct append_param param;
2012 struct append_result result;
2015 debug_print("imap append - begin\n");
2017 imap = get_imap(folder);
2019 param.mailbox = mailbox;
2020 param.filename = filename;
2021 param.flag_list = flag_list;
2023 threaded_run(folder, ¶m, &result, append_run);
2025 if (result.error != MAILIMAP_NO_ERROR)
2026 return result.error;
2028 debug_print("imap append - end\n");
2030 return result.error;
2036 struct expunge_param {
2040 struct expunge_result {
2044 static void expunge_run(struct etpan_thread_op * op)
2046 struct expunge_param * param;
2047 struct expunge_result * result;
2051 r = mailimap_expunge(param->imap);
2053 result = op->result;
2055 debug_print("imap expunge run - end %i\n", r);
2058 int imap_threaded_expunge(Folder * folder)
2060 struct expunge_param param;
2061 struct expunge_result result;
2063 debug_print("imap expunge - begin\n");
2065 param.imap = get_imap(folder);
2067 threaded_run(folder, ¶m, &result, expunge_run);
2069 debug_print("imap expunge - end\n");
2071 return result.error;
2077 struct mailimap_set * set;
2081 struct copy_result {
2085 static void copy_run(struct etpan_thread_op * op)
2087 struct copy_param * param;
2088 struct copy_result * result;
2093 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2095 result = op->result;
2098 debug_print("imap copy run - end %i\n", r);
2101 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2104 struct copy_param param;
2105 struct copy_result result;
2108 debug_print("imap copy - begin\n");
2110 imap = get_imap(folder);
2115 threaded_run(folder, ¶m, &result, copy_run);
2117 if (result.error != MAILIMAP_NO_ERROR)
2118 return result.error;
2120 debug_print("imap copy - end\n");
2122 return result.error;
2127 struct store_param {
2129 struct mailimap_set * set;
2130 struct mailimap_store_att_flags * store_att_flags;
2133 struct store_result {
2137 static void store_run(struct etpan_thread_op * op)
2139 struct store_param * param;
2140 struct store_result * result;
2145 r = mailimap_uid_store(param->imap, param->set,
2146 param->store_att_flags);
2148 result = op->result;
2151 debug_print("imap store run - end %i\n", r);
2154 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2155 struct mailimap_store_att_flags * store_att_flags)
2157 struct store_param param;
2158 struct store_result result;
2161 debug_print("imap store - begin\n");
2163 imap = get_imap(folder);
2166 param.store_att_flags = store_att_flags;
2168 threaded_run(folder, ¶m, &result, store_run);
2170 if (result.error != MAILIMAP_NO_ERROR)
2171 return result.error;
2173 debug_print("imap store - end\n");
2175 return result.error;
2179 #define ENV_BUFFER_SIZE 512
2181 static void do_exec_command(int fd, const char * command,
2182 const char * servername, uint16_t port)
2186 char env_buffer[ENV_BUFFER_SIZE];
2190 /* Fork again to become a child of init rather than
2191 the etpan client. */
2197 snprintf(env_buffer, ENV_BUFFER_SIZE,
2198 "ETPANSERVER=%s", servername);
2200 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2204 setenv("ETPANSERVER", servername, 1);
2206 unsetenv("ETPANSERVER");
2211 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2213 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2219 snprintf(porttext, sizeof(porttext), "%d", port);
2220 setenv("ETPANPORT", porttext, 1);
2223 unsetenv("ETPANPORT");
2227 /* Not a lot we can do if there's an error other than bail. */
2228 if (dup2(fd, 0) == -1)
2230 if (dup2(fd, 1) == -1)
2233 /* Should we close stderr and reopen /dev/null? */
2235 maxopen = sysconf(_SC_OPEN_MAX);
2236 for (i=3; i < maxopen; i++)
2240 /* Detach from the controlling tty if we have one. Otherwise,
2241 SSH might do something stupid like trying to use it instead
2242 of running $SSH_ASKPASS. Doh. */
2243 fd = open("/dev/tty", O_RDONLY);
2245 ioctl(fd, TIOCNOTTY, NULL);
2248 #endif /* TIOCNOTTY */
2250 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2252 /* Eep. Shouldn't reach this */
2256 static int subcommand_connect(const char *command,
2257 const char *servername, uint16_t port)
2259 /* SEB unsupported on Windows */
2263 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2268 do_exec_command(sockfds[1], command, servername, port);
2270 else if (childpid == -1) {
2278 /* Reap child, leaving grandchild process to run */
2279 waitpid(childpid, NULL, 0);
2284 int socket_connect_cmd(mailimap * imap, const char * command,
2285 const char * server, int port)
2291 fd = subcommand_connect(command, server, port);
2293 return MAILIMAP_ERROR_STREAM;
2295 s = mailstream_socket_open(fd);
2298 return MAILIMAP_ERROR_STREAM;
2301 r = mailimap_connect(imap, s);
2302 if (r != MAILIMAP_NO_ERROR) {
2303 mailstream_close(s);
2307 return MAILIMAP_NO_ERROR;
2312 struct connect_cmd_param {
2314 const char * command;
2315 const char * server;
2319 struct connect_cmd_result {
2323 static void connect_cmd_run(struct etpan_thread_op * op)
2326 struct connect_cmd_param * param;
2327 struct connect_cmd_result * result;
2330 result = op->result;
2332 r = socket_connect_cmd(param->imap, param->command,
2333 param->server, param->port);
2339 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2340 const char * server, int port)
2342 struct connect_cmd_param param;
2343 struct connect_cmd_result result;
2348 imap = mailimap_new(0, NULL);
2351 key.len = sizeof(folder);
2354 chash_set(session_hash, &key, &value, NULL);
2357 param.command = command;
2358 param.server = server;
2361 threaded_run(folder, ¶m, &result, connect_cmd_run);
2363 debug_print("connect_cmd ok %i\n", result.error);
2365 return result.error;
2369 void imap_main_init(void)
2372 void imap_main_done(void)
2375 void imap_main_set_timeout(int sec)