2005-06-17 [colin] 1.9.11cvs80
[claws.git] / src / imap.c
index 9e3abfca6bc0cec29f9ca91a42407468f45a74db..5c184c17af3c9a3a1a63106ca5338040a5fbde2d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
  *
  * 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
@@ -24,6 +24,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -41,8 +42,8 @@
 #include "folder.h"
 #include "session.h"
 #include "procmsg.h"
-#include "intl.h"
 #include "imap.h"
+#include "imap_gtk.h"
 #include "socket.h"
 #include "ssl.h"
 #include "recv.h"
 #include "log.h"
 #include "remotefolder.h"
 #include "alertpanel.h"
+#include "sylpheed.h"
+#include "statusbar.h"
+#include "msgcache.h"
+
+#ifdef USE_PTHREAD
+#include <pthread.h>
+#endif
 
 typedef struct _IMAPFolder     IMAPFolder;
 typedef struct _IMAPSession    IMAPSession;
@@ -92,7 +100,6 @@ struct _IMAPSession
        guint cmd_count;
 
        /* CLAWS */
-       time_t last_access_time;
        gboolean folder_content_changed;
        guint exists;
 };
@@ -138,7 +145,7 @@ typedef enum
 
 #define QUOTE_IF_REQUIRED(out, str)                            \
 {                                                              \
-       if (*str != '"' && strpbrk(str, " \t(){}%*") != NULL) { \
+       if (*str != '"' && strpbrk(str, " \t(){}[]%*\\") != NULL) {     \
                gchar *__tmp;                                   \
                gint len;                                       \
                                                                \
@@ -160,6 +167,7 @@ struct _IMAPFolderItem
        guint lastuid;
        guint uid_next;
        GSList *uid_list;
+       gboolean batching;
 };
 
 static void imap_folder_init           (Folder         *folder,
@@ -178,6 +186,11 @@ static void        imap_session_destroy    (Session        *session);
 static gchar   *imap_fetch_msg         (Folder         *folder, 
                                         FolderItem     *item, 
                                         gint            uid);
+static gchar   *imap_fetch_msg_full    (Folder         *folder, 
+                                        FolderItem     *item, 
+                                        gint            uid,
+                                        gboolean        headers,
+                                        gboolean        body);
 static gint    imap_add_msg            (Folder         *folder,
                                         FolderItem     *dest,
                                         const gchar    *file, 
@@ -198,6 +211,10 @@ static gint        imap_copy_msgs          (Folder         *folder,
 static gint    imap_remove_msg         (Folder         *folder, 
                                         FolderItem     *item, 
                                         gint            uid);
+static gint    imap_remove_msgs        (Folder         *folder, 
+                                        FolderItem     *dest, 
+                                        MsgInfoList    *msglist, 
+                                        GRelation      *relation);
 static gint    imap_remove_all_msg     (Folder         *folder, 
                                         FolderItem     *item);
 
@@ -252,7 +269,9 @@ static gint imap_do_copy_msgs               (Folder         *folder,
                                         GRelation      *relation);
 
 static void imap_delete_all_cached_messages    (FolderItem     *item);
-
+static void imap_set_batch             (Folder         *folder,
+                                        FolderItem     *item,
+                                        gboolean        batch);
 #if USE_OPENSSL
 static SockInfo *imap_open             (const gchar    *server,
                                         gushort         port,
@@ -288,7 +307,8 @@ static gint imap_select                     (IMAPSession    *session,
                                         gint           *exists,
                                         gint           *recent,
                                         gint           *unseen,
-                                        guint32        *uid_validity);
+                                        guint32        *uid_validity,
+                                        gboolean        block);
 static gint imap_status                        (IMAPSession    *session,
                                         IMAPFolder     *folder,
                                         const gchar    *path,
@@ -296,7 +316,8 @@ static gint imap_status                     (IMAPSession    *session,
                                         gint           *recent,
                                         guint32        *uid_next,
                                         guint32        *uid_validity,
-                                        gint           *unseen);
+                                        gint           *unseen,
+                                        gboolean        block);
 
 static void imap_parse_namespace               (IMAPSession    *session,
                                                 IMAPFolder     *folder);
@@ -334,7 +355,9 @@ static gint imap_cmd_login  (IMAPSession    *session,
                                 const gchar    *pass);
 static gint imap_cmd_logout    (IMAPSession    *session);
 static gint imap_cmd_noop      (IMAPSession    *session);
+#if USE_OPENSSL
 static gint imap_cmd_starttls  (IMAPSession    *session);
+#endif
 static gint imap_cmd_namespace (IMAPSession    *session,
                                 gchar         **ns_str);
 static gint imap_cmd_list      (IMAPSession    *session,
@@ -347,19 +370,22 @@ static gint imap_cmd_do_select    (IMAPSession    *session,
                                 gint           *exists,
                                 gint           *recent,
                                 gint           *unseen,
-                                guint32        *uid_validity);
+                                guint32        *uid_validity,
+                                gboolean        block);
 static gint imap_cmd_select    (IMAPSession    *session,
                                 const gchar    *folder,
                                 gint           *exists,
                                 gint           *recent,
                                 gint           *unseen,
-                                guint32        *uid_validity);
+                                guint32        *uid_validity,
+                                gboolean        block);
 static gint imap_cmd_examine   (IMAPSession    *session,
                                 const gchar    *folder,
                                 gint           *exists,
                                 gint           *recent,
                                 gint           *unseen,
-                                guint32        *uid_validity);
+                                guint32        *uid_validity,
+                                gboolean        block);
 static gint imap_cmd_create    (IMAPSession    *sock,
                                 const gchar    *folder);
 static gint imap_cmd_rename    (IMAPSession    *sock,
@@ -371,7 +397,9 @@ static gint imap_cmd_envelope       (IMAPSession    *session,
                                 IMAPSet         set);
 static gint imap_cmd_fetch     (IMAPSession    *sock,
                                 guint32         uid,
-                                const gchar    *filename);
+                                const gchar    *filename,
+                                gboolean        headers,
+                                gboolean        body);
 static gint imap_cmd_append    (IMAPSession    *session,
                                 const gchar    *destfolder,
                                 const gchar    *file,
@@ -390,10 +418,22 @@ static gint imap_cmd_close      (IMAPSession    *session);
 
 static gint imap_cmd_ok                (IMAPSession    *session,
                                 GPtrArray      *argbuf);
+static gint imap_cmd_ok_block  (IMAPSession    *session,
+                                GPtrArray      *argbuf);
+static gint imap_cmd_ok_with_block
+                               (IMAPSession    *session, 
+                                GPtrArray      *argbuf, 
+                                gboolean       block);
 static void imap_gen_send      (IMAPSession    *session,
                                 const gchar    *format, ...);
 static gint imap_gen_recv      (IMAPSession    *session,
                                 gchar         **ret);
+static gint imap_gen_recv_block        (IMAPSession    *session,
+                                gchar         **ret);
+static gint imap_gen_recv_with_block   
+                               (IMAPSession    *session,
+                                gchar         **ret,
+                                gboolean        block);
 
 /* misc utility functions */
 static gchar *strchr_cpy                       (const gchar    *src,
@@ -435,60 +475,145 @@ static gboolean imap_scan_required               (Folder         *folder,
 static void imap_change_flags                  (Folder         *folder,
                                                 FolderItem     *item,
                                                 MsgInfo        *msginfo,
-                                                MsgPermFlags   newflags);
+                                                MsgPermFlags    newflags);
+static gint imap_get_flags                     (Folder         *folder,
+                                                FolderItem     *item,
+                                                MsgInfoList    *msglist,
+                                                GRelation      *msgflags);
 static gchar *imap_folder_get_path             (Folder         *folder);
 static gchar *imap_item_get_path               (Folder         *folder,
                                                 FolderItem     *item);
+static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
+static GHashTable *flags_set_table = NULL;
+static GHashTable *flags_unset_table = NULL;
+typedef struct _hashtable_data {
+       IMAPSession *session;
+       GSList *msglist;
+} hashtable_data;
 
-static FolderClass imap_class =
-{
-       F_IMAP,
-       "imap",
-       "IMAP4",
-
-       /* Folder functions */
-       imap_folder_new,
-       imap_folder_destroy,
-       NULL,
-       NULL,
-       imap_scan_tree,
-       imap_create_tree,
-
-       /* FolderItem functions */
-       imap_folder_item_new,
-       imap_folder_item_destroy,
-       NULL,
-       NULL,
-       imap_item_get_path,
-       imap_create_folder,
-       imap_rename_folder,
-       imap_remove_folder,
-       imap_close,
-       imap_get_num_list,
-       NULL,
-       NULL,
-       NULL,
-       imap_scan_required,
-
-       /* Message functions */
-       imap_get_msginfo,
-       imap_get_msginfos,
-       imap_fetch_msg,
-       imap_add_msg,
-       imap_add_msgs,
-       imap_copy_msg,
-       imap_copy_msgs,
-       imap_remove_msg,
-       imap_remove_all_msg,
-       imap_is_msg_changed,
-       imap_change_flags,
-};
+static FolderClass imap_class;
+
+typedef struct _thread_data {
+       gchar *server;
+       gushort port;
+       gboolean done;
+       SockInfo *sock;
+#ifdef USE_OPENSSL
+       SSLType ssl_type;
+#endif
+} thread_data;
+
+#ifdef USE_PTHREAD
+void *imap_getline_thread(void *data)
+{
+       thread_data *td = (thread_data *)data;
+       gchar *line = NULL;
+       
+       line = sock_getline(td->sock);
+       
+       td->done = TRUE;
+       
+       return (void *)line;
+}
+#endif
+
+/* imap_getline just wraps sock_getline inside a thread, 
+ * performing gtk updates so that the interface isn't frozen.
+ */
+static gchar *imap_getline(SockInfo *sock)
+{
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       thread_data *td = g_new0(thread_data, 1);
+       pthread_t pt;
+       gchar *line;
+       td->sock = sock;
+       td->done = FALSE;
+       
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_getline_thread, td) != 0) {
+               g_free(td);
+               return sock_getline(sock);
+       }
+       
+       debug_print("+++waiting for imap_getline_thread...\n");
+       while(!td->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+       }
+       debug_print("---imap_getline_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, (void *)&line);
+       g_free(td);
+
+       return line;
+#else
+       return sock_getline(sock);
+#endif
+}
 
 FolderClass *imap_get_class(void)
 {
+       if (imap_class.idstr == NULL) {
+               imap_class.type = F_IMAP;
+               imap_class.idstr = "imap";
+               imap_class.uistr = "IMAP4";
+
+               /* Folder functions */
+               imap_class.new_folder = imap_folder_new;
+               imap_class.destroy_folder = imap_folder_destroy;
+               imap_class.scan_tree = imap_scan_tree;
+               imap_class.create_tree = imap_create_tree;
+
+               /* FolderItem functions */
+               imap_class.item_new = imap_folder_item_new;
+               imap_class.item_destroy = imap_folder_item_destroy;
+               imap_class.item_get_path = imap_item_get_path;
+               imap_class.create_folder = imap_create_folder;
+               imap_class.rename_folder = imap_rename_folder;
+               imap_class.remove_folder = imap_remove_folder;
+               imap_class.close = imap_close;
+               imap_class.get_num_list = imap_get_num_list;
+               imap_class.scan_required = imap_scan_required;
+
+               /* Message functions */
+               imap_class.get_msginfo = imap_get_msginfo;
+               imap_class.get_msginfos = imap_get_msginfos;
+               imap_class.fetch_msg = imap_fetch_msg;
+               imap_class.fetch_msg_full = imap_fetch_msg_full;
+               imap_class.add_msg = imap_add_msg;
+               imap_class.add_msgs = imap_add_msgs;
+               imap_class.copy_msg = imap_copy_msg;
+               imap_class.copy_msgs = imap_copy_msgs;
+               imap_class.remove_msg = imap_remove_msg;
+               imap_class.remove_msgs = imap_remove_msgs;
+               imap_class.remove_all_msg = imap_remove_all_msg;
+               imap_class.is_msg_changed = imap_is_msg_changed;
+               imap_class.change_flags = imap_change_flags;
+               imap_class.get_flags = imap_get_flags;
+               imap_class.set_batch = imap_set_batch;
+       }
+       
        return &imap_class;
 }
 
+static gchar *get_seq_set_from_seq_list(GSList *seq_list)
+{
+       gchar *val = NULL;
+       gchar *tmp = NULL;
+       GSList *cur = NULL;
+       
+       for (cur = seq_list; cur != NULL; cur = cur->next) {
+               tmp = val?g_strdup(val):NULL;
+               g_free(val);
+               val = g_strconcat(tmp?tmp:"", tmp?",":"",(gchar *)cur->data,
+                                 NULL);
+               g_free(tmp);
+       }
+       return val;
+}
+
+
 static Folder *imap_folder_new(const gchar *name, const gchar *path)
 {
        Folder *folder;
@@ -619,8 +744,9 @@ static IMAPSession *imap_session_get(Folder *folder)
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
        
-       if (prefs_common.work_offline)
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
                return NULL;
+       }
 
        /* Make sure we have a session */
        if (rfolder->session != NULL) {
@@ -660,19 +786,22 @@ static IMAPSession *imap_session_get(Folder *folder)
         * A better solution than sending a NOOP every time would be
         * for every command to be prepared to retry until it is
         * successfully sent. -- mbp */
-       if (time(NULL) - session->last_access_time > SESSION_TIMEOUT) {
+       if (time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) {
                /* verify that the session is still alive */
                if (imap_cmd_noop(session) != IMAP_SUCCESS) {
                        /* Check if this is the first try to establish a
                           connection, if yes we don't try to reconnect */
                        if (rfolder->session == NULL) {
-                               log_warning(_("Connecting %s failed"),
+                               log_warning(_("Connecting to %s failed"),
                                            folder->account->recv_server);
                                session_destroy(SESSION(session));
                                session = NULL;
                        } else {
                                log_warning(_("IMAP4 connection to %s has been"
-                                             " disconnected. Reconnecting...\n"),
+                                           " disconnected. Reconnecting...\n"),
+                                           folder->account->recv_server);
+                               statusbar_print_all(_("IMAP4 connection to %s has been"
+                                           " disconnected. Reconnecting...\n"),
                                            folder->account->recv_server);
                                session_destroy(SESSION(session));
                                /* Clear folders session to make imap_session_get create
@@ -681,14 +810,13 @@ static IMAPSession *imap_session_get(Folder *folder)
                                   endless loop */
                                rfolder->session = NULL;
                                session = imap_session_get(folder);
+                               statusbar_pop_all();
                        }
                }
        }
 
        rfolder->session = SESSION(session);
-       if (session) {
-               session->last_access_time = time(NULL);
-       }
+       
        return IMAP_SESSION(session);
 }
 
@@ -806,12 +934,15 @@ static void imap_session_authenticate(IMAPSession *session,
                Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
                g_free(tmp_pass);
        }
-
+       statusbar_print_all(_("Connecting to IMAP4 server %s...\n"),
+                               account->recv_server);
        if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
                imap_cmd_logout(session);
+               statusbar_pop_all();
+               
                return;
        }
-
+       statusbar_pop_all();
        session->authenticated = TRUE;
 }
 
@@ -824,6 +955,35 @@ static void imap_session_destroy(Session *session)
 }
 
 static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
+{
+       return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
+}
+
+static guint get_size_with_lfs(MsgInfo *info) 
+{
+       FILE *fp = NULL;
+       guint cnt = 0;
+       gchar buf[4096];
+       
+       if (info == NULL)
+               return -1;
+       
+       fp = procmsg_open_message(info);
+       if (!fp)
+               return -1;
+       
+       while (fgets(buf, sizeof (buf), fp) != NULL) {
+               cnt += strlen(buf);
+               if (!strstr(buf, "\r") && strstr(buf, "\n"))
+                       cnt++;
+       }
+       
+       fclose(fp);
+       return cnt;
+}
+
+static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
+                                 gboolean headers, gboolean body)
 {
        gchar *path, *filename;
        IMAPSession *session;
@@ -832,6 +992,9 @@ static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(item != NULL, NULL);
 
+       if (uid == 0)
+               return NULL;
+
        path = folder_item_get_path(item);
        if (!is_dir_exist(path))
                make_dir_hier(path);
@@ -839,8 +1002,24 @@ static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
        g_free(path);
 
        if (is_file_exist(filename)) {
-               debug_print("message %d has been already cached.\n", uid);
-               return filename;
+               /* see whether the local file represents the whole message
+                * or not. As the IMAP server reports size with \r chars,
+                * we have to update the local file (UNIX \n only) size */
+               MsgInfo *msginfo = imap_parse_msg(filename, item);
+               MsgInfo *cached = msgcache_get_msg(item->cache,uid);
+               guint have_size = get_size_with_lfs(msginfo);
+               debug_print("message %d has been already %scached (%d/%d).\n", uid,
+                               have_size == cached->size ? "fully ":"",
+                               have_size, cached? (int)cached->size : -1);
+                               
+               if (cached && (cached->size == have_size || !body)) {
+                       procmsg_msginfo_free(cached);
+                       procmsg_msginfo_free(msginfo);
+                       return filename;
+               } else {
+                       procmsg_msginfo_free(cached);
+                       procmsg_msginfo_free(msginfo);
+               }
        }
 
        session = imap_session_get(folder);
@@ -849,8 +1028,9 @@ static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
                return NULL;
        }
 
+       debug_print("IMAP fetching messages\n");
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
-                        NULL, NULL, NULL, NULL);
+                        NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
                g_warning("can't select mailbox %s\n", item->path);
                g_free(filename);
