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;
444 struct login_result {
448 static void login_run(struct etpan_thread_op * op)
450 struct login_param * param;
451 struct login_result * result;
453 #ifdef DISABLE_LOG_DURING_LOGIN
459 #ifdef DISABLE_LOG_DURING_LOGIN
460 old_debug = mailstream_debug;
461 mailstream_debug = 0;
463 if (!strcmp(param->type, "LOGIN"))
464 r = mailimap_login(param->imap,
465 param->login, param->password);
467 r = mailimap_authenticate(param->imap,
468 param->type, NULL, NULL, NULL,
469 param->login, param->login,
470 param->password, NULL);
471 #ifdef DISABLE_LOG_DURING_LOGIN
472 mailstream_debug = old_debug;
477 debug_print("imap login run - end %i\n", r);
480 int imap_threaded_login(Folder * folder,
481 const char * login, const char * password,
484 struct login_param param;
485 struct login_result result;
487 debug_print("imap login - begin\n");
489 param.imap = get_imap(folder);
491 param.password = password;
494 threaded_run(folder, ¶m, &result, login_run);
496 debug_print("imap login - end\n");
502 struct status_param {
505 struct mailimap_status_att_list * status_att_list;
508 struct status_result {
510 struct mailimap_mailbox_data_status * data_status;
513 static void status_run(struct etpan_thread_op * op)
515 struct status_param * param;
516 struct status_result * result;
522 r = mailimap_status(param->imap, param->mb,
523 param->status_att_list,
524 &result->data_status);
527 debug_print("imap status run - end %i\n", r);
530 int imap_threaded_status(Folder * folder, const char * mb,
531 struct mailimap_mailbox_data_status ** data_status)
533 struct status_param param;
534 struct status_result result;
535 struct mailimap_status_att_list * status_att_list;
537 debug_print("imap status - begin\n");
539 status_att_list = mailimap_status_att_list_new_empty();
540 mailimap_status_att_list_add(status_att_list,
541 MAILIMAP_STATUS_ATT_MESSAGES);
542 mailimap_status_att_list_add(status_att_list,
543 MAILIMAP_STATUS_ATT_RECENT);
544 mailimap_status_att_list_add(status_att_list,
545 MAILIMAP_STATUS_ATT_UIDNEXT);
546 mailimap_status_att_list_add(status_att_list,
547 MAILIMAP_STATUS_ATT_UIDVALIDITY);
548 mailimap_status_att_list_add(status_att_list,
549 MAILIMAP_STATUS_ATT_UNSEEN);
551 param.imap = get_imap(folder);
553 param.status_att_list = status_att_list;
555 threaded_run(folder, ¶m, &result, status_run);
557 debug_print("imap status - end\n");
559 * data_status = result.data_status;
574 static void noop_run(struct etpan_thread_op * op)
576 struct noop_param * param;
577 struct noop_result * result;
581 r = mailimap_noop(param->imap);
585 debug_print("imap noop run - end %i\n", r);
588 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
590 struct noop_param param;
591 struct noop_result result;
594 debug_print("imap noop - begin\n");
596 imap = get_imap(folder);
599 threaded_run(folder, ¶m, &result, noop_run);
601 if (imap->imap_selection_info != NULL) {
602 * p_exists = imap->imap_selection_info->sel_exists;
608 debug_print("imap noop - end\n");
614 struct starttls_param {
618 struct starttls_result {
622 static void starttls_run(struct etpan_thread_op * op)
624 struct starttls_param * param;
625 struct starttls_result * result;
629 r = mailimap_starttls(param->imap);
633 debug_print("imap starttls run - end %i\n", r);
636 mailimap *imap = param->imap;
637 mailstream_low *plain_low = NULL;
638 mailstream_low *tls_low = NULL;
641 plain_low = mailstream_get_low(imap->imap_stream);
642 fd = mailstream_low_get_fd(plain_low);
644 debug_print("imap starttls run - can't get fd\n");
645 result->error = MAILIMAP_ERROR_STREAM;
648 tls_low = mailstream_low_ssl_open(fd);
649 if (tls_low == NULL) {
650 debug_print("imap starttls run - can't ssl_open\n");
651 result->error = MAILIMAP_ERROR_STREAM;
654 mailstream_low_free(plain_low);
655 mailstream_set_low(imap->imap_stream, tls_low);
659 int imap_threaded_starttls(Folder * folder)
661 struct starttls_param param;
662 struct starttls_result result;
664 debug_print("imap starttls - begin\n");
666 param.imap = get_imap(folder);
668 threaded_run(folder, ¶m, &result, starttls_run);
670 debug_print("imap starttls - end\n");
677 struct create_param {
682 struct create_result {
686 static void create_run(struct etpan_thread_op * op)
688 struct create_param * param;
689 struct create_result * result;
693 r = mailimap_create(param->imap, param->mb);
697 debug_print("imap create run - end %i\n", r);
700 int imap_threaded_create(Folder * folder, const char * mb)
702 struct create_param param;
703 struct create_result result;
705 debug_print("imap create - begin\n");
707 param.imap = get_imap(folder);
710 threaded_run(folder, ¶m, &result, create_run);
712 debug_print("imap create - end\n");
720 struct rename_param {
723 const char * new_name;
726 struct rename_result {
730 static void rename_run(struct etpan_thread_op * op)
732 struct rename_param * param;
733 struct rename_result * result;
737 r = mailimap_rename(param->imap, param->mb, param->new_name);
741 debug_print("imap rename run - end %i\n", r);
744 int imap_threaded_rename(Folder * folder,
745 const char * mb, const char * new_name)
747 struct rename_param param;
748 struct rename_result result;
750 debug_print("imap rename - begin\n");
752 param.imap = get_imap(folder);
754 param.new_name = new_name;
756 threaded_run(folder, ¶m, &result, rename_run);
758 debug_print("imap rename - end\n");
766 struct delete_param {
771 struct delete_result {
775 static void delete_run(struct etpan_thread_op * op)
777 struct delete_param * param;
778 struct delete_result * result;
782 r = mailimap_delete(param->imap, param->mb);
786 debug_print("imap delete run - end %i\n", r);
789 int imap_threaded_delete(Folder * folder, const char * mb)
791 struct delete_param param;
792 struct delete_result result;
794 debug_print("imap delete - begin\n");
796 param.imap = get_imap(folder);
799 threaded_run(folder, ¶m, &result, delete_run);
801 debug_print("imap delete - end\n");
808 struct select_param {
813 struct select_result {
817 static void select_run(struct etpan_thread_op * op)
819 struct select_param * param;
820 struct select_result * result;
824 r = mailimap_select(param->imap, param->mb);
828 debug_print("imap select run - end %i\n", r);
831 int imap_threaded_select(Folder * folder, const char * mb,
832 gint * exists, gint * recent, gint * unseen,
833 guint32 * uid_validity)
835 struct select_param param;
836 struct select_result result;
839 debug_print("imap select - begin\n");
841 imap = get_imap(folder);
845 threaded_run(folder, ¶m, &result, select_run);
847 if (result.error != MAILIMAP_NO_ERROR)
850 if (imap->imap_selection_info == NULL)
851 return MAILIMAP_ERROR_PARSE;
853 * exists = imap->imap_selection_info->sel_exists;
854 * recent = imap->imap_selection_info->sel_recent;
855 * unseen = imap->imap_selection_info->sel_unseen;
856 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
858 debug_print("imap select - end\n");
865 struct examine_param {
870 struct examine_result {
874 static void examine_run(struct etpan_thread_op * op)
876 struct examine_param * param;
877 struct examine_result * result;
881 r = mailimap_examine(param->imap, param->mb);
885 debug_print("imap examine run - end %i\n", r);
888 int imap_threaded_examine(Folder * folder, const char * mb,
889 gint * exists, gint * recent, gint * unseen,
890 guint32 * uid_validity)
892 struct examine_param param;
893 struct examine_result result;
896 debug_print("imap examine - begin\n");
898 imap = get_imap(folder);
902 threaded_run(folder, ¶m, &result, examine_run);
904 if (result.error != MAILIMAP_NO_ERROR)
907 if (imap->imap_selection_info == NULL)
908 return MAILIMAP_ERROR_PARSE;
910 * exists = imap->imap_selection_info->sel_exists;
911 * recent = imap->imap_selection_info->sel_recent;
912 * unseen = imap->imap_selection_info->sel_unseen;
913 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
915 debug_print("imap examine - end\n");
923 struct search_param {
926 struct mailimap_set * set;
929 struct search_result {
931 clist * search_result;
934 static void search_run(struct etpan_thread_op * op)
936 struct search_param * param;
937 struct search_result * result;
939 struct mailimap_search_key * key;
940 struct mailimap_search_key * uid_key;
941 struct mailimap_search_key * search_type_key;
942 clist * search_result;
946 uid_key = mailimap_search_key_new_uid(param->set);
948 search_type_key = NULL;
949 switch (param->type) {
950 case IMAP_SEARCH_TYPE_SIMPLE:
951 search_type_key = NULL;
954 case IMAP_SEARCH_TYPE_SEEN:
955 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
956 NULL, NULL, NULL, NULL, NULL,
957 NULL, NULL, NULL, NULL, NULL,
958 NULL, NULL, NULL, NULL, 0,
959 NULL, NULL, NULL, NULL, NULL,
960 NULL, 0, NULL, NULL, NULL);
963 case IMAP_SEARCH_TYPE_UNSEEN:
964 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
965 NULL, NULL, NULL, NULL, NULL,
966 NULL, NULL, NULL, NULL, NULL,
967 NULL, NULL, NULL, NULL, 0,
968 NULL, NULL, NULL, NULL, NULL,
969 NULL, 0, NULL, NULL, NULL);
972 case IMAP_SEARCH_TYPE_ANSWERED:
973 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
974 NULL, NULL, NULL, NULL, NULL,
975 NULL, NULL, NULL, NULL, NULL,
976 NULL, NULL, NULL, NULL, 0,
977 NULL, NULL, NULL, NULL, NULL,
978 NULL, 0, NULL, NULL, NULL);
981 case IMAP_SEARCH_TYPE_FLAGGED:
982 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
983 NULL, NULL, NULL, NULL, NULL,
984 NULL, NULL, NULL, NULL, NULL,
985 NULL, NULL, NULL, NULL, 0,
986 NULL, NULL, NULL, NULL, NULL,
987 NULL, 0, NULL, NULL, NULL);
991 if (search_type_key != NULL) {
992 key = mailimap_search_key_new_multiple_empty();
993 mailimap_search_key_multiple_add(key, search_type_key);
994 mailimap_search_key_multiple_add(key, uid_key);
1000 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1002 result = op->result;
1004 result->search_result = search_result;
1005 debug_print("imap search run - end %i\n", r);
1008 int imap_threaded_search(Folder * folder, int search_type,
1009 struct mailimap_set * set, clist ** search_result)
1011 struct search_param param;
1012 struct search_result result;
1015 debug_print("imap search - begin\n");
1017 imap = get_imap(folder);
1020 param.type = search_type;
1022 threaded_run(folder, ¶m, &result, search_run);
1024 if (result.error != MAILIMAP_NO_ERROR)
1025 return result.error;
1027 debug_print("imap search - end\n");
1029 * search_result = result.search_result;
1031 return result.error;
1038 uid_list_to_env_list(clist * fetch_result, carray ** result)
1046 tab = carray_new(128);
1048 res = MAILIMAP_ERROR_MEMORY;
1052 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1053 cur = clist_next(cur)) {
1054 struct mailimap_msg_att * msg_att;
1055 clistiter * item_cur;
1060 msg_att = clist_content(cur);
1064 for(item_cur = clist_begin(msg_att->att_list) ;
1066 item_cur = clist_next(item_cur)) {
1067 struct mailimap_msg_att_item * item;
1069 item = clist_content(item_cur);
1071 switch (item->att_type) {
1072 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1073 switch (item->att_data.att_static->att_type) {
1074 case MAILIMAP_MSG_ATT_UID:
1075 uid = item->att_data.att_static->att_data.att_uid;
1081 puid = malloc(sizeof(* puid));
1083 res = MAILIMAP_ERROR_MEMORY;
1088 r = carray_add(tab, puid, NULL);
1091 res = MAILIMAP_ERROR_MEMORY;
1098 return MAILIMAP_NO_ERROR;
1101 for(i = 0 ; i < carray_count(tab) ; i++)
1102 mailmessage_free(carray_get(tab, i));
1107 static int imap_get_messages_list(mailimap * imap,
1108 uint32_t first_index,
1113 struct mailimap_fetch_att * fetch_att;
1114 struct mailimap_fetch_type * fetch_type;
1115 struct mailimap_set * set;
1116 clist * fetch_result;
1119 set = mailimap_set_new_interval(first_index, 0);
1121 res = MAILIMAP_ERROR_MEMORY;
1125 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1126 if (fetch_type == NULL) {
1127 res = MAILIMAP_ERROR_MEMORY;
1131 fetch_att = mailimap_fetch_att_new_uid();
1132 if (fetch_att == NULL) {
1133 res = MAILIMAP_ERROR_MEMORY;
1134 goto free_fetch_type;
1137 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1138 if (r != MAILIMAP_NO_ERROR) {
1139 mailimap_fetch_att_free(fetch_att);
1140 res = MAILIMAP_ERROR_MEMORY;
1141 goto free_fetch_type;
1144 r = mailimap_uid_fetch(imap, set,
1145 fetch_type, &fetch_result);
1147 mailimap_fetch_type_free(fetch_type);
1148 mailimap_set_free(set);
1150 if (r != MAILIMAP_NO_ERROR) {
1156 r = uid_list_to_env_list(fetch_result, &env_list);
1157 mailimap_fetch_list_free(fetch_result);
1159 * result = env_list;
1161 return MAILIMAP_NO_ERROR;
1164 mailimap_fetch_type_free(fetch_type);
1166 mailimap_set_free(set);
1174 struct fetch_uid_param {
1176 uint32_t first_index;
1179 struct fetch_uid_result {
1181 carray * fetch_result;
1184 static void fetch_uid_run(struct etpan_thread_op * op)
1186 struct fetch_uid_param * param;
1187 struct fetch_uid_result * result;
1188 carray * fetch_result;
1193 fetch_result = NULL;
1194 r = imap_get_messages_list(param->imap, param->first_index,
1197 result = op->result;
1199 result->fetch_result = fetch_result;
1200 debug_print("imap fetch_uid run - end %i\n", r);
1203 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1204 carray ** fetch_result)
1206 struct fetch_uid_param param;
1207 struct fetch_uid_result result;
1210 debug_print("imap fetch_uid - begin\n");
1212 imap = get_imap(folder);
1214 param.first_index = first_index;
1216 threaded_run(folder, ¶m, &result, fetch_uid_run);
1218 if (result.error != MAILIMAP_NO_ERROR)
1219 return result.error;
1221 debug_print("imap fetch_uid - end\n");
1223 * fetch_result = result.fetch_result;
1225 return result.error;
1229 void imap_fetch_uid_list_free(carray * uid_list)
1233 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1236 puid = carray_get(uid_list, i);
1239 carray_free(uid_list);
1244 static int imap_fetch(mailimap * imap,
1247 size_t * result_len)
1250 struct mailimap_set * set;
1251 struct mailimap_fetch_att * fetch_att;
1252 struct mailimap_fetch_type * fetch_type;
1253 clist * fetch_result;
1254 struct mailimap_msg_att * msg_att;
1255 struct mailimap_msg_att_item * msg_att_item;
1260 struct mailimap_section * section;
1262 set = mailimap_set_new_single(msg_index);
1264 res = MAILIMAP_ERROR_MEMORY;
1268 section = mailimap_section_new(NULL);
1269 if (section == NULL) {
1270 res = MAILIMAP_ERROR_MEMORY;
1274 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1275 if (fetch_att == NULL) {
1276 mailimap_section_free(section);
1277 res = MAILIMAP_ERROR_MEMORY;
1281 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1282 if (fetch_type == NULL) {
1283 res = MAILIMAP_ERROR_MEMORY;
1284 goto free_fetch_att;
1287 r = mailimap_uid_fetch(imap, set,
1288 fetch_type, &fetch_result);
1290 mailimap_fetch_type_free(fetch_type);
1291 mailimap_set_free(set);
1294 case MAILIMAP_NO_ERROR:
1300 if (clist_begin(fetch_result) == NULL) {
1301 mailimap_fetch_list_free(fetch_result);
1302 return MAILIMAP_ERROR_FETCH;
1305 msg_att = clist_begin(fetch_result)->data;
1310 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1311 cur = clist_next(cur)) {
1312 msg_att_item = clist_content(cur);
1314 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1315 if (msg_att_item->att_data.att_static->att_type ==
1316 MAILIMAP_MSG_ATT_BODY_SECTION) {
1317 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1319 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1321 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1326 mailimap_fetch_list_free(fetch_result);
1329 return MAILIMAP_ERROR_FETCH;
1332 * result_len = text_length;
1334 return MAILIMAP_NO_ERROR;
1337 mailimap_fetch_att_free(fetch_att);
1339 mailimap_set_free(set);
1344 static int imap_fetch_header(mailimap * imap,
1347 size_t * result_len)
1350 struct mailimap_set * set;
1351 struct mailimap_fetch_att * fetch_att;
1352 struct mailimap_fetch_type * fetch_type;
1353 clist * fetch_result;
1354 struct mailimap_msg_att * msg_att;
1355 struct mailimap_msg_att_item * msg_att_item;
1360 struct mailimap_section * section;
1362 set = mailimap_set_new_single(msg_index);
1364 res = MAILIMAP_ERROR_MEMORY;
1368 section = mailimap_section_new_header();
1369 if (section == NULL) {
1370 res = MAILIMAP_ERROR_MEMORY;
1374 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1375 if (fetch_att == NULL) {
1376 mailimap_section_free(section);
1377 res = MAILIMAP_ERROR_MEMORY;
1381 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1382 if (fetch_type == NULL) {
1383 res = MAILIMAP_ERROR_MEMORY;
1384 goto free_fetch_att;
1387 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1389 mailimap_fetch_type_free(fetch_type);
1390 mailimap_set_free(set);
1393 case MAILIMAP_NO_ERROR:
1399 if (clist_begin(fetch_result) == NULL) {
1400 mailimap_fetch_list_free(fetch_result);
1401 return MAILIMAP_ERROR_FETCH;
1404 msg_att = clist_begin(fetch_result)->data;
1409 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1410 cur = clist_next(cur)) {
1411 msg_att_item = clist_content(cur);
1413 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1414 if (msg_att_item->att_data.att_static->att_type ==
1415 MAILIMAP_MSG_ATT_BODY_SECTION) {
1416 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1417 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1419 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1424 mailimap_fetch_list_free(fetch_result);
1427 return MAILIMAP_ERROR_FETCH;
1430 * result_len = text_length;
1432 return MAILIMAP_NO_ERROR;
1435 mailimap_fetch_att_free(fetch_att);
1437 mailimap_set_free(set);
1444 struct fetch_content_param {
1447 const char * filename;
1451 struct fetch_content_result {
1455 static void fetch_content_run(struct etpan_thread_op * op)
1457 struct fetch_content_param * param;
1458 struct fetch_content_result * result;
1460 size_t content_size;
1469 if (param->with_body)
1470 r = imap_fetch(param->imap, param->msg_index,
1471 &content, &content_size);
1473 r = imap_fetch_header(param->imap, param->msg_index,
1474 &content, &content_size);
1476 result = op->result;
1479 if (r == MAILIMAP_NO_ERROR) {
1480 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1482 result->error = MAILIMAP_ERROR_FETCH;
1486 f = fdopen(fd, "wb");
1488 result->error = MAILIMAP_ERROR_FETCH;
1492 r = fwrite(content, 1, content_size, f);
1499 g_unlink(param->filename);
1510 g_unlink(param->filename);
1513 if (mmap_string_unref(content) != 0)
1517 debug_print("imap fetch_content run - end %i\n", r);
1520 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
1522 const char * filename)
1524 struct fetch_content_param param;
1525 struct fetch_content_result result;
1528 debug_print("imap fetch_content - begin\n");
1530 imap = get_imap(folder);
1532 param.msg_index = msg_index;
1533 param.filename = filename;
1534 param.with_body = with_body;
1536 threaded_run(folder, ¶m, &result, fetch_content_run);
1538 if (result.error != MAILIMAP_NO_ERROR)
1539 return result.error;
1541 debug_print("imap fetch_content - end\n");
1543 return result.error;
1548 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
1556 flag_list = att_dyn->att_list;
1557 if (flag_list == NULL)
1560 for(cur = clist_begin(flag_list) ; cur != NULL ;
1561 cur = clist_next(cur)) {
1562 struct mailimap_flag_fetch * flag_fetch;
1564 flag_fetch = clist_content(cur);
1565 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
1568 switch (flag_fetch->fl_flag->fl_type) {
1569 case MAILIMAP_FLAG_ANSWERED:
1570 flags |= MSG_REPLIED;
1572 case MAILIMAP_FLAG_FLAGGED:
1573 flags |= MSG_MARKED;
1575 case MAILIMAP_FLAG_DELETED:
1576 flags |= MSG_DELETED;
1578 case MAILIMAP_FLAG_SEEN:
1579 flags &= ~MSG_UNREAD;
1589 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1593 struct mailimap_msg_att_dynamic ** patt_dyn)
1595 clistiter * item_cur;
1599 struct mailimap_msg_att_dynamic * att_dyn;
1606 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
1607 item_cur = clist_next(item_cur)) {
1608 struct mailimap_msg_att_item * item;
1610 item = clist_content(item_cur);
1612 switch (item->att_type) {
1613 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1614 switch (item->att_data.att_static->att_type) {
1615 case MAILIMAP_MSG_ATT_UID:
1616 uid = item->att_data.att_static->att_data.att_uid;
1619 case MAILIMAP_MSG_ATT_BODY_SECTION:
1620 if (headers == NULL) {
1621 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
1624 case MAILIMAP_MSG_ATT_RFC822_SIZE:
1625 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
1630 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
1631 if (att_dyn == NULL) {
1632 att_dyn = item->att_data.att_dyn;
1640 if (pheaders != NULL)
1641 * pheaders = headers;
1642 if (pref_size != NULL)
1643 * pref_size = ref_size;
1644 if (patt_dyn != NULL)
1645 * patt_dyn = att_dyn;
1647 return MAIL_NO_ERROR;
1650 static struct imap_fetch_env_info *
1651 fetch_to_env_info(struct mailimap_msg_att * msg_att)
1653 struct imap_fetch_env_info * info;
1657 struct mailimap_msg_att_dynamic * att_dyn;
1659 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
1662 info = malloc(sizeof(* info));
1664 info->headers = strdup(headers);
1666 info->flags = imap_flags_to_flags(att_dyn);
1672 imap_fetch_result_to_envelop_list(clist * fetch_result,
1673 carray ** p_env_list)
1681 env_list = carray_new(16);
1683 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1684 cur = clist_next(cur)) {
1685 struct mailimap_msg_att * msg_att;
1686 struct imap_fetch_env_info * env_info;
1688 msg_att = clist_content(cur);
1690 env_info = fetch_to_env_info(msg_att);
1691 carray_add(env_list, env_info, NULL);
1694 * p_env_list = env_list;
1696 return MAIL_NO_ERROR;
1699 int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
1701 struct mailimap_fetch_att * fetch_att;
1705 struct mailimap_header_list * imap_hdrlist;
1706 struct mailimap_section * section;
1708 hdrlist = clist_new();
1710 header = strdup("Date");
1711 r = clist_append(hdrlist, header);
1712 header = strdup("From");
1713 r = clist_append(hdrlist, header);
1714 header = strdup("To");
1715 r = clist_append(hdrlist, header);
1716 header = strdup("Cc");
1717 r = clist_append(hdrlist, header);
1718 header = strdup("Subject");
1719 r = clist_append(hdrlist, header);
1720 header = strdup("Message-ID");
1721 r = clist_append(hdrlist, header);
1722 header = strdup("References");
1723 r = clist_append(hdrlist, header);
1724 header = strdup("In-Reply-To");
1725 r = clist_append(hdrlist, header);
1727 imap_hdrlist = mailimap_header_list_new(hdrlist);
1728 section = mailimap_section_new_header_fields(imap_hdrlist);
1729 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1730 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1732 return MAIL_NO_ERROR;
1735 int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
1737 struct mailimap_fetch_att * fetch_att;
1738 struct mailimap_section * section;
1740 section = mailimap_section_new_header();
1741 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1742 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1744 return MAIL_NO_ERROR;
1748 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
1749 carray ** p_env_list)
1751 struct mailimap_fetch_att * fetch_att;
1752 struct mailimap_fetch_type * fetch_type;
1754 clist * fetch_result;
1760 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1763 fetch_att = mailimap_fetch_att_new_uid();
1764 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1767 fetch_att = mailimap_fetch_att_new_flags();
1768 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1771 fetch_att = mailimap_fetch_att_new_rfc822_size();
1772 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1776 key.len = sizeof(imap);
1777 r = chash_get(courier_workaround_hash, &key, &value);
1779 r = imap_add_envelope_fetch_att(fetch_type);
1781 r = imap_add_header_fetch_att(fetch_type);
1783 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1786 case MAILIMAP_NO_ERROR:
1789 mailimap_fetch_type_free(fetch_type);
1793 if (clist_begin(fetch_result) == NULL) {
1794 res = MAILIMAP_ERROR_FETCH;
1798 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
1799 mailimap_fetch_list_free(fetch_result);
1801 if (r != MAILIMAP_NO_ERROR) {
1802 mailimap_fetch_type_free(fetch_type);
1803 res = MAILIMAP_ERROR_MEMORY;
1807 mailimap_fetch_type_free(fetch_type);
1809 * p_env_list = env_list;
1811 return MAILIMAP_NO_ERROR;
1817 struct fetch_env_param {
1819 struct mailimap_set * set;
1822 struct fetch_env_result {
1823 carray * fetch_env_result;
1827 static void fetch_env_run(struct etpan_thread_op * op)
1829 struct fetch_env_param * param;
1830 struct fetch_env_result * result;
1837 r = imap_get_envelopes_list(param->imap, param->set,
1840 result = op->result;
1842 result->fetch_env_result = env_list;
1844 debug_print("imap fetch_env run - end %i\n", r);
1847 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
1848 carray ** p_env_list)
1850 struct fetch_env_param param;
1851 struct fetch_env_result result;
1854 debug_print("imap fetch_env - begin\n");
1856 imap = get_imap(folder);
1860 threaded_run(folder, ¶m, &result, fetch_env_run);
1862 if (result.error != MAILIMAP_NO_ERROR) {
1868 key.len = sizeof(imap);
1869 r = chash_get(courier_workaround_hash, &key, &value);
1873 chash_set(courier_workaround_hash, &key, &value, NULL);
1875 threaded_run(folder, ¶m, &result, fetch_env_run);
1879 if (result.error != MAILIMAP_NO_ERROR)
1880 return result.error;
1882 debug_print("imap fetch_env - end\n");
1884 * p_env_list = result.fetch_env_result;
1886 return result.error;
1889 void imap_fetch_env_free(carray * env_list)
1893 for(i = 0 ; i < carray_count(env_list) ; i ++) {
1894 struct imap_fetch_env_info * env_info;
1896 env_info = carray_get(env_list, i);
1897 free(env_info->headers);
1900 carray_free(env_list);
1907 struct append_param {
1909 const char * mailbox;
1910 const char * filename;
1911 struct mailimap_flag_list * flag_list;
1914 struct append_result {
1918 static void append_run(struct etpan_thread_op * op)
1920 struct append_param * param;
1921 struct append_result * result;
1925 struct stat stat_buf;
1929 result = op->result;
1931 r = stat(param->filename, &stat_buf);
1933 result->error = MAILIMAP_ERROR_APPEND;
1936 size = stat_buf.st_size;
1938 fd = open(param->filename, O_RDONLY);
1940 result->error = MAILIMAP_ERROR_APPEND;
1944 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
1945 if (data == (void *) MAP_FAILED) {
1947 result->error = MAILIMAP_ERROR_APPEND;
1951 r = mailimap_append(param->imap, param->mailbox,
1952 param->flag_list, NULL,
1960 debug_print("imap append run - end %i\n", r);
1963 int imap_threaded_append(Folder * folder, const char * mailbox,
1964 const char * filename,
1965 struct mailimap_flag_list * flag_list)
1967 struct append_param param;
1968 struct append_result result;
1971 debug_print("imap append - begin\n");
1973 imap = get_imap(folder);
1975 param.mailbox = mailbox;
1976 param.filename = filename;
1977 param.flag_list = flag_list;
1979 threaded_run(folder, ¶m, &result, append_run);
1981 if (result.error != MAILIMAP_NO_ERROR)
1982 return result.error;
1984 debug_print("imap append - end\n");
1986 return result.error;
1992 struct expunge_param {
1996 struct expunge_result {
2000 static void expunge_run(struct etpan_thread_op * op)
2002 struct expunge_param * param;
2003 struct expunge_result * result;
2007 r = mailimap_expunge(param->imap);
2009 result = op->result;
2011 debug_print("imap expunge run - end %i\n", r);
2014 int imap_threaded_expunge(Folder * folder)
2016 struct expunge_param param;
2017 struct expunge_result result;
2019 debug_print("imap expunge - begin\n");
2021 param.imap = get_imap(folder);
2023 threaded_run(folder, ¶m, &result, expunge_run);
2025 debug_print("imap expunge - end\n");
2027 return result.error;
2033 struct mailimap_set * set;
2037 struct copy_result {
2041 static void copy_run(struct etpan_thread_op * op)
2043 struct copy_param * param;
2044 struct copy_result * result;
2049 r = mailimap_uid_copy(param->imap, param->set, param->mb);
2051 result = op->result;
2054 debug_print("imap copy run - end %i\n", r);
2057 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2060 struct copy_param param;
2061 struct copy_result result;
2064 debug_print("imap copy - begin\n");
2066 imap = get_imap(folder);
2071 threaded_run(folder, ¶m, &result, copy_run);
2073 if (result.error != MAILIMAP_NO_ERROR)
2074 return result.error;
2076 debug_print("imap copy - end\n");
2078 return result.error;
2083 struct store_param {
2085 struct mailimap_set * set;
2086 struct mailimap_store_att_flags * store_att_flags;
2089 struct store_result {
2093 static void store_run(struct etpan_thread_op * op)
2095 struct store_param * param;
2096 struct store_result * result;
2101 r = mailimap_uid_store(param->imap, param->set,
2102 param->store_att_flags);
2104 result = op->result;
2107 debug_print("imap store run - end %i\n", r);
2110 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2111 struct mailimap_store_att_flags * store_att_flags)
2113 struct store_param param;
2114 struct store_result result;
2117 debug_print("imap store - begin\n");
2119 imap = get_imap(folder);
2122 param.store_att_flags = store_att_flags;
2124 threaded_run(folder, ¶m, &result, store_run);
2126 if (result.error != MAILIMAP_NO_ERROR)
2127 return result.error;
2129 debug_print("imap store - end\n");
2131 return result.error;
2135 #define ENV_BUFFER_SIZE 512
2137 static void do_exec_command(int fd, const char * command,
2138 const char * servername, uint16_t port)
2142 char env_buffer[ENV_BUFFER_SIZE];
2146 /* Fork again to become a child of init rather than
2147 the etpan client. */
2153 snprintf(env_buffer, ENV_BUFFER_SIZE,
2154 "ETPANSERVER=%s", servername);
2156 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2160 setenv("ETPANSERVER", servername, 1);
2162 unsetenv("ETPANSERVER");
2167 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2169 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2175 snprintf(porttext, sizeof(porttext), "%d", port);
2176 setenv("ETPANPORT", porttext, 1);
2179 unsetenv("ETPANPORT");
2183 /* Not a lot we can do if there's an error other than bail. */
2184 if (dup2(fd, 0) == -1)
2186 if (dup2(fd, 1) == -1)
2189 /* Should we close stderr and reopen /dev/null? */
2191 maxopen = sysconf(_SC_OPEN_MAX);
2192 for (i=3; i < maxopen; i++)
2196 /* Detach from the controlling tty if we have one. Otherwise,
2197 SSH might do something stupid like trying to use it instead
2198 of running $SSH_ASKPASS. Doh. */
2199 fd = open("/dev/tty", O_RDONLY);
2201 ioctl(fd, TIOCNOTTY, NULL);
2204 #endif /* TIOCNOTTY */
2206 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2208 /* Eep. Shouldn't reach this */
2212 static int subcommand_connect(const char *command,
2213 const char *servername, uint16_t port)
2215 /* SEB unsupported on Windows */
2219 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2224 do_exec_command(sockfds[1], command, servername, port);
2226 else if (childpid == -1) {
2234 /* Reap child, leaving grandchild process to run */
2235 waitpid(childpid, NULL, 0);
2240 int socket_connect_cmd(mailimap * imap, const char * command,
2241 const char * server, int port)
2247 fd = subcommand_connect(command, server, port);
2249 return MAILIMAP_ERROR_STREAM;
2251 s = mailstream_socket_open(fd);
2254 return MAILIMAP_ERROR_STREAM;
2257 r = mailimap_connect(imap, s);
2258 if (r != MAILIMAP_NO_ERROR) {
2259 mailstream_close(s);
2263 return MAILIMAP_NO_ERROR;
2268 struct connect_cmd_param {
2270 const char * command;
2271 const char * server;
2275 struct connect_cmd_result {
2279 static void connect_cmd_run(struct etpan_thread_op * op)
2282 struct connect_cmd_param * param;
2283 struct connect_cmd_result * result;
2286 result = op->result;
2288 r = socket_connect_cmd(param->imap, param->command,
2289 param->server, param->port);
2295 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2296 const char * server, int port)
2298 struct connect_cmd_param param;
2299 struct connect_cmd_result result;
2304 imap = mailimap_new(0, NULL);
2307 key.len = sizeof(folder);
2310 chash_set(session_hash, &key, &value, NULL);
2313 param.command = command;
2314 param.server = server;
2317 threaded_run(folder, ¶m, &result, connect_cmd_run);
2319 debug_print("connect_cmd ok %i\n", result.error);
2321 return result.error;
2325 void imap_main_init(void)
2328 void imap_main_done(void)
2331 void imap_main_set_timeout(int sec)