2007-08-08 [colin] 2.10.0cvs101
[claws.git] / src / etpan / imap-thread.c
index 99c19197a35e82acefe42faa9cf451a7515cefac..ddb6757ac8a020ea9f875e41f4ee51367dac8448 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #endif
 #include <log.h>
 #include "etpan-thread-manager.h"
 #include "utils.h"
+#include "mainwindow.h"
 #include "ssl_certificate.h"
 #include "socket.h"
+#include "remotefolder.h"
 
 #define DISABLE_LOG_DURING_LOGIN
 
@@ -31,6 +52,28 @@ static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
 static GIOChannel * io_channel = NULL;
 
+static void delete_imap(Folder *folder, mailimap *imap)
+{
+       chashdatum key;
+       chashdatum value;
+
+       key.data = &folder;
+       key.len = sizeof(folder);
+       value.data = imap;
+       value.len = 0;
+       chash_delete(session_hash, &key, NULL);
+       
+       key.data = &imap;
+       key.len = sizeof(imap);
+       chash_delete(courier_workaround_hash, &key, NULL);
+       if (imap && imap->imap_stream) {
+               /* we don't want libetpan to logout */
+               mailstream_close(imap->imap_stream);
+               imap->imap_stream = NULL;
+       }
+       debug_print("removing mailimap %p\n", imap);
+       mailimap_free(imap);    
+}
 
 static gboolean thread_manager_event(GIOChannel * source,
     GIOCondition condition,
@@ -47,6 +90,10 @@ static void imap_logger_cmd(int direction, const char * str, size_t size)
        gchar **lines;
        int i = 0;
 
+       if (size > 8192) {
+               log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
+               return;
+       }
        buf = malloc(size+1);
        memset(buf, 0, size+1);
        strncpy(buf, str, size);
@@ -65,7 +112,7 @@ static void imap_logger_cmd(int direction, const char * str, size_t size)
        lines = g_strsplit(buf, "\n", -1);
 
        while (lines[i] && *lines[i]) {
-               log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
                i++;
        }
        g_strfreev(lines);
@@ -78,6 +125,11 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
        gchar **lines;
        int i = 0;
 
+       if (size > 8192) {
+               log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
+               return;
+       }
+       
        buf = malloc(size+1);
        memset(buf, 0, size+1);
        strncpy(buf, str, size);
@@ -96,11 +148,11 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
 
        if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
                while (lines[i] && *lines[i]) {
-                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
                        i++;
                }
        } else {
-               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
        }
        g_strfreev(lines);
        free(buf);
@@ -112,6 +164,10 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        gchar **lines;
        int i = 0;
 
+       if (size > 8192) {
+               log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
+               return;
+       }
        buf = malloc(size+1);
        memset(buf, 0, size+1);
        strncpy(buf, str, size);
@@ -131,11 +187,11 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        while (lines[i] && *lines[i]) {
                int llen = strlen(lines[i]);
                if (llen < 64)
-                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
                else {
                        gchar tmp[64];
                        strncpy2(tmp, lines[i], 63);
-                       log_print("IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
+                       log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
                                  llen-64);
                }
                i++;
@@ -144,12 +200,19 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        free(buf);
 }
 
-void imap_logger_append(int direction, const char * str, size_t size) 
+static void imap_logger_append(int direction, const char * str, size_t size) 
 {
        gchar *buf;
        gchar **lines;
        int i = 0;
 
+       if (size > 8192) {
+               log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+               return;
+       } else if (direction == 0 && size > 64) {
+               log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+               return;
+       } 
        buf = malloc(size+1);
        memset(buf, 0, size+1);
        strncpy(buf, str, size);
@@ -168,11 +231,11 @@ void imap_logger_append(int direction, const char * str, size_t size)
 
        if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
                while (lines[i] && *lines[i]) {
-                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
                        i++;
                }
        } else {
-               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
        }
        g_strfreev(lines);
        free(buf);
@@ -180,6 +243,7 @@ void imap_logger_append(int direction, const char * str, size_t size)
 
 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
 static gboolean etpan_skip_ssl_cert_check = FALSE;
