2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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/>.
22 #include "claws-features.h"
30 #include <glib/gi18n.h>
37 #include <libetpan/libetpan.h>
39 #include "nntp-thread.h"
45 #include "procheader.h"
48 #include "statusbar.h"
51 #include "passwordstore.h"
52 #include "prefs_common.h"
53 #include "prefs_account.h"
54 #include "inputdialog.h"
56 #include "progressindicator.h"
57 #include "remotefolder.h"
58 #include "alertpanel.h"
65 #include "file-utils.h"
69 #define NNTPS_PORT 563
72 typedef struct _NewsFolder NewsFolder
;
73 typedef struct _NewsSession NewsSession
;
75 #define NEWS_FOLDER(obj) ((NewsFolder *)obj)
76 #define NEWS_SESSION(obj) ((NewsSession *)obj)
94 static void news_folder_init(Folder
*folder
, const gchar
*name
,
97 static Folder
*news_folder_new (const gchar
*name
,
99 static void news_folder_destroy (Folder
*folder
);
101 static gchar
*news_fetch_msg (Folder
*folder
,
104 static void news_remove_cached_msg (Folder
*folder
,
108 static Session
*news_session_new (Folder
*folder
,
109 const PrefsAccount
*account
,
113 static Session
*news_session_new (Folder
*folder
,
114 const PrefsAccount
*account
,
118 static gint
news_get_article (Folder
*folder
,
122 static gint
news_select_group (Folder
*folder
,
127 static MsgInfo
*news_parse_xover (struct newsnntp_xover_resp_item
*item
);
128 static gint
news_get_num_list (Folder
*folder
,
131 gboolean
*old_uids_valid
);
132 static MsgInfo
*news_get_msginfo (Folder
*folder
,
135 static GSList
*news_get_msginfos (Folder
*folder
,
137 GSList
*msgnum_list
);
138 static gboolean
news_scan_required (Folder
*folder
,
141 static gchar
*news_folder_get_path (Folder
*folder
);
142 static gchar
*news_item_get_path (Folder
*folder
,
144 static void news_synchronise (FolderItem
*item
, gint days
);
145 static int news_remove_msg (Folder
*folder
,
148 static gint
news_rename_folder (Folder
*folder
,
151 static gint
news_remove_folder (Folder
*folder
,
153 static FolderClass news_class
;
155 FolderClass
*news_get_class(void)
157 if (news_class
.idstr
== NULL
) {
158 news_class
.type
= F_NEWS
;
159 news_class
.idstr
= "news";
160 news_class
.uistr
= "News";
161 news_class
.supports_server_search
= FALSE
;
163 /* Folder functions */
164 news_class
.new_folder
= news_folder_new
;
165 news_class
.destroy_folder
= news_folder_destroy
;
167 /* FolderItem functions */
168 news_class
.item_get_path
= news_item_get_path
;
169 news_class
.get_num_list
= news_get_num_list
;
170 news_class
.scan_required
= news_scan_required
;
171 news_class
.rename_folder
= news_rename_folder
;
172 news_class
.remove_folder
= news_remove_folder
;
174 /* Message functions */
175 news_class
.get_msginfo
= news_get_msginfo
;
176 news_class
.get_msginfos
= news_get_msginfos
;
177 news_class
.fetch_msg
= news_fetch_msg
;
178 news_class
.synchronise
= news_synchronise
;
179 news_class
.search_msgs
= folder_item_search_msgs_local
;
180 news_class
.remove_msg
= news_remove_msg
;
181 news_class
.remove_cached_msg
= news_remove_cached_msg
;
187 guint
nntp_folder_get_refcnt(Folder
*folder
)
189 return ((NewsFolder
*)folder
)->refcnt
;
192 void nntp_folder_ref(Folder
*folder
)
194 ((NewsFolder
*)folder
)->refcnt
++;
197 void nntp_folder_unref(Folder
*folder
)
199 if (((NewsFolder
*)folder
)->refcnt
> 0)
200 ((NewsFolder
*)folder
)->refcnt
--;
203 static int news_remove_msg (Folder
*folder
,
207 gchar
*path
, *filename
;
209 cm_return_val_if_fail(folder
!= NULL
, -1);
210 cm_return_val_if_fail(item
!= NULL
, -1);
212 path
= folder_item_get_path(item
);
213 if (!is_dir_exist(path
))
216 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msgnum
), NULL
);
218 claws_unlink(filename
);
223 static void news_folder_lock(NewsFolder
*folder
)
225 folder
->lock_count
++;
228 static void news_folder_unlock(NewsFolder
*folder
)
230 if (folder
->lock_count
> 0)
231 folder
->lock_count
--;
234 int news_folder_locked(Folder
*folder
)
239 return NEWS_FOLDER(folder
)->lock_count
;
242 static Folder
*news_folder_new(const gchar
*name
, const gchar
*path
)
246 folder
= (Folder
*)g_new0(NewsFolder
, 1);
247 folder
->klass
= &news_class
;
248 news_folder_init(folder
, name
, path
);
253 static void news_folder_destroy(Folder
*folder
)
257 while (nntp_folder_get_refcnt(folder
) > 0)
258 gtk_main_iteration();
260 dir
= news_folder_get_path(folder
);
261 if (is_dir_exist(dir
))
262 remove_dir_recursive(dir
);
266 folder_remote_folder_destroy(REMOTE_FOLDER(folder
));
269 static void news_folder_init(Folder
*folder
, const gchar
*name
,
272 folder_remote_folder_init(folder
, name
, path
);
275 static void news_session_destroy(Session
*session
)
277 NewsSession
*news_session
= NEWS_SESSION(session
);
279 cm_return_if_fail(session
!= NULL
);
281 if (news_session
->group
)
282 g_free(news_session
->group
);
285 static gboolean
nntp_ping(gpointer data
)
287 Session
*session
= (Session
*)data
;
288 NewsSession
*news_session
= NEWS_SESSION(session
);
292 if (session
->state
!= SESSION_READY
|| news_folder_locked(news_session
->folder
))
295 news_folder_lock(NEWS_FOLDER(news_session
->folder
));
297 if ((r
= nntp_threaded_date(news_session
->folder
, <
)) != NEWSNNTP_NO_ERROR
) {
298 if (r
!= NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED
&&
299 r
!= NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD
) {
300 log_warning(LOG_PROTOCOL
, _("NNTP connection to %s:%d has been"
302 news_session
->folder
->account
->nntp_server
,
303 news_session
->folder
->account
->set_nntpport
?
304 news_session
->folder
->account
->nntpport
: NNTP_PORT
);
305 REMOTE_FOLDER(news_session
->folder
)->session
= NULL
;
306 news_folder_unlock(NEWS_FOLDER(news_session
->folder
));
307 session
->state
= SESSION_DISCONNECTED
;
308 session
->sock
= NULL
;
309 session_destroy(session
);
314 news_folder_unlock(NEWS_FOLDER(news_session
->folder
));
315 session_set_access_time(session
);
321 static Session
*news_session_new(Folder
*folder
, const PrefsAccount
*account
, gushort port
,
324 static Session
*news_session_new(Folder
*folder
, const PrefsAccount
*account
, gushort port
)
327 NewsSession
*session
;
328 const char *server
= account
->nntp_server
;
330 ProxyInfo
*proxy_info
= NULL
;
332 cm_return_val_if_fail(server
!= NULL
, NULL
);
334 log_message(LOG_PROTOCOL
,
335 _("Account '%s': Connecting to NNTP server: %s:%d...\n"),
336 folder
->account
->account_name
, server
, port
);
338 session
= g_new0(NewsSession
, 1);
339 session_init(SESSION(session
), folder
->account
, FALSE
);
340 SESSION(session
)->type
= SESSION_NEWS
;
341 SESSION(session
)->server
= g_strdup(server
);
342 SESSION(session
)->port
= port
;
343 SESSION(session
)->sock
= NULL
;
344 SESSION(session
)->destroy
= news_session_destroy
;
346 if (account
->use_proxy
) {
347 if (account
->use_default_proxy
) {
348 proxy_info
= (ProxyInfo
*)&(prefs_common
.proxy_info
);
349 if (proxy_info
->use_proxy_auth
)
350 proxy_info
->proxy_pass
= passwd_store_get(PWS_CORE
, PWS_CORE_PROXY
,
351 PWS_CORE_PROXY_PASS
);
353 proxy_info
= (ProxyInfo
*)&(account
->proxy_info
);
354 if (proxy_info
->use_proxy_auth
)
355 proxy_info
->proxy_pass
= passwd_store_get_account(account
->account_id
,
356 PWS_ACCOUNT_PROXY_PASS
);
359 SESSION(session
)->proxy_info
= proxy_info
;
364 SESSION(session
)->use_tls_sni
= account
->use_tls_sni
;
365 if (ssl_type
!= SSL_NONE
)
366 r
= nntp_threaded_connect_ssl(folder
, server
, port
, proxy_info
);
369 r
= nntp_threaded_connect(folder
, server
, port
, proxy_info
);
371 if (r
!= NEWSNNTP_NO_ERROR
) {
372 log_error(LOG_PROTOCOL
, _("Error logging in to %s:%d...\n"), server
, port
);
373 session_destroy(SESSION(session
));
377 session
->folder
= folder
;
379 return SESSION(session
);
382 static Session
*news_session_new_for_folder(Folder
*folder
)
386 const gchar
*userid
= NULL
;
387 gchar
*passwd
= NULL
;
391 cm_return_val_if_fail(folder
!= NULL
, NULL
);
392 cm_return_val_if_fail(folder
->account
!= NULL
, NULL
);
394 ac
= folder
->account
;
397 port
= ac
->set_nntpport
? ac
->nntpport
398 : ac
->ssl_nntp
? NNTPS_PORT
: NNTP_PORT
;
399 session
= news_session_new(folder
, ac
, port
, ac
->ssl_nntp
);
401 if (ac
->ssl_nntp
!= SSL_NONE
) {
402 if (alertpanel_full(_("Insecure connection"),
403 _("This connection is configured to be secured "
404 "using TLS, but TLS is not available "
405 "in this build of Claws Mail. \n\n"
406 "Do you want to continue connecting to this "
407 "server? The communication would not be "
409 NULL
, _("_Cancel"), NULL
, _("Con_tinue connecting"),
410 NULL
, NULL
, ALERTFOCUS_FIRST
, FALSE
, NULL
, ALERT_WARNING
) != G_ALERTALTERNATE
)
413 port
= ac
->set_nntpport
? ac
->nntpport
: NNTP_PORT
;
414 session
= news_session_new(folder
, ac
, port
);
417 if (ac
->use_nntp_auth
&& ac
->userid
&& ac
->userid
[0]) {
419 if (password_get(userid
, ac
->nntp_server
, "nntp", port
, &passwd
)) {
421 } else if ((passwd
= passwd_store_get_account(ac
->account_id
,
422 PWS_ACCOUNT_RECV
)) == NULL
) {
423 passwd
= input_dialog_query_password_keep(ac
->nntp_server
,
425 &(ac
->session_passwd
));
430 r
= nntp_threaded_mode_reader(folder
);
432 r
= NEWSNNTP_ERROR_CONNECTION_REFUSED
;
434 if (r
!= NEWSNNTP_NO_ERROR
) {
435 if (r
== NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME
) {
437 FIX ME when libetpan implements 480 to indicate authorization
438 is required to use this capability. Libetpan treats a 480 as a
439 381 which is clearly wrong.
443 Meaning: command unavailable until the client
444 has authenticated itself.
446 /* if the server does not advertise the capability MODE-READER,
447 we normally should not send MODE READER. However this can't
448 hurt: a transit-only server returns 502 and closes the cnx.
449 Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
451 log_error(LOG_PROTOCOL
, _("Libetpan does not support return code 480 "
452 "so for now we choose to continue\n"));
454 else if (r
== NEWSNNTP_ERROR_UNEXPECTED_RESPONSE
) {
455 /* if the server does not advertise the capability MODE-READER,
456 we normally should not send MODE READER. However this can't
457 hurt: a transit-only server returns 502 and closes the cnx.
458 Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
460 log_error(LOG_PROTOCOL
, _("Mode reader failed, continuing nevertheless\n"));
463 /* An error state bail out */
464 log_error(LOG_PROTOCOL
, _("Error creating session with %s:%d\n"), ac
->nntp_server
, port
);
466 session_destroy(SESSION(session
));
468 if (ac
->session_passwd
) {
469 g_free(ac
->session_passwd
);
470 ac
->session_passwd
= NULL
;
476 if ((session
!= NULL
) && ac
->use_nntp_auth
) { /* FIXME: && ac->use_nntp_auth_onconnect */
477 if (nntp_threaded_login(folder
, userid
, passwd
) !=
479 log_error(LOG_PROTOCOL
, _("Error authenticating to %s:%d...\n"), ac
->nntp_server
, port
);
480 session_destroy(SESSION(session
));
482 if (ac
->session_passwd
) {
483 g_free(ac
->session_passwd
);
484 ac
->session_passwd
= NULL
;
494 static NewsSession
*news_session_get(Folder
*folder
)
496 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
498 cm_return_val_if_fail(folder
!= NULL
, NULL
);
499 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
500 cm_return_val_if_fail(folder
->account
!= NULL
, NULL
);
502 if (prefs_common
.work_offline
&&
503 !inc_offline_should_override(FALSE
,
504 _("Claws Mail needs network access in order "
505 "to access the News server."))) {
509 if (!rfolder
->session
) {
510 rfolder
->session
= news_session_new_for_folder(folder
);
511 session_register_ping(SESSION(rfolder
->session
), nntp_ping
);
512 return NEWS_SESSION(rfolder
->session
);
515 /* Handle port change (also ssl/nossl change) without needing to
516 * restart application. */
517 if (rfolder
->session
->port
!= folder
->account
->nntpport
) {
518 session_destroy(rfolder
->session
);
519 rfolder
->session
= news_session_new_for_folder(folder
);
520 session_register_ping(SESSION(rfolder
->session
), nntp_ping
);
524 if (time(NULL
) - rfolder
->session
->last_access_time
<
525 SESSION_TIMEOUT_INTERVAL
) {
526 return NEWS_SESSION(rfolder
->session
);
529 if (!nntp_ping(rfolder
->session
)) {
530 rfolder
->session
= news_session_new_for_folder(folder
);
531 session_register_ping(SESSION(rfolder
->session
), nntp_ping
);
535 if (rfolder
->session
)
536 session_set_access_time(rfolder
->session
);
538 return NEWS_SESSION(rfolder
->session
);
541 static void news_remove_cached_msg(Folder
*folder
, FolderItem
*item
, MsgInfo
*msginfo
)
543 gchar
*path
, *filename
;
545 path
= folder_item_get_path(item
);
547 if (!is_dir_exist(path
)) {
552 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msginfo
->msgnum
), NULL
);
555 if (is_file_exist(filename
)) {
556 claws_unlink(filename
);
561 static gchar
*news_fetch_msg(Folder
*folder
, FolderItem
*item
, gint num
)
563 gchar
*path
, *filename
;
564 NewsSession
*session
;
567 cm_return_val_if_fail(folder
!= NULL
, NULL
);
568 cm_return_val_if_fail(item
!= NULL
, NULL
);
570 path
= folder_item_get_path(item
);
571 if (!is_dir_exist(path
))
573 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(num
), NULL
);
576 if (is_file_exist(filename
)) {
577 debug_print("article %d has been already cached.\n", num
);
581 session
= news_session_get(folder
);
587 ok
= news_select_group(folder
, item
->path
, NULL
, NULL
, NULL
);
588 if (ok
!= NEWSNNTP_NO_ERROR
) {
589 if (ok
== NEWSNNTP_ERROR_STREAM
) {
590 session_destroy(SESSION(session
));
591 REMOTE_FOLDER(folder
)->session
= NULL
;
597 debug_print("getting article %d...\n", num
);
598 ok
= news_get_article(folder
,
600 if (ok
!= NEWSNNTP_NO_ERROR
) {
601 g_warning("can't read article %d", num
);
602 if (ok
== NEWSNNTP_ERROR_STREAM
) {
603 session_destroy(SESSION(session
));
604 REMOTE_FOLDER(folder
)->session
= NULL
;
613 static NewsGroupInfo
*news_group_info_new(const gchar
*name
,
614 gint first
, gint last
, gchar type
)
616 NewsGroupInfo
*ginfo
;
618 ginfo
= g_new(NewsGroupInfo
, 1);
619 ginfo
->name
= g_strdup(name
);
620 ginfo
->first
= first
;
627 static void news_group_info_free(NewsGroupInfo
*ginfo
)
633 static gint
news_group_info_compare(NewsGroupInfo
*ginfo1
,
634 NewsGroupInfo
*ginfo2
)
636 return g_ascii_strcasecmp(ginfo1
->name
, ginfo2
->name
);
639 GSList
*news_get_group_list(Folder
*folder
)
641 gchar
*path
, *filename
;
647 cm_return_val_if_fail(folder
!= NULL
, NULL
);
648 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
650 path
= folder_item_get_path(FOLDER_ITEM(folder
->node
->data
));
651 if (!is_dir_exist(path
))
653 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, NEWSGROUP_LIST
, NULL
);
656 if ((fp
= claws_fopen(filename
, "rb")) == NULL
) {
657 NewsSession
*session
;
659 clist
*grouplist
= NULL
;
661 fp
= claws_fopen(filename
, "wb");
667 session
= news_session_get(folder
);
674 ok
= nntp_threaded_list(folder
, &grouplist
);
676 if (ok
!= NEWSNNTP_NO_ERROR
) {
677 if (ok
== NEWSNNTP_ERROR_STREAM
) {
678 session_destroy(SESSION(session
));
679 REMOTE_FOLDER(folder
)->session
= NULL
;
687 for (cur
= clist_begin(grouplist
); cur
; cur
= clist_next(cur
)) {
688 struct newsnntp_group_info
*info
= (struct newsnntp_group_info
*)
690 if (fprintf(fp
, "%s %d %d %c\n",
694 info
->grp_type
) < 0) {
695 log_error(LOG_PROTOCOL
, ("Can't write newsgroup list\n"));
696 session_destroy(SESSION(session
));
697 REMOTE_FOLDER(folder
)->session
= NULL
;
700 newsnntp_list_free(grouplist
);
704 newsnntp_list_free(grouplist
);
706 if (claws_safe_fclose(fp
) == EOF
) {
707 log_error(LOG_PROTOCOL
, ("Can't write newsgroup list\n"));
708 session_destroy(SESSION(session
));
709 REMOTE_FOLDER(folder
)->session
= NULL
;
714 if ((fp
= claws_fopen(filename
, "rb")) == NULL
) {
715 FILE_OP_ERROR(filename
, "claws_fopen");
721 while (claws_fgets(buf
, sizeof(buf
), fp
) != NULL
) {
727 NewsGroupInfo
*ginfo
;
735 if (sscanf(p
, "%d %d %c", &last_num
, &first_num
, &type
) < 3)
738 ginfo
= news_group_info_new(name
, first_num
, last_num
, type
);
741 last
= list
= g_slist_append(NULL
, ginfo
);
743 last
= g_slist_append(last
, ginfo
);
751 list
= g_slist_sort(list
, (GCompareFunc
)news_group_info_compare
);
756 void news_group_list_free(GSList
*group_list
)
760 if (!group_list
) return;
762 for (cur
= group_list
; cur
!= NULL
; cur
= cur
->next
)
763 news_group_info_free((NewsGroupInfo
*)cur
->data
);
764 g_slist_free(group_list
);
767 void news_remove_group_list_cache(Folder
*folder
)
769 gchar
*path
, *filename
;
771 cm_return_if_fail(folder
!= NULL
);
772 cm_return_if_fail(FOLDER_CLASS(folder
) == &news_class
);
774 path
= folder_item_get_path(FOLDER_ITEM(folder
->node
->data
));
775 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, NEWSGROUP_LIST
, NULL
);
778 if (is_file_exist(filename
)) {
779 if (claws_unlink(filename
) < 0)
780 FILE_OP_ERROR(filename
, "remove");
785 gint
news_post(Folder
*folder
, const gchar
*file
)
788 char *contents
= file_read_to_str_no_recode(file
);
789 NewsSession
*session
;
791 cm_return_val_if_fail(folder
!= NULL
, -1);
792 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
793 cm_return_val_if_fail(contents
!= NULL
, -1);
795 session
= news_session_get(folder
);
801 ok
= nntp_threaded_post(folder
, contents
, strlen(contents
));
805 if (ok
== NEWSNNTP_ERROR_STREAM
) {
806 session_destroy(SESSION(session
));
807 REMOTE_FOLDER(folder
)->session
= NULL
;
810 return (ok
== NEWSNNTP_NO_ERROR
? 0 : -1);
813 static gint
news_get_article(Folder
*folder
, gint num
, gchar
*filename
)
819 r
= nntp_threaded_article(folder
, num
, &result
, &len
);
821 if (r
== NEWSNNTP_NO_ERROR
) {
822 if (str_write_to_file(result
, filename
, FALSE
) < 0) {
823 mmap_string_unref(result
);
826 mmap_string_unref(result
);
834 * @session: Active NNTP session.
835 * @group: Newsgroup name.
836 * @num: Estimated number of articles.
837 * @first: First article number.
838 * @last: Last article number.
840 * Select newsgroup @group with the GROUP command if it is not already
841 * selected in @session, or article numbers need to be returned.
843 * Return value: NNTP result code.
845 static gint
news_select_group(Folder
*folder
, const gchar
*group
,
846 gint
*num
, gint
*first
, gint
*last
)
849 gint num_
, first_
, last_
;
850 struct newsnntp_group_info
*info
= NULL
;
851 NewsSession
*session
= NEWS_SESSION(news_session_get(folder
));
853 cm_return_val_if_fail(session
!= NULL
, -1);
855 if (!num
|| !first
|| !last
) {
856 if (session
->group
&& g_ascii_strcasecmp(session
->group
, group
) == 0)
857 return NEWSNNTP_NO_ERROR
;
863 g_free(session
->group
);
864 session
->group
= NULL
;
866 ok
= nntp_threaded_group(folder
, group
, &info
);
868 if (ok
== NEWSNNTP_NO_ERROR
&& info
) {
869 session
->group
= g_strdup(group
);
870 *num
= info
->grp_first
;
871 *first
= info
->grp_first
;
872 *last
= info
->grp_last
;
873 newsnntp_group_free(info
);
875 log_warning(LOG_PROTOCOL
, _("couldn't select group: %s\n"), group
);
880 static MsgInfo
*news_parse_xover(struct newsnntp_xover_resp_item
*item
)
885 msginfo
= procmsg_msginfo_new();
886 msginfo
->msgnum
= item
->ovr_article
;
887 msginfo
->size
= item
->ovr_size
;
889 msginfo
->date
= g_strdup(item
->ovr_date
);
890 msginfo
->date_t
= procheader_date_parse(NULL
, item
->ovr_date
, 0);
892 msginfo
->from
= conv_unmime_header(item
->ovr_author
, NULL
, TRUE
);
893 msginfo
->fromname
= procheader_get_fromname(msginfo
->from
);
895 msginfo
->subject
= conv_unmime_header(item
->ovr_subject
, NULL
, TRUE
);
897 remove_return(msginfo
->from
);
898 remove_return(msginfo
->fromname
);
899 remove_return(msginfo
->subject
);
901 if (item
->ovr_message_id
) {
902 gchar
*tmp
= g_strdup(item
->ovr_message_id
);
903 extract_parenthesis(tmp
, '<', '>');
906 msginfo
->msgid
= g_strdup(tmp
);
910 /* FIXME: this is a quick fix; references' meaning was changed
911 * into having the actual list of references in the References: header.
912 * We need a GSList here, so msginfo_free() and msginfo_copy() can do
913 * their things properly. */
914 if (item
->ovr_references
&& *(item
->ovr_references
)) {
915 gchar
**ref_tokens
= g_strsplit(item
->ovr_references
, " ", -1);
919 while (ref_tokens
[i
]) {
920 gchar
*cur_ref
= ref_tokens
[i
];
921 msginfo
->references
= references_list_append(msginfo
->references
,
925 g_strfreev(ref_tokens
);
927 tmp
= g_strdup(item
->ovr_references
);
928 eliminate_parenthesis(tmp
, '(', ')');
929 if ((p
= strrchr(tmp
, '<')) != NULL
) {
930 extract_parenthesis(p
, '<', '>');
933 msginfo
->inreplyto
= g_strdup(p
);
941 gint
news_cancel_article(Folder
* folder
, MsgInfo
* msginfo
)
945 gchar date
[RFC822_DATE_BUFFSIZE
];
947 tmp
= g_strdup_printf("%s%ccancel%p", get_tmp_dir(),
948 G_DIR_SEPARATOR
, msginfo
);
952 if ((tmpfp
= claws_fopen(tmp
, "wb")) == NULL
) {
953 FILE_OP_ERROR(tmp
, "claws_fopen");
957 if (change_file_mode_rw(tmpfp
, tmp
) < 0) {
958 FILE_OP_ERROR(tmp
, "chmod");
959 g_warning("can't change file mode");
962 if (prefs_common
.hide_timezone
)
963 get_rfc822_date_hide_tz(date
, sizeof(date
));
965 get_rfc822_date(date
, sizeof(date
));
966 if (fprintf(tmpfp
, "From: %s\r\n"
968 "Subject: cmsg cancel <%s>\r\n"
969 "Control: cancel <%s>\r\n"
971 "X-Cancelled-by: %s\r\n"
974 "removed with Claws Mail\r\n",
982 FILE_OP_ERROR(tmp
, "fprintf");
989 if (claws_safe_fclose(tmpfp
) == EOF
) {
990 FILE_OP_ERROR(tmp
, "claws_fclose");
996 news_post(folder
, tmp
);
1004 static gchar
*news_folder_get_path(Folder
*folder
)
1008 cm_return_val_if_fail(folder
->account
!= NULL
, NULL
);
1010 folder_path
= g_strconcat(get_news_cache_dir(),
1012 folder
->account
->nntp_server
,
1017 static gchar
*news_item_get_path(Folder
*folder
, FolderItem
*item
)
1019 gchar
*folder_path
, *path
;
1021 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1022 cm_return_val_if_fail(item
!= NULL
, NULL
);
1023 folder_path
= news_folder_get_path(folder
);
1025 cm_return_val_if_fail(folder_path
!= NULL
, NULL
);
1026 if (g_path_is_absolute(folder_path
)) {
1028 path
= g_strconcat(folder_path
, G_DIR_SEPARATOR_S
,
1031 path
= g_strdup(folder_path
);
1034 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
1035 folder_path
, G_DIR_SEPARATOR_S
,
1038 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
1041 g_free(folder_path
);
1043 while (strchr(path
, '/'))
1044 *strchr(path
, '/') = '\\';
1049 static gint
news_get_num_list(Folder
*folder
, FolderItem
*item
, GSList
**msgnum_list
, gboolean
*old_uids_valid
)
1051 NewsSession
*session
;
1052 gint i
, ok
, num
, first
, last
, nummsgs
= 0;
1055 cm_return_val_if_fail(item
!= NULL
, -1);
1056 cm_return_val_if_fail(item
->folder
!= NULL
, -1);
1057 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
1059 session
= news_session_get(folder
);
1060 cm_return_val_if_fail(session
!= NULL
, -1);
1062 *old_uids_valid
= TRUE
;
1064 news_folder_lock(NEWS_FOLDER(item
->folder
));
1066 ok
= news_select_group(folder
, item
->path
, &num
, &first
, &last
);
1067 if (ok
!= NEWSNNTP_NO_ERROR
) {
1068 log_warning(LOG_PROTOCOL
, _("couldn't set group: %s\n"), item
->path
);
1069 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1073 dir
= news_folder_get_path(folder
);
1075 remove_all_numbered_files(dir
);
1076 else if (last
< first
)
1077 log_warning(LOG_PROTOCOL
, _("invalid article range: %d - %d\n"),
1080 for (i
= first
; i
<= last
; i
++) {
1081 *msgnum_list
= g_slist_prepend(*msgnum_list
,
1082 GINT_TO_POINTER(i
));
1085 debug_print("removing old messages from %d to %d in %s\n",
1087 remove_numbered_files(dir
, 1, first
- 1);
1090 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1094 static void news_set_msg_flags(FolderItem
*item
, MsgInfo
*msginfo
)
1096 msginfo
->flags
.tmp_flags
= 0;
1097 if (item
->folder
->account
->mark_crosspost_read
&& msginfo
->msgid
) {
1098 if (item
->folder
->newsart
&&
1099 g_hash_table_lookup(item
->folder
->newsart
, msginfo
->msgid
) != NULL
) {
1100 msginfo
->flags
.perm_flags
= MSG_COLORLABEL_TO_FLAGS(item
->folder
->account
->crosspost_col
);
1103 if (!item
->folder
->newsart
)
1104 item
->folder
->newsart
= g_hash_table_new(g_str_hash
, g_str_equal
);
1105 g_hash_table_insert(item
->folder
->newsart
,
1106 g_strdup(msginfo
->msgid
), GINT_TO_POINTER(1));
1107 msginfo
->flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
1110 msginfo
->flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
1114 static void news_get_extra_fields(NewsSession
*session
, FolderItem
*item
, GSList
*msglist
)
1116 MsgInfo
*msginfo
= NULL
;
1119 clist
*hdrlist
= NULL
;
1121 gint first
= -1, last
= -1;
1122 GHashTable
*hash_table
;
1124 cm_return_if_fail(session
!= NULL
);
1125 cm_return_if_fail(item
!= NULL
);
1126 cm_return_if_fail(item
->folder
!= NULL
);
1127 cm_return_if_fail(FOLDER_CLASS(item
->folder
) == &news_class
);
1129 if (msglist
== NULL
)
1132 news_folder_lock(NEWS_FOLDER(item
->folder
));
1134 hash_table
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1136 for (cur
= msglist
; cur
; cur
= cur
->next
) {
1137 msginfo
= (MsgInfo
*)cur
->data
;
1138 if (first
== -1 || msginfo
->msgnum
< first
)
1139 first
= msginfo
->msgnum
;
1140 if (last
== -1 || msginfo
->msgnum
> last
)
1141 last
= msginfo
->msgnum
;
1142 g_hash_table_insert(hash_table
,
1143 GINT_TO_POINTER(msginfo
->msgnum
), msginfo
);
1146 if (first
== -1 || last
== -1) {
1147 g_hash_table_destroy(hash_table
);
1152 ok
= nntp_threaded_xhdr(item
->folder
, "newsgroups", first
, last
, &hdrlist
);
1154 if (ok
!= NEWSNNTP_NO_ERROR
) {
1155 log_warning(LOG_PROTOCOL
, _("couldn't get xhdr\n"));
1156 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1157 session_destroy(SESSION(session
));
1158 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1160 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1161 if (hdrlist
!= NULL
)
1162 newsnntp_xhdr_free(hdrlist
);
1166 for (hdr
= clist_begin(hdrlist
); hdr
; hdr
= clist_next(hdr
)) {
1167 struct newsnntp_xhdr_resp_item
*hdrval
= clist_content(hdr
);
1168 msginfo
= g_hash_table_lookup(hash_table
, GINT_TO_POINTER(hdrval
->hdr_article
));
1170 if (msginfo
->newsgroups
)
1171 g_free(msginfo
->newsgroups
);
1172 msginfo
->newsgroups
= g_strdup(hdrval
->hdr_value
);
1175 newsnntp_xhdr_free(hdrlist
);
1179 ok
= nntp_threaded_xhdr(item
->folder
, "to", first
, last
, &hdrlist
);
1181 if (ok
!= NEWSNNTP_NO_ERROR
) {
1182 log_warning(LOG_PROTOCOL
, _("couldn't get xhdr\n"));
1183 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1184 session_destroy(SESSION(session
));
1185 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1187 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1188 if (hdrlist
!= NULL
)
1189 newsnntp_xhdr_free(hdrlist
);
1193 for (hdr
= clist_begin(hdrlist
); hdr
; hdr
= clist_next(hdr
)) {
1194 struct newsnntp_xhdr_resp_item
*hdrval
= clist_content(hdr
);
1195 msginfo
= g_hash_table_lookup(hash_table
, GINT_TO_POINTER(hdrval
->hdr_article
));
1198 g_free(msginfo
->to
);
1199 msginfo
->to
= g_strdup(hdrval
->hdr_value
);
1202 newsnntp_xhdr_free(hdrlist
);
1206 ok
= nntp_threaded_xhdr(item
->folder
, "cc", first
, last
, &hdrlist
);
1208 if (ok
!= NEWSNNTP_NO_ERROR
) {
1209 log_warning(LOG_PROTOCOL
, _("couldn't get xhdr\n"));
1210 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1211 session_destroy(SESSION(session
));
1212 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1214 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1215 if (hdrlist
!= NULL
)
1216 newsnntp_xhdr_free(hdrlist
);
1220 for (hdr
= clist_begin(hdrlist
); hdr
; hdr
= clist_next(hdr
)) {
1221 struct newsnntp_xhdr_resp_item
*hdrval
= clist_content(hdr
);
1222 msginfo
= g_hash_table_lookup(hash_table
, GINT_TO_POINTER(hdrval
->hdr_article
));
1225 g_free(msginfo
->cc
);
1226 msginfo
->cc
= g_strdup(hdrval
->hdr_value
);
1229 newsnntp_xhdr_free(hdrlist
);
1232 g_hash_table_destroy(hash_table
);
1233 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1236 static GSList
*news_get_msginfos_for_range(NewsSession
*session
, FolderItem
*item
, guint begin
, guint end
)
1238 GSList
*newlist
= NULL
;
1239 GSList
*llast
= NULL
;
1242 clist
*msglist
= NULL
;
1244 cm_return_val_if_fail(session
!= NULL
, NULL
);
1245 cm_return_val_if_fail(item
!= NULL
, NULL
);
1247 log_message(LOG_PROTOCOL
, _("getting xover %d - %d in %s...\n"),
1248 begin
, end
, item
->path
);
1250 news_folder_lock(NEWS_FOLDER(item
->folder
));
1252 ok
= news_select_group(item
->folder
, item
->path
, NULL
, NULL
, NULL
);
1253 if (ok
!= NEWSNNTP_NO_ERROR
) {
1254 log_warning(LOG_PROTOCOL
, _("couldn't set group: %s\n"), item
->path
);
1255 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1259 ok
= nntp_threaded_xover(item
->folder
, begin
, end
, NULL
, &msglist
);
1261 if (ok
!= NEWSNNTP_NO_ERROR
) {
1262 log_warning(LOG_PROTOCOL
, _("couldn't get xover\n"));
1263 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1264 session_destroy(SESSION(session
));
1265 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1267 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1268 if (msglist
!= NULL
)
1269 newsnntp_xover_resp_list_free(msglist
);
1274 for (cur
= clist_begin(msglist
); cur
; cur
= clist_next(cur
)) {
1275 struct newsnntp_xover_resp_item
*ritem
= (struct newsnntp_xover_resp_item
*)clist_content(cur
);
1276 msginfo
= news_parse_xover(ritem
);
1279 log_warning(LOG_PROTOCOL
, _("invalid xover line\n"));
1283 msginfo
->folder
= item
;
1284 news_set_msg_flags(item
, msginfo
);
1285 msginfo
->flags
.tmp_flags
|= MSG_NEWS
;
1288 llast
= newlist
= g_slist_append(newlist
, msginfo
);
1290 llast
= g_slist_append(llast
, msginfo
);
1291 llast
= llast
->next
;
1294 newsnntp_xover_resp_list_free(msglist
);
1297 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1299 session_set_access_time(SESSION(session
));
1301 news_get_extra_fields(session
, item
, newlist
);
1306 static MsgInfo
*news_get_msginfo(Folder
*folder
, FolderItem
*item
, gint num
)
1308 GSList
*msglist
= NULL
;
1309 NewsSession
*session
;
1310 MsgInfo
*msginfo
= NULL
;
1312 session
= news_session_get(folder
);
1313 cm_return_val_if_fail(session
!= NULL
, NULL
);
1314 cm_return_val_if_fail(item
!= NULL
, NULL
);
1315 cm_return_val_if_fail(item
->folder
!= NULL
, NULL
);
1316 cm_return_val_if_fail(FOLDER_CLASS(item
->folder
) == &news_class
, NULL
);
1318 msglist
= news_get_msginfos_for_range(session
, item
, num
, num
);
1321 msginfo
= msglist
->data
;
1323 g_slist_free(msglist
);
1328 static GSList
*news_get_msginfos(Folder
*folder
, FolderItem
*item
, GSList
*msgnum_list
)
1330 NewsSession
*session
;
1331 GSList
*elem
, *msginfo_list
= NULL
, *tmp_msgnum_list
, *tmp_msginfo_list
;
1332 guint first
, last
, next
;
1334 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1335 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
1336 cm_return_val_if_fail(msgnum_list
!= NULL
, NULL
);
1337 cm_return_val_if_fail(item
!= NULL
, NULL
);
1339 session
= news_session_get(folder
);
1340 cm_return_val_if_fail(session
!= NULL
, NULL
);
1342 tmp_msgnum_list
= g_slist_copy(msgnum_list
);
1343 tmp_msgnum_list
= g_slist_sort(tmp_msgnum_list
, g_int_compare
);
1345 progressindicator_start(PROGRESS_TYPE_NETWORK
);
1347 first
= GPOINTER_TO_INT(tmp_msgnum_list
->data
);
1350 news_folder_lock(NEWS_FOLDER(item
->folder
));
1352 for(elem
= g_slist_next(tmp_msgnum_list
); elem
!= NULL
; elem
= g_slist_next(elem
)) {
1353 next
= GPOINTER_TO_INT(elem
->data
);
1354 if(next
!= (last
+ 1)) {
1355 tmp_msginfo_list
= news_get_msginfos_for_range(session
, item
, first
, last
);
1356 msginfo_list
= g_slist_concat(msginfo_list
, tmp_msginfo_list
);
1362 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1364 tmp_msginfo_list
= news_get_msginfos_for_range(session
, item
, first
, last
);
1365 msginfo_list
= g_slist_concat(msginfo_list
, tmp_msginfo_list
);
1367 g_slist_free(tmp_msgnum_list
);
1369 progressindicator_stop(PROGRESS_TYPE_NETWORK
);
1371 return msginfo_list
;
1374 static gboolean
news_scan_required(Folder
*folder
, FolderItem
*item
)
1379 void news_synchronise(FolderItem
*item
, gint days
)
1381 news_gtk_synchronise(item
, days
);
1384 static gint
news_rename_folder(Folder
*folder
, FolderItem
*item
,
1389 cm_return_val_if_fail(folder
!= NULL
, -1);
1390 cm_return_val_if_fail(item
!= NULL
, -1);
1391 cm_return_val_if_fail(item
->path
!= NULL
, -1);
1392 cm_return_val_if_fail(name
!= NULL
, -1);
1394 path
= folder_item_get_path(item
);
1395 if (!is_dir_exist(path
))
1396 make_dir_hier(path
);
1399 item
->name
= g_strdup(name
);
1404 static gint
news_remove_folder(Folder
*folder
, FolderItem
*item
)
1408 cm_return_val_if_fail(folder
!= NULL
, -1);
1409 cm_return_val_if_fail(item
!= NULL
, -1);
1410 cm_return_val_if_fail(item
->path
!= NULL
, -1);
1412 path
= folder_item_get_path(item
);
1413 if (remove_dir_recursive(path
) < 0) {
1414 g_warning("can't remove directory '%s'", path
);
1420 folder_item_remove(item
);
1424 void nntp_disconnect_all(gboolean have_connectivity
)
1427 gboolean short_timeout
;
1428 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1432 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1434 short_timeout
= !networkmanager_is_online(&error
);
1436 short_timeout
= TRUE
;
1437 g_error_free(error
);
1440 short_timeout
= TRUE
;
1444 nntp_main_set_timeout(1);
1446 for (list
= account_get_list(); list
!= NULL
; list
= list
->next
) {
1447 PrefsAccount
*account
= list
->data
;
1448 if (account
->protocol
== A_NNTP
) {
1449 RemoteFolder
*folder
= (RemoteFolder
*)account
->folder
;
1450 if (folder
&& folder
->session
) {
1451 NewsSession
*session
= (NewsSession
*)folder
->session
;
1452 if (have_connectivity
)
1453 nntp_threaded_disconnect(FOLDER(folder
));
1454 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1455 SESSION(session
)->sock
= NULL
;
1456 session_destroy(SESSION(session
));
1457 folder
->session
= NULL
;
1463 nntp_main_set_timeout(prefs_common
.io_timeout_secs
);
1468 #include <glib/gi18n.h>
1469 #include <gtk/gtk.h>
1471 #include "alertpanel.h"
1473 static FolderClass news_class
;
1475 static void warn_etpan(void)
1477 static gboolean missing_news_warning
= TRUE
;
1478 if (missing_news_warning
) {
1479 missing_news_warning
= FALSE
;
1481 _("You have one or more News accounts "
1482 "defined. However this version of "
1483 "Claws Mail has been built without "
1484 "News support; your News accounts are "
1486 "You probably need to "
1487 "install libetpan and recompile "
1491 static Folder
*news_folder_new(const gchar
*name
, const gchar
*path
)
1496 void news_group_list_free(GSList
*group_list
)
1500 void news_remove_group_list_cache(Folder
*folder
)
1504 int news_folder_locked(Folder
*folder
)
1509 gint
news_post(Folder
*folder
, const gchar
*file
)
1515 gint
news_cancel_article(Folder
* folder
, MsgInfo
* msginfo
)
1521 GSList
*news_get_group_list(Folder
*folder
)
1528 FolderClass
*news_get_class(void)
1530 if (news_class
.idstr
== NULL
) {
1531 news_class
.type
= F_NEWS
;
1532 news_class
.idstr
= "news";
1533 news_class
.uistr
= "News";
1535 /* Folder functions */
1536 news_class
.new_folder
= news_folder_new
;
1542 void nntp_disconnect_all(gboolean have_connectivity
)