2005-07-04 [colin] 1.9.12cvs19
[claws.git] / src / imap.c
index 562c906d7bbd2083c339e02e97b2ad81ea3fa799..e443ee1ae992bbe2473763ca8aa7fe9d3126bf34 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 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>
@@ -31,6 +32,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <time.h>
+#include <errno.h>
 #if HAVE_ICONV
 #  include <iconv.h>
 #endif
 #if USE_OPENSSL
 #  include "ssl.h"
 #endif
+
 #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 "procheader.h"
 #include "prefs_account.h"
 #include "codeconv.h"
+#include "md5.h"
+#include "base64.h"
 #include "utils.h"
+#include "prefs_common.h"
 #include "inputdialog.h"
 #include "log.h"
+#include "remotefolder.h"
+#include "alertpanel.h"
+#include "sylpheed.h"
+#include "statusbar.h"
+#include "msgcache.h"
+#include "imap-thread.h"
+
+#ifdef USE_PTHREAD
+#include <pthread.h>
+static pthread_mutex_t imap_mutex;
+static const char *mutex_hold = NULL;
+
+#ifndef __GNUC__
+#define __FUNCTION__ __FILE__
+#endif 
+
+#if 0
+#define MUTEX_TRYLOCK_OR_RETURN() {                                    \
+       debug_print("%s: locking mutex\n", __FUNCTION__);               \
+       if (pthread_mutex_trylock(&imap_mutex) == EBUSY) {              \
+               g_warning("can't lock mutex (held by %s)\n",            \
+                               mutex_hold ? mutex_hold:"(nil)");       \
+               return;                                                 \
+       }                                                               \
+       mutex_hold = __FUNCTION__;                                      \
+}
+
+#define MUTEX_TRYLOCK_OR_RETURN_VAL(retval) {                          \
+       debug_print("%s: locking mutex\n", __FUNCTION__);               \
+       if (pthread_mutex_trylock(&imap_mutex) == EBUSY) {              \
+               g_warning("can't lock mutex (held by %s)\n",            \
+                               mutex_hold ? mutex_hold:"(nil)");       \
+               return retval;                                          \
+       }                                                               \
+       mutex_hold = __FUNCTION__;                                      \
+}
+
+#define MUTEX_UNLOCK() {                                               \
+       debug_print("%s: unlocking mutex\n", __FUNCTION__);\
+       pthread_mutex_unlock(&imap_mutex);                              \
+       mutex_hold = NULL;                                              \
+}
+
+#else
+#define MUTEX_TRYLOCK_OR_RETURN() do {} while(0)
+#define MUTEX_TRYLOCK_OR_RETURN_VAL(retval) do {} while(0)
+#define MUTEX_UNLOCK() do {} while(0)
+#endif
+#endif
+
+#define MUTEX_TRYLOCK_OR_RETURN() do {} while(0)
+#define MUTEX_TRYLOCK_OR_RETURN_VAL(retval) do {} while(0)
+#define MUTEX_UNLOCK() do {} while(0)
 
 typedef struct _IMAPFolder     IMAPFolder;
 typedef struct _IMAPSession    IMAPSession;
@@ -61,6 +119,7 @@ typedef struct _IMAPFolderItem       IMAPFolderItem;
 #include "prefs_account.h"
 
 #define IMAP_FOLDER(obj)       ((IMAPFolder *)obj)
+#define IMAP_FOLDER_ITEM(obj)  ((IMAPFolderItem *)obj)
 #define IMAP_SESSION(obj)      ((IMAPSession *)obj)
 
 struct _IMAPFolder
@@ -77,13 +136,18 @@ struct _IMAPSession
 {
        Session session;
 
+       gboolean authenticated;
+
        gchar **capability;
+       gboolean uidplus;
+
        gchar *mbox;
-       time_t last_access_time;
-       gboolean authenticated;
        guint cmd_count;
+
+       /* CLAWS */
        gboolean folder_content_changed;
        guint exists;
+       Folder * folder;
 };
 
 struct _IMAPNameSpace
@@ -101,7 +165,6 @@ struct _IMAPNameSpace
 #define IMAP_ERROR     7
 
 #define IMAPBUFSIZE    8192
-#define IMAPCMDLIMIT   1000
 
 typedef enum
 {
@@ -124,22 +187,7 @@ typedef enum
 #define IMAPS_PORT     993
 #endif
 
-#define QUOTE_IF_REQUIRED(out, str)                            \
-{                                                              \
-       if (*str != '"' && strpbrk(str, " \t(){}%*") != NULL) { \
-               gchar *__tmp;                                   \
-               gint len;                                       \
-                                                               \
-               len = strlen(str) + 3;                          \
-               Xalloca(__tmp, len, return IMAP_ERROR);         \
-               g_snprintf(__tmp, len, "\"%s\"", str);          \
-               out = __tmp;                                    \
-       } else {                                                \
-               Xstrdup_a(out, str, return IMAP_ERROR);         \
-       }                                                       \
-}
-
-typedef gchar * IMAPSet;
+#define IMAP_CMD_LIMIT 1000
 
 struct _IMAPFolderItem
 {
@@ -148,99 +196,110 @@ struct _IMAPFolderItem
        guint lastuid;
        guint uid_next;
        GSList *uid_list;
+       gboolean batching;
 };
 
-static Folder *imap_folder_new(const gchar * name, const gchar * path);
-static void imap_folder_destroy(Folder * folder);
-
-static IMAPSession *imap_session_new(const PrefsAccount * account);
-static void imap_session_authenticate(IMAPSession * session,
-                                     const PrefsAccount * account);
-static void imap_session_destroy(Session * session);
-
-static gchar *imap_fetch_msg(Folder * folder, FolderItem * item, gint uid);
-static gint imap_add_msg(Folder * folder,
-                        FolderItem * dest,
-                        const gchar * file, gboolean remove_source);
-
-static gint imap_copy_msg(Folder * folder,
-                         FolderItem * dest, MsgInfo * msginfo);
-
-static gint imap_remove_msg(Folder * folder, FolderItem * item, gint uid);
-static gint imap_remove_all_msg(Folder * folder, FolderItem * item);
-
-static gboolean imap_is_msg_changed(Folder * folder,
-                                   FolderItem * item, MsgInfo * msginfo);
-
-static void imap_scan_tree(Folder * folder);
-
-static gint imap_create_tree(Folder * folder);
-
-static FolderItem *imap_create_folder(Folder * folder,
-                                     FolderItem * parent,
-                                     const gchar * name);
-static gint imap_rename_folder(Folder * folder,
-                              FolderItem * item, const gchar * name);
-static gint imap_remove_folder(Folder * folder, FolderItem * item);
-
-
 static void imap_folder_init           (Folder         *folder,
                                         const gchar    *name,
                                         const gchar    *path);
 
+static Folder  *imap_folder_new        (const gchar    *name,
+                                        const gchar    *path);
+static void     imap_folder_destroy    (Folder         *folder);
+
+static IMAPSession *imap_session_new   (Folder         *folder,
+                                        const PrefsAccount     *account);
+static void    imap_session_authenticate(IMAPSession           *session,
+                                         const PrefsAccount    *account);
+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, 
+                                        MsgFlags       *flags);
+static gint    imap_add_msgs           (Folder         *folder, 
+                                        FolderItem     *dest,
+                                        GSList         *file_list,
+                                        GRelation      *relation);
+
+static gint    imap_copy_msg           (Folder         *folder,
+                                        FolderItem     *dest, 
+                                        MsgInfo        *msginfo);
+static gint    imap_copy_msgs          (Folder         *folder, 
+                                        FolderItem     *dest, 
+                                        MsgInfoList    *msglist, 
+                                        GRelation      *relation);
+
+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);
+
+static gboolean imap_is_msg_changed    (Folder         *folder,
+                                        FolderItem     *item, 
+                                        MsgInfo        *msginfo);
+
+static gint    imap_close              (Folder         *folder, 
+                                        FolderItem     *item);
+
+static gint    imap_scan_tree          (Folder         *folder);
+
+static gint    imap_create_tree        (Folder         *folder);
+
+static FolderItem *imap_create_folder  (Folder         *folder,
+                                        FolderItem     *parent,
+                                        const gchar    *name);
+static gint    imap_rename_folder      (Folder         *folder,
+                                        FolderItem     *item, 
+                                        const gchar    *name);
+static gint    imap_remove_folder      (Folder         *folder, 
+                                        FolderItem     *item);
+
 static FolderItem *imap_folder_item_new        (Folder         *folder);
 static void imap_folder_item_destroy   (Folder         *folder,
                                         FolderItem     *item);
 
 static IMAPSession *imap_session_get   (Folder         *folder);
 
+static gint imap_auth                  (IMAPSession    *session,
+                                        const gchar    *user,
+                                        const gchar    *pass,
+                                        IMAPAuthType    type);
+
 static gint imap_scan_tree_recursive   (IMAPSession    *session,
                                         FolderItem     *item);
-static GSList *imap_parse_list         (IMAPFolder     *folder,
-                                        IMAPSession    *session,
-                                        const gchar    *real_path,
-                                        gchar          *separator);
 
-static void imap_create_missing_folders        (Folder                 *folder);
+static void imap_create_missing_folders        (Folder         *folder);
 static FolderItem *imap_create_special_folder
                                        (Folder                 *folder,
                                         SpecialFolderItemType   stype,
                                         const gchar            *name);
 
-static gint imap_do_copy               (Folder         *folder,
+static gint imap_do_copy_msgs          (Folder         *folder,
                                         FolderItem     *dest,
-                                        MsgInfo        *msginfo,
-                                        gboolean        remove_source);
+                                        MsgInfoList    *msglist,
+                                        GRelation      *relation);
 
 static void imap_delete_all_cached_messages    (FolderItem     *item);
-
-#if USE_OPENSSL
-static SockInfo *imap_open             (const gchar    *server,
-                                        gushort         port,
-                                        SSLType         ssl_type);
-#else
-static SockInfo *imap_open             (const gchar    *server,
-                                        gushort         port);
-#endif
-
-#if USE_OPENSSL
-static SockInfo *imap_open_tunnel(const gchar *server,
-                                 const gchar *tunnelcmd,
-                                 SSLType ssl_type);
-#else
-static SockInfo *imap_open_tunnel(const gchar *server,
-                                 const gchar *tunnelcmd);
-#endif
-
-#if USE_OPENSSL
-static SockInfo *imap_init_sock(SockInfo *sock, SSLType        ssl_type);
-#else
-static SockInfo *imap_init_sock(SockInfo *sock);
-#endif
-
+static void imap_set_batch             (Folder         *folder,
+                                        FolderItem     *item,
+                                        gboolean        batch);
 static gint imap_set_message_flags     (IMAPSession    *session,
                                         MsgNumberList  *numlist,
-                                        IMAPFlags       flag,
+                                        IMAPFlags       flags,
                                         gboolean        is_set);
 static gint imap_select                        (IMAPSession    *session,
                                         IMAPFolder     *folder,
@@ -248,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,
@@ -256,12 +316,9 @@ 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);
-static void imap_get_namespace_by_list         (IMAPSession    *session,
-                                                IMAPFolder     *folder);
 static IMAPNameSpace *imap_find_namespace      (IMAPFolder     *folder,
                                                 const gchar    *path);
 static gchar imap_get_path_separator           (IMAPFolder     *folder,
@@ -269,169 +326,173 @@ static gchar imap_get_path_separator            (IMAPFolder     *folder,
 static gchar *imap_get_real_path               (IMAPFolder     *folder,
                                                 const gchar    *path);
 
-static gchar *imap_parse_atom          (SockInfo       *sock,
-                                        gchar          *src,
-                                        gchar          *dest,
-                                        gint            dest_len,
-                                        GString        *str);
-static MsgFlags imap_parse_flags       (const gchar    *flag_str);
-static MsgInfo *imap_parse_envelope    (SockInfo       *sock,
-                                        FolderItem     *item,
-                                        GString        *line_str);
-static gint imap_greeting              (IMAPSession    *session);
-static gboolean imap_has_capability    (IMAPSession    *session,
-                                        const gchar    *cap);
-void imap_free_capabilities            (IMAPSession    *session);
-static const IMAPSet numberlist_to_imapset
-                                       (MsgNumberList *list);
+static void imap_free_capabilities     (IMAPSession    *session);
 
 /* low-level IMAP4rev1 commands */
-static gint imap_cmd_login     (IMAPSession    *sock,
+static gint imap_cmd_login     (IMAPSession    *session,
                                 const gchar    *user,
                                 const gchar    *pass);
-static gint imap_cmd_logout    (IMAPSession    *sock);
-static gint imap_cmd_noop      (IMAPSession    *sock);
-static gint imap_cmd_starttls  (IMAPSession    *sock);
-static gint imap_cmd_namespace (IMAPSession    *sock,
-                                gchar         **ns_str);
-static gint imap_cmd_list      (IMAPSession    *session,
-                                const gchar    *ref,
-                                const gchar    *mailbox,
-                                GPtrArray      *argbuf);
-static gint imap_cmd_do_select (IMAPSession    *sock,
-                                const gchar    *folder,
-                                gboolean        examine,
-                                gint           *exists,
-                                gint           *recent,
-                                gint           *unseen,
-                                guint32        *uid_validity);
-static gint imap_cmd_select    (IMAPSession    *sock,
+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_select    (IMAPSession    *session,
                                 const gchar    *folder,
                                 gint           *exists,
                                 gint           *recent,
                                 gint           *unseen,
-                                guint32        *uid_validity);
-static gint imap_cmd_examine   (IMAPSession    *sock,
+                                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,
                                 const gchar    *oldfolder,
                                 const gchar    *newfolder);
-static gint imap_cmd_delete    (IMAPSession    *sock,
+static gint imap_cmd_delete    (IMAPSession    *session,
                                 const gchar    *folder);
-static gint imap_cmd_envelope  (IMAPSession    *sock,
-                                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,
-                                gint32         *newuid);
-static gint imap_cmd_copy      (IMAPSession    *session,
-                                gint32         msgnum,
-                                const gchar    *destfolder,
-                                gint32         *new_uid);
-static gint imap_cmd_store     (IMAPSession    *sock,
-                                IMAPSet         set,
-                                gchar          *sub_cmd);
-static gint imap_cmd_expunge   (IMAPSession    *sock);
-
-static gint imap_cmd_ok                (IMAPSession    *session,
-                                GPtrArray      *argbuf);
-static void imap_gen_send      (IMAPSession    *sock,
-                                const gchar    *format, ...);
-static gint imap_gen_recv      (IMAPSession    *sock,
-                                gchar         **buf);
-
-/* misc utility functions */
-static gchar *strchr_cpy                       (const gchar    *src,
-                                                gchar           ch,
-                                                gchar          *dest,
-                                                gint            len);
-static gchar *get_quoted                       (const gchar    *src,
-                                                gchar           ch,
-                                                gchar          *dest,
-                                                gint            len);
-static gchar *search_array_contain_str         (GPtrArray      *array,
-                                                const gchar    *str);
-static gchar *search_array_str                 (GPtrArray      *array,
-                                                const gchar    *str);
+                                IMAPFlags       flags,
+                                guint32        *new_uid);
+static gint imap_cmd_copy       (IMAPSession *session,
+                                struct mailimap_set * set,
+                                const gchar *destfolder,
+                                GRelation *uid_mapping);
+static gint imap_cmd_store     (IMAPSession    *session,
+                                struct mailimap_set * set,
+                                IMAPFlags flags,
+                                int do_add);
+static gint imap_cmd_expunge   (IMAPSession    *session);
+
 static void imap_path_separator_subst          (gchar          *str,
                                                 gchar           separator);
 
-static gchar *imap_modified_utf7_to_locale     (const gchar    *mutf7_str);
-static gchar *imap_locale_to_modified_utf7     (const gchar    *from);
+static gchar *imap_utf8_to_modified_utf7       (const gchar    *from);
+static gchar *imap_modified_utf7_to_utf8       (const gchar    *mutf7_str);
 
 static gboolean imap_rename_folder_func                (GNode          *node,
                                                 gpointer        data);
 static gint imap_get_num_list                  (Folder         *folder,
                                                 FolderItem     *item,
-                                                GSList        **list);
+                                                GSList        **list,
+                                                gboolean       *old_uids_valid);
 static GSList *imap_get_msginfos               (Folder         *folder,
                                                 FolderItem     *item,
                                                 GSList         *msgnum_list);
 static MsgInfo *imap_get_msginfo               (Folder         *folder,
                                                 FolderItem     *item,
                                                 gint            num);
-static gboolean imap_check_msgnum_validity     (Folder         *folder,
+static gboolean imap_scan_required             (Folder         *folder,
                                                 FolderItem     *item);
 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);
 
-FolderClass imap_class =
-{
-       F_IMAP,
-       "imap",
-       "IMAP4",
-
-       /* Folder functions */
-       imap_folder_new,
-       imap_folder_destroy,
-       imap_scan_tree,
-       imap_create_tree,
-
-       /* FolderItem functions */
-       imap_folder_item_new,
-       imap_folder_item_destroy,
-       imap_item_get_path,
-       imap_create_folder,
-       imap_rename_folder,
-       imap_remove_folder,
-       imap_get_num_list,
-       NULL,
-       NULL,
-       NULL,
-       imap_check_msgnum_validity,
-
-       /* Message functions */
-       imap_get_msginfo,
-       imap_get_msginfos,
-       imap_fetch_msg,
-       imap_add_msg,
-       imap_copy_msg,
-       imap_remove_msg,
-       imap_remove_all_msg,
-       imap_is_msg_changed,
-       imap_change_flags,
-};
+
+/* data types conversion libetpan <-> sylpheed */
+static GSList * imap_list_from_lep(IMAPFolder * folder,
+                                  clist * list, const gchar * real_path);
+static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist);
+static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist);
+static GSList * imap_uid_list_from_lep(clist * list);
+static GSList * imap_uid_list_from_lep_tab(carray * list);
+static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
+                                      FolderItem *item);
+static void imap_lep_set_free(GSList *seq_list);
+static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags);
+
+
+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;
+
+typedef struct _thread_data {
+       gchar *server;
+       gushort port;
+       gboolean done;
+       SockInfo *sock;
+#ifdef USE_OPENSSL
+       SSLType ssl_type;
+#endif
+} thread_data;
 
 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;
+#ifdef USE_PTREAD
+               pthread_mutex_init(&imap_mutex, NULL);
+#endif
+       }
+       
        return &imap_class;
 }
 
-Folder *imap_folder_new(const gchar *name, const gchar *path)
+static Folder *imap_folder_new(const gchar *name, const gchar *path)
 {
        Folder *folder;
 
@@ -442,10 +503,11 @@ Folder *imap_folder_new(const gchar *name, const gchar *path)
        return folder;
 }
 
-void imap_folder_destroy(Folder *folder)
+static void imap_folder_destroy(Folder *folder)
 {
        gchar *dir;
 
+       imap_done(folder);
        dir = imap_folder_get_path(folder);
        if (is_dir_exist(dir))
                remove_dir_recursive(dir);
@@ -503,54 +565,57 @@ static void imap_reset_uid_lists(Folder *folder)
        g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
 }
 
+static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
+                     IMAPAuthType type)
+{
+       gint ok;
+
+       ok = imap_cmd_login(session, user, pass);
+       
+       if (ok == IMAP_SUCCESS)
+               session->authenticated = TRUE;
+
+       return ok;
+}
+
 static IMAPSession *imap_session_get(Folder *folder)
 {
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
        IMAPSession *session = NULL;
-       gushort port;
 
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
-
-#if USE_OPENSSL
-       port = folder->account->set_imapport ? folder->account->imapport
-               : folder->account->ssl_imap == SSL_TUNNEL
-               ? IMAPS_PORT : IMAP4_PORT;
-#else
-       port = folder->account->set_imapport ? folder->account->imapport
-               : IMAP4_PORT;
-#endif
+       
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               return NULL;
+       }
 
        /* Make sure we have a session */
        if (rfolder->session != NULL) {
                session = IMAP_SESSION(rfolder->session);
        } else {
                imap_reset_uid_lists(folder);
-               session = imap_session_new(folder->account);
+               session = imap_session_new(folder, folder->account);
        }
        if(session == NULL)
                return NULL;
 
