2005-03-18 [paul] 1.0.3cvs2.4
[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 *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
612         gchar *p;
613         gint num, size_int, line_int;
614         gchar *xover_buf;
615
616         Xstrdup_a(xover_buf, xover_str, return NULL);
617
618         PARSE_ONE_PARAM(subject, xover_buf);
619         PARSE_ONE_PARAM(sender, subject);
620         PARSE_ONE_PARAM(date, sender);
621         PARSE_ONE_PARAM(msgid, date);
622         PARSE_ONE_PARAM(ref, msgid);
623         PARSE_ONE_PARAM(size, ref);
624         PARSE_ONE_PARAM(line, size);
625         /*
626          * PARSE_ONE_PARAM(xref, line);
627          *
628          * if we parse extra headers we should first examine the
629          * LIST OVERVIEW.FMT response from the server. See
630          * RFC2980 for details
631          */
632
633         tmp = strchr(line, '\t');
634         if (!tmp) tmp = strchr(line, '\r');
635         if (!tmp) tmp = strchr(line, '\n');
636         if (tmp) *tmp = '\0';
637
638         num = atoi(xover_str);
639         size_int = atoi(size);
640         line_int = atoi(line);
641
642         /* set MsgInfo */
643         msginfo = procmsg_msginfo_new();
644         msginfo->msgnum = num;
645         msginfo->size = size_int;
646
647         msginfo->date = g_strdup(date);
648         msginfo->date_t = procheader_date_parse(NULL, date, 0);
649
650         msginfo->from = conv_unmime_header(sender, NULL);
651         msginfo->fromname = procheader_get_fromname(msginfo->from);
652
653         msginfo->subject = conv_unmime_header(subject, NULL);
654
655         extract_parenthesis(msgid, '<', '>');
656         remove_space(msgid);
657         if (*msgid != '\0')
658                 msginfo->msgid = g_strdup(msgid);
659
660         msginfo->references = g_strdup(ref);
661         eliminate_parenthesis(ref, '(', ')');
662         if ((p = strrchr(ref, '<')) != NULL) {
663                 extract_parenthesis(p, '<', '>');
664                 remove_space(p);
665                 if (*p != '\0')
666                         msginfo->inreplyto = g_strdup(p);
667         }
668
669         /*
670         msginfo->xref = g_strdup(xref);
671         p = msginfo->xref+strlen(msginfo->xref) - 1;
672         while (*p == '\r' || *p == '\n') {
673                 *p = '\0';
674                 p--;
675         }
676         */
677
678         return msginfo;
679 }
680
681 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
682 {
683         gchar *p;
684         gchar *tmp;
685         gint num;
686
687         p = strchr(xhdr_str, ' ');
688         if (!p)
689                 return NULL;
690         else
691                 p++;
692
693         num = atoi(xhdr_str);
694         if (msginfo->msgnum != num) return NULL;
695
696         tmp = strchr(p, '\r');
697         if (!tmp) tmp = strchr(p, '\n');
698
699         if (tmp)
700                 return g_strndup(p, tmp - p);
701         else
702                 return g_strdup(p);
703 }
704
705 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
706 {
707         gchar * tmp;
708         FILE * tmpfp;
709         gchar buf[BUFFSIZE];
710
711         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
712                               G_DIR_SEPARATOR, (gint)msginfo);
713         if (tmp == NULL)
714                 return -1;
715
716         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
717                 FILE_OP_ERROR(tmp, "fopen");
718                 return -1;
719         }
720         if (change_file_mode_rw(tmpfp, tmp) < 0) {
721                 FILE_OP_ERROR(tmp, "chmod");
722                 g_warning("can't change file mode\n");
723         }
724         
725         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
726         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
727         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
728         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
729         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
730         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
731         get_rfc822_date(buf, sizeof(buf));
732         fprintf(tmpfp, "Date: %s\r\n", buf);
733         fprintf(tmpfp, "\r\n");
734         fprintf(tmpfp, "removed with sylpheed\r\n");
735
736         fclose(tmpfp);
737
738         news_post(folder, tmp);
739         remove(tmp);
740
741         g_free(tmp);
742
743         return 0;
744 }
745
746 static gchar *news_folder_get_path(Folder *folder)
747 {
748         gchar *folder_path;
749
750         g_return_val_if_fail(folder->account != NULL, NULL);
751
752         folder_path = g_strconcat(get_news_cache_dir(),
753                                   G_DIR_SEPARATOR_S,
754                                   folder->account->nntp_server,
755                                   NULL);
756         return folder_path;
757 }
758
759 gchar *news_item_get_path(Folder *folder, FolderItem *item)
760 {
761         gchar *folder_path, *path;
762
763         g_return_val_if_fail(folder != NULL, NULL);
764         g_return_val_if_fail(item != NULL, NULL);
765         folder_path = news_folder_get_path(folder);
766
767         g_return_val_if_fail(folder_path != NULL, NULL);
768         if (folder_path[0] == G_DIR_SEPARATOR) {
769                 if (item->path)
770                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
771                                            item->path, NULL);
772                 else
773                         path = g_strdup(folder_path);
774         } else {
775                 if (item->path)
776                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
777                                            folder_path, G_DIR_SEPARATOR_S,
778                                            item->path, NULL);
779                 else
780                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
781                                            folder_path, NULL);
782         }
783         g_free(folder_path);
784
785         return path;
786 }
787
788 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
789 {
790         NNTPSession *session;
791         gint i, ok, num, first, last, nummsgs = 0;
792         gchar *dir;
793
794         g_return_val_if_fail(item != NULL, -1);
795         g_return_val_if_fail(item->folder != NULL, -1);
796         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
797
798         session = news_session_get(folder);
799         g_return_val_if_fail(session != NULL, -1);
800
801         *old_uids_valid = TRUE;
802
803         ok = news_select_group(session, item->path, &num, &first, &last);
804         if (ok != NN_SUCCESS) {
805                 log_warning(_("can't set group: %s\n"), item->path);
806                 return -1;
807         }
808
809         dir = news_folder_get_path(folder);
810         if (num <= 0)
811                 remove_all_numbered_files(dir);
812         else if (last < first)
813                 log_warning(_("invalid article range: %d - %d\n"),
814                             first, last);
815         else {
816                 for (i = first; i <= last; i++) {
817                         *msgnum_list = g_slist_prepend(*msgnum_list, 
818                                                        GINT_TO_POINTER(i));
819                         nummsgs++;
820                 }
821                 debug_print("removing old messages from %d to %d in %s\n",
822                             first, last, dir);
823                 remove_numbered_files(dir, 1, first - 1);
824         }
825         g_free(dir);
826
827         return nummsgs;
828 }
829
830 #define READ_TO_LISTEND(hdr) \
831         while (!(buf[0] == '.' && buf[1] == '\r')) { \
832                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
833                         log_warning(_("error occurred while getting %s.\n"), hdr); \
834                         return msginfo; \
835                 } \
836         }
837
838 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
839 {
840         NNTPSession *session;
841         MsgInfo *msginfo = NULL;
842         gchar buf[NNTPBUFSIZE];
843         gint ok;
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         ok = nntp_xover(session, num, num);
854         if (ok != NN_SUCCESS) {
855                 log_warning(_("can't get xover\n"));
856                 if (ok == NN_SOCKET) {
857                         session_destroy(SESSION(session));
858                         REMOTE_FOLDER(item->folder)->session = NULL;
859                 }
860                 return NULL;
861         }
862         
863         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
864                 log_warning(_("error occurred while getting xover.\n"));
865                 return NULL;
866         }
867         
868         msginfo = news_parse_xover(buf);
869         if (!msginfo) {
870                 log_warning(_("invalid xover line: %s\n"), buf);
871         }
872
873         READ_TO_LISTEND("xover");
874
875         if(!msginfo)
876                 return NULL;
877         
878         msginfo->folder = item;
879         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
880         msginfo->flags.tmp_flags = MSG_NEWS;
881         msginfo->newsgroups = g_strdup(item->path);
882
883         ok = nntp_xhdr(session, "to", num, num);
884         if (ok != NN_SUCCESS) {
885                 log_warning(_("can't get xhdr\n"));
886                 if (ok == NN_SOCKET) {
887                         session_destroy(SESSION(session));
888                         REMOTE_FOLDER(item->folder)->session = NULL;
889                 }
890                 return msginfo;
891         }
892
893         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
894                 log_warning(_("error occurred while getting xhdr.\n"));
895                 return msginfo;
896         }
897
898         msginfo->to = news_parse_xhdr(buf, msginfo);
899
900         READ_TO_LISTEND("xhdr (to)");
901
902         ok = nntp_xhdr(session, "cc", num, num);
903         if (ok != NN_SUCCESS) {
904                 log_warning(_("can't get xhdr\n"));
905                 if (ok == NN_SOCKET) {
906                         session_destroy(SESSION(session));
907                         REMOTE_FOLDER(item->folder)->session = NULL;
908                 }
909                 return msginfo;
910         }
911
912         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
913                 log_warning(_("error occurred while getting xhdr.\n"));
914                 return msginfo;
915         }
916
917         msginfo->cc = news_parse_xhdr(buf, msginfo);
918
919         READ_TO_LISTEND("xhdr (cc)");
920
921         return msginfo;
922 }
923
924 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
925 {
926         gchar buf[NNTPBUFSIZE];
927         GSList *newlist = NULL;
928         GSList *llast = NULL;
929         MsgInfo *msginfo;
930         guint count = 0, lines = (end - begin + 2) * 3;
931         gint ok;
932
933         g_return_val_if_fail(session != NULL, NULL);
934         g_return_val_if_fail(item != NULL, NULL);
935
936         log_message(_("getting xover %d - %d in %s...\n"),
937                     begin, end, item->path);
938         ok = nntp_xover(session, begin, end);
939         if (ok != NN_SUCCESS) {
940                 log_warning(_("can't get xover\n"));
941                 if (ok == NN_SOCKET) {
942                         session_destroy(SESSION(session));
943                         REMOTE_FOLDER(item->folder)->session = NULL;
944                 }
945                 return NULL;
946         }
947
948         for (;;) {
949                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
950                         log_warning(_("error occurred while getting xover.\n"));
951                         return newlist;
952                 }
953                 count++;
954                 progressindicator_set_percentage
955                         (PROGRESS_TYPE_NETWORK,
956                          session->fetch_base_percentage +
957                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
958
959                 if (buf[0] == '.' && buf[1] == '\r') break;
960
961                 msginfo = news_parse_xover(buf);
962                 if (!msginfo) {
963                         log_warning(_("invalid xover line: %s\n"), buf);
964                         continue;
965                 }
966
967                 msginfo->folder = item;
968                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
969                 msginfo->flags.tmp_flags = MSG_NEWS;
970                 msginfo->newsgroups = g_strdup(item->path);
971
972                 if (!newlist)
973                         llast = newlist = g_slist_append(newlist, msginfo);
974                 else {
975                         llast = g_slist_append(llast, msginfo);
976                         llast = llast->next;
977                 }
978         }
979
980         ok = nntp_xhdr(session, "to", begin, end);
981         if (ok != NN_SUCCESS) {
982                 log_warning(_("can't get xhdr\n"));
983                 if (ok == NN_SOCKET) {
984                         session_destroy(SESSION(session));
985                         REMOTE_FOLDER(item->folder)->session = NULL;
986                 }
987                 return newlist;
988         }
989
990         llast = newlist;
991
992         for (;;) {
993                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
994                         log_warning(_("error occurred while getting xhdr.\n"));
995                         return newlist;
996                 }
997                 count++;
998                 progressindicator_set_percentage
999                         (PROGRESS_TYPE_NETWORK,
1000                          session->fetch_base_percentage +
1001                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
1002
1003                 if (buf[0] == '.' && buf[1] == '\r') break;
1004                 if (!llast) {
1005                         g_warning("llast == NULL\n");
1006                         continue;
1007                 }
1008
1009                 msginfo = (MsgInfo *)llast->data;
1010                 msginfo->to = news_parse_xhdr(buf, msginfo);
1011
1012                 llast = llast->next;
1013         }
1014
1015         ok = nntp_xhdr(session, "cc", begin, end);
1016         if (ok != NN_SUCCESS) {
1017                 log_warning(_("can't get xhdr\n"));
1018                 if (ok == NN_SOCKET) {
1019                         session_destroy(SESSION(session));
1020                         REMOTE_FOLDER(item->folder)->session = NULL;
1021                 }
1022                 return newlist;
1023         }
1024
1025         llast = newlist;
1026
1027         for (;;) {
1028                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1029                         log_warning(_("error occurred while getting xhdr.\n"));
1030                         return newlist;
1031                 }
1032                 count++;
1033                 progressindicator_set_percentage
1034                         (PROGRESS_TYPE_NETWORK,
1035                          session->fetch_base_percentage +
1036                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
1037
1038                 if (buf[0] == '.' && buf[1] == '\r') break;
1039                 if (!llast) {
1040                         g_warning("llast == NULL\n");
1041                         continue;
1042                 }
1043
1044                 msginfo = (MsgInfo *)llast->data;
1045                 msginfo->cc = news_parse_xhdr(buf, msginfo);
1046
1047                 llast = llast->next;
1048         }
1049
1050         session_set_access_time(SESSION(session));
1051
1052         return newlist;
1053 }
1054
1055 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1056 {
1057         NNTPSession *session;
1058         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1059         guint first, last, next;
1060         guint tofetch, fetched;
1061         
1062         g_return_val_if_fail(folder != NULL, NULL);
1063         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1064         g_return_val_if_fail(msgnum_list != NULL, NULL);
1065         g_return_val_if_fail(item != NULL, NULL);
1066         
1067         session = news_session_get(folder);
1068         g_return_val_if_fail(session != NULL, NULL);
1069
1070         tmp_msgnum_list = g_slist_copy(msgnum_list);
1071         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1072
1073         progressindicator_start(PROGRESS_TYPE_NETWORK);
1074         tofetch = g_slist_length(tmp_msgnum_list);
1075         fetched = 0;
1076
1077         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1078         last = first;
1079         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1080                 next = GPOINTER_TO_INT(elem->data);
1081                 if(next != (last + 1)) {
1082                         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1083                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1084                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1085                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1086                         fetched = last - first + 1;
1087                         first = next;
1088                 }
1089                 last = next;
1090         }
1091         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1092         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1093         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1094         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1095
1096         g_slist_free(tmp_msgnum_list);
1097         
1098         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1099
1100         return msginfo_list;
1101 }
1102
1103 gboolean news_scan_required(Folder *folder, FolderItem *item)
1104 {
1105         return TRUE;
1106 }