Fix printf formats for size_t and goffset arguments.
[claws.git] / src / etpan / nntp-thread.c
index c20802867cda87da09371defbd3b176a95f35756..bf67cd004c0d2fb781392a81e41e8d840394741c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2012 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
@@ -14,7 +14,6 @@
  *
  * 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 "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 * nntp_hash = NULL;
 static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
 static GIOChannel * io_channel = NULL;
 
+static int do_newsnntp_socket_connect(newsnntp * imap, const char * server,
+                              gushort port, ProxyInfo * proxy_info)
+{
+       SockInfo * sock;
+       mailstream * stream;
+
+       if (!proxy_info)
+               return newsnntp_socket_connect(imap, server, port);
+
+       if (port == 0)
+               port = 119;
+
+       sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
+
+       if (sock == NULL)
+               return NEWSNNTP_ERROR_CONNECTION_REFUSED;
+
+       if (proxy_connect(sock, server, port, proxy_info) < 0) {
+               sock_close(sock);
+               return NEWSNNTP_ERROR_CONNECTION_REFUSED;
+       }
+
+       stream = mailstream_socket_open_timeout(sock->sock,
+                       imap->nntp_timeout);
+       if (stream == NULL) {
+               sock_close(sock);
+               return NEWSNNTP_ERROR_MEMORY;
+       }
+
+       return newsnntp_connect(imap, stream);
+}
+
+static int do_newsnntp_ssl_connect_with_callback(newsnntp * imap, const char * server,
+       gushort port,
+       void (* callback)(struct mailstream_ssl_context * ssl_context, void * data),
+       void * data,
+       ProxyInfo *proxy_info)
+{
+       SockInfo * sock;
+       mailstream * stream;
+
+       if (!proxy_info)
+               return newsnntp_ssl_connect_with_callback(imap, server,
+                               port, callback, data);
+
+       if (port == 0)
+               port = 563;
+
+       sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
+
+       if (sock == NULL)
+               return NEWSNNTP_ERROR_CONNECTION_REFUSED;
+
+       if (proxy_connect(sock, server, port, proxy_info) < 0) {
+               sock_close(sock);
+               return NEWSNNTP_ERROR_CONNECTION_REFUSED;
+       }
+
+       stream = mailstream_ssl_open_with_callback_timeout(sock->sock,
+                       imap->nntp_timeout, callback, data);
+       if (stream == NULL) {
+               sock_close(sock);
+               return NEWSNNTP_ERROR_SSL;
+       }
+
+       return newsnntp_connect(imap, stream);
+}
+
+
 static void nntp_logger(int direction, const char * str, size_t size) 
 {
        gchar *buf;
@@ -65,7 +136,7 @@ static void nntp_logger(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 256) {
-               log_print(LOG_PROTOCOL, "NNTP%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "NNTP%c [data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
                return;
        }
        buf = malloc(size+1);
@@ -210,7 +281,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)
@@ -307,6 +378,7 @@ struct connect_param {
        PrefsAccount *account;
        const char * server;
        int port;
+       ProxyInfo * proxy_info;
 };
 
 struct connect_result {
@@ -331,14 +403,15 @@ static void connect_run(struct etpan_thread_op * op)
        
        CHECK_NNTP();
 
-       r = newsnntp_socket_connect(param->nntp,
-                                   param->server, param->port);
+       r = do_newsnntp_socket_connect(param->nntp,
+                                   param->server, param->port,
+                                   param->proxy_info);
        
        result->error = r;
 }
 
 
-int nntp_threaded_connect(Folder * folder, const char * server, int port)
+int nntp_threaded_connect(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
 {
        struct connect_param param;
        struct connect_result result;
@@ -364,7 +437,8 @@ int nntp_threaded_connect(Folder * folder, const char * server, int port)
        param.nntp = nntp;
        param.server = server;
        param.port = port;
-       
+       param.proxy_info = proxy_info;
+
        refresh_resolvers();
        threaded_run(folder, &param, &result, connect_run);
        
@@ -384,13 +458,14 @@ static void connect_ssl_run(struct etpan_thread_op * op)
        
        CHECK_NNTP();
 
-       r = newsnntp_ssl_connect_with_callback(param->nntp,
+       r = do_newsnntp_ssl_connect_with_callback(param->nntp,
                                 param->server, param->port,
-                                etpan_connect_ssl_context_cb, param->account);
+                                etpan_connect_ssl_context_cb, param->account,
+                                param->proxy_info);
        result->error = r;
 }
 
-int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
+int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
 {
        struct connect_param param;
        struct connect_result result;
@@ -418,6 +493,7 @@ int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
        param.server = server;
        param.port = port;
        param.account = folder->account;
+       param.proxy_info = proxy_info;
 
        if (folder->account)
                accept_if_valid = folder->account->ssl_certs_auto_accept;
@@ -823,26 +899,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;
 }
 
@@ -876,26 +996,72 @@ static void xhdr_run(struct etpan_thread_op * op)
        }
        
        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;
 }