2006-12-18 [colin] 2.6.1cvs50
[claws.git] / src / imap.c
index 3dc6b35cca08480fd0aff224160213b32d5859d5..dc0d851b6734a47b36f21601f26a19b0c7c02dfc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2006 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
 #include "log.h"
 #include "remotefolder.h"
 #include "alertpanel.h"
-#include "sylpheed.h"
+#include "claws.h"
 #include "statusbar.h"
 #include "msgcache.h"
 #include "imap-thread.h"
+#include "account.h"
 
 typedef struct _IMAPFolder     IMAPFolder;
 typedef struct _IMAPSession    IMAPSession;
@@ -307,7 +308,6 @@ static gint imap_cmd_login  (IMAPSession    *session,
                                 const gchar    *user,
                                 const gchar    *pass,
                                 const gchar    *type);
-static gint imap_cmd_logout    (IMAPSession    *session);
 static gint imap_cmd_noop      (IMAPSession    *session);
 #if USE_OPENSSL
 static gint imap_cmd_starttls  (IMAPSession    *session);
@@ -346,7 +346,9 @@ static gint imap_cmd_append (IMAPSession    *session,
 static gint imap_cmd_copy       (IMAPSession *session,
                                 struct mailimap_set * set,
                                 const gchar *destfolder,
-                                GRelation *uid_mapping);
+                                GRelation *uid_mapping,
+                                struct mailimap_set ** source,
+                                struct mailimap_set ** dest);
 static gint imap_cmd_store     (IMAPSession    *session,
                                 struct mailimap_set * set,
                                 IMAPFlags flags,
@@ -387,7 +389,7 @@ static gchar *imap_item_get_path            (Folder         *folder,
 static MsgInfo *imap_parse_msg(const gchar *file, FolderItem *item);
 
 
-/* data types conversion libetpan <-> sylpheed */
+/* data types conversion libetpan <-> claws */
 static GSList * imap_list_from_lep(IMAPFolder * folder,
                                   clist * list, const gchar * real_path, gboolean all);
 static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist);
@@ -483,16 +485,9 @@ static Folder *imap_folder_new(const gchar *name, const gchar *path)
 
 static void imap_folder_destroy(Folder *folder)
 {
-       gchar *dir;
-
        while (imap_folder_get_refcnt(folder) > 0)
                gtk_main_iteration();
        
-       dir = imap_folder_get_path(folder);
-       if (is_dir_exist(dir))
-               remove_dir_recursive(dir);
-       g_free(dir);
-
        folder_remote_folder_destroy(REMOTE_FOLDER(folder));
        imap_done(folder);
 }
@@ -545,18 +540,24 @@ static void imap_reset_uid_lists(Folder *folder)
        g_node_traverse(folder->node, G_IN_ORDER, G_TRAVERSE_ALL, -1, imap_reset_uid_lists_func, NULL); 
 }
 
-void imap_get_capabilities(IMAPSession *session)
+int imap_get_capabilities(IMAPSession *session)
 {
        struct mailimap_capability_data *capabilities = NULL;
        clistiter *cur;
+       int result = -1;
 
        if (session->capability != NULL)
-               return;
+               return MAILIMAP_NO_ERROR;
 
-       capabilities = imap_threaded_capability(session->folder);
+       capabilities = imap_threaded_capability(session->folder, &result);
 
-       if (capabilities == NULL)
-               return;
+       if (result != MAILIMAP_NO_ERROR) {
+               return MAILIMAP_ERROR_CAPABILITY;
+       }
+
+       if (capabilities == NULL) {
+               return MAILIMAP_NO_ERROR;
+       }
 
        for(cur = clist_begin(capabilities->cap_list) ; cur != NULL ;
            cur = clist_next(cur)) {
@@ -570,6 +571,7 @@ void imap_get_capabilities(IMAPSession *session)
                debug_print("got capa %s\n", cap->cap_data.cap_name);
        }
        mailimap_capability_data_free(capabilities);
+       return MAILIMAP_NO_ERROR;
 }
 
 gboolean imap_has_capability(IMAPSession *session, const gchar *cap) 
@@ -587,8 +589,10 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
 {
        gint ok = IMAP_ERROR;
        static time_t last_login_err = 0;
+       gchar *ext_info = "";
        
-       imap_get_capabilities(session);
+       if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR)
+               return IMAP_ERROR;
 
        switch(type) {
        case IMAP_AUTH_ANON:
@@ -600,32 +604,36 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
        case IMAP_AUTH_LOGIN:
                ok = imap_cmd_login(session, user, pass, "LOGIN");
                break;
+       case IMAP_AUTH_GSSAPI:
+               ok = imap_cmd_login(session, user, pass, "GSSAPI");
+               break;
        default:
                debug_print("capabilities:\n"
                                "\t ANONYMOUS %d\n"
                                "\t CRAM-MD5 %d\n"
-                               "\t LOGIN %d\n", 
+                               "\t LOGIN %d\n"
+                               "\t GSSAPI %d\n", 
                        imap_has_capability(session, "ANONYMOUS"),
                        imap_has_capability(session, "CRAM-MD5"),
-                       imap_has_capability(session, "LOGIN"));
+                       imap_has_capability(session, "LOGIN"),
+                       imap_has_capability(session, "GSSAPI"));
                if (imap_has_capability(session, "CRAM-MD5"))
                        ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
