Add a fast way to search for Message-ID (at least)
[claws.git] / src / imap.c
index e2eee5e3dc49c987cd0801f11dccc5e34b1743cc..76d1dbd8561adc456719610879433e7457bb60e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2014 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
@@ -59,7 +59,6 @@
 #include "prefs_account.h"
 #include "codeconv.h"
 #include "md5.h"
-#include "base64.h"
 #include "utils.h"
 #include "prefs_common.h"
 #include "inputdialog.h"
@@ -288,6 +287,12 @@ static FolderItem *imap_create_special_folder
                                         const gchar            *name);
 
 static gint imap_do_copy_msgs          (Folder         *folder,
+                                        FolderItem     *dest,
+                                        MsgInfoList    *msglist,
+                                        GHashTable     *relation,
+                                        gboolean        same_dest_ok);
+
+static gint imap_do_remove_msgs                (Folder         *folder,
                                         FolderItem     *dest,
                                         MsgInfoList    *msglist,
                                         GHashTable     *relation);
@@ -769,6 +774,8 @@ static void imap_folder_destroy(Folder *folder)
        while (imap_folder_get_refcnt(folder) > 0)
                gtk_main_iteration();
 
+       g_free(IMAP_FOLDER(folder)->search_charset);
+
        folder_remote_folder_destroy(REMOTE_FOLDER(folder));
        imap_done(folder);
 }
@@ -828,12 +835,12 @@ static int imap_get_capabilities(IMAPSession *session)
 {
        struct mailimap_capability_data *capabilities = NULL;
        clistiter *cur;
-       int result = -1;
+       int result;
 
        if (session->capability != NULL)
                return MAILIMAP_NO_ERROR;
 
-       capabilities = imap_threaded_capability(session->folder, &result);
+       result = imap_threaded_capability(session->folder, &capabilities);
 
        if (result != MAILIMAP_NO_ERROR) {
                return result;
@@ -891,6 +898,9 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
        case IMAP_AUTH_DIGEST_MD5:
                ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
                break;
+       case IMAP_AUTH_SCRAM_SHA1:
+               ok = imap_cmd_login(session, user, pass, "SCRAM-SHA-1");
+               break;
        case IMAP_AUTH_LOGIN:
                ok = imap_cmd_login(session, user, pass, "LOGIN");
                break;
@@ -902,17 +912,21 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
                                "\t ANONYMOUS %d\n"
                                "\t CRAM-MD5 %d\n"
                                "\t DIGEST-MD5 %d\n"
+                               "\t SCRAM-SHA-1 %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, "DIGEST-MD5"),
+                       imap_has_capability(session, "SCRAM-SHA-1"),
                        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 == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
                        ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
+               if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "SCRAM-SHA-1"))
+                       ok = imap_cmd_login(session, user, pass, "SCRAM-SHA-1");
                if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
                        ok = imap_cmd_login(session, user, pass, "GSSAPI");
                if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
@@ -934,6 +948,12 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
                                     "DIGEST-MD5 SASL plugin is installed.");
                } 
 
