2006-05-18 [colin] 2.2.0cvs33
[claws.git] / src / imap.c
index f50c54f2e3cc3a98c55c29171d0c27ec5152e0d3..c59fc8b0553730b9d2a90c262ed9dca4d5f53429 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -29,6 +29,8 @@
 #include <string.h>
 #include "imap.h"
 #include "imap_gtk.h"
+#include "inc.h"
+#include "xml.h"
 
 #ifdef HAVE_LIBETPAN
 
@@ -96,7 +98,7 @@ struct _IMAPSession
 
        gboolean authenticated;
 
-       gchar **capability;
+       GSList *capability;
        gboolean uidplus;
 
        gchar *mbox;
@@ -106,6 +108,7 @@ struct _IMAPSession
        gboolean folder_content_changed;
        guint exists;
        Folder * folder;
+       gboolean busy;
 };
 
 struct _IMAPNameSpace
@@ -155,8 +158,20 @@ struct _IMAPFolderItem
        guint uid_next;
        GSList *uid_list;
        gboolean batching;
+
+       time_t use_cache;
+       gint c_messages;
+       guint32 c_uid_next;
+       guint32 c_uid_validity;
+       gint c_unseen;
+
+       GHashTable *flags_set_table;
+       GHashTable *flags_unset_table;
 };
 
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
+
 static void imap_folder_init           (Folder         *folder,
                                         const gchar    *name,
                                         const gchar    *path);
@@ -270,8 +285,8 @@ static gint imap_select                     (IMAPSession    *session,
 static gint imap_status                        (IMAPSession    *session,
                                         IMAPFolder     *folder,
                                         const gchar    *path,
+                                        IMAPFolderItem *item,
                                         gint           *messages,
-                                        gint           *recent,
                                         guint32        *uid_next,
                                         guint32        *uid_validity,
                                         gint           *unseen,
@@ -283,13 +298,15 @@ static gchar imap_get_path_separator              (IMAPFolder     *folder,
                                                 const gchar    *path);
 static gchar *imap_get_real_path               (IMAPFolder     *folder,
                                                 const gchar    *path);
+static void imap_synchronise           (FolderItem     *item);
 
 static void imap_free_capabilities     (IMAPSession    *session);
 
 /* low-level IMAP4rev1 commands */
 static gint imap_cmd_login     (IMAPSession    *session,
                                 const gchar    *user,
-                                const gchar    *pass);
+                                const gchar    *pass,
+                                const gchar    *type);
 static gint imap_cmd_logout    (IMAPSession    *session);
 static gint imap_cmd_noop      (IMAPSession    *session);
 #if USE_OPENSSL
@@ -372,7 +389,7 @@ static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
 
 /* data types conversion libetpan <-> sylpheed */
 static GSList * imap_list_from_lep(IMAPFolder * folder,
-                                  clist * list, const gchar * real_path);
+                                  clist * list, const gchar * real_path, gboolean all);
 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);
@@ -382,12 +399,10 @@ static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
 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;
+       IMAPFolderItem *item;
 } hashtable_data;
 
 static FolderClass imap_class;
@@ -425,6 +440,10 @@ FolderClass *imap_get_class(void)
                imap_class.close = imap_close;
                imap_class.get_num_list = imap_get_num_list;
                imap_class.scan_required = imap_scan_required;
+               imap_class.set_xml = folder_set_xml;
+               imap_class.get_xml = folder_get_xml;
+               imap_class.item_set_xml = imap_item_set_xml;
+               imap_class.item_get_xml = imap_item_get_xml;
 
                /* Message functions */
                imap_class.get_msginfo = imap_get_msginfo;
@@ -442,6 +461,7 @@ FolderClass *imap_get_class(void)
                imap_class.change_flags = imap_change_flags;
                imap_class.get_flags = imap_get_flags;
                imap_class.set_batch = imap_set_batch;
+               imap_class.synchronise = imap_synchronise;
 #ifdef USE_PTREAD
                pthread_mutex_init(&imap_mutex, NULL);
 #endif
@@ -510,7 +530,6 @@ static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
        IMAPFolderItem *item = (IMAPFolderItem *)node->data;
        
        item->lastuid = 0;
-       item->uid_next = 0;
        g_slist_free(item->uid_list);
        item->uid_list = NULL;
        
@@ -526,41 +545,179 @@ 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); 
 }
 
+void imap_get_capabilities(IMAPSession *session)
+{
+       struct mailimap_capability_data *capabilities = NULL;
+       clistiter *cur;
+
+       if (session->capability != NULL)
+               return;
+
+       capabilities = imap_threaded_capability(session->folder);
+
+       if (capabilities == NULL)
+               return;
+
+       for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
+           cur = clist_next(cur)) {
+               struct mailimap_capability * cap = 
+                       clist_content(cur);
+               if (!cap || cap->cap_data.cap_name == NULL)
+                       continue;
+               session->capability = g_slist_append
+                               (session->capability,
+                                g_strdup(cap->cap_data.cap_name));
+               debug_print("got capa %s\n", cap->cap_data.cap_name);
+       }
+       mailimap_capability_data_free(capabilities);
+}
+
+gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
+{
+       GSList *cur;
+       for (cur = session->capability; cur; cur = cur->next) {
+               if (!g_ascii_strcasecmp(cur->data, cap))
+                       return TRUE;
+       }
+       return FALSE;
+}
+
 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
                      IMAPAuthType type)
 {
-       gint ok;
-
-       ok = imap_cmd_login(session, user, pass);
+       gint ok = IMAP_ERROR;
+       static time_t last_login_err = 0;
        
+       imap_get_capabilities(session);
+
+       switch(type) {
+       case IMAP_AUTH_ANON:
+               ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
+               break;
+       case IMAP_AUTH_CRAM_MD5:
+               ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
+               break;
+       case IMAP_AUTH_LOGIN:
+               ok = imap_cmd_login(session, user, pass, "LOGIN");
+               break;
+       default:
+               debug_print("capabilities:\n"
+                               "\t ANONYMOUS %d\n"
+                               "\t CRAM-MD5 %d\n"
+                               "\t LOGIN %d\n", 
+                       imap_has_capability(session, "ANONYMOUS"),
+                       imap_has_capability(session, "CRAM-MD5"),
+                       imap_has_capability(session, "LOGIN"));
+               if (imap_has_capability(session, "CRAM-MD5"))
+                       ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
+               if (ok == IMAP_ERROR) /* we always try LOGIN before giving up */
+                       ok = imap_cmd_login(session, user, pass, "LOGIN");
+       }
        if (ok == IMAP_SUCCESS)
                session->authenticated = TRUE;
-
+       else {
+               gchar *ext_info = NULL;
+               
+               if (type == IMAP_AUTH_CRAM_MD5) {
+                       ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
+                                    "compiled with SASL support and the "
+                                    "CRAM-MD5 SASL plugin is installed.");
+               } else {
+                       ext_info = "";
+               }
+               
+               if (time(NULL) - last_login_err > 10) {
+                       if (!prefs_common.no_recv_err_panel) {
+                               alertpanel_error(_("Connection to %s failed: "
+                                       "login refused.%s"),
+                                       SESSION(session)->server, ext_info);
+                       } else {
+                               log_error(_("Connection to %s failed: "
+                                       "login refused.%s\n"),
+                                       SESSION(session)->server, ext_info);
+                       }
+               }
+               last_login_err = time(NULL);
+       }
        return ok;
 }
 
