2009-01-17 [colin] 3.7.0cvs37
[claws.git] / src / etpan / imap-thread.c
index 6c8651344941dcdeda3946b40f95d9ec307577cb..4521e00184fd1f47d5e3a198599e4da9608b372a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2007 DINH Viet Hoa and the Claws Mail team
+ * Copyright (C) 2005-2009 DINH Viet Hoa and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -40,6 +40,7 @@
 #include "etpan-thread-manager.h"
 #include "utils.h"
 #include "mainwindow.h"
+#include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
 #include "remotefolder.h"
@@ -80,6 +81,13 @@ static gboolean thread_manager_event(GIOChannel * source,
     GIOCondition condition,
     gpointer data)
 {
+#ifdef G_OS_WIN32
+       gsize bytes_read;
+       gchar ch;
+       
+       if (condition & G_IO_IN)
+               g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
+#endif
        etpan_thread_manager_loop(thread_manager);
        
        return TRUE;
@@ -271,8 +279,11 @@ void imap_main_init(gboolean skip_ssl_cert_check)
        
        fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
        
+#ifndef G_OS_WIN32
        io_channel = g_io_channel_unix_new(fd_thread_manager);
-       
+#else
+       io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
+#endif
        thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
                                                    thread_manager_event,
                                                    (gpointer) NULL,
@@ -438,6 +449,7 @@ static void threaded_run(Folder * folder, void * param, void * result,
 
 struct connect_param {
        mailimap * imap;
+       PrefsAccount *account;
        const char * server;
        int port;
 };
@@ -557,6 +569,60 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
        return 0;
 }
 
+static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
+{
+#if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
+       PrefsAccount *account = (PrefsAccount *)data;
+       const gchar *cert_path = NULL;
+       const gchar *password = NULL;
+#ifdef USE_OPENSSL
+       X509 *x509 = NULL;
+       EVP_PKEY *pkey = NULL;
+#else
+       gnutls_x509_crt x509 = NULL;
+       gnutls_x509_privkey pkey = NULL;
+#endif
+
+       if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
+               cert_path = account->in_ssl_client_cert_file;
+       if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
+               password = account->in_ssl_client_cert_pass;
+       
+       if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
+           mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
+               debug_print("Impossible to set the client certificate.\n");
+       x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
+       pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
+       if (!(x509 && pkey)) {
+               /* try pkcs12 format */
+               ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
+       }
+       if (x509 && pkey) {
+               unsigned char *x509_der = NULL, *pkey_der = NULL;
+               size_t x509_len, pkey_len;
+               
+#ifndef USE_GNUTLS
+               x509_len = (size_t)i2d_X509(x509, &x509_der);
+               pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
+#else
+               x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
+               pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
+#endif
+               if (x509_len > 0 && pkey_len > 0) {
+                       if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
+                           mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0) 
+                               log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
+                       g_free(x509_der);
+                       g_free(pkey_der);
+               }
+#ifdef USE_GNUTLS
+               gnutls_x509_crt_deinit(x509);
+               gnutls_x509_privkey_deinit(pkey);
+#endif
+       }
+#endif
+}
+
 static void connect_ssl_run(struct etpan_thread_op * op)
 {
        int r;
@@ -568,8 +634,9 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        CHECK_IMAP();
 
-       r = mailimap_ssl_connect(param->imap,
-                                param->server, param->port);
+       r = mailimap_ssl_connect_with_callback(param->imap,
+                                               param->server, param->port,
+                                               connect_ssl_context_cb, param->account);
        result->error = r;
 }
 
@@ -601,7 +668,8 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        param.imap = imap;
        param.server = server;
        param.port = port;
-       
+       param.account = folder->account;
+
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_ssl_run);
 
@@ -1119,7 +1187,7 @@ static void starttls_run(struct etpan_thread_op * op)
                        return;
                }
 
