2006-11-02 [colin] 2.5.6cvs19
[claws.git] / src / etpan / imap-thread.c
index 0e0fc186ce7faffd92d4b4802b143b4a6164d933..99c19197a35e82acefe42faa9cf451a7515cefac 100644 (file)
@@ -8,7 +8,7 @@
 #include <imap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__CYGWIN__))
+#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
 #include <sys/socket.h>
 #endif
 #include <fcntl.h>
@@ -20,6 +20,7 @@
 #include "etpan-thread-manager.h"
 #include "utils.h"
 #include "ssl_certificate.h"
+#include "socket.h"
 
 #define DISABLE_LOG_DURING_LOGIN
 
@@ -40,39 +41,156 @@ static gboolean thread_manager_event(GIOChannel * source,
        return TRUE;
 }
 
-void imap_logger(int direction, const char * str, size_t size) 
+static void imap_logger_cmd(int direction, const char * str, size_t size) 
 {
-       gchar buf[512];
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
 
-       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')
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
                return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
 
+       while (lines[i] && *lines[i]) {
+               log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               i++;
+       }
+       g_strfreev(lines);
+       free(buf);
+}
+
+static void imap_logger_fetch(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
        while (strstr(buf, "\r"))
                *strstr(buf, "\r") = ' ';
-       while (strstr(buf, "\n"))
-               *strstr(buf, "\n") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
 
-       log_print("IMAP4%c %s\n", direction?'>':'<', buf);
+       if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
+               while (lines[i] && *lines[i]) {
+                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       i++;
+               }
+       } else {
+               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+       }
+       g_strfreev(lines);
+       free(buf);
+}
+
+static void imap_logger_uid(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
+
+       while (lines[i] && *lines[i]) {
+               int llen = strlen(lines[i]);
+               if (llen < 64)
+                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               else {
+                       gchar tmp[64];
+                       strncpy2(tmp, lines[i], 63);
+                       log_print("IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
+                                 llen-64);
+               }
+               i++;
+       }
+       g_strfreev(lines);
+       free(buf);
+}
+
+void imap_logger_append(int direction, const char * str, size_t size) 
+{
+       gchar *buf;
+       gchar **lines;
+       int i = 0;
+
+       buf = malloc(size+1);
+       memset(buf, 0, size+1);
+       strncpy(buf, str, size);
+       buf[size] = '\0';
+       if (!strncmp(buf, "<<<<<<<", 7) 
+       ||  !strncmp(buf, ">>>>>>>", 7)) {
+               free(buf);
+               return;
+       }
+       while (strstr(buf, "\r"))
+               *strstr(buf, "\r") = ' ';
+       while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
+               buf[strlen(buf)-1] = '\0';
+
+       lines = g_strsplit(buf, "\n", -1);
+
+       if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
+               while (lines[i] && *lines[i]) {
+                       log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       i++;
+               }
+       } else {
+               log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+       }
+       g_strfreev(lines);
+       free(buf);
 }
 
 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
+static gboolean etpan_skip_ssl_cert_check = FALSE;
 
-void imap_main_init(void)
+void imap_main_init(gboolean skip_ssl_cert_check)
 {
        int fd_thread_manager;
        
+       etpan_skip_ssl_cert_check = skip_ssl_cert_check;
        mailstream_network_delay.tv_sec = ETPAN_DEFAULT_NETWORK_TIMEOUT;
        mailstream_network_delay.tv_usec = 0;
        
        mailstream_debug = 1;
-       mailstream_logger = imap_logger;
+       mailstream_logger = imap_logger_cmd;
 
        imap_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
@@ -278,6 +396,7 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        param.server = server;
        param.port = port;
        
+       refresh_resolvers();
        threaded_run(folder, &param, &result, connect_run);
        
        debug_print("connect ok %i\n", result.error);
@@ -285,7 +404,7 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
        return result.error;
 }
 