@@ -858,7 +1038,7 @@ static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
        }
 
        debug_print("getting message %d...\n", uid);
-       ok = imap_cmd_fetch(session, (guint32)uid, filename);
+       ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
 
        if (ok != IMAP_SUCCESS) {
                g_warning("can't fetch message %d\n", uid);
@@ -967,8 +1147,9 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        g_return_val_if_fail(msglist != NULL, -1);
 
        session = imap_session_get(folder);
-       if (!session) return -1;
-
+       if (!session) {
+               return -1;
+       }
        msginfo = (MsgInfo *)msglist->data;
 
        src = msginfo->folder;
@@ -978,9 +1159,10 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        }
 
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
+                        NULL, NULL, NULL, NULL, FALSE);
+       if (ok != IMAP_SUCCESS) {
                return ok;
+       }
 
        destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
        seq_list = imap_get_seq_set_from_msglist(msglist);
@@ -1025,6 +1207,11 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        imap_seq_set_free(seq_list);
 
        g_free(destdir);
+       
+       IMAP_FOLDER_ITEM(dest)->lastuid = 0;
+       IMAP_FOLDER_ITEM(dest)->uid_next = 0;
+       g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
+       IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
 
        if (ok == IMAP_SUCCESS)
                return last_num;
@@ -1071,96 +1258,87 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        return ret;
 }
 
-static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
+
+static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest, 
+                               MsgInfoList *msglist, GRelation *relation)
 {
-       gint ok;
+       gchar *destdir;
+       GSList *seq_list = NULL, *cur;
+       MsgInfo *msginfo;
        IMAPSession *session;
-       gchar *dir;
-       MsgNumberList numlist;
+       gint ok = IMAP_SUCCESS;
+       GRelation *uid_mapping;
        
        g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
-       g_return_val_if_fail(item != NULL, -1);
+       g_return_val_if_fail(dest != NULL, -1);
+       g_return_val_if_fail(msglist != NULL, -1);
 
        session = imap_session_get(folder);
-       if (!session) return -1;
+       if (!session) {
+               return -1;
+       }
+       msginfo = (MsgInfo *)msglist->data;
 
-       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
+       ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
+                        NULL, NULL, NULL, NULL, FALSE);
+       if (ok != IMAP_SUCCESS) {
                return ok;
+       }
+
+       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+       for (cur = msglist; cur; cur = cur->next) {
+               msginfo = (MsgInfo *)cur->data;
+               seq_list = g_slist_append(seq_list, GINT_TO_POINTER(msginfo->msgnum));
+       }
+
+       uid_mapping = g_relation_new(2);
+       g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
 
-       numlist.next = NULL;
-       numlist.data = GINT_TO_POINTER(uid);
-       
        ok = imap_set_message_flags
                (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
-               &numlist, IMAP_FLAG_DELETED, TRUE);
+               seq_list, IMAP_FLAG_DELETED, TRUE);
        if (ok != IMAP_SUCCESS) {
-               log_warning(_("can't set deleted flags: %d\n"), uid);
+               log_warning(_("can't set deleted flags\n"));
                return ok;
        }
