sync with 0.7.5cvs14
[claws.git] / src / imap.c
index 39ab65d72e3db656fa04dd93fe3ca8591b41d3b2..4ff4d074ac9817219b286784a77bd3e885f28c37 100644 (file)
 #define IMAPS_PORT     993
 #endif
 
-#define QUOTE_IF_REQUIRED(out, str) \
-{ \
-       if (*str != '"' && strchr(str, ' ')) { \
-               gchar *__tmp; \
-               gint len; \
- \
-               len = strlen(str) + 3; \
-               Xalloca(__tmp, len, return IMAP_ERROR); \
-               g_snprintf(__tmp, len, "\"%s\"", str); \
-               out = __tmp; \
-       } else { \
-               Xstrdup_a(out, str, return IMAP_ERROR); \
-       } \
+#define QUOTE_IF_REQUIRED(out, str)                            \
+{                                                              \
+       if (*str != '"' && strpbrk(str, " \t(){}%*") != NULL) { \
+               gchar *__tmp;                                   \
+               gint len;                                       \
                                                              \
+               len = strlen(str) + 3;                          \
+               Xalloca(__tmp, len, return IMAP_ERROR);         \
+               g_snprintf(__tmp, len, "\"%s\"", str);          \
+               out = __tmp;                                    \
+       } else {                                                \
+               Xstrdup_a(out, str, return IMAP_ERROR);         \
+       }                                                       \
 }
 
 static GList *session_list = NULL;
@@ -84,7 +84,12 @@ static gint imap_scan_tree_recursive (IMAPSession    *session,
                                         IMAPNameSpace  *namespace);
 static GSList *imap_parse_list         (IMAPSession    *session,
                                         const gchar    *real_path);
-static gint imap_create_trash          (Folder         *folder);
+
+static void imap_create_missing_folders        (Folder                 *folder);
+static FolderItem *imap_create_special_folder
+                                       (Folder                 *folder,
+                                        SpecialFolderItemType   stype,
+                                        const gchar            *name);
 
 static gint imap_do_copy               (Folder         *folder,
                                         FolderItem     *dest,
@@ -139,8 +144,9 @@ static gint imap_status                     (IMAPSession    *session,
                                         const gchar    *path,
                                         gint           *messages,
                                         gint           *recent,
-                                        gint           *unseen,
-                                        guint32        *uid_validity);
+                                        guint32        *uid_next,
+                                        guint32        *uid_validity,
+                                        gint           *unseen);
 
 static void imap_parse_namespace               (IMAPSession    *session,
                                                 IMAPFolder     *folder);
@@ -203,6 +209,9 @@ static gint imap_cmd_examine        (SockInfo       *sock,
                                 guint32        *uid_validity);
 static gint imap_cmd_create    (SockInfo       *sock,
                                 const gchar    *folder);
+static gint imap_cmd_rename    (SockInfo       *sock,
+                                const gchar    *oldfolder,
+                                const gchar    *newfolder);
 static gint imap_cmd_delete    (SockInfo       *sock,
                                 const gchar    *folder);
 static gint imap_cmd_envelope  (SockInfo       *sock,
@@ -256,6 +265,9 @@ static void imap_path_separator_subst               (gchar          *str,
 static gchar *imap_modified_utf7_to_locale     (const gchar    *mutf7_str);
 static gchar *imap_locale_to_modified_utf7     (const gchar    *from);
 
+static gboolean imap_rename_folder_func                (GNode          *node,
+                                                gpointer        data);
+
 Folder *imap_folder_new(const gchar *name, const gchar *path)
 {
        Folder *folder;
@@ -287,10 +299,12 @@ static void imap_folder_init(Folder *folder, const gchar *name,
        folder->copy_msgs_with_dest = imap_copy_msgs_with_dest;
        folder->remove_msg          = imap_remove_msg;
        folder->remove_all_msg      = imap_remove_all_msg;
+       folder->is_msg_changed      = imap_is_msg_changed;
        folder->scan                = imap_scan_folder;
        folder->scan_tree           = imap_scan_tree;
        folder->create_tree         = imap_create_tree;
        folder->create_folder       = imap_create_folder;
+       folder->rename_folder       = imap_rename_folder;
        folder->remove_folder       = imap_remove_folder;
 }
 
@@ -450,7 +464,7 @@ void imap_session_destroy_all(void)
                IMAPSession *session = (IMAPSession *)session_list->data;
 
                imap_cmd_logout(SESSION(session)->sock);
-               imap_session_destroy(session);
+               session_destroy(SESSION(session));
        }
 }
 
@@ -576,6 +590,15 @@ gchar *imap_fetch_msg(Folder *folder, FolderItem *item, gint uid)
                return NULL;
        }
 
+       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+                        NULL, NULL, NULL, NULL);
+       statusbar_pop_all();
+       if (ok != IMAP_SUCCESS) {
+               g_warning(_("can't select mailbox %s\n"), item->path);
+               g_free(filename);
+               return NULL;
+       }
+
        debug_print(_("getting message %d...\n"), uid);
        ok = imap_cmd_fetch(SESSION(session)->sock, (guint32)uid, filename);
 
