2007-03-04 [iwkse] 2.8.0cvs12
[claws.git] / src / imap.c
index f7a5cea66aa040db67ae64019bde8a47980378bc..b4d749eec1fceb9a43da335ed38716296d833108 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,6 +30,8 @@
 #include "imap.h"
 #include "imap_gtk.h"
 #include "inc.h"
+#include "xml.h"
+#include "alertpanel.h"
 
 #ifdef HAVE_LIBETPAN
 
 #include "inputdialog.h"
 #include "log.h"
 #include "remotefolder.h"
-#include "alertpanel.h"
-#include "sylpheed.h"
+#include "claws.h"
 #include "statusbar.h"
 #include "msgcache.h"
 #include "imap-thread.h"
+#include "account.h"
 
 typedef struct _IMAPFolder     IMAPFolder;
 typedef struct _IMAPSession    IMAPSession;
@@ -168,6 +170,9 @@ struct _IMAPFolderItem
        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);
@@ -249,7 +254,8 @@ static gint imap_auth                       (IMAPSession    *session,
                                         IMAPAuthType    type);
 
 static gint imap_scan_tree_recursive   (IMAPSession    *session,
-                                        FolderItem     *item);
+                                        FolderItem     *item,
+                                        gboolean        subs_only);
 
 static void imap_create_missing_folders        (Folder         *folder);
 static FolderItem *imap_create_special_folder
@@ -288,11 +294,11 @@ static gint imap_status                   (IMAPSession    *session,
                                         gint           *unseen,
                                         gboolean        block);
 
-static IMAPNameSpace *imap_find_namespace      (IMAPFolder     *folder,
-                                                const gchar    *path);
-static gchar imap_get_path_separator           (IMAPFolder     *folder,
+static gchar imap_get_path_separator           (IMAPSession    *session,
+                                                IMAPFolder     *folder,
                                                 const gchar    *path);
-static gchar *imap_get_real_path               (IMAPFolder     *folder,
+static gchar *imap_get_real_path               (IMAPSession    *session,
+                                                IMAPFolder     *folder,
                                                 const gchar    *path);
 static void imap_synchronise           (FolderItem     *item);
 
@@ -303,7 +309,6 @@ static gint imap_cmd_login  (IMAPSession    *session,
                                 const gchar    *user,
                                 const gchar    *pass,
                                 const gchar    *type);
-static gint imap_cmd_logout    (IMAPSession    *session);
 static gint imap_cmd_noop      (IMAPSession    *session);
 #if USE_OPENSSL
 static gint imap_cmd_starttls  (IMAPSession    *session);
@@ -342,7 +347,9 @@ static gint imap_cmd_append (IMAPSession    *session,
 static gint imap_cmd_copy       (IMAPSession *session,
                                 struct mailimap_set * set,
                                 const gchar *destfolder,
-                                GRelation *uid_mapping);
+                                GRelation *uid_mapping,
+                                struct mailimap_set ** source,
+                                struct mailimap_set ** dest);
 static gint imap_cmd_store     (IMAPSession    *session,
                                 struct mailimap_set * set,
                                 IMAPFlags flags,
@@ -383,7 +390,7 @@ static gchar *imap_item_get_path            (Folder         *folder,
 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
 
 
-/* data types conversion libetpan <-> sylpheed */
+/* data types conversion libetpan <-> claws */
 static GSList * imap_list_from_lep(IMAPFolder * folder,
                                   clist * list, const gchar * real_path, gboolean all);
 static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist);
@@ -436,6 +443,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;
@@ -475,16 +486,9 @@ static Folder *imap_folder_new(const gchar *name, const gchar *path)
 
 static void imap_folder_destroy(Folder *folder)
 {
-       gchar *dir;
-
        while (imap_folder_get_refcnt(folder) > 0)
                gtk_main_iteration();
        
-       dir = imap_folder_get_path(folder);
-       if (is_dir_exist(dir))
-               remove_dir_recursive(dir);
-       g_free(dir);
-
        folder_remote_folder_destroy(REMOTE_FOLDER(folder));
        imap_done(folder);
 }
@@ -522,7 +526,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;
        
@@ -538,18 +541,24 @@ 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)
+static int imap_get_capabilities(IMAPSession *session)
 {
        struct mailimap_capability_data *capabilities = NULL;
        clistiter *cur;
+       int result = -1;
 
        if (session->capability != NULL)
-               return;
+               return MAILIMAP_NO_ERROR;
 
-       capabilities = imap_threaded_capability(session->folder);
+       capabilities = imap_threaded_capability(session->folder, &result);
 
-       if (capabilities == NULL)
-               return;
+       if (result != MAILIMAP_NO_ERROR) {
+               return MAILIMAP_ERROR_CAPABILITY;
+       }
+
+       if (capabilities == NULL) {
+               return MAILIMAP_NO_ERROR;
+       }
 
        for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
            cur = clist_next(cur)) {
@@ -563,9 +572,10 @@ void imap_get_capabilities(IMAPSession *session)
                debug_print("got capa %s\n", cap->cap_data.cap_name);
        }
        mailimap_capability_data_free(capabilities);
+       return MAILIMAP_NO_ERROR;
 }
 
-gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
+static gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
 {
        GSList *cur;
        for (cur = session->capability; cur; cur = cur->next) {
@@ -580,40 +590,51 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
 {
        gint ok = IMAP_ERROR;
        static time_t last_login_err = 0;
+       gchar *ext_info = "";
        
-       imap_get_capabilities(session);
+       if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
 
        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;
+       case IMAP_AUTH_GSSAPI:
+               ok = imap_cmd_login(session, user, pass, "GSSAPI");
+               break;
        default:
                debug_print("capabilities:\n"
+                               "\t ANONYMOUS %d\n"
                                "\t CRAM-MD5 %d\n"
-                               "\t LOGIN %d\n", 
+                               "\t LOGIN %d\n"
+                               "\t GSSAPI %d\n", 
+                       imap_has_capability(session, "ANONYMOUS"),
                        imap_has_capability(session, "CRAM-MD5"),
-                       imap_has_capability(session, "LOGIN"));
+                       imap_has_capability(session, "LOGIN"),
+                       imap_has_capability(session, "GSSAPI"));
                if (imap_has_capability(session, "CRAM-MD5"))
                        ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
+               if (ok == IMAP_ERROR && imap_has_capability(session, "GSSAPI"))
+                       ok = imap_cmd_login(session, user, pass, "GSSAPI");
                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: "
@@ -655,6 +676,7 @@ static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *sess
                   it will not try to reconnect again and so avoid an
                   endless loop */
                rfolder->session = NULL;
+               debug_print("getting session...\n");
                session = imap_session_get(folder);
                rfolder->session = SESSION(session);
                statusbar_pop_all();
@@ -663,13 +685,23 @@ static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *sess
 }
 
 #define lock_session() {\
-       debug_print("locking session\n"); \
-       session->busy = TRUE;\
+       if (session) { \
+               debug_print("locking session %p (%d)\n", session, session->busy); \
+               if (session->busy) \
+                       debug_print("         SESSION WAS LOCKED !!      \n"); \
+               session->busy = TRUE;\
+       } else {\
+               debug_print("can't lock null session\n"); \
+       }\
 }
 
 #define unlock_session() {\
-       debug_print("unlocking session\n"); \
-       session->busy = FALSE;\
+       if (session) { \
+               debug_print("unlocking session %p\n", session); \
+               session->busy = FALSE;\
+       } else {\
+               debug_print("can't unlock null session\n"); \
+       }\
 }
 
 static IMAPSession *imap_session_get(Folder *folder)
