2006-05-18 [colin] 2.2.0cvs33
[claws.git] / src / etpan / imap-thread.c
index 1a2580ea491b57c006bd9f9476f349d6c1369fc1..3912d5f8e93effab07e7a5bc8265bdedfbc61dc1 100644 (file)
@@ -8,7 +8,7 @@
 #include <imap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#if (defined (__NetBSD__) || defined (__FreeBSD__) || defined (__CYGWIN__))
+#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__CYGWIN__))
 #include <sys/socket.h>
 #endif
 #include <fcntl.h>
@@ -40,26 +40,69 @@ static gboolean thread_manager_event(GIOChannel * source,
        return TRUE;
 }
 
-void imap_logger(int direction, const char * str, size_t size) 
+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);
+}
+
+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);
 }
 
 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
@@ -72,7 +115,7 @@ void imap_main_init(void)
        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);
@@ -285,7 +328,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,7 +338,7 @@ 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;
@@ -1030,6 +1073,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;
@@ -1042,7 +1105,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) {
@@ -1106,6 +1170,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;
@@ -1391,9 +1458,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);