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