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,
396 new = unread = total = num;
397 } else if (max < first) {
398 new = unread = total = num;
399 } else if (last > max) {
401 unread += last - max;
402 if (new > num) new = num;
403 if (unread > num) unread = num;
406 item->unread = unread;
408 item->last_num = last;
413 static NewsGroupInfo *news_group_info_new(const gchar *name,
414 gint first, gint last, gchar type)
416 NewsGroupInfo *ginfo;
418 ginfo = g_new(NewsGroupInfo, 1);
419 ginfo->name = g_strdup(name);
420 ginfo->first = first;
427 static void news_group_info_free(NewsGroupInfo *ginfo)
433 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
434 NewsGroupInfo *ginfo2)
436 return g_strcasecmp(ginfo1->name, ginfo2->name);
439 GSList *news_get_group_list(Folder *folder)
441 gchar *path, *filename;
445 gchar buf[NNTPBUFSIZE];
447 g_return_val_if_fail(folder != NULL, NULL);
448 g_return_val_if_fail(folder->type == F_NEWS, NULL);
450 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
451 if (!is_dir_exist(path))
453 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
456 if ((fp = fopen(filename, "rb")) == NULL) {
457 NNTPSession *session;
459 session = news_session_get(folder);
465 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
471 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
472 log_warning(_("can't retrieve newsgroup list\n"));
473 session_destroy(SESSION(session));
474 REMOTE_FOLDER(folder)->session = NULL;
479 if ((fp = fopen(filename, "rb")) == NULL) {
480 FILE_OP_ERROR(filename, "fopen");
486 while (fgets(buf, sizeof(buf), fp) != NULL) {
492 NewsGroupInfo *ginfo;
500 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
503 ginfo = news_group_info_new(name, first_num, last_num, type);
506 last = list = g_slist_append(NULL, ginfo);
508 last = g_slist_append(last, ginfo);
516 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
523 void news_group_list_free(GSList *group_list)
527 if (!group_list) return;
529 for (cur = group_list; cur != NULL; cur = cur->next)
530 news_group_info_free((NewsGroupInfo *)cur->data);
531 g_slist_free(group_list);
534 void news_remove_group_list_cache(Folder *folder)
536 gchar *path, *filename;
538 g_return_if_fail(folder != NULL);
539 g_return_if_fail(folder->type == F_NEWS);
541 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
542 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
545 if (is_file_exist(filename)) {
546 if (remove(filename) < 0)
547 FILE_OP_ERROR(filename, "remove");
552 gint news_post(Folder *folder, const gchar *file)
554 NNTPSession *session;
558 g_return_val_if_fail(folder != NULL, -1);
559 g_return_val_if_fail(folder->type == F_NEWS, -1);
560 g_return_val_if_fail(file != NULL, -1);
562 session = news_session_get(folder);
563 if (!session) return -1;
565 if ((fp = fopen(file, "rb")) == NULL) {
566 FILE_OP_ERROR(file, "fopen");
570 ok = nntp_post(session->nntp_sock, fp);
571 if (ok != NN_SUCCESS) {
572 log_warning(_("can't post article.\n"));
583 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
584 gint num, gchar *filename)
588 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
592 debug_print("Message-Id = %s, num = %d\n", msgid, num);
595 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
596 log_warning(_("can't retrieve article %d\n"), num);
603 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
607 MsgFlags msgflags = { 0, 0 };
610 filename = folder_item_fetch_msg(item, num);
611 if (filename == NULL)
614 msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
618 r = news_cancel_article(folder, msginfo);
620 procmsg_msginfo_free(msginfo);
625 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
627 return news_get_article_cmd(session, "ARTICLE", num, filename);
630 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
632 return news_get_article_cmd(session, "HEAD", num, filename);
637 * @session: Active NNTP session.
638 * @group: Newsgroup name.
639 * @num: Estimated number of articles.
640 * @first: First article number.
641 * @last: Last article number.
643 * Select newsgroup @group with the GROUP command if it is not already
644 * selected in @session, or article numbers need to be returned.
646 * Return value: NNTP result code.
648 static gint news_select_group(NNTPSession *session, const gchar *group,
649 gint *num, gint *first, gint *last)
652 gint num_, first_, last_;
654 if (!num || !first || !last) {
655 if (session->group && g_strcasecmp(session->group, group) == 0)
662 g_free(session->group);
663 session->group = NULL;
665 ok = nntp_group(session->nntp_sock, group, num, first, last);
666 if (ok == NN_SUCCESS)
667 session->group = g_strdup(group);
672 static GSList *news_get_uncached_articles(NNTPSession *session,
673 FolderItem *item, gint cache_last,
674 gint *rfirst, gint *rlast)
677 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
678 gchar buf[NNTPBUFSIZE];
679 GSList *newlist = NULL;
680 GSList *llast = NULL;
683 if (rfirst) *rfirst = 0;
684 if (rlast) *rlast = 0;
686 g_return_val_if_fail(session != NULL, NULL);
687 g_return_val_if_fail(item != NULL, NULL);
688 g_return_val_if_fail(item->folder != NULL, NULL);
689 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
691 ok = news_select_group(session, item->path, &num, &first, &last);
692 if (ok != NN_SUCCESS) {
693 log_warning(_("can't set group: %s\n"), item->path);
697 /* calculate getting overview range */
699 log_warning(_("invalid article range: %d - %d\n"),
703 if (cache_last < first)
705 else if (last < cache_last)
707 else if (last == cache_last) {
708 debug_print(_("no new articles.\n"));
711 begin = cache_last + 1;
714 if (rfirst) *rfirst = first;
715 if (rlast) *rlast = last;
717 if (prefs_common.max_articles > 0 &&
718 end - begin + 1 > prefs_common.max_articles)
719 begin = end - prefs_common.max_articles + 1;
721 log_message(_("getting xover %d - %d in %s...\n"),
722 begin, end, item->path);
723 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
724 log_warning(_("can't get xover\n"));
729 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
730 log_warning(_("error occurred while getting xover.\n"));
734 if (buf[0] == '.' && buf[1] == '\r') break;
736 msginfo = news_parse_xover(buf);
738 log_warning(_("invalid xover line: %s\n"), buf);
742 msginfo->folder = item;
743 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
744 msginfo->flags.tmp_flags = MSG_NEWS;
745 msginfo->newsgroups = g_strdup(item->path);
748 llast = newlist = g_slist_append(newlist, msginfo);
750 llast = g_slist_append(llast, msginfo);
755 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
756 log_warning(_("can't get xhdr\n"));
763 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
764 log_warning(_("error occurred while getting xhdr.\n"));
768 if (buf[0] == '.' && buf[1] == '\r') break;
770 g_warning("llast == NULL\n");
774 msginfo = (MsgInfo *)llast->data;
775 msginfo->to = news_parse_xhdr(buf, msginfo);
780 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
781 log_warning(_("can't get xhdr\n"));
788 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
789 log_warning(_("error occurred while getting xhdr.\n"));
793 if (buf[0] == '.' && buf[1] == '\r') break;
795 g_warning("llast == NULL\n");
799 msginfo = (MsgInfo *)llast->data;
800 msginfo->cc = news_parse_xhdr(buf, msginfo);
808 #define PARSE_ONE_PARAM(p, srcp) \
810 p = strchr(srcp, '\t'); \
811 if (!p) return NULL; \
816 static MsgInfo *news_parse_xover(const gchar *xover_str)
819 gchar buf[NNTPBUFSIZE];
820 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
822 gint num, size_int, line_int;
825 Xstrdup_a(xover_buf, xover_str, return NULL);
827 PARSE_ONE_PARAM(subject, xover_buf);
828 PARSE_ONE_PARAM(sender, subject);
829 PARSE_ONE_PARAM(date, sender);
830 PARSE_ONE_PARAM(msgid, date);
831 PARSE_ONE_PARAM(ref, msgid);
832 PARSE_ONE_PARAM(size, ref);
833 PARSE_ONE_PARAM(line, size);
834 PARSE_ONE_PARAM(xref, line);
836 tmp = strchr(xref, '\t');
837 if (!tmp) tmp = strchr(line, '\r');
838 if (!tmp) tmp = strchr(line, '\n');
839 if (tmp) *tmp = '\0';
841 num = atoi(xover_str);
842 size_int = atoi(size);
843 line_int = atoi(line);
846 msginfo = g_new0(MsgInfo, 1);
847 msginfo->msgnum = num;
848 msginfo->size = size_int;
850 msginfo->date = g_strdup(date);
851 msginfo->date_t = procheader_date_parse(NULL, date, 0);
853 conv_unmime_header(buf, sizeof(buf), sender, NULL);
854 msginfo->from = g_strdup(buf);
855 msginfo->fromname = procheader_get_fromname(buf);
857 conv_unmime_header(buf, sizeof(buf), subject, NULL);
858 msginfo->subject = g_strdup(buf);
860 extract_parenthesis(msgid, '<', '>');
863 msginfo->msgid = g_strdup(msgid);
865 msginfo->references = g_strdup(ref);
866 eliminate_parenthesis(ref, '(', ')');
867 if ((p = strrchr(ref, '<')) != NULL) {
868 extract_parenthesis(p, '<', '>');
871 msginfo->inreplyto = g_strdup(p);
874 msginfo->xref = g_strdup(xref);
875 p = msginfo->xref+strlen(msginfo->xref) - 1;
876 while (*p == '\r' || *p == '\n') {
884 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
890 p = strchr(xhdr_str, ' ');
896 num = atoi(xhdr_str);
897 if (msginfo->msgnum != num) return NULL;
899 tmp = strchr(p, '\r');
900 if (!tmp) tmp = strchr(p, '\n');
903 return g_strndup(p, tmp - p);
908 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
915 g_return_val_if_fail(item != NULL, alist);
916 g_return_val_if_fail(item->folder != NULL, alist);
917 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
919 if (first < 2) return alist;
921 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
923 dir = folder_item_get_path(item);
924 remove_numbered_files(dir, 1, first - 1);
927 for (cur = alist; cur != NULL; ) {
930 msginfo = (MsgInfo *)cur->data;
931 if (msginfo && msginfo->msgnum < first) {
932 procmsg_msginfo_free(msginfo);
933 alist = g_slist_remove(alist, msginfo);
942 static void news_delete_all_articles(FolderItem *item)
946 g_return_if_fail(item != NULL);
947 g_return_if_fail(item->folder != NULL);
948 g_return_if_fail(item->folder->type == F_NEWS);
950 debug_print(_("\tDeleting all cached articles... "));
952 dir = folder_item_get_path(item);
953 remove_all_numbered_files(dir);
956 debug_print(_("done.\n"));
959 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
965 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
966 G_DIR_SEPARATOR, (gint)msginfo);
970 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
971 FILE_OP_ERROR(tmp, "fopen");
974 if (change_file_mode_rw(tmpfp, tmp) < 0) {
975 FILE_OP_ERROR(tmp, "chmod");
976 g_warning(_("can't change file mode\n"));
979 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
980 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
981 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
982 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
983 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
984 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
985 get_rfc822_date(buf, sizeof(buf));
986 fprintf(tmpfp, "Date: %s\r\n", buf);
987 fprintf(tmpfp, "\r\n");
988 fprintf(tmpfp, "removed with sylpheed\r\n");
992 news_post(folder, tmp);