7 #include "imap-thread.h"
11 #if defined (__NetBSD__)
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");
328 struct disconnect_param {
332 struct disconnect_result {
336 static void disconnect_run(struct etpan_thread_op * op)
339 struct disconnect_param * param;
340 struct disconnect_result * result;
345 r = mailimap_logout(param->imap);
350 void imap_threaded_disconnect(Folder * folder)
352 struct connect_param param;
353 struct connect_result result;
358 imap = get_imap(folder);
360 debug_print("was disconnected\n");
366 threaded_run(folder, ¶m, &result, disconnect_run);
369 key.len = sizeof(folder);
372 chash_delete(session_hash, &key, NULL);
375 key.len = sizeof(imap);
376 chash_delete(courier_workaround_hash, &key, NULL);
380 debug_print("disconnect ok\n");
387 const char * wildcard;
395 static void list_run(struct etpan_thread_op * op)
397 struct list_param * param;
398 struct list_result * result;
404 r = mailimap_list(param->imap, param->base,
405 param->wildcard, &list);
410 debug_print("imap list run - end\n");
413 int imap_threaded_list(Folder * folder, const char * base,
414 const char * wildcard,
417 struct list_param param;
418 struct list_result result;
420 debug_print("imap list - begin\n");
422 param.imap = get_imap(folder);
424 param.wildcard = wildcard;
426 threaded_run(folder, ¶m, &result, list_run);
428 * p_result = result.list;
430 debug_print("imap list - end %p\n", result.list);
440 const char * password;
443 struct login_result {
447 static void login_run(struct etpan_thread_op * op)
449 struct login_param * param;
450 struct login_result * result;
452 #ifdef DISABLE_LOG_DURING_LOGIN
458 #ifdef DISABLE_LOG_DURING_LOGIN
459 old_debug = mailstream_debug;
460 mailstream_debug = 0;
463 r = mailimap_login(param->imap,
464 param->login, param->password);
466 #ifdef DISABLE_LOG_DURING_LOGIN
467 mailstream_debug = old_debug;
472 debug_print("imap login run - end %i\n", r);
475 int imap_threaded_login(Folder * folder,
476 const char * login, const char * password)
478 struct login_param param;
479 struct login_result result;
481 debug_print("imap login - begin\n");
483 param.imap = get_imap(folder);
485 param.password = password;
487 threaded_run(folder, ¶m, &result, login_run);
489 debug_print("imap login - end\n");
495 struct status_param {
498 struct mailimap_status_att_list * status_att_list;
501 struct status_result {
503 struct mailimap_mailbox_data_status * data_status;
506 static void status_run(struct etpan_thread_op * op)
508 struct status_param * param;
509 struct status_result * result;
515 r = mailimap_status(param->imap, param->mb,
516 param->status_att_list,
517 &result->data_status);
520 debug_print("imap status run - end %i\n", r);
523 int imap_threaded_status(Folder * folder, const char * mb,
524 struct mailimap_mailbox_data_status ** data_status)
526 struct status_param param;
527 struct status_result result;
528 struct mailimap_status_att_list * status_att_list;
530 debug_print("imap status - begin\n");
532 status_att_list = mailimap_status_att_list_new_empty();
533 mailimap_status_att_list_add(status_att_list,
534 MAILIMAP_STATUS_ATT_MESSAGES);
535 mailimap_status_att_list_add(status_att_list,
536 MAILIMAP_STATUS_ATT_RECENT);
537 mailimap_status_att_list_add(status_att_list,
538 MAILIMAP_STATUS_ATT_UIDNEXT);
539 mailimap_status_att_list_add(status_att_list,
540 MAILIMAP_STATUS_ATT_UIDVALIDITY);
541 mailimap_status_att_list_add(status_att_list,
542 MAILIMAP_STATUS_ATT_UNSEEN);
544 param.imap = get_imap(folder);
546 param.status_att_list = status_att_list;
548 threaded_run(folder, ¶m, &result, status_run);
550 debug_print("imap status - end\n");
552 * data_status = result.data_status;
567 static void noop_run(struct etpan_thread_op * op)
569 struct noop_param * param;
570 struct noop_result * result;
574 r = mailimap_noop(param->imap);
578 debug_print("imap noop run - end %i\n", r);
581 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
583 struct noop_param param;
584 struct noop_result result;
587 debug_print("imap noop - begin\n");
589 imap = get_imap(folder);
592 threaded_run(folder, ¶m, &result, noop_run);
594 if (imap->imap_selection_info != NULL) {
595 * p_exists = imap->imap_selection_info->sel_exists;
601 debug_print("imap noop - end\n");
607 struct starttls_param {
611 struct starttls_result {
615 static void starttls_run(struct etpan_thread_op * op)
617 struct starttls_param * param;
618 struct starttls_result * result;
622 r = mailimap_starttls(param->imap);
626 debug_print("imap starttls run - end %i\n", r);
629 mailimap *imap = param->imap;
630 mailstream_low *plain_low = NULL;
631 mailstream_low *tls_low = NULL;
634 plain_low = mailstream_get_low(imap->imap_stream);
635 fd = mailstream_low_get_fd(plain_low);
637 debug_print("imap starttls run - can't get fd\n");
638 result->error = MAILIMAP_ERROR_STREAM;
641 tls_low = mailstream_low_ssl_open(fd);
642 if (tls_low == NULL) {
643 debug_print("imap starttls run - can't ssl_open\n");
644 result->error = MAILIMAP_ERROR_STREAM;
647 mailstream_low_free(plain_low);
648 mailstream_set_low(imap->imap_stream, tls_low);
652 int imap_threaded_starttls(Folder * folder)
654 struct starttls_param param;
655 struct starttls_result result;
657 debug_print("imap starttls - begin\n");
659 param.imap = get_imap(folder);
661 threaded_run(folder, ¶m, &result, starttls_run);
663 debug_print("imap starttls - end\n");
670 struct create_param {
675 struct create_result {
679 static void create_run(struct etpan_thread_op * op)
681 struct create_param * param;
682 struct create_result * result;
686 r = mailimap_create(param->imap, param->mb);
690 debug_print("imap create run - end %i\n", r);
693 int imap_threaded_create(Folder * folder, const char * mb)
695 struct create_param param;
696 struct create_result result;
698 debug_print("imap create - begin\n");
700 param.imap = get_imap(folder);
703 threaded_run(folder, ¶m, &result, create_run);
705 debug_print("imap create - end\n");
713 struct rename_param {
716 const char * new_name;
719 struct rename_result {
723 static void rename_run(struct etpan_thread_op * op)
725 struct rename_param * param;
726 struct rename_result * result;
730 r = mailimap_rename(param->imap, param->mb, param->new_name);
734 debug_print("imap rename run - end %i\n", r);
737 int imap_threaded_rename(Folder * folder,
738 const char * mb, const char * new_name)
740 struct rename_param param;
741 struct rename_result result;
743 debug_print("imap rename - begin\n");
745 param.imap = get_imap(folder);
747 param.new_name = new_name;
749 threaded_run(folder, ¶m, &result, rename_run);
751 debug_print("imap rename - end\n");
759 struct delete_param {
764 struct delete_result {
768 static void delete_run(struct etpan_thread_op * op)
770 struct delete_param * param;
771 struct delete_result * result;
775 r = mailimap_delete(param->imap, param->mb);
779 debug_print("imap delete run - end %i\n", r);
782 int imap_threaded_delete(Folder * folder, const char * mb)
784 struct delete_param param;
785 struct delete_result result;
787 debug_print("imap delete - begin\n");
789 param.imap = get_imap(folder);
792 threaded_run(folder, ¶m, &result, delete_run);
794 debug_print("imap delete - end\n");
801 struct select_param {
806 struct select_result {
810 static void select_run(struct etpan_thread_op * op)
812 struct select_param * param;
813 struct select_result * result;
817 r = mailimap_select(param->imap, param->mb);
821 debug_print("imap select run - end %i\n", r);
824 int imap_threaded_select(Folder * folder, const char * mb,
825 gint * exists, gint * recent, gint * unseen,
826 guint32 * uid_validity)
828 struct select_param param;
829 struct select_result result;
832 debug_print("imap select - begin\n");
834 imap = get_imap(folder);
838 threaded_run(folder, ¶m, &result, select_run);
840 if (result.error != MAILIMAP_NO_ERROR)
843 if (imap->imap_selection_info == NULL)
844 return MAILIMAP_ERROR_PARSE;
846 * exists = imap->imap_selection_info->sel_exists;
847 * recent = imap->imap_selection_info->sel_recent;
848 * unseen = imap->imap_selection_info->sel_unseen;
849 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
851 debug_print("imap select - end\n");
858 struct examine_param {
863 struct examine_result {
867 static void examine_run(struct etpan_thread_op * op)
869 struct examine_param * param;
870 struct examine_result * result;
874 r = mailimap_examine(param->imap, param->mb);
878 debug_print("imap examine run - end %i\n", r);
881 int imap_threaded_examine(Folder * folder, const char * mb,
882 gint * exists, gint * recent, gint * unseen,
883 guint32 * uid_validity)
885 struct examine_param param;
886 struct examine_result result;
889 debug_print("imap examine - begin\n");
891 imap = get_imap(folder);
895 threaded_run(folder, ¶m, &result, examine_run);
897 if (result.error != MAILIMAP_NO_ERROR)
900 if (imap->imap_selection_info == NULL)
901 return MAILIMAP_ERROR_PARSE;
903 * exists = imap->imap_selection_info->sel_exists;
904 * recent = imap->imap_selection_info->sel_recent;
905 * unseen = imap->imap_selection_info->sel_unseen;
906 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
908 debug_print("imap examine - end\n");
916 struct search_param {
919 struct mailimap_set * set;
922 struct search_result {
924 clist * search_result;
927 static void search_run(struct etpan_thread_op * op)
929 struct search_param * param;
930 struct search_result * result;
932 struct mailimap_search_key * key;
933 struct mailimap_search_key * uid_key;
934 struct mailimap_search_key * search_type_key;
935 clist * search_result;
939 uid_key = mailimap_search_key_new_uid(param->set);
941 search_type_key = NULL;
942 switch (param->type) {
943 case IMAP_SEARCH_TYPE_SIMPLE:
944 search_type_key = NULL;
947 case IMAP_SEARCH_TYPE_SEEN:
948 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
949 NULL, NULL, NULL, NULL, NULL,
950 NULL, NULL, NULL, NULL, NULL,
951 NULL, NULL, NULL, NULL, 0,
952 NULL, NULL, NULL, NULL, NULL,
953 NULL, 0, NULL, NULL, NULL);
956 case IMAP_SEARCH_TYPE_UNSEEN:
957 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
958 NULL, NULL, NULL, NULL, NULL,
959 NULL, NULL, NULL, NULL, NULL,
960 NULL, NULL, NULL, NULL, 0,
961 NULL, NULL, NULL, NULL, NULL,
962 NULL, 0, NULL, NULL, NULL);
965 case IMAP_SEARCH_TYPE_ANSWERED:
966 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
967 NULL, NULL, NULL, NULL, NULL,
968 NULL, NULL, NULL, NULL, NULL,
969 NULL, NULL, NULL, NULL, 0,
970 NULL, NULL, NULL, NULL, NULL,
971 NULL, 0, NULL, NULL, NULL);
974 case IMAP_SEARCH_TYPE_FLAGGED:
975 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
976 NULL, NULL, NULL, NULL, NULL,
977 NULL, NULL, NULL, NULL, NULL,
978 NULL, NULL, NULL, NULL, 0,
979 NULL, NULL, NULL, NULL, NULL,
980 NULL, 0, NULL, NULL, NULL);
984 if (search_type_key != NULL) {
985 key = mailimap_search_key_new_multiple_empty();
986 mailimap_search_key_multiple_add(key, search_type_key);
987 mailimap_search_key_multiple_add(key, uid_key);
993 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
997 result->search_result = search_result;
998 debug_print("imap search run - end %i\n", r);
1001 int imap_threaded_search(Folder * folder, int search_type,
1002 struct mailimap_set * set, clist ** search_result)
1004 struct search_param param;
1005 struct search_result result;
1008 debug_print("imap search - begin\n");
1010 imap = get_imap(folder);
1013 param.type = search_type;
1015 threaded_run(folder, ¶m, &result, search_run);
1017 if (result.error != MAILIMAP_NO_ERROR)
1018 return result.error;
1020 debug_print("imap search - end\n");
1022 * search_result = result.search_result;
1024 return result.error;
1031 uid_list_to_env_list(clist * fetch_result, carray ** result)
1039 tab = carray_new(128);
1041 res = MAILIMAP_ERROR_MEMORY;
1045 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1046 cur = clist_next(cur)) {
1047 struct mailimap_msg_att * msg_att;
1048 clistiter * item_cur;
1053 msg_att = clist_content(cur);
1057 for(item_cur = clist_begin(msg_att->att_list) ;
1059 item_cur = clist_next(item_cur)) {
1060 struct mailimap_msg_att_item * item;
1062 item = clist_content(item_cur);
1064 switch (item->att_type) {
1065 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1066 switch (item->att_data.att_static->att_type) {
1067 case MAILIMAP_MSG_ATT_UID:
1068 uid = item->att_data.att_static->att_data.att_uid;
1074 puid = malloc(sizeof(* puid));
1076 res = MAILIMAP_ERROR_MEMORY;
1081 r = carray_add(tab, puid, NULL);
1084 res = MAILIMAP_ERROR_MEMORY;
1091 return MAILIMAP_NO_ERROR;
1094 for(i = 0 ; i < carray_count(tab) ; i++)
1095 mailmessage_free(carray_get(tab, i));
1100 static int imap_get_messages_list(mailimap * imap,
1101 uint32_t first_index,
1106 struct mailimap_fetch_att * fetch_att;
1107 struct mailimap_fetch_type * fetch_type;
1108 struct mailimap_set * set;
1109 clist * fetch_result;
1112 set = mailimap_set_new_interval(first_index, 0);
1114 res = MAILIMAP_ERROR_MEMORY;
1118 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1119 if (fetch_type == NULL) {
1120 res = MAILIMAP_ERROR_MEMORY;
1124 fetch_att = mailimap_fetch_att_new_uid();
1125 if (fetch_att == NULL) {
1126 res = MAILIMAP_ERROR_MEMORY;
1127 goto free_fetch_type;
1130 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1131 if (r != MAILIMAP_NO_ERROR) {
1132 mailimap_fetch_att_free(fetch_att);
1133 res = MAILIMAP_ERROR_MEMORY;
1134 goto free_fetch_type;
1137 r = mailimap_uid_fetch(imap, set,
1138 fetch_type, &fetch_result);
1140 mailimap_fetch_type_free(fetch_type);
1141 mailimap_set_free(set);
1143 if (r != MAILIMAP_NO_ERROR) {
1149 r = uid_list_to_env_list(fetch_result, &env_list);
1150 mailimap_fetch_list_free(fetch_result);
1152 * result = env_list;
1154 return MAILIMAP_NO_ERROR;
1157 mailimap_fetch_type_free(fetch_type);
1159 mailimap_set_free(set);
1167 struct fetch_uid_param {
1169 uint32_t first_index;
1172 struct fetch_uid_result {
1174 carray * fetch_result;
1177 static void fetch_uid_run(struct etpan_thread_op * op)
1179 struct fetch_uid_param * param;
1180 struct fetch_uid_result * result;
1181 carray * fetch_result;
1186 fetch_result = NULL;
1187 r = imap_get_messages_list(param->imap, param->first_index,
1190 result = op->result;
1192 result->fetch_result = fetch_result;
1193 debug_print("imap fetch_uid run - end %i\n", r);
1196 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1197 carray ** fetch_result)
1199 struct fetch_uid_param param;
1200 struct fetch_uid_result result;
1203 debug_print("imap fetch_uid - begin\n");
1205 imap = get_imap(folder);
1207 param.first_index = first_index;
1209 threaded_run(folder, ¶m, &result, fetch_uid_run);
1211 if (result.error != MAILIMAP_NO_ERROR)
1212 return result.error;
1214 debug_print("imap fetch_uid - end\n");
1216 * fetch_result = result.fetch_result;
1218 return result.error;
1222 void imap_fetch_uid_list_free(carray * uid_list)
1226 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1229 puid = carray_get(uid_list, i);
1232 carray_free(uid_list);
1237 static int imap_fetch(mailimap * imap,
1240 size_t * result_len)
1243 struct mailimap_set * set;
1244 struct mailimap_fetch_att * fetch_att;
1245 struct mailimap_fetch_type * fetch_type;
1246 clist * fetch_result;
1247 struct mailimap_msg_att * msg_att;
1248 struct mailimap_msg_att_item * msg_att_item;
1253 struct mailimap_section * section;
1255 set = mailimap_set_new_single(msg_index);
1257 res = MAILIMAP_ERROR_MEMORY;
1261 section = mailimap_section_new(NULL);
1262 if (section == NULL) {
1263 res = MAILIMAP_ERROR_MEMORY;
1267 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1268 if (fetch_att == NULL) {
1269 mailimap_section_free(section);
1270 res = MAILIMAP_ERROR_MEMORY;
1274 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1275 if (fetch_type == NULL) {
1276 res = MAILIMAP_ERROR_MEMORY;
1277 goto free_fetch_att;
1280 r = mailimap_uid_fetch(imap, set,
1281 fetch_type, &fetch_result);
1283 mailimap_fetch_type_free(fetch_type);
1284 mailimap_set_free(set);
1287 case MAILIMAP_NO_ERROR:
1293 if (clist_begin(fetch_result) == NULL) {
1294 mailimap_fetch_list_free(fetch_result);
1295 return MAILIMAP_ERROR_FETCH;
1298 msg_att = clist_begin(fetch_result)->data;
1303 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1304 cur = clist_next(cur)) {
1305 msg_att_item = clist_content(cur);
1307 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1308 if (msg_att_item->att_data.att_static->att_type ==
1309 MAILIMAP_MSG_ATT_BODY_SECTION) {
1310 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1312 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1314 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1319 mailimap_fetch_list_free(fetch_result);
1322 return MAILIMAP_ERROR_FETCH;
1325 * result_len = text_length;
1327 return MAILIMAP_NO_ERROR;
1330 mailimap_fetch_att_free(fetch_att);
1332 mailimap_set_free(set);
1337 static int imap_fetch_header(mailimap * imap,
1340 size_t * result_len)
1343 struct mailimap_set * set;
1344 struct mailimap_fetch_att * fetch_att;
1345 struct mailimap_fetch_type * fetch_type;
1346 clist * fetch_result;
1347 struct mailimap_msg_att * msg_att;
1348 struct mailimap_msg_att_item * msg_att_item;
1353 struct mailimap_section * section;
1355 set = mailimap_set_new_single(msg_index);
1357 res = MAILIMAP_ERROR_MEMORY;
1361 section = mailimap_section_new_header();
1362 if (section == NULL) {
1363 res = MAILIMAP_ERROR_MEMORY;
1367 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1368 if (fetch_att == NULL) {
1369 mailimap_section_free(section);
1370 res = MAILIMAP_ERROR_MEMORY;
1374 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1375 if (fetch_type == NULL) {
1376 res = MAILIMAP_ERROR_MEMORY;
1377 goto free_fetch_att;
1380 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1382 mailimap_fetch_type_free(fetch_type);
1383 mailimap_set_free(set);
1386 case MAILIMAP_NO_ERROR:
1392 if (clist_begin(fetch_result) == NULL) {
1393 mailimap_fetch_list_free(fetch_result);
1394 return MAILIMAP_ERROR_FETCH;
1397 msg_att = clist_begin(fetch_result)->data;
1402 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1403 cur = clist_next(cur)) {
1404 msg_att_item = clist_content(cur);
1406 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1407 if (msg_att_item->att_data.att_static->att_type ==
1408 MAILIMAP_MSG_ATT_BODY_SECTION) {
1409 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1410 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1412 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1417 mailimap_fetch_list_free(fetch_result);
1420 return MAILIMAP_ERROR_FETCH;
1423 * result_len = text_length;
1425 return MAILIMAP_NO_ERROR;
1428 mailimap_fetch_att_free(fetch_att);
1430 mailimap_set_free(set);
1437 struct fetch_content_param {
1440 const char * filename;
1444 struct fetch_content_result {
1448 static void fetch_content_run(struct etpan_thread_op * op)
1450 struct fetch_content_param * param;
1451 struct fetch_content_result * result;
1453 size_t content_size;
1462 if (param->with_body)
1463 r = imap_fetch(param->imap, param->msg_index,
1464 &content, &content_size);
1466 r = imap_fetch_header(param->imap, param->msg_index,
1467 &content, &content_size);
1469 result = op->result;
1472 if (r == MAILIMAP_NO_ERROR) {
1473 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1475 result->error = MAILIMAP_ERROR_FETCH;
1479 f = fdopen(fd, "wb");
1481 result->error = MAILIMAP_ERROR_FETCH;
1485 r = fwrite(content, 1, content_size, f);
1492 g_unlink(param->filename);
1503 g_unlink(param->filename);
1506 if (mmap_string_unref(content) != 0)
1510 debug_print("imap fetch_content run - end %i\n", r);
1513 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1515 const char * filename)
1517 struct fetch_content_param param;
1518 struct fetch_content_result result;
1521 debug_print("imap fetch_content - begin\n");
1523 imap = get_imap(folder);
1525 param.msg_index = msg_index;
1526 param.filename = filename;
1527 param.with_body = with_body;
1529 threaded_run(folder, ¶m, &result, fetch_content_run);
1531 if (result.error != MAILIMAP_NO_ERROR)
1532 return result.error;
1534 debug_print("imap fetch_content - end\n");
1536 return result.error;
1541 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1549 flag_list = att_dyn->att_list;
1550 if (flag_list == NULL)
1553 for(cur = clist_begin(flag_list) ; cur != NULL ;
1554 cur = clist_next(cur)) {
1555 struct mailimap_flag_fetch * flag_fetch;
1557 flag_fetch = clist_content(cur);
1558 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1561 switch (flag_fetch->fl_flag->fl_type) {
1562 case MAILIMAP_FLAG_ANSWERED:
1563 flags |= MSG_REPLIED;
1565 case MAILIMAP_FLAG_FLAGGED:
1566 flags |= MSG_MARKED;
1568 case MAILIMAP_FLAG_DELETED:
1569 flags |= MSG_DELETED;
1571 case MAILIMAP_FLAG_SEEN:
1572 flags &= ~MSG_UNREAD;
1582 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1586 struct mailimap_msg_att_dynamic ** patt_dyn)
1588 clistiter * item_cur;
1592 struct mailimap_msg_att_dynamic * att_dyn;
1599 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1600 item_cur = clist_next(item_cur)) {
1601 struct mailimap_msg_att_item * item;
1603 item = clist_content(item_cur);
1605 switch (item->att_type) {
1606 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1607 switch (item->att_data.att_static->att_type) {
1608 case MAILIMAP_MSG_ATT_UID:
1609 uid = item->att_data.att_static->att_data.att_uid;
1612 case MAILIMAP_MSG_ATT_BODY_SECTION:
1613 if (headers == NULL) {
1614 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1617 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1618 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1623 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1624 if (att_dyn == NULL) {
1625 att_dyn = item->att_data.att_dyn;
1633 if (pheaders != NULL)
1634 * pheaders = headers;
1635 if (pref_size != NULL)
1636 * pref_size = ref_size;
1637 if (patt_dyn != NULL)
1638 * patt_dyn = att_dyn;
1640 return MAIL_NO_ERROR;
1643 static struct imap_fetch_env_info *
1644 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1646 struct imap_fetch_env_info * info;
1650 struct mailimap_msg_att_dynamic * att_dyn;
1652 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1655 info = malloc(sizeof(* info));
1657 info->headers = strdup(headers);
1659 info->flags = imap_flags_to_flags(att_dyn);
1665 imap_fetch_result_to_envelop_list(clist * fetch_result,
1666 carray ** p_env_list)
1674 env_list = carray_new(16);
1676 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1677 cur = clist_next(cur)) {
1678 struct mailimap_msg_att * msg_att;
1679 struct imap_fetch_env_info * env_info;
1681 msg_att = clist_content(cur);
1683 env_info = fetch_to_env_info(msg_att);
1684 carray_add(env_list, env_info, NULL);
1687 * p_env_list = env_list;
1689 return MAIL_NO_ERROR;
1692 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1694 struct mailimap_fetch_att * fetch_att;
1698 struct mailimap_header_list * imap_hdrlist;
1699 struct mailimap_section * section;
1701 hdrlist = clist_new();
1703 header = strdup("Date");
1704 r = clist_append(hdrlist, header);
1705 header = strdup("From");
1706 r = clist_append(hdrlist, header);
1707 header = strdup("To");
1708 r = clist_append(hdrlist, header);
1709 header = strdup("Cc");
1710 r = clist_append(hdrlist, header);
1711 header = strdup("Subject");
1712 r = clist_append(hdrlist, header);
1713 header = strdup("Message-ID");
1714 r = clist_append(hdrlist, header);
1715 header = strdup("References");
1716 r = clist_append(hdrlist, header);
1717 header = strdup("In-Reply-To");
1718 r = clist_append(hdrlist, header);
1720 imap_hdrlist = mailimap_header_list_new(hdrlist);
1721 section = mailimap_section_new_header_fields(imap_hdrlist);
1722 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1723 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1725 return MAIL_NO_ERROR;
1728 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1730 struct mailimap_fetch_att * fetch_att;
1731 struct mailimap_section * section;
1733 section = mailimap_section_new_header();
1734 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1735 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1737 return MAIL_NO_ERROR;
1741 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1742 carray ** p_env_list)
1744 struct mailimap_fetch_att * fetch_att;
1745 struct mailimap_fetch_type * fetch_type;
1747 clist * fetch_result;
1753 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1756 fetch_att = mailimap_fetch_att_new_uid();
1757 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1760 fetch_att = mailimap_fetch_att_new_flags();
1761 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1764 fetch_att = mailimap_fetch_att_new_rfc822_size();
1765 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1769 key.len = sizeof(imap);
1770 r = chash_get(courier_workaround_hash, &key, &value);
1772 r = imap_add_envelope_fetch_att(fetch_type);
1774 r = imap_add_header_fetch_att(fetch_type);
1776 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1779 case MAILIMAP_NO_ERROR:
1782 mailimap_fetch_type_free(fetch_type);
1786 if (clist_begin(fetch_result) == NULL) {
1787 res = MAILIMAP_ERROR_FETCH;
1791 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1792 mailimap_fetch_list_free(fetch_result);
1794 if (r != MAILIMAP_NO_ERROR) {
1795 mailimap_fetch_type_free(fetch_type);
1796 res = MAILIMAP_ERROR_MEMORY;
1800 mailimap_fetch_type_free(fetch_type);
1802 * p_env_list = env_list;
1804 return MAILIMAP_NO_ERROR;
1810 struct fetch_env_param {
1812 struct mailimap_set * set;
1815 struct fetch_env_result {
1816 carray * fetch_env_result;
1820 static void fetch_env_run(struct etpan_thread_op * op)
1822 struct fetch_env_param * param;
1823 struct fetch_env_result * result;
1830 r = imap_get_envelopes_list(param->imap, param->set,
1833 result = op->result;
1835 result->fetch_env_result = env_list;
1837 debug_print("imap fetch_env run - end %i\n", r);
1840 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1841 carray ** p_env_list)
1843 struct fetch_env_param param;
1844 struct fetch_env_result result;
1847 debug_print("imap fetch_env - begin\n");
1849 imap = get_imap(folder);
1853 threaded_run(folder, ¶m, &result, fetch_env_run);
1855 if (result.error != MAILIMAP_NO_ERROR) {
1861 key.len = sizeof(imap);
1862 r = chash_get(courier_workaround_hash, &key, &value);
1866 chash_set(courier_workaround_hash, &key, &value, NULL);
1868 threaded_run(folder, ¶m, &result, fetch_env_run);
1872 if (result.error != MAILIMAP_NO_ERROR)
1873 return result.error;
1875 debug_print("imap fetch_env - end\n");
1877 * p_env_list = result.fetch_env_result;
1879 return result.error;
1882 void imap_fetch_env_free(carray * env_list)
1886 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1887 struct imap_fetch_env_info * env_info;
1889 env_info = carray_get(env_list, i);
1890 free(env_info->headers);
1893 carray_free(env_list);
1900 struct append_param {
1902 const char * mailbox;
1903 const char * filename;
1904 struct mailimap_flag_list * flag_list;
1907 struct append_result {
1911 static void append_run(struct etpan_thread_op * op)
1913 struct append_param * param;
1914 struct append_result * result;
1918 struct stat stat_buf;
1922 result = op->result;
1924 r = stat(param->filename, &stat_buf);
1926 result->error = MAILIMAP_ERROR_APPEND;
1929 size = stat_buf.st_size;
1931 fd = open(param->filename, O_RDONLY);
1933 result->error = MAILIMAP_ERROR_APPEND;
1937 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1938 if (data == (void *) MAP_FAILED) {
1940 result->error = MAILIMAP_ERROR_APPEND;
1944 r = mailimap_append(param->imap, param->mailbox,
1945 param->flag_list, NULL,
1953 debug_print("imap append run - end %i\n", r);
1956 int imap_threaded_append(Folder * folder, const char * mailbox,
1957 const char * filename,
1958 struct mailimap_flag_list * flag_list)
1960 struct append_param param;
1961 struct append_result result;
1964 debug_print("imap append - begin\n");
1966 imap = get_imap(folder);
1968 param.mailbox = mailbox;
1969 param.filename = filename;
1970 param.flag_list = flag_list;
1972 threaded_run(folder, ¶m, &result, append_run);
1974 if (result.error != MAILIMAP_NO_ERROR)
1975 return result.error;
1977 debug_print("imap append - end\n");
1979 return result.error;
1985 struct expunge_param {
1989 struct expunge_result {
1993 static void expunge_run(struct etpan_thread_op * op)
1995 struct expunge_param * param;
1996 struct expunge_result * result;
2000 r = mailimap_expunge(param->imap);
2002 result = op->result;
2004 debug_print("imap expunge run - end %i\n", r);
2007 int imap_threaded_expunge(Folder * folder)
2009 struct expunge_param param;
2010 struct expunge_result result;
2012 debug_print("imap expunge - begin\n");
2014 param.imap = get_imap(folder);
2016 threaded_run(folder, ¶m, &result, expunge_run);
2018 debug_print("imap expunge - end\n");
2020 return result.error;
2026 struct mailimap_set * set;
2030 struct copy_result {
2034 static void copy_run(struct etpan_thread_op * op)
2036 struct copy_param * param;
2037 struct copy_result * result;
2042 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2044 result = op->result;
2047 debug_print("imap copy run - end %i\n", r);
2050 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2053 struct copy_param param;
2054 struct copy_result result;
2057 debug_print("imap copy - begin\n");
2059 imap = get_imap(folder);
2064 threaded_run(folder, ¶m, &result, copy_run);
2066 if (result.error != MAILIMAP_NO_ERROR)
2067 return result.error;
2069 debug_print("imap copy - end\n");
2071 return result.error;
2076 struct store_param {
2078 struct mailimap_set * set;
2079 struct mailimap_store_att_flags * store_att_flags;
2082 struct store_result {
2086 static void store_run(struct etpan_thread_op * op)
2088 struct store_param * param;
2089 struct store_result * result;
2094 r = mailimap_uid_store(param->imap, param->set,
2095 param->store_att_flags);
2097 result = op->result;
2100 debug_print("imap store run - end %i\n", r);
2103 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2104 struct mailimap_store_att_flags * store_att_flags)
2106 struct store_param param;
2107 struct store_result result;
2110 debug_print("imap store - begin\n");
2112 imap = get_imap(folder);
2115 param.store_att_flags = store_att_flags;
2117 threaded_run(folder, ¶m, &result, store_run);
2119 if (result.error != MAILIMAP_NO_ERROR)
2120 return result.error;
2122 debug_print("imap store - end\n");
2124 return result.error;
2128 #define ENV_BUFFER_SIZE 512
2130 static void do_exec_command(int fd, const char * command,
2131 const char * servername, uint16_t port)
2135 char env_buffer[ENV_BUFFER_SIZE];
2139 /* Fork again to become a child of init rather than
2140 the etpan client. */
2146 snprintf(env_buffer, ENV_BUFFER_SIZE,
2147 "ETPANSERVER=%s", servername);
2149 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2153 setenv("ETPANSERVER", servername, 1);
2155 unsetenv("ETPANSERVER");
2160 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2162 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2168 snprintf(porttext, sizeof(porttext), "%d", port);
2169 setenv("ETPANPORT", porttext, 1);
2172 unsetenv("ETPANPORT");
2176 /* Not a lot we can do if there's an error other than bail. */
2177 if (dup2(fd, 0) == -1)
2179 if (dup2(fd, 1) == -1)
2182 /* Should we close stderr and reopen /dev/null? */
2184 maxopen = sysconf(_SC_OPEN_MAX);
2185 for (i=3; i < maxopen; i++)
2189 /* Detach from the controlling tty if we have one. Otherwise,
2190 SSH might do something stupid like trying to use it instead
2191 of running $SSH_ASKPASS. Doh. */
2192 fd = open("/dev/tty", O_RDONLY);
2194 ioctl(fd, TIOCNOTTY, NULL);
2197 #endif /* TIOCNOTTY */
2199 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2201 /* Eep. Shouldn't reach this */
2205 static int subcommand_connect(const char *command,
2206 const char *servername, uint16_t port)
2208 /* SEB unsupported on Windows */
2212 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2217 do_exec_command(sockfds[1], command, servername, port);
2219 else if (childpid == -1) {
2227 /* Reap child, leaving grandchild process to run */
2228 waitpid(childpid, NULL, 0);
2233 int socket_connect_cmd(mailimap * imap, const char * command,
2234 const char * server, int port)
2240 fd = subcommand_connect(command, server, port);
2242 return MAILIMAP_ERROR_STREAM;
2244 s = mailstream_socket_open(fd);
2247 return MAILIMAP_ERROR_STREAM;
2250 r = mailimap_connect(imap, s);
2251 if (r != MAILIMAP_NO_ERROR) {
2252 mailstream_close(s);
2256 return MAILIMAP_NO_ERROR;
2261 struct connect_cmd_param {
2263 const char * command;
2264 const char * server;
2268 struct connect_cmd_result {
2272 static void connect_cmd_run(struct etpan_thread_op * op)
2275 struct connect_cmd_param * param;
2276 struct connect_cmd_result * result;
2279 result = op->result;
2281 r = socket_connect_cmd(param->imap, param->command,
2282 param->server, param->port);
2288 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2289 const char * server, int port)
2291 struct connect_cmd_param param;
2292 struct connect_cmd_result result;
2297 imap = mailimap_new(0, NULL);
2300 key.len = sizeof(folder);
2303 chash_set(session_hash, &key, &value, NULL);
2306 param.command = command;
2307 param.server = server;
2310 threaded_run(folder, ¶m, &result, connect_cmd_run);
2312 debug_print("connect_cmd ok %i\n", result.error);
2314 return result.error;
2318 void imap_main_init(void)
2321 void imap_main_done(void)
2324 void imap_main_set_timeout(int sec)