@@ -681,34 +713,42 @@ static IMAPSession *imap_session_get(Folder *folder)
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
        
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Claws Mail 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) - rfolder->last_failure <= 2)
+                       return NULL;
                session = imap_session_new(folder, folder->account);
        }
-       if(session == NULL)
+       if(session == NULL) {
+               rfolder->last_failure = time(NULL);
                return NULL;
+       }
 
        /* Make sure session is authenticated */
        if (!IMAP_SESSION(session)->authenticated)
                imap_session_authenticate(IMAP_SESSION(session), folder->account);
        
        if (!IMAP_SESSION(session)->authenticated) {
+               imap_threaded_disconnect(session->folder);
+               SESSION(session)->state = SESSION_DISCONNECTED;
                session_destroy(SESSION(session));
                rfolder->session = NULL;
+               rfolder->last_failure = time(NULL);
                return NULL;
        }
 
+       lock_session();
+
        /* 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
@@ -726,7 +766,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        }
 
        rfolder->session = SESSION(session);
-       
+
        return IMAP_SESSION(session);
 }
 
@@ -736,7 +776,7 @@ static IMAPSession *imap_session_new(Folder * folder,
        IMAPSession *session;
        gushort port;
        int r;
-       int authenticated;
+       int authenticated = FALSE;
        
 #ifdef USE_OPENSSL
        /* FIXME: IMAP over SSL only... */ 
@@ -750,14 +790,13 @@ static IMAPSession *imap_session_new(Folder * folder,
                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"
+                         "build of Claws Mail. \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)
+                         GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
+                         NULL, FALSE, NULL, ALERT_WARNING,
+                         G_ALERTDEFAULT) != G_ALERTALTERNATE)
                        return NULL;
        }
        port = account->set_imapport ? account->imapport
@@ -796,8 +835,14 @@ static IMAPSession *imap_session_new(Folder * folder,
                authenticated = FALSE;
        }
        else {
+#if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
+#ifdef USE_OPENSSL
+               if (r == MAILIMAP_ERROR_SSL)
+                       log_error(_("SSL handshake failed\n"));
+#endif
+#endif
                if(!prefs_common.no_recv_err_panel) {
-                       alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
+                       alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
                                         account->recv_server, port);
                } else {
                        log_error(_("Can't connect to IMAP4 server: %s:%d\n"),
@@ -849,30 +894,46 @@ static IMAPSession *imap_session_new(Folder * folder,
 static void imap_session_authenticate(IMAPSession *session, 
                                      const PrefsAccount *account)
 {
-       gchar *pass;
+       gchar *pass, *acc_pass;
+       gboolean failed = FALSE;
 
        g_return_if_fail(account->userid != NULL);
-
-       pass = account->passwd;
-       if (!pass) {
+       acc_pass = account->passwd;
+try_again:
+       pass = acc_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);
        if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
-               imap_threaded_disconnect(session->folder);
-               imap_cmd_logout(session);
                statusbar_pop_all();
                
+               if (!failed) {
+                       acc_pass = NULL;
+                       failed = TRUE;
+                       goto try_again;
+               } else {
+                       if (prefs_common.no_recv_err_panel) {
+                               log_error(_("Couldn't login to IMAP server %s."), account->recv_server);
+                               mainwindow_show_error();
+                       } else
+                               alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
+               }               
+
                return;
-       }
+       } 
+
        statusbar_pop_all();
        session->authenticated = TRUE;
+       return;
 }
 
 static void imap_session_destroy(Session *session)
@@ -943,16 +1004,16 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
 
                if (cached)
                        debug_print("message %d has been already %scached (%d/%d).\n", uid,
-                               have_size == cached->size ? "fully ":"",
+                               have_size >= cached->size ? "fully ":"",
                                have_size, (int)cached->size);
                
-               if (cached && (cached->size == have_size || !body)) {
+               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");
+               } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
+                       debug_print("message not cached and file recent, considering file complete\n");
                        procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
@@ -962,6 +1023,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                }
        }
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        
        if (!session) {
@@ -969,8 +1031,6 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                return NULL;
        }
 
-       lock_session();
-
        debug_print("IMAP fetching messages\n");
        ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
@@ -1031,12 +1091,12 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(file_list != NULL, -1);
        
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                return -1;
        }
-       lock_session();
-       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+       destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
 
        statusbar_print_all(_("Adding messages..."));
        total = g_slist_length(file_list);
@@ -1044,10 +1104,9 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                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);
+               statusbar_progress_all(curnum, total, total < 10 ? 1:10);
                curnum++;
 
                if (fileinfo->flags) {
@@ -1059,28 +1118,6 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                iflags |= IMAP_FLAG_SEEN;
                }
                
-               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);
                
@@ -1095,8 +1132,6 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
 
                if (ok != IMAP_SUCCESS) {
                        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();
@@ -1130,10 +1165,9 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                if (new_uid == 0) {
                        new_uid = dest->last_num+1;
                }
-               if (last_uid < new_uid)
+               if (last_uid < new_uid) {
                        last_uid = new_uid;
-               if (file_is_tmp)
-                       g_unlink(real_file);
+               }
 
                g_free(real_file);
        }
@@ -1159,19 +1193,22 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        gint ok = IMAP_SUCCESS;
        GRelation *uid_mapping;
        gint last_num = 0;
+       gboolean single = FALSE;
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
        
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        
        if (!session) {
                return -1;
        }
