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 rfolder->session->last_access_time = time(NULL);
199 return NNTP_SESSION(rfolder->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 NewsGroupInfo *news_group_info_new(const gchar *name,
299 gint first, gint last, gchar type)
301 NewsGroupInfo *ginfo;
303 ginfo = g_new(NewsGroupInfo, 1);
304 ginfo->name = g_strdup(name);
305 ginfo->first = first;
312 static void news_group_info_free(NewsGroupInfo *ginfo)
318 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
319 NewsGroupInfo *ginfo2)
321 return g_strcasecmp(ginfo1->name, ginfo2->name);
324 GSList *news_get_group_list(Folder *folder)
326 gchar *path, *filename;
330 gchar buf[NNTPBUFSIZE];
332 g_return_val_if_fail(folder != NULL, NULL);
333 g_return_val_if_fail(folder->type == F_NEWS, NULL);
335 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
336 if (!is_dir_exist(path))
338 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
341 if ((fp = fopen(filename, "r")) == NULL) {
342 NNTPSession *session;
344 session = news_session_get(folder);
350 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
356 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
357 log_warning(_("can't retrieve newsgroup list\n"));
362 if ((fp = fopen(filename, "r")) == NULL) {
363 FILE_OP_ERROR(filename, "fopen");
369 while (fgets(buf, sizeof(buf), fp) != NULL) {
375 NewsGroupInfo *ginfo;
383 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
386 ginfo = news_group_info_new(name, first_num, last_num, type);
389 last = list = g_slist_append(NULL, ginfo);
391 last = g_slist_append(last, ginfo);
399 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
406 void news_group_list_free(GSList *group_list)
410 if (!group_list) return;
412 for (cur = group_list; cur != NULL; cur = cur->next)
413 news_group_info_free((NewsGroupInfo *)cur->data);
414 g_slist_free(group_list);
417 void news_remove_group_list_cache(Folder *folder)
419 gchar *path, *filename;
421 g_return_if_fail(folder != NULL);
422 g_return_if_fail(folder->type == F_NEWS);
424 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
425 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
428 if (is_file_exist(filename)) {
429 if (remove(filename) < 0)
430 FILE_OP_ERROR(filename, "remove");
435 gint news_post(Folder *folder, const gchar *file)
437 NNTPSession *session;
441 g_return_val_if_fail(folder != NULL, -1);
442 g_return_val_if_fail(folder->type == F_NEWS, -1);
443 g_return_val_if_fail(file != NULL, -1);
445 session = news_session_get(folder);
446 if (!session) return -1;
448 if ((fp = fopen(file, "r")) == NULL) {
449 FILE_OP_ERROR(file, "fopen");
453 ok = nntp_post(session->nntp_sock, fp);
454 if (ok != NN_SUCCESS) {
455 log_warning(_("can't post article.\n"));
466 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
467 gint num, gchar *filename)
471 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
475 debug_print("Message-Id = %s, num = %d\n", msgid, num);
478 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
479 log_warning(_("can't retrieve article %d\n"), num);
486 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
488 return news_get_article_cmd(session, "ARTICLE", num, filename);
491 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
493 return news_get_article_cmd(session, "HEAD", num, filename);
498 * @session: Active NNTP session.
499 * @group: Newsgroup name.
501 * Select newsgroup @group with the GROUP command if it is not already
502 * selected in @session.
504 * Return value: NNTP result code.
506 static gint news_select_group(NNTPSession *session, const gchar *group)
509 gint num, first, last;
511 if (session->group && g_strcasecmp(session->group, group) == 0)
514 g_free(session->group);
515 session->group = NULL;
517 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
518 if (ok == NN_SUCCESS)
519 session->group = g_strdup(group);
524 static GSList *news_get_uncached_articles(NNTPSession *session,
525 FolderItem *item, gint cache_last,
526 gint *rfirst, gint *rlast)
529 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
530 gchar buf[NNTPBUFSIZE];
531 GSList *newlist = NULL;
532 GSList *llast = NULL;
535 if (rfirst) *rfirst = 0;
536 if (rlast) *rlast = 0;
538 g_return_val_if_fail(session != NULL, NULL);
539 g_return_val_if_fail(item != NULL, NULL);
540 g_return_val_if_fail(item->folder != NULL, NULL);
541 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
543 g_free(session->group);
544 session->group = NULL;
546 ok = nntp_group(session->nntp_sock, item->path,
547 &num, &first, &last);
548 if (ok != NN_SUCCESS) {
549 log_warning(_("can't set group: %s\n"), item->path);
552 session->group = g_strdup(item->path);
554 /* calculate getting overview range */
556 log_warning(_("invalid article range: %d - %d\n"),
560 if (cache_last < first)
562 else if (last < cache_last)
564 else if (last == cache_last) {
565 debug_print(_("no new articles.\n"));
568 begin = cache_last + 1;
571 if (rfirst) *rfirst = first;
572 if (rlast) *rlast = last;
574 if (prefs_common.max_articles > 0 &&
575 end - begin + 1 > prefs_common.max_articles)
576 begin = end - prefs_common.max_articles + 1;
578 log_message(_("getting xover %d - %d in %s...\n"),
579 begin, end, item->path);
580 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
581 log_warning(_("can't get xover\n"));
586 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
587 log_warning(_("error occurred while getting xover.\n"));
591 if (buf[0] == '.' && buf[1] == '\r') break;
593 msginfo = news_parse_xover(buf);
595 log_warning(_("invalid xover line: %s\n"), buf);
599 msginfo->folder = item;
600 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
601 msginfo->flags.tmp_flags = MSG_NEWS;
602 msginfo->newsgroups = g_strdup(item->path);
605 llast = newlist = g_slist_append(newlist, msginfo);
607 llast = g_slist_append(llast, msginfo);
612 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
613 log_warning(_("can't get xhdr\n"));
620 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
621 log_warning(_("error occurred while getting xhdr.\n"));
625 if (buf[0] == '.' && buf[1] == '\r') break;
627 g_warning("llast == NULL\n");
631 msginfo = (MsgInfo *)llast->data;
632 msginfo->to = news_parse_xhdr(buf, msginfo);
637 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
638 log_warning(_("can't get xhdr\n"));
645 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
646 log_warning(_("error occurred while getting xhdr.\n"));
650 if (buf[0] == '.' && buf[1] == '\r') break;
652 g_warning("llast == NULL\n");
656 msginfo = (MsgInfo *)llast->data;
657 msginfo->cc = news_parse_xhdr(buf, msginfo);
665 #define PARSE_ONE_PARAM(p, srcp) \
667 p = strchr(srcp, '\t'); \
668 if (!p) return NULL; \
673 static MsgInfo *news_parse_xover(const gchar *xover_str)
676 gchar buf[NNTPBUFSIZE];
677 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
679 gint num, size_int, line_int;
682 Xstrdup_a(xover_buf, xover_str, return NULL);
684 PARSE_ONE_PARAM(subject, xover_buf);
685 PARSE_ONE_PARAM(sender, subject);
686 PARSE_ONE_PARAM(date, sender);
687 PARSE_ONE_PARAM(msgid, date);
688 PARSE_ONE_PARAM(ref, msgid);
689 PARSE_ONE_PARAM(size, ref);
690 PARSE_ONE_PARAM(line, size);
692 tmp = strchr(line, '\t');
693 if (!tmp) tmp = strchr(line, '\r');
694 if (!tmp) tmp = strchr(line, '\n');
695 if (tmp) *tmp = '\0';
697 num = atoi(xover_str);
698 size_int = atoi(size);
699 line_int = atoi(line);
702 msginfo = g_new0(MsgInfo, 1);
703 msginfo->msgnum = num;
704 msginfo->size = size_int;
706 msginfo->date = g_strdup(date);
707 msginfo->date_t = procheader_date_parse(NULL, date, 0);
709 conv_unmime_header(buf, sizeof(buf), sender, NULL);
710 msginfo->from = g_strdup(buf);
711 msginfo->fromname = procheader_get_fromname(buf);
713 conv_unmime_header(buf, sizeof(buf), subject, NULL);
714 msginfo->subject = g_strdup(buf);
716 extract_parenthesis(msgid, '<', '>');
719 msginfo->msgid = g_strdup(msgid);
721 msginfo->references = g_strdup(ref);
722 eliminate_parenthesis(ref, '(', ')');
723 if ((p = strrchr(ref, '<')) != NULL) {
724 extract_parenthesis(p, '<', '>');
727 msginfo->inreplyto = g_strdup(p);
733 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
739 p = strchr(xhdr_str, ' ');
745 num = atoi(xhdr_str);
746 if (msginfo->msgnum != num) return NULL;
748 tmp = strchr(p, '\r');
749 if (!tmp) tmp = strchr(p, '\n');
752 return g_strndup(p, tmp - p);
757 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
764 g_return_val_if_fail(item != NULL, alist);
765 g_return_val_if_fail(item->folder != NULL, alist);
766 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
768 if (first < 2) return alist;
770 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
772 dir = folder_item_get_path(item);
773 remove_numbered_files(dir, 1, first - 1);
776 for (cur = alist; cur != NULL; ) {
779 msginfo = (MsgInfo *)cur->data;
780 if (msginfo && msginfo->msgnum < first) {
781 procmsg_msginfo_free(msginfo);
782 alist = g_slist_remove(alist, msginfo);
791 static void news_delete_all_articles(FolderItem *item)
795 g_return_if_fail(item != NULL);
796 g_return_if_fail(item->folder != NULL);
797 g_return_if_fail(item->folder->type == F_NEWS);
799 debug_print(_("\tDeleting all cached articles... "));
801 dir = folder_item_get_path(item);
802 remove_all_numbered_files(dir);
805 debug_print(_("done.\n"));