2006-08-10 [colin] 2.4.0cvs39
[claws.git] / src / etpan / imap-thread.c
index b7d00265bef84f14c5ecd2610f58a05ff226f2fb..263e0dcbee70be25ead47a26aecc8c22bd63c6f9 100644 (file)
@@ -5,17 +5,27 @@
 #ifdef HAVE_LIBETPAN
 
 #include "imap-thread.h"
-
+#include <imap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__CYGWIN__))
+#include <sys/socket.h>
+#endif
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/wait.h>
 
 #include <gtk/gtk.h>
+#include <log.h>
 #include "etpan-thread-manager.h"
+#include "utils.h"
+#include "ssl_certificate.h"
+#include "socket.h"
+
+#define DISABLE_LOG_DURING_LOGIN
 
 static struct etpan_thread_manager * thread_manager = NULL;
+static chash * courier_workaround_hash = NULL;
 static chash * imap_hash = NULL;
 static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
@@ -31,21 +41,122 @@ static gboolean thread_manager_event(GIOChannel * source,
        return TRUE;
 }
 
+void imap_logger_cmd(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
+
+       while (lines[i] && *lines[i]) {
+               log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               i++;
+       }
+       g_strfreev(lines);
+       free(buf);
+}
+
+void imap_logger_fetch(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
+
+       if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
+               while (lines[i] && *lines[i]) {
+                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       i++;
+               }
+       } else {
+               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+       }
+       g_strfreev(lines);
+       free(buf);
+}
+
+void imap_logger_append(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
+
+       if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
+               while (lines[i] && *lines[i]) {
+                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       i++;
+               }
+       } else {
+               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+       }
+       g_strfreev(lines);
+       free(buf);
+}
 
 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
+static gboolean etpan_skip_ssl_cert_check = FALSE;
 
-void imap_main_init(void)
+void imap_main_init(gboolean skip_ssl_cert_check)
 {
        int fd_thread_manager;
        
+       etpan_skip_ssl_cert_check = skip_ssl_cert_check;
        mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
        mailstream_network_delay.tv_usec = 0;
-
-#if 0
+       
        mailstream_debug = 1;
-#endif
+       mailstream_logger = imap_logger_cmd;
+
        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);
        
        thread_manager = etpan_thread_manager_new();
        
@@ -67,13 +178,15 @@ void imap_main_set_timeout(int sec)
 
 void imap_main_done(void)
 {
+       etpan_thread_manager_stop(thread_manager);
+       etpan_thread_manager_join(thread_manager);
+       
        g_source_remove(thread_manager_signal);
        g_io_channel_unref(io_channel);
        
-       etpan_thread_manager_stop(thread_manager);
-       etpan_thread_manager_join(thread_manager);
        etpan_thread_manager_free(thread_manager);
        
+       chash_free(courier_workaround_hash);
        chash_free(session_hash);
        chash_free(imap_hash);
 }
@@ -110,9 +223,11 @@ void imap_done(Folder * folder)
        
        thread = value.data;
        
-       etpan_thread_stop(thread);
+       etpan_thread_unbind(thread);
        
        chash_delete(imap_hash, &key, NULL);
+       
+       debug_print("remove thread");
 }
 
 static struct etpan_thread * get_thread(Folder * folder)
@@ -155,6 +270,8 @@ static void generic_cb(int cancelled, void * result, void * callback_data)
        int * p_finished;
        
        p_finished = callback_data;
+
+       debug_print("generic_cb\n");
        
        * p_finished = 1;
 }
@@ -166,6 +283,8 @@ static void threaded_run(Folder * folder, void * param, void * result,
        struct etpan_thread * thread;
        int finished;
        
+       imap_folder_ref(folder);
+
        op = etpan_thread_op_new();
        op->param = param;
        op->result = result;
@@ -186,6 +305,8 @@ static void threaded_run(Folder * folder, void * param, void * result,
        }
        
        etpan_thread_op_free(op);
+
+       imap_folder_unref(folder);
 }
 
 
