2009-01-17 [colin] 3.7.0cvs37
[claws.git] / src / etpan / imap-thread.c
index ca3512d71da5d3e4ef711fbe4f1f118dcc195c87..4521e00184fd1f47d5e3a198599e4da9608b372a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
+ * Copyright (C) 2005-2009 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
 #include <sys/socket.h>
 #endif
 #include <fcntl.h>
+#ifndef G_OS_WIN32
 #include <sys/mman.h>
 #include <sys/wait.h>
-
+#endif
 #include <gtk/gtk.h>
 #include <log.h>
 #include "etpan-thread-manager.h"
 #include "utils.h"
 #include "mainwindow.h"
+#include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
 #include "remotefolder.h"
@@ -79,11 +81,23 @@ static gboolean thread_manager_event(GIOChannel * source,
     GIOCondition condition,
     gpointer data)
 {
+#ifdef G_OS_WIN32
+       gsize bytes_read;
+       gchar ch;
+       
+       if (condition & G_IO_IN)
+               g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
+#endif
        etpan_thread_manager_loop(thread_manager);
        
        return TRUE;
 }
 
+static void imap_logger_noop(int direction, const char * str, size_t size) 
+{
+       /* inhibit logging */
+}
+
 static void imap_logger_cmd(int direction, const char * str, size_t size) 
 {
        gchar *buf;
@@ -125,7 +139,7 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
        gchar **lines;
        int i = 0;
 
-       if (size > 8192) {
+       if (size > 128 && !direction) {
                log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
                return;
        }
@@ -191,7 +205,7 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
                else {
                        gchar tmp[64];
                        strncpy2(tmp, lines[i], 63);
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
+                       log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
                                  llen-64);
                }
                i++;
@@ -242,7 +256,7 @@ static 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;
+gboolean etpan_skip_ssl_cert_check = FALSE;
 extern void mailsasl_ref(void);
 
 void imap_main_init(gboolean skip_ssl_cert_check)
@@ -265,8 +279,11 @@ void imap_main_init(gboolean skip_ssl_cert_check)
        
        fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
        
+#ifndef G_OS_WIN32
        io_channel = g_io_channel_unix_new(fd_thread_manager);
-       
+#else
+       io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
+#endif
        thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
                                                    thread_manager_event,
                                                    (gpointer) NULL,
@@ -281,7 +298,11 @@ void imap_main_set_timeout(int sec)
 
 void imap_main_done(void)
 {
+       imap_disconnect_all();
        etpan_thread_manager_stop(thread_manager);
+#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
+       return;
+#endif
        etpan_thread_manager_join(thread_manager);
        
        g_source_remove(thread_manager_signal);
@@ -367,6 +388,11 @@ static mailimap * get_imap(Folder * folder)
        return imap;
 }
 
+static gboolean cb_show_error(gpointer data)
+{
+       mainwindow_show_error();
+       return FALSE;
+}
 
 static void generic_cb(int cancelled, void * result, void * callback_data)
 {
@@ -379,7 +405,7 @@ static void generic_cb(int cancelled, void * result, void * callback_data)
            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();
+               g_timeout_add(10, cb_show_error, NULL);
        } 
        op->finished = 1;
 }
@@ -423,6 +449,7 @@ static void threaded_run(Folder * folder, void * param, void * result,
 
 struct connect_param {
        mailimap * imap;
+       PrefsAccount *account;
        const char * server;
        int port;
 };
@@ -501,9 +528,9 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
                g_warning("no cert presented.\n");
                return 0;
        }
-       cert = d2i_X509(NULL, &certificate, len);
+       cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
        if (cert == NULL) {
-               g_warning("can't get cert\n");
+               g_warning("IMAP: can't get cert\n");
                return 0;
        } else if (ssl_certificate_check(cert, NULL,
                (gchar *)param->server, (gushort)param->port) == TRUE) {
@@ -513,8 +540,86 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
                X509_free(cert);
                return -1;
        }
