2008-09-20 [colin] 3.5.0cvs116
[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 #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(void)
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_authinfo_username(param->nntp, param->login);
596         if (r == NEWSNNTP_NO_ERROR || 
597             r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
598                 r = newsnntp_authinfo_password(param->nntp, param->password);
599         }
600         
601
602
603 #ifdef DISABLE_LOG_DURING_LOGIN
604         mailstream_debug = old_debug;
605 #endif
606         
607         result->error = r;
608         if (param->nntp->nntp_response)
609                 nntp_logger(0, param->nntp->nntp_response, strlen(param->nntp->nntp_response));
610
611         debug_print("nntp login run - end %i\n", r);
612 }
613
614 int nntp_threaded_login(Folder * folder, const char * login, const char * password)
615 {
616         struct login_param param;
617         struct login_result result;
618         
619         debug_print("nntp login - begin\n");
620         
621         param.nntp = get_nntp(folder);
622         param.login = login;
623         param.password = password;
624
625         threaded_run(folder, &param, &result, login_run);
626         
627         debug_print("nntp login - end\n");
628         
629         return result.error;
630 }
631
632 struct date_param {
633         newsnntp * nntp;
634         struct tm * lt;
635 };
636
637 struct date_result {
638         int error;
639 };
640
641 static void date_run(struct etpan_thread_op * op)
642 {
643         struct date_param * param;
644         struct date_result * result;
645         int r;
646         
647         param = op->param;
648         result = op->result;
649
650         CHECK_NNTP();
651
652         r = newsnntp_date(param->nntp, param->lt);
653         
654         result->error = r;
655         debug_print("nntp date run - end %i\n", r);
656 }
657
658 int nntp_threaded_date(Folder * folder, struct tm *lt)
659 {
660         struct date_param param;
661         struct date_result result;
662         
663         debug_print("nntp date - begin\n");
664         
665         param.nntp = get_nntp(folder);
666         param.lt = lt;
667
668         threaded_run(folder, &param, &result, date_run);
669         
670         debug_print("nntp date - end\n");
671         
672         return result.error;
673 }
674
675 struct list_param {
676         newsnntp * nntp;
677         clist **grouplist;
678 };
679
680 struct list_result {
681         int error;
682 };
683
684 static void list_run(struct etpan_thread_op * op)
685 {
686         struct list_param * param;
687         struct list_result * result;
688         int r;
689         
690         param = op->param;
691         result = op->result;
692
693         CHECK_NNTP();
694
695         r = newsnntp_list(param->nntp, param->grouplist);
696         
697         result->error = r;
698         debug_print("nntp list run - end %i\n", r);
699 }
700
701 int nntp_threaded_list(Folder * folder, clist **grouplist)
702 {
703         struct list_param param;
704         struct list_result result;
705         
706         debug_print("nntp list - begin\n");
707         
708         param.nntp = get_nntp(folder);
709         param.grouplist = grouplist;
710
711         threaded_run(folder, &param, &result, list_run);
712         
713         debug_print("nntp list - end\n");
714         
715         return result.error;
716 }
717
718 struct post_param {
719         newsnntp * nntp;
720         char *contents;
721         size_t len;
722 };
723
724 struct post_result {
725         int error;
726 };
727
728 static void post_run(struct etpan_thread_op * op)
729 {
730         struct post_param * param;
731         struct post_result * result;
732         int r;
733         
734         param = op->param;
735         result = op->result;
736
737         CHECK_NNTP();
738
739         r = newsnntp_post(param->nntp, param->contents, param->len);
740         
741         result->error = r;
742         debug_print("nntp post run - end %i\n", r);
743 }
744
745 int nntp_threaded_post(Folder * folder, char *contents, size_t len)
746 {
747         struct post_param param;
748         struct post_result result;
749         
750         debug_print("nntp post - begin\n");
751         
752         param.nntp = get_nntp(folder);
753         param.contents = contents;
754         param.len = len;
755
756         threaded_run(folder, &param, &result, post_run);
757         
758         debug_print("nntp post - end\n");
759         
760         return result.error;
761 }
762
763 struct article_param {
764         newsnntp * nntp;
765         guint32 num;
766         char **contents;
767         size_t *len;
768 };
769
770 struct article_result {
771         int error;
772 };
773
774 static void article_run(struct etpan_thread_op * op)
775 {
776         struct article_param * param;
777         struct article_result * result;
778         int r;
779         
780         param = op->param;
781         result = op->result;
782
783         CHECK_NNTP();
784
785         r = newsnntp_article(param->nntp, param->num, param->contents, param->len);
786         
787         result->error = r;
788         debug_print("nntp article run - end %i\n", r);
789 }
790
791 int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t *len)
792 {
793         struct article_param param;
794         struct article_result result;
795         
796         debug_print("nntp article - begin\n");
797         
798         param.nntp = get_nntp(folder);
799         param.num = num;
800         param.contents = contents;
801         param.len = len;
802
803         threaded_run(folder, &param, &result, article_run);
804         
805         debug_print("nntp post - end\n");
806         
807         return result.error;
808 }
809
810 struct group_param {
811         newsnntp * nntp;
812         const char *group;
813         struct newsnntp_group_info **info;
814 };
815
816 struct group_result {
817         int error;
818 };
819
820 static void group_run(struct etpan_thread_op * op)
821 {
822         struct group_param * param;
823         struct group_result * result;
824         int r;
825         
826         param = op->param;
827         result = op->result;
828
829         CHECK_NNTP();
830
831         r = newsnntp_group(param->nntp, param->group, param->info);
832         
833         result->error = r;
834         debug_print("nntp group run - end %i\n", r);
835 }
836
837 int nntp_threaded_group(Folder * folder, const char *group, struct newsnntp_group_info **info)
838 {
839         struct group_param param;
840         struct group_result result;
841         
842         debug_print("nntp group - begin\n");
843         
844         param.nntp = get_nntp(folder);
845         param.group = group;
846         param.info = info;
847
848         threaded_run(folder, &param, &result, group_run);
849         
850         debug_print("nntp group - end\n");
851         
852         return result.error;
853 }
854
855 struct mode_reader_param {
856         newsnntp * nntp;
857 };
858
859 struct mode_reader_result {
860         int error;
861 };
862
863 static void mode_reader_run(struct etpan_thread_op * op)
864 {
865         struct mode_reader_param * param;
866         struct mode_reader_result * result;
867         int r;
868         
869         param = op->param;
870         result = op->result;
871
872         CHECK_NNTP();
873
874         r = newsnntp_mode_reader(param->nntp);
875         
876         result->error = r;
877         debug_print("nntp mode_reader run - end %i\n", r);
878 }
879
880 int nntp_threaded_mode_reader(Folder * folder)
881 {
882         struct mode_reader_param param;
883         struct mode_reader_result result;
884         
885         debug_print("nntp mode_reader - begin\n");
886         
887         param.nntp = get_nntp(folder);
888
889         threaded_run(folder, &param, &result, mode_reader_run);
890         
891         debug_print("nntp mode_reader - end\n");
892         
893         return result.error;
894 }
895
896 struct xover_param {
897         newsnntp * nntp;
898         guint32 beg;
899         guint32 end;
900         struct newsnntp_xover_resp_item **result;
901         clist **msglist;
902 };
903
904 struct xover_result {
905         int error;
906 };
907
908 static void xover_run(struct etpan_thread_op * op)
909 {
910         struct xover_param * param;
911         struct xover_result * result;
912         int r;
913         
914         param = op->param;
915         result = op->result;
916
917         CHECK_NNTP();
918         
919         if (param->result) {
920                 r = newsnntp_xover_single(param->nntp, param->beg, param->result);
921         } else {
922                 r = newsnntp_xover_range(param->nntp, param->beg, param->end, param->msglist);
923         }
924         
925         result->error = r;
926         debug_print("nntp xover run - end %i\n", r);
927 }
928
929 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
930 {
931         struct xover_param param;
932         struct xover_result result;
933         
934         debug_print("nntp xover - begin\n");
935         
936         param.nntp = get_nntp(folder);
937         param.beg = beg;
938         param.end = end;
939         param.result = single_result;
940         param.msglist = multiple_result;
941
942         threaded_run(folder, &param, &result, xover_run);
943         
944         debug_print("nntp xover - end\n");
945         
946         return result.error;
947 }
948
949 struct xhdr_param {
950         newsnntp * nntp;
951         const char *header;
952         guint32 beg;
953         guint32 end;
954         clist **hdrlist;
955 };
956
957 struct xhdr_result {
958         int error;
959 };
960
961 static void xhdr_run(struct etpan_thread_op * op)
962 {
963         struct xhdr_param * param;
964         struct xhdr_result * result;
965         int r;
966         
967         param = op->param;
968         result = op->result;
969
970         CHECK_NNTP();
971         
972         if (param->beg == param->end) {
973                 r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
974         } else {
975                 r = -1;
976                 g_warning("XHDR range not implemented\n");
977         }
978         
979         result->error = r;
980         debug_print("nntp xhdr run - end %i\n", r);
981 }
982
983 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
984 {
985         struct xhdr_param param;
986         struct xhdr_result result;
987         
988         debug_print("nntp xhdr - begin\n");
989         
990         param.nntp = get_nntp(folder);
991         param.header = header;
992         param.beg = beg;
993         param.end = end;
994         param.hdrlist = hdrlist;
995
996         threaded_run(folder, &param, &result, xhdr_run);
997         
998         debug_print("nntp xhdr - end\n");
999         
1000         return result.error;
1001 }
1002
1003
1004 #else
1005
1006 void nntp_main_init(void)
1007 {
1008 }
1009 void nntp_main_done(void)
1010 {
1011 }
1012 void nntp_main_set_timeout(int sec)
1013 {
1014 }
1015
1016 void nntp_threaded_cancel(Folder * folder);
1017 {
1018 }
1019
1020 #endif