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);
85 static Session *news_session_new(const gchar *server, gushort port,
86 const gchar *userid, const gchar *passwd)
88 gchar buf[NNTPBUFSIZE];
90 NNTPSockInfo *nntp_sock;
92 g_return_val_if_fail(server != NULL, NULL);
94 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
97 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
99 nntp_sock = nntp_open(server, port, buf);
100 if (nntp_sock == NULL)
103 session = g_new(NNTPSession, 1);
104 SESSION(session)->type = SESSION_NEWS;
105 SESSION(session)->server = g_strdup(server);
106 session->nntp_sock = nntp_sock;
107 SESSION(session)->sock = nntp_sock->sock;
108 SESSION(session)->connected = TRUE;
109 SESSION(session)->phase = SESSION_READY;
110 SESSION(session)->last_access_time = time(NULL);
111 SESSION(session)->data = NULL;
112 session->group = NULL;
114 return SESSION(session);
117 void news_session_destroy(NNTPSession *session)
119 nntp_close(session->nntp_sock);
120 session->nntp_sock = NULL;
121 SESSION(session)->sock = NULL;
123 g_free(session->group);
126 static gchar *news_query_password(const gchar *server, const gchar *user)
131 message = g_strdup_printf(_("Input password for %s on %s:"),
133 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
139 static Session *news_session_new_for_folder(Folder *folder)
143 const gchar *userid = NULL;
144 gchar *passwd = NULL;
146 g_return_val_if_fail(folder != NULL, NULL);
147 g_return_val_if_fail(folder->account != NULL, NULL);
149 ac = folder->account;
150 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
152 if (ac->passwd && ac->passwd[0])
153 passwd = g_strdup(ac->passwd);
155 passwd = news_query_password(ac->nntp_server, userid);
158 session = news_session_new(ac->nntp_server,
159 ac->set_nntpport ? ac->nntpport : NNTP_PORT,
166 NNTPSession *news_session_get(Folder *folder)
168 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
170 g_return_val_if_fail(folder != NULL, NULL);
171 g_return_val_if_fail(folder->type == F_NEWS, NULL);
172 g_return_val_if_fail(folder->account != NULL, NULL);
174 if (!rfolder->session) {
175 rfolder->session = news_session_new_for_folder(folder);
177 return NNTP_SESSION(rfolder->session);
180 if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
181 rfolder->session->last_access_time = time(NULL);
183 return NNTP_SESSION(rfolder->session);
186 if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
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(rfolder->session);
194 rfolder->session = news_session_new_for_folder(folder);
197 if (rfolder->session)
198 rfolder->session->last_access_time = time(NULL);
200 return NNTP_SESSION(rfolder->session);
203 GSList *news_get_article_list(Folder *folder, FolderItem *item,
207 NNTPSession *session;
209 g_return_val_if_fail(folder != NULL, NULL);
210 g_return_val_if_fail(item != NULL, NULL);
211 g_return_val_if_fail(folder->type == F_NEWS, NULL);
213 session = news_session_get(folder);
216 alist = procmsg_read_cache(item, FALSE);
217 item->last_num = procmsg_get_last_num_in_cache(alist);
218 } else if (use_cache) {
223 alist = procmsg_read_cache(item, FALSE);
225 cache_last = procmsg_get_last_num_in_cache(alist);
226 newlist = news_get_uncached_articles
227 (session, item, cache_last, &first, &last);
228 alist = news_delete_old_articles(alist, item, first);
230 alist = g_slist_concat(alist, newlist);
231 item->last_num = last;
235 alist = news_get_uncached_articles
236 (session, item, 0, NULL, &last);
237 news_delete_all_articles(item);
238 item->last_num = last;
241 procmsg_set_flags(alist, item);
248 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
250 gchar *path, *filename;
251 NNTPSession *session;
254 g_return_val_if_fail(folder != NULL, NULL);
255 g_return_val_if_fail(item != NULL, NULL);
257 path = folder_item_get_path(item);
258 if (!is_dir_exist(path))
260 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
263 if (is_file_exist(filename)) {
264 debug_print(_("article %d has been already cached.\n"), num);
268 session = news_session_get(folder);
274 ok = news_select_group(session, item->path);
276 if (ok != NN_SUCCESS) {
277 g_warning(_("can't select group %s\n"), item->path);
282 debug_print(_("getting article %d...\n"), num);
283 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
287 g_warning(_("can't read article %d\n"), num);
295 void news_scan_group(Folder *folder, FolderItem *item)
299 static NewsGroupInfo *news_group_info_new(const gchar *name,
300 gint first, gint last, gchar type)
302 NewsGroupInfo *ginfo;
304 ginfo = g_new(NewsGroupInfo, 1);
305 ginfo->name = g_strdup(name);
306 ginfo->first = first;
313 static void news_group_info_free(NewsGroupInfo *ginfo)
319 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
320 NewsGroupInfo *ginfo2)
322 return g_strcasecmp(ginfo1->name, ginfo2->name);
325 GSList *news_get_group_list(Folder *folder)
327 gchar *path, *filename;
331 gchar buf[NNTPBUFSIZE];
333 g_return_val_if_fail(folder != NULL, NULL);
334 g_return_val_if_fail(folder->type == F_NEWS, NULL);
336 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
337 if (!is_dir_exist(path))
339 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
342 if ((fp = fopen(filename, "r")) == NULL) {
343 NNTPSession *session;
345 session = news_session_get(folder);
351 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
357 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
358 log_warning(_("can't retrieve newsgroup list\n"));
363 if ((fp = fopen(filename, "r")) == NULL) {
364 FILE_OP_ERROR(filename, "fopen");
370 while (fgets(buf, sizeof(buf), fp) != NULL) {
376 NewsGroupInfo *ginfo;
384 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
387 ginfo = news_group_info_new(name, first_num, last_num, type);
390 last = list = g_slist_append(NULL, ginfo);
392 last = g_slist_append(last, ginfo);
400 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
407 void news_group_list_free(GSList *group_list)
411 if (!group_list) return;
413 for (cur = group_list; cur != NULL; cur = cur->next)
414 news_group_info_free((NewsGroupInfo *)cur->data);
415 g_slist_free(group_list);
418 void news_remove_group_list_cache(Folder *folder)
420 gchar *path, *filename;
422 g_return_if_fail(folder != NULL);
423 g_return_if_fail(folder->type == F_NEWS);
425 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
426 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
429 if (is_file_exist(filename)) {
430 if (remove(filename) < 0)
431 FILE_OP_ERROR(filename, "remove");
436 gint news_post(Folder *folder, const gchar *file)
438 NNTPSession *session;
442 g_return_val_if_fail(folder != NULL, -1);
443 g_return_val_if_fail(folder->type == F_NEWS, -1);
444 g_return_val_if_fail(file != NULL, -1);
446 session = news_session_get(folder);
447 if (!session) return -1;
449 if ((fp = fopen(file, "r")) == NULL) {
450 FILE_OP_ERROR(file, "fopen");
454 ok = nntp_post(session->nntp_sock, fp);
455 if (ok != NN_SUCCESS) {
456 log_warning(_("can't post article.\n"));
467 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
468 gint num, gchar *filename)
472 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
476 debug_print("Message-Id = %s, num = %d\n", msgid, num);
479 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
480 log_warning(_("can't retrieve article %d\n"), num);
487 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
489 return news_get_article_cmd(session, "ARTICLE", num, filename);
492 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
494 return news_get_article_cmd(session, "HEAD", num, filename);
499 * @session: Active NNTP session.
500 * @group: Newsgroup name.
502 * Select newsgroup @group with the GROUP command if it is not already
503 * selected in @session.
505 * Return value: NNTP result code.
507 static gint news_select_group(NNTPSession *session, const gchar *group)
510 gint num, first, last;
512 if (session->group && g_strcasecmp(session->group, group) == 0)
515 g_free(session->group);
516 session->group = NULL;
518 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
519 if (ok == NN_SUCCESS)
520 session->group = g_strdup(group);
525 static GSList *news_get_uncached_articles(NNTPSession *session,
526 FolderItem *item, gint cache_last,
527 gint *rfirst, gint *rlast)
530 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
531 gchar buf[NNTPBUFSIZE];
532 GSList *newlist = NULL;
533 GSList *llast = NULL;
536 if (rfirst) *rfirst = 0;
537 if (rlast) *rlast = 0;
539 g_return_val_if_fail(session != NULL, NULL);
540 g_return_val_if_fail(item != NULL, NULL);
541 g_return_val_if_fail(item->folder != NULL, NULL);
542 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
544 g_free(session->group);
545 session->group = NULL;
547 ok = nntp_group(session->nntp_sock, item->path,
548 &num, &first, &last);
549 if (ok != NN_SUCCESS) {
550 log_warning(_("can't set group: %s\n"), item->path);
553 session->group = g_strdup(item->path);
555 /* calculate getting overview range */
557 log_warning(_("invalid article range: %d - %d\n"),
561 if (cache_last < first)
563 else if (last < cache_last)
565 else if (last == cache_last) {
566 debug_print(_("no new articles.\n"));
569 begin = cache_last + 1;
572 if (rfirst) *rfirst = first;
573 if (rlast) *rlast = last;
575 if (prefs_common.max_articles > 0 &&
576 end - begin + 1 > prefs_common.max_articles)
577 begin = end - prefs_common.max_articles + 1;
579 log_message(_("getting xover %d - %d in %s...\n"),
580 begin, end, item->path);
581 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
582 log_warning(_("can't get xover\n"));
587 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
588 log_warning(_("error occurred while getting xover.\n"));
592 if (buf[0] == '.' && buf[1] == '\r') break;
594 msginfo = news_parse_xover(buf);
596 log_warning(_("invalid xover line: %s\n"), buf);
600 msginfo->folder = item;
601 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
602 msginfo->flags.tmp_flags = MSG_NEWS;
603 msginfo->newsgroups = g_strdup(item->path);
606 llast = newlist = g_slist_append(newlist, msginfo);
608 llast = g_slist_append(llast, msginfo);
613 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
614 log_warning(_("can't get xhdr\n"));
621 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
622 log_warning(_("error occurred while getting xhdr.\n"));
626 if (buf[0] == '.' && buf[1] == '\r') break;
628 g_warning("llast == NULL\n");
632 msginfo = (MsgInfo *)llast->data;
633 msginfo->to = news_parse_xhdr(buf, msginfo);
638 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
639 log_warning(_("can't get xhdr\n"));
646 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
647 log_warning(_("error occurred while getting xhdr.\n"));
651 if (buf[0] == '.' && buf[1] == '\r') break;
653 g_warning("llast == NULL\n");
657 msginfo = (MsgInfo *)llast->data;
658 msginfo->cc = news_parse_xhdr(buf, msginfo);
666 #define PARSE_ONE_PARAM(p, srcp) \
668 p = strchr(srcp, '\t'); \
669 if (!p) return NULL; \
674 static MsgInfo *news_parse_xover(const gchar *xover_str)
677 gchar buf[NNTPBUFSIZE];
678 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
680 gint num, size_int, line_int;
683 Xstrdup_a(xover_buf, xover_str, return NULL);
685 PARSE_ONE_PARAM(subject, xover_buf);
686 PARSE_ONE_PARAM(sender, subject);
687 PARSE_ONE_PARAM(date, sender);
688 PARSE_ONE_PARAM(msgid, date);
689 PARSE_ONE_PARAM(ref, msgid);
690 PARSE_ONE_PARAM(size, ref);
691 PARSE_ONE_PARAM(line, size);
693 tmp = strchr(line, '\t');
694 if (!tmp) tmp = strchr(line, '\r');
695 if (!tmp) tmp = strchr(line, '\n');
696 if (tmp) *tmp = '\0';
698 num = atoi(xover_str);
699 size_int = atoi(size);
700 line_int = atoi(line);
703 msginfo = g_new0(MsgInfo, 1);
704 msginfo->msgnum = num;
705 msginfo->size = size_int;
707 msginfo->date = g_strdup(date);
708 msginfo->date_t = procheader_date_parse(NULL, date, 0);
710 conv_unmime_header(buf, sizeof(buf), sender, NULL);
711 msginfo->from = g_strdup(buf);
712 msginfo->fromname = procheader_get_fromname(buf);
714 conv_unmime_header(buf, sizeof(buf), subject, NULL);
715 msginfo->subject = g_strdup(buf);
717 extract_parenthesis(msgid, '<', '>');
720 msginfo->msgid = g_strdup(msgid);
722 msginfo->references = g_strdup(ref);
723 eliminate_parenthesis(ref, '(', ')');
724 if ((p = strrchr(ref, '<')) != NULL) {
725 extract_parenthesis(p, '<', '>');
728 msginfo->inreplyto = g_strdup(p);
734 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
740 p = strchr(xhdr_str, ' ');
746 num = atoi(xhdr_str);
747 if (msginfo->msgnum != num) return NULL;
749 tmp = strchr(p, '\r');
750 if (!tmp) tmp = strchr(p, '\n');
753 return g_strndup(p, tmp - p);
758 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
765 g_return_val_if_fail(item != NULL, alist);
766 g_return_val_if_fail(item->folder != NULL, alist);
767 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
769 if (first < 2) return alist;
771 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
773 dir = folder_item_get_path(item);
774 remove_numbered_files(dir, 1, first - 1);
777 for (cur = alist; cur != NULL; ) {
780 msginfo = (MsgInfo *)cur->data;
781 if (msginfo && msginfo->msgnum < first) {
782 procmsg_msginfo_free(msginfo);
783 alist = g_slist_remove(alist, msginfo);
792 static void news_delete_all_articles(FolderItem *item)
796 g_return_if_fail(item != NULL);
797 g_return_if_fail(item->folder != NULL);
798 g_return_if_fail(item->folder->type == F_NEWS);
800 debug_print(_("\tDeleting all cached articles... "));
802 dir = folder_item_get_path(item);
803 remove_all_numbered_files(dir);
806 debug_print(_("done.\n"));