Clear statusbar's progressbar in case of NNTP xhdr/xover failure.
[claws.git] / src / etpan / nntp-thread.c
index 57cafe9aef39f953d8fe6e8452914a5946290ca2..3c147d26d12e6ac15886b5ac41c8a5554dcdbb07 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-2016 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #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>
 #include <gtk/gtk.h>
 #include <log.h>
 #include "etpan-thread-manager.h"
+#include "etpan-ssl.h"
 #include "utils.h"
 #include "mainwindow.h"
 #include "ssl_certificate.h"
 #include "socket.h"
 #include "remotefolder.h"
+#include "main.h"
+#include "account.h"
+#include "statusbar.h"
 
 #define DISABLE_LOG_DURING_LOGIN
 
+#define NNTP_BATCH_SIZE 5000
+
 static struct etpan_thread_manager * thread_manager = NULL;
-static chash * courier_workaround_hash = NULL;
 static chash * nntp_hash = NULL;
 static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
 static GIOChannel * io_channel = NULL;
 
-static void (*previous_stream_logger)(int direction,
-    const char * str, size_t size);
-
 static void nntp_logger(int direction, const char * str, size_t size) 
 {
        gchar *buf;
@@ -94,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;
@@ -117,6 +118,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;
@@ -133,13 +141,16 @@ void nntp_main_init(gboolean skip_ssl_cert_check)
        
        nntp_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
-       courier_workaround_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
        
        thread_manager = etpan_thread_manager_new();
        
        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,
@@ -147,9 +158,13 @@ void nntp_main_init(gboolean skip_ssl_cert_check)
                                                    NULL);
 }
 
-void nntp_main_done(void)
+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;
+#endif
        etpan_thread_manager_join(thread_manager);
        
        g_source_remove(thread_manager_signal);
@@ -197,7 +212,7 @@ void nntp_done(Folder * folder)
        
        chash_delete(nntp_hash, &key, NULL);
        
-       debug_print("remove thread");
+       debug_print("remove thread\n");
 }
 
 static struct etpan_thread * get_thread(Folder * folder)
@@ -205,13 +220,17 @@ static struct etpan_thread * get_thread(Folder * folder)
        struct etpan_thread * thread;
        chashdatum key;
        chashdatum value;
-       
+       int r;
+
        key.data = &folder;
        key.len = sizeof(folder);
-       
-       chash_get(nntp_hash, &key, &value);
+
+       r = chash_get(nntp_hash, &key, &value);
+       if (r < 0)
+               return NULL;
+
        thread = value.data;
-       
+
        return thread;
 }
 
@@ -250,7 +269,9 @@ static void threaded_run(Folder * folder, void * param, void * result,
 {
        struct etpan_thread_op * op;
        struct etpan_thread * thread;
-       
+       void (*previous_stream_logger)(int direction,
+               const char * str, size_t size);
+
        nntp_folder_ref(folder);
 
        op = etpan_thread_op_new();
@@ -258,14 +279,10 @@ static void threaded_run(Folder * folder, void * param, void * result,
        op->nntp = get_nntp(folder);
        op->param = param;
        op->result = result;
-       
-       op->cancellable = 0;
+
        op->run = func;
        op->callback = generic_cb;
        op->callback_data = op;
-       op->cleanup = NULL;
-       
-       op->finished = 0;
        
        previous_stream_logger = mailstream_logger;
        mailstream_logger = nntp_logger;
@@ -289,6 +306,7 @@ static void threaded_run(Folder * folder, void * param, void * result,
 
 struct connect_param {
        newsnntp * nntp;
+       PrefsAccount *account;
        const char * server;
        int port;
 };
@@ -356,58 +374,7 @@ int nntp_threaded_connect(Folder * folder, const char * server, int port)
        
        return result.error;
 }
-
-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
-       struct connect_param *param = (struct connect_param *)data;
-       gnutls_x509_crt cert = NULL;
-       gnutls_datum tmp;
-       
-       if (certificate == NULL || len < 0) {
-               g_warning("no cert presented.\n");
-               return 0;
-       }
-       
-       tmp.data = malloc(len);
-       memcpy(tmp.data, certificate, len);
-       tmp.size = len;
-       gnutls_x509_crt_init(&cert);
-       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,
-               (gchar *)param->server, (gushort)param->port) == TRUE) {
-               gnutls_x509_crt_deinit(cert);
-               return 0;
-       } else {
-               gnutls_x509_crt_deinit(cert);
-               return -1;
-       }
-#endif
-       return 0;
-}
-
+#ifdef USE_GNUTLS
 static void connect_ssl_run(struct etpan_thread_op * op)
 {
        int r;
@@ -419,8 +386,9 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        CHECK_NNTP();
 
-       r = newsnntp_ssl_connect(param->nntp,
-                                param->server, param->port);
+       r = newsnntp_ssl_connect_with_callback(param->nntp,
+                                param->server, param->port,
+                                etpan_connect_ssl_context_cb, param->account);
        result->error = r;
 }
 
