2005-10-04 [colin] 1.9.15cvs9
[claws.git] / src / imap.c
index 57e69184e8c23cb17829e3fe9c2676e988f4524a..98db8d5bcd1e1a857fa6503d8649ea1674844549 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -29,6 +29,7 @@
 #include <string.h>
 #include "imap.h"
 #include "imap_gtk.h"
+#include "inc.h"
 
 #ifdef HAVE_LIBETPAN
 
@@ -86,6 +87,8 @@ struct _IMAPFolder
        GList *ns_personal;
        GList *ns_others;
        GList *ns_shared;
+       gchar last_seen_separator;
+       guint refcnt;
 };
 
 struct _IMAPSession
@@ -94,7 +97,7 @@ struct _IMAPSession
 
        gboolean authenticated;
 
-       gchar **capability;
+       GSList *capability;
        gboolean uidplus;
 
        gchar *mbox;
@@ -153,6 +156,12 @@ struct _IMAPFolderItem
        guint uid_next;
        GSList *uid_list;
        gboolean batching;
+
+       time_t use_cache;
+       gint c_messages;
+       guint32 c_uid_next;
+       guint32 c_uid_validity;
+       gint c_unseen;
 };
 
 static void imap_folder_init           (Folder         *folder,
@@ -268,8 +277,8 @@ static gint imap_select                     (IMAPSession    *session,
 static gint imap_status                        (IMAPSession    *session,
                                         IMAPFolder     *folder,
                                         const gchar    *path,
+                                        IMAPFolderItem *item,
                                         gint           *messages,
-                                        gint           *recent,
                                         guint32        *uid_next,
                                         guint32        *uid_validity,
                                         gint           *unseen,
@@ -281,13 +290,15 @@ static gchar imap_get_path_separator              (IMAPFolder     *folder,
                                                 const gchar    *path);
 static gchar *imap_get_real_path               (IMAPFolder     *folder,
                                                 const gchar    *path);
+static void imap_synchronise           (FolderItem     *item);
 
 static void imap_free_capabilities     (IMAPSession    *session);
 
 /* low-level IMAP4rev1 commands */
 static gint imap_cmd_login     (IMAPSession    *session,
                                 const gchar    *user,
-                                const gchar    *pass);
+                                const gchar    *pass,
+                                const gchar    *type);
 static gint imap_cmd_logout    (IMAPSession    *session);
 static gint imap_cmd_noop      (IMAPSession    *session);
 #if USE_OPENSSL
@@ -440,6 +451,7 @@ FolderClass *imap_get_class(void)
                imap_class.change_flags = imap_change_flags;
                imap_class.get_flags = imap_get_flags;
                imap_class.set_batch = imap_set_batch;
+               imap_class.synchronise = imap_synchronise;
 #ifdef USE_PTREAD
                pthread_mutex_init(&imap_mutex, NULL);
 #endif
@@ -463,13 +475,16 @@ static void imap_folder_destroy(Folder *folder)
 {
        gchar *dir;
 
-       imap_done(folder);
+       while (imap_folder_get_refcnt(folder) > 0)
+               gtk_main_iteration();
+       
        dir = imap_folder_get_path(folder);
        if (is_dir_exist(dir))
                remove_dir_recursive(dir);
        g_free(dir);
 
        folder_remote_folder_destroy(REMOTE_FOLDER(folder));
+       imap_done(folder);
 }
 
 static void imap_folder_init(Folder *folder, const gchar *name,
@@ -521,19 +536,130 @@ static void imap_reset_uid_lists(Folder *folder)
        g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
 }
 
+void imap_get_capabilities(IMAPSession *session)
+{
+       struct mailimap_capability_data *capabilities = NULL;
+       clistiter *cur;
+
+       if (session->capability != NULL)
+               return;
+
+       capabilities = imap_threaded_capability(session->folder);
+
+       if (capabilities == NULL)
+               return;
+
+       for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
+           cur = clist_next(cur)) {
+               struct mailimap_capability * cap = 
+                       clist_content(cur);
+               if (!cap || cap->cap_data.cap_name == NULL)
+                       continue;
+               session->capability = g_slist_append
+                               (session->capability,
+                                g_strdup(cap->cap_data.cap_name));
+               debug_print("got capa %s\n", cap->cap_data.cap_name);
+       }
+       mailimap_capability_data_free(capabilities);
+}
+
+gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
+{
+       GSList *cur;
+       for (cur = session->capability; cur; cur = cur->next) {
+               if (!g_ascii_strcasecmp(cur->data, cap))
+                       return TRUE;
+       }
+       return FALSE;
+}
+
 static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass,
                      IMAPAuthType type)
 {
-       gint ok;
-
-       ok = imap_cmd_login(session, user, pass);
+       gint ok = IMAP_ERROR;
+       static time_t last_login_err = 0;
        
+       imap_get_capabilities(session);
+
+       switch(type) {
+       case IMAP_AUTH_CRAM_MD5:
+               ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
+               break;
+       case IMAP_AUTH_LOGIN:
+               ok = imap_cmd_login(session, user, pass, "LOGIN");
+               break;
+       default:
+               debug_print("capabilities:\n"
+                               "\t CRAM-MD5 %d\n"
+                               "\t LOGIN %d\n", 
+                       imap_has_capability(session, "CRAM-MD5"),
+                       imap_has_capability(session, "LOGIN"));
+               if (imap_has_capability(session, "CRAM-MD5"))
+                       ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
+               if (ok == IMAP_ERROR) /* we always try LOGIN before giving up */
+                       ok = imap_cmd_login(session, user, pass, "LOGIN");
+       }
        if (ok == IMAP_SUCCESS)
                session->authenticated = TRUE;
-
+       else {
+               gchar *ext_info = NULL;
+               
+               if (type == IMAP_AUTH_CRAM_MD5) {
+                       ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
+                                    "compiled with SASL support and the "
+                                    "CRAM-MD5 SASL plugin is installed.");
+               } else {
+                       ext_info = "";
+               }
+               
+               if (time(NULL) - last_login_err > 10) {
+                       if (!prefs_common.no_recv_err_panel) {
+                               alertpanel_error(_("Connection to %s failed: "
+                                       "login refused.%s"),
+                                       SESSION(session)->server, ext_info);
+                       } else {
+                               log_error(_("Connection to %s failed: "
+                                       "login refused.%s\n"),
+                                       SESSION(session)->server, ext_info);
+                       }
+               }
+               last_login_err = time(NULL);
+       }
        return ok;
 }
 