@@ -595,6 +618,8 @@ gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
 {
        gchar *destdir;
        IMAPSession *session;
+       gint messages, recent, unseen;
+       guint32 uid_next, uid_validity;
        gint ok;
 
        g_return_val_if_fail(folder != NULL, -1);
@@ -604,6 +629,14 @@ gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
        session = imap_session_get(folder);
        if (!session) return -1;
 
+       ok = imap_status(session, IMAP_FOLDER(folder), dest->path,
+                        &messages, &recent, &uid_next, &uid_validity, &unseen);
+       statusbar_pop_all();
+       if (ok != IMAP_SUCCESS) {
+               g_warning(_("can't append message %s\n"), file);
+               return -1;
+       }
+
        destdir = imap_get_real_path(IMAP_FOLDER(folder), dest->path);
        ok = imap_cmd_append(SESSION(session)->sock, destdir, file);
        g_free(destdir);
@@ -618,7 +651,7 @@ gint imap_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
                        FILE_OP_ERROR(file, "unlink");
        }
 
-       return 0;
+       return uid_next;
 }
 
 static gint imap_do_copy(Folder *folder, FolderItem *dest, MsgInfo *msginfo,
@@ -903,34 +936,43 @@ gint imap_remove_all_msg(Folder *folder, FolderItem *item)
        return IMAP_SUCCESS;
 }
 
-void imap_scan_folder(Folder *folder, FolderItem *item)
+gboolean imap_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
+{
+       /* TODO: properly implement this method */
+       return FALSE;
+}
+
+gint imap_scan_folder(Folder *folder, FolderItem *item)
 {
        IMAPSession *session;
        gint messages, recent, unseen;
-       guint32 uid_validity;
+       guint32 uid_next, uid_validity;
        gint ok;
 
-       g_return_if_fail(folder != NULL);
-       g_return_if_fail(item != NULL);
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(item != NULL, -1);
 
        session = imap_session_get(folder);
-       if (!session) return;
+       if (!session) return -1;
 
        ok = imap_status(session, IMAP_FOLDER(folder), item->path,
-                        &messages, &recent, &unseen, &uid_validity);
+                        &messages, &recent, &uid_next, &uid_validity, &unseen);
        statusbar_pop_all();
-       if (ok != IMAP_SUCCESS) return;
+       if (ok != IMAP_SUCCESS) return -1;
 
-       item->new = recent;
+       item->new = unseen > 0 ? recent : 0;
        item->unread = unseen;
        item->total = messages;
+       item->last_num = (messages > 0 && uid_next > 0) ? uid_next - 1 : 0;
        /* item->mtime = uid_validity; */