-
-       if (!session->uidplus) {
-               ok = imap_cmd_expunge(session, NULL);
-       } else {
-               gchar *uidstr;
-
-               uidstr = g_strdup_printf("%u", uid);
-               ok = imap_cmd_expunge(session, uidstr);
-               g_free(uidstr);
-       }
+       ok = imap_cmd_expunge(session, NULL);
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't expunge\n"));
                return ok;
        }
+       
+       g_relation_destroy(uid_mapping);
+       g_slist_free(seq_list);
 
-       IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
-           IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
-       dir = folder_item_get_path(item);
-       if (is_dir_exist(dir))
-               remove_numbered_files(dir, uid, uid);
-       g_free(dir);
+       g_free(destdir);
 
-       return IMAP_SUCCESS;
+       if (ok == IMAP_SUCCESS)
+               return 0;
+       else
+               return -1;
 }
 
-static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
+static gint imap_remove_msgs(Folder *folder, FolderItem *dest, 
+                   MsgInfoList *msglist, GRelation *relation)
 {
-       gint ok;
-       IMAPSession *session;
-       gchar *dir;
+       MsgInfo *msginfo;
 
        g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(item != NULL, -1);
-
-       session = imap_session_get(folder);
-       if (!session) return -1;
-
-       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
-               return ok;
-
-       imap_gen_send(session, "STORE 1:* +FLAGS.SILENT (\\Deleted)");
-       ok = imap_cmd_ok(session, NULL);
-       if (ok != IMAP_SUCCESS) {
-               log_warning(_("can't set deleted flags: 1:*\n"));
-               return ok;
-       }
+       g_return_val_if_fail(dest != NULL, -1);
+       g_return_val_if_fail(msglist != NULL, -1);
 
-       ok = imap_cmd_expunge(session, NULL);
-       if (ok != IMAP_SUCCESS) {
-               log_warning(_("can't expunge\n"));
-               return ok;
-       }
+       msginfo = (MsgInfo *)msglist->data;
+       g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       dir = folder_item_get_path(item);
-       if (is_dir_exist(dir))
-               remove_all_numbered_files(dir);
-       g_free(dir);
+       return imap_do_remove_msgs(folder, dest, msglist, relation);
+}
 
-       return IMAP_SUCCESS;
+static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
+{
+       GSList *list = folder_item_get_msg_list(item);
+       gint res = imap_remove_msgs(folder, item, list, NULL);
+       g_slist_free(list);
+       return res;
 }
 
 static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
@@ -1190,6 +1368,7 @@ static gint imap_close(Folder *folder, FolderItem *item)
                        log_warning(_("can't close folder\n"));
 
                g_free(session->mbox);
+
                session->mbox = NULL;
 
                return ok;
@@ -1372,21 +1551,22 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
                } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
                        gchar *base;
 
-                       base = g_basename(new_item->path);
+                       base = g_path_get_basename(new_item->path);
 
-                       if (!folder->outbox && !strcasecmp(base, "Sent")) {
+                       if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
                                new_item->stype = F_OUTBOX;
                                folder->outbox = new_item;
-                       } else if (!folder->draft && !strcasecmp(base, "Drafts")) {
+                       } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
                                new_item->stype = F_DRAFT;
                                folder->draft = new_item;
-                       } else if (!folder->queue && !strcasecmp(base, "Queue")) {
+                       } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
                                new_item->stype = F_QUEUE;
                                folder->queue = new_item;
-                       } else if (!folder->trash && !strcasecmp(base, "Trash")) {
+                       } else if (!folder->trash && !g_ascii_strcasecmp(base, "Trash")) {
                                new_item->stype = F_TRASH;
                                folder->trash = new_item;
                        }
+                       g_free(base);
                }
 
                if (new_item->no_sub == FALSE)
@@ -1405,7 +1585,7 @@ static GSList *imap_parse_list(IMAPFolder *folder, IMAPSession *session,
        gchar flags[256];
        gchar separator_str[16];
        gchar *p;
-       const gchar *name;
+       gchar *base;
        gchar *loc_name, *loc_path;
        GSList *item_list = NULL;
        GString *str;
@@ -1464,10 +1644,10 @@ static GSList *imap_parse_list(IMAPFolder *folder, IMAPSession *session,
 
                if (separator_str[0] != '\0')
                        subst_char(buf, separator_str[0], '/');
-               name = g_basename(buf);
-               if (name[0] == '.') continue;
+               base = g_path_get_basename(buf);
+               if (base[0] == '.') continue;
 
-               loc_name = imap_modified_utf7_to_utf8(name);
+               loc_name = imap_modified_utf7_to_utf8(base);
                loc_path = imap_modified_utf7_to_utf8(buf);
                new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
                if (strcasestr(flags, "\\Noinferiors") != NULL)
@@ -1479,6 +1659,7 @@ static GSList *imap_parse_list(IMAPFolder *folder, IMAPSession *session,
                item_list = g_slist_append(item_list, new_item);
 
                debug_print("folder '%s' found.\n", loc_path);
+               g_free(base);
                g_free(loc_path);
                g_free(loc_name);
        }
@@ -1718,6 +1899,12 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        g_return_val_if_fail(item->path != NULL, -1);
        g_return_val_if_fail(name != NULL, -1);
 
+       if (strchr(name, imap_get_path_separator(IMAP_FOLDER(folder), item->path)) != NULL) {
+               g_warning(_("New folder name must not contain the namespace "
+                           "path separator"));
+               return -1;
+       }
+
        session = imap_session_get(folder);
        if (!session) return -1;
 
@@ -1726,7 +1913,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        g_free(session->mbox);
        session->mbox = NULL;
        ok = imap_cmd_examine(session, "INBOX",
-                             &exists, &recent, &unseen, &uid_validity);
+                             &exists, &recent, &unseen, &uid_validity, FALSE);
        if (ok != IMAP_SUCCESS) {
                g_free(real_oldpath);
                return -1;
@@ -1734,7 +1921,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
 
        separator = imap_get_path_separator(IMAP_FOLDER(folder), item->path);
        if (strchr(item->path, G_DIR_SEPARATOR)) {
-               dirpath = g_dirname(item->path);
+               dirpath = g_path_get_dirname(item->path);
                newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
                g_free(dirpath);
        } else
@@ -1799,7 +1986,7 @@ static gint imap_remove_folder(Folder *folder, FolderItem *item)
        path = imap_get_real_path(IMAP_FOLDER(folder), item->path);
 
        ok = imap_cmd_examine(session, "INBOX",
-                             &exists, &recent, &unseen, &uid_validity);
+                             &exists, &recent, &unseen, &uid_validity, FALSE);
        if (ok != IMAP_SUCCESS) {
                g_free(path);
                return -1;
@@ -1822,26 +2009,45 @@ static gint imap_remove_folder(Folder *folder, FolderItem *item)
        return 0;
 }
 
-static GSList *imap_get_uncached_messages(IMAPSession *session,
-                                         FolderItem *item,
-                                         MsgNumberList *numlist)
+typedef struct _uncached_data {
+       IMAPSession *session;
+       FolderItem *item;
+       MsgNumberList *numlist;
+       guint cur;
+       guint total;
+       gboolean done;
+} uncached_data;
+
+static void *imap_get_uncached_messages_thread(void *data)
 {
+       uncached_data *stuff = (uncached_data *)data;
+       IMAPSession *session = stuff->session;
+       FolderItem *item = stuff->item;
+       MsgNumberList *numlist = stuff->numlist;
+
        gchar *tmp;
        GSList *newlist = NULL;
        GSList *llast = NULL;
-       GString *str;
+       GString *str = NULL;
        MsgInfo *msginfo;
        GSList *seq_list, *cur;
        IMAPSet imapset;
+       
+       stuff->total = g_slist_length(numlist);
+       stuff->cur = 0;
 
-       g_return_val_if_fail(session != NULL, NULL);
-       g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(item->folder != NULL, NULL);
-       g_return_val_if_fail(FOLDER_CLASS(item->folder) == &imap_class, NULL);
+       if (session == NULL || item == NULL || item->folder == NULL
+           || FOLDER_CLASS(item->folder) != &imap_class) {
+               stuff->done = TRUE;
+               return NULL;
+       }
 
        seq_list = imap_get_seq_set_from_numlist(numlist);
        for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
                imapset = cur->data;
+               
+               if (!imapset || strlen(imapset) == 0)
+                       continue;
 
                if (imap_cmd_envelope(session, imapset)
                    != IMAP_SUCCESS) {
@@ -1852,9 +2058,10 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                str = g_string_new(NULL);
 
                for (;;) {
-                       if ((tmp = sock_getline(SESSION(session)->sock)) == NULL) {
+                       if ((tmp =sock_getline(SESSION(session)->sock)) == NULL) {
                                log_warning(_("error occurred while getting envelope.\n"));
                                g_string_free(str, TRUE);
+                               str = NULL;
                                break;
                        }
                        strretchomp(tmp);
@@ -1871,6 +2078,8 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                        log_print("IMAP4< %s\n", tmp);
                        g_string_assign(str, tmp);
                        g_free(tmp);
+                       
+                       stuff->cur++;
 
                        msginfo = imap_parse_envelope
                                (SESSION(session)->sock, item, str);
@@ -1893,14 +2102,80 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                                llast = llast->next;
                        }
                }
-
-               g_string_free(str, TRUE);
+               if (str)
+                       g_string_free(str, TRUE);
        }
        imap_seq_set_free(seq_list);
+       
+       session_set_access_time(SESSION(session));
+       stuff->done = TRUE;
 
        return newlist;
 }
 
+static GSList *imap_get_uncached_messages(IMAPSession *session,
+                                       FolderItem *item,
+                                       MsgNumberList *numlist)
+{
+       uncached_data *data = g_new0(uncached_data, 1);
+       GSList *result = NULL;
+       gint last_cur = 0;
+#ifdef USE_PTHREAD
+       pthread_t pt;
+#endif
+       data->done = FALSE;
+       data->session = session;
+       data->item = item;
+       data->numlist = numlist;
+       data->cur = 0;
+       data->total = 0;
+       
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return NULL;
+       }
+
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_get_uncached_messages_thread, data) != 0) {
+               result = (GSList *)imap_get_uncached_messages_thread(data);
+               g_free(data);
+               return result;
+       }
+       debug_print("+++waiting for imap_get_uncached_messages_thread...\n");
+       statusbar_print_all(_("IMAP4 Fetching uncached short headers..."));
+       while(!data->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+               if (data->total != 0 && last_cur != data->cur && data->cur % 10 == 0) {
+                       gchar buf[32];
+                       g_snprintf(buf, sizeof(buf), "%d / %d",
+                                  data->cur, data->total);
+                       gtk_progress_bar_set_text
+                               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), buf);
+                       gtk_progress_bar_set_fraction
+                       (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar),
+                        (gfloat)data->cur / (gfloat)data->total);
+                       last_cur = data->cur;
+               }
+       }
+       gtk_progress_bar_set_fraction
+               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), 0);
+       gtk_progress_bar_set_text
+               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), "");
+       statusbar_pop_all();
+
+       debug_print("---imap_get_uncached_messages_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, (void *)&result);
+#else
+       result = (GSList *)imap_get_uncached_messages_thread(data);
+#endif
+       g_free(data);
+       return result;
+}
+
 static void imap_delete_all_cached_messages(FolderItem *item)
 {
        gchar *dir;
@@ -1942,6 +2217,20 @@ static SockInfo *imap_open_tunnel(const gchar *server,
 #endif
 }
 
+void *imap_open_thread(void *data)
+{
+       SockInfo *sock = NULL;
+       thread_data *td = (thread_data *)data;
+       if ((sock = sock_connect(td->server, td->port)) == NULL) {
+               log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
+                           td->server, td->port);
+               td->done = TRUE;
+               return NULL;
+       }
+
+       td->done = TRUE;
+       return sock;
+}
 
 #if USE_OPENSSL
 static SockInfo *imap_open(const gchar *server, gushort port,
@@ -1950,22 +2239,59 @@ static SockInfo *imap_open(const gchar *server, gushort port,
 static SockInfo *imap_open(const gchar *server, gushort port)
 #endif
 {
-       SockInfo *sock;
+       thread_data *td = g_new0(thread_data, 1);
+#if USE_PTHREAD
+       pthread_t pt;
+#endif
+       SockInfo *sock = NULL;
 
-       if ((sock = sock_connect(server, port)) == NULL) {
-               log_warning(_("Can't connect to IMAP4 server: %s:%d\n"),
-                           server, port);
-               return NULL;
-       }
+#if USE_OPENSSL
+       td->ssl_type = ssl_type;
+#endif
+       td->server = g_strdup(server);
+       td->port = port;
+       td->done = FALSE;
+
+       statusbar_print_all(_("Connecting to IMAP4 server: %s..."), server);
+
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_open_thread, td) != 0) {
+               statusbar_pop_all();
+               sock = imap_open_thread(td);
+       } else {        
+               debug_print("+++waiting for imap_open_thread...\n");
+               while(!td->done) {
+                       /* don't let the interface freeze while waiting */
+                       sylpheed_do_idle();
+               }
 
+               /* get the thread's return value and clean its resources */
+               pthread_join(pt, (void *)&sock);
+       }
+#else
+       sock = imap_open_thread(td);
+#endif
 #if USE_OPENSSL
-       if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
+       if (sock && td->ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
                log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
-                           server, port);
+                           td->server, td->port);
                sock_close(sock);