@@ -237,6 +358,7 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        param.server = server;
        param.port = port;
        
+       refresh_resolvers();
        threaded_run(folder, &param, &result, connect_run);
        
        debug_print("connect ok %i\n", result.error);
@@ -244,6 +366,32 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        return result.error;
 }
 
+static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
+{
+#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);
+       if (cert == NULL) {
+               g_warning("can't get cert\n");
+               return 0;
+       } else if (ssl_certificate_check(cert, 
+               (gchar *)param->server, (gushort)param->port) == TRUE) {
+               X509_free(cert);
+               return 0;
+       } else {
+               X509_free(cert);
+               return -1;
+       }
+#else
+       return 0;
+#endif
+}
 
 static void connect_ssl_run(struct etpan_thread_op * op)
 {
@@ -256,7 +404,6 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        r = mailimap_ssl_connect(param->imap,
                                 param->server, param->port);
-       
        result->error = r;
 }
 
@@ -267,7 +414,9 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        chashdatum key;
        chashdatum value;
        mailimap * imap;
-       
+       unsigned char *certificate = NULL;
+       int cert_len;
+
        imap = mailimap_new(0, NULL);
        
        key.data = &folder;
@@ -280,12 +429,67 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        param.server = server;
        param.port = port;
        
+       refresh_resolvers();
        threaded_run(folder, &param, &result, connect_ssl_run);
        
-       debug_print("connect ok\n");
+       if (result.error >= 0 && !etpan_skip_ssl_cert_check) {
+               cert_len = 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);
        
        return result.error;
 }
+
+struct capa_param {
+       mailimap * imap;
+};
+
+struct capa_result {
+       int error;
+       struct mailimap_capability_data *caps;
+};
+
+static void capability_run(struct etpan_thread_op * op)
+{
+       int r;
+       struct capa_param * param;
+       struct capa_result * result;
+       struct mailimap_capability_data *caps;
+
+       param = op->param;
+       result = op->result;
+       
+       r = mailimap_capability(param->imap, &caps);
+       
+       result->error = r;
+       result->caps = (r == 0 ? caps : NULL);
+}
+
+
+struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
+{
+       struct capa_param param;
+       struct capa_result result;
+       mailimap *imap;
+       
+       imap = get_imap(folder);
+       
+       param.imap = imap;
+       
+       threaded_run(folder, &param, &result, capability_run);
+       
+       debug_print("capa %d\n", result.error);
+       
+       if (ok)
+               *ok = result.error;
+
+       return result.caps;
+       
+}
        
 struct disconnect_param {
        mailimap * imap;
@@ -333,6 +537,10 @@ void imap_threaded_disconnect(Folder * folder)
        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);
        
        debug_print("disconnect ok\n");
