2012-11-28 [colin] 3.9.0cvs28
[claws.git] / src / etpan / nntp-thread.c
index 47a59a38944e8c01d8d23907a4bb25b640c7092c..e380e80df4e4da0316753e82ab30daf3e0be0dbf 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2009 DINH Viet Hoa and the Claws Mail team
+ * Copyright (C) 2005-2012 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
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef HAVE_LIBETPAN
 
+#include <glib.h>
+#include <glib/gi18n.h>
 #include "nntp-thread.h"
 #include "news.h"
 #include <sys/types.h>
@@ -43,6 +46,8 @@
 #include "ssl_certificate.h"
 #include "socket.h"
 #include "remotefolder.h"
+#include "main.h"
+#include "account.h"
 
 #define DISABLE_LOG_DURING_LOGIN
 
@@ -93,12 +98,9 @@ static void nntp_logger(int direction, const char * str, size_t size)
 static void delete_nntp(Folder *folder, newsnntp *nntp)
 {
        chashdatum key;
-       chashdatum value;
 
        key.data = &folder;
        key.len = sizeof(folder);
-       value.data = nntp;
-       value.len = 0;
        chash_delete(session_hash, &key, NULL);
        
        key.data = &nntp;
@@ -158,6 +160,7 @@ void nntp_main_init(gboolean skip_ssl_cert_check)
 
 void nntp_main_done(gboolean have_connectivity)
 {
+       nntp_disconnect_all(have_connectivity);
        etpan_thread_manager_stop(thread_manager);
 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
        return;
@@ -372,30 +375,10 @@ int nntp_threaded_connect(Folder * folder, const char * server, int port)
 
 static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
 {
-#ifdef USE_OPENSSL
-       struct connect_param *param = (struct connect_param *)data;
-       X509 *cert = NULL;
-       
-       if (certificate == NULL || len < 0) {
-               g_warning("no cert presented.\n");
-               return 0;
-       }
-       cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
-       if (cert == NULL) {
-               g_warning("nntp: can't get cert\n");
-               return 0;
-       } 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;
-       }
-#elif USE_GNUTLS
+#ifdef USE_GNUTLS
        struct connect_param *param = (struct connect_param *)data;
-       gnutls_x509_crt cert = NULL;
-       gnutls_datum tmp;
+       gnutls_x509_crt_t cert = NULL;
+       gnutls_datum_t tmp;
        
        if (certificate == NULL || len < 0) {
                g_warning("no cert presented.\n");
@@ -409,7 +392,7 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
        if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
                g_warning("nntp: can't get cert\n");
                return 0;
-       } else if (ssl_certificate_check(cert, (guint)-1, NULL,
+       } else if (ssl_certificate_check(cert, (guint)-1,
                (gchar *)param->server, (gushort)param->port) == TRUE) {
                gnutls_x509_crt_deinit(cert);
                return 0;
@@ -423,17 +406,12 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
 
 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
 {
-#if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
+#ifdef 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
+       gnutls_x509_crt_t x509 = NULL;
+       gnutls_x509_privkey_t pkey = NULL;
 
        if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
                cert_path = account->in_ssl_client_cert_file;
@@ -453,24 +431,17 @@ static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context,
                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");
+                               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
 }
@@ -593,8 +564,9 @@ static void login_run(struct etpan_thread_op * op)
 #endif
 
        r = newsnntp_authinfo_username(param->nntp, param->login);
-       if (r == NEWSNNTP_NO_ERROR || 
-           r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
+       /* libetpan returning NO_ERROR means it received resp.code 281:
+          in this case auth. is already successful, no password is needed. */
+       if (r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
                r = newsnntp_authinfo_password(param->nntp, param->password);
        }
        
@@ -999,6 +971,11 @@ int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32
        return result.error;
 }
 
+void nntp_main_set_timeout(int sec)
+{
+       mailstream_network_delay.tv_sec = sec;
+       mailstream_network_delay.tv_usec = 0;
+}
 
 #else