2008-07-03 [colin] 3.5.0cvs2
[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                 x509_len = (size_t)i2d_X509(x509, &x509_der);
446                 pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
447                 if (x509_len > 0 && pkey_len > 0) {
448                         if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
449                             mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0) 
450                                 log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
451                         g_free(x509_der);
452                         g_free(pkey_der);
453                 }
454 #ifdef USE_GNUTLS
455                 gnutls_x509_crt_deinit(x509);
456                 gnutls_x509_privkey_deinit(pkey);
457 #endif
458         }
459 #endif
460 }
461
462 static void connect_ssl_run(struct etpan_thread_op * op)
463 {
464         int r;
465         struct connect_param * param;
466         struct connect_result * result;
467         
468         param = op->param;
469         result = op->result;
470         
471         CHECK_NNTP();
472
473         r = newsnntp_ssl_connect_with_callback(param->nntp,
474                                  param->server, param->port,
475                                  connect_ssl_context_cb, param->account);
476         result->error = r;
477 }
478
479 int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
480 {
481         struct connect_param param;
482         struct connect_result result;
483         chashdatum key;
484         chashdatum value;
485         newsnntp * nntp, * oldnntp;
486         unsigned char *certificate = NULL;
487         int cert_len;
488         
489         oldnntp = get_nntp(folder);
490
491         nntp = newsnntp_new(0, NULL);
492         
493         if (oldnntp) {
494                 debug_print("deleting old nntp %p\n", oldnntp);
495                 delete_nntp(folder, oldnntp);
496         }
497
498         key.data = &folder;
499         key.len = sizeof(folder);
500         value.data = nntp;
501         value.len = 0;
502         chash_set(session_hash, &key, &value, NULL);
503         
504         param.nntp = nntp;
505         param.server = server;
506         param.port = port;
507         param.account = folder->account;
508
509         refresh_resolvers();
510         threaded_run(folder, &param, &result, connect_ssl_run);
511
512         if (result.error == NEWSNNTP_NO_ERROR && !etpan_skip_ssl_cert_check) {
513                 cert_len = (int)mailstream_ssl_get_certificate(nntp->nntp_stream, &certificate);
514                 if (etpan_certificate_check(certificate, cert_len, &param) < 0)
515                         return -1;
516                 if (certificate) 
517                         free(certificate); 
518         }
519         debug_print("connect %d with nntp %p\n", result.error, nntp);
520         
521         return result.error;
522 }
523
524 void nntp_threaded_disconnect(Folder * folder)
525 {
526         newsnntp * nntp;
527         
528         nntp = get_nntp(folder);
529         if (nntp == NULL) {
530                 debug_print("was disconnected\n");
531                 return;
532         }
533         
534         debug_print("deleting old nntp %p\n", nntp);
535         delete_nntp(folder, nntp);
536         
537         debug_print("disconnect ok\n");
538 }
539
540 void nntp_threaded_cancel(Folder * folder)
541 {
542         newsnntp * nntp;
543         
544         nntp = get_nntp(folder);
545         if (nntp->nntp_stream != NULL)
546                 mailstream_cancel(nntp->nntp_stream);
547 }
548
549
550 struct login_param {
551         newsnntp * nntp;
552         const char * login;
553         const char * password;
554 };
555
556 struct login_result {
557         int error;
558 };
559
560 static void login_run(struct etpan_thread_op * op)
561 {
562         struct login_param * param;
563         struct login_result * result;
564         int r;
565 #ifdef DISABLE_LOG_DURING_LOGIN
566         int old_debug;
567 #endif
568         
569         param = op->param;
570         result = op->result;
571
572         CHECK_NNTP();
573
574 #ifdef DISABLE_LOG_DURING_LOGIN
575         old_debug = mailstream_debug;
576         mailstream_debug = 0;
577 #endif
578
579         r = newsnntp_authinfo_username(param->nntp, param->login);
580         if (r == NEWSNNTP_NO_ERROR || 
581             r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
582                 r = newsnntp_authinfo_password(param->nntp, param->password);
583         }
584         
585
586
587 #ifdef DISABLE_LOG_DURING_LOGIN
588         mailstream_debug = old_debug;
589 #endif
590         
591         result->error = r;
592         if (param->nntp->nntp_response)
593                 nntp_logger(0, param->nntp->nntp_response, strlen(param->nntp->nntp_response));
594
595         debug_print("nntp login run - end %i\n", r);
596 }
597
598 int nntp_threaded_login(Folder * folder, const char * login, const char * password)
599 {
600         struct login_param param;
601         struct login_result result;
602         
603         debug_print("nntp login - begin\n");
604         
605         param.nntp = get_nntp(folder);
606         param.login = login;
607         param.password = password;
608
609         threaded_run(folder, &param, &result, login_run);
610         
611         debug_print("nntp login - end\n");
612         
613         return result.error;
614 }
615
616 struct date_param {
617         newsnntp * nntp;
618         struct tm * lt;
619 };
620
621 struct date_result {
622         int error;
623 };
624
625 static void date_run(struct etpan_thread_op * op)
626 {
627         struct date_param * param;
628         struct date_result * result;
629         int r;
630         
631         param = op->param;
632         result = op->result;
633
634         CHECK_NNTP();
635
636         r = newsnntp_date(param->nntp, param->lt);
637         
638         result->error = r;
639         debug_print("nntp date run - end %i\n", r);
640 }
641
642 int nntp_threaded_date(Folder * folder, struct tm *lt)
643 {
644         struct date_param param;
645         struct date_result result;
646         
647         debug_print("nntp date - begin\n");
648         
649         param.nntp = get_nntp(folder);
650         param.lt = lt;
651
652         threaded_run(folder, &param, &result, date_run);
653         
654         debug_print("nntp date - end\n");
655         
656         return result.error;
657 }
658
659 struct list_param {
660         newsnntp * nntp;
661         clist **grouplist;
662 };
663
664 struct list_result {
665         int error;
666 };
667
668 static void list_run(struct etpan_thread_op * op)
669 {
670         struct list_param * param;
671         struct list_result * result;
672         int r;
673         
674         param = op->param;
675         result = op->result;
676
677         CHECK_NNTP();
678
679         r = newsnntp_list(param->nntp, param->grouplist);
680         
681         result->error = r;
682         debug_print("nntp list run - end %i\n", r);
683 }
684
685 int nntp_threaded_list(Folder * folder, clist **grouplist)
686 {
687         struct list_param param;
688         struct list_result result;
689         
690         debug_print("nntp list - begin\n");
691         
692         param.nntp = get_nntp(folder);
693         param.grouplist = grouplist;
694
695         threaded_run(folder, &param, &result, list_run);
696         
697         debug_print("nntp list - end\n");
698         
699         return result.error;
700 }
701
702 struct post_param {
703         newsnntp * nntp;
704         char *contents;
705         size_t len;
706 };
707
708 struct post_result {
709         int error;
710 };
711
712 static void post_run(struct etpan_thread_op * op)
713 {
714         struct post_param * param;
715         struct post_result * result;
716         int r;
717         
718         param = op->param;
719         result = op->result;
720
721         CHECK_NNTP();
722
723         r = newsnntp_post(param->nntp, param->contents, param->len);
724         
725         result->error = r;
726         debug_print("nntp post run - end %i\n", r);
727 }
728
729 int nntp_threaded_post(Folder * folder, char *contents, size_t len)
730 {
731         struct post_param param;
732         struct post_result result;
733         
734         debug_print("nntp post - begin\n");
735         
736         param.nntp = get_nntp(folder);
737         param.contents = contents;
738         param.len = len;
739
740         threaded_run(folder, &param, &result, post_run);
741         
742         debug_print("nntp post - end\n");
743         
744         return result.error;
745 }
746
747 struct article_param {
748         newsnntp * nntp;
749         guint32 num;
750         char **contents;
751         size_t *len;
752 };
753
754 struct article_result {
755         int error;
756 };
757
758 static void article_run(struct etpan_thread_op * op)
759 {
760         struct article_param * param;
761         struct article_result * result;
762         int r;
763         
764         param = op->param;
765         result = op->result;
766
767         CHECK_NNTP();
768
769         r = newsnntp_article(param->nntp, param->num, param->contents, param->len);
770         
771         result->error = r;
772         debug_print("nntp article run - end %i\n", r);
773 }
774
775 int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t *len)
776 {
777         struct article_param param;
778         struct article_result result;
779         
780         debug_print("nntp article - begin\n");
781         
782         param.nntp = get_nntp(folder);
783         param.num = num;
784         param.contents = contents;
785         param.len = len;
786
787         threaded_run(folder, &param, &result, article_run);
788         
789         debug_print("nntp post - end\n");
790         
791         return result.error;
792 }
793
794 struct group_param {
795         newsnntp * nntp;
796         const char *group;
797         struct newsnntp_group_info **info;
798 };
799
800 struct group_result {
801         int error;
802 };
803
804 static void group_run(struct etpan_thread_op * op)
805 {
806         struct group_param * param;
807         struct group_result * result;
808         int r;
809         
810         param = op->param;
811         result = op->result;
812
813         CHECK_NNTP();
814
815         r = newsnntp_group(param->nntp, param->group, param->info);
816         
817         result->error = r;
818         debug_print("nntp group run - end %i\n", r);
819 }
820
821 int nntp_threaded_group(Folder * folder, const char *group, struct newsnntp_group_info **info)
822 {
823         struct group_param param;
824         struct group_result result;
825         
826         debug_print("nntp group - begin\n");
827         
828         param.nntp = get_nntp(folder);
829         param.group = group;
830         param.info = info;
831
832         threaded_run(folder, &param, &result, group_run);
833         
834         debug_print("nntp group - end\n");
835         
836         return result.error;
837 }
838
839 struct mode_reader_param {
840         newsnntp * nntp;
841 };
842
843 struct mode_reader_result {
844         int error;
845 };
846
847 static void mode_reader_run(struct etpan_thread_op * op)
848 {
849         struct mode_reader_param * param;
850         struct mode_reader_result * result;
851         int r;
852         
853         param = op->param;
854         result = op->result;
855
856         CHECK_NNTP();
857
858         r = newsnntp_mode_reader(param->nntp);
859         
860         result->error = r;
861         debug_print("nntp mode_reader run - end %i\n", r);
862 }
863
864 int nntp_threaded_mode_reader(Folder * folder)
865 {
866         struct mode_reader_param param;
867         struct mode_reader_result result;
868         
869         debug_print("nntp mode_reader - begin\n");
870         
871         param.nntp = get_nntp(folder);
872
873         threaded_run(folder, &param, &result, mode_reader_run);
874         
875         debug_print("nntp mode_reader - end\n");
876         
877         return result.error;
878 }
879
880 struct xover_param {
881         newsnntp * nntp;
882         guint32 beg;
883         guint32 end;
884         struct newsnntp_xover_resp_item **result;
885         clist **msglist;
886 };
887
888 struct xover_result {
889         int error;
890 };
891
892 static void xover_run(struct etpan_thread_op * op)
893 {
894         struct xover_param * param;
895         struct xover_result * result;
896         int r;
897         
898         param = op->param;
899         result = op->result;
900
901         CHECK_NNTP();
902         
903         if (param->result) {
904                 r = newsnntp_xover_single(param->nntp, param->beg, param->result);
905         } else {
906                 r = newsnntp_xover_range(param->nntp, param->beg, param->end, param->msglist);
907         }
908         
909         result->error = r;
910         debug_print("nntp xover run - end %i\n", r);
911 }
912
913 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
914 {
915         struct xover_param param;
916         struct xover_result result;
917         
918         debug_print("nntp xover - begin\n");
919         
920         param.nntp = get_nntp(folder);
921         param.beg = beg;
922         param.end = end;
923         param.result = single_result;
924         param.msglist = multiple_result;
925
926         threaded_run(folder, &param, &result, xover_run);
927         
928         debug_print("nntp xover - end\n");
929         
930         return result.error;
931 }
932
933 struct xhdr_param {
934         newsnntp * nntp;
935         const char *header;
936         guint32 beg;
937         guint32 end;
938         clist **hdrlist;
939 };
940
941 struct xhdr_result {
942         int error;
943 };
944
945 static void xhdr_run(struct etpan_thread_op * op)
946 {
947         struct xhdr_param * param;
948         struct xhdr_result * result;
949         int r;
950         
951         param = op->param;
952         result = op->result;
953
954         CHECK_NNTP();
955         
956         if (param->beg == param->end) {
957                 r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
958         } else {
959                 r = -1;
960                 g_warning("XHDR range not implemented\n");
961         }
962         
963         result->error = r;
964         debug_print("nntp xhdr run - end %i\n", r);
965 }
966
967 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
968 {
969         struct xhdr_param param;
970         struct xhdr_result result;
971         
972         debug_print("nntp xhdr - begin\n");
973         
974         param.nntp = get_nntp(folder);
975         param.header = header;
976         param.beg = beg;
977         param.end = end;
978         param.hdrlist = hdrlist;
979
980         threaded_run(folder, &param, &result, xhdr_run);
981         
982         debug_print("nntp xhdr - end\n");
983         
984         return result.error;
985 }
986
987
988 #else
989
990 void nntp_main_init(void)
991 {
992 }
993 void nntp_main_done(void)
994 {
995 }
996 void nntp_main_set_timeout(int sec)
997 {
998 }
999
1000 void nntp_threaded_cancel(Folder * folder);
1001 {
1002 }
1003
1004 #endif