@@ -396,6 +604,7 @@ struct login_param {
        mailimap * imap;
        const char * login;
        const char * password;
+       const char * type;
 };
 
 struct login_result {
@@ -407,10 +616,27 @@ static void login_run(struct etpan_thread_op * op)
        struct login_param * param;
        struct login_result * result;
        int r;
+#ifdef DISABLE_LOG_DURING_LOGIN
+       int old_debug;
+#endif
        
        param = op->param;
-       r = mailimap_login(param->imap,
+       
+#ifdef DISABLE_LOG_DURING_LOGIN
+       old_debug = mailstream_debug;
+       mailstream_debug = 0;
+#endif
+       if (!strcmp(param->type, "LOGIN"))
+               r = mailimap_login(param->imap,
                           param->login, param->password);
+       else 
+               r = mailimap_authenticate(param->imap,
+                       param->type, NULL, NULL, NULL,
+                       param->login, param->login,
+                       param->password, NULL);
+#ifdef DISABLE_LOG_DURING_LOGIN
+       mailstream_debug = old_debug;
+#endif
        
        result = op->result;
        result->error = r;
@@ -418,7 +644,8 @@ static void login_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_login(Folder * folder,
-                       const char * login, const char * password)
+                       const char * login, const char * password,
+                       const char * type)
 {
        struct login_param param;
        struct login_result result;
@@ -428,7 +655,8 @@ int imap_threaded_login(Folder * folder,
        param.imap = get_imap(folder);
        param.login = login;
        param.password = password;
-       
+       param.type = type;
+
        threaded_run(folder, &param, &result, login_run);
        
        debug_print("imap login - end\n");
@@ -466,7 +694,8 @@ static void status_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_status(Folder * folder, const char * mb,
-                        struct mailimap_mailbox_data_status ** data_status)
+                        struct mailimap_mailbox_data_status ** data_status,
+                        guint mask)
 {
        struct status_param param;
        struct status_result result;
@@ -475,17 +704,26 @@ int imap_threaded_status(Folder * folder, const char * mb,
        debug_print("imap status - begin\n");
        
        status_att_list = mailimap_status_att_list_new_empty();
-       mailimap_status_att_list_add(status_att_list,
+       if (mask & 1 << 0) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_MESSAGES);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 1) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_RECENT);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 2) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UIDNEXT);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 3) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UIDVALIDITY);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 4) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UNSEEN);
-       
+       }
        param.imap = get_imap(folder);
        param.mb = mb;
        param.status_att_list = status_att_list;
@@ -496,6 +734,8 @@ int imap_threaded_status(Folder * folder, const char * mb,
        
        * data_status = result.data_status;
        
+       mailimap_status_att_list_free(status_att_list);
+       
        return result.error;
 }
 
@@ -549,41 +789,72 @@ int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
 }
 
 
-struct starttls_param {
-       mailimap * imap;
-};
-
 struct starttls_result {
        int error;
 };
 
 static void starttls_run(struct etpan_thread_op * op)
 {
-       struct starttls_param * param;
+       struct connect_param * param;
        struct starttls_result * result;
        int r;
-       
+
        param = op->param;
        r = mailimap_starttls(param->imap);
        
        result = op->result;
        result->error = r;
        debug_print("imap starttls run - end %i\n", r);
+       
+       if (r == 0) {
+               mailimap *imap = param->imap;
+               mailstream_low *plain_low = NULL;
+               mailstream_low *tls_low = NULL;
+               int fd = -1;
+               
+               plain_low = mailstream_get_low(imap->imap_stream);
+               fd = mailstream_low_get_fd(plain_low);
+               if (fd == -1) {
+                       debug_print("imap starttls run - can't get fd\n");
+                       result->error = MAILIMAP_ERROR_STREAM;
+                       return;
+               }
+
+               tls_low = mailstream_low_tls_open(fd);
+               if (tls_low == NULL) {
+                       debug_print("imap starttls run - can't tls_open\n");
+                       result->error = MAILIMAP_ERROR_STREAM;
+                       return;
+               }
+               mailstream_low_free(plain_low);
+               mailstream_set_low(imap->imap_stream, tls_low);
+       }
 }
 
-int imap_threaded_starttls(Folder * folder)
+int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
 {
-       struct starttls_param param;
+       struct connect_param param;
        struct starttls_result result;
+       int cert_len;
+       unsigned char *certificate;
        
        debug_print("imap starttls - begin\n");
        
        param.imap = get_imap(folder);
+       param.server = host;
+       param.port = 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 (etpan_certificate_check(certificate, cert_len, &param) < 0)
+                       result.error = MAILIMAP_ERROR_STREAM;
+               if (certificate) 
+                       free(certificate); 
+       }       
        return result.error;
 }
 
@@ -846,19 +1117,45 @@ struct search_result {
        clist * search_result;
 };
 
