2007-03-04 [iwkse] 2.8.0cvs12
[claws.git] / src / imap.c
index edacb29a0d0ad1acb42a9d4054f34f02dafa9e77..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
@@ -31,6 +31,7 @@
 #include "imap_gtk.h"
 #include "inc.h"
 #include "xml.h"
+#include "alertpanel.h"
 
 #ifdef HAVE_LIBETPAN
 
@@ -63,8 +64,7 @@
 #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"
@@ -254,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
@@ -293,11 +294,11 @@ static gint imap_status                   (IMAPSession    *session,
                                         gint           *unseen,
                                         gboolean        block);
 
-static IMAPNameSpace *imap_find_namespace      (IMAPFolder     *folder,
+static gchar imap_get_path_separator           (IMAPSession    *session,
+                                                IMAPFolder     *folder,
                                                 const gchar    *path);
-static gchar imap_get_path_separator           (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);
 
@@ -389,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);
@@ -540,7 +541,7 @@ 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); 
 }
 
-int imap_get_capabilities(IMAPSession *session)
+static int imap_get_capabilities(IMAPSession *session)
 {
        struct mailimap_capability_data *capabilities = NULL;
        clistiter *cur;
@@ -574,7 +575,7 @@ int imap_get_capabilities(IMAPSession *session)
        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) {
@@ -675,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();
@@ -683,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)
@@ -703,7 +715,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                return NULL;
        }
@@ -711,10 +723,6 @@ static IMAPSession *imap_session_get(Folder *folder)
        /* 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)
@@ -739,6 +747,8 @@ static IMAPSession *imap_session_get(Folder *folder)
                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
@@ -756,7 +766,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        }
 
        rfolder->session = SESSION(session);
-       
+
        return IMAP_SESSION(session);
 }
 
@@ -780,7 +790,7 @@ 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."),
@@ -825,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"),
@@ -1007,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) {
@@ -1014,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);
@@ -1076,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);
@@ -1091,7 +1106,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                gchar *real_file = NULL;
                fileinfo = (MsgFileInfo *)cur->data;
 
-               statusbar_progress_all(curnum, total, 1);
+               statusbar_progress_all(curnum, total, total < 10 ? 1:10);
                curnum++;
 
                if (fileinfo->flags) {
@@ -1184,12 +1199,13 @@ static gint imap_do_copy_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;
        if (msglist->next == NULL)
                single = TRUE;
@@ -1211,6 +1227,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                        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;
@@ -1228,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);
@@ -1270,6 +1287,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                        g_relation_destroy(uid_mapping);
                        imap_lep_set_free(seq_list);
                        unlock_session();
+                       statusbar_pop_all();
                        return -1;
                }
        }
@@ -1379,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,
@@ -1393,7 +1412,7 @@ 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))
@@ -1475,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;
@@ -1484,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) {
@@ -1495,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;
@@ -1504,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 */
@@ -1526,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);
@@ -1540,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;
@@ -1568,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);
@@ -1577,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';
@@ -1587,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;
        }
@@ -1613,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;
@@ -1675,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);
@@ -1683,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);
@@ -1805,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)
@@ -1844,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;
                
@@ -1892,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;
@@ -1924,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;
 }
 
@@ -1948,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;
@@ -1973,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);
@@ -2022,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;
@@ -2033,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);
@@ -2219,7 +2394,7 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                
                if (prefs_common.work_offline && 
                    !inc_offline_should_override(
-                       _("Sylpheed-Claws needs network access in order "
+                       _("Claws Mail needs network access in order "
                          "to access the IMAP server."))) {
                        g_free(data);
                        return NULL;
@@ -2260,51 +2435,13 @@ 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)
-{
-       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;
-}
-
-static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
-                                         const gchar *path)
-{
-       IMAPNameSpace *namespace;
-
-       g_return_val_if_fail(folder != NULL, NULL);
-
-       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;
-
-       return NULL;
-}
-
 gchar imap_get_path_separator_for_item(FolderItem *item)
 {
        Folder *folder = NULL;
        IMAPFolder *imap_folder = NULL;
+       IMAPSession *session = NULL;
+       gchar result = '/';
+       
        if (!item)
                return '/';
        folder = item->folder;
@@ -2317,33 +2454,49 @@ gchar imap_get_path_separator_for_item(FolderItem *item)
        if (!imap_folder)
                return '/';
        
-       return imap_get_path_separator(imap_folder, item->path);
+       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 gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
+static gchar imap_refresh_path_separator(IMAPSession *session, IMAPFolder *folder, const gchar *subfolder)
 {
-       IMAPNameSpace *namespace;
-       gchar separator = '/';
-       IMAPSession *session = imap_session_get(FOLDER(folder));
+       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';
+       }
+
+       if (clist_count(lep_list) > 0) {
+               clistiter * iter = clist_begin(lep_list); 
+               struct mailimap_mailbox_list * mb;
+               mb = clist_content(iter);
+
+               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(IMAPSession *session, IMAPFolder *folder, const gchar *path)
+{
+       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) {
@@ -2351,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;
@@ -2367,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;
@@ -2434,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);
@@ -2462,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;
@@ -2563,7 +2712,7 @@ static gint imap_cmd_login(IMAPSession *session,
                        }
 #else          
                        log_error(_("Connection to %s failed: "
-                                       "server requires TLS, but Sylpheed-Claws "
+                                       "server requires TLS, but Claws Mail "
                                        "has been compiled without OpenSSL "
                                        "support.\n"),
                                        SESSION(session)->server);
@@ -2743,7 +2892,7 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
 
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
                return -1;
@@ -2828,7 +2977,7 @@ static gint imap_cmd_expunge(IMAPSession *session)
        
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                return -1;
        }
@@ -3162,7 +3311,7 @@ static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderIte
        data->session = session;
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
                return -1;
@@ -3191,9 +3340,9 @@ 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();
 
        if (FOLDER_ITEM(item)->path) 
                statusbar_print_all(_("Scanning folder %s%c%s ..."),
@@ -3368,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);
@@ -3468,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) {
@@ -3480,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
@@ -3529,11 +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;
-       }
-
        if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
                flags_set |= IMAP_FLAG_FLAGGED;
        if ( MSG_IS_MARKED(msginfo->flags) && !(newflags & MSG_MARKED))
@@ -3561,7 +3706,12 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
                return;
        }
 
-       lock_session();
+       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();
@@ -3638,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) {
@@ -3745,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));
 
@@ -3917,7 +4069,7 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
        
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
                return -1;
@@ -3948,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);
        
@@ -3958,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);
        }
@@ -3967,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;
@@ -4016,7 +4170,7 @@ static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
 
 
 
-/* data types conversion libetpan <-> sylpheed */
+/* data types conversion libetpan <-> claws */
 
 
 
@@ -4101,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);
@@ -4381,12 +4532,12 @@ static Folder   *imap_folder_new        (const gchar    *name,
                alertpanel_error(
                        _("You have one or more IMAP accounts "
                          "defined. However this version of "
-                         "Sylpheed-Claws has been built without "
+                         "Claws Mail has been built without "
                          "IMAP support; your IMAP account(s) are "
                          "disabled.\n\n"
                          "You probably need to "
                          "install libetpan and recompile "
-                         "Sylpheed-Claws."));
+                         "Claws Mail."));
        }
        return NULL;
 }
@@ -4438,6 +4589,20 @@ 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)