+               if (type == IMAP_AUTH_SCRAM_SHA1) {
+                       ext_info = _("\n\nSCRAM-SHA-1 logins only work if libetpan has been "
+                                    "compiled with SASL support and the "
+                                    "SCRAM SASL plugin is installed.");
+               }
+
                if (time(NULL) - last_login_err > 10) {
                        if (!prefs_common.no_recv_err_panel) {
                                alertpanel_error_log(_("Connection to %s failed: "
@@ -1322,20 +1342,38 @@ static guint get_file_size_with_crs(const gchar *filename)
        return cnt;
 }
 
-static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+static gchar *imap_get_cached_filename(FolderItem *item, guint msgnum)
 {
        gchar *path, *filename;
 
+       cm_return_val_if_fail(item != NULL, NULL);
+
        path = folder_item_get_path(item);
 
        if (!is_dir_exist(path)) {
                g_free(path);
-               return;
+               return NULL;
        }
 
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
+       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
+
+       if (is_dir_exist(filename)) {
+               g_free(filename);
+               filename = g_strconcat(path, G_DIR_SEPARATOR_S, ".", itos(msgnum), NULL);
+       }
        g_free(path);
 
+       return filename;
+}
+
+static void imap_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+{
+       gchar *filename;
+
+       filename = imap_get_cached_filename(item, msginfo->msgnum);
+
+       cm_return_if_fail(filename != NULL);
+
        if (is_file_exist(filename)) {
                claws_unlink(filename);
        }
@@ -1471,11 +1509,16 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                return NULL;
 
        path = folder_item_get_path(item);
-       if (!is_dir_exist(path))
+       if (!is_dir_exist(path)) {
+               if(is_file_exist(path))
+                       claws_unlink(path);
                make_dir_hier(path);
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
+       }
        g_free(path);
+
+       filename = imap_get_cached_filename(item, uid);
        debug_print("trying to fetch cached %s\n", filename);
+
        if (is_file_exist(filename)) {
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
@@ -1534,7 +1577,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        ok = imap_select(session, IMAP_FOLDER(folder), item,
                         NULL, NULL, NULL, NULL, NULL, FALSE);
        if (ok != MAILIMAP_NO_ERROR) {
-               g_warning("can't select mailbox %s\n", item->path);
+               g_warning("can't select mailbox %s", item->path);
                g_free(filename);
                return NULL;
        }
@@ -1545,7 +1588,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
 
        if (ok != MAILIMAP_NO_ERROR) {
-               g_warning("can't fetch message %d\n", uid);
+               g_warning("can't fetch message %d", uid);
                g_free(filename);
                return NULL;
        }
@@ -1573,7 +1616,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
 
 static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint uid)
 {
-       gchar *path, *filename;
+       gchar *filename;
        guint size = 0;
        MsgInfo *cached = msgcache_get_msg(item->cache,uid);
        
@@ -1584,14 +1627,9 @@ static gboolean imap_is_msg_fully_cached(Folder *folder, FolderItem *item, gint
                procmsg_msginfo_free(cached);
                return TRUE;
        }
-       path = folder_item_get_path(item);
-       if (!is_dir_exist(path)) {
-               g_free(path);
-               return FALSE;
-       }
 
-       filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
-       g_free(path);
+       filename = imap_get_cached_filename(item, uid);
+
        if (is_file_exist(filename)) {
                if (cached && cached->total_size == cached->size) {
                        /* fast path */
@@ -1670,8 +1708,10 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                return -1;
        }
        destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
-       if (is_fatal(ok))
+       if (is_fatal(ok)) {
+               g_free(destdir);
                return -1;
+       }
        statusbar_print_all(_("Adding messages..."));
        total = g_slist_length(file_list);
        for (cur = file_list; cur != NULL; cur = cur->next) {
@@ -1712,7 +1752,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                     &new_uid);
 
                if (ok != MAILIMAP_NO_ERROR) {
-                       g_warning("can't append message %s\n", real_file);
+                       g_warning("can't append message %s", real_file);
                        g_free(real_file);
                        g_free(destdir);
                        statusbar_progress_all(0,0,0);
@@ -1806,7 +1846,8 @@ static GSList *flatten_mailimap_set(struct mailimap_set * set)
        return result;
 }
 static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest, 
-                             MsgInfoList *msglist, GHashTable *relation)
+                             MsgInfoList *msglist, GHashTable *relation,
+                             gboolean same_dest_ok)
 {
        FolderItem *src;
        gchar *destdir;
@@ -1830,8 +1871,8 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
 
        msginfo = (MsgInfo *)msglist->data;
        src = msginfo->folder;
-       if (src == dest) {
-               g_warning("the src folder is identical to the dest.\n");
+       if (!same_dest_ok && src == dest) {
+               g_warning("the src folder is identical to the dest.");
                return -1;
        }
 
@@ -1868,8 +1909,10 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
 
        destdir = imap_get_real_path(session, IMAP_FOLDER(folder), dest->path, &ok);
 
-       if (is_fatal(ok))
+       if (is_fatal(ok)) {
+               g_free(destdir);
                return ok;
+       }
 
        seq_list = imap_get_lep_set_from_msglist(IMAP_FOLDER(folder), msglist);
        uid_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
@@ -1957,8 +2000,11 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                                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);
+                                       if (copy_file(real_file, cache_file, TRUE) < 0)
+                                               debug_print("couldn't cache to %s: %s\n", cache_file,
+                                                           strerror(errno));
+                                       else
+                                               debug_print("copied to cache: %s\n", cache_file);
                                }
                                g_free(real_file);
                                g_free(cache_file);
@@ -2012,7 +2058,7 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       ret = imap_do_copy_msgs(folder, dest, msglist, relation);
+       ret = imap_do_copy_msgs(folder, dest, msglist, relation, FALSE);
        return ret;
 }
 
@@ -2025,6 +2071,7 @@ static gboolean imap_matcher_type_is_local(gint matchertype)
        case MATCHCRITERIA_TO_OR_CC:
        case MATCHCRITERIA_SUBJECT:
        case MATCHCRITERIA_REFERENCES:
+       case MATCHCRITERIA_MESSAGEID:
        case MATCHCRITERIA_INREPLYTO:
        case MATCHCRITERIA_AGE_GREATER:
        case MATCHCRITERIA_AGE_LOWER:
@@ -2067,9 +2114,11 @@ static IMAPSearchKey* search_make_key(MatcherProp* match, gboolean* is_all)
                case MATCHCRITERIA_NOT_HEADER: invert = TRUE; matchertype = MATCHCRITERIA_HEADER; break;
                case MATCHCRITERIA_NOT_TAG: invert = TRUE; matchertype = MATCHCRITERIA_TAG; break;
                case MATCHCRITERIA_NOT_HEADERS_PART: invert = TRUE; matchertype = MATCHCRITERIA_HEADERS_PART; break;
+               case MATCHCRITERIA_NOT_HEADERS_CONT: invert = TRUE; matchertype = MATCHCRITERIA_HEADERS_CONT; break;
                case MATCHCRITERIA_NOT_MESSAGE: invert = TRUE; matchertype = MATCHCRITERIA_MESSAGE; break;
                case MATCHCRITERIA_NOT_BODY_PART: invert = TRUE; matchertype = MATCHCRITERIA_BODY_PART; break;
                case MATCHCRITERIA_NOT_TO_AND_NOT_CC: invert = TRUE; matchertype = MATCHCRITERIA_TO_OR_CC; break;
+               case MATCHCRITERIA_NOT_MESSAGEID: invert = TRUE; matchertype = MATCHCRITERIA_MESSAGEID; break;
                case MATCHCRITERIA_NOT_INREPLYTO: invert = TRUE; matchertype = MATCHCRITERIA_INREPLYTO; break;
                }
 
@@ -2096,6 +2145,10 @@ static IMAPSearchKey* search_make_key(MatcherProp* match, gboolean* is_all)
                        result = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
                        break;
 
+               case MATCHCRITERIA_MESSAGEID:
+                       result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, "Message-ID", match->expr, 0);
+                       break;
+
                case MATCHCRITERIA_INREPLYTO:
                        result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, "In-Reply-To", match->expr, 0);
                        break;
