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