-       lock_session();
-       msginfo = (MsgInfo *)msglist->data;
 
+       msginfo = (MsgInfo *)msglist->data;
+       if (msglist->next == NULL)
+               single = TRUE;
        src = msginfo->folder;
        if (src == dest) {
                g_warning("the src folder is identical to the dest.\n");
@@ -1179,6 +1216,28 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                return -1;
        }
 
+       if (src->folder != dest->folder) {
+               GSList *infolist = NULL, *cur;
+               int res = -1;
+               for (cur = msglist; cur; cur = cur->next) {
+                       msginfo = (MsgInfo *)cur->data;
+                       MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
+                       fileinfo->file = procmsg_get_message_file(msginfo);
+                       fileinfo->flags = &(msginfo->flags);
+                       infolist = g_slist_prepend(infolist, fileinfo);
+               }
+               infolist = g_slist_reverse(infolist);
+               unlock_session();
+               res = folder_item_add_msgs(dest, infolist, FALSE);
+               for (cur = infolist; cur; cur = cur->next) {
+                       MsgFileInfo *info = (MsgFileInfo *)cur->data;
+                       g_free(info->file);
+                       g_free(info);
+               }
+               g_slist_free(infolist);
+               return res;
+       } 
+
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
@@ -1186,7 +1245,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                return ok;
        }
 
-       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+       destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
        seq_list = imap_get_lep_set_from_msglist(msglist);
        uid_mapping = g_relation_new(2);
        g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
@@ -1194,16 +1253,41 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        statusbar_print_all(_("Copying messages..."));
        for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
                struct mailimap_set * seq_set;
+               struct mailimap_set * source = NULL;
+               struct mailimap_set * dest = NULL;
                seq_set = cur->data;
 
                debug_print("Copying messages from %s to %s ...\n",
                            src->path, destdir);
 
-               ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping);
+               ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
+                       &source, &dest);
+               
+               if (ok == IMAP_SUCCESS) {
+                       if (single && relation && source && dest) {
+                               clistiter *l = clist_begin(source->set_list);
+                               struct mailimap_set_item *i = (struct mailimap_set_item *)clist_content(l);
+                               int snum = i->set_first;
+                               int dnum = 0;
+                               l = clist_begin(dest->set_list);
+                               i = (struct mailimap_set_item *)clist_content(l);
+                               dnum = i->set_first;
+                               g_relation_insert(uid_mapping, GINT_TO_POINTER(snum), 
+                                       GINT_TO_POINTER(dnum));
+                       }
+               }
+
+
+               if (source)
+                       mailimap_set_free(source);
+               if (dest)
+                       mailimap_set_free(dest);
+
                if (ok != IMAP_SUCCESS) {
                        g_relation_destroy(uid_mapping);
                        imap_lep_set_free(seq_list);
                        unlock_session();
+                       statusbar_pop_all();
                        return -1;
                }
        }
@@ -1218,12 +1302,36 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                if (tuples->len > 0) {
                        gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
                        g_relation_insert(relation, msginfo,
-                                         GPOINTER_TO_INT(num));
+                                         GINT_TO_POINTER(num));
                        if (num > last_num)
                                last_num = num;
+                       debug_print("copied new message as %d\n", num);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (num > 0) {
+                               gchar *cache_path = folder_item_get_path(msginfo->folder);
+                               gchar *real_file = g_strconcat(
+                                       cache_path, G_DIR_SEPARATOR_S, 
+                                       itos(msginfo->msgnum), NULL);
+                               gchar *cache_file = NULL;
+                               g_free(cache_path);
+                               cache_path = folder_item_get_path(dest);
+                               cache_file = g_strconcat(
+                                       cache_path, G_DIR_SEPARATOR_S, 
+                                       itos(num), NULL);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                               }
+                               g_free(real_file);
+                               g_free(cache_file);
+                               g_free(cache_path);
+                       }
                } else
                        g_relation_insert(relation, msginfo,
-                                         GPOINTER_TO_INT(0));
+                                         GINT_TO_POINTER(0));
                g_tuples_destroy(tuples);
        }
        statusbar_pop_all();
@@ -1261,7 +1369,6 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
                    MsgInfoList *msglist, GRelation *relation)
 {
        MsgInfo *msginfo;
-       GSList *file_list;
        gint ret;
 
        g_return_val_if_fail(folder != NULL, -1);
@@ -1271,30 +1378,7 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       /* 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);
-
+       ret = imap_do_copy_msgs(folder, dest, msglist, relation);
        return ret;
 }
 
@@ -1302,7 +1386,7 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest, 
                                MsgInfoList *msglist, GRelation *relation)
 {
-       gchar *destdir;
+       gchar *destdir, *dir;
        GSList *numlist = NULL, *cur;
        MsgInfo *msginfo;
        IMAPSession *session;
@@ -1313,11 +1397,12 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                return -1;
        }
-       lock_session();
+
        msginfo = (MsgInfo *)msglist->data;
 
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
@@ -1327,12 +1412,13 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
                return ok;
        }
 
-       destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
+       destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path);
        for (cur = msglist; cur; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (!MSG_IS_DELETED(msginfo->flags))
-                       numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
+                       numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
        }
+       numlist = g_slist_reverse(numlist);
 
        uid_mapping = g_relation_new(2);
        g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
@@ -1352,6 +1438,15 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
                return ok;
        }
        
+       dir = folder_item_get_path(msginfo->folder);
+       if (is_dir_exist(dir)) {
+               for (cur = msglist; cur; cur = cur->next) {
+                       msginfo = (MsgInfo *)cur->data;
+                       remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
+               }
+       }
+       g_free(dir);
+
        g_relation_destroy(uid_mapping);
        g_slist_free(numlist);
 
@@ -1399,7 +1494,7 @@ static gint imap_close(Folder *folder, FolderItem *item)
        return 0;
 }
 
-static gint imap_scan_tree(Folder *folder)
+gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
 {
        FolderItem *item = NULL;
        IMAPSession *session;
@@ -1408,6 +1503,7 @@ static gint imap_scan_tree(Folder *folder)
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(folder->account != NULL, -1);
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                if (!folder->node) {
@@ -1419,7 +1515,6 @@ 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;
@@ -1428,12 +1523,12 @@ static gint imap_scan_tree(Folder *folder)
                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),
+                          imap_get_path_separator(session, IMAP_FOLDER(folder),
                                                   root_folder),
                           '/');
                strtailchomp(root_folder, '/');
                real_path = imap_get_real_path
-                       (IMAP_FOLDER(folder), root_folder);
+                       (session, IMAP_FOLDER(folder), root_folder);
                debug_print("IMAP root directory: %s\n", real_path);
 
                /* check if root directory exist */
@@ -1450,12 +1545,17 @@ static gint imap_scan_tree(Folder *folder)
                        return -1;
                }
                mailimap_list_result_free(lep_list);
