2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
41 #include "procheader.h"
44 #include "statusbar.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
49 #include "inputdialog.h"
51 #include "progressindicator.h"
52 #include "remotefolder.h"
53 #include "alertpanel.h"
62 #define NNTPS_PORT 563
65 typedef struct _NewsFolder NewsFolder
;
67 #define NEWS_FOLDER(obj) ((NewsFolder *)obj)
77 static void news_folder_init (Folder
*folder
,
81 static Folder
*news_folder_new (const gchar
*name
,
83 static void news_folder_destroy (Folder
*folder
);
85 static gchar
*news_fetch_msg (Folder
*folder
,
91 static Session
*news_session_new (const gchar
*server
,
97 static Session
*news_session_new (const gchar
*server
,
100 const gchar
*passwd
);
103 static gint
news_get_article_cmd (NNTPSession
*session
,
107 static gint
news_get_article (NNTPSession
*session
,
111 static gint
news_select_group (NNTPSession
*session
,
116 static MsgInfo
*news_parse_xover (const gchar
*xover_str
);
117 static gchar
*news_parse_xhdr (const gchar
*xhdr_str
,
119 gint
news_get_num_list (Folder
*folder
,
122 gboolean
*old_uids_valid
);
123 MsgInfo
*news_get_msginfo (Folder
*folder
,
126 GSList
*news_get_msginfos (Folder
*folder
,
128 GSList
*msgnum_list
);
129 gboolean
news_scan_required (Folder
*folder
,
132 gint
news_post_stream (Folder
*folder
,
134 static gchar
*news_folder_get_path (Folder
*folder
);
135 gchar
*news_item_get_path (Folder
*folder
,
137 static void news_synchronise (FolderItem
*item
);
138 static int news_remove_msg (Folder
*folder
,
141 static gint
news_remove_folder (Folder
*folder
,
143 static FolderClass news_class
;
145 FolderClass
*news_get_class(void)
147 if (news_class
.idstr
== NULL
) {
148 news_class
.type
= F_NEWS
;
149 news_class
.idstr
= "news";
150 news_class
.uistr
= "News";
152 /* Folder functions */
153 news_class
.new_folder
= news_folder_new
;
154 news_class
.destroy_folder
= news_folder_destroy
;
156 /* FolderItem functions */
157 news_class
.item_get_path
= news_item_get_path
;
158 news_class
.get_num_list
= news_get_num_list
;
159 news_class
.scan_required
= news_scan_required
;
160 news_class
.remove_folder
= news_remove_folder
;
162 /* Message functions */
163 news_class
.get_msginfo
= news_get_msginfo
;
164 news_class
.get_msginfos
= news_get_msginfos
;
165 news_class
.fetch_msg
= news_fetch_msg
;
166 news_class
.synchronise
= news_synchronise
;
167 news_class
.remove_msg
= news_remove_msg
;
173 static int news_remove_msg (Folder
*folder
,
177 gchar
*path
, *filename
;
179 g_return_val_if_fail(folder
!= NULL
, -1);
180 g_return_val_if_fail(item
!= NULL
, -1);
182 path
= folder_item_get_path(item
);
183 if (!is_dir_exist(path
))
186 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(msgnum
), NULL
);
193 static void news_folder_lock(NewsFolder
*folder
)
195 folder
->lock_count
++;
198 static void news_folder_unlock(NewsFolder
*folder
)
200 if (folder
->lock_count
> 0)
201 folder
->lock_count
--;
204 int news_folder_locked(Folder
*folder
)
209 return NEWS_FOLDER(folder
)->lock_count
;
212 static Folder
*news_folder_new(const gchar
*name
, const gchar
*path
)
216 folder
= (Folder
*)g_new0(NewsFolder
, 1);
217 folder
->klass
= &news_class
;
218 news_folder_init(folder
, name
, path
);
223 static void news_folder_destroy(Folder
*folder
)
227 dir
= news_folder_get_path(folder
);
228 if (is_dir_exist(dir
))
229 remove_dir_recursive(dir
);
232 folder_remote_folder_destroy(REMOTE_FOLDER(folder
));
235 static void news_folder_init(Folder
*folder
, const gchar
*name
,
238 folder_remote_folder_init(folder
, name
, path
);
242 static Session
*news_session_new(const gchar
*server
, gushort port
,
243 const gchar
*userid
, const gchar
*passwd
,
246 static Session
*news_session_new(const gchar
*server
, gushort port
,
247 const gchar
*userid
, const gchar
*passwd
)
250 gchar buf
[NNTPBUFSIZE
];
253 g_return_val_if_fail(server
!= NULL
, NULL
);
255 log_message(_("creating NNTP connection to %s:%d ...\n"), server
, port
);
258 session
= nntp_session_new(server
, port
, buf
, userid
, passwd
, ssl_type
);
260 session
= nntp_session_new(server
, port
, buf
, userid
, passwd
);
266 static Session
*news_session_new_for_folder(Folder
*folder
)
270 const gchar
*userid
= NULL
;
271 gchar
*passwd
= NULL
;
273 gchar buf
[NNTPBUFSIZE
];
275 g_return_val_if_fail(folder
!= NULL
, NULL
);
276 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
278 ac
= folder
->account
;
279 if (ac
->use_nntp_auth
&& ac
->userid
&& ac
->userid
[0]) {
281 if (ac
->passwd
&& ac
->passwd
[0])
282 passwd
= g_strdup(ac
->passwd
);
284 passwd
= input_dialog_query_password(ac
->nntp_server
,
289 port
= ac
->set_nntpport
? ac
->nntpport
290 : ac
->ssl_nntp
? NNTPS_PORT
: NNTP_PORT
;
291 session
= news_session_new(ac
->nntp_server
, port
, userid
, passwd
,
294 if (ac
->ssl_nntp
!= SSL_NONE
) {
295 if (alertpanel_full(_("Insecure connection"),
296 _("This connection is configured to be secured "
297 "using SSL, but SSL is not available in this "
298 "build of Claws Mail. \n\n"
299 "Do you want to continue connecting to this "
300 "server? The communication would not be "
302 GTK_STOCK_CANCEL
, _("Con_tinue connecting"),
303 NULL
, FALSE
, NULL
, ALERT_WARNING
,
304 G_ALERTDEFAULT
) != G_ALERTALTERNATE
)
307 port
= ac
->set_nntpport
? ac
->nntpport
: NNTP_PORT
;
308 session
= news_session_new(ac
->nntp_server
, port
, userid
, passwd
);
310 if ((session
!= NULL
) && ac
->use_nntp_auth
&& ac
->use_nntp_auth_onconnect
)
311 nntp_forceauth(NNTP_SESSION(session
), buf
, userid
, passwd
);
318 static NNTPSession
*news_session_get(Folder
*folder
)
320 RemoteFolder
*rfolder
= REMOTE_FOLDER(folder
);
322 g_return_val_if_fail(folder
!= NULL
, NULL
);
323 g_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
324 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
326 if (prefs_common
.work_offline
&&
327 !inc_offline_should_override(
328 _("Claws Mail needs network access in order "
329 "to access the News server."))) {
333 if (!rfolder
->session
) {
334 rfolder
->session
= news_session_new_for_folder(folder
);
335 return NNTP_SESSION(rfolder
->session
);
338 if (time(NULL
) - rfolder
->session
->last_access_time
<
339 SESSION_TIMEOUT_INTERVAL
) {
340 return NNTP_SESSION(rfolder
->session
);
343 if (nntp_mode(NNTP_SESSION(rfolder
->session
), FALSE
)
345 log_warning(_("NNTP connection to %s:%d has been"
346 " disconnected. Reconnecting...\n"),
347 folder
->account
->nntp_server
,
348 folder
->account
->set_nntpport
?
349 folder
->account
->nntpport
: NNTP_PORT
);
350 session_destroy(rfolder
->session
);
351 rfolder
->session
= news_session_new_for_folder(folder
);
354 if (rfolder
->session
)
355 session_set_access_time(rfolder
->session
);
357 return NNTP_SESSION(rfolder
->session
);
360 static gchar
*news_fetch_msg(Folder
*folder
, FolderItem
*item
, gint num
)
362 gchar
*path
, *filename
;
363 NNTPSession
*session
;
366 g_return_val_if_fail(folder
!= NULL
, NULL
);
367 g_return_val_if_fail(item
!= NULL
, NULL
);
369 path
= folder_item_get_path(item
);
370 if (!is_dir_exist(path
))
372 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(num
), NULL
);
375 if (is_file_exist(filename
)) {
376 debug_print("article %d has been already cached.\n", num
);
380 session
= news_session_get(folder
);
386 ok
= news_select_group(session
, item
->path
, NULL
, NULL
, NULL
);
387 if (ok
!= NN_SUCCESS
) {
388 if (ok
== NN_SOCKET
) {
389 session_destroy(SESSION(session
));
390 REMOTE_FOLDER(folder
)->session
= NULL
;
396 debug_print("getting article %d...\n", num
);
397 ok
= news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder
)->session
),
399 if (ok
!= NN_SUCCESS
) {
400 g_warning("can't read article %d\n", num
);
401 if (ok
== NN_SOCKET
) {
402 session_destroy(SESSION(session
));
403 REMOTE_FOLDER(folder
)->session
= NULL
;
412 static NewsGroupInfo
*news_group_info_new(const gchar
*name
,
413 gint first
, gint last
, gchar type
)
415 NewsGroupInfo
*ginfo
;
417 ginfo
= g_new(NewsGroupInfo
, 1);
418 ginfo
->name
= g_strdup(name
);
419 ginfo
->first
= first
;
426 static void news_group_info_free(NewsGroupInfo
*ginfo
)
432 static gint
news_group_info_compare(NewsGroupInfo
*ginfo1
,
433 NewsGroupInfo
*ginfo2
)
435 return g_ascii_strcasecmp(ginfo1
->name
, ginfo2
->name
);
438 GSList
*news_get_group_list(Folder
*folder
)
440 gchar
*path
, *filename
;
444 gchar buf
[NNTPBUFSIZE
];
446 g_return_val_if_fail(folder
!= NULL
, NULL
);
447 g_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
449 path
= folder_item_get_path(FOLDER_ITEM(folder
->node
->data
));
450 if (!is_dir_exist(path
))
452 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, NEWSGROUP_LIST
, NULL
);
455 if ((fp
= g_fopen(filename
, "rb")) == NULL
) {
456 NNTPSession
*session
;
459 session
= news_session_get(folder
);
465 ok
= nntp_list(session
);
466 if (ok
!= NN_SUCCESS
) {
467 if (ok
== NN_SOCKET
) {
468 session_destroy(SESSION(session
));
469 REMOTE_FOLDER(folder
)->session
= NULL
;
474 if (recv_write_to_file(SESSION(session
)->sock
, filename
) < 0) {
475 log_warning(_("can't retrieve newsgroup list\n"));
476 session_destroy(SESSION(session
));
477 REMOTE_FOLDER(folder
)->session
= NULL
;
482 if ((fp
= g_fopen(filename
, "rb")) == NULL
) {
483 FILE_OP_ERROR(filename
, "fopen");
489 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
495 NewsGroupInfo
*ginfo
;
503 if (sscanf(p
, "%d %d %c", &last_num
, &first_num
, &type
) < 3)
506 ginfo
= news_group_info_new(name
, first_num
, last_num
, type
);
509 last
= list
= g_slist_append(NULL
, ginfo
);
511 last
= g_slist_append(last
, ginfo
);
519 list
= g_slist_sort(list
, (GCompareFunc
)news_group_info_compare
);
524 void news_group_list_free(GSList
*group_list
)
528 if (!group_list
) return;
530 for (cur
= group_list
; cur
!= NULL
; cur
= cur
->next
)
531 news_group_info_free((NewsGroupInfo
*)cur
->data
);
532 g_slist_free(group_list
);
535 void news_remove_group_list_cache(Folder
*folder
)
537 gchar
*path
, *filename
;
539 g_return_if_fail(folder
!= NULL
);
540 g_return_if_fail(FOLDER_CLASS(folder
) == &news_class
);
542 path
= folder_item_get_path(FOLDER_ITEM(folder
->node
->data
));
543 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, NEWSGROUP_LIST
, NULL
);
546 if (is_file_exist(filename
)) {
547 if (remove(filename
) < 0)
548 FILE_OP_ERROR(filename
, "remove");
553 gint
news_post(Folder
*folder
, const gchar
*file
)
558 g_return_val_if_fail(folder
!= NULL
, -1);
559 g_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
560 g_return_val_if_fail(file
!= NULL
, -1);
562 if ((fp
= g_fopen(file
, "rb")) == NULL
) {
563 FILE_OP_ERROR(file
, "fopen");
567 ok
= news_post_stream(folder
, fp
);
574 gint
news_post_stream(Folder
*folder
, FILE *fp
)
576 NNTPSession
*session
;
579 g_return_val_if_fail(folder
!= NULL
, -1);
580 g_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
581 g_return_val_if_fail(fp
!= NULL
, -1);
583 session
= news_session_get(folder
);
584 if (!session
) return -1;
586 ok
= nntp_post(session
, fp
);
587 if (ok
!= NN_SUCCESS
) {
588 log_warning(_("couldn't post article.\n"));
589 if (ok
== NN_SOCKET
) {
590 session_destroy(SESSION(session
));
591 REMOTE_FOLDER(folder
)->session
= NULL
;
599 static gint
news_get_article_cmd(NNTPSession
*session
, const gchar
*cmd
,
600 gint num
, gchar
*filename
)
605 ok
= nntp_get_article(session
, cmd
, num
, &msgid
);
606 if (ok
!= NN_SUCCESS
)
609 debug_print("Message-ID = %s, num = %d\n", msgid
, num
);
612 ok
= recv_write_to_file(SESSION(session
)->sock
, filename
);
614 log_warning(_("couldn't retrieve article %d\n"), num
);
624 static gint
news_get_article(NNTPSession
*session
, gint num
, gchar
*filename
)
626 return news_get_article_cmd(session
, "ARTICLE", num
, filename
);
631 * @session: Active NNTP session.
632 * @group: Newsgroup name.
633 * @num: Estimated number of articles.
634 * @first: First article number.
635 * @last: Last article number.
637 * Select newsgroup @group with the GROUP command if it is not already
638 * selected in @session, or article numbers need to be returned.
640 * Return value: NNTP result code.
642 static gint
news_select_group(NNTPSession
*session
, const gchar
*group
,
643 gint
*num
, gint
*first
, gint
*last
)
646 gint num_
, first_
, last_
;
648 if (!num
|| !first
|| !last
) {
649 if (session
->group
&& g_ascii_strcasecmp(session
->group
, group
) == 0)
656 g_free(session
->group
);
657 session
->group
= NULL
;
659 ok
= nntp_group(session
, group
, num
, first
, last
);
660 if (ok
== NN_SUCCESS
)
661 session
->group
= g_strdup(group
);
663 log_warning(_("couldn't select group: %s\n"), group
);
668 #define PARSE_ONE_PARAM(p, srcp) \
670 p = strchr(srcp, '\t'); \
671 if (!p) return NULL; \
676 static MsgInfo
*news_parse_xover(const gchar
*xover_str
)
679 gchar
*subject
, *sender
, *size
, *line
, *date
, *msgid
, *ref
, *tmp
;
681 gint num
, size_int
, line_int
;
684 Xstrdup_a(xover_buf
, xover_str
, return NULL
);
686 PARSE_ONE_PARAM(subject
, xover_buf
);
687 PARSE_ONE_PARAM(sender
, subject
);
688 PARSE_ONE_PARAM(date
, sender
);
689 PARSE_ONE_PARAM(msgid
, date
);
690 PARSE_ONE_PARAM(ref
, msgid
);
691 PARSE_ONE_PARAM(size
, ref
);
692 PARSE_ONE_PARAM(line
, size
);
694 * PARSE_ONE_PARAM(xref, line);
696 * if we parse extra headers we should first examine the
697 * LIST OVERVIEW.FMT response from the server. See
698 * RFC2980 for details
701 tmp
= strchr(line
, '\t');
702 if (!tmp
) tmp
= strchr(line
, '\r');
703 if (!tmp
) tmp
= strchr(line
, '\n');
704 if (tmp
) *tmp
= '\0';
706 num
= atoi(xover_str
);
707 size_int
= atoi(size
);
708 line_int
= atoi(line
);
711 msginfo
= procmsg_msginfo_new();
712 msginfo
->msgnum
= num
;
713 msginfo
->size
= size_int
;
715 msginfo
->date
= g_strdup(date
);
716 msginfo
->date_t
= procheader_date_parse(NULL
, date
, 0);
718 msginfo
->from
= conv_unmime_header(sender
, NULL
);
719 msginfo
->fromname
= procheader_get_fromname(msginfo
->from
);
721 msginfo
->subject
= conv_unmime_header(subject
, NULL
);
723 remove_return(msginfo
->from
);
724 remove_return(msginfo
->fromname
);
725 remove_return(msginfo
->subject
);
728 extract_parenthesis(msgid
, '<', '>');
731 msginfo
->msgid
= g_strdup(msgid
);
734 /* FIXME: this is a quick fix; references' meaning was changed
735 * into having the actual list of references in the References: header.
736 * We need a GSList here, so msginfo_free() and msginfo_copy() can do
737 * their things properly. */
738 if (ref
&& strlen(ref
)) {
739 gchar
**ref_tokens
= g_strsplit(ref
, " ", -1);
742 while (ref_tokens
[i
]) {
743 gchar
*cur_ref
= ref_tokens
[i
];
744 msginfo
->references
= references_list_append(msginfo
->references
,
748 g_strfreev(ref_tokens
);
750 eliminate_parenthesis(ref
, '(', ')');
751 if ((p
= strrchr(ref
, '<')) != NULL
) {
752 extract_parenthesis(p
, '<', '>');
755 msginfo
->inreplyto
= g_strdup(p
);
762 static gchar
*news_parse_xhdr(const gchar
*xhdr_str
, MsgInfo
*msginfo
)
768 p
= strchr(xhdr_str
, ' ');
774 num
= atoi(xhdr_str
);
775 if (msginfo
->msgnum
!= num
) return NULL
;
777 tmp
= strchr(p
, '\r');
778 if (!tmp
) tmp
= strchr(p
, '\n');
781 return g_strndup(p
, tmp
- p
);
786 gint
news_cancel_article(Folder
* folder
, MsgInfo
* msginfo
)
792 tmp
= g_strdup_printf("%s%ctmp%p", g_get_tmp_dir(),
793 G_DIR_SEPARATOR
, msginfo
);
797 if ((tmpfp
= g_fopen(tmp
, "wb")) == NULL
) {
798 FILE_OP_ERROR(tmp
, "fopen");
801 if (change_file_mode_rw(tmpfp
, tmp
) < 0) {
802 FILE_OP_ERROR(tmp
, "chmod");
803 g_warning("can't change file mode\n");
806 fprintf(tmpfp
, "From: %s\r\n", msginfo
->from
);
807 fprintf(tmpfp
, "Newsgroups: %s\r\n", msginfo
->newsgroups
);
808 fprintf(tmpfp
, "Subject: cmsg cancel <%s>\r\n", msginfo
->msgid
);
809 fprintf(tmpfp
, "Control: cancel <%s>\r\n", msginfo
->msgid
);
810 fprintf(tmpfp
, "Approved: %s\r\n", msginfo
->from
);
811 fprintf(tmpfp
, "X-Cancelled-by: %s\r\n", msginfo
->from
);
812 get_rfc822_date(buf
, sizeof(buf
));
813 fprintf(tmpfp
, "Date: %s\r\n", buf
);
814 fprintf(tmpfp
, "\r\n");
815 fprintf(tmpfp
, "removed with Claws Mail\r\n");
819 news_post(folder
, tmp
);
827 static gchar
*news_folder_get_path(Folder
*folder
)
831 g_return_val_if_fail(folder
->account
!= NULL
, NULL
);
833 folder_path
= g_strconcat(get_news_cache_dir(),
835 folder
->account
->nntp_server
,
840 gchar
*news_item_get_path(Folder
*folder
, FolderItem
*item
)
842 gchar
*folder_path
, *path
;
844 g_return_val_if_fail(folder
!= NULL
, NULL
);
845 g_return_val_if_fail(item
!= NULL
, NULL
);
846 folder_path
= news_folder_get_path(folder
);
848 g_return_val_if_fail(folder_path
!= NULL
, NULL
);
849 if (folder_path
[0] == G_DIR_SEPARATOR
) {
851 path
= g_strconcat(folder_path
, G_DIR_SEPARATOR_S
,
854 path
= g_strdup(folder_path
);
857 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
858 folder_path
, G_DIR_SEPARATOR_S
,
861 path
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
869 gint
news_get_num_list(Folder
*folder
, FolderItem
*item
, GSList
**msgnum_list
, gboolean
*old_uids_valid
)
871 NNTPSession
*session
;
872 gint i
, ok
, num
, first
, last
, nummsgs
= 0;
875 g_return_val_if_fail(item
!= NULL
, -1);
876 g_return_val_if_fail(item
->folder
!= NULL
, -1);
877 g_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, -1);
879 session
= news_session_get(folder
);
880 g_return_val_if_fail(session
!= NULL
, -1);
882 *old_uids_valid
= TRUE
;
884 news_folder_lock(NEWS_FOLDER(item
->folder
));
886 ok
= news_select_group(session
, item
->path
, &num
, &first
, &last
);
887 if (ok
!= NN_SUCCESS
) {
888 log_warning(_("couldn't set group: %s\n"), item
->path
);
889 news_folder_unlock(NEWS_FOLDER(item
->folder
));
893 dir
= news_folder_get_path(folder
);
895 remove_all_numbered_files(dir
);
896 else if (last
< first
)
897 log_warning(_("invalid article range: %d - %d\n"),
900 for (i
= first
; i
<= last
; i
++) {
901 *msgnum_list
= g_slist_prepend(*msgnum_list
,
905 debug_print("removing old messages from %d to %d in %s\n",
907 remove_numbered_files(dir
, 1, first
- 1);
910 news_folder_unlock(NEWS_FOLDER(item
->folder
));
914 #define READ_TO_LISTEND(hdr) \
915 while (!(buf[0] == '.' && buf[1] == '\r')) { \
916 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
917 log_warning(_("error occurred while getting %s.\n"), hdr); \
922 static void news_set_msg_flags(FolderItem
*item
, MsgInfo
*msginfo
)
924 msginfo
->flags
.tmp_flags
= 0;
925 if (item
->folder
->account
->mark_crosspost_read
&& msginfo
->msgid
) {
926 if (item
->folder
->newsart
&&
927 g_hash_table_lookup(item
->folder
->newsart
, msginfo
->msgid
) != NULL
) {
928 msginfo
->flags
.perm_flags
= MSG_COLORLABEL_TO_FLAGS(item
->folder
->account
->crosspost_col
);
931 if (!item
->folder
->newsart
)
932 item
->folder
->newsart
= g_hash_table_new(g_str_hash
, g_str_equal
);
933 g_hash_table_insert(item
->folder
->newsart
,
934 g_strdup(msginfo
->msgid
), GINT_TO_POINTER(1));
935 msginfo
->flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
938 msginfo
->flags
.perm_flags
= MSG_NEW
|MSG_UNREAD
;
942 MsgInfo
*news_get_msginfo(Folder
*folder
, FolderItem
*item
, gint num
)
944 NNTPSession
*session
;
945 MsgInfo
*msginfo
= NULL
;
946 gchar buf
[NNTPBUFSIZE
];
949 session
= news_session_get(folder
);
950 g_return_val_if_fail(session
!= NULL
, NULL
);
951 g_return_val_if_fail(item
!= NULL
, NULL
);
952 g_return_val_if_fail(item
->folder
!= NULL
, NULL
);
953 g_return_val_if_fail(FOLDER_CLASS(item
->folder
) == &news_class
, NULL
);
955 log_message(_("getting xover %d in %s...\n"),
957 ok
= nntp_xover(session
, num
, num
);
959 news_folder_lock(NEWS_FOLDER(item
->folder
));
961 if (ok
!= NN_SUCCESS
) {
962 log_warning(_("couldn't get xover\n"));
963 if (ok
== NN_SOCKET
) {
964 session_destroy(SESSION(session
));
965 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
967 news_folder_unlock(NEWS_FOLDER(item
->folder
));
971 if (sock_gets(SESSION(session
)->sock
, buf
, sizeof(buf
)) < 0) {
972 log_warning(_("error occurred while getting xover.\n"));
973 news_folder_unlock(NEWS_FOLDER(item
->folder
));
977 msginfo
= news_parse_xover(buf
);
979 log_warning(_("invalid xover line: %s\n"), buf
);
982 READ_TO_LISTEND("xover");
985 news_folder_unlock(NEWS_FOLDER(item
->folder
));
989 msginfo
->folder
= item
;
991 news_set_msg_flags(item
, msginfo
);
992 msginfo
->flags
.tmp_flags
|= MSG_NEWS
;
993 msginfo
->newsgroups
= g_strdup(item
->path
);
995 ok
= nntp_xhdr(session
, "to", num
, num
);
996 if (ok
!= NN_SUCCESS
) {
997 log_warning(_("couldn't get xhdr\n"));
998 if (ok
== NN_SOCKET
) {
999 session_destroy(SESSION(session
));
1000 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1002 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1006 if (sock_gets(SESSION(session
)->sock
, buf
, sizeof(buf
)) < 0) {
1007 log_warning(_("error occurred while getting xhdr.\n"));
1008 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1012 msginfo
->to
= news_parse_xhdr(buf
, msginfo
);
1014 READ_TO_LISTEND("xhdr (to)");
1016 ok
= nntp_xhdr(session
, "cc", num
, num
);
1017 if (ok
!= NN_SUCCESS
) {
1018 log_warning(_("couldn't get xhdr\n"));
1019 if (ok
== NN_SOCKET
) {
1020 session_destroy(SESSION(session
));
1021 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1023 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1027 if (sock_gets(SESSION(session
)->sock
, buf
, sizeof(buf
)) < 0) {
1028 log_warning(_("error occurred while getting xhdr.\n"));
1029 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1033 msginfo
->cc
= news_parse_xhdr(buf
, msginfo
);
1035 READ_TO_LISTEND("xhdr (cc)");
1036 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1040 static GSList
*news_get_msginfos_for_range(NNTPSession
*session
, FolderItem
*item
, guint begin
, guint end
)
1042 gchar buf
[NNTPBUFSIZE
];
1043 GSList
*newlist
= NULL
;
1044 GSList
*llast
= NULL
;
1046 guint count
= 0, lines
= (end
- begin
) + 2;
1049 g_return_val_if_fail(session
!= NULL
, NULL
);
1050 g_return_val_if_fail(item
!= NULL
, NULL
);
1052 log_message(_("getting xover %d - %d in %s...\n"),
1053 begin
, end
, item
->path
);
1054 ok
= nntp_xover(session
, begin
, end
);
1055 if (ok
!= NN_SUCCESS
) {
1056 log_warning(_("couldn't get xover\n"));
1057 if (ok
== NN_SOCKET
) {
1058 session_destroy(SESSION(session
));
1059 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1064 news_folder_lock(NEWS_FOLDER(item
->folder
));
1067 if (sock_gets(SESSION(session
)->sock
, buf
, sizeof(buf
)) < 0) {
1068 log_warning(_("error occurred while getting xover.\n"));
1069 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1073 progressindicator_set_percentage
1074 (PROGRESS_TYPE_NETWORK
,
1075 session
->fetch_base_percentage
+
1076 (((gfloat
) count
) / ((gfloat
) lines
)) * session
->fetch_total_percentage
);
1078 if (buf
[0] == '.' && buf
[1] == '\r') break;
1080 msginfo
= news_parse_xover(buf
);
1082 log_warning(_("invalid xover line: %s\n"), buf
);
1086 msginfo
->folder
= item
;
1087 news_set_msg_flags(item
, msginfo
);
1088 msginfo
->flags
.tmp_flags
|= MSG_NEWS
;
1089 msginfo
->newsgroups
= g_strdup(item
->path
);
1092 llast
= newlist
= g_slist_append(newlist
, msginfo
);
1094 llast
= g_slist_append(llast
, msginfo
);
1095 llast
= llast
->next
;
1099 ok
= nntp_xhdr(session
, "to", begin
, end
);
1100 if (ok
!= NN_SUCCESS
) {
1101 log_warning(_("couldn't get xhdr\n"));
1102 if (ok
== NN_SOCKET
) {
1103 session_destroy(SESSION(session
));
1104 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1106 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1113 if (sock_gets(SESSION(session
)->sock
, buf
, sizeof(buf
)) < 0) {
1114 log_warning(_("error occurred while getting xhdr.\n"));
1115 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1119 if (buf
[0] == '.' && buf
[1] == '\r') break;
1121 g_warning("llast == NULL\n");
1125 msginfo
= (MsgInfo
*)llast
->data
;
1126 msginfo
->to
= news_parse_xhdr(buf
, msginfo
);
1128 llast
= llast
->next
;
1131 ok
= nntp_xhdr(session
, "cc", begin
, end
);
1132 if (ok
!= NN_SUCCESS
) {
1133 log_warning(_("couldn't get xhdr\n"));
1134 if (ok
== NN_SOCKET
) {
1135 session_destroy(SESSION(session
));
1136 REMOTE_FOLDER(item
->folder
)->session
= NULL
;
1138 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1145 if (sock_gets(SESSION(session
)->sock
, buf
, sizeof(buf
)) < 0) {
1146 log_warning(_("error occurred while getting xhdr.\n"));
1147 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1151 if (buf
[0] == '.' && buf
[1] == '\r') break;
1153 g_warning("llast == NULL\n");
1157 msginfo
= (MsgInfo
*)llast
->data
;
1158 msginfo
->cc
= news_parse_xhdr(buf
, msginfo
);
1160 llast
= llast
->next
;
1162 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1164 session_set_access_time(SESSION(session
));
1169 GSList
*news_get_msginfos(Folder
*folder
, FolderItem
*item
, GSList
*msgnum_list
)
1171 NNTPSession
*session
;
1172 GSList
*elem
, *msginfo_list
= NULL
, *tmp_msgnum_list
, *tmp_msginfo_list
;
1173 guint first
, last
, next
;
1174 guint tofetch
, fetched
;
1176 g_return_val_if_fail(folder
!= NULL
, NULL
);
1177 g_return_val_if_fail(FOLDER_CLASS(folder
) == &news_class
, NULL
);
1178 g_return_val_if_fail(msgnum_list
!= NULL
, NULL
);
1179 g_return_val_if_fail(item
!= NULL
, NULL
);
1181 session
= news_session_get(folder
);
1182 g_return_val_if_fail(session
!= NULL
, NULL
);
1184 tmp_msgnum_list
= g_slist_copy(msgnum_list
);
1185 tmp_msgnum_list
= g_slist_sort(tmp_msgnum_list
, g_int_compare
);
1187 progressindicator_start(PROGRESS_TYPE_NETWORK
);
1188 tofetch
= g_slist_length(tmp_msgnum_list
);
1191 first
= GPOINTER_TO_INT(tmp_msgnum_list
->data
);
1194 news_folder_lock(NEWS_FOLDER(item
->folder
));
1196 for(elem
= g_slist_next(tmp_msgnum_list
); elem
!= NULL
; elem
= g_slist_next(elem
)) {
1197 next
= GPOINTER_TO_INT(elem
->data
);
1198 if(next
!= (last
+ 1)) {
1199 session
->fetch_base_percentage
= ((gfloat
) fetched
) / ((gfloat
) tofetch
);
1200 session
->fetch_total_percentage
= ((gfloat
) (last
- first
+ 1)) / ((gfloat
) tofetch
);
1201 tmp_msginfo_list
= news_get_msginfos_for_range(session
, item
, first
, last
);
1202 msginfo_list
= g_slist_concat(msginfo_list
, tmp_msginfo_list
);
1203 fetched
= last
- first
+ 1;
1209 news_folder_unlock(NEWS_FOLDER(item
->folder
));
1211 session
->fetch_base_percentage
= ((gfloat
) fetched
) / ((gfloat
) tofetch
);
1212 session
->fetch_total_percentage
= ((gfloat
) (last
- first
+ 1)) / ((gfloat
) tofetch
);
1213 tmp_msginfo_list
= news_get_msginfos_for_range(session
, item
, first
, last
);
1214 msginfo_list
= g_slist_concat(msginfo_list
, tmp_msginfo_list
);
1216 g_slist_free(tmp_msgnum_list
);
1218 progressindicator_stop(PROGRESS_TYPE_NETWORK
);
1220 return msginfo_list
;
1223 gboolean
news_scan_required(Folder
*folder
, FolderItem
*item
)
1228 void news_synchronise(FolderItem
*item
)
1230 news_gtk_synchronise(item
);
1233 static gint
news_remove_folder(Folder
*folder
, FolderItem
*item
)
1237 g_return_val_if_fail(folder
!= NULL
, -1);
1238 g_return_val_if_fail(item
!= NULL
, -1);
1239 g_return_val_if_fail(item
->path
!= NULL
, -1);
1241 path
= folder_item_get_path(item
);
1242 if (remove_dir_recursive(path
) < 0) {
1243 g_warning("can't remove directory `%s'\n", path
);
1249 folder_item_remove(item
);