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 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
241 if (is_file_exist(filename)) {
242 debug_print(_("article %d has been already cached.\n"), num);
246 if (!REMOTE_FOLDER(folder)->session) {
251 ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
253 if (ok != NN_SUCCESS) {
254 g_warning(_("can't select group %s\n"), item->path);
259 debug_print(_("getting article %d...\n"), num);
260 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
264 g_warning(_("can't read article %d\n"), num);
272 void news_scan_group(Folder *folder, FolderItem *item)
276 gint news_post(Folder *folder, const gchar *file)
278 NNTPSession *session;
282 g_return_val_if_fail(folder != NULL, -1);
283 g_return_val_if_fail(folder->type == F_NEWS, -1);
284 g_return_val_if_fail(file != NULL, -1);
286 session = news_session_get(folder);
287 if (!session) return -1;
289 if ((fp = fopen(file, "r")) == NULL) {
290 FILE_OP_ERROR(file, "fopen");
294 ok = nntp_post(session->nntp_sock, fp);
295 if (ok != NN_SUCCESS) {
296 log_warning(_("can't post article.\n"));
307 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
308 gint num, gchar *filename)
312 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
316 debug_print("Message-Id = %s, num = %d\n", msgid, num);
319 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
320 log_warning(_("can't retrieve article %d\n"), num);
327 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
329 return news_get_article_cmd(session, "ARTICLE", num, filename);
332 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
334 return news_get_article_cmd(session, "HEAD", num, filename);
339 * @session: Active NNTP session.
340 * @group: Newsgroup name.
342 * Select newsgroup @group with the GROUP command if it is not already
343 * selected in @session.
345 * Return value: NNTP result code.
347 static gint news_select_group(NNTPSession *session,
351 gint num, first, last;
353 if (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);
361 if (ok == NN_SUCCESS)
362 session->group = g_strdup(group);
367 static GSList *news_get_uncached_articles(NNTPSession *session,
368 FolderItem *item, gint cache_last,
369 gint *rfirst, gint *rlast)
372 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
373 gchar buf[NNTPBUFSIZE];
374 GSList *newlist = NULL;
375 GSList *llast = NULL;
378 if (rfirst) *rfirst = 0;
379 if (rlast) *rlast = 0;
381 g_return_val_if_fail(session != NULL, NULL);
382 g_return_val_if_fail(item != NULL, NULL);
383 g_return_val_if_fail(item->folder != NULL, NULL);
384 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
386 g_free(session->group);
387 session->group = NULL;
389 ok = nntp_group(session->nntp_sock, item->path,
390 &num, &first, &last);
391 if (ok != NN_SUCCESS) {
392 log_warning(_("can't set group: %s\n"), item->path);
396 session->group = g_strdup(item->path);
398 /* calculate getting overview range */
400 log_warning(_("invalid article range: %d - %d\n"),
404 if (cache_last < first)
406 else if (last < cache_last)
408 else if (last == cache_last) {
409 debug_print(_("no new articles.\n"));
412 begin = cache_last + 1;
415 if (prefs_common.max_articles > 0 &&
416 end - begin + 1 > prefs_common.max_articles)
417 begin = end - prefs_common.max_articles + 1;
419 log_message(_("getting xover %d - %d in %s...\n"),
420 begin, end, item->path);
421 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
422 log_warning(_("can't get xover\n"));
427 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
428 log_warning(_("error occurred while getting xover.\n"));
432 if (buf[0] == '.' && buf[1] == '\r') break;
434 msginfo = news_parse_xover(buf);
436 log_warning(_("invalid xover line: %s\n"), buf);
440 msginfo->folder = item;
441 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
444 llast = newlist = g_slist_append(newlist, msginfo);
446 llast = g_slist_append(llast, msginfo);
451 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
452 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 xover.\n"));
466 if (buf[0] == '.' && buf[1] == '\r') break;
468 msginfo = (MsgInfo *) llast->data;
470 value = news_parse_xhdr(buf, msginfo);
479 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
480 log_warning(_("can't get xhdr\n"));
489 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
490 log_warning(_("error occurred while getting xover.\n"));
494 if (buf[0] == '.' && buf[1] == '\r') break;
496 msginfo = (MsgInfo *) llast->data;
498 value = news_parse_xhdr(buf, msginfo);
506 if (rfirst) *rfirst = first;
507 if (rlast) *rlast = last;
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 eliminate_parenthesis(ref, '(', ')');
569 if ((p = strrchr(ref, '<')) != NULL) {
570 extract_parenthesis(p, '<', '>');
573 msginfo->inreplyto = g_strdup(p);
579 static gchar * news_parse_xhdr(const gchar *xover_str, MsgInfo * info)
581 gchar buf[NNTPBUFSIZE];
585 p = strchr(xover_str, ' ');
591 num = atoi(xover_str);
593 if (info->msgnum != num)
599 static GSList *news_delete_old_article(GSList *alist, gint first)
605 if (first < 2) return alist;
607 for (cur = alist; cur != NULL; ) {
610 msginfo = (MsgInfo *)cur->data;
611 if (msginfo && msginfo->msgnum < first) {
612 debug_print(_("deleting article %d...\n"),
615 cache_file = procmsg_get_message_file_path(msginfo);
616 if (is_file_exist(cache_file)) unlink(cache_file);
619 procmsg_msginfo_free(msginfo);
620 alist = g_slist_remove(alist, msginfo);
629 static void news_delete_all_article(FolderItem *item)
636 dir = folder_item_get_path(item);
637 if ((dp = opendir(dir)) == NULL) {
638 FILE_OP_ERROR(dir, "opendir");
643 debug_print(_("\tDeleting all cached articles... "));
645 while ((d = readdir(dp)) != NULL) {
646 if (to_number(d->d_name) < 0) continue;
648 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
650 if (is_file_exist(file)) {
651 if (unlink(file) < 0)
652 FILE_OP_ERROR(file, "unlink");
661 debug_print(_("done.\n"));
665 news_get_group_list returns a strings list.
666 These strings are the names of the newsgroups of a server.
667 item is the FolderItem of the news server.
668 The names of the newsgroups are cached into a file so that
669 when the function is called again, there is no need to make
670 a request to the server.
673 GSList * news_get_group_list(FolderItem *item)
675 gchar *path, *filename;
677 NNTPSession *session;
678 GSList * group_list = NULL;
680 gchar buf[NNTPBUFSIZE];
686 path = folder_item_get_path(item);
688 if (!is_dir_exist(path))
691 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
694 session = news_session_get(item->folder);
699 if (is_file_exist(filename)) {
700 debug_print(_("group list has been already cached.\n"));
703 ok = nntp_list(session->nntp_sock);
704 if (ok != NN_SUCCESS)
707 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
708 log_warning(_("can't retrieve group list\n"));
713 f = fopen(filename, "r");
714 while (fgets(buf, NNTPBUFSIZE, f)) {
718 while ((buf[len] != 0) && (buf[len] != ' '))
723 group_list = g_slist_append(group_list, s);
728 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
734 remove the cache file of the names of the newsgroups.
737 void news_reset_group_list(FolderItem *item)
739 gchar *path, *filename;
741 debug_print(_("\tDeleting cached group list... "));
742 path = folder_item_get_path(item);
743 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
745 if (remove(filename) != 0)
746 log_warning(_("can't delete cached group list %s\n"), filename);