2005-11-12 [colin] 1.9.100cvs9
[claws.git] / src / etpan / imap-thread.c
index 4d76e07d1c82a25c8a8ef5a866f53b225ac3c91d..d89d83d876a75418b4de95054ce47fb7ac96ab4f 100644 (file)
@@ -1,15 +1,29 @@
-#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>
+#if (defined (__NetBSD__) || defined (__FreeBSD__))
+#include <sys/socket.h>
+#endif
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/wait.h>
 
 #include <gtk/gtk.h>
+#include <log.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;
@@ -25,6 +39,27 @@ static gboolean thread_manager_event(GIOChannel * source,
        return TRUE;
 }
 
+void imap_logger(int direction, const char * str, size_t size) 
+{
+       gchar buf[512];
+
+       memset(buf, 0, 512);
+       strncpy(buf, str, size > 510 ? 510:size);
+       buf[511] = '\0';
+       if (size < 511)
+               buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7) 
+       ||  buf[0] == '\r' ||  buf[0] == '\n')
+               return;
+
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strstr(buf, "\n"))
+               *strstr(buf, "\n") = ' ';
+
+       log_print("IMAP4%c %s\n", direction?'>':'<', buf);
+}
 
 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
 
@@ -34,12 +69,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
+       mailstream_logger = imap_logger;
+
        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 +97,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 +142,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 +189,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 +202,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 +224,8 @@ static void threaded_run(Folder * folder, void * param, void * result,
        }
        
        etpan_thread_op_free(op);
+
+       imap_folder_unref(folder);
 }
 
 
@@ -276,10 +322,54 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        
        threaded_run(folder, &param, &result, connect_ssl_run);
        
-       debug_print("connect ok\n");
+       debug_print("connect %d\n", result.error);
        
        return result.error;
 }
+
+struct capa_param {
+       mailimap * imap;
+};
+
+struct capa_result {
+       int error;
+       struct mailimap_capability_data *caps;
+};
+
+static void capability_run(struct etpan_thread_op * op)
+{
+       int r;
+       struct capa_param * param;
+       struct capa_result * result;
+       struct mailimap_capability_data *caps;
+
+       param = op->param;
+       result = op->result;
+       
+       r = mailimap_capability(param->imap, &caps);
+       
+       result->error = r;
+       result->caps = (r == 0 ? caps : NULL);
+}
+
+
+struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
+{
+       struct capa_param param;
+       struct capa_result result;
+       mailimap *imap;
+       
+       imap = get_imap(folder);
+       
+       param.imap = imap;
+       
+       threaded_run(folder, &param, &result, capability_run);
+       
+       debug_print("capa ok\n");
+       
+       return result.caps;
+       
+}
        
 struct disconnect_param {
        mailimap * imap;
@@ -327,6 +417,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");
@@ -390,6 +484,7 @@ struct login_param {
        mailimap * imap;
        const char * login;
        const char * password;
+       const char * type;
 };
 
 struct login_result {
@@ -401,10 +496,27 @@ 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;
-       r = mailimap_login(param->imap,
+       
+#ifdef DISABLE_LOG_DURING_LOGIN
+       old_debug = mailstream_debug;
+       mailstream_debug = 0;
+#endif
+       if (!strcmp(param->type, "LOGIN"))
+               r = mailimap_login(param->imap,
                           param->login, param->password);
+       else 
+               r = mailimap_authenticate(param->imap,
+                       param->type, NULL, NULL, NULL,
+                       param->login, param->login,
+                       param->password, NULL);
+#ifdef DISABLE_LOG_DURING_LOGIN
+       mailstream_debug = old_debug;
+#endif
        
        result = op->result;
        result->error = r;
@@ -412,7 +524,8 @@ static void login_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_login(Folder * folder,
-                       const char * login, const char * password)
+                       const char * login, const char * password,
+                       const char * type)
 {
        struct login_param param;
        struct login_result result;
@@ -422,7 +535,8 @@ int imap_threaded_login(Folder * folder,
        param.imap = get_imap(folder);
        param.login = login;
        param.password = password;
-       
+       param.type = type;
+
        threaded_run(folder, &param, &result, login_run);
        
        debug_print("imap login - end\n");
@@ -460,7 +574,8 @@ static void status_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_status(Folder * folder, const char * mb,
-                        struct mailimap_mailbox_data_status ** data_status)
+                        struct mailimap_mailbox_data_status ** data_status,
+                        guint mask)
 {
        struct status_param param;
        struct status_result result;
@@ -469,17 +584,26 @@ int imap_threaded_status(Folder * folder, const char * mb,
        debug_print("imap status - begin\n");
        
        status_att_list = mailimap_status_att_list_new_empty();
-       mailimap_status_att_list_add(status_att_list,
+       if (mask & 1 << 0) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_MESSAGES);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 1) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_RECENT);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 2) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UIDNEXT);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 3) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UIDVALIDITY);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 4) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UNSEEN);
-       
+       }
        param.imap = get_imap(folder);
        param.mb = mb;
        param.status_att_list = status_att_list;
@@ -517,17 +641,26 @@ static void noop_run(struct etpan_thread_op * op)
        debug_print("imap noop run - end %i\n", r);
 }
 
