2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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.
40 #include "procheader.h"
43 #include "statusbar.h"
46 #include "prefs_common.h"
47 #include "prefs_account.h"
48 #include "inputdialog.h"
50 #include "progressindicator.h"
57 #define NNTPS_PORT 563
60 static Folder *news_folder_new(const gchar * name, const gchar * folder);
61 static void news_folder_destroy(Folder * folder);
63 static gchar *news_fetch_msg(Folder * folder, FolderItem * item, gint num);
65 static void news_folder_init (Folder *folder,
70 static Session *news_session_new (const gchar *server,
76 static Session *news_session_new (const gchar *server,
82 static gint news_get_article_cmd (NNTPSession *session,
86 static gint news_get_article (NNTPSession *session,
90 static gint news_select_group (NNTPSession *session,
95 static MsgInfo *news_parse_xover (const gchar *xover_str);
96 static gchar *news_parse_xhdr (const gchar *xhdr_str,
98 gint news_get_num_list (Folder *folder,
101 MsgInfo *news_get_msginfo (Folder *folder,
104 GSList *news_get_msginfos (Folder *folder,
106 GSList *msgnum_list);
108 gint news_post_stream (Folder *folder,
111 FolderClass news_class =
117 /* Folder functions */
123 /* FolderItem functions */
135 /* Message functions */
147 FolderClass *news_get_class(void)
152 Folder *news_folder_new(const gchar *name, const gchar *path)
156 folder = (Folder *)g_new0(NewsFolder, 1);
157 folder->klass = &news_class;
158 news_folder_init(folder, name, path);
163 void news_folder_destroy(Folder *folder)
167 dir = folder_get_path(folder);
168 if (is_dir_exist(dir))
169 remove_dir_recursive(dir);
172 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
175 static void news_folder_init(Folder *folder, const gchar *name,
178 folder_remote_folder_init(folder, name, path);
182 static Session *news_session_new(const gchar *server, gushort port,
183 const gchar *userid, const gchar *passwd,
186 static Session *news_session_new(const gchar *server, gushort port,
187 const gchar *userid, const gchar *passwd)
190 gchar buf[NNTPBUFSIZE];
193 g_return_val_if_fail(server != NULL, NULL);
195 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
198 session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
200 session = nntp_session_new(server, port, buf, userid, passwd);
206 static Session *news_session_new_for_folder(Folder *folder)
210 const gchar *userid = NULL;
211 gchar *passwd = NULL;
213 gchar buf[NNTPBUFSIZE];
215 g_return_val_if_fail(folder != NULL, NULL);
216 g_return_val_if_fail(folder->account != NULL, NULL);
218 ac = folder->account;
219 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
221 if (ac->passwd && ac->passwd[0])
222 passwd = g_strdup(ac->passwd);
224 passwd = input_dialog_query_password(ac->nntp_server,
229 port = ac->set_nntpport ? ac->nntpport
230 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
231 session = news_session_new(ac->nntp_server, port, userid, passwd,
234 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
235 session = news_session_new(ac->nntp_server, port, userid, passwd);
237 if ((session != NULL) && ac->use_nntp_auth && ac->use_nntp_auth_onconnect)
238 nntp_forceauth(NNTP_SESSION(session), buf, userid, passwd);
245 NNTPSession *news_session_get(Folder *folder)
247 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
249 g_return_val_if_fail(folder != NULL, NULL);
250 g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
251 g_return_val_if_fail(folder->account != NULL, NULL);
253 if (!rfolder->session) {
254 rfolder->session = news_session_new_for_folder(folder);
255 return NNTP_SESSION(rfolder->session);
258 if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
259 rfolder->session->last_access_time = time(NULL);
260 return NNTP_SESSION(rfolder->session);
263 if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
265 log_warning("NNTP connection to %s:%d has been"
266 " disconnected. Reconnecting...\n",
267 folder->account->nntp_server,
268 folder->account->set_nntpport ?
269 folder->account->nntpport : NNTP_PORT);
270 session_destroy(rfolder->session);
271 rfolder->session = news_session_new_for_folder(folder);
274 if (rfolder->session)
275 rfolder->session->last_access_time = time(NULL);
276 return NNTP_SESSION(rfolder->session);
279 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
281 gchar *path, *filename;
282 NNTPSession *session;
285 g_return_val_if_fail(folder != NULL, NULL);
286 g_return_val_if_fail(item != NULL, NULL);
288 path = folder_item_get_path(item);
289 if (!is_dir_exist(path))
291 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
294 if (is_file_exist(filename)) {
295 debug_print("article %d has been already cached.\n", num);
299 session = news_session_get(folder);
305 ok = news_select_group(session, item->path, NULL, NULL, NULL);
306 if (ok != NN_SUCCESS) {
307 g_warning("can't select group %s\n", item->path);
312 debug_print("getting article %d...\n", num);
313 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
316 g_warning("can't read article %d\n", num);
317 session_destroy(SESSION(session));
318 REMOTE_FOLDER(folder)->session = NULL;
326 static NewsGroupInfo *news_group_info_new(const gchar *name,
327 gint first, gint last, gchar type)
329 NewsGroupInfo *ginfo;
331 ginfo = g_new(NewsGroupInfo, 1);
332 ginfo->name = g_strdup(name);
333 ginfo->first = first;
340 static void news_group_info_free(NewsGroupInfo *ginfo)
346 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
347 NewsGroupInfo *ginfo2)
349 return g_strcasecmp(ginfo1->name, ginfo2->name);
352 GSList *news_get_group_list(Folder *folder)
354 gchar *path, *filename;
358 gchar buf[NNTPBUFSIZE];
360 g_return_val_if_fail(folder != NULL, NULL);
361 g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
363 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
364 if (!is_dir_exist(path))
366 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
369 if ((fp = fopen(filename, "rb")) == NULL) {
370 NNTPSession *session;
372 session = news_session_get(folder);
378 if (nntp_list(session) != NN_SUCCESS) {
382 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
383 log_warning("can't retrieve newsgroup list\n");
384 session_destroy(SESSION(session));
385 REMOTE_FOLDER(folder)->session = NULL;
390 if ((fp = fopen(filename, "rb")) == NULL) {
391 FILE_OP_ERROR(filename, "fopen");
397 while (fgets(buf, sizeof(buf), fp) != NULL) {
403 NewsGroupInfo *ginfo;
411 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
414 ginfo = news_group_info_new(name, first_num, last_num, type);
417 last = list = g_slist_append(NULL, ginfo);
419 last = g_slist_append(last, ginfo);
427 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
432 void news_group_list_free(GSList *group_list)
436 if (!group_list) return;
438 for (cur = group_list; cur != NULL; cur = cur->next)
439 news_group_info_free((NewsGroupInfo *)cur->data);
440 g_slist_free(group_list);
443 void news_remove_group_list_cache(Folder *folder)
445 gchar *path, *filename;
447 g_return_if_fail(folder != NULL);
448 g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
450 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
451 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
454 if (is_file_exist(filename)) {
455 if (remove(filename) < 0)
456 FILE_OP_ERROR(filename, "remove");
461 gint news_post(Folder *folder, const gchar *file)
466 g_return_val_if_fail(folder != NULL, -1);
467 g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
468 g_return_val_if_fail(file != NULL, -1);
470 if ((fp = fopen(file, "rb")) == NULL) {
471 FILE_OP_ERROR(file, "fopen");
475 ok = news_post_stream(folder, fp);
482 gint news_post_stream(Folder *folder, FILE *fp)
484 NNTPSession *session;
487 g_return_val_if_fail(folder != NULL, -1);
488 g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
489 g_return_val_if_fail(fp != NULL, -1);
491 session = news_session_get(folder);
492 if (!session) return -1;
494 ok = nntp_post(session, fp);
495 if (ok != NN_SUCCESS) {
496 log_warning("can't post article.\n");
503 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
504 gint num, gchar *filename)
508 if (nntp_get_article(session, cmd, num, &msgid)
512 debug_print("Message-Id = %s, num = %d\n", msgid, num);
515 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
516 log_warning("can't retrieve article %d\n", num);
523 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
525 return news_get_article_cmd(session, "ARTICLE", num, filename);
530 * @session: Active NNTP session.
531 * @group: Newsgroup name.
532 * @num: Estimated number of articles.
533 * @first: First article number.
534 * @last: Last article number.
536 * Select newsgroup @group with the GROUP command if it is not already
537 * selected in @session, or article numbers need to be returned.
539 * Return value: NNTP result code.
541 static gint news_select_group(NNTPSession *session, const gchar *group,
542 gint *num, gint *first, gint *last)
545 gint num_, first_, last_;
547 if (!num || !first || !last) {
548 if (session->group && g_strcasecmp(session->group, group) == 0)
555 g_free(session->group);
556 session->group = NULL;
558 ok = nntp_group(session, group, num, first, last);
559 if (ok == NN_SUCCESS)
560 session->group = g_strdup(group);
565 #define PARSE_ONE_PARAM(p, srcp) \
567 p = strchr(srcp, '\t'); \
568 if (!p) return NULL; \
573 static MsgInfo *news_parse_xover(const gchar *xover_str)
576 gchar buf[NNTPBUFSIZE];
577 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
579 gint num, size_int, line_int;
582 Xstrdup_a(xover_buf, xover_str, return NULL);
584 PARSE_ONE_PARAM(subject, xover_buf);
585 PARSE_ONE_PARAM(sender, subject);
586 PARSE_ONE_PARAM(date, sender);
587 PARSE_ONE_PARAM(msgid, date);
588 PARSE_ONE_PARAM(ref, msgid);
589 PARSE_ONE_PARAM(size, ref);
590 PARSE_ONE_PARAM(line, size);
591 PARSE_ONE_PARAM(xref, line);
593 tmp = strchr(xref, '\t');
594 if (!tmp) tmp = strchr(line, '\r');
595 if (!tmp) tmp = strchr(line, '\n');
596 if (tmp) *tmp = '\0';
598 num = atoi(xover_str);
599 size_int = atoi(size);
600 line_int = atoi(line);
603 msginfo = procmsg_msginfo_new();
604 msginfo->msgnum = num;
605 msginfo->size = size_int;
607 msginfo->date = g_strdup(date);
608 msginfo->date_t = procheader_date_parse(NULL, date, 0);
610 conv_unmime_header(buf, sizeof(buf), sender, NULL);
611 msginfo->from = g_strdup(buf);
612 msginfo->fromname = procheader_get_fromname(buf);
614 conv_unmime_header(buf, sizeof(buf), subject, NULL);
615 msginfo->subject = g_strdup(buf);
617 extract_parenthesis(msgid, '<', '>');
620 msginfo->msgid = g_strdup(msgid);
622 msginfo->references = g_strdup(ref);
623 eliminate_parenthesis(ref, '(', ')');
624 if ((p = strrchr(ref, '<')) != NULL) {
625 extract_parenthesis(p, '<', '>');
628 msginfo->inreplyto = g_strdup(p);
631 msginfo->xref = g_strdup(xref);
632 p = msginfo->xref+strlen(msginfo->xref) - 1;
633 while (*p == '\r' || *p == '\n') {
641 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
647 p = strchr(xhdr_str, ' ');
653 num = atoi(xhdr_str);
654 if (msginfo->msgnum != num) return NULL;
656 tmp = strchr(p, '\r');
657 if (!tmp) tmp = strchr(p, '\n');
660 return g_strndup(p, tmp - p);
665 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
671 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
672 G_DIR_SEPARATOR, (gint)msginfo);
676 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
677 FILE_OP_ERROR(tmp, "fopen");
680 if (change_file_mode_rw(tmpfp, tmp) < 0) {
681 FILE_OP_ERROR(tmp, "chmod");
682 g_warning("can't change file mode\n");
685 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
686 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
687 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
688 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
689 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
690 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
691 get_rfc822_date(buf, sizeof(buf));
692 fprintf(tmpfp, "Date: %s\r\n", buf);
693 fprintf(tmpfp, "\r\n");
694 fprintf(tmpfp, "removed with sylpheed\r\n");
698 news_post(folder, tmp);
706 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list)
708 NNTPSession *session;
709 gint i, ok, num, first, last, nummsgs = 0;
712 g_return_val_if_fail(item != NULL, -1);
713 g_return_val_if_fail(item->folder != NULL, -1);
714 g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
716 session = news_session_get(folder);
717 g_return_val_if_fail(session != NULL, -1);
719 ok = news_select_group(session, item->path, &num, &first, &last);
720 if (ok != NN_SUCCESS) {
721 log_warning(_("can't set group: %s\n"), item->path);
726 log_warning(_("invalid article range: %d - %d\n"),
731 for(i = first; i <= last; i++) {
732 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
736 dir = folder_item_get_path(item);
737 debug_print("removing old messages from %d to %d in %s\n", first, last, dir);
738 remove_numbered_files(dir, 1, first - 1);
744 #define READ_TO_LISTEND(hdr) \
745 while (!(buf[0] == '.' && buf[1] == '\r')) { \
746 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
747 log_warning(_("error occurred while getting %s.\n"), hdr); \
752 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
754 NNTPSession *session;
755 MsgInfo *msginfo = NULL;
756 gchar buf[NNTPBUFSIZE];
758 session = news_session_get(folder);
759 g_return_val_if_fail(session != NULL, NULL);
760 g_return_val_if_fail(item != NULL, NULL);
761 g_return_val_if_fail(item->folder != NULL, NULL);
762 g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
764 log_message(_("getting xover %d in %s...\n"),
766 if (nntp_xover(session, num, num) != NN_SUCCESS) {
767 log_warning(_("can't get xover\n"));
771 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
772 log_warning(_("error occurred while getting xover.\n"));
776 msginfo = news_parse_xover(buf);
778 log_warning(_("invalid xover line: %s\n"), buf);
781 READ_TO_LISTEND("xover");
786 msginfo->folder = item;
787 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
788 msginfo->flags.tmp_flags = MSG_NEWS;
789 msginfo->newsgroups = g_strdup(item->path);
791 if (nntp_xhdr(session, "to", num, num) != NN_SUCCESS) {
792 log_warning(_("can't get xhdr\n"));
796 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
797 log_warning(_("error occurred while getting xhdr.\n"));
801 msginfo->to = news_parse_xhdr(buf, msginfo);
803 READ_TO_LISTEND("xhdr (to)");
805 if (nntp_xhdr(session, "cc", num, num) != NN_SUCCESS) {
806 log_warning(_("can't get xhdr\n"));
810 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
811 log_warning(_("error occurred while getting xhdr.\n"));
815 msginfo->cc = news_parse_xhdr(buf, msginfo);
817 READ_TO_LISTEND("xhdr (cc)");
822 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
824 gchar buf[NNTPBUFSIZE];
825 GSList *newlist = NULL;
826 GSList *llast = NULL;
828 guint count = 0, lines = (end - begin + 2) * 3;
830 g_return_val_if_fail(session != NULL, NULL);
831 g_return_val_if_fail(item != NULL, NULL);
833 log_message(_("getting xover %d - %d in %s...\n"),
834 begin, end, item->path);
835 if (nntp_xover(session, begin, end) != NN_SUCCESS) {
836 log_warning(_("can't get xover\n"));
841 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
842 log_warning(_("error occurred while getting xover.\n"));
846 progressindicator_set_percentage
847 (PROGRESS_TYPE_NETWORK,
848 session->fetch_base_percentage +
849 (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
851 if (buf[0] == '.' && buf[1] == '\r') break;
853 msginfo = news_parse_xover(buf);
855 log_warning(_("invalid xover line: %s\n"), buf);
859 msginfo->folder = item;
860 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
861 msginfo->flags.tmp_flags = MSG_NEWS;
862 msginfo->newsgroups = g_strdup(item->path);
865 llast = newlist = g_slist_append(newlist, msginfo);
867 llast = g_slist_append(llast, msginfo);
872 if (nntp_xhdr(session, "to", begin, end) != NN_SUCCESS) {
873 log_warning(_("can't get xhdr\n"));
880 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
881 log_warning(_("error occurred while getting xhdr.\n"));
885 progressindicator_set_percentage
886 (PROGRESS_TYPE_NETWORK,
887 session->fetch_base_percentage +
888 (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
890 if (buf[0] == '.' && buf[1] == '\r') break;
892 g_warning("llast == NULL\n");
896 msginfo = (MsgInfo *)llast->data;
897 msginfo->to = news_parse_xhdr(buf, msginfo);
902 if (nntp_xhdr(session, "cc", begin, end) != NN_SUCCESS) {
903 log_warning(_("can't get xhdr\n"));
910 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
911 log_warning(_("error occurred while getting xhdr.\n"));
915 progressindicator_set_percentage
916 (PROGRESS_TYPE_NETWORK,
917 session->fetch_base_percentage +
918 (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
920 if (buf[0] == '.' && buf[1] == '\r') break;
922 g_warning("llast == NULL\n");
926 msginfo = (MsgInfo *)llast->data;
927 msginfo->cc = news_parse_xhdr(buf, msginfo);
935 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
937 NNTPSession *session;
938 GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
939 guint first, last, next;
940 guint tofetch, fetched;
942 g_return_val_if_fail(folder != NULL, NULL);
943 g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
944 g_return_val_if_fail(msgnum_list != NULL, NULL);
945 g_return_val_if_fail(item != NULL, NULL);
947 session = news_session_get(folder);
948 g_return_val_if_fail(session != NULL, NULL);
950 tmp_msgnum_list = g_slist_copy(msgnum_list);
951 tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
953 progressindicator_start(PROGRESS_TYPE_NETWORK);
954 tofetch = g_slist_length(tmp_msgnum_list);
957 first = GPOINTER_TO_INT(tmp_msgnum_list->data);
959 for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
960 next = GPOINTER_TO_INT(elem->data);
961 if(next != (last + 1)) {
962 session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
963 session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
964 tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
965 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
966 fetched = last - first + 1;
971 session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
972 session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
973 tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
974 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
976 g_slist_free(tmp_msgnum_list);
978 progressindicator_stop(PROGRESS_TYPE_NETWORK);