-#else
+#elif USE_GNUTLS
+       struct connect_param *param = (struct connect_param *)data;
+       gnutls_x509_crt cert = NULL;
+       gnutls_datum tmp;
+       
+       if (certificate == NULL || len < 0) {
+               g_warning("no cert presented.\n");
+               return 0;
+       }
+       
+       tmp.data = malloc(len);
+       memcpy(tmp.data, certificate, len);
+       tmp.size = len;
+       gnutls_x509_crt_init(&cert);
+       if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
+               g_warning("IMAP: can't get cert\n");
+               return 0;
+       } else if (ssl_certificate_check(cert, (guint)-1, NULL,
+               (gchar *)param->server, (gushort)param->port) == TRUE) {
+               gnutls_x509_crt_deinit(cert);
+               return 0;
+       } else {
+               gnutls_x509_crt_deinit(cert);
+               return -1;
+       }
+#endif
        return 0;
+}
+
+static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
+{
+#if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
+       PrefsAccount *account = (PrefsAccount *)data;
+       const gchar *cert_path = NULL;
+       const gchar *password = NULL;
+#ifdef USE_OPENSSL
+       X509 *x509 = NULL;
+       EVP_PKEY *pkey = NULL;
+#else
+       gnutls_x509_crt x509 = NULL;
+       gnutls_x509_privkey pkey = NULL;
+#endif
+
+       if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
+               cert_path = account->in_ssl_client_cert_file;
+       if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
+               password = account->in_ssl_client_cert_pass;
+       
+       if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
+           mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
+               debug_print("Impossible to set the client certificate.\n");
+       x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
+       pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
+       if (!(x509 && pkey)) {
+               /* try pkcs12 format */
+               ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
+       }
+       if (x509 && pkey) {
+               unsigned char *x509_der = NULL, *pkey_der = NULL;
+               size_t x509_len, pkey_len;
+               
+#ifndef USE_GNUTLS
+               x509_len = (size_t)i2d_X509(x509, &x509_der);
+               pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
+#else
+               x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
+               pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
+#endif
+               if (x509_len > 0 && pkey_len > 0) {
+                       if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
+                           mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0) 
+                               log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
+                       g_free(x509_der);
+                       g_free(pkey_der);
+               }
+#ifdef USE_GNUTLS
+               gnutls_x509_crt_deinit(x509);
+               gnutls_x509_privkey_deinit(pkey);
+#endif
+       }
 #endif
 }
 
@@ -529,8 +634,9 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        CHECK_IMAP();
 
-       r = mailimap_ssl_connect(param->imap,
-                                param->server, param->port);
+       r = mailimap_ssl_connect_with_callback(param->imap,
+                                               param->server, param->port,
+                                               connect_ssl_context_cb, param->account);
        result->error = r;
 }
 
@@ -562,7 +668,8 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        param.imap = imap;
        param.server = server;
        param.port = port;
-       
+       param.account = folder->account;
+
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_ssl_run);
 
@@ -867,6 +974,8 @@ static void login_run(struct etpan_thread_op * op)
 #endif
        
        result->error = r;
+       if (param->imap->imap_response)
+               imap_logger_cmd(0, param->imap->imap_response, strlen(param->imap->imap_response));
        debug_print("imap login run - end %i\n", r);
 }
 
@@ -999,7 +1108,12 @@ static void noop_run(struct etpan_thread_op * op)
        debug_print("imap noop run - end %i\n", r);
 }
 
-int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
+int imap_threaded_noop(Folder * folder, unsigned int * p_exists, 
+                      unsigned int *p_recent, 
+                      unsigned int *p_expunge,
+                      unsigned int *p_unseen,
+                      unsigned int *p_uidnext,
+                      unsigned int *p_uidval)
 {
        struct noop_param param;
        struct noop_result result;
@@ -1012,14 +1126,28 @@ int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
 
        threaded_run(folder, &param, &result, noop_run);
        
-       if (imap && imap->imap_selection_info != NULL) {
+       if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
                * p_exists = imap->imap_selection_info->sel_exists;
-       }
-       else {
+               * p_recent = imap->imap_selection_info->sel_recent;
+               * p_unseen = imap->imap_selection_info->sel_unseen;
+               * p_uidnext = imap->imap_selection_info->sel_uidnext;
+               * p_uidval = imap->imap_selection_info->sel_uidvalidity;
+       } else {
                * p_exists = 0;
-       }
-       
-       debug_print("imap noop - end\n");
+               * p_recent = 0;
+               * p_unseen = 0;
+               * p_uidnext = 0;
+               * p_uidval = 0;
+       }
+       if (result.error == 0 && imap && imap->imap_response_info != NULL &&
+           imap->imap_response_info->rsp_expunged != NULL) {
+               * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
+       } else {
+               * p_expunge = 0;
+       }       
+       debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
+               *p_exists, *p_recent, *p_expunge, *p_unseen,
+               *p_uidnext, *p_uidval);
        
        return result.error;
 }