+static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
+{
+       RemoteFolder *rfolder = REMOTE_FOLDER(folder);
+       /* Check if this is the first try to establish a
+          connection, if yes we don't try to reconnect */
+       debug_print("reconnecting\n");
+       if (rfolder->session == NULL) {
+               log_warning(_("Connecting to %s failed"),
+                           folder->account->recv_server);
+               session_destroy(SESSION(session));
+               session = NULL;
+       } else {
+               log_warning(_("IMAP4 connection to %s has been"
+                           " disconnected. Reconnecting...\n"),
+                           folder->account->recv_server);
+               statusbar_print_all(_("IMAP4 connection to %s has been"
+                           " disconnected. Reconnecting...\n"),
+                           folder->account->recv_server);
+               SESSION(session)->state = SESSION_DISCONNECTED;
+               session_destroy(SESSION(session));
+               /* Clear folders session to make imap_session_get create
+                  a new session, because of rfolder->session == NULL
+                  it will not try to reconnect again and so avoid an
+                  endless loop */
+               rfolder->session = NULL;
+               session = imap_session_get(folder);
+               rfolder->session = SESSION(session);
+               statusbar_pop_all();
+       }
+       return session;
+}
+
+#define lock_session() {\
+       debug_print("locking session\n"); \
+       session->busy = TRUE;\
+}
+
+#define unlock_session() {\
+       debug_print("unlocking session\n"); \
+       session->busy = FALSE;\
+}
+
 static IMAPSession *imap_session_get(Folder *folder)
 {
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
        IMAPSession *session = NULL;
+       static time_t last_failure = 0;
 
        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 (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                return NULL;
        }
 
        /* Make sure we have a session */
        if (rfolder->session != NULL) {
                session = IMAP_SESSION(rfolder->session);
+               /* don't do that yet... 
+               if (session->busy) {
+                       return NULL;
+               } */
        } else {
                imap_reset_uid_lists(folder);
+               if (time(NULL) - last_failure <= 2)
+                       return NULL;
                session = imap_session_new(folder, folder->account);
        }
-       if(session == NULL)
+       if(session == NULL) {
+               last_failure = time(NULL);
                return NULL;
+       }
 
        /* Make sure session is authenticated */
        if (!IMAP_SESSION(session)->authenticated)
@@ -569,15 +726,10 @@ static IMAPSession *imap_session_get(Folder *folder)
        if (!IMAP_SESSION(session)->authenticated) {
                session_destroy(SESSION(session));
                rfolder->session = NULL;
+               last_failure = time(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
         * think it's still alive.  Unfortunately, most of the code
@@ -589,29 +741,8 @@ static IMAPSession *imap_session_get(Folder *folder)
        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 to %s failed"),
-                                           folder->account->recv_server);
-                               session_destroy(SESSION(session));
-                               session = NULL;
-                       } else {
-                               log_warning(_("IMAP4 connection to %s has been"
-                                           " disconnected. Reconnecting...\n"),
-                                           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
-                                  it will not try to reconnect again and so avoid an
-                                  endless loop */
-                               rfolder->session = NULL;
-                               session = imap_session_get(folder);
-                               statusbar_pop_all();
-                       }
+                       debug_print("disconnected!\n");
+                       session = imap_reconnect_if_possible(folder, session);
                }
        }
 
@@ -636,6 +767,20 @@ static IMAPSession *imap_session_new(Folder * folder,
                : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
        ssl_type = account->ssl_imap;   
 #else
+       if (account->ssl_imap != SSL_NONE) {
+               if (alertpanel_full(_("Insecure connection"),
+                       _("This connection is configured to be secured "
+                         "using SSL, but SSL is not available in this "
+                         "build of Sylpheed-Claws. \n\n"
+                         "Do you want to continue connecting to this "
+                         "server? The communication would not be "
+                         "secure."),
+                         _("Con_tinue connecting"), 
+                         GTK_STOCK_CANCEL, NULL,
+                         FALSE, NULL, ALERT_WARNING,
+                         G_ALERTALTERNATE) != G_ALERTDEFAULT)
+                       return NULL;
+       }
        port = account->set_imapport ? account->imapport
                : IMAP4_PORT;
 #endif
@@ -675,7 +820,10 @@ static IMAPSession *imap_session_new(Folder * folder,
                if(!prefs_common.no_recv_err_panel) {
                        alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
                                         account->recv_server, port);
-               }
+               } else {
+                       log_error(_("Can't connect to IMAP4 server: %s:%d\n"),
+                                        account->recv_server, port);
+               } 
                
                return NULL;
        }
@@ -727,13 +875,15 @@ static void imap_session_authenticate(IMAPSession *session,
        g_return_if_fail(account->userid != NULL);
 
        pass = account->passwd;
-       if (!pass) {
+       if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
                gchar *tmp_pass;
                tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
                if (!tmp_pass)
-                       tmp_pass = g_strdup(""); /* allow empty password */
+                       return;
                Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
                g_free(tmp_pass);
+       } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
+               pass = "";
        }
        statusbar_print_all(_("Connecting to IMAP4 server %s...\n"),
                                account->recv_server);
