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