-       if (SESSION(session)->sock->state == CONN_DISCONNECTED) {
-               debug_print("IMAP server disconnected\n");
-               session_destroy(SESSION(session));
-               imap_reset_uid_lists(folder);
-               session = imap_session_new(folder->account);
-       }
-
        /* Make sure session is authenticated */
        if (!IMAP_SESSION(session)->authenticated)
                imap_session_authenticate(IMAP_SESSION(session), folder->account);
+       
        if (!IMAP_SESSION(session)->authenticated) {
                session_destroy(SESSION(session));
                rfolder->session = NULL;
                return NULL;
        }
 
+#if 0
        /* Make sure we have parsed the IMAP namespace */
        imap_parse_namespace(IMAP_SESSION(session),
                             IMAP_FOLDER(folder));
+#endif
 
        /* I think the point of this code is to avoid sending a
         * keepalive if we've used the session recently and therefore
@@ -560,20 +625,23 @@ 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:%d failed"),
-                                           folder->account->recv_server, port);
+                               log_warning(_("Connecting to %s failed"),
+                                           folder->account->recv_server);
                                session_destroy(SESSION(session));
                                session = NULL;
                        } else {
-                               log_warning(_("IMAP4 connection to %s:%d has been"
-                                             " disconnected. Reconnecting...\n"),
-                                           folder->account->recv_server, port);
+                               log_warning(_("IMAP4 connection to %s has been"
+                                           " 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
                                   a new session, because of rfolder->session == NULL
@@ -581,23 +649,24 @@ 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);
 }
 
-IMAPSession *imap_session_new(const PrefsAccount *account)
+static IMAPSession *imap_session_new(Folder * folder,
+                                    const PrefsAccount *account)
 {
        IMAPSession *session;
-       SockInfo *imap_sock;
        gushort port;
-
+       int r;
+       int authenticated;
+       
 #ifdef USE_OPENSSL
        /* FIXME: IMAP over SSL only... */ 
        SSLType ssl_type;
@@ -610,54 +679,63 @@ IMAPSession *imap_session_new(const PrefsAccount *account)
                : IMAP4_PORT;
 #endif
 
+       imap_init(folder);
+       statusbar_print_all(_("Connecting to IMAP4 server: %s..."), folder->account->recv_server);
        if (account->set_tunnelcmd) {
-               log_message(_("creating tunneled IMAP4 connection\n"));
-#if USE_OPENSSL
-               if ((imap_sock = imap_open_tunnel(account->recv_server, 
-                                                 account->tunnelcmd,
-                                                 ssl_type)) == NULL)
-#else
-               if ((imap_sock = imap_open_tunnel(account->recv_server, 
-                                                 account->tunnelcmd)) == NULL)
+               r = imap_threaded_connect_cmd(folder,
+                                             account->tunnelcmd,
+                                             account->recv_server,
+                                             port);
+       }
+       else {
+#ifdef USE_OPENSSL
+               if (ssl_type == SSL_TUNNEL) {
+                       r = imap_threaded_connect_ssl(folder,
+                                                     account->recv_server,
+                                                     port);
+               }
+               else 
 #endif
-                       return NULL;
-       } else {
-               g_return_val_if_fail(account->recv_server != NULL, NULL);
-
-               log_message(_("creating IMAP4 connection to %s:%d ...\n"),
-                           account->recv_server, port);
+               {
+                       r = imap_threaded_connect(folder,
+                                                 account->recv_server,
+                                                 port);
+               }
+       }
+       
+       statusbar_pop_all();
+       if (r == MAILIMAP_NO_ERROR_AUTHENTICATED) {
+               authenticated = TRUE;
+       }
+       else if (r == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
+               authenticated = FALSE;
+       }
+       else {
+               if(!prefs_common.no_recv_err_panel) {
+                       alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
+                                        account->recv_server, port);
+               }
                
-#if USE_OPENSSL
-               if ((imap_sock = imap_open(account->recv_server, port,
-                                          ssl_type)) == NULL)
-#else
-               if ((imap_sock = imap_open(account->recv_server, port)) == NULL)
-#endif
-                       return NULL;
+               return NULL;
        }
-
+       
        session = g_new0(IMAPSession, 1);
        session_init(SESSION(session));
        SESSION(session)->type             = SESSION_IMAP;
        SESSION(session)->server           = g_strdup(account->recv_server);
-       SESSION(session)->sock             = imap_sock;
-
+       SESSION(session)->sock             = NULL;
+       
        SESSION(session)->destroy          = imap_session_destroy;
 
        session->capability = NULL;
-
+       
+       session->authenticated = authenticated;
        session->mbox = NULL;
-       session->authenticated = FALSE;
        session->cmd_count = 0;
-
-       /* Only need to log in if the connection was not PREAUTH */
-       if (imap_greeting(session) != IMAP_SUCCESS) {
-               session_destroy(SESSION(session));
-               return NULL;
-       }
+       session->folder = folder;
 
 #if USE_OPENSSL
-       if (account->ssl_imap == SSL_STARTTLS && imap_has_capability(session, "STARTTLS")) {
+       if (account->ssl_imap == SSL_STARTTLS) {
                gint ok;
 
                ok = imap_cmd_starttls(session);
@@ -666,28 +744,21 @@ IMAPSession *imap_session_new(const PrefsAccount *account)
                        session_destroy(SESSION(session));
                        return NULL;
                }
-               if (!ssl_init_socket_with_method(SESSION(session)->sock, SSL_METHOD_TLSv1)) {
-                       session_destroy(SESSION(session));
-                       return NULL;
-               }
 
                imap_free_capabilities(session);
                session->authenticated = FALSE;
+               session->uidplus = FALSE;
                session->cmd_count = 1;
-
-               if (imap_greeting(session) != IMAP_SUCCESS) {
-                       session_destroy(SESSION(session));
-                       return NULL;
-               }               
        }
 #endif
        log_message("IMAP connection is %s-authenticated\n",
                    (session->authenticated) ? "pre" : "un");
-
+       
        return session;
 }
 
-void imap_session_authenticate(IMAPSession *session, const PrefsAccount *account)
+static void imap_session_authenticate(IMAPSession *session, 
+                                     const PrefsAccount *account)
 {
        gchar *pass;
 
@@ -702,25 +773,59 @@ void imap_session_authenticate(IMAPSession *session, const PrefsAccount *account
                Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
                g_free(tmp_pass);
        }
-
-       if (imap_cmd_login(session, account->userid, pass) != IMAP_SUCCESS) {
+       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_threaded_disconnect(session->folder);
                imap_cmd_logout(session);
+               statusbar_pop_all();
+               
                return;
        }
-
+       statusbar_pop_all();
        session->authenticated = TRUE;
 }
 
-void imap_session_destroy(Session *session)
+static void imap_session_destroy(Session *session)
 {
+       imap_threaded_disconnect(IMAP_SESSION(session)->folder);
+       
+       imap_free_capabilities(IMAP_SESSION(session));
+       g_free(IMAP_SESSION(session)->mbox);
        sock_close(session->sock);
        session->sock = NULL;
+}
 
-       g_free(IMAP_SESSION(session)->mbox);
-       imap_free_capabilities(IMAP_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;
 }
 
-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)
 {
        gchar *path, *filename;
        IMAPSession *session;
@@ -729,15 +834,34 @@ 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);
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        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);
@@ -746,8 +870,9 @@ 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);
@@ -755,7 +880,7 @@ 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);
@@ -766,220 +891,346 @@ gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
        return filename;
 }
 
-gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
-                 gboolean remove_source)
+static gint imap_add_msg(Folder *folder, FolderItem *dest, 
+                        const gchar *file, MsgFlags *flags)
+{
+       gint ret;
+       GSList file_list;
+       MsgFileInfo fileinfo;
+
+       g_return_val_if_fail(file != NULL, -1);
+
+       fileinfo.msginfo = NULL;
+       fileinfo.file = (gchar *)file;
+       fileinfo.flags = flags;
+       file_list.data = &fileinfo;
+       file_list.next = NULL;
+
+       ret = imap_add_msgs(folder, dest, &file_list, NULL);
+       return ret;
+}
+
+static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
+                  GRelation *relation)
 {
        gchar *destdir;
        IMAPSession *session;
-       gint ok, newuid;
+       guint32 last_uid = 0;
+       GSList *cur;
+       MsgFileInfo *fileinfo;
+       gint ok;
+
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(file != NULL, -1);
+       g_return_val_if_fail(file_list != NULL, -1);
+       
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
 
        session = imap_session_get(folder);
-       if (!session) return -1;
-
-       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
-       ok = imap_cmd_append(session, destdir, file, &newuid);
-       g_free(destdir);
-
-       if (ok != IMAP_SUCCESS) {
-               g_warning("can't append message %s\n", file);
+       if (!session) {
+               MUTEX_UNLOCK();
                return -1;
        }
+       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+
+       for (cur = file_list; cur != NULL; cur = cur->next) {
+               IMAPFlags iflags = 0;
+               guint32 new_uid = 0;
+
+               fileinfo = (MsgFileInfo *)cur->data;
+
+               if (fileinfo->flags) {
+                       if (MSG_IS_MARKED(*fileinfo->flags))
+                               iflags |= IMAP_FLAG_FLAGGED;
+                       if (MSG_IS_REPLIED(*fileinfo->flags))
+                               iflags |= IMAP_FLAG_ANSWERED;
+                       if (!MSG_IS_UNREAD(*fileinfo->flags))
+                               iflags |= IMAP_FLAG_SEEN;
+               }
+
+               if (dest->stype == F_OUTBOX ||
+                   dest->stype == F_QUEUE  ||
+                   dest->stype == F_DRAFT  ||
+                   dest->stype == F_TRASH)
+                       iflags |= IMAP_FLAG_SEEN;
+
+               ok = imap_cmd_append(session, destdir, fileinfo->file, iflags, 
+                                    &new_uid);
+
+               if (ok != IMAP_SUCCESS) {
+                       g_warning("can't append message %s\n", fileinfo->file);
+                       g_free(destdir);
+                       MUTEX_UNLOCK();
+                       return -1;
+               }
 
-       if (remove_source) {
-               if (unlink(file) < 0)
-                       FILE_OP_ERROR(file, "unlink");
+               if (relation != NULL)
+                       g_relation_insert(relation, fileinfo->msginfo != NULL ? 
+                                         (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
+                                         GINT_TO_POINTER(dest->last_num + 1));
+               if (last_uid < new_uid)
+                       last_uid = new_uid;
        }
 
-       return newuid;
+       g_free(destdir);
+
+       MUTEX_UNLOCK();
+       return last_uid;
 }
 
-static gint imap_do_copy(Folder *folder, FolderItem *dest, MsgInfo *msginfo,
-                        gboolean remove_source)
+static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest, 
+                             MsgInfoList *msglist, GRelation *relation)
 {
+       FolderItem *src;
        gchar *destdir;
+       GSList *seq_list, *cur;
+       MsgInfo *msginfo;
        IMAPSession *session;
-       guint32 newuid = 0;
-       gint ok;
+       gint ok = IMAP_SUCCESS;
+       GRelation *uid_mapping;
+       gint last_num = 0;
        
        g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
        g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msginfo != NULL, -1);
+       g_return_val_if_fail(msglist != NULL, -1);
 
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
+       
        session = imap_session_get(folder);
-       if (!session) return -1;
+       
+       if (!session) {
+               MUTEX_UNLOCK();
+               return -1;
+       }
+       msginfo = (MsgInfo *)msglist->data;
 
-       if (msginfo->folder == dest) {
+       src = msginfo->folder;
+       if (src == dest) {
                g_warning("the src folder is identical to the dest.\n");
+               MUTEX_UNLOCK();
                return -1;
        }
 
-       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
-
-       /* ensure source folder selected */
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
-               return -1;
-        
-       if (remove_source)
-               debug_print("Moving message %s%c%d to %s ...\n",
-                           msginfo->folder->path, G_DIR_SEPARATOR,
-                           msginfo->msgnum, destdir);
-       else
-               debug_print("Copying message %s%c%d to %s ...\n",
-                           msginfo->folder->path, G_DIR_SEPARATOR,
-                           msginfo->msgnum, destdir);
+                        NULL, NULL, NULL, NULL, FALSE);
+       if (ok != IMAP_SUCCESS) {
+               MUTEX_UNLOCK();
+               return ok;
+       }
+
+       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+       seq_list = imap_get_lep_set_from_msglist(msglist);
+       uid_mapping = g_relation_new(2);
+       g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
+       
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               struct mailimap_set * seq_set;
+               
+               seq_set = cur->data;
+               
+               debug_print("Copying messages from %s to %s ...\n",
+                           src->path, destdir);
 
-       ok = imap_cmd_copy(session, msginfo->msgnum, destdir, &newuid);
+               ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping);
+               if (ok != IMAP_SUCCESS) {
+                       g_relation_destroy(uid_mapping);
+                       imap_lep_set_free(seq_list);
+                       return -1;
+               }
+       }
 
-       if (ok == IMAP_SUCCESS && remove_source) {
-               MsgNumberList numlist;
+       for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
+               MsgInfo *msginfo = (MsgInfo *)cur->data;
+               GTuples *tuples;
 
-               numlist.next = NULL;
-               numlist.data = GINT_TO_POINTER(msginfo->msgnum);
-               
-               imap_set_message_flags(session, &numlist,
-                                      IMAP_FLAG_DELETED, TRUE);
-               ok = imap_cmd_expunge(session);
+               tuples = g_relation_select(uid_mapping, 
+                                          GINT_TO_POINTER(msginfo->msgnum),
+                                          0);
+               if (tuples->len > 0) {
+                       gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
+                       g_relation_insert(relation, msginfo,
+                                         GPOINTER_TO_INT(num));
+                       if (num > last_num)
+                               last_num = num;
+               } else
+                       g_relation_insert(relation, msginfo,
+                                         GPOINTER_TO_INT(0));
+               g_tuples_destroy(tuples);
        }
 
+       g_relation_destroy(uid_mapping);
+       imap_lep_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;
+
+       MUTEX_UNLOCK();
 
        if (ok == IMAP_SUCCESS)
-               return newuid;
+               return last_num;
        else
                return -1;
 }
 
-gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+static gint imap_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
+{
+       GSList msglist;
+
+       g_return_val_if_fail(msginfo != NULL, -1);
+
+       msglist.data = msginfo;
+       msglist.next = NULL;
+
+       return imap_copy_msgs(folder, dest, &msglist, NULL);
+}
+
+static gint imap_copy_msgs(Folder *folder, FolderItem *dest, 
+                   MsgInfoList *msglist, GRelation *relation)
 {
-       gchar *srcfile;
-       gint ret = 0;
+       MsgInfo *msginfo;
+       GSList *file_list;
+       gint ret;
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(dest != NULL, -1);
-       g_return_val_if_fail(msginfo != NULL, -1);
+       g_return_val_if_fail(msglist != NULL, -1);
+
+       msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       if (folder == msginfo->folder->folder)
-               return imap_do_copy(folder, dest, msginfo, FALSE);
+       if (folder == msginfo->folder->folder) {
+               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
+               return ret;
+       }
 
-       srcfile = procmsg_get_message_file(msginfo);
-       if (!srcfile) return -1;
+       file_list = procmsg_get_message_file_list(msglist);
+       g_return_val_if_fail(file_list != NULL, -1);
 
-       ret = imap_add_msg(folder, dest, srcfile, FALSE);
+       ret = imap_add_msgs(folder, dest, file_list, relation);
 
-       g_free(srcfile);
+       procmsg_message_file_list_free(file_list);
 
        return ret;
 }
 
-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);
+
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
 
        session = imap_session_get(folder);
-       if (!session) return -1;
+       if (!session) {
+               MUTEX_UNLOCK();
+               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) {
+               MUTEX_UNLOCK();
                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"));
+               MUTEX_UNLOCK();
                return ok;
        }
-
        ok = imap_cmd_expunge(session);
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't expunge\n"));
+               MUTEX_UNLOCK();
                return ok;
        }
+       
+       g_relation_destroy(uid_mapping);
+       g_slist_free(seq_list);
 
-       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;
+       MUTEX_UNLOCK();
+       if (ok == IMAP_SUCCESS)
+               return 0;
+       else
+               return -1;
 }
 
-gint imap_remove_all_msg(Folder *folder, FolderItem *item)
+static gint imap_remove_msgs(Folder *folder, FolderItem *dest, 
+                   MsgInfoList *msglist, GRelation *relation)
 {
-        gint exists, recent, unseen;
-        guint32 uid_validity;
-       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,
-                        &exists, &recent, &unseen, &uid_validity);
-       if (ok != IMAP_SUCCESS)
-               return ok;
-       if (exists == 0)
-               return IMAP_SUCCESS;
-
-       imap_gen_send(session,
-                     "STORE 1:%d +FLAGS (\\Deleted)", exists);
-       ok = imap_cmd_ok(session, NULL);
-       if (ok != IMAP_SUCCESS) {
-               log_warning(_("can't set deleted flags: 1:%d\n"), exists);
-               return ok;
-       }
+       g_return_val_if_fail(dest != NULL, -1);
+       if (msglist == NULL)
+               return 0;
 
-       ok = imap_cmd_expunge(session);
-       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;
 }
 
-gboolean imap_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+static gboolean imap_is_msg_changed(Folder *folder, FolderItem *item,
+                                   MsgInfo *msginfo)
 {
        /* TODO: properly implement this method */
        return FALSE;
 }
 
-void imap_scan_tree(Folder *folder)
+static gint imap_close(Folder *folder, FolderItem *item)
 {
-       FolderItem *item;
+       return 0;
+}
+
+static gint imap_scan_tree(Folder *folder)
+{
+       FolderItem *item = NULL;
        IMAPSession *session;
        gchar *root_folder = NULL;
 
-       g_return_if_fail(folder != NULL);
-       g_return_if_fail(folder->account != NULL);
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(folder->account != NULL, -1);
+
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
 
        session = imap_session_get(folder);
        if (!session) {
@@ -987,25 +1238,62 @@ void imap_scan_tree(Folder *folder)
                        folder_tree_destroy(folder);
                        item = folder_item_new(folder, folder->name, NULL);
                        item->folder = folder;
-                       folder->node = g_node_new(item);
+                       folder->node = item->node = g_node_new(item);
                }
-               return;
+               MUTEX_UNLOCK();
+               return -1;
        }
 
        if (folder->account->imap_dir && *folder->account->imap_dir) {
-               Xstrdup_a(root_folder, folder->account->imap_dir, return);
+               gchar *real_path;
+               int r;
+               clist * lep_list;
+
+               Xstrdup_a(root_folder, folder->account->imap_dir, {MUTEX_UNLOCK();return -1;});
+               extract_quote(root_folder, '"');
+               subst_char(root_folder,
+                          imap_get_path_separator(IMAP_FOLDER(folder),
+                                                  root_folder),
+                          '/');
                strtailchomp(root_folder, '/');
-               debug_print("IMAP root directory: %s\n", root_folder);
+               real_path = imap_get_real_path
+                       (IMAP_FOLDER(folder), root_folder);
+               debug_print("IMAP root directory: %s\n", real_path);
+
+               /* check if root directory exist */
+
+               r = imap_threaded_list(session->folder, "", real_path,
+                                      &lep_list);
+               if ((r != MAILIMAP_NO_ERROR) || (clist_count(lep_list) == 0)) {
+                       if (!folder->node) {
+                               item = folder_item_new(folder, folder->name, NULL);
+                               item->folder = folder;
+                               folder->node = item->node = g_node_new(item);
+                       }
+                       MUTEX_UNLOCK();
+                       return -1;
+               }
+               
+               mailimap_list_result_free(lep_list);
+               
+               g_free(real_path);
        }
 
