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