-               return NULL;
+               sock = NULL;
+               td->done = TRUE;
        }
 #endif
+       g_free(td->server);
+       g_free(td);
+
+       debug_print("---imap_open_thread returned %p\n", sock);
+       statusbar_pop_all();
+
+       if(!sock && !prefs_common.no_recv_err_panel) {
+               alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
+                                server, port);
+       }
+
        return sock;
 }
 
@@ -2175,7 +2501,7 @@ static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
        /* read the next line if the current response buffer is empty */
        while (isspace(*(guchar *)cur_pos)) cur_pos++;
        while (*cur_pos == '\0') {
-               if ((nextline = sock_getline(sock)) == NULL)
+               if ((nextline = imap_getline(sock)) == NULL)
                        return cur_pos;
                g_string_assign(str, nextline);
                cur_pos = str->str;
@@ -2208,7 +2534,7 @@ static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
                cur_pos = str->str;
 
                do {
-                       if ((nextline = sock_getline(sock)) == NULL)
+                       if ((nextline = imap_getline(sock)) == NULL)
                                return cur_pos;
                        line_len += strlen(nextline);
                        g_string_append(str, nextline);
@@ -2251,8 +2577,10 @@ static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
        cur_pos = str->str;
 
        do {
-               if ((nextline = sock_getline(sock)) == NULL)
+               if ((nextline = sock_getline(sock)) == NULL) {
+                       *headers = NULL;
                        return cur_pos;
+               }
                block_len += strlen(nextline);
                g_string_append(str, nextline);
                cur_pos = str->str;
@@ -2261,7 +2589,7 @@ static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
                g_free(nextline);
        } while (block_len < len);
 
-       debug_print("IMAP4< [contents of BODY.PEEK[HEADER.FIELDS (...)]]\n");
+       debug_print("IMAP4< [contents of BODY.PEEK[HEADER_FIELDS (...)]\n");
 
        *headers = g_strndup(cur_pos, len);
        cur_pos += len;
@@ -2292,15 +2620,15 @@ static MsgFlags imap_parse_flags(const gchar *flag_str)
        while ((p = strchr(p, '\\')) != NULL) {
                p++;
 
-               if (g_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
+               if (g_ascii_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
                        MSG_SET_PERM_FLAGS(flags, MSG_NEW);
-               } else if (g_strncasecmp(p, "Seen", 4) == 0) {
+               } else if (g_ascii_strncasecmp(p, "Seen", 4) == 0) {
                        MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
-               } else if (g_strncasecmp(p, "Deleted", 7) == 0) {
+               } else if (g_ascii_strncasecmp(p, "Deleted", 7) == 0) {
                        MSG_SET_PERM_FLAGS(flags, MSG_DELETED);
-               } else if (g_strncasecmp(p, "Flagged", 7) == 0) {
+               } else if (g_ascii_strncasecmp(p, "Flagged", 7) == 0) {
                        MSG_SET_PERM_FLAGS(flags, MSG_MARKED);
-               } else if (g_strncasecmp(p, "Answered", 8) == 0) {
+               } else if (g_ascii_strncasecmp(p, "Answered", 8) == 0) {
                        MSG_SET_PERM_FLAGS(flags, MSG_REPLIED);
                }
        }
@@ -2387,7 +2715,6 @@ static MsgInfo *imap_parse_envelope(SockInfo *sock, FolderItem *item,
                                return NULL;
                        }
                        cur_pos++;
-
                        cur_pos = imap_get_header(sock, cur_pos, &headers,
                                                  line_str);
                        msginfo = procheader_parse_str(headers, flags, FALSE, FALSE);
@@ -2437,8 +2764,8 @@ static gint imap_set_message_flags(IMAPSession *session,
 {
        gchar *cmd;
        gchar *flag_str;
-       gint ok;
-       GSList *seq_list, *cur;
+       gint ok = 0;
+       GSList *seq_list;
        IMAPSet imapset;
 
        flag_str = imap_get_flag_str(flags);
@@ -2447,26 +2774,54 @@ static gint imap_set_message_flags(IMAPSession *session,
        g_free(flag_str);
 
        seq_list = imap_get_seq_set_from_numlist(numlist);
-       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
-               imapset = cur->data;
+       imapset = get_seq_set_from_seq_list(seq_list);
 
-               ok = imap_cmd_store(session, imapset, cmd);
-       }
+       ok = imap_cmd_store(session, imapset, cmd);
+       
+       g_free(imapset);
        imap_seq_set_free(seq_list);
        g_free(cmd);
 
        return ok;
 }
 
-static gint imap_select(IMAPSession *session, IMAPFolder *folder,
-                       const gchar *path,
-                       gint *exists, gint *recent, gint *unseen,
-                       guint32 *uid_validity)
+typedef struct _select_data {
+       IMAPSession *session;
+       gchar *real_path;
+       gint *exists;
+       gint *recent;
+       gint *unseen;
+       guint32 *uid_validity;
+       gboolean done;
+} select_data;
+
+static void *imap_select_thread(void *data)
+{
+       select_data *stuff = (select_data *)data;
+       IMAPSession *session = stuff->session;
+       gchar *real_path = stuff->real_path;
+       gint *exists = stuff->exists;
+       gint *recent = stuff->recent;
+       gint *unseen = stuff->unseen;
+       guint32 *uid_validity = stuff->uid_validity;
+       gint ok;
+       
+       ok = imap_cmd_select(session, real_path,
+                            exists, recent, unseen, uid_validity, TRUE);
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(ok);
+}
+               
+static gint imap_select(IMAPSession *session, IMAPFolder *folder,
+                       const gchar *path,
+                       gint *exists, gint *recent, gint *unseen,
+                       guint32 *uid_validity, gboolean block)
 {
        gchar *real_path;
        gint ok;
-       gint exists_, recent_, unseen_, uid_validity_;
-
+       gint exists_, recent_, unseen_;
+       guint32 uid_validity_;
+       
        if (!exists || !recent || !unseen || !uid_validity) {
                if (session->mbox && strcmp(session->mbox, path) == 0)
                        return IMAP_SUCCESS;
@@ -2480,8 +2835,45 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
        session->mbox = NULL;
 
        real_path = imap_get_real_path(folder, path);
+       
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (block == FALSE) {
+               select_data *data = g_new0(select_data, 1);
+               void *tmp;
+               pthread_t pt;
+               data->session = session;
+               data->real_path = real_path;
+               data->exists = exists;
+               data->recent = recent;
+               data->unseen = unseen;
+               data->uid_validity = uid_validity;
+               data->done = FALSE;
+               
+               if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                               imap_select_thread, data) != 0) {
+                       ok = GPOINTER_TO_INT(imap_select_thread(data));
+                       g_free(data);
+               } else {
+                       debug_print("+++waiting for imap_select_thread...\n");
+                       while(!data->done) {
+                               /* don't let the interface freeze while waiting */
+                               sylpheed_do_idle();
+                       }
+                       debug_print("---imap_select_thread done\n");
+
+                       /* get the thread's return value and clean its resources */
+                       pthread_join(pt, &tmp);
+                       ok = GPOINTER_TO_INT(tmp);
+                       g_free(data);
+               }
+       } else {
+               ok = imap_cmd_select(session, real_path,
+                            exists, recent, unseen, uid_validity, block);
+       }
+#else
        ok = imap_cmd_select(session, real_path,
-                            exists, recent, unseen, uid_validity);
+                            exists, recent, unseen, uid_validity, block);
+#endif
        if (ok != IMAP_SUCCESS)
                log_warning(_("can't select folder: %s\n"), real_path);
        else {
@@ -2499,7 +2891,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                        const gchar *path,
                        gint *messages, gint *recent,
                        guint32 *uid_next, guint32 *uid_validity,
-                       gint *unseen)
+                       gint *unseen, gboolean block)
 {
        gchar *real_path;
        gchar *real_path_;
@@ -2518,7 +2910,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                          "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
                          real_path_);
 
-       ok = imap_cmd_ok(session, argbuf);
+       ok = imap_cmd_ok_with_block(session, argbuf, block);
        if (ok != IMAP_SUCCESS || !argbuf) THROW(ok);
 
        str = search_array_str(argbuf, "STATUS");
@@ -2568,7 +2960,7 @@ static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
        gchar **p;
        
        for (p = session->capability; *p != NULL; ++p) {
-               if (!g_strcasecmp(*p, cap))
+               if (!g_ascii_strcasecmp(*p, cap))
                        return TRUE;
        }
 
@@ -2631,13 +3023,25 @@ static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
 static gint imap_cmd_login(IMAPSession *session,
                           const gchar *user, const gchar *pass)
 {
-       gchar *user_, *pass_;
+       gchar *ans;
        gint ok;
 
-       QUOTE_IF_REQUIRED(user_, user);
-       QUOTE_IF_REQUIRED(pass_, pass);
-       imap_gen_send(session, "LOGIN %s %s", user_, pass_);
-
+       imap_gen_send(session, "LOGIN {%d}\r\n%s {%d}\r\n%s", 
+                               strlen(user), user, 
+                               strlen(pass), pass);
+                               
+       ok = imap_gen_recv_with_block(session, &ans, TRUE);
+       if (ok != IMAP_SUCCESS || ans[0] != '+' || ans[1] != ' ') {
+               g_free(ans);
+               return IMAP_ERROR;
+       }
+       g_free(ans);
+       ok = imap_gen_recv_with_block(session, &ans, TRUE);
+       if (ok != IMAP_SUCCESS || ans[0] != '+' || ans[1] != ' ') {
+               g_free(ans);
+               return IMAP_ERROR;
+       }
+       g_free(ans);
        ok = imap_cmd_ok(session, NULL);
        if (ok != IMAP_SUCCESS)
                log_warning(_("IMAP4 login failed.\n"));
@@ -2657,11 +3061,13 @@ static gint imap_cmd_noop(IMAPSession *session)
        return imap_cmd_ok(session, NULL);
 }
 
+#if USE_OPENSSL
 static gint imap_cmd_starttls(IMAPSession *session)
 {
        imap_gen_send(session, "STARTTLS");
        return imap_cmd_ok(session, NULL);
 }
+#endif
 
 #define THROW(err) { ok = err; goto catch; }
 
@@ -2710,13 +3116,14 @@ static gint imap_cmd_list(IMAPSession *session, const gchar *ref,
 static gint imap_cmd_do_select(IMAPSession *session, const gchar *folder,
                               gboolean examine,
                               gint *exists, gint *recent, gint *unseen,
-                              guint32 *uid_validity)
+                              guint32 *uid_validity, gboolean block)
 {
        gint ok;
        gchar *resp_str;
        GPtrArray *argbuf;
        gchar *select_cmd;
        gchar *folder_;
+       unsigned int uid_validity_;
 
        *exists = *recent = *unseen = *uid_validity = 0;
        argbuf = g_ptr_array_new();
@@ -2729,7 +3136,7 @@ static gint imap_cmd_do_select(IMAPSession *session, const gchar *folder,
        QUOTE_IF_REQUIRED(folder_, folder);
        imap_gen_send(session, "%s %s", select_cmd, folder_);
 
-       if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW;
+       if ((ok = imap_cmd_ok_with_block(session, argbuf, block)) != IMAP_SUCCESS) THROW;
 
        resp_str = search_array_contain_str(argbuf, "EXISTS");
        if (resp_str) {
@@ -2749,11 +3156,12 @@ static gint imap_cmd_do_select(IMAPSession *session, const gchar *folder,
 
        resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
        if (resp_str) {
-               if (sscanf(resp_str, "OK [UIDVALIDITY %u] ", uid_validity)
+               if (sscanf(resp_str, "OK [UIDVALIDITY %u] ", &uid_validity_)
                    != 1) {
                        g_warning("imap_cmd_select(): invalid UIDVALIDITY line.\n");
                        THROW;
                }
+               *uid_validity = uid_validity_;
        }
 
        resp_str = search_array_contain_str(argbuf, "UNSEEN");
@@ -2773,18 +3181,18 @@ catch:
 
 static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
                            gint *exists, gint *recent, gint *unseen,
-                           guint32 *uid_validity)
+                           guint32 *uid_validity, gboolean block)
 {
        return imap_cmd_do_select(session, folder, FALSE,
-                                 exists, recent, unseen, uid_validity);
+                                 exists, recent, unseen, uid_validity, block);
 }
 
 static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
                             gint *exists, gint *recent, gint *unseen,
-                            guint32 *uid_validity)
+                            guint32 *uid_validity, gboolean block)
 {
        return imap_cmd_do_select(session, folder, TRUE,
-                                 exists, recent, unseen, uid_validity);
+                                 exists, recent, unseen, uid_validity, block);
 }
 
 #undef THROW
@@ -2822,7 +3230,7 @@ static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
 }
 
 static gint imap_cmd_search(IMAPSession *session, const gchar *criteria, 
-                           GSList **list)
+                           GSList **list, gboolean block)
 {
        gint ok;
        gchar *uidlist;
@@ -2836,7 +3244,7 @@ static gint imap_cmd_search(IMAPSession *session, const gchar *criteria,
        argbuf = g_ptr_array_new();
        imap_gen_send(session, "UID SEARCH %s", criteria);
 
-       ok = imap_cmd_ok(session, argbuf);
+       ok = imap_cmd_ok_with_block(session, argbuf, block);
        if (ok != IMAP_SUCCESS) {
                ptr_array_free_strings(argbuf);
                g_ptr_array_free(argbuf, TRUE);
@@ -2850,7 +3258,7 @@ static gint imap_cmd_search(IMAPSession *session, const gchar *criteria,
                for (p = strlist; *p != NULL; ++p) {
                        guint msgnum;
 
-                       if (sscanf(*p, "%d", &msgnum) == 1)
+                       if (sscanf(*p, "%u", &msgnum) == 1)
                                *list = g_slist_append(*list, GINT_TO_POINTER(msgnum));
                }
                g_strfreev(strlist);
@@ -2861,36 +3269,58 @@ static gint imap_cmd_search(IMAPSession *session, const gchar *criteria,
        return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_fetch(IMAPSession *session, guint32 uid, 
-                          const gchar *filename)
+typedef struct _fetch_data {
+       IMAPSession *session;
+       guint32 uid;
+       const gchar *filename;
+       gboolean headers;
+       gboolean body;
+       gboolean done;
+} fetch_data;
+
+static void *imap_cmd_fetch_thread(void *data)
 {
+       fetch_data *stuff = (fetch_data *)data;
+       IMAPSession *session = stuff->session;
+       guint32 uid = stuff->uid;
+       const gchar *filename = stuff->filename;
+       
        gint ok;
        gchar *buf = NULL;
        gchar *cur_pos;
        gchar size_str[32];
        glong size_num;
 
-       g_return_val_if_fail(filename != NULL, IMAP_ERROR);
-
-       imap_gen_send(session, "UID FETCH %d BODY.PEEK[]", uid);
-
-       while ((ok = imap_gen_recv(session, &buf)) == IMAP_SUCCESS) {
+       if (filename == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
+       }
+       
+       if (stuff->body)
+               imap_gen_send(session, "UID FETCH %d BODY.PEEK[]", uid);
+       else
+               imap_gen_send(session, "UID FETCH %d BODY.PEEK[HEADER]",
+                               uid);
+       while ((ok = imap_gen_recv_block(session, &buf)) == IMAP_SUCCESS) {
                if (buf[0] != '*' || buf[1] != ' ') {
                        g_free(buf);
-                       return IMAP_ERROR;
+                       stuff->done = TRUE;
+                       return GINT_TO_POINTER(IMAP_ERROR);
                }
                if (strstr(buf, "FETCH") != NULL) break;
                g_free(buf);
        }
        if (ok != IMAP_SUCCESS) {
                g_free(buf);
-               return ok;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(ok);
        }
 
 #define RETURN_ERROR_IF_FAIL(cond)     \
        if (!(cond)) {                  \
                g_free(buf);            \
-               return IMAP_ERROR;      \
+               stuff->done = TRUE;     \
+               return GINT_TO_POINTER(IMAP_ERROR);     \
        }
 
        cur_pos = strchr(buf, '{');
@@ -2907,23 +3337,73 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        g_free(buf);
 
        if (recv_bytes_write_to_file(SESSION(session)->sock,
-                                    size_num, filename) != 0)
-               return IMAP_ERROR;
-
-       if (imap_gen_recv(session, &buf) != IMAP_SUCCESS) {
+                                    size_num, filename) != 0) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
+       }
+       if (imap_gen_recv_block(session, &buf) != IMAP_SUCCESS) {
                g_free(buf);
-               return IMAP_ERROR;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
        }
 
        if (buf[0] == '\0' || buf[strlen(buf) - 1] != ')') {
                g_free(buf);
-               return IMAP_ERROR;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
        }
        g_free(buf);
 
-       ok = imap_cmd_ok(session, NULL);
+       ok = imap_cmd_ok_block(session, NULL);
 
-       return ok;
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(ok);
+}
+
+static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
+                               const gchar *filename, gboolean headers,
+                               gboolean body)
+{
+       fetch_data *data = g_new0(fetch_data, 1);
+       int result = 0;
+#ifdef USE_PTHREAD
+       void *tmp;
+       pthread_t pt;
+#endif
+       data->done = FALSE;
+       data->session = session;
+       data->uid = uid;
+       data->filename = filename;
+       data->headers = headers;
+       data->body = body;
+
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
+       }
+
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_cmd_fetch_thread, data) != 0) {
+               result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+               g_free(data);
+               return result;
+       }
+       debug_print("+++waiting for imap_cmd_fetch_thread...\n");
+       while(!data->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+       }
+       debug_print("---imap_cmd_fetch_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, &tmp);
+       result = GPOINTER_TO_INT(tmp);
+#else
+       result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+#endif
+       g_free(data);
+       return result;
 }
 
 static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
@@ -2934,7 +3414,7 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
        gint size;
        gchar *destfolder_;
        gchar *flag_str;
-       guint32 new_uid_;
+       unsigned int new_uid_;
        gchar *ret = NULL;
        gchar buf[BUFFSIZE];
        FILE *fp;
@@ -2963,7 +3443,7 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
        }
        g_free(ret);
 