+static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
+{
+       return mailimap_set_item_new(orig->set_first, orig->set_last);
+}
+
+static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
+{
+       clist *list = orig ? orig->set_list : NULL;
+       clist *newlist = clist_new();
+       clistiter *cur;
+       
+       if (!orig)
+               return NULL;
+       for (cur = clist_begin(list); cur; cur = clist_next(cur))
+               clist_append(newlist, 
+                       sc_mailimap_set_item_copy(
+                       (struct mailimap_set_item *)clist_content(cur)));
+       return mailimap_set_new(newlist);
+}
+
 static void search_run(struct etpan_thread_op * op)
 {
        struct search_param * param;
        struct search_result * result;
        int r;
-       struct mailimap_search_key * key;
-       struct mailimap_search_key * uid_key;
+       struct mailimap_search_key * key = NULL;
+       struct mailimap_search_key * uid_key = NULL;
        struct mailimap_search_key * search_type_key;
        clist * search_result;
        
        param = op->param;
        
-       uid_key = mailimap_search_key_new_uid(param->set);
+       if (param->set == NULL && param->type == IMAP_SEARCH_TYPE_SIMPLE) {
+               g_warning("broken search");
+       }
+       
+       /* 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));
        
        search_type_key = NULL;
        switch (param->type) {
@@ -901,23 +1198,44 @@ static void search_run(struct etpan_thread_op * op)
                                                          NULL, NULL, NULL, NULL, NULL,
                                                          NULL, 0, NULL, NULL, NULL);
                break;
+       case IMAP_SEARCH_TYPE_DELETED:
+               search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, NULL, NULL, NULL, 0,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, 0, NULL, NULL, NULL);
+               break;
        }
        
        if (search_type_key != NULL) {
-               key = mailimap_search_key_new_multiple_empty();
-               mailimap_search_key_multiple_add(key, search_type_key);
-               mailimap_search_key_multiple_add(key, uid_key);
-       }
-       else {
+               if (param->set != NULL) {
+                       key = mailimap_search_key_new_multiple_empty();
+                       mailimap_search_key_multiple_add(key, search_type_key);
+                       mailimap_search_key_multiple_add(key, uid_key);
+               } else {
+                       key = search_type_key;
+               }
+       } else if (uid_key != NULL) {
                key = uid_key;
        }
        
-       r = mailimap_uid_search(param->imap, NULL, key, &search_result);
-       
-       result = op->result;
-       result->error = r;
-       result->search_result = search_result;
-       debug_print("imap search run - end %i\n", r);
+       if (key == NULL) {
+               g_warning("no key!");
+               result = op->result;
+               result->error = -1;
+               result->search_result = NULL;
+       } else {
+               r = mailimap_uid_search(param->imap, NULL, key, &search_result);
+
+               /* free the key (with the imapset) */
+               mailimap_search_key_free(key);
+
+               result = op->result;
+               result->error = r;
+               result->search_result = search_result;
+       }
+       debug_print("imap search run - end %i\n", result->error);
 }
 
 int imap_threaded_search(Folder * folder, int search_type,
@@ -928,7 +1246,7 @@ int imap_threaded_search(Folder * folder, int search_type,
        mailimap * imap;
        
        debug_print("imap search - begin\n");
-       
+
        imap = get_imap(folder);
        param.imap = imap;
        param.set = set;
@@ -1105,6 +1423,7 @@ static void fetch_uid_run(struct etpan_thread_op * op)
        
        param = op->param;
        
+       fetch_result = NULL;
        r = imap_get_messages_list(param->imap, param->first_index,
                                   &fetch_result);
        
@@ -1198,9 +1517,13 @@ static int imap_fetch(mailimap * imap,
                goto free_fetch_att;
        }
 
+       mailstream_logger = imap_logger_fetch;
+       
        r = mailimap_uid_fetch(imap, set,
                               fetch_type, &fetch_result);
   
+       mailstream_logger = imap_logger_cmd;
+       
        mailimap_fetch_type_free(fetch_type);
        mailimap_set_free(set);
   
@@ -1378,6 +1701,8 @@ static void fetch_content_run(struct etpan_thread_op * op)
        
        param = op->param;
        
+       content = NULL;
+       content_size = 0;
        if (param->with_body)
                r = imap_fetch(param->imap, param->msg_index,
                               &content, &content_size);
@@ -1408,7 +1733,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
                
                r = fclose(f);
                if (r == EOF) {
-                       unlink(param->filename);
+                       g_unlink(param->filename);
                        goto close;
                }
                goto free;
@@ -1419,7 +1744,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
        close:
                close(fd);
        unlink:
-               unlink(param->filename);
+               g_unlink(param->filename);
        
        free:
                if (mmap_string_unref(content) != 0)
@@ -1571,6 +1896,8 @@ fetch_to_env_info(struct mailimap_msg_att * msg_att)
        imap_get_msg_att_info(msg_att, &uid, &headers, &size,
                              &att_dyn);
        
+       if (!headers)
+               return NULL;
        info = malloc(sizeof(* info));
        info->uid = uid;
        info->headers = strdup(headers);
@@ -1587,7 +1914,6 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
        clistiter * cur;
        unsigned int i;
        carray * env_list;
-  
        i = 0;
   
        env_list = carray_new(16);
@@ -1600,6 +1926,8 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
                msg_att = clist_content(cur);
 
                env_info = fetch_to_env_info(msg_att);
+               if (!env_info)
+                       return MAILIMAP_ERROR_MEMORY;
                carray_add(env_list, env_info, NULL);
        }
   
@@ -1644,6 +1972,18 @@ 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)
+{
+       struct mailimap_fetch_att * fetch_att;
+       struct mailimap_section * section;
+       
+       section = mailimap_section_new_header();
+       fetch_att = mailimap_fetch_att_new_body_peek_section(section);
+       mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
+       
+       return MAIL_NO_ERROR;
+}
+
 static int
 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                        carray ** p_env_list)
