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