-               tls_low = mailstream_low_tls_open(fd);
+               tls_low = mailstream_low_tls_open_with_callback(fd, connect_ssl_context_cb, param->account);
                if (tls_low == NULL) {
                        debug_print("imap starttls run - can't tls_open\n");
                        result->error = MAILIMAP_ERROR_STREAM;
@@ -1142,7 +1210,8 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        param.imap = get_imap(folder);
        param.server = host;
        param.port = port;
-       
+       param.account = folder->account;
+
        threaded_run(folder, &param, &result, starttls_run);
        
        debug_print("imap starttls - end\n");
@@ -1356,8 +1425,10 @@ int imap_threaded_select(Folder * folder, const char * mb,
 
        if (imap->imap_selection_info->sel_perm_flags) {
                GSList *t_flags = NULL;
-               clistiter *cur =
-                       clist_begin(imap->imap_selection_info->sel_perm_flags);
+               clistiter *cur = NULL;
+               if (imap->imap_selection_info->sel_perm_flags)
+                       cur = clist_begin(imap->imap_selection_info->sel_perm_flags);
+
                for (; cur; cur = clist_next(cur)) {
                        struct mailimap_flag_perm *flag = (struct mailimap_flag_perm *)clist_content(cur);
                        if (flag->fl_type == MAILIMAP_FLAG_PERM_ALL)
@@ -1387,6 +1458,12 @@ int imap_threaded_select(Folder * folder, const char * mb,
                                case MAILIMAP_FLAG_KEYWORD:
                                        if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
                                                c_flag = IMAP_FLAG_FORWARDED;
+                                       if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "Junk"))
+                                               c_flag = IMAP_FLAG_SPAM;
+                                       if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NonJunk") ||
+                                           !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NoJunk") ||
+                                           !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NotJunk"))
+                                               c_flag = IMAP_FLAG_HAM;
                                        break;
                                default:
                                        break;
@@ -1613,6 +1690,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_SPAM:
+               search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
+                                                         NULL, NULL, NULL, NULL, NULL,
+                                                         strdup("Junk"), 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) {
@@ -1685,7 +1770,7 @@ static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
 static int
 result_to_uid_list(clist * fetch_result, carray ** result)
 {
-       clistiter * cur;
+       clistiter * cur = NULL;
        int r;
        int res;
        carray * tab;
@@ -1696,8 +1781,10 @@ result_to_uid_list(clist * fetch_result, carray ** result)
                goto err;
        }
        
-       for(cur = clist_begin(fetch_result) ; cur != NULL ;
-           cur = clist_next(cur)) {
+       if (fetch_result)
+               cur = clist_begin(fetch_result);
+
+       for(; cur != NULL ; cur = clist_next(cur)) {
                struct mailimap_msg_att * msg_att;
                uint32_t uid;
                uint32_t * puid;
@@ -1885,7 +1972,7 @@ static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList
 static int
 result_to_uid_flags_list(clist * fetch_result, carray ** result)
 {
-       clistiter * cur;
+       clistiter * cur = NULL;
        int r;
        int res;
        carray * tab;
@@ -1896,9 +1983,11 @@ result_to_uid_flags_list(clist * fetch_result, carray ** result)
                res = MAILIMAP_ERROR_MEMORY;
                goto err;
        }
-       
-       for(cur = clist_begin(fetch_result) ; cur != NULL ;
-           cur = clist_next(cur)) {
+
+       if (fetch_result)
+               cur = clist_begin(fetch_result);
+
+       for(; cur != NULL ; cur = clist_next(cur)) {
                struct mailimap_msg_att * msg_att;
                uint32_t uid;
                uint32_t * puid;
@@ -2177,7 +2266,7 @@ static int imap_fetch(mailimap * imap,
                return r;
        }
   
-       if (clist_begin(fetch_result) == NULL) {
+       if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
                mailimap_fetch_list_free(fetch_result);
                return MAILIMAP_ERROR_FETCH;
        }
@@ -2187,8 +2276,12 @@ static int imap_fetch(mailimap * imap,
        text = NULL;
        text_length = 0;
 
-       for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
-           cur = clist_next(cur)) {
+       if (msg_att->att_list)
+               cur = clist_begin(msg_att->att_list);
+       else
+               cur = NULL;
+
+       for(; cur != NULL ; cur = clist_next(cur)) {
                msg_att_item = clist_content(cur);
 
                if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
@@ -2279,7 +2372,7 @@ static int imap_fetch_header(mailimap * imap,
     return r;
   }
 
-  if (clist_begin(fetch_result) == NULL) {
+  if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
     mailimap_fetch_list_free(fetch_result);
     return MAILIMAP_ERROR_FETCH;
   }
@@ -2289,8 +2382,12 @@ static int imap_fetch_header(mailimap * imap,
   text = NULL;
   text_length = 0;
 
-  for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
-      cur = clist_next(cur)) {
+  if (msg_att->att_list)
+     cur = clist_begin(msg_att->att_list);
+  else
+     cur = NULL;
+
+  for(; cur != NULL ; cur = clist_next(cur)) {
     msg_att_item = clist_content(cur);
 
     if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
@@ -2362,7 +2459,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
        result->error = r;
        
        if (r == MAILIMAP_NO_ERROR) {
-               fd = open(param->filename, O_RDWR | O_CREAT, 0600);
+               fd = g_open(param->filename, O_RDWR | O_CREAT, 0600);
                if (fd < 0) {
                        result->error = MAILIMAP_ERROR_FETCH;
                        goto free;
@@ -2471,6 +2568,12 @@ static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn, GSList
                        case MAILIMAP_FLAG_KEYWORD:
                                if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
                                        flags |= MSG_FORWARDED;
+                               else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "Junk")) 
+                                       flags |= MSG_SPAM;
+                               else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NonJunk") ||
+                                        !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NoJunk") ||
+                                        !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NotJunk")) 
+                                       flags &= ~MSG_SPAM;
                                else if (s_tags)
                                        tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
                                break;
@@ -2499,8 +2602,11 @@ static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
   ref_size = 0;
   att_dyn = NULL;
 
-  for(item_cur = clist_begin(msg_att->att_list) ; item_cur != NULL ;
-      item_cur = clist_next(item_cur)) {
+  if (msg_att->att_list)
+     item_cur = clist_begin(msg_att->att_list);
+  else
+     item_cur = NULL;
+  for(; item_cur != NULL ; item_cur = clist_next(item_cur)) {
     struct mailimap_msg_att_item * item;
 
     item = clist_content(item_cur);
@@ -2577,23 +2683,26 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
        i = 0;
        env_list = carray_new(16);
   
-       for(cur = clist_begin(fetch_result) ; cur != NULL ;
-           cur = clist_next(cur)) {
-               struct mailimap_msg_att * msg_att;
-               struct imap_fetch_env_info * env_info;
-               GSList *tags = NULL;
-
-               msg_att = clist_content(cur);
-
-               env_info = fetch_to_env_info(msg_att, &tags);
-               if (!env_info)
-                       return MAILIMAP_ERROR_MEMORY;
-               carray_add(env_list, env_info, NULL);
-               carray_add(env_list, tags, NULL);
+       if (fetch_result) {
+               for(cur = clist_begin(fetch_result) ; cur != NULL ;
+                   cur = clist_next(cur)) {
+                       struct mailimap_msg_att * msg_att;
+                       struct imap_fetch_env_info * env_info;
+                       GSList *tags = NULL;
+
+                       msg_att = clist_content(cur);
+
+                       env_info = fetch_to_env_info(msg_att, &tags);
+                       if (!env_info)
+                               return MAILIMAP_ERROR_MEMORY;
+                       carray_add(env_list, env_info, NULL);
+                       carray_add(env_list, tags, NULL);
+               }
+               * p_env_list = env_list;
+       } else {
+               * p_env_list = NULL;
        }
-  
-       * p_env_list = env_list;
-  
+
        return MAIL_NO_ERROR;
 }
 
@@ -2695,7 +2804,7 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                return r;
        }
        
-       if (clist_begin(fetch_result) == NULL) {
+       if (fetch_result == NULL || clist_begin(fetch_result) == NULL) {
                res = MAILIMAP_ERROR_FETCH;
                debug_print("clist_begin = NULL\n");
                goto err;
@@ -2851,7 +2960,7 @@ static void append_run(struct etpan_thread_op * op)
        }
        size = stat_buf.st_size;
        
-       fd = open(param->filename, O_RDONLY);
+       fd = g_open(param->filename, O_RDONLY);
        if (fd < 0) {
                result->error = MAILIMAP_ERROR_APPEND;
                return;
@@ -3137,7 +3246,7 @@ static void do_exec_command(int fd, const char * command,
        /* Detach from the controlling tty if we have one. Otherwise,
           SSH might do something stupid like trying to use it instead
           of running $SSH_ASKPASS. Doh. */
-       fd = open("/dev/tty", O_RDONLY);
+       fd = g_open("/dev/tty", O_RDONLY);
        if (fd != -1) {
                ioctl(fd, TIOCNOTTY, NULL);
                close(fd);