2006-05-03 [colin] 2.1.1cvs46
[claws.git] / src / imap.c
index 48ddf08ee48cbd1d18a2544b85c5afde998b1674..d9bfe510d972a3ddd336f98cf44c5ce4e0032ae5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,6 +30,7 @@
 #include "imap.h"
 #include "imap_gtk.h"
 #include "inc.h"
+#include "xml.h"
 
 #ifdef HAVE_LIBETPAN
 
@@ -168,6 +169,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);
@@ -436,6 +440,10 @@ FolderClass *imap_get_class(void)
                imap_class.close = imap_close;
                imap_class.get_num_list = imap_get_num_list;
                imap_class.scan_required = imap_scan_required;
+               imap_class.set_xml = folder_set_xml;
+               imap_class.get_xml = folder_get_xml;
+               imap_class.item_set_xml = imap_item_set_xml;
+               imap_class.item_get_xml = imap_item_get_xml;
 
                /* Message functions */
                imap_class.get_msginfo = imap_get_msginfo;
@@ -522,7 +530,6 @@ static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
        IMAPFolderItem *item = (IMAPFolderItem *)node->data;
        
        item->lastuid = 0;
-       item->uid_next = 0;
        g_slist_free(item->uid_list);
        item->uid_list = NULL;
        
@@ -584,6 +591,9 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
        imap_get_capabilities(session);
 
        switch(type) {
+       case IMAP_AUTH_ANON:
+               ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
+               break;
        case IMAP_AUTH_CRAM_MD5:
                ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
                break;
@@ -592,8 +602,10 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
                break;
        default:
                debug_print("capabilities:\n"
+                               "\t ANONYMOUS %d\n"
                                "\t CRAM-MD5 %d\n"
                                "\t LOGIN %d\n", 
+                       imap_has_capability(session, "ANONYMOUS"),
                        imap_has_capability(session, "CRAM-MD5"),
                        imap_has_capability(session, "LOGIN"));
                if (imap_has_capability(session, "CRAM-MD5"))
@@ -676,12 +688,16 @@ static IMAPSession *imap_session_get(Folder *folder)
 {
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
        IMAPSession *session = NULL;
+       static time_t last_failure = 0;
 
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
        
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                return NULL;
        }
 
@@ -694,10 +710,14 @@ static IMAPSession *imap_session_get(Folder *folder)
                } */
        } else {
                imap_reset_uid_lists(folder);
+               if (time(NULL) - last_failure <= 2)
+                       return NULL;
                session = imap_session_new(folder, folder->account);
        }
-       if(session == NULL)
+       if(session == NULL) {
+               last_failure = time(NULL);
                return NULL;
+       }
 
        /* Make sure session is authenticated */
        if (!IMAP_SESSION(session)->authenticated)
@@ -706,6 +726,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        if (!IMAP_SESSION(session)->authenticated) {
                session_destroy(SESSION(session));
                rfolder->session = NULL;
+               last_failure = time(NULL);
                return NULL;
        }
 
@@ -854,13 +875,15 @@ static void imap_session_authenticate(IMAPSession *session,
        g_return_if_fail(account->userid != NULL);
 
        pass = account->passwd;
-       if (!pass) {
+       if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
                gchar *tmp_pass;
                tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
                if (!tmp_pass)
-                       tmp_pass = g_strdup(""); /* allow empty password */
+                       return;
                Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
                g_free(tmp_pass);
+       } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
+               pass = "";
        }
        statusbar_print_all(_("Connecting to IMAP4 server %s...\n"),
                                account->recv_server);
@@ -932,7 +955,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                make_dir_hier(path);
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        g_free(path);
-
+       debug_print("trying to fetch cached %s\n", filename);
        if (is_file_exist(filename)) {
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
@@ -951,6 +974,11 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                        procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
+               } else if (!cached) {
+                       debug_print("message not cached, considering file complete\n");
+                       procmsg_msginfo_free(msginfo);
+                       file_strip_crs(filename);
+                       return filename;
                } else {
                        procmsg_msginfo_free(cached);
                        procmsg_msginfo_free(msginfo);
@@ -1069,6 +1097,11 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                        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)
@@ -1093,20 +1126,44 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        statusbar_progress_all(0,0,0);
                        statusbar_pop_all();
                        return -1;
+               } else {
+                       debug_print("appended new message as %d\n", new_uid);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (new_uid > 0) {
+                               gchar *cache_path = folder_item_get_path(dest);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_dir_exist(cache_path)) {
+                                       gchar *cache_file = g_strconcat(
+                                               cache_path, G_DIR_SEPARATOR_S, 
+                                               itos(new_uid), NULL);
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                                       g_free(cache_file);
+                               }
+                               g_free(cache_path);
+                       }
                }
 
                if (relation != NULL)
                        g_relation_insert(relation, fileinfo->msginfo != NULL ? 
                                          (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
                                          GINT_TO_POINTER(dest->last_num + 1));
+               if (new_uid == 0) {
+                       new_uid = dest->last_num+1;
+               }
                if (last_uid < new_uid)
                        last_uid = new_uid;
                if (file_is_tmp)
                        g_unlink(real_file);
+
+               g_free(real_file);
        }
        statusbar_progress_all(0,0,0);
        statusbar_pop_all();
        