@@ -2112,6 +2165,7 @@ static IMAPSearchKey* search_make_key(MatcherProp* match, gboolean* is_all)
                        break;
 
                case MATCHCRITERIA_HEADERS_PART:
+               case MATCHCRITERIA_HEADERS_CONT:
                        result = imap_search_and(
                                        imap_search_not(imap_search_new(IMAP_SEARCH_CRITERIA_BODY, NULL, match->expr, 0)),
                                        imap_search_new(IMAP_SEARCH_CRITERIA_MESSAGE, NULL, match->expr, 0)
@@ -2485,10 +2539,8 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        imap_scan_required(folder, dest);
 
        g_free(destdir);
-       if (ok == MAILIMAP_NO_ERROR)
-               return 0;
-       else
-               return -1;
+
+       return 0;
 }
 
 static gint imap_remove_msgs(Folder *folder, FolderItem *dest, 
@@ -2564,8 +2616,10 @@ static gint imap_scan_tree_real(Folder *folder, gboolean subs_only)
                strtailchomp(root_folder, '/');
                real_path = imap_get_real_path
                        (session, IMAP_FOLDER(folder), root_folder, &r);
-               if (is_fatal(r))
+               if (is_fatal(r)) {
+                       g_free(real_path);
                        return -1;
+               }
                debug_print("IMAP root directory: %s\n", real_path);
 
                /* check if root directory exist */
@@ -2700,7 +2754,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gbo
                        }
                }
                if (!new_item) {
-                       if (old_item && old_item->path && !strcmp(old_item->path, "INBOX")) {
+                       if (old_item && old_item->path && !strcasecmp(old_item->path, "INBOX")) {
                                debug_print("not removing INBOX\n");
                        } else {
                                debug_print("folder '%s' not found. removing...\n",
@@ -2741,7 +2795,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gbo
                        folder_item_append(item, new_item);
                }
 
-               if (!strcmp(new_item->path, "INBOX")) {
+               if (!strcasecmp(new_item->path, "INBOX")) {
                        new_item->stype = F_INBOX;
                        folder->inbox = new_item;
                } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
@@ -2752,7 +2806,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item, gbo
                        if (!folder->outbox && !g_ascii_strcasecmp(base, "Sent")) {
                                new_item->stype = F_OUTBOX;
                                folder->outbox = new_item;
-                       } else if (!folder->draft && !g_ascii_strcasecmp(base, "Drafts")) {
+                       } else if (!folder->draft && (!g_ascii_strcasecmp(base, "Drafts") || !g_ascii_strcasecmp(base, "Draft"))) {
                                new_item->stype = F_DRAFT;
                                folder->draft = new_item;
                        } else if (!folder->queue && !g_ascii_strcasecmp(base, "Queue")) {
@@ -2870,6 +2924,7 @@ GList *imap_scan_subtree(Folder *folder, FolderItem *item, gboolean unsubs_only,
                        if (r) {
                                g_free(real_path);
                                statusbar_pop_all();
+                               g_free(tmp);
                                return NULL;
                        }
                        folder_item_destroy(cur_item);
@@ -2941,12 +2996,12 @@ static FolderItem *imap_create_special_folder(Folder *folder,
        new_item = imap_create_folder(folder, item, name);
 
        if (!new_item) {
-               g_warning("Can't create '%s'\n", name);
+               g_warning("Can't create '%s'", name);
                if (!folder->inbox) return NULL;
 
                new_item = imap_create_folder(folder, folder->inbox, name);
                if (!new_item)
-                       g_warning("Can't create '%s' under INBOX\n", name);
+                       g_warning("Can't create '%s' under INBOX", name);
                else
                        new_item->stype = stype;
        } else
@@ -2962,13 +3017,25 @@ static gchar *imap_folder_get_path(Folder *folder)
        g_return_val_if_fail(folder != NULL, NULL);
         g_return_val_if_fail(folder->account != NULL, NULL);
 
+#ifdef G_OS_WIN32
+       gchar *sanitized_dirname = g_strdup(folder->account->recv_server);
+       g_strdelimit(sanitized_dirname, ":", ',');
+#endif
+
         folder_path = g_strconcat(get_imap_cache_dir(),
                                   G_DIR_SEPARATOR_S,
+#ifdef G_OS_WIN32
+                                                                                                                                       sanitized_dirname,
+#else
                                   folder->account->recv_server,
+#endif
                                   G_DIR_SEPARATOR_S,
                                   folder->account->userid,
                                   NULL);
 
+#ifdef G_OS_WIN32
+       g_free(sanitized_dirname);
+#endif
        return folder_path;
 }
 
@@ -3062,13 +3129,22 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        g_return_val_if_fail(parent != NULL, NULL);
        g_return_val_if_fail(name != NULL, NULL);
 
+       if (to_number(name) > 0) {
+               gchar *cached_msg = imap_get_cached_filename(parent, to_number(name));
+               if (is_file_exist(cached_msg)) {
+                       if (claws_unlink(cached_msg) != 0) {
+                               return NULL;
+                       }
+               }
+       }
+
        debug_print("getting session...\n");
        session = imap_session_get(folder);
        if (!session) {
                return NULL;
        }
 
-       if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0) {
+       if (!folder_item_parent(parent) && strcasecmp(name, "INBOX") == 0) {
                dirpath = g_strdup(name);
        }else if (parent->path)
                dirpath = g_strconcat(parent->path, "/", name, NULL);
@@ -3104,7 +3180,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        /* remove trailing / for display */
        strtailchomp(new_name, '/');
 
-       if (strcmp(dirpath, "INBOX") != 0) {
+       if (strcasecmp(dirpath, "INBOX") != 0) {
                GPtrArray *argbuf;
                int r;
                clist * lep_list;
@@ -3219,13 +3295,14 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
 
        if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
                is_fatal(ok)) {
-               g_warning(_("New folder name must not contain the namespace "
-                           "path separator"));
+               g_warning("New folder name must not contain the namespace "
+                           "path separator");
                return -1;
        }
 
        real_oldpath = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
        if (is_fatal(ok)) {
+               g_free(real_oldpath);
                return -1;
        }
 
@@ -3242,8 +3319,10 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        }
 
        separator = imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok);
-       if (is_fatal(ok))
+       if (is_fatal(ok)) {
+               g_free(real_oldpath);
                return -1;
+       }
        if (strchr(item->path, G_DIR_SEPARATOR)) {
                dirpath = g_path_get_dirname(item->path);
                newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
@@ -3308,7 +3387,7 @@ gint imap_subscribe(Folder *folder, FolderItem *item, gchar *rpath, gboolean sub
                        g_free(path);
                        return -1;
                }
-               if (!strcmp(path, "INBOX") && sub == FALSE) {
+               if (!strcasecmp(path, "INBOX") && sub == FALSE) {
                        g_free(path);
                        return -1;
                }
@@ -3340,9 +3419,10 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
                return -1;
        }
        path = imap_get_real_path(session, IMAP_FOLDER(folder), item->path, &ok);
-       if (is_fatal(ok))
+       if (is_fatal(ok)) {
+               g_free(path);
                return -1;
-
+       }
        imap_threaded_subscribe(folder, path, FALSE);
 
        selected_folder = (session->mbox != NULL) &&
@@ -3376,7 +3456,7 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        g_free(path);
        cache_dir = folder_item_get_path(item);
        if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
-               g_warning("can't remove directory '%s'\n", cache_dir);
+               g_warning("can't remove directory '%s'", cache_dir);
        g_free(cache_dir);
        folder_item_remove(item);
        return 0;
@@ -3624,15 +3704,12 @@ gchar imap_get_path_separator_for_item(FolderItem *item)
        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, &ok);
@@ -3828,8 +3905,10 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
        session->expunge = 0;
 
        real_path = imap_get_real_path(session, folder, path, &ok);
-       if (is_fatal(ok))
-               return ok;              
+       if (is_fatal(ok)) {
+               g_free(real_path);
+               return ok;      
+       }       
        g_slist_free(IMAP_FOLDER_ITEM(item)->ok_flags);
        IMAP_FOLDER_ITEM(item)->ok_flags = NULL;
        ok = imap_cmd_select(session, real_path,
@@ -3964,7 +4043,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        mailimap_mailbox_data_status_free(data_status);
        
        if (got_values != mask) {
-               g_warning("status: incomplete values received (%d)\n", got_values);
+               g_warning("status: incomplete values received (%d)", got_values);
        }
        return MAILIMAP_NO_ERROR;
 }
@@ -4005,7 +4084,7 @@ static gint imap_cmd_login(IMAPSession *session,
 #else          
                        log_error(LOG_PROTOCOL, _("Connection to %s failed: "
                                        "server requires TLS, but Claws Mail "
-                                       "has been compiled without OpenSSL "
+                                       "has been compiled without TLS "
                                        "support.\n"),
                                        SESSION(session)->server);
                        return MAILIMAP_ERROR_LOGIN;
@@ -4380,7 +4459,7 @@ static gboolean imap_rename_folder_func(GNode *node, gpointer data)
        gint ok = MAILIMAP_NO_ERROR;
        oldpathlen = strlen(oldpath);
        if (strncmp(oldpath, item->path, oldpathlen) != 0) {
-               g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
+               g_warning("path doesn't match: %s, %s", oldpath, item->path);
                return TRUE;
        }
 
@@ -4481,6 +4560,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        GSList *uidlist = NULL;
        gchar *dir;
        gint known_list_len = 0;
+       gchar *path;
+
        debug_print("get_num_list\n");
        
        g_return_val_if_fail(folder != NULL, -1);
@@ -4509,6 +4590,14 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                return -1;
        }
        
+       path = folder_item_get_path(_item);
+       if (!is_dir_exist(path)) {
+               if(is_file_exist(path))
+                       claws_unlink(path);
+               make_dir_hier(path);
+       }
+       g_free(path);
+
        debug_print("getting session...\n");
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, -1);
@@ -4756,8 +4845,12 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                        return FALSE;
                }
                
-               debug_print("exists %d, item->item.total_msgs %d\n", 
-                       exists, item->item.total_msgs);
+               debug_print("exists %d, item->item.total_msgs %d\n"
+                           "\tunseen %d, item->item.unread_msgs %d\n"
+                           "\tuid_next %d, item->uid_next %d\n"
+                           "\tuid_val %d, item->item.mtime %ld\n",
+                           exists, item->item.total_msgs, unseen, item->item.unread_msgs,
+                           uid_next, item->uid_next, uid_val, (long)(item->item.mtime));
                if (exists != item->item.total_msgs
                    || unseen != item->item.unread_msgs 
                    || uid_next != item->uid_next
@@ -5353,7 +5446,7 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
                ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
                        data->msglist, flags_value, NULL, flags_set);
        } else {
-               g_warning("can't select mailbox %s\n", item->path);
+               g_warning("can't select mailbox %s", item->path);
        }
 
        if (!is_fatal(ok))
@@ -5396,7 +5489,7 @@ static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
                ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
                        data->msglist, 0, &list, tags_set);
        } else {
-               g_warning("can't select mailbox %s\n", item->path);
+               g_warning("can't select mailbox %s", item->path);
        }
 
        if (!is_fatal(ok))