+static IMAPSession *imap_reconnect_if_possible(Folder *folder, IMAPSession *session)
+{
+       RemoteFolder *rfolder = REMOTE_FOLDER(folder);
+       /* Check if this is the first try to establish a
+          connection, if yes we don't try to reconnect */
+       debug_print("reconnecting\n");
+       if (rfolder->session == NULL) {
+               log_warning(_("Connecting to %s failed"),
+                           folder->account->recv_server);
+               session_destroy(SESSION(session));
+               session = NULL;
+       } else {
+               log_warning(_("IMAP4 connection to %s has been"
+                           " disconnected. Reconnecting...\n"),
+                           folder->account->recv_server);
+               statusbar_print_all(_("IMAP4 connection to %s has been"
+                           " disconnected. Reconnecting...\n"),
+                           folder->account->recv_server);
+               SESSION(session)->state = SESSION_DISCONNECTED;
+               session_destroy(SESSION(session));
+               /* Clear folders session to make imap_session_get create
+                  a new session, because of rfolder->session == NULL
+                  it will not try to reconnect again and so avoid an
+                  endless loop */
+               rfolder->session = NULL;
+               session = imap_session_get(folder);
+               rfolder->session = SESSION(session);
+               statusbar_pop_all();
+       }
+       return session;
+}
+
 static IMAPSession *imap_session_get(Folder *folder)
 {
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
@@ -543,7 +669,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
        
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && !inc_offline_should_override()) {
                return NULL;
        }
 
@@ -584,29 +710,8 @@ static IMAPSession *imap_session_get(Folder *folder)
        if (time(NULL) - SESSION(session)->last_access_time > SESSION_TIMEOUT_INTERVAL) {
                /* verify that the session is still alive */
                if (imap_cmd_noop(session) != IMAP_SUCCESS) {
-                       /* Check if this is the first try to establish a
-                          connection, if yes we don't try to reconnect */
-                       if (rfolder->session == NULL) {
-                               log_warning(_("Connecting to %s failed"),
-                                           folder->account->recv_server);
-                               session_destroy(SESSION(session));
-                               session = NULL;
-                       } else {
-                               log_warning(_("IMAP4 connection to %s has been"
-                                           " disconnected. Reconnecting...\n"),
-                                           folder->account->recv_server);
-                               statusbar_print_all(_("IMAP4 connection to %s has been"
-                                           " disconnected. Reconnecting...\n"),
-                                           folder->account->recv_server);
-                               session_destroy(SESSION(session));
-                               /* Clear folders session to make imap_session_get create
-                                  a new session, because of rfolder->session == NULL
-                                  it will not try to reconnect again and so avoid an
-                                  endless loop */
-                               rfolder->session = NULL;
-                               session = imap_session_get(folder);
-                               statusbar_pop_all();
-                       }
+                       debug_print("disconnected!\n");
+                       session = imap_reconnect_if_possible(folder, session);
                }
        }
 
@@ -631,6 +736,20 @@ static IMAPSession *imap_session_new(Folder * folder,
                : account->ssl_imap == SSL_TUNNEL ? IMAPS_PORT : IMAP4_PORT;
        ssl_type = account->ssl_imap;   
 #else
+       if (account->ssl_imap != SSL_NONE) {
+               if (alertpanel_full(_("Insecure connection"),
+                       _("This connection is configured to be secured "
+                         "using SSL, but SSL is not available in this "
+                         "build of Sylpheed-Claws. \n\n"
+                         "Do you want to continue connecting to this "
+                         "server? The communication would not be "
+                         "secure."),
+                         _("Con_tinue connecting"), 
+                         GTK_STOCK_CANCEL, NULL,
+                         FALSE, NULL, ALERT_WARNING,
+                         G_ALERTALTERNATE) != G_ALERTDEFAULT)
+                       return NULL;
+       }
        port = account->set_imapport ? account->imapport
                : IMAP4_PORT;
 #endif
@@ -670,7 +789,10 @@ static IMAPSession *imap_session_new(Folder * folder,
                if(!prefs_common.no_recv_err_panel) {
                        alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
                                         account->recv_server, port);
-               }
+               } else {
+                       log_error(_("Can't connect to IMAP4 server: %s:%d\n"),
+                                        account->recv_server, port);
+               } 
                
                return NULL;
        }
