2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include "procheader.h"
43 #include "statusbar.h"
46 #include "prefs_common.h"
47 #include "prefs_account.h"
48 #include "inputdialog.h"
49 #include "alertpanel.h"
53 #define NNTPS_PORT 563
56 static void news_folder_init (Folder *folder,
61 static Session *news_session_new (const gchar *server,
64 const gchar *passwd, gboolean use_ssl);
66 static Session *news_session_new (const gchar *server,
72 static gint news_get_article_cmd (NNTPSession *session,
76 static gint news_get_article (NNTPSession *session,
79 static gint news_get_header (NNTPSession *session,
83 static gint news_select_group (NNTPSession *session,
88 static GSList *news_get_uncached_articles(NNTPSession *session,
93 static MsgInfo *news_parse_xover (const gchar *xover_str);
94 static gchar *news_parse_xhdr (const gchar *xhdr_str,
96 static GSList *news_delete_old_articles (GSList *alist,
99 static void news_delete_all_articles (FolderItem *item);
101 static gint news_remove_msg (Folder *folder,
105 Folder *news_folder_new(const gchar *name, const gchar *path)
109 folder = (Folder *)g_new0(NewsFolder, 1);
110 news_folder_init(folder, name, path);
115 void news_folder_destroy(NewsFolder *folder)
117 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
120 static void news_folder_init(Folder *folder, const gchar *name,
123 folder_remote_folder_init(folder, name, path);
125 folder->type = F_NEWS;
127 folder->get_msg_list = news_get_article_list;
128 folder->fetch_msg = news_fetch_msg;
129 folder->scan = news_scan_group;
130 folder->remove_msg = news_remove_msg;
134 static Session *news_session_new(const gchar *server, gushort port,
135 const gchar *userid, const gchar *passwd, gboolean use_ssl)
137 static Session *news_session_new(const gchar *server, gushort port,
138 const gchar *userid, const gchar *passwd)
141 gchar buf[NNTPBUFSIZE];
142 NNTPSession *session;
143 NNTPSockInfo *nntp_sock;
145 g_return_val_if_fail(server != NULL, NULL);
147 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
149 if (userid && passwd)
151 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd, use_ssl);
153 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
157 nntp_sock = nntp_open(server, port, buf, use_ssl);
159 nntp_sock = nntp_open(server, port, buf);
162 if (nntp_sock == NULL)
165 session = g_new(NNTPSession, 1);
166 SESSION(session)->type = SESSION_NEWS;
167 SESSION(session)->server = g_strdup(server);
168 session->nntp_sock = nntp_sock;
169 SESSION(session)->sock = nntp_sock->sock;
170 SESSION(session)->connected = TRUE;
171 SESSION(session)->phase = SESSION_READY;
172 SESSION(session)->last_access_time = time(NULL);
173 SESSION(session)->data = NULL;
174 session->group = NULL;
176 return SESSION(session);
179 void news_session_destroy(NNTPSession *session)
181 nntp_close(session->nntp_sock);
182 session->nntp_sock = NULL;
183 SESSION(session)->sock = NULL;
185 g_free(session->group);
188 static Session *news_session_new_for_folder(Folder *folder)
192 const gchar *userid = NULL;
193 gchar *passwd = NULL;
196 g_return_val_if_fail(folder != NULL, NULL);
197 g_return_val_if_fail(folder->account != NULL, NULL);
199 ac = folder->account;
202 if (ac->set_nntpport) {
205 port = ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
208 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
210 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
212 if (ac->passwd && ac->passwd[0])
213 passwd = g_strdup(ac->passwd);
215 passwd = input_dialog_query_password(ac->nntp_server,
220 session = news_session_new(ac->nntp_server,
222 userid, passwd, ac->ssl_nntp);
224 session = news_session_new(ac->nntp_server,
233 NNTPSession *news_session_get(Folder *folder)
235 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
237 g_return_val_if_fail(folder != NULL, NULL);
238 g_return_val_if_fail(folder->type == F_NEWS, NULL);
239 g_return_val_if_fail(folder->account != NULL, NULL);
241 if (!rfolder->session) {
242 rfolder->session = news_session_new_for_folder(folder);
244 return NNTP_SESSION(rfolder->session);
247 if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
248 rfolder->session->last_access_time = time(NULL);
250 return NNTP_SESSION(rfolder->session);
253 if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
255 log_warning(_("NNTP connection to %s:%d has been"
256 " disconnected. Reconnecting...\n"),
257 folder->account->nntp_server,
258 folder->account->set_nntpport ?
259 folder->account->nntpport : NNTP_PORT);
260 session_destroy(rfolder->session);
261 rfolder->session = news_session_new_for_folder(folder);
264 if (rfolder->session)
265 rfolder->session->last_access_time = time(NULL);
267 return NNTP_SESSION(rfolder->session);
270 GSList *news_get_article_list(Folder *folder, FolderItem *item,
274 NNTPSession *session;
276 g_return_val_if_fail(folder != NULL, NULL);
277 g_return_val_if_fail(item != NULL, NULL);
278 g_return_val_if_fail(folder->type == F_NEWS, NULL);
280 session = news_session_get(folder);
283 alist = procmsg_read_cache(item, FALSE);
284 item->last_num = procmsg_get_last_num_in_cache(alist);
285 } else if (use_cache) {
290 alist = procmsg_read_cache(item, FALSE);
292 cache_last = procmsg_get_last_num_in_cache(alist);
293 newlist = news_get_uncached_articles
294 (session, item, cache_last, &first, &last);
295 alist = news_delete_old_articles(alist, item, first);
297 alist = g_slist_concat(alist, newlist);
298 item->last_num = last;
302 alist = news_get_uncached_articles
303 (session, item, 0, NULL, &last);
304 news_delete_all_articles(item);
305 item->last_num = last;
308 procmsg_set_flags(alist, item);
315 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
317 gchar *path, *filename;
318 NNTPSession *session;
321 g_return_val_if_fail(folder != NULL, NULL);
322 g_return_val_if_fail(item != NULL, NULL);
324 path = folder_item_get_path(item);
325 if (!is_dir_exist(path))
327 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
330 if (is_file_exist(filename)) {
331 debug_print(_("article %d has been already cached.\n"), num);
335 session = news_session_get(folder);
341 ok = news_select_group(session, item->path, NULL, NULL, NULL);
343 if (ok != NN_SUCCESS) {
344 g_warning(_("can't select group %s\n"), item->path);
349 debug_print(_("getting article %d...\n"), num);
350 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
354 g_warning(_("can't read article %d\n"), num);
362 gint news_scan_group(Folder *folder, FolderItem *item)
364 NNTPSession *session;
365 gint num = 0, first = 0, last = 0;
366 gint new = 0, unread = 0, total = 0;
367 gint min = 0, max = 0;
371 g_return_val_if_fail(folder != NULL, -1);
372 g_return_val_if_fail(item != NULL, -1);
374 session = news_session_get(folder);
375 if (!session) return -1;
377 ok = news_select_group(session, item->path, &num, &first, &last);
378 if (ok != NN_SUCCESS) {
379 log_warning(_("can't set group: %s\n"), item->path);
384 item->new = item->unread = item->total = item->last_num = 0;
388 path = folder_item_get_path(item);
389 if (path && is_dir_exist(path)) {
390 procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
395 if (max < first || last < min)
396 new = unread = total = num;
400 else if (first < min) {
407 else if (max < last) {
409 unread += last - max;
412 if (new > num) new = num;
413 if (unread > num) unread = num;
417 item->unread = unread;
419 item->last_num = last;
424 static NewsGroupInfo *news_group_info_new(const gchar *name,
425 gint first, gint last, gchar type)
427 NewsGroupInfo *ginfo;
429 ginfo = g_new(NewsGroupInfo, 1);
430 ginfo->name = g_strdup(name);
431 ginfo->first = first;
438 static void news_group_info_free(NewsGroupInfo *ginfo)
444 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
445 NewsGroupInfo *ginfo2)
447 return g_strcasecmp(ginfo1->name, ginfo2->name);
450 GSList *news_get_group_list(Folder *folder)
452 gchar *path, *filename;
456 gchar buf[NNTPBUFSIZE];
458 g_return_val_if_fail(folder != NULL, NULL);
459 g_return_val_if_fail(folder->type == F_NEWS, NULL);
461 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
462 if (!is_dir_exist(path))
464 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
467 if ((fp = fopen(filename, "rb")) == NULL) {
468 NNTPSession *session;
470 session = news_session_get(folder);
476 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
482 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
483 log_warning(_("can't retrieve newsgroup list\n"));
484 session_destroy(SESSION(session));
485 REMOTE_FOLDER(folder)->session = NULL;
490 if ((fp = fopen(filename, "rb")) == NULL) {
491 FILE_OP_ERROR(filename, "fopen");
497 while (fgets(buf, sizeof(buf), fp) != NULL) {
503 NewsGroupInfo *ginfo;
511 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
514 ginfo = news_group_info_new(name, first_num, last_num, type);
517 last = list = g_slist_append(NULL, ginfo);
519 last = g_slist_append(last, ginfo);
527 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
534 void news_group_list_free(GSList *group_list)
538 if (!group_list) return;
540 for (cur = group_list; cur != NULL; cur = cur->next)
541 news_group_info_free((NewsGroupInfo *)cur->data);
542 g_slist_free(group_list);
545 void news_remove_group_list_cache(Folder *folder)
547 gchar *path, *filename;
549 g_return_if_fail(folder != NULL);
550 g_return_if_fail(folder->type == F_NEWS);
552 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
553 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
556 if (is_file_exist(filename)) {
557 if (remove(filename) < 0)
558 FILE_OP_ERROR(filename, "remove");
563 gint news_post(Folder *folder, const gchar *file)
565 NNTPSession *session;
569 g_return_val_if_fail(folder != NULL, -1);
570 g_return_val_if_fail(folder->type == F_NEWS, -1);
571 g_return_val_if_fail(file != NULL, -1);
573 session = news_session_get(folder);
574 if (!session) return -1;
576 if ((fp = fopen(file, "rb")) == NULL) {
577 FILE_OP_ERROR(file, "fopen");
581 ok = nntp_post(session->nntp_sock, fp);
582 if (ok != NN_SUCCESS) {
583 log_warning(_("can't post article.\n"));
594 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
595 gint num, gchar *filename)
599 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
603 debug_print("Message-Id = %s, num = %d\n", msgid, num);
606 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
607 log_warning(_("can't retrieve article %d\n"), num);
614 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
618 MsgFlags msgflags = { 0, 0 };
621 filename = folder_item_fetch_msg(item, num);
622 if (filename == NULL)
625 msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
629 r = news_cancel_article(folder, msginfo);
631 procmsg_msginfo_free(msginfo);
636 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
638 return news_get_article_cmd(session, "ARTICLE", num, filename);
641 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
643 return news_get_article_cmd(session, "HEAD", num, filename);
648 * @session: Active NNTP session.
649 * @group: Newsgroup name.
650 * @num: Estimated number of articles.
651 * @first: First article number.
652 * @last: Last article number.
654 * Select newsgroup @group with the GROUP command if it is not already
655 * selected in @session, or article numbers need to be returned.
657 * Return value: NNTP result code.
659 static gint news_select_group(NNTPSession *session, const gchar *group,
660 gint *num, gint *first, gint *last)
663 gint num_, first_, last_;
665 if (!num || !first || !last) {
666 if (session->group && g_strcasecmp(session->group, group) == 0)
673 g_free(session->group);
674 session->group = NULL;
676 ok = nntp_group(session->nntp_sock, group, num, first, last);
677 if (ok == NN_SUCCESS)
678 session->group = g_strdup(group);
683 static GSList *news_get_uncached_articles(NNTPSession *session,
684 FolderItem *item, gint cache_last,
685 gint *rfirst, gint *rlast)
688 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
689 gchar buf[NNTPBUFSIZE];
690 GSList *newlist = NULL;
691 GSList *llast = NULL;
694 if (rfirst) *rfirst = 0;
695 if (rlast) *rlast = 0;
697 g_return_val_if_fail(session != NULL, NULL);
698 g_return_val_if_fail(item != NULL, NULL);
699 g_return_val_if_fail(item->folder != NULL, NULL);
700 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
702 ok = news_select_group(session, item->path, &num, &first, &last);
703 if (ok != NN_SUCCESS) {
704 log_warning(_("can't set group: %s\n"), item->path);
708 /* calculate getting overview range */
710 log_warning(_("invalid article range: %d - %d\n"),
714 if (cache_last < first)
716 else if (last < cache_last)
718 else if (last == cache_last) {
719 debug_print(_("no new articles.\n"));
722 begin = cache_last + 1;
725 if (rfirst) *rfirst = first;
726 if (rlast) *rlast = last;
728 if (prefs_common.max_articles > 0 &&
729 end - begin + 1 > prefs_common.max_articles)
730 begin = end - prefs_common.max_articles + 1;
732 log_message(_("getting xover %d - %d in %s...\n"),
733 begin, end, item->path);
734 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
735 log_warning(_("can't get xover\n"));
740 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
741 log_warning(_("error occurred while getting xover.\n"));
745 if (buf[0] == '.' && buf[1] == '\r') break;
747 msginfo = news_parse_xover(buf);
749 log_warning(_("invalid xover line: %s\n"), buf);
753 msginfo->folder = item;
754 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
755 msginfo->flags.tmp_flags = MSG_NEWS;
756 msginfo->newsgroups = g_strdup(item->path);
759 llast = newlist = g_slist_append(newlist, msginfo);
761 llast = g_slist_append(llast, msginfo);
766 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
767 log_warning(_("can't get xhdr\n"));
774 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
775 log_warning(_("error occurred while getting xhdr.\n"));
779 if (buf[0] == '.' && buf[1] == '\r') break;
781 g_warning("llast == NULL\n");
785 msginfo = (MsgInfo *)llast->data;
786 msginfo->to = news_parse_xhdr(buf, msginfo);
791 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
792 log_warning(_("can't get xhdr\n"));
799 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
800 log_warning(_("error occurred while getting xhdr.\n"));
804 if (buf[0] == '.' && buf[1] == '\r') break;
806 g_warning("llast == NULL\n");
810 msginfo = (MsgInfo *)llast->data;
811 msginfo->cc = news_parse_xhdr(buf, msginfo);
819 #define PARSE_ONE_PARAM(p, srcp) \
821 p = strchr(srcp, '\t'); \
822 if (!p) return NULL; \
827 static MsgInfo *news_parse_xover(const gchar *xover_str)
830 gchar buf[NNTPBUFSIZE];
831 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
833 gint num, size_int, line_int;
836 Xstrdup_a(xover_buf, xover_str, return NULL);
838 PARSE_ONE_PARAM(subject, xover_buf);
839 PARSE_ONE_PARAM(sender, subject);
840 PARSE_ONE_PARAM(date, sender);
841 PARSE_ONE_PARAM(msgid, date);
842 PARSE_ONE_PARAM(ref, msgid);
843 PARSE_ONE_PARAM(size, ref);
844 PARSE_ONE_PARAM(line, size);
845 PARSE_ONE_PARAM(xref, line);
847 tmp = strchr(xref, '\t');
848 if (!tmp) tmp = strchr(line, '\r');
849 if (!tmp) tmp = strchr(line, '\n');
850 if (tmp) *tmp = '\0';
852 num = atoi(xover_str);
853 size_int = atoi(size);
854 line_int = atoi(line);
857 msginfo = g_new0(MsgInfo, 1);
858 msginfo->msgnum = num;
859 msginfo->size = size_int;
861 msginfo->date = g_strdup(date);
862 msginfo->date_t = procheader_date_parse(NULL, date, 0);
864 conv_unmime_header(buf, sizeof(buf), sender, NULL);
865 msginfo->from = g_strdup(buf);
866 msginfo->fromname = procheader_get_fromname(buf);
868 conv_unmime_header(buf, sizeof(buf), subject, NULL);
869 msginfo->subject = g_strdup(buf);
871 extract_parenthesis(msgid, '<', '>');
874 msginfo->msgid = g_strdup(msgid);
876 msginfo->references = g_strdup(ref);
877 eliminate_parenthesis(ref, '(', ')');
878 if ((p = strrchr(ref, '<')) != NULL) {
879 extract_parenthesis(p, '<', '>');
882 msginfo->inreplyto = g_strdup(p);
885 msginfo->xref = g_strdup(xref);
886 p = msginfo->xref+strlen(msginfo->xref) - 1;
887 while (*p == '\r' || *p == '\n') {
895 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
901 p = strchr(xhdr_str, ' ');
907 num = atoi(xhdr_str);
908 if (msginfo->msgnum != num) return NULL;
910 tmp = strchr(p, '\r');
911 if (!tmp) tmp = strchr(p, '\n');
914 return g_strndup(p, tmp - p);
919 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
926 g_return_val_if_fail(item != NULL, alist);
927 g_return_val_if_fail(item->folder != NULL, alist);
928 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
930 if (first < 2) return alist;
932 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
934 dir = folder_item_get_path(item);
935 remove_numbered_files(dir, 1, first - 1);
938 for (cur = alist; cur != NULL; ) {
941 msginfo = (MsgInfo *)cur->data;
942 if (msginfo && msginfo->msgnum < first) {
943 procmsg_msginfo_free(msginfo);
944 alist = g_slist_remove(alist, msginfo);
953 static void news_delete_all_articles(FolderItem *item)
957 g_return_if_fail(item != NULL);
958 g_return_if_fail(item->folder != NULL);
959 g_return_if_fail(item->folder->type == F_NEWS);
961 debug_print(_("\tDeleting all cached articles... "));
963 dir = folder_item_get_path(item);
964 remove_all_numbered_files(dir);
967 debug_print(_("done.\n"));
970 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
976 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
977 G_DIR_SEPARATOR, (gint)msginfo);
981 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
982 FILE_OP_ERROR(tmp, "fopen");
985 if (change_file_mode_rw(tmpfp, tmp) < 0) {
986 FILE_OP_ERROR(tmp, "chmod");
987 g_warning(_("can't change file mode\n"));
990 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
991 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
992 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
993 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
994 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
995 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
996 get_rfc822_date(buf, sizeof(buf));
997 fprintf(tmpfp, "Date: %s\r\n", buf);
998 fprintf(tmpfp, "\r\n");
999 fprintf(tmpfp, "removed with sylpheed\r\n");
1003 news_post(folder, tmp);