2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto 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/>.
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"
68 #define NNTPS_PORT 563
71 typedef struct _NewsFolder NewsFolder
;
72 typedef struct _NewsSession NewsSession
;
74 #define NEWS_FOLDER(obj) ((NewsFolder *)obj)
75 #define NEWS_SESSION(obj) ((NewsSession *)obj)
93 static void news_folder_init(Folder
*folder
, const gchar
*name
,
96 static Folder
*news_folder_new (const gchar
*name
,
98 static void news_folder_destroy (Folder
*folder
);
100 static gchar
*news_fetch_msg (Folder
*folder
,
103 static void news_remove_cached_msg (Folder
*folder
,
107 static Session
*news_session_new (Folder
*folder
,
114 static Session
*news_session_new (Folder
*folder
,
118 const gchar
*passwd
);
121 static gint
news_get_article (Folder
*folder
,
125 static gint
news_select_group (Folder
*folder
,
130 static MsgInfo
*news_parse_xover (struct newsnntp_xover_resp_item
*item
);
131 static gint
news_get_num_list (Folder
*folder
,
134 gboolean
*old_uids_valid
);
135 static MsgInfo
*news_get_msginfo (Folder
*folder
,
138 static GSList
*news_get_msginfos (Folder
*folder
,
140 GSList
*msgnum_list
);
141 static gboolean
news_scan_required (Folder
*folder
,
144 static gchar
*news_folder_get_path (Folder
*folder
);
145 static gchar
*news_item_get_path (Folder
*folder
,
147 static void news_synchronise (FolderItem
*item
, gint days
);
148 static int news_remove_msg (Folder
*folder
,
151 static gint
news_rename_folder (Folder
*folder
,
154 static gint
news_remove_folder (Folder
*folder
,
156 static FolderClass news_class
;
158 FolderClass
*news_get_class(void)
160 if (news_class
.idstr
== NULL
) {
161 news_class
.type
= F_NEWS
;
162 news_class
.idstr
= "news";
163 news_class
.uistr
= "News";
164 news_class
.supports_server_search
= FALSE
;
166 /* Folder functions */
167 news_class
.new_folder
= news_folder_new
;
168 news_class
.destroy_folder
= news_folder_destroy
;
170 /* FolderItem functions */
171 news_class
.item_get_path
= news_item_get_path
;
172 news_class
.get_num_list
= news_get_num_list
;
173 news_class
.scan_required
= news_scan_required
;
174 news_class
.rename_folder
= news_rename_folder
;
175 news_class
.remove_folder
= news_remove_folder
;
177 /* Message functions */
178 news_class
.get_msginfo
= news_get_msginfo
;
179 news_class
.get_msginfos
= news_get_msginfos
;
180 news_class
.fetch_msg
= news_fetch_msg
;
181 news_class
.synchronise
= news_synchronise
;
182 news_class
.search_msgs
= folder_item_search_msgs_local
;
183 news_class
.remove_msg
= news_remove_msg
;
184 news_class
.remove_cached_msg
= news_remove_cached_msg
;
190 guint
nntp_folder_get_refcnt(Folder
*folder
)
192 return ((NewsFolder
*)folder
)->refcnt
;
195 void nntp_folder_ref(Folder
*folder
)
197 ((NewsFolder
*)folder
)->refcnt
++;
200 void nntp_folder_unref(Folder
*folder
)
202 if (((NewsFolder
*)folder
)->refcnt
> 0)
203 ((NewsFolder
*)folder
)->refcnt
--;
206 static int news_remove_msg (Folder
*folder
,
210 gchar
*path
, *filename
;
212 cm_return_val_if_fail(folder
!= NULL
, -1);
213 cm_return_val_if_fail(item
!= NULL
, -1);
215 path
= folder_item_get_path(item
);
216 if (!is_dir_exist(path
))
219 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msgnum
), NULL
);
221 claws_unlink(filename
);
226 static void news_folder_lock(NewsFolder
*folder
)
228 folder
->lock_count
++;
231 static void news_folder_unlock(NewsFolder
*folder
)
233 if (folder
->lock_count
> 0)
234 folder
->lock_count
--;
237 int news_folder_locked(Folder
*folder
)
242 return NEWS_FOLDER(folder
)->lock_count
;
245 static Folder
*news_folder_new(const gchar
*name
, const gchar
*path
)
249 folder
= (Folder
*)g_new0(NewsFolder
, 1);
250 folder
->klass
= &news_class
;
251 news_folder_init(folder
, name
, path
);
256 static void news_folder_destroy(Folder
*folder
)
260 while (nntp_folder_get_refcnt(folder
) > 0)
261 gtk_main_iteration();
263 dir
= news_folder_get_path(folder
);
264 if (is_dir_exist(dir
))
265 remove_dir_recursive(dir
);
269 folder_remote_folder_destroy(REMOTE_FOLDER(folder
));
272 static void news_folder_init(Folder
*folder
, const gchar
*name
,
275 folder_remote_folder_init(folder
, name
, path
);
278 static void news_session_destroy(Session
*session
)
280 NewsSession
*news_session
= NEWS_SESSION(session
);
282 cm_return_if_fail(session
!= NULL
);
284 if (news_session
->group
)
285 g_free(news_session
->group
);
288 static gboolean
nntp_ping(gpointer data
)
290 Session
*session
= (Session
*)data
;
291 NewsSession
*news_session
= NEWS_SESSION(session
);
295 if (session
->state
!= SESSION_READY
|| news_folder_locked(news_session
->folder
))
298 news_folder_lock(NEWS_FOLDER(news_session
->folder
));
300 if ((r
= nntp_threaded_date(news_session
->folder
, <
)) != NEWSNNTP_NO_ERROR
) {
301 if (r
!= NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED
&&
302 r
!= NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD
) {
303 log_warning(LOG_PROTOCOL
, _("NNTP connection to %s:%d has been"
305 news_session
->folder
->account
->nntp_server
,
306 news_session
->folder
->account
->set_nntpport
?
307 news_session
->folder
->account
->nntpport
: NNTP_PORT
);
308 REMOTE_FOLDER(news_session
->folder
)->session
= NULL
;
309 news_folder_unlock(NEWS_FOLDER(news_session
->folder
));
310 session
->state
= SESSION_DISCONNECTED
;
311 session
->sock
= NULL
;
312 session_destroy(session
);
317 news_folder_unlock(NEWS_FOLDER(news_session
->folder
));
318 session_set_access_time(session
);
324 static Session
*news_session_new(Folder
*folder
, const gchar
*server
, gushort port
,
325 const gchar
*userid
, const gchar
*passwd
,
328 static Session
*news_session_new(Folder
*folder
, const gchar
*server
, gushort port
,
329 const gchar
*userid
, const gchar
*passwd
)
332 NewsSession
*session
;
334 cm_return_val_if_fail(server
!= NULL
, NULL
);
336 log_message(LOG_PROTOCOL
, _("Account '%s': Connecting to NNTP server: %s:%d...\n"),
337 folder
->account
->account_name
, server
, port
);
339 session
= g_new0(NewsSession
, 1);
340 session_init(SESSION(session
), folder
->account
, FALSE
);
341 SESSION(session
)->type
= SESSION_NEWS
;
342 SESSION(session
)->server
= g_strdup(server
);
343 SESSION(session
)->port
= port
;
344 SESSION(session
)->sock
= NULL
;
345 SESSION(session
)->destroy
= news_session_destroy
;
350 if (ssl_type
!= SSL_NONE
)
351 r
= nntp_threaded_connect_ssl(folder
, server
, port
);
354 r
= nntp_threaded_connect(folder
, server
, port
);
356 if (r
!= NEWSNNTP_NO_ERROR
) {
357 log_error(LOG_PROTOCOL
, _("Error logging in to %s:%d...\n"), server
, port
);
358 session_destroy(SESSION(session
));
362 session
->folder
= folder
;
364 return SESSION(session
);
367 static Session
*news_session_new_for_folder(Folder
*folder
)
371 const gchar
*userid
= NULL
;
372 gchar
*passwd
= NULL
;
376 cm_return_val_if_fail(folder
!= NULL
, NULL
);
377 cm_return_val_if_fail(folder
->account
!= NULL
, NULL
);
379 ac
= folder
->account
;
382 port
= ac
->set_nntpport
? ac
->nntpport
383 : ac
->ssl_nntp
? NNTPS_PORT
: NNTP_PORT
;
384 session
= news_session_new(folder
, ac
->nntp_server
, port
, userid
, passwd
,
387 if (ac
->ssl_nntp
!= SSL_NONE
) {
388 if (alertpanel_full(_("Insecure connection"),
389 _("This connection is configured to be secured "
390 "using SSL/TLS, but SSL/TLS is not available "
391 "in this build of Claws Mail. \n\n"
392 "Do you want to continue connecting to this "
393 "server? The communication would not be "
395 GTK_STOCK_CANCEL
, _("Con_tinue connecting"),
396 NULL
, FALSE
, NULL
, ALERT_WARNING
,
397 G_ALERTDEFAULT
) != G_ALERTALTERNATE
)
400 port
= ac
->set_nntpport
? ac
->nntpport
: NNTP_PORT
;
401 session
= news_session_new(folder
, ac
->nntp_server
, port
, userid
, passwd
);
404 if (ac
->use_nntp_auth
&& ac
->userid
&& ac
->userid
[0]) {
406 if (password_get(userid
, ac
->nntp_server
, "nntp", port
, &passwd
)) {
408 } else if ((passwd
= passwd_store_get_account(ac
->account_id
,
409 PWS_ACCOUNT_RECV
)) == NULL
) {
410 passwd
= input_dialog_query_password_keep(ac
->nntp_server
,
412 &(ac
->session_passwd
));
417 r
= nntp_threaded_mode_reader(folder
);
419 r
= NEWSNNTP_ERROR_CONNECTION_REFUSED
;
421 if (r
!= NEWSNNTP_NO_ERROR
) {
422 if (r
== NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME
) {
424 FIX ME when libetpan implements 480 to indicate authorization
425 is required to use this capability. Libetpan treats a 480 as a
426 381 which is clearly wrong.
430 Meaning: command unavailable until the client
431 has authenticated itself.
433 /* if the server does not advertise the capability MODE-READER,
434 we normally should not send MODE READER. However this can't
435 hurt: a transit-only server returns 502 and closes the cnx.
436 Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
438 log_error(LOG_PROTOCOL
, _("Libetpan does not support return code 480 "
439 "so for now we choose to continue\n"));
441 else if (r
== NEWSNNTP_ERROR_UNEXPECTED_RESPONSE
) {
442 /* if the server does not advertise the capability MODE-READER,
443 we normally should not send MODE READER. However this can't
444 hurt: a transit-only server returns 502 and closes the cnx.
445 Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
447 log_error(LOG_PROTOCOL
, _("Mode reader failed, continuing nevertheless\n"));
450 /* An error state bail out */
451 log_error(LOG_PROTOCOL
, _("Error creating session with %s:%d\n"), ac
->nntp_server
, port
);
453 session_destroy(SESSION(session
));
455 if (ac
->session_passwd
) {
456 g_free(ac
->session_passwd
);
457 ac
->session_passwd
= NULL
;
463 if ((session
!= NULL
) && ac
->use_nntp_auth
) { /* FIXME: && ac->use_nntp_auth_onconnect */
464 if (nntp_threaded_login(folder
, userid
, passwd
) !=
466 log_error(LOG_PROTOCOL
, _("Error authenticating to %s:%d...\n"), ac
->nntp_server
, port
);
467 session_destroy(SESSION(session
));
469 if (ac
->session_passwd
) {
470 g_free(ac
->session_passwd
);
471 ac
->session_passwd
= NULL
;
481 static NewsSession
*news_session_get(Folder
*folder
)
483 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
485 cm_return_val_if_fail(folder
!= NULL
, NULL
);
486 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
487 cm_return_val_if_fail(folder
->account
!= NULL
, NULL
);
489 if (prefs_common
.work_offline
&&
490 !inc_offline_should_override(FALSE
,
491 _("Claws Mail needs network access in order "
492 "to access the News server."))) {
496 if (!rfolder
->session
) {
497 rfolder
->session
= news_session_new_for_folder(folder
);
498 session_register_ping(SESSION(rfolder
->session
), nntp_ping
);
499 return NEWS_SESSION(rfolder
->session
);
502 /* Handle port change (also ssl/nossl change) without needing to
503 * restart application. */
504 if (rfolder
->session
->port
!= folder
->account
->nntpport
) {
505 session_destroy(rfolder
->session
);
506 rfolder
->session
= news_session_new_for_folder(folder
);
507 session_register_ping(SESSION(rfolder
->session
), nntp_ping
);
511 if (time(NULL
) - rfolder
->session
->last_access_time
<
512 SESSION_TIMEOUT_INTERVAL
) {
513 return NEWS_SESSION(rfolder
->session
);
516 if (!nntp_ping(rfolder
->session
)) {
517 rfolder
->session
= news_session_new_for_folder(folder
);
518 session_register_ping(SESSION(rfolder
->session
), nntp_ping
);
522 if (rfolder
->session
)
523 session_set_access_time(rfolder
->session
);
525 return NEWS_SESSION(rfolder
->session
);
528 static void news_remove_cached_msg(Folder
*folder
, FolderItem
*item
, MsgInfo
*msginfo
)
530 gchar
*path
, *filename
;
532 path
= folder_item_get_path(item
);
534 if (!is_dir_exist(path
)) {
539 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msginfo
->msgnum
), NULL
);
542 if (is_file_exist(filename
)) {
543 claws_unlink(filename
);
548 static gchar
*news_fetch_msg(Folder
*folder
, FolderItem
*item
, gint num
)
550 gchar
*path
, *filename
;
551 NewsSession
*session
;
554 cm_return_val_if_fail(folder
!= NULL
, NULL
);
555 cm_return_val_if_fail(item
!= NULL
, NULL
);
557 path
= folder_item_get_path(item
);
558 if (!is_dir_exist(path
))
560 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(num
), NULL
);
563 if (is_file_exist(filename
)) {
564 debug_print("article %d has been already cached.\n", num
);
568 session
= news_session_get(folder
);
574 ok
= news_select_group(folder
, item
->path
, NULL
, NULL
, NULL
);
575 if (ok
!= NEWSNNTP_NO_ERROR
) {
576 if (ok
== NEWSNNTP_ERROR_STREAM
) {
577 session_destroy(SESSION(session
));
578 REMOTE_FOLDER(folder
)->session
= NULL
;
584 debug_print("getting article %d...\n", num
);
585 ok
= news_get_article(folder
,
587 if (ok
!= NEWSNNTP_NO_ERROR
) {
588 g_warning("can't read article %d", num
);
589 if (ok
== NEWSNNTP_ERROR_STREAM
) {
590 session_destroy(SESSION(session
));
591 REMOTE_FOLDER(folder
)->session
= NULL
;
600 static NewsGroupInfo
*news_group_info_new(const gchar
*name
,
601 gint first
, gint last
, gchar type
)
603 NewsGroupInfo
*ginfo
;
605 ginfo
= g_new(NewsGroupInfo
, 1);
606 ginfo
->name
= g_strdup(name
);
607 ginfo
->first
= first
;
614 static void news_group_info_free(NewsGroupInfo
*ginfo
)
620 static gint
news_group_info_compare(NewsGroupInfo
*ginfo1
,
621 NewsGroupInfo
*ginfo2
)
623 return g_ascii_strcasecmp(ginfo1
->name
, ginfo2
->name
);
626 GSList
*news_get_group_list(Folder
*folder
)
628 gchar
*path
, *filename
;
634 cm_return_val_if_fail(folder
!= NULL
, NULL
);
635 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
637 path
= folder_item_get_path(FOLDER_ITEM(folder
->node
->data
));
638 if (!is_dir_exist(path
))
640 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, NEWSGROUP_LIST
, NULL
);
643 if ((fp
= g_fopen(filename
, "rb")) == NULL
) {
644 NewsSession
*session
;
646 clist
*grouplist
= NULL
;
648 fp
= g_fopen(filename
, "wb");
654 session
= news_session_get(folder
);
661 ok
= nntp_threaded_list(folder
, &grouplist
);
663 if (ok
!= NEWSNNTP_NO_ERROR
) {
664 if (ok
== NEWSNNTP_ERROR_STREAM
) {
665 session_destroy(SESSION(session
));
666 REMOTE_FOLDER(folder
)->session
= NULL
;
674 for (cur
= clist_begin(grouplist
); cur
; cur
= clist_next(cur
)) {
675 struct newsnntp_group_info
*info
= (struct newsnntp_group_info
*)
677 if (fprintf(fp
, "%s %d %d %c\n",
681 info
->grp_type
) < 0) {
682 log_error(LOG_PROTOCOL
, ("Can't write newsgroup list\n"));
683 session_destroy(SESSION(session
));
684 REMOTE_FOLDER(folder
)->session
= NULL
;
687 newsnntp_list_free(grouplist
);
691 newsnntp_list_free(grouplist
);
693 if (fclose(fp
) == EOF
) {
694 log_error(LOG_PROTOCOL
, ("Can't write newsgroup list\n"));
695 session_destroy(SESSION(session
));
696 REMOTE_FOLDER(folder
)->session
= NULL
;
701 if ((fp
= g_fopen(filename
, "rb")) == NULL
) {
702 FILE_OP_ERROR(filename
, "fopen");
708 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
714 NewsGroupInfo
*ginfo
;
722 if (sscanf(p
, "%d %d %c", &last_num
, &first_num
, &type
) < 3)
725 ginfo
= news_group_info_new(name
, first_num
, last_num
, type
);
728 last
= list
= g_slist_append(NULL
, ginfo
);
730 last
= g_slist_append(last
, ginfo
);
738 list
= g_slist_sort(list
, (GCompareFunc
)news_group_info_compare
);
743 void news_group_list_free(GSList
*group_list
)
747 if (!group_list
) return;
749 for (cur
= group_list
; cur
!= NULL
; cur
= cur
->next
)
750 news_group_info_free((NewsGroupInfo
*)cur
->data
);
751 g_slist_free(group_list
);
754 void news_remove_group_list_cache(Folder
*folder
)
756 gchar
*path
, *filename
;
758 cm_return_if_fail(folder
!= NULL
);
759 cm_return_if_fail(FOLDER_CLASS(folder
) == &news_class
);
761 path
= folder_item_get_path(FOLDER_ITEM(folder
->node
->data
));
762 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, NEWSGROUP_LIST
, NULL
);
765 if (is_file_exist(filename
)) {
766 if (claws_unlink(filename
) < 0)
767 FILE_OP_ERROR(filename
, "remove");
772 gint
news_post(Folder
*folder
, const gchar
*file
)
775 char *contents
= file_read_to_str_no_recode(file
);
776 NewsSession
*session
;
778 cm_return_val_if_fail(folder
!= NULL
, -1);
779 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
780 cm_return_val_if_fail(contents
!= NULL
, -1);
782 session
= news_session_get(folder
);
788 ok
= nntp_threaded_post(folder
, contents
, strlen(contents
));
792 if (ok
== NEWSNNTP_ERROR_STREAM
) {
793 session_destroy(SESSION(session
));
794 REMOTE_FOLDER(folder
)->session
= NULL
;
797 return (ok
== NEWSNNTP_NO_ERROR
? 0 : -1);
800 static gint
news_get_article(Folder
*folder
, gint num
, gchar
*filename
)
806 r
= nntp_threaded_article(folder
, num
, &result
, &len
);
808 if (r
== NEWSNNTP_NO_ERROR
) {
809 if (str_write_to_file(result
, filename
) < 0) {
810 mmap_string_unref(result
);
813 mmap_string_unref(result
);
821 * @session: Active NNTP session.
822 * @group: Newsgroup name.
823 * @num: Estimated number of articles.
824 * @first: First article number.
825 * @last: Last article number.
827 * Select newsgroup @group with the GROUP command if it is not already
828 * selected in @session, or article numbers need to be returned.
830 * Return value: NNTP result code.
832 static gint
news_select_group(Folder
*folder
, const gchar
*group
,
833 gint
*num
, gint
*first
, gint
*last
)
836 gint num_
, first_
, last_
;
837 struct newsnntp_group_info
*info
= NULL
;
838 NewsSession
*session
= NEWS_SESSION(news_session_get(folder
));
840 cm_return_val_if_fail(session
!= NULL
, -1);
842 if (!num
|| !first
|| !last
) {
843 if (session
->group
&& g_ascii_strcasecmp(session
->group
, group
) == 0)
844 return NEWSNNTP_NO_ERROR
;
850 g_free(session
->group
);
851 session
->group
= NULL
;
853 ok
= nntp_threaded_group(folder
, group
, &info
);
855 if (ok
== NEWSNNTP_NO_ERROR
&& info
) {
856 session
->group
= g_strdup(group
);
857 *num
= info
->grp_first
;
858 *first
= info
->grp_first
;
859 *last
= info
->grp_last
;
860 newsnntp_group_free(info
);
862 log_warning(LOG_PROTOCOL
, _("couldn't select group: %s\n"), group
);
867 static MsgInfo
*news_parse_xover(struct newsnntp_xover_resp_item
*item
)
872 msginfo
= procmsg_msginfo_new();
873 msginfo
->msgnum
= item
->ovr_article
;
874 msginfo
->size
= item
->ovr_size
;
876 msginfo
->date
= g_strdup(item
->ovr_date
);
877 msginfo
->date_t
= procheader_date_parse(NULL
, item
->ovr_date
, 0);
879 msginfo
->from
= conv_unmime_header(item
->ovr_author
, NULL
, TRUE
);
880 msginfo
->fromname
= procheader_get_fromname(msginfo
->from
);
882 msginfo
->subject
= conv_unmime_header(item
->ovr_subject
, NULL
, TRUE
);
884 remove_return(msginfo
->from
);
885 remove_return(msginfo
->fromname
);
886 remove_return(msginfo
->subject
);
888 if (item
->ovr_message_id
) {
889 gchar
*tmp
= g_strdup(item
->ovr_message_id
);
890 extract_parenthesis(tmp
, '<', '>');
893 msginfo
->msgid
= g_strdup(tmp
);
897 /* FIXME: this is a quick fix; references' meaning was changed
898 * into having the actual list of references in the References: header.
899 * We need a GSList here, so msginfo_free() and msginfo_copy() can do
900 * their things properly. */
901 if (item
->ovr_references
&& *(item
->ovr_references
)) {
902 gchar
**ref_tokens
= g_strsplit(item
->ovr_references
, " ", -1);
906 while (ref_tokens
[i
]) {
907 gchar
*cur_ref
= ref_tokens
[i
];
908 msginfo
->references
= references_list_append(msginfo
->references
,
912 g_strfreev(ref_tokens
);
914 tmp
= g_strdup(item
->ovr_references
);
915 eliminate_parenthesis(tmp
, '(', ')');
916 if ((p
= strrchr(tmp
, '<')) != NULL
) {
917 extract_parenthesis(p
, '<', '>');
920 msginfo
->inreplyto
= g_strdup(p
);
928 gint
news_cancel_article(Folder
* folder
, MsgInfo
* msginfo
)
934 tmp
= g_strdup_printf("%s%ccancel%p", get_tmp_dir(),
935 G_DIR_SEPARATOR
, msginfo
);
939 if ((tmpfp
= g_fopen(tmp
, "wb")) == NULL
) {
940 FILE_OP_ERROR(tmp
, "fopen");
943 if (change_file_mode_rw(tmpfp
, tmp
) < 0) {
944 FILE_OP_ERROR(tmp
, "chmod");
945 g_warning("can't change file mode");
948 if (prefs_common
.hide_timezone
)
949 get_rfc822_date_hide_tz(buf
, sizeof(buf
));
951 get_rfc822_date(buf
, sizeof(buf
));
952 if (fprintf(tmpfp
, "From: %s\r\n"
954 "Subject: cmsg cancel <%s>\r\n"
955 "Control: cancel <%s>\r\n"
957 "X-Cancelled-by: %s\r\n"
960 "removed with Claws Mail\r\n",
968 FILE_OP_ERROR(tmp
, "fprintf");
975 if (fclose(tmpfp
) == EOF
) {
976 FILE_OP_ERROR(tmp
, "fclose");
982 news_post(folder
, tmp
);
990 static gchar
*news_folder_get_path(Folder
*folder
)
994 cm_return_val_if_fail(folder
->account
!= NULL
, NULL
);
996 folder_path
= g_strconcat(get_news_cache_dir(),
998 folder
->account
->nntp_server
,
1003 static gchar
*news_item_get_path(Folder
*folder
, FolderItem
*item
)
1005 gchar
*folder_path
, *path
;
1007 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1008 cm_return_val_if_fail(item
!= NULL
, NULL
);
1009 folder_path
= news_folder_get_path(folder
);
1011 cm_return_val_if_fail(folder_path
!= NULL
, NULL
);
1012 if (g_path_is_absolute(folder_path
)) {
1014 path
= g_strconcat(folder_path
, G_DIR_SEPARATOR_S
,
1017 path
= g_strdup(folder_path
);
1020 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
1021 folder_path
, G_DIR_SEPARATOR_S
,
1024 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
1027 g_free(folder_path
);
1029 while (strchr(path
, '/'))
1030 *strchr(path
, '/') = '\\';
1035 static gint
news_get_num_list(Folder
*folder
, FolderItem
*item
, GSList
**msgnum_list
, gboolean
*old_uids_valid
)
1037 NewsSession
*session
;
1038 gint i
, ok
, num
, first
, last
, nummsgs
= 0;
1041 cm_return_val_if_fail(item
!= NULL
, -1);
1042 cm_return_val_if_fail(item
->folder
!= NULL
, -1);
1043 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
1045 session
= news_session_get(folder
);
1046 cm_return_val_if_fail(session
!= NULL
, -1);
1048 *old_uids_valid
= TRUE
;
1050 news_folder_lock(NEWS_FOLDER(item
->folder
));
1052 ok
= news_select_group(folder
, item
->path
, &num
, &first
, &last
);
1053 if (ok
!= NEWSNNTP_NO_ERROR
) {
1054 log_warning(LOG_PROTOCOL
, _("couldn't set group: %s\n"), item
->path
);
1055 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1059 dir
= news_folder_get_path(folder
);
1061 remove_all_numbered_files(dir
);
1062 else if (last
< first
)
1063 log_warning(LOG_PROTOCOL
, _("invalid article range: %d - %d\n"),
1066 for (i
= first
; i
<= last
; i
++) {
1067 *msgnum_list
= g_slist_prepend(*msgnum_list
,
1068 GINT_TO_POINTER(i
));
1071 debug_print("removing old messages from %d to %d in %s\n",
1073 remove_numbered_files(dir
, 1, first
- 1);
1076 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1080 static void news_set_msg_flags(FolderItem
*item
, MsgInfo
*msginfo
)
1082 msginfo
->flags
.tmp_flags
= 0;
1083 if (item
->folder
->account
->mark_crosspost_read
&& msginfo
->msgid
) {
1084 if (item
->folder
->newsart
&&
1085 g_hash_table_lookup(item
->folder
->newsart
, msginfo
->msgid
) != NULL
) {
1086 msginfo
->flags
.perm_flags
= MSG_COLORLABEL_TO_FLAGS(item
->folder
->account
->crosspost_col
);
1089 if (!item
->folder
->newsart
)
1090 item
->folder
->newsart
= g_hash_table_new(g_str_hash
, g_str_equal
);
1091 g_hash_table_insert(item
->folder
->newsart
,
1092 g_strdup(msginfo
->msgid
), GINT_TO_POINTER(1));
1093 msginfo
->flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
1096 msginfo
->flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
1100 static void news_get_extra_fields(NewsSession
*session
, FolderItem
*item
, GSList
*msglist
)
1102 MsgInfo
*msginfo
= NULL
;
1105 clist
*hdrlist
= NULL
;
1107 gint first
= -1, last
= -1;
1108 GHashTable
*hash_table
;
1110 cm_return_if_fail(session
!= NULL
);
1111 cm_return_if_fail(item
!= NULL
);
1112 cm_return_if_fail(item
->folder
!= NULL
);
1113 cm_return_if_fail(FOLDER_CLASS(item
->folder
) == &news_class
);
1115 news_folder_lock(NEWS_FOLDER(item
->folder
));
1117 hash_table
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
1119 for (cur
= msglist
; cur
; cur
= cur
->next
) {
1120 msginfo
= (MsgInfo
*)cur
->data
;
1121 if (first
== -1 || msginfo
->msgnum
< first
)
1122 first
= msginfo
->msgnum
;
1123 if (last
== -1 || msginfo
->msgnum
> last
)
1124 last
= msginfo
->msgnum
;
1125 g_hash_table_insert(hash_table
,
1126 GINT_TO_POINTER(msginfo
->msgnum
), msginfo
);
1130 ok
= nntp_threaded_xhdr(item
->folder
, "newsgroups", first
, last
, &hdrlist
);
1132 if (ok
!= NEWSNNTP_NO_ERROR
) {
1133 log_warning(LOG_PROTOCOL
, _("couldn't get xhdr\n"));
1134 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1135 session_destroy(SESSION(session
));
1136 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1138 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1142 for (hdr
= clist_begin(hdrlist
); hdr
; hdr
= clist_next(hdr
)) {
1143 struct newsnntp_xhdr_resp_item
*hdrval
= clist_content(hdr
);
1144 msginfo
= g_hash_table_lookup(hash_table
, GINT_TO_POINTER(hdrval
->hdr_article
));
1146 if (msginfo
->newsgroups
)
1147 g_free(msginfo
->newsgroups
);
1148 msginfo
->newsgroups
= g_strdup(hdrval
->hdr_value
);
1151 newsnntp_xhdr_free(hdrlist
);
1154 ok
= nntp_threaded_xhdr(item
->folder
, "to", first
, last
, &hdrlist
);
1156 if (ok
!= NEWSNNTP_NO_ERROR
) {
1157 log_warning(LOG_PROTOCOL
, _("couldn't get xhdr\n"));
1158 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1159 session_destroy(SESSION(session
));
1160 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1162 news_folder_unlock(NEWS_FOLDER(item
->folder
));
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
));
1171 g_free(msginfo
->to
);
1172 msginfo
->to
= g_strdup(hdrval
->hdr_value
);
1175 newsnntp_xhdr_free(hdrlist
);
1178 ok
= nntp_threaded_xhdr(item
->folder
, "cc", first
, last
, &hdrlist
);
1180 if (ok
!= NEWSNNTP_NO_ERROR
) {
1181 log_warning(LOG_PROTOCOL
, _("couldn't get xhdr\n"));
1182 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1183 session_destroy(SESSION(session
));
1184 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1186 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1190 for (hdr
= clist_begin(hdrlist
); hdr
; hdr
= clist_next(hdr
)) {
1191 struct newsnntp_xhdr_resp_item
*hdrval
= clist_content(hdr
);
1192 msginfo
= g_hash_table_lookup(hash_table
, GINT_TO_POINTER(hdrval
->hdr_article
));
1195 g_free(msginfo
->cc
);
1196 msginfo
->cc
= g_strdup(hdrval
->hdr_value
);
1199 newsnntp_xhdr_free(hdrlist
);
1202 g_hash_table_destroy(hash_table
);
1203 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1206 static GSList
*news_get_msginfos_for_range(NewsSession
*session
, FolderItem
*item
, guint begin
, guint end
)
1208 GSList
*newlist
= NULL
;
1209 GSList
*llast
= NULL
;
1212 clist
*msglist
= NULL
;
1214 cm_return_val_if_fail(session
!= NULL
, NULL
);
1215 cm_return_val_if_fail(item
!= NULL
, NULL
);
1217 log_message(LOG_PROTOCOL
, _("getting xover %d - %d in %s...\n"),
1218 begin
, end
, item
->path
);
1220 news_folder_lock(NEWS_FOLDER(item
->folder
));
1222 ok
= news_select_group(item
->folder
, item
->path
, NULL
, NULL
, NULL
);
1223 if (ok
!= NEWSNNTP_NO_ERROR
) {
1224 log_warning(LOG_PROTOCOL
, _("couldn't set group: %s\n"), item
->path
);
1225 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1229 ok
= nntp_threaded_xover(item
->folder
, begin
, end
, NULL
, &msglist
);
1231 if (ok
!= NEWSNNTP_NO_ERROR
) {
1232 log_warning(LOG_PROTOCOL
, _("couldn't get xover\n"));
1233 if (ok
== NEWSNNTP_ERROR_STREAM
) {
1234 session_destroy(SESSION(session
));
1235 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1237 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1242 for (cur
= clist_begin(msglist
); cur
; cur
= clist_next(cur
)) {
1243 struct newsnntp_xover_resp_item
*ritem
= (struct newsnntp_xover_resp_item
*)clist_content(cur
);
1244 msginfo
= news_parse_xover(ritem
);
1247 log_warning(LOG_PROTOCOL
, _("invalid xover line\n"));
1251 msginfo
->folder
= item
;
1252 news_set_msg_flags(item
, msginfo
);
1253 msginfo
->flags
.tmp_flags
|= MSG_NEWS
;
1256 llast
= newlist
= g_slist_append(newlist
, msginfo
);
1258 llast
= g_slist_append(llast
, msginfo
);
1259 llast
= llast
->next
;
1262 newsnntp_xover_resp_list_free(msglist
);
1265 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1267 session_set_access_time(SESSION(session
));
1269 news_get_extra_fields(session
, item
, newlist
);
1274 static MsgInfo
*news_get_msginfo(Folder
*folder
, FolderItem
*item
, gint num
)
1276 GSList
*msglist
= NULL
;
1277 NewsSession
*session
;
1278 MsgInfo
*msginfo
= NULL
;
1280 session
= news_session_get(folder
);
1281 cm_return_val_if_fail(session
!= NULL
, NULL
);
1282 cm_return_val_if_fail(item
!= NULL
, NULL
);
1283 cm_return_val_if_fail(item
->folder
!= NULL
, NULL
);
1284 cm_return_val_if_fail(FOLDER_CLASS(item
->folder
) == &news_class
, NULL
);
1286 msglist
= news_get_msginfos_for_range(session
, item
, num
, num
);
1289 msginfo
= msglist
->data
;
1291 g_slist_free(msglist
);
1296 static GSList
*news_get_msginfos(Folder
*folder
, FolderItem
*item
, GSList
*msgnum_list
)
1298 NewsSession
*session
;
1299 GSList
*elem
, *msginfo_list
= NULL
, *tmp_msgnum_list
, *tmp_msginfo_list
;
1300 guint first
, last
, next
;
1302 cm_return_val_if_fail(folder
!= NULL
, NULL
);
1303 cm_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
1304 cm_return_val_if_fail(msgnum_list
!= NULL
, NULL
);
1305 cm_return_val_if_fail(item
!= NULL
, NULL
);
1307 session
= news_session_get(folder
);
1308 cm_return_val_if_fail(session
!= NULL
, NULL
);
1310 tmp_msgnum_list
= g_slist_copy(msgnum_list
);
1311 tmp_msgnum_list
= g_slist_sort(tmp_msgnum_list
, g_int_compare
);
1313 progressindicator_start(PROGRESS_TYPE_NETWORK
);
1315 first
= GPOINTER_TO_INT(tmp_msgnum_list
->data
);
1318 news_folder_lock(NEWS_FOLDER(item
->folder
));
1320 for(elem
= g_slist_next(tmp_msgnum_list
); elem
!= NULL
; elem
= g_slist_next(elem
)) {
1321 next
= GPOINTER_TO_INT(elem
->data
);
1322 if(next
!= (last
+ 1)) {
1323 tmp_msginfo_list
= news_get_msginfos_for_range(session
, item
, first
, last
);
1324 msginfo_list
= g_slist_concat(msginfo_list
, tmp_msginfo_list
);
1330 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1332 tmp_msginfo_list
= news_get_msginfos_for_range(session
, item
, first
, last
);
1333 msginfo_list
= g_slist_concat(msginfo_list
, tmp_msginfo_list
);
1335 g_slist_free(tmp_msgnum_list
);
1337 progressindicator_stop(PROGRESS_TYPE_NETWORK
);
1339 return msginfo_list
;
1342 static gboolean
news_scan_required(Folder
*folder
, FolderItem
*item
)
1347 void news_synchronise(FolderItem
*item
, gint days
)
1349 news_gtk_synchronise(item
, days
);
1352 static gint
news_rename_folder(Folder
*folder
, FolderItem
*item
,
1357 cm_return_val_if_fail(folder
!= NULL
, -1);
1358 cm_return_val_if_fail(item
!= NULL
, -1);
1359 cm_return_val_if_fail(item
->path
!= NULL
, -1);
1360 cm_return_val_if_fail(name
!= NULL
, -1);
1362 path
= folder_item_get_path(item
);
1363 if (!is_dir_exist(path
))
1364 make_dir_hier(path
);
1367 item
->name
= g_strdup(name
);
1372 static gint
news_remove_folder(Folder
*folder
, FolderItem
*item
)
1376 cm_return_val_if_fail(folder
!= NULL
, -1);
1377 cm_return_val_if_fail(item
!= NULL
, -1);
1378 cm_return_val_if_fail(item
->path
!= NULL
, -1);
1380 path
= folder_item_get_path(item
);
1381 if (remove_dir_recursive(path
) < 0) {
1382 g_warning("can't remove directory '%s'", path
);
1388 folder_item_remove(item
);
1392 void nntp_disconnect_all(gboolean have_connectivity
)
1395 gboolean short_timeout
;
1396 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1400 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1402 short_timeout
= !networkmanager_is_online(&error
);
1404 short_timeout
= TRUE
;
1405 g_error_free(error
);
1408 short_timeout
= TRUE
;
1412 nntp_main_set_timeout(1);
1414 for (list
= account_get_list(); list
!= NULL
; list
= list
->next
) {
1415 PrefsAccount
*account
= list
->data
;
1416 if (account
->protocol
== A_NNTP
) {
1417 RemoteFolder
*folder
= (RemoteFolder
*)account
->folder
;
1418 if (folder
&& folder
->session
) {
1419 NewsSession
*session
= (NewsSession
*)folder
->session
;
1420 if (have_connectivity
)
1421 nntp_threaded_disconnect(FOLDER(folder
));
1422 SESSION(session
)->state
= SESSION_DISCONNECTED
;
1423 SESSION(session
)->sock
= NULL
;
1424 session_destroy(SESSION(session
));
1425 folder
->session
= NULL
;
1431 nntp_main_set_timeout(prefs_common
.io_timeout_secs
);
1436 #include <glib/gi18n.h>
1437 #include <gtk/gtk.h>
1439 #include "alertpanel.h"
1441 static FolderClass news_class
;
1443 static void warn_etpan(void)
1445 static gboolean missing_news_warning
= TRUE
;
1446 if (missing_news_warning
) {
1447 missing_news_warning
= FALSE
;
1449 _("You have one or more News accounts "
1450 "defined. However this version of "
1451 "Claws Mail has been built without "
1452 "News support; your News accounts are "
1454 "You probably need to "
1455 "install libetpan and recompile "
1459 static Folder
*news_folder_new(const gchar
*name
, const gchar
*path
)
1464 void news_group_list_free(GSList
*group_list
)
1468 void news_remove_group_list_cache(Folder
*folder
)
1472 int news_folder_locked(Folder
*folder
)
1477 gint
news_post(Folder
*folder
, const gchar
*file
)
1483 gint
news_cancel_article(Folder
* folder
, MsgInfo
* msginfo
)
1489 GSList
*news_get_group_list(Folder
*folder
)
1496 FolderClass
*news_get_class(void)
1498 if (news_class
.idstr
== NULL
) {
1499 news_class
.type
= F_NEWS
;
1500 news_class
.idstr
= "news";
1501 news_class
.uistr
= "News";
1503 /* Folder functions */
1504 news_class
.new_folder
= news_folder_new
;
1510 void nntp_disconnect_all(gboolean have_connectivity
)