@@ -689,6 +811,7 @@ static IMAPSession *imap_session_new(Folder * folder,
        session->mbox = NULL;
        session->cmd_count = 0;
        session->folder = folder;
+       IMAP_FOLDER(session->folder)->last_seen_separator = 0;
 
 #if USE_OPENSSL
        if (account->ssl_imap == SSL_STARTTLS) {
@@ -725,7 +848,7 @@ static void imap_session_authenticate(IMAPSession *session,
                gchar *tmp_pass;
                tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
                if (!tmp_pass)
-                       return;
+                       tmp_pass = g_strdup(""); /* allow empty password */
                Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
                g_free(tmp_pass);
        }
@@ -744,7 +867,8 @@ static void imap_session_authenticate(IMAPSession *session,
 
 static void imap_session_destroy(Session *session)
 {
-       imap_threaded_disconnect(IMAP_SESSION(session)->folder);
+       if (session->state != SESSION_DISCONNECTED)
+               imap_threaded_disconnect(IMAP_SESSION(session)->folder);
        
        imap_free_capabilities(IMAP_SESSION(session));
        g_free(IMAP_SESSION(session)->mbox);
@@ -757,7 +881,7 @@ static gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
        return imap_fetch_msg_full(folder, item, uid, TRUE, TRUE);
 }
 
-static guint get_size_with_lfs(MsgInfo *info) 
+static guint get_size_with_crs(MsgInfo *info) 
 {
        FILE *fp = NULL;
        guint cnt = 0;
@@ -805,14 +929,17 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                 * we have to update the local file (UNIX \n only) size */
                MsgInfo *msginfo = imap_parse_msg(filename, item);
                MsgInfo *cached = msgcache_get_msg(item->cache,uid);
-               guint have_size = get_size_with_lfs(msginfo);
-               debug_print("message %d has been already %scached (%d/%d).\n", uid,
+               guint have_size = get_size_with_crs(msginfo);
+
+               if (cached)
+                       debug_print("message %d has been already %scached (%d/%d).\n", uid,
                                have_size == cached->size ? "fully ":"",
-                               have_size, cached? (int)cached->size : -1);
+                               have_size, (int)cached->size);
                
                if (cached && (cached->size == have_size || !body)) {
                        procmsg_msginfo_free(cached);
                        procmsg_msginfo_free(msginfo);
+                       file_strip_crs(filename);
                        return filename;
                } else {
                        procmsg_msginfo_free(cached);
@@ -844,6 +971,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                return NULL;
        }
 
+       file_strip_crs(filename);
        return filename;
 }
 
@@ -875,6 +1003,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        GSList *cur;
        MsgFileInfo *fileinfo;
        gint ok;
+       gint curnum = 0, total = 0;
 
 
        g_return_val_if_fail(folder != NULL, -1);
@@ -887,12 +1016,18 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
        }
        destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
 
+       statusbar_print_all(_("Adding messages..."));
+       total = g_slist_length(file_list);
        for (cur = file_list; cur != NULL; cur = cur->next) {
                IMAPFlags iflags = 0;
                guint32 new_uid = 0;
-
+               gchar *real_file = NULL;
+               gboolean file_is_tmp = FALSE;
                fileinfo = (MsgFileInfo *)cur->data;
 
+               statusbar_progress_all(curnum, total, 1);
+               curnum++;
+
                if (fileinfo->flags) {
                        if (MSG_IS_MARKED(*fileinfo->flags))
                                iflags |= IMAP_FLAG_FLAGGED;
@@ -901,18 +1036,40 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        if (!MSG_IS_UNREAD(*fileinfo->flags))
                                iflags |= IMAP_FLAG_SEEN;
                }
-
-               if (dest->stype == F_OUTBOX ||
-                   dest->stype == F_QUEUE  ||
-                   dest->stype == F_DRAFT  ||
-                   dest->stype == F_TRASH)
+               
+               if (fileinfo->flags) {
+                       if ((MSG_IS_QUEUED(*fileinfo->flags) 
+                            || MSG_IS_DRAFT(*fileinfo->flags))
+                       && !folder_has_parent_of_type(dest, F_QUEUE)
+                       && !folder_has_parent_of_type(dest, F_DRAFT)) {
+                               real_file = get_tmp_file();
+                               file_is_tmp = TRUE;
+                               if (procmsg_remove_special_headers(
+                                               fileinfo->file, 
+                                               real_file) !=0) {
+                                       g_free(real_file);
+                                       g_free(destdir);
+                                       return -1;
+                               }
+                       } 
+               }
+               if (real_file == NULL)
+                       real_file = g_strdup(fileinfo->file);
+               
+               if (folder_has_parent_of_type(dest, F_QUEUE) ||
+                   folder_has_parent_of_type(dest, F_OUTBOX) ||
+                   folder_has_parent_of_type(dest, F_DRAFT) ||
+                   folder_has_parent_of_type(dest, F_TRASH))
                        iflags |= IMAP_FLAG_SEEN;
 
-               ok = imap_cmd_append(session, destdir, fileinfo->file, iflags, 
+               ok = imap_cmd_append(session, destdir, real_file, iflags, 
                                     &new_uid);
 
                if (ok != IMAP_SUCCESS) {
-                       g_warning("can't append message %s\n", fileinfo->file);
+                       g_warning("can't append message %s\n", real_file);
+                       if (file_is_tmp)
+                               g_unlink(real_file);
+                       g_free(real_file);
                        g_free(destdir);
                        return -1;
                }
@@ -923,7 +1080,11 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                          GINT_TO_POINTER(dest->last_num + 1));
                if (last_uid < new_uid)
                        last_uid = new_uid;
+               if (file_is_tmp)
+                       g_unlink(real_file);
        }
+       statusbar_progress_all(0,0,0);
+       statusbar_pop_all();
 
        g_free(destdir);
 
@@ -941,7 +1102,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        gint ok = IMAP_SUCCESS;
        GRelation *uid_mapping;
        gint last_num = 0;
-       
+
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(dest != NULL, -1);
        g_return_val_if_fail(msglist != NULL, -1);
@@ -970,11 +1131,11 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        uid_mapping = g_relation_new(2);
        g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
        
+       statusbar_print_all(_("Copying messages..."));
        for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
                struct mailimap_set * seq_set;
-               
                seq_set = cur->data;
-               
+
                debug_print("Copying messages from %s to %s ...\n",
                            src->path, destdir);
 
@@ -1004,6 +1165,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                                          GPOINTER_TO_INT(0));
                g_tuples_destroy(tuples);
        }
