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
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.
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.
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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
28 #include "nntp-thread.h"
30 #include <sys/types.h>
32 #if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__CYGWIN__))
33 #include <sys/socket.h>
42 #include "etpan-thread-manager.h"
43 #include "etpan-ssl.h"
45 #include "mainwindow.h"
46 #include "ssl_certificate.h"
48 #include "remotefolder.h"
51 #include "statusbar.h"
53 #define DISABLE_LOG_DURING_LOGIN
55 #define NNTP_BATCH_SIZE 5000
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;
63 static void nntp_logger(int direction, const char * str, size_t size)
70 log_print(LOG_PROTOCOL, "NNTP%c [data - %zd bytes]\n", direction?'>':'<', size);
74 memset(buf, 0, size+1);
75 strncpy(buf, str, size);
78 if (!strncmp(buf, "<<<<<<<", 7)
79 || !strncmp(buf, ">>>>>>>", 7)) {
83 while (strstr(buf, "\r"))
84 *strstr(buf, "\r") = ' ';
85 while (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n')
86 buf[strlen(buf)-1] = '\0';
88 lines = g_strsplit(buf, "\n", -1);
90 while (lines[i] && *lines[i]) {
91 log_print(LOG_PROTOCOL, "NNTP%c %s\n", direction?'>':'<', lines[i]);
98 static void delete_nntp(Folder *folder, newsnntp *nntp)
103 key.len = sizeof(folder);
104 chash_delete(session_hash, &key, NULL);
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;
113 debug_print("removing newsnntp %p\n", nntp);
117 static gboolean thread_manager_event(GIOChannel * source,
118 GIOCondition condition,
125 if (condition & G_IO_IN)
126 g_io_channel_read_chars(source, &ch, 1, &bytes_read, NULL);
128 etpan_thread_manager_loop(thread_manager);
133 #define ETPAN_DEFAULT_NETWORK_TIMEOUT 60
134 extern gboolean etpan_skip_ssl_cert_check;
136 void nntp_main_init(gboolean skip_ssl_cert_check)
138 int fd_thread_manager;
140 etpan_skip_ssl_cert_check = skip_ssl_cert_check;
142 nntp_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
143 session_hash = chash_new(CHASH_COPYKEY, CHASH_DEFAULTSIZE);
145 thread_manager = etpan_thread_manager_new();
147 fd_thread_manager = etpan_thread_manager_get_fd(thread_manager);
150 io_channel = g_io_channel_unix_new(fd_thread_manager);
152 io_channel = g_io_channel_win32_new_fd(fd_thread_manager);
155 thread_manager_signal = g_io_add_watch_full(io_channel, 0, G_IO_IN,
156 thread_manager_event,
161 void nntp_main_done(gboolean have_connectivity)
163 nntp_disconnect_all(have_connectivity);
164 etpan_thread_manager_stop(thread_manager);
165 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
168 etpan_thread_manager_join(thread_manager);
170 g_source_remove(thread_manager_signal);
171 g_io_channel_unref(io_channel);
173 etpan_thread_manager_free(thread_manager);
175 chash_free(session_hash);
176 chash_free(nntp_hash);
179 void nntp_init(Folder * folder)
181 struct etpan_thread * thread;
185 thread = etpan_thread_manager_get_thread(thread_manager);
188 key.len = sizeof(folder);
192 chash_set(nntp_hash, &key, &value, NULL);
195 void nntp_done(Folder * folder)
197 struct etpan_thread * thread;
203 key.len = sizeof(folder);
205 r = chash_get(nntp_hash, &key, &value);
211 etpan_thread_unbind(thread);
213 chash_delete(nntp_hash, &key, NULL);
215 debug_print("remove thread\n");
218 static struct etpan_thread * get_thread(Folder * folder)
220 struct etpan_thread * thread;
226 key.len = sizeof(folder);
228 r = chash_get(nntp_hash, &key, &value);
237 static newsnntp * get_nntp(Folder * folder)
245 key.len = sizeof(folder);
247 r = chash_get(session_hash, &key, &value);
252 debug_print("found nntp %p\n", nntp);
257 static void generic_cb(int cancelled, void * result, void * callback_data)
259 struct etpan_thread_op * op;
261 op = (struct etpan_thread_op *) callback_data;
263 debug_print("generic_cb\n");
267 static void threaded_run(Folder * folder, void * param, void * result,
268 void (* func)(struct etpan_thread_op * ))
270 struct etpan_thread_op * op;
271 struct etpan_thread * thread;
272 void (*previous_stream_logger)(int direction,
273 const char * str, size_t size);
275 nntp_folder_ref(folder);
277 op = etpan_thread_op_new();
279 op->nntp = get_nntp(folder);
284 op->callback = generic_cb;
285 op->callback_data = op;
287 previous_stream_logger = mailstream_logger;
288 mailstream_logger = nntp_logger;
290 thread = get_thread(folder);
291 etpan_thread_op_schedule(thread, op);
293 while (!op->finished) {
294 gtk_main_iteration();
297 mailstream_logger = previous_stream_logger;
299 etpan_thread_op_free(op);
301 nntp_folder_unref(folder);
307 struct connect_param {
309 PrefsAccount *account;
314 struct connect_result {
318 #define CHECK_NNTP() { \
319 if (!param->nntp) { \
320 result->error = NEWSNNTP_ERROR_BAD_STATE; \
325 static void connect_run(struct etpan_thread_op * op)
328 struct connect_param * param;
329 struct connect_result * result;
336 r = newsnntp_socket_connect(param->nntp,
337 param->server, param->port);
343 int nntp_threaded_connect(Folder * folder, const char * server, int port)
345 struct connect_param param;
346 struct connect_result result;
349 newsnntp * nntp, * oldnntp;
351 oldnntp = get_nntp(folder);
353 nntp = newsnntp_new(0, NULL);
356 debug_print("deleting old nntp %p\n", oldnntp);
357 delete_nntp(folder, oldnntp);
361 key.len = sizeof(folder);
364 chash_set(session_hash, &key, &value, NULL);
367 param.server = server;
371 threaded_run(folder, ¶m, &result, connect_run);
373 debug_print("connect ok %i with nntp %p\n", result.error, nntp);
378 static void connect_ssl_run(struct etpan_thread_op * op)
381 struct connect_param * param;
382 struct connect_result * result;
389 r = newsnntp_ssl_connect_with_callback(param->nntp,
390 param->server, param->port,
391 etpan_connect_ssl_context_cb, param->account);
395 int nntp_threaded_connect_ssl(Folder * folder, const char * server, int port)
397 struct connect_param param;
398 struct connect_result result;
401 newsnntp * nntp, * oldnntp;
402 gboolean accept_if_valid = FALSE;
404 oldnntp = get_nntp(folder);
406 nntp = newsnntp_new(0, NULL);
409 debug_print("deleting old nntp %p\n", oldnntp);
410 delete_nntp(folder, oldnntp);
414 key.len = sizeof(folder);
417 chash_set(session_hash, &key, &value, NULL);
420 param.server = server;
422 param.account = folder->account;
425 accept_if_valid = folder->account->ssl_certs_auto_accept;
428 threaded_run(folder, ¶m, &result, connect_ssl_run);
430 if (result.error == NEWSNNTP_NO_ERROR && !etpan_skip_ssl_cert_check) {
431 if (etpan_certificate_check(nntp->nntp_stream, server, port,
432 accept_if_valid) != TRUE)
435 debug_print("connect %d with nntp %p\n", result.error, nntp);
441 void nntp_threaded_disconnect(Folder * folder)
445 nntp = get_nntp(folder);
447 debug_print("was disconnected\n");
451 debug_print("deleting old nntp %p\n", nntp);
452 delete_nntp(folder, nntp);
454 debug_print("disconnect ok\n");
457 void nntp_threaded_cancel(Folder * folder)
461 nntp = get_nntp(folder);
462 if (nntp->nntp_stream != NULL)
463 mailstream_cancel(nntp->nntp_stream);
470 const char * password;
473 struct login_result {
477 static void login_run(struct etpan_thread_op * op)
479 struct login_param * param;
480 struct login_result * result;
482 #ifdef DISABLE_LOG_DURING_LOGIN
491 #ifdef DISABLE_LOG_DURING_LOGIN
492 old_debug = mailstream_debug;
493 mailstream_debug = 0;
496 r = newsnntp_authinfo_username(param->nntp, param->login);
497 /* libetpan returning NO_ERROR means it received resp.code 281:
498 in this case auth. is already successful, no password is needed. */
499 if (r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD) {
500 r = newsnntp_authinfo_password(param->nntp, param->password);
505 #ifdef DISABLE_LOG_DURING_LOGIN
506 mailstream_debug = old_debug;
510 if (param->nntp->nntp_response)
511 nntp_logger(0, param->nntp->nntp_response, strlen(param->nntp->nntp_response));
513 debug_print("nntp login run - end %i\n", r);
516 int nntp_threaded_login(Folder * folder, const char * login, const char * password)
518 struct login_param param;
519 struct login_result result;
521 debug_print("nntp login - begin\n");
523 param.nntp = get_nntp(folder);
525 param.password = password;
527 threaded_run(folder, ¶m, &result, login_run);
529 debug_print("nntp login - end\n");
543 static void date_run(struct etpan_thread_op * op)
545 struct date_param * param;
546 struct date_result * result;
554 r = newsnntp_date(param->nntp, param->lt);
557 debug_print("nntp date run - end %i\n", r);
560 int nntp_threaded_date(Folder * folder, struct tm *lt)
562 struct date_param param;
563 struct date_result result;
565 debug_print("nntp date - begin\n");
567 param.nntp = get_nntp(folder);
570 threaded_run(folder, ¶m, &result, date_run);
572 debug_print("nntp date - end\n");
586 static void list_run(struct etpan_thread_op * op)
588 struct list_param * param;
589 struct list_result * result;
597 r = newsnntp_list(param->nntp, param->grouplist);
600 debug_print("nntp list run - end %i\n", r);
603 int nntp_threaded_list(Folder * folder, clist **grouplist)
605 struct list_param param;
606 struct list_result result;
608 debug_print("nntp list - begin\n");
610 param.nntp = get_nntp(folder);
611 param.grouplist = grouplist;
613 threaded_run(folder, ¶m, &result, list_run);
615 debug_print("nntp list - end\n");
630 static void post_run(struct etpan_thread_op * op)
632 struct post_param * param;
633 struct post_result * result;
641 r = newsnntp_post(param->nntp, param->contents, param->len);
644 debug_print("nntp post run - end %i\n", r);
647 int nntp_threaded_post(Folder * folder, char *contents, size_t len)
649 struct post_param param;
650 struct post_result result;
652 debug_print("nntp post - begin\n");
654 param.nntp = get_nntp(folder);
655 param.contents = contents;
658 threaded_run(folder, ¶m, &result, post_run);
660 debug_print("nntp post - end\n");
665 struct article_param {
672 struct article_result {
676 static void article_run(struct etpan_thread_op * op)
678 struct article_param * param;
679 struct article_result * result;
687 r = newsnntp_article(param->nntp, param->num, param->contents, param->len);
690 debug_print("nntp article run - end %i\n", r);
693 int nntp_threaded_article(Folder * folder, guint32 num, char **contents, size_t *len)
695 struct article_param param;
696 struct article_result result;
698 debug_print("nntp article - begin\n");
700 param.nntp = get_nntp(folder);
702 param.contents = contents;
705 threaded_run(folder, ¶m, &result, article_run);
707 debug_print("nntp article - end\n");
715 struct newsnntp_group_info **info;
718 struct group_result {
722 static void group_run(struct etpan_thread_op * op)
724 struct group_param * param;
725 struct group_result * result;
733 r = newsnntp_group(param->nntp, param->group, param->info);
736 debug_print("nntp group run - end %i\n", r);
739 int nntp_threaded_group(Folder * folder, const char *group, struct newsnntp_group_info **info)
741 struct group_param param;
742 struct group_result result;
744 debug_print("nntp group - begin\n");
746 param.nntp = get_nntp(folder);
750 threaded_run(folder, ¶m, &result, group_run);
752 debug_print("nntp group - end\n");
757 struct mode_reader_param {
761 struct mode_reader_result {
765 static void mode_reader_run(struct etpan_thread_op * op)
767 struct mode_reader_param * param;
768 struct mode_reader_result * result;
776 r = newsnntp_mode_reader(param->nntp);
779 debug_print("nntp mode_reader run - end %i\n", r);
782 int nntp_threaded_mode_reader(Folder * folder)
784 struct mode_reader_param param;
785 struct mode_reader_result result;
787 debug_print("nntp mode_reader - begin\n");
789 param.nntp = get_nntp(folder);
791 threaded_run(folder, ¶m, &result, mode_reader_run);
793 debug_print("nntp mode_reader - end\n");
802 struct newsnntp_xover_resp_item **result;
806 struct xover_result {
810 static void xover_run(struct etpan_thread_op * op)
812 struct xover_param * param;
813 struct xover_result * result;
822 r = newsnntp_xover_single(param->nntp, param->beg, param->result);
824 r = newsnntp_xover_range(param->nntp, param->beg, param->end, param->msglist);
828 debug_print("nntp xover run %d-%d - end %i\n",
829 param->beg, param->end, r);
832 int nntp_threaded_xover(Folder * folder, guint32 beg, guint32 end, struct newsnntp_xover_resp_item **single_result, clist **multiple_result)
834 struct xover_param param;
835 struct xover_result result;
836 clist *l = NULL, *h = NULL;
837 guint32 cbeg = 0, cend = 0;
839 debug_print("nntp xover - begin (%d-%d)\n", beg, end);
843 /* Request the overview in batches of NNTP_BATCH_SIZE, to prevent
844 * long stalls or libetpan choking on too large server response,
845 * and to allow updating any progress indicators while we work. */
847 while (cbeg <= end && cend <= end) {
848 cend = cbeg + (NNTP_BATCH_SIZE - 1);
852 statusbar_progress_all(cbeg - beg, end - beg, 1);
855 param.nntp = get_nntp(folder);
858 param.result = single_result;
861 threaded_run(folder, ¶m, &result, xover_run);
864 if (result.error != NEWSNNTP_NO_ERROR) {
865 log_warning(LOG_PROTOCOL, _("couldn't get xover range\n"));
866 debug_print("couldn't get xover for %d-%d\n", cbeg, cend);
868 newsnntp_xover_resp_list_free(l);
869 newsnntp_xover_resp_list_free(h);
873 /* Append the new data (l) to list of results (h). */
875 debug_print("total items so far %d, items this batch %d\n",
876 clist_count(h), clist_count(l));
882 cbeg += NNTP_BATCH_SIZE;
885 statusbar_progress_all(0, 0, 0);
887 debug_print("nntp xover - end\n");
889 *multiple_result = h;
906 static void xhdr_run(struct etpan_thread_op * op)
908 struct xhdr_param * param;
909 struct xhdr_result * result;
917 if (param->beg == param->end) {
918 r = newsnntp_xhdr_single(param->nntp, param->header, param->beg, param->hdrlist);
920 r = newsnntp_xhdr_range(param->nntp, param->header, param->beg, param->end, param->hdrlist);
924 debug_print("nntp xhdr '%s %d-%d' run - end %i\n",
925 param->header, param->beg, param->end, r);
928 int nntp_threaded_xhdr(Folder * folder, const char *header, guint32 beg, guint32 end, clist **hdrlist)
930 struct xhdr_param param;
931 struct xhdr_result result;
934 guint32 cbeg = 0, cend = 0;
936 debug_print("nntp xhdr %s - begin (%d-%d)\n", header, beg, end);
941 /* Request the headers in batches of NNTP_BATCH_SIZE, to prevent
942 * long stalls or libetpan choking on too large server response,
943 * and to allow updating any progress indicators while we work. */
945 while (cbeg <= end && cend <= end) {
946 cend = cbeg + NNTP_BATCH_SIZE - 1;
950 statusbar_progress_all(cbeg - beg, end - beg, 1);
953 param.nntp = get_nntp(folder);
954 param.header = header;
959 threaded_run(folder, ¶m, &result, xhdr_run);
962 if (result.error != NEWSNNTP_NO_ERROR) {
963 log_warning(LOG_PROTOCOL, _("couldn't get xhdr range\n"));
964 debug_print("couldn't get xhdr %s %d-%d\n", header, cbeg, cend);
966 newsnntp_xhdr_free(l);
967 newsnntp_xhdr_free(h);
971 /* Append the new data (l) to list of results (h). */
973 debug_print("total items so far %d, items this batch %d\n",
974 clist_count(h), clist_count(l));
980 cbeg += NNTP_BATCH_SIZE;
983 statusbar_progress_all(0, 0, 0);
985 debug_print("nntp xhdr %s - end (%d-%d)\n", header, beg, end);
992 void nntp_main_set_timeout(int sec)
994 mailstream_network_delay.tv_sec = sec;
995 mailstream_network_delay.tv_usec = 0;
1000 void nntp_main_init(void)
1003 void nntp_main_done(gboolean have_connectivity)
1006 void nntp_main_set_timeout(int sec)
1010 void nntp_threaded_cancel(Folder * folder);