@@ -1059,7 +1187,7 @@ static void starttls_run(struct etpan_thread_op * op)
                        return;
                }
 
-               tls_low = mailstream_low_tls_open(fd);
+               tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
                if (tls_low == NULL) {
                        debug_print("imap starttls run - can't tls_open\n");
                        result->error = MAILIMAP_ERROR_STREAM;
@@ -1082,7 +1210,8 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        param.imap = get_imap(folder);
        param.server = host;
        param.port = port;
-       
+       param.account = folder->account;
+
        threaded_run(folder, &param, &result, starttls_run);
        
        debug_print("imap starttls - end\n");
@@ -1267,12 +1396,13 @@ static void select_run(struct etpan_thread_op * op)
 
 int imap_threaded_select(Folder * folder, const char * mb,
                         gint * exists, gint * recent, gint * unseen,
-                        guint32 * uid_validity)
+                        guint32 * uid_validity,gint *can_create_flags,
+                        GSList **ok_flags)
 {
        struct select_param param;
        struct select_result result;
        mailimap * imap;
-       
+
        debug_print("imap select - begin\n");
        
        imap = get_imap(folder);
@@ -1291,13 +1421,104 @@ int imap_threaded_select(Folder * folder, const char * mb,
        * recent = imap->imap_selection_info->sel_recent;
        * unseen = imap->imap_selection_info->sel_unseen;
        * uid_validity = imap->imap_selection_info->sel_uidvalidity;
-       
+       * can_create_flags = FALSE;
+
+       if (imap->imap_selection_info->sel_perm_flags) {
+               GSList *t_flags = NULL;
+               clistiter *cur = NULL;
+               if (imap->imap_selection_info->sel_perm_flags)
+                       cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
+
+               for (; cur; cur = clist_next(cur)) {
+                       struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
+                       if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
+                               *can_create_flags = TRUE;
+                       else if (flag->fl_flag && 
+                                       flag->fl_flag->fl_type == 6 &&
+                                       !strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
+                               *can_create_flags = TRUE; 
+                       if (ok_flags) {
+                               MsgPermFlags c_flag = 0;
+                               switch (flag->fl_flag->fl_type) {
+                               case MAILIMAP_FLAG_ANSWERED:
+                                       c_flag = IMAP_FLAG_ANSWERED;
+                                       break;
+                               case MAILIMAP_FLAG_FLAGGED:
+                                       c_flag = IMAP_FLAG_FLAGGED;
+                                       break;
+                               case MAILIMAP_FLAG_DELETED:
+                                       c_flag = IMAP_FLAG_DELETED;
+                                       break;
+                               case MAILIMAP_FLAG_DRAFT:
+                                       c_flag = IMAP_FLAG_DRAFT;
+                                       break;
+                               case MAILIMAP_FLAG_SEEN:
+                                       c_flag = IMAP_FLAG_SEEN;
+                                       break;
+                               case MAILIMAP_FLAG_KEYWORD:
+                                       if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
+                                               c_flag = IMAP_FLAG_FORWARDED;
+                                       if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "Junk"))
+                                               c_flag = IMAP_FLAG_SPAM;
+                                       if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NonJunk") ||
+                                           !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NoJunk") ||
+                                           !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NotJunk"))
+                                               c_flag = IMAP_FLAG_HAM;
+                                       break;
+                               default:
+                                       break;
+                               }
+                               if (c_flag != 0) {
+                                       t_flags = g_slist_prepend(t_flags, 
+                                               GUINT_TO_POINTER(c_flag));
+                               }
+                       }
+               }
+               if (ok_flags)
+                       *ok_flags = t_flags;
+       }
        debug_print("imap select - end\n");
        
        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;
@@ -1461,6 +1682,22 @@ static void search_run(struct etpan_thread_op * op)
                                                          NULL, NULL, NULL, NULL, NULL,
                                                          NULL, 0, NULL, NULL, NULL);
                break;