+
+       return 0;
 }
 
 void imap_scan_tree(Folder *folder)
 {
        IMAPFolder *imapfolder = IMAP_FOLDER(folder);
-       FolderItem *item, *inbox;
+       FolderItem *item;
        IMAPSession *session;
        IMAPNameSpace *namespace = NULL;
        gchar *root_folder = NULL;
@@ -941,8 +983,8 @@ void imap_scan_tree(Folder *folder)
        session = imap_session_get(folder);
        if (!session) return;
 
-       if (imapfolder->namespace && imapfolder->namespace->data)
-               namespace = (IMAPNameSpace *)imapfolder->namespace->data;
+       if (imapfolder->ns_personal && imapfolder->ns_personal->data)
+               namespace = (IMAPNameSpace *)imapfolder->ns_personal->data;
 
        if (folder->account->imap_dir && *folder->account->imap_dir) {
                gchar *imap_dir;
@@ -966,19 +1008,13 @@ void imap_scan_tree(Folder *folder)
 
        imap_scan_tree_recursive(session, item, namespace);
 
-       if (!folder->inbox) {
-               inbox = folder_item_new("INBOX", "INBOX");
-               inbox->stype = F_INBOX;
-               folder_item_append(item, inbox);
-               folder->inbox = inbox;
-       }
-       if (!folder->trash)
-               imap_create_trash(folder);
+       imap_create_missing_folders(folder);
 }
 
 static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item,
                                     IMAPNameSpace *namespace)
 {
+       Folder *folder;
        IMAPFolder *imapfolder;
        FolderItem *new_item;
        GSList *item_list, *cur;
@@ -991,14 +1027,14 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item,
        g_return_val_if_fail(item->folder != NULL, -1);
        g_return_val_if_fail(item->no_sub == FALSE, -1);
 
-       imapfolder = IMAP_FOLDER(item->folder);
+       folder = FOLDER(item->folder);
+       imapfolder = IMAP_FOLDER(folder);
 
        if (namespace && namespace->separator)
                separator = namespace->separator;
 
        if (item->folder->ui_func)
-               item->folder->ui_func(item->folder, item,
-                                     item->folder->ui_func_data);
+               item->folder->ui_func(folder, item, folder->ui_func_data);
 
        if (item->path) {
                wildcard[0] = separator;
@@ -1008,7 +1044,8 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item,
        } else {
                wildcard[0] = '%';
                wildcard[1] = '\0';
-               real_path = g_strdup(namespace && namespace->name
+               real_path = g_strdup(namespace && namespace->name &&
+                                    strncmp(namespace->name, "INBOX", 5) != 0
                                     ? namespace->name : "");
        }
 
@@ -1026,22 +1063,35 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item,
        for (cur = item_list; cur != NULL; cur = cur->next) {
                new_item = cur->data;
                if (!strcmp(new_item->path, "INBOX")) {
-                       if (!item->folder->inbox) {
+                       if (!folder->inbox) {
                                new_item->stype = F_INBOX;
                                item->folder->inbox = new_item;
                        } else {
                                folder_item_destroy(new_item);
                                continue;
                        }
-               } else if (!item->parent && !item->folder->trash) {
-                       if (!strcasecmp(g_basename(new_item->path), "Trash")) {
+               } else if (!item->parent || item->stype == F_INBOX) {
+                       gchar *base;
+
+                       base = g_basename(new_item->path);
+
+                       if (!folder->outbox && !strcasecmp(base, "Sent")) {
+                               new_item->stype = F_OUTBOX;
+                               folder->outbox = new_item;
+                       } else if (!folder->draft && !strcasecmp(base, "Drafts")) {
+                               new_item->stype = F_DRAFT;
+                               folder->draft = new_item;
+                       } else if (!folder->queue && !strcasecmp(base, "Queue")) {
+                               new_item->stype = F_QUEUE;
+                               folder->queue = new_item;
+                       } else if (!folder->trash && !strcasecmp(base, "Trash")) {
                                new_item->stype = F_TRASH;
-                               item->folder->trash = new_item;
+                               folder->trash = new_item;
                        }
                }
                folder_item_append(item, new_item);
                if (new_item->no_select == FALSE)
-                       imap_scan_folder(new_item->folder, new_item);
+                       imap_scan_folder(folder, new_item);
                if (new_item->no_sub == FALSE)
                        imap_scan_tree_recursive(session, new_item, namespace);
        }
@@ -1116,7 +1166,8 @@ static GSList *imap_parse_list(IMAPSession *session, const gchar *real_path)
                new_item = folder_item_new(loc_name, loc_path);
                if (strcasestr(flags, "\\Noinferiors") != NULL)
                        new_item->no_sub = TRUE;
-               if (strcasestr(flags, "\\Noselect") != NULL)
+               if (strcmp(buf, "INBOX") != 0 &&
+                   strcasestr(flags, "\\Noselect") != NULL)
                        new_item->no_select = TRUE;
 
                item_list = g_slist_append(item_list, new_item);
@@ -1134,62 +1185,69 @@ static GSList *imap_parse_list(IMAPSession *session, const gchar *real_path)
 
 gint imap_create_tree(Folder *folder)
 {
-       FolderItem *item;
-
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(folder->node != NULL, -1);
        g_return_val_if_fail(folder->node->data != NULL, -1);
        g_return_val_if_fail(folder->account != NULL, -1);
 
        imap_scan_tree(folder);
+       imap_create_missing_folders(folder);
 
-       item = FOLDER_ITEM(folder->node->data);
+       return 0;
+}
 
-       if (!folder->inbox) {
-               FolderItem *inbox;
+static void imap_create_missing_folders(Folder *folder)
+{
+       g_return_if_fail(folder != NULL);
 
-               inbox = folder_item_new("INBOX", "INBOX");
-               inbox->stype = F_INBOX;
-               folder_item_append(item, inbox);
-               folder->inbox = inbox;
-       }
+       if (!folder->inbox)
+               folder->inbox = imap_create_special_folder
+                       (folder, F_INBOX, "INBOX");
+#if 0
+       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");
+       if (!folder->queue)
+               folder->queue = imap_create_special_folder
+                       (folder, F_QUEUE, "Queue");
+#endif
        if (!folder->trash)
-               imap_create_trash(folder);
-
-       return 0;
+               folder->trash = imap_create_special_folder
+                       (folder, F_TRASH, "Trash");
 }
 
-static gint imap_create_trash(Folder *folder)
+static FolderItem *imap_create_special_folder(Folder *folder,
+                                             SpecialFolderItemType stype,
+                                             const gchar *name)
 {
        FolderItem *item;
        FolderItem *new_item;
 
-       g_return_val_if_fail(folder != NULL, -1);
-       g_return_val_if_fail(folder->node != NULL, -1);
-       g_return_val_if_fail(folder->node->data != NULL, -1);
-       g_return_val_if_fail(folder->account != NULL, -1);
+       g_return_val_if_fail(folder != NULL, NULL);
+       g_return_val_if_fail(folder->node != NULL, NULL);
+       g_return_val_if_fail(folder->node->data != NULL, NULL);
+       g_return_val_if_fail(folder->account != NULL, NULL);
+       g_return_val_if_fail(name != NULL, NULL);
 
        item = FOLDER_ITEM(folder->node->data);
-       new_item = imap_create_folder(folder, item, "Trash");
+       new_item = imap_create_folder(folder, item, name);
 
        if (!new_item) {
-               gchar *path;
+               g_warning(_("Can't create '%s'\n"), name);
+               if (!folder->inbox) return NULL;
 
-               new_item = folder_item_new("Trash", "Trash");
-               folder_item_append(item, new_item);
-
-               path = folder_item_get_path(new_item);
-               if (!is_dir_exist(path))
-                       make_dir_hier(path);
-               g_free(path);
-       } else {
-               g_free(new_item->name);
-               new_item->name = g_strdup("Trash");
-       }
-       new_item->stype = F_TRASH;
-       folder->trash = new_item;
+               new_item = imap_create_folder(folder, folder->inbox, name);
+               if (!new_item)
+                       g_warning(_("Can't create '%s' under INBOX\n"), name);
+               else
+                       new_item->stype = stype;
+       } else
+               new_item->stype = stype;
 
-       return 0;
+       return new_item;
 }
 
 FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
@@ -1211,7 +1269,9 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        session = imap_session_get(folder);
        if (!session) return NULL;
 
-       if (parent->path)
+       if (!parent->parent && strcmp(name, "INBOX") == 0)
+               dirpath = g_strdup(name);
+       else if (parent->path)
                dirpath = g_strconcat(parent->path, "/", name, NULL);
        else if ((p = strchr(name, '/')) != NULL && *(p + 1) != '\0')
                dirpath = g_strdup(name);
@@ -1286,11 +1346,100 @@ FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        return new_item;
 }
 
+gint imap_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
+{
+       gchar *dirpath;
+       gchar *newpath;
+       gchar *real_oldpath;
+       gchar *real_newpath;
+       GNode *node;
+       gchar *paths[2];
+       gchar *old_cache_dir;
+       gchar *new_cache_dir;
+       IMAPSession *session;
+       IMAPNameSpace *namespace;
+       gint ok;
+       gint exists, recent, unseen;
+       guint32 uid_validity;
+
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(item != NULL, -1);
+       g_return_val_if_fail(item->path != NULL, -1);
+       g_return_val_if_fail(name != NULL, -1);
+
+       session = imap_session_get(folder);
+       if (!session) return -1;
+
+       real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
+
+       g_free(session->mbox);
+       session->mbox = NULL;
+       ok = imap_cmd_examine(SESSION(session)->sock, "INBOX",
+                             &exists, &recent, &unseen, &uid_validity);
+       statusbar_pop_all();
+       if (ok != IMAP_SUCCESS) {
+               g_free(real_oldpath);
+               return -1;
+       }
+
+       namespace = imap_find_namespace(IMAP_FOLDER(folder), item->path);
+       if (strchr(item->path, G_DIR_SEPARATOR)) {
+               dirpath = g_dirname(item->path);
+               newpath = g_strconcat(dirpath, G_DIR_SEPARATOR_S, name, NULL);
+               g_free(dirpath);
+       } else
+               newpath = g_strdup(name);
+
+       real_newpath = imap_locale_to_modified_utf7(newpath);
+       if (namespace && namespace->separator)
+               imap_path_separator_subst(real_newpath, namespace->separator);
+
+       ok = imap_cmd_rename(SESSION(session)->sock, real_oldpath, real_newpath);
+       statusbar_pop_all();
+       if (ok != IMAP_SUCCESS) {
+               log_warning(_("can't rename mailbox: %s to %s\n"),
+                           real_oldpath, real_newpath);
+               g_free(real_oldpath);
+               g_free(newpath);
+               g_free(real_newpath);
+               return -1;
+       }
+
+       g_free(item->name);
+       item->name = g_strdup(name);
+
+       old_cache_dir = folder_item_get_path(item);
+
+       node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
+                          item);
+       paths[0] = g_strdup(item->path);
+       paths[1] = newpath;
+       g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+                       imap_rename_folder_func, paths);
+
+       if (is_dir_exist(old_cache_dir)) {
+               new_cache_dir = folder_item_get_path(item);
+               if (rename(old_cache_dir, new_cache_dir) < 0) {
+                       FILE_OP_ERROR(old_cache_dir, "rename");
+               }
+               g_free(new_cache_dir);
+       }
+
+       g_free(old_cache_dir);
+       g_free(paths[0]);
+       g_free(newpath);
+       g_free(real_oldpath);
+       g_free(real_newpath);
+
+       return 0;
+}
+
 gint imap_remove_folder(Folder *folder, FolderItem *item)
 {
        gint ok;
        IMAPSession *session;
        gchar *path;
+       gchar *cache_dir;
        gint exists, recent, unseen;
        guint32 uid_validity;
 
@@ -1320,6 +1469,10 @@ gint imap_remove_folder(Folder *folder, FolderItem *item)
        }
 
        g_free(path);
