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.
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 static Session *news_session_new (const gchar *server,
58 static gint news_get_article_cmd (NNTPSession *session,
62 static gint news_get_article (NNTPSession *session,
65 static gint news_get_header (NNTPSession *session,
69 static gint news_select_group (NNTPSession *session,
71 static GSList *news_get_uncached_articles(NNTPSession *session,
76 static MsgInfo *news_parse_xover (const gchar *xover_str);
77 static gchar *news_parse_xhdr (const gchar *xhdr_str,
79 static GSList *news_delete_old_articles (GSList *alist,
82 static void news_delete_all_articles (FolderItem *item);
84 static void news_group_list_free(GSList * list);
87 static Session *news_session_new(const gchar *server, gushort port,
88 const gchar *userid, const gchar *passwd)
90 gchar buf[NNTPBUFSIZE];
92 NNTPSockInfo *nntp_sock;
94 g_return_val_if_fail(server != NULL, NULL);
96 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
99 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
101 nntp_sock = nntp_open(server, port, buf);
102 if (nntp_sock == NULL)
105 session = g_new(NNTPSession, 1);
106 SESSION(session)->type = SESSION_NEWS;
107 SESSION(session)->server = g_strdup(server);
108 session->nntp_sock = nntp_sock;
109 SESSION(session)->sock = nntp_sock->sock;
110 SESSION(session)->connected = TRUE;
111 SESSION(session)->phase = SESSION_READY;
112 SESSION(session)->last_access_time = time(NULL);
113 SESSION(session)->data = NULL;
114 session->group = NULL;
115 session->group_list = NULL;
117 return SESSION(session);
120 void news_session_destroy(NNTPSession *session)
122 nntp_close(session->nntp_sock);
123 session->nntp_sock = NULL;
124 SESSION(session)->sock = NULL;
126 news_group_list_free(session->group_list);
127 session->group_list = NULL;
129 g_free(session->group);
132 static gchar *news_query_password(const gchar *server, const gchar *user)
137 message = g_strdup_printf(_("Input password for %s on %s:"),
139 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
145 static Session *news_session_new_for_folder(Folder *folder)
149 const gchar *userid = NULL;
150 gchar *passwd = NULL;
152 g_return_val_if_fail(folder != NULL, NULL);
153 g_return_val_if_fail(folder->account != NULL, NULL);
155 ac = folder->account;
156 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
158 if (ac->passwd && ac->passwd[0])
159 passwd = g_strdup(ac->passwd);
161 passwd = news_query_password(ac->nntp_server, userid);
164 session = news_session_new(ac->nntp_server,
165 ac->set_nntpport ? ac->nntpport : NNTP_PORT,
172 NNTPSession *news_session_get(Folder *folder)
174 NNTPSession *session;
176 g_return_val_if_fail(folder != NULL, NULL);
177 g_return_val_if_fail(folder->type == F_NEWS, NULL);
178 g_return_val_if_fail(folder->account != NULL, NULL);
180 if (!REMOTE_FOLDER(folder)->session) {
181 REMOTE_FOLDER(folder)->session =
182 news_session_new_for_folder(folder);
184 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
187 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
189 if (time(NULL) - SESSION(session)->last_access_time < SESSION_TIMEOUT) {
190 SESSION(session)->last_access_time = time(NULL);
195 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
196 log_warning(_("NNTP connection to %s:%d has been"
197 " disconnected. Reconnecting...\n"),
198 folder->account->nntp_server,
199 folder->account->set_nntpport ?
200 folder->account->nntpport : NNTP_PORT);
201 session_destroy(REMOTE_FOLDER(folder)->session);
202 REMOTE_FOLDER(folder)->session =
203 news_session_new_for_folder(folder);
207 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
210 GSList *news_get_article_list(Folder *folder, FolderItem *item,
214 NNTPSession *session;
216 g_return_val_if_fail(folder != NULL, NULL);
217 g_return_val_if_fail(item != NULL, NULL);
218 g_return_val_if_fail(folder->type == F_NEWS, NULL);
220 session = news_session_get(folder);
223 alist = procmsg_read_cache(item, FALSE);
224 item->last_num = procmsg_get_last_num_in_cache(alist);
225 } else if (use_cache) {
230 alist = procmsg_read_cache(item, FALSE);
232 cache_last = procmsg_get_last_num_in_cache(alist);
233 newlist = news_get_uncached_articles
234 (session, item, cache_last, &first, &last);
235 alist = news_delete_old_articles(alist, item, first);
237 alist = g_slist_concat(alist, newlist);
238 item->last_num = last;
242 alist = news_get_uncached_articles
243 (session, item, 0, NULL, &last);
244 news_delete_all_articles(item);
245 item->last_num = last;
248 procmsg_set_flags(alist, item);
255 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
257 gchar *path, *filename;
258 NNTPSession *session;
261 g_return_val_if_fail(folder != NULL, NULL);
262 g_return_val_if_fail(item != NULL, NULL);
264 path = folder_item_get_path(item);
265 if (!is_dir_exist(path))
267 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
270 if (is_file_exist(filename)) {
271 debug_print(_("article %d has been already cached.\n"), num);
275 session = news_session_get(folder);
281 ok = news_select_group(session, item->path);
283 if (ok != NN_SUCCESS) {
284 g_warning(_("can't select group %s\n"), item->path);
289 debug_print(_("getting article %d...\n"), num);
290 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
294 g_warning(_("can't read article %d\n"), num);
302 void news_scan_group(Folder *folder, FolderItem *item)
306 static struct NNTPGroupInfo * group_info_new(gchar * name,
307 gint first, gint last,
310 struct NNTPGroupInfo * info;
312 info = g_new(struct NNTPGroupInfo, 1);
315 info->name = g_strdup(name);
323 static void group_info_free(struct NNTPGroupInfo * info)
329 static void news_group_list_free(GSList * list)
331 g_slist_foreach(list, (GFunc) group_info_free, NULL);
335 gint news_group_info_compare(struct NNTPGroupInfo * info1,
336 struct NNTPGroupInfo * info2)
338 return g_strcasecmp(info1->name, info2->name);
341 GSList *news_get_group_list(Folder *folder)
343 gchar *path, *filename;
347 gchar buf[NNTPBUFSIZE];
348 NNTPSession *session;
350 g_return_val_if_fail(folder != NULL, NULL);
351 g_return_val_if_fail(folder->type == F_NEWS, NULL);
353 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
354 if (!is_dir_exist(path))
356 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
359 session = news_session_get(folder);
365 if (session->group_list) {
367 return session->group_list;
370 if ((fp = fopen(filename, "r")) == NULL) {
371 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
377 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
378 log_warning(_("can't retrieve newsgroup list\n"));
383 if ((fp = fopen(filename, "r")) == NULL) {
384 FILE_OP_ERROR(filename, "fopen");
390 while (fgets(buf, sizeof(buf), fp) != NULL) {
397 struct NNTPGroupInfo * info;
400 p = strchr(cur, ' ');
408 p = strchr(cur, ' ');
412 last_article = atoi(cur);
415 p = strchr(cur, ' ');
419 first_article = atoi(cur);
424 info = group_info_new(name, first_article,
430 last = list = g_slist_append(NULL, info);
432 last = g_slist_append(last, info);
440 list = g_slist_sort(list, (GCompareFunc) news_group_info_compare);
442 session->group_list = list;
449 void news_cancel_group_list_cache(Folder *folder)
451 gchar *path, *filename;
452 NNTPSession *session;
454 g_return_if_fail(folder != NULL);
455 g_return_if_fail(folder->type == F_NEWS);
457 session = news_session_get(folder);
461 news_group_list_free(session->group_list);
462 session->group_list = NULL;
464 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
465 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
468 if (is_file_exist(filename)) {
469 if (remove(filename) < 0)
470 FILE_OP_ERROR(filename, "remove");
475 gint news_post(Folder *folder, const gchar *file)
477 NNTPSession *session;
481 g_return_val_if_fail(folder != NULL, -1);
482 g_return_val_if_fail(folder->type == F_NEWS, -1);
483 g_return_val_if_fail(file != NULL, -1);
485 session = news_session_get(folder);
486 if (!session) return -1;
488 if ((fp = fopen(file, "r")) == NULL) {
489 FILE_OP_ERROR(file, "fopen");
493 ok = nntp_post(session->nntp_sock, fp);
494 if (ok != NN_SUCCESS) {
495 log_warning(_("can't post article.\n"));
506 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
507 gint num, gchar *filename)
511 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
515 debug_print("Message-Id = %s, num = %d\n", msgid, num);
518 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
519 log_warning(_("can't retrieve article %d\n"), num);
526 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
528 return news_get_article_cmd(session, "ARTICLE", num, filename);
531 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
533 return news_get_article_cmd(session, "HEAD", num, filename);
538 * @session: Active NNTP session.
539 * @group: Newsgroup name.
541 * Select newsgroup @group with the GROUP command if it is not already
542 * selected in @session.
544 * Return value: NNTP result code.
546 static gint news_select_group(NNTPSession *session, const gchar *group)
549 gint num, first, last;
551 if (session->group && g_strcasecmp(session->group, group) == 0)
554 g_free(session->group);
555 session->group = NULL;
557 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
558 if (ok == NN_SUCCESS)
559 session->group = g_strdup(group);
564 static GSList *news_get_uncached_articles(NNTPSession *session,
565 FolderItem *item, gint cache_last,
566 gint *rfirst, gint *rlast)
569 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
570 gchar buf[NNTPBUFSIZE];
571 GSList *newlist = NULL;
572 GSList *llast = NULL;
575 if (rfirst) *rfirst = 0;
576 if (rlast) *rlast = 0;
578 g_return_val_if_fail(session != NULL, NULL);
579 g_return_val_if_fail(item != NULL, NULL);
580 g_return_val_if_fail(item->folder != NULL, NULL);
581 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
583 g_free(session->group);
584 session->group = NULL;
586 ok = nntp_group(session->nntp_sock, item->path,
587 &num, &first, &last);
588 if (ok != NN_SUCCESS) {
589 log_warning(_("can't set group: %s\n"), item->path);
592 session->group = g_strdup(item->path);
594 /* calculate getting overview range */
596 log_warning(_("invalid article range: %d - %d\n"),
600 if (cache_last < first)
602 else if (last < cache_last)
604 else if (last == cache_last) {
605 debug_print(_("no new articles.\n"));
608 begin = cache_last + 1;
611 if (rfirst) *rfirst = first;
612 if (rlast) *rlast = last;
614 if (prefs_common.max_articles > 0 &&
615 end - begin + 1 > prefs_common.max_articles)
616 begin = end - prefs_common.max_articles + 1;
618 log_message(_("getting xover %d - %d in %s...\n"),
619 begin, end, item->path);
620 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
621 log_warning(_("can't get xover\n"));
626 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
627 log_warning(_("error occurred while getting xover.\n"));
631 if (buf[0] == '.' && buf[1] == '\r') break;
633 msginfo = news_parse_xover(buf);
635 log_warning(_("invalid xover line: %s\n"), buf);
639 msginfo->folder = item;
640 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
641 msginfo->flags.tmp_flags = MSG_NEWS;
642 msginfo->newsgroups = g_strdup(item->path);
645 llast = newlist = g_slist_append(newlist, msginfo);
647 llast = g_slist_append(llast, msginfo);
652 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
653 log_warning(_("can't get xhdr\n"));
660 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
661 log_warning(_("error occurred while getting xhdr.\n"));
665 if (buf[0] == '.' && buf[1] == '\r') break;
667 g_warning("llast == NULL\n");
671 msginfo = (MsgInfo *)llast->data;
672 msginfo->to = news_parse_xhdr(buf, msginfo);
677 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
678 log_warning(_("can't get xhdr\n"));
685 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
686 log_warning(_("error occurred while getting xhdr.\n"));
690 if (buf[0] == '.' && buf[1] == '\r') break;
692 g_warning("llast == NULL\n");
696 msginfo = (MsgInfo *)llast->data;
697 msginfo->cc = news_parse_xhdr(buf, msginfo);
705 #define PARSE_ONE_PARAM(p, srcp) \
707 p = strchr(srcp, '\t'); \
708 if (!p) return NULL; \
713 static MsgInfo *news_parse_xover(const gchar *xover_str)
716 gchar buf[NNTPBUFSIZE];
717 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
719 gint num, size_int, line_int;
722 Xstrdup_a(xover_buf, xover_str, return NULL);
724 PARSE_ONE_PARAM(subject, xover_buf);
725 PARSE_ONE_PARAM(sender, subject);
726 PARSE_ONE_PARAM(date, sender);
727 PARSE_ONE_PARAM(msgid, date);
728 PARSE_ONE_PARAM(ref, msgid);
729 PARSE_ONE_PARAM(size, ref);
730 PARSE_ONE_PARAM(line, size);
732 tmp = strchr(line, '\t');
733 if (!tmp) tmp = strchr(line, '\r');
734 if (!tmp) tmp = strchr(line, '\n');
735 if (tmp) *tmp = '\0';
737 num = atoi(xover_str);
738 size_int = atoi(size);
739 line_int = atoi(line);
742 msginfo = g_new0(MsgInfo, 1);
743 msginfo->msgnum = num;
744 msginfo->size = size_int;
746 msginfo->date = g_strdup(date);
747 msginfo->date_t = procheader_date_parse(NULL, date, 0);
749 conv_unmime_header(buf, sizeof(buf), sender, NULL);
750 msginfo->from = g_strdup(buf);
751 msginfo->fromname = procheader_get_fromname(buf);
753 conv_unmime_header(buf, sizeof(buf), subject, NULL);
754 msginfo->subject = g_strdup(buf);
756 extract_parenthesis(msgid, '<', '>');
759 msginfo->msgid = g_strdup(msgid);
761 msginfo->references = g_strdup(ref);
762 eliminate_parenthesis(ref, '(', ')');
763 if ((p = strrchr(ref, '<')) != NULL) {
764 extract_parenthesis(p, '<', '>');
767 msginfo->inreplyto = g_strdup(p);
773 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
779 p = strchr(xhdr_str, ' ');
785 num = atoi(xhdr_str);
786 if (msginfo->msgnum != num) return NULL;
788 tmp = strchr(p, '\r');
789 if (!tmp) tmp = strchr(p, '\n');
792 return g_strndup(p, tmp - p);
797 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
804 g_return_val_if_fail(item != NULL, alist);
805 g_return_val_if_fail(item->folder != NULL, alist);
806 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
808 if (first < 2) return alist;
810 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
812 dir = folder_item_get_path(item);
813 remove_numbered_files(dir, 1, first - 1);
816 for (cur = alist; cur != NULL; ) {
819 msginfo = (MsgInfo *)cur->data;
820 if (msginfo && msginfo->msgnum < first) {
821 procmsg_msginfo_free(msginfo);
822 alist = g_slist_remove(alist, msginfo);
831 static void news_delete_all_articles(FolderItem *item)
835 g_return_if_fail(item != NULL);
836 g_return_if_fail(item->folder != NULL);
837 g_return_if_fail(item->folder->type == F_NEWS);
839 debug_print(_("\tDeleting all cached articles... "));
841 dir = folder_item_get_path(item);
842 remove_all_numbered_files(dir);
845 debug_print(_("done.\n"));