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.
38 #include "procheader.h"
41 #include "statusbar.h"
44 #include "prefs_common.h"
45 #include "prefs_account.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
49 static gint news_get_article_cmd (NNTPSession *session,
53 static gint news_get_article (NNTPSession *session,
56 static gint news_get_header (NNTPSession *session,
60 static gint news_select_group (NNTPSession *session,
62 static GSList *news_get_uncached_articles(NNTPSession *session,
67 static MsgInfo *news_parse_xover (const gchar *xover_str);
68 static gchar * news_parse_xhdr (const gchar *xover_str,
70 static GSList *news_delete_old_article (GSList *alist,
72 static void news_delete_all_article (FolderItem *item);
75 static Session *news_session_new(const gchar *server, gushort port,
76 const gchar *userid, const gchar *passwd)
78 gchar buf[NNTPBUFSIZE];
80 NNTPSockInfo *nntp_sock;
82 g_return_val_if_fail(server != NULL, NULL);
84 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
87 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
89 nntp_sock = nntp_open(server, port, buf);
90 if (nntp_sock == NULL)
93 session = g_new(NNTPSession, 1);
94 SESSION(session)->type = SESSION_NEWS;
95 SESSION(session)->server = g_strdup(server);
96 session->nntp_sock = nntp_sock;
97 SESSION(session)->sock = nntp_sock->sock;
98 SESSION(session)->connected = TRUE;
99 SESSION(session)->phase = SESSION_READY;
100 SESSION(session)->data = NULL;
101 session->group = NULL;
103 return SESSION(session);
106 void news_session_destroy(NNTPSession *session)
108 nntp_close(session->nntp_sock);
109 session->nntp_sock = NULL;
110 SESSION(session)->sock = NULL;
112 g_free(session->group);
115 static gchar *news_query_password(const gchar *server,
121 message = g_strdup_printf(_("Input password for %s on %s:"),
124 pass = input_dialog_with_invisible(_("Input password"),
127 /* manage_window_focus_in(inc_dialog->mainwin->window, */
132 static Session *news_session_new_for_folder(Folder *folder)
139 ac = folder->account;
140 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
142 if (ac->passwd && ac->passwd[0])
143 passwd = g_strdup(ac->passwd);
145 passwd = news_query_password(ac->nntp_server, userid);
150 userid = passwd = NULL;
152 session = news_session_new(ac->nntp_server, 119, userid, passwd);
157 NNTPSession *news_session_get(Folder *folder)
159 NNTPSession *session;
161 g_return_val_if_fail(folder != NULL, NULL);
162 g_return_val_if_fail(folder->type == F_NEWS, NULL);
163 g_return_val_if_fail(folder->account != NULL, NULL);
165 if (!REMOTE_FOLDER(folder)->session) {
166 REMOTE_FOLDER(folder)->session =
167 news_session_new_for_folder(folder);
169 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
170 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
171 log_warning(_("NNTP connection to %s:%d has been"
172 " disconnected. Reconnecting...\n"),
173 folder->account->nntp_server, 119);
174 session_destroy(REMOTE_FOLDER(folder)->session);
175 REMOTE_FOLDER(folder)->session =
176 news_session_new_for_folder(folder);
180 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
183 GSList *news_get_article_list(Folder *folder, FolderItem *item,
187 NNTPSession *session;
189 g_return_val_if_fail(folder != NULL, NULL);
190 g_return_val_if_fail(item != NULL, NULL);
191 g_return_val_if_fail(folder->type == F_NEWS, NULL);
193 session = news_session_get(folder);
196 alist = procmsg_read_cache(item, FALSE);
197 item->last_num = procmsg_get_last_num_in_cache(alist);
198 } else if (use_cache) {
203 alist = procmsg_read_cache(item, FALSE);
205 cache_last = procmsg_get_last_num_in_cache(alist);
206 newlist = news_get_uncached_articles
207 (session, item, cache_last, &first, &last);
208 alist = news_delete_old_article(alist, first);
210 alist = g_slist_concat(alist, newlist);
211 item->last_num = last;
215 alist = news_get_uncached_articles
216 (session, item, 0, NULL, &last);
217 news_delete_all_article(item);
218 item->last_num = last;
221 procmsg_set_flags(alist, item);
228 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
230 gchar *path, *filename;
233 g_return_val_if_fail(folder != NULL, NULL);
234 g_return_val_if_fail(item != NULL, NULL);
236 path = folder_item_get_path(item);
237 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
240 if (is_file_exist(filename)) {
241 debug_print(_("article %d has been already cached.\n"), num);
245 if (!REMOTE_FOLDER(folder)->session) {
250 ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
252 if (ok != NN_SUCCESS) {
253 g_warning(_("can't select group %s\n"), item->path);
258 debug_print(_("getting article %d...\n"), num);
259 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
263 g_warning(_("can't read article %d\n"), num);
271 void news_scan_group(Folder *folder, FolderItem *item)
275 gint news_post(Folder *folder, const gchar *file)
277 NNTPSession *session;
281 g_return_val_if_fail(folder != NULL, -1);
282 g_return_val_if_fail(folder->type == F_NEWS, -1);
283 g_return_val_if_fail(file != NULL, -1);
285 session = news_session_get(folder);
286 if (!session) return -1;
288 if ((fp = fopen(file, "r")) == NULL) {
289 FILE_OP_ERROR(file, "fopen");
293 ok = nntp_post(session->nntp_sock, fp);
294 if (ok != NN_SUCCESS) {
295 log_warning(_("can't post article.\n"));
306 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
307 gint num, gchar *filename)
311 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
315 debug_print("Message-Id = %s, num = %d\n", msgid, num);
318 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
319 log_warning(_("can't retrieve article %d\n"), num);
326 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
328 return news_get_article_cmd(session, "ARTICLE", num, filename);
331 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
333 return news_get_article_cmd(session, "HEAD", num, filename);
338 * @session: Active NNTP session.
339 * @group: Newsgroup name.
341 * Select newsgroup @group with the GROUP command if it is not already
342 * selected in @session.
344 * Return value: NNTP result code.
346 static gint news_select_group(NNTPSession *session,
350 gint num, first, last;
352 if (g_strcasecmp(session->group, group) == 0)
355 g_free(session->group);
356 session->group = NULL;
358 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
360 if (ok == NN_SUCCESS)
361 session->group = g_strdup(group);
366 static GSList *news_get_uncached_articles(NNTPSession *session,
367 FolderItem *item, gint cache_last,
368 gint *rfirst, gint *rlast)
371 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
372 gchar buf[NNTPBUFSIZE];
373 GSList *newlist = NULL;
374 GSList *llast = NULL;
377 if (rfirst) *rfirst = 0;
378 if (rlast) *rlast = 0;
380 g_return_val_if_fail(session != NULL, NULL);
381 g_return_val_if_fail(item != NULL, NULL);
382 g_return_val_if_fail(item->folder != NULL, NULL);
383 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
385 g_free(session->group);
386 session->group = NULL;
388 ok = nntp_group(session->nntp_sock, item->path,
389 &num, &first, &last);
390 if (ok != NN_SUCCESS) {
391 log_warning(_("can't set group: %s\n"), item->path);
395 session->group = g_strdup(item->path);
397 /* calculate getting overview range */
399 log_warning(_("invalid article range: %d - %d\n"),
403 if (cache_last < first)
405 else if (last < cache_last)
407 else if (last == cache_last) {
408 debug_print(_("no new articles.\n"));
411 begin = cache_last + 1;
414 if (prefs_common.max_articles > 0 &&
415 end - begin + 1 > prefs_common.max_articles)
416 begin = end - prefs_common.max_articles + 1;
418 log_message(_("getting xover %d - %d in %s...\n"),
419 begin, end, item->path);
420 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
421 log_warning(_("can't get xover\n"));
426 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
427 log_warning(_("error occurred while getting xover.\n"));
431 if (buf[0] == '.' && buf[1] == '\r') break;
433 msginfo = news_parse_xover(buf);
435 log_warning(_("invalid xover line: %s\n"), buf);
439 msginfo->folder = item;
440 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
443 llast = newlist = g_slist_append(newlist, msginfo);
445 llast = g_slist_append(llast, msginfo);
450 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
451 log_warning(_("can't get xhdr\n"));
460 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
461 log_warning(_("error occurred while getting xover.\n"));
465 if (buf[0] == '.' && buf[1] == '\r') break;
467 msginfo = (MsgInfo *) llast->data;
469 value = news_parse_xhdr(buf, msginfo);
478 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
479 log_warning(_("can't get xhdr\n"));
488 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
489 log_warning(_("error occurred while getting xover.\n"));
493 if (buf[0] == '.' && buf[1] == '\r') break;
495 msginfo = (MsgInfo *) llast->data;
497 value = news_parse_xhdr(buf, msginfo);
505 if (rfirst) *rfirst = first;
506 if (rlast) *rlast = last;
510 #define PARSE_ONE_PARAM(p, srcp) \
512 p = strchr(srcp, '\t'); \
513 if (!p) return NULL; \
518 static MsgInfo *news_parse_xover(const gchar *xover_str)
521 gchar buf[NNTPBUFSIZE];
522 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
524 gint num, size_int, line_int;
527 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
528 strcpy(xover_buf, xover_str);
530 PARSE_ONE_PARAM(subject, xover_buf);
531 PARSE_ONE_PARAM(sender, subject);
532 PARSE_ONE_PARAM(date, sender);
533 PARSE_ONE_PARAM(msgid, date);
534 PARSE_ONE_PARAM(ref, msgid);
535 PARSE_ONE_PARAM(size, ref);
536 PARSE_ONE_PARAM(line, size);
538 tmp = strchr(line, '\t');
539 if (!tmp) tmp = strchr(line, '\r');
540 if (!tmp) tmp = strchr(line, '\n');
541 if (tmp) *tmp = '\0';
543 num = atoi(xover_str);
544 size_int = atoi(size);
545 line_int = atoi(line);
548 msginfo = g_new0(MsgInfo, 1);
549 msginfo->msgnum = num;
550 msginfo->size = size_int;
552 msginfo->date = g_strdup(date);
553 msginfo->date_t = procheader_date_parse(NULL, date, 0);
555 conv_unmime_header(buf, sizeof(buf), sender, NULL);
556 msginfo->from = g_strdup(buf);
557 msginfo->fromname = procheader_get_fromname(buf);
559 conv_unmime_header(buf, sizeof(buf), subject, NULL);
560 msginfo->subject = g_strdup(buf);
562 extract_parenthesis(msgid, '<', '>');
565 msginfo->msgid = g_strdup(msgid);
567 eliminate_parenthesis(ref, '(', ')');
568 if ((p = strrchr(ref, '<')) != NULL) {
569 extract_parenthesis(p, '<', '>');
572 msginfo->inreplyto = g_strdup(p);
578 static gchar * news_parse_xhdr(const gchar *xover_str, MsgInfo * info)
580 gchar buf[NNTPBUFSIZE];
584 p = strchr(xover_str, ' ');
590 num = atoi(xover_str);
592 if (info->msgnum != num)
598 static GSList *news_delete_old_article(GSList *alist, gint first)
604 if (first < 2) return alist;
606 for (cur = alist; cur != NULL; ) {
609 msginfo = (MsgInfo *)cur->data;
610 if (msginfo && msginfo->msgnum < first) {
611 debug_print(_("deleting article %d...\n"),
614 cache_file = procmsg_get_message_file_path(msginfo);
615 if (is_file_exist(cache_file)) unlink(cache_file);
618 procmsg_msginfo_free(msginfo);
619 alist = g_slist_remove(alist, msginfo);
628 static void news_delete_all_article(FolderItem *item)
635 dir = folder_item_get_path(item);
636 if ((dp = opendir(dir)) == NULL) {
637 FILE_OP_ERROR(dir, "opendir");
642 debug_print(_("\tDeleting all cached articles... "));
644 while ((d = readdir(dp)) != NULL) {
645 if (to_number(d->d_name) < 0) continue;
647 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
649 if (is_file_exist(file)) {
650 if (unlink(file) < 0)
651 FILE_OP_ERROR(file, "unlink");
660 debug_print(_("done.\n"));
664 news_get_group_list returns a strings list.
665 These strings are the names of the newsgroups of a server.
666 item is the FolderItem of the news server.
667 The names of the newsgroups are cached into a file so that
668 when the function is called again, there is no need to make
669 a request to the server.
672 GSList * news_get_group_list(FolderItem *item)
674 gchar *path, *filename;
676 NNTPSession *session;
677 GSList * group_list = NULL;
679 gchar buf[NNTPBUFSIZE];
685 path = folder_item_get_path(item);
687 if (!is_dir_exist(path))
690 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
693 session = news_session_get(item->folder);
698 if (is_file_exist(filename)) {
699 debug_print(_("group list has been already cached.\n"));
702 ok = nntp_list(session->nntp_sock);
703 if (ok != NN_SUCCESS)
706 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
707 log_warning(_("can't retrieve group list\n"));
712 f = fopen(filename, "r");
713 while (fgets(buf, NNTPBUFSIZE, f)) {
717 while ((buf[len] != 0) && (buf[len] != ' '))
722 group_list = g_slist_append(group_list, s);
727 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
733 remove the cache file of the names of the newsgroups.
736 void news_reset_group_list(FolderItem *item)
738 gchar *path, *filename;
740 debug_print(_("\tDeleting cached group list... "));
741 path = folder_item_get_path(item);
742 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
744 if (remove(filename) != 0)
745 log_warning(_("can't delete cached group list %s\n"), filename);