-int imap_threaded_noop(Folder * folder)
+int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
 {
        struct noop_param param;
        struct noop_result result;
+       mailimap * imap;
        
        debug_print("imap noop - begin\n");
        
-       param.imap = get_imap(folder);
+       imap = get_imap(folder);
+       param.imap = imap;
        
        threaded_run(folder, &param, &result, noop_run);
        
+       if (imap->imap_selection_info != NULL) {
+               * p_exists = imap->imap_selection_info->sel_exists;
+       }
+       else {
+               * p_exists = 0;
+       }
+       
        debug_print("imap noop - end\n");
        
        return result.error;
@@ -554,6 +687,29 @@ static void starttls_run(struct etpan_thread_op * op)
        result = op->result;
        result->error = r;
        debug_print("imap starttls run - end %i\n", r);
+       
+       if (r == 0) {
+               mailimap *imap = param->imap;
+               mailstream_low *plain_low = NULL;
+               mailstream_low *tls_low = NULL;
+               int fd = -1;
+               
+               plain_low = mailstream_get_low(imap->imap_stream);
+               fd = mailstream_low_get_fd(plain_low);
+               if (fd == -1) {
+                       debug_print("imap starttls run - can't get fd\n");
+                       result->error = MAILIMAP_ERROR_STREAM;
+                       return;
+               }
+               tls_low = mailstream_low_ssl_open(fd);
+               if (tls_low == NULL) {
+                       debug_print("imap starttls run - can't ssl_open\n");
+                       result->error = MAILIMAP_ERROR_STREAM;
+                       return;
+               }
+               mailstream_low_free(plain_low);
+               mailstream_set_low(imap->imap_stream, tls_low);
+       }
 }
 
 int imap_threaded_starttls(Folder * folder)
@@ -886,6 +1042,14 @@ static void search_run(struct etpan_thread_op * op)
                                                          NULL, NULL, NULL, NULL, NULL,
                                                          NULL, 0, NULL, NULL, NULL);
                break;
+       case IMAP_SEARCH_TYPE_DELETED:
+               search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, NULL, NULL, NULL, 0,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         NULL, 0, NULL, NULL, NULL);
+               break;
        }
        
        if (search_type_key != NULL) {
@@ -913,7 +1077,7 @@ int imap_threaded_search(Folder * folder, int search_type,
        mailimap * imap;
        
        debug_print("imap search - begin\n");
-       
+
        imap = get_imap(folder);
        param.imap = imap;
        param.set = set;
@@ -1090,6 +1254,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);
        
@@ -1363,6 +1528,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);
@@ -1393,7 +1560,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
                
                r = fclose(f);
                if (r == EOF) {
-                       unlink(param->filename);
+                       g_unlink(param->filename);
                        goto close;
                }
                goto free;
@@ -1404,7 +1571,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
        close:
                close(fd);
        unlink:
-               unlink(param->filename);
+               g_unlink(param->filename);
        
        free:
                if (mmap_string_unref(content) != 0)
@@ -1556,6 +1723,8 @@ fetch_to_env_info(struct mailimap_msg_att * msg_att)
        imap_get_msg_att_info(msg_att, &uid, &headers, &size,
                              &att_dyn);
        
+       if (!headers)
+               return NULL;
        info = malloc(sizeof(* info));
        info->uid = uid;
        info->headers = strdup(headers);
@@ -1572,7 +1741,6 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
        clistiter * cur;
        unsigned int i;
        carray * env_list;
-  
        i = 0;
   
        env_list = carray_new(16);
@@ -1585,6 +1753,8 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
                msg_att = clist_content(cur);
 
                env_info = fetch_to_env_info(msg_att);
+               if (!env_info)
+                       return MAILIMAP_ERROR_MEMORY;
                carray_add(env_list, env_info, NULL);
        }
   
@@ -1629,6 +1799,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)
@@ -1638,7 +1820,9 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
        int res;
        clist * fetch_result;
        int r;
-       carray * env_list;
+       carray * env_list = NULL;
+       chashdatum key;
+       chashdatum value;
        
        fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
   
@@ -1655,7 +1839,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);
        
@@ -1710,6 +1900,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);
        
@@ -1735,6 +1926,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;
        
@@ -1991,33 +2199,54 @@ int imap_threaded_store(Folder * folder, struct mailimap_set * set,
 }
 
 
+#define ENV_BUFFER_SIZE 512
 
 static void do_exec_command(int fd, const char * command,
                            const char * servername, uint16_t port)
 {
        int i, maxopen;
-  
+#ifdef SOLARIS
+       char env_buffer[ENV_BUFFER_SIZE];
+#endif
+       
        if (fork() > 0) {
                /* Fork again to become a child of init rather than
                   the etpan client. */
                exit(0);
        }
   
+#ifdef SOLARIS
+       if (servername)
+               snprintf(env_buffer, ENV_BUFFER_SIZE,
+                        "ETPANSERVER=%s", servername);
+       else
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
+       putenv(env_buffer);
+#else
        if (servername)
                setenv("ETPANSERVER", servername, 1);
        else
                unsetenv("ETPANSERVER");
+#endif
   
+#ifdef SOLARIS
+       if (port)
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
+       else
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
+       putenv(env_buffer);
+#else
        if (port) {
                char porttext[20];
-    
+               
                snprintf(porttext, sizeof(porttext), "%d", port);
                setenv("ETPANPORT", porttext, 1);
        }
        else {
                unsetenv("ETPANPORT");
        }
-  
+#endif
+               
        /* Not a lot we can do if there's an error other than bail. */
        if (dup2(fd, 0) == -1)
                exit(1);
@@ -2158,3 +2387,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