-       log_print("IMAP4> %s\n", _("(sending file...)"));
+       log_print("IMAP4> %s\n", "(sending file...)");
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
@@ -3014,13 +3494,12 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 static MsgNumberList *imapset_to_numlist(IMAPSet imapset)
 {
        gchar **ranges, **range;
-       guint32 low, high;
+       unsigned int low, high;
        MsgNumberList *uids = NULL;
        
        ranges = g_strsplit(imapset, ",", 0);
        for (range = ranges; *range != NULL; range++) {
-               printf("%s\n", *range);
-               if(sscanf(*range, "%u:%u", &low, &high) == 1)
+               if (sscanf(*range, "%u:%u", &low, &high) == 1)
                        uids = g_slist_prepend(uids, GINT_TO_POINTER(low));
                else {
                        int i;
@@ -3094,37 +3573,12 @@ static gint imap_cmd_copy(IMAPSession *session, const gchar *seq_set,
 
 gint imap_cmd_envelope(IMAPSession *session, IMAPSet set)
 {
-       static GString *header_fields = NULL;
-
-       if (header_fields == NULL) {
-               const HeaderEntry *headers, *elem;
-
-               headers = procheader_get_headernames(FALSE);
-               header_fields = g_string_new("");
-
-               for (elem = headers; elem->name != NULL; ++elem) {
-                       gint namelen = strlen(elem->name);
-
-                       /* Header fields ending with space are not rfc822 headers */
-                       if (elem->name[namelen - 1] == ' ')
-                               continue;
-
-                       /* strip : at the of header field */
-                       if(elem->name[namelen - 1] == ':')
-                               namelen--;
-                       
-                       if (namelen <= 0)
-                               continue;
-
-                       g_string_sprintfa(header_fields, "%s%.*s",
-                                       header_fields->str[0] != '\0' ? " " : "",
-                                       namelen, elem->name);
-               }
-       }
+       static gchar *header_fields = 
+               "Date From To Cc Subject Message-ID References In-Reply-To" ;
 
        imap_gen_send
                (session, "UID FETCH %s (UID FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (%s)])",
-                set, header_fields->str);
+                set, header_fields);
 
        return IMAP_SUCCESS;
 }
@@ -3145,20 +3599,73 @@ static gint imap_cmd_store(IMAPSession *session, IMAPSet seq_set,
        return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_expunge(IMAPSession *session, IMAPSet seq_set)
+typedef struct _expunge_data {
+       IMAPSession *session;
+       IMAPSet seq_set;
+       gboolean done;
+} expunge_data;
+
+static void *imap_cmd_expunge_thread(void *data)
 {
+       expunge_data *stuff = (expunge_data *)data;
+       IMAPSession *session = stuff->session;
+       IMAPSet seq_set = stuff->seq_set;
+
        gint ok;
 
        if (seq_set && session->uidplus)
                imap_gen_send(session, "UID EXPUNGE %s", seq_set);
        else    
                imap_gen_send(session, "EXPUNGE");
-       if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS) {
+       if ((ok = imap_cmd_ok_with_block(session, NULL, TRUE)) != IMAP_SUCCESS) {
                log_warning(_("error while imap command: EXPUNGE\n"));
-               return ok;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(ok);
        }
 
-       return IMAP_SUCCESS;
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(IMAP_SUCCESS);
+}
+
+static gint imap_cmd_expunge(IMAPSession *session, IMAPSet seq_set)
+{
+       expunge_data *data = g_new0(expunge_data, 1);
+       int result;
+#ifdef USE_PTHREAD
+       void *tmp;
+       pthread_t pt;
+#endif
+       data->done = FALSE;
+       data->session = session;
+       data->seq_set = seq_set;
+
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
+       }
+
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_cmd_expunge_thread, data) != 0) {
+               result = GPOINTER_TO_INT(imap_cmd_expunge_thread(data));
+               g_free(data);
+               return result;
+       }
+       debug_print("+++waiting for imap_cmd_expunge_thread...\n");
+       while(!data->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+       }
+       debug_print("---imap_cmd_expunge_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, &tmp);
+       result = GPOINTER_TO_INT(tmp);
+#else
+       result = GPOINTER_TO_INT(imap_cmd_expunge_thread(data));
+#endif
+       g_free(data);
+       return result;
 }
 
 static gint imap_cmd_close(IMAPSession *session)
@@ -3172,19 +3679,19 @@ static gint imap_cmd_close(IMAPSession *session)
        return ok;
 }
 
-static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
+static gint imap_cmd_ok_with_block(IMAPSession *session, GPtrArray *argbuf, gboolean block)
 {
        gint ok = IMAP_SUCCESS;
        gchar *buf;
        gint cmd_num;
        gchar *data;
 
-       while ((ok = imap_gen_recv(session, &buf))
+       while ((ok = imap_gen_recv_with_block(session, &buf, block))
               == IMAP_SUCCESS) {
-               // make sure data is long enough for any substring of buf
+               /* make sure data is long enough for any substring of buf */
                data = alloca(strlen(buf) + 1);
 
-               // untagged line read
+               /* untagged line read */
                if (buf[0] == '*' && buf[1] == ' ') {
                        gint num;
                        if (argbuf)
@@ -3201,14 +3708,14 @@ static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
                                        session->folder_content_changed = TRUE;
                                }
                        }
-               // tagged line with correct tag and OK response found
+               /* tagged line with correct tag and OK response found */
                } else if ((sscanf(buf, "%d %s", &cmd_num, data) >= 2) &&
                           (cmd_num == session->cmd_count) &&
                           !strcmp(data, "OK")) {
                        if (argbuf)
                                g_ptr_array_add(argbuf, g_strdup(buf));
                        break;
-               // everything else
+               /* everything else */
                } else {
                        ok = IMAP_ERROR;
                        break;
@@ -3219,7 +3726,14 @@ static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
 
        return ok;
 }
-
+static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
+{
+       return imap_cmd_ok_with_block(session, argbuf, FALSE);
+}
+static gint imap_cmd_ok_block(IMAPSession *session, GPtrArray *argbuf)
+{
+       return imap_cmd_ok_with_block(session, argbuf, TRUE);
+}
 static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
 {
        gchar *buf;
@@ -3234,7 +3748,7 @@ static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
        session->cmd_count++;
 
        buf = g_strdup_printf("%d %s\r\n", session->cmd_count, tmp);
-       if (!strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
+       if (!g_ascii_strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
                *p = '\0';
                log_print("IMAP4> %d %s ********\n", session->cmd_count, tmp);
        } else
@@ -3245,19 +3759,33 @@ static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
        g_free(buf);
 }
 
-static gint imap_gen_recv(IMAPSession *session, gchar **ret)
+static gint imap_gen_recv_with_block(IMAPSession *session, gchar **ret, gboolean block)
 {
-       if ((*ret = sock_getline(SESSION(session)->sock)) == NULL)
-               return IMAP_SOCKET;
-
+       if (!block) {
+               if ((*ret = imap_getline(SESSION(session)->sock)) == NULL)
+                       return IMAP_SOCKET;
+       } else {
+               if ((*ret = sock_getline(SESSION(session)->sock)) == NULL)
+                       return IMAP_SOCKET;
+       }
        strretchomp(*ret);
 
        log_print("IMAP4< %s\n", *ret);
+       
+       session_set_access_time(SESSION(session));
 
        return IMAP_SUCCESS;
 }
 
+static gint imap_gen_recv_block(IMAPSession *session, gchar **ret)
+{
+       return imap_gen_recv_with_block(session, ret, TRUE);
+}
 
+static gint imap_gen_recv(IMAPSession *session, gchar **ret)
+{
+       return imap_gen_recv_with_block(session, ret, FALSE);
+}
 /* misc utility functions */
 
 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
@@ -3351,24 +3879,6 @@ static void imap_path_separator_subst(gchar *str, gchar separator)
 
 static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
 {
-#if !HAVE_ICONV
-       const gchar *from_p;
-       gchar *to, *to_p;
-
-       to = g_malloc(strlen(mutf7_str) + 1);
-       to_p = to;
-
-       for (from_p = mutf7_str; *from_p != '\0'; from_p++) {
-               if (*from_p == '&' && *(from_p + 1) == '-') {
-                       *to_p++ = '&';
-                       from_p++;
-               } else
-                       *to_p++ = *from_p;
-       }
-       *to_p = '\0';
-
-       return to;
-#else
        static iconv_t cd = (iconv_t)-1;
        static gboolean iconv_ok = TRUE;
        GString *norm_utf7;
@@ -3382,14 +3892,16 @@ static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
        if (!iconv_ok) return g_strdup(mutf7_str);
 
        if (cd == (iconv_t)-1) {
-               cd = iconv_open(conv_get_current_charset_str(), CS_UTF_8);
+               cd = iconv_open(CS_INTERNAL, CS_UTF_7);
                if (cd == (iconv_t)-1) {
-                       g_warning(_("iconv cannot convert UTF-7 to UTF-8\n"));
+                       g_warning("iconv cannot convert UTF-7 to %s\n",
+                                 CS_INTERNAL);
                        iconv_ok = FALSE;
                        return g_strdup(mutf7_str);
                }
        }
 
+       /* modified UTF-7 to normal UTF-7 conversion */
        norm_utf7 = g_string_new(NULL);
 
        for (p = mutf7_str; *p != '\0'; p++) {
@@ -3421,7 +3933,8 @@ static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
 
        if (iconv(cd, (ICONV_CONST gchar **)&norm_utf7_p, &norm_utf7_len,
                  &to_p, &to_len) == -1) {
-               g_warning(_("iconv cannot convert UTF-7 to UTF-8\n"));
+               g_warning(_("iconv cannot convert UTF-7 to %s\n"),
+                         conv_get_locale_charset_str());
                g_string_free(norm_utf7, TRUE);
                g_free(to_str);
                return g_strdup(mutf7_str);
@@ -3433,29 +3946,10 @@ static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
        *to_p = '\0';
 
        return to_str;
-#endif /* !HAVE_ICONV */
 }
 
 static gchar *imap_utf8_to_modified_utf7(const gchar *from)
 {
-#if !HAVE_ICONV
-       const gchar *from_p;
-       gchar *to, *to_p;
-
-       to = g_malloc(strlen(from) * 2 + 1);
-       to_p = to;
-
-       for (from_p = from; *from_p != '\0'; from_p++) {
-               if (*from_p == '&') {
-                       *to_p++ = '&';
-                       *to_p++ = '-';
-               } else
-                       *to_p++ = *from_p;
-       }
-       *to_p = '\0';
-
-       return to;
-#else
        static iconv_t cd = (iconv_t)-1;
        static gboolean iconv_ok = TRUE;
        gchar *norm_utf7, *norm_utf7_p;
@@ -3467,21 +3961,23 @@ static gchar *imap_utf8_to_modified_utf7(const gchar *from)
        if (!iconv_ok) return g_strdup(from);
 
        if (cd == (iconv_t)-1) {
-               cd = iconv_open("UTF-7", CS_UTF_8);
+               cd = iconv_open(CS_UTF_7, CS_INTERNAL);
                if (cd == (iconv_t)-1) {
-                       g_warning("iconv cannot convert UTF-8 to UTF-7\n");
+                       g_warning(_("iconv cannot convert %s to UTF-7\n"),
+                                 CS_INTERNAL);
                        iconv_ok = FALSE;
                        return g_strdup(from);
                }
        }
 
+       /* UTF-8 to normal UTF-7 conversion */
        Xstrdup_a(from_tmp, from, return g_strdup(from));
        from_len = strlen(from);
        norm_utf7_len = from_len * 5;
        Xalloca(norm_utf7, norm_utf7_len + 1, return g_strdup(from));
        norm_utf7_p = norm_utf7;
 
-#define IS_PRINT(ch) (isprint(ch) && isascii(ch))
+#define IS_PRINT(ch) (isprint(ch) && IS_ASCII(ch))
 
        while (from_len > 0) {
                if (*from_tmp == '+') {
@@ -3498,18 +3994,13 @@ static gchar *imap_utf8_to_modified_utf7(const gchar *from)
                        from_tmp++;
                        from_len--;
                } else {
-                       size_t mb_len = 0, conv_len = 0;
+                       size_t conv_len = 0;
 
                        /* unprintable char: convert to UTF-7 */
                        p = from_tmp;
                        while (!IS_PRINT(*(guchar *)p) && conv_len < from_len) {
-                               mb_len = mblen(p, MB_LEN_MAX);
-                               if (mb_len <= 0) {
-                                       g_warning("wrong multibyte sequence\n");
-                                       return g_strdup(from);
-                               }
-                               conv_len += mb_len;
-                               p += mb_len;
+                               conv_len += g_utf8_skip[*(guchar *)p];
+                               p += g_utf8_skip[*(guchar *)p];
                        }
 
                        from_len -= conv_len;
@@ -3563,7 +4054,6 @@ static gchar *imap_utf8_to_modified_utf7(const gchar *from)
        g_string_free(to_str, FALSE);
 
        return to;
-#endif /* !HAVE_ICONV */
 }
 
 static GSList *imap_get_seq_set_from_numlist(MsgNumberList *numlist)
@@ -3585,6 +4075,9 @@ static GSList *imap_get_seq_set_from_numlist(MsgNumberList *numlist)
        first = GPOINTER_TO_INT(sorted_list->data);
 
        for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
+               if (GPOINTER_TO_INT(cur->data) == 0)
+                       continue;
+
                last = GPOINTER_TO_INT(cur->data);
                if (cur->next)
                        next = GPOINTER_TO_INT(cur->next->data);
@@ -3595,9 +4088,9 @@ static GSList *imap_get_seq_set_from_numlist(MsgNumberList *numlist)
                        if (str->len > 0)
                                g_string_append_c(str, ',');
                        if (first == last)
-                               g_string_sprintfa(str, "%u", first);
+                               g_string_append_printf(str, "%u", first);
                        else
-                               g_string_sprintfa(str, "%u:%u", first, last);
+                               g_string_append_printf(str, "%u:%u", first, last);
 
                        first = next;
 
@@ -3673,29 +4166,46 @@ static gboolean imap_rename_folder_func(GNode *node, gpointer data)
        return FALSE;
 }
 
-static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
+typedef struct _get_list_uid_data {
+       Folder *folder;
+       IMAPFolderItem *item;
+       GSList **msgnum_list;
+       gboolean done;
+} get_list_uid_data;
+
+static void *get_list_of_uids_thread(void *data)
 {
+       get_list_uid_data *stuff = (get_list_uid_data *)data;
+       Folder *folder = stuff->folder;
+       IMAPFolderItem *item = stuff->item;
+       GSList **msgnum_list = stuff->msgnum_list;
        gint ok, nummsgs = 0, lastuid_old;
        IMAPSession *session;
        GSList *uidlist, *elem;
        gchar *cmd_buf;
 
        session = imap_session_get(folder);
-       g_return_val_if_fail(session != NULL, -1);
+       if (session == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
+       }
 
        ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
-               return -1;
+                        NULL, NULL, NULL, NULL, TRUE);
+       if (ok != IMAP_SUCCESS) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
+       }
 
        cmd_buf = g_strdup_printf("UID %d:*", item->lastuid + 1);
-       ok = imap_cmd_search(session, cmd_buf, &uidlist);
+       ok = imap_cmd_search(session, cmd_buf, &uidlist, TRUE);
        g_free(cmd_buf);
 
        if (ok == IMAP_SOCKET) {
                session_destroy((Session *)session);
                ((RemoteFolder *)folder)->session = NULL;
-               return -1;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
        }
 
        if (ok != IMAP_SUCCESS) {
@@ -3707,11 +4217,12 @@ static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgn
                cmd_buf = g_strdup_printf("UID FETCH %d:* (UID)", item->lastuid + 1);
                imap_gen_send(session, cmd_buf);
                g_free(cmd_buf);
-               ok = imap_cmd_ok(session, argbuf);
+               ok = imap_cmd_ok_block(session, argbuf);
                if (ok != IMAP_SUCCESS) {
                        ptr_array_free_strings(argbuf);
                        g_ptr_array_free(argbuf, TRUE);
-                       return -1;
+                       stuff->done = TRUE;
+                       return GINT_TO_POINTER(-1);
                }
        
                for(i = 0; i < argbuf->len; i++) {
@@ -3745,7 +4256,51 @@ static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgn
        }
        g_slist_free(uidlist);
 
-       return nummsgs;
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(nummsgs);
+}
+
+static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
+{
+       gint result;
+       get_list_uid_data *data = g_new0(get_list_uid_data, 1);
+#ifdef USE_PTHREAD
+       void *tmp;
+       pthread_t pt;
+#endif
+       data->done = FALSE;
+       data->folder = folder;
+       data->item = item;
+       data->msgnum_list = msgnum_list;
+
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
+       }
+
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       get_list_of_uids_thread, data) != 0) {
+               result = GPOINTER_TO_INT(get_list_of_uids_thread(data));
+               g_free(data);
+               return result;
+       }
+       debug_print("+++waiting for get_list_of_uids_thread...\n");
+       while(!data->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+       }
+       debug_print("---get_list_of_uids_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, &tmp);
+       result = GPOINTER_TO_INT(tmp);
+#else
+       result = GPOINTER_TO_INT(get_list_of_uids_thread(data));
+#endif
+       g_free(data);
+       return result;
+
 }
 
 gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
@@ -3753,7 +4308,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        IMAPSession *session;
        gint ok, nummsgs = 0, exists, recent, uid_val, uid_next, unseen;
-       GSList *uidlist;
+       GSList *uidlist = NULL;
        gchar *dir;
        gboolean selected_folder;
 
@@ -3777,7 +4332,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                *old_uids_valid = TRUE;
        } else {
                ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                                &exists, &recent, &uid_next, &uid_val, &unseen);
+                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
                if (ok != IMAP_SUCCESS)
                        return -1;
 
@@ -3873,7 +4428,8 @@ static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
 
        msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
        if (!msginfo) return NULL;
-
+       
+       msginfo->plaintext_file = g_strdup(file);
        msginfo->folder = item;
 
        return msginfo;
@@ -3892,8 +4448,9 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, NULL);
 
+       debug_print("IMAP getting msginfos\n");
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
-                        NULL, NULL, NULL, NULL);
+                        NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS)
                return NULL;
 