-       item = folder_item_new(folder, folder->name, root_folder);
-       item->folder = folder;
-       item->no_select = TRUE;
-       folder->node = g_node_new(item);
-
-       imap_scan_tree_recursive(session, item);
+       if (folder->node)
+               item = FOLDER_ITEM(folder->node->data);
+       if (!item || ((item->path || root_folder) &&
+                     strcmp2(item->path, root_folder) != 0)) {
+               folder_tree_destroy(folder);
+               item = folder_item_new(folder, folder->name, root_folder);
+               item->folder = folder;
+               folder->node = item->node = g_node_new(item);
+       }
 
+       imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data));
        imap_create_missing_folders(folder);
+
+       MUTEX_UNLOCK();
+       return 0;
 }
 
 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
@@ -1014,22 +1302,25 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
        IMAPFolder *imapfolder;
        FolderItem *new_item;
        GSList *item_list, *cur;
+       GNode *node;
        gchar *real_path;
        gchar *wildcard_path;
        gchar separator;
        gchar wildcard[3];
-
+       clist * lep_list;
+       int r;
+       
        g_return_val_if_fail(item != NULL, -1);
        g_return_val_if_fail(item->folder != NULL, -1);
        g_return_val_if_fail(item->no_sub == FALSE, -1);
 
-       folder = FOLDER(item->folder);
+       folder = item->folder;
        imapfolder = IMAP_FOLDER(folder);
 
        separator = imap_get_path_separator(imapfolder, item->path);
 
-       if (item->folder->ui_func)
-               item->folder->ui_func(folder, item, folder->ui_func_data);
+       if (folder->ui_func)
+               folder->ui_func(folder, item, folder->ui_func_data);
 
        if (item->path) {
                wildcard[0] = separator;
@@ -1044,139 +1335,103 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
 
        Xstrcat_a(wildcard_path, real_path, wildcard,
                  {g_free(real_path); return IMAP_ERROR;});
-       QUOTE_IF_REQUIRED(wildcard_path, wildcard_path);
+       lep_list = NULL;
+       r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
+       if (r != MAILIMAP_NO_ERROR) {
+               item_list = NULL;
+       }
+       else {
+               item_list = imap_list_from_lep(imapfolder,
+                                              lep_list, real_path);
+               mailimap_list_result_free(lep_list);
+       }
+       
+       g_free(real_path);
 
-       imap_gen_send(session, "LIST \"\" %s",
-                     wildcard_path);
+       node = item->node->children;
+       while (node != NULL) {
+               FolderItem *old_item = FOLDER_ITEM(node->data);
+               GNode *next = node->next;
 
-       strtailchomp(real_path, separator);
-       item_list = imap_parse_list(imapfolder, session, real_path, NULL);
-       g_free(real_path);
+               new_item = NULL;
+               for (cur = item_list; cur != NULL; cur = cur->next) {
+                       FolderItem *cur_item = FOLDER_ITEM(cur->data);
+                       if (!strcmp2(old_item->path, cur_item->path)) {
+                               new_item = cur_item;
+                               break;
+                       }
+               }
+               if (!new_item) {
+                       debug_print("folder '%s' not found. removing...\n",
+                                   old_item->path);
+                       folder_item_remove(old_item);
+               } else {
+                       old_item->no_sub = new_item->no_sub;
+                       old_item->no_select = new_item->no_select;
+                       if (old_item->no_sub == TRUE && node->children) {
+                               debug_print("folder '%s' doesn't have "
+                                           "subfolders. removing...\n",
+                                           old_item->path);
+                               folder_item_remove_children(old_item);
+                       }
+               }
+
+               node = next;
+       }
 
        for (cur = item_list; cur != NULL; cur = cur->next) {
-               new_item = cur->data;
-               if (!strcmp(new_item->path, "INBOX")) {
-                       if (!folder->inbox) {
-                               new_item->stype = F_INBOX;
-                               item->folder->inbox = new_item;
-                       } else {
-                               folder_item_destroy(new_item);
-                               continue;
+               FolderItem *cur_item = FOLDER_ITEM(cur->data);
+               new_item = NULL;
+               for (node = item->node->children; node != NULL;
+                    node = node->next) {
+                       if (!strcmp2(FOLDER_ITEM(node->data)->path,
+                                    cur_item->path)) {
+                               new_item = FOLDER_ITEM(node->data);
+                               folder_item_destroy(cur_item);
+                               cur_item = NULL;
+                               break;
                        }
-               } else if (!item->parent || item->stype == F_INBOX) {
+               }
+               if (!new_item) {
+                       new_item = cur_item;
+                       debug_print("new folder '%s' found.\n", new_item->path);
+                       folder_item_append(item, new_item);
+               }
+
+               if (!strcmp(new_item->path, "INBOX")) {
+                       new_item->stype = F_INBOX;
+                       folder->inbox = new_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);
                }
-               folder_item_append(item, new_item);
+
                if (new_item->no_sub == FALSE)
                        imap_scan_tree_recursive(session, new_item);
        }
 
-       return IMAP_SUCCESS;
-}
-
-static GSList *imap_parse_list(IMAPFolder *folder, IMAPSession *session,
-                              const gchar *real_path, gchar *separator)
-{
-       gchar buf[IMAPBUFSIZE];
-       gchar flags[256];
-       gchar separator_str[16];
-       gchar *p;
-       gchar *name;
-       gchar *loc_name, *loc_path;
-       GSList *item_list = NULL;
-       GString *str;
-       FolderItem *new_item;
-
-       debug_print("getting list of %s ...\n",
-                   *real_path ? real_path : "\"\"");
-
-       str = g_string_new(NULL);
-
-       for (;;) {
-               if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) <= 0) {
-                       log_warning(_("error occurred while getting LIST.\n"));
-                       break;
-               }
-               strretchomp(buf);
-               if (buf[0] != '*' || buf[1] != ' ') {
-                       log_print("IMAP4< %s\n", buf);
-                       break;
-               }
-               debug_print("IMAP4< %s\n", buf);
-
-               g_string_assign(str, buf);
-               p = str->str + 2;
-               if (strncmp(p, "LIST ", 5) != 0) continue;
-               p += 5;
-
-               if (*p != '(') continue;
-               p++;
-               p = strchr_cpy(p, ')', flags, sizeof(flags));
-               if (!p) continue;
-               while (*p == ' ') p++;
-
-               p = strchr_cpy(p, ' ', separator_str, sizeof(separator_str));
-               if (!p) continue;
-               extract_quote(separator_str, '"');
-               if (!strcmp(separator_str, "NIL"))
-                       separator_str[0] = '\0';
-               if (separator)
-                       *separator = separator_str[0];
-
-               buf[0] = '\0';
-               while (*p == ' ') p++;
-               if (*p == '{' || *p == '"')
-                       p = imap_parse_atom(SESSION(session)->sock, p,
-                                           buf, sizeof(buf), str);
-               else
-                       strncpy2(buf, p, sizeof(buf));
-               strtailchomp(buf, separator_str[0]);
-               if (buf[0] == '\0') continue;
-               if (!strcmp(buf, real_path)) continue;
-
-               if (separator_str[0] != '\0')
-                       subst_char(buf, separator_str[0], '/');
-               name = g_basename(buf);
-               if (name[0] == '.') continue;
-
-               loc_name = imap_modified_utf7_to_locale(name);
-               loc_path = imap_modified_utf7_to_locale(buf);
-               new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
-               if (strcasestr(flags, "\\Noinferiors") != NULL)
-                       new_item->no_sub = TRUE;
-               if (strcmp(buf, "INBOX") != 0 &&
-                   strcasestr(flags, "\\Noselect") != NULL)
-                       new_item->no_select = TRUE;
-
-               item_list = g_slist_append(item_list, new_item);
-
-               debug_print("folder %s has been added.\n", loc_path);
-               g_free(loc_path);
-               g_free(loc_name);
-       }
-
-       g_string_free(str, TRUE);
+       g_slist_free(item_list);
 
-       return item_list;
+       return IMAP_SUCCESS;
 }
 
-gint imap_create_tree(Folder *folder)
+static gint imap_create_tree(Folder *folder)
 {
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(folder->node != NULL, -1);
@@ -1196,17 +1451,6 @@ static void imap_create_missing_folders(Folder *folder)
        if (!folder->inbox)
                folder->inbox = imap_create_special_folder
                        (folder, F_INBOX, "INBOX");
-#if 0
-       if (!folder->outbox)
-               folder->outbox = imap_create_special_folder
-                       (folder, F_OUTBOX, "Sent");
-       if (!folder->draft)
-               folder->draft = imap_create_special_folder
-                       (folder, F_DRAFT, "Drafts");
-       if (!folder->queue)
-               folder->queue = imap_create_special_folder
-                       (folder, F_QUEUE, "Queue");
-#endif
        if (!folder->trash)
                folder->trash = imap_create_special_folder
                        (folder, F_TRASH, "Trash");
@@ -1289,7 +1533,7 @@ static gchar *imap_item_get_path(Folder *folder, FolderItem *item)
        return path;
 }
 
-FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
+static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                               const gchar *name)
 {
        gchar *dirpath, *imap_path;
@@ -1305,10 +1549,15 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        g_return_val_if_fail(parent != NULL, NULL);
        g_return_val_if_fail(name != NULL, NULL);
 
+       MUTEX_TRYLOCK_OR_RETURN_VAL(NULL);
+
        session = imap_session_get(folder);
-       if (!session) return NULL;
+       if (!session) {
+               MUTEX_UNLOCK();
+               return NULL;
+       }
 
-       if (!parent->parent && strcmp(name, "INBOX") == 0)
+       if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0)
                dirpath = g_strdup(name);
        else if (parent->path)
                dirpath = g_strconcat(parent->path, "/", name, NULL);
@@ -1317,7 +1566,7 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        else if (folder->account->imap_dir && *folder->account->imap_dir) {
                gchar *imap_dir;
 
-               Xstrdup_a(imap_dir, folder->account->imap_dir, return NULL);
+               Xstrdup_a(imap_dir, folder->account->imap_dir, {MUTEX_UNLOCK();return NULL;});
                strtailchomp(imap_dir, '/');
                dirpath = g_strconcat(imap_dir, "/", name, NULL);
        } else
@@ -1325,9 +1574,13 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
 
        /* keep trailing directory separator to create a folder that contains
           sub folder */
-       imap_path = imap_locale_to_modified_utf7(dirpath);
+       imap_path = imap_utf8_to_modified_utf7(dirpath);
        strtailchomp(dirpath, '/');
-       Xstrdup_a(new_name, name, {g_free(dirpath); return NULL;});
+       Xstrdup_a(new_name, name, {
+               g_free(dirpath);                
+               MUTEX_UNLOCK();
+               return NULL;});
+
        strtailchomp(new_name, '/');
        separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path);
        imap_path_separator_subst(imap_path, separator);
@@ -1335,38 +1588,32 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
 
        if (strcmp(name, "INBOX") != 0) {
                GPtrArray *argbuf;
-               gint i;
                gboolean exist = FALSE;
-
+               int r;
+               clist * lep_list;
+               
                argbuf = g_ptr_array_new();
-               ok = imap_cmd_list(session, NULL, imap_path,
-                                  argbuf);
-               if (ok != IMAP_SUCCESS) {
+               r = imap_threaded_list(folder, "", imap_path, &lep_list);
+               if (r != MAILIMAP_NO_ERROR) {
                        log_warning(_("can't create mailbox: LIST failed\n"));
                        g_free(imap_path);
                        g_free(dirpath);
                        ptr_array_free_strings(argbuf);
                        g_ptr_array_free(argbuf, TRUE);
+                       MUTEX_UNLOCK();
                        return NULL;
                }
-
-               for (i = 0; i < argbuf->len; i++) {
-                       gchar *str;
-                       str = g_ptr_array_index(argbuf, i);
-                       if (!strncmp(str, "LIST ", 5)) {
-                               exist = TRUE;
-                               break;
-                       }
-               }
-               ptr_array_free_strings(argbuf);
-               g_ptr_array_free(argbuf, TRUE);
-
+               
+               if (clist_count(lep_list) > 0)
+                       exist = TRUE;
+               
                if (!exist) {
                        ok = imap_cmd_create(session, imap_path);
                        if (ok != IMAP_SUCCESS) {
                                log_warning(_("can't create mailbox\n"));
                                g_free(imap_path);
                                g_free(dirpath);
+                               MUTEX_UNLOCK();
                                return NULL;
                        }
                }
@@ -1382,16 +1629,17 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                make_dir_hier(dirpath);
        g_free(dirpath);
 
+       MUTEX_UNLOCK();
        return new_item;
 }
 
-gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
+static gint imap_rename_folder(Folder *folder, FolderItem *item,
+                              const gchar *name)
 {
        gchar *dirpath;
        gchar *newpath;
        gchar *real_oldpath;
        gchar *real_newpath;
-       GNode *node;
        gchar *paths[2];
        gchar *old_cache_dir;
        gchar *new_cache_dir;
@@ -1406,29 +1654,41 @@ gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
        g_return_val_if_fail(item->path != NULL, -1);
        g_return_val_if_fail(name != NULL, -1);
 
-       session = imap_session_get(folder);
-       if (!session) return -1;
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-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"));
+               MUTEX_UNLOCK();
+               return -1;
+       }
+
+       session = imap_session_get(folder);
+       if (!session) {
+               MUTEX_UNLOCK();
+               return -1;
+       }
        real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
 
        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);
+               MUTEX_UNLOCK();
                return -1;
        }
 
        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
                newpath = g_strdup(name);
 
-       real_newpath = imap_locale_to_modified_utf7(newpath);
+       real_newpath = imap_utf8_to_modified_utf7(newpath);
        imap_path_separator_subst(real_newpath, separator);
 
        ok = imap_cmd_rename(session, real_oldpath, real_newpath);
@@ -1438,6 +1698,7 @@ gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
                g_free(real_oldpath);
                g_free(newpath);
                g_free(real_newpath);
+               MUTEX_UNLOCK();
                return -1;
        }
 
@@ -1446,11 +1707,9 @@ gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
 
        old_cache_dir = folder_item_get_path(item);
 
-       node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
-                          item);
        paths[0] = g_strdup(item->path);
        paths[1] = newpath;
-       g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+       g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
                        imap_rename_folder_func, paths);
 
        if (is_dir_exist(old_cache_dir)) {
@@ -1467,10 +1726,11 @@ gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
        g_free(real_oldpath);
        g_free(real_newpath);
 
+       MUTEX_UNLOCK();
        return 0;
 }
 
-gint imap_remove_folder(Folder *folder, FolderItem *item)
+static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
 {
        gint ok;
        IMAPSession *session;
@@ -1483,15 +1743,20 @@ gint imap_remove_folder(Folder *folder, FolderItem *item)
        g_return_val_if_fail(item != NULL, -1);
        g_return_val_if_fail(item->path != NULL, -1);
 
-       session = imap_session_get(folder);
-       if (!session) return -1;
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
 
+       session = imap_session_get(folder);
+       if (!session) {
+               MUTEX_UNLOCK();
+               return -1;
+       }
        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);
+               MUTEX_UNLOCK();
                return -1;
        }
 
@@ -1499,6 +1764,7 @@ gint imap_remove_folder(Folder *folder, FolderItem *item)
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't delete mailbox\n"));
                g_free(path);
+               MUTEX_UNLOCK();
                return -1;
        }
 
@@ -1509,85 +1775,186 @@ gint imap_remove_folder(Folder *folder, FolderItem *item)
        g_free(cache_dir);
        folder_item_remove(item);
 
+       MUTEX_UNLOCK();
        return 0;
 }
 
-static GSList *imap_get_uncached_messages(IMAPSession *session,
-                                         FolderItem *item,
-                                         MsgNumberList *numlist)
+static gint imap_remove_folder(Folder *folder, FolderItem *item)
 {
-       gchar *tmp;
-       GSList *newlist = NULL;
-       GSList *llast = NULL;
-       GString *str;
-       MsgInfo *msginfo;
-       IMAPSet imapset;
+       GNode *node, *next;
 
-       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);
-
-       imapset = numberlist_to_imapset(numlist);
-       while (imapset != NULL) {
-               if (imap_cmd_envelope(session, imapset)
-                   != IMAP_SUCCESS) {
-                       log_warning(_("can't get envelope\n"));
-                       continue;
-               }
+       g_return_val_if_fail(item != NULL, -1);
+       g_return_val_if_fail(item->folder != NULL, -1);
+       g_return_val_if_fail(item->node != NULL, -1);
 
-               str = g_string_new(NULL);
+       node = item->node->children;
+       while (node != NULL) {
+               next = node->next;
+               if (imap_remove_folder(folder, FOLDER_ITEM(node->data)) < 0)
+                       return -1;
+               node = next;
+       }
+       debug_print("IMAP removing %s\n", item->path);
 
-               for (;;) {
-                       if ((tmp = sock_getline(SESSION(session)->sock)) == NULL) {
-                               log_warning(_("error occurred while getting envelope.\n"));
-                               g_string_free(str, TRUE);
-                               break;
-                       }
-                       strretchomp(tmp);
-                       if (tmp[0] != '*' || tmp[1] != ' ') {
-                               log_print("IMAP4< %s\n", tmp);
-                               g_free(tmp);
-                               break;
-                       }
-                       if (strstr(tmp, "FETCH") == NULL) {
-                               log_print("IMAP4< %s\n", tmp);
-                               g_free(tmp);
-                               continue;
-                       }
-                       log_print("IMAP4< %s\n", tmp);
-                       g_string_assign(str, tmp);
-                       g_free(tmp);
-
-                       msginfo = imap_parse_envelope
-                               (SESSION(session)->sock, item, str);
-                       if (!msginfo) {
-                               log_warning(_("can't parse envelope: %s\n"), str->str);
-                               continue;
-                       }
-                       if (item->stype == F_QUEUE) {
-                               MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED);
-                       } else if (item->stype == F_DRAFT) {
-                               MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT);
-                       }
+       if (imap_remove_all_msg(folder, item) < 0)
+               return -1;
+       return imap_remove_folder_real(folder, item);
+}
 
-                       msginfo->folder = item;
+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;
+       
+       GSList *newlist = NULL;
+       GSList *llast = NULL;
+       GSList *seq_list, *cur;
 
+       debug_print("uncached_messages\n");
+       
+       if (session == NULL || item == NULL || item->folder == NULL
+           || FOLDER_CLASS(item->folder) != &imap_class) {
+               stuff->done = TRUE;
+               return NULL;
+       }
+       
+       seq_list = imap_get_lep_set_from_numlist(numlist);
+       debug_print("get msgs info\n");
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               struct mailimap_set * imapset;
+               unsigned int i;
+               int r;
+               carray * env_list;
+               int count;
+               
+               imapset = cur->data;
+               
+               r = imap_threaded_fetch_env(session->folder,
+                                           imapset, &env_list);
+               if (r != MAILIMAP_NO_ERROR)
+                       continue;
+               
+               count = 0;
+               for(i = 0 ; i < carray_count(env_list) ; i ++) {
+                       struct imap_fetch_env_info * info;
+                       MsgInfo * msginfo;
+                       
+                       info = carray_get(env_list, i);
+                       msginfo = imap_envelope_from_lep(info, item);
+                       msginfo->folder = item;
                        if (!newlist)
                                llast = newlist = g_slist_append(newlist, msginfo);
                        else {
                                llast = g_slist_append(llast, msginfo);
                                llast = llast->next;
                        }
+                       count ++;
                }
-
-               g_string_free(str, TRUE);
-               imapset = numberlist_to_imapset(NULL);
+               
+               imap_fetch_env_free(env_list);
        }
+       
+       session_set_access_time(SESSION(session));
+       stuff->done = TRUE;
 
        return newlist;
 }
 