+       statusbar_pop_all();
 
        g_relation_destroy(uid_mapping);
        imap_lep_set_free(seq_list);
@@ -1047,7 +1209,9 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       if (folder == msginfo->folder->folder) {
+       if (folder == msginfo->folder->folder &&
+           !folder_has_parent_of_type(msginfo->folder, F_DRAFT) &&
+           !folder_has_parent_of_type(msginfo->folder, F_QUEUE)) {
                ret = imap_do_copy_msgs(folder, dest, msglist, relation);
                return ret;
        }
@@ -1142,7 +1306,7 @@ static gint imap_remove_all_msg(Folder *folder, FolderItem *item)
 {
        GSList *list = folder_item_get_msg_list(item);
        gint res = imap_remove_msgs(folder, item, list, NULL);
-       g_slist_free(list);
+       procmsg_msg_list_free(list);
        return res;
 }
 
@@ -1386,6 +1550,15 @@ static void imap_create_missing_folders(Folder *folder)
        if (!folder->trash)
                folder->trash = imap_create_special_folder
                        (folder, F_TRASH, "Trash");
+       if (!folder->queue)
+               folder->queue = imap_create_special_folder
+                       (folder, F_QUEUE, "Queue");
+       if (!folder->outbox)
+               folder->outbox = imap_create_special_folder
+                       (folder, F_OUTBOX, "Sent");
+       if (!folder->draft)
+               folder->draft = imap_create_special_folder
+                       (folder, F_DRAFT, "Drafts");
 }
 
 static FolderItem *imap_create_special_folder(Folder *folder,
@@ -1797,7 +1970,7 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
        cur = numlist;
        data->total = g_slist_length(numlist);
        debug_print("messages list : %i\n", data->total);
-       log_print(_("IMAP> Getting uncached messages for %s\n"), item->path);
+
        while (cur != NULL) {
                GSList * partial_result;
                int count;
@@ -1831,27 +2004,15 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                data->numlist = newlist;
                data->cur += count;
                
-               if (prefs_common.work_offline && !imap_gtk_should_override()) {
+               if (prefs_common.work_offline && !inc_offline_should_override()) {
                        g_free(data);
-                       log_error(_("IMAP< Error\n"));
                        return NULL;
                }
                
                partial_result =
                        (GSList *)imap_get_uncached_messages_thread(data);
                
-               {
-                       gchar buf[32];
-                       g_snprintf(buf, sizeof(buf), "%d / %d",
-                                  data->cur, data->total);
-                       gtk_progress_bar_set_text
-                               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), buf);
-                       gtk_progress_bar_set_fraction
-                               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar),
-                                (gfloat)data->cur / (gfloat)data->total);
-                       debug_print("update progress %g\n",
-                               (gfloat)data->cur / (gfloat)data->total);
-               }
+               statusbar_progress_all(data->cur,data->total, 1);
                
                g_slist_free(newlist);
                
@@ -1859,13 +2020,9 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
        }
        g_free(data);
        
-       gtk_progress_bar_set_fraction
-               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), 0);
-       gtk_progress_bar_set_text
-               (GTK_PROGRESS_BAR(mainwindow_get_mainwindow()->progressbar), "");
+       statusbar_progress_all(0,0,0);
        statusbar_pop_all();
        
-       log_print(_("IMAP< Done\n"));
        return result;
 }
 
@@ -1928,11 +2085,36 @@ static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
        return NULL;
 }
 
+
 static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
 {
        IMAPNameSpace *namespace;
        gchar separator = '/';
 
+       if (folder->last_seen_separator == 0) {
+               clist * lep_list;
+               int r = imap_threaded_list((Folder *)folder, "", "", &lep_list);
+               if (r != MAILIMAP_NO_ERROR) {
+                       log_warning(_("LIST failed\n"));
+                       return '/';
+               }
+               
+               if (clist_count(lep_list) > 0) {
+                       clistiter * iter = clist_begin(lep_list); 
+                       struct mailimap_mailbox_list * mb;
+                       mb = clist_content(iter);
+               
+                       folder->last_seen_separator = mb->mb_delimiter;
+                       debug_print("got separator: %c\n", folder->last_seen_separator);
+               }
+               mailimap_list_result_free(lep_list);
+       }
+
+       if (folder->last_seen_separator != 0) {
+               debug_print("using separator: %c\n", folder->last_seen_separator);
+               return folder->last_seen_separator;
+       }
+
        namespace = imap_find_namespace(folder, path);
        if (namespace && namespace->separator)
                separator = namespace->separator;
@@ -2028,8 +2210,8 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
 }
 
 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
