2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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"
50 static gint news_get_article_cmd (NNTPSession *session,
54 static gint news_get_article (NNTPSession *session,
57 static gint news_get_header (NNTPSession *session,
61 static gint news_select_group (NNTPSession *session,
63 static GSList *news_get_uncached_articles(NNTPSession *session,
68 static MsgInfo *news_parse_xover (const gchar *xover_str);
69 static gchar * news_parse_xhdr (const gchar *xover_str,
71 static GSList *news_delete_old_article (GSList *alist,
73 static void news_delete_all_article (FolderItem *item);
76 static Session *news_session_new(const gchar *server, gushort port,
77 const gchar *userid, const gchar *passwd)
79 gchar buf[NNTPBUFSIZE];
81 NNTPSockInfo *nntp_sock;
83 g_return_val_if_fail(server != NULL, NULL);
85 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
88 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
90 nntp_sock = nntp_open(server, port, buf);
91 if (nntp_sock == NULL)
94 session = g_new(NNTPSession, 1);
95 SESSION(session)->type = SESSION_NEWS;
96 SESSION(session)->server = g_strdup(server);
97 session->nntp_sock = nntp_sock;
98 SESSION(session)->sock = nntp_sock->sock;
99 SESSION(session)->connected = TRUE;
100 SESSION(session)->phase = SESSION_READY;
101 SESSION(session)->data = NULL;
102 session->group = NULL;
104 return SESSION(session);
107 void news_session_destroy(NNTPSession *session)
109 nntp_close(session->nntp_sock);
110 session->nntp_sock = NULL;
111 SESSION(session)->sock = NULL;
113 g_free(session->group);
116 static gchar *news_query_password(const gchar *server,
122 message = g_strdup_printf(_("Input password for %s on %s:"),
125 pass = input_dialog_with_invisible(_("Input password"),
128 /* manage_window_focus_in(inc_dialog->mainwin->window, */
133 static Session *news_session_new_for_folder(Folder *folder)
140 ac = folder->account;
141 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
143 if (ac->passwd && ac->passwd[0])
144 passwd = g_strdup(ac->passwd);
146 passwd = news_query_password(ac->nntp_server, userid);
151 userid = passwd = NULL;
153 session = news_session_new(ac->nntp_server, 119, userid, passwd);
158 NNTPSession *news_session_get(Folder *folder)
160 NNTPSession *session;
162 g_return_val_if_fail(folder != NULL, NULL);
163 g_return_val_if_fail(folder->type == F_NEWS, NULL);
164 g_return_val_if_fail(folder->account != NULL, NULL);
166 if (!REMOTE_FOLDER(folder)->session) {
167 REMOTE_FOLDER(folder)->session =
168 news_session_new_for_folder(folder);
170 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
171 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
172 log_warning(_("NNTP connection to %s:%d has been"
173 " disconnected. Reconnecting...\n"),
174 folder->account->nntp_server, 119);
175 session_destroy(REMOTE_FOLDER(folder)->session);
176 REMOTE_FOLDER(folder)->session =
177 news_session_new_for_folder(folder);
181 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
184 GSList *news_get_article_list(Folder *folder, FolderItem *item,
188 NNTPSession *session;
190 g_return_val_if_fail(folder != NULL, NULL);
191 g_return_val_if_fail(item != NULL, NULL);
192 g_return_val_if_fail(folder->type == F_NEWS, NULL);
194 session = news_session_get(folder);
197 alist = procmsg_read_cache(item, FALSE);
198 item->last_num = procmsg_get_last_num_in_cache(alist);
199 } else if (use_cache) {
204 alist = procmsg_read_cache(item, FALSE);
206 cache_last = procmsg_get_last_num_in_cache(alist);
207 newlist = news_get_uncached_articles
208 (session, item, cache_last, &first, &last);
209 alist = news_delete_old_article(alist, first);
211 alist = g_slist_concat(alist, newlist);
212 item->last_num = last;
216 alist = news_get_uncached_articles
217 (session, item, 0, NULL, &last);
218 news_delete_all_article(item);
219 item->last_num = last;
222 procmsg_set_flags(alist, item);
229 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
231 gchar *path, *filename;
234 g_return_val_if_fail(folder != NULL, NULL);
235 g_return_val_if_fail(item != NULL, NULL);
237 path = folder_item_get_path(item);
238 if (!is_dir_exist(path))
241 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
244 if (is_file_exist(filename)) {
245 debug_print(_("article %d has been already cached.\n"), num);
249 if (!REMOTE_FOLDER(folder)->session) {
254 ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
256 if (ok != NN_SUCCESS) {
257 g_warning(_("can't select group %s\n"), item->path);
262 debug_print(_("getting article %d...\n"), num);
263 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
267 g_warning(_("can't read article %d\n"), num);
275 void news_scan_group(Folder *folder, FolderItem *item)
279 gint news_post(Folder *folder, const gchar *file)
281 NNTPSession *session;
285 g_return_val_if_fail(folder != NULL, -1);
286 g_return_val_if_fail(folder->type == F_NEWS, -1);
287 g_return_val_if_fail(file != NULL, -1);
289 session = news_session_get(folder);
290 if (!session) return -1;
292 if ((fp = fopen(file, "r")) == NULL) {
293 FILE_OP_ERROR(file, "fopen");
297 ok = nntp_post(session->nntp_sock, fp);
298 if (ok != NN_SUCCESS) {
299 log_warning(_("can't post article.\n"));
310 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
311 gint num, gchar *filename)
315 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
319 debug_print("Message-Id = %s, num = %d\n", msgid, num);
322 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
323 log_warning(_("can't retrieve article %d\n"), num);
330 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
332 return news_get_article_cmd(session, "ARTICLE", num, filename);
335 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
337 return news_get_article_cmd(session, "HEAD", num, filename);
342 * @session: Active NNTP session.
343 * @group: Newsgroup name.
345 * Select newsgroup @group with the GROUP command if it is not already
346 * selected in @session.
348 * Return value: NNTP result code.
350 static gint news_select_group(NNTPSession *session,
354 gint num, first, last;
356 if (g_strcasecmp(session->group, group) == 0)
359 g_free(session->group);
360 session->group = NULL;
362 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
364 if (ok == NN_SUCCESS)
365 session->group = g_strdup(group);
370 static GSList *news_get_uncached_articles(NNTPSession *session,
371 FolderItem *item, gint cache_last,
372 gint *rfirst, gint *rlast)
375 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
376 gchar buf[NNTPBUFSIZE];
377 GSList *newlist = NULL;
378 GSList *llast = NULL;
381 if (rfirst) *rfirst = 0;
382 if (rlast) *rlast = 0;
384 g_return_val_if_fail(session != NULL, NULL);
385 g_return_val_if_fail(item != NULL, NULL);
386 g_return_val_if_fail(item->folder != NULL, NULL);
387 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
389 g_free(session->group);
390 session->group = NULL;
392 ok = nntp_group(session->nntp_sock, item->path,
393 &num, &first, &last);
394 if (ok != NN_SUCCESS) {
395 log_warning(_("can't set group: %s\n"), item->path);
399 session->group = g_strdup(item->path);
401 /* calculate getting overview range */
403 log_warning(_("invalid article range: %d - %d\n"),
407 if (cache_last < first)
409 else if (last < cache_last)
411 else if (last == cache_last) {
412 debug_print(_("no new articles.\n"));
415 begin = cache_last + 1;
418 if (prefs_common.max_articles > 0 &&
419 end - begin + 1 > prefs_common.max_articles)
420 begin = end - prefs_common.max_articles + 1;
422 log_message(_("getting xover %d - %d in %s...\n"),
423 begin, end, item->path);
424 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
425 log_warning(_("can't get xover\n"));
430 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
431 log_warning(_("error occurred while getting xover.\n"));
435 if (buf[0] == '.' && buf[1] == '\r') break;
437 msginfo = news_parse_xover(buf);
439 log_warning(_("invalid xover line: %s\n"), buf);
443 msginfo->folder = item;
444 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
445 msginfo->newsgroups = g_strdup(item->path);
448 llast = newlist = g_slist_append(newlist, msginfo);
450 llast = g_slist_append(llast, msginfo);
455 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
456 log_warning(_("can't get xhdr\n"));
465 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
466 log_warning(_("error occurred while getting xover.\n"));
470 if (buf[0] == '.' && buf[1] == '\r') break;
472 msginfo = (MsgInfo *) llast->data;
474 value = news_parse_xhdr(buf, msginfo);
483 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
484 log_warning(_("can't get xhdr\n"));
493 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
494 log_warning(_("error occurred while getting xover.\n"));
498 if (buf[0] == '.' && buf[1] == '\r') break;
500 msginfo = (MsgInfo *) llast->data;
502 value = news_parse_xhdr(buf, msginfo);
510 if (rfirst) *rfirst = first;
511 if (rlast) *rlast = last;
515 #define PARSE_ONE_PARAM(p, srcp) \
517 p = strchr(srcp, '\t'); \
518 if (!p) return NULL; \
523 static MsgInfo *news_parse_xover(const gchar *xover_str)
526 gchar buf[NNTPBUFSIZE];
527 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
529 gint num, size_int, line_int;
532 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
533 strcpy(xover_buf, xover_str);
535 PARSE_ONE_PARAM(subject, xover_buf);
536 PARSE_ONE_PARAM(sender, subject);
537 PARSE_ONE_PARAM(date, sender);
538 PARSE_ONE_PARAM(msgid, date);
539 PARSE_ONE_PARAM(ref, msgid);
540 PARSE_ONE_PARAM(size, ref);
541 PARSE_ONE_PARAM(line, size);
543 tmp = strchr(line, '\t');
544 if (!tmp) tmp = strchr(line, '\r');
545 if (!tmp) tmp = strchr(line, '\n');
546 if (tmp) *tmp = '\0';
548 num = atoi(xover_str);
549 size_int = atoi(size);
550 line_int = atoi(line);
553 msginfo = g_new0(MsgInfo, 1);
554 msginfo->msgnum = num;
555 msginfo->size = size_int;
557 msginfo->date = g_strdup(date);
558 msginfo->date_t = procheader_date_parse(NULL, date, 0);
560 conv_unmime_header(buf, sizeof(buf), sender, NULL);
561 msginfo->from = g_strdup(buf);
562 msginfo->fromname = procheader_get_fromname(buf);
564 conv_unmime_header(buf, sizeof(buf), subject, NULL);
565 msginfo->subject = g_strdup(buf);
567 extract_parenthesis(msgid, '<', '>');
570 msginfo->msgid = g_strdup(msgid);
572 msginfo->references = g_strdup(ref);
573 eliminate_parenthesis(ref, '(', ')');
574 if ((p = strrchr(ref, '<')) != NULL) {
575 extract_parenthesis(p, '<', '>');
578 msginfo->inreplyto = g_strdup(p);
584 static gchar * news_parse_xhdr(const gchar *xover_str, MsgInfo * info)
586 gchar buf[NNTPBUFSIZE];
591 p = strchr(xover_str, ' ');
597 num = atoi(xover_str);
599 if (info->msgnum != num)
602 tmp = strchr(p, '\r');
603 if (!tmp) tmp = strchr(p, '\n');
606 return g_strndup(p, tmp - p);
611 static GSList *news_delete_old_article(GSList *alist, gint first)
617 if (first < 2) return alist;
619 for (cur = alist; cur != NULL; ) {
622 msginfo = (MsgInfo *)cur->data;
623 if (msginfo && msginfo->msgnum < first) {
624 debug_print(_("deleting article %d...\n"),
627 cache_file = procmsg_get_message_file_path(msginfo);
628 if (is_file_exist(cache_file)) unlink(cache_file);
631 procmsg_msginfo_free(msginfo);
632 alist = g_slist_remove(alist, msginfo);
641 static void news_delete_all_article(FolderItem *item)
648 dir = folder_item_get_path(item);
649 if (!is_dir_exist(dir))
652 if ((dp = opendir(dir)) == NULL) {
653 FILE_OP_ERROR(dir, "opendir");
658 debug_print(_("\tDeleting all cached articles... "));
660 while ((d = readdir(dp)) != NULL) {
661 if (to_number(d->d_name) < 0) continue;
663 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
665 if (is_file_exist(file)) {
666 if (unlink(file) < 0)
667 FILE_OP_ERROR(file, "unlink");
676 debug_print(_("done.\n"));
680 news_get_group_list returns a strings list.
681 These strings are the names of the newsgroups of a server.
682 item is the FolderItem of the news server.
683 The names of the newsgroups are cached into a file so that
684 when the function is called again, there is no need to make
685 a request to the server.
688 GSList * news_get_group_list(FolderItem *item)
690 gchar *path, *filename;
692 NNTPSession *session;
693 GSList * group_list = NULL;
695 gchar buf[NNTPBUFSIZE];
701 path = folder_item_get_path(item);
703 if (!is_dir_exist(path))
706 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
709 session = news_session_get(item->folder);
714 if (is_file_exist(filename)) {
715 debug_print(_("group list has been already cached.\n"));
718 ok = nntp_list(session->nntp_sock);
719 if (ok != NN_SUCCESS)
722 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
723 log_warning(_("can't retrieve group list\n"));
728 f = fopen(filename, "r");
729 while (fgets(buf, NNTPBUFSIZE, f)) {
733 while ((buf[len] != 0) && (buf[len] != ' '))
738 group_list = g_slist_append(group_list, s);
743 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
749 remove the cache file of the names of the newsgroups.
752 void news_reset_group_list(FolderItem *item)
754 gchar *path, *filename;
756 debug_print(_("\tDeleting cached group list... "));
757 path = folder_item_get_path(item);
758 if (!is_dir_exist(path))
761 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
763 if (remove(filename) != 0)
764 log_warning(_("can't delete cached group list %s\n"), filename);