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