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;
400 path = folder_item_get_path(item);
401 if (path && is_dir_exist(path)) {
402 procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
407 if (max < first || last < min)
408 new = unread = total = num;
412 else if (first < min) {
419 else if (max < last) {
421 unread += last - max;
424 if (new > num) new = num;
425 if (unread > num) unread = num;
429 item->unread = unread;
431 item->last_num = last;
436 static NewsGroupInfo *news_group_info_new(const gchar *name,
437 gint first, gint last, gchar type)
439 NewsGroupInfo *ginfo;
441 ginfo = g_new(NewsGroupInfo, 1);
442 ginfo->name = g_strdup(name);
443 ginfo->first = first;
450 static void news_group_info_free(NewsGroupInfo *ginfo)
456 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
457 NewsGroupInfo *ginfo2)
459 return g_strcasecmp(ginfo1->name, ginfo2->name);
462 GSList *news_get_group_list(Folder *folder)
464 gchar *path, *filename;
468 gchar buf[NNTPBUFSIZE];
470 g_return_val_if_fail(folder != NULL, NULL);
471 g_return_val_if_fail(folder->type == F_NEWS, NULL);
473 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
474 if (!is_dir_exist(path))
476 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
479 if ((fp = fopen(filename, "rb")) == NULL) {
480 NNTPSession *session;
482 session = news_session_get(folder);
488 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
494 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
495 log_warning(_("can't retrieve newsgroup list\n"));
496 session_destroy(SESSION(session));
497 REMOTE_FOLDER(folder)->session = NULL;
502 if ((fp = fopen(filename, "rb")) == NULL) {
503 FILE_OP_ERROR(filename, "fopen");
509 while (fgets(buf, sizeof(buf), fp) != NULL) {
515 NewsGroupInfo *ginfo;
523 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
526 ginfo = news_group_info_new(name, first_num, last_num, type);
529 last = list = g_slist_append(NULL, ginfo);
531 last = g_slist_append(last, ginfo);
539 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
546 void news_group_list_free(GSList *group_list)
550 if (!group_list) return;
552 for (cur = group_list; cur != NULL; cur = cur->next)
553 news_group_info_free((NewsGroupInfo *)cur->data);
554 g_slist_free(group_list);
557 void news_remove_group_list_cache(Folder *folder)
559 gchar *path, *filename;
561 g_return_if_fail(folder != NULL);
562 g_return_if_fail(folder->type == F_NEWS);
564 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
565 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
568 if (is_file_exist(filename)) {
569 if (remove(filename) < 0)
570 FILE_OP_ERROR(filename, "remove");
575 gint news_post(Folder *folder, const gchar *file)
577 NNTPSession *session;
581 g_return_val_if_fail(folder != NULL, -1);
582 g_return_val_if_fail(folder->type == F_NEWS, -1);
583 g_return_val_if_fail(file != NULL, -1);
585 session = news_session_get(folder);
586 if (!session) return -1;
588 if ((fp = fopen(file, "rb")) == NULL) {
589 FILE_OP_ERROR(file, "fopen");
593 ok = nntp_post(session->nntp_sock, fp);
594 if (ok != NN_SUCCESS) {
595 log_warning(_("can't post article.\n"));
606 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
607 gint num, gchar *filename)
611 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
615 debug_print("Message-Id = %s, num = %d\n", msgid, num);
618 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
619 log_warning(_("can't retrieve article %d\n"), num);
626 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
630 MsgFlags msgflags = { 0, 0 };
633 filename = folder_item_fetch_msg(item, num);
634 if (filename == NULL)
637 msginfo = procheader_parse_file(filename, msgflags, FALSE, FALSE);
641 r = news_cancel_article(folder, msginfo);
643 procmsg_msginfo_free(msginfo);
648 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
650 return news_get_article_cmd(session, "ARTICLE", num, filename);
653 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
655 return news_get_article_cmd(session, "HEAD", num, filename);
660 * @session: Active NNTP session.
661 * @group: Newsgroup name.
662 * @num: Estimated number of articles.
663 * @first: First article number.
664 * @last: Last article number.
666 * Select newsgroup @group with the GROUP command if it is not already
667 * selected in @session, or article numbers need to be returned.
669 * Return value: NNTP result code.
671 static gint news_select_group(NNTPSession *session, const gchar *group,
672 gint *num, gint *first, gint *last)
675 gint num_, first_, last_;
677 if (!num || !first || !last) {
678 if (session->group && g_strcasecmp(session->group, group) == 0)
685 g_free(session->group);
686 session->group = NULL;
688 ok = nntp_group(session->nntp_sock, group, num, first, last);
689 if (ok == NN_SUCCESS)
690 session->group = g_strdup(group);
695 static GSList *news_get_uncached_articles(NNTPSession *session,
696 FolderItem *item, gint cache_last,
697 gint *rfirst, gint *rlast)
700 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
701 gchar buf[NNTPBUFSIZE];
702 GSList *newlist = NULL;
703 GSList *llast = NULL;
706 if (rfirst) *rfirst = 0;
707 if (rlast) *rlast = 0;
709 g_return_val_if_fail(session != NULL, NULL);
710 g_return_val_if_fail(item != NULL, NULL);
711 g_return_val_if_fail(item->folder != NULL, NULL);
712 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
714 ok = news_select_group(session, item->path, &num, &first, &last);
715 if (ok != NN_SUCCESS) {
716 log_warning(_("can't set group: %s\n"), item->path);
720 /* calculate getting overview range */
722 log_warning(_("invalid article range: %d - %d\n"),
726 if (cache_last < first)
728 else if (last < cache_last)
730 else if (last == cache_last) {
731 debug_print(_("no new articles.\n"));
734 begin = cache_last + 1;
737 if (rfirst) *rfirst = first;
738 if (rlast) *rlast = last;
740 if (prefs_common.max_articles > 0 &&
741 end - begin + 1 > prefs_common.max_articles)
742 begin = end - prefs_common.max_articles + 1;
744 log_message(_("getting xover %d - %d in %s...\n"),
745 begin, end, item->path);
746 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
747 log_warning(_("can't get xover\n"));
752 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
753 log_warning(_("error occurred while getting xover.\n"));
757 if (buf[0] == '.' && buf[1] == '\r') break;
759 msginfo = news_parse_xover(buf);
761 log_warning(_("invalid xover line: %s\n"), buf);
765 msginfo->folder = item;
766 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
767 msginfo->flags.tmp_flags = MSG_NEWS;
768 msginfo->newsgroups = g_strdup(item->path);
771 llast = newlist = g_slist_append(newlist, msginfo);
773 llast = g_slist_append(llast, msginfo);
778 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
779 log_warning(_("can't get xhdr\n"));
786 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
787 log_warning(_("error occurred while getting xhdr.\n"));
791 if (buf[0] == '.' && buf[1] == '\r') break;
793 g_warning("llast == NULL\n");
797 msginfo = (MsgInfo *)llast->data;
798 msginfo->to = news_parse_xhdr(buf, msginfo);
803 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
804 log_warning(_("can't get xhdr\n"));
811 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
812 log_warning(_("error occurred while getting xhdr.\n"));
816 if (buf[0] == '.' && buf[1] == '\r') break;
818 g_warning("llast == NULL\n");
822 msginfo = (MsgInfo *)llast->data;
823 msginfo->cc = news_parse_xhdr(buf, msginfo);
831 #define PARSE_ONE_PARAM(p, srcp) \
833 p = strchr(srcp, '\t'); \
834 if (!p) return NULL; \
839 static MsgInfo *news_parse_xover(const gchar *xover_str)
842 gchar buf[NNTPBUFSIZE];
843 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
845 gint num, size_int, line_int;
848 Xstrdup_a(xover_buf, xover_str, return NULL);
850 PARSE_ONE_PARAM(subject, xover_buf);
851 PARSE_ONE_PARAM(sender, subject);
852 PARSE_ONE_PARAM(date, sender);
853 PARSE_ONE_PARAM(msgid, date);
854 PARSE_ONE_PARAM(ref, msgid);
855 PARSE_ONE_PARAM(size, ref);
856 PARSE_ONE_PARAM(line, size);
857 PARSE_ONE_PARAM(xref, line);
859 tmp = strchr(xref, '\t');
860 if (!tmp) tmp = strchr(line, '\r');
861 if (!tmp) tmp = strchr(line, '\n');
862 if (tmp) *tmp = '\0';
864 num = atoi(xover_str);
865 size_int = atoi(size);
866 line_int = atoi(line);
869 msginfo = procmsg_msginfo_new();
870 msginfo->msgnum = num;
871 msginfo->size = size_int;
873 msginfo->date = g_strdup(date);
874 msginfo->date_t = procheader_date_parse(NULL, date, 0);
876 conv_unmime_header(buf, sizeof(buf), sender, NULL);
877 msginfo->from = g_strdup(buf);
878 msginfo->fromname = procheader_get_fromname(buf);
880 conv_unmime_header(buf, sizeof(buf), subject, NULL);
881 msginfo->subject = g_strdup(buf);
883 extract_parenthesis(msgid, '<', '>');
886 msginfo->msgid = g_strdup(msgid);
888 msginfo->references = g_strdup(ref);
889 eliminate_parenthesis(ref, '(', ')');
890 if ((p = strrchr(ref, '<')) != NULL) {
891 extract_parenthesis(p, '<', '>');
894 msginfo->inreplyto = g_strdup(p);
897 msginfo->xref = g_strdup(xref);
898 p = msginfo->xref+strlen(msginfo->xref) - 1;
899 while (*p == '\r' || *p == '\n') {
907 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
913 p = strchr(xhdr_str, ' ');
919 num = atoi(xhdr_str);
920 if (msginfo->msgnum != num) return NULL;
922 tmp = strchr(p, '\r');
923 if (!tmp) tmp = strchr(p, '\n');
926 return g_strndup(p, tmp - p);
931 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
938 g_return_val_if_fail(item != NULL, alist);
939 g_return_val_if_fail(item->folder != NULL, alist);
940 g_return_val_if_fail(item->folder->type == F_NEWS, alist);
942 if (first < 2) return alist;
944 debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
946 dir = folder_item_get_path(item);
947 remove_numbered_files(dir, 1, first - 1);
950 for (cur = alist; cur != NULL; ) {
953 msginfo = (MsgInfo *)cur->data;
954 if (msginfo && msginfo->msgnum < first) {
955 procmsg_msginfo_free(msginfo);
956 alist = g_slist_remove(alist, msginfo);
965 static void news_delete_all_articles(FolderItem *item)
969 g_return_if_fail(item != NULL);
970 g_return_if_fail(item->folder != NULL);
971 g_return_if_fail(item->folder->type == F_NEWS);
973 debug_print(_("\tDeleting all cached articles... "));
975 dir = folder_item_get_path(item);
976 remove_all_numbered_files(dir);
979 debug_print(_("done.\n"));
982 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
988 tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
989 G_DIR_SEPARATOR, (gint)msginfo);
993 if ((tmpfp = fopen(tmp, "wb")) == NULL) {
994 FILE_OP_ERROR(tmp, "fopen");
997 if (change_file_mode_rw(tmpfp, tmp) < 0) {
998 FILE_OP_ERROR(tmp, "chmod");
999 g_warning(_("can't change file mode\n"));
1002 fprintf(tmpfp, "From: %s\r\n", msginfo->from);
1003 fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
1004 fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
1005 fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
1006 fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
1007 fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
1008 get_rfc822_date(buf, sizeof(buf));
1009 fprintf(tmpfp, "Date: %s\r\n", buf);
1010 fprintf(tmpfp, "\r\n");
1011 fprintf(tmpfp, "removed with sylpheed\r\n");
1015 news_post(folder, tmp);
1023 GSList *news_get_num_list(Folder *folder, FolderItem *item)
1025 NNTPSession *session;
1026 gint i, ok, num, first, last;
1027 GSList *msgnum_list = NULL;
1029 session = news_session_get(folder);
1030 g_return_val_if_fail(session != NULL, NULL);
1031 g_return_val_if_fail(item != NULL, NULL);
1032 g_return_val_if_fail(item->folder != NULL, NULL);
1033 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1035 ok = news_select_group(session, item->path, &num, &first, &last);
1036 if (ok != NN_SUCCESS) {
1037 log_warning(_("can't set group: %s\n"), item->path);
1042 log_warning(_("invalid article range: %d - %d\n"),
1047 for(i = first; i <= last; i++) {
1048 msgnum_list = g_slist_prepend(msgnum_list, GINT_TO_POINTER(i));
1054 #define READ_TO_LISTEND(hdr) \
1055 while (!(buf[0] == '.' && buf[1] == '\r')) { \
1056 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
1057 log_warning(_("error occurred while getting " hdr ".\n")); \
1062 MsgInfo *news_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
1064 NNTPSession *session;
1065 MsgInfo *msginfo = NULL;
1066 gchar buf[NNTPBUFSIZE];
1068 session = news_session_get(folder);
1069 g_return_val_if_fail(session != NULL, NULL);
1070 g_return_val_if_fail(item != NULL, NULL);
1071 g_return_val_if_fail(item->folder != NULL, NULL);
1072 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1074 log_message(_("getting xover %d in %s...\n"),
1076 if (nntp_xover(session->nntp_sock, num, num) != NN_SUCCESS) {
1077 log_warning(_("can't get xover\n"));
1081 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1082 log_warning(_("error occurred while getting xover.\n"));
1086 msginfo = news_parse_xover(buf);
1088 log_warning(_("invalid xover line: %s\n"), buf);
1091 READ_TO_LISTEND("xover");
1096 msginfo->folder = item;
1097 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1098 msginfo->flags.tmp_flags = MSG_NEWS;
1099 msginfo->newsgroups = g_strdup(item->path);
1101 if (nntp_xhdr(session->nntp_sock, "to", num, num) != NN_SUCCESS) {
1102 log_warning(_("can't get xhdr\n"));
1106 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1107 log_warning(_("error occurred while getting xhdr.\n"));
1111 msginfo->to = news_parse_xhdr(buf, msginfo);
1113 READ_TO_LISTEND("xhdr (to)");
1115 if (nntp_xhdr(session->nntp_sock, "cc", num, num) != NN_SUCCESS) {
1116 log_warning(_("can't get xhdr\n"));
1120 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1121 log_warning(_("error occurred while getting xhdr.\n"));
1125 msginfo->cc = news_parse_xhdr(buf, msginfo);
1127 READ_TO_LISTEND("xhdr (cc)");
1132 static GSList *news_fetch_msginfo_from_to(NNTPSession *session, FolderItem *item, guint begin, guint end)
1134 gchar buf[NNTPBUFSIZE];
1135 GSList *newlist = NULL;
1136 GSList *llast = NULL;
1139 g_return_val_if_fail(session != NULL, NULL);
1140 g_return_val_if_fail(item != NULL, NULL);
1142 log_message(_("getting xover %d - %d in %s...\n"),
1143 begin, end, item->path);
1144 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
1145 log_warning(_("can't get xover\n"));
1150 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1151 log_warning(_("error occurred while getting xover.\n"));
1155 if (buf[0] == '.' && buf[1] == '\r') break;
1157 msginfo = news_parse_xover(buf);
1159 log_warning(_("invalid xover line: %s\n"), buf);
1163 msginfo->folder = item;
1164 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1165 msginfo->flags.tmp_flags = MSG_NEWS;
1166 msginfo->newsgroups = g_strdup(item->path);
1169 llast = newlist = g_slist_append(newlist, msginfo);
1171 llast = g_slist_append(llast, msginfo);
1172 llast = llast->next;
1176 if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
1177 log_warning(_("can't get xhdr\n"));
1184 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1185 log_warning(_("error occurred while getting xhdr.\n"));
1189 if (buf[0] == '.' && buf[1] == '\r') break;
1191 g_warning("llast == NULL\n");
1195 msginfo = (MsgInfo *)llast->data;
1196 msginfo->to = news_parse_xhdr(buf, msginfo);
1198 llast = llast->next;
1201 if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
1202 log_warning(_("can't get xhdr\n"));
1209 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1210 log_warning(_("error occurred while getting xhdr.\n"));
1214 if (buf[0] == '.' && buf[1] == '\r') break;
1216 g_warning("llast == NULL\n");
1220 msginfo = (MsgInfo *)llast->data;
1221 msginfo->cc = news_parse_xhdr(buf, msginfo);
1223 llast = llast->next;
1229 gint news_fetch_msgnum_sort(gconstpointer a, gconstpointer b)
1231 return (GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b));
1234 GSList *news_fetch_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1236 NNTPSession *session;
1237 GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1238 guint first, last, next;
1240 g_return_val_if_fail(folder != NULL, NULL);
1241 g_return_val_if_fail(folder->type == F_NEWS, NULL);
1242 g_return_val_if_fail(msgnum_list != NULL, NULL);
1243 g_return_val_if_fail(item != NULL, NULL);
1245 session = news_session_get(folder);
1246 g_return_val_if_fail(session != NULL, NULL);
1248 tmp_msgnum_list = g_slist_copy(msgnum_list);
1249 tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, news_fetch_msgnum_sort);
1251 first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1253 for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1254 next = GPOINTER_TO_INT(elem->data);
1255 if(next != (last + 1)) {
1256 tmp_msginfo_list = news_fetch_msginfo_from_to(session, item, first, last);
1257 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1262 tmp_msginfo_list = news_fetch_msginfo_from_to(session, item, first, last);
1263 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1265 g_slist_free(tmp_msgnum_list);
1267 return msginfo_list;