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