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 3 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, see <http://www.gnu.org/licenses/>.
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)
128 if (size > 128 && !direction) {
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,
1003 unsigned int *p_recent,
1004 unsigned int *p_expunge,
1005 unsigned int *p_unseen,
1006 unsigned int *p_uidnext,
1007 unsigned int *p_uidval)
1009 struct noop_param param;
1010 struct noop_result result;
1013 debug_print("imap noop - begin\n");
1015 imap = get_imap(folder);
1018 threaded_run(folder, ¶m, &result, noop_run);
1020 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1021 * p_exists = imap->imap_selection_info->sel_exists;
1022 * p_recent = imap->imap_selection_info->sel_recent;
1023 * p_unseen = imap->imap_selection_info->sel_unseen;
1024 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1025 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1033 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1034 imap->imap_response_info->rsp_expunged != NULL) {
1035 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1039 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1040 *p_exists, *p_recent, *p_expunge, *p_unseen,
1041 *p_uidnext, *p_uidval);
1043 return result.error;
1047 struct starttls_result {
1051 static void starttls_run(struct etpan_thread_op * op)
1053 struct connect_param * param;
1054 struct starttls_result * result;
1058 result = op->result;
1062 r = mailimap_starttls(param->imap);
1065 debug_print("imap starttls run - end %i\n", r);
1068 mailimap *imap = param->imap;
1069 mailstream_low *plain_low = NULL;
1070 mailstream_low *tls_low = NULL;
1073 plain_low = mailstream_get_low(imap->imap_stream);
1074 fd = mailstream_low_get_fd(plain_low);
1076 debug_print("imap starttls run - can't get fd\n");
1077 result->error = MAILIMAP_ERROR_STREAM;
1081 tls_low = mailstream_low_tls_open(fd);
1082 if (tls_low == NULL) {
1083 debug_print("imap starttls run - can't tls_open\n");
1084 result->error = MAILIMAP_ERROR_STREAM;
1087 mailstream_low_free(plain_low);
1088 mailstream_set_low(imap->imap_stream, tls_low);
1092 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1094 struct connect_param param;
1095 struct starttls_result result;
1097 unsigned char *certificate;
1099 debug_print("imap starttls - begin\n");
1101 param.imap = get_imap(folder);
1102 param.server = host;
1105 threaded_run(folder, ¶m, &result, starttls_run);
1107 debug_print("imap starttls - end\n");
1109 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1110 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1111 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1112 result.error = MAILIMAP_ERROR_STREAM;
1116 return result.error;
1121 struct create_param {
1126 struct create_result {
1130 static void create_run(struct etpan_thread_op * op)
1132 struct create_param * param;
1133 struct create_result * result;
1137 result = op->result;
1141 r = mailimap_create(param->imap, param->mb);
1144 debug_print("imap create run - end %i\n", r);
1147 int imap_threaded_create(Folder * folder, const char * mb)
1149 struct create_param param;
1150 struct create_result result;
1152 debug_print("imap create - begin\n");
1154 param.imap = get_imap(folder);
1157 threaded_run(folder, ¶m, &result, create_run);
1159 debug_print("imap create - end\n");
1161 return result.error;
1167 struct rename_param {
1170 const char * new_name;
1173 struct rename_result {
1177 static void rename_run(struct etpan_thread_op * op)
1179 struct rename_param * param;
1180 struct rename_result * result;
1184 result = op->result;
1188 r = mailimap_rename(param->imap, param->mb, param->new_name);
1191 debug_print("imap rename run - end %i\n", r);
1194 int imap_threaded_rename(Folder * folder,
1195 const char * mb, const char * new_name)
1197 struct rename_param param;
1198 struct rename_result result;
1200 debug_print("imap rename - begin\n");
1202 param.imap = get_imap(folder);
1204 param.new_name = new_name;
1206 threaded_run(folder, ¶m, &result, rename_run);
1208 debug_print("imap rename - end\n");
1210 return result.error;
1216 struct delete_param {
1221 struct delete_result {
1225 static void delete_run(struct etpan_thread_op * op)
1227 struct delete_param * param;
1228 struct delete_result * result;
1232 result = op->result;
1236 r = mailimap_delete(param->imap, param->mb);
1239 debug_print("imap delete run - end %i\n", r);
1242 int imap_threaded_delete(Folder * folder, const char * mb)
1244 struct delete_param param;
1245 struct delete_result result;
1247 debug_print("imap delete - begin\n");
1249 param.imap = get_imap(folder);
1252 threaded_run(folder, ¶m, &result, delete_run);
1254 debug_print("imap delete - end\n");
1256 return result.error;
1261 struct select_param {
1266 struct select_result {
1270 static void select_run(struct etpan_thread_op * op)
1272 struct select_param * param;
1273 struct select_result * result;
1277 result = op->result;
1281 r = mailimap_select(param->imap, param->mb);
1284 debug_print("imap select run - end %i\n", r);
1287 int imap_threaded_select(Folder * folder, const char * mb,
1288 gint * exists, gint * recent, gint * unseen,
1289 guint32 * uid_validity)
1291 struct select_param param;
1292 struct select_result result;
1295 debug_print("imap select - begin\n");
1297 imap = get_imap(folder);
1301 threaded_run(folder, ¶m, &result, select_run);
1303 if (result.error != MAILIMAP_NO_ERROR)
1304 return result.error;
1306 if (!imap || imap->imap_selection_info == NULL)
1307 return MAILIMAP_ERROR_PARSE;
1309 * exists = imap->imap_selection_info->sel_exists;
1310 * recent = imap->imap_selection_info->sel_recent;
1311 * unseen = imap->imap_selection_info->sel_unseen;
1312 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1314 debug_print("imap select - end\n");
1316 return result.error;
1319 static void close_run(struct etpan_thread_op * op)
1321 struct select_param * param;
1322 struct select_result * result;
1326 result = op->result;
1330 r = mailimap_close(param->imap);
1333 debug_print("imap close run - end %i\n", r);
1336 int imap_threaded_close(Folder * folder)
1338 struct select_param param;
1339 struct select_result result;
1342 debug_print("imap close - begin\n");
1344 imap = get_imap(folder);
1347 threaded_run(folder, ¶m, &result, close_run);
1349 if (result.error != MAILIMAP_NO_ERROR)
1350 return result.error;
1352 debug_print("imap close - end\n");
1354 return result.error;
1357 struct examine_param {
1362 struct examine_result {
1366 static void examine_run(struct etpan_thread_op * op)
1368 struct examine_param * param;
1369 struct examine_result * result;
1373 result = op->result;
1377 r = mailimap_examine(param->imap, param->mb);
1380 debug_print("imap examine run - end %i\n", r);
1383 int imap_threaded_examine(Folder * folder, const char * mb,
1384 gint * exists, gint * recent, gint * unseen,
1385 guint32 * uid_validity)
1387 struct examine_param param;
1388 struct examine_result result;
1391 debug_print("imap examine - begin\n");
1393 imap = get_imap(folder);
1397 threaded_run(folder, ¶m, &result, examine_run);
1399 if (result.error != MAILIMAP_NO_ERROR)
1400 return result.error;
1402 if (!imap || imap->imap_selection_info == NULL)
1403 return MAILIMAP_ERROR_PARSE;
1405 * exists = imap->imap_selection_info->sel_exists;
1406 * recent = imap->imap_selection_info->sel_recent;
1407 * unseen = imap->imap_selection_info->sel_unseen;
1408 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1410 debug_print("imap examine - end\n");
1412 return result.error;
1418 struct search_param {
1421 struct mailimap_set * set;
1424 struct search_result {
1426 clist * search_result;
1429 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1431 return mailimap_set_item_new(orig->set_first, orig->set_last);
1434 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1436 clist *list = orig ? orig->set_list : NULL;
1437 clist *newlist = clist_new();
1442 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1443 clist_append(newlist,
1444 sc_mailimap_set_item_copy(
1445 (struct mailimap_set_item *)clist_content(cur)));
1446 return mailimap_set_new(newlist);
1449 static void search_run(struct etpan_thread_op * op)
1451 struct search_param * param;
1452 struct search_result * result;
1454 struct mailimap_search_key * key = NULL;
1455 struct mailimap_search_key * uid_key = NULL;
1456 struct mailimap_search_key * search_type_key;
1457 clist * search_result;
1460 result = op->result;
1464 /* we copy the mailimap_set because freeing the key is recursive */
1465 if (param->set != NULL) {
1466 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1467 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1468 uid_key = mailimap_search_key_new_all();
1470 search_type_key = NULL;
1471 switch (param->type) {
1472 case IMAP_SEARCH_TYPE_SIMPLE:
1473 search_type_key = NULL;
1476 case IMAP_SEARCH_TYPE_SEEN:
1477 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1478 NULL, NULL, NULL, NULL, NULL,
1479 NULL, NULL, NULL, NULL, NULL,
1480 NULL, NULL, NULL, NULL, 0,
1481 NULL, NULL, NULL, NULL, NULL,
1482 NULL, 0, NULL, NULL, NULL);
1485 case IMAP_SEARCH_TYPE_UNSEEN:
1486 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1487 NULL, NULL, NULL, NULL, NULL,
1488 NULL, NULL, NULL, NULL, NULL,
1489 NULL, NULL, NULL, NULL, 0,
1490 NULL, NULL, NULL, NULL, NULL,
1491 NULL, 0, NULL, NULL, NULL);
1494 case IMAP_SEARCH_TYPE_ANSWERED:
1495 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1496 NULL, NULL, NULL, NULL, NULL,
1497 NULL, NULL, NULL, NULL, NULL,
1498 NULL, NULL, NULL, NULL, 0,
1499 NULL, NULL, NULL, NULL, NULL,
1500 NULL, 0, NULL, NULL, NULL);
1503 case IMAP_SEARCH_TYPE_FLAGGED:
1504 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1505 NULL, NULL, NULL, NULL, NULL,
1506 NULL, NULL, NULL, NULL, NULL,
1507 NULL, NULL, NULL, NULL, 0,
1508 NULL, NULL, NULL, NULL, NULL,
1509 NULL, 0, NULL, NULL, NULL);
1511 case IMAP_SEARCH_TYPE_DELETED:
1512 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1513 NULL, NULL, NULL, NULL, NULL,
1514 NULL, NULL, NULL, NULL, NULL,
1515 NULL, NULL, NULL, NULL, 0,
1516 NULL, NULL, NULL, NULL, NULL,
1517 NULL, 0, NULL, NULL, NULL);
1521 if (search_type_key != NULL) {
1522 if (param->set != NULL) {
1523 key = mailimap_search_key_new_multiple_empty();
1524 mailimap_search_key_multiple_add(key, search_type_key);
1525 mailimap_search_key_multiple_add(key, uid_key);
1527 key = search_type_key;
1529 } else if (uid_key != NULL) {
1534 g_warning("no key!");
1535 result = op->result;
1537 result->search_result = NULL;
1539 mailstream_logger = imap_logger_uid;
1541 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1543 mailstream_logger = imap_logger_cmd;
1545 /* free the key (with the imapset) */
1546 mailimap_search_key_free(key);
1549 result->search_result = search_result;
1551 debug_print("imap search run - end %i\n", result->error);
1554 int imap_threaded_search(Folder * folder, int search_type,
1555 struct mailimap_set * set, clist ** search_result)
1557 struct search_param param;
1558 struct search_result result;
1561 debug_print("imap search - begin\n");
1563 imap = get_imap(folder);
1566 param.type = search_type;
1568 threaded_run(folder, ¶m, &result, search_run);
1570 if (result.error != MAILIMAP_NO_ERROR)
1571 return result.error;
1573 debug_print("imap search - end\n");
1575 * search_result = result.search_result;
1577 return result.error;
1582 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1586 struct mailimap_msg_att_dynamic ** patt_dyn);
1589 result_to_uid_list(clist * fetch_result, carray ** result)
1596 tab = carray_new(128);
1598 res = MAILIMAP_ERROR_MEMORY;
1602 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1603 cur = clist_next(cur)) {
1604 struct mailimap_msg_att * msg_att;
1608 msg_att = clist_content(cur);
1611 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1613 puid = malloc(sizeof(* puid));
1615 res = MAILIMAP_ERROR_MEMORY;
1620 r = carray_add(tab, puid, NULL);
1623 res = MAILIMAP_ERROR_MEMORY;
1630 return MAILIMAP_NO_ERROR;
1633 imap_fetch_uid_list_free(tab);
1638 static int imap_get_messages_list(mailimap * imap,
1639 uint32_t first_index,
1644 struct mailimap_fetch_att * fetch_att;
1645 struct mailimap_fetch_type * fetch_type;
1646 struct mailimap_set * set;
1647 clist * fetch_result;
1650 set = mailimap_set_new_interval(first_index, 0);
1652 res = MAILIMAP_ERROR_MEMORY;
1656 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1657 if (fetch_type == NULL) {
1658 res = MAILIMAP_ERROR_MEMORY;
1662 fetch_att = mailimap_fetch_att_new_uid();
1663 if (fetch_att == NULL) {
1664 res = MAILIMAP_ERROR_MEMORY;
1665 goto free_fetch_type;
1668 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1669 if (r != MAILIMAP_NO_ERROR) {
1670 mailimap_fetch_att_free(fetch_att);
1671 res = MAILIMAP_ERROR_MEMORY;
1672 goto free_fetch_type;
1675 mailstream_logger = imap_logger_fetch;
1677 r = mailimap_uid_fetch(imap, set,
1678 fetch_type, &fetch_result);
1680 mailstream_logger = imap_logger_cmd;
1681 mailimap_fetch_type_free(fetch_type);
1682 mailimap_set_free(set);
1684 if (r != MAILIMAP_NO_ERROR) {
1690 r = result_to_uid_list(fetch_result, &env_list);
1691 mailimap_fetch_list_free(fetch_result);
1693 * result = env_list;
1695 return MAILIMAP_NO_ERROR;
1698 mailimap_fetch_type_free(fetch_type);
1700 mailimap_set_free(set);
1708 struct fetch_uid_param {
1710 uint32_t first_index;
1713 struct fetch_uid_result {
1715 carray * fetch_result;
1718 static void fetch_uid_run(struct etpan_thread_op * op)
1720 struct fetch_uid_param * param;
1721 struct fetch_uid_result * result;
1722 carray * fetch_result;
1726 result = op->result;
1730 fetch_result = NULL;
1731 mailstream_logger = NULL;
1732 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1734 r = imap_get_messages_list(param->imap, param->first_index,
1737 mailstream_logger = imap_logger_cmd;
1740 result->fetch_result = fetch_result;
1741 debug_print("imap fetch_uid run - end %i\n", r);
1744 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1745 carray ** fetch_result)
1747 struct fetch_uid_param param;
1748 struct fetch_uid_result result;
1751 debug_print("imap fetch_uid - begin\n");
1753 imap = get_imap(folder);
1755 param.first_index = first_index;
1757 threaded_run(folder, ¶m, &result, fetch_uid_run);
1759 if (result.error != MAILIMAP_NO_ERROR)
1760 return result.error;
1762 debug_print("imap fetch_uid - end\n");
1764 * fetch_result = result.fetch_result;
1766 return result.error;
1770 void imap_fetch_uid_list_free(carray * uid_list)
1774 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1777 puid = carray_get(uid_list, i);
1780 carray_free(uid_list);
1786 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn);
1789 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1796 tab = carray_new(128);
1798 res = MAILIMAP_ERROR_MEMORY;
1802 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1803 cur = clist_next(cur)) {
1804 struct mailimap_msg_att * msg_att;
1807 struct mailimap_msg_att_dynamic * att_dyn;
1811 msg_att = clist_content(cur);
1815 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1818 if (att_dyn == NULL)
1821 flags = imap_flags_to_flags(att_dyn);
1823 puid = malloc(sizeof(* puid));
1825 res = MAILIMAP_ERROR_MEMORY;
1830 r = carray_add(tab, puid, NULL);
1833 res = MAILIMAP_ERROR_MEMORY;
1836 pflags = malloc(sizeof(* pflags));
1837 if (pflags == NULL) {
1838 res = MAILIMAP_ERROR_MEMORY;
1842 r = carray_add(tab, pflags, NULL);
1845 res = MAILIMAP_ERROR_MEMORY;
1852 return MAILIMAP_NO_ERROR;
1855 imap_fetch_uid_flags_list_free(tab);
1860 static int imap_get_messages_flags_list(mailimap * imap,
1861 uint32_t first_index,
1866 struct mailimap_fetch_att * fetch_att;
1867 struct mailimap_fetch_type * fetch_type;
1868 struct mailimap_set * set;
1869 clist * fetch_result;
1872 set = mailimap_set_new_interval(first_index, 0);
1874 res = MAILIMAP_ERROR_MEMORY;
1878 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1879 if (fetch_type == NULL) {
1880 res = MAILIMAP_ERROR_MEMORY;
1884 fetch_att = mailimap_fetch_att_new_flags();
1885 if (fetch_att == NULL) {
1886 res = MAILIMAP_ERROR_MEMORY;
1887 goto free_fetch_type;
1890 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1891 if (r != MAILIMAP_NO_ERROR) {
1892 mailimap_fetch_att_free(fetch_att);
1893 res = MAILIMAP_ERROR_MEMORY;
1894 goto free_fetch_type;
1897 fetch_att = mailimap_fetch_att_new_uid();
1898 if (fetch_att == NULL) {
1899 res = MAILIMAP_ERROR_MEMORY;
1900 goto free_fetch_type;
1903 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1904 if (r != MAILIMAP_NO_ERROR) {
1905 mailimap_fetch_att_free(fetch_att);
1906 res = MAILIMAP_ERROR_MEMORY;
1907 goto free_fetch_type;
1910 mailstream_logger = imap_logger_fetch;
1912 r = mailimap_uid_fetch(imap, set,
1913 fetch_type, &fetch_result);
1915 mailstream_logger = imap_logger_cmd;
1916 mailimap_fetch_type_free(fetch_type);
1917 mailimap_set_free(set);
1919 if (r != MAILIMAP_NO_ERROR) {
1925 r = result_to_uid_flags_list(fetch_result, &env_list);
1926 mailimap_fetch_list_free(fetch_result);
1928 * result = env_list;
1930 return MAILIMAP_NO_ERROR;
1933 mailimap_fetch_type_free(fetch_type);
1935 mailimap_set_free(set);
1942 static void fetch_uid_flags_run(struct etpan_thread_op * op)
1944 struct fetch_uid_param * param;
1945 struct fetch_uid_result * result;
1946 carray * fetch_result;
1950 result = op->result;
1954 fetch_result = NULL;
1955 r = imap_get_messages_flags_list(param->imap, param->first_index,
1959 result->fetch_result = fetch_result;
1960 debug_print("imap fetch_uid run - end %i\n", r);
1963 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
1964 carray ** fetch_result)
1966 struct fetch_uid_param param;
1967 struct fetch_uid_result result;
1970 debug_print("imap fetch_uid - begin\n");
1972 imap = get_imap(folder);
1974 param.first_index = first_index;
1976 mailstream_logger = NULL;
1977 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
1979 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
1981 mailstream_logger = imap_logger_cmd;
1984 if (result.error != MAILIMAP_NO_ERROR)
1985 return result.error;
1987 debug_print("imap fetch_uid - end\n");
1989 * fetch_result = result.fetch_result;
1991 return result.error;
1995 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
1999 for(i = 0 ; i < carray_count(uid_flags_list) ; i ++) {
2002 data = carray_get(uid_flags_list, i);
2005 carray_free(uid_flags_list);
2010 static int imap_fetch(mailimap * imap,
2013 size_t * result_len)
2016 struct mailimap_set * set;
2017 struct mailimap_fetch_att * fetch_att;
2018 struct mailimap_fetch_type * fetch_type;
2019 clist * fetch_result;
2020 struct mailimap_msg_att * msg_att;
2021 struct mailimap_msg_att_item * msg_att_item;
2026 struct mailimap_section * section;
2028 set = mailimap_set_new_single(msg_index);
2030 res = MAILIMAP_ERROR_MEMORY;
2034 section = mailimap_section_new(NULL);
2035 if (section == NULL) {
2036 res = MAILIMAP_ERROR_MEMORY;
2040 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2041 if (fetch_att == NULL) {
2042 mailimap_section_free(section);
2043 res = MAILIMAP_ERROR_MEMORY;
2047 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2048 if (fetch_type == NULL) {
2049 res = MAILIMAP_ERROR_MEMORY;
2050 goto free_fetch_att;
2053 mailstream_logger = imap_logger_fetch;
2055 r = mailimap_uid_fetch(imap, set,
2056 fetch_type, &fetch_result);
2058 mailstream_logger = imap_logger_cmd;
2060 mailimap_fetch_type_free(fetch_type);
2061 mailimap_set_free(set);
2064 case MAILIMAP_NO_ERROR:
2070 if (clist_begin(fetch_result) == NULL) {
2071 mailimap_fetch_list_free(fetch_result);
2072 return MAILIMAP_ERROR_FETCH;
2075 msg_att = clist_begin(fetch_result)->data;
2080 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2081 cur = clist_next(cur)) {
2082 msg_att_item = clist_content(cur);
2084 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2085 if (msg_att_item->att_data.att_static->att_type ==
2086 MAILIMAP_MSG_ATT_BODY_SECTION) {
2087 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2089 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2091 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2096 mailimap_fetch_list_free(fetch_result);
2099 return MAILIMAP_ERROR_FETCH;
2102 * result_len = text_length;
2104 return MAILIMAP_NO_ERROR;
2107 mailimap_fetch_att_free(fetch_att);
2109 mailimap_set_free(set);
2114 static int imap_fetch_header(mailimap * imap,
2117 size_t * result_len)
2120 struct mailimap_set * set;
2121 struct mailimap_fetch_att * fetch_att;
2122 struct mailimap_fetch_type * fetch_type;
2123 clist * fetch_result;
2124 struct mailimap_msg_att * msg_att;
2125 struct mailimap_msg_att_item * msg_att_item;
2130 struct mailimap_section * section;
2132 set = mailimap_set_new_single(msg_index);
2134 res = MAILIMAP_ERROR_MEMORY;
2138 section = mailimap_section_new_header();
2139 if (section == NULL) {
2140 res = MAILIMAP_ERROR_MEMORY;
2144 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2145 if (fetch_att == NULL) {
2146 mailimap_section_free(section);
2147 res = MAILIMAP_ERROR_MEMORY;
2151 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2152 if (fetch_type == NULL) {
2153 res = MAILIMAP_ERROR_MEMORY;
2154 goto free_fetch_att;
2157 mailstream_logger = imap_logger_fetch;
2159 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2161 mailstream_logger = imap_logger_cmd;
2162 mailimap_fetch_type_free(fetch_type);
2163 mailimap_set_free(set);
2166 case MAILIMAP_NO_ERROR:
2172 if (clist_begin(fetch_result) == NULL) {
2173 mailimap_fetch_list_free(fetch_result);
2174 return MAILIMAP_ERROR_FETCH;
2177 msg_att = clist_begin(fetch_result)->data;
2182 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2183 cur = clist_next(cur)) {
2184 msg_att_item = clist_content(cur);
2186 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2187 if (msg_att_item->att_data.att_static->att_type ==
2188 MAILIMAP_MSG_ATT_BODY_SECTION) {
2189 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2190 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2192 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2197 mailimap_fetch_list_free(fetch_result);
2200 return MAILIMAP_ERROR_FETCH;
2203 * result_len = text_length;
2205 return MAILIMAP_NO_ERROR;
2208 mailimap_fetch_att_free(fetch_att);
2210 mailimap_set_free(set);
2217 struct fetch_content_param {
2220 const char * filename;
2224 struct fetch_content_result {
2228 static void fetch_content_run(struct etpan_thread_op * op)
2230 struct fetch_content_param * param;
2231 struct fetch_content_result * result;
2233 size_t content_size;
2239 result = op->result;
2245 if (param->with_body)
2246 r = imap_fetch(param->imap, param->msg_index,
2247 &content, &content_size);
2249 r = imap_fetch_header(param->imap, param->msg_index,
2250 &content, &content_size);
2254 if (r == MAILIMAP_NO_ERROR) {
2255 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2257 result->error = MAILIMAP_ERROR_FETCH;
2261 f = fdopen(fd, "wb");
2263 result->error = MAILIMAP_ERROR_FETCH;
2267 r = fwrite(content, 1, content_size, f);
2274 g_unlink(param->filename);
2285 g_unlink(param->filename);
2288 if (mmap_string_unref(content) != 0)
2292 debug_print("imap fetch_content run - end %i\n", r);
2295 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2297 const char * filename)
2299 struct fetch_content_param param;
2300 struct fetch_content_result result;
2303 debug_print("imap fetch_content - begin\n");
2305 imap = get_imap(folder);
2307 param.msg_index = msg_index;
2308 param.filename = filename;
2309 param.with_body = with_body;
2311 threaded_run(folder, ¶m, &result, fetch_content_run);
2313 if (result.error != MAILIMAP_NO_ERROR)
2314 return result.error;
2316 debug_print("imap fetch_content - end\n");
2318 return result.error;
2323 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
2331 flag_list = att_dyn->att_list;
2332 if (flag_list == NULL)
2335 for(cur = clist_begin(flag_list) ; cur != NULL ;
2336 cur = clist_next(cur)) {
2337 struct mailimap_flag_fetch * flag_fetch;
2339 flag_fetch = clist_content(cur);
2340 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2343 switch (flag_fetch->fl_flag->fl_type) {
2344 case MAILIMAP_FLAG_ANSWERED:
2345 flags |= MSG_REPLIED;
2347 case MAILIMAP_FLAG_FLAGGED:
2348 flags |= MSG_MARKED;
2350 case MAILIMAP_FLAG_DELETED:
2351 flags |= MSG_DELETED;
2353 case MAILIMAP_FLAG_SEEN:
2354 flags &= ~MSG_UNREAD;
2364 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2368 struct mailimap_msg_att_dynamic ** patt_dyn)
2370 clistiter * item_cur;
2374 struct mailimap_msg_att_dynamic * att_dyn;
2381 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2382 item_cur = clist_next(item_cur)) {
2383 struct mailimap_msg_att_item * item;
2385 item = clist_content(item_cur);
2387 switch (item->att_type) {
2388 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2389 switch (item->att_data.att_static->att_type) {
2390 case MAILIMAP_MSG_ATT_UID:
2391 uid = item->att_data.att_static->att_data.att_uid;
2394 case MAILIMAP_MSG_ATT_BODY_SECTION:
2395 if (headers == NULL) {
2396 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2399 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2400 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2405 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2406 if (att_dyn == NULL) {
2407 att_dyn = item->att_data.att_dyn;
2415 if (pheaders != NULL)
2416 * pheaders = headers;
2417 if (pref_size != NULL)
2418 * pref_size = ref_size;
2419 if (patt_dyn != NULL)
2420 * patt_dyn = att_dyn;
2422 return MAIL_NO_ERROR;
2425 static struct imap_fetch_env_info *
2426 fetch_to_env_info(struct mailimap_msg_att * msg_att)
2428 struct imap_fetch_env_info * info;
2432 struct mailimap_msg_att_dynamic * att_dyn;
2434 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2439 info = malloc(sizeof(* info));
2441 info->headers = strdup(headers);
2443 info->flags = imap_flags_to_flags(att_dyn);
2449 imap_fetch_result_to_envelop_list(clist * fetch_result,
2450 carray ** p_env_list)
2457 env_list = carray_new(16);
2459 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2460 cur = clist_next(cur)) {
2461 struct mailimap_msg_att * msg_att;
2462 struct imap_fetch_env_info * env_info;
2464 msg_att = clist_content(cur);
2466 env_info = fetch_to_env_info(msg_att);
2468 return MAILIMAP_ERROR_MEMORY;
2469 carray_add(env_list, env_info, NULL);
2472 * p_env_list = env_list;
2474 return MAIL_NO_ERROR;
2477 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2479 struct mailimap_fetch_att * fetch_att;
2483 struct mailimap_header_list * imap_hdrlist;
2484 struct mailimap_section * section;
2486 hdrlist = clist_new();
2488 header = strdup("Date");
2489 r = clist_append(hdrlist, header);
2490 header = strdup("From");
2491 r = clist_append(hdrlist, header);
2492 header = strdup("To");
2493 r = clist_append(hdrlist, header);
2494 header = strdup("Cc");
2495 r = clist_append(hdrlist, header);
2496 header = strdup("Subject");
2497 r = clist_append(hdrlist, header);
2498 header = strdup("Message-ID");
2499 r = clist_append(hdrlist, header);
2500 header = strdup("References");
2501 r = clist_append(hdrlist, header);
2502 header = strdup("In-Reply-To");
2503 r = clist_append(hdrlist, header);
2505 imap_hdrlist = mailimap_header_list_new(hdrlist);
2506 section = mailimap_section_new_header_fields(imap_hdrlist);
2507 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2508 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2510 return MAIL_NO_ERROR;
2513 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2515 struct mailimap_fetch_att * fetch_att;
2516 struct mailimap_section * section;
2518 section = mailimap_section_new_header();
2519 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2520 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2522 return MAIL_NO_ERROR;
2526 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2527 carray ** p_env_list)
2529 struct mailimap_fetch_att * fetch_att;
2530 struct mailimap_fetch_type * fetch_type;
2532 clist * fetch_result;
2534 carray * env_list = NULL;
2538 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2541 fetch_att = mailimap_fetch_att_new_uid();
2542 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2545 fetch_att = mailimap_fetch_att_new_flags();
2546 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2549 fetch_att = mailimap_fetch_att_new_rfc822_size();
2550 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2554 key.len = sizeof(imap);
2555 r = chash_get(courier_workaround_hash, &key, &value);
2557 r = imap_add_envelope_fetch_att(fetch_type);
2559 r = imap_add_header_fetch_att(fetch_type);
2561 mailstream_logger = imap_logger_fetch;
2563 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2565 mailstream_logger = imap_logger_cmd;
2567 case MAILIMAP_NO_ERROR:
2570 mailimap_fetch_type_free(fetch_type);
2571 debug_print("uid_fetch: %d\n", r);
2575 if (clist_begin(fetch_result) == NULL) {
2576 res = MAILIMAP_ERROR_FETCH;
2577 debug_print("clist_begin = NULL\n");
2581 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2582 mailimap_fetch_list_free(fetch_result);
2584 if (r != MAILIMAP_NO_ERROR) {
2585 mailimap_fetch_type_free(fetch_type);
2586 res = MAILIMAP_ERROR_MEMORY;
2587 debug_print("fetch_result_to_envelop_list: %d\n", res);
2591 mailimap_fetch_type_free(fetch_type);
2593 * p_env_list = env_list;
2595 return MAILIMAP_NO_ERROR;
2601 struct fetch_env_param {
2603 struct mailimap_set * set;
2606 struct fetch_env_result {
2607 carray * fetch_env_result;
2611 static void fetch_env_run(struct etpan_thread_op * op)
2613 struct fetch_env_param * param;
2614 struct fetch_env_result * result;
2619 result = op->result;
2624 r = imap_get_envelopes_list(param->imap, param->set,
2628 result->fetch_env_result = env_list;
2630 debug_print("imap fetch_env run - end %i\n", r);
2633 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2634 carray ** p_env_list)
2636 struct fetch_env_param param;
2637 struct fetch_env_result result;
2640 debug_print("imap fetch_env - begin\n");
2642 imap = get_imap(folder);
2646 threaded_run(folder, ¶m, &result, fetch_env_run);
2648 if (result.error != MAILIMAP_NO_ERROR) {
2654 key.len = sizeof(imap);
2655 r = chash_get(courier_workaround_hash, &key, &value);
2659 chash_set(courier_workaround_hash, &key, &value, NULL);
2661 threaded_run(folder, ¶m, &result, fetch_env_run);
2665 if (result.error != MAILIMAP_NO_ERROR)
2666 return result.error;
2668 debug_print("imap fetch_env - end\n");
2670 * p_env_list = result.fetch_env_result;
2672 return result.error;
2675 void imap_fetch_env_free(carray * env_list)
2679 for(i = 0 ; i < carray_count(env_list) ; i ++) {
2680 struct imap_fetch_env_info * env_info;
2682 env_info = carray_get(env_list, i);
2683 free(env_info->headers);
2686 carray_free(env_list);
2693 struct append_param {
2695 const char * mailbox;
2696 const char * filename;
2697 struct mailimap_flag_list * flag_list;
2700 struct append_result {
2705 static void append_run(struct etpan_thread_op * op)
2707 struct append_param * param;
2708 struct append_result * result;
2712 struct stat stat_buf;
2714 guint32 uid = 0, val = 0;
2717 result = op->result;
2721 r = stat(param->filename, &stat_buf);
2723 result->error = MAILIMAP_ERROR_APPEND;
2726 size = stat_buf.st_size;
2728 fd = open(param->filename, O_RDONLY);
2730 result->error = MAILIMAP_ERROR_APPEND;
2734 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2735 if (data == (void *) MAP_FAILED) {
2737 result->error = MAILIMAP_ERROR_APPEND;
2741 mailstream_logger = imap_logger_append;
2743 r = mailimap_uidplus_append(param->imap, param->mailbox,
2744 param->flag_list, NULL,
2745 data, size, &val, &uid);
2747 mailstream_logger = imap_logger_cmd;
2754 debug_print("imap append run - end %i uid %d\n", r, uid);
2757 int imap_threaded_append(Folder * folder, const char * mailbox,
2758 const char * filename,
2759 struct mailimap_flag_list * flag_list,
2762 struct append_param param;
2763 struct append_result result;
2766 debug_print("imap append - begin\n");
2768 imap = get_imap(folder);
2770 param.mailbox = mailbox;
2771 param.filename = filename;
2772 param.flag_list = flag_list;
2774 threaded_run(folder, ¶m, &result, append_run);
2776 if (result.error != MAILIMAP_NO_ERROR)
2777 return result.error;
2779 debug_print("imap append - end\n");
2783 return result.error;
2789 struct expunge_param {
2793 struct expunge_result {
2797 static void expunge_run(struct etpan_thread_op * op)
2799 struct expunge_param * param;
2800 struct expunge_result * result;
2804 result = op->result;
2808 r = mailimap_expunge(param->imap);
2811 debug_print("imap expunge run - end %i\n", r);
2814 int imap_threaded_expunge(Folder * folder)
2816 struct expunge_param param;
2817 struct expunge_result result;
2819 debug_print("imap expunge - begin\n");
2821 param.imap = get_imap(folder);
2823 threaded_run(folder, ¶m, &result, expunge_run);
2825 debug_print("imap expunge - end\n");
2827 return result.error;
2833 struct mailimap_set * set;
2837 struct copy_result {
2839 struct mailimap_set *source;
2840 struct mailimap_set *dest;
2843 static void copy_run(struct etpan_thread_op * op)
2845 struct copy_param * param;
2846 struct copy_result * result;
2849 struct mailimap_set *source = NULL, *dest = NULL;
2852 result = op->result;
2856 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
2857 &val, &source, &dest);
2861 result->source = source;
2862 result->dest = dest;
2864 result->source = NULL;
2865 result->dest = NULL;
2867 debug_print("imap copy run - end %i\n", r);
2870 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
2871 const char * mb, struct mailimap_set **source,
2872 struct mailimap_set **dest)
2874 struct copy_param param;
2875 struct copy_result result;
2878 debug_print("imap copy - begin\n");
2880 imap = get_imap(folder);
2885 threaded_run(folder, ¶m, &result, copy_run);
2889 if (result.error != MAILIMAP_NO_ERROR)
2890 return result.error;
2892 *source = result.source;
2893 *dest = result.dest;
2895 debug_print("imap copy - end\n");
2897 return result.error;
2902 struct store_param {
2904 struct mailimap_set * set;
2905 struct mailimap_store_att_flags * store_att_flags;
2908 struct store_result {
2912 static void store_run(struct etpan_thread_op * op)
2914 struct store_param * param;
2915 struct store_result * result;
2919 result = op->result;
2923 r = mailimap_uid_store(param->imap, param->set,
2924 param->store_att_flags);
2928 debug_print("imap store run - end %i\n", r);
2931 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
2932 struct mailimap_store_att_flags * store_att_flags)
2934 struct store_param param;
2935 struct store_result result;
2938 debug_print("imap store - begin\n");
2940 imap = get_imap(folder);
2943 param.store_att_flags = store_att_flags;
2945 threaded_run(folder, ¶m, &result, store_run);
2947 if (result.error != MAILIMAP_NO_ERROR)
2948 return result.error;
2950 debug_print("imap store - end\n");
2952 return result.error;
2956 #define ENV_BUFFER_SIZE 512
2958 static void do_exec_command(int fd, const char * command,
2959 const char * servername, uint16_t port)
2963 char env_buffer[ENV_BUFFER_SIZE];
2967 /* Fork again to become a child of init rather than
2968 the etpan client. */
2974 snprintf(env_buffer, ENV_BUFFER_SIZE,
2975 "ETPANSERVER=%s", servername);
2977 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
2981 setenv("ETPANSERVER", servername, 1);
2983 unsetenv("ETPANSERVER");
2988 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
2990 snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
2996 snprintf(porttext, sizeof(porttext), "%d", port);
2997 setenv("ETPANPORT", porttext, 1);
3000 unsetenv("ETPANPORT");
3004 /* Not a lot we can do if there's an error other than bail. */
3005 if (dup2(fd, 0) == -1)
3007 if (dup2(fd, 1) == -1)
3010 /* Should we close stderr and reopen /dev/null? */
3012 maxopen = sysconf(_SC_OPEN_MAX);
3013 for (i=3; i < maxopen; i++)
3017 /* Detach from the controlling tty if we have one. Otherwise,
3018 SSH might do something stupid like trying to use it instead
3019 of running $SSH_ASKPASS. Doh. */
3020 fd = open("/dev/tty", O_RDONLY);
3022 ioctl(fd, TIOCNOTTY, NULL);
3025 #endif /* TIOCNOTTY */
3027 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3029 /* Eep. Shouldn't reach this */
3033 static int subcommand_connect(const char *command,
3034 const char *servername, uint16_t port)
3036 /* SEB unsupported on Windows */
3040 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3045 do_exec_command(sockfds[1], command, servername, port);
3047 else if (childpid == -1) {
3055 /* Reap child, leaving grandchild process to run */
3056 waitpid(childpid, NULL, 0);
3061 static int socket_connect_cmd(mailimap * imap, const char * command,
3062 const char * server, int port)
3068 fd = subcommand_connect(command, server, port);
3070 return MAILIMAP_ERROR_STREAM;
3072 s = mailstream_socket_open(fd);
3075 return MAILIMAP_ERROR_STREAM;
3078 r = mailimap_connect(imap, s);
3079 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3080 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3081 mailstream_close(s);
3083 imap->imap_stream = NULL;
3092 struct connect_cmd_param {
3094 const char * command;
3095 const char * server;
3099 struct connect_cmd_result {
3103 static void connect_cmd_run(struct etpan_thread_op * op)
3106 struct connect_cmd_param * param;
3107 struct connect_cmd_result * result;
3110 result = op->result;
3114 r = socket_connect_cmd(param->imap, param->command,
3115 param->server, param->port);
3121 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3122 const char * server, int port)
3124 struct connect_cmd_param param;
3125 struct connect_cmd_result result;
3128 mailimap * imap, * oldimap;
3130 oldimap = get_imap(folder);
3132 imap = mailimap_new(0, NULL);
3135 debug_print("deleting old imap %p\n", oldimap);
3136 delete_imap(folder, oldimap);
3140 key.len = sizeof(folder);
3143 chash_set(session_hash, &key, &value, NULL);
3146 param.command = command;
3147 param.server = server;
3150 threaded_run(folder, ¶m, &result, connect_cmd_run);
3152 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3154 return result.error;
3157 void imap_threaded_cancel(Folder * folder)
3161 imap = get_imap(folder);
3162 if (imap->imap_stream != NULL)
3163 mailstream_cancel(imap->imap_stream);
3168 void imap_main_init(void)
3171 void imap_main_done(void)
3174 void imap_main_set_timeout(int sec)
3178 void imap_threaded_cancel(Folder * folder);