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