adecc70ab8bf73295df6c5a71cd5165f09cbca18
[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         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 (!rfolder->session) {
284                 rfolder->session = news_session_new_for_folder(folder);
285                 return NNTP_SESSION(rfolder->session);
286         }
287
288         if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
289                 rfolder->session->last_access_time = time(NULL);
290                 return NNTP_SESSION(rfolder->session);
291         }
292
293         if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
294             != NN_SUCCESS) {
295                 log_warning("NNTP connection to %s:%d has been"
296                               " disconnected. Reconnecting...\n",
297                             folder->account->nntp_server,
298                             folder->account->set_nntpport ?
299                             folder->account->nntpport : NNTP_PORT);
300                 session_destroy(rfolder->session);
301                 rfolder->session = news_session_new_for_folder(folder);
302         }
303
304         if (rfolder->session)
305                 rfolder->session->last_access_time = time(NULL);
306         return NNTP_SESSION(rfolder->session);
307 }
308
309 static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
310 {
311         gchar *path, *filename;
312         NNTPSession *session;
313         gint ok;
314
315         g_return_val_if_fail(folder != NULL, NULL);
316         g_return_val_if_fail(item != NULL, NULL);
317
318         path = folder_item_get_path(item);
319         if (!is_dir_exist(path))
320                 make_dir_hier(path);
321         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
322         g_free(path);
323
324         if (is_file_exist(filename)) {
325                 debug_print("article %d has been already cached.\n", num);
326                 return filename;
327         }
328
329         session = news_session_get(folder);
330         if (!session) {
331                 g_free(filename);
332                 return NULL;
333         }
334
335         ok = news_select_group(session, item->path, NULL, NULL, NULL);
336         if (ok != NN_SUCCESS) {
337                 g_warning("can't select group %s\n", item->path);
338                 g_free(filename);
339                 return NULL;
340         }
341
342         debug_print("getting article %d...\n", num);
343         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
344                               num, filename);
345         if (ok < 0) {
346                 g_warning("can't read article %d\n", num);
347                 session_destroy(SESSION(session));
348                 REMOTE_FOLDER(folder)->session = NULL;
349                 g_free(filename);
350                 return NULL;
351         }
352
353         return filename;
354 }
355
356 static NewsGroupInfo *news_group_info_new(const gchar *name,
357                                           gint first, gint last, gchar type)
358 {
359         NewsGroupInfo *ginfo;
360
361         ginfo = g_new(NewsGroupInfo, 1);
362         ginfo->name = g_strdup(name);
363         ginfo->first = first;
364         ginfo->last = last;
365         ginfo->type = type;
366
367         return ginfo;
368 }
369
370 static void news_group_info_free(NewsGroupInfo *ginfo)
371 {
372         g_free(ginfo->name);
373         g_free(ginfo);
374 }
375
376 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
377                                     NewsGroupInfo *ginfo2)
378 {
379         return g_strcasecmp(ginfo1->name, ginfo2->name);
380 }
381
382 GSList *news_get_group_list(Folder *folder)
383 {
384         gchar *path, *filename;
385         FILE *fp;
386         GSList *list = NULL;
387         GSList *last = NULL;
388         gchar buf[NNTPBUFSIZE];
389
390         g_return_val_if_fail(folder != NULL, NULL);
391         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
392
393         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
394         if (!is_dir_exist(path))
395                 make_dir_hier(path);
396         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
397         g_free(path);
398
399         if ((fp = fopen(filename, "rb")) == NULL) {
400                 NNTPSession *session;
401
402                 session = news_session_get(folder);
403                 if (!session) {
404                         g_free(filename);
405                         return NULL;
406                 }
407
408                 if (nntp_list(session) != NN_SUCCESS) {
409                         g_free(filename);
410                         return NULL;
411                 }
412                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
413                         log_warning("can't retrieve newsgroup list\n");
414                         session_destroy(SESSION(session));
415                         REMOTE_FOLDER(folder)->session = NULL;
416                         g_free(filename);
417                         return NULL;
418                 }
419
420                 if ((fp = fopen(filename, "rb")) == NULL) {
421                         FILE_OP_ERROR(filename, "fopen");
422                         g_free(filename);
423                         return NULL;
424                 }
425         }
426
427         while (fgets(buf, sizeof(buf), fp) != NULL) {
428                 gchar *p = buf;
429                 gchar *name;
430                 gint last_num;
431                 gint first_num;
432                 gchar type;
433                 NewsGroupInfo *ginfo;
434
435                 p = strchr(p, ' ');
436                 if (!p) continue;
437                 *p = '\0';
438                 p++;
439                 name = buf;
440
441                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
442                         continue;
443
444                 ginfo = news_group_info_new(name, first_num, last_num, type);
445
446                 if (!last)
447                         last = list = g_slist_append(NULL, ginfo);
448                 else {
449                         last = g_slist_append(last, ginfo);
450                         last = last->next;
451                 }
452         }
453
454         fclose(fp);
455         g_free(filename);
456
457         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
458
459         return list;
460 }
461
462 void news_group_list_free(GSList *group_list)
463 {
464         GSList *cur;
465
466         if (!group_list) return;
467
468         for (cur = group_list; cur != NULL; cur = cur->next)
469                 news_group_info_free((NewsGroupInfo *)cur->data);
470         g_slist_free(group_list);
471 }
472
473 void news_remove_group_list_cache(Folder *folder)
474 {
475         gchar *path, *filename;
476
477         g_return_if_fail(folder != NULL);
478         g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
479
480         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
481         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
482         g_free(path);
483
484         if (is_file_exist(filename)) {
485                 if (remove(filename) < 0)
486                         FILE_OP_ERROR(filename, "remove");
487         }
488         g_free(filename);
489 }
490
491 gint news_post(Folder *folder, const gchar *file)
492 {
493         FILE *fp;
494         gint ok;
495
496         g_return_val_if_fail(folder != NULL, -1);
497         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
498         g_return_val_if_fail(file != NULL, -1);
499
500         if ((fp = fopen(file, "rb")) == NULL) {
501                 FILE_OP_ERROR(file, "fopen");
502                 return -1;
503         }
504
505         ok = news_post_stream(folder, fp);
506
507         fclose(fp);
508
509         return ok;
510 }
511
512 gint news_post_stream(Folder *folder, FILE *fp)
513 {
514         NNTPSession *session;
515         gint ok;
516
517         g_return_val_if_fail(folder != NULL, -1);
518         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
519         g_return_val_if_fail(fp != NULL, -1);
520
521         session = news_session_get(folder);
522         if (!session) return -1;
523
524         ok = nntp_post(session, fp);
525         if (ok != NN_SUCCESS) {
526                 log_warning("can't post article.\n");
527                 return -1;
528         }
529
530         return 0;
531 }
532
533 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
534                                  gint num, gchar *filename)
535 {
536         gchar *msgid;
537
538         if (nntp_get_article(session, cmd, num, &msgid)
539             != NN_SUCCESS)
540                 return -1;
541
542         debug_print("Message-Id = %s, num = %d\n", msgid, num);
543         g_free(msgid);
544
545         if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
546                 log_warning("can't retrieve article %d\n", num);
547                 return -1;
548         }
549
550         return 0;
551 }
552
553 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
554 {
555         return news_get_article_cmd(session, "ARTICLE", num, filename);
556 }
557
558 /**
559  * news_select_group:
560  * @session: Active NNTP session.
561  * @group: Newsgroup name.
562  * @num: Estimated number of articles.
563  * @first: First article number.
564  * @last: Last article number.
565  *
566  * Select newsgroup @group with the GROUP command if it is not already
567  * selected in @session, or article numbers need to be returned.
568  *
569  * Return value: NNTP result code.
570  **/
571 static gint news_select_group(NNTPSession *session, const gchar *group,
572                               gint *num, gint *first, gint *last)
573 {
574         gint ok;
575         gint num_, first_, last_;
576
577         if (!num || !first || !last) {
578                 if (session->group && g_strcasecmp(session->group, group) == 0)
579                         return NN_SUCCESS;
580                 num = &num_;
581                 first = &first_;
582                 last = &last_;
583         }
584
585         g_free(session->group);
586         session->group = NULL;
587
588         ok = nntp_group(session, group, num, first, last);
589         if (ok == NN_SUCCESS)
590                 session->group = g_strdup(group);
591
592         return ok;
593 }
594
595 #define PARSE_ONE_PARAM(p, srcp) \
596 { \
597         p = strchr(srcp, '\t'); \
598         if (!p) return NULL; \
599         else \
600                 *p++ = '\0'; \
601 }
602
603 static MsgInfo *news_parse_xover(const gchar *xover_str)
604 {
605         MsgInfo *msginfo;
606         gchar buf[NNTPBUFSIZE];
607         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
608         gchar *p;
609         gint num, size_int, line_int;
610         gchar *xover_buf;
611
612         Xstrdup_a(xover_buf, xover_str, return NULL);
613
614         PARSE_ONE_PARAM(subject, xover_buf);
615         PARSE_ONE_PARAM(sender, subject);
616         PARSE_ONE_PARAM(date, sender);
617         PARSE_ONE_PARAM(msgid, date);
618         PARSE_ONE_PARAM(ref, msgid);
619         PARSE_ONE_PARAM(size, ref);
620         PARSE_ONE_PARAM(line, size);
621         /*
622          * PARSE_ONE_PARAM(xref, line);
623          *
624          * if we parse extra headers we should first examine the
625          * LIST OVERVIEW.FMT response from the server. See
626          * RFC2980 for details
627          */
628
629         tmp = strchr(line, '\t');
630         if (!tmp) tmp = strchr(line, '\r');
631         if (!tmp) tmp = strchr(line, '\n');
632         if (tmp) *tmp = '\0';
633
634         num = atoi(xover_str);
635         size_int = atoi(size);
636         line_int = atoi(line);
637
638         /* set MsgInfo */
639         msginfo = procmsg_msginfo_new();
640         msginfo->msgnum = num;
641         msginfo->size = size_int;
642
643         msginfo->date = g_strdup(date);
644         msginfo->date_t = procheader_date_parse(NULL, date, 0);
645
646         conv_unmime_header(buf, sizeof(buf), sender, NULL);
647         msginfo->from = g_strdup(buf);
648         msginfo->fromname = procheader_get_fromname(buf);
649
650         conv_unmime_header(buf, sizeof(buf), subject, NULL);
651         msginfo->subject = g_strdup(buf);
652
653         extract_parenthesis(msgid, '<', '>');
654         remove_space(msgid);
655         if (*msgid != '\0')
656                 msginfo->msgid = g_strdup(msgid);
657
658         msginfo->references = g_strdup(ref);
659         eliminate_parenthesis(ref, '(', ')');
660         if ((p = strrchr(ref, '<')) != NULL) {
661                 extract_parenthesis(p, '<', '>');
662                 remove_space(p);
663                 if (*p != '\0')
664                         msginfo->inreplyto = g_strdup(p);
665         }
666
667         /*
668         msginfo->xref = g_strdup(xref);
669         p = msginfo->xref+strlen(msginfo->xref) - 1;
670         while (*p == '\r' || *p == '\n') {
671                 *p = '\0';
672                 p--;
673         }
674         */
675
676         return msginfo;
677 }
678
679 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
680 {
681         gchar *p;
682         gchar *tmp;
683         gint num;
684
685         p = strchr(xhdr_str, ' ');
686         if (!p)
687                 return NULL;
688         else
689                 p++;
690
691         num = atoi(xhdr_str);
692         if (msginfo->msgnum != num) return NULL;
693
694         tmp = strchr(p, '\r');
695         if (!tmp) tmp = strchr(p, '\n');
696
697         if (tmp)
698                 return g_strndup(p, tmp - p);
699         else
700                 return g_strdup(p);
701 }
702
703 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
704 {
705         gchar * tmp;
706         FILE * tmpfp;
707         gchar buf[BUFFSIZE];
708
709         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
710                               G_DIR_SEPARATOR, (gint)msginfo);
711         if (tmp == NULL)
712                 return -1;
713
714         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
715                 FILE_OP_ERROR(tmp, "fopen");
716                 return -1;
717         }
718         if (change_file_mode_rw(tmpfp, tmp) < 0) {
719                 FILE_OP_ERROR(tmp, "chmod");
720                 g_warning("can't change file mode\n");
721         }
722         
723         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
724         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
725         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
726         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
727         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
728         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
729         get_rfc822_date(buf, sizeof(buf));
730         fprintf(tmpfp, "Date: %s\r\n", buf);
731         fprintf(tmpfp, "\r\n");
732         fprintf(tmpfp, "removed with sylpheed\r\n");
733
734         fclose(tmpfp);
735
736         news_post(folder, tmp);
737         remove(tmp);
738
739         g_free(tmp);
740
741         return 0;
742 }
743
744 static gchar *news_folder_get_path(Folder *folder)
745 {
746         gchar *folder_path;
747
748         g_return_val_if_fail(folder->account != NULL, NULL);
749
750         folder_path = g_strconcat(get_news_cache_dir(),
751                                   G_DIR_SEPARATOR_S,
752                                   folder->account->nntp_server,
753                                   NULL);
754         return folder_path;
755 }
756
757 gchar *news_item_get_path(Folder *folder, FolderItem *item)
758 {
759         gchar *folder_path, *path;
760
761         g_return_val_if_fail(folder != NULL, NULL);
762         g_return_val_if_fail(item != NULL, NULL);
763         folder_path = news_folder_get_path(folder);
764
765         g_return_val_if_fail(folder_path != NULL, NULL);
766         if (folder_path[0] == G_DIR_SEPARATOR) {
767                 if (item->path)
768                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
769                                            item->path, NULL);
770                 else
771                         path = g_strdup(folder_path);
772         } else {
773                 if (item->path)
774                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
775                                            folder_path, G_DIR_SEPARATOR_S,
776                                            item->path, NULL);
777                 else
778                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
779                                            folder_path, NULL);
780         }
781         g_free(folder_path);
782
783         return path;
784 }
785
786 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
787 {
788         NNTPSession *session;
789         gint i, ok, num, first, last, nummsgs = 0;
790         gchar *dir;
791
792         g_return_val_if_fail(item != NULL, -1);
793         g_return_val_if_fail(item->folder != NULL, -1);
794         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
795
796         session = news_session_get(folder);
797         g_return_val_if_fail(session != NULL, -1);
798
799         *old_uids_valid = TRUE;
800
801         ok = news_select_group(session, item->path, &num, &first, &last);
802         if (ok != NN_SUCCESS) {
803                 log_warning(_("can't set group: %s\n"), item->path);
804                 return -1;
805         }
806
807         if (num <= 0) {
808                 remove_all_numbered_files(dir);
809                 return 0;
810         }
811
812         if(last < first) {
813                 log_warning(_("invalid article range: %d - %d\n"),
814                             first, last);
815                 return 0;
816         }
817
818         for(i = first; i <= last; i++) {
819                 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
820                 nummsgs++;
821         }
822
823         dir = folder_item_get_path(item);
824         debug_print("removing old messages from %d to %d in %s\n", first, last, dir);
825         remove_numbered_files(dir, 1, first - 1);
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 }