@@ -750,7 +900,8 @@ static void imap_session_authenticate(IMAPSession *session,
 
 static void imap_session_destroy(Session *session)
 {
-       imap_threaded_disconnect(IMAP_SESSION(session)->folder);
+       if (session->state != SESSION_DISCONNECTED)
+               imap_threaded_disconnect(IMAP_SESSION(session)->folder);
        
        imap_free_capabilities(IMAP_SESSION(session));
        g_free(IMAP_SESSION(session)->mbox);
@@ -763,7 +914,7 @@ 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) 
+static guint get_size_with_crs(MsgInfo *info) 
 {
        FILE *fp = NULL;
        guint cnt = 0;
@@ -804,21 +955,29 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                make_dir_hier(path);
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        g_free(path);
-
+       debug_print("trying to fetch cached %s\n", filename);
        if (is_file_exist(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,
+               guint have_size = get_size_with_crs(msginfo);
+
+               if (cached)
+                       debug_print("message %d has been already %scached (%d/%d).\n", uid,
                                have_size == cached->size ? "fully ":"",
-                               have_size, cached? (int)cached->size : -1);
+                               have_size, (int)cached->size);
                
                if (cached && (cached->size == have_size || !body)) {
                        procmsg_msginfo_free(cached);
                        procmsg_msginfo_free(msginfo);
+                       file_strip_crs(filename);
+                       return filename;
+               } else if (!cached) {
+                       debug_print("message not cached, considering file complete\n");
+                       procmsg_msginfo_free(msginfo);
+                       file_strip_crs(filename);
                        return filename;
                } else {
                        procmsg_msginfo_free(cached);
@@ -827,17 +986,21 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        }
 
        session = imap_session_get(folder);
+       
        if (!session) {
                g_free(filename);
                return NULL;
        }
 
+       lock_session();
+
        debug_print("IMAP fetching messages\n");
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
                g_warning("can't select mailbox %s\n", item->path);
                g_free(filename);
+               unlock_session();
                return NULL;
        }
 
@@ -847,9 +1010,12 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        if (ok != IMAP_SUCCESS) {
                g_warning("can't fetch message %d\n", uid);
                g_free(filename);
+               unlock_session();
                return NULL;
        }
 
+       unlock_session();
+       file_strip_crs(filename);
        return filename;
 }
 
@@ -881,6 +1047,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        GSList *cur;
        MsgFileInfo *fileinfo;
        gint ok;
+       gint curnum = 0, total = 0;
 
 
        g_return_val_if_fail(folder != NULL, -1);
@@ -891,14 +1058,21 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        if (!session) {
                return -1;
        }
+       lock_session();
        destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
 
+       statusbar_print_all(_("Adding messages..."));
+       total = g_slist_length(file_list);
        for (cur = file_list; cur != NULL; cur = cur->next) {
                IMAPFlags iflags = 0;
                guint32 new_uid = 0;
-
+               gchar *real_file = NULL;
+               gboolean file_is_tmp = FALSE;
                fileinfo = (MsgFileInfo *)cur->data;
 
+               statusbar_progress_all(curnum, total, 1);
+               curnum++;
+
                if (fileinfo->flags) {
                        if (MSG_IS_MARKED(*fileinfo->flags))
                                iflags |= IMAP_FLAG_FLAGGED;
@@ -907,30 +1081,91 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        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)
+               
+               if (fileinfo->flags) {
+                       if ((MSG_IS_QUEUED(*fileinfo->flags) 
+                            || MSG_IS_DRAFT(*fileinfo->flags))
+                       && !folder_has_parent_of_type(dest, F_QUEUE)
+                       && !folder_has_parent_of_type(dest, F_DRAFT)) {
+                               real_file = get_tmp_file();
+                               file_is_tmp = TRUE;
+                               if (procmsg_remove_special_headers(
+                                               fileinfo->file, 
+                                               real_file) !=0) {
+                                       g_free(real_file);
+                                       g_free(destdir);
+                                       unlock_session();
+                                       return -1;
+                               }
+                       }  else if (!(MSG_IS_QUEUED(*fileinfo->flags) 
+                                     || MSG_IS_DRAFT(*fileinfo->flags))
+                                   && (folder_has_parent_of_type(dest, F_QUEUE)
+                                   || folder_has_parent_of_type(dest, F_DRAFT))) {
+                               return -1;
+                       } 
+               }
+               if (real_file == NULL)
+                       real_file = g_strdup(fileinfo->file);
+               
+               if (folder_has_parent_of_type(dest, F_QUEUE) ||
+                   folder_has_parent_of_type(dest, F_OUTBOX) ||
+                   folder_has_parent_of_type(dest, F_DRAFT) ||
+                   folder_has_parent_of_type(dest, F_TRASH))
                        iflags |= IMAP_FLAG_SEEN;
 
-               ok = imap_cmd_append(session, destdir, fileinfo->file, iflags, 
+               ok = imap_cmd_append(session, destdir, real_file, iflags, 
                                     &new_uid);
 
                if (ok != IMAP_SUCCESS) {
-                       g_warning("can't append message %s\n", fileinfo->file);
+                       g_warning("can't append message %s\n", real_file);
+                       if (file_is_tmp)
+                               g_unlink(real_file);
+                       g_free(real_file);
                        g_free(destdir);
+                       unlock_session();
+                       statusbar_progress_all(0,0,0);
+                       statusbar_pop_all();
                        return -1;
+               } else {
+                       debug_print("appended new message as %d\n", new_uid);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (new_uid > 0) {
+                               gchar *cache_path = folder_item_get_path(dest);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_dir_exist(cache_path)) {
+                                       gchar *cache_file = g_strconcat(
+                                               cache_path, G_DIR_SEPARATOR_S, 
+                                               itos(new_uid), NULL);
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                                       g_free(cache_file);
+                               }
+                               g_free(cache_path);
+                       }
                }
 
                if (relation != NULL)
                        g_relation_insert(relation, fileinfo->msginfo != NULL ? 
                                          (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
                                          GINT_TO_POINTER(dest->last_num + 1));
+               if (new_uid == 0) {
+                       new_uid = dest->last_num+1;
+               }
                if (last_uid < new_uid)
                        last_uid = new_uid;
-       }
+               if (file_is_tmp)
+                       g_unlink(real_file);
 
+               g_free(real_file);
+       }
+       statusbar_progress_all(0,0,0);
+       statusbar_pop_all();
+       
+       imap_cmd_expunge(session);
+       unlock_session();
+       
        g_free(destdir);
 
        return last_uid;
@@ -947,7 +1182,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        gint ok = IMAP_SUCCESS;
        GRelation *uid_mapping;
        gint last_num = 0;
-       
+
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
@@ -957,17 +1192,20 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        if (!session) {
                return -1;
        }
+       lock_session();
        msginfo = (MsgInfo *)msglist->data;
 
        src = msginfo->folder;
        if (src == dest) {
                g_warning("the src folder is identical to the dest.\n");
+               unlock_session();
                return -1;
        }
 
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
+               unlock_session();
                return ok;
        }
 
@@ -976,11 +1214,11 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        uid_mapping = g_relation_new(2);
        g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
        
+       statusbar_print_all(_("Copying messages..."));
        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);
 
@@ -988,6 +1226,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                if (ok != IMAP_SUCCESS) {
                        g_relation_destroy(uid_mapping);
                        imap_lep_set_free(seq_list);
+                       unlock_session();
                        return -1;
                }
        }
@@ -1010,6 +1249,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                                          GPOINTER_TO_INT(0));
                g_tuples_destroy(tuples);
        }
+       statusbar_pop_all();
 
        g_relation_destroy(uid_mapping);
        imap_lep_set_free(seq_list);
@@ -1021,6 +1261,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        g_slist_free(IMAP_FOLDER_ITEM(dest)->uid_list);
        IMAP_FOLDER_ITEM(dest)->uid_list = NULL;
 
+       unlock_session();
        if (ok == IMAP_SUCCESS)
                return last_num;
        else
@@ -1053,16 +1294,28 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       if (folder == msginfo->folder->folder) {
+       /* if from/to are the same "type" (with or without extra headers),
+        * copy them via imap */
+       if (folder == msginfo->folder->folder &&
+           !folder_has_parent_of_type(msginfo->folder, F_DRAFT) &&
+           !folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+           !folder_has_parent_of_type(dest, F_DRAFT) &&
+           !folder_has_parent_of_type(dest, F_QUEUE)) {
+               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
+               return ret;
+       } else if (folder == msginfo->folder->folder &&
+           (folder_has_parent_of_type(msginfo->folder, F_DRAFT) ||
+            folder_has_parent_of_type(msginfo->folder, F_QUEUE)) && 
+           (folder_has_parent_of_type(dest, F_DRAFT) ||
+            folder_has_parent_of_type(dest, F_QUEUE))) {
                ret = imap_do_copy_msgs(folder, dest, msglist, relation);
                return ret;
        }
-
+       /* else reupload them */
        file_list = procmsg_get_message_file_list(msglist);
        g_return_val_if_fail(file_list != NULL, -1);
 
        ret = imap_add_msgs(folder, dest, file_list, relation);
-
        procmsg_message_file_list_free(file_list);
 
        return ret;
@@ -1073,7 +1326,7 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
                                MsgInfoList *msglist, GRelation *relation)
 {
        gchar *destdir;
-       GSList *seq_list = NULL, *cur;
+       GSList *numlist = NULL, *cur;
        MsgInfo *msginfo;
        IMAPSession *session;
        gint ok = IMAP_SUCCESS;
@@ -1087,18 +1340,21 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        if (!session) {
                return -1;
        }
+       lock_session();
        msginfo = (MsgInfo *)msglist->data;
 
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
+               unlock_session();
                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));
+               if (!MSG_IS_DELETED(msginfo->flags))
+                       numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
        }
 
        uid_mapping = g_relation_new(2);
@@ -1106,22 +1362,24 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
 
        ok = imap_set_message_flags
                (IMAP_SESSION(REMOTE_FOLDER(folder)->session),
-               seq_list, IMAP_FLAG_DELETED, TRUE);
+               numlist, IMAP_FLAG_DELETED, TRUE);
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't set deleted flags\n"));
+               unlock_session();
                return ok;
        }
        ok = imap_cmd_expunge(session);
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't expunge\n"));
+               unlock_session();
                return ok;
        }
        
        g_relation_destroy(uid_mapping);
-       g_slist_free(seq_list);
+       g_slist_free(numlist);
 
        g_free(destdir);
-
+       unlock_session();
        if (ok == IMAP_SUCCESS)
                return 0;
        else
@@ -1184,12 +1442,13 @@ static gint imap_scan_tree(Folder *folder)
                return -1;
        }
 
