2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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"
49 #include "alertpanel.h"
56 #define NNTPS_PORT 563
59 static void news_folder_init (Folder *folder,
64 static Session *news_session_new (const gchar *server,
70 static Session *news_session_new (const gchar *server,
76 static gint news_get_article_cmd (NNTPSession *session,
80 static gint news_get_article (NNTPSession *session,
83 static gint news_get_header (NNTPSession *session,
87 static gint news_select_group (NNTPSession *session,
92 static GSList *news_get_uncached_articles(NNTPSession *session,
97 static MsgInfo *news_parse_xover (const gchar *xover_str);
98 static gchar *news_parse_xhdr (const gchar *xhdr_str,
100 static GSList *news_delete_old_articles (GSList *alist,
103 static void news_delete_all_articles (FolderItem *item);
105 static gint news_remove_msg (Folder *folder,
108 GSList *news_get_num_list (Folder *folder,
110 MsgInfo *news_fetch_msginfo (Folder *folder,
113 GSList *news_fetch_msginfos (Folder *folder,
115 GSList *msgnum_list);
117 Folder *news_folder_new(const gchar *name, const gchar *path)
121 folder = (Folder *)g_new0(NewsFolder, 1);
122 news_folder_init(folder, name, path);
127 void news_folder_destroy(NewsFolder *folder)
129 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
132 static void news_folder_init(Folder *folder, const gchar *name,
135 folder_remote_folder_init(folder, name, path);
137 folder->type = F_NEWS;
140 folder->get_msg_list = news_get_article_list;
142 folder->fetch_msg = news_fetch_msg;
144 folder->scan = news_scan_group;
146 folder->remove_msg = news_remove_msg;
147 folder->get_num_list = news_get_num_list;
148 folder->fetch_msginfo = news_fetch_msginfo;
149 folder->fetch_msginfos = news_fetch_msginfos;
153 static Session *news_session_new(const gchar *server, gushort port,
154 const gchar *userid, const gchar *passwd,
157 static Session *news_session_new(const gchar *server, gushort port,
158 const gchar *userid, const gchar *passwd)
161 gchar buf[NNTPBUFSIZE];
162 NNTPSession *session;
163 NNTPSockInfo *nntp_sock;
165 g_return_val_if_fail(server != NULL, NULL);
167 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
170 if (userid && passwd)
171 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd,
174 nntp_sock = nntp_open(server, port, buf, ssl_type);
176 if (userid && passwd)
177 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
179 nntp_sock = nntp_open(server, port, buf);
182 if (nntp_sock == NULL)
185 session = g_new(NNTPSession, 1);
186 SESSION(session)->type = SESSION_NEWS;
187 SESSION(session)->server = g_strdup(server);
188 session->nntp_sock = nntp_sock;
189 SESSION(session)->sock = nntp_sock->sock;
190 SESSION(session)->connected = TRUE;
191 SESSION(session)->phase = SESSION_READY;
192 SESSION(session)->last_access_time = time(NULL);
193 SESSION(session)->data = NULL;
194 session->group = NULL;
196 return SESSION(session);
199 void news_session_destroy(NNTPSession *session)
201 nntp_close(session->nntp_sock);
202 session->nntp_sock = NULL;
203 SESSION(session)->sock = NULL;
205 g_free(session->group);
208 static Session *news_session_new_for_folder(Folder *folder)
212 const gchar *userid = NULL;
213 gchar *passwd = NULL;
216 g_return_val_if_fail(folder != NULL, NULL);
217 g_return_val_if_fail(folder->account != NULL, NULL);
219 ac = folder->account;
220 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
222 if (ac->passwd && ac->passwd[0])
223 passwd = g_strdup(ac->passwd);
225 passwd = input_dialog_query_password(ac->nntp_server,
230 port = ac->set_nntpport ? ac->nntpport
231 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
232 session = news_session_new(ac->nntp_server, port, userid, passwd,
235 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
236 session = news_session_new(ac->nntp_server, port, userid, passwd);
244 NNTPSession *news_session_get(Folder *folder)
246 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
248 g_return_val_if_fail(folder != NULL, NULL);
249 g_return_val_if_fail(folder->type == F_NEWS, NULL);
250 g_return_val_if_fail(folder->account != NULL, NULL);
252 if (!rfolder->session) {
253 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);
261 return NNTP_SESSION(rfolder->session);
264 if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
266 log_warning(_("NNTP connection to %s:%d has been"
267 " disconnected. Reconnecting...\n"),
268 folder->account->nntp_server,
269 folder->account->set_nntpport ?
270 folder->account->nntpport : NNTP_PORT);
271 session_destroy(rfolder->session);
272 rfolder->session = news_session_new_for_folder(folder);
275 if (rfolder->session)
276 rfolder->session->last_access_time = time(NULL);
278 return NNTP_SESSION(rfolder->session);
281 GSList *news_get_article_list(Folder *folder, FolderItem *item,
285 NNTPSession *session;
287 g_return_val_if_fail(folder != NULL, NULL);
288 g_return_val_if_fail(item != NULL, NULL);
289 g_return_val_if_fail(folder->type == F_NEWS, NULL);
291 session = news_session_get(folder);
294 alist = procmsg_read_cache(item, FALSE);
295 item->last_num = procmsg_get_last_num_in_cache(alist);
296 } else if (use_cache) {
301 alist = procmsg_read_cache(item, FALSE);
303 cache_last = procmsg_get_last_num_in_cache(alist);
304 newlist = news_get_uncached_articles
305 (session, item, cache_last, &first, &last);
306 alist = news_delete_old_articles(alist, item, first);
308 alist = g_slist_concat(alist, newlist);
309 item->last_num = last;
313 alist = news_get_uncached_articles
314 (session, item, 0, NULL, &last);
315 news_delete_all_articles(item);
316 item->last_num = last;
319 procmsg_set_flags(alist, item);
326 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
328 gchar *path, *filename;
329 NNTPSession *session;
332 g_return_val_if_fail(folder != NULL, NULL);
333 g_return_val_if_fail(item != NULL, NULL);
335 path = folder_item_get_path(item);
336 if (!is_dir_exist(path))
338 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
341 if (is_file_exist(filename)) {
342 debug_print(_("article %d has been already cached.\n"), num);
346 session = news_session_get(folder);
352 ok = news_select_group(session, item->path, NULL, NULL, NULL);
354 if (ok != NN_SUCCESS) {
355 g_warning(_("can't select group %s\n"), item->path);
360 debug_print(_("getting article %d...\n"), num);
361 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
365 g_warning(_("can't read article %d\n"), num);
373 gint news_scan_group(Folder *folder, FolderItem *item)
375 NNTPSession *session;
376 gint num = 0, first = 0, last = 0;
377 gint new = 0, unread = 0, total = 0;
378 gint min = 0, max = 0;
382 g_return_val_if_fail(folder != NULL, -1);
383 g_return_val_if_fail(item != NULL, -1);
385 session = news_session_get(folder);
386 if (!session) return -1;
388 ok = news_select_group(session, item->path, &num, &first, &last);
389 if (ok != NN_SUCCESS) {
390 log_warning(_("can't set group: %s\n"), item->path);
395 item->new = item->unread = item->total = item->last_num = 0;
399 path = folder_item_get_path(item);
400 if (path && is_dir_exist(path)) {
401 procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
406 if (max < first || last < min)
407 new = unread = total = num;
411 else if (first < min) {
418 else if (max < last) {
420 unread += last - max;
423 if (new > num) new = num;
424 if (unread > num) unread = num;
428 item->unread = unread;
430 item->last_num = last;
435 static NewsGroupInfo *news_group_info_new(const gchar *name,
436 gint first, gint last, gchar type)
438 NewsGroupInfo *ginfo;
440 ginfo = g_new(NewsGroupInfo, 1);
441 ginfo->name = g_strdup(name);
442 ginfo->first = first;
449 static void news_group_info_free(NewsGroupInfo *ginfo)
455 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
456 NewsGroupInfo *ginfo2)
458 return g_strcasecmp(ginfo1->name, ginfo2->name);
461 GSList *news_get_group_list(Folder *folder)
463 gchar *path, *filename;
467 gchar buf[NNTPBUFSIZE];
469 g_return_val_if_fail(folder != NULL, NULL);
470 g_return_val_if_fail(folder->type == F_NEWS, NULL);
472 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
473 if (!is_dir_exist(path))
475 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
478 if ((fp = fopen(filename, "rb")) == NULL) {
479 NNTPSession *session;
481 session = news_session_get(folder);
487 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
493 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
494 log_warning(_("can't retrieve newsgroup list\n"));
495 session_destroy(SESSION(session));
496 REMOTE_FOLDER(folder)->session = NULL;
501 if ((fp = fopen(filename, "rb")) == NULL) {
502 FILE_OP_ERROR(filename, "fopen");
508 while (fgets(buf, sizeof(buf), fp) != NULL) {
514 NewsGroupInfo *ginfo;
522 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
525 ginfo = news_group_info_new(name, first_num, last_num, type);
528 last = list = g_slist_append(NULL, ginfo);
530 last = g_slist_append(last, ginfo);
538 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
545 void news_group_list_free(GSList *group_list)
549 if (!group_list) return;
551 for (cur = group_list; cur != NULL; cur = cur->next)
552 news_group_info_free((NewsGroupInfo *)cur->data);
553 g_slist_free(group_list);
556 void news_remove_group_list_cache(Folder *folder)
558 gchar *path, *filename;
560 g_return_if_fail(folder != NULL);
561 g_return_if_fail(folder->type == F_NEWS);
563 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
564 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
567 if (is_file_exist(filename)) {
568 if (remove(filename) < 0)
569 FILE_OP_ERROR(filename, "remove");
574 gint news_post(Folder *folder, const gchar *file)
576 NNTPSession *session;
580 g_return_val_if_fail(folder != NULL, -1);
581 g_return_val_if_fail(folder->type == F_NEWS, -1);
582 g_return_val_if_fail(file != NULL, -1);
584 session = news_session_get(folder);
585 if (!session) return -1;
587 if ((fp = fopen(file, "rb")) == NULL) {
588 FILE_OP_ERROR(file, "fopen");
592 ok = nntp_post(session->nntp_sock, fp);
593 if (ok != NN_SUCCESS) {
594 log_warning(_("can't post article.\n"));
605 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
606 gint num, gchar *filename)
610 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
614 debug_print("Message-Id = %s, num = %d\n", msgid, num);
617 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
618 log_warning(_("can't retrieve article %d\n"), num);
625 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
629 MsgFlags msgflags = { 0, 0 };
632 filename = folder_item_fetch_msg(item, num);
633 if (filename == NULL)
636 msginfo = procheader_parse_file(filename, msgflags, FALSE, FALSE);
640 r = news_cancel_article(folder, msginfo);
642 procmsg_msginfo_free(msginfo);
647 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
649 return news_get_article_cmd(session, "ARTICLE", num, filename);
652 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
654 return news_get_article_cmd(session, "HEAD", num, filename);
659 * @session: Active NNTP session.
660 * @group: Newsgroup name.
661 * @num: Estimated number of articles.
662 * @first: First article number.
663 * @last: Last article number.
665 * Select newsgroup @group with the GROUP command if it is not already
666 * selected in @session, or article numbers need to be returned.
668 * Return value: NNTP result code.
670 static gint news_select_group(NNTPSession *session, const gchar *group,
671 gint *num, gint *first, gint *last)
674 gint num_, first_, last_;
676 if (!num || !first || !last) {
677 if (session->group && g_strcasecmp(session->group, group) == 0)
684 g_free(session->group);
685 session->group = NULL;
687 ok = nntp_group(session->nntp_sock, group, num, first, last);
688 if (ok == NN_SUCCESS)
689 session->group = g_strdup(group);
694 static GSList *news_get_uncached_articles(NNTPSession *session,
695 FolderItem *item, gint cache_last,
696 gint *rfirst, gint *rlast)
699 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
700 gchar buf[NNTPBUFSIZE];
701 GSList *newlist = NULL;
702 GSList *llast = NULL;
705 if (rfirst) *rfirst = 0;
706 if (rlast) *rlast = 0;
708 g_return_val_if_fail(session != NULL, NULL);
709 g_return_val_if_fail(item != NULL, NULL);
710 g_return_val_if_fail(item->folder != NULL, NULL);
711 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
713 ok = news_select_group(session, item->path, &num, &first, &last);
714 if (ok != NN_SUCCESS) {
715 log_warning(_("can't set group: %s\n"), item->path);
719 /* calculate getting overview range */
721 log_warning(_("invalid article range: %d - %d\n"),
725 if (cache_last < first)
727 else if (last < cache_last)
729 else if (last == cache_last) {
730 debug_print(_("no new articles.\n"));
733 begin = cache_last + 1;
736 if (rfirst) *rfirst = first;
737 if (rlast) *rlast = last;
739 if (prefs_common.max_articles > 0 &&
740 end - begin + 1 > prefs_common.max_articles)
741 begin = end - prefs_common.max_articles + 1;
743 log_message(_("getting xover %d - %d in %s...\n"),
744 begin, end, item->path);
745 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
746 log_warning(_("can't get xover\n"));
751 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
752 log_warning(_("error occurred while getting xover.\n"));
756 if (buf[0] == '.' && buf[1] == '\r') break;
758 msginfo = news_parse_xover(buf);
760 log_warning(_("invalid xover line: %s\n"), buf);
764 msginfo->folder = item;
765 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
766 msginfo->flags.tmp_flags = MSG_NEWS;
767 msginfo->newsgroups = g_strdup(item->path);
770 llast = newlist = g_slist_append(newlist, msginfo);
772 llast = g_slist_append(llast, msginfo);
777 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
778 log_warning(_("can't get xhdr\n"));
785 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
786 log_warning(_("error occurred while getting xhdr.\n"));
790 if (buf[0] == '.' && buf[1] == '\r') break;
792 g_warning("llast == NULL\n");
796 msginfo = (MsgInfo *)llast->data;
797 msginfo->to = news_parse_xhdr(buf, msginfo);
802 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
803 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 if (buf[0] == '.' && buf[1] == '\r') break;
817 g_warning("llast == NULL\n");
821 msginfo = (MsgInfo *)llast->data;
822 msginfo->cc = news_parse_xhdr(buf, msginfo);
830 #define PARSE_ONE_PARAM(p, srcp) \
832 p = strchr(srcp, '\t'); \
833 if (!p) return NULL; \
838 static MsgInfo *news_parse_xover(const gchar *xover_str)
841 gchar buf[NNTPBUFSIZE];
842 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
844 gint num, size_int, line_int;
847 Xstrdup_a(xover_buf, xover_str, return NULL);
849 PARSE_ONE_PARAM(subject, xover_buf);
850 PARSE_ONE_PARAM(sender, subject);
851 PARSE_ONE_PARAM(date, sender);
852 PARSE_ONE_PARAM(msgid, date);
853 PARSE_ONE_PARAM(ref, msgid);
854 PARSE_ONE_PARAM(size, ref);
855 PARSE_ONE_PARAM(line, size);
856 PARSE_ONE_PARAM(xref, line);
858 tmp = strchr(xref, '\t');
859 if (!tmp) tmp = strchr(line, '\r');
860 if (!tmp) tmp = strchr(line, '\n');
861 if (tmp) *tmp = '\0';
863 num = atoi(xover_str);
864 size_int = atoi(size);
865 line_int = atoi(line);
868 msginfo = procmsg_msginfo_new();
869 msginfo->msgnum = num;
870 msginfo->size = size_int;
872 msginfo->date = g_strdup(date);
873 msginfo->date_t = procheader_date_parse(NULL, date, 0);
875 conv_unmime_header(buf, sizeof(buf), sender, NULL);
876 msginfo->from = g_strdup(buf);
877 msginfo->fromname = procheader_get_fromname(buf);
879 conv_unmime_header(buf, sizeof(buf), subject, NULL);
880 msginfo->subject = g_strdup(buf);
882 extract_parenthesis(msgid, '<', '>');
885 msginfo->msgid = g_strdup(msgid);
887 msginfo->references = g_strdup(ref);
888 eliminate_parenthesis(ref, '(', ')');
889 if ((p = strrchr(ref, '<')) != NULL) {
890 extract_parenthesis(p, '<', '>');
893 msginfo->inreplyto = g_strdup(p);
896 msginfo->xref = g_strdup(xref);
897 p = msginfo->xref+strlen(msginfo->xref) - 1;
898 while (*p == '\r' || *p == '\n') {
906 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
912 p = strchr(xhdr_str, ' ');
918 num = atoi(xhdr_str);
919 if (msginfo->msgnum != num) return NULL;
921 tmp = strchr(p, '\r');
922 if (!tmp) tmp = strchr(p, '\n');
925 return g_strndup(p, tmp - p);
930 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
937 g_return_val_if_fail(item != NULL, alist);
938 g_return_val_if_fail(item->folder != NULL, alist);
939 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
941 if (first < 2) return alist;
943 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
945 dir = folder_item_get_path(item);
946 remove_numbered_files(dir, 1, first - 1);
949 for (cur = alist; cur != NULL; ) {
952 msginfo = (MsgInfo *)cur->data;
953 if (msginfo && msginfo->msgnum < first) {
954 procmsg_msginfo_free(msginfo);
955 alist = g_slist_remove(alist, msginfo);
964 static void news_delete_all_articles(FolderItem *item)
968 g_return_if_fail(item != NULL);
969 g_return_if_fail(item->folder != NULL);
970 g_return_if_fail(item->folder->type == F_NEWS);
972 debug_print(_("\tDeleting all cached articles... "));
974 dir = folder_item_get_path(item);
975 remove_all_numbered_files(dir);
978 debug_print(_("done.\n"));
981 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
987 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
988 G_DIR_SEPARATOR, (gint)msginfo);
992 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
993 FILE_OP_ERROR(tmp, "fopen");
996 if (change_file_mode_rw(tmpfp, tmp) < 0) {
997 FILE_OP_ERROR(tmp, "chmod");
998 g_warning(_("can't change file mode\n"));
1001 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
1002 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
1003 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
1004 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
1005 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
1006 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
1007 get_rfc822_date(buf, sizeof(buf));
1008 fprintf(tmpfp, "Date: %s\r\n", buf);
1009 fprintf(tmpfp, "\r\n");
1010 fprintf(tmpfp, "removed with sylpheed\r\n");
1014 news_post(folder, tmp);
1022 GSList *news_get_num_list(Folder *folder, FolderItem *item)
1024 NNTPSession *session;
1025 gint i, ok, num, first, last;
1026 GSList *msgnum_list = NULL;
1028 session = news_session_get(folder);
1029 g_return_val_if_fail(session != NULL, NULL);
1030 g_return_val_if_fail(item != NULL, NULL);
1031 g_return_val_if_fail(item->folder != NULL, NULL);
1032 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1034 ok = news_select_group(session, item->path, &num, &first, &last);
1035 if (ok != NN_SUCCESS) {
1036 log_warning(_("can't set group: %s\n"), item->path);
1041 log_warning(_("invalid article range: %d - %d\n"),
1046 for(i = first; i <= last; i++) {
1047 msgnum_list = g_slist_prepend(msgnum_list, GINT_TO_POINTER(i));
1053 #define READ_TO_LISTEND(hdr) \
1054 while (!(buf[0] == '.' && buf[1] == '\r')) { \
1055 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
1056 log_warning(_("error occurred while getting " hdr ".\n")); \
1061 MsgInfo *news_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
1063 NNTPSession *session;
1064 MsgInfo *msginfo = NULL;
1065 gchar buf[NNTPBUFSIZE];
1067 session = news_session_get(folder);
1068 g_return_val_if_fail(session != NULL, NULL);
1069 g_return_val_if_fail(item != NULL, NULL);
1070 g_return_val_if_fail(item->folder != NULL, NULL);
1071 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1073 log_message(_("getting xover %d in %s...\n"),
1075 if (nntp_xover(session->nntp_sock, num, num) != NN_SUCCESS) {
1076 log_warning(_("can't get xover\n"));
1080 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1081 log_warning(_("error occurred while getting xover.\n"));
1085 msginfo = news_parse_xover(buf);
1087 log_warning(_("invalid xover line: %s\n"), buf);
1090 READ_TO_LISTEND("xover");
1095 msginfo->folder = item;
1096 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1097 msginfo->flags.tmp_flags = MSG_NEWS;
1098 msginfo->newsgroups = g_strdup(item->path);
1100 if (nntp_xhdr(session->nntp_sock, "to", num, num) != NN_SUCCESS) {
1101 log_warning(_("can't get xhdr\n"));
1105 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1106 log_warning(_("error occurred while getting xhdr.\n"));
1110 msginfo->to = news_parse_xhdr(buf, msginfo);
1112 READ_TO_LISTEND("xhdr (to)");
1114 if (nntp_xhdr(session->nntp_sock, "cc", num, num) != NN_SUCCESS) {
1115 log_warning(_("can't get xhdr\n"));
1119 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1120 log_warning(_("error occurred while getting xhdr.\n"));
1124 msginfo->cc = news_parse_xhdr(buf, msginfo);
1126 READ_TO_LISTEND("xhdr (cc)");
1131 static GSList *news_fetch_msginfo_from_to(NNTPSession *session, FolderItem *item, guint begin, guint end)
1133 gchar buf[NNTPBUFSIZE];
1134 GSList *newlist = NULL;
1135 GSList *llast = NULL;
1138 g_return_val_if_fail(session != NULL, NULL);
1139 g_return_val_if_fail(item != NULL, NULL);
1141 log_message(_("getting xover %d - %d in %s...\n"),
1142 begin, end, item->path);
1143 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
1144 log_warning(_("can't get xover\n"));
1149 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1150 log_warning(_("error occurred while getting xover.\n"));
1154 if (buf[0] == '.' && buf[1] == '\r') break;
1156 msginfo = news_parse_xover(buf);
1158 log_warning(_("invalid xover line: %s\n"), buf);
1162 msginfo->folder = item;
1163 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1164 msginfo->flags.tmp_flags = MSG_NEWS;
1165 msginfo->newsgroups = g_strdup(item->path);
1168 llast = newlist = g_slist_append(newlist, msginfo);
1170 llast = g_slist_append(llast, msginfo);
1171 llast = llast->next;
1175 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
1176 log_warning(_("can't get xhdr\n"));
1183 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1184 log_warning(_("error occurred while getting xhdr.\n"));
1188 if (buf[0] == '.' && buf[1] == '\r') break;
1190 g_warning("llast == NULL\n");
1194 msginfo = (MsgInfo *)llast->data;
1195 msginfo->to = news_parse_xhdr(buf, msginfo);
1197 llast = llast->next;
1200 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
1201 log_warning(_("can't get xhdr\n"));
1208 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1209 log_warning(_("error occurred while getting xhdr.\n"));
1213 if (buf[0] == '.' && buf[1] == '\r') break;
1215 g_warning("llast == NULL\n");
1219 msginfo = (MsgInfo *)llast->data;
1220 msginfo->cc = news_parse_xhdr(buf, msginfo);
1222 llast = llast->next;
1228 gint news_fetch_msgnum_sort(gconstpointer a, gconstpointer b)
1230 return (GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b));
1233 GSList *news_fetch_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1235 NNTPSession *session;
1236 GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1237 guint first, last, next;
1239 g_return_val_if_fail(folder != NULL, NULL);
1240 g_return_val_if_fail(folder->type == F_NEWS, NULL);
1241 g_return_val_if_fail(msgnum_list != NULL, NULL);
1242 g_return_val_if_fail(item != NULL, NULL);
1244 session = news_session_get(folder);
1245 g_return_val_if_fail(session != NULL, NULL);
1247 tmp_msgnum_list = g_slist_copy(msgnum_list);
1248 tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, news_fetch_msgnum_sort);
1250 first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1252 for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1253 next = GPOINTER_TO_INT(elem->data);
1254 if(next != (last + 1)) {
1255 tmp_msginfo_list = news_fetch_msginfo_from_to(session, item, first, last);
1256 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1261 tmp_msginfo_list = news_fetch_msginfo_from_to(session, item, first, last);
1262 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1264 g_slist_free(tmp_msgnum_list);
1266 return msginfo_list;