+       imap_cmd_expunge(session);
        unlock_session();
        
        g_free(destdir);
@@ -1237,18 +1294,28 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
+       /* if 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(msginfo->folder, F_QUEUE) &&
+           !folder_has_parent_of_type(dest, F_DRAFT) &&
+           !folder_has_parent_of_type(dest, F_QUEUE)) {
+               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
+               return ret;
+       } else if (folder == msginfo->folder->folder &&
+           (folder_has_parent_of_type(msginfo->folder, F_DRAFT) ||
+            folder_has_parent_of_type(msginfo->folder, F_QUEUE)) && 
+           (folder_has_parent_of_type(dest, F_DRAFT) ||
+            folder_has_parent_of_type(dest, F_QUEUE))) {
                ret = imap_do_copy_msgs(folder, dest, msglist, relation);
                return ret;
        }
-
+       /* else reupload them */
        file_list = procmsg_get_message_file_list(msglist);
        g_return_val_if_fail(file_list != NULL, -1);
 
        ret = imap_add_msgs(folder, dest, file_list, relation);
-
        procmsg_message_file_list_free(file_list);
 
        return ret;
@@ -2012,6 +2079,8 @@ static void *imap_get_uncached_messages_thread(void *data)
                if (r != MAILIMAP_NO_ERROR)
                        continue;
                
+               session_set_access_time(SESSION(session));
+
                count = 0;
                for(i = 0 ; i < carray_count(env_list) ; i ++) {
                        struct imap_fetch_env_info * info;
@@ -2095,7 +2164,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(
+                       _("Sylpheed-Claws needs network access in order "
+                         "to access the IMAP server."))) {
                        g_free(data);
                        return NULL;
                }
@@ -2176,11 +2248,31 @@ static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
        return NULL;
 }
 
+gchar imap_get_path_separator_for_item(FolderItem *item)
+{
+       Folder *folder = NULL;
+       IMAPFolder *imap_folder = NULL;
+       if (!item)
+               return '/';
+       folder = item->folder;
+       
+       if (!folder)
+               return '/';
+       
+       imap_folder = IMAP_FOLDER(folder);
+       
+       if (!imap_folder)
+               return '/';
+       
+       return imap_get_path_separator(imap_folder, item->path);
+}
 
 static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
 {
        IMAPNameSpace *namespace;
        gchar separator = '/';
+       Session *session = imap_session_get(folder);
+       g_return_val_if_fail(session != NULL, -1);
 
        if (folder->last_seen_separator == 0) {
                clist * lep_list;
@@ -2319,45 +2411,6 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        
        real_path = imap_get_real_path(folder, path);
 
-#if 0
-       if (time(NULL) - item->last_update >= 5 && item->last_update != 1) {
-               /* do the full stuff */
-               item->last_update = 1; /* force update */
-               debug_print("updating everything\n");
-               r = imap_status(session, folder, path, item,
-               &item->c_messages, &item->c_uid_next,
-               &item->c_uid_validity, &item->c_unseen, block);
-               if (r != MAILIMAP_NO_ERROR) {
-                       debug_print("status err %d\n", r);
-                       return IMAP_ERROR;
-               }
-               item->last_update = time(NULL);
-               if (messages) 
-                       *messages = item->c_messages;
-               if (uid_next)
-                       *uid_next = item->c_uid_next;
-               if (uid_validity)
-                       *uid_validity = item->c_uid_validity;
-               if (unseen)
-                       *unseen = item->c_unseen;
-               return 0;
-       } else if (time(NULL) - item->last_update < 5) {
-               /* return cached stuff */
-               debug_print("using cache\n");
-               if (messages) 
-                       *messages = item->c_messages;
-               if (uid_next)
-                       *uid_next = item->c_uid_next;
-               if (uid_validity)
-                       *uid_validity = item->c_uid_validity;
-               if (unseen)
-                       *unseen = item->c_unseen;
-               return 0;
-       }
-#endif
-
-       /* if we get there, we're updating cache */
-
        if (messages) {
                mask |= 1 << 0;
        }
@@ -2431,50 +2484,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)
@@ -2488,10 +2497,12 @@ static gint imap_cmd_login(IMAPSession *session,
                        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;
@@ -2525,7 +2536,8 @@ static gint imap_cmd_starttls(IMAPSession *session)
 {
        int r;
        
-       r = imap_threaded_starttls(session->folder);
+       r = imap_threaded_starttls(session->folder, 
+               SESSION(session)->server, SESSION(session)->port);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("starttls err %d\n", r);
                return IMAP_ERROR;
@@ -2652,7 +2664,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(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -2675,10 +2690,8 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 
        flag_list = imap_flag_to_lep(flags);
        r = imap_threaded_append(session->folder, destfolder,
-                        file, flag_list);
+                        file, flag_list, new_uid);
        mailimap_flag_list_free(flag_list);
-       if (new_uid != NULL)
-               *new_uid = 0;
 
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("append err %d\n", r);
@@ -2735,7 +2748,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(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                return -1;
        }
 
@@ -3070,7 +3086,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(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -3085,7 +3104,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 {
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        IMAPSession *session;
-       gint ok, nummsgs = 0, exists, uid_val, uid_next;
+       gint ok, nummsgs = 0, exists, uid_val, uid_next = 0;
        GSList *uidlist = NULL;
        gchar *dir;
        gboolean selected_folder;
@@ -3101,12 +3120,19 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, -1);
        lock_session();
-       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));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
@@ -3119,6 +3145,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) {
@@ -3131,6 +3159,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                        ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, item,
                                 &exists, &uid_next, &uid_val, NULL, FALSE);
                }