@@ -3910,7 +4467,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
                startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
 
                for (elem = sorted_list;; elem = g_slist_next(elem)) {
-                       guint num;
+                       guint num = 0;
 
                        if (elem)
                                num = GPOINTER_TO_INT(elem->data);
@@ -3993,11 +4550,11 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                        return TRUE;
        } else {
                ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                                &exists, &recent, &uid_next, &uid_val, &unseen);
+                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
                if (ok != IMAP_SUCCESS)
                        return FALSE;
 
-               if ((uid_next != item->uid_next) || (exists < item->item.total_msgs))
+               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs))
                        return TRUE;
        }
 
@@ -4010,7 +4567,8 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        IMAPFlags flags_set = 0, flags_unset = 0;
        gint ok = IMAP_SUCCESS;
        MsgNumberList numlist;
-       
+       hashtable_data *ht_data = NULL;
+
        g_return_if_fail(folder != NULL);
        g_return_if_fail(folder->klass == &imap_class);
        g_return_if_fail(item != NULL);
@@ -4020,11 +4578,11 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
 
        session = imap_session_get(folder);
        if (!session) return;
-
+       
        if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
-           NULL, NULL, NULL, NULL)) != IMAP_SUCCESS)
+           NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS)
                return;
-
+               
        if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
                flags_set |= IMAP_FLAG_FLAGGED;
        if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