+#define MAX_MSG_NUM 50
+
+static GSList *imap_get_uncached_messages(IMAPSession *session,
+                                       FolderItem *item,
+                                       MsgNumberList *numlist)
+{
+       GSList *result = NULL;
+       GSList * cur;
+       uncached_data *data = g_new0(uncached_data, 1);
+       int finished;
+       
+       finished = 0;
+       cur = numlist;
+       data->total = g_slist_length(numlist);
+       debug_print("messages list : %i\n", data->total);
+       while (cur != NULL) {
+               GSList * partial_result;
+               int count;
+               GSList * newlist;
+               GSList * llast;
+               
+               llast = NULL;
+               count = 0;
+               newlist = NULL;
+               while (count < MAX_MSG_NUM) {
+                       void * p;
+                       
+                       p = cur->data;
+                       
+                       if (newlist == NULL)
+                               llast = newlist = g_slist_append(newlist, p);
+                       else {
+                               llast = g_slist_append(llast, p);
+                               llast = llast->next;
+                       }
+                       count ++;
+                       
+                       cur = cur->next;
+                       if (cur == NULL)
+                               break;
+               }
+               
+               data->done = FALSE;
+               data->session = session;
+               data->item = item;
+               data->numlist = newlist;
+               data->cur += count;
+               
+               if (prefs_common.work_offline && !imap_gtk_should_override()) {
+                       g_free(data);
+                       return NULL;
+               }
+               
+               partial_result =
+                       (GSList *)imap_get_uncached_messages_thread(data);
+               
+               {
+                       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);
+                       debug_print("update progress %g\n",
+                               (gfloat)data->cur / (gfloat)data->total);
+               }
+               
+               g_slist_free(newlist);
+               
+               result = g_slist_concat(result, partial_result);
+       }
+       g_free(data);
+       
+       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();
+       
+       return result;
+}
+
 static void imap_delete_all_cached_messages(FolderItem *item)
 {
        gchar *dir;
@@ -1606,199 +1973,23 @@ static void imap_delete_all_cached_messages(FolderItem *item)
        debug_print("done.\n");
 }
 
-#if USE_OPENSSL
-static SockInfo *imap_open_tunnel(const gchar *server,
-                          const gchar *tunnelcmd,
-                          SSLType ssl_type)
-#else
-static SockInfo *imap_open_tunnel(const gchar *server,
-                          const gchar *tunnelcmd)
-#endif
+static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
+                                                   const gchar *path)
 {
-       SockInfo *sock;
+       IMAPNameSpace *namespace = NULL;
+       gchar *tmp_path, *name;
 
-       if ((sock = sock_connect_cmd(server, tunnelcmd)) == NULL) {
-               log_warning(_("Can't establish IMAP4 session with: %s\n"),
-                           server);
-               return NULL;
-       }
-#if USE_OPENSSL
-       return imap_init_sock(sock, ssl_type);
-#else
-       return imap_init_sock(sock);
-#endif
-}
+       if (!path) path = "";
 
+       for (; ns_list != NULL; ns_list = ns_list->next) {
+               IMAPNameSpace *tmp_ns = ns_list->data;
 
-#if USE_OPENSSL
-static SockInfo *imap_open(const gchar *server, gushort port,
-                          SSLType ssl_type)
-#else
-static SockInfo *imap_open(const gchar *server, gushort port)
-#endif
-{
-       SockInfo *sock;
-
-       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
-       if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
-               log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
-                           server, port);
-               sock_close(sock);
-               return NULL;
-       }
-#endif
-       return sock;
-}
-
-#if USE_OPENSSL
-static SockInfo *imap_init_sock(SockInfo *sock, SSLType ssl_type)
-#else
-static SockInfo *imap_init_sock(SockInfo *sock)
-#endif
-{
-
-       return sock;
-}
-
-static GList *imap_parse_namespace_str(gchar *str)
-{
-       gchar *p = str;
-       gchar *name;
-       gchar *separator;
-       IMAPNameSpace *namespace;
-       GList *ns_list = NULL;
-
-       while (*p != '\0') {
-               /* parse ("#foo" "/") */
-
-               while (*p && *p != '(') p++;
-               if (*p == '\0') break;
-               p++;
-
-               while (*p && *p != '"') p++;
-               if (*p == '\0') break;
-               p++;
-               name = p;
-
-               while (*p && *p != '"') p++;
-               if (*p == '\0') break;
-               *p = '\0';
-               p++;
-
-               while (*p && isspace(*p)) p++;
-               if (*p == '\0') break;
-               if (strncmp(p, "NIL", 3) == 0)
-                       separator = NULL;
-               else if (*p == '"') {
-                       p++;
-                       separator = p;
-                       while (*p && *p != '"') p++;
-                       if (*p == '\0') break;
-                       *p = '\0';
-                       p++;
-               } else break;
-
-               while (*p && *p != ')') p++;
-               if (*p == '\0') break;
-               p++;
-
-               namespace = g_new(IMAPNameSpace, 1);
-               namespace->name = g_strdup(name);
-               namespace->separator = separator ? separator[0] : '\0';
-               ns_list = g_list_append(ns_list, namespace);
-       }
-
-       return ns_list;
-}
-
-static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
-{
-       gchar *ns_str;
-       gchar **str_array;
-
-       g_return_if_fail(session != NULL);
-       g_return_if_fail(folder != NULL);
-
-       if (folder->ns_personal != NULL ||
-           folder->ns_others   != NULL ||
-           folder->ns_shared   != NULL)
-               return;
-
-       if (!imap_has_capability(session, "NAMESPACE")) {
-               imap_get_namespace_by_list(session, folder);
-               return;
-       }
-       
-       if (imap_cmd_namespace(session, &ns_str)
-           != IMAP_SUCCESS) {
-               log_warning(_("can't get namespace\n"));
-               return;
-       }
-
-       str_array = strsplit_parenthesis(ns_str, '(', ')', 3);
-       if (str_array == NULL) {
-               g_free(ns_str);
-               imap_get_namespace_by_list(session, folder);
-               return;
-       }
-       if (str_array[0])
-               folder->ns_personal = imap_parse_namespace_str(str_array[0]);
-       if (str_array[0] && str_array[1])
-               folder->ns_others = imap_parse_namespace_str(str_array[1]);
-       if (str_array[0] && str_array[1] && str_array[2])
-               folder->ns_shared = imap_parse_namespace_str(str_array[2]);
-       g_strfreev(str_array);
-       g_free(ns_str);
-}
-
-static void imap_get_namespace_by_list(IMAPSession *session, IMAPFolder *folder)
-{
-       GSList *item_list, *cur;
-       gchar separator = '\0';
-       IMAPNameSpace *namespace;
-
-       g_return_if_fail(session != NULL);
-       g_return_if_fail(folder != NULL);
-
-       if (folder->ns_personal != NULL ||
-           folder->ns_others   != NULL ||
-           folder->ns_shared   != NULL)
-               return;
-
-       imap_gen_send(session, "LIST \"\" \"\"");
-       item_list = imap_parse_list(folder, session, "", &separator);
-       for (cur = item_list; cur != NULL; cur = cur->next)
-               folder_item_destroy(FOLDER_ITEM(cur->data));
-       g_slist_free(item_list);
-
-       namespace = g_new(IMAPNameSpace, 1);
-       namespace->name = g_strdup("");
-       namespace->separator = separator;
-       folder->ns_personal = g_list_append(NULL, namespace);
-}
-
-static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
-                                                   const gchar *path)
-{
-       IMAPNameSpace *namespace = NULL;
-       gchar *tmp_path, *name;
-
-       if (!path) path = "";
-
-       Xstrcat_a(tmp_path, path, "/", return NULL);
-
-       for (; ns_list != NULL; ns_list = ns_list->next) {
-               IMAPNameSpace *tmp_ns = ns_list->data;
-
+               Xstrcat_a(tmp_path, path, "/", return namespace);
                Xstrdup_a(name, tmp_ns->name, return namespace);
-               if (tmp_ns->separator && tmp_ns->separator != '/')
+               if (tmp_ns->separator && tmp_ns->separator != '/') {
+                       subst_char(tmp_path, tmp_ns->separator, '/');
                        subst_char(name, tmp_ns->separator, '/');
+               }
                if (strncmp(tmp_path, name, strlen(name)) == 0)
                        namespace = tmp_ns;
        }
@@ -1843,299 +2034,58 @@ static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(path != NULL, NULL);
 
-       real_path = imap_locale_to_modified_utf7(path);
+       real_path = imap_utf8_to_modified_utf7(path);
        separator = imap_get_path_separator(folder, path);
        imap_path_separator_subst(real_path, separator);
 
        return real_path;
 }
 
-static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
-                             gchar *dest, gint dest_len, GString *str)
-{
-       gchar *cur_pos = src;
-       gchar *nextline;
-
-       g_return_val_if_fail(str != NULL, cur_pos);
-
-       /* read the next line if the current response buffer is empty */
-       while (isspace(*cur_pos)) cur_pos++;
-       while (*cur_pos == '\0') {
-               if ((nextline = sock_getline(sock)) == NULL)
-                       return cur_pos;
-               g_string_assign(str, nextline);
-               cur_pos = str->str;
-               strretchomp(nextline);
-               /* log_print("IMAP4< %s\n", nextline); */
-               debug_print("IMAP4< %s\n", nextline);
-               g_free(nextline);
-
-               while (isspace(*cur_pos)) cur_pos++;
-       }
-
-       if (!strncmp(cur_pos, "NIL", 3)) {
-               *dest = '\0';
-               cur_pos += 3;
-       } else if (*cur_pos == '\"') {
-               gchar *p;
-
-               p = get_quoted(cur_pos, '\"', dest, dest_len);
-               cur_pos = p ? p : cur_pos + 2;
-       } else if (*cur_pos == '{') {
-               gchar buf[32];
-               gint len;
-               gint line_len = 0;
-
-               cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
-               len = atoi(buf);
-               g_return_val_if_fail(len > 0, cur_pos);
-
-               g_string_truncate(str, 0);
-               cur_pos = str->str;
-
-               do {
-                       if ((nextline = sock_getline(sock)) == NULL)
-                               return cur_pos;
-                       line_len += strlen(nextline);
-                       g_string_append(str, nextline);
-                       cur_pos = str->str;
-                       strretchomp(nextline);
-                       /* log_print("IMAP4< %s\n", nextline); */
-                       debug_print("IMAP4< %s\n", nextline);
-                       g_free(nextline);
-               } while (line_len < len);
-
-               memcpy(dest, cur_pos, MIN(len, dest_len - 1));
-               dest[MIN(len, dest_len - 1)] = '\0';
-               cur_pos += len;
-       }
-
-       return cur_pos;
-}
-
-static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
-                             GString *str)
-{
-       gchar *nextline;
-       gchar buf[32];
-       gint len;
-       gint block_len = 0;
-
-       *headers = NULL;
-
-       g_return_val_if_fail(str != NULL, cur_pos);
-
-       while (isspace(*cur_pos)) cur_pos++;
-
-       g_return_val_if_fail(*cur_pos == '{', cur_pos);
-
-       cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
-       len = atoi(buf);
-       g_return_val_if_fail(len > 0, cur_pos);
-
-       g_string_truncate(str, 0);
-       cur_pos = str->str;
-
-       do {
-               if ((nextline = sock_getline(sock)) == NULL)
-                       return cur_pos;
-               block_len += strlen(nextline);
-               g_string_append(str, nextline);
-               cur_pos = str->str;
-               strretchomp(nextline);
-               /* debug_print("IMAP4< %s\n", nextline); */
-               g_free(nextline);
-       } while (block_len < len);
-
-       debug_print("IMAP4< [contents of BODY.PEEK[HEADER.FIELDS (...)]]\n");
-
-       *headers = g_strndup(cur_pos, len);
-       cur_pos += len;
-
-       while (isspace(*cur_pos)) cur_pos++;
-       while (*cur_pos == '\0') {
-               if ((nextline = sock_getline(sock)) == NULL)
-                       return cur_pos;
-               g_string_assign(str, nextline);
-               cur_pos = str->str;
-               strretchomp(nextline);
-               debug_print("IMAP4< %s\n", nextline);
-               g_free(nextline);
-
-               while (isspace(*cur_pos)) cur_pos++;
-       }
-
-       return cur_pos;
-}
-
-static MsgFlags imap_parse_flags(const gchar *flag_str)  
-{
-       const gchar *p = flag_str;
-       MsgFlags flags = {0, 0};
-
-       flags.perm_flags = MSG_UNREAD;
-
-       while ((p = strchr(p, '\\')) != NULL) {
-               p++;
-
-               if (g_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
-                       MSG_SET_PERM_FLAGS(flags, MSG_NEW);
-               } else if (g_strncasecmp(p, "Seen", 4) == 0) {
-                       MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
-               } else if (g_strncasecmp(p, "Deleted", 7) == 0) {
-                       MSG_SET_PERM_FLAGS(flags, MSG_DELETED);
-               } else if (g_strncasecmp(p, "Flagged", 7) == 0) {
-                       MSG_SET_PERM_FLAGS(flags, MSG_MARKED);
-               } else if (g_strncasecmp(p, "Answered", 8) == 0) {
-                       MSG_SET_PERM_FLAGS(flags, MSG_REPLIED);
-               }
-       }
-
-       return flags;
-}
-
-static MsgInfo *imap_parse_envelope(SockInfo *sock, FolderItem *item,
-                                   GString *line_str)
-{
-       gchar buf[IMAPBUFSIZE];
-       MsgInfo *msginfo = NULL;
-       gchar *cur_pos;
-       gint msgnum;
-       guint32 uid = 0;
-       size_t size = 0;
-       MsgFlags flags = {0, 0}, imap_flags = {0, 0};
-
-       g_return_val_if_fail(line_str != NULL, NULL);
-       g_return_val_if_fail(line_str->str[0] == '*' &&
-                            line_str->str[1] == ' ', NULL);
-
-       MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
-       if (item->stype == F_QUEUE) {
-               MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
-               MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
-       }
-
-       cur_pos = line_str->str + 2;
-
-#define PARSE_ONE_ELEMENT(ch)                                  \
-{                                                              \
-       cur_pos = strchr_cpy(cur_pos, ch, buf, sizeof(buf));    \
-       if (cur_pos == NULL) {                                  \
-               g_warning("cur_pos == NULL\n");                 \
-               procmsg_msginfo_free(msginfo);                  \
-               return NULL;                                    \
-       }                                                       \
-}
-
-       PARSE_ONE_ELEMENT(' ');
-       msgnum = atoi(buf);
-
-       PARSE_ONE_ELEMENT(' ');
-       g_return_val_if_fail(!strcmp(buf, "FETCH"), NULL);
-
-       g_return_val_if_fail(*cur_pos == '(', NULL);
-       cur_pos++;
-
-       while (*cur_pos != '\0' && *cur_pos != ')') {
-               while (*cur_pos == ' ') cur_pos++;
-
-               if (!strncmp(cur_pos, "UID ", 4)) {
-                       cur_pos += 4;
-                       uid = strtoul(cur_pos, &cur_pos, 10);
-               } else if (!strncmp(cur_pos, "FLAGS ", 6)) {
-                       cur_pos += 6;
-                       if (*cur_pos != '(') {
-                               g_warning("*cur_pos != '('\n");
-                               procmsg_msginfo_free(msginfo);
-                               return NULL;
-                       }
-                       cur_pos++;
-                       PARSE_ONE_ELEMENT(')');
-                       imap_flags = imap_parse_flags(buf);
-               } else if (!strncmp(cur_pos, "RFC822.SIZE ", 12)) {
-                       cur_pos += 12;
-                       size = strtol(cur_pos, &cur_pos, 10);
-               } else if (!strncmp(cur_pos, "BODY[HEADER.FIELDS ", 19)) {
-                       gchar *headers;
-
-                       cur_pos += 19;
-                       if (*cur_pos != '(') {
-                               g_warning("*cur_pos != '('\n");
-                               procmsg_msginfo_free(msginfo);
-                               return NULL;
-                       }
-                       cur_pos++;
-                       PARSE_ONE_ELEMENT(')');
-                       if (*cur_pos != ']') {
-                               g_warning("*cur_pos != ']'\n");
-                               procmsg_msginfo_free(msginfo);
-                               return NULL;
-                       }
-                       cur_pos++;
-
-                       cur_pos = imap_get_header(sock, cur_pos, &headers,
-                                                 line_str);
-                       msginfo = procheader_parse_str(headers, flags, FALSE, FALSE);
-                       g_free(headers);
-               } else {
-                       g_warning("invalid FETCH response: %s\n", cur_pos);
-                       break;
-               }
-       }
-
-       if (msginfo) {
-               msginfo->msgnum = uid;
-               msginfo->size = size;
-               msginfo->flags.tmp_flags |= imap_flags.tmp_flags;
-               msginfo->flags.perm_flags = imap_flags.perm_flags;
-       }
-
-       return msginfo;
-}
-
 static gint imap_set_message_flags(IMAPSession *session,
                                   MsgNumberList *numlist,
                                   IMAPFlags flags,
                                   gboolean is_set)
 {
-       GString *buf;
-       gint ok;
-       IMAPSet imapset;
-
-       buf = g_string_new(is_set ? "+FLAGS (" : "-FLAGS (");
-
-       if (IMAP_IS_SEEN(flags))        g_string_append(buf, "\\Seen ");
-       if (IMAP_IS_ANSWERED(flags))    g_string_append(buf, "\\Answered ");
-       if (IMAP_IS_FLAGGED(flags))     g_string_append(buf, "\\Flagged ");
-       if (IMAP_IS_DELETED(flags))     g_string_append(buf, "\\Deleted ");
-       if (IMAP_IS_DRAFT(flags))       g_string_append(buf, "\\Draft");
-
-       if (buf->str[buf->len - 1] == ' ')
-               g_string_truncate(buf, buf->len - 1);
+       gint ok = 0;
+       GSList *seq_list;
+       GSList * cur;
 
-       g_string_append_c(buf, ')');
-
-       imapset = numberlist_to_imapset(numlist);
-       while (imapset != NULL) {
+       seq_list = imap_get_lep_set_from_numlist(numlist);
+       
+       for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
+               struct mailimap_set * imapset;
+               
+               imapset = cur->data;
+               
                ok = imap_cmd_store(session, imapset,
-                                   buf->str);
-               imapset = numberlist_to_imapset(NULL);
+                                   flags, is_set);
        }
-       g_string_free(buf, TRUE);
-
-       return ok;
+       
+       imap_lep_set_free(seq_list);
+       
+       return IMAP_SUCCESS;
 }
 
+typedef struct _select_data {
+       IMAPSession *session;
+       gchar *real_path;
+       gint *exists;
+       gint *recent;
+       gint *unseen;
+       guint32 *uid_validity;
+       gboolean done;
+} select_data;
+
 static gint imap_select(IMAPSession *session, IMAPFolder *folder,
                        const gchar *path,
                        gint *exists, gint *recent, gint *unseen,
-                       guint32 *uid_validity)
+                       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;
@@ -2149,8 +2099,9 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
        session->mbox = NULL;
 
        real_path = imap_get_real_path(folder, path);
+
        ok = imap_cmd_select(session, real_path,
-                            exists, recent, unseen, uid_validity);
+                            exists, recent, unseen, uid_validity, block);
        if (ok != IMAP_SUCCESS)
                log_warning(_("can't select folder: %s\n"), real_path);
        else {
@@ -2162,1441 +2113,1698 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
        return ok;
 }
 
-#define THROW(err) { ok = err; goto catch; }
-
 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_;