@@ -5572,7 +5665,9 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                        }
 
                        if (!all && dup_name[strlen(dup_name)-1] == '/') {
-                               dup_name[strlen(dup_name)-1] = '\0';
+                               g_free(base);
+                               free(dup_name);
+                               continue;
                        }
 
                        loc_name = imap_modified_utf7_to_utf8(base, FALSE);
@@ -5581,7 +5676,7 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                        new_item = folder_item_new(FOLDER(folder), loc_name, loc_path);
                        if ((flags & ETPAN_IMAP_MB_NOINFERIORS) != 0)
                                new_item->no_sub = TRUE;
-                       if (strcmp(dup_name, "INBOX") != 0 &&
+                       if (strcasecmp(dup_name, "INBOX") != 0 &&
                            ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
                                new_item->no_select = TRUE;
 
@@ -5843,11 +5938,8 @@ void imap_disconnect_all(gboolean have_connectivity)
        GList *list;
        gboolean short_timeout;
 #ifdef HAVE_NETWORKMANAGER_SUPPORT
-       GError *error;
-#endif
+       GError *error = NULL;
 
-#ifdef HAVE_NETWORKMANAGER_SUPPORT
-       error = NULL;
        short_timeout = !networkmanager_is_online(&error);
        if(error) {
                short_timeout = TRUE;