+       cache_dir = folder_item_get_path(item);
+       if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
+               g_warning("can't remove directory '%s'\n", cache_dir);
+       g_free(cache_dir);
        folder_item_remove(item);
 
        return 0;
@@ -1356,11 +1509,18 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                        return newlist;
                }
                strretchomp(tmp);
-               log_print("IMAP4< %s\n", tmp);
                if (tmp[0] != '*' || tmp[1] != ' ') {
+                       log_print("IMAP4< %s\n", tmp);
                        g_free(tmp);
                        break;
                }
+               if (strstr(tmp, "FETCH") == NULL) {
+                       log_print("IMAP4< %s\n", tmp);
+                       g_free(tmp);
+                       continue;
+               }
+               /* log_print("IMAP4< %s\n", tmp); */
+               debug_print("IMAP4< %s\n", tmp);
                g_string_assign(str, tmp);
                g_free(tmp);
 
@@ -1369,6 +1529,11 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                        log_warning(_("can't parse envelope: %s\n"), str->str);
                        continue;
                }
+               if (item->stype == F_QUEUE) {
+                       MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED);
+               } else if (item->stype == F_DRAFT) {
+                       MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT);
+               }
 
                msginfo->folder = item;
 
@@ -1445,6 +1610,8 @@ static SockInfo *imap_open_tunnel(const gchar *server,
        SockInfo *sock;
 
        if ((sock = sock_connect_cmd(server, tunnelcmd)) == NULL)
+               log_warning(_("Can't establish IMAP4 session with: %s\n"),
+                           server);
                return NULL;
 
        return imap_init_sock(sock);
@@ -1468,6 +1635,8 @@ static SockInfo *imap_open(const gchar *server, gushort port)
 
 #if USE_SSL
        if (use_ssl && !ssl_init_socket(sock)) {
+               log_warning(_("Can't establish IMAP4 session with: %s:%d\n"),
+                           server, port);
                sock_close(sock);
                return NULL;
        }
@@ -1482,6 +1651,7 @@ static SockInfo *imap_init_sock(SockInfo *sock)
        imap_cmd_count = 0;
 
        if (imap_cmd_noop(sock) != IMAP_SUCCESS) {
+               log_warning(_("Can't establish IMAP4 session\n"));
                sock_close(sock);
                return NULL;
        }
@@ -1489,95 +1659,95 @@ static SockInfo *imap_init_sock(SockInfo *sock)
        return sock;
 }
 
