0.9.3claws57
[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         PARSE_ONE_PARAM(xref, line);
599
600         tmp = strchr(xref, '\t');
601         if (!tmp) tmp = strchr(line, '\r');
602         if (!tmp) tmp = strchr(line, '\n');
603         if (tmp) *tmp = '\0';
604
605         num = atoi(xover_str);
606         size_int = atoi(size);
607         line_int = atoi(line);
608
609         /* set MsgInfo */
610         msginfo = procmsg_msginfo_new();
611         msginfo->msgnum = num;
612         msginfo->size = size_int;
613
614         msginfo->date = g_strdup(date);
615         msginfo->date_t = procheader_date_parse(NULL, date, 0);
616
617         conv_unmime_header(buf, sizeof(buf), sender, NULL);
618         msginfo->from = g_strdup(buf);
619         msginfo->fromname = procheader_get_fromname(buf);
620
621         conv_unmime_header(buf, sizeof(buf), subject, NULL);
622         msginfo->subject = g_strdup(buf);
623
624         extract_parenthesis(msgid, '<', '>');
625         remove_space(msgid);
626         if (*msgid != '\0')
627                 msginfo->msgid = g_strdup(msgid);
628
629         msginfo->references = g_strdup(ref);
630         eliminate_parenthesis(ref, '(', ')');
631         if ((p = strrchr(ref, '<')) != NULL) {
632                 extract_parenthesis(p, '<', '>');
633                 remove_space(p);
634                 if (*p != '\0')
635                         msginfo->inreplyto = g_strdup(p);
636         }
637
638         msginfo->xref = g_strdup(xref);
639         p = msginfo->xref+strlen(msginfo->xref) - 1;
640         while (*p == '\r' || *p == '\n') {
641                 *p = '\0';
642                 p--;
643         }
644
645         return msginfo;
646 }
647
648 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
649 {
650         gchar *p;
651         gchar *tmp;
652         gint num;
653
654         p = strchr(xhdr_str, ' ');
655         if (!p)
656                 return NULL;
657         else
658                 p++;
659
660         num = atoi(xhdr_str);
661         if (msginfo->msgnum != num) return NULL;
662
663         tmp = strchr(p, '\r');
664         if (!tmp) tmp = strchr(p, '\n');
665
666         if (tmp)
667                 return g_strndup(p, tmp - p);
668         else
669                 return g_strdup(p);
670 }
671
672 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
673 {
674         gchar * tmp;
675         FILE * tmpfp;
676         gchar buf[BUFFSIZE];
677
678         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
679                               G_DIR_SEPARATOR, (gint)msginfo);
680         if (tmp == NULL)
681                 return -1;
682
683         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
684                 FILE_OP_ERROR(tmp, "fopen");
685                 return -1;
686         }
687         if (change_file_mode_rw(tmpfp, tmp) < 0) {
688                 FILE_OP_ERROR(tmp, "chmod");
689                 g_warning("can't change file mode\n");
690         }
691         
692         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
693         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
694         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
695         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
696         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
697         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
698         get_rfc822_date(buf, sizeof(buf));
699         fprintf(tmpfp, "Date: %s\r\n", buf);
700         fprintf(tmpfp, "\r\n");
701         fprintf(tmpfp, "removed with sylpheed\r\n");
702
703         fclose(tmpfp);
704
705         news_post(folder, tmp);
706         remove(tmp);
707
708         g_free(tmp);
709
710         return 0;
711 }
712
713 static gchar *news_folder_get_path(Folder *folder)
714 {
715         gchar *folder_path;
716
717         g_return_val_if_fail(folder->account != NULL, NULL);
718
719         folder_path = g_strconcat(get_news_cache_dir(),
720                                   G_DIR_SEPARATOR_S,
721                                   folder->account->nntp_server,
722                                   NULL);
723         return folder_path;
724 }
725
726 gchar *news_item_get_path(Folder *folder, FolderItem *item)
727 {
728         gchar *folder_path, *path;
729
730         g_return_val_if_fail(folder != NULL, NULL);
731         g_return_val_if_fail(item != NULL, NULL);
732         folder_path = news_folder_get_path(folder);
733
734         g_return_val_if_fail(folder_path != NULL, NULL);
735         if (folder_path[0] == G_DIR_SEPARATOR) {
736                 if (item->path)
737                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
738                                            item->path, NULL);
739                 else
740                         path = g_strdup(folder_path);
741         } else {
742                 if (item->path)
743                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
744                                            folder_path, G_DIR_SEPARATOR_S,
745                                            item->path, NULL);
746                 else
747                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
748                                            folder_path, NULL);
749         }
750         g_free(folder_path);
751
752         return path;
753 }
754
755 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list)
756 {
757         NNTPSession *session;
758         gint i, ok, num, first, last, nummsgs = 0;
759         gchar *dir;
760
761         g_return_val_if_fail(item != NULL, -1);
762         g_return_val_if_fail(item->folder != NULL, -1);
763         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
764
765         session = news_session_get(folder);
766         g_return_val_if_fail(session != NULL, -1);
767
768         ok = news_select_group(session, item->path, &num, &first, &last);
769         if (ok != NN_SUCCESS) {
770                 log_warning(_("can't set group: %s\n"), item->path);
771                 return -1;
772         }
773
774         if(last < first) {
775                 log_warning(_("invalid article range: %d - %d\n"),
776                             first, last);
777                 return 0;
778         }
779
780         for(i = first; i <= last; i++) {
781                 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
782                 nummsgs++;
783         }
784
785         dir = folder_item_get_path(item);
786         debug_print("removing old messages from %d to %d in %s\n", first, last, dir);
787         remove_numbered_files(dir, 1, first - 1);
788         g_free(dir);
789
790         return nummsgs;
791 }
792
793 #define READ_TO_LISTEND(hdr) \
794         while (!(buf[0] == '.' && buf[1] == '\r')) { \
795                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
796                         log_warning(_("error occurred while getting %s.\n"), hdr); \
797                         return msginfo; \
798                 } \
799         }
800
801 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
802 {
803         NNTPSession *session;
804         MsgInfo *msginfo = NULL;
805         gchar buf[NNTPBUFSIZE];
806
807         session = news_session_get(folder);
808         g_return_val_if_fail(session != NULL, NULL);
809         g_return_val_if_fail(item != NULL, NULL);
810         g_return_val_if_fail(item->folder != NULL, NULL);
811         g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
812
813         log_message(_("getting xover %d in %s...\n"),
814                     num, item->path);
815         if (nntp_xover(session, num, num) != NN_SUCCESS) {
816                 log_warning(_("can't get xover\n"));
817                 return NULL;
818         }
819         
820         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
821                 log_warning(_("error occurred while getting xover.\n"));
822                 return NULL;
823         }
824         
825         msginfo = news_parse_xover(buf);
826         if (!msginfo) {
827                 log_warning(_("invalid xover line: %s\n"), buf);
828         }
829
830         READ_TO_LISTEND("xover");
831
832         if(!msginfo)
833                 return NULL;
834         
835         msginfo->folder = item;
836         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
837         msginfo->flags.tmp_flags = MSG_NEWS;
838         msginfo->newsgroups = g_strdup(item->path);
839
840         if (nntp_xhdr(session, "to", num, num) != NN_SUCCESS) {
841                 log_warning(_("can't get xhdr\n"));
842                 return msginfo;
843         }
844
845         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
846                 log_warning(_("error occurred while getting xhdr.\n"));
847                 return msginfo;
848         }
849
850         msginfo->to = news_parse_xhdr(buf, msginfo);
851
852         READ_TO_LISTEND("xhdr (to)");
853
854         if (nntp_xhdr(session, "cc", num, num) != NN_SUCCESS) {
855                 log_warning(_("can't get xhdr\n"));
856                 return msginfo;
857         }
858
859         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
860                 log_warning(_("error occurred while getting xhdr.\n"));
861                 return msginfo;
862         }
863
864         msginfo->cc = news_parse_xhdr(buf, msginfo);
865
866         READ_TO_LISTEND("xhdr (cc)");
867
868         return msginfo;
869 }
870
871 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
872 {
873         gchar buf[NNTPBUFSIZE];
874         GSList *newlist = NULL;
875         GSList *llast = NULL;
876         MsgInfo *msginfo;
877         guint count = 0, lines = (end - begin + 2) * 3;
878
879         g_return_val_if_fail(session != NULL, NULL);
880         g_return_val_if_fail(item != NULL, NULL);
881
882         log_message(_("getting xover %d - %d in %s...\n"),
883                     begin, end, item->path);
884         if (nntp_xover(session, begin, end) != NN_SUCCESS) {
885                 log_warning(_("can't get xover\n"));
886                 return NULL;
887         }
888
889         for (;;) {
890                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
891                         log_warning(_("error occurred while getting xover.\n"));
892                         return newlist;
893                 }
894                 count++;
895                 progressindicator_set_percentage
896                         (PROGRESS_TYPE_NETWORK,
897                          session->fetch_base_percentage +
898                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
899
900                 if (buf[0] == '.' && buf[1] == '\r') break;
901
902                 msginfo = news_parse_xover(buf);
903                 if (!msginfo) {
904                         log_warning(_("invalid xover line: %s\n"), buf);
905                         continue;
906                 }
907
908                 msginfo->folder = item;
909                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
910                 msginfo->flags.tmp_flags = MSG_NEWS;
911                 msginfo->newsgroups = g_strdup(item->path);
912
913                 if (!newlist)
914                         llast = newlist = g_slist_append(newlist, msginfo);
915                 else {
916                         llast = g_slist_append(llast, msginfo);
917                         llast = llast->next;
918                 }
919         }
920
921         if (nntp_xhdr(session, "to", begin, end) != NN_SUCCESS) {
922                 log_warning(_("can't get xhdr\n"));
923                 return newlist;
924         }
925
926         llast = newlist;
927
928         for (;;) {
929                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
930                         log_warning(_("error occurred while getting xhdr.\n"));
931                         return newlist;
932                 }
933                 count++;
934                 progressindicator_set_percentage
935                         (PROGRESS_TYPE_NETWORK,
936                          session->fetch_base_percentage +
937                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
938
939                 if (buf[0] == '.' && buf[1] == '\r') break;
940                 if (!llast) {
941                         g_warning("llast == NULL\n");
942                         continue;
943                 }
944
945                 msginfo = (MsgInfo *)llast->data;
946                 msginfo->to = news_parse_xhdr(buf, msginfo);
947
948                 llast = llast->next;
949         }
950
951         if (nntp_xhdr(session, "cc", begin, end) != NN_SUCCESS) {
952                 log_warning(_("can't get xhdr\n"));
953                 return newlist;
954         }
955
956         llast = newlist;
957
958         for (;;) {
959                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
960                         log_warning(_("error occurred while getting xhdr.\n"));
961                         return newlist;
962                 }
963                 count++;
964                 progressindicator_set_percentage
965                         (PROGRESS_TYPE_NETWORK,
966                          session->fetch_base_percentage +
967                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
968
969                 if (buf[0] == '.' && buf[1] == '\r') break;
970                 if (!llast) {
971                         g_warning("llast == NULL\n");
972                         continue;
973                 }
974
975                 msginfo = (MsgInfo *)llast->data;
976                 msginfo->cc = news_parse_xhdr(buf, msginfo);
977
978                 llast = llast->next;
979         }
980
981         return newlist;
982 }
983
984 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
985 {
986         NNTPSession *session;
987         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
988         guint first, last, next;
989         guint tofetch, fetched;
990         
991         g_return_val_if_fail(folder != NULL, NULL);
992         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
993         g_return_val_if_fail(msgnum_list != NULL, NULL);
994         g_return_val_if_fail(item != NULL, NULL);
995         
996         session = news_session_get(folder);
997         g_return_val_if_fail(session != NULL, NULL);
998
999         tmp_msgnum_list = g_slist_copy(msgnum_list);
1000         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1001
1002         progressindicator_start(PROGRESS_TYPE_NETWORK);
1003         tofetch = g_slist_length(tmp_msgnum_list);
1004         fetched = 0;
1005
1006         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1007         last = first;
1008         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1009                 next = GPOINTER_TO_INT(elem->data);
1010                 if(next != (last + 1)) {
1011                         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1012                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1013                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1014                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1015                         fetched = last - first + 1;
1016                         first = next;
1017                 }
1018                 last = next;
1019         }
1020         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1021         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1022         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1023         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1024
1025         g_slist_free(tmp_msgnum_list);
1026         
1027         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1028
1029         return msginfo_list;
1030 }