2006-02-06 [colin] 2.0.0cvs21
[claws.git] / src / imap.c
index 92ba6f0c7402d937f0fddf03ca5ccf92396e1331..f637763bcca145bee600dd315ccebe68803476e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -932,7 +932,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                make_dir_hier(path);
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        g_free(path);
-
+       debug_print("trying to fetch cached %s\n", filename);
        if (is_file_exist(filename)) {
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
@@ -1069,6 +1069,11 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                        unlock_session();
                                        return -1;
                                }
+                       }  else if (!(MSG_IS_QUEUED(*fileinfo->flags) 
+                                     || MSG_IS_DRAFT(*fileinfo->flags))
+                                   && (folder_has_parent_of_type(dest, F_QUEUE)
+                                   || folder_has_parent_of_type(dest, F_DRAFT))) {
+                               return -1;
                        } 
                }
                if (real_file == NULL)
@@ -1093,20 +1098,44 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        statusbar_progress_all(0,0,0);
                        statusbar_pop_all();
                        return -1;
+               } else {
+                       debug_print("appended new message as %d\n", new_uid);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (new_uid > 0) {
+                               gchar *cache_path = folder_item_get_path(dest);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_dir_exist(cache_path)) {
+                                       gchar *cache_file = g_strconcat(
+                                               cache_path, G_DIR_SEPARATOR_S, 
+                                               itos(new_uid), NULL);
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                                       g_free(cache_file);
+                               }
+                               g_free(cache_path);
+                       }
                }
 
                if (relation != NULL)
                        g_relation_insert(relation, fileinfo->msginfo != NULL ? 
                                          (gpointer) fileinfo->msginfo : (gpointer) fileinfo,
                                          GINT_TO_POINTER(dest->last_num + 1));
+               if (new_uid == 0) {
+                       new_uid = dest->last_num+1;
+               }
                if (last_uid < new_uid)
                        last_uid = new_uid;
                if (file_is_tmp)
                        g_unlink(real_file);
+
+               g_free(real_file);
        }
        statusbar_progress_all(0,0,0);
        statusbar_pop_all();
        
+       imap_cmd_expunge(session);
        unlock_session();
        
        g_free(destdir);
@@ -1237,18 +1266,28 @@ static gint imap_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        g_return_val_if_fail(msginfo->folder != NULL, -1);
 
+       /* if from/to are the same "type" (with or without extra headers),
+        * copy them via imap */
        if (folder == msginfo->folder->folder &&
            !folder_has_parent_of_type(msginfo->folder, F_DRAFT) &&
-           !folder_has_parent_of_type(msginfo->folder, F_QUEUE)) {
+           !folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
+           !folder_has_parent_of_type(dest, F_DRAFT) &&
+           !folder_has_parent_of_type(dest, F_QUEUE)) {
+               ret = imap_do_copy_msgs(folder, dest, msglist, relation);
+               return ret;
+       } else if (folder == msginfo->folder->folder &&
+           (folder_has_parent_of_type(msginfo->folder, F_DRAFT) ||
+            folder_has_parent_of_type(msginfo->folder, F_QUEUE)) && 
+           (folder_has_parent_of_type(dest, F_DRAFT) ||
+            folder_has_parent_of_type(dest, F_QUEUE))) {
                ret = imap_do_copy_msgs(folder, dest, msglist, relation);
                return ret;
        }
-
+       /* else reupload them */
        file_list = procmsg_get_message_file_list(msglist);
        g_return_val_if_fail(file_list != NULL, -1);
 
        ret = imap_add_msgs(folder, dest, file_list, relation);
-
        procmsg_message_file_list_free(file_list);
 
        return ret;
@@ -1828,17 +1867,19 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        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;
+       }
+       lock_session();
+
        if (strchr(name, imap_get_path_separator(IMAP_FOLDER(folder), item->path)) != NULL) {
                g_warning(_("New folder name must not contain the namespace "
                            "path separator"));
+               unlock_session();
                return -1;
        }
 
-       session = imap_session_get(folder);
-       if (!session) {
-               return -1;
-       }
-       lock_session();
        real_oldpath = imap_get_real_path(IMAP_FOLDER(folder), item->path);
 
        g_free(session->mbox);