@@ -1653,7 +1993,9 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        int res;
        clist * fetch_result;
        int r;
-       carray * env_list;
+       carray * env_list = NULL;
+       chashdatum key;
+       chashdatum value;
        
        fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
   
@@ -1670,7 +2012,13 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
   
        /* headers */
-       r = imap_add_envelope_fetch_att(fetch_type);
+       key.data = &imap;
+       key.len = sizeof(imap);
+       r = chash_get(courier_workaround_hash, &key, &value);
+       if (r < 0)
+               r = imap_add_envelope_fetch_att(fetch_type);
+       else
+               r = imap_add_header_fetch_att(fetch_type);
        
        r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
        
@@ -1679,11 +2027,13 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                break;
        default:
                mailimap_fetch_type_free(fetch_type);
+               debug_print("uid_fetch: %d\n", r);
                return r;
        }
        
        if (clist_begin(fetch_result) == NULL) {
                res = MAILIMAP_ERROR_FETCH;
+               debug_print("clist_begin = NULL\n");
                goto err;
        }
        
@@ -1693,6 +2043,7 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        if (r != MAILIMAP_NO_ERROR) {
                mailimap_fetch_type_free(fetch_type);
                res = MAILIMAP_ERROR_MEMORY;
+               debug_print("fetch_result_to_envelop_list: %d\n", res);
                goto err;
        }
        
@@ -1725,6 +2076,7 @@ static void fetch_env_run(struct etpan_thread_op * op)
        
        param = op->param;
        
+       env_list = NULL;
        r = imap_get_envelopes_list(param->imap, param->set,
                                    &env_list);
        
@@ -1750,6 +2102,23 @@ int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
        
        threaded_run(folder, &param, &result, fetch_env_run);
        
