2006-06-19 [colin] 2.3.0cvs23
[claws.git] / src / etpan / imap-thread.c
index 465d14aec3ef2827bf74d38f151a0178649d5cd5..f3d0d192f963f724544fbd2401cdb3b197434e7b 100644 (file)
@@ -105,12 +105,48 @@ void imap_logger_fetch(int direction, const char * str, size_t size)
        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 < 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);
+}
+
 #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;
        
@@ -338,14 +374,16 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
                g_warning("no cert presented.\n");
                return 0;
        }
-       cert = d2i_X509(NULL, (unsigned char **) &certificate, len);
+       cert = d2i_X509(NULL, &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) {
+               (gchar *)param->server, (gushort)param->port) == TRUE) {
+               X509_free(cert);
                return 0;
        } else {
+               X509_free(cert);
                return -1;
        }
 #else
@@ -391,7 +429,7 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        
        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;
@@ -804,7 +842,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;
@@ -1073,6 +1111,26 @@ 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;
@@ -1085,7 +1143,8 @@ static void search_run(struct etpan_thread_op * op)
        
        param = op->param;
        
-       uid_key = mailimap_search_key_new_uid(param->set);
+       /* we copy the mailimap_set because freeing the key is recursive */
+       uid_key = mailimap_search_key_new_uid(sc_mailimap_set_copy(param->set));
        
        search_type_key = NULL;
        switch (param->type) {
@@ -1149,6 +1208,9 @@ static void search_run(struct etpan_thread_op * op)
        
        r = mailimap_uid_search(param->imap, NULL, key, &search_result);
        
+       /* free the key (with the imapset) */
+       mailimap_search_key_free(key);
+
        result = op->result;
        result->error = r;
        result->search_result = search_result;
@@ -2109,9 +2171,13 @@ static void append_run(struct etpan_thread_op * op)
                return;
        }
        
+       mailstream_logger = imap_logger_append;
+       
        r = mailimap_append(param->imap, param->mailbox,
                            param->flag_list, NULL,
                            data, size/*, &uid */);
+
+       mailstream_logger = imap_logger_cmd;
        
        munmap(data, size);
        close(fd);