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.
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 Session *news_session_new (const gchar *server,
55 static gint news_get_article_cmd (NNTPSession *session,
59 static gint news_get_article (NNTPSession *session,
62 static gint news_get_header (NNTPSession *session,
66 static gint news_select_group (NNTPSession *session,
68 static GSList *news_get_uncached_articles(NNTPSession *session,
73 static MsgInfo *news_parse_xover (const gchar *xover_str);
74 static gchar *news_parse_xhdr (const gchar *xhdr_str,
76 static GSList *news_delete_old_article (GSList *alist,
78 static void news_delete_all_article (FolderItem *item);
81 static Session *news_session_new(const gchar *server, gushort port,
82 const gchar *userid, const gchar *passwd)
84 gchar buf[NNTPBUFSIZE];
86 NNTPSockInfo *nntp_sock;
88 g_return_val_if_fail(server != NULL, NULL);
90 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
93 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
95 nntp_sock = nntp_open(server, port, buf);
96 if (nntp_sock == NULL)
99 session = g_new(NNTPSession, 1);
100 SESSION(session)->type = SESSION_NEWS;
101 SESSION(session)->server = g_strdup(server);
102 session->nntp_sock = nntp_sock;
103 SESSION(session)->sock = nntp_sock->sock;
104 SESSION(session)->connected = TRUE;
105 SESSION(session)->phase = SESSION_READY;
106 SESSION(session)->data = NULL;
107 session->group = NULL;
109 return SESSION(session);
112 void news_session_destroy(NNTPSession *session)
114 nntp_close(session->nntp_sock);
115 session->nntp_sock = NULL;
116 SESSION(session)->sock = NULL;
118 g_free(session->group);
121 static gchar *news_query_password(const gchar *server, const gchar *user)
126 message = g_strdup_printf(_("Input password for %s on %s:"),
128 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
134 static Session *news_session_new_for_folder(Folder *folder)
138 const gchar *userid = NULL;
139 gchar *passwd = NULL;
141 ac = folder->account;
142 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
144 if (ac->passwd && ac->passwd[0])
145 passwd = g_strdup(ac->passwd);
147 passwd = news_query_password(ac->nntp_server, userid);
150 session = news_session_new(ac->nntp_server, 119, userid, passwd);
156 NNTPSession *news_session_get(Folder *folder)
158 NNTPSession *session;
160 g_return_val_if_fail(folder != NULL, NULL);
161 g_return_val_if_fail(folder->type == F_NEWS, NULL);
162 g_return_val_if_fail(folder->account != NULL, NULL);
164 if (!REMOTE_FOLDER(folder)->session) {
165 REMOTE_FOLDER(folder)->session =
166 news_session_new_for_folder(folder);
168 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
169 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
170 log_warning(_("NNTP connection to %s:%d has been"
171 " disconnected. Reconnecting...\n"),
172 folder->account->nntp_server, 119);
173 session_destroy(REMOTE_FOLDER(folder)->session);
174 REMOTE_FOLDER(folder)->session =
175 news_session_new_for_folder(folder);
179 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
182 GSList *news_get_article_list(Folder *folder, FolderItem *item,
186 NNTPSession *session;
188 g_return_val_if_fail(folder != NULL, NULL);
189 g_return_val_if_fail(item != NULL, NULL);
190 g_return_val_if_fail(folder->type == F_NEWS, NULL);
192 session = news_session_get(folder);
195 alist = procmsg_read_cache(item, FALSE);
196 item->last_num = procmsg_get_last_num_in_cache(alist);
197 } else if (use_cache) {
202 alist = procmsg_read_cache(item, FALSE);
204 cache_last = procmsg_get_last_num_in_cache(alist);
205 newlist = news_get_uncached_articles
206 (session, item, cache_last, &first, &last);
207 alist = news_delete_old_article(alist, first);
209 alist = g_slist_concat(alist, newlist);
210 item->last_num = last;
214 alist = news_get_uncached_articles
215 (session, item, 0, NULL, &last);
216 news_delete_all_article(item);
217 item->last_num = last;
220 procmsg_set_flags(alist, item);
227 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
229 gchar *path, *filename;
232 g_return_val_if_fail(folder != NULL, NULL);
233 g_return_val_if_fail(item != NULL, NULL);
235 path = folder_item_get_path(item);
236 if (!is_dir_exist(path))
239 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
242 if (is_file_exist(filename)) {
243 debug_print(_("article %d has been already cached.\n"), num);
247 if (!REMOTE_FOLDER(folder)->session) {
252 ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
254 if (ok != NN_SUCCESS) {
255 g_warning(_("can't select group %s\n"), item->path);
260 debug_print(_("getting article %d...\n"), num);
261 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
265 g_warning(_("can't read article %d\n"), num);
273 void news_scan_group(Folder *folder, FolderItem *item)
277 gint news_post(Folder *folder, const gchar *file)
279 NNTPSession *session;
283 g_return_val_if_fail(folder != NULL, -1);
284 g_return_val_if_fail(folder->type == F_NEWS, -1);
285 g_return_val_if_fail(file != NULL, -1);
287 session = news_session_get(folder);
288 if (!session) return -1;
290 if ((fp = fopen(file, "r")) == NULL) {
291 FILE_OP_ERROR(file, "fopen");
295 ok = nntp_post(session->nntp_sock, fp);
296 if (ok != NN_SUCCESS) {
297 log_warning(_("can't post article.\n"));
308 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
309 gint num, gchar *filename)
313 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
317 debug_print("Message-Id = %s, num = %d\n", msgid, num);
320 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
321 log_warning(_("can't retrieve article %d\n"), num);
328 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
330 return news_get_article_cmd(session, "ARTICLE", num, filename);
333 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
335 return news_get_article_cmd(session, "HEAD", num, filename);
340 * @session: Active NNTP session.
341 * @group: Newsgroup name.
343 * Select newsgroup @group with the GROUP command if it is not already
344 * selected in @session.
346 * Return value: NNTP result code.
348 static gint news_select_group(NNTPSession *session, const gchar *group)
351 gint num, first, last;
353 if (session->group && g_strcasecmp(session->group, group) == 0)
356 g_free(session->group);
357 session->group = NULL;
359 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);
394 session->group = g_strdup(item->path);
396 /* calculate getting overview range */
398 log_warning(_("invalid article range: %d - %d\n"),
402 if (cache_last < first)
404 else if (last < cache_last)
406 else if (last == cache_last) {
407 debug_print(_("no new articles.\n"));
410 begin = cache_last + 1;
413 if (rfirst) *rfirst = first;
414 if (rlast) *rlast = last;
416 if (prefs_common.max_articles > 0 &&
417 end - begin + 1 > prefs_common.max_articles)
418 begin = end - prefs_common.max_articles + 1;
420 log_message(_("getting xover %d - %d in %s...\n"),
421 begin, end, item->path);
422 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
423 log_warning(_("can't get xover\n"));
428 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
429 log_warning(_("error occurred while getting xover.\n"));
433 if (buf[0] == '.' && buf[1] == '\r') break;
435 msginfo = news_parse_xover(buf);
437 log_warning(_("invalid xover line: %s\n"), buf);
441 msginfo->folder = item;
442 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
443 msginfo->newsgroups = g_strdup(item->path);
446 llast = newlist = g_slist_append(newlist, msginfo);
448 llast = g_slist_append(llast, msginfo);
453 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
454 log_warning(_("can't get xhdr\n"));
461 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
462 log_warning(_("error occurred while getting xhdr.\n"));
466 if (buf[0] == '.' && buf[1] == '\r') break;
468 g_warning("llast == NULL\n");
472 msginfo = (MsgInfo *)llast->data;
473 msginfo->to = 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"));
486 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
487 log_warning(_("error occurred while getting xhdr.\n"));
491 if (buf[0] == '.' && buf[1] == '\r') break;
493 g_warning("llast == NULL\n");
497 msginfo = (MsgInfo *)llast->data;
498 msginfo->cc = news_parse_xhdr(buf, msginfo);
506 #define PARSE_ONE_PARAM(p, srcp) \
508 p = strchr(srcp, '\t'); \
509 if (!p) return NULL; \
514 static MsgInfo *news_parse_xover(const gchar *xover_str)
517 gchar buf[NNTPBUFSIZE];
518 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
520 gint num, size_int, line_int;
523 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
524 strcpy(xover_buf, xover_str);
526 PARSE_ONE_PARAM(subject, xover_buf);
527 PARSE_ONE_PARAM(sender, subject);
528 PARSE_ONE_PARAM(date, sender);
529 PARSE_ONE_PARAM(msgid, date);
530 PARSE_ONE_PARAM(ref, msgid);
531 PARSE_ONE_PARAM(size, ref);
532 PARSE_ONE_PARAM(line, size);
534 tmp = strchr(line, '\t');
535 if (!tmp) tmp = strchr(line, '\r');
536 if (!tmp) tmp = strchr(line, '\n');
537 if (tmp) *tmp = '\0';
539 num = atoi(xover_str);
540 size_int = atoi(size);
541 line_int = atoi(line);
544 msginfo = g_new0(MsgInfo, 1);
545 msginfo->msgnum = num;
546 msginfo->size = size_int;
548 msginfo->date = g_strdup(date);
549 msginfo->date_t = procheader_date_parse(NULL, date, 0);
551 conv_unmime_header(buf, sizeof(buf), sender, NULL);
552 msginfo->from = g_strdup(buf);
553 msginfo->fromname = procheader_get_fromname(buf);
555 conv_unmime_header(buf, sizeof(buf), subject, NULL);
556 msginfo->subject = g_strdup(buf);
558 extract_parenthesis(msgid, '<', '>');
561 msginfo->msgid = g_strdup(msgid);
563 msginfo->references = g_strdup(ref);
564 eliminate_parenthesis(ref, '(', ')');
565 if ((p = strrchr(ref, '<')) != NULL) {
566 extract_parenthesis(p, '<', '>');
569 msginfo->inreplyto = g_strdup(p);
575 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
577 gchar buf[NNTPBUFSIZE];
582 p = strchr(xhdr_str, ' ');
588 num = atoi(xhdr_str);
589 if (msginfo->msgnum != num) return NULL;
591 tmp = strchr(p, '\r');
592 if (!tmp) tmp = strchr(p, '\n');
595 return g_strndup(p, tmp - p);
600 static GSList *news_delete_old_article(GSList *alist, gint first)
606 if (first < 2) return alist;
608 for (cur = alist; cur != NULL; ) {
611 msginfo = (MsgInfo *)cur->data;
612 if (msginfo && msginfo->msgnum < first) {
613 debug_print(_("deleting article %d...\n"),
616 cache_file = procmsg_get_message_file_path(msginfo);
617 if (is_file_exist(cache_file)) unlink(cache_file);
620 procmsg_msginfo_free(msginfo);
621 alist = g_slist_remove(alist, msginfo);
630 static void news_delete_all_article(FolderItem *item)
637 dir = folder_item_get_path(item);
638 if (!is_dir_exist(dir))
641 if ((dp = opendir(dir)) == NULL) {
642 FILE_OP_ERROR(dir, "opendir");
647 debug_print(_("\tDeleting all cached articles... "));
649 while ((d = readdir(dp)) != NULL) {
650 if (to_number(d->d_name) < 0) continue;
652 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
654 if (is_file_exist(file)) {
655 if (unlink(file) < 0)
656 FILE_OP_ERROR(file, "unlink");
665 debug_print(_("done.\n"));
669 news_get_group_list returns a strings list.
670 These strings are the names of the newsgroups of a server.
671 item is the FolderItem of the news server.
672 The names of the newsgroups are cached into a file so that
673 when the function is called again, there is no need to make
674 a request to the server.
677 GSList * news_get_group_list(FolderItem *item)
679 gchar *path, *filename;
681 NNTPSession *session;
682 GSList * group_list = NULL;
684 gchar buf[NNTPBUFSIZE];
690 path = folder_item_get_path(item);
692 if (!is_dir_exist(path))
695 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
698 session = news_session_get(item->folder);
703 if (is_file_exist(filename)) {
704 debug_print(_("group list has been already cached.\n"));
707 ok = nntp_list(session->nntp_sock);
708 if (ok != NN_SUCCESS)
711 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
712 log_warning(_("can't retrieve group list\n"));
717 f = fopen(filename, "r");
718 while (fgets(buf, NNTPBUFSIZE, f)) {
722 while ((buf[len] != 0) && (buf[len] != ' '))
727 group_list = g_slist_append(group_list, s);
732 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
738 remove the cache file of the names of the newsgroups.
741 void news_reset_group_list(FolderItem *item)
743 gchar *path, *filename;
745 debug_print(_("\tDeleting cached group list... "));
746 path = folder_item_get_path(item);
747 if (!is_dir_exist(path))
750 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
752 if (remove(filename) != 0)
753 log_warning(_("can't delete cached group list %s\n"), filename);