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>
40 #include "etpan-thread-manager.h"
42 #include "mainwindow.h"
44 #include "ssl_certificate.h"
46 #include "remotefolder.h"
48 #define DISABLE_LOG_DURING_LOGIN
50 static struct etpan_thread_manager * thread_manager = NULL;
51 static chash * courier_workaround_hash = NULL;
52 static chash * imap_hash = NULL;
53 static chash * session_hash = NULL;
54 static guint thread_manager_signal = 0;
55 static GIOChannel * io_channel = NULL;
57 static void delete_imap(Folder *folder, mailimap *imap)
63 key.len = sizeof(folder);
66 chash_delete(session_hash, &key, NULL);
69 key.len = sizeof(imap);
70 chash_delete(courier_workaround_hash, &key, NULL);
71 if (imap && imap->imap_stream) {
72 /* we don't want libetpan to logout */
73 mailstream_close(imap->imap_stream);
74 imap->imap_stream = NULL;
76 debug_print("removing mailimap %p\n", imap);
80 static gboolean thread_manager_event(GIOChannel * source,
81 GIOCondition condition,
84 etpan_thread_manager_loop(thread_manager);
89 static void imap_logger_noop(int direction, const char * str, size_t size)
94 static void imap_logger_cmd(int direction, const char * str, size_t size)
101 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
104 buf = malloc(size+1);
105 memset(buf, 0, size+1);
106 strncpy(buf, str, size);
109 if (!strncmp(buf, "<<<<<<<", 7)
110 || !strncmp(buf, ">>>>>>>", 7)) {
114 while (strstr(buf, "\r"))
115 *strstr(buf, "\r") = ' ';
116 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
117 buf[strlen(buf)-1] = '\0';
119 lines = g_strsplit(buf, "\n", -1);
121 while (lines[i] && *lines[i]) {
122 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
129 static void imap_logger_fetch(int direction, const char * str, size_t size)
135 if (size > 128 && !direction) {
136 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
140 buf = malloc(size+1);
141 memset(buf, 0, size+1);
142 strncpy(buf, str, size);
144 if (!strncmp(buf, "<<<<<<<", 7)
145 || !strncmp(buf, ">>>>>>>", 7)) {
149 while (strstr(buf, "\r"))
150 *strstr(buf, "\r") = ' ';
151 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
152 buf[strlen(buf)-1] = '\0';
154 lines = g_strsplit(buf, "\n", -1);
156 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
157 while (lines[i] && *lines[i]) {
158 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
162 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
168 static void imap_logger_uid(int direction, const char * str, size_t size)
175 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
178 buf = malloc(size+1);
179 memset(buf, 0, size+1);
180 strncpy(buf, str, size);
182 if (!strncmp(buf, "<<<<<<<", 7)
183 || !strncmp(buf, ">>>>>>>", 7)) {
187 while (strstr(buf, "\r"))
188 *strstr(buf, "\r") = ' ';
189 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
190 buf[strlen(buf)-1] = '\0';
192 lines = g_strsplit(buf, "\n", -1);
194 while (lines[i] && *lines[i]) {
195 int llen = strlen(lines[i]);
197 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
200 strncpy2(tmp, lines[i], 63);
201 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
210 static void imap_logger_append(int direction, const char * str, size_t size)
217 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
219 } else if (direction == 0 && size > 64) {
220 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
223 buf = malloc(size+1);
224 memset(buf, 0, size+1);
225 strncpy(buf, str, size);
227 if (!strncmp(buf, "<<<<<<<", 7)
228 || !strncmp(buf, ">>>>>>>", 7)) {
232 while (strstr(buf, "\r"))
233 *strstr(buf, "\r") = ' ';
234 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
235 buf[strlen(buf)-1] = '\0';
237 lines = g_strsplit(buf, "\n", -1);
239 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
240 while (lines[i] && *lines[i]) {
241 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
245 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
251 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
252 gboolean etpan_skip_ssl_cert_check = FALSE;
253 extern void mailsasl_ref(void);
255 void imap_main_init(gboolean skip_ssl_cert_check)
257 int fd_thread_manager;
259 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
260 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
261 mailstream_network_delay.tv_usec = 0;
263 mailstream_debug = 1;
264 mailstream_logger = imap_logger_cmd;
267 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
268 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
269 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
271 thread_manager = etpan_thread_manager_new();
273 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
275 io_channel = g_io_channel_unix_new(fd_thread_manager);
277 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
278 thread_manager_event,
283 void imap_main_set_timeout(int sec)
285 mailstream_network_delay.tv_sec = sec;
286 mailstream_network_delay.tv_usec = 0;
289 void imap_main_done(void)
291 imap_disconnect_all();
292 etpan_thread_manager_stop(thread_manager);
293 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
296 etpan_thread_manager_join(thread_manager);
298 g_source_remove(thread_manager_signal);
299 g_io_channel_unref(io_channel);
301 etpan_thread_manager_free(thread_manager);
303 chash_free(courier_workaround_hash);
304 chash_free(session_hash);
305 chash_free(imap_hash);
308 void imap_init(Folder * folder)
310 struct etpan_thread * thread;
314 thread = etpan_thread_manager_get_thread(thread_manager);
317 key.len = sizeof(folder);
321 chash_set(imap_hash, &key, &value, NULL);
324 void imap_done(Folder * folder)
326 struct etpan_thread * thread;
332 key.len = sizeof(folder);
334 r = chash_get(imap_hash, &key, &value);
340 etpan_thread_unbind(thread);
342 chash_delete(imap_hash, &key, NULL);
344 debug_print("remove thread");
347 static struct etpan_thread * get_thread(Folder * folder)
349 struct etpan_thread * thread;
354 key.len = sizeof(folder);
356 chash_get(imap_hash, &key, &value);
362 static mailimap * get_imap(Folder * folder)
370 key.len = sizeof(folder);
372 r = chash_get(session_hash, &key, &value);
377 debug_print("found imap %p\n", imap);
381 static gboolean cb_show_error(gpointer data)
383 mainwindow_show_error();
387 static void generic_cb(int cancelled, void * result, void * callback_data)
389 struct etpan_thread_op * op;
391 op = (struct etpan_thread_op *) callback_data;
393 debug_print("generic_cb\n");
394 if (op->imap && op->imap->imap_response_info &&
395 op->imap->imap_response_info->rsp_alert) {
396 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
397 op->imap->imap_response_info->rsp_alert);
398 g_timeout_add(10, cb_show_error, NULL);
403 static void threaded_run(Folder * folder, void * param, void * result,
404 void (* func)(struct etpan_thread_op * ))
406 struct etpan_thread_op * op;
407 struct etpan_thread * thread;
409 imap_folder_ref(folder);
411 op = etpan_thread_op_new();
413 op->imap = get_imap(folder);
419 op->callback = generic_cb;
420 op->callback_data = op;
425 thread = get_thread(folder);
426 etpan_thread_op_schedule(thread, op);
428 while (!op->finished) {
429 gtk_main_iteration();
432 etpan_thread_op_free(op);
434 imap_folder_unref(folder);
440 struct connect_param {
442 PrefsAccount *account;
447 struct connect_result {
451 #define CHECK_IMAP() { \
452 if (!param->imap) { \
453 result->error = MAILIMAP_ERROR_BAD_STATE; \
458 static void connect_run(struct etpan_thread_op * op)
461 struct connect_param * param;
462 struct connect_result * result;
469 r = mailimap_socket_connect(param->imap,
470 param->server, param->port);
476 int imap_threaded_connect(Folder * folder, const char * server, int port)
478 struct connect_param param;
479 struct connect_result result;
482 mailimap * imap, * oldimap;
484 oldimap = get_imap(folder);
486 imap = mailimap_new(0, NULL);
489 debug_print("deleting old imap %p\n", oldimap);
490 delete_imap(folder, oldimap);
494 key.len = sizeof(folder);
497 chash_set(session_hash, &key, &value, NULL);
500 param.server = server;
504 threaded_run(folder, ¶m, &result, connect_run);
506 debug_print("connect ok %i with imap %p\n", result.error, imap);
511 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
514 struct connect_param *param = (struct connect_param *)data;
517 if (certificate == NULL || len < 0) {
518 g_warning("no cert presented.\n");
521 cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
523 g_warning("IMAP: can't get cert\n");
525 } else if (ssl_certificate_check(cert, NULL,
526 (gchar *)param->server, (gushort)param->port) == TRUE) {
534 struct connect_param *param = (struct connect_param *)data;
535 gnutls_x509_crt cert = NULL;
538 if (certificate == NULL || len < 0) {
539 g_warning("no cert presented.\n");
543 tmp.data = malloc(len);
544 memcpy(tmp.data, certificate, len);
546 gnutls_x509_crt_init(&cert);
547 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
548 g_warning("IMAP: can't get cert\n");
550 } else if (ssl_certificate_check(cert, (guint)-1, NULL,
551 (gchar *)param->server, (gushort)param->port) == TRUE) {
552 gnutls_x509_crt_deinit(cert);
555 gnutls_x509_crt_deinit(cert);
562 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
564 #if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
565 PrefsAccount *account = (PrefsAccount *)data;
566 const gchar *cert_path = NULL;
567 const gchar *password = NULL;
570 EVP_PKEY *pkey = NULL;
572 gnutls_x509_crt x509 = NULL;
573 gnutls_x509_privkey pkey = NULL;
576 if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
577 cert_path = account->in_ssl_client_cert_file;
578 if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
579 password = account->in_ssl_client_cert_pass;
581 if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
582 mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
583 debug_print("Impossible to set the client certificate.\n");
584 x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
585 pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
586 if (!(x509 && pkey)) {
587 /* try pkcs12 format */
588 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
591 unsigned char *x509_der = NULL, *pkey_der = NULL;
592 size_t x509_len, pkey_len;
595 x509_len = (size_t)i2d_X509(x509, &x509_der);
596 pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
598 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
599 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
601 if (x509_len > 0 && pkey_len > 0) {
602 if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
603 mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
604 log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
609 gnutls_x509_crt_deinit(x509);
610 gnutls_x509_privkey_deinit(pkey);
616 static void connect_ssl_run(struct etpan_thread_op * op)
619 struct connect_param * param;
620 struct connect_result * result;
627 r = mailimap_ssl_connect_with_callback(param->imap,
628 param->server, param->port,
629 connect_ssl_context_cb, param->account);
633 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
635 struct connect_param param;
636 struct connect_result result;
639 mailimap * imap, * oldimap;
640 unsigned char *certificate = NULL;
643 oldimap = get_imap(folder);
645 imap = mailimap_new(0, NULL);
648 debug_print("deleting old imap %p\n", oldimap);
649 delete_imap(folder, oldimap);
653 key.len = sizeof(folder);
656 chash_set(session_hash, &key, &value, NULL);
659 param.server = server;
661 param.account = folder->account;
664 threaded_run(folder, ¶m, &result, connect_ssl_run);
666 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
667 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
668 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
669 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
674 debug_print("connect %d with imap %p\n", result.error, imap);
685 struct mailimap_capability_data *caps;
688 static void capability_run(struct etpan_thread_op * op)
691 struct capa_param * param;
692 struct capa_result * result;
693 struct mailimap_capability_data *caps;
700 r = mailimap_capability(param->imap, &caps);
703 result->caps = (r == 0 ? caps : NULL);
707 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
709 struct capa_param param;
710 struct capa_result result;
713 imap = get_imap(folder);
717 threaded_run(folder, ¶m, &result, capability_run);
719 debug_print("capa %d\n", result.error);
728 struct disconnect_param {
732 struct disconnect_result {
736 static void disconnect_run(struct etpan_thread_op * op)
739 struct disconnect_param * param;
740 struct disconnect_result * result;
747 r = mailimap_logout(param->imap);
752 void imap_threaded_disconnect(Folder * folder)
754 struct connect_param param;
755 struct connect_result result;
758 imap = get_imap(folder);
760 debug_print("was disconnected\n");
766 threaded_run(folder, ¶m, &result, disconnect_run);
768 if (imap == get_imap(folder)) {
769 debug_print("deleting old imap %p\n", imap);
770 delete_imap(folder, imap);
772 debug_print("imap already deleted %p\n", imap);
775 debug_print("disconnect ok\n");
782 const char * wildcard;
791 static void list_run(struct etpan_thread_op * op)
793 struct list_param * param;
794 struct list_result * result;
805 if (param->base == NULL || param->wildcard == NULL) {
808 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
812 r = mailimap_lsub(param->imap, param->base,
813 param->wildcard, &list);
815 r = mailimap_list(param->imap, param->base,
816 param->wildcard, &list);
819 debug_print("imap list run - end\n");
822 int imap_threaded_list(Folder * folder, const char * base,
823 const char * wildcard,
826 struct list_param param;
827 struct list_result result;
829 debug_print("imap list - begin\n");
831 param.imap = get_imap(folder);
833 param.wildcard = wildcard;
834 param.sub_only = FALSE;
836 threaded_run(folder, ¶m, &result, list_run);
838 * p_result = result.list;
840 debug_print("imap list - end %p\n", result.list);
845 int imap_threaded_lsub(Folder * folder, const char * base,
846 const char * wildcard,
849 struct list_param param;
850 struct list_result result;
852 debug_print("imap lsub - begin\n");
854 param.imap = get_imap(folder);
856 param.wildcard = wildcard;
857 param.sub_only = TRUE;
859 threaded_run(folder, ¶m, &result, list_run);
861 * p_result = result.list;
863 debug_print("imap lsub - end %p\n", result.list);
868 struct subscribe_param {
874 struct subscribe_result {
878 static void subscribe_run(struct etpan_thread_op * op)
880 struct subscribe_param * param;
881 struct subscribe_result * result;
889 if (param->mb == NULL) {
891 debug_print("no mb\n");
894 if (param->subscribe)
895 r = mailimap_subscribe(param->imap, param->mb);
897 r = mailimap_unsubscribe(param->imap, param->mb);
899 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
902 int imap_threaded_subscribe(Folder * folder, const char * mb,
905 struct subscribe_param param;
906 struct subscribe_result result;
908 debug_print("imap list - begin\n");
910 param.imap = get_imap(folder);
912 param.subscribe = subscribe;
914 threaded_run(folder, ¶m, &result, subscribe_run);
922 const char * password;
927 struct login_result {
931 static void login_run(struct etpan_thread_op * op)
933 struct login_param * param;
934 struct login_result * result;
936 #ifdef DISABLE_LOG_DURING_LOGIN
945 #ifdef DISABLE_LOG_DURING_LOGIN
946 old_debug = mailstream_debug;
947 mailstream_debug = 0;
949 if (!strcmp(param->type, "LOGIN"))
950 r = mailimap_login(param->imap,
951 param->login, param->password);
952 else if (!strcmp(param->type, "GSSAPI"))
953 r = mailimap_authenticate(param->imap,
954 param->type, param->server, NULL, NULL,
955 param->login, param->login,
956 param->password, NULL);
958 r = mailimap_authenticate(param->imap,
959 param->type, NULL, NULL, NULL,
960 param->login, param->login,
961 param->password, NULL);
962 #ifdef DISABLE_LOG_DURING_LOGIN
963 mailstream_debug = old_debug;
967 if (param->imap->imap_response)
968 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
969 debug_print("imap login run - end %i\n", r);
972 int imap_threaded_login(Folder * folder,
973 const char * login, const char * password,
976 struct login_param param;
977 struct login_result result;
979 debug_print("imap login - begin\n");
981 param.imap = get_imap(folder);
983 param.password = password;
985 if (folder && folder->account)
986 param.server = folder->account->recv_server;
990 threaded_run(folder, ¶m, &result, login_run);
992 debug_print("imap login - end\n");
998 struct status_param {
1001 struct mailimap_status_att_list * status_att_list;
1004 struct status_result {
1006 struct mailimap_mailbox_data_status * data_status;
1009 static void status_run(struct etpan_thread_op * op)
1011 struct status_param * param;
1012 struct status_result * result;
1016 result = op->result;
1020 r = mailimap_status(param->imap, param->mb,
1021 param->status_att_list,
1022 &result->data_status);
1025 debug_print("imap status run - end %i\n", r);
1028 int imap_threaded_status(Folder * folder, const char * mb,
1029 struct mailimap_mailbox_data_status ** data_status,
1032 struct status_param param;
1033 struct status_result result;
1034 struct mailimap_status_att_list * status_att_list;
1036 debug_print("imap status - begin\n");
1038 status_att_list = mailimap_status_att_list_new_empty();
1039 if (mask & 1 << 0) {
1040 mailimap_status_att_list_add(status_att_list,
1041 MAILIMAP_STATUS_ATT_MESSAGES);
1043 if (mask & 1 << 1) {
1044 mailimap_status_att_list_add(status_att_list,
1045 MAILIMAP_STATUS_ATT_RECENT);
1047 if (mask & 1 << 2) {
1048 mailimap_status_att_list_add(status_att_list,
1049 MAILIMAP_STATUS_ATT_UIDNEXT);
1051 if (mask & 1 << 3) {
1052 mailimap_status_att_list_add(status_att_list,
1053 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1055 if (mask & 1 << 4) {
1056 mailimap_status_att_list_add(status_att_list,
1057 MAILIMAP_STATUS_ATT_UNSEEN);
1059 param.imap = get_imap(folder);
1061 param.status_att_list = status_att_list;
1063 threaded_run(folder, ¶m, &result, status_run);
1065 debug_print("imap status - end\n");
1067 * data_status = result.data_status;
1069 mailimap_status_att_list_free(status_att_list);
1071 return result.error;
1080 struct noop_result {
1084 static void noop_run(struct etpan_thread_op * op)
1086 struct noop_param * param;
1087 struct noop_result * result;
1091 result = op->result;
1095 r = mailimap_noop(param->imap);
1098 debug_print("imap noop run - end %i\n", r);
1101 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1102 unsigned int *p_recent,
1103 unsigned int *p_expunge,
1104 unsigned int *p_unseen,
1105 unsigned int *p_uidnext,
1106 unsigned int *p_uidval)
1108 struct noop_param param;
1109 struct noop_result result;
1112 debug_print("imap noop - begin\n");
1114 imap = get_imap(folder);
1117 threaded_run(folder, ¶m, &result, noop_run);
1119 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1120 * p_exists = imap->imap_selection_info->sel_exists;
1121 * p_recent = imap->imap_selection_info->sel_recent;
1122 * p_unseen = imap->imap_selection_info->sel_unseen;
1123 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1124 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1132 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1133 imap->imap_response_info->rsp_expunged != NULL) {
1134 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1138 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1139 *p_exists, *p_recent, *p_expunge, *p_unseen,
1140 *p_uidnext, *p_uidval);
1142 return result.error;
1146 struct starttls_result {
1150 static void starttls_run(struct etpan_thread_op * op)
1152 struct connect_param * param;
1153 struct starttls_result * result;
1157 result = op->result;
1161 r = mailimap_starttls(param->imap);
1164 debug_print("imap starttls run - end %i\n", r);
1167 mailimap *imap = param->imap;
1168 mailstream_low *plain_low = NULL;
1169 mailstream_low *tls_low = NULL;
1172 plain_low = mailstream_get_low(imap->imap_stream);
1173 fd = mailstream_low_get_fd(plain_low);
1175 debug_print("imap starttls run - can't get fd\n");
1176 result->error = MAILIMAP_ERROR_STREAM;
1180 tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
1181 if (tls_low == NULL) {
1182 debug_print("imap starttls run - can't tls_open\n");
1183 result->error = MAILIMAP_ERROR_STREAM;
1186 mailstream_low_free(plain_low);
1187 mailstream_set_low(imap->imap_stream, tls_low);
1191 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1193 struct connect_param param;
1194 struct starttls_result result;
1196 unsigned char *certificate;
1198 debug_print("imap starttls - begin\n");
1200 param.imap = get_imap(folder);
1201 param.server = host;
1203 param.account = folder->account;
1205 threaded_run(folder, ¶m, &result, starttls_run);
1207 debug_print("imap starttls - end\n");
1209 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1210 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1211 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1212 result.error = MAILIMAP_ERROR_STREAM;
1216 return result.error;
1221 struct create_param {
1226 struct create_result {
1230 static void create_run(struct etpan_thread_op * op)
1232 struct create_param * param;
1233 struct create_result * result;
1237 result = op->result;
1241 r = mailimap_create(param->imap, param->mb);
1244 debug_print("imap create run - end %i\n", r);
1247 int imap_threaded_create(Folder * folder, const char * mb)
1249 struct create_param param;
1250 struct create_result result;
1252 debug_print("imap create - begin\n");
1254 param.imap = get_imap(folder);
1257 threaded_run(folder, ¶m, &result, create_run);
1259 debug_print("imap create - end\n");
1261 return result.error;
1267 struct rename_param {
1270 const char * new_name;
1273 struct rename_result {
1277 static void rename_run(struct etpan_thread_op * op)
1279 struct rename_param * param;
1280 struct rename_result * result;
1284 result = op->result;
1288 r = mailimap_rename(param->imap, param->mb, param->new_name);
1291 debug_print("imap rename run - end %i\n", r);
1294 int imap_threaded_rename(Folder * folder,
1295 const char * mb, const char * new_name)
1297 struct rename_param param;
1298 struct rename_result result;
1300 debug_print("imap rename - begin\n");
1302 param.imap = get_imap(folder);
1304 param.new_name = new_name;
1306 threaded_run(folder, ¶m, &result, rename_run);
1308 debug_print("imap rename - end\n");
1310 return result.error;
1316 struct delete_param {
1321 struct delete_result {
1325 static void delete_run(struct etpan_thread_op * op)
1327 struct delete_param * param;
1328 struct delete_result * result;
1332 result = op->result;
1336 r = mailimap_delete(param->imap, param->mb);
1339 debug_print("imap delete run - end %i\n", r);
1342 int imap_threaded_delete(Folder * folder, const char * mb)
1344 struct delete_param param;
1345 struct delete_result result;
1347 debug_print("imap delete - begin\n");
1349 param.imap = get_imap(folder);
1352 threaded_run(folder, ¶m, &result, delete_run);
1354 debug_print("imap delete - end\n");
1356 return result.error;
1361 struct select_param {
1366 struct select_result {
1370 static void select_run(struct etpan_thread_op * op)
1372 struct select_param * param;
1373 struct select_result * result;
1377 result = op->result;
1381 r = mailimap_select(param->imap, param->mb);
1384 debug_print("imap select run - end %i\n", r);
1387 int imap_threaded_select(Folder * folder, const char * mb,
1388 gint * exists, gint * recent, gint * unseen,
1389 guint32 * uid_validity,gint *can_create_flags,
1392 struct select_param param;
1393 struct select_result result;
1396 debug_print("imap select - begin\n");
1398 imap = get_imap(folder);
1402 threaded_run(folder, ¶m, &result, select_run);
1404 if (result.error != MAILIMAP_NO_ERROR)
1405 return result.error;
1407 if (!imap || imap->imap_selection_info == NULL)
1408 return MAILIMAP_ERROR_PARSE;
1410 * exists = imap->imap_selection_info->sel_exists;
1411 * recent = imap->imap_selection_info->sel_recent;
1412 * unseen = imap->imap_selection_info->sel_unseen;
1413 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1414 * can_create_flags = FALSE;
1416 if (imap->imap_selection_info->sel_perm_flags) {
1417 GSList *t_flags = NULL;
1419 clist_begin(imap->imap_selection_info->sel_perm_flags);
1420 for (; cur; cur = clist_next(cur)) {
1421 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1422 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1423 *can_create_flags = TRUE;
1424 else if (flag->fl_flag &&
1425 flag->fl_flag->fl_type == 6 &&
1426 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1427 *can_create_flags = TRUE;
1429 MsgPermFlags c_flag = 0;
1430 switch (flag->fl_flag->fl_type) {
1431 case MAILIMAP_FLAG_ANSWERED:
1432 c_flag = IMAP_FLAG_ANSWERED;
1434 case MAILIMAP_FLAG_FLAGGED:
1435 c_flag = IMAP_FLAG_FLAGGED;
1437 case MAILIMAP_FLAG_DELETED:
1438 c_flag = IMAP_FLAG_DELETED;
1440 case MAILIMAP_FLAG_DRAFT:
1441 c_flag = IMAP_FLAG_DRAFT;
1443 case MAILIMAP_FLAG_SEEN:
1444 c_flag = IMAP_FLAG_SEEN;
1446 case MAILIMAP_FLAG_KEYWORD:
1447 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
1448 c_flag = IMAP_FLAG_FORWARDED;
1449 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "Junk"))
1450 c_flag = IMAP_FLAG_SPAM;
1451 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NonJunk") ||
1452 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NoJunk") ||
1453 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NotJunk"))
1454 c_flag = IMAP_FLAG_HAM;
1460 t_flags = g_slist_prepend(t_flags,
1461 GUINT_TO_POINTER(c_flag));
1466 *ok_flags = t_flags;
1468 debug_print("imap select - end\n");
1470 return result.error;
1473 static void close_run(struct etpan_thread_op * op)
1475 struct select_param * param;
1476 struct select_result * result;
1480 result = op->result;
1484 r = mailimap_close(param->imap);
1487 debug_print("imap close run - end %i\n", r);
1490 int imap_threaded_close(Folder * folder)
1492 struct select_param param;
1493 struct select_result result;
1496 debug_print("imap close - begin\n");
1498 imap = get_imap(folder);
1501 threaded_run(folder, ¶m, &result, close_run);
1503 if (result.error != MAILIMAP_NO_ERROR)
1504 return result.error;
1506 debug_print("imap close - end\n");
1508 return result.error;
1511 struct examine_param {
1516 struct examine_result {
1520 static void examine_run(struct etpan_thread_op * op)
1522 struct examine_param * param;
1523 struct examine_result * result;
1527 result = op->result;
1531 r = mailimap_examine(param->imap, param->mb);
1534 debug_print("imap examine run - end %i\n", r);
1537 int imap_threaded_examine(Folder * folder, const char * mb,
1538 gint * exists, gint * recent, gint * unseen,
1539 guint32 * uid_validity)
1541 struct examine_param param;
1542 struct examine_result result;
1545 debug_print("imap examine - begin\n");
1547 imap = get_imap(folder);
1551 threaded_run(folder, ¶m, &result, examine_run);
1553 if (result.error != MAILIMAP_NO_ERROR)
1554 return result.error;
1556 if (!imap || imap->imap_selection_info == NULL)
1557 return MAILIMAP_ERROR_PARSE;
1559 * exists = imap->imap_selection_info->sel_exists;
1560 * recent = imap->imap_selection_info->sel_recent;
1561 * unseen = imap->imap_selection_info->sel_unseen;
1562 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1564 debug_print("imap examine - end\n");
1566 return result.error;
1572 struct search_param {
1575 struct mailimap_set * set;
1578 struct search_result {
1580 clist * search_result;
1583 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1585 return mailimap_set_item_new(orig->set_first, orig->set_last);
1588 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1590 clist *list = orig ? orig->set_list : NULL;
1591 clist *newlist = clist_new();
1596 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1597 clist_append(newlist,
1598 sc_mailimap_set_item_copy(
1599 (struct mailimap_set_item *)clist_content(cur)));
1600 return mailimap_set_new(newlist);
1603 static void search_run(struct etpan_thread_op * op)
1605 struct search_param * param;
1606 struct search_result * result;
1608 struct mailimap_search_key * key = NULL;
1609 struct mailimap_search_key * uid_key = NULL;
1610 struct mailimap_search_key * search_type_key;
1611 clist * search_result;
1614 result = op->result;
1618 /* we copy the mailimap_set because freeing the key is recursive */
1619 if (param->set != NULL) {
1620 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1621 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1622 uid_key = mailimap_search_key_new_all();
1624 search_type_key = NULL;
1625 switch (param->type) {
1626 case IMAP_SEARCH_TYPE_SIMPLE:
1627 search_type_key = NULL;
1630 case IMAP_SEARCH_TYPE_SEEN:
1631 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1632 NULL, NULL, NULL, NULL, NULL,
1633 NULL, NULL, NULL, NULL, NULL,
1634 NULL, NULL, NULL, NULL, 0,
1635 NULL, NULL, NULL, NULL, NULL,
1636 NULL, 0, NULL, NULL, NULL);
1639 case IMAP_SEARCH_TYPE_UNSEEN:
1640 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1641 NULL, NULL, NULL, NULL, NULL,
1642 NULL, NULL, NULL, NULL, NULL,
1643 NULL, NULL, NULL, NULL, 0,
1644 NULL, NULL, NULL, NULL, NULL,
1645 NULL, 0, NULL, NULL, NULL);
1648 case IMAP_SEARCH_TYPE_ANSWERED:
1649 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1650 NULL, NULL, NULL, NULL, NULL,
1651 NULL, NULL, NULL, NULL, NULL,
1652 NULL, NULL, NULL, NULL, 0,
1653 NULL, NULL, NULL, NULL, NULL,
1654 NULL, 0, NULL, NULL, NULL);
1657 case IMAP_SEARCH_TYPE_FLAGGED:
1658 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1659 NULL, NULL, NULL, NULL, NULL,
1660 NULL, NULL, NULL, NULL, NULL,
1661 NULL, NULL, NULL, NULL, 0,
1662 NULL, NULL, NULL, NULL, NULL,
1663 NULL, 0, NULL, NULL, NULL);
1665 case IMAP_SEARCH_TYPE_DELETED:
1666 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
1667 NULL, NULL, NULL, NULL, NULL,
1668 NULL, NULL, NULL, NULL, NULL,
1669 NULL, NULL, NULL, NULL, 0,
1670 NULL, NULL, NULL, NULL, NULL,
1671 NULL, 0, NULL, NULL, NULL);
1673 case IMAP_SEARCH_TYPE_FORWARDED:
1674 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1675 NULL, NULL, NULL, NULL, NULL,
1676 strdup("$Forwarded"), NULL, NULL, NULL, NULL,
1677 NULL, NULL, NULL, NULL, 0,
1678 NULL, NULL, NULL, NULL, NULL,
1679 NULL, 0, NULL, NULL, NULL);
1681 case IMAP_SEARCH_TYPE_SPAM:
1682 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1683 NULL, NULL, NULL, NULL, NULL,
1684 strdup("Junk"), NULL, NULL, NULL, NULL,
1685 NULL, NULL, NULL, NULL, 0,
1686 NULL, NULL, NULL, NULL, NULL,
1687 NULL, 0, NULL, NULL, NULL);
1691 if (search_type_key != NULL) {
1692 if (param->set != NULL) {
1693 key = mailimap_search_key_new_multiple_empty();
1694 mailimap_search_key_multiple_add(key, search_type_key);
1695 mailimap_search_key_multiple_add(key, uid_key);
1697 key = search_type_key;
1699 } else if (uid_key != NULL) {
1704 g_warning("no key!");
1705 result = op->result;
1707 result->search_result = NULL;
1709 mailstream_logger = imap_logger_uid;
1711 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1713 mailstream_logger = imap_logger_cmd;
1715 /* free the key (with the imapset) */
1716 mailimap_search_key_free(key);
1719 result->search_result = search_result;
1721 debug_print("imap search run - end %i\n", result->error);
1724 int imap_threaded_search(Folder * folder, int search_type,
1725 struct mailimap_set * set, clist ** search_result)
1727 struct search_param param;
1728 struct search_result result;
1731 debug_print("imap search - begin\n");
1733 imap = get_imap(folder);
1736 param.type = search_type;
1738 threaded_run(folder, ¶m, &result, search_run);
1740 if (result.error != MAILIMAP_NO_ERROR)
1741 return result.error;
1743 debug_print("imap search - end\n");
1745 * search_result = result.search_result;
1747 return result.error;
1752 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1756 struct mailimap_msg_att_dynamic ** patt_dyn);
1759 result_to_uid_list(clist * fetch_result, carray ** result)
1766 tab = carray_new(128);
1768 res = MAILIMAP_ERROR_MEMORY;
1772 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1773 cur = clist_next(cur)) {
1774 struct mailimap_msg_att * msg_att;
1778 msg_att = clist_content(cur);
1781 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1783 puid = malloc(sizeof(* puid));
1785 res = MAILIMAP_ERROR_MEMORY;
1790 r = carray_add(tab, puid, NULL);
1793 res = MAILIMAP_ERROR_MEMORY;
1800 return MAILIMAP_NO_ERROR;
1803 imap_fetch_uid_list_free(tab);
1808 static int imap_get_messages_list(mailimap * imap,
1809 uint32_t first_index,
1814 struct mailimap_fetch_att * fetch_att;
1815 struct mailimap_fetch_type * fetch_type;
1816 struct mailimap_set * set;
1817 clist * fetch_result;
1820 set = mailimap_set_new_interval(first_index, 0);
1822 res = MAILIMAP_ERROR_MEMORY;
1826 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1827 if (fetch_type == NULL) {
1828 res = MAILIMAP_ERROR_MEMORY;
1832 fetch_att = mailimap_fetch_att_new_uid();
1833 if (fetch_att == NULL) {
1834 res = MAILIMAP_ERROR_MEMORY;
1835 goto free_fetch_type;
1838 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1839 if (r != MAILIMAP_NO_ERROR) {
1840 mailimap_fetch_att_free(fetch_att);
1841 res = MAILIMAP_ERROR_MEMORY;
1842 goto free_fetch_type;
1845 mailstream_logger = imap_logger_fetch;
1847 r = mailimap_uid_fetch(imap, set,
1848 fetch_type, &fetch_result);
1850 mailstream_logger = imap_logger_cmd;
1851 mailimap_fetch_type_free(fetch_type);
1852 mailimap_set_free(set);
1854 if (r != MAILIMAP_NO_ERROR) {
1860 r = result_to_uid_list(fetch_result, &env_list);
1861 mailimap_fetch_list_free(fetch_result);
1863 * result = env_list;
1865 return MAILIMAP_NO_ERROR;
1868 mailimap_fetch_type_free(fetch_type);
1870 mailimap_set_free(set);
1878 struct fetch_uid_param {
1880 uint32_t first_index;
1883 struct fetch_uid_result {
1885 carray * fetch_result;
1888 static void fetch_uid_run(struct etpan_thread_op * op)
1890 struct fetch_uid_param * param;
1891 struct fetch_uid_result * result;
1892 carray * fetch_result;
1896 result = op->result;
1900 fetch_result = NULL;
1901 mailstream_logger = imap_logger_noop;
1902 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1904 r = imap_get_messages_list(param->imap, param->first_index,
1907 mailstream_logger = imap_logger_cmd;
1910 result->fetch_result = fetch_result;
1911 debug_print("imap fetch_uid run - end %i\n", r);
1914 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1915 carray ** fetch_result)
1917 struct fetch_uid_param param;
1918 struct fetch_uid_result result;
1921 debug_print("imap fetch_uid - begin\n");
1923 imap = get_imap(folder);
1925 param.first_index = first_index;
1927 threaded_run(folder, ¶m, &result, fetch_uid_run);
1929 if (result.error != MAILIMAP_NO_ERROR)
1930 return result.error;
1932 debug_print("imap fetch_uid - end\n");
1934 * fetch_result = result.fetch_result;
1936 return result.error;
1940 void imap_fetch_uid_list_free(carray * uid_list)
1944 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1947 puid = carray_get(uid_list, i);
1950 carray_free(uid_list);
1956 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1959 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1965 GSList *tags = NULL;
1967 tab = carray_new(128);
1969 res = MAILIMAP_ERROR_MEMORY;
1973 for(cur = clist_begin(fetch_result) ; cur != NULL ;
1974 cur = clist_next(cur)) {
1975 struct mailimap_msg_att * msg_att;
1978 struct mailimap_msg_att_dynamic * att_dyn;
1984 msg_att = clist_content(cur);
1988 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1991 if (att_dyn == NULL)
1994 flags = imap_flags_to_flags(att_dyn, &tags);
1996 puid = malloc(sizeof(* puid));
1998 res = MAILIMAP_ERROR_MEMORY;
2003 r = carray_add(tab, puid, NULL);
2006 res = MAILIMAP_ERROR_MEMORY;
2009 pflags = malloc(sizeof(* pflags));
2010 if (pflags == NULL) {
2011 res = MAILIMAP_ERROR_MEMORY;
2015 r = carray_add(tab, pflags, NULL);
2018 res = MAILIMAP_ERROR_MEMORY;
2021 r = carray_add(tab, tags, NULL);
2024 res = MAILIMAP_ERROR_MEMORY;
2031 return MAILIMAP_NO_ERROR;
2034 imap_fetch_uid_flags_list_free(tab);
2035 slist_free_strings(tags);
2041 static int imap_get_messages_flags_list(mailimap * imap,
2042 uint32_t first_index,
2047 struct mailimap_fetch_att * fetch_att;
2048 struct mailimap_fetch_type * fetch_type;
2049 struct mailimap_set * set;
2050 clist * fetch_result;
2053 set = mailimap_set_new_interval(first_index, 0);
2055 res = MAILIMAP_ERROR_MEMORY;
2059 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2060 if (fetch_type == NULL) {
2061 res = MAILIMAP_ERROR_MEMORY;
2065 fetch_att = mailimap_fetch_att_new_flags();
2066 if (fetch_att == NULL) {
2067 res = MAILIMAP_ERROR_MEMORY;
2068 goto free_fetch_type;
2071 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2072 if (r != MAILIMAP_NO_ERROR) {
2073 mailimap_fetch_att_free(fetch_att);
2074 res = MAILIMAP_ERROR_MEMORY;
2075 goto free_fetch_type;
2078 fetch_att = mailimap_fetch_att_new_uid();
2079 if (fetch_att == NULL) {
2080 res = MAILIMAP_ERROR_MEMORY;
2081 goto free_fetch_type;
2084 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2085 if (r != MAILIMAP_NO_ERROR) {
2086 mailimap_fetch_att_free(fetch_att);
2087 res = MAILIMAP_ERROR_MEMORY;
2088 goto free_fetch_type;
2091 mailstream_logger = imap_logger_fetch;
2093 r = mailimap_uid_fetch(imap, set,
2094 fetch_type, &fetch_result);
2096 mailstream_logger = imap_logger_cmd;
2097 mailimap_fetch_type_free(fetch_type);
2098 mailimap_set_free(set);
2100 if (r != MAILIMAP_NO_ERROR) {
2106 r = result_to_uid_flags_list(fetch_result, &env_list);
2107 mailimap_fetch_list_free(fetch_result);
2109 * result = env_list;
2111 return MAILIMAP_NO_ERROR;
2114 mailimap_fetch_type_free(fetch_type);
2116 mailimap_set_free(set);
2123 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2125 struct fetch_uid_param * param;
2126 struct fetch_uid_result * result;
2127 carray * fetch_result;
2131 result = op->result;
2135 fetch_result = NULL;
2136 r = imap_get_messages_flags_list(param->imap, param->first_index,
2140 result->fetch_result = fetch_result;
2141 debug_print("imap fetch_uid run - end %i\n", r);
2144 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2145 carray ** fetch_result)
2147 struct fetch_uid_param param;
2148 struct fetch_uid_result result;
2151 debug_print("imap fetch_uid - begin\n");
2153 imap = get_imap(folder);
2155 param.first_index = first_index;
2157 mailstream_logger = imap_logger_noop;
2158 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2160 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2162 mailstream_logger = imap_logger_cmd;
2165 if (result.error != MAILIMAP_NO_ERROR)
2166 return result.error;
2168 debug_print("imap fetch_uid - end\n");
2170 * fetch_result = result.fetch_result;
2172 return result.error;
2176 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2180 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2183 data = carray_get(uid_flags_list, i);
2185 data = carray_get(uid_flags_list, i + 1);
2188 carray_free(uid_flags_list);
2193 static int imap_fetch(mailimap * imap,
2196 size_t * result_len)
2199 struct mailimap_set * set;
2200 struct mailimap_fetch_att * fetch_att;
2201 struct mailimap_fetch_type * fetch_type;
2202 clist * fetch_result;
2203 struct mailimap_msg_att * msg_att;
2204 struct mailimap_msg_att_item * msg_att_item;
2209 struct mailimap_section * section;
2211 set = mailimap_set_new_single(msg_index);
2213 res = MAILIMAP_ERROR_MEMORY;
2217 section = mailimap_section_new(NULL);
2218 if (section == NULL) {
2219 res = MAILIMAP_ERROR_MEMORY;
2223 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2224 if (fetch_att == NULL) {
2225 mailimap_section_free(section);
2226 res = MAILIMAP_ERROR_MEMORY;
2230 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2231 if (fetch_type == NULL) {
2232 res = MAILIMAP_ERROR_MEMORY;
2233 goto free_fetch_att;
2236 mailstream_logger = imap_logger_fetch;
2238 r = mailimap_uid_fetch(imap, set,
2239 fetch_type, &fetch_result);
2241 mailstream_logger = imap_logger_cmd;
2243 mailimap_fetch_type_free(fetch_type);
2244 mailimap_set_free(set);
2247 case MAILIMAP_NO_ERROR:
2253 if (clist_begin(fetch_result) == NULL) {
2254 mailimap_fetch_list_free(fetch_result);
2255 return MAILIMAP_ERROR_FETCH;
2258 msg_att = clist_begin(fetch_result)->data;
2263 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2264 cur = clist_next(cur)) {
2265 msg_att_item = clist_content(cur);
2267 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2268 if (msg_att_item->att_data.att_static->att_type ==
2269 MAILIMAP_MSG_ATT_BODY_SECTION) {
2270 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2272 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2274 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2279 mailimap_fetch_list_free(fetch_result);
2282 return MAILIMAP_ERROR_FETCH;
2285 * result_len = text_length;
2287 return MAILIMAP_NO_ERROR;
2290 mailimap_fetch_att_free(fetch_att);
2292 mailimap_set_free(set);
2297 static int imap_fetch_header(mailimap * imap,
2300 size_t * result_len)
2303 struct mailimap_set * set;
2304 struct mailimap_fetch_att * fetch_att;
2305 struct mailimap_fetch_type * fetch_type;
2306 clist * fetch_result;
2307 struct mailimap_msg_att * msg_att;
2308 struct mailimap_msg_att_item * msg_att_item;
2313 struct mailimap_section * section;
2315 set = mailimap_set_new_single(msg_index);
2317 res = MAILIMAP_ERROR_MEMORY;
2321 section = mailimap_section_new_header();
2322 if (section == NULL) {
2323 res = MAILIMAP_ERROR_MEMORY;
2327 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2328 if (fetch_att == NULL) {
2329 mailimap_section_free(section);
2330 res = MAILIMAP_ERROR_MEMORY;
2334 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2335 if (fetch_type == NULL) {
2336 res = MAILIMAP_ERROR_MEMORY;
2337 goto free_fetch_att;
2340 mailstream_logger = imap_logger_fetch;
2342 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2344 mailstream_logger = imap_logger_cmd;
2345 mailimap_fetch_type_free(fetch_type);
2346 mailimap_set_free(set);
2349 case MAILIMAP_NO_ERROR:
2355 if (clist_begin(fetch_result) == NULL) {
2356 mailimap_fetch_list_free(fetch_result);
2357 return MAILIMAP_ERROR_FETCH;
2360 msg_att = clist_begin(fetch_result)->data;
2365 for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
2366 cur = clist_next(cur)) {
2367 msg_att_item = clist_content(cur);
2369 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2370 if (msg_att_item->att_data.att_static->att_type ==
2371 MAILIMAP_MSG_ATT_BODY_SECTION) {
2372 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2373 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2375 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2380 mailimap_fetch_list_free(fetch_result);
2383 return MAILIMAP_ERROR_FETCH;
2386 * result_len = text_length;
2388 return MAILIMAP_NO_ERROR;
2391 mailimap_fetch_att_free(fetch_att);
2393 mailimap_set_free(set);
2400 struct fetch_content_param {
2403 const char * filename;
2407 struct fetch_content_result {
2411 static void fetch_content_run(struct etpan_thread_op * op)
2413 struct fetch_content_param * param;
2414 struct fetch_content_result * result;
2416 size_t content_size;
2422 result = op->result;
2428 if (param->with_body)
2429 r = imap_fetch(param->imap, param->msg_index,
2430 &content, &content_size);
2432 r = imap_fetch_header(param->imap, param->msg_index,
2433 &content, &content_size);
2437 if (r == MAILIMAP_NO_ERROR) {
2438 fd = open(param->filename, O_RDWR | O_CREAT, 0600);
2440 result->error = MAILIMAP_ERROR_FETCH;
2444 f = fdopen(fd, "wb");
2446 result->error = MAILIMAP_ERROR_FETCH;
2450 r = fwrite(content, 1, content_size, f);
2451 if (r < content_size) {
2452 result->error = MAILIMAP_ERROR_FETCH;
2458 result->error = MAILIMAP_ERROR_FETCH;
2469 claws_unlink(param->filename);
2472 /* mmap_string_unref is a simple free in libetpan
2473 * when it has MMAP_UNAVAILABLE defined */
2474 if (mmap_string_unref(content) != 0)
2478 debug_print("imap fetch_content run - end %i\n", result->error);
2481 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2483 const char * filename)
2485 struct fetch_content_param param;
2486 struct fetch_content_result result;
2489 debug_print("imap fetch_content - begin\n");
2491 imap = get_imap(folder);
2493 param.msg_index = msg_index;
2494 param.filename = filename;
2495 param.with_body = with_body;
2497 threaded_run(folder, ¶m, &result, fetch_content_run);
2499 if (result.error != MAILIMAP_NO_ERROR)
2500 return result.error;
2502 debug_print("imap fetch_content - end\n");
2504 return result.error;
2509 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2514 GSList *tags = NULL;
2518 flag_list = att_dyn->att_list;
2519 if (flag_list == NULL)
2522 for(cur = clist_begin(flag_list) ; cur != NULL ;
2523 cur = clist_next(cur)) {
2524 struct mailimap_flag_fetch * flag_fetch;
2526 flag_fetch = clist_content(cur);
2527 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2530 switch (flag_fetch->fl_flag->fl_type) {
2531 case MAILIMAP_FLAG_ANSWERED:
2532 flags |= MSG_REPLIED;
2534 case MAILIMAP_FLAG_FLAGGED:
2535 flags |= MSG_MARKED;
2537 case MAILIMAP_FLAG_DELETED:
2538 flags |= MSG_DELETED;
2540 case MAILIMAP_FLAG_SEEN:
2541 flags &= ~MSG_UNREAD;
2544 case MAILIMAP_FLAG_KEYWORD:
2545 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
2546 flags |= MSG_FORWARDED;
2547 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "Junk"))
2549 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NonJunk") ||
2550 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NoJunk") ||
2551 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NotJunk"))
2554 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2564 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2568 struct mailimap_msg_att_dynamic ** patt_dyn)
2570 clistiter * item_cur;
2574 struct mailimap_msg_att_dynamic * att_dyn;
2581 for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
2582 item_cur = clist_next(item_cur)) {
2583 struct mailimap_msg_att_item * item;
2585 item = clist_content(item_cur);
2587 switch (item->att_type) {
2588 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2589 switch (item->att_data.att_static->att_type) {
2590 case MAILIMAP_MSG_ATT_UID:
2591 uid = item->att_data.att_static->att_data.att_uid;
2594 case MAILIMAP_MSG_ATT_BODY_SECTION:
2595 if (headers == NULL) {
2596 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2599 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2600 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2605 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2606 if (att_dyn == NULL) {
2607 att_dyn = item->att_data.att_dyn;
2615 if (pheaders != NULL)
2616 * pheaders = headers;
2617 if (pref_size != NULL)
2618 * pref_size = ref_size;
2619 if (patt_dyn != NULL)
2620 * patt_dyn = att_dyn;
2622 return MAIL_NO_ERROR;
2625 static struct imap_fetch_env_info *
2626 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2628 struct imap_fetch_env_info * info;
2632 struct mailimap_msg_att_dynamic * att_dyn;
2634 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2639 info = malloc(sizeof(* info));
2641 info->headers = strdup(headers);
2643 info->flags = imap_flags_to_flags(att_dyn, tags);
2649 imap_fetch_result_to_envelop_list(clist * fetch_result,
2650 carray ** p_env_list)
2657 env_list = carray_new(16);
2659 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2660 cur = clist_next(cur)) {
2661 struct mailimap_msg_att * msg_att;
2662 struct imap_fetch_env_info * env_info;
2663 GSList *tags = NULL;
2665 msg_att = clist_content(cur);
2667 env_info = fetch_to_env_info(msg_att, &tags);
2669 return MAILIMAP_ERROR_MEMORY;
2670 carray_add(env_list, env_info, NULL);
2671 carray_add(env_list, tags, NULL);
2674 * p_env_list = env_list;
2676 return MAIL_NO_ERROR;
2679 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2681 struct mailimap_fetch_att * fetch_att;
2685 struct mailimap_header_list * imap_hdrlist;
2686 struct mailimap_section * section;
2688 hdrlist = clist_new();
2690 header = strdup("Date");
2691 r = clist_append(hdrlist, header);
2692 header = strdup("From");
2693 r = clist_append(hdrlist, header);
2694 header = strdup("To");
2695 r = clist_append(hdrlist, header);
2696 header = strdup("Cc");
2697 r = clist_append(hdrlist, header);
2698 header = strdup("Subject");
2699 r = clist_append(hdrlist, header);
2700 header = strdup("Message-ID");
2701 r = clist_append(hdrlist, header);
2702 header = strdup("References");
2703 r = clist_append(hdrlist, header);
2704 header = strdup("In-Reply-To");
2705 r = clist_append(hdrlist, header);
2707 imap_hdrlist = mailimap_header_list_new(hdrlist);
2708 section = mailimap_section_new_header_fields(imap_hdrlist);
2709 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2710 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2712 return MAIL_NO_ERROR;
2715 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2717 struct mailimap_fetch_att * fetch_att;
2718 struct mailimap_section * section;
2720 section = mailimap_section_new_header();
2721 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2722 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2724 return MAIL_NO_ERROR;
2728 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2729 carray ** p_env_list)
2731 struct mailimap_fetch_att * fetch_att;
2732 struct mailimap_fetch_type * fetch_type;
2734 clist * fetch_result;
2736 carray * env_list = NULL;
2740 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2743 fetch_att = mailimap_fetch_att_new_uid();
2744 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2747 fetch_att = mailimap_fetch_att_new_flags();
2748 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2751 fetch_att = mailimap_fetch_att_new_rfc822_size();
2752 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2756 key.len = sizeof(imap);
2757 r = chash_get(courier_workaround_hash, &key, &value);
2759 r = imap_add_envelope_fetch_att(fetch_type);
2761 r = imap_add_header_fetch_att(fetch_type);
2763 mailstream_logger = imap_logger_fetch;
2765 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2767 mailstream_logger = imap_logger_cmd;
2769 case MAILIMAP_NO_ERROR:
2772 mailimap_fetch_type_free(fetch_type);
2773 debug_print("uid_fetch: %d\n", r);
2777 if (clist_begin(fetch_result) == NULL) {
2778 res = MAILIMAP_ERROR_FETCH;
2779 debug_print("clist_begin = NULL\n");
2783 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2784 mailimap_fetch_list_free(fetch_result);
2786 if (r != MAILIMAP_NO_ERROR) {
2787 mailimap_fetch_type_free(fetch_type);
2788 res = MAILIMAP_ERROR_MEMORY;
2789 debug_print("fetch_result_to_envelop_list: %d\n", res);
2793 mailimap_fetch_type_free(fetch_type);
2795 * p_env_list = env_list;
2797 return MAILIMAP_NO_ERROR;
2803 struct fetch_env_param {
2805 struct mailimap_set * set;
2808 struct fetch_env_result {
2809 carray * fetch_env_result;
2813 static void fetch_env_run(struct etpan_thread_op * op)
2815 struct fetch_env_param * param;
2816 struct fetch_env_result * result;
2821 result = op->result;
2826 r = imap_get_envelopes_list(param->imap, param->set,
2830 result->fetch_env_result = env_list;
2832 debug_print("imap fetch_env run - end %i\n", r);
2835 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2836 carray ** p_env_list)
2838 struct fetch_env_param param;
2839 struct fetch_env_result result;
2842 debug_print("imap fetch_env - begin\n");
2844 imap = get_imap(folder);
2848 threaded_run(folder, ¶m, &result, fetch_env_run);
2850 if (result.error != MAILIMAP_NO_ERROR) {
2856 key.len = sizeof(imap);
2857 r = chash_get(courier_workaround_hash, &key, &value);
2861 chash_set(courier_workaround_hash, &key, &value, NULL);
2863 threaded_run(folder, ¶m, &result, fetch_env_run);
2867 if (result.error != MAILIMAP_NO_ERROR)
2868 return result.error;
2870 debug_print("imap fetch_env - end\n");
2872 * p_env_list = result.fetch_env_result;
2874 return result.error;
2877 void imap_fetch_env_free(carray * env_list)
2881 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2882 struct imap_fetch_env_info * env_info;
2884 env_info = carray_get(env_list, i);
2885 free(env_info->headers);
2888 carray_free(env_list);
2895 struct append_param {
2897 const char * mailbox;
2898 const char * filename;
2899 struct mailimap_flag_list * flag_list;
2902 struct append_result {
2907 static void append_run(struct etpan_thread_op * op)
2909 struct append_param * param;
2910 struct append_result * result;
2915 struct stat stat_buf;
2918 guint32 uid = 0, val = 0;
2921 result = op->result;
2926 r = stat(param->filename, &stat_buf);
2928 result->error = MAILIMAP_ERROR_APPEND;
2931 size = stat_buf.st_size;
2933 fd = open(param->filename, O_RDONLY);
2935 result->error = MAILIMAP_ERROR_APPEND;
2939 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2940 if (data == (void *) MAP_FAILED) {
2942 result->error = MAILIMAP_ERROR_APPEND;
2946 data = file_read_to_str_no_recode(param->filename);
2948 result->error = MAILIMAP_ERROR_APPEND;
2951 size = strlen(data);
2953 mailstream_logger = imap_logger_append;
2955 r = mailimap_uidplus_append(param->imap, param->mailbox,
2956 param->flag_list, NULL,
2957 data, size, &val, &uid);
2959 mailstream_logger = imap_logger_cmd;
2970 debug_print("imap append run - end %i uid %d\n", r, uid);
2973 int imap_threaded_append(Folder * folder, const char * mailbox,
2974 const char * filename,
2975 struct mailimap_flag_list * flag_list,
2978 struct append_param param;
2979 struct append_result result;
2982 debug_print("imap append - begin\n");
2984 imap = get_imap(folder);
2986 param.mailbox = mailbox;
2987 param.filename = filename;
2988 param.flag_list = flag_list;
2990 threaded_run(folder, ¶m, &result, append_run);
2992 if (result.error != MAILIMAP_NO_ERROR)
2993 return result.error;
2995 debug_print("imap append - end\n");
2999 return result.error;
3005 struct expunge_param {
3009 struct expunge_result {
3013 static void expunge_run(struct etpan_thread_op * op)
3015 struct expunge_param * param;
3016 struct expunge_result * result;
3020 result = op->result;
3024 r = mailimap_expunge(param->imap);
3027 debug_print("imap expunge run - end %i\n", r);
3030 int imap_threaded_expunge(Folder * folder)
3032 struct expunge_param param;
3033 struct expunge_result result;
3035 debug_print("imap expunge - begin\n");
3037 param.imap = get_imap(folder);
3039 threaded_run(folder, ¶m, &result, expunge_run);
3041 debug_print("imap expunge - end\n");
3043 return result.error;
3049 struct mailimap_set * set;
3053 struct copy_result {
3055 struct mailimap_set *source;
3056 struct mailimap_set *dest;
3059 static void copy_run(struct etpan_thread_op * op)
3061 struct copy_param * param;
3062 struct copy_result * result;
3065 struct mailimap_set *source = NULL, *dest = NULL;
3068 result = op->result;
3072 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3073 &val, &source, &dest);
3077 result->source = source;
3078 result->dest = dest;
3080 result->source = NULL;
3081 result->dest = NULL;
3083 debug_print("imap copy run - end %i\n", r);
3086 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3087 const char * mb, struct mailimap_set **source,
3088 struct mailimap_set **dest)
3090 struct copy_param param;
3091 struct copy_result result;
3094 debug_print("imap copy - begin\n");
3096 imap = get_imap(folder);
3101 threaded_run(folder, ¶m, &result, copy_run);
3105 if (result.error != MAILIMAP_NO_ERROR)
3106 return result.error;
3108 *source = result.source;
3109 *dest = result.dest;
3111 debug_print("imap copy - end\n");
3113 return result.error;
3118 struct store_param {
3120 struct mailimap_set * set;
3121 struct mailimap_store_att_flags * store_att_flags;
3124 struct store_result {
3128 static void store_run(struct etpan_thread_op * op)
3130 struct store_param * param;
3131 struct store_result * result;
3135 result = op->result;
3139 r = mailimap_uid_store(param->imap, param->set,
3140 param->store_att_flags);
3144 debug_print("imap store run - end %i\n", r);
3147 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3148 struct mailimap_store_att_flags * store_att_flags)
3150 struct store_param param;
3151 struct store_result result;
3154 debug_print("imap store - begin\n");
3156 imap = get_imap(folder);
3159 param.store_att_flags = store_att_flags;
3161 threaded_run(folder, ¶m, &result, store_run);
3163 if (result.error != MAILIMAP_NO_ERROR)
3164 return result.error;
3166 debug_print("imap store - end\n");
3168 return result.error;
3172 #define ENV_BUFFER_SIZE 512
3174 static void do_exec_command(int fd, const char * command,
3175 const char * servername, uint16_t port)
3179 char env_buffer[ENV_BUFFER_SIZE];
3183 /* Fork again to become a child of init rather than
3184 the etpan client. */
3189 g_setenv("ETPANSERVER", servername, TRUE);
3191 g_unsetenv("ETPANSERVER");
3196 snprintf(porttext, sizeof(porttext), "%d", port);
3197 g_setenv("ETPANPORT", porttext, TRUE);
3200 g_unsetenv("ETPANPORT");
3203 /* Not a lot we can do if there's an error other than bail. */
3204 if (dup2(fd, 0) == -1)
3206 if (dup2(fd, 1) == -1)
3209 /* Should we close stderr and reopen /dev/null? */
3211 maxopen = sysconf(_SC_OPEN_MAX);
3212 for (i=3; i < maxopen; i++)
3216 /* Detach from the controlling tty if we have one. Otherwise,
3217 SSH might do something stupid like trying to use it instead
3218 of running $SSH_ASKPASS. Doh. */
3219 fd = open("/dev/tty", O_RDONLY);
3221 ioctl(fd, TIOCNOTTY, NULL);
3224 #endif /* TIOCNOTTY */
3226 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3228 /* Eep. Shouldn't reach this */
3232 static int subcommand_connect(const char *command,
3233 const char *servername, uint16_t port)
3235 /* SEB unsupported on Windows */
3239 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3244 do_exec_command(sockfds[1], command, servername, port);
3246 else if (childpid == -1) {
3254 /* Reap child, leaving grandchild process to run */
3255 waitpid(childpid, NULL, 0);
3260 static int socket_connect_cmd(mailimap * imap, const char * command,
3261 const char * server, int port)
3267 fd = subcommand_connect(command, server, port);
3269 return MAILIMAP_ERROR_STREAM;
3271 s = mailstream_socket_open(fd);
3274 return MAILIMAP_ERROR_STREAM;
3277 r = mailimap_connect(imap, s);
3278 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3279 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3280 mailstream_close(s);
3282 imap->imap_stream = NULL;
3291 struct connect_cmd_param {
3293 const char * command;
3294 const char * server;
3298 struct connect_cmd_result {
3302 static void connect_cmd_run(struct etpan_thread_op * op)
3305 struct connect_cmd_param * param;
3306 struct connect_cmd_result * result;
3309 result = op->result;
3313 r = socket_connect_cmd(param->imap, param->command,
3314 param->server, param->port);
3320 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3321 const char * server, int port)
3323 struct connect_cmd_param param;
3324 struct connect_cmd_result result;
3327 mailimap * imap, * oldimap;
3329 oldimap = get_imap(folder);
3331 imap = mailimap_new(0, NULL);
3334 debug_print("deleting old imap %p\n", oldimap);
3335 delete_imap(folder, oldimap);
3339 key.len = sizeof(folder);
3342 chash_set(session_hash, &key, &value, NULL);
3345 param.command = command;
3346 param.server = server;
3349 threaded_run(folder, ¶m, &result, connect_cmd_run);
3351 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3353 return result.error;
3355 #endif /* G_OS_WIN32 */
3357 void imap_threaded_cancel(Folder * folder)
3361 imap = get_imap(folder);
3362 if (imap->imap_stream != NULL)
3363 mailstream_cancel(imap->imap_stream);
3368 void imap_main_init(void)
3371 void imap_main_done(void)
3374 void imap_main_set_timeout(int sec)
3378 void imap_threaded_cancel(Folder * folder);