-               
+                               
                g_free(real_path);
        }
 
        if (folder->node)
                item = FOLDER_ITEM(folder->node->data);
+               
+       if (item && !item->path && root_folder) {
+               item->path = g_strdup(root_folder);
+       }
+
        if (!item || ((item->path || root_folder) &&
                      strcmp2(item->path, root_folder) != 0)) {
                folder_tree_destroy(folder);
@@ -1464,14 +1564,24 @@ static gint imap_scan_tree(Folder *folder)
                folder->node = item->node = g_node_new(item);
        }
 
-       imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data));
+       imap_scan_tree_recursive(session, FOLDER_ITEM(folder->node->data), subs_only);
        imap_create_missing_folders(folder);
        unlock_session();
 
        return 0;
 }
 
-static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
+static gint imap_scan_tree(Folder *folder)
+{
+       gboolean subs_only = FALSE;
+       if (folder->account) {
+               debug_print(" scanning only subs %d\n", folder->account->imap_subsonly);
+               subs_only = folder->account->imap_subsonly;
+       }
+       return imap_scan_tree_real(folder, subs_only);
+}
+
+static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gboolean subs_only)
 {
        Folder *folder;
        IMAPFolder *imapfolder;
@@ -1492,7 +1602,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
        folder = item->folder;
        imapfolder = IMAP_FOLDER(folder);
 
-       separator = imap_get_path_separator(imapfolder, item->path);
+       separator = imap_get_path_separator(session, imapfolder, item->path);
 
        if (folder->ui_func)
                folder->ui_func(folder, item, folder->ui_func_data);
@@ -1501,7 +1611,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
                wildcard[0] = separator;
                wildcard[1] = '%';
                wildcard[2] = '\0';
-               real_path = imap_get_real_path(imapfolder, item->path);
+               real_path = imap_get_real_path(session, imapfolder, item->path);
        } else {
                wildcard[0] = '%';
                wildcard[1] = '\0';
@@ -1511,7 +1621,12 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
        Xstrcat_a(wildcard_path, real_path, wildcard,
                  {g_free(real_path); return IMAP_ERROR;});
        lep_list = NULL;
-       r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
+       
+       if (subs_only)
+               r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
+       else
+               r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
+
        if (r != MAILIMAP_NO_ERROR) {
                item_list = NULL;
        }
@@ -1537,9 +1652,13 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
                        }
                }
                if (!new_item) {
-                       debug_print("folder '%s' not found. removing...\n",
-                                   old_item->path);
-                       folder_item_remove(old_item);
+                       if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
+                               debug_print("not removing INBOX\n");
+                       } else {
+                               debug_print("folder '%s' not found. removing...\n",
+                                           old_item->path);
+                               folder_item_remove(old_item);
+                       }
                } else {
                        old_item->no_sub = new_item->no_sub;
                        old_item->no_select = new_item->no_select;
@@ -1599,7 +1718,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
                }
 
                if (new_item->no_sub == FALSE)
-                       imap_scan_tree_recursive(session, new_item);
+                       imap_scan_tree_recursive(session, new_item, subs_only);
        }
 
        g_slist_free(item_list);
@@ -1607,6 +1726,104 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
        return IMAP_SUCCESS;
 }
 
+GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
+{
+       IMAPSession *session = imap_session_get(folder);
+       gchar *real_path;
+       gchar *wildcard_path;
+       gchar separator;
+       gchar wildcard[3];
+       clist * lep_list;
+       GSList *item_list = NULL, *cur;
+       GList *child_list = NULL, *tmplist = NULL;
+       GSList *sub_list = NULL;
+       int r;
+
+       if (!session)
+               return NULL;
+
+       separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
+
+       if (item->path) {
+               wildcard[0] = separator;
+               wildcard[1] = '%';
+               wildcard[2] = '\0';
+               real_path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
+       } else {
+               wildcard[0] = '%';
+               wildcard[1] = '\0';
+               real_path = g_strdup("");
+       }
+
+       Xstrcat_a(wildcard_path, real_path, wildcard,
+                 {g_free(real_path); return NULL;});
+       lep_list = NULL;
+       
+       if (unsubs_only)
+               statusbar_print_all(_("Looking for unsubscribed folders in %s..."), 
+                               item->path?item->path:item->name);
+       else
+               statusbar_print_all(_("Looking for subfolders of %s..."), 
+                               item->path?item->path:item->name);
+
+       r = imap_threaded_list(folder, "", wildcard_path, &lep_list);
+       if (r) {
+               statusbar_pop_all();
+               return NULL;
+       }
+       item_list = imap_list_from_lep(IMAP_FOLDER(folder),
+                              lep_list, real_path, FALSE);
+       mailimap_list_result_free(lep_list);
+
+       for (cur = item_list; cur != NULL; cur = cur->next) {
+               FolderItem *cur_item = FOLDER_ITEM(cur->data);
+               if (recursive) {
+                       tmplist = imap_scan_subtree(folder, cur_item, 
+                                       unsubs_only, recursive);
+                       if (tmplist)
+                               child_list = g_list_concat(child_list, tmplist);
+               }
+               child_list = g_list_prepend(child_list,
+                               imap_get_real_path(session, 
+                                       IMAP_FOLDER(folder), cur_item->path));
+               
+               folder_item_destroy(cur_item);
+       }
+       child_list = g_list_reverse(child_list);
+       g_slist_free(item_list);
+
+       if (unsubs_only) {
+               r = imap_threaded_lsub(folder, "", wildcard_path, &lep_list);
+               if (r) {
+                       statusbar_pop_all();
+                       return NULL;
+               }
+               sub_list = imap_list_from_lep(IMAP_FOLDER(folder),
+                                      lep_list, real_path, FALSE);
+               mailimap_list_result_free(lep_list);
+
+               for (cur = sub_list; cur != NULL; cur = cur->next) {
+                       FolderItem *cur_item = FOLDER_ITEM(cur->data);
+                       GList *oldlitem = NULL;
+                       gchar *tmp = imap_get_real_path(session, 
+                                       IMAP_FOLDER(folder), cur_item->path);
+                       folder_item_destroy(cur_item);
+                       oldlitem = g_list_find_custom(
+                                       child_list, tmp, (GCompareFunc)strcmp2);
+                       if (oldlitem) {
+                               child_list = g_list_remove_link(child_list, oldlitem);
+                               g_free(oldlitem->data);
+                               g_list_free(oldlitem);
+                       }
+                       g_free(tmp);
+               }
+       }
+
+       statusbar_pop_all();
+
+       return child_list;
+}
+
 static gint imap_create_tree(Folder *folder)
 {
        g_return_val_if_fail(folder != NULL, -1);
@@ -1729,18 +1946,19 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        const gchar *p;
        gint ok;
        gboolean no_select = FALSE, no_sub = FALSE;
+       gboolean exist = 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);
        g_return_val_if_fail(name != NULL, NULL);
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                return NULL;
        }
 