@@ -4042,18 +4600,368 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
 
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(msginfo->msgnum);
+
+       if (IMAP_FOLDER_ITEM(item)->batching) {
+               /* instead of performing an UID STORE command for each message change,
+                * as a lot of them can change "together", we just fill in hashtables
+                * and defer the treatment so that we're able to send only one
+                * command.
+                */
+               debug_print("IMAP batch mode on, deferring flags change\n");
+               if (flags_set) {
+                       ht_data = g_hash_table_lookup(flags_set_table, GINT_TO_POINTER(flags_set));
+                       if (ht_data == NULL) {
+                               ht_data = g_new0(hashtable_data, 1);
+                               ht_data->session = session;
+                               g_hash_table_insert(flags_set_table, GINT_TO_POINTER(flags_set), ht_data);
+                       }
+                       if (!g_slist_find(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum)))
+                               ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));
+               } 
+               if (flags_unset) {
+                       ht_data = g_hash_table_lookup(flags_unset_table, GINT_TO_POINTER(flags_unset));
+                       if (ht_data == NULL) {
+                               ht_data = g_new0(hashtable_data, 1);
+                               ht_data->session = session;
+                               g_hash_table_insert(flags_unset_table, GINT_TO_POINTER(flags_unset), ht_data);
+                       }
+                       if (!g_slist_find(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum)))
+                               ht_data->msglist = g_slist_prepend(ht_data->msglist, GINT_TO_POINTER(msginfo->msgnum));         
+               }
+       } else {
+               debug_print("IMAP changing flags\n");
+               if (flags_set) {
+                       ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
+                       if (ok != IMAP_SUCCESS) return;
+               }
+
+               if (flags_unset) {
+                       ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
+                       if (ok != IMAP_SUCCESS) return;
+               }
+       }
+       msginfo->flags.perm_flags = newflags;
        
