2005-07-15 [hoa] 1.9.12cvs59
[claws.git] / src / etpan / imap-thread.c
index 45ae45e00f3fb500ef68c980b2b9ba9b7153676f..7d0a3a7de6197523b1cf54c29d25c863beb68ed5 100644 (file)
@@ -1,5 +1,11 @@
-#include "imap-thread.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
 
+#ifdef HAVE_LIBETPAN
+
+#include "imap-thread.h"
+#include <imap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -8,8 +14,12 @@
 
 #include <gtk/gtk.h>
 #include "etpan-thread-manager.h"
+#include "utils.h"
+
+#define DISABLE_LOG_DURING_LOGIN
 
 static struct etpan_thread_manager * thread_manager = NULL;
+static chash * courier_workaround_hash = NULL;
 static chash * imap_hash = NULL;
 static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
@@ -34,12 +44,13 @@ void imap_main_init(void)
        
        mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
        mailstream_network_delay.tv_usec = 0;
-
-#if 0
-       mailstream_debug = 1;
-#endif
+       
+       if (debug_get_mode())
+               mailstream_debug = 1;
+       
        imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
+       courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        
        thread_manager = etpan_thread_manager_new();
        
@@ -61,13 +72,15 @@ void imap_main_set_timeout(int sec)
 
 void imap_main_done(void)
 {
+       etpan_thread_manager_stop(thread_manager);
+       etpan_thread_manager_join(thread_manager);
+       
        g_source_remove(thread_manager_signal);
        g_io_channel_unref(io_channel);
        
-       etpan_thread_manager_stop(thread_manager);
-       etpan_thread_manager_join(thread_manager);
        etpan_thread_manager_free(thread_manager);
        
+       chash_free(courier_workaround_hash);
        chash_free(session_hash);
        chash_free(imap_hash);
 }
@@ -104,9 +117,11 @@ void imap_done(Folder * folder)
        
        thread = value.data;
        
-       etpan_thread_stop(thread);
+       etpan_thread_unbind(thread);
        
        chash_delete(imap_hash, &key, NULL);
+       
+       debug_print("remove thread");
 }
 
 static struct etpan_thread * get_thread(Folder * folder)
@@ -149,6 +164,8 @@ static void generic_cb(int cancelled, void * result, void * callback_data)
        int * p_finished;
        
        p_finished = callback_data;
+
+       debug_print("generic_cb\n");
        
        * p_finished = 1;
 }
@@ -160,6 +177,8 @@ static void threaded_run(Folder * folder, void * param, void * result,
        struct etpan_thread * thread;
        int finished;
        
+       imap_folder_ref(folder);
+
        op = etpan_thread_op_new();
        op->param = param;
        op->result = result;
@@ -180,6 +199,8 @@ static void threaded_run(Folder * folder, void * param, void * result,
        }
        
        etpan_thread_op_free(op);
+
+       imap_folder_unref(folder);
 }
 
 
@@ -327,6 +348,10 @@ void imap_threaded_disconnect(Folder * folder)
        value.len = 0;
        chash_delete(session_hash, &key, NULL);
        
+       key.data = &imap;
+       key.len = sizeof(imap);
+       chash_delete(courier_workaround_hash, &key, NULL);
+       
        mailimap_free(imap);
        
        debug_print("disconnect ok\n");
@@ -401,11 +426,23 @@ static void login_run(struct etpan_thread_op * op)
        struct login_param * param;
        struct login_result * result;
        int r;
+#ifdef DISABLE_LOG_DURING_LOGIN
+       int old_debug;
+#endif
        
        param = op->param;
+       
+#ifdef DISABLE_LOG_DURING_LOGIN
+       old_debug = mailstream_debug;
+#endif
+       
        r = mailimap_login(param->imap,
                           param->login, param->password);
        
+#ifdef DISABLE_LOG_DURING_LOGIN
+       mailstream_debug = old_debug;
+#endif
+       
        result = op->result;
        result->error = r;
        debug_print("imap login run - end %i\n", r);
@@ -1099,6 +1136,7 @@ static void fetch_uid_run(struct etpan_thread_op * op)
        
        param = op->param;
        
+       fetch_result = NULL;
        r = imap_get_messages_list(param->imap, param->first_index,
                                   &fetch_result);
        
@@ -1372,6 +1410,8 @@ static void fetch_content_run(struct etpan_thread_op * op)
        
        param = op->param;
        
+       content = NULL;
+       content_size = 0;
        if (param->with_body)
                r = imap_fetch(param->imap, param->msg_index,
                               &content, &content_size);
@@ -1638,6 +1678,18 @@ int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
        return MAIL_NO_ERROR;
 }
 
+int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
+{
+       struct mailimap_fetch_att * fetch_att;
+       struct mailimap_section * section;
+       
+       section = mailimap_section_new_header();
+       fetch_att = mailimap_fetch_att_new_body_peek_section(section);
+       mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
+       
+       return MAIL_NO_ERROR;
+}
+
 static int
 imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                        carray ** p_env_list)
@@ -1648,6 +1700,8 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        clist * fetch_result;
        int r;
        carray * env_list;
+       chashdatum key;
+       chashdatum value;
        
        fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
   
@@ -1664,7 +1718,13 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
   
        /* headers */
-       r = imap_add_envelope_fetch_att(fetch_type);
+       key.data = &imap;
+       key.len = sizeof(imap);
+       r = chash_get(courier_workaround_hash, &key, &value);
+       if (r < 0)
+               r = imap_add_envelope_fetch_att(fetch_type);
+       else
+               r = imap_add_header_fetch_att(fetch_type);
        
        r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
        
@@ -1719,6 +1779,7 @@ static void fetch_env_run(struct etpan_thread_op * op)
        
        param = op->param;
        
+       env_list = NULL;
        r = imap_get_envelopes_list(param->imap, param->set,
                                    &env_list);
        
@@ -1744,6 +1805,23 @@ int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
        
        threaded_run(folder, &param, &result, fetch_env_run);
        
+       if (result.error != MAILIMAP_NO_ERROR) {
+               chashdatum key;
+               chashdatum value;
+               int r;
+               
+               key.data = &imap;
+               key.len = sizeof(imap);
+               r = chash_get(courier_workaround_hash, &key, &value);
+               if (r < 0) {
+                       value.data = NULL;
+                       value.len = 0;
+                       chash_set(courier_workaround_hash, &key, &value, NULL);
+                       
+                       threaded_run(folder, &param, &result, fetch_env_run);
+               }
+       }
+       
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
@@ -2167,3 +2245,16 @@ int imap_threaded_connect_cmd(Folder * folder, const char * command,
        
        return result.error;
 }
+#else
+
+void imap_main_init(void)
+{
+}
+void imap_main_done(void)
+{
+}
+void imap_main_set_timeout(int sec)
+{
+}
+
+#endif