-       lock_session();
        if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
                dirpath = g_strdup(name);
        }else if (parent->path)
@@ -1768,14 +1986,13 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                unlock_session();               
                return NULL;});
 
-       separator = imap_get_path_separator(IMAP_FOLDER(folder), imap_path);
+       separator = imap_get_path_separator(session, IMAP_FOLDER(folder), imap_path);
        imap_path_separator_subst(imap_path, separator);
        /* remove trailing / for display */
        strtailchomp(new_name, '/');
 
        if (strcmp(dirpath, "INBOX") != 0) {
                GPtrArray *argbuf;
-               gboolean exist = FALSE;
                int r;
                clist * lep_list;
                
@@ -1816,8 +2033,8 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                                } 
                                mailimap_list_result_free(lep_list);
                        }
-
                }
+               imap_threaded_subscribe(folder, imap_path, TRUE);
        } else {
                clist *lep_list;
                int r;
@@ -1848,6 +2065,12 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
                make_dir_hier(dirpath);
        g_free(dirpath);
        unlock_session();
+
+       if (exist) {
+               /* folder existed, scan it */
+               folder_item_scan_full(new_item, FALSE);
+       }
+
        return new_item;
 }
 
@@ -1872,20 +2095,20 @@ 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);
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                return -1;
        }
-       lock_session();
 
-       if (strchr(name, imap_get_path_separator(IMAP_FOLDER(folder), item->path)) != NULL) {
+       if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path)) != NULL) {
                g_warning(_("New folder name must not contain the namespace "
                            "path separator"));
                unlock_session();
                return -1;
        }
 
-       real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
+       real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
 
        g_free(session->mbox);
        session->mbox = NULL;
@@ -1897,7 +2120,7 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
                return -1;
        }
 
-       separator = imap_get_path_separator(IMAP_FOLDER(folder), item->path);
+       separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path);
        if (strchr(item->path, G_DIR_SEPARATOR)) {
                dirpath = g_path_get_dirname(item->path);
                newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
@@ -1946,6 +2169,33 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        return 0;
 }
 
+gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
+{
+       gchar *path;
+       gint r = -1;
+       IMAPSession *session;
+       debug_print("getting session...\n");
+
+       session = imap_session_get(folder);
+       if (!session) {
+               return -1;
+       }
+       if (item && item->path) {
+               path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
+               if (!path)
+                       return -1;
+               if (!strcmp(path, "INBOX") && sub == FALSE)
+                       return -1;
+               debug_print("%ssubscribing %s\n", sub?"":"un", path);
+               r = imap_threaded_subscribe(folder, path, sub);
+               g_free(path);
+       } else if (rpath) {
+               r = imap_threaded_subscribe(folder, rpath, sub);
+       } else
+               return -1;
+       return r;
+}
+
 static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
 {
        gint ok;
@@ -1957,17 +2207,18 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        g_return_val_if_fail(item != NULL, -1);
        g_return_val_if_fail(item->path != NULL, -1);
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                return -1;
        }
-       lock_session();
-       path = imap_get_real_path(IMAP_FOLDER(folder), item->path);
+       path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path);
 
+       imap_threaded_subscribe(folder, path, 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));
+                               imap_get_path_separator(session, IMAP_FOLDER(folder), path));
                g_free(path);
                path = tmp;
                ok = imap_cmd_delete(session, path);
@@ -2056,6 +2307,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;
@@ -2139,7 +2392,10 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                data->numlist = newlist;
                data->cur += count;
                
-               if (prefs_common.work_offline && !inc_offline_should_override()) {
+               if (prefs_common.work_offline && 
+                   !inc_offline_should_override(
+                       _("Claws Mail needs network access in order "
+                         "to access the IMAP server."))) {
                        g_free(data);
                        return NULL;
                }
@@ -2179,70 +2435,68 @@ static void imap_delete_all_cached_messages(FolderItem *item)
        debug_print("done.\n");
 }
 
-static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
-                                                   const gchar *path)
+gchar imap_get_path_separator_for_item(FolderItem *item)
 {
-       IMAPNameSpace *namespace = NULL;
-       gchar *tmp_path, *name;
-
-       if (!path) path = "";
-
-       for (; ns_list != NULL; ns_list = ns_list->next) {
-               IMAPNameSpace *tmp_ns = ns_list->data;
-
-               Xstrcat_a(tmp_path, path, "/", return namespace);
-               Xstrdup_a(name, tmp_ns->name, return namespace);
-               if (tmp_ns->separator && tmp_ns->separator != '/') {
-                       subst_char(tmp_path, tmp_ns->separator, '/');
-                       subst_char(name, tmp_ns->separator, '/');
-               }
-               if (strncmp(tmp_path, name, strlen(name)) == 0)
-                       namespace = tmp_ns;
-       }
-
-       return namespace;
+       Folder *folder = NULL;
+       IMAPFolder *imap_folder = NULL;
+       IMAPSession *session = NULL;
+       gchar result = '/';
+       
+       if (!item)
+               return '/';
+       folder = item->folder;
+       
+       if (!folder)
+               return '/';
+       
+       imap_folder = IMAP_FOLDER(folder);
+       
+       if (!imap_folder)
+               return '/';
+       
+       debug_print("getting session...");
+       session = imap_session_get(FOLDER(folder));
+       result = imap_get_path_separator(session, imap_folder, item->path);
+       unlock_session();
+       return result;
 }
 
