Sync Spanish manual
[claws.git] / src / etpan / nntp-thread.c
blob76020ca66da802ab0320d6f52b7b81fd93d0687e
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2005-2022 the Claws Mail team and DINH Viet Hoa
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #ifdef HAVE_LIBETPAN
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include "nntp-thread.h"
29 #include "news.h"
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
33 #include <sys/socket.h>
34 #endif
35 #include <fcntl.h>
36 #ifndef G_OS_WIN32
37 #include <sys/mman.h>
38 #include <sys/wait.h>
39 #endif
40 #include <gtk/gtk.h>
41 #include <log.h>
42 #include "etpan-thread-manager.h"
43 #include "etpan-ssl.h"
44 #include "utils.h"
45 #include "mainwindow.h"
46 #include "ssl_certificate.h"
47 #include "socket.h"
48 #include "remotefolder.h"
49 #include "main.h"
50 #include "account.h"
51 #include "statusbar.h"
53 #define DISABLE_LOG_DURING_LOGIN
55 #define NNTP_BATCH_SIZE 5000
57 static struct etpan_thread_manager * thread_manager = NULL;
58 static chash * nntp_hash = NULL;
59 static chash * session_hash = NULL;
60 static guint thread_manager_signal = 0;
61 static GIOChannel * io_channel = NULL;
63 static int do_newsnntp_socket_connect(newsnntp * imap, const char * server,
64 gushort port, ProxyInfo * proxy_info)
66 SockInfo * sock;
67 mailstream * stream;
69 if (!proxy_info)
70 return newsnntp_socket_connect(imap, server, port);
72 if (port == 0)
73 port = 119;
75 sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
77 if (sock == NULL)
78 return NEWSNNTP_ERROR_CONNECTION_REFUSED;
80 if (proxy_connect(sock, server, port, proxy_info) < 0) {
81 sock_close(sock, TRUE);
82 return NEWSNNTP_ERROR_CONNECTION_REFUSED;
85 stream = mailstream_socket_open_timeout(sock->sock,
86 imap->nntp_timeout);
87 if (stream == NULL) {
88 sock_close(sock, TRUE);
89 return NEWSNNTP_ERROR_MEMORY;
92 /* Libetpan now has the socket fd, and we're not interested in
93 * rest of the SockInfo struct. Let's free it, while not touching
94 * the socket itself. */
95 sock_close(sock, FALSE);
97 return newsnntp_connect(imap, stream);
100 #ifdef USE_GNUTLS
101 static int do_newsnntp_ssl_connect_with_callback(newsnntp * imap, const char * server,
102 gushort port,
103 void (* callback)(struct mailstream_ssl_context * ssl_context, void * data),
104 void * data,
105 ProxyInfo *proxy_info)
107 SockInfo * sock;
108 mailstream * stream;
110 if (!proxy_info)
111 return newsnntp_ssl_connect_with_callback(imap, server,
112 port, callback, data);
114 if (port == 0)
115 port = 563;
117 sock = sock_connect(proxy_info->proxy_host, proxy_info->proxy_port);
119 if (sock == NULL)
120 return NEWSNNTP_ERROR_CONNECTION_REFUSED;
122 if (proxy_connect(sock, server, port, proxy_info) < 0) {
123 sock_close(sock, TRUE);
124 return NEWSNNTP_ERROR_CONNECTION_REFUSED;
127 stream = mailstream_ssl_open_with_callback_timeout(sock->sock,
128 imap->nntp_timeout, callback, data);
129 if (stream == NULL) {
130 sock_close(sock, TRUE);
131 return NEWSNNTP_ERROR_SSL;
134 /* Libetpan now has the socket fd, and we're not interested in
135 * rest of the SockInfo struct. Let's free it, while not touching
136 * the socket itself. */
137 sock_close(sock, FALSE);
139 return newsnntp_connect(imap, stream);
141 #endif
143 static void nntp_logger(int direction, const char * str, size_t size)
145 gchar *buf;
146 gchar **lines;
147 int i = 0;
149 if (size > 256) {
150 log_print(LOG_PROTOCOL, "NNTP%c [data - %"G_GSIZE_FORMAT" bytes]\n", direction?'>':'<', size);
151 return;
153 buf = malloc(size+1);
154 memset(buf, 0, size+1);
155 strncpy(buf, str, size);
156 buf[size] = '\0';
158 if (!strncmp(buf, "<<<<<<<", 7)
159 || !strncmp(buf, ">>>>>>>", 7)) {
160 free(buf);
161 return;
163 while (strstr(buf, "\r"))
164 *strstr(buf, "\r") = ' ';
165 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
166 buf[strlen(buf)-1] = '\0';
168 lines = g_strsplit(buf, "\n", -1);
170 while (lines[i] && *lines[i]) {
171 log_print(LOG_PROTOCOL, "NNTP%c %s\n", direction?'>':'<', lines[i]);
172 i++;
174 g_strfreev(lines);
175 free(buf);
178 static void delete_nntp(Folder *folder, newsnntp *nntp)
180 chashdatum key;
182 key.data = &folder;
183 key.len = sizeof(folder);
184 chash_delete(session_hash, &key, NULL);
186 key.data = &nntp;
187 key.len = sizeof(nntp);
188 if (nntp && nntp->nntp_stream) {
189 /* we don't want libetpan to logout */
190 mailstream_close(nntp->nntp_stream);
191 nntp->nntp_stream = NULL;
193 debug_print("removing newsnntp %p\n", nntp);
194 newsnntp_free(nntp);
197 static gboolean thread_manager_event(GIOChannel * source,
198 GIOCondition condition,
199 gpointer data)
201 #ifdef G_OS_WIN32
202 gsize bytes_read;
203 gchar ch;
205 if (condition & G_IO_IN)
206 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
207 #endif
208 etpan_thread_manager_loop(thread_manager);
210 return TRUE;
213 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
214 extern gboolean etpan_skip_ssl_cert_check;
216 void nntp_main_init(gboolean skip_ssl_cert_check)
218 int fd_thread_manager;
220 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
222 nntp_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
223 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
225 thread_manager = etpan_thread_manager_new();
227 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
229 #ifndef G_OS_WIN32
230 io_channel = g_io_channel_unix_new(fd_thread_manager);
231 #else
232 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
233 #endif
235 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
236 thread_manager_event,
237 (gpointer) NULL,
238 NULL);
241 void nntp_main_done(gboolean have_connectivity)
243 nntp_disconnect_all(have_connectivity);
244 etpan_thread_manager_stop(thread_manager);
245 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
246 return;
247 #endif
248 etpan_thread_manager_join(thread_manager);
250 g_source_remove(thread_manager_signal);
251 g_io_channel_unref(io_channel);
253 etpan_thread_manager_free(thread_manager);
255 chash_free(session_hash);
256 chash_free(nntp_hash);
259 void nntp_init(Folder * folder)
261 struct etpan_thread * thread;
262 chashdatum key;
263 chashdatum value;
265 thread = etpan_thread_manager_get_thread(thread_manager);
267 key.data = &folder;
268 key.len = sizeof(folder);
269 value.data = thread;
270 value.len = 0;
272 chash_set(nntp_hash, &key, &value, NULL);
275 void nntp_done(Folder * folder)
277 struct etpan_thread * thread;
278 chashdatum key;
279 chashdatum value;
280 int r;
282 key.data = &folder;
283 key.len = sizeof(folder);
285 r = chash_get(nntp_hash, &key, &value);
286 if (r < 0)
287 return;
289 thread = value.data;
291 etpan_thread_unbind(thread);
293 chash_delete(nntp_hash, &key, NULL);
295 debug_print("remove thread\n");
298 static struct etpan_thread * get_thread(Folder * folder)
300 struct etpan_thread * thread;
301 chashdatum key;
302 chashdatum value;
303 int r;
305 key.data = &folder;
306 key.len = sizeof(folder);
308 r = chash_get(nntp_hash, &key, &value);
309 if (r < 0)
310 return NULL;
312 thread = value.data;
314 return thread;
317 static newsnntp * get_nntp(Folder * folder)
319 newsnntp * nntp;
320 chashdatum key;
321 chashdatum value;
322 int r;
324 key.data = &folder;
325 key.len = sizeof(folder);
327 r = chash_get(session_hash, &key, &value);
328 if (r < 0)
329 return NULL;
331 nntp = value.data;
332 debug_print("found nntp %p\n", nntp);
333 return nntp;
337 static void generic_cb(int cancelled, void * result, void * callback_data)
339 struct etpan_thread_op * op;
341 op = (struct etpan_thread_op *) callback_data;
343 debug_print("generic_cb\n");
344 op->finished = 1;
347 static void threaded_run(Folder * folder, void * param, void * result,
348 void (* func)(struct etpan_thread_op * ))
350 struct etpan_thread_op * op;
351 struct etpan_thread * thread;
352 void (*previous_stream_logger)(int direction,
353 const char * str, size_t size);
355 nntp_folder_ref(folder);
357 op = etpan_thread_op_new();
359 op->nntp = get_nntp(folder);
360 op->param = param;
361 op->result = result;
363 op->run = func;
364 op->callback = generic_cb;
365 op->callback_data = op;
367 previous_stream_logger = mailstream_logger;
368 mailstream_logger = nntp_logger;
370 thread = get_thread(folder);
371 etpan_thread_op_schedule(thread, op);
373 while (!op->finished) {
374 gtk_main_iteration();
377 mailstream_logger = previous_stream_logger;
379 etpan_thread_op_free(op);
381 nntp_folder_unref(folder);
385 /* connect */
387 struct connect_param {
388 newsnntp * nntp;
389 PrefsAccount *account;
390 const char * server;
391 int port;
392 ProxyInfo * proxy_info;
395 struct connect_result {
396 int error;
399 #define CHECK_NNTP() { \
400 if (!param->nntp) { \
401 result->error = NEWSNNTP_ERROR_BAD_STATE; \
402 return; \
406 static void connect_run(struct etpan_thread_op * op)
408 int r;
409 struct connect_param * param;
410 struct connect_result * result;
412 param = op->param;
413 result = op->result;
415 CHECK_NNTP();
417 r = do_newsnntp_socket_connect(param->nntp,
418 param->server, param->port,
419 param->proxy_info);
421 result->error = r;
425 int nntp_threaded_connect(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
427 struct connect_param param;
428 struct connect_result result;
429 chashdatum key;
430 chashdatum value;
431 newsnntp * nntp, * oldnntp;
433 oldnntp = get_nntp(folder);
435 nntp = newsnntp_new(0, NULL);
437 if (oldnntp) {
438 debug_print("deleting old nntp %p\n", oldnntp);
439 delete_nntp(folder, oldnntp);
442 key.data = &folder;
443 key.len = sizeof(folder);
444 value.data = nntp;
445 value.len = 0;
446 chash_set(session_hash, &key, &value, NULL);
448 param.nntp = nntp;
449 param.server = server;
450 param.port = port;
451 param.proxy_info = proxy_info;
453 refresh_resolvers();
454 threaded_run(folder, &param, &result, connect_run);
456 debug_print("connect ok %i with nntp %p\n", result.error, nntp);
458 return result.error;
460 #ifdef USE_GNUTLS
461 static void connect_ssl_run(struct etpan_thread_op * op)
463 int r;
464 struct connect_param * param;
465 struct connect_result * result;
467 param = op->param;
468 result = op->result;
470 CHECK_NNTP();
472 r = do_newsnntp_ssl_connect_with_callback(param->nntp,
473 param->server, param->port,
474 etpan_connect_ssl_context_cb, param->account,
475 param->proxy_info);
476 result->error = r;
479 int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port, ProxyInfo *proxy_info)
481 struct connect_param param;
482 struct connect_result result;
483 chashdatum key;
484 chashdatum value;
485 newsnntp * nntp, * oldnntp;
486 gboolean accept_if_valid = FALSE;
488 oldnntp = get_nntp(folder);
490 nntp = newsnntp_new(0, NULL);
492 if (oldnntp) {
493 debug_print("deleting old nntp %p\n", oldnntp);
494 delete_nntp(folder, oldnntp);
497 key.data = &folder;
498 key.len = sizeof(folder);
499 value.data = nntp;
500 value.len = 0;
501 chash_set(session_hash, &key, &value, NULL);
503 param.nntp = nntp;
504 param.server = server;
505 param.port = port;
506 param.account = folder->account;
507 param.proxy_info = proxy_info;
509 if (folder->account)
510 accept_if_valid = folder->account->ssl_certs_auto_accept;
512 refresh_resolvers();
513 threaded_run(folder, &param, &result, connect_ssl_run);
515 if (result.error == NEWSNNTP_NO_ERROR && !etpan_skip_ssl_cert_check) {
516 if (etpan_certificate_check(nntp->nntp_stream, server, port,
517 accept_if_valid) != TRUE)
518 return -1;
520 debug_print("connect %d with nntp %p\n", result.error, nntp);
522 return result.error;
524 #endif
526 void nntp_threaded_disconnect(Folder * folder)
528 newsnntp * nntp;
530 nntp = get_nntp(folder);
531 if (nntp == NULL) {
532 debug_print("was disconnected\n");
533 return;
536 debug_print("deleting old nntp %p\n", nntp);
537 delete_nntp(folder, nntp);
539 debug_print("disconnect ok\n");
542 void nntp_threaded_cancel(Folder * folder)
544 newsnntp * nntp;
546 nntp = get_nntp(folder);
547 if (nntp->nntp_stream != NULL)
548 mailstream_cancel(nntp->nntp_stream);
552 struct login_param {
553 newsnntp * nntp;
554 const char * login;
555 const char * password;
558 struct login_result {
559 int error;
562 static void login_run(struct etpan_thread_op * op)
564 struct login_param * param;
565 struct login_result * result;
566 int r;
567 #ifdef DISABLE_LOG_DURING_LOGIN
568 int old_debug;
569 #endif
571 param = op->param;
572 result = op->result;
574 CHECK_NNTP();
576 #ifdef DISABLE_LOG_DURING_LOGIN
577 old_debug = mailstream_debug;
578 mailstream_debug = 0;
579 #endif
581 r = newsnntp_authinfo_username(param->nntp, param->login);
582 /* libetpan returning NO_ERROR means it received resp.code 281:
583 in this case auth. is already successful, no password is needed. */
584 if (r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
585 r = newsnntp_authinfo_password(param->nntp, param->password);
590 #ifdef DISABLE_LOG_DURING_LOGIN
591 mailstream_debug = old_debug;
592 #endif
594 result->error = r;
595 if (param->nntp->nntp_response)
596 nntp_logger(0, param->nntp->nntp_response, strlen(param->nntp->nntp_response));
598 debug_print("nntp login run - end %i\n", r);
601 int nntp_threaded_login(Folder * folder, const char * login, const char * password)
603 struct login_param param;
604 struct login_result result;
606 debug_print("nntp login - begin\n");
608 param.nntp = get_nntp(folder);
609 param.login = login;
610 param.password = password;
612 threaded_run(folder, &param, &result, login_run);
614 debug_print("nntp login - end\n");
616 return result.error;
619 struct date_param {
620 newsnntp * nntp;
621 struct tm * lt;
624 struct date_result {
625 int error;
628 static void date_run(struct etpan_thread_op * op)
630 struct date_param * param;
631 struct date_result * result;
632 int r;
634 param = op->param;
635 result = op->result;
637 CHECK_NNTP();
639 r = newsnntp_date(param->nntp, param->lt);
641 result->error = r;
642 debug_print("nntp date run - end %i\n", r);
645 int nntp_threaded_date(Folder * folder, struct tm *lt)
647 struct date_param param;
648 struct date_result result;
650 debug_print("nntp date - begin\n");
652 param.nntp = get_nntp(folder);
653 param.lt = lt;
655 threaded_run(folder, &param, &result, date_run);
657 debug_print("nntp date - end\n");
659 return result.error;
662 struct list_param {
663 newsnntp * nntp;
664 clist **grouplist;
667 struct list_result {
668 int error;
671 static void list_run(struct etpan_thread_op * op)
673 struct list_param * param;
674 struct list_result * result;
675 int r;
677 param = op->param;
678 result = op->result;
680 CHECK_NNTP();
682 r = newsnntp_list(param->nntp, param->grouplist);
684 result->error = r;
685 debug_print("nntp list run - end %i\n", r);
688 int nntp_threaded_list(Folder * folder, clist **grouplist)
690 struct list_param param;
691 struct list_result result;
693 debug_print("nntp list - begin\n");
695 param.nntp = get_nntp(folder);
696 param.grouplist = grouplist;
698 threaded_run(folder, &param, &result, list_run);
700 debug_print("nntp list - end\n");
702 return result.error;
705 struct post_param {
706 newsnntp * nntp;
707 char *contents;
708 size_t len;
711 struct post_result {
712 int error;
715 static void post_run(struct etpan_thread_op * op)
717 struct post_param * param;
718 struct post_result * result;
719 int r;
721 param = op->param;
722 result = op->result;
724 CHECK_NNTP();
726 r = newsnntp_post(param->nntp, param->contents, param->len);
728 result->error = r;
729 debug_print("nntp post run - end %i\n", r);
732 int nntp_threaded_post(Folder * folder, char *contents, size_t len)
734 struct post_param param;
735 struct post_result result;
737 debug_print("nntp post - begin\n");
739 param.nntp = get_nntp(folder);
740 param.contents = contents;
741 param.len = len;
743 threaded_run(folder, &param, &result, post_run);
745 debug_print("nntp post - end\n");
747 return result.error;
750 struct article_param {
751 newsnntp * nntp;
752 guint32 num;
753 char **contents;
754 size_t *len;
757 struct article_result {
758 int error;
761 static void article_run(struct etpan_thread_op * op)
763 struct article_param * param;
764 struct article_result * result;
765 int r;
767 param = op->param;
768 result = op->result;
770 CHECK_NNTP();
772 r = newsnntp_article(param->nntp, param->num, param->contents, param->len);
774 result->error = r;
775 debug_print("nntp article run - end %i\n", r);
778 int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t *len)
780 struct article_param param;
781 struct article_result result;
783 debug_print("nntp article - begin\n");
785 param.nntp = get_nntp(folder);
786 param.num = num;
787 param.contents = contents;
788 param.len = len;
790 threaded_run(folder, &param, &result, article_run);
792 debug_print("nntp article - end\n");
794 return result.error;
797 struct group_param {
798 newsnntp * nntp;
799 const char *group;
800 struct newsnntp_group_info **info;
803 struct group_result {
804 int error;
807 static void group_run(struct etpan_thread_op * op)
809 struct group_param * param;
810 struct group_result * result;
811 int r;
813 param = op->param;
814 result = op->result;
816 CHECK_NNTP();
818 r = newsnntp_group(param->nntp, param->group, param->info);
820 result->error = r;
821 debug_print("nntp group run - end %i\n", r);
824 int nntp_threaded_group(Folder * folder, const char *group, struct newsnntp_group_info **info)
826 struct group_param param;
827 struct group_result result;
829 debug_print("nntp group - begin\n");
831 param.nntp = get_nntp(folder);
832 param.group = group;
833 param.info = info;
835 threaded_run(folder, &param, &result, group_run);
837 debug_print("nntp group - end\n");
839 return result.error;
842 struct mode_reader_param {
843 newsnntp * nntp;
846 struct mode_reader_result {
847 int error;
850 static void mode_reader_run(struct etpan_thread_op * op)
852 struct mode_reader_param * param;
853 struct mode_reader_result * result;
854 int r;
856 param = op->param;
857 result = op->result;
859 CHECK_NNTP();
861 r = newsnntp_mode_reader(param->nntp);
863 result->error = r;
864 debug_print("nntp mode_reader run - end %i\n", r);
867 int nntp_threaded_mode_reader(Folder * folder)
869 struct mode_reader_param param;
870 struct mode_reader_result result;
872 debug_print("nntp mode_reader - begin\n");
874 param.nntp = get_nntp(folder);
876 threaded_run(folder, &param, &result, mode_reader_run);
878 debug_print("nntp mode_reader - end\n");
880 return result.error;
883 struct xover_param {
884 newsnntp * nntp;
885 guint32 beg;
886 guint32 end;
887 struct newsnntp_xover_resp_item **result;
888 clist **msglist;
891 struct xover_result {
892 int error;
895 static void xover_run(struct etpan_thread_op * op)
897 struct xover_param * param;
898 struct xover_result * result;
899 int r;
901 param = op->param;
902 result = op->result;
904 CHECK_NNTP();
906 if (param->result) {
907 r = newsnntp_xover_single(param->nntp, param->beg, param->result);
908 } else {
909 r = newsnntp_xover_range(param->nntp, param->beg, param->end, param->msglist);
912 result->error = r;
913 debug_print("nntp xover run %d-%d - end %i\n",
914 param->beg, param->end, r);
917 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
919 struct xover_param param;
920 struct xover_result result;
921 clist *l = NULL, *h = NULL;
922 guint32 cbeg = 0, cend = 0;
924 debug_print("nntp xover - begin (%d-%d)\n", beg, end);
926 h = clist_new();
928 /* Request the overview in batches of NNTP_BATCH_SIZE, to prevent
929 * long stalls or libetpan choking on too large server response,
930 * and to allow updating any progress indicators while we work. */
931 cbeg = beg;
932 while (cbeg <= end && cend <= end) {
933 cend = cbeg + (NNTP_BATCH_SIZE - 1);
934 if (cend > end)
935 cend = end;
937 statusbar_progress_all(cbeg - beg, end - beg, 1);
938 GTK_EVENTS_FLUSH();
940 param.nntp = get_nntp(folder);
941 param.beg = cbeg;
942 param.end = cend;
943 param.result = single_result;
944 param.msglist = &l;
946 threaded_run(folder, &param, &result, xover_run);
948 /* Handle errors */
949 if (result.error != NEWSNNTP_NO_ERROR) {
950 log_warning(LOG_PROTOCOL, _("couldn't get xover range\n"));
951 debug_print("couldn't get xover for %d-%d\n", cbeg, cend);
952 if (l != NULL)
953 newsnntp_xover_resp_list_free(l);
954 newsnntp_xover_resp_list_free(h);
955 statusbar_progress_all(0, 0, 0);
956 return result.error;
959 /* Append the new data (l) to list of results (h). */
960 if (l != NULL) {
961 debug_print("total items so far %d, items this batch %d\n",
962 clist_count(h), clist_count(l));
963 clist_concat(h, l);
964 clist_free(l);
965 l = NULL;
968 cbeg += NNTP_BATCH_SIZE;
971 statusbar_progress_all(0, 0, 0);
973 debug_print("nntp xover - end\n");
975 *multiple_result = h;
977 return result.error;
980 struct xhdr_param {
981 newsnntp * nntp;
982 const char *header;
983 guint32 beg;
984 guint32 end;
985 clist **hdrlist;
988 struct xhdr_result {
989 int error;
992 static void xhdr_run(struct etpan_thread_op * op)
994 struct xhdr_param * param;
995 struct xhdr_result * result;
996 int r;
998 param = op->param;
999 result = op->result;
1001 CHECK_NNTP();
1003 if (param->beg == param->end) {
1004 r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
1005 } else {
1006 r = newsnntp_xhdr_range(param->nntp, param->header, param->beg, param->end, param->hdrlist);
1009 result->error = r;
1010 debug_print("nntp xhdr '%s %d-%d' run - end %i\n",
1011 param->header, param->beg, param->end, r);
1014 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
1016 struct xhdr_param param;
1017 struct xhdr_result result;
1018 clist *l = NULL;
1019 clist *h = *hdrlist;
1020 guint32 cbeg = 0, cend = 0;
1022 debug_print("nntp xhdr %s - begin (%d-%d)\n", header, beg, end);
1024 if (h == NULL)
1025 h = clist_new();
1027 /* Request the headers in batches of NNTP_BATCH_SIZE, to prevent
1028 * long stalls or libetpan choking on too large server response,
1029 * and to allow updating any progress indicators while we work. */
1030 cbeg = beg;
1031 while (cbeg <= end && cend <= end) {
1032 cend = cbeg + NNTP_BATCH_SIZE - 1;
1033 if (cend > end)
1034 cend = end;
1036 statusbar_progress_all(cbeg - beg, end - beg, 1);
1037 GTK_EVENTS_FLUSH();
1039 param.nntp = get_nntp(folder);
1040 param.header = header;
1041 param.beg = cbeg;
1042 param.end = cend;
1043 param.hdrlist = &l;
1045 threaded_run(folder, &param, &result, xhdr_run);
1047 /* Handle errors */
1048 if (result.error != NEWSNNTP_NO_ERROR) {
1049 log_warning(LOG_PROTOCOL, _("couldn't get xhdr range\n"));
1050 debug_print("couldn't get xhdr %s %d-%d\n", header, cbeg, cend);
1051 if (l != NULL)
1052 newsnntp_xhdr_free(l);
1053 newsnntp_xhdr_free(h);
1054 statusbar_progress_all(0, 0, 0);
1055 return result.error;
1058 /* Append the new data (l) to list of results (h). */
1059 if (l != NULL) {
1060 debug_print("total items so far %d, items this batch %d\n",
1061 clist_count(h), clist_count(l));
1062 clist_concat(h, l);
1063 clist_free(l);
1064 l = NULL;
1067 cbeg += NNTP_BATCH_SIZE;
1070 statusbar_progress_all(0, 0, 0);
1072 debug_print("nntp xhdr %s - end (%d-%d)\n", header, beg, end);
1074 *hdrlist = h;
1076 return result.error;
1079 void nntp_main_set_timeout(int sec)
1081 mailstream_network_delay.tv_sec = sec;
1082 mailstream_network_delay.tv_usec = 0;
1085 #else
1087 void nntp_main_init(void)
1090 void nntp_main_done(gboolean have_connectivity)
1093 void nntp_main_set_timeout(int sec)
1097 void nntp_threaded_cancel(Folder * folder);
1101 #endif