@@ -431,13 +399,12 @@ int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
        chashdatum key;
        chashdatum value;
        newsnntp * nntp, * oldnntp;
-       unsigned char *certificate = NULL;
-       int cert_len;
-       
+       gboolean accept_if_valid = FALSE;
+
        oldnntp = get_nntp(folder);
 
        nntp = newsnntp_new(0, NULL);
-       
+
        if (oldnntp) {
                debug_print("deleting old nntp %p\n", oldnntp);
                delete_nntp(folder, oldnntp);
@@ -448,25 +415,28 @@ int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
        value.data = nntp;
        value.len = 0;
        chash_set(session_hash, &key, &value, NULL);
-       
+
        param.nntp = nntp;
        param.server = server;
        param.port = port;
-       
+       param.account = folder->account;
+
+       if (folder->account)
+               accept_if_valid = folder->account->ssl_certs_auto_accept;
+
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_ssl_run);
 
        if (result.error == NEWSNNTP_NO_ERROR && !etpan_skip_ssl_cert_check) {
-               cert_len = (int)mailstream_ssl_get_certificate(nntp->nntp_stream, &certificate);
-               if (etpan_certificate_check(certificate, cert_len, &param) < 0)
+               if (etpan_certificate_check(nntp->nntp_stream, server, port,
+                                           accept_if_valid) != TRUE)
                        return -1;
-               if (certificate) 
-                       free(certificate); 
        }
        debug_print("connect %d with nntp %p\n", result.error, nntp);
        
        return result.error;
 }
+#endif
 
 void nntp_threaded_disconnect(Folder * folder)
 {
@@ -524,8 +494,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);
        }
        
@@ -733,7 +704,7 @@ int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t
 
        threaded_run(folder, &param, &result, article_run);
        
-       debug_print("nntp post - end\n");
+       debug_print("nntp article - end\n");
        
        return result.error;
 }
@@ -854,26 +825,70 @@ static void xover_run(struct etpan_thread_op * op)
        }
        
        result->error = r;
-       debug_print("nntp xover run - end %i\n", r);
+       debug_print("nntp xover run %d-%d - end %i\n",
+                       param->beg, param->end, r);
 }
 
 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
 {
        struct xover_param param;
        struct xover_result result;
-       
-       debug_print("nntp xover - begin\n");
-       
-       param.nntp = get_nntp(folder);
-       param.beg = beg;
-       param.end = end;
-       param.result = single_result;
-       param.msglist = multiple_result;
+       clist *l = NULL, *h = NULL;
+       guint32 cbeg = 0, cend = 0;
+
+       debug_print("nntp xover - begin (%d-%d)\n", beg, end);
+
+       h = clist_new();
+
+       /* Request the overview in batches of NNTP_BATCH_SIZE, to prevent
+        * long stalls or libetpan choking on too large server response,
+        * and to allow updating any progress indicators while we work. */
+       cbeg = beg;
+       while (cbeg <= end && cend <= end) {
+               cend = cbeg + (NNTP_BATCH_SIZE - 1);
+               if (cend > end)
+                       cend = end;
+
+               statusbar_progress_all(cbeg - beg, end - beg, 1);
+               GTK_EVENTS_FLUSH();
+
+               param.nntp = get_nntp(folder);
+               param.beg = cbeg;
+               param.end = cend;
+               param.result = single_result;
+               param.msglist = &l;
+
+               threaded_run(folder, &param, &result, xover_run);
+
+               /* Handle errors */
+               if (result.error != NEWSNNTP_NO_ERROR) {
+                       log_warning(LOG_PROTOCOL, _("couldn't get xover range\n"));
+                       debug_print("couldn't get xover for %d-%d\n", cbeg, cend);
+                       if (l != NULL)
+                               newsnntp_xover_resp_list_free(l);
+                       newsnntp_xover_resp_list_free(h);
+                       statusbar_progress_all(0, 0, 0);
+                       return result.error;
+               }
+
+               /* Append the new data (l) to list of results (h). */
+               if (l != NULL) {
+                       debug_print("total items so far %d, items this batch %d\n",
+                                       clist_count(h), clist_count(l));
+                       clist_concat(h, l);
+                       clist_free(l);
+                       l = NULL;
+               }
+
+               cbeg += NNTP_BATCH_SIZE;
+       }
 
-       threaded_run(folder, &param, &result, xover_run);
+       statusbar_progress_all(0, 0, 0);
        
        debug_print("nntp xover - end\n");
-       
+
+       *multiple_result = h;
+
        return result.error;
 }
 
