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