* src/procmsg.c
[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 static gchar *news_folder_get_path       (Folder        *folder);
111 gchar *news_item_get_path                (Folder        *folder,
112                                           FolderItem    *item);
113
114 FolderClass news_class =
115 {
116         F_NEWS,
117         "news",
118         "News",
119
120         /* Folder functions */
121         news_folder_new,
122         news_folder_destroy,
123         NULL,
124         NULL,
125
126         /* FolderItem functions */
127         NULL,
128         NULL,
129         news_item_get_path,
130         NULL,
131         NULL,
132         NULL,
133         news_get_num_list,
134         NULL,
135         NULL,
136         NULL,
137         NULL,
138
139         /* Message functions */
140         news_get_msginfo,
141         news_get_msginfos,
142         news_fetch_msg,
143         NULL,
144         NULL,
145         NULL,
146         NULL,
147         NULL,
148         NULL,
149 };
150
151 FolderClass *news_get_class(void)
152 {
153         return &news_class;
154 }
155
156 Folder *news_folder_new(const gchar *name, const gchar *path)
157 {
158         Folder *folder;
159
160         folder = (Folder *)g_new0(NewsFolder, 1);
161         folder->klass = &news_class;
162         news_folder_init(folder, name, path);
163
164         return folder;
165 }
166
167 void news_folder_destroy(Folder *folder)
168 {
169         gchar *dir;
170
171         dir = news_folder_get_path(folder);
172         if (is_dir_exist(dir))
173                 remove_dir_recursive(dir);
174         g_free(dir);
175
176         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
177 }
178
179 static void news_folder_init(Folder *folder, const gchar *name,
180                              const gchar *path)
181 {
182         folder_remote_folder_init(folder, name, path);
183 }
184
185 #if USE_OPENSSL
186 static Session *news_session_new(const gchar *server, gushort port,
187                                  const gchar *userid, const gchar *passwd,
188                                  SSLType ssl_type)
189 #else
190 static Session *news_session_new(const gchar *server, gushort port,
191                                  const gchar *userid, const gchar *passwd)
192 #endif
193 {
194         gchar buf[NNTPBUFSIZE];
195         Session *session;
196
197         g_return_val_if_fail(server != NULL, NULL);
198
199         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
200
201 #if USE_OPENSSL
202         session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
203 #else
204         session = nntp_session_new(server, port, buf, userid, passwd);
205 #endif
206
207         return session;
208 }
209
210 static Session *news_session_new_for_folder(Folder *folder)
211 {
212         Session *session;
213         PrefsAccount *ac;
214         const gchar *userid = NULL;
215         gchar *passwd = NULL;
216         gushort port;
217         gchar buf[NNTPBUFSIZE];
218
219         g_return_val_if_fail(folder != NULL, NULL);
220         g_return_val_if_fail(folder->account != NULL, NULL);
221
222         ac = folder->account;
223         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
224                 userid = ac->userid;
225                 if (ac->passwd && ac->passwd[0])
226                         passwd = g_strdup(ac->passwd);
227                 else
228                         passwd = input_dialog_query_password(ac->nntp_server,
229                                                              userid);
230         }
231
232 #if USE_OPENSSL
233         port = ac->set_nntpport ? ac->nntpport
234                 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
235         session = news_session_new(ac->nntp_server, port, userid, passwd,
236                                    ac->ssl_nntp);
237 #else
238         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
239         session = news_session_new(ac->nntp_server, port, userid, passwd);
240 #endif
241         if ((session != NULL) && ac->use_nntp_auth && ac->use_nntp_auth_onconnect)
242                 nntp_forceauth(NNTP_SESSION(session), buf, userid, passwd);
243
244         g_free(passwd);
245
246         return session;
247 }
248
249 NNTPSession *news_session_get(Folder *folder)
250 {
251         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
252
253         g_return_val_if_fail(folder != NULL, NULL);
254         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
255         g_return_val_if_fail(folder->account != NULL, NULL);
256
257         if (!rfolder->session) {
258                 rfolder->session = news_session_new_for_folder(folder);
259                 return NNTP_SESSION(rfolder->session);
260         }
261
262         if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
263                 rfolder->session->last_access_time = time(NULL);
264                 return NNTP_SESSION(rfolder->session);
265         }
266
267         if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
268             != NN_SUCCESS) {
269                 log_warning("NNTP connection to %s:%d has been"
270                               " disconnected. Reconnecting...\n",
271                             folder->account->nntp_server,
272                             folder->account->set_nntpport ?
273                             folder->account->nntpport : NNTP_PORT);
274                 session_destroy(rfolder->session);
275                 rfolder->session = news_session_new_for_folder(folder);
276         }
277
278         if (rfolder->session)
279                 rfolder->session->last_access_time = time(NULL);
280         return NNTP_SESSION(rfolder->session);
281 }
282
283 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
284 {
285         gchar *path, *filename;
286         NNTPSession *session;
287         gint ok;
288
289         g_return_val_if_fail(folder != NULL, NULL);
290         g_return_val_if_fail(item != NULL, NULL);
291
292         path = folder_item_get_path(item);
293         if (!is_dir_exist(path))
294                 make_dir_hier(path);
295         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
296         g_free(path);
297
298         if (is_file_exist(filename)) {
299                 debug_print("article %d has been already cached.\n", num);
300                 return filename;
301         }
302
303         session = news_session_get(folder);
304         if (!session) {
305                 g_free(filename);
306                 return NULL;
307         }
308
309         ok = news_select_group(session, item->path, NULL, NULL, NULL);
310         if (ok != NN_SUCCESS) {
311                 g_warning("can't select group %s\n", item->path);
312                 g_free(filename);
313                 return NULL;
314         }
315
316         debug_print("getting article %d...\n", num);
317         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
318                               num, filename);
319         if (ok < 0) {
320                 g_warning("can't read article %d\n", num);
321                 session_destroy(SESSION(session));
322                 REMOTE_FOLDER(folder)->session = NULL;
323                 g_free(filename);
324                 return NULL;
325         }
326
327         return filename;
328 }
329
330 static NewsGroupInfo *news_group_info_new(const gchar *name,
331                                           gint first, gint last, gchar type)
332 {
333         NewsGroupInfo *ginfo;
334
335         ginfo = g_new(NewsGroupInfo, 1);
336         ginfo->name = g_strdup(name);
337         ginfo->first = first;
338         ginfo->last = last;
339         ginfo->type = type;
340
341         return ginfo;
342 }
343
344 static void news_group_info_free(NewsGroupInfo *ginfo)
345 {
346         g_free(ginfo->name);
347         g_free(ginfo);
348 }
349
350 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
351                                     NewsGroupInfo *ginfo2)
352 {
353         return g_strcasecmp(ginfo1->name, ginfo2->name);
354 }
355
356 GSList *news_get_group_list(Folder *folder)
357 {
358         gchar *path, *filename;
359         FILE *fp;
360         GSList *list = NULL;
361         GSList *last = NULL;
362         gchar buf[NNTPBUFSIZE];
363
364         g_return_val_if_fail(folder != NULL, NULL);
365         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
366
367         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
368         if (!is_dir_exist(path))
369                 make_dir_hier(path);
370         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
371         g_free(path);
372
373         if ((fp = fopen(filename, "rb")) == NULL) {
374                 NNTPSession *session;
375
376                 session = news_session_get(folder);
377                 if (!session) {
378                         g_free(filename);
379                         return NULL;
380                 }
381
382                 if (nntp_list(session) != NN_SUCCESS) {
383                         g_free(filename);
384                         return NULL;
385                 }
386                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
387                         log_warning("can't retrieve newsgroup list\n");
388                         session_destroy(SESSION(session));
389                         REMOTE_FOLDER(folder)->session = NULL;
390                         g_free(filename);
391                         return NULL;
392                 }
393
394                 if ((fp = fopen(filename, "rb")) == NULL) {
395                         FILE_OP_ERROR(filename, "fopen");
396                         g_free(filename);
397                         return NULL;
398                 }
399         }
400
401         while (fgets(buf, sizeof(buf), fp) != NULL) {
402                 gchar *p = buf;
403                 gchar *name;
404                 gint last_num;
405                 gint first_num;
406                 gchar type;
407                 NewsGroupInfo *ginfo;
408
409                 p = strchr(p, ' ');
410                 if (!p) continue;
411                 *p = '\0';
412                 p++;
413                 name = buf;
414
415                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
416                         continue;
417
418                 ginfo = news_group_info_new(name, first_num, last_num, type);
419
420                 if (!last)
421                         last = list = g_slist_append(NULL, ginfo);
422                 else {
423                         last = g_slist_append(last, ginfo);
424                         last = last->next;
425                 }
426         }
427
428         fclose(fp);
429         g_free(filename);
430
431         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
432
433         return list;
434 }
435
436 void news_group_list_free(GSList *group_list)
437 {
438         GSList *cur;
439
440         if (!group_list) return;
441
442         for (cur = group_list; cur != NULL; cur = cur->next)
443                 news_group_info_free((NewsGroupInfo *)cur->data);
444         g_slist_free(group_list);
445 }
446
447 void news_remove_group_list_cache(Folder *folder)
448 {
449         gchar *path, *filename;
450
451         g_return_if_fail(folder != NULL);
452         g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
453
454         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
455         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
456         g_free(path);
457
458         if (is_file_exist(filename)) {
459                 if (remove(filename) < 0)
460                         FILE_OP_ERROR(filename, "remove");
461         }
462         g_free(filename);
463 }
464
465 gint news_post(Folder *folder, const gchar *file)
466 {
467         FILE *fp;
468         gint ok;
469
470         g_return_val_if_fail(folder != NULL, -1);
471         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
472         g_return_val_if_fail(file != NULL, -1);
473
474         if ((fp = fopen(file, "rb")) == NULL) {
475                 FILE_OP_ERROR(file, "fopen");
476                 return -1;
477         }
478
479         ok = news_post_stream(folder, fp);
480
481         fclose(fp);
482
483         return ok;
484 }
485
486 gint news_post_stream(Folder *folder, FILE *fp)
487 {
488         NNTPSession *session;
489         gint ok;
490
491         g_return_val_if_fail(folder != NULL, -1);
492         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
493         g_return_val_if_fail(fp != NULL, -1);
494
495         session = news_session_get(folder);
496         if (!session) return -1;
497
498         ok = nntp_post(session, fp);
499         if (ok != NN_SUCCESS) {
500                 log_warning("can't post article.\n");
501                 return -1;
502         }
503
504         return 0;
505 }
506
507 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
508                                  gint num, gchar *filename)
509 {
510         gchar *msgid;
511
512         if (nntp_get_article(session, cmd, num, &msgid)
513             != NN_SUCCESS)
514                 return -1;
515
516         debug_print("Message-Id = %s, num = %d\n", msgid, num);
517         g_free(msgid);
518
519         if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
520                 log_warning("can't retrieve article %d\n", num);
521                 return -1;
522         }
523
524         return 0;
525 }
526
527 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
528 {
529         return news_get_article_cmd(session, "ARTICLE", num, filename);
530 }
531
532 /**
533  * news_select_group:
534  * @session: Active NNTP session.
535  * @group: Newsgroup name.
536  * @num: Estimated number of articles.
537  * @first: First article number.
538  * @last: Last article number.
539  *
540  * Select newsgroup @group with the GROUP command if it is not already
541  * selected in @session, or article numbers need to be returned.
542  *
543  * Return value: NNTP result code.
544  **/
545 static gint news_select_group(NNTPSession *session, const gchar *group,
546                               gint *num, gint *first, gint *last)
547 {
548         gint ok;
549         gint num_, first_, last_;
550
551         if (!num || !first || !last) {
552                 if (session->group && g_strcasecmp(session->group, group) == 0)
553                         return NN_SUCCESS;
554                 num = &num_;
555                 first = &first_;
556                 last = &last_;
557         }
558
559         g_free(session->group);
560         session->group = NULL;
561
562         ok = nntp_group(session, group, num, first, last);
563         if (ok == NN_SUCCESS)
564                 session->group = g_strdup(group);
565
566         return ok;
567 }
568
569 #define PARSE_ONE_PARAM(p, srcp) \
570 { \
571         p = strchr(srcp, '\t'); \
572         if (!p) return NULL; \
573         else \
574                 *p++ = '\0'; \
575 }
576
577 static MsgInfo *news_parse_xover(const gchar *xover_str)
578 {
579         MsgInfo *msginfo;
580         gchar buf[NNTPBUFSIZE];
581         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
582         gchar *p;
583         gint num, size_int, line_int;
584         gchar *xover_buf;
585
586         Xstrdup_a(xover_buf, xover_str, return NULL);
587
588         PARSE_ONE_PARAM(subject, xover_buf);
589         PARSE_ONE_PARAM(sender, subject);
590         PARSE_ONE_PARAM(date, sender);
591         PARSE_ONE_PARAM(msgid, date);
592         PARSE_ONE_PARAM(ref, msgid);
593         PARSE_ONE_PARAM(size, ref);
594         PARSE_ONE_PARAM(line, size);
595         PARSE_ONE_PARAM(xref, line);
596
597         tmp = strchr(xref, '\t');
598         if (!tmp) tmp = strchr(line, '\r');
599         if (!tmp) tmp = strchr(line, '\n');
600         if (tmp) *tmp = '\0';
601
602         num = atoi(xover_str);
603         size_int = atoi(size);
604         line_int = atoi(line);
605
606         /* set MsgInfo */
607         msginfo = procmsg_msginfo_new();
608         msginfo->msgnum = num;
609         msginfo->size = size_int;
610
611         msginfo->date = g_strdup(date);
612         msginfo->date_t = procheader_date_parse(NULL, date, 0);
613
614         conv_unmime_header(buf, sizeof(buf), sender, NULL);
615         msginfo->from = g_strdup(buf);
616         msginfo->fromname = procheader_get_fromname(buf);
617
618         conv_unmime_header(buf, sizeof(buf), subject, NULL);
619         msginfo->subject = g_strdup(buf);
620
621         extract_parenthesis(msgid, '<', '>');
622         remove_space(msgid);
623         if (*msgid != '\0')
624                 msginfo->msgid = g_strdup(msgid);
625
626         msginfo->references = g_strdup(ref);
627         eliminate_parenthesis(ref, '(', ')');
628         if ((p = strrchr(ref, '<')) != NULL) {
629                 extract_parenthesis(p, '<', '>');
630                 remove_space(p);
631                 if (*p != '\0')
632                         msginfo->inreplyto = g_strdup(p);
633         }
634
635         msginfo->xref = g_strdup(xref);
636         p = msginfo->xref+strlen(msginfo->xref) - 1;
637         while (*p == '\r' || *p == '\n') {
638                 *p = '\0';
639                 p--;
640         }
641
642         return msginfo;
643 }
644
645 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
646 {
647         gchar *p;
648         gchar *tmp;
649         gint num;
650
651         p = strchr(xhdr_str, ' ');
652         if (!p)
653                 return NULL;
654         else
655                 p++;
656
657         num = atoi(xhdr_str);
658         if (msginfo->msgnum != num) return NULL;
659
660         tmp = strchr(p, '\r');
661         if (!tmp) tmp = strchr(p, '\n');
662
663         if (tmp)
664                 return g_strndup(p, tmp - p);
665         else
666                 return g_strdup(p);
667 }
668
669 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
670 {
671         gchar * tmp;
672         FILE * tmpfp;
673         gchar buf[BUFFSIZE];
674
675         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
676                               G_DIR_SEPARATOR, (gint)msginfo);
677         if (tmp == NULL)
678                 return -1;
679
680         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
681                 FILE_OP_ERROR(tmp, "fopen");
682                 return -1;
683         }
684         if (change_file_mode_rw(tmpfp, tmp) < 0) {
685                 FILE_OP_ERROR(tmp, "chmod");
686                 g_warning("can't change file mode\n");
687         }
688         
689         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
690         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
691         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
692         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
693         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
694         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
695         get_rfc822_date(buf, sizeof(buf));
696         fprintf(tmpfp, "Date: %s\r\n", buf);
697         fprintf(tmpfp, "\r\n");
698         fprintf(tmpfp, "removed with sylpheed\r\n");
699
700         fclose(tmpfp);
701
702         news_post(folder, tmp);
703         remove(tmp);
704
705         g_free(tmp);
706
707         return 0;
708 }
709
710 static gchar *news_folder_get_path(Folder *folder)
711 {
712         gchar *folder_path;
713
714         g_return_val_if_fail(folder->account != NULL, NULL);
715
716         folder_path = g_strconcat(get_news_cache_dir(),
717                                   G_DIR_SEPARATOR_S,
718                                   folder->account->nntp_server,
719                                   NULL);
720         return folder_path;
721 }
722
723 gchar *news_item_get_path(Folder *folder, FolderItem *item)
724 {
725         gchar *folder_path, *path;
726
727         g_return_val_if_fail(folder != NULL, NULL);
728         g_return_val_if_fail(item != NULL, NULL);
729         folder_path = news_folder_get_path(folder);
730
731         g_return_val_if_fail(folder_path != NULL, NULL);
732         if (folder_path[0] == G_DIR_SEPARATOR) {
733                 if (item->path)
734                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
735                                            item->path, NULL);
736                 else
737                         path = g_strdup(folder_path);
738         } else {
739                 if (item->path)
740                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
741                                            folder_path, G_DIR_SEPARATOR_S,
742                                            item->path, NULL);
743                 else
744                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
745                                            folder_path, NULL);
746         }
747         g_free(folder_path);
748
749         return path;
750 }
751
752 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list)
753 {
754         NNTPSession *session;
755         gint i, ok, num, first, last, nummsgs = 0;
756         gchar *dir;
757
758         g_return_val_if_fail(item != NULL, -1);
759         g_return_val_if_fail(item->folder != NULL, -1);
760         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
761
762         session = news_session_get(folder);
763         g_return_val_if_fail(session != NULL, -1);
764
765         ok = news_select_group(session, item->path, &num, &first, &last);
766         if (ok != NN_SUCCESS) {
767                 log_warning(_("can't set group: %s\n"), item->path);
768                 return -1;
769         }
770
771         if(last < first) {
772                 log_warning(_("invalid article range: %d - %d\n"),
773                             first, last);
774                 return 0;
775         }
776
777         for(i = first; i <= last; i++) {
778                 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
779                 nummsgs++;
780         }
781
782         dir = folder_item_get_path(item);
783         debug_print("removing old messages from %d to %d in %s\n", first, last, dir);
784         remove_numbered_files(dir, 1, first - 1);
785         g_free(dir);
786
787         return nummsgs;
788 }
789
790 #define READ_TO_LISTEND(hdr) \
791         while (!(buf[0] == '.' && buf[1] == '\r')) { \
792                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
793                         log_warning(_("error occurred while getting %s.\n"), hdr); \
794                         return msginfo; \
795                 } \
796         }
797
798 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
799 {
800         NNTPSession *session;
801         MsgInfo *msginfo = NULL;
802         gchar buf[NNTPBUFSIZE];
803
804         session = news_session_get(folder);
805         g_return_val_if_fail(session != NULL, NULL);
806         g_return_val_if_fail(item != NULL, NULL);
807         g_return_val_if_fail(item->folder != NULL, NULL);
808         g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
809
810         log_message(_("getting xover %d in %s...\n"),
811                     num, item->path);
812         if (nntp_xover(session, num, num) != NN_SUCCESS) {
813                 log_warning(_("can't get xover\n"));
814                 return NULL;
815         }
816         
817         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
818                 log_warning(_("error occurred while getting xover.\n"));
819                 return NULL;
820         }
821         
822         msginfo = news_parse_xover(buf);
823         if (!msginfo) {
824                 log_warning(_("invalid xover line: %s\n"), buf);
825         }
826
827         READ_TO_LISTEND("xover");
828
829         if(!msginfo)
830                 return NULL;
831         
832         msginfo->folder = item;
833         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
834         msginfo->flags.tmp_flags = MSG_NEWS;
835         msginfo->newsgroups = g_strdup(item->path);
836
837         if (nntp_xhdr(session, "to", num, num) != NN_SUCCESS) {
838                 log_warning(_("can't get xhdr\n"));
839                 return msginfo;
840         }
841
842         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
843                 log_warning(_("error occurred while getting xhdr.\n"));
844                 return msginfo;
845         }
846
847         msginfo->to = news_parse_xhdr(buf, msginfo);
848
849         READ_TO_LISTEND("xhdr (to)");
850
851         if (nntp_xhdr(session, "cc", num, num) != NN_SUCCESS) {
852                 log_warning(_("can't get xhdr\n"));
853                 return msginfo;
854         }
855
856         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
857                 log_warning(_("error occurred while getting xhdr.\n"));
858                 return msginfo;
859         }
860
861         msginfo->cc = news_parse_xhdr(buf, msginfo);
862
863         READ_TO_LISTEND("xhdr (cc)");
864
865         return msginfo;
866 }
867
868 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
869 {
870         gchar buf[NNTPBUFSIZE];
871         GSList *newlist = NULL;
872         GSList *llast = NULL;
873         MsgInfo *msginfo;
874         guint count = 0, lines = (end - begin + 2) * 3;
875
876         g_return_val_if_fail(session != NULL, NULL);
877         g_return_val_if_fail(item != NULL, NULL);
878
879         log_message(_("getting xover %d - %d in %s...\n"),
880                     begin, end, item->path);
881         if (nntp_xover(session, begin, end) != NN_SUCCESS) {
882                 log_warning(_("can't get xover\n"));
883                 return NULL;
884         }
885
886         for (;;) {
887                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
888                         log_warning(_("error occurred while getting xover.\n"));
889                         return newlist;
890                 }
891                 count++;
892                 progressindicator_set_percentage
893                         (PROGRESS_TYPE_NETWORK,
894                          session->fetch_base_percentage +
895                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
896
897                 if (buf[0] == '.' && buf[1] == '\r') break;
898
899                 msginfo = news_parse_xover(buf);
900                 if (!msginfo) {
901                         log_warning(_("invalid xover line: %s\n"), buf);
902                         continue;
903                 }
904
905                 msginfo->folder = item;
906                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
907                 msginfo->flags.tmp_flags = MSG_NEWS;
908                 msginfo->newsgroups = g_strdup(item->path);
909
910                 if (!newlist)
911                         llast = newlist = g_slist_append(newlist, msginfo);
912                 else {
913                         llast = g_slist_append(llast, msginfo);
914                         llast = llast->next;
915                 }
916         }
917
918         if (nntp_xhdr(session, "to", begin, end) != NN_SUCCESS) {
919                 log_warning(_("can't get xhdr\n"));
920                 return newlist;
921         }
922
923         llast = newlist;
924
925         for (;;) {
926                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
927                         log_warning(_("error occurred while getting xhdr.\n"));
928                         return newlist;
929                 }
930                 count++;
931                 progressindicator_set_percentage
932                         (PROGRESS_TYPE_NETWORK,
933                          session->fetch_base_percentage +
934                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
935
936                 if (buf[0] == '.' && buf[1] == '\r') break;
937                 if (!llast) {
938                         g_warning("llast == NULL\n");
939                         continue;
940                 }
941
942                 msginfo = (MsgInfo *)llast->data;
943                 msginfo->to = news_parse_xhdr(buf, msginfo);
944
945                 llast = llast->next;
946         }
947
948         if (nntp_xhdr(session, "cc", begin, end) != NN_SUCCESS) {
949                 log_warning(_("can't get xhdr\n"));
950                 return newlist;
951         }
952
953         llast = newlist;
954
955         for (;;) {
956                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
957                         log_warning(_("error occurred while getting xhdr.\n"));
958                         return newlist;
959                 }
960                 count++;
961                 progressindicator_set_percentage
962                         (PROGRESS_TYPE_NETWORK,
963                          session->fetch_base_percentage +
964                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
965
966                 if (buf[0] == '.' && buf[1] == '\r') break;
967                 if (!llast) {
968                         g_warning("llast == NULL\n");
969                         continue;
970                 }
971
972                 msginfo = (MsgInfo *)llast->data;
973                 msginfo->cc = news_parse_xhdr(buf, msginfo);
974
975                 llast = llast->next;
976         }
977
978         return newlist;
979 }
980
981 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
982 {
983         NNTPSession *session;
984         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
985         guint first, last, next;
986         guint tofetch, fetched;
987         
988         g_return_val_if_fail(folder != NULL, NULL);
989         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
990         g_return_val_if_fail(msgnum_list != NULL, NULL);
991         g_return_val_if_fail(item != NULL, NULL);
992         
993         session = news_session_get(folder);
994         g_return_val_if_fail(session != NULL, NULL);
995
996         tmp_msgnum_list = g_slist_copy(msgnum_list);
997         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
998
999         progressindicator_start(PROGRESS_TYPE_NETWORK);
1000         tofetch = g_slist_length(tmp_msgnum_list);
1001         fetched = 0;
1002
1003         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1004         last = first;
1005         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1006                 next = GPOINTER_TO_INT(elem->data);
1007                 if(next != (last + 1)) {
1008                         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1009                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1010                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1011                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1012                         fetched = last - first + 1;
1013                         first = next;
1014                 }
1015                 last = next;
1016         }
1017         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1018         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1019         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1020         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1021
1022         g_slist_free(tmp_msgnum_list);
1023         
1024         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1025
1026         return msginfo_list;
1027 }