-
-
-#define THROW goto catch
-
-static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
+static GList *imap_parse_namespace_str(gchar *str)
 {
-       gchar *ns_str;
+       gchar *p = str;
        gchar *name;
        gchar *separator;
-       gchar *p;
        IMAPNameSpace *namespace;
        GList *ns_list = NULL;
 
-       g_return_if_fail(session != NULL);
-       g_return_if_fail(folder != NULL);
-
-       if (folder->namespace != NULL) return;
-
-       if (imap_cmd_namespace(SESSION(session)->sock, &ns_str)
-           != IMAP_SUCCESS) {
-               log_warning(_("can't get namespace\n"));
-               return;
-       }
-
-       /* get the first element */
-       extract_one_parenthesis_with_skip_quote(ns_str, '"', '(', ')');
-       g_strstrip(ns_str);
-       p = ns_str;
-
        while (*p != '\0') {
                /* parse ("#foo" "/") */
 
                while (*p && *p != '(') p++;
-               if (*p == '\0') THROW;
+               if (*p == '\0') break;
                p++;
 
                while (*p && *p != '"') p++;
-               if (*p == '\0') THROW;
+               if (*p == '\0') break;
                p++;
                name = p;
 
                while (*p && *p != '"') p++;
-               if (*p == '\0') THROW;
+               if (*p == '\0') break;
                *p = '\0';
                p++;
 
                while (*p && isspace(*p)) p++;
-               if (*p == '\0') THROW;
+               if (*p == '\0') break;
                if (strncmp(p, "NIL", 3) == 0)
                        separator = NULL;
                else if (*p == '"') {
                        p++;
                        separator = p;
                        while (*p && *p != '"') p++;
-                       if (*p == '\0') THROW;
+                       if (*p == '\0') break;
                        *p = '\0';
                        p++;
-               } else THROW;
+               } else break;
 
                while (*p && *p != ')') p++;
-               if (*p == '\0') THROW;
+               if (*p == '\0') break;
                p++;
 
                namespace = g_new(IMAPNameSpace, 1);
                namespace->name = g_strdup(name);
                namespace->separator = separator ? separator[0] : '\0';
                ns_list = g_list_append(ns_list, namespace);
-               IMAP_FOLDER(folder)->namespace = ns_list;
        }
 