-                       const gchar *path,
-                       gint *messages, gint *recent,
+                       const gchar *path, IMAPFolderItem *item,
+                       gint *messages,
                        guint32 *uid_next, guint32 *uid_validity,
                        gint *unseen, gboolean block)
 {
@@ -2038,10 +2220,64 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        struct mailimap_mailbox_data_status * data_status;
        int got_values;
        gchar *real_path;
-
+       guint mask = 0;
+       
        real_path = imap_get_real_path(folder, path);
 
-       r = imap_threaded_status(FOLDER(folder), real_path, &data_status);
+#if 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;
+       }
+       if (uid_next) {
+               mask |= 1 << 2;
+       }
+       if (uid_validity) {
+               mask |= 1 << 3;
+       }
+       if (unseen) {
+               mask |= 1 << 4;
+       }
+       r = imap_threaded_status(FOLDER(folder), real_path, 
+               &data_status, mask);
+
        g_free(real_path);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("status err %d\n", r);
@@ -2066,11 +2302,6 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                        got_values |= 1 << 0;
                        break;
                        
-               case MAILIMAP_STATUS_ATT_RECENT:
-                       * recent = info->st_value;
-                       got_values |= 1 << 1;
-                       break;
-                       
                case MAILIMAP_STATUS_ATT_UIDNEXT:
                        * uid_next = info->st_value;
                        got_values |= 1 << 2;
@@ -2089,8 +2320,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        }
        mailimap_mailbox_data_status_free(data_status);
        
-       if (got_values != ((1 << 4) + (1 << 3) +
-                          (1 << 2) + (1 << 1) + (1 << 0))) {
+       if (got_values != mask) {
                debug_print("status: incomplete values received (%d)\n", got_values);
                return IMAP_ERROR;
        }
@@ -2099,7 +2329,8 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
 
 static void imap_free_capabilities(IMAPSession *session)
 {
-       g_strfreev(session->capability);
+       slist_free_strings(session->capability);
+       g_slist_free(session->capability);
        session->capability = NULL;
 }
 
@@ -2131,18 +2362,15 @@ static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
        challenge_len = base64_decode(challenge, buf + 2, -1);
        challenge[challenge_len] = '\0';
        g_free(buf);
-       log_print(_("IMAP< [Decoded: %s]\n"), challenge);
 
        md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
        g_free(challenge);
 
        response = g_strdup_printf("%s %s", user, hexdigest);
-       log_print(_("IMAP> [Encoded: %s]\n"), response);
        response64 = g_malloc((strlen(response) + 3) * 2 + 1);
        base64_encode(response64, response, strlen(response));
        g_free(response);
 
-       log_print(_("IMAP> %s\n"), response64);
        sock_puts(SESSION(session)->sock, response64);
        ok = imap_cmd_ok(session, NULL);
        if (ok != IMAP_SUCCESS)
@@ -2153,24 +2381,22 @@ static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
 #endif
 
 static gint imap_cmd_login(IMAPSession *session,
-                          const gchar *user, const gchar *pass)
+                          const gchar *user, const gchar *pass,
+                          const gchar *type)
 {
        int r;
        gint ok;
-       static time_t last_login_err = 0;
 
-       log_print(_("IMAP> Logging in to %s\n"), SESSION(session)->server);
-       r = imap_threaded_login(session->folder, user, pass);
+       log_print("IMAP4> Logging %s to %s using %s\n", 
+                       user,
+                       SESSION(session)->server,
+                       type);
+       r = imap_threaded_login(session->folder, user, pass, type);
        if (r != MAILIMAP_NO_ERROR) {
-               if (time(NULL) - last_login_err > 10) {
-                       alertpanel_error(_("Connection to %s failed: login refused."),
-                                       SESSION(session)->server);
-               }
-               last_login_err = time(NULL);
-               log_error(_("IMAP< Error\n"));
+               log_error("IMAP4< Error logging in to %s\n",
+                               SESSION(session)->server);
                ok = IMAP_ERROR;
        } else {
-               log_print(_("IMAP< Done\n"));
                ok = IMAP_SUCCESS;
        }
        return ok;
@@ -2178,7 +2404,6 @@ static gint imap_cmd_login(IMAPSession *session,
 
 static gint imap_cmd_logout(IMAPSession *session)
 {
-       log_print(_("IMAP> Logging out of %s\n"), SESSION(session)->server);
        imap_threaded_disconnect(session->folder);
 
        return IMAP_SUCCESS;
@@ -2195,7 +2420,8 @@ static gint imap_cmd_noop(IMAPSession *session)
                return IMAP_ERROR;
        }
        session->exists = exists;
-       
+       session_set_access_time(SESSION(session));
+
        return IMAP_SUCCESS;
 }
 
@@ -2204,14 +2430,11 @@ static gint imap_cmd_starttls(IMAPSession *session)
 {
        int r;
        
-       log_print(_("IMAP> Starting TLS\n"));
        r = imap_threaded_starttls(session->folder);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("starttls err %d\n", r);
-               log_error(_("IMAP< Error\n"));
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
        return IMAP_SUCCESS;
 }
 #endif
@@ -2222,15 +2445,12 @@ static gint imap_cmd_select(IMAPSession *session, const gchar *folder,
 {
        int r;
 
-       log_print(_("IMAP> Selecting %s\n"), folder);
        r = imap_threaded_select(session->folder, folder,
                                 exists, recent, unseen, uid_validity);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("select err %d\n", r);
-               log_error(_("IMAP< Error\n"));
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
        return IMAP_SUCCESS;
 }
 
@@ -2240,28 +2460,26 @@ static gint imap_cmd_examine(IMAPSession *session, const gchar *folder,
 {
        int r;
 
-       log_print(_("IMAP> Examining %s\n"), folder);
        r = imap_threaded_examine(session->folder, folder,
                                  exists, recent, unseen, uid_validity);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("examine err %d\n", r);
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
        return IMAP_SUCCESS;
 }
 
 static gint imap_cmd_create(IMAPSession *session, const gchar *folder)
 {
        int r;
-       log_print(_("IMAP> Creating %s\n"), folder);
+
        r = imap_threaded_create(session->folder, folder);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
+
        return IMAP_SUCCESS;
 }
 
@@ -2269,14 +2487,14 @@ static gint imap_cmd_rename(IMAPSession *session, const gchar *old_folder,
                            const gchar *new_folder)
 {
        int r;
-       log_print(_("IMAP> Renaming %s to %s\n"), old_folder, new_folder);
+
        r = imap_threaded_rename(session->folder, old_folder,
                                 new_folder);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
+
        return IMAP_SUCCESS;
 }
 
@@ -2284,13 +2502,13 @@ static gint imap_cmd_delete(IMAPSession *session, const gchar *folder)
 {
        int r;
        
-       log_print(_("IMAP> Deleting %s\n"), folder);
+
        r = imap_threaded_delete(session->folder, folder);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
+
        return IMAP_SUCCESS;
 }
 
@@ -2339,12 +2557,13 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        data->headers = headers;
        data->body = body;
 
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && !inc_offline_should_override()) {
                g_free(data);
                return -1;
        }
-
+       statusbar_print_all(_("Fetching message..."));
        result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+       statusbar_pop_all();
        g_free(data);
        return result;
 }
@@ -2362,7 +2581,6 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
        flag_list = imap_flag_to_lep(flags);
        r = imap_threaded_append(session->folder, destfolder,
                         file, flag_list);
-       
        if (new_uid != NULL)
                *new_uid = 0;
 
@@ -2382,13 +2600,12 @@ static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
        g_return_val_if_fail(set != NULL, IMAP_ERROR);
        g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
 
-       log_print(_("IMAP> Copying to %s\n"), destfolder);
        r = imap_threaded_copy(session->folder, set, destfolder);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
+
        return IMAP_SUCCESS;
 }
 