+               if (ok == IMAP_ERROR && imap_has_capability(session, "GSSAPI"))
+                       ok = imap_cmd_login(session, user, pass, "GSSAPI");
                if (ok == IMAP_ERROR) /* we always try LOGIN before giving up */
                        ok = imap_cmd_login(session, user, pass, "LOGIN");
        }
+
        if (ok == IMAP_SUCCESS)
                session->authenticated = TRUE;
        else {
-               gchar *ext_info = NULL;
-               
                if (type == IMAP_AUTH_CRAM_MD5) {
                        ext_info = _("\n\nCRAM-MD5 logins only work if libetpan has been "
                                     "compiled with SASL support and the "
                                     "CRAM-MD5 SASL plugin is installed.");
-               } else {
-                       ext_info = "";
-               }
-               
+               } 
+
                if (time(NULL) - last_login_err > 10) {
                        if (!prefs_common.no_recv_err_panel) {
                                alertpanel_error(_("Connection to %s failed: "
@@ -688,7 +696,6 @@ 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);
@@ -696,7 +703,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                return NULL;
        }
@@ -710,12 +717,12 @@ static IMAPSession *imap_session_get(Folder *folder)
                } */
        } else {
                imap_reset_uid_lists(folder);
-               if (time(NULL) - last_failure <= 2)
+               if (time(NULL) - rfolder->last_failure <= 2)
                        return NULL;
                session = imap_session_new(folder, folder->account);
        }
        if(session == NULL) {
-               last_failure = time(NULL);
+               rfolder->last_failure = time(NULL);
                return NULL;
        }
 
@@ -724,9 +731,11 @@ static IMAPSession *imap_session_get(Folder *folder)
                imap_session_authenticate(IMAP_SESSION(session), folder->account);
        
        if (!IMAP_SESSION(session)->authenticated) {
+               imap_threaded_disconnect(session->folder);
+               SESSION(session)->state = SESSION_DISCONNECTED;
                session_destroy(SESSION(session));
                rfolder->session = NULL;
-               last_failure = time(NULL);
+               rfolder->last_failure = time(NULL);
                return NULL;
        }
 
@@ -757,7 +766,7 @@ static IMAPSession *imap_session_new(Folder * folder,
        IMAPSession *session;
        gushort port;
        int r;
-       int authenticated;
+       int authenticated = FALSE;
        
 #ifdef USE_OPENSSL
        /* FIXME: IMAP over SSL only... */ 
@@ -771,14 +780,13 @@ static IMAPSession *imap_session_new(Folder * folder,
                if (alertpanel_full(_("Insecure connection"),
                        _("This connection is configured to be secured "
                          "using SSL, but SSL is not available in this "
-                         "build of Sylpheed-Claws. \n\n"
+                         "build of Claws Mail. \n\n"
                          "Do you want to continue connecting to this "
                          "server? The communication would not be "
                          "secure."),
-                         _("Con_tinue connecting"), 
-                         GTK_STOCK_CANCEL, NULL,
-                         FALSE, NULL, ALERT_WARNING,
-                         G_ALERTALTERNATE) != G_ALERTDEFAULT)
+                         GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
+                         NULL, FALSE, NULL, ALERT_WARNING,
+                         G_ALERTDEFAULT) != G_ALERTALTERNATE)
                        return NULL;
        }
        port = account->set_imapport ? account->imapport