+       case IMAP_SEARCH_TYPE_FORWARDED:
+               search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         strdup("$Forwarded"), NULL, NULL, NULL, NULL,
+                                                         NULL, NULL, NULL, NULL, 0,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, 0, NULL, NULL, NULL);
+               break;
+       case IMAP_SEARCH_TYPE_SPAM:
+               search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         strdup("Junk"), 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) {
@@ -1533,7 +1770,7 @@ static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
 static int
 result_to_uid_list(clist * fetch_result, carray ** result)
 {
-       clistiter * cur;
+       clistiter * cur = NULL;
        int r;
        int res;
        carray * tab;
@@ -1544,8 +1781,10 @@ result_to_uid_list(clist * fetch_result, carray ** result)
                goto err;
        }
        
-       for(cur = clist_begin(fetch_result) ; cur != NULL ;
-           cur = clist_next(cur)) {
+       if (fetch_result)
+               cur = clist_begin(fetch_result);
+
+       for(; cur != NULL ; cur = clist_next(cur)) {
                struct mailimap_msg_att * msg_att;
                uint32_t uid;
                uint32_t * puid;
@@ -1617,9 +1856,12 @@ static int imap_get_messages_list(mailimap * imap,
                goto free_fetch_type;
        }
 
+       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);
 
@@ -1670,7 +1912,7 @@ static void fetch_uid_run(struct etpan_thread_op * op)
        CHECK_IMAP();
 
        fetch_result = NULL;
-       mailstream_logger = NULL;
+       mailstream_logger = imap_logger_noop;
        log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
 
        r = imap_get_messages_list(param->imap, param->first_index,
@@ -1725,24 +1967,27 @@ void imap_fetch_uid_list_free(carray * uid_list)
 
 
 
-static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn);
+static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **tags);
 
 static int
 result_to_uid_flags_list(clist * fetch_result, carray ** result)
 {
-       clistiter * cur;
+       clistiter * cur = NULL;
        int r;
        int res;
        carray * tab;
-       
+       GSList *tags = NULL;
+
        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)) {
+
+       if (fetch_result)
+               cur = clist_begin(fetch_result);
+
+       for(; cur != NULL ; cur = clist_next(cur)) {
                struct mailimap_msg_att * msg_att;
                uint32_t uid;
                uint32_t * puid;
@@ -1750,6 +1995,8 @@ result_to_uid_flags_list(clist * fetch_result, carray ** result)
                int flags;
                int * pflags;
                
+               tags = NULL;
+
                msg_att = clist_content(cur);
                
                uid = 0;
@@ -1760,7 +2007,7 @@ result_to_uid_flags_list(clist * fetch_result, carray ** result)
                if (att_dyn == NULL)
                        continue;
                
-               flags = imap_flags_to_flags(att_dyn);
+               flags = imap_flags_to_flags(att_dyn, &tags);
                
                puid = malloc(sizeof(* puid));
                if (puid == NULL) {
@@ -1787,6 +2034,12 @@ result_to_uid_flags_list(clist * fetch_result, carray ** result)
                        res = MAILIMAP_ERROR_MEMORY;
                        goto free_list;
                }
+               r = carray_add(tab, tags, NULL);
+               if (r < 0) {
+                       free(pflags);
+                       res = MAILIMAP_ERROR_MEMORY;
+                       goto free_list;
+               }
        }
                
        * result = tab;
@@ -1795,6 +2048,8 @@ result_to_uid_flags_list(clist * fetch_result, carray ** result)
   
  free_list:
        imap_fetch_uid_flags_list_free(tab);
+       slist_free_strings(tags);
+       g_slist_free(tags);
  err:
        return res;
 }
