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