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"
54 #define NNTPS_PORT 563
57 static void news_folder_init (Folder *folder,
62 static Session *news_session_new (const gchar *server,
65 const gchar *passwd, SSLType ssl_type);
67 static Session *news_session_new (const gchar *server,
73 static gint news_get_article_cmd (NNTPSession *session,
77 static gint news_get_article (NNTPSession *session,
80 static gint news_get_header (NNTPSession *session,
84 static gint news_select_group (NNTPSession *session,
89 static GSList *news_get_uncached_articles(NNTPSession *session,
94 static MsgInfo *news_parse_xover (const gchar *xover_str);
95 static gchar *news_parse_xhdr (const gchar *xhdr_str,
97 static GSList *news_delete_old_articles (GSList *alist,
100 static void news_delete_all_articles (FolderItem *item);
102 static gint news_remove_msg (Folder *folder,
106 Folder *news_folder_new(const gchar *name, const gchar *path)
110 folder = (Folder *)g_new0(NewsFolder, 1);
111 news_folder_init(folder, name, path);
116 void news_folder_destroy(NewsFolder *folder)
118 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
121 static void news_folder_init(Folder *folder, const gchar *name,
124 folder_remote_folder_init(folder, name, path);
126 folder->type = F_NEWS;
128 folder->get_msg_list = news_get_article_list;
129 folder->fetch_msg = news_fetch_msg;
130 folder->scan = news_scan_group;
131 folder->remove_msg = news_remove_msg;
135 static Session *news_session_new(const gchar *server, gushort port,
136 const gchar *userid, const gchar *passwd, SSLType ssl_type)
138 static Session *news_session_new(const gchar *server, gushort port,
139 const gchar *userid, const gchar *passwd)
142 gchar buf[NNTPBUFSIZE];
143 NNTPSession *session;
144 NNTPSockInfo *nntp_sock;
146 g_return_val_if_fail(server != NULL, NULL);
148 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
150 if (userid && passwd)
152 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd, ssl_type);
154 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
158 nntp_sock = nntp_open(server, port, buf, ssl_type);
160 nntp_sock = nntp_open(server, port, buf);
163 if (nntp_sock == NULL)
166 session = g_new(NNTPSession, 1);
167 SESSION(session)->type = SESSION_NEWS;
168 SESSION(session)->server = g_strdup(server);
169 session->nntp_sock = nntp_sock;
170 SESSION(session)->sock = nntp_sock->sock;
171 SESSION(session)->connected = TRUE;
172 SESSION(session)->phase = SESSION_READY;
173 SESSION(session)->last_access_time = time(NULL);
174 SESSION(session)->data = NULL;
175 session->group = NULL;
177 return SESSION(session);
180 void news_session_destroy(NNTPSession *session)
182 nntp_close(session->nntp_sock);
183 session->nntp_sock = NULL;
184 SESSION(session)->sock = NULL;
186 g_free(session->group);
189 static Session *news_session_new_for_folder(Folder *folder)
193 const gchar *userid = NULL;
194 gchar *passwd = NULL;
197 g_return_val_if_fail(folder != NULL, NULL);
198 g_return_val_if_fail(folder->account != NULL, NULL);
200 ac = folder->account;
203 if (ac->set_nntpport) {
206 port = ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
209 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
211 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
213 if (ac->passwd && ac->passwd[0])
214 passwd = g_strdup(ac->passwd);
216 passwd = input_dialog_query_password(ac->nntp_server,
221 session = news_session_new(ac->nntp_server,
223 userid, passwd, ac->ssl_nntp);
225 session = news_session_new(ac->nntp_server,
234 NNTPSession *news_session_get(Folder *folder)
236 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
238 g_return_val_if_fail(folder != NULL, NULL);
239 g_return_val_if_fail(folder->type == F_NEWS, NULL);
240 g_return_val_if_fail(folder->account != NULL, NULL);
242 if (!rfolder->session) {
243 rfolder->session = news_session_new_for_folder(folder);
245 return NNTP_SESSION(rfolder->session);
248 if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
249 rfolder->session->last_access_time = time(NULL);
251 return NNTP_SESSION(rfolder->session);
254 if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
256 log_warning(_("NNTP connection to %s:%d has been"
257 " disconnected. Reconnecting...\n"),
258 folder->account->nntp_server,
259 folder->account->set_nntpport ?
260 folder->account->nntpport : NNTP_PORT);
261 session_destroy(rfolder->session);
262 rfolder->session = news_session_new_for_folder(folder);
265 if (rfolder->session)
266 rfolder->session->last_access_time = time(NULL);
268 return NNTP_SESSION(rfolder->session);
271 GSList *news_get_article_list(Folder *folder, FolderItem *item,
275 NNTPSession *session;
277 g_return_val_if_fail(folder != NULL, NULL);
278 g_return_val_if_fail(item != NULL, NULL);
279 g_return_val_if_fail(folder->type == F_NEWS, NULL);
281 session = news_session_get(folder);
284 alist = procmsg_read_cache(item, FALSE);
285 item->last_num = procmsg_get_last_num_in_cache(alist);
286 } else if (use_cache) {
291 alist = procmsg_read_cache(item, FALSE);
293 cache_last = procmsg_get_last_num_in_cache(alist);
294 newlist = news_get_uncached_articles
295 (session, item, cache_last, &first, &last);
296 alist = news_delete_old_articles(alist, item, first);
298 alist = g_slist_concat(alist, newlist);
299 item->last_num = last;
303 alist = news_get_uncached_articles
304 (session, item, 0, NULL, &last);
305 news_delete_all_articles(item);
306 item->last_num = last;
309 procmsg_set_flags(alist, item);
316 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
318 gchar *path, *filename;
319 NNTPSession *session;
322 g_return_val_if_fail(folder != NULL, NULL);
323 g_return_val_if_fail(item != NULL, NULL);
325 path = folder_item_get_path(item);
326 if (!is_dir_exist(path))
328 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
331 if (is_file_exist(filename)) {
332 debug_print(_("article %d has been already cached.\n"), num);
336 session = news_session_get(folder);
342 ok = news_select_group(session, item->path, NULL, NULL, NULL);
344 if (ok != NN_SUCCESS) {
345 g_warning(_("can't select group %s\n"), item->path);
350 debug_print(_("getting article %d...\n"), num);
351 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
355 g_warning(_("can't read article %d\n"), num);
363 gint news_scan_group(Folder *folder, FolderItem *item)
365 NNTPSession *session;
366 gint num = 0, first = 0, last = 0;
367 gint new = 0, unread = 0, total = 0;
368 gint min = 0, max = 0;
372 g_return_val_if_fail(folder != NULL, -1);
373 g_return_val_if_fail(item != NULL, -1);
375 session = news_session_get(folder);
376 if (!session) return -1;
378 ok = news_select_group(session, item->path, &num, &first, &last);
379 if (ok != NN_SUCCESS) {
380 log_warning(_("can't set group: %s\n"), item->path);
385 item->new = item->unread = item->total = item->last_num = 0;
389 path = folder_item_get_path(item);
390 if (path && is_dir_exist(path)) {
391 procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
396 if (max < first || last < min)
397 new = unread = total = num;
401 else if (first < min) {
408 else if (max < last) {
410 unread += last - max;
413 if (new > num) new = num;
414 if (unread > num) unread = num;
418 item->unread = unread;
420 item->last_num = last;
425 static NewsGroupInfo *news_group_info_new(const gchar *name,
426 gint first, gint last, gchar type)
428 NewsGroupInfo *ginfo;
430 ginfo = g_new(NewsGroupInfo, 1);
431 ginfo->name = g_strdup(name);
432 ginfo->first = first;
439 static void news_group_info_free(NewsGroupInfo *ginfo)
445 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
446 NewsGroupInfo *ginfo2)
448 return g_strcasecmp(ginfo1->name, ginfo2->name);
451 GSList *news_get_group_list(Folder *folder)
453 gchar *path, *filename;
457 gchar buf[NNTPBUFSIZE];
459 g_return_val_if_fail(folder != NULL, NULL);
460 g_return_val_if_fail(folder->type == F_NEWS, NULL);
462 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
463 if (!is_dir_exist(path))
465 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
468 if ((fp = fopen(filename, "rb")) == NULL) {
469 NNTPSession *session;
471 session = news_session_get(folder);
477 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
483 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
484 log_warning(_("can't retrieve newsgroup list\n"));
485 session_destroy(SESSION(session));
486 REMOTE_FOLDER(folder)->session = NULL;
491 if ((fp = fopen(filename, "rb")) == NULL) {
492 FILE_OP_ERROR(filename, "fopen");
498 while (fgets(buf, sizeof(buf), fp) != NULL) {
504 NewsGroupInfo *ginfo;
512 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
515 ginfo = news_group_info_new(name, first_num, last_num, type);
518 last = list = g_slist_append(NULL, ginfo);
520 last = g_slist_append(last, ginfo);
528 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
535 void news_group_list_free(GSList *group_list)
539 if (!group_list) return;
541 for (cur = group_list; cur != NULL; cur = cur->next)
542 news_group_info_free((NewsGroupInfo *)cur->data);
543 g_slist_free(group_list);
546 void news_remove_group_list_cache(Folder *folder)
548 gchar *path, *filename;
550 g_return_if_fail(folder != NULL);
551 g_return_if_fail(folder->type == F_NEWS);
553 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
554 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
557 if (is_file_exist(filename)) {
558 if (remove(filename) < 0)
559 FILE_OP_ERROR(filename, "remove");
564 gint news_post(Folder *folder, const gchar *file)
566 NNTPSession *session;
570 g_return_val_if_fail(folder != NULL, -1);
571 g_return_val_if_fail(folder->type == F_NEWS, -1);
572 g_return_val_if_fail(file != NULL, -1);
574 session = news_session_get(folder);
575 if (!session) return -1;
577 if ((fp = fopen(file, "rb")) == NULL) {
578 FILE_OP_ERROR(file, "fopen");
582 ok = nntp_post(session->nntp_sock, fp);
583 if (ok != NN_SUCCESS) {
584 log_warning(_("can't post article.\n"));
595 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
596 gint num, gchar *filename)
600 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
604 debug_print("Message-Id = %s, num = %d\n", msgid, num);
607 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
608 log_warning(_("can't retrieve article %d\n"), num);
615 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
619 MsgFlags msgflags = { 0, 0 };
622 filename = folder_item_fetch_msg(item, num);
623 if (filename == NULL)
626 msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
630 r = news_cancel_article(folder, msginfo);
632 procmsg_msginfo_free(msginfo);
637 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
639 return news_get_article_cmd(session, "ARTICLE", num, filename);
642 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
644 return news_get_article_cmd(session, "HEAD", num, filename);
649 * @session: Active NNTP session.
650 * @group: Newsgroup name.
651 * @num: Estimated number of articles.
652 * @first: First article number.
653 * @last: Last article number.
655 * Select newsgroup @group with the GROUP command if it is not already
656 * selected in @session, or article numbers need to be returned.
658 * Return value: NNTP result code.
660 static gint news_select_group(NNTPSession *session, const gchar *group,
661 gint *num, gint *first, gint *last)
664 gint num_, first_, last_;
666 if (!num || !first || !last) {
667 if (session->group && g_strcasecmp(session->group, group) == 0)
674 g_free(session->group);
675 session->group = NULL;
677 ok = nntp_group(session->nntp_sock, group, num, first, last);
678 if (ok == NN_SUCCESS)
679 session->group = g_strdup(group);
684 static GSList *news_get_uncached_articles(NNTPSession *session,
685 FolderItem *item, gint cache_last,
686 gint *rfirst, gint *rlast)
689 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
690 gchar buf[NNTPBUFSIZE];
691 GSList *newlist = NULL;
692 GSList *llast = NULL;
695 if (rfirst) *rfirst = 0;
696 if (rlast) *rlast = 0;
698 g_return_val_if_fail(session != NULL, NULL);
699 g_return_val_if_fail(item != NULL, NULL);
700 g_return_val_if_fail(item->folder != NULL, NULL);
701 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
703 ok = news_select_group(session, item->path, &num, &first, &last);
704 if (ok != NN_SUCCESS) {
705 log_warning(_("can't set group: %s\n"), item->path);
709 /* calculate getting overview range */
711 log_warning(_("invalid article range: %d - %d\n"),
715 if (cache_last < first)
717 else if (last < cache_last)
719 else if (last == cache_last) {
720 debug_print(_("no new articles.\n"));
723 begin = cache_last + 1;
726 if (rfirst) *rfirst = first;
727 if (rlast) *rlast = last;
729 if (prefs_common.max_articles > 0 &&
730 end - begin + 1 > prefs_common.max_articles)
731 begin = end - prefs_common.max_articles + 1;
733 log_message(_("getting xover %d - %d in %s...\n"),
734 begin, end, item->path);
735 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
736 log_warning(_("can't get xover\n"));
741 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
742 log_warning(_("error occurred while getting xover.\n"));
746 if (buf[0] == '.' && buf[1] == '\r') break;
748 msginfo = news_parse_xover(buf);
750 log_warning(_("invalid xover line: %s\n"), buf);
754 msginfo->folder = item;
755 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
756 msginfo->flags.tmp_flags = MSG_NEWS;
757 msginfo->newsgroups = g_strdup(item->path);
760 llast = newlist = g_slist_append(newlist, msginfo);
762 llast = g_slist_append(llast, msginfo);
767 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
768 log_warning(_("can't get xhdr\n"));
775 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
776 log_warning(_("error occurred while getting xhdr.\n"));
780 if (buf[0] == '.' && buf[1] == '\r') break;
782 g_warning("llast == NULL\n");
786 msginfo = (MsgInfo *)llast->data;
787 msginfo->to = news_parse_xhdr(buf, msginfo);
792 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
793 log_warning(_("can't get xhdr\n"));
800 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
801 log_warning(_("error occurred while getting xhdr.\n"));
805 if (buf[0] == '.' && buf[1] == '\r') break;
807 g_warning("llast == NULL\n");
811 msginfo = (MsgInfo *)llast->data;
812 msginfo->cc = news_parse_xhdr(buf, msginfo);
820 #define PARSE_ONE_PARAM(p, srcp) \
822 p = strchr(srcp, '\t'); \
823 if (!p) return NULL; \
828 static MsgInfo *news_parse_xover(const gchar *xover_str)
831 gchar buf[NNTPBUFSIZE];
832 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
834 gint num, size_int, line_int;
837 Xstrdup_a(xover_buf, xover_str, return NULL);
839 PARSE_ONE_PARAM(subject, xover_buf);
840 PARSE_ONE_PARAM(sender, subject);
841 PARSE_ONE_PARAM(date, sender);
842 PARSE_ONE_PARAM(msgid, date);
843 PARSE_ONE_PARAM(ref, msgid);
844 PARSE_ONE_PARAM(size, ref);
845 PARSE_ONE_PARAM(line, size);
846 PARSE_ONE_PARAM(xref, line);
848 tmp = strchr(xref, '\t');
849 if (!tmp) tmp = strchr(line, '\r');
850 if (!tmp) tmp = strchr(line, '\n');
851 if (tmp) *tmp = '\0';
853 num = atoi(xover_str);
854 size_int = atoi(size);
855 line_int = atoi(line);
858 msginfo = g_new0(MsgInfo, 1);
859 msginfo->msgnum = num;
860 msginfo->size = size_int;
862 msginfo->date = g_strdup(date);
863 msginfo->date_t = procheader_date_parse(NULL, date, 0);
865 conv_unmime_header(buf, sizeof(buf), sender, NULL);
866 msginfo->from = g_strdup(buf);
867 msginfo->fromname = procheader_get_fromname(buf);
869 conv_unmime_header(buf, sizeof(buf), subject, NULL);
870 msginfo->subject = g_strdup(buf);
872 extract_parenthesis(msgid, '<', '>');
875 msginfo->msgid = g_strdup(msgid);
877 msginfo->references = g_strdup(ref);
878 eliminate_parenthesis(ref, '(', ')');
879 if ((p = strrchr(ref, '<')) != NULL) {
880 extract_parenthesis(p, '<', '>');
883 msginfo->inreplyto = g_strdup(p);
886 msginfo->xref = g_strdup(xref);
887 p = msginfo->xref+strlen(msginfo->xref) - 1;
888 while (*p == '\r' || *p == '\n') {
896 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
902 p = strchr(xhdr_str, ' ');
908 num = atoi(xhdr_str);
909 if (msginfo->msgnum != num) return NULL;
911 tmp = strchr(p, '\r');
912 if (!tmp) tmp = strchr(p, '\n');
915 return g_strndup(p, tmp - p);
920 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
927 g_return_val_if_fail(item != NULL, alist);
928 g_return_val_if_fail(item->folder != NULL, alist);
929 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
931 if (first < 2) return alist;
933 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
935 dir = folder_item_get_path(item);
936 remove_numbered_files(dir, 1, first - 1);
939 for (cur = alist; cur != NULL; ) {
942 msginfo = (MsgInfo *)cur->data;
943 if (msginfo && msginfo->msgnum < first) {
944 procmsg_msginfo_free(msginfo);
945 alist = g_slist_remove(alist, msginfo);
954 static void news_delete_all_articles(FolderItem *item)
958 g_return_if_fail(item != NULL);
959 g_return_if_fail(item->folder != NULL);
960 g_return_if_fail(item->folder->type == F_NEWS);
962 debug_print(_("\tDeleting all cached articles... "));
964 dir = folder_item_get_path(item);
965 remove_all_numbered_files(dir);
968 debug_print(_("done.\n"));
971 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
977 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
978 G_DIR_SEPARATOR, (gint)msginfo);
982 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
983 FILE_OP_ERROR(tmp, "fopen");
986 if (change_file_mode_rw(tmpfp, tmp) < 0) {
987 FILE_OP_ERROR(tmp, "chmod");
988 g_warning(_("can't change file mode\n"));
991 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
992 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
993 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
994 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
995 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
996 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
997 get_rfc822_date(buf, sizeof(buf));
998 fprintf(tmpfp, "Date: %s\r\n", buf);
999 fprintf(tmpfp, "\r\n");
1000 fprintf(tmpfp, "removed with sylpheed\r\n");
1004 news_post(folder, tmp);