0.9.0claws61
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <time.h>
33
34 #include "intl.h"
35 #include "news.h"
36 #include "nntp.h"
37 #include "socket.h"
38 #include "recv.h"
39 #include "procmsg.h"
40 #include "procheader.h"
41 #include "folder.h"
42 #include "session.h"
43 #include "statusbar.h"
44 #include "codeconv.h"
45 #include "utils.h"
46 #include "prefs_common.h"
47 #include "prefs_account.h"
48 #include "inputdialog.h"
49 #include "log.h"
50 #include "progressindicator.h"
51 #if USE_OPENSSL
52 #  include "ssl.h"
53 #endif
54
55 #define NNTP_PORT       119
56 #if USE_OPENSSL
57 #define NNTPS_PORT      563
58 #endif
59
60 static Folder *news_folder_new(const gchar * name, const gchar * folder);
61 static void news_folder_destroy(Folder * folder);
62
63 static gchar *news_fetch_msg(Folder * folder, FolderItem * item, gint num);
64
65 static void news_folder_init             (Folder        *folder,
66                                           const gchar   *name,
67                                           const gchar   *path);
68
69 #if USE_OPENSSL
70 static Session *news_session_new         (const gchar   *server,
71                                           gushort        port,
72                                           const gchar   *userid,
73                                           const gchar   *passwd,
74                                           SSLType        ssl_type);
75 #else
76 static Session *news_session_new         (const gchar   *server,
77                                           gushort        port,
78                                           const gchar   *userid,
79                                           const gchar   *passwd);
80 #endif
81
82 static gint news_get_article_cmd         (NNTPSession   *session,
83                                           const gchar   *cmd,
84                                           gint           num,
85                                           gchar         *filename);
86 static gint news_get_article             (NNTPSession   *session,
87                                           gint           num,
88                                           gchar         *filename);
89
90 static gint news_select_group            (NNTPSession   *session,
91                                           const gchar   *group,
92                                           gint          *num,
93                                           gint          *first,
94                                           gint          *last);
95 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
96 static gchar *news_parse_xhdr            (const gchar   *xhdr_str,
97                                           MsgInfo       *msginfo);
98 gint news_get_num_list                   (Folder        *folder, 
99                                           FolderItem    *item,
100                                           GSList       **list);
101 MsgInfo *news_get_msginfo                (Folder        *folder, 
102                                           FolderItem    *item,
103                                           gint           num);
104 GSList *news_get_msginfos                (Folder        *folder,
105                                           FolderItem    *item,
106                                           GSList        *msgnum_list);
107
108 gint news_post_stream                    (Folder        *folder, 
109                                           FILE          *fp);
110
111 FolderClass news_class =
112 {
113         F_NEWS,
114         "news",
115         "News",
116
117         /* Folder functions */
118         news_folder_new,
119         news_folder_destroy,
120         NULL,
121         NULL,
122
123         /* FolderItem functions */
124         NULL,
125         NULL,
126         NULL,
127         NULL,
128         NULL,
129         news_get_num_list,
130         NULL,
131         NULL,
132         NULL,
133         NULL,
134
135         /* Message functions */
136         news_get_msginfo,
137         news_get_msginfos,
138         news_fetch_msg,
139         NULL,
140         NULL,
141         NULL,
142         NULL,
143         NULL,
144         NULL,
145 };
146
147 FolderClass *news_get_class(void)
148 {
149         return &news_class;
150 }
151
152 Folder *news_folder_new(const gchar *name, const gchar *path)
153 {
154         Folder *folder;
155
156         folder = (Folder *)g_new0(NewsFolder, 1);
157         folder->klass = &news_class;
158         news_folder_init(folder, name, path);
159
160         return folder;
161 }
162
163 void news_folder_destroy(Folder *folder)
164 {
165         gchar *dir;
166
167         dir = folder_get_path(folder);
168         if (is_dir_exist(dir))
169                 remove_dir_recursive(dir);
170         g_free(dir);
171
172         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
173 }
174
175 static void news_folder_init(Folder *folder, const gchar *name,
176                              const gchar *path)
177 {
178         folder_remote_folder_init(folder, name, path);
179 }
180
181 #if USE_OPENSSL
182 static Session *news_session_new(const gchar *server, gushort port,
183                                  const gchar *userid, const gchar *passwd,
184                                  SSLType ssl_type)
185 #else
186 static Session *news_session_new(const gchar *server, gushort port,
187                                  const gchar *userid, const gchar *passwd)
188 #endif
189 {
190         gchar buf[NNTPBUFSIZE];
191         Session *session;
192
193         g_return_val_if_fail(server != NULL, NULL);
194
195         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
196
197 #if USE_OPENSSL
198         session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
199 #else
200         session = nntp_session_new(server, port, buf, userid, passwd);
201 #endif
202
203         return session;
204 }
205
206 static Session *news_session_new_for_folder(Folder *folder)
207 {
208         Session *session;
209         PrefsAccount *ac;
210         const gchar *userid = NULL;
211         gchar *passwd = NULL;
212         gushort port;
213         gchar buf[NNTPBUFSIZE];
214
215         g_return_val_if_fail(folder != NULL, NULL);
216         g_return_val_if_fail(folder->account != NULL, NULL);
217
218         ac = folder->account;
219         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
220                 userid = ac->userid;
221                 if (ac->passwd && ac->passwd[0])
222                         passwd = g_strdup(ac->passwd);
223                 else
224                         passwd = input_dialog_query_password(ac->nntp_server,
225                                                              userid);
226         }
227
228 #if USE_OPENSSL
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,
232                                    ac->ssl_nntp);
233 #else
234         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
235         session = news_session_new(ac->nntp_server, port, userid, passwd);
236 #endif
237         if ((session != NULL) && ac->use_nntp_auth && ac->use_nntp_auth_onconnect)
238                 nntp_forceauth(NNTP_SESSION(session), buf, userid, passwd);
239
240         g_free(passwd);
241
242         return session;
243 }
244
245 NNTPSession *news_session_get(Folder *folder)
246 {
247         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
248
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);
252
253         if (!rfolder->session) {
254                 rfolder->session = news_session_new_for_folder(folder);
255                 return NNTP_SESSION(rfolder->session);
256         }
257
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);
261         }
262
263         if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
264             != NN_SUCCESS) {
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);
272         }
273
274         if (rfolder->session)
275                 rfolder->session->last_access_time = time(NULL);
276         return NNTP_SESSION(rfolder->session);
277 }
278
279 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
280 {
281         gchar *path, *filename;
282         NNTPSession *session;
283         gint ok;
284
285         g_return_val_if_fail(folder != NULL, NULL);
286         g_return_val_if_fail(item != NULL, NULL);
287
288         path = folder_item_get_path(item);
289         if (!is_dir_exist(path))
290                 make_dir_hier(path);
291         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
292         g_free(path);
293
294         if (is_file_exist(filename)) {
295                 debug_print("article %d has been already cached.\n", num);
296                 return filename;
297         }
298
299         session = news_session_get(folder);
300         if (!session) {
301                 g_free(filename);
302                 return NULL;
303         }
304
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);
308                 g_free(filename);
309                 return NULL;
310         }
311
312         debug_print("getting article %d...\n", num);
313         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
314                               num, filename);
315         if (ok < 0) {
316                 g_warning("can't read article %d\n", num);
317                 session_destroy(SESSION(session));
318                 REMOTE_FOLDER(folder)->session = NULL;
319                 g_free(filename);
320                 return NULL;
321         }
322
323         return filename;
324 }
325
326 static NewsGroupInfo *news_group_info_new(const gchar *name,
327                                           gint first, gint last, gchar type)
328 {
329         NewsGroupInfo *ginfo;
330
331         ginfo = g_new(NewsGroupInfo, 1);
332         ginfo->name = g_strdup(name);
333         ginfo->first = first;
334         ginfo->last = last;
335         ginfo->type = type;
336
337         return ginfo;
338 }
339
340 static void news_group_info_free(NewsGroupInfo *ginfo)
341 {
342         g_free(ginfo->name);
343         g_free(ginfo);
344 }
345
346 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
347                                     NewsGroupInfo *ginfo2)
348 {
349         return g_strcasecmp(ginfo1->name, ginfo2->name);
350 }
351
352 GSList *news_get_group_list(Folder *folder)
353 {
354         gchar *path, *filename;
355         FILE *fp;
356         GSList *list = NULL;
357         GSList *last = NULL;
358         gchar buf[NNTPBUFSIZE];
359
360         g_return_val_if_fail(folder != NULL, NULL);
361         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
362
363         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
364         if (!is_dir_exist(path))
365                 make_dir_hier(path);
366         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
367         g_free(path);
368
369         if ((fp = fopen(filename, "rb")) == NULL) {
370                 NNTPSession *session;
371
372                 session = news_session_get(folder);
373                 if (!session) {
374                         g_free(filename);
375                         return NULL;
376                 }
377
378                 if (nntp_list(session) != NN_SUCCESS) {
379                         g_free(filename);
380                         return NULL;
381                 }
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;
386                         g_free(filename);
387                         return NULL;
388                 }
389
390                 if ((fp = fopen(filename, "rb")) == NULL) {
391                         FILE_OP_ERROR(filename, "fopen");
392                         g_free(filename);
393                         return NULL;
394                 }
395         }
396
397         while (fgets(buf, sizeof(buf), fp) != NULL) {
398                 gchar *p = buf;
399                 gchar *name;
400                 gint last_num;
401                 gint first_num;
402                 gchar type;
403                 NewsGroupInfo *ginfo;
404
405                 p = strchr(p, ' ');
406                 if (!p) continue;
407                 *p = '\0';
408                 p++;
409                 name = buf;
410
411                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
412                         continue;
413
414                 ginfo = news_group_info_new(name, first_num, last_num, type);
415
416                 if (!last)
417                         last = list = g_slist_append(NULL, ginfo);
418                 else {
419                         last = g_slist_append(last, ginfo);
420                         last = last->next;
421                 }
422         }
423
424         fclose(fp);
425         g_free(filename);
426
427         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
428
429         return list;
430 }
431
432 void news_group_list_free(GSList *group_list)
433 {
434         GSList *cur;
435
436         if (!group_list) return;
437
438         for (cur = group_list; cur != NULL; cur = cur->next)
439                 news_group_info_free((NewsGroupInfo *)cur->data);
440         g_slist_free(group_list);
441 }
442
443 void news_remove_group_list_cache(Folder *folder)
444 {
445         gchar *path, *filename;
446
447         g_return_if_fail(folder != NULL);
448         g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
449
450         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
451         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
452         g_free(path);
453
454         if (is_file_exist(filename)) {
455                 if (remove(filename) < 0)
456                         FILE_OP_ERROR(filename, "remove");
457         }
458         g_free(filename);
459 }
460
461 gint news_post(Folder *folder, const gchar *file)
462 {
463         FILE *fp;
464         gint ok;
465
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);
469
470         if ((fp = fopen(file, "rb")) == NULL) {
471                 FILE_OP_ERROR(file, "fopen");
472                 return -1;
473         }
474
475         ok = news_post_stream(folder, fp);
476
477         fclose(fp);
478
479         return ok;
480 }
481
482 gint news_post_stream(Folder *folder, FILE *fp)
483 {
484         NNTPSession *session;
485         gint ok;
486
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);
490
491         session = news_session_get(folder);
492         if (!session) return -1;
493
494         ok = nntp_post(session, fp);
495         if (ok != NN_SUCCESS) {
496                 log_warning("can't post article.\n");
497                 return -1;
498         }
499
500         return 0;
501 }
502
503 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
504                                  gint num, gchar *filename)
505 {
506         gchar *msgid;
507
508         if (nntp_get_article(session, cmd, num, &msgid)
509             != NN_SUCCESS)
510                 return -1;
511
512         debug_print("Message-Id = %s, num = %d\n", msgid, num);
513         g_free(msgid);
514
515         if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
516                 log_warning("can't retrieve article %d\n", num);
517                 return -1;
518         }
519
520         return 0;
521 }
522
523 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
524 {
525         return news_get_article_cmd(session, "ARTICLE", num, filename);
526 }
527
528 /**
529  * news_select_group:
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.
535  *
536  * Select newsgroup @group with the GROUP command if it is not already
537  * selected in @session, or article numbers need to be returned.
538  *
539  * Return value: NNTP result code.
540  **/
541 static gint news_select_group(NNTPSession *session, const gchar *group,
542                               gint *num, gint *first, gint *last)
543 {
544         gint ok;
545         gint num_, first_, last_;
546
547         if (!num || !first || !last) {
548                 if (session->group && g_strcasecmp(session->group, group) == 0)
549                         return NN_SUCCESS;
550                 num = &num_;
551                 first = &first_;
552                 last = &last_;
553         }
554
555         g_free(session->group);
556         session->group = NULL;
557
558         ok = nntp_group(session, group, num, first, last);
559         if (ok == NN_SUCCESS)
560                 session->group = g_strdup(group);
561
562         return ok;
563 }
564
565 #define PARSE_ONE_PARAM(p, srcp) \
566 { \
567         p = strchr(srcp, '\t'); \
568         if (!p) return NULL; \
569         else \
570                 *p++ = '\0'; \
571 }
572
573 static MsgInfo *news_parse_xover(const gchar *xover_str)
574 {
575         MsgInfo *msginfo;
576         gchar buf[NNTPBUFSIZE];
577         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
578         gchar *p;
579         gint num, size_int, line_int;
580         gchar *xover_buf;
581
582         Xstrdup_a(xover_buf, xover_str, return NULL);
583
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);
592
593         tmp = strchr(xref, '\t');
594         if (!tmp) tmp = strchr(line, '\r');
595         if (!tmp) tmp = strchr(line, '\n');
596         if (tmp) *tmp = '\0';
597
598         num = atoi(xover_str);
599         size_int = atoi(size);
600         line_int = atoi(line);
601
602         /* set MsgInfo */
603         msginfo = procmsg_msginfo_new();
604         msginfo->msgnum = num;
605         msginfo->size = size_int;
606
607         msginfo->date = g_strdup(date);
608         msginfo->date_t = procheader_date_parse(NULL, date, 0);
609
610         conv_unmime_header(buf, sizeof(buf), sender, NULL);
611         msginfo->from = g_strdup(buf);
612         msginfo->fromname = procheader_get_fromname(buf);
613
614         conv_unmime_header(buf, sizeof(buf), subject, NULL);
615         msginfo->subject = g_strdup(buf);
616
617         extract_parenthesis(msgid, '<', '>');
618         remove_space(msgid);
619         if (*msgid != '\0')
620                 msginfo->msgid = g_strdup(msgid);
621
622         msginfo->references = g_strdup(ref);
623         eliminate_parenthesis(ref, '(', ')');
624         if ((p = strrchr(ref, '<')) != NULL) {
625                 extract_parenthesis(p, '<', '>');
626                 remove_space(p);
627                 if (*p != '\0')
628                         msginfo->inreplyto = g_strdup(p);
629         }
630
631         msginfo->xref = g_strdup(xref);
632         p = msginfo->xref+strlen(msginfo->xref) - 1;
633         while (*p == '\r' || *p == '\n') {
634                 *p = '\0';
635                 p--;
636         }
637
638         return msginfo;
639 }
640
641 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
642 {
643         gchar *p;
644         gchar *tmp;
645         gint num;
646
647         p = strchr(xhdr_str, ' ');
648         if (!p)
649                 return NULL;
650         else
651                 p++;
652
653         num = atoi(xhdr_str);
654         if (msginfo->msgnum != num) return NULL;
655
656         tmp = strchr(p, '\r');
657         if (!tmp) tmp = strchr(p, '\n');
658
659         if (tmp)
660                 return g_strndup(p, tmp - p);
661         else
662                 return g_strdup(p);
663 }
664
665 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
666 {
667         gchar * tmp;
668         FILE * tmpfp;
669         gchar buf[BUFFSIZE];
670
671         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
672                               G_DIR_SEPARATOR, (gint)msginfo);
673         if (tmp == NULL)
674                 return -1;
675
676         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
677                 FILE_OP_ERROR(tmp, "fopen");
678                 return -1;
679         }
680         if (change_file_mode_rw(tmpfp, tmp) < 0) {
681                 FILE_OP_ERROR(tmp, "chmod");
682                 g_warning("can't change file mode\n");
683         }
684         
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");
695
696         fclose(tmpfp);
697
698         news_post(folder, tmp);
699         remove(tmp);
700
701         g_free(tmp);
702
703         return 0;
704 }
705
706 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list)
707 {
708         NNTPSession *session;
709         gint i, ok, num, first, last, nummsgs = 0;
710         gchar *dir;
711
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);
715
716         session = news_session_get(folder);
717         g_return_val_if_fail(session != NULL, -1);
718
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);
722                 return -1;
723         }
724
725         if(last < first) {
726                 log_warning(_("invalid article range: %d - %d\n"),
727                             first, last);
728                 return 0;
729         }
730
731         for(i = first; i <= last; i++) {
732                 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
733                 nummsgs++;
734         }
735
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);
739         g_free(dir);
740
741         return nummsgs;
742 }
743
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); \
748                         return msginfo; \
749                 } \
750         }
751
752 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
753 {
754         NNTPSession *session;
755         MsgInfo *msginfo = NULL;
756         gchar buf[NNTPBUFSIZE];
757
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);
763
764         log_message(_("getting xover %d in %s...\n"),
765                     num, item->path);
766         if (nntp_xover(session, num, num) != NN_SUCCESS) {
767                 log_warning(_("can't get xover\n"));
768                 return NULL;
769         }
770         
771         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
772                 log_warning(_("error occurred while getting xover.\n"));
773                 return NULL;
774         }
775         
776         msginfo = news_parse_xover(buf);
777         if (!msginfo) {
778                 log_warning(_("invalid xover line: %s\n"), buf);
779         }
780
781         READ_TO_LISTEND("xover");
782
783         if(!msginfo)
784                 return NULL;
785         
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);
790
791         if (nntp_xhdr(session, "to", num, num) != NN_SUCCESS) {
792                 log_warning(_("can't get xhdr\n"));
793                 return msginfo;
794         }
795
796         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
797                 log_warning(_("error occurred while getting xhdr.\n"));
798                 return msginfo;
799         }
800
801         msginfo->to = news_parse_xhdr(buf, msginfo);
802
803         READ_TO_LISTEND("xhdr (to)");
804
805         if (nntp_xhdr(session, "cc", num, num) != NN_SUCCESS) {
806                 log_warning(_("can't get xhdr\n"));
807                 return msginfo;
808         }
809
810         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
811                 log_warning(_("error occurred while getting xhdr.\n"));
812                 return msginfo;
813         }
814
815         msginfo->cc = news_parse_xhdr(buf, msginfo);
816
817         READ_TO_LISTEND("xhdr (cc)");
818
819         return msginfo;
820 }
821
822 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
823 {
824         gchar buf[NNTPBUFSIZE];
825         GSList *newlist = NULL;
826         GSList *llast = NULL;
827         MsgInfo *msginfo;
828         guint count = 0, lines = (end - begin + 2) * 3;
829
830         g_return_val_if_fail(session != NULL, NULL);
831         g_return_val_if_fail(item != NULL, NULL);
832
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"));
837                 return NULL;
838         }
839
840         for (;;) {
841                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
842                         log_warning(_("error occurred while getting xover.\n"));
843                         return newlist;
844                 }
845                 count++;
846                 progressindicator_set_percentage
847                         (PROGRESS_TYPE_NETWORK,
848                          session->fetch_base_percentage +
849                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
850
851                 if (buf[0] == '.' && buf[1] == '\r') break;
852
853                 msginfo = news_parse_xover(buf);
854                 if (!msginfo) {
855                         log_warning(_("invalid xover line: %s\n"), buf);
856                         continue;
857                 }
858
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);
863
864                 if (!newlist)
865                         llast = newlist = g_slist_append(newlist, msginfo);
866                 else {
867                         llast = g_slist_append(llast, msginfo);
868                         llast = llast->next;
869                 }
870         }
871
872         if (nntp_xhdr(session, "to", begin, end) != NN_SUCCESS) {
873                 log_warning(_("can't get xhdr\n"));
874                 return newlist;
875         }
876
877         llast = newlist;
878
879         for (;;) {
880                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
881                         log_warning(_("error occurred while getting xhdr.\n"));
882                         return newlist;
883                 }
884                 count++;
885                 progressindicator_set_percentage
886                         (PROGRESS_TYPE_NETWORK,
887                          session->fetch_base_percentage +
888                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
889
890                 if (buf[0] == '.' && buf[1] == '\r') break;
891                 if (!llast) {
892                         g_warning("llast == NULL\n");
893                         continue;
894                 }
895
896                 msginfo = (MsgInfo *)llast->data;
897                 msginfo->to = news_parse_xhdr(buf, msginfo);
898
899                 llast = llast->next;
900         }
901
902         if (nntp_xhdr(session, "cc", begin, end) != NN_SUCCESS) {
903                 log_warning(_("can't get xhdr\n"));
904                 return newlist;
905         }
906
907         llast = newlist;
908
909         for (;;) {
910                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
911                         log_warning(_("error occurred while getting xhdr.\n"));
912                         return newlist;
913                 }
914                 count++;
915                 progressindicator_set_percentage
916                         (PROGRESS_TYPE_NETWORK,
917                          session->fetch_base_percentage +
918                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
919
920                 if (buf[0] == '.' && buf[1] == '\r') break;
921                 if (!llast) {
922                         g_warning("llast == NULL\n");
923                         continue;
924                 }
925
926                 msginfo = (MsgInfo *)llast->data;
927                 msginfo->cc = news_parse_xhdr(buf, msginfo);
928
929                 llast = llast->next;
930         }
931
932         return newlist;
933 }
934
935 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
936 {
937         NNTPSession *session;
938         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
939         guint first, last, next;
940         guint tofetch, fetched;
941         
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);
946         
947         session = news_session_get(folder);
948         g_return_val_if_fail(session != NULL, NULL);
949
950         tmp_msgnum_list = g_slist_copy(msgnum_list);
951         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
952
953         progressindicator_start(PROGRESS_TYPE_NETWORK);
954         tofetch = g_slist_length(tmp_msgnum_list);
955         fetched = 0;
956
957         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
958         last = first;
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;
967                         first = next;
968                 }
969                 last = next;
970         }
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);
975
976         g_slist_free(tmp_msgnum_list);
977         
978         progressindicator_stop(PROGRESS_TYPE_NETWORK);
979
980         return msginfo_list;
981 }