@@ -2408,13 +2625,12 @@ static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
                store_att_flags =
                        mailimap_store_att_flags_new_remove_flags_silent(flag_list);
        
-       log_print(_("IMAP> Storing\n"));
        r = imap_threaded_store(session->folder, set, store_att_flags);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
+       
        return IMAP_SUCCESS;
 }
 
@@ -2422,16 +2638,16 @@ static gint imap_cmd_expunge(IMAPSession *session)
 {
        int r;
        
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && !inc_offline_should_override()) {
                return -1;
        }
-       log_print(_("IMAP> Expunging\n"));
+
        r = imap_threaded_expunge(session->folder);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error(_("IMAP< Error\n"));
+               
                return IMAP_ERROR;
        }
-       log_print(_("IMAP< Done\n"));
+
        return IMAP_SUCCESS;
 }
 
@@ -2679,7 +2895,7 @@ static void *get_list_of_uids_thread(void *data)
        struct mailimap_set * set;
        clist * lep_uidlist;
        int r;
-       
+
        session = imap_session_get(folder);
        if (session == NULL) {
                stuff->done = TRUE;
@@ -2696,6 +2912,7 @@ static void *get_list_of_uids_thread(void *data)
        uidlist = NULL;
        
        set = mailimap_set_new_interval(item->lastuid + 1, 0);
+
        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, set,
                                 &lep_uidlist);
        if (r == MAILIMAP_NO_ERROR) {
@@ -2751,7 +2968,7 @@ static gint get_list_of_uids(Folder *folder, IMAPFolderItem *item, GSList **msgn
        data->item = item;
        data->msgnum_list = msgnum_list;
 
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       if (prefs_common.work_offline && !inc_offline_should_override()) {
                g_free(data);
                return -1;
        }
@@ -2766,7 +2983,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 {
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        IMAPSession *session;
-       gint ok, nummsgs = 0, exists, recent, uid_val, uid_next, unseen;
+       gint ok, nummsgs = 0, exists, uid_val, uid_next;
        GSList *uidlist = NULL;
        gchar *dir;
        gboolean selected_folder;
@@ -2782,24 +2999,38 @@ 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);
 
-       log_print(_("IMAP> Getting numbers list for %s\n"), item->item.path);
+       statusbar_print_all("Scanning %s...\n", FOLDER_ITEM(item)->path 
+                               ? FOLDER_ITEM(item)->path:"");
 
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
        if (selected_folder) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
-                       log_error(_("IMAP< Error\n"));
-                       return -1;
+                       debug_print("disconnected!\n");
+                       session = imap_reconnect_if_possible(folder, session);
+                       if (session == NULL) {
+                               statusbar_pop_all();
+                               return -1;
+                       }
                }
                exists = session->exists;
 
                *old_uids_valid = TRUE;
        } else {
-               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
+               if (item->use_cache && time(NULL) - item->use_cache < 2) {
+                       exists = item->c_messages;
+                       uid_next = item->c_uid_next;
+                       uid_val = item->c_uid_validity;
+                       ok = IMAP_SUCCESS;
+                       debug_print("using cache %d %d %d\n", exists, uid_next, uid_val);
+               } else {
+                       ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, item,
+                                &exists, &uid_next, &uid_val, NULL, FALSE);
+               }
+               item->use_cache = (time_t)0;
                if (ok != IMAP_SUCCESS) {
-                       log_error(_("IMAP< Error\n"));
+                       statusbar_pop_all();
                        return -1;
                }
                if(item->item.mtime == uid_val)
