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