+       lock_session();
        if (folder->account->imap_dir && *folder->account->imap_dir) {
                gchar *real_path;
                int r;
                clist * lep_list;
 
-               Xstrdup_a(root_folder, folder->account->imap_dir, {return -1;});
+               Xstrdup_a(root_folder, folder->account->imap_dir, {unlock_session();return -1;});
                extract_quote(root_folder, '"');
                subst_char(root_folder,
                           imap_get_path_separator(IMAP_FOLDER(folder),
@@ -1210,9 +1469,9 @@ static gint imap_scan_tree(Folder *folder)
                                item->folder = folder;
                                folder->node = item->node = g_node_new(item);
                        }
+                       unlock_session();
                        return -1;
                }
-               
                mailimap_list_result_free(lep_list);
                
                g_free(real_path);
@@ -1230,6 +1489,7 @@ static gint imap_scan_tree(Folder *folder)
 
        imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data));
        imap_create_missing_folders(folder);
+       unlock_session();
 
        return 0;
 }
@@ -1280,7 +1540,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
        }
        else {
                item_list = imap_list_from_lep(imapfolder,
-                                              lep_list, real_path);
+                                              lep_list, real_path, FALSE);
                mailimap_list_result_free(lep_list);
        }
        
@@ -1320,6 +1580,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
        for (cur = item_list; cur != NULL; cur = cur->next) {
                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,
@@ -1490,7 +1751,8 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        gchar *new_name;
        const gchar *p;
        gint ok;
-
+       gboolean no_select = FALSE, no_sub = FALSE;
+       
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
        g_return_val_if_fail(parent != NULL, NULL);
@@ -1501,35 +1763,40 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                return NULL;
        }
 
-       if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0)
+       lock_session();
+       if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
                dirpath = g_strdup(name);
-       else if (parent->path)
+       }else if (parent->path)
                dirpath = g_strconcat(parent->path, "/", name, NULL);
        else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
                dirpath = g_strdup(name);
        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, {unlock_session();return NULL;});
                strtailchomp(imap_dir, '/');
                dirpath = g_strconcat(imap_dir, "/", name, NULL);
        } else
                dirpath = g_strdup(name);
+               
+       
 
        /* keep trailing directory separator to create a folder that contains
           sub folder */
        imap_path = imap_utf8_to_modified_utf7(dirpath);
+
        strtailchomp(dirpath, '/');
        Xstrdup_a(new_name, name, {
-               g_free(dirpath);                
+               g_free(dirpath); 
+               unlock_session();               
                return NULL;});
 
-       strtailchomp(new_name, '/');
        separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path);
        imap_path_separator_subst(imap_path, separator);
-       subst_char(new_name, '/', separator);
+       /* remove trailing / for display */
+       strtailchomp(new_name, '/');
 
-       if (strcmp(name, "INBOX") != 0) {
+       if (strcmp(dirpath, "INBOX") != 0) {
                GPtrArray *argbuf;
                gboolean exist = FALSE;
                int r;
@@ -1543,24 +1810,58 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                        g_free(dirpath);
                        ptr_array_free_strings(argbuf);
                        g_ptr_array_free(argbuf, TRUE);
+                       unlock_session();
                        return NULL;
                }
                
                if (clist_count(lep_list) > 0)
                        exist = TRUE;
-               
+               mailimap_list_result_free(lep_list);
+               lep_list = NULL;
                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);
+                               unlock_session();
                                return NULL;
                        }
+                       r = imap_threaded_list(folder, "", imap_path, &lep_list);
+                       if (r == MAILIMAP_NO_ERROR) {
+                               GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
+                                              lep_list, dirpath, TRUE);
+                               if (item_list) {
+                                       FolderItem *cur_item = FOLDER_ITEM(item_list->data);
+                                       no_select = cur_item->no_select;
+                                       no_sub = cur_item->no_sub;
+                                       g_slist_free(item_list);
+                               } 
+                               mailimap_list_result_free(lep_list);
+                       }
+
+               }
+       } else {
+               clist *lep_list;
+               int r;
+               /* just get flags */
+               r = imap_threaded_list(folder, "", "INBOX", &lep_list);
+               if (r == MAILIMAP_NO_ERROR) {
+                       GSList *item_list = imap_list_from_lep(IMAP_FOLDER(folder),
+                                      lep_list, dirpath, TRUE);
+                       if (item_list) {
+                               FolderItem *cur_item = FOLDER_ITEM(item_list->data);
+                               no_select = cur_item->no_select;
+                               no_sub = cur_item->no_sub;
+                               g_slist_free(item_list);
+                       } 
+                       mailimap_list_result_free(lep_list);
                }
        }
 
        new_item = folder_item_new(folder, new_name, dirpath);
+       new_item->no_select = no_select;
+       new_item->no_sub = no_sub;
        folder_item_append(parent, new_item);
        g_free(imap_path);
        g_free(dirpath);
@@ -1569,7 +1870,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        if (!is_dir_exist(dirpath))
                make_dir_hier(dirpath);
        g_free(dirpath);
-
+       unlock_session();
        return new_item;
 }
 
@@ -1594,16 +1895,19 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        g_return_val_if_fail(item->path != NULL, -1);
        g_return_val_if_fail(name != NULL, -1);
 
+       session = imap_session_get(folder);
+       if (!session) {
+               return -1;
+       }
+       lock_session();
+
        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"));
+               unlock_session();
                return -1;
        }
 
-       session = imap_session_get(folder);
-       if (!session) {
-               return -1;
-       }
        real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
 
        g_free(session->mbox);
@@ -1612,6 +1916,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
                              &exists, &recent, &unseen, &uid_validity, FALSE);
        if (ok != IMAP_SUCCESS) {
                g_free(real_oldpath);
+               unlock_session();
                return -1;
        }
 
@@ -1633,6 +1938,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
                g_free(real_oldpath);
                g_free(newpath);
                g_free(real_newpath);
+               unlock_session();
                return -1;
        }
 
@@ -1659,7 +1965,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        g_free(newpath);
        g_free(real_oldpath);
        g_free(real_newpath);
-
+       unlock_session();
        return 0;
 }
 
@@ -1669,8 +1975,6 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        IMAPSession *session;
        gchar *path;
        gchar *cache_dir;
-       gint exists, recent, unseen;
-       guint32 uid_validity;
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(item != NULL, -1);
@@ -1680,19 +1984,22 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        if (!session) {
                return -1;
        }
+       lock_session();
        path = imap_get_real_path(IMAP_FOLDER(folder), item->path);
 
-       ok = imap_cmd_examine(session, "INBOX",
-                             &exists, &recent, &unseen, &uid_validity, FALSE);
+       ok = imap_cmd_delete(session, path);
        if (ok != IMAP_SUCCESS) {
+               gchar *tmp = g_strdup_printf("%s%c", path, 
+                               imap_get_path_separator(IMAP_FOLDER(folder), path));
                g_free(path);
-               return -1;
+               path = tmp;
+               ok = imap_cmd_delete(session, path);
        }
 
-       ok = imap_cmd_delete(session, path);
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't delete mailbox\n"));
                g_free(path);
+               unlock_session();
                return -1;
        }
 
@@ -1702,7 +2009,7 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
                g_warning("can't remove directory '%s'\n", cache_dir);
        g_free(cache_dir);
        folder_item_remove(item);
-
+       unlock_session();
        return 0;
 }
 
@@ -1772,6 +2079,8 @@ static void *imap_get_uncached_messages_thread(void *data)
                if (r != MAILIMAP_NO_ERROR)
                        continue;
                