-catch:
-       g_free(ns_str);
-       return;
+       return ns_list;
 }
 
-#undef THROW
+static void imap_parse_namespace(IMAPSession *session, IMAPFolder *folder)
+{
+       gchar *ns_str;
+       gchar **str_array;
 
-static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
-                                         const gchar *path)
+       g_return_if_fail(session != NULL);
+       g_return_if_fail(folder != NULL);
+
+       if (folder->ns_personal != NULL ||
+           folder->ns_others   != NULL ||
+           folder->ns_shared   != NULL)
+               return;
+
+       if (imap_cmd_namespace(SESSION(session)->sock, &ns_str)
+           != IMAP_SUCCESS) {
+               log_warning(_("can't get namespace\n"));
+               return;
+       }
+
+       str_array = strsplit_parenthesis(ns_str, '(', ')', 3);
+       if (str_array[0])
+               folder->ns_personal = imap_parse_namespace_str(str_array[0]);
+       if (str_array[0] && str_array[1])
+               folder->ns_others = imap_parse_namespace_str(str_array[1]);
+       if (str_array[0] && str_array[1] && str_array[2])
+               folder->ns_shared = imap_parse_namespace_str(str_array[2]);
+       g_strfreev(str_array);
+       return;
+}
+
+static IMAPNameSpace *imap_find_namespace_from_list(GList *ns_list,
+                                                   const gchar *path)
 {
        IMAPNameSpace *namespace = NULL;
-       GList *ns_list;
        gchar *name;
 
-       g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(path != NULL, NULL);
 
-       ns_list = folder->namespace;
-
        for (; ns_list != NULL; ns_list = ns_list->next) {
                IMAPNameSpace *tmp_ns = ns_list->data;
 
@@ -1591,6 +1761,21 @@ static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
        return namespace;
 }
 
+static IMAPNameSpace *imap_find_namespace(IMAPFolder *folder,
+                                         const gchar *path)
+{
+       IMAPNameSpace *namespace;
+
+       namespace = imap_find_namespace_from_list(folder->ns_personal, path);
+       if (namespace) return namespace;
+       namespace = imap_find_namespace_from_list(folder->ns_others, path);
+       if (namespace) return namespace;
+       namespace = imap_find_namespace_from_list(folder->ns_shared, path);
+       if (namespace) return namespace;
+
+       return NULL;
+}
+
 static gchar *imap_get_real_path(IMAPFolder *folder, const gchar *path)
 {
        gchar *real_path;
@@ -1611,10 +1796,24 @@ static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
                              gchar *dest, gint dest_len, GString *str)
 {
        gchar *cur_pos = src;
+       gchar *nextline;
 
        g_return_val_if_fail(str != NULL, cur_pos);
 
-       while (*cur_pos == ' ') cur_pos++;
+       /* read the next line if the current response buffer is empty */
+       while (isspace(*cur_pos)) cur_pos++;
+       while (*cur_pos == '\0') {
+               if ((nextline = sock_getline(sock)) == NULL)
+                       return cur_pos;
+               g_string_assign(str, nextline);
+               cur_pos = str->str;
+               strretchomp(nextline);
+               /* log_print("IMAP4< %s\n", nextline); */
+               debug_print("IMAP4< %s\n", nextline);
+               g_free(nextline);
+
+               while (isspace(*cur_pos)) cur_pos++;
+       }
 
        if (!strncmp(cur_pos, "NIL", 3)) {
                *dest = '\0';
@@ -1627,21 +1826,29 @@ static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
        } else if (*cur_pos == '{') {
                gchar buf[32];
                gint len;
-               gchar *nextline;
+               gint line_len = 0;
 
                cur_pos = strchr_cpy(cur_pos + 1, '}', buf, sizeof(buf));
                len = atoi(buf);
 
-               if ((nextline = sock_getline(sock)) == NULL)
-                       return cur_pos;
-               strretchomp(nextline);
-               log_print("IMAP4< %s\n", nextline);
-               g_string_assign(str, nextline);
-
-               len = MIN(len, strlen(nextline));
-               memcpy(dest, nextline, MIN(len, dest_len - 1));
+               g_string_truncate(str, 0);
+               cur_pos = str->str;
+
+               do {
+                       if ((nextline = sock_getline(sock)) == NULL)
+                               return cur_pos;
+                       line_len += strlen(nextline);
+                       g_string_append(str, nextline);
+                       cur_pos = str->str;
+                       strretchomp(nextline);
+                       /* log_print("IMAP4< %s\n", nextline); */
+                       debug_print("IMAP4< %s\n", nextline);
+                       g_free(nextline);
+               } while (line_len < len);
+
+               memcpy(dest, cur_pos, MIN(len, dest_len - 1));
                dest[MIN(len, dest_len - 1)] = '\0';
-               cur_pos = str->str + len;
+               cur_pos += len;
        }
 
        return cur_pos;
@@ -1743,8 +1950,8 @@ static MsgFlags imap_parse_flags(const gchar *flag_str)
        while ((p = strchr(p, '\\')) != NULL) {
                p++;
 
-               if (g_strncasecmp(p, "Recent", 6) == 0) {
-                       MSG_SET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
+               if (g_strncasecmp(p, "Recent", 6) == 0 && MSG_IS_UNREAD(flags)) {
+                       MSG_SET_PERM_FLAGS(flags, MSG_NEW);
                } else if (g_strncasecmp(p, "Seen", 4) == 0) {
                        MSG_UNSET_PERM_FLAGS(flags, MSG_NEW|MSG_UNREAD);
                } else if (g_strncasecmp(p, "Deleted", 7) == 0) {
@@ -1822,8 +2029,10 @@ static MsgInfo *imap_parse_envelope(SockInfo *sock, GString *line_str)
                        g_return_val_if_fail(*cur_pos == '(', NULL);
                        cur_pos = imap_parse_atom
                                (sock, cur_pos + 1, buf, sizeof(buf), line_str);
-                       Xstrdup_a(date, buf, return NULL);
-                       date_t = procheader_date_parse(NULL, date, 0);
+                       if (buf[0] != '\0') {
+                               Xstrdup_a(date, buf, return NULL);
+                               date_t = procheader_date_parse(NULL, date, 0);
+                       }
 
                        cur_pos = imap_parse_atom
                                (sock, cur_pos, buf, sizeof(buf), line_str);
@@ -1837,12 +2046,16 @@ static MsgInfo *imap_parse_envelope(SockInfo *sock, GString *line_str)
                                                     &tmp_from, &tmp_fromname,
                                                     line_str);
                        g_return_val_if_fail(cur_pos != NULL, NULL);
-                       Xstrdup_a(from, tmp_from,
-                                 {g_free(tmp_from); g_free(tmp_fromname);
-                                  return NULL;});
-                       Xstrdup_a(fromname, tmp_fromname,
-                                 {g_free(tmp_from); g_free(tmp_fromname);
-                                  return NULL;});
+                       if (tmp_from && *tmp_from != '\0')
+                               Xstrdup_a(from, tmp_from,
+                                         {g_free(tmp_from);
+                                          g_free(tmp_fromname);
+                                          return NULL;});
+                       if (tmp_fromname && *tmp_fromname != '\0')
+                               Xstrdup_a(fromname, tmp_fromname,
+                                         {g_free(tmp_from);
+                                          g_free(tmp_fromname);
+                                          return NULL;});
                        g_free(tmp_from);
                        g_free(tmp_fromname);
 
@@ -2018,12 +2231,27 @@ static gint imap_select(IMAPSession *session, IMAPFolder *folder,
 {
        gchar *real_path;
        gint ok;
+       gint exists_, recent_, unseen_, uid_validity_;
+
+       if (!exists || !recent || !unseen || !uid_validity) {
+               if (session->mbox && strcmp(session->mbox, path) == 0)
+                       return IMAP_SUCCESS;
+               exists = &exists_;
+               recent = &recent_;
+               unseen = &unseen_;
+               uid_validity = &uid_validity_;
+       }
+
+       g_free(session->mbox);
+       session->mbox = NULL;
 
        real_path = imap_get_real_path(folder, path);
        ok = imap_cmd_select(SESSION(session)->sock, real_path,
                             exists, recent, unseen, uid_validity);
        if (ok != IMAP_SUCCESS)
                log_warning(_("can't select folder: %s\n"), real_path);
+       else
+               session->mbox = g_strdup(path);
        g_free(real_path);
 
        return ok;
@@ -2063,8 +2291,9 @@ catch:
 
 static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                        const gchar *path,
-                       gint *messages, gint *recent, gint *unseen,
-                       guint32 *uid_validity)
+                       gint *messages, gint *recent,
+                       guint32 *uid_next, guint32 *uid_validity,
+                       gint *unseen)
 {
        gchar *real_path;
        gchar *real_path_;
@@ -2072,14 +2301,15 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        GPtrArray *argbuf;
        gchar *str;
 
-       *messages = *recent = *unseen = *uid_validity = 0;
+       *messages = *recent = *uid_next = *uid_validity = *unseen = 0;
 
        argbuf = g_ptr_array_new();
 
        real_path = imap_get_real_path(folder, path);
        QUOTE_IF_REQUIRED(real_path_, real_path);
        imap_cmd_gen_send(SESSION(session)->sock, "STATUS %s "
-                         "(MESSAGES RECENT UNSEEN UIDVALIDITY)", real_path_);
+                         "(MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)",
+                         real_path_);
 
        ok = imap_cmd_ok(SESSION(session)->sock, argbuf);
        if (ok != IMAP_SUCCESS) THROW(ok);
@@ -2099,12 +2329,15 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
                } else if (!strncmp(str, "RECENT ", 7)) {
                        str += 7;
                        *recent = strtol(str, &str, 10);
-               } else if (!strncmp(str, "UNSEEN ", 7)) {
-                       str += 7;
-                       *unseen = strtol(str, &str, 10);
+               } else if (!strncmp(str, "UIDNEXT ", 8)) {
+                       str += 8;
+                       *uid_next = strtoul(str, &str, 10);
                } else if (!strncmp(str, "UIDVALIDITY ", 12)) {
                        str += 12;
                        *uid_validity = strtoul(str, &str, 10);
+               } else if (!strncmp(str, "UNSEEN ", 7)) {
+                       str += 7;
+                       *unseen = strtol(str, &str, 10);
                } else {
                        g_warning("invalid STATUS response: %s\n", str);
                        break;
@@ -2304,6 +2537,18 @@ static gint imap_cmd_create(SockInfo *sock, const gchar *folder)
        return imap_cmd_ok(sock, NULL);
 }
 
+static gint imap_cmd_rename(SockInfo *sock, const gchar *old_folder,
+                           const gchar *new_folder)
+{
+       gchar *old_folder_, *new_folder_;
+
+       QUOTE_IF_REQUIRED(old_folder_, old_folder);
+       QUOTE_IF_REQUIRED(new_folder_, new_folder);
+       imap_cmd_gen_send(sock, "RENAME %s %s", old_folder_, new_folder_);
+
+       return imap_cmd_ok(sock, NULL);
+}
+
 static gint imap_cmd_delete(SockInfo *sock, const gchar *folder)
 {
        gchar *folder_;
@@ -2326,12 +2571,15 @@ static gint imap_cmd_fetch(SockInfo *sock, guint32 uid, const gchar *filename)
 
        imap_cmd_gen_send(sock, "UID FETCH %d BODY[]", uid);
 
-       if (sock_gets(sock, buf, sizeof(buf)) < 0)
-               return IMAP_ERROR;
-       strretchomp(buf);
-       if (buf[0] != '*' || buf[1] != ' ')
-               return IMAP_ERROR;
-       log_print("IMAP4< %s\n", buf);
+       while ((ok = imap_cmd_gen_recv(sock, buf, sizeof(buf)))
+              == IMAP_SUCCESS) {
+               if (buf[0] != '*' || buf[1] != ' ')
+                       return IMAP_ERROR;
+               if (strstr(buf, "FETCH") != NULL)
+                       break;
+       }
+       if (ok != IMAP_SUCCESS)
+               return ok;
 
        cur_pos = strchr(buf, '{');
        g_return_val_if_fail(cur_pos != NULL, IMAP_ERROR);
@@ -2803,3 +3051,32 @@ static gchar *imap_locale_to_modified_utf7(const gchar *from)
        return to;
 #endif
 }
+
+static gboolean imap_rename_folder_func(GNode *node, gpointer data)
+{
+       FolderItem *item = node->data;
+       gchar **paths = data;
+       const gchar *oldpath = paths[0];
+       const gchar *newpath = paths[1];
+       gchar *base;
+       gchar *new_itempath;
+       gint oldpathlen;
+
+       oldpathlen = strlen(oldpath);
+       if (strncmp(oldpath, item->path, oldpathlen) != 0) {
+               g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
+               return TRUE;
+       }
+
+       base = item->path + oldpathlen;
+       while (*base == G_DIR_SEPARATOR) base++;
+       if (*base == '\0')
+               new_itempath = g_strdup(newpath);
+       else
+               new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
+                                          NULL);
+       g_free(item->path);
+       item->path = new_itempath;
+
+       return FALSE;
+}