2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2011 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/>.
27 #include <glib/gi18n.h>
28 #include "imap-thread.h"
30 #include <sys/types.h>
32 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
33 #include <sys/socket.h>
42 #include "etpan-thread-manager.h"
44 #include "mainwindow.h"
46 #include "ssl_certificate.h"
48 #include "remotefolder.h"
51 #define DISABLE_LOG_DURING_LOGIN
53 static struct etpan_thread_manager * thread_manager = NULL;
54 static chash * courier_workaround_hash = NULL;
55 static chash * imap_hash = NULL;
56 static chash * session_hash = NULL;
57 static guint thread_manager_signal = 0;
58 static GIOChannel * io_channel = NULL;
60 static void delete_imap(Folder *folder, mailimap *imap)
66 key.len = sizeof(folder);
69 chash_delete(session_hash, &key, NULL);
72 key.len = sizeof(imap);
73 chash_delete(courier_workaround_hash, &key, NULL);
74 if (imap && imap->imap_stream) {
75 /* we don't want libetpan to logout */
76 mailstream_close(imap->imap_stream);
77 imap->imap_stream = NULL;
79 debug_print("removing mailimap %p\n", imap);
83 static gboolean thread_manager_event(GIOChannel * source,
84 GIOCondition condition,
91 if (condition & G_IO_IN)
92 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
94 etpan_thread_manager_loop(thread_manager);
99 static void imap_logger_noop(int direction, const char * str, size_t size)
101 /* inhibit logging */
104 static void imap_logger_cmd(int direction, const char * str, size_t size)
111 log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
114 buf = malloc(size+1);
115 memset(buf, 0, size+1);
116 strncpy(buf, str, size);
119 if (!strncmp(buf, "<<<<<<<", 7)
120 || !strncmp(buf, ">>>>>>>", 7)) {
124 while (strstr(buf, "\r"))
125 *strstr(buf, "\r") = ' ';
126 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
127 buf[strlen(buf)-1] = '\0';
129 lines = g_strsplit(buf, "\n", -1);
131 while (lines[i] && *lines[i]) {
132 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
139 static void imap_logger_fetch(int direction, const char * str, size_t size)
145 if (size > 128 && !direction) {
146 log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
150 buf = malloc(size+1);
151 memset(buf, 0, size+1);
152 strncpy(buf, str, size);
154 if (!strncmp(buf, "<<<<<<<", 7)
155 || !strncmp(buf, ">>>>>>>", 7)) {
159 while (strstr(buf, "\r"))
160 *strstr(buf, "\r") = ' ';
161 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
162 buf[strlen(buf)-1] = '\0';
164 lines = g_strsplit(buf, "\n", -1);
166 if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
167 while (lines[i] && *lines[i]) {
168 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
172 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
178 static void imap_logger_uid(int direction, const char * str, size_t size)
185 log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
188 buf = malloc(size+1);
189 memset(buf, 0, size+1);
190 strncpy(buf, str, size);
192 if (!strncmp(buf, "<<<<<<<", 7)
193 || !strncmp(buf, ">>>>>>>", 7)) {
197 while (strstr(buf, "\r"))
198 *strstr(buf, "\r") = ' ';
199 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
200 buf[strlen(buf)-1] = '\0';
202 lines = g_strsplit(buf, "\n", -1);
204 while (lines[i] && *lines[i]) {
205 int llen = strlen(lines[i]);
207 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
210 strncpy2(tmp, lines[i], 63);
211 log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
220 static void imap_logger_append(int direction, const char * str, size_t size)
227 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
229 } else if (direction == 0 && size > 64) {
230 log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
233 buf = malloc(size+1);
234 memset(buf, 0, size+1);
235 strncpy(buf, str, size);
237 if (!strncmp(buf, "<<<<<<<", 7)
238 || !strncmp(buf, ">>>>>>>", 7)) {
242 while (strstr(buf, "\r"))
243 *strstr(buf, "\r") = ' ';
244 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
245 buf[strlen(buf)-1] = '\0';
247 lines = g_strsplit(buf, "\n", -1);
249 if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
250 while (lines[i] && *lines[i]) {
251 log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
255 log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
261 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
262 gboolean etpan_skip_ssl_cert_check = FALSE;
263 extern void mailsasl_ref(void);
265 void imap_main_init(gboolean skip_ssl_cert_check)
267 int fd_thread_manager;
269 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
270 mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
271 mailstream_network_delay.tv_usec = 0;
273 mailstream_debug = 1;
274 mailstream_logger = imap_logger_cmd;
277 imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
278 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
279 courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
281 thread_manager = etpan_thread_manager_new();
283 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
286 io_channel = g_io_channel_unix_new(fd_thread_manager);
288 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
290 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
291 thread_manager_event,
296 void imap_main_set_timeout(int sec)
298 mailstream_network_delay.tv_sec = sec;
299 mailstream_network_delay.tv_usec = 0;
302 void imap_main_done(gboolean have_connectivity)
304 imap_disconnect_all(have_connectivity);
305 etpan_thread_manager_stop(thread_manager);
306 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
309 etpan_thread_manager_join(thread_manager);
311 g_source_remove(thread_manager_signal);
312 g_io_channel_unref(io_channel);
314 etpan_thread_manager_free(thread_manager);
316 chash_free(courier_workaround_hash);
317 chash_free(session_hash);
318 chash_free(imap_hash);
321 void imap_init(Folder * folder)
323 struct etpan_thread * thread;
327 thread = etpan_thread_manager_get_thread(thread_manager);
330 key.len = sizeof(folder);
334 chash_set(imap_hash, &key, &value, NULL);
337 void imap_done(Folder * folder)
339 struct etpan_thread * thread;
345 key.len = sizeof(folder);
347 r = chash_get(imap_hash, &key, &value);
353 etpan_thread_unbind(thread);
355 chash_delete(imap_hash, &key, NULL);
357 debug_print("remove thread");
360 static struct etpan_thread * get_thread(Folder * folder)
362 struct etpan_thread * thread;
367 key.len = sizeof(folder);
369 chash_get(imap_hash, &key, &value);
375 static mailimap * get_imap(Folder * folder)
383 key.len = sizeof(folder);
385 r = chash_get(session_hash, &key, &value);
390 debug_print("found imap %p\n", imap);
394 static gboolean cb_show_error(gpointer data)
396 mainwindow_show_error();
400 static void generic_cb(int cancelled, void * result, void * callback_data)
402 struct etpan_thread_op * op;
404 op = (struct etpan_thread_op *) callback_data;
406 debug_print("generic_cb\n");
407 if (op->imap && op->imap->imap_response_info &&
408 op->imap->imap_response_info->rsp_alert) {
409 log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
410 op->imap->imap_response_info->rsp_alert);
411 g_timeout_add(10, cb_show_error, NULL);
416 static void threaded_run(Folder * folder, void * param, void * result,
417 void (* func)(struct etpan_thread_op * ))
419 struct etpan_thread_op * op;
420 struct etpan_thread * thread;
422 imap_folder_ref(folder);
424 op = etpan_thread_op_new();
426 op->imap = get_imap(folder);
432 op->callback = generic_cb;
433 op->callback_data = op;
438 thread = get_thread(folder);
439 etpan_thread_op_schedule(thread, op);
441 while (!op->finished) {
442 gtk_main_iteration();
445 etpan_thread_op_free(op);
447 imap_folder_unref(folder);
453 struct connect_param {
455 PrefsAccount *account;
460 struct connect_result {
464 #define CHECK_IMAP() { \
465 if (!param->imap) { \
466 result->error = MAILIMAP_ERROR_BAD_STATE; \
471 static void connect_run(struct etpan_thread_op * op)
474 struct connect_param * param;
475 struct connect_result * result;
482 r = mailimap_socket_connect(param->imap,
483 param->server, param->port);
489 int imap_threaded_connect(Folder * folder, const char * server, int port)
491 struct connect_param param;
492 struct connect_result result;
495 mailimap * imap, * oldimap;
497 oldimap = get_imap(folder);
499 imap = mailimap_new(0, NULL);
502 debug_print("deleting old imap %p\n", oldimap);
503 delete_imap(folder, oldimap);
507 key.len = sizeof(folder);
510 chash_set(session_hash, &key, &value, NULL);
513 param.server = server;
517 threaded_run(folder, ¶m, &result, connect_run);
519 debug_print("connect ok %i with imap %p\n", result.error, imap);
524 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
527 struct connect_param *param = (struct connect_param *)data;
528 gnutls_x509_crt cert = NULL;
531 if (certificate == NULL || len < 0) {
532 g_warning("no cert presented.\n");
536 tmp.data = malloc(len);
537 memcpy(tmp.data, certificate, len);
539 gnutls_x509_crt_init(&cert);
540 if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
541 g_warning("IMAP: can't get cert\n");
543 } else if (ssl_certificate_check(cert, (guint)-1, (gchar *)param->server,
544 (gushort)param->port) == TRUE) {
545 gnutls_x509_crt_deinit(cert);
548 gnutls_x509_crt_deinit(cert);
555 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
558 PrefsAccount *account = (PrefsAccount *)data;
559 const gchar *cert_path = NULL;
560 const gchar *password = NULL;
561 gnutls_x509_crt x509 = NULL;
562 gnutls_x509_privkey pkey = NULL;
564 if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
565 cert_path = account->in_ssl_client_cert_file;
566 if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
567 password = account->in_ssl_client_cert_pass;
569 if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
570 mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
571 debug_print("Impossible to set the client certificate.\n");
572 x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
573 pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
574 if (!(x509 && pkey)) {
575 /* try pkcs12 format */
576 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
579 unsigned char *x509_der = NULL, *pkey_der = NULL;
580 size_t x509_len, pkey_len;
582 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
583 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
584 if (x509_len > 0 && pkey_len > 0) {
585 if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
586 mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
587 log_error(LOG_PROTOCOL, _("Impossible to set the client certificate.\n"));
591 gnutls_x509_crt_deinit(x509);
592 gnutls_x509_privkey_deinit(pkey);
597 static void connect_ssl_run(struct etpan_thread_op * op)
600 struct connect_param * param;
601 struct connect_result * result;
608 r = mailimap_ssl_connect_with_callback(param->imap,
609 param->server, param->port,
610 connect_ssl_context_cb, param->account);
614 int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
616 struct connect_param param;
617 struct connect_result result;
620 mailimap * imap, * oldimap;
621 unsigned char *certificate = NULL;
624 oldimap = get_imap(folder);
626 imap = mailimap_new(0, NULL);
629 debug_print("deleting old imap %p\n", oldimap);
630 delete_imap(folder, oldimap);
634 key.len = sizeof(folder);
637 chash_set(session_hash, &key, &value, NULL);
640 param.server = server;
642 param.account = folder->account;
645 threaded_run(folder, ¶m, &result, connect_ssl_run);
647 if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
648 result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
649 cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
650 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
655 debug_print("connect %d with imap %p\n", result.error, imap);
666 struct mailimap_capability_data *caps;
669 static void capability_run(struct etpan_thread_op * op)
672 struct capa_param * param;
673 struct capa_result * result;
674 struct mailimap_capability_data *caps;
681 r = mailimap_capability(param->imap, &caps);
684 result->caps = (r == 0 ? caps : NULL);
688 struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
690 struct capa_param param;
691 struct capa_result result;
694 imap = get_imap(folder);
698 threaded_run(folder, ¶m, &result, capability_run);
700 debug_print("capa %d\n", result.error);
709 struct disconnect_param {
713 struct disconnect_result {
717 static void disconnect_run(struct etpan_thread_op * op)
720 struct disconnect_param * param;
721 struct disconnect_result * result;
728 r = mailimap_logout(param->imap);
733 void imap_threaded_disconnect(Folder * folder)
735 struct connect_param param;
736 struct connect_result result;
739 imap = get_imap(folder);
741 debug_print("was disconnected\n");
747 threaded_run(folder, ¶m, &result, disconnect_run);
749 if (imap == get_imap(folder)) {
750 debug_print("deleting old imap %p\n", imap);
751 delete_imap(folder, imap);
753 debug_print("imap already deleted %p\n", imap);
756 debug_print("disconnect ok\n");
763 const char * wildcard;
772 static void list_run(struct etpan_thread_op * op)
774 struct list_param * param;
775 struct list_result * result;
786 if (param->base == NULL || param->wildcard == NULL) {
789 debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
793 r = mailimap_lsub(param->imap, param->base,
794 param->wildcard, &list);
796 r = mailimap_list(param->imap, param->base,
797 param->wildcard, &list);
800 debug_print("imap list run - end\n");
803 int imap_threaded_list(Folder * folder, const char * base,
804 const char * wildcard,
807 struct list_param param;
808 struct list_result result;
810 debug_print("imap list - begin\n");
812 param.imap = get_imap(folder);
814 param.wildcard = wildcard;
815 param.sub_only = FALSE;
817 threaded_run(folder, ¶m, &result, list_run);
819 * p_result = result.list;
821 debug_print("imap list - end %p\n", result.list);
826 int imap_threaded_lsub(Folder * folder, const char * base,
827 const char * wildcard,
830 struct list_param param;
831 struct list_result result;
833 debug_print("imap lsub - begin\n");
835 param.imap = get_imap(folder);
837 param.wildcard = wildcard;
838 param.sub_only = TRUE;
840 threaded_run(folder, ¶m, &result, list_run);
842 * p_result = result.list;
844 debug_print("imap lsub - end %p\n", result.list);
849 struct subscribe_param {
855 struct subscribe_result {
859 static void subscribe_run(struct etpan_thread_op * op)
861 struct subscribe_param * param;
862 struct subscribe_result * result;
870 if (param->mb == NULL) {
872 debug_print("no mb\n");
875 if (param->subscribe)
876 r = mailimap_subscribe(param->imap, param->mb);
878 r = mailimap_unsubscribe(param->imap, param->mb);
880 debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
883 int imap_threaded_subscribe(Folder * folder, const char * mb,
886 struct subscribe_param param;
887 struct subscribe_result result;
889 debug_print("imap list - begin\n");
891 param.imap = get_imap(folder);
893 param.subscribe = subscribe;
895 threaded_run(folder, ¶m, &result, subscribe_run);
903 const char * password;
908 struct login_result {
912 static void login_run(struct etpan_thread_op * op)
914 struct login_param * param;
915 struct login_result * result;
917 #ifdef DISABLE_LOG_DURING_LOGIN
926 #ifdef DISABLE_LOG_DURING_LOGIN
927 old_debug = mailstream_debug;
928 mailstream_debug = 0;
930 if (!strcmp(param->type, "LOGIN"))
931 r = mailimap_login(param->imap,
932 param->login, param->password);
933 else if (!strcmp(param->type, "GSSAPI"))
934 r = mailimap_authenticate(param->imap,
935 param->type, param->server, NULL, NULL,
936 param->login, param->login,
937 param->password, NULL);
939 r = mailimap_authenticate(param->imap,
940 param->type, NULL, NULL, NULL,
941 param->login, param->login,
942 param->password, NULL);
943 #ifdef DISABLE_LOG_DURING_LOGIN
944 mailstream_debug = old_debug;
948 if (param->imap->imap_response)
949 imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
950 debug_print("imap login run - end %i\n", r);
953 int imap_threaded_login(Folder * folder,
954 const char * login, const char * password,
957 struct login_param param;
958 struct login_result result;
960 debug_print("imap login - begin\n");
962 param.imap = get_imap(folder);
964 param.password = password;
966 if (folder && folder->account)
967 param.server = folder->account->recv_server;
971 threaded_run(folder, ¶m, &result, login_run);
973 debug_print("imap login - end\n");
979 struct status_param {
982 struct mailimap_status_att_list * status_att_list;
985 struct status_result {
987 struct mailimap_mailbox_data_status * data_status;
990 static void status_run(struct etpan_thread_op * op)
992 struct status_param * param;
993 struct status_result * result;
1001 r = mailimap_status(param->imap, param->mb,
1002 param->status_att_list,
1003 &result->data_status);
1006 debug_print("imap status run - end %i\n", r);
1009 int imap_threaded_status(Folder * folder, const char * mb,
1010 struct mailimap_mailbox_data_status ** data_status,
1013 struct status_param param;
1014 struct status_result result;
1015 struct mailimap_status_att_list * status_att_list;
1017 debug_print("imap status - begin\n");
1019 status_att_list = mailimap_status_att_list_new_empty();
1020 if (mask & 1 << 0) {
1021 mailimap_status_att_list_add(status_att_list,
1022 MAILIMAP_STATUS_ATT_MESSAGES);
1024 if (mask & 1 << 1) {
1025 mailimap_status_att_list_add(status_att_list,
1026 MAILIMAP_STATUS_ATT_RECENT);
1028 if (mask & 1 << 2) {
1029 mailimap_status_att_list_add(status_att_list,
1030 MAILIMAP_STATUS_ATT_UIDNEXT);
1032 if (mask & 1 << 3) {
1033 mailimap_status_att_list_add(status_att_list,
1034 MAILIMAP_STATUS_ATT_UIDVALIDITY);
1036 if (mask & 1 << 4) {
1037 mailimap_status_att_list_add(status_att_list,
1038 MAILIMAP_STATUS_ATT_UNSEEN);
1040 param.imap = get_imap(folder);
1042 param.status_att_list = status_att_list;
1044 threaded_run(folder, ¶m, &result, status_run);
1046 debug_print("imap status - end\n");
1048 * data_status = result.data_status;
1050 mailimap_status_att_list_free(status_att_list);
1052 return result.error;
1061 struct noop_result {
1065 static void noop_run(struct etpan_thread_op * op)
1067 struct noop_param * param;
1068 struct noop_result * result;
1072 result = op->result;
1076 r = mailimap_noop(param->imap);
1079 debug_print("imap noop run - end %i\n", r);
1082 int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
1083 unsigned int *p_recent,
1084 unsigned int *p_expunge,
1085 unsigned int *p_unseen,
1086 unsigned int *p_uidnext,
1087 unsigned int *p_uidval)
1089 struct noop_param param;
1090 struct noop_result result;
1093 debug_print("imap noop - begin\n");
1095 imap = get_imap(folder);
1098 threaded_run(folder, ¶m, &result, noop_run);
1100 if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
1101 * p_exists = imap->imap_selection_info->sel_exists;
1102 * p_recent = imap->imap_selection_info->sel_recent;
1103 * p_unseen = imap->imap_selection_info->sel_unseen;
1104 * p_uidnext = imap->imap_selection_info->sel_uidnext;
1105 * p_uidval = imap->imap_selection_info->sel_uidvalidity;
1113 if (result.error == 0 && imap && imap->imap_response_info != NULL &&
1114 imap->imap_response_info->rsp_expunged != NULL) {
1115 * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
1119 debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
1120 *p_exists, *p_recent, *p_expunge, *p_unseen,
1121 *p_uidnext, *p_uidval);
1123 return result.error;
1127 struct starttls_result {
1131 static void starttls_run(struct etpan_thread_op * op)
1133 struct connect_param * param;
1134 struct starttls_result * result;
1138 result = op->result;
1142 r = mailimap_starttls(param->imap);
1145 debug_print("imap starttls run - end %i\n", r);
1148 mailimap *imap = param->imap;
1149 mailstream_low *plain_low = NULL;
1150 mailstream_low *tls_low = NULL;
1153 plain_low = mailstream_get_low(imap->imap_stream);
1154 fd = mailstream_low_get_fd(plain_low);
1156 debug_print("imap starttls run - can't get fd\n");
1157 result->error = MAILIMAP_ERROR_STREAM;
1161 tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
1162 if (tls_low == NULL) {
1163 debug_print("imap starttls run - can't tls_open\n");
1164 result->error = MAILIMAP_ERROR_STREAM;
1167 mailstream_low_free(plain_low);
1168 mailstream_set_low(imap->imap_stream, tls_low);
1172 int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
1174 struct connect_param param;
1175 struct starttls_result result;
1177 unsigned char *certificate = NULL;
1179 debug_print("imap starttls - begin\n");
1181 param.imap = get_imap(folder);
1182 param.server = host;
1184 param.account = folder->account;
1186 threaded_run(folder, ¶m, &result, starttls_run);
1188 debug_print("imap starttls - end\n");
1190 if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
1191 cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
1192 if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
1193 result.error = MAILIMAP_ERROR_STREAM;
1197 return result.error;
1202 struct create_param {
1207 struct create_result {
1211 static void create_run(struct etpan_thread_op * op)
1213 struct create_param * param;
1214 struct create_result * result;
1218 result = op->result;
1222 r = mailimap_create(param->imap, param->mb);
1225 debug_print("imap create run - end %i\n", r);
1228 int imap_threaded_create(Folder * folder, const char * mb)
1230 struct create_param param;
1231 struct create_result result;
1233 debug_print("imap create - begin\n");
1235 param.imap = get_imap(folder);
1238 threaded_run(folder, ¶m, &result, create_run);
1240 debug_print("imap create - end\n");
1242 return result.error;
1248 struct rename_param {
1251 const char * new_name;
1254 struct rename_result {
1258 static void rename_run(struct etpan_thread_op * op)
1260 struct rename_param * param;
1261 struct rename_result * result;
1265 result = op->result;
1269 r = mailimap_rename(param->imap, param->mb, param->new_name);
1272 debug_print("imap rename run - end %i\n", r);
1275 int imap_threaded_rename(Folder * folder,
1276 const char * mb, const char * new_name)
1278 struct rename_param param;
1279 struct rename_result result;
1281 debug_print("imap rename - begin\n");
1283 param.imap = get_imap(folder);
1285 param.new_name = new_name;
1287 threaded_run(folder, ¶m, &result, rename_run);
1289 debug_print("imap rename - end\n");
1291 return result.error;
1297 struct delete_param {
1302 struct delete_result {
1306 static void delete_run(struct etpan_thread_op * op)
1308 struct delete_param * param;
1309 struct delete_result * result;
1313 result = op->result;
1317 r = mailimap_delete(param->imap, param->mb);
1320 debug_print("imap delete run - end %i\n", r);
1323 int imap_threaded_delete(Folder * folder, const char * mb)
1325 struct delete_param param;
1326 struct delete_result result;
1328 debug_print("imap delete - begin\n");
1330 param.imap = get_imap(folder);
1333 threaded_run(folder, ¶m, &result, delete_run);
1335 debug_print("imap delete - end\n");
1337 return result.error;
1342 struct select_param {
1347 struct select_result {
1351 static void select_run(struct etpan_thread_op * op)
1353 struct select_param * param;
1354 struct select_result * result;
1358 result = op->result;
1362 r = mailimap_select(param->imap, param->mb);
1365 debug_print("imap select run - end %i\n", r);
1368 int imap_threaded_select(Folder * folder, const char * mb,
1369 gint * exists, gint * recent, gint * unseen,
1370 guint32 * uid_validity,gint *can_create_flags,
1373 struct select_param param;
1374 struct select_result result;
1377 debug_print("imap select - begin\n");
1379 imap = get_imap(folder);
1383 threaded_run(folder, ¶m, &result, select_run);
1385 if (result.error != MAILIMAP_NO_ERROR)
1386 return result.error;
1388 if (!imap || imap->imap_selection_info == NULL)
1389 return MAILIMAP_ERROR_PARSE;
1391 * exists = imap->imap_selection_info->sel_exists;
1392 * recent = imap->imap_selection_info->sel_recent;
1393 * unseen = imap->imap_selection_info->sel_unseen;
1394 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1395 * can_create_flags = FALSE;
1397 if (imap->imap_selection_info->sel_perm_flags) {
1398 GSList *t_flags = NULL;
1399 clistiter *cur = NULL;
1400 if (imap->imap_selection_info->sel_perm_flags)
1401 cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
1403 for (; cur; cur = clist_next(cur)) {
1404 struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
1405 if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
1406 *can_create_flags = TRUE;
1407 else if (flag->fl_flag &&
1408 flag->fl_flag->fl_type == 6 &&
1409 !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
1410 *can_create_flags = TRUE;
1411 if (flag->fl_flag && ok_flags) {
1412 MsgPermFlags c_flag = 0;
1413 switch (flag->fl_flag->fl_type) {
1414 case MAILIMAP_FLAG_ANSWERED:
1415 c_flag = IMAP_FLAG_ANSWERED;
1417 case MAILIMAP_FLAG_FLAGGED:
1418 c_flag = IMAP_FLAG_FLAGGED;
1420 case MAILIMAP_FLAG_DELETED:
1421 c_flag = IMAP_FLAG_DELETED;
1423 case MAILIMAP_FLAG_DRAFT:
1424 c_flag = IMAP_FLAG_DRAFT;
1426 case MAILIMAP_FLAG_SEEN:
1427 c_flag = IMAP_FLAG_SEEN;
1429 case MAILIMAP_FLAG_KEYWORD:
1430 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
1431 c_flag = IMAP_FLAG_FORWARDED;
1432 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
1433 c_flag = IMAP_FLAG_SPAM;
1434 if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
1435 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
1436 !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
1437 c_flag = IMAP_FLAG_HAM;
1443 t_flags = g_slist_prepend(t_flags,
1444 GUINT_TO_POINTER(c_flag));
1449 *ok_flags = t_flags;
1451 debug_print("imap select - end\n");
1453 return result.error;
1456 static void close_run(struct etpan_thread_op * op)
1458 struct select_param * param;
1459 struct select_result * result;
1463 result = op->result;
1467 r = mailimap_close(param->imap);
1470 debug_print("imap close run - end %i\n", r);
1473 int imap_threaded_close(Folder * folder)
1475 struct select_param param;
1476 struct select_result result;
1479 debug_print("imap close - begin\n");
1481 imap = get_imap(folder);
1484 threaded_run(folder, ¶m, &result, close_run);
1486 if (result.error != MAILIMAP_NO_ERROR)
1487 return result.error;
1489 debug_print("imap close - end\n");
1491 return result.error;
1494 struct examine_param {
1499 struct examine_result {
1503 static void examine_run(struct etpan_thread_op * op)
1505 struct examine_param * param;
1506 struct examine_result * result;
1510 result = op->result;
1514 r = mailimap_examine(param->imap, param->mb);
1517 debug_print("imap examine run - end %i\n", r);
1520 int imap_threaded_examine(Folder * folder, const char * mb,
1521 gint * exists, gint * recent, gint * unseen,
1522 guint32 * uid_validity)
1524 struct examine_param param;
1525 struct examine_result result;
1528 debug_print("imap examine - begin\n");
1530 imap = get_imap(folder);
1534 threaded_run(folder, ¶m, &result, examine_run);
1536 if (result.error != MAILIMAP_NO_ERROR)
1537 return result.error;
1539 if (!imap || imap->imap_selection_info == NULL)
1540 return MAILIMAP_ERROR_PARSE;
1542 * exists = imap->imap_selection_info->sel_exists;
1543 * recent = imap->imap_selection_info->sel_recent;
1544 * unseen = imap->imap_selection_info->sel_unseen;
1545 * uid_validity = imap->imap_selection_info->sel_uidvalidity;
1547 debug_print("imap examine - end\n");
1549 return result.error;
1555 struct search_param {
1558 struct mailimap_set * set;
1561 struct search_result {
1563 clist * search_result;
1566 static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
1568 return mailimap_set_item_new(orig->set_first, orig->set_last);
1571 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
1573 clist *list = orig ? orig->set_list : NULL;
1574 clist *newlist = clist_new();
1579 for (cur = clist_begin(list); cur; cur = clist_next(cur))
1580 clist_append(newlist,
1581 sc_mailimap_set_item_copy(
1582 (struct mailimap_set_item *)clist_content(cur)));
1583 return mailimap_set_new(newlist);
1586 static void search_run(struct etpan_thread_op * op)
1588 struct search_param * param;
1589 struct search_result * result;
1591 struct mailimap_search_key * key = NULL;
1592 struct mailimap_search_key * uid_key = NULL;
1593 struct mailimap_search_key * search_type_key;
1594 clist * search_result;
1597 result = op->result;
1601 /* we copy the mailimap_set because freeing the key is recursive */
1602 if (param->set != NULL) {
1603 uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
1604 } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
1605 uid_key = mailimap_search_key_new_all();
1607 search_type_key = NULL;
1608 switch (param->type) {
1609 case IMAP_SEARCH_TYPE_SIMPLE:
1610 search_type_key = NULL;
1613 case IMAP_SEARCH_TYPE_SEEN:
1614 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
1615 NULL, NULL, NULL, NULL, NULL,
1616 NULL, NULL, NULL, NULL, NULL,
1617 NULL, NULL, NULL, NULL, 0,
1618 NULL, NULL, NULL, NULL, NULL,
1619 NULL, 0, NULL, NULL, NULL);
1622 case IMAP_SEARCH_TYPE_UNSEEN:
1623 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
1624 NULL, NULL, NULL, NULL, NULL,
1625 NULL, NULL, NULL, NULL, NULL,
1626 NULL, NULL, NULL, NULL, 0,
1627 NULL, NULL, NULL, NULL, NULL,
1628 NULL, 0, NULL, NULL, NULL);
1631 case IMAP_SEARCH_TYPE_ANSWERED:
1632 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
1633 NULL, NULL, NULL, NULL, NULL,
1634 NULL, NULL, NULL, NULL, NULL,
1635 NULL, NULL, NULL, NULL, 0,
1636 NULL, NULL, NULL, NULL, NULL,
1637 NULL, 0, NULL, NULL, NULL);
1640 case IMAP_SEARCH_TYPE_FLAGGED:
1641 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
1642 NULL, NULL, NULL, NULL, NULL,
1643 NULL, NULL, NULL, NULL, NULL,
1644 NULL, NULL, NULL, NULL, 0,
1645 NULL, NULL, NULL, NULL, NULL,
1646 NULL, 0, NULL, NULL, NULL);
1648 case IMAP_SEARCH_TYPE_DELETED:
1649 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
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);
1656 case IMAP_SEARCH_TYPE_FORWARDED:
1657 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1658 NULL, NULL, NULL, NULL, NULL,
1659 strdup(RTAG_FORWARDED), NULL, NULL, NULL, NULL,
1660 NULL, NULL, NULL, NULL, 0,
1661 NULL, NULL, NULL, NULL, NULL,
1662 NULL, 0, NULL, NULL, NULL);
1664 case IMAP_SEARCH_TYPE_SPAM:
1665 search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
1666 NULL, NULL, NULL, NULL, NULL,
1667 strdup(RTAG_JUNK), NULL, NULL, NULL, NULL,
1668 NULL, NULL, NULL, NULL, 0,
1669 NULL, NULL, NULL, NULL, NULL,
1670 NULL, 0, NULL, NULL, NULL);
1674 if (search_type_key != NULL) {
1675 if (param->set != NULL) {
1676 key = mailimap_search_key_new_multiple_empty();
1677 mailimap_search_key_multiple_add(key, search_type_key);
1678 mailimap_search_key_multiple_add(key, uid_key);
1680 key = search_type_key;
1682 } else if (uid_key != NULL) {
1687 g_warning("no key!");
1688 result = op->result;
1690 result->search_result = NULL;
1692 mailstream_logger = imap_logger_uid;
1694 r = mailimap_uid_search(param->imap, NULL, key, &search_result);
1696 mailstream_logger = imap_logger_cmd;
1698 /* free the key (with the imapset) */
1699 mailimap_search_key_free(key);
1702 result->search_result = search_result;
1704 debug_print("imap search run - end %i\n", result->error);
1707 int imap_threaded_search(Folder * folder, int search_type,
1708 struct mailimap_set * set, clist ** search_result)
1710 struct search_param param;
1711 struct search_result result;
1714 debug_print("imap search - begin\n");
1716 imap = get_imap(folder);
1719 param.type = search_type;
1721 threaded_run(folder, ¶m, &result, search_run);
1723 if (result.error != MAILIMAP_NO_ERROR)
1724 return result.error;
1726 debug_print("imap search - end\n");
1728 * search_result = result.search_result;
1730 return result.error;
1735 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
1739 struct mailimap_msg_att_dynamic ** patt_dyn);
1742 result_to_uid_list(clist * fetch_result, carray ** result)
1744 clistiter * cur = NULL;
1749 tab = carray_new(128);
1751 res = MAILIMAP_ERROR_MEMORY;
1756 cur = clist_begin(fetch_result);
1758 for(; cur != NULL ; cur = clist_next(cur)) {
1759 struct mailimap_msg_att * msg_att;
1763 msg_att = clist_content(cur);
1766 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
1768 puid = malloc(sizeof(* puid));
1770 res = MAILIMAP_ERROR_MEMORY;
1775 r = carray_add(tab, puid, NULL);
1778 res = MAILIMAP_ERROR_MEMORY;
1785 return MAILIMAP_NO_ERROR;
1788 imap_fetch_uid_list_free(tab);
1793 static int imap_get_messages_list(mailimap * imap,
1794 uint32_t first_index,
1799 struct mailimap_fetch_att * fetch_att;
1800 struct mailimap_fetch_type * fetch_type;
1801 struct mailimap_set * set;
1802 clist * fetch_result;
1805 set = mailimap_set_new_interval(first_index, 0);
1807 res = MAILIMAP_ERROR_MEMORY;
1811 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
1812 if (fetch_type == NULL) {
1813 res = MAILIMAP_ERROR_MEMORY;
1817 fetch_att = mailimap_fetch_att_new_uid();
1818 if (fetch_att == NULL) {
1819 res = MAILIMAP_ERROR_MEMORY;
1820 goto free_fetch_type;
1823 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
1824 if (r != MAILIMAP_NO_ERROR) {
1825 mailimap_fetch_att_free(fetch_att);
1826 res = MAILIMAP_ERROR_MEMORY;
1827 goto free_fetch_type;
1830 mailstream_logger = imap_logger_fetch;
1832 r = mailimap_uid_fetch(imap, set,
1833 fetch_type, &fetch_result);
1835 mailstream_logger = imap_logger_cmd;
1836 mailimap_fetch_type_free(fetch_type);
1837 mailimap_set_free(set);
1839 if (r != MAILIMAP_NO_ERROR) {
1845 r = result_to_uid_list(fetch_result, &env_list);
1846 mailimap_fetch_list_free(fetch_result);
1848 * result = env_list;
1850 return MAILIMAP_NO_ERROR;
1853 mailimap_fetch_type_free(fetch_type);
1855 mailimap_set_free(set);
1863 struct fetch_uid_param {
1865 uint32_t first_index;
1868 struct fetch_uid_result {
1870 carray * fetch_result;
1873 static void fetch_uid_run(struct etpan_thread_op * op)
1875 struct fetch_uid_param * param;
1876 struct fetch_uid_result * result;
1877 carray * fetch_result;
1881 result = op->result;
1885 fetch_result = NULL;
1886 mailstream_logger = imap_logger_noop;
1887 log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
1889 r = imap_get_messages_list(param->imap, param->first_index,
1892 mailstream_logger = imap_logger_cmd;
1895 result->fetch_result = fetch_result;
1896 debug_print("imap fetch_uid run - end %i\n", r);
1899 int imap_threaded_fetch_uid(Folder * folder, uint32_t first_index,
1900 carray ** fetch_result)
1902 struct fetch_uid_param param;
1903 struct fetch_uid_result result;
1906 debug_print("imap fetch_uid - begin\n");
1908 imap = get_imap(folder);
1910 param.first_index = first_index;
1912 threaded_run(folder, ¶m, &result, fetch_uid_run);
1914 if (result.error != MAILIMAP_NO_ERROR)
1915 return result.error;
1917 debug_print("imap fetch_uid - end\n");
1919 * fetch_result = result.fetch_result;
1921 return result.error;
1925 void imap_fetch_uid_list_free(carray * uid_list)
1929 for(i = 0 ; i < carray_count(uid_list) ; i ++) {
1932 puid = carray_get(uid_list, i);
1935 carray_free(uid_list);
1941 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
1944 result_to_uid_flags_list(clist * fetch_result, carray ** result)
1946 clistiter * cur = NULL;
1950 GSList *tags = NULL;
1952 tab = carray_new(128);
1954 res = MAILIMAP_ERROR_MEMORY;
1959 cur = clist_begin(fetch_result);
1961 for(; cur != NULL ; cur = clist_next(cur)) {
1962 struct mailimap_msg_att * msg_att;
1965 struct mailimap_msg_att_dynamic * att_dyn;
1971 msg_att = clist_content(cur);
1975 imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
1978 if (att_dyn == NULL)
1981 flags = imap_flags_to_flags(att_dyn, &tags);
1983 puid = malloc(sizeof(* puid));
1985 res = MAILIMAP_ERROR_MEMORY;
1990 r = carray_add(tab, puid, NULL);
1993 res = MAILIMAP_ERROR_MEMORY;
1996 pflags = malloc(sizeof(* pflags));
1997 if (pflags == NULL) {
1998 res = MAILIMAP_ERROR_MEMORY;
2002 r = carray_add(tab, pflags, NULL);
2005 res = MAILIMAP_ERROR_MEMORY;
2008 r = carray_add(tab, tags, NULL);
2011 res = MAILIMAP_ERROR_MEMORY;
2018 return MAILIMAP_NO_ERROR;
2021 imap_fetch_uid_flags_list_free(tab);
2022 slist_free_strings(tags);
2028 static int imap_get_messages_flags_list(mailimap * imap,
2029 uint32_t first_index,
2034 struct mailimap_fetch_att * fetch_att;
2035 struct mailimap_fetch_type * fetch_type;
2036 struct mailimap_set * set;
2037 clist * fetch_result;
2040 set = mailimap_set_new_interval(first_index, 0);
2042 res = MAILIMAP_ERROR_MEMORY;
2046 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2047 if (fetch_type == NULL) {
2048 res = MAILIMAP_ERROR_MEMORY;
2052 fetch_att = mailimap_fetch_att_new_flags();
2053 if (fetch_att == NULL) {
2054 res = MAILIMAP_ERROR_MEMORY;
2055 goto free_fetch_type;
2058 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2059 if (r != MAILIMAP_NO_ERROR) {
2060 mailimap_fetch_att_free(fetch_att);
2061 res = MAILIMAP_ERROR_MEMORY;
2062 goto free_fetch_type;
2065 fetch_att = mailimap_fetch_att_new_uid();
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 mailstream_logger = imap_logger_fetch;
2080 r = mailimap_uid_fetch(imap, set,
2081 fetch_type, &fetch_result);
2083 mailstream_logger = imap_logger_cmd;
2084 mailimap_fetch_type_free(fetch_type);
2085 mailimap_set_free(set);
2087 if (r != MAILIMAP_NO_ERROR) {
2093 r = result_to_uid_flags_list(fetch_result, &env_list);
2094 mailimap_fetch_list_free(fetch_result);
2096 * result = env_list;
2098 return MAILIMAP_NO_ERROR;
2101 mailimap_fetch_type_free(fetch_type);
2103 mailimap_set_free(set);
2110 static void fetch_uid_flags_run(struct etpan_thread_op * op)
2112 struct fetch_uid_param * param;
2113 struct fetch_uid_result * result;
2114 carray * fetch_result;
2118 result = op->result;
2122 fetch_result = NULL;
2123 r = imap_get_messages_flags_list(param->imap, param->first_index,
2127 result->fetch_result = fetch_result;
2128 debug_print("imap fetch_uid run - end %i\n", r);
2131 int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
2132 carray ** fetch_result)
2134 struct fetch_uid_param param;
2135 struct fetch_uid_result result;
2138 debug_print("imap fetch_uid - begin\n");
2140 imap = get_imap(folder);
2142 param.first_index = first_index;
2144 mailstream_logger = imap_logger_noop;
2145 log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
2147 threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
2149 mailstream_logger = imap_logger_cmd;
2152 if (result.error != MAILIMAP_NO_ERROR)
2153 return result.error;
2155 debug_print("imap fetch_uid - end\n");
2157 * fetch_result = result.fetch_result;
2159 return result.error;
2163 void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
2167 for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
2170 data = carray_get(uid_flags_list, i);
2172 data = carray_get(uid_flags_list, i + 1);
2175 carray_free(uid_flags_list);
2180 static int imap_fetch(mailimap * imap,
2183 size_t * result_len)
2186 struct mailimap_set * set;
2187 struct mailimap_fetch_att * fetch_att;
2188 struct mailimap_fetch_type * fetch_type;
2189 clist * fetch_result;
2190 struct mailimap_msg_att * msg_att;
2191 struct mailimap_msg_att_item * msg_att_item;
2196 struct mailimap_section * section;
2198 set = mailimap_set_new_single(msg_index);
2200 res = MAILIMAP_ERROR_MEMORY;
2204 section = mailimap_section_new(NULL);
2205 if (section == NULL) {
2206 res = MAILIMAP_ERROR_MEMORY;
2210 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2211 if (fetch_att == NULL) {
2212 mailimap_section_free(section);
2213 res = MAILIMAP_ERROR_MEMORY;
2217 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2218 if (fetch_type == NULL) {
2219 res = MAILIMAP_ERROR_MEMORY;
2220 goto free_fetch_att;
2223 mailstream_logger = imap_logger_fetch;
2225 r = mailimap_uid_fetch(imap, set,
2226 fetch_type, &fetch_result);
2228 mailstream_logger = imap_logger_cmd;
2230 mailimap_fetch_type_free(fetch_type);
2231 mailimap_set_free(set);
2234 case MAILIMAP_NO_ERROR:
2240 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2241 mailimap_fetch_list_free(fetch_result);
2242 return MAILIMAP_ERROR_FETCH;
2245 msg_att = clist_begin(fetch_result)->data;
2250 if (msg_att->att_list)
2251 cur = clist_begin(msg_att->att_list);
2255 for(; cur != NULL ; cur = clist_next(cur)) {
2256 msg_att_item = clist_content(cur);
2258 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2259 if (msg_att_item->att_data.att_static->att_type ==
2260 MAILIMAP_MSG_ATT_BODY_SECTION) {
2261 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2263 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2265 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2270 mailimap_fetch_list_free(fetch_result);
2273 return MAILIMAP_ERROR_FETCH;
2276 * result_len = text_length;
2278 return MAILIMAP_NO_ERROR;
2281 mailimap_fetch_att_free(fetch_att);
2283 mailimap_set_free(set);
2288 static int imap_fetch_header(mailimap * imap,
2291 size_t * result_len)
2294 struct mailimap_set * set;
2295 struct mailimap_fetch_att * fetch_att;
2296 struct mailimap_fetch_type * fetch_type;
2297 clist * fetch_result;
2298 struct mailimap_msg_att * msg_att;
2299 struct mailimap_msg_att_item * msg_att_item;
2304 struct mailimap_section * section;
2306 set = mailimap_set_new_single(msg_index);
2308 res = MAILIMAP_ERROR_MEMORY;
2312 section = mailimap_section_new_header();
2313 if (section == NULL) {
2314 res = MAILIMAP_ERROR_MEMORY;
2318 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2319 if (fetch_att == NULL) {
2320 mailimap_section_free(section);
2321 res = MAILIMAP_ERROR_MEMORY;
2325 fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
2326 if (fetch_type == NULL) {
2327 res = MAILIMAP_ERROR_MEMORY;
2328 goto free_fetch_att;
2331 mailstream_logger = imap_logger_fetch;
2333 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2335 mailstream_logger = imap_logger_cmd;
2336 mailimap_fetch_type_free(fetch_type);
2337 mailimap_set_free(set);
2340 case MAILIMAP_NO_ERROR:
2346 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2347 mailimap_fetch_list_free(fetch_result);
2348 return MAILIMAP_ERROR_FETCH;
2351 msg_att = clist_begin(fetch_result)->data;
2356 if (msg_att->att_list)
2357 cur = clist_begin(msg_att->att_list);
2361 for(; cur != NULL ; cur = clist_next(cur)) {
2362 msg_att_item = clist_content(cur);
2364 if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
2365 if (msg_att_item->att_data.att_static->att_type ==
2366 MAILIMAP_MSG_ATT_BODY_SECTION) {
2367 text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
2368 msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
2370 msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
2375 mailimap_fetch_list_free(fetch_result);
2378 return MAILIMAP_ERROR_FETCH;
2381 * result_len = text_length;
2383 return MAILIMAP_NO_ERROR;
2386 mailimap_fetch_att_free(fetch_att);
2388 mailimap_set_free(set);
2395 struct fetch_content_param {
2398 const char * filename;
2402 struct fetch_content_result {
2406 static void fetch_content_run(struct etpan_thread_op * op)
2408 struct fetch_content_param * param;
2409 struct fetch_content_result * result;
2411 size_t content_size;
2417 result = op->result;
2423 if (param->with_body)
2424 r = imap_fetch(param->imap, param->msg_index,
2425 &content, &content_size);
2427 r = imap_fetch_header(param->imap, param->msg_index,
2428 &content, &content_size);
2432 if (r == MAILIMAP_NO_ERROR) {
2433 fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
2435 result->error = MAILIMAP_ERROR_FETCH;
2439 f = fdopen(fd, "wb");
2441 result->error = MAILIMAP_ERROR_FETCH;
2445 r = fwrite(content, 1, content_size, f);
2446 if (r < content_size) {
2447 result->error = MAILIMAP_ERROR_FETCH;
2453 result->error = MAILIMAP_ERROR_FETCH;
2464 claws_unlink(param->filename);
2467 /* mmap_string_unref is a simple free in libetpan
2468 * when it has MMAP_UNAVAILABLE defined */
2469 if (mmap_string_unref(content) != 0)
2473 debug_print("imap fetch_content run - end %i\n", result->error);
2476 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
2478 const char * filename)
2480 struct fetch_content_param param;
2481 struct fetch_content_result result;
2484 debug_print("imap fetch_content - begin\n");
2486 imap = get_imap(folder);
2488 param.msg_index = msg_index;
2489 param.filename = filename;
2490 param.with_body = with_body;
2492 threaded_run(folder, ¶m, &result, fetch_content_run);
2494 if (result.error != MAILIMAP_NO_ERROR)
2495 return result.error;
2497 debug_print("imap fetch_content - end\n");
2499 return result.error;
2504 static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
2509 GSList *tags = NULL;
2513 flag_list = att_dyn->att_list;
2514 if (flag_list == NULL)
2517 for(cur = clist_begin(flag_list) ; cur != NULL ;
2518 cur = clist_next(cur)) {
2519 struct mailimap_flag_fetch * flag_fetch;
2521 flag_fetch = clist_content(cur);
2522 if (flag_fetch->fl_type == MAILIMAP_FLAG_FETCH_RECENT)
2525 switch (flag_fetch->fl_flag->fl_type) {
2526 case MAILIMAP_FLAG_ANSWERED:
2527 flags |= MSG_REPLIED;
2529 case MAILIMAP_FLAG_FLAGGED:
2530 flags |= MSG_MARKED;
2532 case MAILIMAP_FLAG_DELETED:
2533 flags |= MSG_DELETED;
2535 case MAILIMAP_FLAG_SEEN:
2536 flags &= ~MSG_UNREAD;
2539 case MAILIMAP_FLAG_KEYWORD:
2540 if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
2541 flags |= MSG_FORWARDED;
2542 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
2544 else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
2545 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
2546 !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
2549 tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
2559 static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
2563 struct mailimap_msg_att_dynamic ** patt_dyn)
2565 clistiter * item_cur;
2569 struct mailimap_msg_att_dynamic * att_dyn;
2576 if (msg_att->att_list)
2577 item_cur = clist_begin(msg_att->att_list);
2580 for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
2581 struct mailimap_msg_att_item * item;
2583 item = clist_content(item_cur);
2585 switch (item->att_type) {
2586 case MAILIMAP_MSG_ATT_ITEM_STATIC:
2587 switch (item->att_data.att_static->att_type) {
2588 case MAILIMAP_MSG_ATT_UID:
2589 uid = item->att_data.att_static->att_data.att_uid;
2592 case MAILIMAP_MSG_ATT_BODY_SECTION:
2593 if (headers == NULL) {
2594 headers = item->att_data.att_static->att_data.att_body_section->sec_body_part;
2597 case MAILIMAP_MSG_ATT_RFC822_SIZE:
2598 ref_size = item->att_data.att_static->att_data.att_rfc822_size;
2603 case MAILIMAP_MSG_ATT_ITEM_DYNAMIC:
2604 if (att_dyn == NULL) {
2605 att_dyn = item->att_data.att_dyn;
2613 if (pheaders != NULL)
2614 * pheaders = headers;
2615 if (pref_size != NULL)
2616 * pref_size = ref_size;
2617 if (patt_dyn != NULL)
2618 * patt_dyn = att_dyn;
2620 return MAIL_NO_ERROR;
2623 static struct imap_fetch_env_info *
2624 fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
2626 struct imap_fetch_env_info * info;
2630 struct mailimap_msg_att_dynamic * att_dyn;
2632 imap_get_msg_att_info(msg_att, &uid, &headers, &size,
2637 info = malloc(sizeof(* info));
2639 info->headers = strdup(headers);
2641 info->flags = imap_flags_to_flags(att_dyn, tags);
2647 imap_fetch_result_to_envelop_list(clist * fetch_result,
2648 carray ** p_env_list)
2655 env_list = carray_new(16);
2658 for(cur = clist_begin(fetch_result) ; cur != NULL ;
2659 cur = clist_next(cur)) {
2660 struct mailimap_msg_att * msg_att;
2661 struct imap_fetch_env_info * env_info;
2662 GSList *tags = NULL;
2664 msg_att = clist_content(cur);
2666 env_info = fetch_to_env_info(msg_att, &tags);
2668 return MAILIMAP_ERROR_MEMORY;
2669 carray_add(env_list, env_info, NULL);
2670 carray_add(env_list, tags, NULL);
2672 * p_env_list = env_list;
2674 * p_env_list = NULL;
2677 return MAIL_NO_ERROR;
2680 static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
2682 struct mailimap_fetch_att * fetch_att;
2686 struct mailimap_header_list * imap_hdrlist;
2687 struct mailimap_section * section;
2689 hdrlist = clist_new();
2691 header = strdup("Date");
2692 r = clist_append(hdrlist, header);
2693 header = strdup("From");
2694 r = clist_append(hdrlist, header);
2695 header = strdup("To");
2696 r = clist_append(hdrlist, header);
2697 header = strdup("Cc");
2698 r = clist_append(hdrlist, header);
2699 header = strdup("Subject");
2700 r = clist_append(hdrlist, header);
2701 header = strdup("Message-ID");
2702 r = clist_append(hdrlist, header);
2703 header = strdup("References");
2704 r = clist_append(hdrlist, header);
2705 header = strdup("In-Reply-To");
2706 r = clist_append(hdrlist, header);
2708 imap_hdrlist = mailimap_header_list_new(hdrlist);
2709 section = mailimap_section_new_header_fields(imap_hdrlist);
2710 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2711 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2713 return MAIL_NO_ERROR;
2716 static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
2718 struct mailimap_fetch_att * fetch_att;
2719 struct mailimap_section * section;
2721 section = mailimap_section_new_header();
2722 fetch_att = mailimap_fetch_att_new_body_peek_section(section);
2723 mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2725 return MAIL_NO_ERROR;
2729 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
2730 carray ** p_env_list)
2732 struct mailimap_fetch_att * fetch_att;
2733 struct mailimap_fetch_type * fetch_type;
2735 clist * fetch_result;
2737 carray * env_list = NULL;
2741 fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
2744 fetch_att = mailimap_fetch_att_new_uid();
2745 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2748 fetch_att = mailimap_fetch_att_new_flags();
2749 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2752 fetch_att = mailimap_fetch_att_new_rfc822_size();
2753 r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
2757 key.len = sizeof(imap);
2758 r = chash_get(courier_workaround_hash, &key, &value);
2760 r = imap_add_envelope_fetch_att(fetch_type);
2762 r = imap_add_header_fetch_att(fetch_type);
2764 mailstream_logger = imap_logger_fetch;
2766 r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
2768 mailstream_logger = imap_logger_cmd;
2770 case MAILIMAP_NO_ERROR:
2773 mailimap_fetch_type_free(fetch_type);
2774 debug_print("uid_fetch: %d\n", r);
2778 if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
2779 res = MAILIMAP_ERROR_FETCH;
2780 debug_print("clist_begin = NULL\n");
2784 r = imap_fetch_result_to_envelop_list(fetch_result, &env_list);
2785 mailimap_fetch_list_free(fetch_result);
2787 if (r != MAILIMAP_NO_ERROR) {
2788 mailimap_fetch_type_free(fetch_type);
2789 res = MAILIMAP_ERROR_MEMORY;
2790 debug_print("fetch_result_to_envelop_list: %d\n", res);
2794 mailimap_fetch_type_free(fetch_type);
2796 * p_env_list = env_list;
2798 return MAILIMAP_NO_ERROR;
2804 struct fetch_env_param {
2806 struct mailimap_set * set;
2809 struct fetch_env_result {
2810 carray * fetch_env_result;
2814 static void fetch_env_run(struct etpan_thread_op * op)
2816 struct fetch_env_param * param;
2817 struct fetch_env_result * result;
2822 result = op->result;
2827 r = imap_get_envelopes_list(param->imap, param->set,
2831 result->fetch_env_result = env_list;
2833 debug_print("imap fetch_env run - end %i\n", r);
2836 int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
2837 carray ** p_env_list)
2839 struct fetch_env_param param;
2840 struct fetch_env_result result;
2843 debug_print("imap fetch_env - begin\n");
2845 imap = get_imap(folder);
2849 threaded_run(folder, ¶m, &result, fetch_env_run);
2851 if (result.error != MAILIMAP_NO_ERROR) {
2857 key.len = sizeof(imap);
2858 r = chash_get(courier_workaround_hash, &key, &value);
2862 chash_set(courier_workaround_hash, &key, &value, NULL);
2864 threaded_run(folder, ¶m, &result, fetch_env_run);
2868 if (result.error != MAILIMAP_NO_ERROR)
2869 return result.error;
2871 debug_print("imap fetch_env - end\n");
2873 * p_env_list = result.fetch_env_result;
2875 return result.error;
2878 void imap_fetch_env_free(carray * env_list)
2882 for(i = 0 ; i < carray_count(env_list) ; i += 2) {
2883 struct imap_fetch_env_info * env_info;
2885 env_info = carray_get(env_list, i);
2886 free(env_info->headers);
2889 carray_free(env_list);
2896 struct append_param {
2898 const char * mailbox;
2899 const char * filename;
2900 struct mailimap_flag_list * flag_list;
2903 struct append_result {
2908 static void append_run(struct etpan_thread_op * op)
2910 struct append_param * param;
2911 struct append_result * result;
2916 struct stat stat_buf;
2919 guint32 uid = 0, val = 0;
2922 result = op->result;
2927 r = stat(param->filename, &stat_buf);
2929 result->error = MAILIMAP_ERROR_APPEND;
2932 size = stat_buf.st_size;
2934 fd = g_open(param->filename, O_RDONLY, 0);
2936 result->error = MAILIMAP_ERROR_APPEND;
2940 data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2941 if (data == (void *) MAP_FAILED) {
2943 result->error = MAILIMAP_ERROR_APPEND;
2947 data = file_read_to_str_no_recode(param->filename);
2949 result->error = MAILIMAP_ERROR_APPEND;
2952 size = strlen(data);
2954 mailstream_logger = imap_logger_append;
2956 r = mailimap_uidplus_append(param->imap, param->mailbox,
2957 param->flag_list, NULL,
2958 data, size, &val, &uid);
2960 mailstream_logger = imap_logger_cmd;
2971 debug_print("imap append run - end %i uid %d\n", r, uid);
2974 int imap_threaded_append(Folder * folder, const char * mailbox,
2975 const char * filename,
2976 struct mailimap_flag_list * flag_list,
2979 struct append_param param;
2980 struct append_result result;
2983 debug_print("imap append - begin\n");
2985 imap = get_imap(folder);
2987 param.mailbox = mailbox;
2988 param.filename = filename;
2989 param.flag_list = flag_list;
2991 threaded_run(folder, ¶m, &result, append_run);
2993 if (result.error != MAILIMAP_NO_ERROR)
2994 return result.error;
2996 debug_print("imap append - end\n");
3000 return result.error;
3006 struct expunge_param {
3010 struct expunge_result {
3014 static void expunge_run(struct etpan_thread_op * op)
3016 struct expunge_param * param;
3017 struct expunge_result * result;
3021 result = op->result;
3025 r = mailimap_expunge(param->imap);
3028 debug_print("imap expunge run - end %i\n", r);
3031 int imap_threaded_expunge(Folder * folder)
3033 struct expunge_param param;
3034 struct expunge_result result;
3036 debug_print("imap expunge - begin\n");
3038 param.imap = get_imap(folder);
3040 threaded_run(folder, ¶m, &result, expunge_run);
3042 debug_print("imap expunge - end\n");
3044 return result.error;
3050 struct mailimap_set * set;
3054 struct copy_result {
3056 struct mailimap_set *source;
3057 struct mailimap_set *dest;
3060 static void copy_run(struct etpan_thread_op * op)
3062 struct copy_param * param;
3063 struct copy_result * result;
3066 struct mailimap_set *source = NULL, *dest = NULL;
3069 result = op->result;
3073 r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
3074 &val, &source, &dest);
3078 result->source = source;
3079 result->dest = dest;
3081 result->source = NULL;
3082 result->dest = NULL;
3084 debug_print("imap copy run - end %i\n", r);
3087 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
3088 const char * mb, struct mailimap_set **source,
3089 struct mailimap_set **dest)
3091 struct copy_param param;
3092 struct copy_result result;
3095 debug_print("imap copy - begin\n");
3097 imap = get_imap(folder);
3102 threaded_run(folder, ¶m, &result, copy_run);
3106 if (result.error != MAILIMAP_NO_ERROR)
3107 return result.error;
3109 *source = result.source;
3110 *dest = result.dest;
3112 debug_print("imap copy - end\n");
3114 return result.error;
3119 struct store_param {
3121 struct mailimap_set * set;
3122 struct mailimap_store_att_flags * store_att_flags;
3125 struct store_result {
3129 static void store_run(struct etpan_thread_op * op)
3131 struct store_param * param;
3132 struct store_result * result;
3136 result = op->result;
3140 r = mailimap_uid_store(param->imap, param->set,
3141 param->store_att_flags);
3145 debug_print("imap store run - end %i\n", r);
3148 int imap_threaded_store(Folder * folder, struct mailimap_set * set,
3149 struct mailimap_store_att_flags * store_att_flags)
3151 struct store_param param;
3152 struct store_result result;
3155 debug_print("imap store - begin\n");
3157 imap = get_imap(folder);
3160 param.store_att_flags = store_att_flags;
3162 threaded_run(folder, ¶m, &result, store_run);
3164 if (result.error != MAILIMAP_NO_ERROR)
3165 return result.error;
3167 debug_print("imap store - end\n");
3169 return result.error;
3173 #define ENV_BUFFER_SIZE 512
3175 static void do_exec_command(int fd, const char * command,
3176 const char * servername, uint16_t port)
3180 char env_buffer[ENV_BUFFER_SIZE];
3184 /* Fork again to become a child of init rather than
3185 the etpan client. */
3190 g_setenv("ETPANSERVER", servername, TRUE);
3192 g_unsetenv("ETPANSERVER");
3197 snprintf(porttext, sizeof(porttext), "%d", port);
3198 g_setenv("ETPANPORT", porttext, TRUE);
3201 g_unsetenv("ETPANPORT");
3204 /* Not a lot we can do if there's an error other than bail. */
3205 if (dup2(fd, 0) == -1)
3207 if (dup2(fd, 1) == -1)
3210 /* Should we close stderr and reopen /dev/null? */
3212 maxopen = sysconf(_SC_OPEN_MAX);
3213 for (i=3; i < maxopen; i++)
3217 /* Detach from the controlling tty if we have one. Otherwise,
3218 SSH might do something stupid like trying to use it instead
3219 of running $SSH_ASKPASS. Doh. */
3220 fd = g_open("/dev/tty", O_RDONLY, 0);
3222 ioctl(fd, TIOCNOTTY, NULL);
3225 #endif /* TIOCNOTTY */
3227 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
3229 /* Eep. Shouldn't reach this */
3233 static int subcommand_connect(const char *command,
3234 const char *servername, uint16_t port)
3236 /* SEB unsupported on Windows */
3240 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds))
3245 do_exec_command(sockfds[1], command, servername, port);
3247 else if (childpid == -1) {
3255 /* Reap child, leaving grandchild process to run */
3256 waitpid(childpid, NULL, 0);
3261 static int socket_connect_cmd(mailimap * imap, const char * command,
3262 const char * server, int port)
3268 fd = subcommand_connect(command, server, port);
3270 return MAILIMAP_ERROR_STREAM;
3272 s = mailstream_socket_open(fd);
3275 return MAILIMAP_ERROR_STREAM;
3278 r = mailimap_connect(imap, s);
3279 if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
3280 && r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
3281 mailstream_close(s);
3283 imap->imap_stream = NULL;
3292 struct connect_cmd_param {
3294 const char * command;
3295 const char * server;
3299 struct connect_cmd_result {
3303 static void connect_cmd_run(struct etpan_thread_op * op)
3306 struct connect_cmd_param * param;
3307 struct connect_cmd_result * result;
3310 result = op->result;
3314 r = socket_connect_cmd(param->imap, param->command,
3315 param->server, param->port);
3321 int imap_threaded_connect_cmd(Folder * folder, const char * command,
3322 const char * server, int port)
3324 struct connect_cmd_param param;
3325 struct connect_cmd_result result;
3328 mailimap * imap, * oldimap;
3330 oldimap = get_imap(folder);
3332 imap = mailimap_new(0, NULL);
3335 debug_print("deleting old imap %p\n", oldimap);
3336 delete_imap(folder, oldimap);
3340 key.len = sizeof(folder);
3343 chash_set(session_hash, &key, &value, NULL);
3346 param.command = command;
3347 param.server = server;
3350 threaded_run(folder, ¶m, &result, connect_cmd_run);
3352 debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
3354 return result.error;
3356 #endif /* G_OS_WIN32 */
3358 void imap_threaded_cancel(Folder * folder)
3362 imap = get_imap(folder);
3363 if (imap->imap_stream != NULL)
3364 mailstream_cancel(imap->imap_stream);
3369 void imap_main_init(void)
3372 void imap_main_done(gboolean have_connectivity)
3375 void imap_main_set_timeout(int sec)
3379 void imap_threaded_cancel(Folder * folder);