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