Fetch XOVER and XHDR data in batches of 5000.
[claws.git] / src / etpan / nntp-thread.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2016 DINH Viet Hoa and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #ifdef HAVE_LIBETPAN
25
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
52 #define DISABLE_LOG_DURING_LOGIN
53
54 #define NNTP_BATCH_SIZE 4999
55
56 static struct etpan_thread_manager * thread_manager = NULL;
57 static chash * nntp_hash = NULL;
58 static chash * session_hash = NULL;
59 static guint thread_manager_signal = 0;
60 static GIOChannel * io_channel = NULL;
61
62 static void nntp_logger(int direction, const char * str, size_t size) 
63 {
64         gchar *buf;
65         gchar **lines;
66         int i = 0;
67
68         if (size > 256) {
69                 log_print(LOG_PROTOCOL, "NNTP%c [data - %zd bytes]\n", direction?'>':'<', size);
70                 return;
71         }
72         buf = malloc(size+1);
73         memset(buf, 0, size+1);
74         strncpy(buf, str, size);
75         buf[size] = '\0';
76
77         if (!strncmp(buf, "<<<<<<<", 7) 
78         ||  !strncmp(buf, ">>>>>>>", 7)) {
79                 free(buf);
80                 return;
81         }
82         while (strstr(buf, "\r"))
83                 *strstr(buf, "\r") = ' ';
84         while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
85                 buf[strlen(buf)-1] = '\0';
86
87         lines = g_strsplit(buf, "\n", -1);
88
89         while (lines[i] && *lines[i]) {
90                 log_print(LOG_PROTOCOL, "NNTP%c %s\n", direction?'>':'<', lines[i]);
91                 i++;
92         }
93         g_strfreev(lines);
94         free(buf);
95 }
96
97 static void delete_nntp(Folder *folder, newsnntp *nntp)
98 {
99         chashdatum key;
100
101         key.data = &folder;
102         key.len = sizeof(folder);
103         chash_delete(session_hash, &key, NULL);
104         
105         key.data = &nntp;
106         key.len = sizeof(nntp);
107         if (nntp && nntp->nntp_stream) {
108                 /* we don't want libetpan to logout */
109                 mailstream_close(nntp->nntp_stream);
110                 nntp->nntp_stream = NULL;
111         }
112         debug_print("removing newsnntp %p\n", nntp);
113         newsnntp_free(nntp);    
114 }
115
116 static gboolean thread_manager_event(GIOChannel * source,
117     GIOCondition condition,
118     gpointer data)
119 {
120 #ifdef G_OS_WIN32
121         gsize bytes_read;
122         gchar ch;
123         
124         if (condition & G_IO_IN)
125                 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
126 #endif
127         etpan_thread_manager_loop(thread_manager);
128         
129         return TRUE;
130 }
131
132 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
133 extern gboolean etpan_skip_ssl_cert_check;
134
135 void nntp_main_init(gboolean skip_ssl_cert_check)
136 {
137         int fd_thread_manager;
138         
139         etpan_skip_ssl_cert_check = skip_ssl_cert_check;
140         
141         nntp_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
142         session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
143         
144         thread_manager = etpan_thread_manager_new();
145         
146         fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
147         
148 #ifndef G_OS_WIN32
149         io_channel = g_io_channel_unix_new(fd_thread_manager);
150 #else
151         io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
152 #endif
153         
154         thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
155                                                     thread_manager_event,
156                                                     (gpointer) NULL,
157                                                     NULL);
158 }
159
160 void nntp_main_done(gboolean have_connectivity)
161 {
162         nntp_disconnect_all(have_connectivity);
163         etpan_thread_manager_stop(thread_manager);
164 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
165         return;
166 #endif
167         etpan_thread_manager_join(thread_manager);
168         
169         g_source_remove(thread_manager_signal);
170         g_io_channel_unref(io_channel);
171         
172         etpan_thread_manager_free(thread_manager);
173         
174         chash_free(session_hash);
175         chash_free(nntp_hash);
176 }
177
178 void nntp_init(Folder * folder)
179 {
180         struct etpan_thread * thread;
181         chashdatum key;
182         chashdatum value;
183         
184         thread = etpan_thread_manager_get_thread(thread_manager);
185         
186         key.data = &folder;
187         key.len = sizeof(folder);
188         value.data = thread;
189         value.len = 0;
190         
191         chash_set(nntp_hash, &key, &value, NULL);
192 }
193
194 void nntp_done(Folder * folder)
195 {
196         struct etpan_thread * thread;
197         chashdatum key;
198         chashdatum value;
199         int r;
200         
201         key.data = &folder;
202         key.len = sizeof(folder);
203         
204         r = chash_get(nntp_hash, &key, &value);
205         if (r < 0)
206                 return;
207         
208         thread = value.data;
209         
210         etpan_thread_unbind(thread);
211         
212         chash_delete(nntp_hash, &key, NULL);
213         
214         debug_print("remove thread\n");
215 }
216
217 static struct etpan_thread * get_thread(Folder * folder)
218 {
219         struct etpan_thread * thread;
220         chashdatum key;
221         chashdatum value;
222         int r;
223
224         key.data = &folder;
225         key.len = sizeof(folder);
226
227         r = chash_get(nntp_hash, &key, &value);
228         if (r < 0)
229                 return NULL;
230
231         thread = value.data;
232
233         return thread;
234 }
235
236 static newsnntp * get_nntp(Folder * folder)
237 {
238         newsnntp * nntp;
239         chashdatum key;
240         chashdatum value;
241         int r;
242         
243         key.data = &folder;
244         key.len = sizeof(folder);
245         
246         r = chash_get(session_hash, &key, &value);
247         if (r < 0)
248                 return NULL;
249         
250         nntp = value.data;
251         debug_print("found nntp %p\n", nntp);
252         return nntp;
253 }
254
255
256 static void generic_cb(int cancelled, void * result, void * callback_data)
257 {
258         struct etpan_thread_op * op;
259         
260         op = (struct etpan_thread_op *) callback_data;
261
262         debug_print("generic_cb\n");
263         op->finished = 1;
264 }
265
266 static void threaded_run(Folder * folder, void * param, void * result,
267                          void (* func)(struct etpan_thread_op * ))
268 {
269         struct etpan_thread_op * op;
270         struct etpan_thread * thread;
271         void (*previous_stream_logger)(int direction,
272                 const char * str, size_t size);
273
274         nntp_folder_ref(folder);
275
276         op = etpan_thread_op_new();
277         
278         op->nntp = get_nntp(folder);
279         op->param = param;
280         op->result = result;
281
282         op->run = func;
283         op->callback = generic_cb;
284         op->callback_data = op;
285         
286         previous_stream_logger = mailstream_logger;
287         mailstream_logger = nntp_logger;
288
289         thread = get_thread(folder);
290         etpan_thread_op_schedule(thread, op);
291         
292         while (!op->finished) {
293                 gtk_main_iteration();
294         }
295         
296         mailstream_logger = previous_stream_logger;
297
298         etpan_thread_op_free(op);
299
300         nntp_folder_unref(folder);
301 }
302
303
304 /* connect */
305
306 struct connect_param {
307         newsnntp * nntp;
308         PrefsAccount *account;
309         const char * server;
310         int port;
311 };
312
313 struct connect_result {
314         int error;
315 };
316
317 #define CHECK_NNTP() {                                          \
318         if (!param->nntp) {                                     \
319                 result->error = NEWSNNTP_ERROR_BAD_STATE;       \
320                 return;                                         \
321         }                                                       \
322 }
323
324 static void connect_run(struct etpan_thread_op * op)
325 {
326         int r;
327         struct connect_param * param;
328         struct connect_result * result;
329         
330         param = op->param;
331         result = op->result;
332         
333         CHECK_NNTP();
334
335         r = newsnntp_socket_connect(param->nntp,
336                                     param->server, param->port);
337         
338         result->error = r;
339 }
340
341
342 int nntp_threaded_connect(Folder * folder, const char * server, int port)
343 {
344         struct connect_param param;
345         struct connect_result result;
346         chashdatum key;
347         chashdatum value;
348         newsnntp * nntp, * oldnntp;
349         
350         oldnntp = get_nntp(folder);
351
352         nntp = newsnntp_new(0, NULL);
353         
354         if (oldnntp) {
355                 debug_print("deleting old nntp %p\n", oldnntp);
356                 delete_nntp(folder, oldnntp);
357         }
358         
359         key.data = &folder;
360         key.len = sizeof(folder);
361         value.data = nntp;
362         value.len = 0;
363         chash_set(session_hash, &key, &value, NULL);
364         
365         param.nntp = nntp;
366         param.server = server;
367         param.port = port;
368         
369         refresh_resolvers();
370         threaded_run(folder, &param, &result, connect_run);
371         
372         debug_print("connect ok %i with nntp %p\n", result.error, nntp);
373         
374         return result.error;
375 }
376 #ifdef USE_GNUTLS
377 static void connect_ssl_run(struct etpan_thread_op * op)
378 {
379         int r;
380         struct connect_param * param;
381         struct connect_result * result;
382         
383         param = op->param;
384         result = op->result;
385         
386         CHECK_NNTP();
387
388         r = newsnntp_ssl_connect_with_callback(param->nntp,
389                                  param->server, param->port,
390                                  etpan_connect_ssl_context_cb, param->account);
391         result->error = r;
392 }
393
394 int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
395 {
396         struct connect_param param;
397         struct connect_result result;
398         chashdatum key;
399         chashdatum value;
400         newsnntp * nntp, * oldnntp;
401         gboolean accept_if_valid = FALSE;
402
403         oldnntp = get_nntp(folder);
404
405         nntp = newsnntp_new(0, NULL);
406
407         if (oldnntp) {
408                 debug_print("deleting old nntp %p\n", oldnntp);
409                 delete_nntp(folder, oldnntp);
410         }
411
412         key.data = &folder;
413         key.len = sizeof(folder);
414         value.data = nntp;
415         value.len = 0;
416         chash_set(session_hash, &key, &value, NULL);
417
418         param.nntp = nntp;
419         param.server = server;
420         param.port = port;
421         param.account = folder->account;
422
423         if (folder->account)
424                 accept_if_valid = folder->account->ssl_certs_auto_accept;
425
426         refresh_resolvers();
427         threaded_run(folder, &param, &result, connect_ssl_run);
428
429         if (result.error == NEWSNNTP_NO_ERROR && !etpan_skip_ssl_cert_check) {
430                 if (etpan_certificate_check(nntp->nntp_stream, server, port,
431                                             accept_if_valid) != TRUE)
432                         return -1;
433         }
434         debug_print("connect %d with nntp %p\n", result.error, nntp);
435         
436         return result.error;
437 }
438 #endif
439
440 void nntp_threaded_disconnect(Folder * folder)
441 {
442         newsnntp * nntp;
443         
444         nntp = get_nntp(folder);
445         if (nntp == NULL) {
446                 debug_print("was disconnected\n");
447                 return;
448         }
449         
450         debug_print("deleting old nntp %p\n", nntp);
451         delete_nntp(folder, nntp);
452         
453         debug_print("disconnect ok\n");
454 }
455
456 void nntp_threaded_cancel(Folder * folder)
457 {
458         newsnntp * nntp;
459         
460         nntp = get_nntp(folder);
461         if (nntp->nntp_stream != NULL)
462                 mailstream_cancel(nntp->nntp_stream);
463 }
464
465
466 struct login_param {
467         newsnntp * nntp;
468         const char * login;
469         const char * password;
470 };
471
472 struct login_result {
473         int error;
474 };
475
476 static void login_run(struct etpan_thread_op * op)
477 {
478         struct login_param * param;
479         struct login_result * result;
480         int r;
481 #ifdef DISABLE_LOG_DURING_LOGIN
482         int old_debug;
483 #endif
484         
485         param = op->param;
486         result = op->result;
487
488         CHECK_NNTP();
489
490 #ifdef DISABLE_LOG_DURING_LOGIN
491         old_debug = mailstream_debug;
492         mailstream_debug = 0;
493 #endif
494
495         r = newsnntp_authinfo_username(param->nntp, param->login);
496         /* libetpan returning NO_ERROR means it received resp.code 281:
497            in this case auth. is already successful, no password is needed. */
498         if (r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
499                 r = newsnntp_authinfo_password(param->nntp, param->password);
500         }
501         
502
503
504 #ifdef DISABLE_LOG_DURING_LOGIN
505         mailstream_debug = old_debug;
506 #endif
507         
508         result->error = r;
509         if (param->nntp->nntp_response)
510                 nntp_logger(0, param->nntp->nntp_response, strlen(param->nntp->nntp_response));
511
512         debug_print("nntp login run - end %i\n", r);
513 }
514
515 int nntp_threaded_login(Folder * folder, const char * login, const char * password)
516 {
517         struct login_param param;
518         struct login_result result;
519         
520         debug_print("nntp login - begin\n");
521         
522         param.nntp = get_nntp(folder);
523         param.login = login;
524         param.password = password;
525
526         threaded_run(folder, &param, &result, login_run);
527         
528         debug_print("nntp login - end\n");
529         
530         return result.error;
531 }
532
533 struct date_param {
534         newsnntp * nntp;
535         struct tm * lt;
536 };
537
538 struct date_result {
539         int error;
540 };
541
542 static void date_run(struct etpan_thread_op * op)
543 {
544         struct date_param * param;
545         struct date_result * result;
546         int r;
547         
548         param = op->param;
549         result = op->result;
550
551         CHECK_NNTP();
552
553         r = newsnntp_date(param->nntp, param->lt);
554         
555         result->error = r;
556         debug_print("nntp date run - end %i\n", r);
557 }
558
559 int nntp_threaded_date(Folder * folder, struct tm *lt)
560 {
561         struct date_param param;
562         struct date_result result;
563         
564         debug_print("nntp date - begin\n");
565         
566         param.nntp = get_nntp(folder);
567         param.lt = lt;
568
569         threaded_run(folder, &param, &result, date_run);
570         
571         debug_print("nntp date - end\n");
572         
573         return result.error;
574 }
575
576 struct list_param {
577         newsnntp * nntp;
578         clist **grouplist;
579 };
580
581 struct list_result {
582         int error;
583 };
584
585 static void list_run(struct etpan_thread_op * op)
586 {
587         struct list_param * param;
588         struct list_result * result;
589         int r;
590         
591         param = op->param;
592         result = op->result;
593
594         CHECK_NNTP();
595
596         r = newsnntp_list(param->nntp, param->grouplist);
597         
598         result->error = r;
599         debug_print("nntp list run - end %i\n", r);
600 }
601
602 int nntp_threaded_list(Folder * folder, clist **grouplist)
603 {
604         struct list_param param;
605         struct list_result result;
606         
607         debug_print("nntp list - begin\n");
608         
609         param.nntp = get_nntp(folder);
610         param.grouplist = grouplist;
611
612         threaded_run(folder, &param, &result, list_run);
613         
614         debug_print("nntp list - end\n");
615         
616         return result.error;
617 }
618
619 struct post_param {
620         newsnntp * nntp;
621         char *contents;
622         size_t len;
623 };
624
625 struct post_result {
626         int error;
627 };
628
629 static void post_run(struct etpan_thread_op * op)
630 {
631         struct post_param * param;
632         struct post_result * result;
633         int r;
634         
635         param = op->param;
636         result = op->result;
637
638         CHECK_NNTP();
639
640         r = newsnntp_post(param->nntp, param->contents, param->len);
641         
642         result->error = r;
643         debug_print("nntp post run - end %i\n", r);
644 }
645
646 int nntp_threaded_post(Folder * folder, char *contents, size_t len)
647 {
648         struct post_param param;
649         struct post_result result;
650         
651         debug_print("nntp post - begin\n");
652         
653         param.nntp = get_nntp(folder);
654         param.contents = contents;
655         param.len = len;
656
657         threaded_run(folder, &param, &result, post_run);
658         
659         debug_print("nntp post - end\n");
660         
661         return result.error;
662 }
663
664 struct article_param {
665         newsnntp * nntp;
666         guint32 num;
667         char **contents;
668         size_t *len;
669 };
670
671 struct article_result {
672         int error;
673 };
674
675 static void article_run(struct etpan_thread_op * op)
676 {
677         struct article_param * param;
678         struct article_result * result;
679         int r;
680         
681         param = op->param;
682         result = op->result;
683
684         CHECK_NNTP();
685
686         r = newsnntp_article(param->nntp, param->num, param->contents, param->len);
687         
688         result->error = r;
689         debug_print("nntp article run - end %i\n", r);
690 }
691
692 int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t *len)
693 {
694         struct article_param param;
695         struct article_result result;
696         
697         debug_print("nntp article - begin\n");
698         
699         param.nntp = get_nntp(folder);
700         param.num = num;
701         param.contents = contents;
702         param.len = len;
703
704         threaded_run(folder, &param, &result, article_run);
705         
706         debug_print("nntp article - end\n");
707         
708         return result.error;
709 }
710
711 struct group_param {
712         newsnntp * nntp;
713         const char *group;
714         struct newsnntp_group_info **info;
715 };
716
717 struct group_result {
718         int error;
719 };
720
721 static void group_run(struct etpan_thread_op * op)
722 {
723         struct group_param * param;
724         struct group_result * result;
725         int r;
726         
727         param = op->param;
728         result = op->result;
729
730         CHECK_NNTP();
731
732         r = newsnntp_group(param->nntp, param->group, param->info);
733         
734         result->error = r;
735         debug_print("nntp group run - end %i\n", r);
736 }
737
738 int nntp_threaded_group(Folder * folder, const char *group, struct newsnntp_group_info **info)
739 {
740         struct group_param param;
741         struct group_result result;
742         
743         debug_print("nntp group - begin\n");
744         
745         param.nntp = get_nntp(folder);
746         param.group = group;
747         param.info = info;
748
749         threaded_run(folder, &param, &result, group_run);
750         
751         debug_print("nntp group - end\n");
752         
753         return result.error;
754 }
755
756 struct mode_reader_param {
757         newsnntp * nntp;
758 };
759
760 struct mode_reader_result {
761         int error;
762 };
763
764 static void mode_reader_run(struct etpan_thread_op * op)
765 {
766         struct mode_reader_param * param;
767         struct mode_reader_result * result;
768         int r;
769         
770         param = op->param;
771         result = op->result;
772
773         CHECK_NNTP();
774
775         r = newsnntp_mode_reader(param->nntp);
776         
777         result->error = r;
778         debug_print("nntp mode_reader run - end %i\n", r);
779 }
780
781 int nntp_threaded_mode_reader(Folder * folder)
782 {
783         struct mode_reader_param param;
784         struct mode_reader_result result;
785         
786         debug_print("nntp mode_reader - begin\n");
787         
788         param.nntp = get_nntp(folder);
789
790         threaded_run(folder, &param, &result, mode_reader_run);
791         
792         debug_print("nntp mode_reader - end\n");
793         
794         return result.error;
795 }
796
797 struct xover_param {
798         newsnntp * nntp;
799         guint32 beg;
800         guint32 end;
801         struct newsnntp_xover_resp_item **result;
802         clist **msglist;
803 };
804
805 struct xover_result {
806         int error;
807 };
808
809 static void xover_run(struct etpan_thread_op * op)
810 {
811         struct xover_param * param;
812         struct xover_result * result;
813         int r;
814         
815         param = op->param;
816         result = op->result;
817
818         CHECK_NNTP();
819         
820         if (param->result) {
821                 r = newsnntp_xover_single(param->nntp, param->beg, param->result);
822         } else {
823                 r = newsnntp_xover_range(param->nntp, param->beg, param->end, param->msglist);
824         }
825         
826         result->error = r;
827         debug_print("nntp xover run %d-%d - end %i\n",
828                         param->beg, param->end, r);
829 }
830
831 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
832 {
833         struct xover_param param;
834         struct xover_result result;
835         clist *l = NULL, *h = NULL;
836         guint32 cbeg = 0, cend = 0;
837
838         debug_print("nntp xover - begin (%d-%d)\n", beg, end);
839
840         h = clist_new();
841
842         /* Request the overview in batches of NNTP_BATCH_SIZE, to prevent
843          * long stalls or libetpan choking on too large server response,
844          * and to allow updating any progress indicators while we work. */
845         cbeg = beg;
846         while (cbeg <= end && cend <= end) {
847                 cend = cbeg + NNTP_BATCH_SIZE;
848                 if (cend > end)
849                         cend = end;
850
851                 param.nntp = get_nntp(folder);
852                 param.beg = cbeg;
853                 param.end = cend;
854                 param.result = single_result;
855                 param.msglist = &l;
856
857                 threaded_run(folder, &param, &result, xover_run);
858
859                 /* Handle errors */
860                 if (result.error != NEWSNNTP_NO_ERROR) {
861                         log_warning(LOG_PROTOCOL, _("couldn't get xover range\n"));
862                         debug_print("couldn't get xover for %d-%d\n", cbeg, cend);
863                         if (l != NULL)
864                                 clist_free(l);
865                         return result.error;
866                 }
867
868                 /* Append the new data (l) to list of results (h). */
869                 if (l != NULL) {
870                         debug_print("total items so far %d, items this batch %d\n",
871                                         clist_count(h), clist_count(l));
872                         clist_concat(h, l);
873                         l = NULL;
874                 }
875
876                 cbeg += NNTP_BATCH_SIZE + 1;
877         }
878         
879         debug_print("nntp xover - end\n");
880
881         *multiple_result = h;
882
883         return result.error;
884 }
885
886 struct xhdr_param {
887         newsnntp * nntp;
888         const char *header;
889         guint32 beg;
890         guint32 end;
891         clist **hdrlist;
892 };
893
894 struct xhdr_result {
895         int error;
896 };
897
898 static void xhdr_run(struct etpan_thread_op * op)
899 {
900         struct xhdr_param * param;
901         struct xhdr_result * result;
902         int r;
903         
904         param = op->param;
905         result = op->result;
906
907         CHECK_NNTP();
908         
909         if (param->beg == param->end) {
910                 r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
911         } else {
912                 r = newsnntp_xhdr_range(param->nntp, param->header, param->beg, param->end, param->hdrlist);
913         }
914         
915         result->error = r;
916         debug_print("nntp xhdr '%s %d-%d' run - end %i\n",
917                         param->header, param->beg, param->end, r);
918 }
919
920 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
921 {
922         struct xhdr_param param;
923         struct xhdr_result result;
924         clist *l = NULL;
925         clist *h = *hdrlist;
926         guint32 cbeg = 0, cend = 0;
927
928         debug_print("nntp xhdr %s - begin (%d-%d)\n", header, beg, end);
929
930         if (h == NULL)
931                 h = clist_new();
932
933         /* Request the headers in batches of NNTP_BATCH_SIZE, to prevent
934          * long stalls or libetpan choking on too large server response,
935          * and to allow updating any progress indicators while we work. */
936         cbeg = beg;
937         while (cbeg <= end && cend <= end) {
938                 cend = cbeg + NNTP_BATCH_SIZE;
939                 if (cend > end)
940                         cend = end;
941
942                 param.nntp = get_nntp(folder);
943                 param.header = header;
944                 param.beg = cbeg;
945                 param.end = cend;
946                 param.hdrlist = &l;
947
948                 threaded_run(folder, &param, &result, xhdr_run);
949
950                 /* Handle errors */
951                 if (result.error != NEWSNNTP_NO_ERROR) {
952                         log_warning(LOG_PROTOCOL, _("couldn't get xhdr range\n"));
953                         debug_print("couldn't get xhdr %s %d-%d\n",     header, cbeg, cend);
954                         if (l != NULL)
955                                 clist_free(l);
956                         return result.error;
957                 }
958
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                         l = NULL;
965                 }
966
967                 cbeg += NNTP_BATCH_SIZE + 1;
968         }
969         
970         debug_print("nntp xhdr %s - end (%d-%d)\n", header, beg, end);
971
972         *hdrlist = h;
973
974         return result.error;
975 }
976
977 void nntp_main_set_timeout(int sec)
978 {
979         mailstream_network_delay.tv_sec = sec;
980         mailstream_network_delay.tv_usec = 0;
981 }
982
983 #else
984
985 void nntp_main_init(void)
986 {
987 }
988 void nntp_main_done(gboolean have_connectivity)
989 {
990 }
991 void nntp_main_set_timeout(int sec)
992 {
993 }
994
995 void nntp_threaded_cancel(Folder * folder);
996 {
997 }
998
999 #endif