+extern void mailsasl_ref(void);
 
 void imap_main_init(gboolean skip_ssl_cert_check)
 {
@@ -191,7 +255,8 @@ void imap_main_init(gboolean skip_ssl_cert_check)
        
        mailstream_debug = 1;
        mailstream_logger = imap_logger_cmd;
-
+       mailsasl_ref();
+       
        imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
@@ -298,20 +363,25 @@ static mailimap * get_imap(Folder * folder)
                return NULL;
        
        imap = value.data;
-       
+       debug_print("found imap %p\n", imap);
        return imap;
 }
 
 
 static void generic_cb(int cancelled, void * result, void * callback_data)
 {
-       int * p_finished;
+       struct etpan_thread_op * op;
        
-       p_finished = callback_data;
+       op = (struct etpan_thread_op *) callback_data;
 
        debug_print("generic_cb\n");
-       
-       * p_finished = 1;
+       if (op->imap && op->imap->imap_response_info &&
+           op->imap->imap_response_info->rsp_alert) {
+               log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n", 
+                       op->imap->imap_response_info->rsp_alert);
+               mainwindow_show_error();
+       } 
+       op->finished = 1;
 }
 
 static void threaded_run(Folder * folder, void * param, void * result,
@@ -319,26 +389,27 @@ static void threaded_run(Folder * folder, void * param, void * result,
 {
        struct etpan_thread_op * op;
        struct etpan_thread * thread;
-       int finished;
        
        imap_folder_ref(folder);
 
        op = etpan_thread_op_new();
+       
+       op->imap = get_imap(folder);
        op->param = param;
        op->result = result;
        
        op->cancellable = 0;
        op->run = func;
        op->callback = generic_cb;
-       op->callback_data = &finished;
+       op->callback_data = op;
        op->cleanup = NULL;
        
-       finished = 0;
+       op->finished = 0;
        
        thread = get_thread(folder);
        etpan_thread_op_schedule(thread, op);
        
-       while (!finished) {
+       while (!op->finished) {
                gtk_main_iteration();
        }
        
@@ -360,6 +431,13 @@ struct connect_result {
        int error;
 };
 
+#define CHECK_IMAP() {                                         \
+       if (!param->imap) {                                     \
+               result->error = MAILIMAP_ERROR_BAD_STATE;       \
+               return;                                         \
+       }                                                       \
+}
+
 static void connect_run(struct etpan_thread_op * op)
 {
        int r;
@@ -369,6 +447,8 @@ static void connect_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = mailimap_socket_connect(param->imap,
                                    param->server, param->port);
        
@@ -382,10 +462,17 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        struct connect_result result;
        chashdatum key;
        chashdatum value;
-       mailimap * imap;
+       mailimap * imap, * oldimap;
        
+       oldimap = get_imap(folder);
+
        imap = mailimap_new(0, NULL);
        
+       if (oldimap) {
+               debug_print("deleting old imap %p\n", oldimap);
+               delete_imap(folder, oldimap);
+       }
+       
        key.data = &folder;
        key.len = sizeof(folder);
        value.data = imap;
@@ -399,7 +486,7 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_run);
        
-       debug_print("connect ok %i\n", result.error);
+       debug_print("connect ok %i with imap %p\n", result.error, imap);
        
        return result.error;
 }
@@ -409,12 +496,12 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
 #ifdef USE_OPENSSL
        struct connect_param *param = (struct connect_param *)data;
        X509 *cert = NULL;
-
+       
        if (certificate == NULL || len < 0) {
                g_warning("no cert presented.\n");
                return 0;
        }
-       cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
+       cert = d2i_X509(NULL, &certificate, len);
        if (cert == NULL) {
                g_warning("can't get cert\n");
                return 0;
@@ -440,6 +527,8 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = mailimap_ssl_connect(param->imap,
                                 param->server, param->port);
        result->error = r;
@@ -451,12 +540,19 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        struct connect_result result;
        chashdatum key;
        chashdatum value;
-       mailimap * imap;
+       mailimap * imap, * oldimap;
        unsigned char *certificate = NULL;
        int cert_len;
+       
+       oldimap = get_imap(folder);
 
        imap = mailimap_new(0, NULL);
        
+       if (oldimap) {
+               debug_print("deleting old imap %p\n", oldimap);
+               delete_imap(folder, oldimap);
+       }
+
        key.data = &folder;
        key.len = sizeof(folder);
        value.data = imap;
@@ -469,15 +565,16 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_ssl_run);
-       
-       if (result.error >= 0 && !etpan_skip_ssl_cert_check) {
-               cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
+
+       if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
+            result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
+               cert_len = (int)mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
                if (etpan_certificate_check(certificate, cert_len, &param) < 0)
                        return -1;
                if (certificate) 
                        free(certificate); 
        }
-       debug_print("connect %d\n", result.error);
+       debug_print("connect %d with imap %p\n", result.error, imap);
        
        return result.error;
 }
@@ -501,6 +598,8 @@ static void capability_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = mailimap_capability(param->imap, &caps);
        
        result->error = r;
@@ -546,6 +645,8 @@ static void disconnect_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = mailimap_logout(param->imap);
        
        result->error = r;
@@ -555,8 +656,6 @@ void imap_threaded_disconnect(Folder * folder)
 {
        struct connect_param param;
        struct connect_result result;
-       chashdatum key;
-       chashdatum value;
        mailimap * imap;
        
        imap = get_imap(folder);
@@ -569,17 +668,12 @@ void imap_threaded_disconnect(Folder * folder)
        
        threaded_run(folder, &param, &result, disconnect_run);
        
-       key.data = &folder;
-       key.len = sizeof(folder);
-       value.data = imap;
-       value.len = 0;
-       chash_delete(session_hash, &key, NULL);
-       
-       key.data = &imap;
-       key.len = sizeof(imap);
-       chash_delete(courier_workaround_hash, &key, NULL);
-       
-       mailimap_free(imap);
+       if (imap == get_imap(folder)) {
+               debug_print("deleting old imap %p\n", imap);
+               delete_imap(folder, imap);
+       } else {
+               debug_print("imap already deleted %p\n", imap);
+       }
        
        debug_print("disconnect ok\n");
 }
@@ -589,6 +683,7 @@ struct list_param {
        mailimap * imap;
        const char * base;
        const char * wildcard;
+       gboolean sub_only;
 };
 
 struct list_result {
@@ -604,11 +699,24 @@ static void list_run(struct etpan_thread_op * op)
        clist * list;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        list = NULL;
-       r = mailimap_list(param->imap, param->base,
-                         param->wildcard, &list);
        
-       result = op->result;
+       if (param->base == NULL || param->wildcard == NULL) {
+               result->list = list;
+               result->error = -1;
+               debug_print("no base or wildcard (%p %p)\n", param->base, param->wildcard);
+               return;
+       }
+       if (param->sub_only)
+               r = mailimap_lsub(param->imap, param->base,
+                         param->wildcard, &list);
+       else
+               r = mailimap_list(param->imap, param->base,
+                         param->wildcard, &list);
        result->error = r;
        result->list = list;
        debug_print("imap list run - end\n");
@@ -626,7 +734,8 @@ int imap_threaded_list(Folder * folder, const char * base,
        param.imap = get_imap(folder);
        param.base = base;
        param.wildcard = wildcard;
-       
+       param.sub_only = FALSE;
+
        threaded_run(folder, &param, &result, list_run);
        
        * p_result = result.list;
@@ -636,7 +745,79 @@ int imap_threaded_list(Folder * folder, const char * base,
        return result.error;
 }
 
+int imap_threaded_lsub(Folder * folder, const char * base,
+                      const char * wildcard,
+                      clist ** p_result)
+{
+       struct list_param param;
+       struct list_result result;
+       
+       debug_print("imap lsub - begin\n");
+       
+       param.imap = get_imap(folder);
+       param.base = base;
+       param.wildcard = wildcard;
+       param.sub_only = TRUE;
+       
+       threaded_run(folder, &param, &result, list_run);
+       
+       * p_result = result.list;
+       
+       debug_print("imap lsub - end %p\n", result.list);
+       
+       return result.error;
+}
 
+struct subscribe_param {
+       mailimap * imap;
+       const char * mb;
+       gboolean subscribe;
+};
+
+struct subscribe_result {
+       int error;
+};
+
+static void subscribe_run(struct etpan_thread_op * op)
+{
+       struct subscribe_param * param;
+       struct subscribe_result * result;
+       int r;
+       
+       param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
+       if (param->mb == NULL) {
+               result->error = -1;
+               debug_print("no mb\n");
+               return;
+       }
+       if (param->subscribe)
+               r = mailimap_subscribe(param->imap, param->mb);
+       else
+               r = mailimap_unsubscribe(param->imap, param->mb);
+       result->error = r;
+       debug_print("imap %ssubscribe run - end %d\n", param->subscribe?"":"un", r);
+}
+
+int imap_threaded_subscribe(Folder * folder, const char * mb,
+                      gboolean subscribe)
+{
+       struct subscribe_param param;
+       struct subscribe_result result;
+       
+       debug_print("imap list - begin\n");
+       
+       param.imap = get_imap(folder);
+       param.mb = mb;
+       param.subscribe = subscribe;
+
+       threaded_run(folder, &param, &result, subscribe_run);
+       
+       return result.error;
+}
 
 struct login_param {
        mailimap * imap;
@@ -660,7 +841,10 @@ static void login_run(struct etpan_thread_op * op)
 #endif
        
        param = op->param;
-       
+       result = op->result;
+
+       CHECK_IMAP();
+
 #ifdef DISABLE_LOG_DURING_LOGIN
        old_debug = mailstream_debug;
        mailstream_debug = 0;
@@ -682,7 +866,6 @@ static void login_run(struct etpan_thread_op * op)
        mailstream_debug = old_debug;
 #endif
        
-       result = op->result;
        result->error = r;
        debug_print("imap login run - end %i\n", r);
 }
@@ -733,6 +916,8 @@ static void status_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = mailimap_status(param->imap, param->mb,
                            param->status_att_list,
                            &result->data_status);
@@ -804,9 +989,12 @@ static void noop_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_noop(param->imap);
        
-       result = op->result;
        result->error = r;
        debug_print("imap noop run - end %i\n", r);
 }
@@ -821,10 +1009,10 @@ int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
        
        imap = get_imap(folder);
        param.imap = imap;
-       
+
        threaded_run(folder, &param, &result, noop_run);
        
-       if (imap->imap_selection_info != NULL) {
+       if (imap && imap->imap_selection_info != NULL) {
                * p_exists = imap->imap_selection_info->sel_exists;
        }
        else {
@@ -848,9 +1036,12 @@ static void starttls_run(struct etpan_thread_op * op)
        int r;
 
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_starttls(param->imap);
        
-       result = op->result;
        result->error = r;
        debug_print("imap starttls run - end %i\n", r);
        
@@ -895,9 +1086,9 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        threaded_run(folder, &param, &result, starttls_run);
        
        debug_print("imap starttls - end\n");
-       
-       if (result.error == 0 && !etpan_skip_ssl_cert_check) {
-               cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
+
+       if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
+               cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
                if (etpan_certificate_check(certificate, cert_len, &param) < 0)
                        result.error = MAILIMAP_ERROR_STREAM;
                if (certificate) 
@@ -924,9 +1115,12 @@ static void create_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_create(param->imap, param->mb);
        
-       result = op->result;
        result->error = r;
        debug_print("imap create run - end %i\n", r);
 }
@@ -968,9 +1162,12 @@ static void rename_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_rename(param->imap, param->mb, param->new_name);
        
-       result = op->result;
        result->error = r;
        debug_print("imap rename run - end %i\n", r);
 }
@@ -1013,9 +1210,12 @@ static void delete_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_delete(param->imap, param->mb);
        
-       result = op->result;
        result->error = r;
        debug_print("imap delete run - end %i\n", r);
 }
@@ -1055,9 +1255,12 @@ static void select_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_select(param->imap, param->mb);
        
-       result = op->result;
        result->error = r;
        debug_print("imap select run - end %i\n", r);
 }
@@ -1081,7 +1284,7 @@ int imap_threaded_select(Folder * folder, const char * mb,
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
-       if (imap->imap_selection_info == NULL)
+       if (!imap || imap->imap_selection_info == NULL)
                return MAILIMAP_ERROR_PARSE;
        
        * exists = imap->imap_selection_info->sel_exists;
@@ -1094,7 +1297,43 @@ int imap_threaded_select(Folder * folder, const char * mb,
        return result.error;
 }
 
+static void close_run(struct etpan_thread_op * op)
+{
+       struct select_param * param;
+       struct select_result * result;
+       int r;
+       
+       param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
 
+       r = mailimap_close(param->imap);
+       
+       result->error = r;
+       debug_print("imap close run - end %i\n", r);
+}
+
+int imap_threaded_close(Folder * folder)
+{
+       struct select_param param;
+       struct select_result result;
+       mailimap * imap;
+       
+       debug_print("imap close - begin\n");
+       
+       imap = get_imap(folder);
+       param.imap = imap;
+       
+       threaded_run(folder, &param, &result, close_run);
+       
+       if (result.error != MAILIMAP_NO_ERROR)
+               return result.error;
+       
+       debug_print("imap close - end\n");
+       
+       return result.error;
+}
 
 struct examine_param {
        mailimap * imap;
@@ -1112,9 +1351,12 @@ static void examine_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_examine(param->imap, param->mb);
        
-       result = op->result;
        result->error = r;
        debug_print("imap examine run - end %i\n", r);
 }
@@ -1138,7 +1380,7 @@ int imap_threaded_examine(Folder * folder, const char * mb,
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
-       if (imap->imap_selection_info == NULL)
+       if (!imap || imap->imap_selection_info == NULL)
                return MAILIMAP_ERROR_PARSE;
        
        * exists = imap->imap_selection_info->sel_exists;
@@ -1196,7 +1438,10 @@ static void search_run(struct etpan_thread_op * op)
        clist * search_result;
        
        param = op->param;
-       
+       result = op->result;
+
+       CHECK_IMAP();
+
        /* we copy the mailimap_set because freeing the key is recursive */
        if (param->set != NULL) {
                uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
@@ -1281,7 +1526,6 @@ static void search_run(struct etpan_thread_op * op)
                /* free the key (with the imapset) */
                mailimap_search_key_free(key);
 
-               result = op->result;
                result->error = r;
                result->search_result = search_result;
        }
@@ -1316,15 +1560,19 @@ int imap_threaded_search(Folder * folder, int search_type,
 
 
 
+static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
+                                uint32_t * puid,
+                                char ** pheaders,
+                                size_t * pref_size,
+                                struct mailimap_msg_att_dynamic ** patt_dyn);
 
 static int
-uid_list_to_env_list(clist * fetch_result, carray ** result)
+result_to_uid_list(clist * fetch_result, carray ** result)
 {
        clistiter * cur;
        int r;
        int res;
        carray * tab;
-       unsigned int i;
        
        tab = carray_new(128);
        if (tab == NULL) {
@@ -1335,31 +1583,13 @@ uid_list_to_env_list(clist * fetch_result, carray ** result)
        for(cur = clist_begin(fetch_result) ; cur != NULL ;
            cur = clist_next(cur)) {
                struct mailimap_msg_att * msg_att;
-               clistiter * item_cur;
                uint32_t uid;
-               size_t size;
                uint32_t * puid;
                
                msg_att = clist_content(cur);
                
                uid = 0;
-               size = 0;
-               for(item_cur = clist_begin(msg_att->att_list) ;
-                   item_cur != NULL ;
-                   item_cur = clist_next(item_cur)) {
-                       struct mailimap_msg_att_item * item;
-                       
-                       item = clist_content(item_cur);
-                       
-                       switch (item->att_type) {
-                       case MAILIMAP_MSG_ATT_ITEM_STATIC:
-                               switch (item->att_data.att_static->att_type) {
-                               case MAILIMAP_MSG_ATT_UID:
-                                       uid = item->att_data.att_static->att_data.att_uid;
-                                       break;
-                               }
-                       }
-               }
+               imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
                
                puid = malloc(sizeof(* puid));
                if (puid == NULL) {
@@ -1381,8 +1611,7 @@ uid_list_to_env_list(clist * fetch_result, carray ** result)
        return MAILIMAP_NO_ERROR;
   
  free_list:
-       for(i = 0 ; i < carray_count(tab) ; i++)
-               mailmessage_free(carray_get(tab, i));
+       imap_fetch_uid_list_free(tab);
  err:
        return res;
 }
@@ -1436,7 +1665,7 @@ static int imap_get_messages_list(mailimap * imap,
        }
 
        env_list = NULL;
-       r = uid_list_to_env_list(fetch_result, &env_list);
+       r = result_to_uid_list(fetch_result, &env_list);
        mailimap_fetch_list_free(fetch_result);
        
        * result = env_list;
@@ -1472,12 +1701,19 @@ static void fetch_uid_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
-       
+       result = op->result;
+
+       CHECK_IMAP();
+
        fetch_result = NULL;
+       mailstream_logger = NULL;
+       log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
+
        r = imap_get_messages_list(param->imap, param->first_index,
                                   &fetch_result);
        
-       result = op->result;
+       mailstream_logger = imap_logger_cmd;
+
        result->error = r;
        result->fetch_result = fetch_result;
        debug_print("imap fetch_uid run - end %i\n", r);
@@ -1524,6 +1760,228 @@ void imap_fetch_uid_list_free(carray * uid_list)
 
 
 
+
+static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn);
+
+static int
+result_to_uid_flags_list(clist * fetch_result, carray ** result)
+{
+       clistiter * cur;
+       int r;
+       int res;
+       carray * tab;
+       
+       tab = carray_new(128);
+       if (tab == NULL) {
+               res = MAILIMAP_ERROR_MEMORY;
+               goto err;
+       }
+       
+       for(cur = clist_begin(fetch_result) ; cur != NULL ;
+           cur = clist_next(cur)) {
+               struct mailimap_msg_att * msg_att;
+               uint32_t uid;
+               uint32_t * puid;
+               struct mailimap_msg_att_dynamic * att_dyn;
+               int flags;
+               int * pflags;
+               
+               msg_att = clist_content(cur);
+               
+               uid = 0;
+               att_dyn = NULL;
+               imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
+               if (uid == 0)
+                       continue;
+               if (att_dyn == NULL)
+                       continue;
+               
+               flags = imap_flags_to_flags(att_dyn);
+               
+               puid = malloc(sizeof(* puid));
+               if (puid == NULL) {
+                       res = MAILIMAP_ERROR_MEMORY;
+                       goto free_list;
+               }
+               * puid = uid;
+               
+               r = carray_add(tab, puid, NULL);
+               if (r < 0) {
+                       free(puid);
+                       res = MAILIMAP_ERROR_MEMORY;
+                       goto free_list;
+               }
+               pflags = malloc(sizeof(* pflags));
+               if (pflags == NULL) {
+                       res = MAILIMAP_ERROR_MEMORY;
+                       goto free_list;
+               }
+               * pflags = flags;
+               r = carray_add(tab, pflags, NULL);
+               if (r < 0) {
+                       free(pflags);
+                       res = MAILIMAP_ERROR_MEMORY;
+                       goto free_list;
+               }
+       }
+               
+       * result = tab;
+
+       return MAILIMAP_NO_ERROR;
+  
+ free_list:
+       imap_fetch_uid_flags_list_free(tab);
+ err:
+       return res;
+}
+
+static int imap_get_messages_flags_list(mailimap * imap,
+                                       uint32_t first_index,
+                                       carray ** result)
+{
+       carray * env_list;
+       int r;
+       struct mailimap_fetch_att * fetch_att;
+       struct mailimap_fetch_type * fetch_type;
+       struct mailimap_set * set;
+       clist * fetch_result;
+       int res;
+       
+       set = mailimap_set_new_interval(first_index, 0);
+       if (set == NULL) {
+               res = MAILIMAP_ERROR_MEMORY;
+               goto err;
+       }
+
+       fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
+       if (fetch_type == NULL) {
+               res = MAILIMAP_ERROR_MEMORY;
+               goto free_set;
+       }
+
+       fetch_att = mailimap_fetch_att_new_flags();
+       if (fetch_att == NULL) {
+               res = MAILIMAP_ERROR_MEMORY;
+               goto free_fetch_type;
+       }
+       
+       r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
+       if (r != MAILIMAP_NO_ERROR) {
+               mailimap_fetch_att_free(fetch_att);
+               res = MAILIMAP_ERROR_MEMORY;
+               goto free_fetch_type;
+       }
+       
+       fetch_att = mailimap_fetch_att_new_uid();
+       if (fetch_att == NULL) {
+               res = MAILIMAP_ERROR_MEMORY;
+               goto free_fetch_type;
+       }
+
+       r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
+       if (r != MAILIMAP_NO_ERROR) {
+               mailimap_fetch_att_free(fetch_att);
+               res = MAILIMAP_ERROR_MEMORY;
+               goto free_fetch_type;
+       }
+
+       r = mailimap_uid_fetch(imap, set,
+                              fetch_type, &fetch_result);
+
+       mailimap_fetch_type_free(fetch_type);
+       mailimap_set_free(set);
+
+       if (r != MAILIMAP_NO_ERROR) {
+               res = r;
+               goto err;
+       }
+
+       env_list = NULL;
+       r = result_to_uid_flags_list(fetch_result, &env_list);
+       mailimap_fetch_list_free(fetch_result);
+       
+       * result = env_list;
+
+       return MAILIMAP_NO_ERROR;
+
+ free_fetch_type:
+       mailimap_fetch_type_free(fetch_type);
+ free_set:
+       mailimap_set_free(set);
+ err:
+       return res;
+}
+
+
+
+static void fetch_uid_flags_run(struct etpan_thread_op * op)
+{
+       struct fetch_uid_param * param;
+       struct fetch_uid_result * result;
+       carray * fetch_result;
+       int r;
+       
+       param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
+       fetch_result = NULL;
+       r = imap_get_messages_flags_list(param->imap, param->first_index,
+                                        &fetch_result);
+       
+       result->error = r;
+       result->fetch_result = fetch_result;
+       debug_print("imap fetch_uid run - end %i\n", r);
+}
+
+int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
+                                 carray ** fetch_result)
+{
+       struct fetch_uid_param param;
+       struct fetch_uid_result result;
+       mailimap * imap;
+       
+       debug_print("imap fetch_uid - begin\n");
+       
+       imap = get_imap(folder);
+       param.imap = imap;
+       param.first_index = first_index;
+       
+       mailstream_logger = NULL;
+       log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
+
+       threaded_run(folder, &param, &result, fetch_uid_flags_run);
+
+       mailstream_logger = imap_logger_cmd;
+
+       
+       if (result.error != MAILIMAP_NO_ERROR)
+               return result.error;
+       
+       debug_print("imap fetch_uid - end\n");
+       
+       * fetch_result = result.fetch_result;
+       
+       return result.error;
+}
+
+
+void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
+{
+       unsigned int i;
+       
+       for(i = 0 ; i < carray_count(uid_flags_list) ; i ++) {
+               void * data;
+               
+               data = carray_get(uid_flags_list, i);
+               free(data);
+       }
+       carray_free(uid_flags_list);
+}
+
+
+
 static int imap_fetch(mailimap * imap,
                      uint32_t msg_index,
                      char ** result,
@@ -1750,7 +2208,10 @@ static void fetch_content_run(struct etpan_thread_op * op)
        FILE * f;
        
        param = op->param;
-       
+       result = op->result;
+
+       CHECK_IMAP();
+
        content = NULL;
        content_size = 0;
        if (param->with_body)
@@ -1760,7 +2221,6 @@ static void fetch_content_run(struct etpan_thread_op * op)
                r = imap_fetch_header(param->imap, param->msg_index,
                                      &content, &content_size);
        
-       result = op->result;
        result->error = r;
        
        if (r == MAILIMAP_NO_ERROR) {
@@ -1986,7 +2446,7 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
        return MAIL_NO_ERROR;
 }
 
-int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
+static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
 {
        struct mailimap_fetch_att * fetch_att;
        int r;
@@ -2022,7 +2482,7 @@ int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
        return MAIL_NO_ERROR;
 }
 
-int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
+static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
 {
        struct mailimap_fetch_att * fetch_att;
        struct mailimap_section * section;
@@ -2125,12 +2585,14 @@ static void fetch_env_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
-       
+       result = op->result;
+
+       CHECK_IMAP();
+
        env_list = NULL;
        r = imap_get_envelopes_list(param->imap, param->set,
                                    &env_list);
        
-       result = op->result;
        result->error = r;
        result->fetch_env_result = env_list;
        
@@ -2223,6 +2685,8 @@ static void append_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = stat(param->filename, &stat_buf);
        if (r < 0) {
                result->error = MAILIMAP_ERROR_APPEND;
@@ -2306,9 +2770,12 @@ static void expunge_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_expunge(param->imap);
        
-       result = op->result;
        result->error = r;
        debug_print("imap expunge run - end %i\n", r);
 }
@@ -2348,13 +2815,16 @@ static void copy_run(struct etpan_thread_op * op)
        struct copy_result * result;
        int r;
        guint32 val;
-       param = op->param;
        struct mailimap_set *source = NULL, *dest = NULL;
-       
+
+       param = op->param;
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
                &val, &source, &dest);
        
-       result = op->result;
        result->error = r;
        if (r == 0) {
                result->source = source;
@@ -2415,11 +2885,13 @@ static void store_run(struct etpan_thread_op * op)
        int r;
        
        param = op->param;
-       
+       result = op->result;
+
+       CHECK_IMAP();
+
        r = mailimap_uid_store(param->imap, param->set,
                               param->store_att_flags);
        
-       result = op->result;
        result->error = r;
        
        debug_print("imap store run - end %i\n", r);
@@ -2555,7 +3027,7 @@ static int subcommand_connect(const char *command,
        return sockfds[0];
 }
 
-int socket_connect_cmd(mailimap * imap, const char * command,
+static int socket_connect_cmd(mailimap * imap, const char * command,
                       const char * server, int port)
 {
        int fd;
@@ -2576,6 +3048,8 @@ int socket_connect_cmd(mailimap * imap, const char * command,
        if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
        &&  r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
                mailstream_close(s);
+               if (imap)
+                       imap->imap_stream = NULL;
                return r;
        }
        
@@ -2604,6 +3078,8 @@ static void connect_cmd_run(struct etpan_thread_op * op)
        param = op->param;
        result = op->result;
        
+       CHECK_IMAP();
+
        r = socket_connect_cmd(param->imap, param->command,
                               param->server, param->port);
        
@@ -2618,10 +3094,17 @@ int imap_threaded_connect_cmd(Folder * folder, const char * command,
        struct connect_cmd_result result;
        chashdatum key;
        chashdatum value;
-       mailimap * imap;
+       mailimap * imap, * oldimap;
        
+       oldimap = get_imap(folder);
+
        imap = mailimap_new(0, NULL);
        
+       if (oldimap) {
+               debug_print("deleting old imap %p\n", oldimap);
+               delete_imap(folder, oldimap);
+       }
+
        key.data = &folder;
        key.len = sizeof(folder);
        value.data = imap;
@@ -2635,10 +3118,20 @@ int imap_threaded_connect_cmd(Folder * folder, const char * command,
        
        threaded_run(folder, &param, &result, connect_cmd_run);
        
-       debug_print("connect_cmd ok %i\n", result.error);
+       debug_print("connect_cmd ok %i with imap %p\n", result.error, imap);
        
        return result.error;
 }
+
+void imap_threaded_cancel(Folder * folder)
+{
+       mailimap * imap;
+       
+       imap = get_imap(folder);
+       if (imap->imap_stream != NULL)
+               mailstream_cancel(imap->imap_stream);
+}
+
 #else
 
 void imap_main_init(void)
@@ -2651,4 +3144,8 @@ void imap_main_set_timeout(int sec)
 {
 }
 
+void imap_threaded_cancel(Folder * folder);
+{
+}
+
 #endif