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 (Folder *folder,
54 static gint news_get_article (Folder *folder,
58 static gint news_get_header (Folder *folder,
63 static GSList *news_get_uncached_articles(NNTPSession *session,
68 static MsgInfo *news_parse_xover (const gchar *xover_str);
69 static GSList *news_delete_old_article (GSList *alist,
71 static void news_delete_all_article (FolderItem *item);
73 static gchar *news_query_password (NNTPSession *session,
75 static gint news_authenticate (Folder *folder);
76 static gint news_nntp_group (Folder *folder,
82 static gint news_nntp_list (Folder *folder,
84 static gint news_nntp_xover (Folder *folder,
88 static gint news_nntp_post (Folder *folder,
91 static gint news_nntp_mode (Folder *folder,
95 Session *news_session_new(const gchar *server, gushort port)
97 gchar buf[NNTPBUFSIZE];
101 g_return_val_if_fail(server != NULL, NULL);
103 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
105 if ((nntp_sock = nntp_open(server, port, buf)) < 0)
108 session = g_new(NNTPSession, 1);
109 SESSION(session)->type = SESSION_NEWS;
110 SESSION(session)->server = g_strdup(server);
111 SESSION(session)->sock = nntp_sock;
112 SESSION(session)->connected = TRUE;
113 SESSION(session)->phase = SESSION_READY;
114 SESSION(session)->data = NULL;
115 session->group = NULL;
117 return SESSION(session);
120 void news_session_destroy(NNTPSession *session)
122 sock_close(SESSION(session)->sock);
123 SESSION(session)->sock = NULL;
125 g_free(session->group);
128 NNTPSession *news_session_get(Folder *folder)
130 g_return_val_if_fail(folder != NULL, NULL);
131 g_return_val_if_fail(folder->type == F_NEWS, NULL);
132 g_return_val_if_fail(folder->account != NULL, NULL);
134 if (!REMOTE_FOLDER(folder)->session) {
135 REMOTE_FOLDER(folder)->session =
136 news_session_new(folder->account->nntp_server, 119);
138 if (news_nntp_mode(folder,
139 REMOTE_FOLDER(folder)->session->sock,
142 log_warning(_("NNTP connection to %s:%d has been"
143 " disconnected. Reconnecting...\n"),
144 folder->account->nntp_server, 119);
145 session_destroy(REMOTE_FOLDER(folder)->session);
146 REMOTE_FOLDER(folder)->session =
147 news_session_new(folder->account->nntp_server,
152 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
155 GSList *news_get_article_list(Folder *folder, FolderItem *item,
159 NNTPSession *session;
161 g_return_val_if_fail(folder != NULL, NULL);
162 g_return_val_if_fail(item != NULL, NULL);
163 g_return_val_if_fail(folder->type == F_NEWS, NULL);
165 session = news_session_get(folder);
168 alist = procmsg_read_cache(item, FALSE);
169 item->last_num = procmsg_get_last_num_in_cache(alist);
170 } else if (use_cache) {
175 alist = procmsg_read_cache(item, FALSE);
177 cache_last = procmsg_get_last_num_in_cache(alist);
178 newlist = news_get_uncached_articles
179 (session, item, cache_last, &first, &last);
180 alist = news_delete_old_article(alist, first);
182 alist = g_slist_concat(alist, newlist);
183 item->last_num = last;
187 alist = news_get_uncached_articles
188 (session, item, 0, NULL, &last);
189 news_delete_all_article(item);
190 item->last_num = last;
193 procmsg_set_flags(alist, item);
200 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
202 gchar *path, *filename;
205 g_return_val_if_fail(folder != NULL, NULL);
206 g_return_val_if_fail(item != NULL, NULL);
208 path = folder_item_get_path(item);
209 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
212 if (is_file_exist(filename)) {
213 debug_print(_("article %d has been already cached.\n"), num);
217 if (!REMOTE_FOLDER(folder)->session) {
222 debug_print(_("getting article %d...\n"), num);
223 ok = news_get_article(folder,
224 NNTP_SESSION(REMOTE_FOLDER(folder)->session),
228 g_warning(_("can't read article %d\n"), num);
236 void news_scan_group(Folder *folder, FolderItem *item)
240 gint news_post(Folder *folder, const gchar *file)
242 NNTPSession *session;
246 g_return_val_if_fail(folder != NULL, -1);
247 g_return_val_if_fail(folder->type == F_NEWS, -1);
248 g_return_val_if_fail(file != NULL, -1);
250 session = news_session_get(folder);
251 if (!session) return -1;
253 if ((fp = fopen(file, "r")) == NULL) {
254 FILE_OP_ERROR(file, "fopen");
258 ok = news_nntp_post(folder, SESSION(session)->sock, fp);
259 if (ok != NN_SUCCESS) {
260 log_warning(_("can't post article.\n"));
271 static gint news_get_article_cmd(Folder *folder, NNTPSession *session,
273 gint num, gchar *filename)
278 ok = nntp_get_article(SESSION(session)->sock, cmd, num, &msgid);
279 if (ok == NN_AUTHREQ) {
280 ok = news_authenticate(folder);
281 if (ok != NN_SUCCESS)
283 ok = nntp_get_article(SESSION(session)->sock, cmd, num,
286 if (ok != NN_SUCCESS)
289 debug_print("Message-Id = %s, num = %d\n", msgid, num);
292 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
293 log_warning(_("can't retrieve article %d\n"), num);
300 static gint news_get_article(Folder *folder, NNTPSession *session,
301 gint num, gchar *filename)
303 return news_get_article_cmd(folder, session, "ARTICLE", num, filename);
306 static gint news_get_header(Folder *folder, NNTPSession *session,
307 gint num, gchar *filename)
309 return news_get_article_cmd(folder, session, "HEAD", num, filename);
312 static GSList *news_get_uncached_articles(NNTPSession *session,
313 FolderItem *item, gint cache_last,
314 gint *rfirst, gint *rlast)
317 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
318 gchar buf[NNTPBUFSIZE];
319 GSList *newlist = NULL;
320 GSList *llast = NULL;
323 if (rfirst) *rfirst = 0;
324 if (rlast) *rlast = 0;
326 g_return_val_if_fail(session != NULL, NULL);
327 g_return_val_if_fail(item != NULL, NULL);
328 g_return_val_if_fail(item->folder != NULL, NULL);
329 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
331 ok = news_nntp_group(item->folder, SESSION(session)->sock, item->path,
332 &num, &first, &last);
333 if (ok != NN_SUCCESS) {
334 log_warning(_("can't set group: %s\n"), item->path);
338 /* calculate getting overview range */
340 log_warning(_("invalid article range: %d - %d\n"),
344 if (cache_last < first)
346 else if (last < cache_last)
348 else if (last == cache_last) {
349 debug_print(_("no new articles.\n"));
352 begin = cache_last + 1;
355 if (prefs_common.max_articles > 0 &&
356 end - begin + 1 > prefs_common.max_articles)
357 begin = end - prefs_common.max_articles + 1;
359 log_message(_("getting xover %d - %d in %s...\n"),
360 begin, end, item->path);
361 if (news_nntp_xover(item->folder, SESSION(session)->sock,
362 begin, end) != NN_SUCCESS) {
363 log_warning(_("can't get xover\n"));
368 if (sock_read(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
369 log_warning(_("error occurred while getting xover.\n"));
373 if (buf[0] == '.' && buf[1] == '\r') break;
375 msginfo = news_parse_xover(buf);
377 log_warning(_("invalid xover line: %s\n"), buf);
381 msginfo->folder = item;
382 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
385 llast = newlist = g_slist_append(newlist, msginfo);
387 llast = g_slist_append(llast, msginfo);
392 if (rfirst) *rfirst = first;
393 if (rlast) *rlast = last;
397 #define PARSE_ONE_PARAM(p, srcp) \
399 p = strchr(srcp, '\t'); \
400 if (!p) return NULL; \
405 static MsgInfo *news_parse_xover(const gchar *xover_str)
408 gchar buf[NNTPBUFSIZE];
409 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
411 gint num, size_int, line_int;
414 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
415 strcpy(xover_buf, xover_str);
417 PARSE_ONE_PARAM(subject, xover_buf);
418 PARSE_ONE_PARAM(sender, subject);
419 PARSE_ONE_PARAM(date, sender);
420 PARSE_ONE_PARAM(msgid, date);
421 PARSE_ONE_PARAM(ref, msgid);
422 PARSE_ONE_PARAM(size, ref);
423 PARSE_ONE_PARAM(line, size);
425 tmp = strchr(line, '\t');
426 if (!tmp) tmp = strchr(line, '\r');
427 if (!tmp) tmp = strchr(line, '\n');
428 if (tmp) *tmp = '\0';
430 num = atoi(xover_str);
431 size_int = atoi(size);
432 line_int = atoi(line);
435 msginfo = g_new0(MsgInfo, 1);
436 msginfo->msgnum = num;
437 msginfo->size = size_int;
439 msginfo->date = g_strdup(date);
440 msginfo->date_t = procheader_date_parse(NULL, date, 0);
442 conv_unmime_header(buf, sizeof(buf), sender, NULL);
443 msginfo->from = g_strdup(buf);
444 msginfo->fromname = procheader_get_fromname(buf);
446 conv_unmime_header(buf, sizeof(buf), subject, NULL);
447 msginfo->subject = g_strdup(buf);
449 extract_parenthesis(msgid, '<', '>');
452 msginfo->msgid = g_strdup(msgid);
454 eliminate_parenthesis(ref, '(', ')');
455 if ((p = strrchr(ref, '<')) != NULL) {
456 extract_parenthesis(p, '<', '>');
459 msginfo->inreplyto = g_strdup(p);
465 static GSList *news_delete_old_article(GSList *alist, gint first)
471 if (first < 2) return alist;
473 for (cur = alist; cur != NULL; ) {
476 msginfo = (MsgInfo *)cur->data;
477 if (msginfo && msginfo->msgnum < first) {
478 debug_print(_("deleting article %d...\n"),
481 cache_file = procmsg_get_message_file_path(msginfo);
482 if (is_file_exist(cache_file)) unlink(cache_file);
485 procmsg_msginfo_free(msginfo);
486 alist = g_slist_remove(alist, msginfo);
495 static void news_delete_all_article(FolderItem *item)
502 dir = folder_item_get_path(item);
503 if ((dp = opendir(dir)) == NULL) {
504 FILE_OP_ERROR(dir, "opendir");
509 debug_print(_("\tDeleting all cached articles... "));
511 while ((d = readdir(dp)) != NULL) {
512 if (to_number(d->d_name) < 0) continue;
514 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
516 if (is_file_exist(file)) {
517 if (unlink(file) < 0)
518 FILE_OP_ERROR(file, "unlink");
527 debug_print(_("done.\n"));
531 news_get_group_list returns a strings list.
532 These strings are the names of the newsgroups of a server.
533 item is the FolderItem of the news server.
534 The names of the newsgroups are cached into a file so that
535 when the function is called again, there is no need to make
536 a request to the server.
539 GSList * news_get_group_list(FolderItem *item)
541 gchar *path, *filename;
543 NNTPSession *session;
544 GSList * group_list = NULL;
546 gchar buf[NNTPBUFSIZE];
552 path = folder_item_get_path(item);
554 if (!is_dir_exist(path))
557 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
560 session = news_session_get(item->folder);
565 if (is_file_exist(filename)) {
566 debug_print(_("group list has been already cached.\n"));
569 ok = news_nntp_list(item->folder, SESSION(session)->sock);
570 if (ok != NN_SUCCESS)
573 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
574 log_warning(_("can't retrieve group list\n"));
579 f = fopen(filename, "r");
580 while (fgets(buf, NNTPBUFSIZE, f)) {
584 while ((buf[len] != 0) && (buf[len] != ' '))
589 group_list = g_slist_append(group_list, s);
594 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
600 remove the cache file of the names of the newsgroups.
603 void news_reset_group_list(FolderItem *item)
605 gchar *path, *filename;
607 debug_print(_("\tDeleting cached group list... "));
608 path = folder_item_get_path(item);
609 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
611 if (remove(filename) != 0)
612 log_warning(_("can't delete cached group list %s\n"), filename);
617 /* NNTP Authentication support (RFC2980). */
621 * news_query_password:
622 * @session: Active NNTP session.
625 * Ask user for the NNTP authentication password.
627 * Return value: Password string; needs to be freed with g_free()
630 static gchar *news_query_password(NNTPSession *session,
636 message = g_strdup_printf
637 (_("Input password for %s on %s:"),
639 SESSION(session)->server);
641 pass = input_dialog_with_invisible(_("Input password"),
644 /* manage_window_focus_in(inc_dialog->mainwin->window, */
651 * @folder: NNTP folder.
653 * Send NNTP authentication information (AUTHINFO USER, AUTHINFO PASS)
654 * as described in RFC2980. May ask user for the password if it is
655 * not stored in account settings.
657 * Return value: NNTP result code (NN_*).
659 static gint news_authenticate(Folder *folder)
661 NNTPSession *session;
665 gboolean need_free_pass = FALSE;
667 debug_print(_("news server requested authentication\n"));
668 if (!folder || !folder->account)
670 user = folder->account->userid;
673 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
676 ok = nntp_authinfo_user(SESSION(session)->sock, user);
677 if (ok == NN_AUTHCONT) {
678 pass = folder->account->passwd;
679 if (!pass || !pass[0]) {
680 pass = news_query_password(session, user);
681 need_free_pass = TRUE;
683 ok = nntp_authinfo_pass(SESSION(session)->sock, pass);
687 if (ok != NN_SUCCESS) {
688 log_warning(_("NNTP authentication failed\n"));
689 alertpanel_error(_("Authentication for %s on %s failed."),
690 user, SESSION(session)->server);
698 * @folder: NNTP folder.
699 * @sock: NNTP socket.
700 * @group: Group name to select.
701 * @num: Pointer to returned number of messages in the group.
702 * @first: Pointer to returned number of first message.
703 * @last: Pointer to returned number of last message.
705 * Wrapper for nntp_group() to do authentication if requested.
706 * Selects newsgroup @group and returns information from the server
707 * response in @num, @first, @last (which cannot be NULL).
709 * Return value: NNTP result code (NN_*).
711 static gint news_nntp_group(Folder *folder, SockInfo *sock,
713 gint *num, gint *first, gint *last)
717 if ((ok = nntp_group(sock, group, num, first, last)) == NN_AUTHREQ) {
718 ok = news_authenticate(folder);
719 if (ok != NN_SUCCESS)
721 ok = nntp_group(sock, group, num, first, last);
728 * @folder: NNTP folder.
729 * @sock: NNTP socket.
731 * Wrapper for nntp_list() to do authentication if requested. Sends
732 * the LIST command to @sock and receives the status code line.
734 * Return value: NNTP result code (NN_*).
736 static gint news_nntp_list(Folder *folder, SockInfo *sock)
740 if ((ok = nntp_list(sock)) == NN_AUTHREQ) {
741 ok = news_authenticate(folder);
742 if (ok != NN_SUCCESS)
744 ok = nntp_list(sock);
751 * @folder: NNTP folder.
752 * @sock: NNTP socket.
753 * @first: First message number.
754 * @last: Last message number.
756 * Wrapper for nntp_xover() to do authentication if requested. Sends
757 * the XOVER command with parameters @first and @last to @sock and
758 * receives the status code line.
760 * Return value: NNTP result code (NN_*).
762 static gint news_nntp_xover(Folder *folder, SockInfo *sock,
763 gint first, gint last)
767 if ((ok = nntp_xover(sock, first, last)) == NN_AUTHREQ) {
768 ok = news_authenticate(folder);
769 if (ok != NN_SUCCESS)
771 ok = nntp_xover(sock, first, last);
778 * @folder: NNTP folder.
779 * @sock: NNTP socket.
780 * @fp: File with the message (header and body), opened for reading.
782 * Wrapper for nntp_post() to do authentication if requested. Sends
783 * the POST command to @sock, and if the server accepts it, sends the
786 * Return value: NNTP result code (NN_*).
788 static gint news_nntp_post(Folder *folder, SockInfo *sock, FILE *fp)
792 if ((ok = nntp_post(sock, fp)) == NN_AUTHREQ) {
793 ok = news_authenticate(folder);
794 if (ok != NN_SUCCESS)
796 ok = nntp_post(sock, fp);
803 * @folder: NNTP folder.
804 * @sock: NNTP socket.
805 * @stream: TRUE for stream mode, FALSE for reader mode.
807 * Wrapper for nntp_mode() to do authentication if requested. Sends
808 * the "MODE READER" or "MODE STREAM" command to @sock, depending on
809 * @stream, and receives the status responce.
811 * Return value: NNTP result code (NN_*).
813 static gint news_nntp_mode(Folder *folder, SockInfo *sock, gboolean stream)
817 if ((ok = nntp_mode(sock, stream)) == NN_AUTHREQ) {
818 ok = news_authenticate(folder);
819 if (ok != NN_SUCCESS)
821 ok = nntp_mode(sock, stream);