-       if (flags_set) {
-               ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
-               if (ok != IMAP_SUCCESS) return;
+       return;
+}
+
+static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
+{
+       gint ok;
+       IMAPSession *session;
+       gchar *dir;
+       MsgNumberList numlist;
+       
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
+       g_return_val_if_fail(item != NULL, -1);
+
+       session = imap_session_get(folder);
+       if (!session) return -1;
+
+       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+                        NULL, NULL, NULL, NULL, FALSE);
+       if (ok != IMAP_SUCCESS)
+               return ok;
+
+       numlist.next = NULL;
+       numlist.data = GINT_TO_POINTER(uid);
+       
+       ok = imap_set_message_flags
+               (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
+               &numlist, IMAP_FLAG_DELETED, TRUE);
+       if (ok != IMAP_SUCCESS) {
+               log_warning(_("can't set deleted flags: %d\n"), uid);
+               return ok;
        }
 
-       if (flags_unset) {
-               ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
-               if (ok != IMAP_SUCCESS) return;
+       if (!session->uidplus) {
+               ok = imap_cmd_expunge(session, NULL);
+       } else {
+               gchar *uidstr;
+
+               uidstr = g_strdup_printf("%u", uid);
+               ok = imap_cmd_expunge(session, uidstr);
+               g_free(uidstr);
+       }
+       if (ok != IMAP_SUCCESS) {
+               log_warning(_("can't expunge\n"));
+               return ok;
        }
 
-       msginfo->flags.perm_flags = newflags;
+       IMAP_FOLDER_ITEM(item)->uid_list = g_slist_remove(
+           IMAP_FOLDER_ITEM(item)->uid_list, numlist.data);
+       dir = folder_item_get_path(item);
+       if (is_dir_exist(dir))
+               remove_numbered_files(dir, uid, uid);
+       g_free(dir);
 
-       return;
+       return IMAP_SUCCESS;
+}
+
+static gint compare_msginfo(gconstpointer a, gconstpointer b)
+{
+       return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
+}
+
+static guint gslist_find_next_num(MsgNumberList **list, guint num)
+{
+       GSList *elem;
+
+       g_return_val_if_fail(list != NULL, -1);
+
+       for (elem = *list; elem != NULL; elem = g_slist_next(elem))
+               if (GPOINTER_TO_INT(elem->data) >= num)
+                       break;
+       *list = elem;
+       return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
+}
+
+/*
+ * NEW and DELETED flags are not syncronized
+ * - The NEW/RECENT flags in IMAP folders can not really be directly
+ *   modified by Sylpheed
+ * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
+ *   meaning, in IMAP it always removes the messages from the FolderItem
+ *   in Sylpheed it can mean to move the message to trash
+ */
+
+typedef struct _get_flags_data {
+       Folder *folder;
+       FolderItem *item;
+       MsgInfoList *msginfo_list;
+       GRelation *msgflags;
+       gboolean done;
+} get_flags_data;
+
+static /*gint*/ void *imap_get_flags_thread(void *data)
+{
+       get_flags_data *stuff = (get_flags_data *)data;
+       Folder *folder = stuff->folder;
+       FolderItem *item = stuff->item;
+       MsgInfoList *msginfo_list = stuff->msginfo_list;
+       GRelation *msgflags = stuff->msgflags;
+       IMAPSession *session;
+       GSList *sorted_list;
+       GSList *unseen = NULL, *answered = NULL, *flagged = NULL;
+       GSList *p_unseen, *p_answered, *p_flagged;
+       GSList *elem;
+       GSList *seq_list, *cur;
+       gboolean reverse_seen = FALSE;
+       GString *cmd_buf;
+       gint ok;
+       gint exists_cnt, recent_cnt, unseen_cnt, uid_next;
+       guint32 uidvalidity;
+       gboolean selected_folder;
+       
+       if (folder == NULL || item == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
+       }
+       if (msginfo_list == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(0);
+       }
+
+       session = imap_session_get(folder);
+       if (session == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
+       }
+
+       selected_folder = (session->mbox != NULL) &&
+                         (!strcmp(session->mbox, item->path));
+
+       if (!selected_folder) {
+               ok = imap_status(session, IMAP_FOLDER(folder), item->path,
+                        &exists_cnt, &recent_cnt, &uid_next, &uidvalidity, &unseen_cnt, TRUE);
+               ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+                       NULL, NULL, NULL, NULL, TRUE);
+               if (ok != IMAP_SUCCESS) {
+                       stuff->done = TRUE;
+                       return GINT_TO_POINTER(-1);
+               }
+
+       } 
+
+       if (unseen_cnt > exists_cnt / 2)
+               reverse_seen = TRUE;
+
+       cmd_buf = g_string_new(NULL);
+
+       sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
+
+       seq_list = imap_get_seq_set_from_msglist(msginfo_list);
+
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               IMAPSet imapset = cur->data;
+
+               g_string_sprintf(cmd_buf, "%sSEEN UID %s", reverse_seen ? "" : "UN", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_unseen, TRUE);
+               unseen = g_slist_concat(unseen, p_unseen);
+
+               g_string_sprintf(cmd_buf, "ANSWERED UID %s", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_answered, TRUE);
+               answered = g_slist_concat(answered, p_answered);
+
+               g_string_sprintf(cmd_buf, "FLAGGED UID %s", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_flagged, TRUE);
+               flagged = g_slist_concat(flagged, p_flagged);
+       }
+
+       p_unseen = unseen;
+       p_answered = answered;
+       p_flagged = flagged;
+
+       for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
+               MsgInfo *msginfo;
+               MsgPermFlags flags;
+               gboolean wasnew;
+               
+               msginfo = (MsgInfo *) elem->data;
+               flags = msginfo->flags.perm_flags;
+               wasnew = (flags & MSG_NEW);
+               flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
+               if (reverse_seen)
+                       flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
+               if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
+                       if (!reverse_seen) {
+                               flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
+                       } else {
+                               flags &= ~(MSG_UNREAD | MSG_NEW);
+                       }
+               }
+               if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
+                       flags |= MSG_REPLIED;
+               if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
+                       flags |= MSG_MARKED;
+               g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
+       }
+
+       imap_seq_set_free(seq_list);
+       g_slist_free(flagged);
+       g_slist_free(answered);
+       g_slist_free(unseen);
+       g_slist_free(sorted_list);
+       g_string_free(cmd_buf, TRUE);
+
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(0);
+}
+
+static gint imap_get_flags(Folder *folder, FolderItem *item,
+                           MsgInfoList *msginfo_list, GRelation *msgflags)
+{
+       gint result;
+       get_flags_data *data = g_new0(get_flags_data, 1);
+#ifdef USE_PTHREAD
+       void *tmp;
+       pthread_t pt;
+#endif
+       data->done = FALSE;
+       data->folder = folder;
+       data->item = item;
+       data->msginfo_list = msginfo_list;
+       data->msgflags = msgflags;
+
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
+       }
+
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_get_flags_thread, data) != 0) {
+               result = GPOINTER_TO_INT(imap_get_flags_thread(data));
+               g_free(data);
+               return result;
+       }
+       debug_print("+++waiting for imap_get_flags_thread...\n");
+       while(!data->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+       }
+       debug_print("---imap_get_flags_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, &tmp);
+       result = GPOINTER_TO_INT(tmp);
+#else
+       result = GPOINTER_TO_INT(imap_get_flags_thread(data));
+#endif
+       g_free(data);
+       return result;
+
+}
+
+static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
+{
+       gboolean flags_set = GPOINTER_TO_INT(user_data);
+       gint flags_value = GPOINTER_TO_INT(key);
+       hashtable_data *data = (hashtable_data *)value;
+       
+       data->msglist = g_slist_reverse(data->msglist);
+       
+       debug_print("IMAP %ssetting flags to %d for %d messages\n",
+               flags_set?"":"un",
+               flags_value,
+               g_slist_length(data->msglist));
+       imap_set_message_flags(data->session, data->msglist, flags_value, flags_set);
+       
+       g_slist_free(data->msglist);    
+       g_free(data);
+       return TRUE;
+}
+
+static void process_hashtable(void)
+{
+       if (flags_set_table) {
+               g_hash_table_foreach_remove(flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
+               g_free(flags_set_table);
+               flags_set_table = NULL;
+       }
+       if (flags_unset_table) {
+               g_hash_table_foreach_remove(flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
+               g_free(flags_unset_table);
+               flags_unset_table = NULL;
+       }
+}
+
+static IMAPFolderItem *batching_item = NULL;
+
+static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
+{
+       IMAPFolderItem *item = (IMAPFolderItem *)_item;
+
+       g_return_if_fail(item != NULL);
+       
+       if (batch && batching_item != NULL) {
+               g_warning("already batching on %s\n", batching_item->item.path);
+               return;
+       }
+       
+       if (item->batching == batch)
+               return;
+       
+       item->batching = batch;
+       
+       batching_item = batch?item:NULL;
+       
+       if (batch) {
+               debug_print("IMAP switching to batch mode\n");
+               if (flags_set_table) {
+                       g_warning("flags_set_table non-null but we just entered batch mode!\n");
+                       flags_set_table = NULL;
+               }
+               if (flags_unset_table) {
+                       g_warning("flags_unset_table non-null but we just entered batch mode!\n");
+                       flags_unset_table = NULL;
+               }
+               flags_set_table = g_hash_table_new(NULL, g_direct_equal);
+               flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
+       } else {
+               debug_print("IMAP switching away from batch mode\n");
+               /* process stuff */
+               process_hashtable();
+       }
 }