-       gint ok;
-       GPtrArray *argbuf;
-       gchar *str;
-
-       *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
-
-       argbuf = g_ptr_array_new();
-
-       real_path = imap_get_real_path(folder, path);
-       QUOTE_IF_REQUIRED(real_path_, real_path);
-       imap_gen_send(session, "STATUS %s "
-                     "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
-                     real_path_);
-
-       ok = imap_cmd_ok(session, argbuf);
-       if (ok != IMAP_SUCCESS) THROW(ok);
-
-       str = search_array_str(argbuf, "STATUS");
-       if (!str) THROW(IMAP_ERROR);
-
-       str = strchr(str, '(');
-       if (!str) THROW(IMAP_ERROR);
-       str++;
-       while (*str != '\0' && *str != ')') {
-               while (*str == ' ') str++;
-
-               if (!strncmp(str, "MESSAGES ", 9)) {
-                       str += 9;
-                       *messages = strtol(str, &str, 10);
-               } else if (!strncmp(str, "RECENT ", 7)) {
-                       str += 7;
-                       *recent = strtol(str, &str, 10);
-               } else if (!strncmp(str, "UIDNEXT ", 8)) {
-                       str += 8;
-                       *uid_next = strtoul(str, &str, 10);
-               } else if (!strncmp(str, "UIDVALIDITY ", 12)) {
-                       str += 12;
-                       *uid_validity = strtoul(str, &str, 10);
-               } else if (!strncmp(str, "UNSEEN ", 7)) {
-                       str += 7;
-                       *unseen = strtol(str, &str, 10);
-               } else {
-                       g_warning("invalid STATUS response: %s\n", str);
+       int r;
+       clistiter * iter;
+       struct mailimap_mailbox_data_status * data_status;
+       int got_values;
+       
+       r = imap_threaded_status(FOLDER(folder), path, &data_status);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       if (data_status->st_info_list == NULL) {
+               mailimap_mailbox_data_status_free(data_status);
+               return IMAP_ERROR;
+       }
+       
+       got_values = 0;
+       for(iter = clist_begin(data_status->st_info_list) ; iter != NULL ;
+           iter = clist_next(iter)) {
+               struct mailimap_status_info * info;             
+               
+               info = clist_content(iter);
+               switch (info->st_att) {
+               case MAILIMAP_STATUS_ATT_MESSAGES:
+                       * messages = info->st_value;
+                       got_values |= 1 << 0;
+                       break;
+                       
+               case MAILIMAP_STATUS_ATT_RECENT:
+                       * recent = info->st_value;
+                       got_values |= 1 << 1;
+                       break;
+                       
+               case MAILIMAP_STATUS_ATT_UIDNEXT:
+                       * uid_next = info->st_value;
+                       got_values |= 1 << 2;
+                       break;
+                       
+               case MAILIMAP_STATUS_ATT_UIDVALIDITY:
+                       * uid_validity = info->st_value;
+                       got_values |= 1 << 3;
+                       break;
+                       
+               case MAILIMAP_STATUS_ATT_UNSEEN:
+                       * unseen = info->st_value;
+                       got_values |= 1 << 4;
                        break;
                }
        }
-
-catch:
-       g_free(real_path);
-       ptr_array_free_strings(argbuf);
-       g_ptr_array_free(argbuf, TRUE);
-
-       return ok;
+       mailimap_mailbox_data_status_free(data_status);
+       
+       if (got_values != ((1 << 4) + (1 << 3) +
+                          (1 << 2) + (1 << 1) + (1 << 0)))
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
 
-#undef THROW
-
+static void imap_free_capabilities(IMAPSession *session)
+{
+       g_strfreev(session->capability);
+       session->capability = NULL;
+}
 
 /* low-level IMAP4rev1 commands */
 
-static gint imap_cmd_login(IMAPSession *session,
-                          const gchar *user, const gchar *pass)
+#if 0
+static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
+                                 const gchar *pass, IMAPAuthType type)
 {
-       gchar *user_, *pass_;
+       gchar *auth_type;
        gint ok;
+       gchar *buf = NULL;
+       gchar *challenge;
+       gint challenge_len;
+       gchar hexdigest[33];
+       gchar *response;
+       gchar *response64;
+
+       auth_type = "CRAM-MD5";
+
+       imap_gen_send(session, "AUTHENTICATE %s", auth_type);
+       ok = imap_gen_recv(session, &buf);
+       if (ok != IMAP_SUCCESS || buf[0] != '+' || buf[1] != ' ') {
+               g_free(buf);
+               return IMAP_ERROR;
+       }
+
+       challenge = g_malloc(strlen(buf + 2) + 1);
+       challenge_len = base64_decode(challenge, buf + 2, -1);
+       challenge[challenge_len] = '\0';
+       g_free(buf);
+       log_print("IMAP< [Decoded: %s]\n", challenge);
+
+       md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
+       g_free(challenge);
 
-       QUOTE_IF_REQUIRED(user_, user);
-       QUOTE_IF_REQUIRED(pass_, pass);
-       imap_gen_send(session, "LOGIN %s %s", user_, pass_);
+       response = g_strdup_printf("%s %s", user, hexdigest);
+       log_print("IMAP> [Encoded: %s]\n", response);
+       response64 = g_malloc((strlen(response) + 3) * 2 + 1);
+       base64_encode(response64, response, strlen(response));
+       g_free(response);
 
+       log_print("IMAP> %s\n", response64);
+       sock_puts(SESSION(session)->sock, response64);
        ok = imap_cmd_ok(session, NULL);
        if (ok != IMAP_SUCCESS)
-               log_warning(_("IMAP4 login failed.\n"));
+               log_warning(_("IMAP4 authentication failed.\n"));
 
        return ok;
 }
+#endif
 
-static gint imap_cmd_logout(IMAPSession *session)
+static gint imap_cmd_login(IMAPSession *session,
+                          const gchar *user, const gchar *pass)
 {
-       imap_gen_send(session, "LOGOUT");
-       return imap_cmd_ok(session, NULL);
+       int r;
+       gint ok;
+
+       r = imap_threaded_login(session->folder, user, pass);
+       if (r != MAILIMAP_NO_ERROR)
+               ok = IMAP_ERROR;
+       else
+               ok = IMAP_SUCCESS;
+       
+       return ok;
 }
 
-/* Send CAPABILITY, and examine the server's response to see whether this
- * connection is pre-authenticated or not and build a list of CAPABILITIES. */
-static gint imap_greeting(IMAPSession *session)
+static gint imap_cmd_logout(IMAPSession *session)
 {
-       gchar *capstr;
-       GPtrArray *argbuf;
-
-       imap_gen_send(session, "CAPABILITY");
+       imap_threaded_disconnect(session->folder);
        
-       argbuf = g_ptr_array_new();
-
-       if (imap_cmd_ok(session, argbuf) != IMAP_SUCCESS ||
-           ((capstr = search_array_str(argbuf, "CAPABILITY ")) == NULL)) {
-               ptr_array_free_strings(argbuf);
-               g_ptr_array_free(argbuf, TRUE);
-               return -1;
-       }
+       return IMAP_SUCCESS;
+}
 
-       session->authenticated = search_array_str(argbuf, "PREAUTH") != NULL;
+static gint imap_cmd_noop(IMAPSession *session)
+{
+       int r;
+       unsigned int exists;
        
-       capstr += strlen("CAPABILITY ");
-
-       IMAP_SESSION(session)->capability = g_strsplit(capstr, " ", 0);
+       r = imap_threaded_noop(session->folder, &exists);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
        
-       ptr_array_free_strings(argbuf);
-       g_ptr_array_free(argbuf, TRUE);
-
-       return 0;
+       session->exists = exists;
+       
+       return IMAP_SUCCESS;
 }
 
-static gboolean imap_has_capability(IMAPSession *session, const gchar *cap)
+#if USE_OPENSSL
+static gint imap_cmd_starttls(IMAPSession *session)
 {
-       gchar **p;
-       
-       for (p = session->capability; *p != NULL; ++p)
-               if (g_strcasecmp(*p, cap) == 0)
-                       return TRUE;
+       int r;
 
-       return FALSE;
+       r = imap_threaded_starttls(session->folder);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
+#endif
 
-void imap_free_capabilities(IMAPSession *session)
+static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
+                           gint *exists, gint *recent, gint *unseen,
+                           guint32 *uid_validity, gboolean block)
 {
-       g_strfreev(session->capability);
-       session->capability = NULL;
-}
+       int r;
 
-static const IMAPSet numberlist_to_imapset(MsgNumberList *list)
-{
-       static GString *imapset = NULL;
-       static MsgNumberList *numlist, *elem;
-       guint first, last, next;
-
-       if (imapset == NULL)
-               imapset = g_string_sized_new(256);
-       else
-               g_string_truncate(imapset, 0);
-
-       if (list != NULL) {
-               g_slist_free(numlist);
-               numlist = g_slist_copy(list);
-               numlist = g_slist_sort(numlist, g_int_compare);
-       } else if (numlist == NULL) {
-               return NULL;
-       }
-
-       first = GPOINTER_TO_INT(numlist->data);
-       last = first;
-       for(elem = g_slist_next(numlist); elem != NULL; elem = g_slist_next(elem)) {
-               next = GPOINTER_TO_INT(elem->data);
-
-               if(next != (last + 1)) {
-                       if (imapset->len > 0)
-                               g_string_append(imapset, ",");
-                       if (first == last)
-                               g_string_sprintfa(imapset, "%d", first);
-                       else
-                               g_string_sprintfa(imapset, "%d:%d", first, last);
-
-                       if (imapset->len > IMAPCMDLIMIT) {
-                               last = 0;
-                               break;
-                       }
-
-                       first = next;
-               }
-               last = next;
-       }
-       if (last != 0) {
-               if (imapset->len > 0)
-                       g_string_append(imapset, ",");
-               if (first == last)
-                       g_string_sprintfa(imapset, "%d", first);
-               else
-                       g_string_sprintfa(imapset, "%d:%d", first, last);
-
-               g_slist_free(numlist);
-               numlist = NULL;
-       } else {
-               MsgNumberList *remaining;
-
-               remaining = elem->next;
-               remaining = g_slist_prepend(remaining, elem->data);
-               elem->next = NULL;
-               g_slist_free(numlist);
-               numlist = remaining;
+       r = imap_threaded_select(session->folder, folder,
+                                exists, recent, unseen, uid_validity);
+       if (r != MAILIMAP_NO_ERROR) {
+               return IMAP_ERROR;
        }
-
-       return imapset->str;
+       
+       return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_noop(IMAPSession *session)
+static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
+                            gint *exists, gint *recent, gint *unseen,
+                            guint32 *uid_validity, gboolean block)
 {
-       imap_gen_send(session, "NOOP");
-       return imap_cmd_ok(session, NULL);
-}
+       int r;
 
-static gint imap_cmd_starttls(IMAPSession *session)
-{
-       imap_gen_send(session, "STARTTLS");
-       return imap_cmd_ok(session, NULL);
+       r = imap_threaded_examine(session->folder, folder,
+                                 exists, recent, unseen, uid_validity);
+       if (r != MAILIMAP_NO_ERROR) {
+               return IMAP_ERROR;
+       }
+       
+       return IMAP_SUCCESS;
 }
 
-#define THROW(err) { ok = err; goto catch; }
-
-static gint imap_cmd_namespace(IMAPSession *session, gchar **ns_str)
+static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
 {
-       gint ok;
-       GPtrArray *argbuf;
-       gchar *str;
-
-       argbuf = g_ptr_array_new();
-
-       imap_gen_send(session, "NAMESPACE");
-       if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW(ok);
-
-       str = search_array_str(argbuf, "NAMESPACE");
-       if (!str) THROW(IMAP_ERROR);
-
-       *ns_str = g_strdup(str);
-
-catch:
-       ptr_array_free_strings(argbuf);
-       g_ptr_array_free(argbuf, TRUE);
-
-       return ok;
+       int r;
+       
+       r = imap_threaded_create(session->folder, folder);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
 
-#undef THROW
-
-static gint imap_cmd_list(IMAPSession *session, const gchar *ref,
-                         const gchar *mailbox, GPtrArray *argbuf)
+static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
+                           const gchar *new_folder)
 {
-       gchar *ref_, *mailbox_;
-
-       if (!ref) ref = "\"\"";
-       if (!mailbox) mailbox = "\"\"";
-
-       QUOTE_IF_REQUIRED(ref_, ref);
-       QUOTE_IF_REQUIRED(mailbox_, mailbox);
-       imap_gen_send(session, "LIST %s %s", ref_, mailbox_);
-
-       return imap_cmd_ok(session, argbuf);
+       int r;
+       
+       r = imap_threaded_rename(session->folder, old_folder,
+                                new_folder);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
 
-#define THROW goto catch
-
-static gint imap_cmd_do_select(IMAPSession *session, const gchar *folder,
-                              gboolean examine,
-                              gint *exists, gint *recent, gint *unseen,
-                              guint32 *uid_validity)
+static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
 {
-       gint ok;
-       gchar *resp_str;
-       GPtrArray *argbuf;
-       gchar *select_cmd;
-       gchar *folder_;
-
-       *exists = *recent = *unseen = *uid_validity = 0;
-       argbuf = g_ptr_array_new();
-
-       if (examine)
-               select_cmd = "EXAMINE";
-       else
-               select_cmd = "SELECT";
-
-       QUOTE_IF_REQUIRED(folder_, folder);
-       imap_gen_send(session, "%s %s", select_cmd, folder_);
-
-       if ((ok = imap_cmd_ok(session, argbuf)) != IMAP_SUCCESS) THROW;
-
-       resp_str = search_array_contain_str(argbuf, "EXISTS");
-       if (resp_str) {
-               if (sscanf(resp_str,"%d EXISTS", exists) != 1) {
-                       g_warning("imap_cmd_select(): invalid EXISTS line.\n");
-                       THROW;
-               }
-       }
-
-       resp_str = search_array_contain_str(argbuf, "RECENT");
-       if (resp_str) {
-               if (sscanf(resp_str, "%d RECENT", recent) != 1) {
-                       g_warning("imap_cmd_select(): invalid RECENT line.\n");
-                       THROW;
-               }
-       }
+       int r;
+       
+       r = imap_threaded_delete(session->folder, folder);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
+}
 
-       resp_str = search_array_contain_str(argbuf, "UIDVALIDITY");
-       if (resp_str) {
-               if (sscanf(resp_str, "OK [UIDVALIDITY %u] ", uid_validity)
-                   != 1) {
-                       g_warning("imap_cmd_select(): invalid UIDVALIDITY line.\n");
-                       THROW;
-               }
+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;
+       int r;
+       
+       if (stuff->body) {
+               r = imap_threaded_fetch_content(session->folder,
+                                              uid, 1, filename);
        }
-
-       resp_str = search_array_contain_str(argbuf, "UNSEEN");
-       if (resp_str) {
-               if (sscanf(resp_str, "OK [UNSEEN %d] ", unseen) != 1) {
-                       g_warning("imap_cmd_select(): invalid UNSEEN line.\n");
-                       THROW;
-               }
+       else {
+               r = imap_threaded_fetch_content(session->folder,
+                                               uid, 0, filename);
        }
-
-catch:
-       ptr_array_free_strings(argbuf);
-       g_ptr_array_free(argbuf, TRUE);
-
-       return ok;
+       if (r != MAILIMAP_NO_ERROR)
+               return GINT_TO_POINTER(IMAP_ERROR);
+       
+       return GINT_TO_POINTER(IMAP_SUCCESS);
 }
 
-static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
-                           gint *exists, gint *recent, gint *unseen,
-                           guint32 *uid_validity)
+static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
+                               const gchar *filename, gboolean headers,
+                               gboolean body)
 {
-       return imap_cmd_do_select(session, folder, FALSE,
-                                 exists, recent, unseen, uid_validity);
-}
+       fetch_data *data = g_new0(fetch_data, 1);
+       int result = 0;
+       data->done = FALSE;
+       data->session = session;
+       data->uid = uid;
+       data->filename = filename;
+       data->headers = headers;
+       data->body = body;
 
-static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
-                            gint *exists, gint *recent, gint *unseen,
-                            guint32 *uid_validity)
-{
-       return imap_cmd_do_select(session, folder, TRUE,
-                                 exists, recent, unseen, uid_validity);
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
+       }
+
+       result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+       g_free(data);
+       return result;
 }
 
-#undef THROW
 