-static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
-                                         const gchar *path)
+static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
 {
-       IMAPNameSpace *namespace;
-
-       g_return_val_if_fail(folder != NULL, NULL);
+       clist * lep_list;
+       int r;
+       gchar separator = '\0';
+       
+       g_return_val_if_fail(session != NULL, '/');
+       r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
+       
+       if (r != MAILIMAP_NO_ERROR) {
+               log_warning(_("LIST failed\n"));
+               return '\0';
+       }
 
-       namespace = imap_find_namespace_from_list(folder->ns_personal, path);
-       if (namespace) return namespace;
-       namespace = imap_find_namespace_from_list(folder->ns_others, path);
-       if (namespace) return namespace;
-       namespace = imap_find_namespace_from_list(folder->ns_shared, path);
-       if (namespace) return namespace;
+       if (clist_count(lep_list) > 0) {
+               clistiter * iter = clist_begin(lep_list); 
+               struct mailimap_mailbox_list * mb;
+               mb = clist_content(iter);
 
-       return NULL;
+               separator = mb->mb_delimiter;
+               debug_print("got separator: %c\n", folder->last_seen_separator);
+       }
+       mailimap_list_result_free(lep_list);
+       return separator;
 }
 
-
-static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
+static gchar imap_get_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *path)
 {
-       IMAPNameSpace *namespace;
        gchar separator = '/';
 
        if (folder->last_seen_separator == 0) {
-               clist * lep_list;
-               int r = imap_threaded_list((Folder *)folder, "", "", &lep_list);
-               if (r != MAILIMAP_NO_ERROR) {
-                       log_warning(_("LIST failed\n"));
-                       return '/';
-               }
-               
-               if (clist_count(lep_list) > 0) {
-                       clistiter * iter = clist_begin(lep_list); 
-                       struct mailimap_mailbox_list * mb;
-                       mb = clist_content(iter);
-               
-                       folder->last_seen_separator = mb->mb_delimiter;
-                       debug_print("got separator: %c\n", folder->last_seen_separator);
-               }
-               mailimap_list_result_free(lep_list);
+               folder->last_seen_separator = imap_refresh_path_separator(session, folder, "");
+       }
+
+       if (folder->last_seen_separator == 0) {
+               folder->last_seen_separator = imap_refresh_path_separator(session, folder, "INBOX");
        }
 
        if (folder->last_seen_separator != 0) {
@@ -2250,14 +2504,10 @@ static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
                return folder->last_seen_separator;
        }
 
-       namespace = imap_find_namespace(folder, path);
-       if (namespace && namespace->separator)
-               separator = namespace->separator;
-
        return separator;
 }
 
-static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
+static gchar *imap_get_real_path(IMAPSession *session, IMAPFolder *folder, const gchar *path)
 {
        gchar *real_path;
        gchar separator;
@@ -2266,7 +2516,7 @@ static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
        g_return_val_if_fail(path != NULL, NULL);
 
        real_path = imap_utf8_to_modified_utf7(path);
-       separator = imap_get_path_separator(folder, path);
+       separator = imap_get_path_separator(session, folder, path);
        imap_path_separator_subst(real_path, separator);
 
        return real_path;
@@ -2333,7 +2583,7 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
        g_free(session->mbox);
        session->mbox = NULL;
 
-       real_path = imap_get_real_path(folder, path);
+       real_path = imap_get_real_path(session, folder, path);
 
        ok = imap_cmd_select(session, real_path,
                             exists, recent, unseen, uid_validity, block);
@@ -2361,7 +2611,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        gchar *real_path;
        guint mask = 0;
        
-       real_path = imap_get_real_path(folder, path);
+       real_path = imap_get_real_path(session, folder, path);
 
        if (messages) {
                mask |= 1 << 0;
@@ -2436,50 +2686,6 @@ static void imap_free_capabilities(IMAPSession *session)
 
 /* 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 *type)
@@ -2487,28 +2693,54 @@ static gint imap_cmd_login(IMAPSession *session,
        int r;
        gint ok;
 
+       if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
+               gint ok = IMAP_ERROR;
+               if (imap_has_capability(session, "STARTTLS")) {
+#if USE_OPENSSL
+                       log_warning(_("Server requires TLS to log in.\n"));
+                       ok = imap_cmd_starttls(session);
+                       if (ok != IMAP_SUCCESS) {
+                               log_warning(_("Can't start TLS session.\n"));
+                               return IMAP_ERROR;
+                       } else {
+                               /* refresh capas */
+                               imap_free_capabilities(session);
+                               if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR) {
+                                       log_warning(_("Can't refresh capabilities.\n"));
+                                       return IMAP_ERROR;
+                               }
+                       }
+#else          
+                       log_error(_("Connection to %s failed: "
+                                       "server requires TLS, but Claws Mail "
+                                       "has been compiled without OpenSSL "
+                                       "support.\n"),
+                                       SESSION(session)->server);
+                       return IMAP_ERROR;
+#endif
+               } else {
+                       log_error(_("Server logins are disabled.\n"));
+                       return IMAP_ERROR;
+               }
+       }
+
        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) {
-               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;
 }
 
-static gint imap_cmd_logout(IMAPSession *session)
-{
-       imap_threaded_disconnect(session->folder);
-
-       return IMAP_SUCCESS;
-}
-
 static gint imap_cmd_noop(IMAPSession *session)
 {
        int r;
@@ -2658,7 +2890,10 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        data->headers = headers;
        data->body = body;
 
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Claws Mail needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -2681,7 +2916,7 @@ 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, new_uid);
+                        file, flag_list, (int *)new_uid);
        mailimap_flag_list_free(flag_list);
 
        if (r != MAILIMAP_NO_ERROR) {
@@ -2692,7 +2927,8 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 }
 
 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
-                         const gchar *destfolder, GRelation *uid_mapping)
+                         const gchar *destfolder, GRelation *uid_mapping,
+                         struct mailimap_set **source, struct mailimap_set **dest)
 {
        int r;
        
@@ -2700,7 +2936,7 @@ static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
        g_return_val_if_fail(set != NULL, IMAP_ERROR);
        g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
 
-       r = imap_threaded_copy(session->folder, set, destfolder);
+       r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
        if (r != MAILIMAP_NO_ERROR) {
                
                return IMAP_ERROR;
@@ -2739,7 +2975,10 @@ static gint imap_cmd_expunge(IMAPSession *session)
 {
        int r;
        
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Claws Mail needs network access in order "
+                 "to access the IMAP server."))) {
                return -1;
        }
 
@@ -2994,7 +3233,6 @@ static void *get_list_of_uids_thread(void *data)
        gint ok, nummsgs = 0, lastuid_old;
        IMAPSession *session;
        GSList *uidlist, *elem;
-       struct mailimap_set * set;
        clist * lep_uidlist;
        int r;
 