@@ -817,8 +825,14 @@ static IMAPSession *imap_session_new(Folder * folder,
                authenticated = FALSE;
        }
        else {
+#if (LIBETPAN_VERSION_MAJOR > 0 || LIBETPAN_VERSION_MINOR > 48)
+#ifdef USE_OPENSSL
+               if (r == MAILIMAP_ERROR_SSL)
+                       log_error(_("SSL handshake failed\n"));
+#endif
+#endif
                if(!prefs_common.no_recv_err_panel) {
-                       alertpanel_error(_("Can't connect to IMAP4 server: %s:%d"),
+                       alertpanel_error_log(_("Can't connect to IMAP4 server: %s:%d"),
                                         account->recv_server, port);
                } else {
                        log_error(_("Can't connect to IMAP4 server: %s:%d\n"),
@@ -870,11 +884,13 @@ static IMAPSession *imap_session_new(Folder * folder,
 static void imap_session_authenticate(IMAPSession *session, 
                                      const PrefsAccount *account)
 {
-       gchar *pass;
+       gchar *pass, *acc_pass;
+       gboolean failed = FALSE;
 
        g_return_if_fail(account->userid != NULL);
-
-       pass = account->passwd;
+       acc_pass = account->passwd;
+try_again:
+       pass = acc_pass;
        if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
                gchar *tmp_pass;
                tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
@@ -888,14 +904,26 @@ static void imap_session_authenticate(IMAPSession *session,
        statusbar_print_all(_("Connecting to IMAP4 server %s...\n"),
                                account->recv_server);
        if (imap_auth(session, account->userid, pass, account->imap_auth_type) != IMAP_SUCCESS) {
-               imap_threaded_disconnect(session->folder);
-               imap_cmd_logout(session);
                statusbar_pop_all();
                
+               if (!failed) {
+                       acc_pass = NULL;
+                       failed = TRUE;
+                       goto try_again;
+               } else {
+                       if (prefs_common.no_recv_err_panel) {
+                               log_error(_("Couldn't login to IMAP server %s."), account->recv_server);
+                               mainwindow_show_error();
+                       } else
+                               alertpanel_error_log(_("Couldn't login to IMAP server %s."), account->recv_server);
+               }               
+
                return;
-       }
+       } 
+
        statusbar_pop_all();
        session->authenticated = TRUE;
+       return;
 }
 
 static void imap_session_destroy(Session *session)
@@ -966,16 +994,16 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
 
                if (cached)
                        debug_print("message %d has been already %scached (%d/%d).\n", uid,
-                               have_size == cached->size ? "fully ":"",
+                               have_size >= cached->size ? "fully ":"",
                                have_size, (int)cached->size);
                
-               if (cached && (cached->size == have_size || !body)) {
+               if (cached && (cached->size <= have_size || !body)) {
                        procmsg_msginfo_free(cached);
                        procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
-               } else if (!cached) {
-                       debug_print("message not cached, considering file complete\n");
+               } else if (!cached && time(NULL) - get_file_mtime(filename) < 60) {
+                       debug_print("message not cached and file recent, considering file complete\n");
                        procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
@@ -1067,7 +1095,6 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                IMAPFlags iflags = 0;
                guint32 new_uid = 0;
                gchar *real_file = NULL;
-               gboolean file_is_tmp = FALSE;
                fileinfo = (MsgFileInfo *)cur->data;
 
                statusbar_progress_all(curnum, total, 1);
@@ -1082,28 +1109,6 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                iflags |= IMAP_FLAG_SEEN;
                }
                
-               if (fileinfo->flags) {
-                       if ((MSG_IS_QUEUED(*fileinfo->flags) 
-                            || MSG_IS_DRAFT(*fileinfo->flags))
-                       && !folder_has_parent_of_type(dest, F_QUEUE)
-                       && !folder_has_parent_of_type(dest, F_DRAFT)) {
-                               real_file = get_tmp_file();
-                               file_is_tmp = TRUE;
-                               if (procmsg_remove_special_headers(
-                                               fileinfo->file, 
-                                               real_file) !=0) {
-                                       g_free(real_file);
-                                       g_free(destdir);
-                                       unlock_session();
-                                       return -1;
-                               }
-                       }  else if (!(MSG_IS_QUEUED(*fileinfo->flags) 
-                                     || MSG_IS_DRAFT(*fileinfo->flags))
-                                   && (folder_has_parent_of_type(dest, F_QUEUE)
-                                   || folder_has_parent_of_type(dest, F_DRAFT))) {
-                               return -1;
-                       } 
-               }
                if (real_file == NULL)
                        real_file = g_strdup(fileinfo->file);
                
@@ -1118,8 +1123,6 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
 
                if (ok != IMAP_SUCCESS) {
                        g_warning("can't append message %s\n", real_file);
-                       if (file_is_tmp)
-                               g_unlink(real_file);
                        g_free(real_file);
                        g_free(destdir);
                        unlock_session();
@@ -1153,10 +1156,9 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                if (new_uid == 0) {
                        new_uid = dest->last_num+1;
                }
-               if (last_uid < new_uid)
+               if (last_uid < new_uid) {
                        last_uid = new_uid;
-               if (file_is_tmp)
-                       g_unlink(real_file);
+               }
 
                g_free(real_file);
        }
@@ -1182,6 +1184,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        gint ok = IMAP_SUCCESS;
        GRelation *uid_mapping;
        gint last_num = 0;
+       gboolean single = FALSE;
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(dest != NULL, -1);
@@ -1194,7 +1197,8 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        }
        lock_session();
        msginfo = (MsgInfo *)msglist->data;
-
+       if (msglist->next == NULL)
+               single = TRUE;
        src = msginfo->folder;
        if (src == dest) {
                g_warning("the src folder is identical to the dest.\n");
@@ -1202,6 +1206,27 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                return -1;
        }
 
+       if (src->folder != dest->folder) {
+               GSList *infolist = NULL, *cur;
+               int res = -1;
+               for (cur = msglist; cur; cur = cur->next) {
+                       msginfo = (MsgInfo *)cur->data;
+                       MsgFileInfo *fileinfo = g_new0(MsgFileInfo, 1);
+                       fileinfo->file = procmsg_get_message_file(msginfo);
+                       fileinfo->flags = &(msginfo->flags);
+                       infolist = g_slist_prepend(infolist, fileinfo);
+               }
+               infolist = g_slist_reverse(infolist);
+               res = folder_item_add_msgs(dest, infolist, FALSE);
+               for (cur = infolist; cur; cur = cur->next) {
+                       MsgFileInfo *info = (MsgFileInfo *)cur->data;
+                       g_free(info->file);
+                       g_free(info);
+               }
+               g_slist_free(infolist);
+               return res;
+       } 
+
        ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
                         NULL, NULL, NULL, NULL, FALSE);
        if (ok != IMAP_SUCCESS) {
@@ -1217,12 +1242,36 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        statusbar_print_all(_("Copying messages..."));
        for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
                struct mailimap_set * seq_set;
+               struct mailimap_set * source = NULL;
+               struct mailimap_set * dest = NULL;
                seq_set = cur->data;
 
                debug_print("Copying messages from %s to %s ...\n",
                            src->path, destdir);
 
-               ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping);
+               ok = imap_cmd_copy(session, seq_set, destdir, uid_mapping,
+                       &source, &dest);
+               
+               if (ok == IMAP_SUCCESS) {
+                       if (single && relation && source && dest) {
+                               clistiter *l = clist_begin(source->set_list);
+                               struct mailimap_set_item *i = (struct mailimap_set_item *)clist_content(l);
+                               int snum = i->set_first;
+                               int dnum = 0;
+                               l = clist_begin(dest->set_list);
+                               i = (struct mailimap_set_item *)clist_content(l);
+                               dnum = i->set_first;
+                               g_relation_insert(uid_mapping, GINT_TO_POINTER(snum), 
+                                       GINT_TO_POINTER(dnum));
+                       }
+               }
+
+
+               if (source)
+                       mailimap_set_free(source);
+               if (dest)
+                       mailimap_set_free(dest);
+
                if (ok != IMAP_SUCCESS) {
                        g_relation_destroy(uid_mapping);
                        imap_lep_set_free(seq_list);
@@ -1241,12 +1290,36 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                if (tuples->len > 0) {
                        gint num = GPOINTER_TO_INT(g_tuples_index(tuples, 0, 1));
                        g_relation_insert(relation, msginfo,
-                                         GPOINTER_TO_INT(num));
+                                         GINT_TO_POINTER(num));
                        if (num > last_num)
                                last_num = num;
+                       debug_print("copied new message as %d\n", num);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (num > 0) {
+                               gchar *cache_path = folder_item_get_path(msginfo->folder);
+                               gchar *real_file = g_strconcat(
+                                       cache_path, G_DIR_SEPARATOR_S, 
+                                       itos(msginfo->msgnum), NULL);
+                               gchar *cache_file = NULL;
+                               g_free(cache_path);
+                               cache_path = folder_item_get_path(dest);
+                               cache_file = g_strconcat(
+                                       cache_path, G_DIR_SEPARATOR_S, 
+                                       itos(num), NULL);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                               }
+                               g_free(real_file);
+                               g_free(cache_file);
+                               g_free(cache_path);
+                       }
                } else
                        g_relation_insert(relation, msginfo,
-                                         GPOINTER_TO_INT(0));
+                                         GINT_TO_POINTER(0));
                g_tuples_destroy(tuples);
        }
        statusbar_pop_all();
@@ -1284,7 +1357,6 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
                    MsgInfoList *msglist, GRelation *relation)
 {
        MsgInfo *msginfo;
-       GSList *file_list;
        gint ret;
 
        g_return_val_if_fail(folder != NULL, -1);
@@ -1294,30 +1366,7 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
-       /* if from/to are the same "type" (with or without extra headers),
-        * copy them via imap */
-       if (folder == msginfo->folder->folder &&
-           !folder_has_parent_of_type(msginfo->folder, F_DRAFT) &&
-           !folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
-           !folder_has_parent_of_type(dest, F_DRAFT) &&
-           !folder_has_parent_of_type(dest, F_QUEUE)) {
-               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
-               return ret;
-       } else if (folder == msginfo->folder->folder &&
-           (folder_has_parent_of_type(msginfo->folder, F_DRAFT) ||
-            folder_has_parent_of_type(msginfo->folder, F_QUEUE)) && 
-           (folder_has_parent_of_type(dest, F_DRAFT) ||
-            folder_has_parent_of_type(dest, F_QUEUE))) {
-               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
-               return ret;
-       }
-       /* else reupload them */
-       file_list = procmsg_get_message_file_list(msglist);
-       g_return_val_if_fail(file_list != NULL, -1);
-
-       ret = imap_add_msgs(folder, dest, file_list, relation);
-       procmsg_message_file_list_free(file_list);
-
+       ret = imap_do_copy_msgs(folder, dest, msglist, relation);
        return ret;
 }
 
