0aa67c19f0bec07ad39a09ebadda206c4fc58792
[claws.git] / src / etpan / nntp-thread.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005-2011 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 "nntp-thread.h"
27 #include "news.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
31 #include <sys/socket.h>
32 #endif
33 #include <fcntl.h>
34 #ifndef G_OS_WIN32
35 #include <sys/mman.h>
36 #include <sys/wait.h>
37 #endif
38 #include <gtk/gtk.h>
39 #include <log.h>
40 #include "etpan-thread-manager.h"
41 #include "utils.h"
42 #include "mainwindow.h"
43 #include "ssl_certificate.h"
44 #include "socket.h"
45 #include "remotefolder.h"
46
47 #define DISABLE_LOG_DURING_LOGIN
48
49 static struct etpan_thread_manager * thread_manager = NULL;
50 static chash * nntp_hash = NULL;
51 static chash * session_hash = NULL;
52 static guint thread_manager_signal = 0;
53 static GIOChannel * io_channel = NULL;
54
55 static void (*previous_stream_logger)(int direction,
56     const char * str, size_t size);
57
58 static void nntp_logger(int direction, const char * str, size_t size) 
59 {
60         gchar *buf;
61         gchar **lines;
62         int i = 0;
63
64         if (size > 256) {
65                 log_print(LOG_PROTOCOL, "NNTP%c [data - %zd bytes]\n", direction?'>':'<', size);
66                 return;
67         }
68         buf = malloc(size+1);
69         memset(buf, 0, size+1);
70         strncpy(buf, str, size);
71         buf[size] = '\0';
72
73         if (!strncmp(buf, "<<<<<<<", 7) 
74         ||  !strncmp(buf, ">>>>>>>", 7)) {
75                 free(buf);
76                 return;
77         }
78         while (strstr(buf, "\r"))
79                 *strstr(buf, "\r") = ' ';
80         while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
81                 buf[strlen(buf)-1] = '\0';
82
83         lines = g_strsplit(buf, "\n", -1);
84
85         while (lines[i] && *lines[i]) {
86                 log_print(LOG_PROTOCOL, "NNTP%c %s\n", direction?'>':'<', lines[i]);
87                 i++;
88         }
89         g_strfreev(lines);
90         free(buf);
91 }
92
93 static void delete_nntp(Folder *folder, newsnntp *nntp)
94 {
95         chashdatum key;
96         chashdatum value;
97
98         key.data = &folder;
99         key.len = sizeof(folder);
100         value.data = nntp;
101         value.len = 0;
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_OPENSSL
376         struct connect_param *param = (struct connect_param *)data;
377         X509 *cert = NULL;
378         
379         if (certificate == NULL || len < 0) {
380                 g_warning("no cert presented.\n");
381                 return 0;
382         }
383         cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
384         if (cert == NULL) {
385                 g_warning("nntp: can't get cert\n");
386                 return 0;
387         } else if (ssl_certificate_check(cert, NULL,
388                 (gchar *)param->server, (gushort)param->port) == TRUE) {
389                 X509_free(cert);
390                 return 0;
391         } else {
392                 X509_free(cert);
393                 return -1;
394         }
395 #elif USE_GNUTLS
396         struct connect_param *param = (struct connect_param *)data;
397         gnutls_x509_crt cert = NULL;
398         gnutls_datum tmp;
399         
400         if (certificate == NULL || len < 0) {
401                 g_warning("no cert presented.\n");
402                 return 0;
403         }
404         
405         tmp.data = malloc(len);
406         memcpy(tmp.data, certificate, len);
407         tmp.size = len;
408         gnutls_x509_crt_init(&cert);
409         if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
410                 g_warning("nntp: can't get cert\n");
411                 return 0;
412         } else if (ssl_certificate_check(cert, (guint)-1, NULL,
413                 (gchar *)param->server, (gushort)param->port) == TRUE) {
414                 gnutls_x509_crt_deinit(cert);
415                 return 0;
416         } else {
417                 gnutls_x509_crt_deinit(cert);
418                 return -1;
419         }
420 #endif
421         return 0;
422 }
423
424 static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
425 {
426 #if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
427         PrefsAccount *account = (PrefsAccount *)data;
428         const gchar *cert_path = NULL;
429         const gchar *password = NULL;
430 #ifdef USE_OPENSSL
431         X509 *x509 = NULL;
432         EVP_PKEY *pkey = NULL;
433 #else
434         gnutls_x509_crt x509 = NULL;
435         gnutls_x509_privkey pkey = NULL;
436 #endif
437
438         if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
439                 cert_path = account->in_ssl_client_cert_file;
440         if (account->in_ssl_client_cert_pass && *account->in_ssl_client_cert_pass)
441                 password = account->in_ssl_client_cert_pass;
442         
443         if (mailstream_ssl_set_client_certificate_data(ssl_context, NULL, 0) < 0 ||
444             mailstream_ssl_set_client_private_key_data(ssl_context, NULL, 0) < 0)
445                 debug_print("Impossible to set the client certificate.\n");
446         x509 = ssl_certificate_get_x509_from_pem_file(cert_path);
447         pkey = ssl_certificate_get_pkey_from_pem_file(cert_path);
448         if (!(x509 && pkey)) {
449                 /* try pkcs12 format */
450                 ssl_certificate_get_x509_and_pkey_from_p12_file(cert_path, password, &x509, &pkey);
451         }
452         if (x509 && pkey) {
453                 unsigned char *x509_der = NULL, *pkey_der = NULL;
454                 size_t x509_len, pkey_len;
455                 
456 #ifndef USE_GNUTLS
457                 x509_len = (size_t)i2d_X509(x509, &x509_der);
458                 pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
459 #else
460                 x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
461                 pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
462 #endif
463                 if (x509_len > 0 && pkey_len > 0) {
464                         if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
465                             mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0) 
466                                 log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
467                         g_free(x509_der);
468                         g_free(pkey_der);
469                 }
470 #ifdef USE_GNUTLS
471                 gnutls_x509_crt_deinit(x509);
472                 gnutls_x509_privkey_deinit(pkey);
473 #endif
474         }
475 #endif
476 }
477
478 static void connect_ssl_run(struct etpan_thread_op * op)
479 {
480         int r;
481         struct connect_param * param;
482         struct connect_result * result;
483         
484         param = op->param;
485         result = op->result;
486         
487         CHECK_NNTP();
488
489         r = newsnntp_ssl_connect_with_callback(param->nntp,
490                                  param->server, param->port,
491                                  connect_ssl_context_cb, param->account);
492         result->error = r;
493 }
494
495 int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
496 {
497         struct connect_param param;
498         struct connect_result result;
499         chashdatum key;
500         chashdatum value;
501         newsnntp * nntp, * oldnntp;
502         unsigned char *certificate = NULL;
503         int cert_len;
504         
505         oldnntp = get_nntp(folder);
506
507         nntp = newsnntp_new(0, NULL);
508         
509         if (oldnntp) {
510                 debug_print("deleting old nntp %p\n", oldnntp);
511                 delete_nntp(folder, oldnntp);
512         }
513
514         key.data = &folder;
515         key.len = sizeof(folder);
516         value.data = nntp;
517         value.len = 0;
518         chash_set(session_hash, &key, &value, NULL);
519         
520         param.nntp = nntp;
521         param.server = server;
522         param.port = port;
523         param.account = folder->account;
524
525         refresh_resolvers();
526         threaded_run(folder, &param, &result, connect_ssl_run);
527
528         if (result.error == NEWSNNTP_NO_ERROR && !etpan_skip_ssl_cert_check) {
529                 cert_len = (int)mailstream_ssl_get_certificate(nntp->nntp_stream, &certificate);
530                 if (etpan_certificate_check(certificate, cert_len, &param) < 0)
531                         return -1;
532                 if (certificate) 
533                         free(certificate); 
534         }
535         debug_print("connect %d with nntp %p\n", result.error, nntp);
536         
537         return result.error;
538 }
539
540 void nntp_threaded_disconnect(Folder * folder)
541 {
542         newsnntp * nntp;
543         
544         nntp = get_nntp(folder);
545         if (nntp == NULL) {
546                 debug_print("was disconnected\n");
547                 return;
548         }
549         
550         debug_print("deleting old nntp %p\n", nntp);
551         delete_nntp(folder, nntp);
552         
553         debug_print("disconnect ok\n");
554 }
555
556 void nntp_threaded_cancel(Folder * folder)
557 {
558         newsnntp * nntp;
559         
560         nntp = get_nntp(folder);
561         if (nntp->nntp_stream != NULL)
562                 mailstream_cancel(nntp->nntp_stream);
563 }
564
565
566 struct login_param {
567         newsnntp * nntp;
568         const char * login;
569         const char * password;
570 };
571
572 struct login_result {
573         int error;
574 };
575
576 static void login_run(struct etpan_thread_op * op)
577 {
578         struct login_param * param;
579         struct login_result * result;
580         int r;
581 #ifdef DISABLE_LOG_DURING_LOGIN
582         int old_debug;
583 #endif
584         
585         param = op->param;
586         result = op->result;
587
588         CHECK_NNTP();
589
590 #ifdef DISABLE_LOG_DURING_LOGIN
591         old_debug = mailstream_debug;
592         mailstream_debug = 0;
593 #endif
594
595         r = newsnntp_mode_reader(param->nntp);
596         if (r == NEWSNNTP_NO_ERROR ||
597             r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME ||
598             r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
599                 r = newsnntp_authinfo_username(param->nntp, param->login);
600                 if (r == NEWSNNTP_NO_ERROR || 
601                     r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
602                         r = newsnntp_authinfo_password(param->nntp, param->password);
603                 }
604         }
605         
606
607
608 #ifdef DISABLE_LOG_DURING_LOGIN
609         mailstream_debug = old_debug;
610 #endif
611         
612         result->error = r;
613         if (param->nntp->nntp_response)
614                 nntp_logger(0, param->nntp->nntp_response, strlen(param->nntp->nntp_response));
615
616         debug_print("nntp login run - end %i\n", r);
617 }
618
619 int nntp_threaded_login(Folder * folder, const char * login, const char * password)
620 {
621         struct login_param param;
622         struct login_result result;
623         
624         debug_print("nntp login - begin\n");
625         
626         param.nntp = get_nntp(folder);
627         param.login = login;
628         param.password = password;
629
630         threaded_run(folder, &param, &result, login_run);
631         
632         debug_print("nntp login - end\n");
633         
634         return result.error;
635 }
636
637 struct date_param {
638         newsnntp * nntp;
639         struct tm * lt;
640 };
641
642 struct date_result {
643         int error;
644 };
645
646 static void date_run(struct etpan_thread_op * op)
647 {
648         struct date_param * param;
649         struct date_result * result;
650         int r;
651         
652         param = op->param;
653         result = op->result;
654
655         CHECK_NNTP();
656
657         r = newsnntp_date(param->nntp, param->lt);
658         
659         result->error = r;
660         debug_print("nntp date run - end %i\n", r);
661 }
662
663 int nntp_threaded_date(Folder * folder, struct tm *lt)
664 {
665         struct date_param param;
666         struct date_result result;
667         
668         debug_print("nntp date - begin\n");
669         
670         param.nntp = get_nntp(folder);
671         param.lt = lt;
672
673         threaded_run(folder, &param, &result, date_run);
674         
675         debug_print("nntp date - end\n");
676         
677         return result.error;
678 }
679
680 struct list_param {
681         newsnntp * nntp;
682         clist **grouplist;
683 };
684
685 struct list_result {
686         int error;
687 };
688
689 static void list_run(struct etpan_thread_op * op)
690 {
691         struct list_param * param;
692         struct list_result * result;
693         int r;
694         
695         param = op->param;
696         result = op->result;
697
698         CHECK_NNTP();
699
700         r = newsnntp_list(param->nntp, param->grouplist);
701         
702         result->error = r;
703         debug_print("nntp list run - end %i\n", r);
704 }
705
706 int nntp_threaded_list(Folder * folder, clist **grouplist)
707 {
708         struct list_param param;
709         struct list_result result;
710         
711         debug_print("nntp list - begin\n");
712         
713         param.nntp = get_nntp(folder);
714         param.grouplist = grouplist;
715
716         threaded_run(folder, &param, &result, list_run);
717         
718         debug_print("nntp list - end\n");
719         
720         return result.error;
721 }
722
723 struct post_param {
724         newsnntp * nntp;
725         char *contents;
726         size_t len;
727 };
728
729 struct post_result {
730         int error;
731 };
732
733 static void post_run(struct etpan_thread_op * op)
734 {
735         struct post_param * param;
736         struct post_result * result;
737         int r;
738         
739         param = op->param;
740         result = op->result;
741
742         CHECK_NNTP();
743
744         r = newsnntp_post(param->nntp, param->contents, param->len);
745         
746         result->error = r;
747         debug_print("nntp post run - end %i\n", r);
748 }
749
750 int nntp_threaded_post(Folder * folder, char *contents, size_t len)
751 {
752         struct post_param param;
753         struct post_result result;
754         
755         debug_print("nntp post - begin\n");
756         
757         param.nntp = get_nntp(folder);
758         param.contents = contents;
759         param.len = len;
760
761         threaded_run(folder, &param, &result, post_run);
762         
763         debug_print("nntp post - end\n");
764         
765         return result.error;
766 }
767
768 struct article_param {
769         newsnntp * nntp;
770         guint32 num;
771         char **contents;
772         size_t *len;
773 };
774
775 struct article_result {
776         int error;
777 };
778
779 static void article_run(struct etpan_thread_op * op)
780 {
781         struct article_param * param;
782         struct article_result * result;
783         int r;
784         
785         param = op->param;
786         result = op->result;
787
788         CHECK_NNTP();
789
790         r = newsnntp_article(param->nntp, param->num, param->contents, param->len);
791         
792         result->error = r;
793         debug_print("nntp article run - end %i\n", r);
794 }
795
796 int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t *len)
797 {
798         struct article_param param;
799         struct article_result result;
800         
801         debug_print("nntp article - begin\n");
802         
803         param.nntp = get_nntp(folder);
804         param.num = num;
805         param.contents = contents;
806         param.len = len;
807
808         threaded_run(folder, &param, &result, article_run);
809         
810         debug_print("nntp post - end\n");
811         
812         return result.error;
813 }
814
815 struct group_param {
816         newsnntp * nntp;
817         const char *group;
818         struct newsnntp_group_info **info;
819 };
820
821 struct group_result {
822         int error;
823 };
824
825 static void group_run(struct etpan_thread_op * op)
826 {
827         struct group_param * param;
828         struct group_result * result;
829         int r;
830         
831         param = op->param;
832         result = op->result;
833
834         CHECK_NNTP();
835
836         r = newsnntp_group(param->nntp, param->group, param->info);
837         
838         result->error = r;
839         debug_print("nntp group run - end %i\n", r);
840 }
841
842 int nntp_threaded_group(Folder * folder, const char *group, struct newsnntp_group_info **info)
843 {
844         struct group_param param;
845         struct group_result result;
846         
847         debug_print("nntp group - begin\n");
848         
849         param.nntp = get_nntp(folder);
850         param.group = group;
851         param.info = info;
852
853         threaded_run(folder, &param, &result, group_run);
854         
855         debug_print("nntp group - end\n");
856         
857         return result.error;
858 }
859
860 struct mode_reader_param {
861         newsnntp * nntp;
862 };
863
864 struct mode_reader_result {
865         int error;
866 };
867
868 static void mode_reader_run(struct etpan_thread_op * op)
869 {
870         struct mode_reader_param * param;
871         struct mode_reader_result * result;
872         int r;
873         
874         param = op->param;
875         result = op->result;
876
877         CHECK_NNTP();
878
879         r = newsnntp_mode_reader(param->nntp);
880         
881         result->error = r;
882         debug_print("nntp mode_reader run - end %i\n", r);
883 }
884
885 int nntp_threaded_mode_reader(Folder * folder)
886 {
887         struct mode_reader_param param;
888         struct mode_reader_result result;
889         
890         debug_print("nntp mode_reader - begin\n");
891         
892         param.nntp = get_nntp(folder);
893
894         threaded_run(folder, &param, &result, mode_reader_run);
895         
896         debug_print("nntp mode_reader - end\n");
897         
898         return result.error;
899 }
900
901 struct xover_param {
902         newsnntp * nntp;
903         guint32 beg;
904         guint32 end;
905         struct newsnntp_xover_resp_item **result;
906         clist **msglist;
907 };
908
909 struct xover_result {
910         int error;
911 };
912
913 static void xover_run(struct etpan_thread_op * op)
914 {
915         struct xover_param * param;
916         struct xover_result * result;
917         int r;
918         
919         param = op->param;
920         result = op->result;
921
922         CHECK_NNTP();
923         
924         if (param->result) {
925                 r = newsnntp_xover_single(param->nntp, param->beg, param->result);
926         } else {
927                 r = newsnntp_xover_range(param->nntp, param->beg, param->end, param->msglist);
928         }
929         
930         result->error = r;
931         debug_print("nntp xover run - end %i\n", r);
932 }
933
934 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
935 {
936         struct xover_param param;
937         struct xover_result result;
938         
939         debug_print("nntp xover - begin\n");
940         
941         param.nntp = get_nntp(folder);
942         param.beg = beg;
943         param.end = end;
944         param.result = single_result;
945         param.msglist = multiple_result;
946
947         threaded_run(folder, &param, &result, xover_run);
948         
949         debug_print("nntp xover - end\n");
950         
951         return result.error;
952 }
953
954 struct xhdr_param {
955         newsnntp * nntp;
956         const char *header;
957         guint32 beg;
958         guint32 end;
959         clist **hdrlist;
960 };
961
962 struct xhdr_result {
963         int error;
964 };
965
966 static void xhdr_run(struct etpan_thread_op * op)
967 {
968         struct xhdr_param * param;
969         struct xhdr_result * result;
970         int r;
971         
972         param = op->param;
973         result = op->result;
974
975         CHECK_NNTP();
976         
977         if (param->beg == param->end) {
978                 r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
979         } else {
980                 r = newsnntp_xhdr_range(param->nntp, param->header, param->beg, param->end, param->hdrlist);
981         }
982         
983         result->error = r;
984         debug_print("nntp xhdr run - end %i\n", r);
985 }
986
987 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
988 {
989         struct xhdr_param param;
990         struct xhdr_result result;
991         
992         debug_print("nntp xhdr - begin\n");
993         
994         param.nntp = get_nntp(folder);
995         param.header = header;
996         param.beg = beg;
997         param.end = end;
998         param.hdrlist = hdrlist;
999
1000         threaded_run(folder, &param, &result, xhdr_run);
1001         
1002         debug_print("nntp xhdr - end\n");
1003         
1004         return result.error;
1005 }
1006
1007
1008 #else
1009
1010 void nntp_main_init(void)
1011 {
1012 }
1013 void nntp_main_done(gboolean have_connectivity)
1014 {
1015 }
1016 void nntp_main_set_timeout(int sec)
1017 {
1018 }
1019
1020 void nntp_threaded_cancel(Folder * folder);
1021 {
1022 }
1023
1024 #endif