+       if (result.error != MAILIMAP_NO_ERROR) {
+               chashdatum key;
+               chashdatum value;
+               int r;
+               
+               key.data = &imap;
+               key.len = sizeof(imap);
+               r = chash_get(courier_workaround_hash, &key, &value);
+               if (r < 0) {
+                       value.data = NULL;
+                       value.len = 0;
+                       chash_set(courier_workaround_hash, &key, &value, NULL);
+                       
+                       threaded_run(folder, &param, &result, fetch_env_run);
+               }
+       }
+       
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
@@ -1787,6 +2156,7 @@ struct append_param {
 
 struct append_result {
        int error;
+       int uid;
 };
 
 static void append_run(struct etpan_thread_op * op)
@@ -1797,7 +2167,7 @@ static void append_run(struct etpan_thread_op * op)
        char * data;
        size_t size;
        struct stat stat_buf;
-       int fd;
+       int fd, uid = 0;
        
        param = op->param;
        result = op->result;
@@ -1822,21 +2192,26 @@ static void append_run(struct etpan_thread_op * op)
                return;
        }
        
+       mailstream_logger = imap_logger_append;
+       
        r = mailimap_append(param->imap, param->mailbox,
                            param->flag_list, NULL,
-                           data, size);
+                           data, size/*, &uid */);
+
+       mailstream_logger = imap_logger_cmd;
        
        munmap(data, size);
        close(fd);
        
        result->error = r;
-       
-       debug_print("imap append run - end %i\n", r);
+       result->uid = uid;
+       debug_print("imap append run - end %i uid %d\n", r, uid);
 }
 
 int imap_threaded_append(Folder * folder, const char * mailbox,
                         const char * filename,
-                        struct mailimap_flag_list * flag_list)
+                        struct mailimap_flag_list * flag_list,
+                        int *uid)
 {
        struct append_param param;
        struct append_result result;
@@ -1856,7 +2231,9 @@ int imap_threaded_append(Folder * folder, const char * mailbox,
                return result.error;
        
        debug_print("imap append - end\n");
-       
+       if (uid != NULL)
+               *uid = result.uid;
+
        return result.error;
 }
 
@@ -2006,33 +2383,54 @@ int imap_threaded_store(Folder * folder, struct mailimap_set * set,
 }
 
 
+#define ENV_BUFFER_SIZE 512
 
 static void do_exec_command(int fd, const char * command,
                            const char * servername, uint16_t port)
 {
        int i, maxopen;
-  
+#ifdef SOLARIS
+       char env_buffer[ENV_BUFFER_SIZE];
+#endif
+       
        if (fork() > 0) {
                /* Fork again to become a child of init rather than
                   the etpan client. */
                exit(0);
        }
   
+#ifdef SOLARIS
+       if (servername)
+               snprintf(env_buffer, ENV_BUFFER_SIZE,
+                        "ETPANSERVER=%s", servername);
+       else
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
+       putenv(env_buffer);
+#else
        if (servername)
                setenv("ETPANSERVER", servername, 1);
        else
                unsetenv("ETPANSERVER");
+#endif
   
+#ifdef SOLARIS
+       if (port)
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
+       else
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
+       putenv(env_buffer);
+#else
        if (port) {
                char porttext[20];
-    
+               
                snprintf(porttext, sizeof(porttext), "%d", port);
                setenv("ETPANPORT", porttext, 1);
        }
        else {
                unsetenv("ETPANPORT");
        }
-  
+#endif
+               
        /* Not a lot we can do if there's an error other than bail. */
        if (dup2(fd, 0) == -1)
                exit(1);
@@ -2108,12 +2506,13 @@ int socket_connect_cmd(mailimap * imap, const char * command,
        }
        
        r = mailimap_connect(imap, s);
-       if (r != MAILIMAP_NO_ERROR) {
+       if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
+       &&  r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
                mailstream_close(s);
                return r;
        }
        
-       return MAILIMAP_NO_ERROR;
+       return r;
 }
 
 /* connect cmd */