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