@@ -2834,7 +3065,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                   out which numbers have been removed */
                if (exists == nummsgs) {
                        *msgnum_list = g_slist_copy(item->uid_list);
-                       log_print(_("IMAP< Done\n"));
+                       statusbar_pop_all();
                        return nummsgs;
                } else if (exists < nummsgs) {
                        debug_print("Freeing imap uid cache");
@@ -2846,14 +3077,14 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 
        if (exists == 0) {
                *msgnum_list = NULL;
-               log_print(_("IMAP< Done\n"));
+               statusbar_pop_all();
                return 0;
        }
 
        nummsgs = get_list_of_uids(folder, item, &uidlist);
 
        if (nummsgs < 0) {
-               log_error(_("IMAP< Error\n"));
+               statusbar_pop_all();
                return -1;
        }
 
@@ -2880,8 +3111,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        g_free(dir);
        
        debug_print("get_num_list - ok - %i\n", nummsgs);
-       
-       log_print(_("IMAP< Done\n"));
+       statusbar_pop_all();
        return nummsgs;
 }
 
@@ -2896,9 +3126,9 @@ static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item)
        g_return_val_if_fail(item != NULL, NULL);
        g_return_val_if_fail(file != NULL, NULL);
 
-       if (item->stype == F_QUEUE) {
+       if (folder_has_parent_of_type(item, F_QUEUE)) {
                MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
+       } else if (folder_has_parent_of_type(item, F_DRAFT)) {
                MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
 
@@ -2933,7 +3163,8 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
        if (ok != IMAP_SUCCESS)
                return NULL;
 
-       if (!(item->stype == F_QUEUE || item->stype == F_DRAFT)) {
+       if (!(folder_has_parent_of_type(item, F_DRAFT) || 
+             folder_has_parent_of_type(item, F_QUEUE))) {
                ret = g_slist_concat(ret,
                        imap_get_uncached_messages(session, item,
                                                   msgnum_list));
@@ -3003,8 +3234,8 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
 {
        IMAPSession *session;
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
-       gint ok, exists = 0, recent = 0, unseen = 0;
-       guint32 uid_next, uid_val = 0;
+       gint ok, exists = 0, unseen = 0;
+       guint32 uid_next, uid_val;
        gboolean selected_folder;
        
        g_return_val_if_fail(folder != NULL, FALSE);
@@ -3022,18 +3253,28 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                          (!strcmp(session->mbox, item->item.path));
        if (selected_folder) {
                ok = imap_cmd_noop(session);
-               if (ok != IMAP_SUCCESS)
-                       return FALSE;
+               if (ok != IMAP_SUCCESS) {
+                       debug_print("disconnected!\n");
+                       session = imap_reconnect_if_possible(folder, session);
+                       if (session == NULL)
+                               return FALSE;
+               }
 
                if (session->folder_content_changed
                ||  session->exists != item->item.total_msgs)
                        return TRUE;
        } else {
-               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path,
-                                &exists, &recent, &uid_next, &uid_val, &unseen, FALSE);
+               ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, IMAP_FOLDER_ITEM(item),
+                                &exists, &uid_next, &uid_val, &unseen, FALSE);
                if (ok != IMAP_SUCCESS)
                        return FALSE;
 
+               item->use_cache = time(NULL);
+               item->c_messages = exists;
+               item->c_uid_next = uid_next;
+               item->c_uid_validity = uid_val;
+               item->c_unseen = unseen;
+
                if ((uid_next != item->uid_next) || (exists != item->item.total_msgs))
                        return TRUE;
        }
@@ -3078,7 +3319,12 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        if (!MSG_IS_REPLIED(msginfo->flags) &&  (newflags & MSG_REPLIED))
                flags_set |= IMAP_FLAG_ANSWERED;
        if ( MSG_IS_REPLIED(msginfo->flags) && !(newflags & MSG_REPLIED))
-               flags_set |= IMAP_FLAG_ANSWERED;
+               flags_unset |= IMAP_FLAG_ANSWERED;
+
+       if (!MSG_IS_DELETED(msginfo->flags) &&  (newflags & MSG_DELETED))
+               flags_set |= IMAP_FLAG_DELETED;
+       if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
+               flags_unset |= IMAP_FLAG_DELETED;
 
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(msginfo->msgnum);
@@ -3217,6 +3463,7 @@ typedef struct _get_flags_data {
        FolderItem *item;
        MsgInfoList *msginfo_list;
        GRelation *msgflags;
+       gboolean full_search;
        gboolean done;
 } get_flags_data;
 
@@ -3227,27 +3474,23 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
        FolderItem *item = stuff->item;
        MsgInfoList *msginfo_list = stuff->msginfo_list;
        GRelation *msgflags = stuff->msgflags;
+       gboolean full_search = stuff->full_search;
        IMAPSession *session;
-       GSList *sorted_list;
-       GSList *unseen = NULL, *answered = NULL, *flagged = NULL;
-       GSList *p_unseen, *p_answered, *p_flagged;
+       GSList *sorted_list = NULL;
+       GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL;
+       GSList *p_unseen, *p_answered, *p_flagged, *p_deleted;
        GSList *elem;
        GSList *seq_list, *cur;
        gboolean reverse_seen = FALSE;
        GString *cmd_buf;
        gint ok;
-       gint exists_cnt, recent_cnt, unseen_cnt, uid_next;
-       guint32 uidvalidity;
+       gint exists_cnt, unseen_cnt;
        gboolean selected_folder;
        
        if (folder == NULL || item == NULL) {
                stuff->done = TRUE;
                return GINT_TO_POINTER(-1);
        }
-       if (msginfo_list == NULL) {
-               stuff->done = TRUE;
-               return GINT_TO_POINTER(0);
-       }
 
        session = imap_session_get(folder);
        if (session == NULL) {
@@ -3259,8 +3502,8 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                          (!strcmp(session->mbox, item->path));
 
        if (!selected_folder) {
-               ok = imap_status(session, IMAP_FOLDER(folder), item->path,
-                        &exists_cnt, &recent_cnt, &uid_next, &uidvalidity, &unseen_cnt, TRUE);
+               ok = imap_status(session, IMAP_FOLDER(folder), item->path, IMAP_FOLDER_ITEM(item),
+                        &exists_cnt, NULL, NULL, &unseen_cnt, TRUE);
                ok = imap_select(session, IMAP_FOLDER(folder), item->path,
                        NULL, NULL, NULL, NULL, TRUE);
                if (ok != IMAP_SUCCESS) {
@@ -3279,8 +3522,13 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
        cmd_buf = g_string_new(NULL);
 
        sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
-
-       seq_list = imap_get_lep_set_from_msglist(msginfo_list);
+       if (!full_search) {
+               seq_list = imap_get_lep_set_from_msglist(msginfo_list);
+       } else {
+               struct mailimap_set * set;
+               set = mailimap_set_new_interval(1, 0);
+               seq_list = g_slist_append(NULL, set);
+       }
 
        for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
                struct mailimap_set * imapset;
@@ -3327,11 +3575,23 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                        
                        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);
+               }
        }
 
        p_unseen = unseen;
        p_answered = answered;
        p_flagged = flagged;
+       p_deleted = deleted;
 
        for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
                MsgInfo *msginfo;
@@ -3353,13 +3613,22 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                }
                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;
                g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
        }
 
        imap_lep_set_free(seq_list);
        g_slist_free(flagged);
+       g_slist_free(deleted);
        g_slist_free(answered);
        g_slist_free(unseen);
        g_slist_free(sorted_list);
@@ -3379,12 +3648,25 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
        data->item = item;
        data->msginfo_list = msginfo_list;
        data->msgflags = msgflags;
+       data->full_search = FALSE;
 
-       if (prefs_common.work_offline && !imap_gtk_should_override()) {
+       GSList *tmp = NULL, *cur;
+       
+       if (prefs_common.work_offline && !inc_offline_should_override()) {
                g_free(data);
                return -1;
        }
 
+       tmp = folder_item_get_msg_list(item);
+
+       if (g_slist_length(tmp) == g_slist_length(msginfo_list))
+               data->full_search = TRUE;
+       
+       for (cur = tmp; cur; cur = cur->next)
+               procmsg_msginfo_free((MsgInfo *)cur->data);
+       
+       g_slist_free(tmp);
+
        result = GPOINTER_TO_INT(imap_get_flags_thread(data));
        
        g_free(data);
@@ -3556,6 +3838,12 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                        free(dup_name);
                        continue;
                }
+
+               if (dup_name[strlen(dup_name)-1] == '/') {
+                       g_free(base);
+                       free(dup_name);
+                       continue;
+               }
                
                loc_name = imap_modified_utf7_to_utf8(base);
                loc_path = imap_modified_utf7_to_utf8(dup_name);
@@ -3703,9 +3991,9 @@ static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
        MsgFlags flags = {0, 0};
        
        MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
-       if (item->stype == F_QUEUE) {
+       if (folder_has_parent_of_type(item, F_QUEUE)) {
                MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
-       } else if (item->stype == F_DRAFT) {
+       } else if (folder_has_parent_of_type(item, F_DRAFT)) {
                MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
        }
        flags.perm_flags = info->flags;
@@ -3759,6 +4047,23 @@ static struct mailimap_flag_list * imap_flag_to_lep(IMAPFlags flags)
        
        return flag_list;
 }
+
+guint imap_folder_get_refcnt(Folder *folder)
+{
+       return ((IMAPFolder *)folder)->refcnt;
+}
+
+void imap_folder_ref(Folder *folder)
+{
+       ((IMAPFolder *)folder)->refcnt++;
+}
+
+void imap_folder_unref(Folder *folder)
+{
+       if (((IMAPFolder *)folder)->refcnt > 0)
+               ((IMAPFolder *)folder)->refcnt--;
+}
+
 #else /* HAVE_LIBETPAN */
 
 static FolderClass imap_class;
@@ -3801,5 +4106,9 @@ FolderClass *imap_get_class(void)
 
        return &imap_class;
 }
-
 #endif
+
+void imap_synchronise(FolderItem *item) 
+{
+       imap_gtk_synchronise(item);
+}