-static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
+static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
+                           const gchar *file, IMAPFlags flags, 
+                           guint32 *new_uid)
 {
-       gchar *folder_;
+       struct mailimap_flag_list * flag_list;
+       int r;
+       
+       g_return_val_if_fail(file != NULL, IMAP_ERROR);
 
-       QUOTE_IF_REQUIRED(folder_, folder);
-       imap_gen_send(session, "CREATE %s", folder_);
+       flag_list = imap_flag_to_lep(flags);
+       r = imap_threaded_append(session->folder, destfolder,
+                        file, flag_list);
+       
+       if (new_uid != NULL)
+               *new_uid = 0;
 
-       return imap_cmd_ok(session, NULL);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
-                           const gchar *new_folder)
+static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
+                         const gchar *destfolder, GRelation *uid_mapping)
 {
-       gchar *old_folder_, *new_folder_;
-
-       QUOTE_IF_REQUIRED(old_folder_, old_folder);
-       QUOTE_IF_REQUIRED(new_folder_, new_folder);
-       imap_gen_send(session, "RENAME %s %s", old_folder_, new_folder_);
+       int r;
+       
+       g_return_val_if_fail(session != NULL, IMAP_ERROR);
+       g_return_val_if_fail(set != NULL, IMAP_ERROR);
+       g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
 
-       return imap_cmd_ok(session, NULL);
+       r = imap_threaded_copy(session->folder, set, destfolder);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
+static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
+                          IMAPFlags flags, int do_add)
 {
-       gchar *folder_;
-
-       QUOTE_IF_REQUIRED(folder_, folder);
-       imap_gen_send(session, "DELETE %s", folder_);
-
-       return imap_cmd_ok(session, NULL);
+       int r;
+       struct mailimap_flag_list * flag_list;
+       struct mailimap_store_att_flags * store_att_flags;
+       
+       flag_list = imap_flag_to_lep(flags);
+       
+       if (do_add)
+               store_att_flags =
+                       mailimap_store_att_flags_new_add_flags_silent(flag_list);
+       else
+               store_att_flags =
+                       mailimap_store_att_flags_new_remove_flags_silent(flag_list);
+       
+       r = imap_threaded_store(session->folder, set, store_att_flags);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_search(IMAPSession *session, const gchar *criteria, GSList **list)
+static gint imap_cmd_expunge(IMAPSession *session)
 {
-       gint ok;
-       gchar *uidlist;
-       GPtrArray *argbuf;
-
-       g_return_val_if_fail(criteria != NULL, IMAP_ERROR);
-       g_return_val_if_fail(list != NULL, IMAP_ERROR);
-
-       *list = NULL;
+       int r;
        
-       argbuf = g_ptr_array_new();
-       imap_gen_send(session, "UID SEARCH %s", criteria);
-
-       ok = imap_cmd_ok(session, argbuf);
-       if (ok != IMAP_SUCCESS) {
-               ptr_array_free_strings(argbuf);
-               g_ptr_array_free(argbuf, TRUE);
-               return ok;
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               return -1;
        }
+       
+       r = imap_threaded_expunge(session->folder);
+       if (r != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
+       
+       return IMAP_SUCCESS;
+}
 
-       if ((uidlist = search_array_str(argbuf, "SEARCH ")) != NULL) {
-               gchar **strlist, **p;
+static void imap_path_separator_subst(gchar *str, gchar separator)
+{
+       gchar *p;
+       gboolean in_escape = FALSE;
 
-               strlist = g_strsplit(uidlist + 7, " ", 0);
-               for (p = strlist; *p != NULL; ++p) {
-                       guint msgnum;
+       if (!separator || separator == '/') return;
 
-                       if (sscanf(*p, "%d", &msgnum) == 1)
-                               *list = g_slist_append(*list, GINT_TO_POINTER(msgnum));
-               }
-               g_strfreev(strlist);
+       for (p = str; *p != '\0'; p++) {
+               if (*p == '/' && !in_escape)
+                       *p = separator;
+               else if (*p == '&' && *(p + 1) != '-' && !in_escape)
+                       in_escape = TRUE;
+               else if (*p == '-' && in_escape)
+                       in_escape = FALSE;
        }
-       ptr_array_free_strings(argbuf);
-       g_ptr_array_free(argbuf, TRUE);
-
-       return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_fetch(IMAPSession *session, guint32 uid, const gchar *filename)
+static gchar *imap_modified_utf7_to_utf8(const gchar *mutf7_str)
 {
-       gint ok;
-       gchar *buf;
-       gchar *cur_pos;
-       gchar size_str[32];
-       glong size_num;
-
-       g_return_val_if_fail(filename != NULL, IMAP_ERROR);
+       static iconv_t cd = (iconv_t)-1;
+       static gboolean iconv_ok = TRUE;
+       GString *norm_utf7;
+       gchar *norm_utf7_p;
+       size_t norm_utf7_len;
+       const gchar *p;
+       gchar *to_str, *to_p;
+       size_t to_len;
+       gboolean in_escape = FALSE;
 
-       imap_gen_send(session, "UID FETCH %d BODY.PEEK[]", uid);
+       if (!iconv_ok) return g_strdup(mutf7_str);
 
-       while ((ok = imap_gen_recv(session, &buf))
-              == IMAP_SUCCESS) {
-               if (buf[0] != '*' || buf[1] != ' ') {
-                       g_free(buf);
-                       return IMAP_ERROR;
+       if (cd == (iconv_t)-1) {
+               cd = iconv_open(CS_INTERNAL, CS_UTF_7);
+               if (cd == (iconv_t)-1) {
+                       g_warning("iconv cannot convert UTF-7 to %s\n",
+                                 CS_INTERNAL);
+                       iconv_ok = FALSE;
+                       return g_strdup(mutf7_str);
                }
-               if (strstr(buf, "FETCH") != NULL)
-                       break;
-               g_free(buf);
-       }
-       if (ok != IMAP_SUCCESS) {
-               g_free(buf);
-               return ok;
-       }
-
-       cur_pos = strchr(buf, '{');
-       if (cur_pos == NULL) {
-               g_free(buf);
-               return IMAP_ERROR;
-       }
-
-       cur_pos = strchr_cpy(cur_pos + 1, '}', size_str, sizeof(size_str));
-       if (cur_pos == NULL) {
-               g_free(buf);
-               return IMAP_ERROR;
        }
-       size_num = atol(size_str);
-       g_return_val_if_fail(size_num > 0, IMAP_ERROR);
 
-       if (*cur_pos != '\0') {
-               g_free(buf);
-               return IMAP_ERROR;
-       }
+       /* modified UTF-7 to normal UTF-7 conversion */
+       norm_utf7 = g_string_new(NULL);
 
-       if (recv_bytes_write_to_file(SESSION(session)->sock, size_num, filename) != 0) {
-               g_free(buf);
-               return IMAP_ERROR;
+       for (p = mutf7_str; *p != '\0'; p++) {
+               /* replace: '&'  -> '+',
+                           "&-" -> '&',
+                           escaped ','  -> '/' */
+               if (!in_escape && *p == '&') {
+                       if (*(p + 1) != '-') {
+                               g_string_append_c(norm_utf7, '+');
+                               in_escape = TRUE;
+                       } else {
+                               g_string_append_c(norm_utf7, '&');
+                               p++;
+                       }
+               } else if (in_escape && *p == ',') {
+                       g_string_append_c(norm_utf7, '/');
+               } else if (in_escape && *p == '-') {
+                       g_string_append_c(norm_utf7, '-');
+                       in_escape = FALSE;
+               } else {
+                       g_string_append_c(norm_utf7, *p);
+               }
        }
 
-       if (imap_gen_recv(session, &buf) != IMAP_SUCCESS) {
-               g_free(buf);
-               return IMAP_ERROR;
-       }
+       norm_utf7_p = norm_utf7->str;
+       norm_utf7_len = norm_utf7->len;
+       to_len = strlen(mutf7_str) * 5;
+       to_p = to_str = g_malloc(to_len + 1);
 
-       if (buf[0] == '\0' || buf[strlen(buf) - 1] != ')') {
-               g_free(buf);
-               return IMAP_ERROR;
+       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 %s\n"),
+                         conv_get_locale_charset_str());
+               g_string_free(norm_utf7, TRUE);
+               g_free(to_str);
+               return g_strdup(mutf7_str);
        }
 
-       g_free(buf);
-       ok = imap_cmd_ok(session, NULL);
+       /* second iconv() call for flushing */
+       iconv(cd, NULL, NULL, &to_p, &to_len);
+       g_string_free(norm_utf7, TRUE);
+       *to_p = '\0';
 
-       return ok;
+       return to_str;
 }
 
-static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
-                           const gchar *file, gint32 *new_uid)
+static gchar *imap_utf8_to_modified_utf7(const gchar *from)
 {
-       gint ok;
-       gint size, newuid;
-       gchar *destfolder_;
-       gchar buf[BUFFSIZE], *imapbuf;
-       FILE *fp;
-       GPtrArray *reply;
-       gchar *okmsginfo;
+       static iconv_t cd = (iconv_t)-1;
+       static gboolean iconv_ok = TRUE;
+       gchar *norm_utf7, *norm_utf7_p;
+       size_t from_len, norm_utf7_len;
+       GString *to_str;
+       gchar *from_tmp, *to, *p;
+       gboolean in_escape = FALSE;
 
-       g_return_val_if_fail(file != NULL, IMAP_ERROR);
+       if (!iconv_ok) return g_strdup(from);
 
-       size = get_file_size_as_crlf(file);
-       if ((fp = fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return -1;
+       if (cd == (iconv_t)-1) {
+               cd = iconv_open(CS_UTF_7, CS_INTERNAL);
+               if (cd == (iconv_t)-1) {
+                       g_warning(_("iconv cannot convert %s to UTF-7\n"),
+                                 CS_INTERNAL);
+                       iconv_ok = FALSE;
+                       return g_strdup(from);
+               }
        }
-       QUOTE_IF_REQUIRED(destfolder_, destfolder);
-       imap_gen_send(session, "APPEND %s (\\Seen) {%d}", destfolder_, size);
 
-       ok = imap_gen_recv(session, &imapbuf);
-       if (ok != IMAP_SUCCESS || imapbuf[0] != '+' || imapbuf[1] != ' ') {
-               log_warning(_("can't append %s to %s\n"), file, destfolder_);
-               g_free(imapbuf);
-               fclose(fp);
-               return IMAP_ERROR;
-       }
-       g_free(imapbuf);
+       /* 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;
 
-       log_print("IMAP4> %s\n", _("(sending file...)"));
+#define IS_PRINT(ch) (isprint(ch) && IS_ASCII(ch))
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               strretchomp(buf);
-               if (sock_puts(SESSION(session)->sock, buf) < 0) {
-                       fclose(fp);
-                       return -1;
-               }
-       }
-
-       if (ferror(fp)) {
-               FILE_OP_ERROR(file, "fgets");
-               fclose(fp);
-               return -1;
-       }
-
-       sock_puts(SESSION(session)->sock, "");
+       while (from_len > 0) {
+               if (*from_tmp == '+') {
+                       *norm_utf7_p++ = '+';
+                       *norm_utf7_p++ = '-';
+                       norm_utf7_len -= 2;
+                       from_tmp++;
+                       from_len--;
+               } else if (IS_PRINT(*(guchar *)from_tmp)) {
+                       /* printable ascii char */
+                       *norm_utf7_p = *from_tmp;
+                       norm_utf7_p++;
+                       norm_utf7_len--;
+                       from_tmp++;
+                       from_len--;
+               } else {
+                       size_t conv_len = 0;
 
-       fclose(fp);
+                       /* unprintable char: convert to UTF-7 */
+                       p = from_tmp;
+                       while (!IS_PRINT(*(guchar *)p) && conv_len < from_len) {
+                               conv_len += g_utf8_skip[*(guchar *)p];
+                               p += g_utf8_skip[*(guchar *)p];
+                       }
 
-       reply = g_ptr_array_new();
+                       from_len -= conv_len;
+                       if (iconv(cd, (ICONV_CONST gchar **)&from_tmp,
+                                 &conv_len,
+                                 &norm_utf7_p, &norm_utf7_len) == -1) {
+                               g_warning(_("iconv cannot convert UTF-8 to UTF-7\n"));
+                               return g_strdup(from);
+                       }
 
-       *new_uid = 0;
-       ok = imap_cmd_ok(session, reply);
-       if (ok != IMAP_SUCCESS)
-               log_warning(_("can't append message to %s\n"), destfolder_);
-       else if (
-           (new_uid != NULL) && 
-           (imap_has_capability(session, "UIDPLUS") && reply->len > 0) &&
-           ((okmsginfo = g_ptr_array_index(reply, reply->len - 1)) != NULL) &&
-           (sscanf(okmsginfo, "%*u OK [APPENDUID %*u %u]", &newuid) == 1)) {
-               *new_uid = newuid;
+                       /* second iconv() call for flushing */
+                       iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len);
+               }
        }
 
-       ptr_array_free_strings(reply);
-       g_ptr_array_free(reply, TRUE);
-       return ok;
-}
-
-
-static gint imap_cmd_copy(IMAPSession * session,
-                         gint32 msgnum,
-                         const gchar * destfolder, gint32 * new_uid)
-{
-       gint ok;
-       gint32 olduid, newuid;
-       gchar *okmsginfo;
-       gchar *destfolder_;
-       GPtrArray *reply;
+#undef IS_PRINT
 
-       g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
-       g_return_val_if_fail(session != NULL, IMAP_ERROR);
-       g_return_val_if_fail(new_uid != NULL, IMAP_ERROR);
+       *norm_utf7_p = '\0';
+       to_str = g_string_new(NULL);
+       for (p = norm_utf7; p < norm_utf7_p; p++) {
+               /* replace: '&' -> "&-",
+                           '+' -> '&',
+                           "+-" -> '+',
+                           BASE64 '/' -> ',' */
+               if (!in_escape && *p == '&') {
+                       g_string_append(to_str, "&-");
+               } else if (!in_escape && *p == '+') {
+                       if (*(p + 1) == '-') {
+                               g_string_append_c(to_str, '+');
+                               p++;
+                       } else {
+                               g_string_append_c(to_str, '&');
+                               in_escape = TRUE;
+                       }
+               } else if (in_escape && *p == '/') {
+                       g_string_append_c(to_str, ',');
+               } else if (in_escape && *p == '-') {
+                       g_string_append_c(to_str, '-');
+                       in_escape = FALSE;
+               } else {
+                       g_string_append_c(to_str, *p);
+               }
+       }
 
-       QUOTE_IF_REQUIRED(destfolder_, destfolder);
-       imap_gen_send(session, "UID COPY %d %s", msgnum, destfolder_);
+       if (in_escape) {
+               in_escape = FALSE;
+               g_string_append_c(to_str, '-');
+       }
 
-       reply = g_ptr_array_new();
+       to = to_str->str;
+       g_string_free(to_str, FALSE);
 
-       *new_uid = 0;
-       ok = imap_cmd_ok(session, reply);
-       if (ok != IMAP_SUCCESS)
-               log_warning(_("can't copy %d to %s\n"), msgnum, destfolder_);
-       else if (imap_has_capability(session, "UIDPLUS") && reply->len > 0)
-               if ((okmsginfo = g_ptr_array_index(reply, reply->len - 1)) != NULL &&
-                   sscanf(okmsginfo, "%*u OK [COPYUID %*u %u %u]", &olduid, &newuid) == 2 &&
-                   olduid == msgnum)
-                       *new_uid = newuid;
-
-       ptr_array_free_strings(reply);
-       g_ptr_array_free(reply, TRUE);
-       return ok;
+       return to;
 }
 
-gint imap_cmd_envelope(IMAPSession *session, IMAPSet set)
+static gboolean imap_rename_folder_func(GNode *node, gpointer data)
 {
-       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;
+       FolderItem *item = node->data;
+       gchar **paths = data;
+       const gchar *oldpath = paths[0];
+       const gchar *newpath = paths[1];
+       gchar *base;
+       gchar *new_itempath;
+       gint oldpathlen;
 
-                       g_string_sprintfa(header_fields, "%s%.*s",
-                                       header_fields->str[0] != '\0' ? " " : "",
-                                       namelen, elem->name);
-               }
+       oldpathlen = strlen(oldpath);
+       if (strncmp(oldpath, item->path, oldpathlen) != 0) {
+               g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
+               return TRUE;
        }
 
-       imap_gen_send
-               (session, "UID FETCH %s (UID FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (%s)])",
-                set, header_fields->str);
+       base = item->path + oldpathlen;
+       while (*base == G_DIR_SEPARATOR) base++;
+       if (*base == '\0')
+               new_itempath = g_strdup(newpath);
+       else
+               new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
+                                          NULL);
+       g_free(item->path);
+       item->path = new_itempath;
 
-       return IMAP_SUCCESS;
+       return FALSE;
 }
 
-static gint imap_cmd_store(IMAPSession *session, IMAPSet set,
-                          gchar *sub_cmd)
-{
-       gint ok;
-
-       imap_gen_send(session, "UID STORE %s %s",
-                         set, sub_cmd);
+typedef struct _get_list_uid_data {
+       Folder *folder;
+       IMAPFolderItem *item;
+       GSList **msgnum_list;
+       gboolean done;
+} get_list_uid_data;
 
-       if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS) {
-               log_warning(_("error while imap command: STORE %s %s\n"),
-                           set, sub_cmd);
-               return ok;
+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;
+       struct mailimap_set * set;
+       clist * lep_uidlist;
+       int r;
+       
+       session = imap_session_get(folder);
+       if (session == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
        }
 
-       return IMAP_SUCCESS;
-}
-
-static gint imap_cmd_expunge(IMAPSession *session)
-{
-       gint ok;
+       ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
+                        NULL, NULL, NULL, NULL, TRUE);
+       if (ok != IMAP_SUCCESS) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
+       }
 
-       imap_gen_send(session, "EXPUNGE");
-       if ((ok = imap_cmd_ok(session, NULL)) != IMAP_SUCCESS) {
-               log_warning(_("error while imap command: EXPUNGE\n"));
-               return ok;
+       uidlist = NULL;
+       
+       set = mailimap_set_new_interval(item->lastuid + 1, 0);
+       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, set,
+                                &lep_uidlist);
+       if (r == MAILIMAP_NO_ERROR) {
+               GSList * fetchuid_list;
+               
+               fetchuid_list =
+                       imap_uid_list_from_lep(lep_uidlist);
+               uidlist = g_slist_concat(fetchuid_list, uidlist);
+       }
+       else {
+               GSList * fetchuid_list;
+               carray * lep_uidtab;
+               
+               r = imap_threaded_fetch_uid(folder, item->lastuid + 1,
+                                           &lep_uidtab);
+               if (r == MAILIMAP_NO_ERROR) {
+                       fetchuid_list =
+                               imap_uid_list_from_lep_tab(lep_uidtab);
+                       uidlist = g_slist_concat(fetchuid_list, uidlist);
+               }
        }
+       
+       lastuid_old = item->lastuid;
+       *msgnum_list = g_slist_copy(item->uid_list);
+       nummsgs = g_slist_length(*msgnum_list);
+       debug_print("Got %d uids from cache\n", g_slist_length(item->uid_list));
 
-       return IMAP_SUCCESS;
-}
+       for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
+               guint msgnum;
 
-static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
-{
-       gint ok = IMAP_SUCCESS;
-       gchar *buf;
-       gint cmd_num;
-       gchar *data;
-
-       while ((ok = imap_gen_recv(session, &buf))
-              == IMAP_SUCCESS) {
-               // make sure data is long enough for any substring of buf
-               data = alloca(strlen(buf) + 1);
-
-               // untagged line read
-               if (buf[0] == '*' && buf[1] == ' ') {
-                       gint num;
-                       if (argbuf)
-                               g_ptr_array_add(argbuf, g_strdup(buf + 2));
-
-                       if (sscanf(buf + 2, "%d %s", &num, data) >= 2) {
-                               if (!strcmp(data, "EXISTS")) {
-                                       session->exists = num;
-                                       session->folder_content_changed = TRUE;
-                               }
+               msgnum = GPOINTER_TO_INT(elem->data);
+               if (msgnum > lastuid_old) {
+                       *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
+                       item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
+                       nummsgs++;
 
-                               if(!strcmp(data, "EXPUNGE")) {
-                                       session->exists--;
-                                       session->folder_content_changed = TRUE;
-                               }
-                       }
-               // 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
-               } else {
-                       ok = IMAP_ERROR;
-                       break;
+                       if(msgnum > item->lastuid)
+                               item->lastuid = msgnum;
                }
-               g_free(buf);
        }
-       g_free(buf);
+       g_slist_free(uidlist);
 
-       return ok;
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(nummsgs);
 }
 
-static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
+static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
 {
-       gchar *buf;
-       gchar *tmp;
-       gchar *p;
-       va_list args;
-
-       va_start(args, format);
-       tmp = g_strdup_vprintf(format, args);
-       va_end(args);
+       gint result;
+       get_list_uid_data *data = g_new0(get_list_uid_data, 1);
+       data->done = FALSE;
+       data->folder = folder;
+       data->item = item;
+       data->msgnum_list = msgnum_list;
 
-       session->cmd_count++;
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
+       }
 
-       buf = g_strdup_printf("%d %s\r\n", session->cmd_count, tmp);
-       if (!strncasecmp(tmp, "LOGIN ", 6) && (p = strchr(tmp + 6, ' '))) {
-               *p = '\0';
-               log_print("IMAP4> %d %s ********\n", session->cmd_count, tmp);
-       } else
-               log_print("IMAP4> %d %s\n", session->cmd_count, tmp);
+       result = GPOINTER_TO_INT(get_list_of_uids_thread(data));
+       g_free(data);
+       return result;
 
-       sock_write_all(SESSION(session)->sock, buf, strlen(buf));
-       g_free(tmp);
-       g_free(buf);
 }
 
-static gint imap_gen_recv(IMAPSession *session, gchar **buf)
+gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list, gboolean *old_uids_valid)
 {
-       if ((*buf = sock_getline(SESSION(session)->sock)) == NULL)
-               return IMAP_SOCKET;
-
-       strretchomp(*buf);
+       IMAPFolderItem *item = (IMAPFolderItem *)_item;
+       IMAPSession *session;
+       gint ok, nummsgs = 0, exists, recent, uid_val, uid_next, unseen;
+       GSList *uidlist = NULL;
+       gchar *dir;
+       gboolean selected_folder;
+       
+       debug_print("get_num_list\n");
+       
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(item != NULL, -1);
+       g_return_val_if_fail(item->item.path != NULL, -1);
+       g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
+       g_return_val_if_fail(folder->account != NULL, -1);
 
-       log_print("IMAP4< %s\n", *buf);
+       MUTEX_TRYLOCK_OR_RETURN_VAL(-1);
 
-       return IMAP_SUCCESS;
-}
+       session = imap_session_get(folder);
+       g_return_val_if_fail(session != NULL, -1);
 
+       selected_folder = (session->mbox != NULL) &&
+                         (!strcmp(session->mbox, item->item.path));
+       if (selected_folder) {
+               ok = imap_cmd_noop(session);
+               if (ok != IMAP_SUCCESS) {
+                       MUTEX_UNLOCK();
+                       return -1;
+               }
+               exists = session->exists;
 
-/* misc utility functions */
+               *old_uids_valid = TRUE;
+       } else {
+               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
+                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
+               if (ok != IMAP_SUCCESS) {
+                       MUTEX_UNLOCK();
+                       return -1;
+               }
+               if(item->item.mtime == uid_val)
+                       *old_uids_valid = TRUE;
+               else {
+                       *old_uids_valid = FALSE;
 
-static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)
-{
-       gchar *tmp;
+                       debug_print("Freeing imap uid cache\n");
+                       item->lastuid = 0;
+                       g_slist_free(item->uid_list);
+                       item->uid_list = NULL;
+               
+                       item->item.mtime = uid_val;
 
-       dest[0] = '\0';
-       tmp = strchr(src, ch);
-       if (!tmp)
-               return NULL;
+                       imap_delete_all_cached_messages((FolderItem *)item);
+               }
+       }
 
