2005-07-18 [colin] 1.9.12cvs78
[claws.git] / src / etpan / imap-thread.c
index 28d9aed1940bdca056bd6c8f26d504d07c92a6ca..71e0da9ca5340d0619fe2806528318a40d151329 100644 (file)
 #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;
@@ -32,6 +36,25 @@ static gboolean thread_manager_event(GIOChannel * source,
        return TRUE;
 }
 
+void imap_logger(int direction, const char * str, size_t size) 
+{
+       gchar buf[512];
+       strncpy(buf, str, 511);
+       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
 
@@ -41,10 +64,10 @@ 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);
@@ -423,11 +446,24 @@ 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;
+       mailstream_debug = 0;
+#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);
@@ -585,6 +621,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 = MAIL_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 = MAIL_ERROR_STREAM;
+                       return;
+               }
+               mailstream_low_free(plain_low);
+               mailstream_set_low(imap->imap_stream, tls_low);
+       }
 }
 
 int imap_threaded_starttls(Folder * folder)