@@ -1906,8 +1947,6 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        IMAPSession *session;
        gchar *path;
        gchar *cache_dir;
-       gint exists, recent, unseen;
-       guint32 uid_validity;
 
        g_return_val_if_fail(folder != NULL, -1);
        g_return_val_if_fail(item != NULL, -1);
@@ -1920,14 +1959,6 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        lock_session();
        path = imap_get_real_path(IMAP_FOLDER(folder), item->path);
 
-       ok = imap_cmd_examine(session, "INBOX",
-                             &exists, &recent, &unseen, &uid_validity, FALSE);
-       if (ok != IMAP_SUCCESS) {
-               g_free(path);
-               unlock_session();
-               return -1;
-       }
-
        ok = imap_cmd_delete(session, path);
        if (ok != IMAP_SUCCESS) {
                gchar *tmp = g_strdup_printf("%s%c", path, 
@@ -2027,6 +2058,8 @@ static void *imap_get_uncached_messages_thread(void *data)
                        
                        info = carray_get(env_list, i);
                        msginfo = imap_envelope_from_lep(info, item);
+                       if (msginfo == NULL)
+                               continue;
                        msginfo->folder = item;
                        if (!newlist)
                                llast = newlist = g_slist_append(newlist, msginfo);
@@ -2040,6 +2073,13 @@ static void *imap_get_uncached_messages_thread(void *data)
                imap_fetch_env_free(env_list);
        }
        
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               struct mailimap_set * imapset;
+               
+               imapset = cur->data;
+               mailimap_set_free(imapset);
+       }
+       
        session_set_access_time(SESSION(session));
        stuff->done = TRUE;
        return newlist;
@@ -2318,45 +2358,6 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        
        real_path = imap_get_real_path(folder, path);
 
-#if 0
-       if (time(NULL) - item->last_update >= 5 && item->last_update != 1) {
-               /* do the full stuff */
-               item->last_update = 1; /* force update */
-               debug_print("updating everything\n");
-               r = imap_status(session, folder, path, item,
-               &item->c_messages, &item->c_uid_next,
-               &item->c_uid_validity, &item->c_unseen, block);
-               if (r != MAILIMAP_NO_ERROR) {
-                       debug_print("status err %d\n", r);
-                       return IMAP_ERROR;
-               }
-               item->last_update = time(NULL);
-               if (messages) 
-                       *messages = item->c_messages;
-               if (uid_next)
-                       *uid_next = item->c_uid_next;
-               if (uid_validity)
-                       *uid_validity = item->c_uid_validity;
-               if (unseen)
-                       *unseen = item->c_unseen;
-               return 0;
-       } else if (time(NULL) - item->last_update < 5) {
-               /* return cached stuff */
-               debug_print("using cache\n");
-               if (messages) 
-                       *messages = item->c_messages;
-               if (uid_next)
-                       *uid_next = item->c_uid_next;
-               if (uid_validity)
-                       *uid_validity = item->c_uid_validity;
-               if (unseen)
-                       *unseen = item->c_unseen;
-               return 0;
-       }
-#endif
-
-       /* if we get there, we're updating cache */
-
        if (messages) {
                mask |= 1 << 0;
        }
@@ -2675,6 +2676,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);
+       mailimap_flag_list_free(flag_list);
        if (new_uid != NULL)
                *new_uid = 0;
 
