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