-static int etpan_certificate_check(unsigned char *certificate, int len, void *data)
+static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
 {
 #ifdef USE_OPENSSL
        struct connect_param *param = (struct connect_param *)data;
@@ -295,14 +414,16 @@ static int etpan_certificate_check(unsigned char *certificate, int len, void *da
                g_warning("no cert presented.\n");
                return 0;
        }
-       cert = d2i_X509(NULL, &certificate, len);
+       cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
        if (cert == NULL) {
                g_warning("can't get cert\n");
                return 0;
-       } else if (ssl_certificate_check(cert, 
-               (gchar *)param->server, param->port) == TRUE) {
+       } else if (ssl_certificate_check(cert, NULL,
+               (gchar *)param->server, (gushort)param->port) == TRUE) {
+               X509_free(cert);
                return 0;
        } else {
+               X509_free(cert);
                return -1;
        }
 #else
@@ -346,9 +467,10 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        param.server = server;
        param.port = port;
        
+       refresh_resolvers();
        threaded_run(folder, &param, &result, connect_ssl_run);
        
-       if (result.error >= 0) {
+       if (result.error >= 0 && !etpan_skip_ssl_cert_check) {
                cert_len = mailstream_ssl_get_certificate(imap->imap_stream, &certificate);
                if (etpan_certificate_check(certificate, cert_len, &param) < 0)
                        return -1;
@@ -386,7 +508,7 @@ static void capability_run(struct etpan_thread_op * op)
 }
 
 
-struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
+struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
 {
        struct capa_param param;
        struct capa_result result;
@@ -398,8 +520,11 @@ struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
        
        threaded_run(folder, &param, &result, capability_run);
        
-       debug_print("capa ok\n");
+       debug_print("capa %d\n", result.error);
        
+       if (ok)
+               *ok = result.error;
+
        return result.caps;
        
 }
@@ -518,6 +643,7 @@ struct login_param {
        const char * login;
        const char * password;
        const char * type;
+       const char * server;
 };
 
 struct login_result {
@@ -542,6 +668,11 @@ static void login_run(struct etpan_thread_op * op)
        if (!strcmp(param->type, "LOGIN"))
                r = mailimap_login(param->imap,
                           param->login, param->password);
+       else if (!strcmp(param->type, "GSSAPI"))
+               r = mailimap_authenticate(param->imap,
+                       param->type, param->server, NULL, NULL,
+                       param->login, param->login,
+                       param->password, NULL);
        else 
                r = mailimap_authenticate(param->imap,
                        param->type, NULL, NULL, NULL,
@@ -569,6 +700,10 @@ int imap_threaded_login(Folder * folder,
        param.login = login;
        param.password = password;
        param.type = type;
+       if (folder && folder->account)
+               param.server = folder->account->recv_server;
+       else
+               param.server = NULL;
 
        threaded_run(folder, &param, &result, login_run);
        
@@ -761,7 +896,7 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        
        debug_print("imap starttls - end\n");
        
-       if (result.error == 0) {
+       if (result.error == 0 && !etpan_skip_ssl_cert_check) {
                cert_len = mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
                if (etpan_certificate_check(certificate, cert_len, &param) < 0)
                        result.error = MAILIMAP_ERROR_STREAM;
@@ -1030,20 +1165,44 @@ struct search_result {
        clist * search_result;
 };
 
+static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_item *orig)
+{
+       return mailimap_set_item_new(orig->set_first, orig->set_last);
+}
+
+static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
+{
+       clist *list = orig ? orig->set_list : NULL;
+       clist *newlist = clist_new();
+       clistiter *cur;
+       
+       if (!orig)
+               return NULL;
+       for (cur = clist_begin(list); cur; cur = clist_next(cur))
+               clist_append(newlist, 
+                       sc_mailimap_set_item_copy(
+                       (struct mailimap_set_item *)clist_content(cur)));
+       return mailimap_set_new(newlist);
+}
+
 static void search_run(struct etpan_thread_op * op)
 {
        struct search_param * param;
        struct search_result * result;
        int r;
-       struct mailimap_search_key * key;
-       struct mailimap_search_key * uid_key;
+       struct mailimap_search_key * key = NULL;
+       struct mailimap_search_key * uid_key = NULL;
        struct mailimap_search_key * search_type_key;
        clist * search_result;
        
        param = op->param;
        
-       uid_key = mailimap_search_key_new_uid(param->set);
-       
+       /* we copy the mailimap_set because freeing the key is recursive */
+       if (param->set != NULL) {
+               uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
+       } else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
+               uid_key = mailimap_search_key_new_all();
+       }
        search_type_key = NULL;
        switch (param->type) {
        case IMAP_SEARCH_TYPE_SIMPLE:
@@ -1096,20 +1255,37 @@ static void search_run(struct etpan_thread_op * op)
        }
        
        if (search_type_key != NULL) {
-               key = mailimap_search_key_new_multiple_empty();
-               mailimap_search_key_multiple_add(key, search_type_key);
-               mailimap_search_key_multiple_add(key, uid_key);
-       }
-       else {
+               if (param->set != NULL) {
+                       key = mailimap_search_key_new_multiple_empty();
+                       mailimap_search_key_multiple_add(key, search_type_key);
+                       mailimap_search_key_multiple_add(key, uid_key);
+               } else {
+                       key = search_type_key;
+               }
+       } else if (uid_key != NULL) {
                key = uid_key;
        }
        
-       r = mailimap_uid_search(param->imap, NULL, key, &search_result);
-       
-       result = op->result;
-       result->error = r;
-       result->search_result = search_result;
-       debug_print("imap search run - end %i\n", r);
+       if (key == NULL) {
+               g_warning("no key!");
+               result = op->result;
+               result->error = -1;
+               result->search_result = NULL;
+       } else {
+               mailstream_logger = imap_logger_uid;
+
+               r = mailimap_uid_search(param->imap, NULL, key, &search_result);
+
+               mailstream_logger = imap_logger_cmd;
+
+               /* free the key (with the imapset) */
+               mailimap_search_key_free(key);
+
+               result = op->result;
+               result->error = r;
+               result->search_result = search_result;
+       }
+       debug_print("imap search run - end %i\n", result->error);
 }
 
 int imap_threaded_search(Folder * folder, int search_type,
@@ -1391,9 +1567,13 @@ static int imap_fetch(mailimap * imap,
                goto free_fetch_att;
        }
 
+       mailstream_logger = imap_logger_fetch;
+       
        r = mailimap_uid_fetch(imap, set,
                               fetch_type, &fetch_result);
   
+       mailstream_logger = imap_logger_cmd;
+       
        mailimap_fetch_type_free(fetch_type);
        mailimap_set_free(set);
   
@@ -2037,7 +2217,8 @@ static void append_run(struct etpan_thread_op * op)
        char * data;
        size_t size;
        struct stat stat_buf;
-       int fd, uid = 0;
+       int fd;
+       guint32 uid = 0, val = 0;
        
        param = op->param;
        result = op->result;
@@ -2062,9 +2243,13 @@ static void append_run(struct etpan_thread_op * op)
                return;
        }
        
-       r = mailimap_append(param->imap, param->mailbox,
+       mailstream_logger = imap_logger_append;
+       
+       r = mailimap_uidplus_append(param->imap, param->mailbox,
                            param->flag_list, NULL,
-                           data, size/*, &uid */);
+                           data, size, &val, &uid);
+
+       mailstream_logger = imap_logger_cmd;
        
        munmap(data, size);
        close(fd);
@@ -2153,6 +2338,8 @@ struct copy_param {
 
 struct copy_result {
        int error;
+       struct mailimap_set *source;
+       struct mailimap_set *dest;
 };
 
 static void copy_run(struct etpan_thread_op * op)
@@ -2160,19 +2347,28 @@ static void copy_run(struct etpan_thread_op * op)
        struct copy_param * param;
        struct copy_result * result;
        int r;
-       
+       guint32 val;
        param = op->param;
+       struct mailimap_set *source = NULL, *dest = NULL;
        
-       r = mailimap_uid_copy(param->imap, param->set, param->mb);
+       r = mailimap_uidplus_uid_copy(param->imap, param->set, param->mb,
+               &val, &source, &dest);
        
        result = op->result;
        result->error = r;
-       
+       if (r == 0) {
+               result->source = source;
+               result->dest = dest;
+       } else {
+               result->source = NULL;
+               result->dest = NULL;
+       }
        debug_print("imap copy run - end %i\n", r);
 }
 
 int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
-                      const char * mb)
+                      const char * mb, struct mailimap_set **source,
+                      struct mailimap_set **dest)
 {
        struct copy_param param;
        struct copy_result result;
@@ -2186,10 +2382,15 @@ int imap_threaded_copy(Folder * folder, struct mailimap_set * set,
        param.mb = mb;
        
        threaded_run(folder, &param, &result, copy_run);
+       *source = NULL;
+       *dest = NULL;
        
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
+       *source = result.source;
+       *dest = result.dest;
+
        debug_print("imap copy - end\n");
        
        return result.error;