+               session_set_access_time(SESSION(session));
+
                count = 0;
                for(i = 0 ; i < carray_count(env_list) ; i ++) {
                        struct imap_fetch_env_info * info;
@@ -1779,6 +2088,8 @@ static void *imap_get_uncached_messages_thread(void *data)
                        
                        info = carray_get(env_list, i);
                        msginfo = imap_envelope_from_lep(info, item);
+                       if (msginfo == NULL)
+                               continue;
                        msginfo->folder = item;
                        if (!newlist)
                                llast = newlist = g_slist_append(newlist, msginfo);
@@ -1792,6 +2103,13 @@ static void *imap_get_uncached_messages_thread(void *data)
                imap_fetch_env_free(env_list);
        }
        
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               struct mailimap_set * imapset;
+               
+               imapset = cur->data;
+               mailimap_set_free(imapset);
+       }
+       
        session_set_access_time(SESSION(session));
        stuff->done = TRUE;
        return newlist;
@@ -1846,7 +2164,10 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                data->numlist = newlist;
                data->cur += count;
                
-               if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               if (prefs_common.work_offline && 
+                   !inc_offline_should_override(
+                       _("Sylpheed-Claws needs network access in order "
+                         "to access the IMAP server."))) {
                        g_free(data);
                        return NULL;
                }
@@ -1927,11 +2248,31 @@ static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
        return NULL;
 }
 
+gchar imap_get_path_separator_for_item(FolderItem *item)
+{
+       Folder *folder = NULL;
+       IMAPFolder *imap_folder = NULL;
+       if (!item)
+               return '/';
+       folder = item->folder;
+       
+       if (!folder)
+               return '/';
+       
+       imap_folder = IMAP_FOLDER(folder);
+       
+       if (!imap_folder)
+               return '/';
+       
+       return imap_get_path_separator(imap_folder, item->path);
+}
 
 static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
 {
        IMAPNameSpace *namespace;
        gchar separator = '/';
+       IMAPSession *session = imap_session_get(FOLDER(folder));
+       g_return_val_if_fail(session != NULL, '/');
 
        if (folder->last_seen_separator == 0) {
                clist * lep_list;
@@ -2024,14 +2365,18 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
        gint exists_, recent_, unseen_;
        guint32 uid_validity_;
        
-       if (!exists || !recent || !unseen || !uid_validity) {
+       if (!exists && !recent && !unseen && !uid_validity) {
                if (session->mbox && strcmp(session->mbox, path) == 0)
                        return IMAP_SUCCESS;
+       }
+       if (!exists)
                exists = &exists_;
+       if (!recent)
                recent = &recent_;
+       if (!unseen)
                unseen = &unseen_;
+       if (!uid_validity)
                uid_validity = &uid_validity_;
-       }
 
        g_free(session->mbox);
        session->mbox = NULL;
@@ -2052,8 +2397,8 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
 }
 
 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
-                       const gchar *path,
-                       gint *messages, gint *recent,
+                       const gchar *path, IMAPFolderItem *item,
+                       gint *messages,
                        guint32 *uid_next, guint32 *uid_validity,
                        gint *unseen, gboolean block)
 {
@@ -2062,10 +2407,25 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        struct mailimap_mailbox_data_status * data_status;
        int got_values;
        gchar *real_path;
-
+       guint mask = 0;
+       
        real_path = imap_get_real_path(folder, path);
 
-       r = imap_threaded_status(FOLDER(folder), real_path, &data_status);
+       if (messages) {
+               mask |= 1 << 0;
+       }
+       if (uid_next) {
+               mask |= 1 << 2;
+       }
+       if (uid_validity) {
+               mask |= 1 << 3;
+       }
+       if (unseen) {
+               mask |= 1 << 4;
+       }
+       r = imap_threaded_status(FOLDER(folder), real_path, 
+               &data_status, mask);
+
        g_free(real_path);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("status err %d\n", r);
@@ -2090,11 +2450,6 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                        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;
@@ -2113,8 +2468,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        }
        mailimap_mailbox_data_status_free(data_status);
        
-       if (got_values != ((1 << 4) + (1 << 3) +
-                          (1 << 2) + (1 << 1) + (1 << 0))) {
+       if (got_values != mask) {
                debug_print("status: incomplete values received (%d)\n", got_values);
                return IMAP_ERROR;
        }
@@ -2123,75 +2477,32 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
 
 static void imap_free_capabilities(IMAPSession *session)
 {
-       g_strfreev(session->capability);
+       slist_free_strings(session->capability);
+       g_slist_free(session->capability);
        session->capability = NULL;
 }
 
 /* low-level IMAP4rev1 commands */
 
-#if 0
-static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
-                                 const gchar *pass, IMAPAuthType type)
-{
-       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);
-
-       md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
-       g_free(challenge);
-
-       response = g_strdup_printf("%s %s", user, hexdigest);
-       response64 = g_malloc((strlen(response) + 3) * 2 + 1);
-       base64_encode(response64, response, strlen(response));
-       g_free(response);
-
-       sock_puts(SESSION(session)->sock, response64);
-       ok = imap_cmd_ok(session, NULL);
-       if (ok != IMAP_SUCCESS)
-               log_warning(_("IMAP4 authentication failed.\n"));
-
-       return ok;
-}
-#endif
-
 static gint imap_cmd_login(IMAPSession *session,
-                          const gchar *user, const gchar *pass)
+                          const gchar *user, const gchar *pass,
+                          const gchar *type)
 {
        int r;
        gint ok;
-       static time_t last_login_err = 0;
 
-       log_print("IMAP4> Logging in to %s\n", SESSION(session)->server);
-       r = imap_threaded_login(session->folder, user, pass);
+       log_print("IMAP4> Logging %s to %s using %s\n", 
+                       user,
+                       SESSION(session)->server,
+                       type);
+       r = imap_threaded_login(session->folder, user, pass, type);
        if (r != MAILIMAP_NO_ERROR) {
-               if (time(NULL) - last_login_err > 10) {
-                       alertpanel_error(_("Connection to %s failed: login refused."),
-                                       SESSION(session)->server);
-               }
-               last_login_err = time(NULL);
-               log_error("IMAP4< Error logging in to %s\n",
+               log_print("IMAP4< Error logging in to %s\n",
                                SESSION(session)->server);
                ok = IMAP_ERROR;
        } else {
+               log_print("IMAP4< Login to %s successful\n",
+                               SESSION(session)->server);
                ok = IMAP_SUCCESS;
        }
        return ok;
@@ -2225,7 +2536,8 @@ static gint imap_cmd_starttls(IMAPSession *session)
 {
        int r;
        
-       r = imap_threaded_starttls(session->folder);
+       r = imap_threaded_starttls(session->folder, 
+               SESSION(session)->server, SESSION(session)->port);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("starttls err %d\n", r);
                return IMAP_ERROR;
@@ -2352,12 +2664,16 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        data->headers = headers;
        data->body = body;
 
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
-
+       statusbar_print_all(_("Fetching message..."));
        result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+       statusbar_pop_all();
        g_free(data);
        return result;
 }
@@ -2374,10 +2690,8 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 
        flag_list = imap_flag_to_lep(flags);
        r = imap_threaded_append(session->folder, destfolder,
-                        file, flag_list);
-       
-       if (new_uid != NULL)
-               *new_uid = 0;
+                        file, flag_list, new_uid);
+       mailimap_flag_list_free(flag_list);
 
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("append err %d\n", r);
@@ -2421,6 +2735,7 @@ static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
                        mailimap_store_att_flags_new_remove_flags_silent(flag_list);
        
        r = imap_threaded_store(session->folder, set, store_att_flags);
+       mailimap_store_att_flags_free(store_att_flags);
        if (r != MAILIMAP_NO_ERROR) {
                
                return IMAP_ERROR;
@@ -2433,7 +2748,10 @@ static gint imap_cmd_expunge(IMAPSession *session)
 {
        int r;
        
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                return -1;
        }
 
@@ -2673,6 +2991,7 @@ static gboolean imap_rename_folder_func(GNode *node, gpointer data)
 
 typedef struct _get_list_uid_data {
        Folder *folder;
+       IMAPSession *session;
        IMAPFolderItem *item;
        GSList **msgnum_list;
        gboolean done;
@@ -2690,13 +3009,13 @@ static void *get_list_of_uids_thread(void *data)
        struct mailimap_set * set;
        clist * lep_uidlist;
        int r;
-       
-       session = imap_session_get(folder);
+
+       session = stuff->session;
        if (session == NULL) {
                stuff->done = TRUE;
                return GINT_TO_POINTER(-1);
        }
-
+       /* no session locking here, it's already locked by caller */
        ok = imap_select(session, IMAP_FOLDER(folder), item->item.path,
                         NULL, NULL, NULL, NULL, TRUE);
        if (ok != IMAP_SUCCESS) {
@@ -2707,13 +3026,18 @@ static void *get_list_of_uids_thread(void *data)
        uidlist = NULL;
        
        set = mailimap_set_new_interval(item->lastuid + 1, 0);
+
        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, set,
                                 &lep_uidlist);
+       mailimap_set_free(set);
+       
        if (r == MAILIMAP_NO_ERROR) {
                GSList * fetchuid_list;
                
                fetchuid_list =
                        imap_uid_list_from_lep(lep_uidlist);
+               mailimap_search_result_free(lep_uidlist);
+               
                uidlist = g_slist_concat(fetchuid_list, uidlist);
        }
        else {
@@ -2725,6 +3049,7 @@ static void *get_list_of_uids_thread(void *data)
                if (r == MAILIMAP_NO_ERROR) {
                        fetchuid_list =
                                imap_uid_list_from_lep_tab(lep_uidtab);
+                       imap_fetch_uid_list_free(lep_uidtab);
                        uidlist = g_slist_concat(fetchuid_list, uidlist);
                }
        }
@@ -2748,12 +3073,11 @@ static void *get_list_of_uids_thread(void *data)
                }
        }
        g_slist_free(uidlist);
-
        stuff->done = TRUE;
        return GINT_TO_POINTER(nummsgs);
 }
 