@@ -3013,11 +3251,8 @@ 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,
+       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
                                 &lep_uidlist);
-       mailimap_set_free(set);
        
        if (r == MAILIMAP_NO_ERROR) {
                GSList * fetchuid_list;
@@ -3074,7 +3309,10 @@ static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderIte
        data->item = item;
        data->msgnum_list = msgnum_list;
        data->session = session;
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Claws Mail needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -3089,11 +3327,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 {
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        IMAPSession *session;
-       gint ok, nummsgs = 0, exists, uid_val, uid_next;
+       gint ok, nummsgs = 0, exists;
+       guint32 uid_next = 0, uid_val = 0;
        GSList *uidlist = NULL;
        gchar *dir;
        gboolean selected_folder;
-       
        debug_print("get_num_list\n");
        
        g_return_val_if_fail(folder != NULL, -1);
@@ -3102,11 +3340,18 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
        g_return_val_if_fail(folder->account != NULL, -1);
 
+       debug_print("getting session...\n");
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, -1);
-       lock_session();
-       statusbar_print_all("Scanning %s...\n", FOLDER_ITEM(item)->path 
-                               ? FOLDER_ITEM(item)->path:"");
+
+       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));
@@ -3123,6 +3368,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                }
                exists = session->exists;
 
+               uid_next = item->c_uid_next;
+               uid_val = item->c_uid_validity;
                *old_uids_valid = TRUE;
        } else {
                if (item->use_cache && time(NULL) - item->use_cache < 2) {
@@ -3148,7 +3395,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                else {
                        *old_uids_valid = FALSE;
 
-                       debug_print("Freeing imap uid cache\n");
+                       debug_print("Freeing imap uid cache (%d != %d)\n",
+                                       (int)item->item.mtime, uid_val);
                        item->lastuid = 0;
                        g_slist_free(item->uid_list);
                        item->uid_list = NULL;
@@ -3269,9 +3517,10 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
        g_return_val_if_fail(item != NULL, NULL);
        g_return_val_if_fail(msgnum_list != NULL, NULL);
 
+       debug_print("getting session...\n");
        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);
@@ -3285,13 +3534,14 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                        imap_get_uncached_messages(session, item,
                                                   msgnum_list));
        } else {
-               MsgNumberList *sorted_list, *elem;
+               MsgNumberList *sorted_list, *elem, *llast = NULL;
                gint startnum, lastnum;
 
                sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
 
                startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
 
+               llast = g_slist_last(ret);
                for (elem = sorted_list;; elem = g_slist_next(elem)) {
                        guint num = 0;
 
@@ -3308,10 +3558,16 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                                                MsgInfo *msginfo = imap_parse_msg(file, item);
                                                if (msginfo != NULL) {
                                                        msginfo->msgnum = i;
-                                                       ret = g_slist_append(ret, msginfo);
+                                                       if (llast == NULL)
+                                                               llast = ret = g_slist_append(ret, msginfo);
+                                                       else {
+                                                               llast = g_slist_append(llast, msginfo);
+                                                               llast = llast->next;
+                                                       }
                                                }
                                                g_free(file);
                                        }
+                                       session_set_access_time(SESSION(session));
                                }
 
                                if (elem == NULL)
@@ -3351,7 +3607,7 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
        IMAPSession *session;
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        gint ok, exists = 0, unseen = 0;
-       guint32 uid_next, uid_val;
+       guint32 uid_next = 0, uid_val = 0;
        gboolean selected_folder;
        
        g_return_val_if_fail(folder != NULL, FALSE);
@@ -3362,9 +3618,10 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
        if (item->item.path == NULL)
                return FALSE;
 
+       debug_print("getting session...\n");
        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 && time(NULL) - item->use_cache < 2) {
@@ -3374,7 +3631,6 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                        session = imap_reconnect_if_possible(folder, session);
                        if (session == NULL)
                                return FALSE;
-                       lock_session();
                }
 
                if (session->folder_content_changed
@@ -3398,7 +3654,8 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                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)) {
+               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;
                }
@@ -3422,17 +3679,6 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        g_return_if_fail(msginfo != NULL);
        g_return_if_fail(msginfo->folder == item);
 
-       session = imap_session_get(folder);
-       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;
-       }
-
        if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
                flags_set |= IMAP_FLAG_FLAGGED;
        if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
@@ -3453,6 +3699,24 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
                flags_unset |= IMAP_FLAG_DELETED;
 
+       if (!flags_set && !flags_unset) {
+               /* the changed flags were not translatable to IMAP-speak.
+                * like MSG_POSTFILTERED, so just apply. */
+               msginfo->flags.perm_flags = newflags;
+               return;
+       }
+
+       debug_print("getting session...\n");
+       session = imap_session_get(folder);
+       if (!session) {
+               return;
+       }
+
+       if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
+           NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
+               unlock_session();
+               return;
+       }
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(msginfo->msgnum);
 
@@ -3524,9 +3788,10 @@ static gint imap_remove_msg(Folder *folder, FolderItem *item, gint uid)
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, -1);
        g_return_val_if_fail(item != NULL, -1);
 
+       debug_print("getting session...\n");
        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) {
@@ -3631,12 +3896,13 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                return GINT_TO_POINTER(-1);
        }
 
+       debug_print("getting session...\n");
        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));
 
@@ -3676,12 +3942,12 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                imapset = cur->data;
                if (reverse_seen) {
                        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
-                                                imapset, &lep_uidlist);
+                                                full_search ? NULL:imapset, &lep_uidlist);
                }
                else {
                        r = imap_threaded_search(folder,
                                                 IMAP_SEARCH_TYPE_UNSEEN,
-                                                imapset, &lep_uidlist);
+                                                full_search ? NULL:imapset, &lep_uidlist);
                }
                if (r == MAILIMAP_NO_ERROR) {
                        GSList * uidlist;
@@ -3692,37 +3958,39 @@ 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);
+                                        full_search ? NULL: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);
                }
-               
-               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);
+
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
+                                                full_search ? NULL: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,
+                                                full_search ? NULL: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);
+                       }
                }
        }
 
@@ -3739,7 +4007,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) {
@@ -3749,18 +4021,22 @@ 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;
-               else
-                       flags &= ~MSG_REPLIED;
+               
                if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
                        flags |= MSG_MARKED;
                else
                        flags &= ~MSG_MARKED;
-               if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
-                       flags |= MSG_DELETED;
-               else
-                       flags &= ~MSG_DELETED;
+
+               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));
        }
 