@@ -1849,9 +2104,12 @@ static int imap_get_messages_flags_list(mailimap * imap,
                goto free_fetch_type;
        }
 
+       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);
 
@@ -1912,7 +2170,7 @@ int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
        param.imap = imap;
        param.first_index = first_index;
        
-       mailstream_logger = NULL;
+       mailstream_logger = imap_logger_noop;
        log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
 
        threaded_run(folder, &param, &result, fetch_uid_flags_run);
@@ -1935,11 +2193,13 @@ void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
 {
        unsigned int i;
        
-       for(i = 0 ; i < carray_count(uid_flags_list) ; i ++) {
+       for(i = 0 ; i < carray_count(uid_flags_list) ; i += 3) {
                void * data;
                
                data = carray_get(uid_flags_list, i);
                free(data);
+               data = carray_get(uid_flags_list, i + 1);
+               free(data);
        }
        carray_free(uid_flags_list);
 }
@@ -2006,7 +2266,7 @@ static int imap_fetch(mailimap * imap,
                return r;
        }
   
-       if (clist_begin(fetch_result) == NULL) {
+       if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
                mailimap_fetch_list_free(fetch_result);
                return MAILIMAP_ERROR_FETCH;
        }
@@ -2016,8 +2276,12 @@ static int imap_fetch(mailimap * imap,
        text = NULL;
        text_length = 0;
 
-       for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
-           cur = clist_next(cur)) {
+       if (msg_att->att_list)
+               cur = clist_begin(msg_att->att_list);
+       else
+               cur = NULL;
+
+       for(; cur != NULL ; cur = clist_next(cur)) {
                msg_att_item = clist_content(cur);
 
                if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
@@ -2093,8 +2357,11 @@ static int imap_fetch_header(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);
 
@@ -2105,7 +2372,7 @@ static int imap_fetch_header(mailimap * imap,
     return r;
   }
 
-  if (clist_begin(fetch_result) == NULL) {
+  if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
     mailimap_fetch_list_free(fetch_result);
     return MAILIMAP_ERROR_FETCH;
   }
@@ -2115,8 +2382,12 @@ static int imap_fetch_header(mailimap * imap,
   text = NULL;
   text_length = 0;
 
-  for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
-      cur = clist_next(cur)) {
+  if (msg_att->att_list)
+     cur = clist_begin(msg_att->att_list);
+  else
+     cur = NULL;
+
+  for(; cur != NULL ; cur = clist_next(cur)) {
     msg_att_item = clist_content(cur);
 
     if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
@@ -2188,7 +2459,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
        result->error = r;
        
        if (r == MAILIMAP_NO_ERROR) {
-               fd = open(param->filename, O_RDWR | O_CREAT, 0600);
+               fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
                if (fd < 0) {
                        result->error = MAILIMAP_ERROR_FETCH;
                        goto free;
@@ -2201,14 +2472,15 @@ static void fetch_content_run(struct etpan_thread_op * op)
                }
                
                r = fwrite(content, 1, content_size, f);
-               if (r == 0) {
+               if (r < content_size) {
+                       result->error = MAILIMAP_ERROR_FETCH;
                        goto fclose;
                }
                
                r = fclose(f);
                if (r == EOF) {
-                       g_unlink(param->filename);
-                       goto close;
+                       result->error = MAILIMAP_ERROR_FETCH;
+                       goto unlink;
                }
                goto free;
                
@@ -2218,14 +2490,16 @@ static void fetch_content_run(struct etpan_thread_op * op)
        close:
                close(fd);
        unlink:
-               g_unlink(param->filename);
+               claws_unlink(param->filename);
        
        free:
+               /* mmap_string_unref is a simple free in libetpan
+                * when it has MMAP_UNAVAILABLE defined */
                if (mmap_string_unref(content) != 0)
                        free(content);
        }
        
-       debug_print("imap fetch_content run - end %i\n", r);
+       debug_print("imap fetch_content run - end %i\n", result->error);
 }
 
 int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
@@ -2256,12 +2530,13 @@ int imap_threaded_fetch_content(Folder * folder, uint32_t msg_index,
 
 
 
-static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
+static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList **s_tags)
 {
        int flags;
        clist * flag_list;
        clistiter * cur;
-       
+       GSList *tags = NULL;
+
        flags = MSG_UNREAD;
        
        flag_list = att_dyn->att_list;
@@ -2290,10 +2565,23 @@ static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn)
                                flags &= ~MSG_UNREAD;
                                flags &= ~MSG_NEW;
                                break;
+                       case MAILIMAP_FLAG_KEYWORD:
+                               if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
+                                       flags |= MSG_FORWARDED;
+                               else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "Junk")) 
+                                       flags |= MSG_SPAM;
+                               else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NonJunk") ||
+                                        !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NoJunk") ||
+                                        !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NotJunk")) 
+                                       flags &= ~MSG_SPAM;
+                               else if (s_tags)
+                                       tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
+                               break;
                        }
                }
        }