@@ -903,41 +918,91 @@ static void xhdr_run(struct etpan_thread_op * op)
        if (param->beg == param->end) {
                r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
        } else {
-               r = -1;
-               g_warning("XHDR range not implemented\n");
+               r = newsnntp_xhdr_range(param->nntp, param->header, param->beg, param->end, param->hdrlist);
        }
        
        result->error = r;
-       debug_print("nntp xhdr run - end %i\n", r);
+       debug_print("nntp xhdr '%s %d-%d' run - end %i\n",
+                       param->header, param->beg, param->end, r);
 }
 
 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
 {
        struct xhdr_param param;
        struct xhdr_result result;
-       
-       debug_print("nntp xhdr - begin\n");
-       
-       param.nntp = get_nntp(folder);
-       param.header = header;
-       param.beg = beg;
-       param.end = end;
-       param.hdrlist = hdrlist;
+       clist *l = NULL;
+       clist *h = *hdrlist;
+       guint32 cbeg = 0, cend = 0;
+
+       debug_print("nntp xhdr %s - begin (%d-%d)\n", header, beg, end);
+
+       if (h == NULL)
+               h = clist_new();
+
+       /* Request the headers in batches of NNTP_BATCH_SIZE, to prevent
+        * long stalls or libetpan choking on too large server response,
+        * and to allow updating any progress indicators while we work. */
+       cbeg = beg;
+       while (cbeg <= end && cend <= end) {
+               cend = cbeg + NNTP_BATCH_SIZE - 1;
+               if (cend > end)
+                       cend = end;
+
+               statusbar_progress_all(cbeg - beg, end - beg, 1);
+               GTK_EVENTS_FLUSH();
+
+               param.nntp = get_nntp(folder);
+               param.header = header;
+               param.beg = cbeg;
+               param.end = cend;
+               param.hdrlist = &l;
+
+               threaded_run(folder, &param, &result, xhdr_run);
+
+               /* Handle errors */
+               if (result.error != NEWSNNTP_NO_ERROR) {
+                       log_warning(LOG_PROTOCOL, _("couldn't get xhdr range\n"));
+                       debug_print("couldn't get xhdr %s %d-%d\n",     header, cbeg, cend);
+                       if (l != NULL)
+                               newsnntp_xhdr_free(l);
+                       newsnntp_xhdr_free(h);
+                       statusbar_progress_all(0, 0, 0);
+                       return result.error;
+               }
+
+               /* Append the new data (l) to list of results (h). */
+               if (l != NULL) {
+                       debug_print("total items so far %d, items this batch %d\n",
+                                       clist_count(h), clist_count(l));
+                       clist_concat(h, l);
+                       clist_free(l);
+                       l = NULL;
+               }
+
+               cbeg += NNTP_BATCH_SIZE;
+       }
 
-       threaded_run(folder, &param, &result, xhdr_run);
-       
-       debug_print("nntp xhdr - end\n");
+       statusbar_progress_all(0, 0, 0);
        
+       debug_print("nntp xhdr %s - end (%d-%d)\n", header, beg, end);
+
+       *hdrlist = h;
+
        return result.error;
 }
 
+void nntp_main_set_timeout(int sec)
+{
+       mailstream_network_delay.tv_sec = sec;
+       mailstream_network_delay.tv_usec = 0;
+}
 
 #else
 
 void nntp_main_init(void)
 {
 }
-void nntp_main_done(void)
+void nntp_main_done(gboolean have_connectivity)
 {
 }
 void nntp_main_set_timeout(int sec)