-static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
+static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderItem *item, GSList **msgnum_list)
 {
        gint result;
        get_list_uid_data *data = g_new0(get_list_uid_data, 1);
@@ -2761,8 +3085,11 @@ static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgn
        data->folder = folder;
        data->item = item;
        data->msgnum_list = msgnum_list;
-
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       data->session = session;
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -2777,7 +3104,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 {
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        IMAPSession *session;
-       gint ok, nummsgs = 0, exists, recent, uid_val, uid_next, unseen;
+       gint ok, nummsgs = 0, exists, uid_val, uid_next = 0;
        GSList *uidlist = NULL;
        gchar *dir;
        gboolean selected_folder;
@@ -2792,23 +3119,52 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, -1);
+       lock_session();
+
+       if (FOLDER_ITEM(item)->path) 
+               statusbar_print_all(_("Scanning folder %s%c%s ..."),
+                                     FOLDER_ITEM(item)->folder->name, 
+                                     G_DIR_SEPARATOR,
+                                     FOLDER_ITEM(item)->path);
+       else
+               statusbar_print_all(_("Scanning folder %s ..."),
+                                     FOLDER_ITEM(item)->folder->name);
 
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
-                       
-                       return -1;
+                       debug_print("disconnected!\n");
+                       session = imap_reconnect_if_possible(folder, session);
+                       if (session == NULL) {
+                               statusbar_pop_all();
+                               unlock_session();
+                               return -1;
+                       }
                }
                exists = session->exists;
 
+               uid_next = item->c_uid_next;
+               uid_val = item->c_uid_validity;
                *old_uids_valid = TRUE;
        } else {
-               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
+               if (item->use_cache && time(NULL) - item->use_cache < 2) {
+                       exists = item->c_messages;
+                       uid_next = item->c_uid_next;
+                       uid_val = item->c_uid_validity;
+                       ok = IMAP_SUCCESS;
+                       debug_print("using cache %d %d %d\n", exists, uid_next, uid_val);
+               } else {
+                       ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, item,
+                                &exists, &uid_next, &uid_val, NULL, FALSE);
+               }
+               item->item.last_num = uid_next - 1;
+               
+               item->use_cache = (time_t)0;
                if (ok != IMAP_SUCCESS) {
-                       
+                       statusbar_pop_all();
+                       unlock_session();
                        return -1;
                }
                if(item->item.mtime == uid_val)
@@ -2827,13 +3183,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                }
        }
 
-       if (!selected_folder)
-               item->uid_next = uid_next;
-
        /* 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)) {
+       debug_print("uid_next is %d and item->uid_next %d \n", 
+               uid_next, item->uid_next);
+       if (uid_next == item->uid_next) {
                nummsgs = g_slist_length(item->uid_list);
 
                /* If number of messages is still the same we
@@ -2842,7 +3196,10 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                   we discard our cache to start a new scan to find
                   out which numbers have been removed */
                if (exists == nummsgs) {
+                       debug_print("exists == nummsgs\n");
                        *msgnum_list = g_slist_copy(item->uid_list);
+                       statusbar_pop_all();
+                       unlock_session();
                        return nummsgs;
                } else if (exists < nummsgs) {
                        debug_print("Freeing imap uid cache");
@@ -2854,13 +3211,16 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 
        if (exists == 0) {
                *msgnum_list = NULL;
+               statusbar_pop_all();
+               unlock_session();
                return 0;
        }
 
-       nummsgs = get_list_of_uids(folder, item, &uidlist);
+       nummsgs = get_list_of_uids(session, folder, item, &uidlist);
 
        if (nummsgs < 0) {
-               
+               statusbar_pop_all();
+               unlock_session();
                return -1;
        }
 
@@ -2876,7 +3236,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 
                g_slist_free(*msgnum_list);
 
-               nummsgs = get_list_of_uids(folder, item, &uidlist);
+               nummsgs = get_list_of_uids(session, folder, item, &uidlist);
        }
 
        *msgnum_list = uidlist;
@@ -2886,8 +3246,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        remove_numbered_files_not_in_list(dir, *msgnum_list);
        g_free(dir);
        
-       debug_print("get_num_list - ok - %i\n", nummsgs);
+       item->uid_next = uid_next;
        
+       debug_print("get_num_list - ok - %i\n", nummsgs);
+       statusbar_pop_all();
+       unlock_session();
        return nummsgs;
 }
 
@@ -2902,9 +3265,9 @@ static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
        g_return_val_if_fail(item != NULL, NULL);
        g_return_val_if_fail(file != NULL, NULL);
 
-       if (item->stype == F_QUEUE) {
+       if (folder_has_parent_of_type(item, F_QUEUE)) {
                MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
+       } else if (folder_has_parent_of_type(item, F_DRAFT)) {
                MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
 
@@ -2932,14 +3295,16 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
 
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, NULL);
-
+       lock_session();
        debug_print("IMAP getting msginfos\n");
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
-       if (ok != IMAP_SUCCESS)
+       if (ok != IMAP_SUCCESS) {
+               unlock_session();
                return NULL;
-
-       if (!(item->stype == F_QUEUE || item->stype == F_DRAFT)) {
+       }
+       if (!(folder_has_parent_of_type(item, F_DRAFT) || 
+             folder_has_parent_of_type(item, F_QUEUE))) {
                ret = g_slist_concat(ret,
                        imap_get_uncached_messages(session, item,
                                                   msgnum_list));
@@ -2971,6 +3336,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                                                }
                                                g_free(file);
                                        }
+                                       session_set_access_time(SESSION(session));
                                }
 
                                if (elem == NULL)
@@ -2983,7 +3349,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
 
                g_slist_free(sorted_list);
        }
-
+       unlock_session();
        return ret;
 }
 
