2006-12-06 [paul] 2.6.1cvs10
[claws.git] / src / news.c
blob8895ad51e9ced40e5975d977b2489f96c531629e
1 /*
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <dirent.h>
32 #include <unistd.h>
33 #include <time.h>
35 #include "news.h"
36 #include "news_gtk.h"
37 #include "nntp.h"
38 #include "socket.h"
39 #include "recv.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "folder.h"
43 #include "session.h"
44 #include "statusbar.h"
45 #include "codeconv.h"
46 #include "utils.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
49 #include "inputdialog.h"
50 #include "log.h"
51 #include "progressindicator.h"
52 #include "remotefolder.h"
53 #include "alertpanel.h"
54 #include "inc.h"
56 #if USE_OPENSSL
57 # include "ssl.h"
58 #endif
60 #define NNTP_PORT 119
61 #if USE_OPENSSL
62 #define NNTPS_PORT 563
63 #endif
65 typedef struct _NewsFolder NewsFolder;
67 #define NEWS_FOLDER(obj) ((NewsFolder *)obj)
69 struct _NewsFolder
71 RemoteFolder rfolder;
73 gboolean use_auth;
74 gboolean lock_count;
77 static void news_folder_init (Folder *folder,
78 const gchar *name,
79 const gchar *path);
81 static Folder *news_folder_new (const gchar *name,
82 const gchar *folder);
83 static void news_folder_destroy (Folder *folder);
85 static gchar *news_fetch_msg (Folder *folder,
86 FolderItem *item,
87 gint num);
90 #if USE_OPENSSL
91 static Session *news_session_new (const gchar *server,
92 gushort port,
93 const gchar *userid,
94 const gchar *passwd,
95 SSLType ssl_type);
96 #else
97 static Session *news_session_new (const gchar *server,
98 gushort port,
99 const gchar *userid,
100 const gchar *passwd);
101 #endif
103 static gint news_get_article_cmd (NNTPSession *session,
104 const gchar *cmd,
105 gint num,
106 gchar *filename);
107 static gint news_get_article (NNTPSession *session,
108 gint num,
109 gchar *filename);
111 static gint news_select_group (NNTPSession *session,
112 const gchar *group,
113 gint *num,
114 gint *first,
115 gint *last);
116 static MsgInfo *news_parse_xover (const gchar *xover_str);
117 static gchar *news_parse_xhdr (const gchar *xhdr_str,
118 MsgInfo *msginfo);
119 gint news_get_num_list (Folder *folder,
120 FolderItem *item,
121 GSList **list,
122 gboolean *old_uids_valid);
123 MsgInfo *news_get_msginfo (Folder *folder,
124 FolderItem *item,
125 gint num);
126 GSList *news_get_msginfos (Folder *folder,
127 FolderItem *item,
128 GSList *msgnum_list);
129 gboolean news_scan_required (Folder *folder,
130 FolderItem *item);
132 gint news_post_stream (Folder *folder,
133 FILE *fp);
134 static gchar *news_folder_get_path (Folder *folder);
135 gchar *news_item_get_path (Folder *folder,
136 FolderItem *item);
137 static void news_synchronise (FolderItem *item);
138 static int news_remove_msg (Folder *folder,
139 FolderItem *item,
140 gint msgnum);
141 static gint news_remove_folder (Folder *folder,
142 FolderItem *item);
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;
170 return &news_class;
173 static int news_remove_msg (Folder *folder,
174 FolderItem *item,
175 gint msgnum)
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))
184 make_dir_hier(path);
186 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
187 g_free(path);
188 g_unlink(filename);
189 g_free(filename);
190 return 0;
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)
206 if (folder == NULL)
207 return 0;
209 return NEWS_FOLDER(folder)->lock_count;
212 static Folder *news_folder_new(const gchar *name, const gchar *path)
214 Folder *folder;
216 folder = (Folder *)g_new0(NewsFolder, 1);
217 folder->klass = &news_class;
218 news_folder_init(folder, name, path);
220 return folder;
223 static void news_folder_destroy(Folder *folder)
225 gchar *dir;
227 dir = news_folder_get_path(folder);
228 if (is_dir_exist(dir))
229 remove_dir_recursive(dir);
230 g_free(dir);
232 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
235 static void news_folder_init(Folder *folder, const gchar *name,
236 const gchar *path)
238 folder_remote_folder_init(folder, name, path);
241 #if USE_OPENSSL
242 static Session *news_session_new(const gchar *server, gushort port,
243 const gchar *userid, const gchar *passwd,
244 SSLType ssl_type)
245 #else
246 static Session *news_session_new(const gchar *server, gushort port,
247 const gchar *userid, const gchar *passwd)
248 #endif
250 gchar buf[NNTPBUFSIZE];
251 Session *session;
253 g_return_val_if_fail(server != NULL, NULL);
255 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
257 #if USE_OPENSSL
258 session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
259 #else
260 session = nntp_session_new(server, port, buf, userid, passwd);
261 #endif
263 return session;
266 static Session *news_session_new_for_folder(Folder *folder)
268 Session *session;
269 PrefsAccount *ac;
270 const gchar *userid = NULL;
271 gchar *passwd = NULL;
272 gushort port;
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]) {
280 userid = ac->userid;
281 if (ac->passwd && ac->passwd[0])
282 passwd = g_strdup(ac->passwd);
283 else
284 passwd = input_dialog_query_password(ac->nntp_server,
285 userid);
288 #if USE_OPENSSL
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,
292 ac->ssl_nntp);
293 #else
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 "
301 "secure."),
302 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
303 NULL, FALSE, NULL, ALERT_WARNING,
304 G_ALERTDEFAULT) != G_ALERTALTERNATE)
305 return NULL;
307 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
308 session = news_session_new(ac->nntp_server, port, userid, passwd);
309 #endif
310 if ((session != NULL) && ac->use_nntp_auth && ac->use_nntp_auth_onconnect)
311 nntp_forceauth(NNTP_SESSION(session), buf, userid, passwd);
313 g_free(passwd);
315 return session;
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."))) {
330 return NULL;
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)
344 != NN_SUCCESS) {
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;
364 gint ok;
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))
371 make_dir_hier(path);
372 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
373 g_free(path);
375 if (is_file_exist(filename)) {
376 debug_print("article %d has been already cached.\n", num);
377 return filename;
380 session = news_session_get(folder);
381 if (!session) {
382 g_free(filename);
383 return NULL;
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;
392 g_free(filename);
393 return NULL;
396 debug_print("getting article %d...\n", num);
397 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
398 num, filename);
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;
405 g_free(filename);
406 return NULL;
408 GTK_EVENTS_FLUSH();
409 return filename;
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;
420 ginfo->last = last;
421 ginfo->type = type;
423 return ginfo;
426 static void news_group_info_free(NewsGroupInfo *ginfo)
428 g_free(ginfo->name);
429 g_free(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;
441 FILE *fp;
442 GSList *list = NULL;
443 GSList *last = NULL;
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))
451 make_dir_hier(path);
452 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
453 g_free(path);
455 if ((fp = g_fopen(filename, "rb")) == NULL) {
456 NNTPSession *session;
457 gint ok;
459 session = news_session_get(folder);
460 if (!session) {
461 g_free(filename);
462 return NULL;
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;
471 g_free(filename);
472 return 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;
478 g_free(filename);
479 return NULL;
482 if ((fp = g_fopen(filename, "rb")) == NULL) {
483 FILE_OP_ERROR(filename, "fopen");
484 g_free(filename);
485 return NULL;
489 while (fgets(buf, sizeof(buf), fp) != NULL) {
490 gchar *p = buf;
491 gchar *name;
492 gint last_num;
493 gint first_num;
494 gchar type;
495 NewsGroupInfo *ginfo;
497 p = strchr(p, ' ');
498 if (!p) continue;
499 *p = '\0';
500 p++;
501 name = buf;
503 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
504 continue;
506 ginfo = news_group_info_new(name, first_num, last_num, type);
508 if (!last)
509 last = list = g_slist_append(NULL, ginfo);
510 else {
511 last = g_slist_append(last, ginfo);
512 last = last->next;
516 fclose(fp);
517 g_free(filename);
519 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
521 return list;
524 void news_group_list_free(GSList *group_list)
526 GSList *cur;
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);
544 g_free(path);
546 if (is_file_exist(filename)) {
547 if (remove(filename) < 0)
548 FILE_OP_ERROR(filename, "remove");
550 g_free(filename);
553 gint news_post(Folder *folder, const gchar *file)
555 FILE *fp;
556 gint ok;
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");
564 return -1;
567 ok = news_post_stream(folder, fp);
569 fclose(fp);
571 return ok;
574 gint news_post_stream(Folder *folder, FILE *fp)
576 NNTPSession *session;
577 gint ok;
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;
593 return -1;
596 return 0;
599 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
600 gint num, gchar *filename)
602 gchar *msgid;
603 gint ok;
605 ok = nntp_get_article(session, cmd, num, &msgid);
606 if (ok != NN_SUCCESS)
607 return ok;
609 debug_print("Message-ID = %s, num = %d\n", msgid, num);
610 g_free(msgid);
612 ok = recv_write_to_file(SESSION(session)->sock, filename);
613 if (ok < 0) {
614 log_warning(_("couldn't retrieve article %d\n"), num);
615 if (ok == -2)
616 return NN_SOCKET;
617 else
618 return NN_IOERR;
621 return NN_SUCCESS;
624 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
626 return news_get_article_cmd(session, "ARTICLE", num, filename);
630 * news_select_group:
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)
645 gint ok;
646 gint num_, first_, last_;
648 if (!num || !first || !last) {
649 if (session->group && g_ascii_strcasecmp(session->group, group) == 0)
650 return NN_SUCCESS;
651 num = &num_;
652 first = &first_;
653 last = &last_;
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);
662 else
663 log_warning(_("couldn't select group: %s\n"), group);
665 return ok;
668 #define PARSE_ONE_PARAM(p, srcp) \
670 p = strchr(srcp, '\t'); \
671 if (!p) return NULL; \
672 else \
673 *p++ = '\0'; \
676 static MsgInfo *news_parse_xover(const gchar *xover_str)
678 MsgInfo *msginfo;
679 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
680 gchar *p;
681 gint num, size_int, line_int;
682 gchar *xover_buf;
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);
710 /* set MsgInfo */
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);
727 if (msgid) {
728 extract_parenthesis(msgid, '<', '>');
729 remove_space(msgid);
730 if (*msgid != '\0')
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);
740 guint i = 0;
742 while (ref_tokens[i]) {
743 gchar *cur_ref = ref_tokens[i];
744 msginfo->references = references_list_append(msginfo->references,
745 cur_ref);
746 i++;
748 g_strfreev(ref_tokens);
750 eliminate_parenthesis(ref, '(', ')');
751 if ((p = strrchr(ref, '<')) != NULL) {
752 extract_parenthesis(p, '<', '>');
753 remove_space(p);
754 if (*p != '\0')
755 msginfo->inreplyto = g_strdup(p);
759 return msginfo;
762 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
764 gchar *p;
765 gchar *tmp;
766 gint num;
768 p = strchr(xhdr_str, ' ');
769 if (!p)
770 return NULL;
771 else
772 p++;
774 num = atoi(xhdr_str);
775 if (msginfo->msgnum != num) return NULL;
777 tmp = strchr(p, '\r');
778 if (!tmp) tmp = strchr(p, '\n');
780 if (tmp)
781 return g_strndup(p, tmp - p);
782 else
783 return g_strdup(p);
786 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
788 gchar * tmp;
789 FILE * tmpfp;
790 gchar buf[BUFFSIZE];
792 tmp = g_strdup_printf("%s%ctmp%p", g_get_tmp_dir(),
793 G_DIR_SEPARATOR, msginfo);
794 if (tmp == NULL)
795 return -1;
797 if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
798 FILE_OP_ERROR(tmp, "fopen");
799 return -1;
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");
817 fclose(tmpfp);
819 news_post(folder, tmp);
820 remove(tmp);
822 g_free(tmp);
824 return 0;
827 static gchar *news_folder_get_path(Folder *folder)
829 gchar *folder_path;
831 g_return_val_if_fail(folder->account != NULL, NULL);
833 folder_path = g_strconcat(get_news_cache_dir(),
834 G_DIR_SEPARATOR_S,
835 folder->account->nntp_server,
836 NULL);
837 return folder_path;
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) {
850 if (item->path)
851 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
852 item->path, NULL);
853 else
854 path = g_strdup(folder_path);
855 } else {
856 if (item->path)
857 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
858 folder_path, G_DIR_SEPARATOR_S,
859 item->path, NULL);
860 else
861 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
862 folder_path, NULL);
864 g_free(folder_path);
866 return path;
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;
873 gchar *dir;
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));
890 return -1;
893 dir = news_folder_get_path(folder);
894 if (num <= 0)
895 remove_all_numbered_files(dir);
896 else if (last < first)
897 log_warning(_("invalid article range: %d - %d\n"),
898 first, last);
899 else {
900 for (i = first; i <= last; i++) {
901 *msgnum_list = g_slist_prepend(*msgnum_list,
902 GINT_TO_POINTER(i));
903 nummsgs++;
905 debug_print("removing old messages from %d to %d in %s\n",
906 first, last, dir);
907 remove_numbered_files(dir, 1, first - 1);
909 g_free(dir);
910 news_folder_unlock(NEWS_FOLDER(item->folder));
911 return nummsgs;
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); \
918 return msginfo; \
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);
930 } else {
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;
937 } else {
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];
947 gint ok;
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"),
956 num, item->path);
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));
968 return NULL;
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));
974 return NULL;
977 msginfo = news_parse_xover(buf);
978 if (!msginfo) {
979 log_warning(_("invalid xover line: %s\n"), buf);
982 READ_TO_LISTEND("xover");
984 if(!msginfo) {
985 news_folder_unlock(NEWS_FOLDER(item->folder));
986 return NULL;
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));
1003 return msginfo;
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));
1009 return msginfo;
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));
1024 return msginfo;
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));
1030 return msginfo;
1033 msginfo->cc = news_parse_xhdr(buf, msginfo);
1035 READ_TO_LISTEND("xhdr (cc)");
1036 news_folder_unlock(NEWS_FOLDER(item->folder));
1037 return msginfo;
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;
1045 MsgInfo *msginfo;
1046 guint count = 0, lines = (end - begin) + 2;
1047 gint ok;
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;
1061 return NULL;
1064 news_folder_lock(NEWS_FOLDER(item->folder));
1066 for (;;) {
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));
1070 return newlist;
1072 count++;
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);
1081 if (!msginfo) {
1082 log_warning(_("invalid xover line: %s\n"), buf);
1083 continue;
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);
1091 if (!newlist)
1092 llast = newlist = g_slist_append(newlist, msginfo);
1093 else {
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));
1107 return newlist;
1110 llast = newlist;
1112 for (;;) {
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));
1116 return newlist;
1119 if (buf[0] == '.' && buf[1] == '\r') break;
1120 if (!llast) {
1121 g_warning("llast == NULL\n");
1122 continue;
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));
1139 return newlist;
1142 llast = newlist;
1144 for (;;) {
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));
1148 return newlist;
1151 if (buf[0] == '.' && buf[1] == '\r') break;
1152 if (!llast) {
1153 g_warning("llast == NULL\n");
1154 continue;
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));
1166 return newlist;
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);
1189 fetched = 0;
1191 first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1192 last = first;
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;
1204 first = next;
1206 last = next;
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)
1225 return TRUE;
1228 void news_synchronise(FolderItem *item)
1230 news_gtk_synchronise(item);
1233 static gint news_remove_folder(Folder *folder, FolderItem *item)
1235 gchar *path;
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);
1244 g_free(path);
1245 return -1;
1248 g_free(path);
1249 folder_item_remove(item);
1250 return 0;