@@ -1325,7 +1374,7 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
 static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest, 
                                MsgInfoList *msglist, GRelation *relation)
 {
-       gchar *destdir;
+       gchar *destdir, *dir;
        GSList *numlist = NULL, *cur;
        MsgInfo *msginfo;
        IMAPSession *session;
@@ -1354,8 +1403,9 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
        for (cur = msglist; cur; cur = cur->next) {
                msginfo = (MsgInfo *)cur->data;
                if (!MSG_IS_DELETED(msginfo->flags))
-                       numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
+                       numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
        }
+       numlist = g_slist_reverse(numlist);
 
        uid_mapping = g_relation_new(2);
        g_relation_index(uid_mapping, 0, g_direct_hash, g_direct_equal);
@@ -1375,6 +1425,15 @@ static gint imap_do_remove_msgs(Folder *folder, FolderItem *dest,
                return ok;
        }
        
+       dir = folder_item_get_path(msginfo->folder);
+       if (is_dir_exist(dir)) {
+               for (cur = msglist; cur; cur = cur->next) {
+                       msginfo = (MsgInfo *)cur->data;
+                       remove_numbered_files(dir, msginfo->msgnum, msginfo->msgnum);
+               }
+       }
+       g_free(dir);
+
        g_relation_destroy(uid_mapping);
        g_slist_free(numlist);
 
@@ -2166,7 +2225,7 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                
                if (prefs_common.work_offline && 
                    !inc_offline_should_override(
-                       _("Sylpheed-Claws needs network access in order "
+                       _("Claws Mail needs network access in order "
                          "to access the IMAP server."))) {
                        g_free(data);
                        return NULL;
@@ -2248,29 +2307,65 @@ 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_refresh_path_separator(IMAPFolder *folder, const gchar *subfolder)
+{
+       IMAPSession *session = imap_session_get(FOLDER(folder));
+       clist * lep_list;
+       int r;
+       gchar separator = '\0';
+       
+       g_return_val_if_fail(session != NULL, '/');
+       r = imap_threaded_list((Folder *)folder, "", subfolder, &lep_list);
+       
+       if (r != MAILIMAP_NO_ERROR) {
+               log_warning(_("LIST failed\n"));
+               return '\0';
+       }
+
+       if (clist_count(lep_list) > 0) {
+               clistiter * iter = clist_begin(lep_list); 
+               struct mailimap_mailbox_list * mb;
+               mb = clist_content(iter);
+
+               separator = mb->mb_delimiter;
+               debug_print("got separator: %c\n", folder->last_seen_separator);
+       }
+       mailimap_list_result_free(lep_list);
+       return separator;
+}
 
 static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
 {
        IMAPNameSpace *namespace;
        gchar separator = '/';
+       IMAPSession *session = imap_session_get(FOLDER(folder));
+       g_return_val_if_fail(session != NULL, '/');
 
        if (folder->last_seen_separator == 0) {
-               clist * lep_list;
-               int r = imap_threaded_list((Folder *)folder, "", "", &lep_list);
-               if (r != MAILIMAP_NO_ERROR) {
-                       log_warning(_("LIST failed\n"));
-                       return '/';
-               }
-               
-               if (clist_count(lep_list) > 0) {
-                       clistiter * iter = clist_begin(lep_list); 
-                       struct mailimap_mailbox_list * mb;
-                       mb = clist_content(iter);
-               
-                       folder->last_seen_separator = mb->mb_delimiter;
-                       debug_print("got separator: %c\n", folder->last_seen_separator);
-               }
-               mailimap_list_result_free(lep_list);
+               folder->last_seen_separator = imap_refresh_path_separator(folder, "");
+       }
+
+       if (folder->last_seen_separator == 0) {
+               folder->last_seen_separator = imap_refresh_path_separator(folder, "INBOX");
        }
 
        if (folder->last_seen_separator != 0) {
@@ -2278,10 +2373,6 @@ static gchar imap_get_path_separator(IMAPFolder *folder, const gchar *path)
                return folder->last_seen_separator;
        }
 
-       namespace = imap_find_namespace(folder, path);
-       if (namespace && namespace->separator)
-               separator = namespace->separator;
-
        return separator;
 }
 
@@ -2471,28 +2562,54 @@ static gint imap_cmd_login(IMAPSession *session,
        int r;
        gint ok;
 
+       if (!strcmp(type, "LOGIN") && imap_has_capability(session, "LOGINDISABLED")) {
+               gint ok = IMAP_ERROR;
+               if (imap_has_capability(session, "STARTTLS")) {
+#if USE_OPENSSL
+                       log_warning(_("Server requires TLS to log in.\n"));
+                       ok = imap_cmd_starttls(session);
+                       if (ok != IMAP_SUCCESS) {
+                               log_warning(_("Can't start TLS session.\n"));
+                               return IMAP_ERROR;
+                       } else {
+                               /* refresh capas */
+                               imap_free_capabilities(session);
+                               if (imap_get_capabilities(session) != MAILIMAP_NO_ERROR) {
+                                       log_warning(_("Can't refresh capabilities.\n"));
+                                       return IMAP_ERROR;
+                               }
+                       }
+#else          
+                       log_error(_("Connection to %s failed: "
+                                       "server requires TLS, but Claws Mail "
+                                       "has been compiled without OpenSSL "
+                                       "support.\n"),
+                                       SESSION(session)->server);
+                       return IMAP_ERROR;
+#endif
+               } else {
+                       log_error(_("Server logins are disabled.\n"));
+                       return IMAP_ERROR;
+               }
+       }
+
        log_print("IMAP4> Logging %s to %s using %s\n", 
                        user,
                        SESSION(session)->server,
                        type);
        r = imap_threaded_login(session->folder, user, pass, type);
        if (r != MAILIMAP_NO_ERROR) {
-               log_error("IMAP4< Error logging in to %s\n",
+               log_print("IMAP4< Error logging in to %s\n",
                                SESSION(session)->server);
                ok = IMAP_ERROR;
        } else {
+               log_print("IMAP4< Login to %s successful\n",
+                               SESSION(session)->server);
                ok = IMAP_SUCCESS;
        }
        return ok;
 }
 
-static gint imap_cmd_logout(IMAPSession *session)
-{
-       imap_threaded_disconnect(session->folder);
-
-       return IMAP_SUCCESS;
-}
-
 static gint imap_cmd_noop(IMAPSession *session)
 {
        int r;
@@ -2644,7 +2761,7 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
 
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
                return -1;
@@ -2668,7 +2785,7 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 
        flag_list = imap_flag_to_lep(flags);
        r = imap_threaded_append(session->folder, destfolder,
-                        file, flag_list, new_uid);
+                        file, flag_list, (int *)new_uid);
        mailimap_flag_list_free(flag_list);
 
        if (r != MAILIMAP_NO_ERROR) {
@@ -2679,7 +2796,8 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 }
 
 static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
-                         const gchar *destfolder, GRelation *uid_mapping)
+                         const gchar *destfolder, GRelation *uid_mapping,
+                         struct mailimap_set **source, struct mailimap_set **dest)
 {
        int r;
        
@@ -2687,7 +2805,7 @@ static gint imap_cmd_copy(IMAPSession *session, struct mailimap_set * set,
        g_return_val_if_fail(set != NULL, IMAP_ERROR);
        g_return_val_if_fail(destfolder != NULL, IMAP_ERROR);
 
-       r = imap_threaded_copy(session->folder, set, destfolder);
+       r = imap_threaded_copy(session->folder, set, destfolder, source, dest);
        if (r != MAILIMAP_NO_ERROR) {
                
                return IMAP_ERROR;
@@ -2728,7 +2846,7 @@ static gint imap_cmd_expunge(IMAPSession *session)
        
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                return -1;
        }
@@ -2984,7 +3102,6 @@ static void *get_list_of_uids_thread(void *data)
        gint ok, nummsgs = 0, lastuid_old;
        IMAPSession *session;
        GSList *uidlist, *elem;
-       struct mailimap_set * set;
        clist * lep_uidlist;
        int r;
 
@@ -3003,11 +3120,8 @@ static void *get_list_of_uids_thread(void *data)
 
        uidlist = NULL;
        
-       set = mailimap_set_new_interval(item->lastuid + 1, 0);
-
-       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, set,
+       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, NULL,
                                 &lep_uidlist);
-       mailimap_set_free(set);
        
        if (r == MAILIMAP_NO_ERROR) {
                GSList * fetchuid_list;
@@ -3066,7 +3180,7 @@ static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderIte
        data->session = session;
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
                return -1;
@@ -3082,11 +3196,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 {
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        IMAPSession *session;
-       gint ok, nummsgs = 0, exists, uid_val, uid_next;
+       gint ok, nummsgs = 0, exists;
+       guint32 uid_next = 0, uid_val = 0;
        GSList *uidlist = NULL;
        gchar *dir;
        gboolean selected_folder;
-       
        debug_print("get_num_list\n");
        
        g_return_val_if_fail(folder != NULL, -1);
@@ -3123,6 +3237,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                }
                exists = session->exists;
 
+               uid_next = item->c_uid_next;
+               uid_val = item->c_uid_validity;
                *old_uids_valid = TRUE;
        } else {
                if (item->use_cache && time(NULL) - item->use_cache < 2) {
@@ -3148,7 +3264,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                else {
                        *old_uids_valid = FALSE;
 
-                       debug_print("Freeing imap uid cache\n");
+                       debug_print("Freeing imap uid cache (%d != %d)\n",
+                                       (int)item->item.mtime, uid_val);
                        item->lastuid = 0;
                        g_slist_free(item->uid_list);
                        item->uid_list = NULL;
@@ -3285,13 +3402,14 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                        imap_get_uncached_messages(session, item,
                                                   msgnum_list));
        } else {
-               MsgNumberList *sorted_list, *elem;
+               MsgNumberList *sorted_list, *elem, *llast = NULL;
                gint startnum, lastnum;
 
                sorted_list = g_slist_sort(g_slist_copy(msgnum_list), g_int_compare);
 
                startnum = lastnum = GPOINTER_TO_INT(sorted_list->data);
 
+               llast = g_slist_last(ret);
                for (elem = sorted_list;; elem = g_slist_next(elem)) {
                        guint num = 0;
 
@@ -3308,7 +3426,12 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                                                MsgInfo *msginfo = imap_parse_msg(file, item);
                                                if (msginfo != NULL) {
                                                        msginfo->msgnum = i;
-                                                       ret = g_slist_append(ret, msginfo);
+                                                       if (llast == NULL)
+                                                               llast = ret = g_slist_append(ret, msginfo);
+                                                       else {
+                                                               llast = g_slist_append(llast, msginfo);
+                                                               llast = llast->next;
+                                                       }
                                                }
                                                g_free(file);
                                        }
@@ -3352,7 +3475,7 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
        IMAPSession *session;
        IMAPFolderItem *item = (IMAPFolderItem *)_item;
        gint ok, exists = 0, unseen = 0;
-       guint32 uid_next, uid_val;
+       guint32 uid_next = 0, uid_val = 0;
        gboolean selected_folder;
        
        g_return_val_if_fail(folder != NULL, FALSE);
@@ -3428,12 +3551,6 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        if (!session) {
                return;
        }
-       lock_session();
-       if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
-           NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
-               unlock_session();
-               return;
-       }
 
        if (!MSG_IS_MARKED(msginfo->flags) &&  (newflags & MSG_MARKED))
                flags_set |= IMAP_FLAG_FLAGGED;
@@ -3455,6 +3572,19 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
        if ( MSG_IS_DELETED(msginfo->flags) && !(newflags & MSG_DELETED))
                flags_unset |= IMAP_FLAG_DELETED;
 
+       if (!flags_set && !flags_unset) {
+               /* the changed flags were not translatable to IMAP-speak.
+                * like MSG_POSTFILTERED, so just apply. */
+               msginfo->flags.perm_flags = newflags;
+               return;
+       }
+
+       lock_session();
+       if ((ok = imap_select(session, IMAP_FOLDER(folder), msginfo->folder->path,
+           NULL, NULL, NULL, NULL, FALSE)) != IMAP_SUCCESS) {
+               unlock_session();
+               return;
+       }
        numlist.next = NULL;
        numlist.data = GINT_TO_POINTER(msginfo->msgnum);
 
@@ -3678,12 +3808,12 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                imapset = cur->data;
                if (reverse_seen) {
                        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SEEN,
-                                                imapset, &lep_uidlist);
+                                                full_search ? NULL:imapset, &lep_uidlist);
                }
                else {
                        r = imap_threaded_search(folder,
                                                 IMAP_SEARCH_TYPE_UNSEEN,
-                                                imapset, &lep_uidlist);
+                                                full_search ? NULL:imapset, &lep_uidlist);
                }
                if (r == MAILIMAP_NO_ERROR) {
                        GSList * uidlist;
@@ -3695,7 +3825,7 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                }
                
                r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
-                                        imapset, &lep_uidlist);
+                                        full_search ? NULL:imapset, &lep_uidlist);
                if (r == MAILIMAP_NO_ERROR) {
                        GSList * uidlist;
 
@@ -3707,7 +3837,7 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
 
                if (item->opened || item->processing_pending || item == folder->inbox) {
                        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
-                                                imapset, &lep_uidlist);
+                                                full_search ? NULL:imapset, &lep_uidlist);
                        if (r == MAILIMAP_NO_ERROR) {
                                GSList * uidlist;
 
@@ -3718,7 +3848,7 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                        }
 
                        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