@@ -3009,8 +3375,8 @@ 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;
+       gint ok, exists = 0, unseen = 0;
+       guint32 uid_next, uid_val;
        gboolean selected_folder;
        
        g_return_val_if_fail(folder != NULL, FALSE);
@@ -3023,27 +3389,47 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
 
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, FALSE);
-
+       lock_session();
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
-               if (ok != IMAP_SUCCESS)
-                       return FALSE;
+               if (ok != IMAP_SUCCESS) {
+                       debug_print("disconnected!\n");
+                       session = imap_reconnect_if_possible(folder, session);
+                       if (session == NULL)
+                               return FALSE;
+                       lock_session();
+               }
 
                if (session->folder_content_changed
-               ||  session->exists != item->item.total_msgs)
+               ||  session->exists != item->item.total_msgs) {
+                       unlock_session();
                        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)
+               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
+                                &exists, &uid_next, &uid_val, &unseen, FALSE);
+               if (ok != IMAP_SUCCESS) {
+                       unlock_session();
                        return FALSE;
+               }
 
-               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs))
+               item->use_cache = time(NULL);
+               item->c_messages = exists;
+               item->c_uid_next = uid_next;
+               item->c_uid_validity = uid_val;
+               item->c_unseen = unseen;
+               item->item.last_num = uid_next - 1;
+               debug_print("uidnext %d, item->uid_next %d, exists %d, item->item.total_msgs %d\n", 
+                       uid_next, item->uid_next, exists, item->item.total_msgs);
+               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)
+                   || unseen != item->item.unread_msgs || uid_val != item->item.mtime) {
+                       unlock_session();
                        return TRUE;
+               }
        }
-
+       unlock_session();
        return FALSE;
 }
 
@@ -3066,8 +3452,10 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        if (!session) {
                return;
        }
+       lock_session();
        if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
            NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
+               unlock_session();
                return;
        }
 
@@ -3084,7 +3472,12 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        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;
+               flags_unset |= IMAP_FLAG_ANSWERED;
+
+       if (!MSG_IS_DELETED(msginfo->flags) &&  (newflags & MSG_DELETED))
+               flags_set |= IMAP_FLAG_DELETED;
+       if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
+               flags_unset |= IMAP_FLAG_DELETED;
 
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(msginfo->msgnum);
@@ -3097,30 +3490,38 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
                 */
                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));
+                       ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->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);
+                               ht_data->item = IMAP_FOLDER_ITEM(item);
+                               g_hash_table_insert(IMAP_FOLDER_ITEM(item)->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));
+                       ht_data = g_hash_table_lookup(IMAP_FOLDER_ITEM(item)->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);
+                               ht_data->item = IMAP_FOLDER_ITEM(item);
+                               g_hash_table_insert(IMAP_FOLDER_ITEM(item)->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));         
+                               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) {
+                               unlock_session();
                                return;
                        }
                }
@@ -3128,12 +3529,13 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
                if (flags_unset) {
                        ok = imap_set_message_flags(session, &numlist, flags_unset, FALSE);
                        if (ok != IMAP_SUCCESS) {
+                               unlock_session();
                                return;
                        }
                }
        }
        msginfo->flags.perm_flags = newflags;
-       
+       unlock_session();
        return;
 }
 
@@ -3150,12 +3552,13 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
 
        session = imap_session_get(folder);
        if (!session) return -1;
-
+       lock_session();
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
-       if (ok != IMAP_SUCCESS)
+       if (ok != IMAP_SUCCESS) {
+               unlock_session();
                return ok;
-
+       }
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(uid);
        
@@ -3164,6 +3567,7 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
                &numlist, IMAP_FLAG_DELETED, TRUE);
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't set deleted flags: %d\n"), uid);
+               unlock_session();
                return ok;
        }
 
@@ -3178,6 +3582,7 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
        }
        if (ok != IMAP_SUCCESS) {
                log_warning(_("can't expunge\n"));
+               unlock_session();
                return ok;
        }
 
@@ -3187,7 +3592,7 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
        if (is_dir_exist(dir))
                remove_numbered_files(dir, uid, uid);
        g_free(dir);
-
+       unlock_session();
        return IMAP_SUCCESS;
 }
 
@@ -3223,6 +3628,7 @@ typedef struct _get_flags_data {
        FolderItem *item;
        MsgInfoList *msginfo_list;
        GRelation *msgflags;
+       gboolean full_search;
        gboolean done;
 } get_flags_data;
 
@@ -3233,44 +3639,39 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
        FolderItem *item = stuff->item;
        MsgInfoList *msginfo_list = stuff->msginfo_list;
        GRelation *msgflags = stuff->msgflags;
+       gboolean full_search = stuff->full_search;
        IMAPSession *session;
-       GSList *sorted_list;
-       GSList *unseen = NULL, *answered = NULL, *flagged = NULL;
-       GSList *p_unseen, *p_answered, *p_flagged;
+       GSList *sorted_list = NULL;
+       GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL;
+       GSList *p_unseen, *p_answered, *p_flagged, *p_deleted;
        GSList *elem;
        GSList *seq_list, *cur;
        gboolean reverse_seen = FALSE;
        GString *cmd_buf;
        gint ok;
-       gint exists_cnt, recent_cnt, unseen_cnt, uid_next;
-       guint32 uidvalidity;
+       gint exists_cnt, unseen_cnt;
        gboolean selected_folder;
        
        if (folder == NULL || item == NULL) {
                stuff->done = TRUE;
                return GINT_TO_POINTER(-1);
        }
-       if (msginfo_list == NULL) {
-               stuff->done = TRUE;
-               return GINT_TO_POINTER(0);
-       }
 
        session = imap_session_get(folder);
        if (session == NULL) {
                stuff->done = TRUE;
                return GINT_TO_POINTER(-1);
        }
-
+       lock_session();
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->path));
 
        if (!selected_folder) {
-               ok = imap_status(session, IMAP_FOLDER(folder), item->path,
-                        &exists_cnt, &recent_cnt, &uid_next, &uidvalidity, &unseen_cnt, TRUE);
                ok = imap_select(session, IMAP_FOLDER(folder), item->path,
-                       NULL, NULL, NULL, NULL, TRUE);
+                       &exists_cnt, NULL, &unseen_cnt, NULL, TRUE);
                if (ok != IMAP_SUCCESS) {
                        stuff->done = TRUE;
+                       unlock_session();
                        return GINT_TO_POINTER(-1);
                }
 
@@ -3285,8 +3686,13 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
        cmd_buf = g_string_new(NULL);
 
        sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
-
-       seq_list = imap_get_lep_set_from_msglist(msginfo_list);
+       if (!full_search) {
+               seq_list = imap_get_lep_set_from_msglist(msginfo_list);
+       } else {
+               struct mailimap_set * set;
+               set = mailimap_set_new_interval(1, 0);
+               seq_list = g_slist_append(NULL, set);
+       }
 
        for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
                struct mailimap_set * imapset;
@@ -3312,32 +3718,46 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                        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);
-               }
-
                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);
                }
+
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       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);
+                       }
+
+                       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
+                                                imapset, &lep_uidlist);
+                       if (r == MAILIMAP_NO_ERROR) {
+                               GSList * uidlist;
+
+                               uidlist = imap_uid_list_from_lep(lep_uidlist);
+                               mailimap_search_result_free(lep_uidlist);
+
+                               deleted = g_slist_concat(deleted, uidlist);
+                       }
+               }
        }
 
        p_unseen = unseen;
        p_answered = answered;
        p_flagged = flagged;
+       p_deleted = deleted;
 
        for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
                MsgInfo *msginfo;
@@ -3347,7 +3767,11 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                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 (item->opened || item->processing_pending || item == folder->inbox) {
+                       flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
+               } else {
+                       flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
+               }
                if (reverse_seen)
                        flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
                if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
