2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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.
39 #include "procheader.h"
42 #include "statusbar.h"
45 #include "prefs_common.h"
46 #include "prefs_account.h"
47 #include "inputdialog.h"
48 #include "alertpanel.h"
52 static Session *news_session_new (const gchar *server,
57 static gint news_get_article_cmd (NNTPSession *session,
61 static gint news_get_article (NNTPSession *session,
64 static gint news_get_header (NNTPSession *session,
68 static gint news_select_group (NNTPSession *session,
70 static GSList *news_get_uncached_articles(NNTPSession *session,
75 static MsgInfo *news_parse_xover (const gchar *xover_str);
76 static gchar *news_parse_xhdr (const gchar *xhdr_str,
78 static GSList *news_delete_old_articles (GSList *alist,
81 static void news_delete_all_articles (FolderItem *item);
83 static void news_group_list_free(GSList * list);
86 static Session *news_session_new(const gchar *server, gushort port,
87 const gchar *userid, const gchar *passwd)
89 gchar buf[NNTPBUFSIZE];
91 NNTPSockInfo *nntp_sock;
93 g_return_val_if_fail(server != NULL, NULL);
95 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
98 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
100 nntp_sock = nntp_open(server, port, buf);
101 if (nntp_sock == NULL)
104 session = g_new(NNTPSession, 1);
105 SESSION(session)->type = SESSION_NEWS;
106 SESSION(session)->server = g_strdup(server);
107 session->nntp_sock = nntp_sock;
108 SESSION(session)->sock = nntp_sock->sock;
109 SESSION(session)->connected = TRUE;
110 SESSION(session)->phase = SESSION_READY;
111 SESSION(session)->data = NULL;
112 session->group = NULL;
113 session->group_list = NULL;
115 return SESSION(session);
118 void news_session_destroy(NNTPSession *session)
120 nntp_close(session->nntp_sock);
121 session->nntp_sock = NULL;
122 SESSION(session)->sock = NULL;
124 news_group_list_free(session->group_list);
125 session->group_list = NULL;
127 g_free(session->group);
130 static gchar *news_query_password(const gchar *server, const gchar *user)
135 message = g_strdup_printf(_("Input password for %s on %s:"),
137 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
143 static Session *news_session_new_for_folder(Folder *folder)
147 const gchar *userid = NULL;
148 gchar *passwd = NULL;
150 g_return_val_if_fail(folder != NULL, NULL);
151 g_return_val_if_fail(folder->account != NULL, NULL);
153 ac = folder->account;
154 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
156 if (ac->passwd && ac->passwd[0])
157 passwd = g_strdup(ac->passwd);
159 passwd = news_query_password(ac->nntp_server, userid);
162 session = news_session_new(ac->nntp_server,
163 ac->set_nntpport ? ac->nntpport : NNTP_PORT,
170 NNTPSession *news_session_get(Folder *folder)
172 NNTPSession *session;
174 g_return_val_if_fail(folder != NULL, NULL);
175 g_return_val_if_fail(folder->type == F_NEWS, NULL);
176 g_return_val_if_fail(folder->account != NULL, NULL);
178 if (!REMOTE_FOLDER(folder)->session) {
179 REMOTE_FOLDER(folder)->session =
180 news_session_new_for_folder(folder);
182 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
185 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
187 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
188 log_warning(_("NNTP connection to %s:%d has been"
189 " disconnected. Reconnecting...\n"),
190 folder->account->nntp_server,
191 folder->account->set_nntpport ?
192 folder->account->nntpport : NNTP_PORT);
193 session_destroy(REMOTE_FOLDER(folder)->session);
194 REMOTE_FOLDER(folder)->session =
195 news_session_new_for_folder(folder);
199 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
202 GSList *news_get_article_list(Folder *folder, FolderItem *item,
206 NNTPSession *session;
208 g_return_val_if_fail(folder != NULL, NULL);
209 g_return_val_if_fail(item != NULL, NULL);
210 g_return_val_if_fail(folder->type == F_NEWS, NULL);
212 session = news_session_get(folder);
215 alist = procmsg_read_cache(item, FALSE);
216 item->last_num = procmsg_get_last_num_in_cache(alist);
217 } else if (use_cache) {
222 alist = procmsg_read_cache(item, FALSE);
224 cache_last = procmsg_get_last_num_in_cache(alist);
225 newlist = news_get_uncached_articles
226 (session, item, cache_last, &first, &last);
227 alist = news_delete_old_articles(alist, item, first);
229 alist = g_slist_concat(alist, newlist);
230 item->last_num = last;
234 alist = news_get_uncached_articles
235 (session, item, 0, NULL, &last);
236 news_delete_all_articles(item);
237 item->last_num = last;
240 procmsg_set_flags(alist, item);
247 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
249 gchar *path, *filename;
250 NNTPSession *session;
253 g_return_val_if_fail(folder != NULL, NULL);
254 g_return_val_if_fail(item != NULL, NULL);
256 path = folder_item_get_path(item);
257 if (!is_dir_exist(path))
259 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
262 if (is_file_exist(filename)) {
263 debug_print(_("article %d has been already cached.\n"), num);
267 session = news_session_get(folder);
273 ok = news_select_group(session, item->path);
275 if (ok != NN_SUCCESS) {
276 g_warning(_("can't select group %s\n"), item->path);
281 debug_print(_("getting article %d...\n"), num);
282 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
286 g_warning(_("can't read article %d\n"), num);
294 void news_scan_group(Folder *folder, FolderItem *item)
298 static struct NNTPGroupInfo * group_info_new(gchar * name,
299 gint first, gint last,
302 struct NNTPGroupInfo * info;
304 info = g_new(struct NNTPGroupInfo, 1);
307 info->name = g_strdup(name);
315 static void group_info_free(struct NNTPGroupInfo * info)
321 static void news_group_list_free(GSList * list)
323 g_slist_foreach(list, (GFunc) group_info_free, NULL);
327 gint news_group_info_compare(struct NNTPGroupInfo * info1,
328 struct NNTPGroupInfo * info2)
330 return g_strcasecmp(info1->name, info2->name);
333 GSList *news_get_group_list(Folder *folder)
335 gchar *path, *filename;
339 gchar buf[NNTPBUFSIZE];
340 NNTPSession *session;
342 g_return_val_if_fail(folder != NULL, NULL);
343 g_return_val_if_fail(folder->type == F_NEWS, NULL);
345 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
346 if (!is_dir_exist(path))
348 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
351 session = news_session_get(folder);
357 if (session->group_list) {
359 return session->group_list;
362 if ((fp = fopen(filename, "r")) == NULL) {
363 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
369 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
370 log_warning(_("can't retrieve newsgroup list\n"));
375 if ((fp = fopen(filename, "r")) == NULL) {
376 FILE_OP_ERROR(filename, "fopen");
382 while (fgets(buf, sizeof(buf), fp) != NULL) {
389 struct NNTPGroupInfo * info;
392 p = strchr(cur, ' ');
400 p = strchr(cur, ' ');
404 last_article = atoi(cur);
407 p = strchr(cur, ' ');
411 first_article = atoi(cur);
416 info = group_info_new(name, first_article,
422 last = list = g_slist_append(NULL, info);
424 last = g_slist_append(last, info);
432 list = g_slist_sort(list, (GCompareFunc) news_group_info_compare);
434 session->group_list = list;
441 void news_cancel_group_list_cache(Folder *folder)
443 gchar *path, *filename;
444 NNTPSession *session;
446 g_return_if_fail(folder != NULL);
447 g_return_if_fail(folder->type == F_NEWS);
449 session = news_session_get(folder);
453 news_group_list_free(session->group_list);
454 session->group_list = NULL;
456 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
457 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
460 if (is_file_exist(filename)) {
461 if (remove(filename) < 0)
462 FILE_OP_ERROR(filename, "remove");
467 gint news_post(Folder *folder, const gchar *file)
469 NNTPSession *session;
473 g_return_val_if_fail(folder != NULL, -1);
474 g_return_val_if_fail(folder->type == F_NEWS, -1);
475 g_return_val_if_fail(file != NULL, -1);
477 session = news_session_get(folder);
478 if (!session) return -1;
480 if ((fp = fopen(file, "r")) == NULL) {
481 FILE_OP_ERROR(file, "fopen");
485 ok = nntp_post(session->nntp_sock, fp);
486 if (ok != NN_SUCCESS) {
487 log_warning(_("can't post article.\n"));
498 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
499 gint num, gchar *filename)
503 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
507 debug_print("Message-Id = %s, num = %d\n", msgid, num);
510 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
511 log_warning(_("can't retrieve article %d\n"), num);
518 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
520 return news_get_article_cmd(session, "ARTICLE", num, filename);
523 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
525 return news_get_article_cmd(session, "HEAD", num, filename);
530 * @session: Active NNTP session.
531 * @group: Newsgroup name.
533 * Select newsgroup @group with the GROUP command if it is not already
534 * selected in @session.
536 * Return value: NNTP result code.
538 static gint news_select_group(NNTPSession *session, const gchar *group)
541 gint num, first, last;
543 if (session->group && g_strcasecmp(session->group, group) == 0)
546 g_free(session->group);
547 session->group = NULL;
549 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
550 if (ok == NN_SUCCESS)
551 session->group = g_strdup(group);
556 static GSList *news_get_uncached_articles(NNTPSession *session,
557 FolderItem *item, gint cache_last,
558 gint *rfirst, gint *rlast)
561 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
562 gchar buf[NNTPBUFSIZE];
563 GSList *newlist = NULL;
564 GSList *llast = NULL;
567 if (rfirst) *rfirst = 0;
568 if (rlast) *rlast = 0;
570 g_return_val_if_fail(session != NULL, NULL);
571 g_return_val_if_fail(item != NULL, NULL);
572 g_return_val_if_fail(item->folder != NULL, NULL);
573 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
575 g_free(session->group);
576 session->group = NULL;
578 ok = nntp_group(session->nntp_sock, item->path,
579 &num, &first, &last);
580 if (ok != NN_SUCCESS) {
581 log_warning(_("can't set group: %s\n"), item->path);
584 session->group = g_strdup(item->path);
586 /* calculate getting overview range */
588 log_warning(_("invalid article range: %d - %d\n"),
592 if (cache_last < first)
594 else if (last < cache_last)
596 else if (last == cache_last) {
597 debug_print(_("no new articles.\n"));
600 begin = cache_last + 1;
603 if (rfirst) *rfirst = first;
604 if (rlast) *rlast = last;
606 if (prefs_common.max_articles > 0 &&
607 end - begin + 1 > prefs_common.max_articles)
608 begin = end - prefs_common.max_articles + 1;
610 log_message(_("getting xover %d - %d in %s...\n"),
611 begin, end, item->path);
612 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
613 log_warning(_("can't get xover\n"));
618 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
619 log_warning(_("error occurred while getting xover.\n"));
623 if (buf[0] == '.' && buf[1] == '\r') break;
625 msginfo = news_parse_xover(buf);
627 log_warning(_("invalid xover line: %s\n"), buf);
631 msginfo->folder = item;
632 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
633 msginfo->flags.tmp_flags = MSG_NEWS;
634 msginfo->newsgroups = g_strdup(item->path);
637 llast = newlist = g_slist_append(newlist, msginfo);
639 llast = g_slist_append(llast, msginfo);
644 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
645 log_warning(_("can't get xhdr\n"));
652 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
653 log_warning(_("error occurred while getting xhdr.\n"));
657 if (buf[0] == '.' && buf[1] == '\r') break;
659 g_warning("llast == NULL\n");
663 msginfo = (MsgInfo *)llast->data;
664 msginfo->to = news_parse_xhdr(buf, msginfo);
669 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
670 log_warning(_("can't get xhdr\n"));
677 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
678 log_warning(_("error occurred while getting xhdr.\n"));
682 if (buf[0] == '.' && buf[1] == '\r') break;
684 g_warning("llast == NULL\n");
688 msginfo = (MsgInfo *)llast->data;
689 msginfo->cc = news_parse_xhdr(buf, msginfo);
697 #define PARSE_ONE_PARAM(p, srcp) \
699 p = strchr(srcp, '\t'); \
700 if (!p) return NULL; \
705 static MsgInfo *news_parse_xover(const gchar *xover_str)
708 gchar buf[NNTPBUFSIZE];
709 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
711 gint num, size_int, line_int;
714 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
715 strcpy(xover_buf, xover_str);
717 PARSE_ONE_PARAM(subject, xover_buf);
718 PARSE_ONE_PARAM(sender, subject);
719 PARSE_ONE_PARAM(date, sender);
720 PARSE_ONE_PARAM(msgid, date);
721 PARSE_ONE_PARAM(ref, msgid);
722 PARSE_ONE_PARAM(size, ref);
723 PARSE_ONE_PARAM(line, size);
725 tmp = strchr(line, '\t');
726 if (!tmp) tmp = strchr(line, '\r');
727 if (!tmp) tmp = strchr(line, '\n');
728 if (tmp) *tmp = '\0';
730 num = atoi(xover_str);
731 size_int = atoi(size);
732 line_int = atoi(line);
735 msginfo = g_new0(MsgInfo, 1);
736 msginfo->msgnum = num;
737 msginfo->size = size_int;
739 msginfo->date = g_strdup(date);
740 msginfo->date_t = procheader_date_parse(NULL, date, 0);
742 conv_unmime_header(buf, sizeof(buf), sender, NULL);
743 msginfo->from = g_strdup(buf);
744 msginfo->fromname = procheader_get_fromname(buf);
746 conv_unmime_header(buf, sizeof(buf), subject, NULL);
747 msginfo->subject = g_strdup(buf);
749 extract_parenthesis(msgid, '<', '>');
752 msginfo->msgid = g_strdup(msgid);
754 msginfo->references = g_strdup(ref);
755 eliminate_parenthesis(ref, '(', ')');
756 if ((p = strrchr(ref, '<')) != NULL) {
757 extract_parenthesis(p, '<', '>');
760 msginfo->inreplyto = g_strdup(p);
766 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
772 p = strchr(xhdr_str, ' ');
778 num = atoi(xhdr_str);
779 if (msginfo->msgnum != num) return NULL;
781 tmp = strchr(p, '\r');
782 if (!tmp) tmp = strchr(p, '\n');
785 return g_strndup(p, tmp - p);
790 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
797 g_return_val_if_fail(item != NULL, alist);
798 g_return_val_if_fail(item->folder != NULL, alist);
799 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
801 if (first < 2) return alist;
803 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
805 dir = folder_item_get_path(item);
806 remove_numbered_files(dir, 1, first - 1);
809 for (cur = alist; cur != NULL; ) {
812 msginfo = (MsgInfo *)cur->data;
813 if (msginfo && msginfo->msgnum < first) {
814 procmsg_msginfo_free(msginfo);
815 alist = g_slist_remove(alist, msginfo);
824 static void news_delete_all_articles(FolderItem *item)
828 g_return_if_fail(item != NULL);
829 g_return_if_fail(item->folder != NULL);
830 g_return_if_fail(item->folder->type == F_NEWS);
832 debug_print(_("\tDeleting all cached articles... "));
834 dir = folder_item_get_path(item);
835 remove_all_numbered_files(dir);
838 debug_print(_("done.\n"));