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 void 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_if_fail(folder != NULL);
372 g_return_if_fail(item != NULL);
374 session = news_session_get(folder);
375 if (!session) return;
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,
397 unread += last - max;
398 if (new > num) new = num;
399 if (unread > num) unread = num;
403 item->unread = unread;
405 item->last_num = last;
408 static NewsGroupInfo *news_group_info_new(const gchar *name,
409 gint first, gint last, gchar type)
411 NewsGroupInfo *ginfo;
413 ginfo = g_new(NewsGroupInfo, 1);
414 ginfo->name = g_strdup(name);
415 ginfo->first = first;
422 static void news_group_info_free(NewsGroupInfo *ginfo)
428 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
429 NewsGroupInfo *ginfo2)
431 return g_strcasecmp(ginfo1->name, ginfo2->name);
434 GSList *news_get_group_list(Folder *folder)
436 gchar *path, *filename;
440 gchar buf[NNTPBUFSIZE];
442 g_return_val_if_fail(folder != NULL, NULL);
443 g_return_val_if_fail(folder->type == F_NEWS, NULL);
445 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
446 if (!is_dir_exist(path))
448 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
451 if ((fp = fopen(filename, "rb")) == NULL) {
452 NNTPSession *session;
454 session = news_session_get(folder);
460 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
466 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
467 log_warning(_("can't retrieve newsgroup list\n"));
468 session_destroy(SESSION(session));
469 REMOTE_FOLDER(folder)->session = NULL;
474 if ((fp = fopen(filename, "rb")) == NULL) {
475 FILE_OP_ERROR(filename, "fopen");
481 while (fgets(buf, sizeof(buf), fp) != NULL) {
487 NewsGroupInfo *ginfo;
495 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
498 ginfo = news_group_info_new(name, first_num, last_num, type);
501 last = list = g_slist_append(NULL, ginfo);
503 last = g_slist_append(last, ginfo);
511 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
518 void news_group_list_free(GSList *group_list)
522 if (!group_list) return;
524 for (cur = group_list; cur != NULL; cur = cur->next)
525 news_group_info_free((NewsGroupInfo *)cur->data);
526 g_slist_free(group_list);
529 void news_remove_group_list_cache(Folder *folder)
531 gchar *path, *filename;
533 g_return_if_fail(folder != NULL);
534 g_return_if_fail(folder->type == F_NEWS);
536 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
537 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
540 if (is_file_exist(filename)) {
541 if (remove(filename) < 0)
542 FILE_OP_ERROR(filename, "remove");
547 gint news_post(Folder *folder, const gchar *file)
549 NNTPSession *session;
553 g_return_val_if_fail(folder != NULL, -1);
554 g_return_val_if_fail(folder->type == F_NEWS, -1);
555 g_return_val_if_fail(file != NULL, -1);
557 session = news_session_get(folder);
558 if (!session) return -1;
560 if ((fp = fopen(file, "rb")) == NULL) {
561 FILE_OP_ERROR(file, "fopen");
565 ok = nntp_post(session->nntp_sock, fp);
566 if (ok != NN_SUCCESS) {
567 log_warning(_("can't post article.\n"));
578 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
579 gint num, gchar *filename)
583 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
587 debug_print("Message-Id = %s, num = %d\n", msgid, num);
590 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
591 log_warning(_("can't retrieve article %d\n"), num);
598 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
602 MsgFlags msgflags = { 0, 0 };
605 filename = folder_item_fetch_msg(item, num);
606 if (filename == NULL)
609 msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
613 r = news_cancel_article(folder, msginfo);
615 procmsg_msginfo_free(msginfo);
620 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
622 return news_get_article_cmd(session, "ARTICLE", num, filename);
625 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
627 return news_get_article_cmd(session, "HEAD", num, filename);
632 * @session: Active NNTP session.
633 * @group: Newsgroup name.
634 * @num: Estimated number of articles.
635 * @first: First article number.
636 * @last: Last article number.
638 * Select newsgroup @group with the GROUP command if it is not already
639 * selected in @session, or article numbers need to be returned.
641 * Return value: NNTP result code.
643 static gint news_select_group(NNTPSession *session, const gchar *group,
644 gint *num, gint *first, gint *last)
647 gint num_, first_, last_;
649 if (!num || !first || !last) {
650 if (session->group && g_strcasecmp(session->group, group) == 0)
657 g_free(session->group);
658 session->group = NULL;
660 ok = nntp_group(session->nntp_sock, group, num, first, last);
661 if (ok == NN_SUCCESS)
662 session->group = g_strdup(group);
667 static GSList *news_get_uncached_articles(NNTPSession *session,
668 FolderItem *item, gint cache_last,
669 gint *rfirst, gint *rlast)
672 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
673 gchar buf[NNTPBUFSIZE];
674 GSList *newlist = NULL;
675 GSList *llast = NULL;
678 if (rfirst) *rfirst = 0;
679 if (rlast) *rlast = 0;
681 g_return_val_if_fail(session != NULL, NULL);
682 g_return_val_if_fail(item != NULL, NULL);
683 g_return_val_if_fail(item->folder != NULL, NULL);
684 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
686 ok = news_select_group(session, item->path, &num, &first, &last);
687 if (ok != NN_SUCCESS) {
688 log_warning(_("can't set group: %s\n"), item->path);
692 /* calculate getting overview range */
694 log_warning(_("invalid article range: %d - %d\n"),
698 if (cache_last < first)
700 else if (last < cache_last)
702 else if (last == cache_last) {
703 debug_print(_("no new articles.\n"));
706 begin = cache_last + 1;
709 if (rfirst) *rfirst = first;
710 if (rlast) *rlast = last;
712 if (prefs_common.max_articles > 0 &&
713 end - begin + 1 > prefs_common.max_articles)
714 begin = end - prefs_common.max_articles + 1;
716 log_message(_("getting xover %d - %d in %s...\n"),
717 begin, end, item->path);
718 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
719 log_warning(_("can't get xover\n"));
724 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
725 log_warning(_("error occurred while getting xover.\n"));
729 if (buf[0] == '.' && buf[1] == '\r') break;
731 msginfo = news_parse_xover(buf);
733 log_warning(_("invalid xover line: %s\n"), buf);
737 msginfo->folder = item;
738 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
739 msginfo->flags.tmp_flags = MSG_NEWS;
740 msginfo->newsgroups = g_strdup(item->path);
743 llast = newlist = g_slist_append(newlist, msginfo);
745 llast = g_slist_append(llast, msginfo);
750 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
751 log_warning(_("can't get xhdr\n"));
758 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
759 log_warning(_("error occurred while getting xhdr.\n"));
763 if (buf[0] == '.' && buf[1] == '\r') break;
765 g_warning("llast == NULL\n");
769 msginfo = (MsgInfo *)llast->data;
770 msginfo->to = news_parse_xhdr(buf, msginfo);
775 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
776 log_warning(_("can't get xhdr\n"));
783 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
784 log_warning(_("error occurred while getting xhdr.\n"));
788 if (buf[0] == '.' && buf[1] == '\r') break;
790 g_warning("llast == NULL\n");
794 msginfo = (MsgInfo *)llast->data;
795 msginfo->cc = news_parse_xhdr(buf, msginfo);
803 #define PARSE_ONE_PARAM(p, srcp) \
805 p = strchr(srcp, '\t'); \
806 if (!p) return NULL; \
811 static MsgInfo *news_parse_xover(const gchar *xover_str)
814 gchar buf[NNTPBUFSIZE];
815 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
817 gint num, size_int, line_int;
820 Xstrdup_a(xover_buf, xover_str, return NULL);
822 PARSE_ONE_PARAM(subject, xover_buf);
823 PARSE_ONE_PARAM(sender, subject);
824 PARSE_ONE_PARAM(date, sender);
825 PARSE_ONE_PARAM(msgid, date);
826 PARSE_ONE_PARAM(ref, msgid);
827 PARSE_ONE_PARAM(size, ref);
828 PARSE_ONE_PARAM(line, size);
829 PARSE_ONE_PARAM(xref, line);
831 tmp = strchr(xref, '\t');
832 if (!tmp) tmp = strchr(line, '\r');
833 if (!tmp) tmp = strchr(line, '\n');
834 if (tmp) *tmp = '\0';
836 num = atoi(xover_str);
837 size_int = atoi(size);
838 line_int = atoi(line);
841 msginfo = g_new0(MsgInfo, 1);
842 msginfo->msgnum = num;
843 msginfo->size = size_int;
845 msginfo->date = g_strdup(date);
846 msginfo->date_t = procheader_date_parse(NULL, date, 0);
848 conv_unmime_header(buf, sizeof(buf), sender, NULL);
849 msginfo->from = g_strdup(buf);
850 msginfo->fromname = procheader_get_fromname(buf);
852 conv_unmime_header(buf, sizeof(buf), subject, NULL);
853 msginfo->subject = g_strdup(buf);
855 extract_parenthesis(msgid, '<', '>');
858 msginfo->msgid = g_strdup(msgid);
860 msginfo->references = g_strdup(ref);
861 eliminate_parenthesis(ref, '(', ')');
862 if ((p = strrchr(ref, '<')) != NULL) {
863 extract_parenthesis(p, '<', '>');
866 msginfo->inreplyto = g_strdup(p);
869 msginfo->xref = g_strdup(xref);
870 p = msginfo->xref+strlen(msginfo->xref) - 1;
871 while (*p == '\r' || *p == '\n') {
879 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
885 p = strchr(xhdr_str, ' ');
891 num = atoi(xhdr_str);
892 if (msginfo->msgnum != num) return NULL;
894 tmp = strchr(p, '\r');
895 if (!tmp) tmp = strchr(p, '\n');
898 return g_strndup(p, tmp - p);
903 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
910 g_return_val_if_fail(item != NULL, alist);
911 g_return_val_if_fail(item->folder != NULL, alist);
912 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
914 if (first < 2) return alist;
916 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
918 dir = folder_item_get_path(item);
919 remove_numbered_files(dir, 1, first - 1);
922 for (cur = alist; cur != NULL; ) {
925 msginfo = (MsgInfo *)cur->data;
926 if (msginfo && msginfo->msgnum < first) {
927 procmsg_msginfo_free(msginfo);
928 alist = g_slist_remove(alist, msginfo);
937 static void news_delete_all_articles(FolderItem *item)
941 g_return_if_fail(item != NULL);
942 g_return_if_fail(item->folder != NULL);
943 g_return_if_fail(item->folder->type == F_NEWS);
945 debug_print(_("\tDeleting all cached articles... "));
947 dir = folder_item_get_path(item);
948 remove_all_numbered_files(dir);
951 debug_print(_("done.\n"));
954 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
960 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
961 G_DIR_SEPARATOR, (gint)msginfo);
965 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
966 FILE_OP_ERROR(tmp, "fopen");
969 if (change_file_mode_rw(tmpfp, tmp) < 0) {
970 FILE_OP_ERROR(tmp, "chmod");
971 g_warning(_("can't change file mode\n"));
974 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
975 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
976 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
977 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
978 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
979 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
980 get_rfc822_date(buf, sizeof(buf));
981 fprintf(tmpfp, "Date: %s\r\n", buf);
982 fprintf(tmpfp, "\r\n");
983 fprintf(tmpfp, "removed with sylpheed\r\n");
987 news_post(folder, tmp);