@@ -3357,21 +3781,35 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                                flags &= ~(MSG_UNREAD | MSG_NEW);
                        }
                }
-               if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
-                       flags |= MSG_REPLIED;
+               
                if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
                        flags |= MSG_MARKED;
+               else
+                       flags &= ~MSG_MARKED;
+
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
+                               flags |= MSG_REPLIED;
+                       else
+                               flags &= ~MSG_REPLIED;
+                       if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
+                               flags |= MSG_DELETED;
+                       else
+                               flags &= ~MSG_DELETED;
+               }
                g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
        }
 
        imap_lep_set_free(seq_list);
        g_slist_free(flagged);
+       g_slist_free(deleted);
        g_slist_free(answered);
        g_slist_free(unseen);
        g_slist_free(sorted_list);
        g_string_free(cmd_buf, TRUE);
 
        stuff->done = TRUE;
+       unlock_session();
        return GINT_TO_POINTER(0);
 }
 
@@ -3385,12 +3823,28 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
        data->item = item;
        data->msginfo_list = msginfo_list;
        data->msgflags = msgflags;
+       data->full_search = FALSE;
 
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       GSList *tmp = NULL, *cur;
+       
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
 
+       tmp = folder_item_get_msg_list(item);
+
+       if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
+               data->full_search = TRUE;
+       
+       for (cur = tmp; cur; cur = cur->next)
+               procmsg_msginfo_free((MsgInfo *)cur->data);
+       
+       g_slist_free(tmp);
+
        result = GPOINTER_TO_INT(imap_get_flags_thread(data));
        
        g_free(data);
@@ -3403,70 +3857,72 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
        gboolean flags_set = GPOINTER_TO_INT(user_data);
        gint flags_value = GPOINTER_TO_INT(key);
        hashtable_data *data = (hashtable_data *)value;
-       
+       IMAPFolderItem *_item = data->item;
+       FolderItem *item = (FolderItem *)_item;
+       gint ok = IMAP_ERROR;
+       IMAPSession *session = imap_session_get(item->folder);
+
        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);
        
+       if (session) {
+               lock_session();
+               ok = imap_select(session, IMAP_FOLDER(item->folder), item->path,
+                        NULL, NULL, NULL, NULL, FALSE);
+       }
+       if (ok == IMAP_SUCCESS) {
+               imap_set_message_flags(data->session, data->msglist, flags_value, flags_set);
+       } else {
+               g_warning("can't select mailbox %s\n", item->path);
+       }
+       if (session)
+               unlock_session();
        g_slist_free(data->msglist);    
        g_free(data);
        return TRUE;
 }
 
-static void process_hashtable(void)
+static void process_hashtable(IMAPFolderItem *item)
 {
-       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 (item->flags_set_table) {
+               g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
+               g_hash_table_destroy(item->flags_set_table);
+               item->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;
+       if (item->flags_unset_table) {
+               g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
+               g_hash_table_destroy(item->flags_unset_table);
+               item->flags_unset_table = NULL;
        }
 }
 
-static IMAPFolderItem *batching_item = NULL;
-
 static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
 {
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
 
        g_return_if_fail(item != NULL);
        
-       if (batch && batching_item != NULL) {
-               g_warning("already batching on %s\n", batching_item->item.path);
-               return;
-       }
-       
        if (item->batching == batch)
                return;
        
-       item->batching = batch;
-       
-       batching_item = batch?item:NULL;
-       
        if (batch) {
+               item->batching = TRUE;
                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 (!item->flags_set_table) {
+                       item->flags_set_table = g_hash_table_new(NULL, g_direct_equal);
                }
-               if (flags_unset_table) {
-                       g_warning("flags_unset_table non-null but we just entered batch mode!\n");
-                       flags_unset_table = NULL;
+               if (!item->flags_unset_table) {
+                       item->flags_unset_table = g_hash_table_new(NULL, g_direct_equal);
                }
-               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();
+               process_hashtable(item);
+               item->batching = FALSE;
        }
 }
 
@@ -3518,7 +3974,7 @@ static int imap_flags_to_flags(struct mailimap_mbx_list_flags * imap_flags)
 }
 
 static GSList * imap_list_from_lep(IMAPFolder * folder,
-                                  clist * list, const gchar * real_path)
+                                  clist * list, const gchar * real_path, gboolean all)
 {
        clistiter * iter;
        GSList * item_list;
@@ -3538,7 +3994,10 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                FolderItem *new_item;
                
                mb = clist_content(iter);
-               
+
+               if (mb == NULL)
+                       continue;
+
                flags = 0;
                if (mb->mb_flag != NULL)
                        flags = imap_flags_to_flags(mb->mb_flag);
@@ -3557,13 +4016,13 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                        continue;
                }
                
-               if (strcmp(dup_name, real_path) == 0) {
+               if (!all && strcmp(dup_name, real_path) == 0) {
                        g_free(base);
                        free(dup_name);
                        continue;
                }
 
-               if (dup_name[strlen(dup_name)-1] == '/') {
+               if (!all && dup_name[strlen(dup_name)-1] == '/') {
                        g_free(base);
                        free(dup_name);
                        continue;
@@ -3618,7 +4077,7 @@ static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
                        continue;
                
                item_count ++;
-               
+
                last = GPOINTER_TO_INT(cur->data);
                if (cur->next)
                        next = GPOINTER_TO_INT(cur->next->data);
@@ -3714,10 +4173,13 @@ static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
        size_t size = 0;
        MsgFlags flags = {0, 0};
        
+       if (info->headers == NULL)
+               return NULL;
+
        MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
-       if (item->stype == F_QUEUE) {
+       if (folder_has_parent_of_type(item, F_QUEUE)) {
                MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
+       } else if (folder_has_parent_of_type(item, F_DRAFT)) {
                MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
        flags.perm_flags = info->flags;
@@ -3792,6 +4254,9 @@ void imap_folder_unref(Folder *folder)
 
 static FolderClass imap_class;
 
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
+
 static Folder  *imap_folder_new        (const gchar    *name,
                                         const gchar    *path)
 {
@@ -3814,6 +4279,11 @@ static gint      imap_rename_folder      (Folder         *folder,
        return -1;
 }
 
+gchar imap_get_path_separator_for_item(FolderItem *item)
+{
+       return '/';
+}
+
 FolderClass *imap_get_class(void)
 {
        if (imap_class.idstr == NULL) {
@@ -3825,9 +4295,51 @@ FolderClass *imap_get_class(void)
                imap_class.create_tree = imap_create_tree;
                imap_class.create_folder = imap_create_folder;
                imap_class.rename_folder = imap_rename_folder;
+
+               imap_class.set_xml = folder_set_xml;
+               imap_class.get_xml = folder_get_xml;
+               imap_class.item_set_xml = imap_item_set_xml;
+               imap_class.item_get_xml = imap_item_get_xml;
                /* nothing implemented */
        }
 
        return &imap_class;
 }
 #endif
+
+void imap_synchronise(FolderItem *item) 
+{
+       imap_gtk_synchronise(item);
+}
+
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
+{
+#ifdef HAVE_LIBETPAN
+       GList *cur;
+#endif
+       folder_item_set_xml(folder, item, tag);
+       
+#ifdef HAVE_LIBETPAN
+       for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
+               XMLAttr *attr = (XMLAttr *) cur->data;
+
+               if (!attr || !attr->name || !attr->value) continue;
+               if (!strcmp(attr->name, "uidnext"))
+                       IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
+       }
+#endif
+}
+
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
+{
+       XMLTag *tag;
+
+       tag = folder_item_get_xml(folder, item);
+
+#ifdef HAVE_LIBETPAN
+       xml_tag_add_attr(tag, xml_attr_new_int("uidnext", 
+                       IMAP_FOLDER_ITEM(item)->uid_next));
+
+#endif
+       return tag;
+}