2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "imap-thread.h"
28 #include <sys/types.h>
30 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
31 #include <sys/socket.h>
39 #include "etpan-thread-manager.h"
41 #include "mainwindow.h"
42 #include "ssl_certificate.h"
44 #include "remotefolder.h"
46 #define DISABLE_LOG_DURING_LOGIN
48 static struct etpan_thread_manager * thread_manager = NULL;
49 static chash * courier_workaround_hash = NULL;
50 static chash * imap_hash = NULL;
51 static chash * session_hash = NULL;
52 static guint thread_manager_signal = 0;
53 static GIOChannel * io_channel = NULL;
55 static void delete_imap(Folder *folder, mailimap *imap)
61 key.len = sizeof(folder);
64 chash_delete(session_hash, &key, NULL);
67 key.len = sizeof(imap);
68 chash_delete(courier_workaround_hash, &key, NULL);
69 if (imap && imap->imap_stream) {
70 /* we don't want libetpan to logout */
71 mailstream_close(imap->imap_stream);
72 imap->imap_stream = NULL;
74 debug_print("removing mailimap %p\n", imap);
78 static gboolean thread_manager_event(GIOChannel * source,
79 GIOCondition condition,
82 etpan_thread_manager_loop(thread_manager);
87 static void imap_logger_cmd(int direction, const char * str, size_t size)
94 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
98 memset(buf, 0, size+1);
99 strncpy(buf, str, size);
102 if (!strncmp(buf, "<<<<<<<", 7)
103 || !strncmp(buf, ">>>>>>>", 7)) {
107 while (strstr(buf, "\r"))
108 *strstr(buf, "\r") = ' ';
109 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
110 buf[strlen(buf)-1] = '\0';
112 lines = g_strsplit(buf, "\n", -1);
114 while (lines[i] && *lines[i]) {
115 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
122 static void imap_logger_fetch(int direction, const char * str, size_t size)
129 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
133 buf = malloc(size+1);
134 memset(buf, 0, size+1);
135 strncpy(buf, str, size);
137 if (!strncmp(buf, "<<<<<<<", 7)
138 || !strncmp(buf, ">>>>>>>", 7)) {
142 while (strstr(buf, "\r"))
143 *strstr(buf, "\r") = ' ';
144 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
145 buf[strlen(buf)-1] = '\0';
147 lines = g_strsplit(buf, "\n", -1);
149 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
150 while (lines[i] && *lines[i]) {
151 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
155 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
161 static void imap_logger_uid(int direction, const char * str, size_t size)
168 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
171 buf = malloc(size+1);
172 memset(buf, 0, size+1);
173 strncpy(buf, str, size);
175 if (!strncmp(buf, "<<<<<<<", 7)
176 || !strncmp(buf, ">>>>>>>", 7)) {
180 while (strstr(buf, "\r"))
181 *strstr(buf, "\r") = ' ';
182 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
183 buf[strlen(buf)-1] = '\0';
185 lines = g_strsplit(buf, "\n", -1);
187 while (lines[i] && *lines[i]) {
188 int llen = strlen(lines[i]);
190 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
193 strncpy2(tmp, lines[i], 63);
194 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
203 static void imap_logger_append(int direction, const char * str, size_t size)
210 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
212 } else if (direction == 0 && size > 64) {
213 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
216 buf = malloc(size+1);
217 memset(buf, 0, size+1);
218 strncpy(buf, str, size);
220 if (!strncmp(buf, "<<<<<<<", 7)
221 || !strncmp(buf, ">>>>>>>", 7)) {
225 while (strstr(buf, "\r"))
226 *strstr(buf, "\r") = ' ';
227 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
228 buf[strlen(buf)-1] = '\0';
230 lines = g_strsplit(buf, "\n", -1);
232 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
233 while (lines[i] && *lines[i]) {
234 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
238 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
244 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
245 static gboolean etpan_skip_ssl_cert_check = FALSE;
246 extern void mailsasl_ref(void);
248 void imap_main_init(gboolean skip_ssl_cert_check)
250 int fd_thread_manager;
252 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
253 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
254 mailstream_network_delay.tv_usec = 0;
256 mailstream_debug = 1;
257 mailstream_logger = imap_logger_cmd;
260 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
261 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
262 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
264 thread_manager = etpan_thread_manager_new();
266 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
268 io_channel = g_io_channel_unix_new(fd_thread_manager);
270 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
271 thread_manager_event,
276 void imap_main_set_timeout(int sec)
278 mailstream_network_delay.tv_sec = sec;
279 mailstream_network_delay.tv_usec = 0;
282 void imap_main_done(void)
284 etpan_thread_manager_stop(thread_manager);
285 etpan_thread_manager_join(thread_manager);
287 g_source_remove(thread_manager_signal);
288 g_io_channel_unref(io_channel);
290 etpan_thread_manager_free(thread_manager);
292 chash_free(courier_workaround_hash);
293 chash_free(session_hash);
294 chash_free(imap_hash);
297 void imap_init(Folder * folder)
299 struct etpan_thread * thread;
303 thread = etpan_thread_manager_get_thread(thread_manager);
306 key.len = sizeof(folder);
310 chash_set(imap_hash, &key, &value, NULL);
313 void imap_done(Folder * folder)
315 struct etpan_thread * thread;
321 key.len = sizeof(folder);
323 r = chash_get(imap_hash, &key, &value);
329 etpan_thread_unbind(thread);
331 chash_delete(imap_hash, &key, NULL);
333 debug_print("remove thread");
336 static struct etpan_thread * get_thread(Folder * folder)
338 struct etpan_thread * thread;
343 key.len = sizeof(folder);
345 chash_get(imap_hash, &key, &value);
351 static mailimap * get_imap(Folder * folder)
359 key.len = sizeof(folder);
361 r = chash_get(session_hash, &key, &value);
366 debug_print("found imap %p\n", imap);
371 static void generic_cb(int cancelled, void * result, void * callback_data)
373 struct etpan_thread_op * op;
375 op = (struct etpan_thread_op *) callback_data;
377 debug_print("generic_cb\n");
378 if (op->imap && op->imap->imap_response_info &&
379 op->imap->imap_response_info->rsp_alert) {
380 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
381 op->imap->imap_response_info->rsp_alert);
382 mainwindow_show_error();
387 static void threaded_run(Folder * folder, void * param, void * result,
388 void (* func)(struct etpan_thread_op * ))
390 struct etpan_thread_op * op;
391 struct etpan_thread * thread;
393 imap_folder_ref(folder);
395 op = etpan_thread_op_new();
397 op->imap = get_imap(folder);
403 op->callback = generic_cb;
404 op->callback_data = op;
409 thread = get_thread(folder);
410 etpan_thread_op_schedule(thread, op);
412 while (!op->finished) {
413 gtk_main_iteration();
416 etpan_thread_op_free(op);
418 imap_folder_unref(folder);
424 struct connect_param {
430 struct connect_result {
434 #define CHECK_IMAP() { \
435 if (!param->imap) { \
436 result->error = MAILIMAP_ERROR_BAD_STATE; \
441 static void connect_run(struct etpan_thread_op * op)
444 struct connect_param * param;
445 struct connect_result * result;
452 r = mailimap_socket_connect(param->imap,
453 param->server, param->port);
459 int imap_threaded_connect(Folder * folder, const char * server, int port)
461 struct connect_param param;
462 struct connect_result result;
465 mailimap * imap, * oldimap;
467 oldimap = get_imap(folder);
469 imap = mailimap_new(0, NULL);
472 debug_print("deleting old imap %p\n", oldimap);
473 delete_imap(folder, oldimap);
477 key.len = sizeof(folder);
480 chash_set(session_hash, &key, &value, NULL);
483 param.server = server;
487 threaded_run(folder, ¶m, &result, connect_run);
489 debug_print("connect ok %i with imap %p\n", result.error, imap);
494 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
497 struct connect_param *param = (struct connect_param *)data;
500 if (certificate == NULL || len < 0) {
501 g_warning("no cert presented.\n");
504 cert = d2i_X509(NULL, &certificate, len);
506 g_warning("can't get cert\n");
508 } else if (ssl_certificate_check(cert, NULL,
509 (gchar *)param->server, (gushort)param->port) == TRUE) {
521 static void connect_ssl_run(struct etpan_thread_op * op)
524 struct connect_param * param;
525 struct connect_result * result;
532 r = mailimap_ssl_connect(param->imap,
533 param->server, param->port);
537 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
539 struct connect_param param;
540 struct connect_result result;
543 mailimap * imap, * oldimap;
544 unsigned char *certificate = NULL;
547 oldimap = get_imap(folder);
549 imap = mailimap_new(0, NULL);
552 debug_print("deleting old imap %p\n", oldimap);
553 delete_imap(folder, oldimap);
557 key.len = sizeof(folder);
560 chash_set(session_hash, &key, &value, NULL);
563 param.server = server;
567 threaded_run(folder, ¶m, &result, connect_ssl_run);
569 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
570 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
571 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
572 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
577 debug_print("connect %d with imap %p\n", result.error, imap);
588 struct mailimap_capability_data *caps;
591 static void capability_run(struct etpan_thread_op * op)
594 struct capa_param * param;
595 struct capa_result * result;
596 struct mailimap_capability_data *caps;
603 r = mailimap_capability(param->imap, &caps);
606 result->caps = (r == 0 ? caps : NULL);
610 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
612 struct capa_param param;
613 struct capa_result result;
616 imap = get_imap(folder);
620 threaded_run(folder, ¶m, &result, capability_run);
622 debug_print("capa %d\n", result.error);
631 struct disconnect_param {
635 struct disconnect_result {
639 static void disconnect_run(struct etpan_thread_op * op)
642 struct disconnect_param * param;
643 struct disconnect_result * result;
650 r = mailimap_logout(param->imap);
655 void imap_threaded_disconnect(Folder * folder)
657 struct connect_param param;
658 struct connect_result result;
661 imap = get_imap(folder);
663 debug_print("was disconnected\n");
669 threaded_run(folder, ¶m, &result, disconnect_run);
671 if (imap == get_imap(folder)) {
672 debug_print("deleting old imap %p\n", imap);
673 delete_imap(folder, imap);
675 debug_print("imap already deleted %p\n", imap);
678 debug_print("disconnect ok\n");
685 const char * wildcard;
694 static void list_run(struct etpan_thread_op * op)
696 struct list_param * param;
697 struct list_result * result;
708 if (param->base == NULL || param->wildcard == NULL) {
711 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
715 r = mailimap_lsub(param->imap, param->base,
716 param->wildcard, &list);
718 r = mailimap_list(param->imap, param->base,
719 param->wildcard, &list);
722 debug_print("imap list run - end\n");
725 int imap_threaded_list(Folder * folder, const char * base,
726 const char * wildcard,
729 struct list_param param;
730 struct list_result result;
732 debug_print("imap list - begin\n");
734 param.imap = get_imap(folder);
736 param.wildcard = wildcard;
737 param.sub_only = FALSE;
739 threaded_run(folder, ¶m, &result, list_run);
741 * p_result = result.list;
743 debug_print("imap list - end %p\n", result.list);
748 int imap_threaded_lsub(Folder * folder, const char * base,
749 const char * wildcard,
752 struct list_param param;
753 struct list_result result;
755 debug_print("imap lsub - begin\n");
757 param.imap = get_imap(folder);
759 param.wildcard = wildcard;
760 param.sub_only = TRUE;
762 threaded_run(folder, ¶m, &result, list_run);
764 * p_result = result.list;
766 debug_print("imap lsub - end %p\n", result.list);
771 struct subscribe_param {
777 struct subscribe_result {
781 static void subscribe_run(struct etpan_thread_op * op)
783 struct subscribe_param * param;
784 struct subscribe_result * result;
792 if (param->mb == NULL) {
794 debug_print("no mb\n");
797 if (param->subscribe)
798 r = mailimap_subscribe(param->imap, param->mb);
800 r = mailimap_unsubscribe(param->imap, param->mb);
802 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
805 int imap_threaded_subscribe(Folder * folder, const char * mb,
808 struct subscribe_param param;
809 struct subscribe_result result;
811 debug_print("imap list - begin\n");
813 param.imap = get_imap(folder);
815 param.subscribe = subscribe;
817 threaded_run(folder, ¶m, &result, subscribe_run);
825 const char * password;
830 struct login_result {
834 static void login_run(struct etpan_thread_op * op)
836 struct login_param * param;
837 struct login_result * result;
839 #ifdef DISABLE_LOG_DURING_LOGIN
848 #ifdef DISABLE_LOG_DURING_LOGIN
849 old_debug = mailstream_debug;
850 mailstream_debug = 0;
852 if (!strcmp(param->type, "LOGIN"))
853 r = mailimap_login(param->imap,
854 param->login, param->password);
855 else if (!strcmp(param->type, "GSSAPI"))
856 r = mailimap_authenticate(param->imap,
857 param->type, param->server, NULL, NULL,
858 param->login, param->login,
859 param->password, NULL);
861 r = mailimap_authenticate(param->imap,
862 param->type, NULL, NULL, NULL,
863 param->login, param->login,
864 param->password, NULL);
865 #ifdef DISABLE_LOG_DURING_LOGIN
866 mailstream_debug = old_debug;
870 debug_print("imap login run - end %i\n", r);
873 int imap_threaded_login(Folder * folder,
874 const char * login, const char * password,
877 struct login_param param;
878 struct login_result result;
880 debug_print("imap login - begin\n");
882 param.imap = get_imap(folder);
884 param.password = password;
886 if (folder && folder->account)
887 param.server = folder->account->recv_server;
891 threaded_run(folder, ¶m, &result, login_run);
893 debug_print("imap login - end\n");
899 struct status_param {
902 struct mailimap_status_att_list * status_att_list;
905 struct status_result {
907 struct mailimap_mailbox_data_status * data_status;
910 static void status_run(struct etpan_thread_op * op)
912 struct status_param * param;
913 struct status_result * result;
921 r = mailimap_status(param->imap, param->mb,
922 param->status_att_list,
923 &result->data_status);
926 debug_print("imap status run - end %i\n", r);
929 int imap_threaded_status(Folder * folder, const char * mb,
930 struct mailimap_mailbox_data_status ** data_status,
933 struct status_param param;
934 struct status_result result;
935 struct mailimap_status_att_list * status_att_list;
937 debug_print("imap status - begin\n");
939 status_att_list = mailimap_status_att_list_new_empty();
941 mailimap_status_att_list_add(status_att_list,
942 MAILIMAP_STATUS_ATT_MESSAGES);
945 mailimap_status_att_list_add(status_att_list,
946 MAILIMAP_STATUS_ATT_RECENT);
949 mailimap_status_att_list_add(status_att_list,
950 MAILIMAP_STATUS_ATT_UIDNEXT);
953 mailimap_status_att_list_add(status_att_list,
954 MAILIMAP_STATUS_ATT_UIDVALIDITY);
957 mailimap_status_att_list_add(status_att_list,
958 MAILIMAP_STATUS_ATT_UNSEEN);
960 param.imap = get_imap(folder);
962 param.status_att_list = status_att_list;
964 threaded_run(folder, ¶m, &result, status_run);
966 debug_print("imap status - end\n");
968 * data_status = result.data_status;
970 mailimap_status_att_list_free(status_att_list);
985 static void noop_run(struct etpan_thread_op * op)
987 struct noop_param * param;
988 struct noop_result * result;
996 r = mailimap_noop(param->imap);
999 debug_print("imap noop run - end %i\n", r);
1002 int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
1004 struct noop_param param;
1005 struct noop_result result;
1008 debug_print("imap noop - begin\n");
1010 imap = get_imap(folder);
1013 threaded_run(folder, ¶m, &result, noop_run);
1015 if (imap && imap->imap_selection_info != NULL) {
1016 * p_exists = imap->imap_selection_info->sel_exists;
1022 debug_print("imap noop - end\n");
1024 return result.error;
1028 struct starttls_result {
1032 static void starttls_run(struct etpan_thread_op * op)
1034 struct connect_param * param;
1035 struct starttls_result * result;
1039 result = op->result;
1043 r = mailimap_starttls(param->imap);
1046 debug_print("imap starttls run - end %i\n", r);
1049 mailimap *imap = param->imap;
1050 mailstream_low *plain_low = NULL;
1051 mailstream_low *tls_low = NULL;
1054 plain_low = mailstream_get_low(imap->imap_stream);
1055 fd = mailstream_low_get_fd(plain_low);
1057 debug_print("imap starttls run - can't get fd\n");
1058 result->error = MAILIMAP_ERROR_STREAM;
1062 tls_low = mailstream_low_tls_open(fd);
1063 if (tls_low == NULL) {
1064 debug_print("imap starttls run - can't tls_open\n");
1065 result->error = MAILIMAP_ERROR_STREAM;
1068 mailstream_low_free(plain_low);
1069 mailstream_set_low(imap->imap_stream, tls_low);
1073 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1075 struct connect_param param;
1076 struct starttls_result result;
1078 unsigned char *certificate;
1080 debug_print("imap starttls - begin\n");
1082 param.imap = get_imap(folder);
1083 param.server = host;
1086 threaded_run(folder, ¶m, &result, starttls_run);
1088 debug_print("imap starttls - end\n");
1090 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1091 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1092 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1093 result.error = MAILIMAP_ERROR_STREAM;
1097 return result.error;
1102 struct create_param {
1107 struct create_result {
1111 static void create_run(struct etpan_thread_op * op)
1113 struct create_param * param;
1114 struct create_result * result;
1118 result = op->result;
1122 r = mailimap_create(param->imap, param->mb);
1125 debug_print("imap create run - end %i\n", r);
1128 int imap_threaded_create(Folder * folder, const char * mb)
1130 struct create_param param;
1131 struct create_result result;
1133 debug_print("imap create - begin\n");
1135 param.imap = get_imap(folder);
1138 threaded_run(folder, ¶m, &result, create_run);
1140 debug_print("imap create - end\n");
1142 return result.error;
1148 struct rename_param {
1151 const char * new_name;
1154 struct rename_result {
1158 static void rename_run(struct etpan_thread_op * op)
1160 struct rename_param * param;
1161 struct rename_result * result;
1165 result = op->result;
1169 r = mailimap_rename(param->imap, param->mb, param->new_name);
1172 debug_print("imap rename run - end %i\n", r);
1175 int imap_threaded_rename(Folder * folder,
1176 const char * mb, const char * new_name)
1178 struct rename_param param;
1179 struct rename_result result;
1181 debug_print("imap rename - begin\n");
1183 param.imap = get_imap(folder);
1185 param.new_name = new_name;
1187 threaded_run(folder, ¶m, &result, rename_run);
1189 debug_print("imap rename - end\n");
1191 return result.error;
1197 struct delete_param {
1202 struct delete_result {
1206 static void delete_run(struct etpan_thread_op * op)
1208 struct delete_param * param;
1209 struct delete_result * result;
1213 result = op->result;
1217 r = mailimap_delete(param->imap, param->mb);
1220 debug_print("imap delete run - end %i\n", r);
1223 int imap_threaded_delete(Folder * folder, const char * mb)
1225 struct delete_param param;
1226 struct delete_result result;
1228 debug_print("imap delete - begin\n");
1230 param.imap = get_imap(folder);
1233 threaded_run(folder, ¶m, &result, delete_run);
1235 debug_print("imap delete - end\n");
1237 return result.error;
1242 struct select_param {
1247 struct select_result {
1251 static void select_run(struct etpan_thread_op * op)
1253 struct select_param * param;
1254 struct select_result * result;
1258 result = op->result;
1262 r = mailimap_select(param->imap, param->mb);
1265 debug_print("imap select run - end %i\n", r);
1268 int imap_threaded_select(Folder * folder, const char * mb,
1269 gint * exists, gint * recent, gint * unseen,
1270 guint32 * uid_validity)
1272 struct select_param param;
1273 struct select_result result;
1276 debug_print("imap select - begin\n");
1278 imap = get_imap(folder);
1282 threaded_run(folder, ¶m, &result, select_run);
1284 if (result.error != MAILIMAP_NO_ERROR)
1285 return result.error;
1287 if (!imap || imap->imap_selection_info == NULL)
1288 return MAILIMAP_ERROR_PARSE;
1290 * exists = imap->imap_selection_info->sel_exists;
1291 * recent = imap->imap_selection_info->sel_recent;
1292 * unseen = imap->imap_selection_info->sel_unseen;
1293 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1295 debug_print("imap select - end\n");
1297 return result.error;
1302 struct examine_param {
1307 struct examine_result {
1311 static void examine_run(struct etpan_thread_op * op)
1313 struct examine_param * param;
1314 struct examine_result * result;
1318 result = op->result;
1322 r = mailimap_examine(param->imap, param->mb);
1325 debug_print("imap examine run - end %i\n", r);
1328 int imap_threaded_examine(Folder * folder, const char * mb,
1329 gint * exists, gint * recent, gint * unseen,
1330 guint32 * uid_validity)
1332 struct examine_param param;
1333 struct examine_result result;
1336 debug_print("imap examine - begin\n");
1338 imap = get_imap(folder);
1342 threaded_run(folder, ¶m, &result, examine_run);
1344 if (result.error != MAILIMAP_NO_ERROR)
1345 return result.error;
1347 if (!imap || imap->imap_selection_info == NULL)
1348 return MAILIMAP_ERROR_PARSE;
1350 * exists = imap->imap_selection_info->sel_exists;
1351 * recent = imap->imap_selection_info->sel_recent;
1352 * unseen = imap->imap_selection_info->sel_unseen;
1353 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1355 debug_print("imap examine - end\n");
1357 return result.error;
1363 struct search_param {
1366 struct mailimap_set * set;
1369 struct search_result {
1371 clist * search_result;
1374 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1376 return mailimap_set_item_new(orig->set_first, orig->set_last);
1379 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1381 clist *list = orig ? orig->set_list : NULL;
1382 clist *newlist = clist_new();
1387 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1388 clist_append(newlist,
1389 sc_mailimap_set_item_copy(
1390 (struct mailimap_set_item *)clist_content(cur)));
1391 return mailimap_set_new(newlist);
1394 static void search_run(struct etpan_thread_op * op)
1396 struct search_param * param;
1397 struct search_result * result;
1399 struct mailimap_search_key * key = NULL;
1400 struct mailimap_search_key * uid_key = NULL;
1401 struct mailimap_search_key * search_type_key;
1402 clist * search_result;
1405 result = op->result;
1409 /* we copy the mailimap_set because freeing the key is recursive */
1410 if (param->set != NULL) {
1411 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1412 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1413 uid_key = mailimap_search_key_new_all();
1415 search_type_key = NULL;
1416 switch (param->type) {
1417 case IMAP_SEARCH_TYPE_SIMPLE:
1418 search_type_key = NULL;
1421 case IMAP_SEARCH_TYPE_SEEN:
1422 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1423 NULL, NULL, NULL, NULL, NULL,
1424 NULL, NULL, NULL, NULL, NULL,
1425 NULL, NULL, NULL, NULL, 0,
1426 NULL, NULL, NULL, NULL, NULL,
1427 NULL, 0, NULL, NULL, NULL);
1430 case IMAP_SEARCH_TYPE_UNSEEN:
1431 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1432 NULL, NULL, NULL, NULL, NULL,
1433 NULL, NULL, NULL, NULL, NULL,
1434 NULL, NULL, NULL, NULL, 0,
1435 NULL, NULL, NULL, NULL, NULL,
1436 NULL, 0, NULL, NULL, NULL);
1439 case IMAP_SEARCH_TYPE_ANSWERED:
1440 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1441 NULL, NULL, NULL, NULL, NULL,
1442 NULL, NULL, NULL, NULL, NULL,
1443 NULL, NULL, NULL, NULL, 0,
1444 NULL, NULL, NULL, NULL, NULL,
1445 NULL, 0, NULL, NULL, NULL);
1448 case IMAP_SEARCH_TYPE_FLAGGED:
1449 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1450 NULL, NULL, NULL, NULL, NULL,
1451 NULL, NULL, NULL, NULL, NULL,
1452 NULL, NULL, NULL, NULL, 0,
1453 NULL, NULL, NULL, NULL, NULL,
1454 NULL, 0, NULL, NULL, NULL);
1456 case IMAP_SEARCH_TYPE_DELETED:
1457 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1458 NULL, NULL, NULL, NULL, NULL,
1459 NULL, NULL, NULL, NULL, NULL,
1460 NULL, NULL, NULL, NULL, 0,
1461 NULL, NULL, NULL, NULL, NULL,
1462 NULL, 0, NULL, NULL, NULL);
1466 if (search_type_key != NULL) {
1467 if (param->set != NULL) {
1468 key = mailimap_search_key_new_multiple_empty();
1469 mailimap_search_key_multiple_add(key, search_type_key);
1470 mailimap_search_key_multiple_add(key, uid_key);
1472 key = search_type_key;
1474 } else if (uid_key != NULL) {
1479 g_warning("no key!");
1480 result = op->result;
1482 result->search_result = NULL;
1484 mailstream_logger = imap_logger_uid;
1486 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1488 mailstream_logger = imap_logger_cmd;
1490 /* free the key (with the imapset) */
1491 mailimap_search_key_free(key);
1494 result->search_result = search_result;
1496 debug_print("imap search run - end %i\n", result->error);
1499 int imap_threaded_search(Folder * folder, int search_type,
1500 struct mailimap_set * set, clist ** search_result)
1502 struct search_param param;
1503 struct search_result result;
1506 debug_print("imap search - begin\n");
1508 imap = get_imap(folder);
1511 param.type = search_type;
1513 threaded_run(folder, ¶m, &result, search_run);
1515 if (result.error != MAILIMAP_NO_ERROR)
1516 return result.error;
1518 debug_print("imap search - end\n");
1520 * search_result = result.search_result;
1522 return result.error;
1529 uid_list_to_env_list(clist * fetch_result, carray ** result)
1537 tab = carray_new(128);
1539 res = MAILIMAP_ERROR_MEMORY;
1543 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1544 cur = clist_next(cur)) {
1545 struct mailimap_msg_att * msg_att;
1546 clistiter * item_cur;
1551 msg_att = clist_content(cur);
1555 for(item_cur = clist_begin(msg_att->att_list) ;
1557 item_cur = clist_next(item_cur)) {
1558 struct mailimap_msg_att_item * item;
1560 item = clist_content(item_cur);
1562 switch (item->att_type) {
1563 case MAILIMAP_MSG_ATT_ITEM_STATIC:
1564 switch (item->att_data.att_static->att_type) {
1565 case MAILIMAP_MSG_ATT_UID:
1566 uid = item->att_data.att_static->att_data.att_uid;
1572 puid = malloc(sizeof(* puid));
1574 res = MAILIMAP_ERROR_MEMORY;
1579 r = carray_add(tab, puid, NULL);
1582 res = MAILIMAP_ERROR_MEMORY;
1589 return MAILIMAP_NO_ERROR;
1592 for(i = 0 ; i < carray_count(tab) ; i++)
1593 mailmessage_free(carray_get(tab, i));
1598 static int imap_get_messages_list(mailimap * imap,
1599 uint32_t first_index,
1604 struct mailimap_fetch_att * fetch_att;
1605 struct mailimap_fetch_type * fetch_type;
1606 struct mailimap_set * set;
1607 clist * fetch_result;
1610 set = mailimap_set_new_interval(first_index, 0);
1612 res = MAILIMAP_ERROR_MEMORY;
1616 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1617 if (fetch_type == NULL) {
1618 res = MAILIMAP_ERROR_MEMORY;
1622 fetch_att = mailimap_fetch_att_new_uid();
1623 if (fetch_att == NULL) {
1624 res = MAILIMAP_ERROR_MEMORY;
1625 goto free_fetch_type;
1628 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1629 if (r != MAILIMAP_NO_ERROR) {
1630 mailimap_fetch_att_free(fetch_att);
1631 res = MAILIMAP_ERROR_MEMORY;
1632 goto free_fetch_type;
1635 r = mailimap_uid_fetch(imap, set,
1636 fetch_type, &fetch_result);
1638 mailimap_fetch_type_free(fetch_type);
1639 mailimap_set_free(set);
1641 if (r != MAILIMAP_NO_ERROR) {
1647 r = uid_list_to_env_list(fetch_result, &env_list);
1648 mailimap_fetch_list_free(fetch_result);
1650 * result = env_list;
1652 return MAILIMAP_NO_ERROR;
1655 mailimap_fetch_type_free(fetch_type);
1657 mailimap_set_free(set);
1665 struct fetch_uid_param {
1667 uint32_t first_index;
1670 struct fetch_uid_result {
1672 carray * fetch_result;
1675 static void fetch_uid_run(struct etpan_thread_op * op)
1677 struct fetch_uid_param * param;
1678 struct fetch_uid_result * result;
1679 carray * fetch_result;
1683 result = op->result;
1687 fetch_result = NULL;
1688 r = imap_get_messages_list(param->imap, param->first_index,
1692 result->fetch_result = fetch_result;
1693 debug_print("imap fetch_uid run - end %i\n", r);
1696 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1697 carray ** fetch_result)
1699 struct fetch_uid_param param;
1700 struct fetch_uid_result result;
1703 debug_print("imap fetch_uid - begin\n");
1705 imap = get_imap(folder);
1707 param.first_index = first_index;
1709 threaded_run(folder, ¶m, &result, fetch_uid_run);
1711 if (result.error != MAILIMAP_NO_ERROR)
1712 return result.error;
1714 debug_print("imap fetch_uid - end\n");
1716 * fetch_result = result.fetch_result;
1718 return result.error;
1722 void imap_fetch_uid_list_free(carray * uid_list)
1726 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1729 puid = carray_get(uid_list, i);
1732 carray_free(uid_list);
1737 static int imap_fetch(mailimap * imap,
1740 size_t * result_len)
1743 struct mailimap_set * set;
1744 struct mailimap_fetch_att * fetch_att;
1745 struct mailimap_fetch_type * fetch_type;
1746 clist * fetch_result;
1747 struct mailimap_msg_att * msg_att;
1748 struct mailimap_msg_att_item * msg_att_item;
1753 struct mailimap_section * section;
1755 set = mailimap_set_new_single(msg_index);
1757 res = MAILIMAP_ERROR_MEMORY;
1761 section = mailimap_section_new(NULL);
1762 if (section == NULL) {
1763 res = MAILIMAP_ERROR_MEMORY;
1767 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1768 if (fetch_att == NULL) {
1769 mailimap_section_free(section);
1770 res = MAILIMAP_ERROR_MEMORY;
1774 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1775 if (fetch_type == NULL) {
1776 res = MAILIMAP_ERROR_MEMORY;
1777 goto free_fetch_att;
1780 mailstream_logger = imap_logger_fetch;
1782 r = mailimap_uid_fetch(imap, set,
1783 fetch_type, &fetch_result);
1785 mailstream_logger = imap_logger_cmd;
1787 mailimap_fetch_type_free(fetch_type);
1788 mailimap_set_free(set);
1791 case MAILIMAP_NO_ERROR:
1797 if (clist_begin(fetch_result) == NULL) {
1798 mailimap_fetch_list_free(fetch_result);
1799 return MAILIMAP_ERROR_FETCH;
1802 msg_att = clist_begin(fetch_result)->data;
1807 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1808 cur = clist_next(cur)) {
1809 msg_att_item = clist_content(cur);
1811 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1812 if (msg_att_item->att_data.att_static->att_type ==
1813 MAILIMAP_MSG_ATT_BODY_SECTION) {
1814 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1816 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1818 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1823 mailimap_fetch_list_free(fetch_result);
1826 return MAILIMAP_ERROR_FETCH;
1829 * result_len = text_length;
1831 return MAILIMAP_NO_ERROR;
1834 mailimap_fetch_att_free(fetch_att);
1836 mailimap_set_free(set);
1841 static int imap_fetch_header(mailimap * imap,
1844 size_t * result_len)
1847 struct mailimap_set * set;
1848 struct mailimap_fetch_att * fetch_att;
1849 struct mailimap_fetch_type * fetch_type;
1850 clist * fetch_result;
1851 struct mailimap_msg_att * msg_att;
1852 struct mailimap_msg_att_item * msg_att_item;
1857 struct mailimap_section * section;
1859 set = mailimap_set_new_single(msg_index);
1861 res = MAILIMAP_ERROR_MEMORY;
1865 section = mailimap_section_new_header();
1866 if (section == NULL) {
1867 res = MAILIMAP_ERROR_MEMORY;
1871 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
1872 if (fetch_att == NULL) {
1873 mailimap_section_free(section);
1874 res = MAILIMAP_ERROR_MEMORY;
1878 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
1879 if (fetch_type == NULL) {
1880 res = MAILIMAP_ERROR_MEMORY;
1881 goto free_fetch_att;
1884 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
1886 mailimap_fetch_type_free(fetch_type);
1887 mailimap_set_free(set);
1890 case MAILIMAP_NO_ERROR:
1896 if (clist_begin(fetch_result) == NULL) {
1897 mailimap_fetch_list_free(fetch_result);
1898 return MAILIMAP_ERROR_FETCH;
1901 msg_att = clist_begin(fetch_result)->data;
1906 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
1907 cur = clist_next(cur)) {
1908 msg_att_item = clist_content(cur);
1910 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
1911 if (msg_att_item->att_data.att_static->att_type ==
1912 MAILIMAP_MSG_ATT_BODY_SECTION) {
1913 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
1914 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
1916 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
1921 mailimap_fetch_list_free(fetch_result);
1924 return MAILIMAP_ERROR_FETCH;
1927 * result_len = text_length;
1929 return MAILIMAP_NO_ERROR;
1932 mailimap_fetch_att_free(fetch_att);
1934 mailimap_set_free(set);
1941 struct fetch_content_param {
1944 const char * filename;
1948 struct fetch_content_result {
1952 static void fetch_content_run(struct etpan_thread_op * op)
1954 struct fetch_content_param * param;
1955 struct fetch_content_result * result;
1957 size_t content_size;
1963 result = op->result;
1969 if (param->with_body)
1970 r = imap_fetch(param->imap, param->msg_index,
1971 &content, &content_size);
1973 r = imap_fetch_header(param->imap, param->msg_index,
1974 &content, &content_size);
1978 if (r == MAILIMAP_NO_ERROR) {
1979 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
1981 result->error = MAILIMAP_ERROR_FETCH;
1985 f = fdopen(fd, "wb");
1987 result->error = MAILIMAP_ERROR_FETCH;
1991 r = fwrite(content, 1, content_size, f);
1998 g_unlink(param->filename);
2009 g_unlink(param->filename);
2012 if (mmap_string_unref(content) != 0)
2016 debug_print("imap fetch_content run - end %i\n", r);
2019 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2021 const char * filename)
2023 struct fetch_content_param param;
2024 struct fetch_content_result result;
2027 debug_print("imap fetch_content - begin\n");
2029 imap = get_imap(folder);
2031 param.msg_index = msg_index;
2032 param.filename = filename;
2033 param.with_body = with_body;
2035 threaded_run(folder, ¶m, &result, fetch_content_run);
2037 if (result.error != MAILIMAP_NO_ERROR)
2038 return result.error;
2040 debug_print("imap fetch_content - end\n");
2042 return result.error;
2047 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
2055 flag_list = att_dyn->att_list;
2056 if (flag_list == NULL)
2059 for(cur = clist_begin(flag_list) ; cur != NULL ;
2060 cur = clist_next(cur)) {
2061 struct mailimap_flag_fetch * flag_fetch;
2063 flag_fetch = clist_content(cur);
2064 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2067 switch (flag_fetch->fl_flag->fl_type) {
2068 case MAILIMAP_FLAG_ANSWERED:
2069 flags |= MSG_REPLIED;
2071 case MAILIMAP_FLAG_FLAGGED:
2072 flags |= MSG_MARKED;
2074 case MAILIMAP_FLAG_DELETED:
2075 flags |= MSG_DELETED;
2077 case MAILIMAP_FLAG_SEEN:
2078 flags &= ~MSG_UNREAD;
2088 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2092 struct mailimap_msg_att_dynamic ** patt_dyn)
2094 clistiter * item_cur;
2098 struct mailimap_msg_att_dynamic * att_dyn;
2105 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2106 item_cur = clist_next(item_cur)) {
2107 struct mailimap_msg_att_item * item;
2109 item = clist_content(item_cur);
2111 switch (item->att_type) {
2112 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2113 switch (item->att_data.att_static->att_type) {
2114 case MAILIMAP_MSG_ATT_UID:
2115 uid = item->att_data.att_static->att_data.att_uid;
2118 case MAILIMAP_MSG_ATT_BODY_SECTION:
2119 if (headers == NULL) {
2120 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2123 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2124 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2129 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2130 if (att_dyn == NULL) {
2131 att_dyn = item->att_data.att_dyn;
2139 if (pheaders != NULL)
2140 * pheaders = headers;
2141 if (pref_size != NULL)
2142 * pref_size = ref_size;
2143 if (patt_dyn != NULL)
2144 * patt_dyn = att_dyn;
2146 return MAIL_NO_ERROR;
2149 static struct imap_fetch_env_info *
2150 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2152 struct imap_fetch_env_info * info;
2156 struct mailimap_msg_att_dynamic * att_dyn;
2158 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2163 info = malloc(sizeof(* info));
2165 info->headers = strdup(headers);
2167 info->flags = imap_flags_to_flags(att_dyn);
2173 imap_fetch_result_to_envelop_list(clist * fetch_result,
2174 carray ** p_env_list)
2181 env_list = carray_new(16);
2183 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2184 cur = clist_next(cur)) {
2185 struct mailimap_msg_att * msg_att;
2186 struct imap_fetch_env_info * env_info;
2188 msg_att = clist_content(cur);
2190 env_info = fetch_to_env_info(msg_att);
2192 return MAILIMAP_ERROR_MEMORY;
2193 carray_add(env_list, env_info, NULL);
2196 * p_env_list = env_list;
2198 return MAIL_NO_ERROR;
2201 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2203 struct mailimap_fetch_att * fetch_att;
2207 struct mailimap_header_list * imap_hdrlist;
2208 struct mailimap_section * section;
2210 hdrlist = clist_new();
2212 header = strdup("Date");
2213 r = clist_append(hdrlist, header);
2214 header = strdup("From");
2215 r = clist_append(hdrlist, header);
2216 header = strdup("To");
2217 r = clist_append(hdrlist, header);
2218 header = strdup("Cc");
2219 r = clist_append(hdrlist, header);
2220 header = strdup("Subject");
2221 r = clist_append(hdrlist, header);
2222 header = strdup("Message-ID");
2223 r = clist_append(hdrlist, header);
2224 header = strdup("References");
2225 r = clist_append(hdrlist, header);
2226 header = strdup("In-Reply-To");
2227 r = clist_append(hdrlist, header);
2229 imap_hdrlist = mailimap_header_list_new(hdrlist);
2230 section = mailimap_section_new_header_fields(imap_hdrlist);
2231 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2232 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2234 return MAIL_NO_ERROR;
2237 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2239 struct mailimap_fetch_att * fetch_att;
2240 struct mailimap_section * section;
2242 section = mailimap_section_new_header();
2243 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2244 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2246 return MAIL_NO_ERROR;
2250 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2251 carray ** p_env_list)
2253 struct mailimap_fetch_att * fetch_att;
2254 struct mailimap_fetch_type * fetch_type;
2256 clist * fetch_result;
2258 carray * env_list = NULL;
2262 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2265 fetch_att = mailimap_fetch_att_new_uid();
2266 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2269 fetch_att = mailimap_fetch_att_new_flags();
2270 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2273 fetch_att = mailimap_fetch_att_new_rfc822_size();
2274 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2278 key.len = sizeof(imap);
2279 r = chash_get(courier_workaround_hash, &key, &value);
2281 r = imap_add_envelope_fetch_att(fetch_type);
2283 r = imap_add_header_fetch_att(fetch_type);
2285 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2288 case MAILIMAP_NO_ERROR:
2291 mailimap_fetch_type_free(fetch_type);
2292 debug_print("uid_fetch: %d\n", r);
2296 if (clist_begin(fetch_result) == NULL) {
2297 res = MAILIMAP_ERROR_FETCH;
2298 debug_print("clist_begin = NULL\n");
2302 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2303 mailimap_fetch_list_free(fetch_result);
2305 if (r != MAILIMAP_NO_ERROR) {
2306 mailimap_fetch_type_free(fetch_type);
2307 res = MAILIMAP_ERROR_MEMORY;
2308 debug_print("fetch_result_to_envelop_list: %d\n", res);
2312 mailimap_fetch_type_free(fetch_type);
2314 * p_env_list = env_list;
2316 return MAILIMAP_NO_ERROR;
2322 struct fetch_env_param {
2324 struct mailimap_set * set;
2327 struct fetch_env_result {
2328 carray * fetch_env_result;
2332 static void fetch_env_run(struct etpan_thread_op * op)
2334 struct fetch_env_param * param;
2335 struct fetch_env_result * result;
2340 result = op->result;
2345 r = imap_get_envelopes_list(param->imap, param->set,
2349 result->fetch_env_result = env_list;
2351 debug_print("imap fetch_env run - end %i\n", r);
2354 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2355 carray ** p_env_list)
2357 struct fetch_env_param param;
2358 struct fetch_env_result result;
2361 debug_print("imap fetch_env - begin\n");
2363 imap = get_imap(folder);
2367 threaded_run(folder, ¶m, &result, fetch_env_run);
2369 if (result.error != MAILIMAP_NO_ERROR) {
2375 key.len = sizeof(imap);
2376 r = chash_get(courier_workaround_hash, &key, &value);
2380 chash_set(courier_workaround_hash, &key, &value, NULL);
2382 threaded_run(folder, ¶m, &result, fetch_env_run);
2386 if (result.error != MAILIMAP_NO_ERROR)
2387 return result.error;
2389 debug_print("imap fetch_env - end\n");
2391 * p_env_list = result.fetch_env_result;
2393 return result.error;
2396 void imap_fetch_env_free(carray * env_list)
2400 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2401 struct imap_fetch_env_info * env_info;
2403 env_info = carray_get(env_list, i);
2404 free(env_info->headers);
2407 carray_free(env_list);
2414 struct append_param {
2416 const char * mailbox;
2417 const char * filename;
2418 struct mailimap_flag_list * flag_list;
2421 struct append_result {
2426 static void append_run(struct etpan_thread_op * op)
2428 struct append_param * param;
2429 struct append_result * result;
2433 struct stat stat_buf;
2435 guint32 uid = 0, val = 0;
2438 result = op->result;
2442 r = stat(param->filename, &stat_buf);
2444 result->error = MAILIMAP_ERROR_APPEND;
2447 size = stat_buf.st_size;
2449 fd = open(param->filename, O_RDONLY);
2451 result->error = MAILIMAP_ERROR_APPEND;
2455 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2456 if (data == (void *) MAP_FAILED) {
2458 result->error = MAILIMAP_ERROR_APPEND;
2462 mailstream_logger = imap_logger_append;
2464 r = mailimap_uidplus_append(param->imap, param->mailbox,
2465 param->flag_list, NULL,
2466 data, size, &val, &uid);
2468 mailstream_logger = imap_logger_cmd;
2475 debug_print("imap append run - end %i uid %d\n", r, uid);
2478 int imap_threaded_append(Folder * folder, const char * mailbox,
2479 const char * filename,
2480 struct mailimap_flag_list * flag_list,
2483 struct append_param param;
2484 struct append_result result;
2487 debug_print("imap append - begin\n");
2489 imap = get_imap(folder);
2491 param.mailbox = mailbox;
2492 param.filename = filename;
2493 param.flag_list = flag_list;
2495 threaded_run(folder, ¶m, &result, append_run);
2497 if (result.error != MAILIMAP_NO_ERROR)
2498 return result.error;
2500 debug_print("imap append - end\n");
2504 return result.error;
2510 struct expunge_param {
2514 struct expunge_result {
2518 static void expunge_run(struct etpan_thread_op * op)
2520 struct expunge_param * param;
2521 struct expunge_result * result;
2525 result = op->result;
2529 r = mailimap_expunge(param->imap);
2532 debug_print("imap expunge run - end %i\n", r);
2535 int imap_threaded_expunge(Folder * folder)
2537 struct expunge_param param;
2538 struct expunge_result result;
2540 debug_print("imap expunge - begin\n");
2542 param.imap = get_imap(folder);
2544 threaded_run(folder, ¶m, &result, expunge_run);
2546 debug_print("imap expunge - end\n");
2548 return result.error;
2554 struct mailimap_set * set;
2558 struct copy_result {
2560 struct mailimap_set *source;
2561 struct mailimap_set *dest;
2564 static void copy_run(struct etpan_thread_op * op)
2566 struct copy_param * param;
2567 struct copy_result * result;
2570 struct mailimap_set *source = NULL, *dest = NULL;
2573 result = op->result;
2577 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2578 &val, &source, &dest);
2582 result->source = source;
2583 result->dest = dest;
2585 result->source = NULL;
2586 result->dest = NULL;
2588 debug_print("imap copy run - end %i\n", r);
2591 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2592 const char * mb, struct mailimap_set **source,
2593 struct mailimap_set **dest)
2595 struct copy_param param;
2596 struct copy_result result;
2599 debug_print("imap copy - begin\n");
2601 imap = get_imap(folder);
2606 threaded_run(folder, ¶m, &result, copy_run);
2610 if (result.error != MAILIMAP_NO_ERROR)
2611 return result.error;
2613 *source = result.source;
2614 *dest = result.dest;
2616 debug_print("imap copy - end\n");
2618 return result.error;
2623 struct store_param {
2625 struct mailimap_set * set;
2626 struct mailimap_store_att_flags * store_att_flags;
2629 struct store_result {
2633 static void store_run(struct etpan_thread_op * op)
2635 struct store_param * param;
2636 struct store_result * result;
2640 result = op->result;
2644 r = mailimap_uid_store(param->imap, param->set,
2645 param->store_att_flags);
2649 debug_print("imap store run - end %i\n", r);
2652 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2653 struct mailimap_store_att_flags * store_att_flags)
2655 struct store_param param;
2656 struct store_result result;
2659 debug_print("imap store - begin\n");
2661 imap = get_imap(folder);
2664 param.store_att_flags = store_att_flags;
2666 threaded_run(folder, ¶m, &result, store_run);
2668 if (result.error != MAILIMAP_NO_ERROR)
2669 return result.error;
2671 debug_print("imap store - end\n");
2673 return result.error;
2677 #define ENV_BUFFER_SIZE 512
2679 static void do_exec_command(int fd, const char * command,
2680 const char * servername, uint16_t port)
2684 char env_buffer[ENV_BUFFER_SIZE];
2688 /* Fork again to become a child of init rather than
2689 the etpan client. */
2695 snprintf(env_buffer, ENV_BUFFER_SIZE,
2696 "ETPANSERVER=%s", servername);
2698 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2702 setenv("ETPANSERVER", servername, 1);
2704 unsetenv("ETPANSERVER");
2709 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2711 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2717 snprintf(porttext, sizeof(porttext), "%d", port);
2718 setenv("ETPANPORT", porttext, 1);
2721 unsetenv("ETPANPORT");
2725 /* Not a lot we can do if there's an error other than bail. */
2726 if (dup2(fd, 0) == -1)
2728 if (dup2(fd, 1) == -1)
2731 /* Should we close stderr and reopen /dev/null? */
2733 maxopen = sysconf(_SC_OPEN_MAX);
2734 for (i=3; i < maxopen; i++)
2738 /* Detach from the controlling tty if we have one. Otherwise,
2739 SSH might do something stupid like trying to use it instead
2740 of running $SSH_ASKPASS. Doh. */
2741 fd = open("/dev/tty", O_RDONLY);
2743 ioctl(fd, TIOCNOTTY, NULL);
2746 #endif /* TIOCNOTTY */
2748 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
2750 /* Eep. Shouldn't reach this */
2754 static int subcommand_connect(const char *command,
2755 const char *servername, uint16_t port)
2757 /* SEB unsupported on Windows */
2761 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
2766 do_exec_command(sockfds[1], command, servername, port);
2768 else if (childpid == -1) {
2776 /* Reap child, leaving grandchild process to run */
2777 waitpid(childpid, NULL, 0);
2782 static int socket_connect_cmd(mailimap * imap, const char * command,
2783 const char * server, int port)
2789 fd = subcommand_connect(command, server, port);
2791 return MAILIMAP_ERROR_STREAM;
2793 s = mailstream_socket_open(fd);
2796 return MAILIMAP_ERROR_STREAM;
2799 r = mailimap_connect(imap, s);
2800 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
2801 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
2802 mailstream_close(s);
2804 imap->imap_stream = NULL;
2813 struct connect_cmd_param {
2815 const char * command;
2816 const char * server;
2820 struct connect_cmd_result {
2824 static void connect_cmd_run(struct etpan_thread_op * op)
2827 struct connect_cmd_param * param;
2828 struct connect_cmd_result * result;
2831 result = op->result;
2835 r = socket_connect_cmd(param->imap, param->command,
2836 param->server, param->port);
2842 int imap_threaded_connect_cmd(Folder * folder, const char * command,
2843 const char * server, int port)
2845 struct connect_cmd_param param;
2846 struct connect_cmd_result result;
2849 mailimap * imap, * oldimap;
2851 oldimap = get_imap(folder);
2853 imap = mailimap_new(0, NULL);
2856 debug_print("deleting old imap %p\n", oldimap);
2857 delete_imap(folder, oldimap);
2861 key.len = sizeof(folder);
2864 chash_set(session_hash, &key, &value, NULL);
2867 param.command = command;
2868 param.server = server;
2871 threaded_run(folder, ¶m, &result, connect_cmd_run);
2873 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
2875 return result.error;
2878 void imap_threaded_cancel(Folder * folder)
2882 imap = get_imap(folder);
2883 if (imap->imap_stream != NULL)
2884 mailstream_cancel(imap->imap_stream);
2889 void imap_main_init(void)
2892 void imap_main_done(void)
2895 void imap_main_set_timeout(int sec)
2899 void imap_threaded_cancel(Folder * folder);