-       memcpy(dest, src, MIN(tmp - src, len - 1));
-       dest[MIN(tmp - src, len - 1)] = '\0';
+       if (!selected_folder)
+               item->uid_next = uid_next;
 
-       return tmp + 1;
-}
+       /* If old uid_next matches new uid_next we can be sure no message
+          was added to the folder */
+       if (( selected_folder && !session->folder_content_changed) ||
+           (!selected_folder && uid_next == item->uid_next)) {
+               nummsgs = g_slist_length(item->uid_list);
 
-static gchar *get_quoted(const gchar *src, gchar ch, gchar *dest, gint len)
-{
-       const gchar *p = src;
-       gint n = 0;
+               /* If number of messages is still the same we
+                   know our caches message numbers are still valid,
+                   otherwise if the number of messages has decrease
+                  we discard our cache to start a new scan to find
+                  out which numbers have been removed */
+               if (exists == nummsgs) {
+                       *msgnum_list = g_slist_copy(item->uid_list);
+                       MUTEX_UNLOCK();
+                       return nummsgs;
+               } else if (exists < nummsgs) {
+                       debug_print("Freeing imap uid cache");
+                       item->lastuid = 0;
+                       g_slist_free(item->uid_list);
+                       item->uid_list = NULL;
+               }
+       }
 
-       g_return_val_if_fail(*p == ch, NULL);
+       if (exists == 0) {
+               *msgnum_list = NULL;
+               MUTEX_UNLOCK();
+               return 0;
+       }
 
-       *dest = '\0';
-       p++;
+       nummsgs = get_list_of_uids(folder, item, &uidlist);
 
-       while (*p != '\0' && *p != ch) {
-               if (n < len - 1) {
-                       if (*p == '\\' && *(p + 1) != '\0')
-                               p++;
-                       *dest++ = *p++;
-               } else
-                       p++;
-               n++;
+       if (nummsgs < 0) {
+               MUTEX_UNLOCK();
+               return -1;
        }
 
-       *dest = '\0';
-       return (gchar *)(*p == ch ? p + 1 : p);
-}
-
-static gchar *search_array_contain_str(GPtrArray *array, const gchar *str)
-{
-       gint i;
+       if (nummsgs != exists) {
+               /* Cache contains more messages then folder, we have cached
+                   an old UID of a message that was removed and new messages
+                   have been added too, otherwise the uid_next check would
+                  not have failed */
+               debug_print("Freeing imap uid cache");
+               item->lastuid = 0;
+               g_slist_free(item->uid_list);
+               item->uid_list = NULL;
 
-       for (i = 0; i < array->len; i++) {
-               gchar *tmp;
+               g_slist_free(*msgnum_list);
 
-               tmp = g_ptr_array_index(array, i);
-               if (strstr(tmp, str) != NULL)
-                       return tmp;
+               nummsgs = get_list_of_uids(folder, item, &uidlist);
        }
 
-       return NULL;
+       *msgnum_list = uidlist;
+
+       dir = folder_item_get_path((FolderItem *)item);
+       debug_print("removing old messages from %s\n", dir);
+       remove_numbered_files_not_in_list(dir, *msgnum_list);
+       g_free(dir);
+       MUTEX_UNLOCK();
+       
+       debug_print("get_num_list - ok - %i\n", nummsgs);
+       
+       return nummsgs;
 }
 
-static gchar *search_array_str(GPtrArray *array, const gchar *str)
+static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
 {
-       gint i;
-       gint len;
+       MsgInfo *msginfo;
+       MsgFlags flags;
 
-       len = strlen(str);
+       flags.perm_flags = MSG_NEW|MSG_UNREAD;
+       flags.tmp_flags = 0;
 
-       for (i = 0; i < array->len; i++) {
-               gchar *tmp;
+       g_return_val_if_fail(item != NULL, NULL);
+       g_return_val_if_fail(file != NULL, NULL);
 
-               tmp = g_ptr_array_index(array, i);
-               if (!strncmp(tmp, str, len))
-                       return tmp;
+       if (item->stype == F_QUEUE) {
+               MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
+       } else if (item->stype == F_DRAFT) {
+               MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
 
-       return NULL;
+       msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
+       if (!msginfo) return NULL;
+       
+       msginfo->plaintext_file = g_strdup(file);
+       msginfo->folder = item;
+
+       return msginfo;
 }
 
-static void imap_path_separator_subst(gchar *str, gchar separator)
+GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
+                         GSList *msgnum_list)
 {
-       gchar *p;
-       gboolean in_escape = FALSE;
+       IMAPSession *session;
+       MsgInfoList *ret = NULL;
+       gint ok;
+       
+       debug_print("get_msginfos\n");
+       
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(item != NULL, NULL);
+       g_return_val_if_fail(msgnum_list != NULL, NULL);
 
-       if (!separator || separator == '/') return;
+       session = imap_session_get(folder);
+       g_return_val_if_fail(session != NULL, NULL);
 
-       for (p = str; *p != '\0'; p++) {
-               if (*p == '/' && !in_escape)
-                       *p = separator;
-               else if (*p == '&' && *(p + 1) != '-' && !in_escape)
-                       in_escape = TRUE;
-               else if (*p == '-' && in_escape)
-                       in_escape = FALSE;
-       }
-}
+       debug_print("IMAP getting msginfos\n");
+       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+                        NULL, NULL, NULL, NULL, FALSE);
+       if (ok != IMAP_SUCCESS)
+               return NULL;
 
-static gchar *imap_modified_utf7_to_locale(const gchar *mutf7_str)
-{
-#if !HAVE_ICONV
-       const gchar *from_p;
-       gchar *to, *to_p;
+       if (!(item->stype == F_QUEUE || item->stype == F_DRAFT)) {
+               ret = g_slist_concat(ret,
+                       imap_get_uncached_messages(session, item,
+                                                  msgnum_list));
+       } else {
+               MsgNumberList *sorted_list, *elem;
+               gint startnum, lastnum;
 
-       to = g_malloc(strlen(mutf7_str) + 1);
-       to_p = to;
+               sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
 
-       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';
+               startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
 
-       return to;
-#else
-       static iconv_t cd = (iconv_t)-1;
-       static gboolean iconv_ok = TRUE;
-       GString *norm_utf7;
-       gchar *norm_utf7_p;
-       size_t norm_utf7_len;
-       const gchar *p;
-       gchar *to_str, *to_p;
-       size_t to_len;
-       gboolean in_escape = FALSE;
+               for (elem = sorted_list;; elem = g_slist_next(elem)) {
+                       guint num = 0;
 
-       if (!iconv_ok) return g_strdup(mutf7_str);
+                       if (elem)
+                               num = GPOINTER_TO_INT(elem->data);
 
-       if (cd == (iconv_t)-1) {
-               cd = iconv_open(conv_get_current_charset_str(), "UTF-7");
-               if (cd == (iconv_t)-1) {
-                       g_warning("iconv cannot convert UTF-7 to %s\n",
-                                 conv_get_current_charset_str());
-                       iconv_ok = FALSE;
-                       return g_strdup(mutf7_str);
-               }
-       }
+                       if (num > lastnum + 1 || elem == NULL) {
+                               int i;
+                               for (i = startnum; i <= lastnum; ++i) {
+                                       gchar *file;
+                       
+                                       file = imap_fetch_msg(folder, item, i);
+                                       if (file != NULL) {
+                                               MsgInfo *msginfo = imap_parse_msg(file, item);
+                                               if (msginfo != NULL) {
+                                                       msginfo->msgnum = i;
+                                                       ret = g_slist_append(ret, msginfo);
+                                               }
+                                               g_free(file);
+                                       }
+                               }
 
-       norm_utf7 = g_string_new(NULL);
+                               if (elem == NULL)
+                                       break;
 
-       for (p = mutf7_str; *p != '\0'; p++) {
-               /* replace: '&'  -> '+',
-                           "&-" -> '&',
-                           escaped ','  -> '/' */
-               if (!in_escape && *p == '&') {
-                       if (*(p + 1) != '-') {
-                               g_string_append_c(norm_utf7, '+');
-                               in_escape = TRUE;
-                       } else {
-                               g_string_append_c(norm_utf7, '&');
-                               p++;
+                               startnum = num;
                        }
-               } else if (in_escape && *p == ',') {
-                       g_string_append_c(norm_utf7, '/');
-               } else if (in_escape && *p == '-') {
-                       g_string_append_c(norm_utf7, '-');
-                       in_escape = FALSE;
-               } else {
-                       g_string_append_c(norm_utf7, *p);
+                       lastnum = num;
                }
-       }
-
-       norm_utf7_p = norm_utf7->str;
-       norm_utf7_len = norm_utf7->len;
-       to_len = strlen(mutf7_str) * 5;
-       to_p = to_str = g_malloc(to_len + 1);
 
-       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 %s\n"),
-                         conv_get_current_charset_str());
-               g_string_free(norm_utf7, TRUE);
-               g_free(to_str);
-               return g_strdup(mutf7_str);
+               g_slist_free(sorted_list);
        }
 
-       /* second iconv() call for flushing */
-       iconv(cd, NULL, NULL, &to_p, &to_len);
-       g_string_free(norm_utf7, TRUE);
-       *to_p = '\0';
-
-       return to_str;
-#endif /* !HAVE_ICONV */
+       return ret;
 }
 
-static gchar *imap_locale_to_modified_utf7(const gchar *from)
+MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
 {
-#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;
-       size_t from_len, norm_utf7_len;
-       GString *to_str;
-       gchar *from_tmp, *to, *p;
-       gboolean in_escape = FALSE;
-
-       if (!iconv_ok) return g_strdup(from);
-
-       if (cd == (iconv_t)-1) {
-               cd = iconv_open("UTF-7", conv_get_current_charset_str());
-               if (cd == (iconv_t)-1) {
-                       g_warning("iconv cannot convert %s to UTF-7\n",
-                                 conv_get_current_charset_str());
-                       iconv_ok = FALSE;
-                       return g_strdup(from);
-               }
-       }
-
-       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))
-
-       while (from_len > 0) {
-               if (*from_tmp == '+') {
-                       *norm_utf7_p++ = '+';
-                       *norm_utf7_p++ = '-';
-                       norm_utf7_len -= 2;
-                       from_tmp++;
-                       from_len--;
-               } else if (IS_PRINT(*from_tmp)) {
-                       /* printable ascii char */
-                       *norm_utf7_p = *from_tmp;
-                       norm_utf7_p++;
-                       norm_utf7_len--;
-                       from_tmp++;
-                       from_len--;
-               } else {
-                       size_t mb_len = 0, conv_len = 0;
-
-                       /* unprintable char: convert to UTF-7 */
-                       p = from_tmp;
-                       while (!IS_PRINT(*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;
-                       }
+       MsgInfo *msginfo = NULL;
+       MsgInfoList *msginfolist;
+       MsgNumberList numlist;
 
-                       from_len -= conv_len;
-                       if (iconv(cd, (ICONV_CONST gchar **)&from_tmp,
-                                 &conv_len,
-                                 &norm_utf7_p, &norm_utf7_len) == -1) {
-                               g_warning("iconv cannot convert %s to UTF-7\n",
-                                         conv_get_current_charset_str());
-                               return g_strdup(from);
-                       }
+       numlist.next = NULL;
+       numlist.data = GINT_TO_POINTER(uid);
 
-                       /* second iconv() call for flushing */
-                       iconv(cd, NULL, NULL, &norm_utf7_p, &norm_utf7_len);
-               }
+       msginfolist = imap_get_msginfos(folder, item, &numlist);
+       if (msginfolist != NULL) {
+               msginfo = msginfolist->data;
+               g_slist_free(msginfolist);
        }
 
-#undef IS_PRINT
+       return msginfo;
+}
 
-       *norm_utf7_p = '\0';
-       to_str = g_string_new(NULL);
-       for (p = norm_utf7; p < norm_utf7_p; p++) {
-               /* replace: '&' -> "&-",
-                           '+' -> '&',
-                           "+-" -> '+',
-                           BASE64 '/' -> ',' */
-               if (!in_escape && *p == '&') {
-                       g_string_append(to_str, "&-");
-               } else if (!in_escape && *p == '+') {
-                       if (*(p + 1) == '-') {
-                               g_string_append_c(to_str, '+');
-                               p++;
-                       } else {
-                               g_string_append_c(to_str, '&');
-                               in_escape = TRUE;
-                       }
-               } else if (in_escape && *p == '/') {
-                       g_string_append_c(to_str, ',');
-               } else if (in_escape && *p == '-') {
-                       g_string_append_c(to_str, '-');
-                       in_escape = FALSE;
-               } else {
-                       g_string_append_c(to_str, *p);
-               }
-       }
+gboolean imap_scan_required(Folder *folder, FolderItem *_item)
+{
+       IMAPSession *session;
+       IMAPFolderItem *item = (IMAPFolderItem *)_item;
+       gint ok, exists = 0, recent = 0, unseen = 0;
+       guint32 uid_next, uid_val = 0;
+       gboolean selected_folder;
+       
+       g_return_val_if_fail(folder != NULL, FALSE);
+       g_return_val_if_fail(item != NULL, FALSE);
+       g_return_val_if_fail(item->item.folder != NULL, FALSE);
+       g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
 
-       if (in_escape) {
-               in_escape = FALSE;
-               g_string_append_c(to_str, '-');
-       }
+       if (item->item.path == NULL)
+               return FALSE;
 
-       to = to_str->str;
-       g_string_free(to_str, FALSE);
+       session = imap_session_get(folder);
+       g_return_val_if_fail(session != NULL, FALSE);
 
-       return to;
-#endif /* !HAVE_ICONV */
-}
+       selected_folder = (session->mbox != NULL) &&
+                         (!strcmp(session->mbox, item->item.path));
+       if (selected_folder) {
+               ok = imap_cmd_noop(session);
+               if (ok != IMAP_SUCCESS)
+                       return FALSE;
 
-static gboolean imap_rename_folder_func(GNode *node, gpointer data)
-{
-       FolderItem *item = node->data;
-       gchar **paths = data;
-       const gchar *oldpath = paths[0];
-       const gchar *newpath = paths[1];
-       gchar *base;
-       gchar *new_itempath;
-       gint oldpathlen;
+               if (session->folder_content_changed
+               ||  session->exists != item->item.total_msgs)
+                       return TRUE;
+       } else {
+               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
+                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
+               if (ok != IMAP_SUCCESS)
+                       return FALSE;
 
-       oldpathlen = strlen(oldpath);
-       if (strncmp(oldpath, item->path, oldpathlen) != 0) {
-               g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
-               return TRUE;
+               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs))
+                       return TRUE;
        }
 
-       base = item->path + oldpathlen;
-       while (*base == G_DIR_SEPARATOR) base++;
-       if (*base == '\0')
-               new_itempath = g_strdup(newpath);
-       else
-               new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
-                                          NULL);
-       g_free(item->path);
-       item->path = new_itempath;
-
        return FALSE;
 }
 
-static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
+void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
 {
-       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);
+       IMAPFlags flags_set = 0, flags_unset = 0;
+       gint ok = IMAP_SUCCESS;
+       MsgNumberList numlist;
+       hashtable_data *ht_data = NULL;
 
-       ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
-               return -1;
+       g_return_if_fail(folder != NULL);
+       g_return_if_fail(folder->klass == &imap_class);
+       g_return_if_fail(item != NULL);
+       g_return_if_fail(item->folder == folder);
+       g_return_if_fail(msginfo != NULL);
+       g_return_if_fail(msginfo->folder == item);
 
-       cmd_buf = g_strdup_printf("UID %d:*", item->lastuid + 1);
-       ok = imap_cmd_search(session, cmd_buf, &uidlist);
-       g_free(cmd_buf);
+       MUTEX_TRYLOCK_OR_RETURN();
 
-       if (ok == IMAP_SOCKET) {
-               session_destroy((Session *)session);
-               ((RemoteFolder *)folder)->session = NULL;
-               return -1;
+       session = imap_session_get(folder);
+       if (!session) {
+               MUTEX_UNLOCK();
+               return;
+       }
+       if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
+           NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
+               MUTEX_UNLOCK();
+               return;
        }
 
-       if (ok != IMAP_SUCCESS) {
-               gint i;
-               GPtrArray *argbuf;
-
-               argbuf = g_ptr_array_new();
+       if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
+               flags_set |= IMAP_FLAG_FLAGGED;
+       if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
+               flags_unset |= IMAP_FLAG_FLAGGED;
 
-               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);
-               if (ok != IMAP_SUCCESS) {
-                       ptr_array_free_strings(argbuf);
-                       g_ptr_array_free(argbuf, TRUE);
-                       return -1;
-               }
-       
-               for(i = 0; i < argbuf->len; i++) {
-                       int ret, msgnum;
-       
-                       if((ret = sscanf(g_ptr_array_index(argbuf, i), 
-                                   "%*d FETCH (UID %d)", &msgnum)) == 1)
-                               uidlist = g_slist_prepend(uidlist, GINT_TO_POINTER(msgnum));
-               }
-               ptr_array_free_strings(argbuf);
-               g_ptr_array_free(argbuf, TRUE);
-       }
+       if (!MSG_IS_UNREAD(msginfo->flags) &&  (newflags & MSG_UNREAD))
+               flags_unset |= IMAP_FLAG_SEEN;
+       if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
+               flags_set |= IMAP_FLAG_SEEN;
 
-       lastuid_old = item->lastuid;
-       *msgnum_list = g_slist_copy(item->uid_list);
-       nummsgs = g_slist_length(*msgnum_list);
-       debug_print("Got %d uids from cache\n", g_slist_length(item->uid_list));
+       if (!MSG_IS_REPLIED(msginfo->flags) &&  (newflags & MSG_REPLIED))
+               flags_set |= IMAP_FLAG_ANSWERED;
+       if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
+               flags_set |= IMAP_FLAG_ANSWERED;
 
-       for (elem = uidlist; elem != NULL; elem = g_slist_next(elem)) {
-               guint msgnum;
+       numlist.next = NULL;
+       numlist.data = GINT_TO_POINTER(msginfo->msgnum);
 
-               msgnum = GPOINTER_TO_INT(elem->data);
-               if (msgnum > lastuid_old) {
-                       *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(msgnum));
-                       item->uid_list = g_slist_prepend(item->uid_list, GINT_TO_POINTER(msgnum));
-                       nummsgs++;
+       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) {
+                               MUTEX_UNLOCK();
+                               return;
+                       }
+               }
 
-                       if(msgnum > item->lastuid)
-                               item->lastuid = msgnum;
+               if (flags_unset) {
+                       ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
+                       if (ok != IMAP_SUCCESS) {
+                               MUTEX_UNLOCK();
+                               return;
+                       }
                }
        }
-       g_slist_free(uidlist);
-
-       return nummsgs;
+       msginfo->flags.perm_flags = newflags;
+       
+       MUTEX_UNLOCK();
+       return;
 }
 
-gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list)
+static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
 {
-       IMAPFolderItem *item = (IMAPFolderItem *)_item;
+       gint ok;
        IMAPSession *session;
-       gint ok, nummsgs = 0, exists, recent, unseen, uid_val, uid_next;
-       GSList *uidlist;
        gchar *dir;
-       gboolean selected_folder;
-
+       MsgNumberList numlist;
+       
        g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(item != NULL, -1);
-       g_return_val_if_fail(item->item.path != NULL, -1);
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
-       g_return_val_if_fail(folder->account != NULL, -1);
+       g_return_val_if_fail(item != NULL, -1);
 
        session = imap_session_get(folder);
-       g_return_val_if_fail(session != NULL, -1);
-
-       selected_folder = (session->mbox != NULL) &&
-                         (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
-               ok = imap_cmd_noop(session);
-               if (ok != IMAP_SUCCESS)
-                       return -1;
-               exists = session->exists;
-       } else {
-               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                                &exists, &recent, &uid_next, &uid_val, &unseen);
-               if (ok != IMAP_SUCCESS)
-                       return -1;
-       }
-
-       /* If old uid_next matches new uid_next we can be sure no message
-          was added to the folder */
-       if (( selected_folder && !session->folder_content_changed) ||
-           (!selected_folder && uid_next == item->uid_next)) {
-               nummsgs = g_slist_length(item->uid_list);
+       if (!session) return -1;
 
-               /* If number of messages is still the same we
-                   know our caches message numbers are still valid,
-                   otherwise if the number of messages has decrease
-                  we discard our cache to start a new scan to find
-                  out which numbers have been removed */
-               if (exists == nummsgs) {
-                       *msgnum_list = g_slist_copy(item->uid_list);
-                       return nummsgs;
-               } else if (exists < nummsgs) {
-                       debug_print("Freeing imap uid cache");
-                       item->lastuid = 0;
-                       g_slist_free(item->uid_list);
-                       item->uid_list = NULL;
-               }
-       }
-       if (!selected_folder)
-               item->uid_next = uid_next;
+       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+                        NULL, NULL, NULL, NULL, FALSE);
+       if (ok != IMAP_SUCCESS)
+               return ok;
 
-       if (exists == 0) {
-               *msgnum_list = NULL;
-               return 0;
+       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;
        }
 
-       nummsgs = get_list_of_uids(folder, item, &uidlist);
-
-       if (nummsgs != exists) {
-               /* Cache contains more messages then folder, we have cached
-                   an old UID of a message that was removed and new messages
-                   have been added too, otherwise the uid_next check would
-                  not have failed */
-               debug_print("Freeing imap uid cache");
-               item->lastuid = 0;
-               g_slist_free(item->uid_list);
-               item->uid_list = NULL;
-
-               g_slist_free(*msgnum_list);
+       if (!session->uidplus) {
+               ok = imap_cmd_expunge(session);
+       } else {
+               gchar *uidstr;
 
-               nummsgs = get_list_of_uids(folder, item, &uidlist);
+               uidstr = g_strdup_printf("%u", uid);
+               ok = imap_cmd_expunge(session);
+               g_free(uidstr);
+       }
+       if (ok != IMAP_SUCCESS) {
+               log_warning(_("can't expunge\n"));
+               return ok;
        }
 
-       *msgnum_list = uidlist;
-
-       dir = folder_item_get_path((FolderItem *)item);
-       debug_print("removing old messages from %s\n", dir);
-       remove_numbered_files_not_in_list(dir, *msgnum_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);
 
-       return nummsgs;
+       return IMAP_SUCCESS;
 }
 
-static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
+static gint compare_msginfo(gconstpointer a, gconstpointer b)
 {
-       MsgInfo *msginfo;
-       MsgFlags flags;
-
-       flags.perm_flags = MSG_NEW|MSG_UNREAD;
-       flags.tmp_flags = 0;
-
-       g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(file != NULL, NULL);
-
-       if (item->stype == F_QUEUE) {
-               MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
-               MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
-       }
+       return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
+}
 
-       msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
-       if (!msginfo) return NULL;
+static guint gslist_find_next_num(MsgNumberList **list, guint num)
+{
+       GSList *elem;
 
-       msginfo->folder = item;
+       g_return_val_if_fail(list != NULL, -1);
 
-       return msginfo;
+       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;
 }
 
-GSList *imap_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
-{
+/*
+ * 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;
-       MsgInfoList *ret = NULL;
+       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;
-
-       g_return_val_if_fail(folder != NULL, NULL);
-       g_return_val_if_fail(item != NULL, NULL);
-       g_return_val_if_fail(msgnum_list != NULL, NULL);
+       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);
-       g_return_val_if_fail(session != NULL, NULL);
+       if (session == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(-1);
+       }
 
-       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
-                        NULL, NULL, NULL, NULL);
-       if (ok != IMAP_SUCCESS)
-               return NULL;
+       selected_folder = (session->mbox != NULL) &&
+                         (!strcmp(session->mbox, item->path));
 
-       if (!(item->stype == F_QUEUE || item->stype == F_DRAFT)) {
-               ret = g_slist_concat(ret,
-                       imap_get_uncached_messages(
-                       session, item, msgnum_list));
-       } else {
-               MsgNumberList *sorted_list, *elem;
-               gint startnum, lastnum;
+       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);
+               }
 
-               sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
+               if (unseen_cnt > exists_cnt / 2)
+                       reverse_seen = TRUE;
+       } 
+       else {
+               if (item->unread_msgs > item->total_msgs / 2)
+                       reverse_seen = TRUE;
+       }
 
-               startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
+       cmd_buf = g_string_new(NULL);
 
-               for (elem = sorted_list;; elem = g_slist_next(elem)) {
-                       guint num;
+       sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
 
-                       if (elem)
-                               num = GPOINTER_TO_INT(elem->data);
+       seq_list = imap_get_lep_set_from_msglist(msginfo_list);
 
-                       if (num > lastnum + 1 || elem == NULL) {
-                               int i;
-                               for (i = startnum; i <= lastnum; ++i) {
-                                       gchar *file;
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               struct mailimap_set * imapset;
+               clist * lep_uidlist;
+               int r;
+               
+               imapset = cur->data;
+               if (reverse_seen) {
+                       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
+                                                imapset, &lep_uidlist);
+               }
+               else {
+                       r = imap_threaded_search(folder,
+                                                IMAP_SEARCH_TYPE_UNSEEN,
+                                                imapset, &lep_uidlist);
+               }
+               if (r == MAILIMAP_NO_ERROR) {
+                       GSList * uidlist;
                        
-                                       file = imap_fetch_msg(folder, item, i);
-                                       if (file != NULL) {
-                                               MsgInfo *msginfo = imap_parse_msg(file, item);
-                                               if (msginfo != NULL) {
-                                                       msginfo->msgnum = i;
-                                                       ret = g_slist_append(ret, msginfo);
-                                               }
-                                               g_free(file);
-                                       }
-                               }
+                       uidlist = imap_uid_list_from_lep(lep_uidlist);
+                       mailimap_search_result_free(lep_uidlist);
+                       
+                       unseen = g_slist_concat(unseen, uidlist);
+               }
+               
+               r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
+                                        imapset, &lep_uidlist);
+               if (r == MAILIMAP_NO_ERROR) {
+                       GSList * uidlist;
+                       
+                       uidlist = imap_uid_list_from_lep(lep_uidlist);
+                       mailimap_search_result_free(lep_uidlist);
+                       
+                       answered = g_slist_concat(answered, uidlist);
+               }
 
-                               if (elem == NULL)
-                                       break;
+               r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
+                                        imapset, &lep_uidlist);
+               if (r == MAILIMAP_NO_ERROR) {
+                       GSList * uidlist;
+                       
+                       uidlist = imap_uid_list_from_lep(lep_uidlist);
+                       mailimap_search_result_free(lep_uidlist);
+                       
+                       flagged = g_slist_concat(flagged, uidlist);
+               }
+       }
 
-                               startnum = num;
+       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);
                        }
-                       lastnum = num;
                }
-
-               g_slist_free(sorted_list);
+               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));
        }
 
-       return ret;
+       imap_lep_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);
 }
 
-MsgInfo *imap_get_msginfo(Folder *folder, FolderItem *item, gint uid)
+static gint imap_get_flags(Folder *folder, FolderItem *item,
+                           MsgInfoList *msginfo_list, GRelation *msgflags)
 {
-       MsgInfo *msginfo = NULL;
-       MsgInfoList *msginfolist;
-       MsgNumberList numlist;
+       gint result;
+       get_flags_data *data = g_new0(get_flags_data, 1);
+       data->done = FALSE;
+       data->folder = folder;
+       data->item = item;
+       data->msginfo_list = msginfo_list;
+       data->msgflags = msgflags;
 
-       numlist.next = NULL;
-       numlist.data = GINT_TO_POINTER(uid);
-
-       msginfolist = imap_get_msginfos(folder, item, &numlist);
-       if (msginfolist != NULL) {
-               msginfo = msginfolist->data;
-               g_slist_free(msginfolist);
+       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               g_free(data);
+               return -1;
        }
 
-       return msginfo;
+       result = GPOINTER_TO_INT(imap_get_flags_thread(data));
+       
+       g_free(data);
+       return result;
+
 }
 
-gboolean imap_check_msgnum_validity(Folder *folder, FolderItem *_item)
+static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
 {
-       IMAPSession *session;
-       IMAPFolderItem *item = (IMAPFolderItem *)_item;
-       gint ok, exists = 0, recent = 0, unseen = 0;
-       guint32 uid_next, uid_validity = 0;
+       gboolean flags_set = GPOINTER_TO_INT(user_data);
+       gint flags_value = GPOINTER_TO_INT(key);
+       hashtable_data *data = (hashtable_data *)value;
        
-       g_return_val_if_fail(folder != NULL, FALSE);
-       g_return_val_if_fail(item != NULL, FALSE);
-       g_return_val_if_fail(item->item.folder != NULL, FALSE);
-       g_return_val_if_fail(FOLDER_CLASS(item->item.folder) == &imap_class, FALSE);
+       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;
+}
 
-       session = imap_session_get(folder);
-       g_return_val_if_fail(session != NULL, FALSE);
+static void process_hashtable(void)
+{
+       MUTEX_TRYLOCK_OR_RETURN();
+       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;
+       }
+       MUTEX_UNLOCK();
+}
 
-       ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                        &exists, &recent, &uid_next, &uid_validity, &unseen);
-       if (ok != IMAP_SUCCESS)
-               return FALSE;
+static IMAPFolderItem *batching_item = NULL;
 
-       if(item->item.mtime == uid_validity)
-               return TRUE;
+static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
+{
+       IMAPFolderItem *item = (IMAPFolderItem *)_item;
 
-       debug_print("Freeing imap uid cache\n");
-       item->lastuid = 0;
-       g_slist_free(item->uid_list);
-       item->uid_list = NULL;
+       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();
+       }
+}
+
+
+
+/* data types conversion libetpan <-> sylpheed */
+
+
+
+#define ETPAN_IMAP_MB_MARKED      1
+#define ETPAN_IMAP_MB_UNMARKED    2
+#define ETPAN_IMAP_MB_NOSELECT    4
+#define ETPAN_IMAP_MB_NOINFERIORS 8
+
+static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
+{
+  int flags;
+  clistiter * cur;
+  
+  flags = 0;
+  if (imap_flags->mbf_type == MAILIMAP_MBX_LIST_FLAGS_SFLAG) {
+    switch (imap_flags->mbf_sflag) {
+    case MAILIMAP_MBX_LIST_SFLAG_MARKED:
+      flags |= ETPAN_IMAP_MB_MARKED;
+      break;
+    case MAILIMAP_MBX_LIST_SFLAG_NOSELECT:
+      flags |= ETPAN_IMAP_MB_NOSELECT;
+      break;
+    case MAILIMAP_MBX_LIST_SFLAG_UNMARKED:
+      flags |= ETPAN_IMAP_MB_UNMARKED;
+      break;
+    }
+  }
+  
+  for(cur = clist_begin(imap_flags->mbf_oflags) ; cur != NULL ;
+      cur = clist_next(cur)) {
+    struct mailimap_mbx_list_oflag * oflag;
+    
+    oflag = clist_content(cur);
+    
+    switch (oflag->of_type) {
+    case MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS:
+      flags |= ETPAN_IMAP_MB_NOINFERIORS;
+      break;
+    }
+  }
+  
+  return flags;
+}
+
+static GSList * imap_list_from_lep(IMAPFolder * folder,
+                                  clist * list, const gchar * real_path)
+{
+       clistiter * iter;
+       GSList * item_list;
+       
+       item_list = NULL;
+       
+       for(iter = clist_begin(list) ; iter != NULL ;
+           iter = clist_next(iter)) {
+               struct mailimap_mailbox_list * mb;
+               int flags;
+               char delimiter;
+               char * name;
+               char * dup_name;
+               gchar * base;
+               gchar * loc_name;
+               gchar * loc_path;
+               FolderItem *new_item;
                
-       item->item.mtime = uid_validity;
-
-       imap_delete_all_cached_messages((FolderItem *)item);
-
-       return FALSE;
+               mb = clist_content(iter);
+               
+               flags = 0;
+               if (mb->mb_flag != NULL)
+                       flags = imap_flags_to_flags(mb->mb_flag);
+               
+               delimiter = mb->mb_delimiter;
+               name = mb->mb_name;
+               
+               dup_name = strdup(name);                
+               if (delimiter != '\0')
+                       subst_char(dup_name, delimiter, '/');
+               
+               base = g_path_get_basename(dup_name);
+               if (base[0] == '.') {
+                       g_free(base);
+                       free(dup_name);
+                       continue;
+               }
+               
+               if (strcmp(dup_name, real_path) == 0) {
+                       g_free(base);
+                       free(dup_name);
+                       continue;
+               }
+               
+               loc_name = imap_modified_utf7_to_utf8(base);
+               loc_path = imap_modified_utf7_to_utf8(dup_name);
+               
+               new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
+               if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
+                       new_item->no_sub = TRUE;
+               if (strcmp(dup_name, "INBOX") != 0 &&
+                   ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
+                       new_item->no_select = TRUE;
+               
+               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);
+               
+               free(dup_name);
+       }
+       
+       return item_list;
 }
 
-void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPermFlags newflags)
+static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
 {
-       IMAPSession *session;
-       IMAPFlags flags_set = 0, flags_unset = 0;
-       gint ok = IMAP_SUCCESS;
-       MsgNumberList numlist;
+       GSList *sorted_list, *cur;
+       guint first, last, next;
+       GSList *ret_list = NULL;
+       unsigned int count;
+       struct mailimap_set * current_set;
+       unsigned int item_count;
        
-       g_return_if_fail(folder != NULL);
-       g_return_if_fail(folder->klass == &imap_class);
-       g_return_if_fail(item != NULL);
-       g_return_if_fail(item->folder == folder);
-       g_return_if_fail(msginfo != NULL);
-       g_return_if_fail(msginfo->folder == item);
+       if (numlist == NULL)
+               return NULL;
+       
+       count = 0;
+       current_set = mailimap_set_new_empty();
+       
+       sorted_list = g_slist_copy(numlist);
+       sorted_list = g_slist_sort(sorted_list, g_int_compare);
 
-       session = imap_session_get(folder);
-       if (!session) return;
+       first = GPOINTER_TO_INT(sorted_list->data);
+       
+       item_count = 0;
+       for (cur = sorted_list; cur != NULL; cur = g_slist_next(cur)) {
+               if (GPOINTER_TO_INT(cur->data) == 0)
+                       continue;
+               
+               item_count ++;
+               
+               last = GPOINTER_TO_INT(cur->data);
+               if (cur->next)
+                       next = GPOINTER_TO_INT(cur->next->data);
+               else
+                       next = 0;
 
-       if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
-           NULL, NULL, NULL, NULL)) != IMAP_SUCCESS)
-               return;
+               if (last + 1 != next || next == 0) {
 
-       if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
-               flags_set |= IMAP_FLAG_FLAGGED;
-       if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
-               flags_unset |= IMAP_FLAG_FLAGGED;
+                       struct mailimap_set_item * item;
+                       item = mailimap_set_item_new(first, last);
+                       mailimap_set_add(current_set, item);
+                       count ++;
+                       
+                       first = next;
+                       
+                       if (count >= IMAP_SET_MAX_COUNT) {
+                               ret_list = g_slist_append(ret_list,
+                                                         current_set);
+                               current_set = mailimap_set_new_empty();
+                               count = 0;
+                               item_count = 0;
+                       }
+               }
+       }
+       
+       if (clist_count(current_set->set_list) > 0) {
+               ret_list = g_slist_append(ret_list,
+                                         current_set);
+       }
+       
+       g_slist_free(sorted_list);
 
-       if (!MSG_IS_UNREAD(msginfo->flags) &&  (newflags & MSG_UNREAD))
-               flags_unset |= IMAP_FLAG_SEEN;
-       if ( MSG_IS_UNREAD(msginfo->flags) && !(newflags & MSG_UNREAD))
-               flags_set |= IMAP_FLAG_SEEN;
+       return ret_list;
+}
 
-       if (!MSG_IS_REPLIED(msginfo->flags) &&  (newflags & MSG_REPLIED))
-               flags_set |= IMAP_FLAG_ANSWERED;
-       if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
-               flags_set |= IMAP_FLAG_ANSWERED;
+static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist)
+{
+       MsgNumberList *numlist = NULL;
+       MsgInfoList *cur;
+       GSList *seq_list;
 
-       numlist.next = NULL;
-       numlist.data = GINT_TO_POINTER(msginfo->msgnum);
+       for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
+               MsgInfo *msginfo = (MsgInfo *) cur->data;
+
+               numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
+       }
+       seq_list = imap_get_lep_set_from_numlist(numlist);
+       g_slist_free(numlist);
+
+       return seq_list;
+}
+
+static GSList * imap_uid_list_from_lep(clist * list)
+{
+       clistiter * iter;
+       GSList * result;
+       
+       result = NULL;
        
-       if (flags_set) {
-               ok = imap_set_message_flags(session, &numlist, flags_set, TRUE);
-               if (ok != IMAP_SUCCESS) return;
+       for(iter = clist_begin(list) ; iter != NULL ;
+           iter = clist_next(iter)) {
+               uint32_t * puid;
+               
+               puid = clist_content(iter);
+               result = g_slist_append(result, GINT_TO_POINTER(* puid));
        }
+       
+       return result;
+}
 
-       if (flags_unset) {
-               ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
-               if (ok != IMAP_SUCCESS) return;
+static GSList * imap_uid_list_from_lep_tab(carray * list)
+{
+       unsigned int i;
+       GSList * result;
+       
+       result = NULL;
+       
+       for(i = 0 ; i < carray_count(list) ; i ++) {
+               uint32_t * puid;
+               
+               puid = carray_get(list, i);
+               result = g_slist_append(result, GINT_TO_POINTER(* puid));
        }
+       
+       return result;
+}
 
-       msginfo->flags.perm_flags = newflags;
+static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
+                                      FolderItem *item)
+{
+       MsgInfo *msginfo = NULL;
+       guint32 uid = 0;
+       size_t size = 0;
+       MsgFlags flags = {0, 0};
+       
+       MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
+       if (item->stype == F_QUEUE) {
+               MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
+       } else if (item->stype == F_DRAFT) {
+               MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
+       }
+       flags.perm_flags = info->flags;
+       
+       uid = info->uid;
+       size = info->size;
+       msginfo = procheader_parse_str(info->headers, flags, FALSE, FALSE);
+       
+       if (msginfo) {
+               msginfo->msgnum = uid;
+               msginfo->size = size;
+       }
 
-       return;
+       return msginfo;
+}
+
+static void imap_lep_set_free(GSList *seq_list)
+{
+       GSList * cur;
+       
+       for(cur = seq_list ; cur != NULL ; cur = g_slist_next(cur)) {
+               struct mailimap_set * imapset;
+               
+               imapset = cur->data;
+               mailimap_set_free(imapset);
+       }
+       g_slist_free(seq_list);
+}
+
+static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags)
+{
+       struct mailimap_flag_list * flag_list;
+       
+       flag_list = mailimap_flag_list_new_empty();
+       
+       if (IMAP_IS_SEEN(flags))
+               mailimap_flag_list_add(flag_list,
+                                      mailimap_flag_new_seen());
+       if (IMAP_IS_ANSWERED(flags))
+               mailimap_flag_list_add(flag_list,
+                                      mailimap_flag_new_answered());
+       if (IMAP_IS_FLAGGED(flags))
+               mailimap_flag_list_add(flag_list,
+                                      mailimap_flag_new_flagged());
+       if (IMAP_IS_DELETED(flags))
+               mailimap_flag_list_add(flag_list,
+                                      mailimap_flag_new_deleted());
+       if (IMAP_IS_DRAFT(flags))
+               mailimap_flag_list_add(flag_list,
+                                      mailimap_flag_new_draft());
+       
+       return flag_list;
 }