+               item->item.last_num = uid_next - 1;
+               
                item->use_cache = (time_t)0;
                if (ok != IMAP_SUCCESS) {
                        statusbar_pop_all();
@@ -3153,13 +3183,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                }
        }
 
-       if (!selected_folder)
-               item->uid_next = uid_next;
-
        /* If old uid_next matches new uid_next we can be sure no message
           was added to the folder */
-       if (( selected_folder && !session->folder_content_changed) ||
-           (!selected_folder && uid_next == item->uid_next)) {
+       debug_print("uid_next is %d and item->uid_next %d \n", 
+               uid_next, item->uid_next);
+       if (uid_next == item->uid_next) {
                nummsgs = g_slist_length(item->uid_list);
 
                /* If number of messages is still the same we
@@ -3168,6 +3196,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                   we discard our cache to start a new scan to find
                   out which numbers have been removed */
                if (exists == nummsgs) {
+                       debug_print("exists == nummsgs\n");
                        *msgnum_list = g_slist_copy(item->uid_list);
                        statusbar_pop_all();
                        unlock_session();
@@ -3217,6 +3246,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        remove_numbered_files_not_in_list(dir, *msgnum_list);
        g_free(dir);
        
+       item->uid_next = uid_next;
+       
        debug_print("get_num_list - ok - %i\n", nummsgs);
        statusbar_pop_all();
        unlock_session();
@@ -3305,6 +3336,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                                                }
                                                g_free(file);
                                        }
+                                       session_set_access_time(SESSION(session));
                                }
 
                                if (elem == NULL)
@@ -3360,7 +3392,7 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
        lock_session();
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
@@ -3388,8 +3420,11 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                item->c_uid_next = uid_next;
                item->c_uid_validity = uid_val;
                item->c_unseen = unseen;
-
-               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)) {
+               item->item.last_num = uid_next - 1;
+               debug_print("uidnext %d, item->uid_next %d, exists %d, item->item.total_msgs %d\n", 
+                       uid_next, item->uid_next, exists, item->item.total_msgs);
+               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)
+                   || unseen != item->item.unread_msgs || uid_val != item->item.mtime) {
                        unlock_session();
                        return TRUE;
                }
@@ -3683,37 +3718,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);
                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,
+                                                imapset, &lep_uidlist);
+                       if (r == MAILIMAP_NO_ERROR) {
+                               GSList * uidlist;
+
+                               uidlist = imap_uid_list_from_lep(lep_uidlist);
+                               mailimap_search_result_free(lep_uidlist);
+
+                               answered = g_slist_concat(answered, uidlist);
+                       }
+
+                       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
+                                                imapset, &lep_uidlist);
+                       if (r == MAILIMAP_NO_ERROR) {
+                               GSList * uidlist;
+
+                               uidlist = imap_uid_list_from_lep(lep_uidlist);
+                               mailimap_search_result_free(lep_uidlist);
+
+                               deleted = g_slist_concat(deleted, uidlist);
+                       }
                }
        }
 
@@ -3730,7 +3767,11 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                msginfo = (MsgInfo *) elem->data;
                flags = msginfo->flags.perm_flags;
                wasnew = (flags & MSG_NEW);
-               flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
+               } else {
+                       flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
+               }
                if (reverse_seen)
                        flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
                if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
@@ -3740,18 +3781,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));
        }
 
@@ -3782,14 +3827,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(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
 
        tmp = folder_item_get_msg_list(item);
 
-       if (g_slist_length(tmp) == g_slist_length(msginfo_list))
+       if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
                data->full_search = TRUE;
        
        for (cur = tmp; cur; cur = cur->next)
@@ -4206,6 +4254,9 @@ void imap_folder_unref(Folder *folder)
 
 static FolderClass imap_class;
 
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
+
 static Folder  *imap_folder_new        (const gchar    *name,
                                         const gchar    *path)
 {
@@ -4228,6 +4279,11 @@ static gint      imap_rename_folder      (Folder         *folder,
        return -1;
 }
 
+gchar imap_get_path_separator_for_item(FolderItem *item)
+{
+       return '/';
+}
+
 FolderClass *imap_get_class(void)
 {
        if (imap_class.idstr == NULL) {
@@ -4239,6 +4295,11 @@ 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 */
        }
 
@@ -4250,3 +4311,35 @@ 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;
+}