-       
+       if (s_tags)
+               *s_tags = tags;
        return flags;
 }
 
@@ -2314,8 +2602,11 @@ static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
   ref_size = 0;
   att_dyn = NULL;
 
-  for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
-      item_cur = clist_next(item_cur)) {
+  if (msg_att->att_list)
+     item_cur = clist_begin(msg_att->att_list);
+  else
+     item_cur = NULL;
+  for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
     struct mailimap_msg_att_item * item;
 
     item = clist_content(item_cur);
@@ -2359,14 +2650,14 @@ static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
 }
 
 static struct imap_fetch_env_info *
-fetch_to_env_info(struct mailimap_msg_att * msg_att)
+fetch_to_env_info(struct mailimap_msg_att * msg_att, GSList **tags)
 {
        struct imap_fetch_env_info * info;
        uint32_t uid;
        char * headers;
        size_t size;
        struct mailimap_msg_att_dynamic * att_dyn;
-       
+
        imap_get_msg_att_info(msg_att, &uid, &headers, &size,
                              &att_dyn);
        
@@ -2376,7 +2667,7 @@ fetch_to_env_info(struct mailimap_msg_att * msg_att)
        info->uid = uid;
        info->headers = strdup(headers);
        info->size = size;
-       info->flags = imap_flags_to_flags(att_dyn);
+       info->flags = imap_flags_to_flags(att_dyn, tags);
        
        return info;
 }
@@ -2388,25 +2679,30 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
        clistiter * cur;
        unsigned int i;
        carray * env_list;
+
        i = 0;
-  
        env_list = carray_new(16);
   
-       for(cur = clist_begin(fetch_result) ; cur != NULL ;
-           cur = clist_next(cur)) {
-               struct mailimap_msg_att * msg_att;
-               struct imap_fetch_env_info * env_info;
-    
-               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);
+       if (fetch_result) {
+               for(cur = clist_begin(fetch_result) ; cur != NULL ;
+                   cur = clist_next(cur)) {
+                       struct mailimap_msg_att * msg_att;
+                       struct imap_fetch_env_info * env_info;
+                       GSList *tags = NULL;
+
+                       msg_att = clist_content(cur);
+
+                       env_info = fetch_to_env_info(msg_att, &tags);
+                       if (!env_info)
+                               return MAILIMAP_ERROR_MEMORY;
+                       carray_add(env_list, env_info, NULL);
+                       carray_add(env_list, tags, NULL);
+               }
+               * p_env_list = env_list;
+       } else {
+               * p_env_list = NULL;
        }
-  
-       * p_env_list = env_list;
-  
+
        return MAIL_NO_ERROR;
 }
 
@@ -2494,8 +2790,11 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        else
                r = imap_add_header_fetch_att(fetch_type);
        
+       mailstream_logger = imap_logger_fetch;
+       
        r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
        
+       mailstream_logger = imap_logger_cmd;
        switch (r) {
        case MAILIMAP_NO_ERROR:
                break;
@@ -2505,7 +2804,7 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                return r;
        }
        