@@ -2720,6 +2722,7 @@ static gint imap_cmd_store(IMAPSession *session, struct mailimap_set * set,
                        mailimap_store_att_flags_new_remove_flags_silent(flag_list);
        
        r = imap_threaded_store(session->folder, set, store_att_flags);
+       mailimap_store_att_flags_free(store_att_flags);
        if (r != MAILIMAP_NO_ERROR) {
                
                return IMAP_ERROR;
@@ -3010,11 +3013,15 @@ static void *get_list_of_uids_thread(void *data)
 
        r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_SIMPLE, set,
                                 &lep_uidlist);
+       mailimap_set_free(set);
+       
        if (r == MAILIMAP_NO_ERROR) {
                GSList * fetchuid_list;
                
                fetchuid_list =
                        imap_uid_list_from_lep(lep_uidlist);
+               mailimap_search_result_free(lep_uidlist);
+               
                uidlist = g_slist_concat(fetchuid_list, uidlist);
        }
        else {
@@ -3026,6 +3033,7 @@ static void *get_list_of_uids_thread(void *data)
                if (r == MAILIMAP_NO_ERROR) {
                        fetchuid_list =
                                imap_uid_list_from_lep_tab(lep_uidtab);
+                       imap_fetch_uid_list_free(lep_uidtab);
                        uidlist = g_slist_concat(fetchuid_list, uidlist);
                }
        }
@@ -3098,7 +3106,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
 
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
@@ -3123,6 +3131,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                        ok = imap_status(session, IMAP_FOLDER(folder), item->item.path, item,
                                 &exists, &uid_next, &uid_val, NULL, FALSE);
                }
+               item->item.last_num = uid_next - 1;
+               
                item->use_cache = (time_t)0;
                if (ok != IMAP_SUCCESS) {
                        statusbar_pop_all();
@@ -3145,13 +3155,11 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                }
        }
 
-       if (!selected_folder)
-               item->uid_next = uid_next;
-
        /* If old uid_next matches new uid_next we can be sure no message
           was added to the folder */
-       if (( selected_folder && !session->folder_content_changed) ||
-           (!selected_folder && uid_next == item->uid_next)) {
+       debug_print("uid_next is %d and item->uid_next %d \n", 
+               uid_next, item->uid_next);
+       if (uid_next == item->uid_next) {
                nummsgs = g_slist_length(item->uid_list);
 
                /* If number of messages is still the same we
@@ -3160,6 +3168,7 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
                   we discard our cache to start a new scan to find
                   out which numbers have been removed */
                if (exists == nummsgs) {
+                       debug_print("exists == nummsgs\n");
                        *msgnum_list = g_slist_copy(item->uid_list);
                        statusbar_pop_all();
                        unlock_session();
@@ -3209,6 +3218,8 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        remove_numbered_files_not_in_list(dir, *msgnum_list);
        g_free(dir);
        
+       item->uid_next = uid_next;
+       
        debug_print("get_num_list - ok - %i\n", nummsgs);
        statusbar_pop_all();
        unlock_session();
@@ -3352,7 +3363,7 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
        lock_session();
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
-       if (selected_folder) {
+       if (selected_folder && time(NULL) - item->use_cache < 2) {
                ok = imap_cmd_noop(session);
                if (ok != IMAP_SUCCESS) {
                        debug_print("disconnected!\n");
@@ -3380,7 +3391,9 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                item->c_uid_next = uid_next;
                item->c_uid_validity = uid_val;
                item->c_unseen = unseen;
-
+               item->item.last_num = uid_next - 1;
+               debug_print("uidnext %d, item->uid_next %d, exists %d, item->item.total_msgs %d\n", 
+                       uid_next, item->uid_next, exists, item->item.total_msgs);
                if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)) {
                        unlock_session();
                        return TRUE;
@@ -3834,12 +3847,12 @@ static void process_hashtable(IMAPFolderItem *item)
 {
        if (item->flags_set_table) {
                g_hash_table_foreach_remove(item->flags_set_table, process_flags, GINT_TO_POINTER(TRUE));
-               g_free(item->flags_set_table);
+               g_hash_table_destroy(item->flags_set_table);
                item->flags_set_table = NULL;
        }
        if (item->flags_unset_table) {
                g_hash_table_foreach_remove(item->flags_unset_table, process_flags, GINT_TO_POINTER(FALSE));
-               g_free(item->flags_unset_table);
+               g_hash_table_destroy(item->flags_unset_table);
                item->flags_unset_table = NULL;
        }
 }
@@ -4117,6 +4130,9 @@ static MsgInfo *imap_envelope_from_lep(struct imap_fetch_env_info * info,
        size_t size = 0;
        MsgFlags flags = {0, 0};
        
+       if (info->headers == NULL)
+               return NULL;
+
        MSG_SET_TMP_FLAGS(flags, MSG_IMAP);
        if (folder_has_parent_of_type(item, F_QUEUE)) {
                MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);