-                                                imapset, &lep_uidlist);
+                                                full_search ? NULL:imapset, &lep_uidlist);
                        if (r == MAILIMAP_NO_ERROR) {
                                GSList * uidlist;
 
@@ -3805,7 +3935,7 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
        
        if (prefs_common.work_offline && 
            !inc_offline_should_override(
-               _("Sylpheed-Claws needs network access in order "
+               _("Claws Mail needs network access in order "
                  "to access the IMAP server."))) {
                g_free(data);
                return -1;
@@ -3904,7 +4034,7 @@ static void imap_set_batch (Folder *folder, FolderItem *_item, gboolean batch)
 
 
 
-/* data types conversion libetpan <-> sylpheed */
+/* data types conversion libetpan <-> claws */
 
 
 
@@ -3953,9 +4083,7 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                                   clist * list, const gchar * real_path, gboolean all)
 {
        clistiter * iter;
-       GSList * item_list;
-       
-       item_list = NULL;
+       GSList * item_list = NULL, *llast = NULL;
        
        for(iter = clist_begin(list) ; iter != NULL ;
            iter = clist_next(iter)) {
@@ -4014,8 +4142,12 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                    ((flags & ETPAN_IMAP_MB_NOSELECT) != 0))
                        new_item->no_select = TRUE;
                
-               item_list = g_slist_append(item_list, new_item);
-               
+               if (item_list == NULL)
+                       llast = item_list = g_slist_append(item_list, new_item);
+               else {
+                       llast = g_slist_append(llast, new_item);
+                       llast = llast->next;
+               }
                debug_print("folder '%s' found.\n", loc_path);
                g_free(base);
                g_free(loc_path);
@@ -4031,7 +4163,7 @@ static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
 {
        GSList *sorted_list, *cur;
        guint first, last, next;
-       GSList *ret_list = NULL;
+       GSList *ret_list = NULL, *llast = NULL;
        unsigned int count;
        struct mailimap_set * current_set;
        unsigned int item_count;
@@ -4070,8 +4202,13 @@ static GSList * imap_get_lep_set_from_numlist(MsgNumberList *numlist)
                        first = next;
                        
                        if (count >= IMAP_SET_MAX_COUNT) {
-                               ret_list = g_slist_append(ret_list,
+                               if (ret_list == NULL)
+                                       llast = ret_list = g_slist_append(ret_list,
                                                          current_set);
+                               else {
+                                       llast = g_slist_append(llast, current_set);
+                                       llast = llast->next;
+                               }
                                current_set = mailimap_set_new_empty();
                                count = 0;
                                item_count = 0;
@@ -4098,8 +4235,9 @@ static GSList * imap_get_lep_set_from_msglist(MsgInfoList *msglist)
        for (cur = msglist; cur != NULL; cur = g_slist_next(cur)) {
                MsgInfo *msginfo = (MsgInfo *) cur->data;
 
-               numlist = g_slist_append(numlist, GINT_TO_POINTER(msginfo->msgnum));
+               numlist = g_slist_prepend(numlist, GINT_TO_POINTER(msginfo->msgnum));
        }
+       numlist = g_slist_reverse(numlist);
        seq_list = imap_get_lep_set_from_numlist(numlist);
        g_slist_free(numlist);
 
@@ -4118,9 +4256,10 @@ static GSList * imap_uid_list_from_lep(clist * list)
                uint32_t * puid;
                
                puid = clist_content(iter);
-               result = g_slist_append(result, GINT_TO_POINTER(* puid));
+               result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
        }
        
+       result = g_slist_reverse(result);
        return result;
 }
 
@@ -4135,9 +4274,9 @@ static GSList * imap_uid_list_from_lep_tab(carray * list)
                uint32_t * puid;
                
                puid = carray_get(list, i);
-               result = g_slist_append(result, GINT_TO_POINTER(* puid));
+               result = g_slist_prepend(result, GINT_TO_POINTER(* puid));
        }
-       
+       result = g_slist_reverse(result);
        return result;
 }
 
@@ -4220,6 +4359,24 @@ void imap_folder_ref(Folder *folder)
        ((IMAPFolder *)folder)->refcnt++;
 }
 
+void imap_disconnect_all(void)
+{
+       GList *list;
+       for (list = account_get_list(); list != NULL; list = list->next) {
+               PrefsAccount *account = list->data;
+               if (account->protocol == A_IMAP4) {
+                       RemoteFolder *folder = (RemoteFolder *)account->folder;
+                       if (folder && folder->session) {
+                               IMAPSession *session = (IMAPSession *)folder->session;
+                               imap_threaded_disconnect(FOLDER(folder));
+                               SESSION(session)->state = SESSION_DISCONNECTED;
+                               session_destroy(SESSION(session));
+                               folder->session = NULL;
+                       }
+               }
+       }
+}
+
 void imap_folder_unref(Folder *folder)
 {
        if (((IMAPFolder *)folder)->refcnt > 0)
@@ -4236,6 +4393,19 @@ static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
 static Folder  *imap_folder_new        (const gchar    *name,
                                         const gchar    *path)
 {
+       static gboolean missing_imap_warning = TRUE;
+       if (missing_imap_warning) {
+               missing_imap_warning = FALSE;
+               alertpanel_error(
+                       _("You have one or more IMAP accounts "
+                         "defined. However this version of "
+                         "Claws Mail has been built without "
+                         "IMAP support; your IMAP account(s) are "
+                         "disabled.\n\n"
+                         "You probably need to "
+                         "install libetpan and recompile "
+                         "Claws Mail."));
+       }
        return NULL;
 }
 static gint    imap_create_tree        (Folder         *folder)
@@ -4255,6 +4425,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) {
@@ -4276,6 +4451,11 @@ FolderClass *imap_get_class(void)
 
        return &imap_class;
 }
+
+void imap_disconnect_all(void)
+{
+}
+
 #endif
 
 void imap_synchronise(FolderItem *item) 
@@ -4285,10 +4465,12 @@ void imap_synchronise(FolderItem *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;
 
@@ -4296,6 +4478,7 @@ static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
                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)
@@ -4304,8 +4487,10 @@ static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
 
        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;
 }