@@ -3791,14 +4067,17 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
 
        GSList *tmp = NULL, *cur;
        
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Claws Mail 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))
+       if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
                data->full_search = TRUE;
        
        for (cur = tmp; cur; cur = cur->next)
@@ -3821,7 +4100,10 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
        IMAPFolderItem *_item = data->item;
        FolderItem *item = (FolderItem *)_item;
        gint ok = IMAP_ERROR;
-       IMAPSession *session = imap_session_get(item->folder);
+       IMAPSession *session = NULL;
+       
+       debug_print("getting session...\n");
+       session = imap_session_get(item->folder);
 
        data->msglist = g_slist_reverse(data->msglist);
        
@@ -3831,7 +4113,6 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
                g_slist_length(data->msglist));
        
        if (session) {
-               lock_session();
                ok = imap_select(session, IMAP_FOLDER(item->folder), item->path,
                         NULL, NULL, NULL, NULL, FALSE);
        }
@@ -3840,8 +4121,8 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
        } else {
                g_warning("can't select mailbox %s\n", item->path);
        }
-       if (session)
-               unlock_session();
+
+       unlock_session();
        g_slist_free(data->msglist);    
        g_free(data);
        return TRUE;
@@ -3889,7 +4170,7 @@ static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
 
 
 
-/* data types conversion libetpan <-> sylpheed */
+/* data types conversion libetpan <-> claws */
 
 
 
@@ -3938,9 +4219,7 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                                   clist * list, const gchar * real_path, gboolean all)
 {
        clistiter * iter;
-       GSList * item_list;
-       
-       item_list = NULL;
+       GSList * item_list = NULL, *llast = NULL;
        
        for(iter = clist_begin(list) ; iter != NULL ;
            iter = clist_next(iter)) {
@@ -3976,17 +4255,14 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                        free(dup_name);
                        continue;
                }
-               
-               if (!all && strcmp(dup_name, real_path) == 0) {
+               if (!all && path_cmp(name, real_path) == 0) {
                        g_free(base);
                        free(dup_name);
                        continue;
                }
 
                if (!all && dup_name[strlen(dup_name)-1] == '/') {
-                       g_free(base);
-                       free(dup_name);
-                       continue;
+                       dup_name[strlen(dup_name)-1] = '\0';
                }
                
                loc_name = imap_modified_utf7_to_utf8(base);
@@ -3999,8 +4275,12 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                    ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
                        new_item->no_select = TRUE;
                
-               item_list = g_slist_append(item_list, new_item);
-               
+               if (item_list == NULL)
+                       llast = item_list = g_slist_append(item_list, new_item);
+               else {
+                       llast = g_slist_append(llast, new_item);
+                       llast = llast->next;
+               }
                debug_print("folder '%s' found.\n", loc_path);
                g_free(base);
                g_free(loc_path);
@@ -4016,7 +4296,7 @@ static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
 {
        GSList *sorted_list, *cur;
        guint first, last, next;
-       GSList *ret_list = NULL;
+       GSList *ret_list = NULL, *llast = NULL;
        unsigned int count;
        struct mailimap_set * current_set;
        unsigned int item_count;
@@ -4055,8 +4335,13 @@ static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
                        first = next;
                        
                        if (count >= IMAP_SET_MAX_COUNT) {
-                               ret_list = g_slist_append(ret_list,
+                               if (ret_list == NULL)
+                                       llast = ret_list = g_slist_append(ret_list,
                                                          current_set);
+                               else {
+                                       llast = g_slist_append(llast, current_set);
+                                       llast = llast->next;
+                               }
                                current_set = mailimap_set_new_empty();
                                count = 0;
                                item_count = 0;
@@ -4083,8 +4368,9 @@ static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist)
        for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
                MsgInfo *msginfo = (MsgInfo *) cur->data;
 
-               numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
+               numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
        }
+       numlist = g_slist_reverse(numlist);
        seq_list = imap_get_lep_set_from_numlist(numlist);
        g_slist_free(numlist);
 
@@ -4103,9 +4389,10 @@ static GSList * imap_uid_list_from_lep(clist * list)
                uint32_t * puid;
                
                puid = clist_content(iter);
-               result = g_slist_append(result, GINT_TO_POINTER(* puid));
+               result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
        }
        
+       result = g_slist_reverse(result);
        return result;
 }
 
@@ -4120,9 +4407,9 @@ static GSList * imap_uid_list_from_lep_tab(carray * list)
                uint32_t * puid;
                
                puid = carray_get(list, i);
-               result = g_slist_append(result, GINT_TO_POINTER(* puid));
+               result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
        }
-       
+       result = g_slist_reverse(result);
        return result;
 }
 
@@ -4205,6 +4492,24 @@ void imap_folder_ref(Folder *folder)
        ((IMAPFolder *)folder)->refcnt++;
 }
 
+void imap_disconnect_all(void)
+{
+       GList *list;
+       for (list = account_get_list(); list != NULL; list = list->next) {
+               PrefsAccount *account = list->data;
+               if (account->protocol == A_IMAP4) {
+                       RemoteFolder *folder = (RemoteFolder *)account->folder;
+                       if (folder && folder->session) {
+                               IMAPSession *session = (IMAPSession *)folder->session;
+                               imap_threaded_disconnect(FOLDER(folder));
+                               SESSION(session)->state = SESSION_DISCONNECTED;
+                               session_destroy(SESSION(session));
+                               folder->session = NULL;
+                       }
+               }
+       }
+}
+
 void imap_folder_unref(Folder *folder)
 {
        if (((IMAPFolder *)folder)->refcnt > 0)
@@ -4215,9 +4520,25 @@ 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)
 {
+       static gboolean missing_imap_warning = TRUE;
+       if (missing_imap_warning) {
+               missing_imap_warning = FALSE;
+               alertpanel_error(
+                       _("You have one or more IMAP accounts "
+                         "defined. However this version of "
+                         "Claws Mail has been built without "
+                         "IMAP support; your IMAP account(s) are "
+                         "disabled.\n\n"
+                         "You probably need to "
+                         "install libetpan and recompile "
+                         "Claws Mail."));
+       }
        return NULL;
 }
 static gint    imap_create_tree        (Folder         *folder)
@@ -4237,6 +4558,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) {
@@ -4248,14 +4574,70 @@ 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;
 }
+
+void imap_disconnect_all(void)
+{
+}
+
+gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
+{
+       return -1;
+}
+
+gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub)
+{
+       return -1;
+}
+
+GList * imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only, gboolean recursive)
+{
+       return -1;
+}
 #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;
+}