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"
56 #define NNTPS_PORT 563
59 static void news_folder_init (Folder *folder,
64 static Session *news_session_new (const gchar *server,
70 static Session *news_session_new (const gchar *server,
76 static gint news_get_article_cmd (NNTPSession *session,
80 static gint news_get_article (NNTPSession *session,
83 static gint news_get_header (NNTPSession *session,
87 static gint news_select_group (NNTPSession *session,
92 static GSList *news_get_uncached_articles(NNTPSession *session,
97 static MsgInfo *news_parse_xover (const gchar *xover_str);
98 static gchar *news_parse_xhdr (const gchar *xhdr_str,
100 static GSList *news_delete_old_articles (GSList *alist,
103 static void news_delete_all_articles (FolderItem *item);
105 static gint news_remove_msg (Folder *folder,
109 Folder *news_folder_new(const gchar *name, const gchar *path)
113 folder = (Folder *)g_new0(NewsFolder, 1);
114 news_folder_init(folder, name, path);
119 void news_folder_destroy(NewsFolder *folder)
121 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
124 static void news_folder_init(Folder *folder, const gchar *name,
127 folder_remote_folder_init(folder, name, path);
129 folder->type = F_NEWS;
131 folder->get_msg_list = news_get_article_list;
132 folder->fetch_msg = news_fetch_msg;
133 folder->scan = news_scan_group;
134 folder->remove_msg = news_remove_msg;
138 static Session *news_session_new(const gchar *server, gushort port,
139 const gchar *userid, const gchar *passwd,
142 static Session *news_session_new(const gchar *server, gushort port,
143 const gchar *userid, const gchar *passwd)
146 gchar buf[NNTPBUFSIZE];
147 NNTPSession *session;
148 NNTPSockInfo *nntp_sock;
150 g_return_val_if_fail(server != NULL, NULL);
152 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
155 if (userid && passwd)
156 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd,
159 nntp_sock = nntp_open(server, port, buf, ssl_type);
161 if (userid && passwd)
162 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
164 nntp_sock = nntp_open(server, port, buf);
167 if (nntp_sock == NULL)
170 session = g_new(NNTPSession, 1);
171 SESSION(session)->type = SESSION_NEWS;
172 SESSION(session)->server = g_strdup(server);
173 session->nntp_sock = nntp_sock;
174 SESSION(session)->sock = nntp_sock->sock;
175 SESSION(session)->connected = TRUE;
176 SESSION(session)->phase = SESSION_READY;
177 SESSION(session)->last_access_time = time(NULL);
178 SESSION(session)->data = NULL;
179 session->group = NULL;
181 return SESSION(session);
184 void news_session_destroy(NNTPSession *session)
186 nntp_close(session->nntp_sock);
187 session->nntp_sock = NULL;
188 SESSION(session)->sock = NULL;
190 g_free(session->group);
193 static Session *news_session_new_for_folder(Folder *folder)
197 const gchar *userid = NULL;
198 gchar *passwd = NULL;
201 g_return_val_if_fail(folder != NULL, NULL);
202 g_return_val_if_fail(folder->account != NULL, NULL);
204 ac = folder->account;
205 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
207 if (ac->passwd && ac->passwd[0])
208 passwd = g_strdup(ac->passwd);
210 passwd = input_dialog_query_password(ac->nntp_server,
215 port = ac->set_nntpport ? ac->nntpport
216 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
217 session = news_session_new(ac->nntp_server, port, userid, passwd,
220 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
221 session = news_session_new(ac->nntp_server, port, userid, passwd);
229 NNTPSession *news_session_get(Folder *folder)
231 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
233 g_return_val_if_fail(folder != NULL, NULL);
234 g_return_val_if_fail(folder->type == F_NEWS, NULL);
235 g_return_val_if_fail(folder->account != NULL, NULL);
237 if (!rfolder->session) {
238 rfolder->session = news_session_new_for_folder(folder);
240 return NNTP_SESSION(rfolder->session);
243 if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
244 rfolder->session->last_access_time = time(NULL);
246 return NNTP_SESSION(rfolder->session);
249 if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
251 log_warning(_("NNTP connection to %s:%d has been"
252 " disconnected. Reconnecting...\n"),
253 folder->account->nntp_server,
254 folder->account->set_nntpport ?
255 folder->account->nntpport : NNTP_PORT);
256 session_destroy(rfolder->session);
257 rfolder->session = news_session_new_for_folder(folder);
260 if (rfolder->session)
261 rfolder->session->last_access_time = time(NULL);
263 return NNTP_SESSION(rfolder->session);
266 GSList *news_get_article_list(Folder *folder, FolderItem *item,
270 NNTPSession *session;
272 g_return_val_if_fail(folder != NULL, NULL);
273 g_return_val_if_fail(item != NULL, NULL);
274 g_return_val_if_fail(folder->type == F_NEWS, NULL);
276 session = news_session_get(folder);
279 alist = procmsg_read_cache(item, FALSE);
280 item->last_num = procmsg_get_last_num_in_cache(alist);
281 } else if (use_cache) {
286 alist = procmsg_read_cache(item, FALSE);
288 cache_last = procmsg_get_last_num_in_cache(alist);
289 newlist = news_get_uncached_articles
290 (session, item, cache_last, &first, &last);
291 alist = news_delete_old_articles(alist, item, first);
293 alist = g_slist_concat(alist, newlist);
294 item->last_num = last;
298 alist = news_get_uncached_articles
299 (session, item, 0, NULL, &last);
300 news_delete_all_articles(item);
301 item->last_num = last;
304 procmsg_set_flags(alist, item);
311 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
313 gchar *path, *filename;
314 NNTPSession *session;
317 g_return_val_if_fail(folder != NULL, NULL);
318 g_return_val_if_fail(item != NULL, NULL);
320 path = folder_item_get_path(item);
321 if (!is_dir_exist(path))
323 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
326 if (is_file_exist(filename)) {
327 debug_print(_("article %d has been already cached.\n"), num);
331 session = news_session_get(folder);
337 ok = news_select_group(session, item->path, NULL, NULL, NULL);
339 if (ok != NN_SUCCESS) {
340 g_warning(_("can't select group %s\n"), item->path);
345 debug_print(_("getting article %d...\n"), num);
346 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
350 g_warning(_("can't read article %d\n"), num);
358 gint news_scan_group(Folder *folder, FolderItem *item)
360 NNTPSession *session;
361 gint num = 0, first = 0, last = 0;
362 gint new = 0, unread = 0, total = 0;
363 gint min = 0, max = 0;
367 g_return_val_if_fail(folder != NULL, -1);
368 g_return_val_if_fail(item != NULL, -1);
370 session = news_session_get(folder);
371 if (!session) return -1;
373 ok = news_select_group(session, item->path, &num, &first, &last);
374 if (ok != NN_SUCCESS) {
375 log_warning(_("can't set group: %s\n"), item->path);
380 item->new = item->unread = item->total = item->last_num = 0;
384 path = folder_item_get_path(item);
385 if (path && is_dir_exist(path)) {
386 procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
391 if (max < first || last < min)
392 new = unread = total = num;
396 else if (first < min) {
403 else if (max < last) {
405 unread += last - max;
408 if (new > num) new = num;
409 if (unread > num) unread = num;
413 item->unread = unread;
415 item->last_num = last;
420 static NewsGroupInfo *news_group_info_new(const gchar *name,
421 gint first, gint last, gchar type)
423 NewsGroupInfo *ginfo;
425 ginfo = g_new(NewsGroupInfo, 1);
426 ginfo->name = g_strdup(name);
427 ginfo->first = first;
434 static void news_group_info_free(NewsGroupInfo *ginfo)
440 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
441 NewsGroupInfo *ginfo2)
443 return g_strcasecmp(ginfo1->name, ginfo2->name);
446 GSList *news_get_group_list(Folder *folder)
448 gchar *path, *filename;
452 gchar buf[NNTPBUFSIZE];
454 g_return_val_if_fail(folder != NULL, NULL);
455 g_return_val_if_fail(folder->type == F_NEWS, NULL);
457 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
458 if (!is_dir_exist(path))
460 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
463 if ((fp = fopen(filename, "rb")) == NULL) {
464 NNTPSession *session;
466 session = news_session_get(folder);
472 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
478 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
479 log_warning(_("can't retrieve newsgroup list\n"));
480 session_destroy(SESSION(session));
481 REMOTE_FOLDER(folder)->session = NULL;
486 if ((fp = fopen(filename, "rb")) == NULL) {
487 FILE_OP_ERROR(filename, "fopen");
493 while (fgets(buf, sizeof(buf), fp) != NULL) {
499 NewsGroupInfo *ginfo;
507 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
510 ginfo = news_group_info_new(name, first_num, last_num, type);
513 last = list = g_slist_append(NULL, ginfo);
515 last = g_slist_append(last, ginfo);
523 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
530 void news_group_list_free(GSList *group_list)
534 if (!group_list) return;
536 for (cur = group_list; cur != NULL; cur = cur->next)
537 news_group_info_free((NewsGroupInfo *)cur->data);
538 g_slist_free(group_list);
541 void news_remove_group_list_cache(Folder *folder)
543 gchar *path, *filename;
545 g_return_if_fail(folder != NULL);
546 g_return_if_fail(folder->type == F_NEWS);
548 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
549 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
552 if (is_file_exist(filename)) {
553 if (remove(filename) < 0)
554 FILE_OP_ERROR(filename, "remove");
559 gint news_post(Folder *folder, const gchar *file)
561 NNTPSession *session;
565 g_return_val_if_fail(folder != NULL, -1);
566 g_return_val_if_fail(folder->type == F_NEWS, -1);
567 g_return_val_if_fail(file != NULL, -1);
569 session = news_session_get(folder);
570 if (!session) return -1;
572 if ((fp = fopen(file, "rb")) == NULL) {
573 FILE_OP_ERROR(file, "fopen");
577 ok = nntp_post(session->nntp_sock, fp);
578 if (ok != NN_SUCCESS) {
579 log_warning(_("can't post article.\n"));
590 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
591 gint num, gchar *filename)
595 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
599 debug_print("Message-Id = %s, num = %d\n", msgid, num);
602 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
603 log_warning(_("can't retrieve article %d\n"), num);
610 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
614 MsgFlags msgflags = { 0, 0 };
617 filename = folder_item_fetch_msg(item, num);
618 if (filename == NULL)
621 msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
625 r = news_cancel_article(folder, msginfo);
627 procmsg_msginfo_free(msginfo);
632 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
634 return news_get_article_cmd(session, "ARTICLE", num, filename);
637 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
639 return news_get_article_cmd(session, "HEAD", num, filename);
644 * @session: Active NNTP session.
645 * @group: Newsgroup name.
646 * @num: Estimated number of articles.
647 * @first: First article number.
648 * @last: Last article number.
650 * Select newsgroup @group with the GROUP command if it is not already
651 * selected in @session, or article numbers need to be returned.
653 * Return value: NNTP result code.
655 static gint news_select_group(NNTPSession *session, const gchar *group,
656 gint *num, gint *first, gint *last)
659 gint num_, first_, last_;
661 if (!num || !first || !last) {
662 if (session->group && g_strcasecmp(session->group, group) == 0)
669 g_free(session->group);
670 session->group = NULL;
672 ok = nntp_group(session->nntp_sock, group, num, first, last);
673 if (ok == NN_SUCCESS)
674 session->group = g_strdup(group);
679 static GSList *news_get_uncached_articles(NNTPSession *session,
680 FolderItem *item, gint cache_last,
681 gint *rfirst, gint *rlast)
684 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
685 gchar buf[NNTPBUFSIZE];
686 GSList *newlist = NULL;
687 GSList *llast = NULL;
690 if (rfirst) *rfirst = 0;
691 if (rlast) *rlast = 0;
693 g_return_val_if_fail(session != NULL, NULL);
694 g_return_val_if_fail(item != NULL, NULL);
695 g_return_val_if_fail(item->folder != NULL, NULL);
696 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
698 ok = news_select_group(session, item->path, &num, &first, &last);
699 if (ok != NN_SUCCESS) {
700 log_warning(_("can't set group: %s\n"), item->path);
704 /* calculate getting overview range */
706 log_warning(_("invalid article range: %d - %d\n"),
710 if (cache_last < first)
712 else if (last < cache_last)
714 else if (last == cache_last) {
715 debug_print(_("no new articles.\n"));
718 begin = cache_last + 1;
721 if (rfirst) *rfirst = first;
722 if (rlast) *rlast = last;
724 if (prefs_common.max_articles > 0 &&
725 end - begin + 1 > prefs_common.max_articles)
726 begin = end - prefs_common.max_articles + 1;
728 log_message(_("getting xover %d - %d in %s...\n"),
729 begin, end, item->path);
730 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
731 log_warning(_("can't get xover\n"));
736 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
737 log_warning(_("error occurred while getting xover.\n"));
741 if (buf[0] == '.' && buf[1] == '\r') break;
743 msginfo = news_parse_xover(buf);
745 log_warning(_("invalid xover line: %s\n"), buf);
749 msginfo->folder = item;
750 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
751 msginfo->flags.tmp_flags = MSG_NEWS;
752 msginfo->newsgroups = g_strdup(item->path);
755 llast = newlist = g_slist_append(newlist, msginfo);
757 llast = g_slist_append(llast, msginfo);
762 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
763 log_warning(_("can't get xhdr\n"));
770 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
771 log_warning(_("error occurred while getting xhdr.\n"));
775 if (buf[0] == '.' && buf[1] == '\r') break;
777 g_warning("llast == NULL\n");
781 msginfo = (MsgInfo *)llast->data;
782 msginfo->to = news_parse_xhdr(buf, msginfo);
787 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
788 log_warning(_("can't get xhdr\n"));
795 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
796 log_warning(_("error occurred while getting xhdr.\n"));
800 if (buf[0] == '.' && buf[1] == '\r') break;
802 g_warning("llast == NULL\n");
806 msginfo = (MsgInfo *)llast->data;
807 msginfo->cc = news_parse_xhdr(buf, msginfo);
815 #define PARSE_ONE_PARAM(p, srcp) \
817 p = strchr(srcp, '\t'); \
818 if (!p) return NULL; \
823 static MsgInfo *news_parse_xover(const gchar *xover_str)
826 gchar buf[NNTPBUFSIZE];
827 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
829 gint num, size_int, line_int;
832 Xstrdup_a(xover_buf, xover_str, return NULL);
834 PARSE_ONE_PARAM(subject, xover_buf);
835 PARSE_ONE_PARAM(sender, subject);
836 PARSE_ONE_PARAM(date, sender);
837 PARSE_ONE_PARAM(msgid, date);
838 PARSE_ONE_PARAM(ref, msgid);
839 PARSE_ONE_PARAM(size, ref);
840 PARSE_ONE_PARAM(line, size);
841 PARSE_ONE_PARAM(xref, line);
843 tmp = strchr(xref, '\t');
844 if (!tmp) tmp = strchr(line, '\r');
845 if (!tmp) tmp = strchr(line, '\n');
846 if (tmp) *tmp = '\0';
848 num = atoi(xover_str);
849 size_int = atoi(size);
850 line_int = atoi(line);
853 msginfo = g_new0(MsgInfo, 1);
854 msginfo->msgnum = num;
855 msginfo->size = size_int;
857 msginfo->date = g_strdup(date);
858 msginfo->date_t = procheader_date_parse(NULL, date, 0);
860 conv_unmime_header(buf, sizeof(buf), sender, NULL);
861 msginfo->from = g_strdup(buf);
862 msginfo->fromname = procheader_get_fromname(buf);
864 conv_unmime_header(buf, sizeof(buf), subject, NULL);
865 msginfo->subject = g_strdup(buf);
867 extract_parenthesis(msgid, '<', '>');
870 msginfo->msgid = g_strdup(msgid);
872 msginfo->references = g_strdup(ref);
873 eliminate_parenthesis(ref, '(', ')');
874 if ((p = strrchr(ref, '<')) != NULL) {
875 extract_parenthesis(p, '<', '>');
878 msginfo->inreplyto = g_strdup(p);
881 msginfo->xref = g_strdup(xref);
882 p = msginfo->xref+strlen(msginfo->xref) - 1;
883 while (*p == '\r' || *p == '\n') {
891 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
897 p = strchr(xhdr_str, ' ');
903 num = atoi(xhdr_str);
904 if (msginfo->msgnum != num) return NULL;
906 tmp = strchr(p, '\r');
907 if (!tmp) tmp = strchr(p, '\n');
910 return g_strndup(p, tmp - p);
915 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
922 g_return_val_if_fail(item != NULL, alist);
923 g_return_val_if_fail(item->folder != NULL, alist);
924 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
926 if (first < 2) return alist;
928 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
930 dir = folder_item_get_path(item);
931 remove_numbered_files(dir, 1, first - 1);
934 for (cur = alist; cur != NULL; ) {
937 msginfo = (MsgInfo *)cur->data;
938 if (msginfo && msginfo->msgnum < first) {
939 procmsg_msginfo_free(msginfo);
940 alist = g_slist_remove(alist, msginfo);
949 static void news_delete_all_articles(FolderItem *item)
953 g_return_if_fail(item != NULL);
954 g_return_if_fail(item->folder != NULL);
955 g_return_if_fail(item->folder->type == F_NEWS);
957 debug_print(_("\tDeleting all cached articles... "));
959 dir = folder_item_get_path(item);
960 remove_all_numbered_files(dir);
963 debug_print(_("done.\n"));
966 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
972 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
973 G_DIR_SEPARATOR, (gint)msginfo);
977 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
978 FILE_OP_ERROR(tmp, "fopen");
981 if (change_file_mode_rw(tmpfp, tmp) < 0) {
982 FILE_OP_ERROR(tmp, "chmod");
983 g_warning(_("can't change file mode\n"));
986 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
987 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
988 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
989 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
990 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
991 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
992 get_rfc822_date(buf, sizeof(buf));
993 fprintf(tmpfp, "Date: %s\r\n", buf);
994 fprintf(tmpfp, "\r\n");
995 fprintf(tmpfp, "removed with sylpheed\r\n");
999 news_post(folder, tmp);