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"
52 static Session *news_session_new (const gchar *server,
57 static gint news_get_article_cmd (NNTPSession *session,
61 static gint news_get_article (NNTPSession *session,
64 static gint news_get_header (NNTPSession *session,
68 static gint news_select_group (NNTPSession *session,
70 static GSList *news_get_uncached_articles(NNTPSession *session,
75 static MsgInfo *news_parse_xover (const gchar *xover_str);
76 static gchar *news_parse_xhdr (const gchar *xhdr_str,
78 static GSList *news_delete_old_articles (GSList *alist,
81 static void news_delete_all_articles (FolderItem *item);
84 static Session *news_session_new(const gchar *server, gushort port,
85 const gchar *userid, const gchar *passwd)
87 gchar buf[NNTPBUFSIZE];
89 NNTPSockInfo *nntp_sock;
91 g_return_val_if_fail(server != NULL, NULL);
93 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
96 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
98 nntp_sock = nntp_open(server, port, buf);
99 if (nntp_sock == NULL)
102 session = g_new(NNTPSession, 1);
103 SESSION(session)->type = SESSION_NEWS;
104 SESSION(session)->server = g_strdup(server);
105 session->nntp_sock = nntp_sock;
106 SESSION(session)->sock = nntp_sock->sock;
107 SESSION(session)->connected = TRUE;
108 SESSION(session)->phase = SESSION_READY;
109 SESSION(session)->data = NULL;
110 session->group = NULL;
112 return SESSION(session);
115 void news_session_destroy(NNTPSession *session)
117 nntp_close(session->nntp_sock);
118 session->nntp_sock = NULL;
119 SESSION(session)->sock = NULL;
121 g_free(session->group);
124 static gchar *news_query_password(const gchar *server, const gchar *user)
129 message = g_strdup_printf(_("Input password for %s on %s:"),
131 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
137 static Session *news_session_new_for_folder(Folder *folder)
141 const gchar *userid = NULL;
142 gchar *passwd = NULL;
144 ac = folder->account;
145 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
147 if (ac->passwd && ac->passwd[0])
148 passwd = g_strdup(ac->passwd);
150 passwd = news_query_password(ac->nntp_server, userid);
153 session = news_session_new(ac->nntp_server, NNTP_PORT, userid, passwd);
159 NNTPSession *news_session_get(Folder *folder)
161 NNTPSession *session;
163 g_return_val_if_fail(folder != NULL, NULL);
164 g_return_val_if_fail(folder->type == F_NEWS, NULL);
165 g_return_val_if_fail(folder->account != NULL, NULL);
167 if (!REMOTE_FOLDER(folder)->session) {
168 REMOTE_FOLDER(folder)->session =
169 news_session_new_for_folder(folder);
170 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
173 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
175 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
176 log_warning(_("NNTP connection to %s:%d has been"
177 " disconnected. Reconnecting...\n"),
178 folder->account->nntp_server, NNTP_PORT);
179 session_destroy(REMOTE_FOLDER(folder)->session);
180 REMOTE_FOLDER(folder)->session =
181 news_session_new_for_folder(folder);
184 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
187 GSList *news_get_article_list(Folder *folder, FolderItem *item,
191 NNTPSession *session;
193 g_return_val_if_fail(folder != NULL, NULL);
194 g_return_val_if_fail(item != NULL, NULL);
195 g_return_val_if_fail(folder->type == F_NEWS, NULL);
197 session = news_session_get(folder);
200 alist = procmsg_read_cache(item, FALSE);
201 item->last_num = procmsg_get_last_num_in_cache(alist);
202 } else if (use_cache) {
207 alist = procmsg_read_cache(item, FALSE);
209 cache_last = procmsg_get_last_num_in_cache(alist);
210 newlist = news_get_uncached_articles
211 (session, item, cache_last, &first, &last);
212 alist = news_delete_old_articles(alist, item, first);
214 alist = g_slist_concat(alist, newlist);
215 item->last_num = last;
219 alist = news_get_uncached_articles
220 (session, item, 0, NULL, &last);
221 news_delete_all_articles(item);
222 item->last_num = last;
225 procmsg_set_flags(alist, item);
232 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
234 gchar *path, *filename;
237 g_return_val_if_fail(folder != NULL, NULL);
238 g_return_val_if_fail(item != NULL, NULL);
240 path = folder_item_get_path(item);
241 if (!is_dir_exist(path))
244 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
247 if (is_file_exist(filename)) {
248 debug_print(_("article %d has been already cached.\n"), num);
252 if (!REMOTE_FOLDER(folder)->session) {
257 ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
259 if (ok != NN_SUCCESS) {
260 g_warning(_("can't select group %s\n"), item->path);
265 debug_print(_("getting article %d...\n"), num);
266 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
270 g_warning(_("can't read article %d\n"), num);
278 void news_scan_group(Folder *folder, FolderItem *item)
282 gint news_post(Folder *folder, const gchar *file)
284 NNTPSession *session;
288 g_return_val_if_fail(folder != NULL, -1);
289 g_return_val_if_fail(folder->type == F_NEWS, -1);
290 g_return_val_if_fail(file != NULL, -1);
292 session = news_session_get(folder);
293 if (!session) return -1;
295 if ((fp = fopen(file, "r")) == NULL) {
296 FILE_OP_ERROR(file, "fopen");
300 ok = nntp_post(session->nntp_sock, fp);
301 if (ok != NN_SUCCESS) {
302 log_warning(_("can't post article.\n"));
313 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
314 gint num, gchar *filename)
318 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
322 debug_print("Message-Id = %s, num = %d\n", msgid, num);
325 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
326 log_warning(_("can't retrieve article %d\n"), num);
333 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
335 return news_get_article_cmd(session, "ARTICLE", num, filename);
338 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
340 return news_get_article_cmd(session, "HEAD", num, filename);
345 * @session: Active NNTP session.
346 * @group: Newsgroup name.
348 * Select newsgroup @group with the GROUP command if it is not already
349 * selected in @session.
351 * Return value: NNTP result code.
353 static gint news_select_group(NNTPSession *session, const gchar *group)
356 gint num, first, last;
358 if (session->group && g_strcasecmp(session->group, group) == 0)
361 g_free(session->group);
362 session->group = NULL;
364 ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
365 if (ok == NN_SUCCESS)
366 session->group = g_strdup(group);
371 static GSList *news_get_uncached_articles(NNTPSession *session,
372 FolderItem *item, gint cache_last,
373 gint *rfirst, gint *rlast)
376 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
377 gchar buf[NNTPBUFSIZE];
378 GSList *newlist = NULL;
379 GSList *llast = NULL;
382 if (rfirst) *rfirst = 0;
383 if (rlast) *rlast = 0;
385 g_return_val_if_fail(session != NULL, NULL);
386 g_return_val_if_fail(item != NULL, NULL);
387 g_return_val_if_fail(item->folder != NULL, NULL);
388 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
390 g_free(session->group);
391 session->group = NULL;
393 ok = nntp_group(session->nntp_sock, item->path,
394 &num, &first, &last);
395 if (ok != NN_SUCCESS) {
396 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 (rfirst) *rfirst = first;
419 if (rlast) *rlast = last;
421 if (prefs_common.max_articles > 0 &&
422 end - begin + 1 > prefs_common.max_articles)
423 begin = end - prefs_common.max_articles + 1;
425 log_message(_("getting xover %d - %d in %s...\n"),
426 begin, end, item->path);
427 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
428 log_warning(_("can't get xover\n"));
433 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
434 log_warning(_("error occurred while getting xover.\n"));
438 if (buf[0] == '.' && buf[1] == '\r') break;
440 msginfo = news_parse_xover(buf);
442 log_warning(_("invalid xover line: %s\n"), buf);
446 msginfo->folder = item;
447 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
448 msginfo->newsgroups = g_strdup(item->path);
451 llast = newlist = g_slist_append(newlist, msginfo);
453 llast = g_slist_append(llast, msginfo);
458 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
459 log_warning(_("can't get xhdr\n"));
466 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
467 log_warning(_("error occurred while getting xhdr.\n"));
471 if (buf[0] == '.' && buf[1] == '\r') break;
473 g_warning("llast == NULL\n");
477 msginfo = (MsgInfo *)llast->data;
478 msginfo->to = 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"));
491 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
492 log_warning(_("error occurred while getting xhdr.\n"));
496 if (buf[0] == '.' && buf[1] == '\r') break;
498 g_warning("llast == NULL\n");
502 msginfo = (MsgInfo *)llast->data;
503 msginfo->cc = news_parse_xhdr(buf, msginfo);
511 #define PARSE_ONE_PARAM(p, srcp) \
513 p = strchr(srcp, '\t'); \
514 if (!p) return NULL; \
519 static MsgInfo *news_parse_xover(const gchar *xover_str)
522 gchar buf[NNTPBUFSIZE];
523 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
525 gint num, size_int, line_int;
528 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
529 strcpy(xover_buf, xover_str);
531 PARSE_ONE_PARAM(subject, xover_buf);
532 PARSE_ONE_PARAM(sender, subject);
533 PARSE_ONE_PARAM(date, sender);
534 PARSE_ONE_PARAM(msgid, date);
535 PARSE_ONE_PARAM(ref, msgid);
536 PARSE_ONE_PARAM(size, ref);
537 PARSE_ONE_PARAM(line, size);
539 tmp = strchr(line, '\t');
540 if (!tmp) tmp = strchr(line, '\r');
541 if (!tmp) tmp = strchr(line, '\n');
542 if (tmp) *tmp = '\0';
544 num = atoi(xover_str);
545 size_int = atoi(size);
546 line_int = atoi(line);
549 msginfo = g_new0(MsgInfo, 1);
550 msginfo->msgnum = num;
551 msginfo->size = size_int;
553 msginfo->date = g_strdup(date);
554 msginfo->date_t = procheader_date_parse(NULL, date, 0);
556 conv_unmime_header(buf, sizeof(buf), sender, NULL);
557 msginfo->from = g_strdup(buf);
558 msginfo->fromname = procheader_get_fromname(buf);
560 conv_unmime_header(buf, sizeof(buf), subject, NULL);
561 msginfo->subject = g_strdup(buf);
563 extract_parenthesis(msgid, '<', '>');
566 msginfo->msgid = g_strdup(msgid);
568 msginfo->references = g_strdup(ref);
569 eliminate_parenthesis(ref, '(', ')');
570 if ((p = strrchr(ref, '<')) != NULL) {
571 extract_parenthesis(p, '<', '>');
574 msginfo->inreplyto = g_strdup(p);
580 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
582 gchar buf[NNTPBUFSIZE];
587 p = strchr(xhdr_str, ' ');
593 num = atoi(xhdr_str);
594 if (msginfo->msgnum != num) return NULL;
596 tmp = strchr(p, '\r');
597 if (!tmp) tmp = strchr(p, '\n');
600 return g_strndup(p, tmp - p);
605 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
612 g_return_val_if_fail(item != NULL, alist);
613 g_return_val_if_fail(item->folder != NULL, alist);
614 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
616 if (first < 2) return alist;
618 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
620 dir = folder_item_get_path(item);
621 remove_numbered_files(dir, 1, first - 1);
624 for (cur = alist; cur != NULL; ) {
627 msginfo = (MsgInfo *)cur->data;
628 if (msginfo && msginfo->msgnum < first) {
629 procmsg_msginfo_free(msginfo);
630 alist = g_slist_remove(alist, msginfo);
639 static void news_delete_all_articles(FolderItem *item)
643 g_return_if_fail(item != NULL);
644 g_return_if_fail(item->folder != NULL);
645 g_return_if_fail(item->folder->type == F_NEWS);
647 debug_print(_("\tDeleting all cached articles... "));
649 dir = folder_item_get_path(item);
650 if (!is_dir_exist(dir))
652 remove_all_numbered_files(dir);
655 debug_print(_("done.\n"));
659 news_get_group_list returns a strings list.
660 These strings are the names of the newsgroups of a server.
661 item is the FolderItem of the news server.
662 The names of the newsgroups are cached into a file so that
663 when the function is called again, there is no need to make
664 a request to the server.
667 GSList * news_get_group_list(FolderItem *item)
669 gchar *path, *filename;
671 NNTPSession *session;
672 GSList * group_list = NULL;
674 gchar buf[NNTPBUFSIZE];
680 path = folder_item_get_path(item);
682 if (!is_dir_exist(path))
685 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
688 session = news_session_get(item->folder);
693 if (is_file_exist(filename)) {
694 debug_print(_("group list has been already cached.\n"));
697 ok = nntp_list(session->nntp_sock);
698 if (ok != NN_SUCCESS)
701 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
702 log_warning(_("can't retrieve group list\n"));
707 f = fopen(filename, "r");
708 while (fgets(buf, NNTPBUFSIZE, f)) {
712 while ((buf[len] != 0) && (buf[len] != ' '))
717 group_list = g_slist_append(group_list, s);
722 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
728 remove the cache file of the names of the newsgroups.
731 void news_reset_group_list(FolderItem *item)
733 gchar *path, *filename;
735 debug_print(_("\tDeleting cached group list... "));
736 path = folder_item_get_path(item);
737 if (!is_dir_exist(path))
740 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
742 if (remove(filename) != 0)
743 log_warning(_("can't delete cached group list %s\n"), filename);