-       if (clist_begin(fetch_result) == NULL) {
+       if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
                res = MAILIMAP_ERROR_FETCH;
                debug_print("clist_begin = NULL\n");
                goto err;
@@ -2609,7 +2908,7 @@ void imap_fetch_env_free(carray * env_list)
 {
        unsigned int i;
        
-       for(i = 0 ; i < carray_count(env_list) ; i ++) {
+       for(i = 0 ; i < carray_count(env_list) ; i += 2) {
                struct imap_fetch_env_info * env_info;
                
                env_info = carray_get(env_list, i);
@@ -2642,8 +2941,10 @@ static void append_run(struct etpan_thread_op * op)
        int r;
        char * data;
        size_t size;
+#ifndef G_OS_WIN32
        struct stat stat_buf;
        int fd;
+#endif
        guint32 uid = 0, val = 0;
        
        param = op->param;
@@ -2651,6 +2952,7 @@ static void append_run(struct etpan_thread_op * op)
        
        CHECK_IMAP();
 
+#ifndef G_OS_WIN32
        r = stat(param->filename, &stat_buf);
        if (r < 0) {
                result->error = MAILIMAP_ERROR_APPEND;
@@ -2658,7 +2960,7 @@ static void append_run(struct etpan_thread_op * op)
        }
        size = stat_buf.st_size;
        
-       fd = open(param->filename, O_RDONLY);
+       fd = g_open(param->filename, O_RDONLY);
        if (fd < 0) {
                result->error = MAILIMAP_ERROR_APPEND;
                return;
@@ -2670,7 +2972,14 @@ static void append_run(struct etpan_thread_op * op)
                result->error = MAILIMAP_ERROR_APPEND;
                return;
        }
-       
+#else
+       data = file_read_to_str_no_recode(param->filename);
+       if (data == NULL) {
+               result->error = MAILIMAP_ERROR_APPEND;
+               return;
+       }
+       size = strlen(data);
+#endif
        mailstream_logger = imap_logger_append;
        
        r = mailimap_uidplus_append(param->imap, param->mailbox,
@@ -2679,8 +2988,12 @@ static void append_run(struct etpan_thread_op * op)
 
        mailstream_logger = imap_logger_cmd;
        
+#ifndef G_OS_WIN32
        munmap(data, size);
        close(fd);
+#else
+       g_free(data);
+#endif
        
        result->error = r;
        result->uid = uid;
@@ -2887,7 +3200,7 @@ int imap_threaded_store(Folder * folder, struct mailimap_set * set,
 
 
 #define ENV_BUFFER_SIZE 512
-
+#ifndef G_OS_WIN32
 static void do_exec_command(int fd, const char * command,
                            const char * servername, uint16_t port)
 {
@@ -2902,37 +3215,20 @@ static void do_exec_command(int fd, const char * command,
                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);
+               g_setenv("ETPANSERVER", servername, TRUE);
        else
-               unsetenv("ETPANSERVER");
-#endif
+               g_unsetenv("ETPANSERVER");
   
-#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);
+               g_setenv("ETPANPORT", porttext, TRUE);
        }
        else {
-               unsetenv("ETPANPORT");
+               g_unsetenv("ETPANPORT");
        }
-#endif
                
        /* Not a lot we can do if there's an error other than bail. */
        if (dup2(fd, 0) == -1)
@@ -2950,7 +3246,7 @@ static void do_exec_command(int fd, const char * command,
        /* Detach from the controlling tty if we have one. Otherwise,
           SSH might do something stupid like trying to use it instead
           of running $SSH_ASKPASS. Doh. */
-       fd = open("/dev/tty", O_RDONLY);
+       fd = g_open("/dev/tty", O_RDONLY);
        if (fd != -1) {
                ioctl(fd, TIOCNOTTY, NULL);
                close(fd);
@@ -3086,6 +3382,7 @@ int imap_threaded_connect_cmd(Folder * folder, const char * command,
        
        return result.error;
 }
+#endif /* G_OS_WIN32 */
 
 void imap_threaded_cancel(Folder * folder)
 {