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