0cd38d152ab6b8de897c60d9dcd8dccb3d0ad6c0
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 "alertpanel.h"
50 #include "log.h"
51 #if USE_SSL
52 #  include "ssl.h"
53 #endif
54
55 #define NNTP_PORT       119
56 #if USE_SSL
57 #define NNTPS_PORT      563
58 #endif
59
60 static void news_folder_init             (Folder        *folder,
61                                           const gchar   *name,
62                                           const gchar   *path);
63
64 #if USE_SSL
65 static Session *news_session_new         (const gchar   *server,
66                                           gushort        port,
67                                           const gchar   *userid,
68                                           const gchar   *passwd,
69                                           SSLType        ssl_type);
70 #else
71 static Session *news_session_new         (const gchar   *server,
72                                           gushort        port,
73                                           const gchar   *userid,
74                                           const gchar   *passwd);
75 #endif
76
77 static gint news_get_article_cmd         (NNTPSession   *session,
78                                           const gchar   *cmd,
79                                           gint           num,
80                                           gchar         *filename);
81 static gint news_get_article             (NNTPSession   *session,
82                                           gint           num,
83                                           gchar         *filename);
84
85 static gint news_select_group            (NNTPSession   *session,
86                                           const gchar   *group,
87                                           gint          *num,
88                                           gint          *first,
89                                           gint          *last);
90 static GSList *news_get_uncached_articles(NNTPSession   *session,
91                                           FolderItem    *item,
92                                           gint           cache_last,
93                                           gint          *rfirst,
94                                           gint          *rlast);
95 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
96 static gchar *news_parse_xhdr            (const gchar   *xhdr_str,
97                                           MsgInfo       *msginfo);
98 static GSList *news_delete_old_articles  (GSList        *alist,
99                                           FolderItem    *item,
100                                           gint           first);
101 static void news_delete_all_articles     (FolderItem    *item);
102 static void news_delete_expired_caches   (GSList        *alist,
103                                           FolderItem    *item);
104
105 static gint news_remove_msg              (Folder        *folder, 
106                                           FolderItem    *item, 
107                                           gint           num);
108 gint news_get_num_list                   (Folder        *folder, 
109                                           FolderItem    *item,
110                                           GSList       **list);
111 MsgInfo *news_get_msginfo                (Folder        *folder, 
112                                           FolderItem    *item,
113                                           gint           num);
114 GSList *news_get_msginfos                (Folder        *folder,
115                                           FolderItem    *item,
116                                           GSList        *msgnum_list);
117
118 gint news_post_stream                    (Folder        *folder, 
119                                           FILE          *fp);
120
121 Folder *news_folder_new(const gchar *name, const gchar *path)
122 {
123         Folder *folder;
124
125         folder = (Folder *)g_new0(NewsFolder, 1);
126         news_folder_init(folder, name, path);
127
128         return folder;
129 }
130
131 void news_folder_destroy(Folder *folder)
132 {
133         gchar *dir;
134
135         dir = folder_get_path(folder);
136         if (is_dir_exist(dir))
137                 remove_dir_recursive(dir);
138         g_free(dir);
139
140         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
141 }
142
143 static void news_folder_init(Folder *folder, const gchar *name,
144                              const gchar *path)
145 {
146         folder->type = F_NEWS;
147
148         folder_remote_folder_init(folder, name, path);
149
150 /*
151         folder->get_msg_list = news_get_article_list;
152 */
153         folder->fetch_msg    = news_fetch_msg;
154 /*
155         folder->scan         = news_scan_group;
156 */
157         folder->destroy      = news_folder_destroy;
158         folder->remove_msg   = news_remove_msg;
159         folder->get_num_list = news_get_num_list;
160         folder->get_msginfo  = news_get_msginfo;
161         folder->get_msginfos = news_get_msginfos;
162 }
163
164 #if USE_SSL
165 static Session *news_session_new(const gchar *server, gushort port,
166                                  const gchar *userid, const gchar *passwd,
167                                  SSLType ssl_type)
168 #else
169 static Session *news_session_new(const gchar *server, gushort port,
170                                  const gchar *userid, const gchar *passwd)
171 #endif
172 {
173         gchar buf[NNTPBUFSIZE];
174         NNTPSession *session;
175         NNTPSockInfo *nntp_sock;
176
177         g_return_val_if_fail(server != NULL, NULL);
178
179         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
180
181 #if USE_SSL
182         if (userid && passwd)
183                 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd,
184                                            ssl_type);
185         else
186                 nntp_sock = nntp_open(server, port, buf, ssl_type);
187 #else
188         if (userid && passwd)
189                 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
190         else
191                 nntp_sock = nntp_open(server, port, buf);
192 #endif
193
194         if (nntp_sock == NULL)
195                 return NULL;
196
197         session = g_new(NNTPSession, 1);
198         SESSION(session)->type             = SESSION_NEWS;
199         SESSION(session)->server           = g_strdup(server);
200         session->nntp_sock                 = nntp_sock;
201         SESSION(session)->sock             = nntp_sock->sock;
202         SESSION(session)->connected        = TRUE;
203         SESSION(session)->phase            = SESSION_READY;
204         SESSION(session)->last_access_time = time(NULL);
205         SESSION(session)->data             = NULL;
206
207         SESSION(session)->destroy          = news_session_destroy;
208
209         session->group = NULL;
210
211         return SESSION(session);
212 }
213
214 void news_session_destroy(Session *session)
215 {
216         nntp_close(NNTP_SESSION(session)->nntp_sock);
217         NNTP_SESSION(session)->nntp_sock = NULL;
218         session->sock = NULL;
219
220         g_free(NNTP_SESSION(session)->group);
221 }
222
223 static Session *news_session_new_for_folder(Folder *folder)
224 {
225         Session *session;
226         PrefsAccount *ac;
227         const gchar *userid = NULL;
228         gchar *passwd = NULL;
229         gushort port;
230
231         g_return_val_if_fail(folder != NULL, NULL);
232         g_return_val_if_fail(folder->account != NULL, NULL);
233
234         ac = folder->account;
235         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
236                 userid = ac->userid;
237                 if (ac->passwd && ac->passwd[0])
238                         passwd = g_strdup(ac->passwd);
239                 else
240                         passwd = input_dialog_query_password(ac->nntp_server,
241                                                              userid);
242         }
243
244 #if USE_SSL
245         port = ac->set_nntpport ? ac->nntpport
246                 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
247         session = news_session_new(ac->nntp_server, port, userid, passwd,
248                                    ac->ssl_nntp);
249 #else
250         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
251         session = news_session_new(ac->nntp_server, port, userid, passwd);
252 #endif
253
254         g_free(passwd);
255
256         return session;
257 }
258
259 NNTPSession *news_session_get(Folder *folder)
260 {
261         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
262
263         g_return_val_if_fail(folder != NULL, NULL);
264         g_return_val_if_fail(folder->type == F_NEWS, NULL);
265         g_return_val_if_fail(folder->account != NULL, NULL);
266
267         if (!rfolder->session) {
268                 rfolder->session = news_session_new_for_folder(folder);
269                 statusbar_pop_all();
270                 return NNTP_SESSION(rfolder->session);
271         }
272
273         if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
274                 rfolder->session->last_access_time = time(NULL);
275                 statusbar_pop_all();
276                 return NNTP_SESSION(rfolder->session);
277         }
278
279         if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
280             != NN_SUCCESS) {
281                 log_warning(_("NNTP connection to %s:%d has been"
282                               " disconnected. Reconnecting...\n"),
283                             folder->account->nntp_server,
284                             folder->account->set_nntpport ?
285                             folder->account->nntpport : NNTP_PORT);
286                 session_destroy(rfolder->session);
287                 rfolder->session = news_session_new_for_folder(folder);
288         }
289
290         if (rfolder->session)
291                 rfolder->session->last_access_time = time(NULL);
292         statusbar_pop_all();
293         return NNTP_SESSION(rfolder->session);
294 }
295
296 GSList *news_get_article_list(Folder *folder, FolderItem *item,
297                               gboolean use_cache)
298 {
299         GSList *alist;
300         NNTPSession *session;
301
302         g_return_val_if_fail(folder != NULL, NULL);
303         g_return_val_if_fail(item != NULL, NULL);
304         g_return_val_if_fail(folder->type == F_NEWS, NULL);
305
306         session = news_session_get(folder);
307
308         if (!session) {
309                 alist = procmsg_read_cache(item, FALSE);
310                 item->last_num = procmsg_get_last_num_in_msg_list(alist);
311         } else if (use_cache) {
312                 GSList *newlist;
313                 gint cache_last;
314                 gint first, last;
315
316                 alist = procmsg_read_cache(item, FALSE);
317
318                 cache_last = procmsg_get_last_num_in_msg_list(alist);
319                 newlist = news_get_uncached_articles
320                         (session, item, cache_last, &first, &last);
321                 if (first == 0 && last == 0) {
322                         news_delete_all_articles(item);
323                         procmsg_msg_list_free(alist);
324                         alist = NULL;
325                 } else {
326                         alist = news_delete_old_articles(alist, item, first);
327                         news_delete_expired_caches(alist, item);
328                 }
329
330                 alist = g_slist_concat(alist, newlist);
331
332                 item->last_num = last;
333         } else {
334                 gint last;
335
336                 alist = news_get_uncached_articles
337                         (session, item, 0, NULL, &last);
338                 news_delete_all_articles(item);
339                 item->last_num = last;
340         }
341
342         procmsg_set_flags(alist, item);
343
344         statusbar_pop_all();
345
346         return alist;
347 }
348
349 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
350 {
351         gchar *path, *filename;
352         NNTPSession *session;
353         gint ok;
354
355         g_return_val_if_fail(folder != NULL, NULL);
356         g_return_val_if_fail(item != NULL, NULL);
357
358         path = folder_item_get_path(item);
359         if (!is_dir_exist(path))
360                 make_dir_hier(path);
361         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
362         g_free(path);
363
364         if (is_file_exist(filename)) {
365                 debug_print("article %d has been already cached.\n", num);
366                 return filename;
367         }
368
369         session = news_session_get(folder);
370         if (!session) {
371                 g_free(filename);
372                 return NULL;
373         }
374
375         ok = news_select_group(session, item->path, NULL, NULL, NULL);
376         statusbar_pop_all();
377         if (ok != NN_SUCCESS) {
378                 g_warning(_("can't select group %s\n"), item->path);
379                 g_free(filename);
380                 return NULL;
381         }
382
383         debug_print("getting article %d...\n", num);
384         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
385                               num, filename);
386         statusbar_pop_all();
387         if (ok < 0) {
388                 g_warning(_("can't read article %d\n"), num);
389                 g_free(filename);
390                 return NULL;
391         }
392
393         return filename;
394 }
395
396 gint news_scan_group(Folder *folder, FolderItem *item)
397 {
398         NNTPSession *session;
399         gint num = 0, first = 0, last = 0;
400         gint ok;
401
402         g_return_val_if_fail(folder != NULL, -1);
403         g_return_val_if_fail(item != NULL, -1);
404
405         session = news_session_get(folder);
406         if (!session) return -1;
407
408         ok = news_select_group(session, item->path, &num, &first, &last);
409         if (ok != NN_SUCCESS) {
410                 log_warning(_("can't set group: %s\n"), item->path);
411                 return -1;
412         }
413
414         if (num == 0) {
415                 item->new = item->unread = item->total = item->last_num = 0;
416                 return 0;
417         }
418
419 /*
420         path = folder_item_get_path(item);
421         if (path && is_dir_exist(path)) {
422                 procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
423                                      first);
424         }
425         g_free(path);
426
427         if (max < first || last < min)
428                 new = unread = total = num;
429         else {
430                 if (min < first)
431                         min = first;
432
433                 if (last < max)
434                         max = last;
435                 else if (max < last) {
436                         new += last - max;
437                         unread += last - max;
438                 }
439
440                 if (new > num) new = num;
441                 if (unread > num) unread = num;
442         }
443
444         item->new = new;
445         item->unread = unread;
446         item->total = num;
447         item->last_num = last;
448 */
449         return 0;
450 }
451
452 static NewsGroupInfo *news_group_info_new(const gchar *name,
453                                           gint first, gint last, gchar type)
454 {
455         NewsGroupInfo *ginfo;
456
457         ginfo = g_new(NewsGroupInfo, 1);
458         ginfo->name = g_strdup(name);
459         ginfo->first = first;
460         ginfo->last = last;
461         ginfo->type = type;
462
463         return ginfo;
464 }
465
466 static void news_group_info_free(NewsGroupInfo *ginfo)
467 {
468         g_free(ginfo->name);
469         g_free(ginfo);
470 }
471
472 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
473                                     NewsGroupInfo *ginfo2)
474 {
475         return g_strcasecmp(ginfo1->name, ginfo2->name);
476 }
477
478 GSList *news_get_group_list(Folder *folder)
479 {
480         gchar *path, *filename;
481         FILE *fp;
482         GSList *list = NULL;
483         GSList *last = NULL;
484         gchar buf[NNTPBUFSIZE];
485
486         g_return_val_if_fail(folder != NULL, NULL);
487         g_return_val_if_fail(folder->type == F_NEWS, NULL);
488
489         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
490         if (!is_dir_exist(path))
491                 make_dir_hier(path);
492         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
493         g_free(path);
494
495         if ((fp = fopen(filename, "rb")) == NULL) {
496                 NNTPSession *session;
497
498                 session = news_session_get(folder);
499                 if (!session) {
500                         g_free(filename);
501                         return NULL;
502                 }
503
504                 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
505                         g_free(filename);
506                         statusbar_pop_all();
507                         return NULL;
508                 }
509                 statusbar_pop_all();
510                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
511                         log_warning(_("can't retrieve newsgroup list\n"));
512                         session_destroy(SESSION(session));
513                         REMOTE_FOLDER(folder)->session = NULL;
514                         g_free(filename);
515                         return NULL;
516                 }
517
518                 if ((fp = fopen(filename, "rb")) == NULL) {
519                         FILE_OP_ERROR(filename, "fopen");
520                         g_free(filename);
521                         return NULL;
522                 }
523         }
524
525         while (fgets(buf, sizeof(buf), fp) != NULL) {
526                 gchar *p = buf;
527                 gchar *name;
528                 gint last_num;
529                 gint first_num;
530                 gchar type;
531                 NewsGroupInfo *ginfo;
532
533                 p = strchr(p, ' ');
534                 if (!p) continue;
535                 *p = '\0';
536                 p++;
537                 name = buf;
538
539                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
540                         continue;
541
542                 ginfo = news_group_info_new(name, first_num, last_num, type);
543
544                 if (!last)
545                         last = list = g_slist_append(NULL, ginfo);
546                 else {
547                         last = g_slist_append(last, ginfo);
548                         last = last->next;
549                 }
550         }
551
552         fclose(fp);
553         g_free(filename);
554
555         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
556
557         statusbar_pop_all();
558
559         return list;
560 }
561
562 void news_group_list_free(GSList *group_list)
563 {
564         GSList *cur;
565
566         if (!group_list) return;
567
568         for (cur = group_list; cur != NULL; cur = cur->next)
569                 news_group_info_free((NewsGroupInfo *)cur->data);
570         g_slist_free(group_list);
571 }
572
573 void news_remove_group_list_cache(Folder *folder)
574 {
575         gchar *path, *filename;
576
577         g_return_if_fail(folder != NULL);
578         g_return_if_fail(folder->type == F_NEWS);
579
580         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
581         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
582         g_free(path);
583
584         if (is_file_exist(filename)) {
585                 if (remove(filename) < 0)
586                         FILE_OP_ERROR(filename, "remove");
587         }
588         g_free(filename);
589 }
590
591 gint news_post(Folder *folder, const gchar *file)
592 {
593         FILE *fp;
594         gint ok;
595
596         g_return_val_if_fail(folder != NULL, -1);
597         g_return_val_if_fail(folder->type == F_NEWS, -1);
598         g_return_val_if_fail(file != NULL, -1);
599
600         if ((fp = fopen(file, "rb")) == NULL) {
601                 FILE_OP_ERROR(file, "fopen");
602                 return -1;
603         }
604
605         ok = news_post_stream(folder, fp);
606
607         fclose(fp);
608
609         statusbar_pop_all();
610
611         return ok;
612 }
613
614 gint news_post_stream(Folder *folder, FILE *fp)
615 {
616         NNTPSession *session;
617         gint ok;
618
619         g_return_val_if_fail(folder != NULL, -1);
620         g_return_val_if_fail(folder->type == F_NEWS, -1);
621         g_return_val_if_fail(fp != NULL, -1);
622
623         session = news_session_get(folder);
624         if (!session) return -1;
625
626         ok = nntp_post(session->nntp_sock, fp);
627         if (ok != NN_SUCCESS) {
628                 log_warning(_("can't post article.\n"));
629                 return -1;
630         }
631
632         return 0;
633 }
634
635 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
636                                  gint num, gchar *filename)
637 {
638         gchar *msgid;
639
640         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
641             != NN_SUCCESS)
642                 return -1;
643
644         debug_print("Message-Id = %s, num = %d\n", msgid, num);
645         g_free(msgid);
646
647         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
648                 log_warning(_("can't retrieve article %d\n"), num);
649                 return -1;
650         }
651
652         return 0;
653 }
654
655 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
656 {
657         gchar * dir;
658         gint r;
659
660         dir = folder_item_get_path(item);
661         debug_print("news_remove_msg: removing msg %d in %s\n",num,dir);
662         r = remove_numbered_files(dir, num, num);
663         g_free(dir);
664
665         return r;
666 }
667
668 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
669 {
670         return news_get_article_cmd(session, "ARTICLE", num, filename);
671 }
672
673 /**
674  * news_select_group:
675  * @session: Active NNTP session.
676  * @group: Newsgroup name.
677  * @num: Estimated number of articles.
678  * @first: First article number.
679  * @last: Last article number.
680  *
681  * Select newsgroup @group with the GROUP command if it is not already
682  * selected in @session, or article numbers need to be returned.
683  *
684  * Return value: NNTP result code.
685  **/
686 static gint news_select_group(NNTPSession *session, const gchar *group,
687                               gint *num, gint *first, gint *last)
688 {
689         gint ok;
690         gint num_, first_, last_;
691
692         if (!num || !first || !last) {
693                 if (session->group && g_strcasecmp(session->group, group) == 0)
694                         return NN_SUCCESS;
695                 num = &num_;
696                 first = &first_;
697                 last = &last_;
698         }
699
700         g_free(session->group);
701         session->group = NULL;
702
703         ok = nntp_group(session->nntp_sock, group, num, first, last);
704         if (ok == NN_SUCCESS)
705                 session->group = g_strdup(group);
706
707         return ok;
708 }
709
710 static GSList *news_get_uncached_articles(NNTPSession *session,
711                                           FolderItem *item, gint cache_last,
712                                           gint *rfirst, gint *rlast)
713 {
714         gint ok;
715         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
716         gchar buf[NNTPBUFSIZE];
717         GSList *newlist = NULL;
718         GSList *llast = NULL;
719         MsgInfo *msginfo;
720
721         if (rfirst) *rfirst = -1;
722         if (rlast)  *rlast  = -1;
723
724         g_return_val_if_fail(session != NULL, NULL);
725         g_return_val_if_fail(item != NULL, NULL);
726         g_return_val_if_fail(item->folder != NULL, NULL);
727         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
728
729         ok = news_select_group(session, item->path, &num, &first, &last);
730         if (ok != NN_SUCCESS) {
731                 log_warning(_("can't set group: %s\n"), item->path);
732                 return NULL;
733         }
734
735         /* calculate getting overview range */
736         if (first > last) {
737                 log_warning(_("invalid article range: %d - %d\n"),
738                             first, last);
739                 return NULL;
740         }
741
742         if (rfirst) *rfirst = first;
743         if (rlast)  *rlast  = last;
744
745         if (cache_last < first)
746                 begin = first;
747         else if (last < cache_last)
748                 begin = first;
749         else if (last == cache_last) {
750                 debug_print("no new articles.\n");
751                 return NULL;
752         } else
753                 begin = cache_last + 1;
754         end = last;
755
756         if (prefs_common.max_articles > 0 &&
757             end - begin + 1 > prefs_common.max_articles)
758                 begin = end - prefs_common.max_articles + 1;
759
760         log_message(_("getting xover %d - %d in %s...\n"),
761                     begin, end, item->path);
762         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
763                 log_warning(_("can't get xover\n"));
764                 return NULL;
765         }
766
767         for (;;) {
768                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
769                         log_warning(_("error occurred while getting xover.\n"));
770                         return newlist;
771                 }
772
773                 if (buf[0] == '.' && buf[1] == '\r') break;
774
775                 msginfo = news_parse_xover(buf);
776                 if (!msginfo) {
777                         log_warning(_("invalid xover line: %s\n"), buf);
778                         continue;
779                 }
780
781                 msginfo->folder = item;
782                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
783                 msginfo->flags.tmp_flags = MSG_NEWS;
784                 msginfo->newsgroups = g_strdup(item->path);
785
786                 if (!newlist)
787                         llast = newlist = g_slist_append(newlist, msginfo);
788                 else {
789                         llast = g_slist_append(llast, msginfo);
790                         llast = llast->next;
791                 }
792         }
793
794         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
795                 log_warning(_("can't get xhdr\n"));
796                 return newlist;
797         }
798
799         llast = newlist;
800
801         for (;;) {
802                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
803                         log_warning(_("error occurred while getting xhdr.\n"));
804                         return newlist;
805                 }
806
807                 if (buf[0] == '.' && buf[1] == '\r') break;
808                 if (!llast) {
809                         g_warning("llast == NULL\n");
810                         continue;
811                 }
812
813                 msginfo = (MsgInfo *)llast->data;
814                 msginfo->to = news_parse_xhdr(buf, msginfo);
815
816                 llast = llast->next;
817         }
818
819         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
820                 log_warning(_("can't get xhdr\n"));
821                 return newlist;
822         }
823
824         llast = newlist;
825
826         for (;;) {
827                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
828                         log_warning(_("error occurred while getting xhdr.\n"));
829                         return newlist;
830                 }
831
832                 if (buf[0] == '.' && buf[1] == '\r') break;
833                 if (!llast) {
834                         g_warning("llast == NULL\n");
835                         continue;
836                 }
837
838                 msginfo = (MsgInfo *)llast->data;
839                 msginfo->cc = news_parse_xhdr(buf, msginfo);
840
841                 llast = llast->next;
842         }
843
844         return newlist;
845 }
846
847 #define PARSE_ONE_PARAM(p, srcp) \
848 { \
849         p = strchr(srcp, '\t'); \
850         if (!p) return NULL; \
851         else \
852                 *p++ = '\0'; \
853 }
854
855 static MsgInfo *news_parse_xover(const gchar *xover_str)
856 {
857         MsgInfo *msginfo;
858         gchar buf[NNTPBUFSIZE];
859         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
860         gchar *p;
861         gint num, size_int, line_int;
862         gchar *xover_buf;
863
864         Xstrdup_a(xover_buf, xover_str, return NULL);
865
866         PARSE_ONE_PARAM(subject, xover_buf);
867         PARSE_ONE_PARAM(sender, subject);
868         PARSE_ONE_PARAM(date, sender);
869         PARSE_ONE_PARAM(msgid, date);
870         PARSE_ONE_PARAM(ref, msgid);
871         PARSE_ONE_PARAM(size, ref);
872         PARSE_ONE_PARAM(line, size);
873         PARSE_ONE_PARAM(xref, line);
874
875         tmp = strchr(xref, '\t');
876         if (!tmp) tmp = strchr(line, '\r');
877         if (!tmp) tmp = strchr(line, '\n');
878         if (tmp) *tmp = '\0';
879
880         num = atoi(xover_str);
881         size_int = atoi(size);
882         line_int = atoi(line);
883
884         /* set MsgInfo */
885         msginfo = procmsg_msginfo_new();
886         msginfo->msgnum = num;
887         msginfo->size = size_int;
888
889         msginfo->date = g_strdup(date);
890         msginfo->date_t = procheader_date_parse(NULL, date, 0);
891
892         conv_unmime_header(buf, sizeof(buf), sender, NULL);
893         msginfo->from = g_strdup(buf);
894         msginfo->fromname = procheader_get_fromname(buf);
895
896         conv_unmime_header(buf, sizeof(buf), subject, NULL);
897         msginfo->subject = g_strdup(buf);
898
899         extract_parenthesis(msgid, '<', '>');
900         remove_space(msgid);
901         if (*msgid != '\0')
902                 msginfo->msgid = g_strdup(msgid);
903
904         msginfo->references = g_strdup(ref);
905         eliminate_parenthesis(ref, '(', ')');
906         if ((p = strrchr(ref, '<')) != NULL) {
907                 extract_parenthesis(p, '<', '>');
908                 remove_space(p);
909                 if (*p != '\0')
910                         msginfo->inreplyto = g_strdup(p);
911         }
912
913         msginfo->xref = g_strdup(xref);
914         p = msginfo->xref+strlen(msginfo->xref) - 1;
915         while (*p == '\r' || *p == '\n') {
916                 *p = '\0';
917                 p--;
918         }
919
920         return msginfo;
921 }
922
923 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
924 {
925         gchar *p;
926         gchar *tmp;
927         gint num;
928
929         p = strchr(xhdr_str, ' ');
930         if (!p)
931                 return NULL;
932         else
933                 p++;
934
935         num = atoi(xhdr_str);
936         if (msginfo->msgnum != num) return NULL;
937
938         tmp = strchr(p, '\r');
939         if (!tmp) tmp = strchr(p, '\n');
940
941         if (tmp)
942                 return g_strndup(p, tmp - p);
943         else
944                 return g_strdup(p);
945 }
946
947 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
948                                         gint first)
949 {
950         GSList *cur, *next;
951         MsgInfo *msginfo;
952         gchar *dir;
953
954         g_return_val_if_fail(item != NULL, alist);
955         g_return_val_if_fail(item->folder != NULL, alist);
956         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
957
958         if (first < 2) return alist;
959
960         debug_print("Deleting cached articles 1 - %d ...\n", first - 1);
961
962         dir = folder_item_get_path(item);
963         remove_numbered_files(dir, 1, first - 1);
964         g_free(dir);
965
966         for (cur = alist; cur != NULL; ) {
967                 next = cur->next;
968
969                 msginfo = (MsgInfo *)cur->data;
970                 if (msginfo && msginfo->msgnum < first) {
971                         procmsg_msginfo_free(msginfo);
972                         alist = g_slist_remove(alist, msginfo);
973                 }
974
975                 cur = next;
976         }
977
978         return alist;
979 }
980
981 static void news_delete_all_articles(FolderItem *item)
982 {
983         gchar *dir;
984
985         g_return_if_fail(item != NULL);
986         g_return_if_fail(item->folder != NULL);
987         g_return_if_fail(item->folder->type == F_NEWS);
988
989         debug_print("Deleting all cached articles...\n");
990
991         dir = folder_item_get_path(item);
992         remove_all_numbered_files(dir);
993         g_free(dir);
994 }
995
996 static void news_delete_expired_caches(GSList *alist, FolderItem *item)
997 {
998         gchar *dir;
999
1000         g_return_if_fail(item != NULL);
1001         g_return_if_fail(item->folder != NULL);
1002         g_return_if_fail(item->folder->type == F_NEWS);
1003
1004         debug_print("Deleting expired cached articles...\n");
1005
1006         dir = folder_item_get_path(item);
1007         remove_expired_files(dir, 24 * 7);
1008         g_free(dir);
1009 }
1010
1011 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
1012 {
1013         gchar * tmp;
1014         FILE * tmpfp;
1015         gchar buf[BUFFSIZE];
1016
1017         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1018                               G_DIR_SEPARATOR, (gint)msginfo);
1019         if (tmp == NULL)
1020                 return -1;
1021
1022         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1023                 FILE_OP_ERROR(tmp, "fopen");
1024                 return -1;
1025         }
1026         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1027                 FILE_OP_ERROR(tmp, "chmod");
1028                 g_warning(_("can't change file mode\n"));
1029         }
1030         
1031         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
1032         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
1033         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
1034         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
1035         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
1036         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
1037         get_rfc822_date(buf, sizeof(buf));
1038         fprintf(tmpfp, "Date: %s\r\n", buf);
1039         fprintf(tmpfp, "\r\n");
1040         fprintf(tmpfp, "removed with sylpheed\r\n");
1041
1042         fclose(tmpfp);
1043
1044         news_post(folder, tmp);
1045         remove(tmp);
1046
1047         g_free(tmp);
1048
1049         return 0;
1050 }
1051
1052 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list)
1053 {
1054         NNTPSession *session;
1055         gint i, ok, num, first, last, nummsgs = 0;
1056         gchar *dir;
1057
1058         g_return_val_if_fail(item != NULL, -1);
1059         g_return_val_if_fail(item->folder != NULL, -1);
1060         g_return_val_if_fail(item->folder->type == F_NEWS, -1);
1061
1062         session = news_session_get(folder);
1063         g_return_val_if_fail(session != NULL, -1);
1064
1065         ok = news_select_group(session, item->path, &num, &first, &last);
1066         if (ok != NN_SUCCESS) {
1067                 log_warning(_("can't set group: %s\n"), item->path);
1068                 return -1;
1069         }
1070
1071         if(last < first) {
1072                 log_warning(_("invalid article range: %d - %d\n"),
1073                             first, last);
1074                 return 0;
1075         }
1076
1077         for(i = first; i <= last; i++) {
1078                 *msgnum_list = g_slist_prepend(*msgnum_list, GINT_TO_POINTER(i));
1079                 nummsgs++;
1080         }
1081
1082         dir = folder_item_get_path(item);
1083         debug_print("removing old messages from %d to %d in %s\n", first, last, dir);
1084         remove_numbered_files(dir, 1, first - 1);
1085         g_free(dir);
1086
1087         return nummsgs;
1088 }
1089
1090 #define READ_TO_LISTEND(hdr) \
1091         while (!(buf[0] == '.' && buf[1] == '\r')) { \
1092                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
1093                         log_warning(_("error occurred while getting %s.\n"), hdr); \
1094                         return msginfo; \
1095                 } \
1096         }
1097
1098 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
1099 {
1100         NNTPSession *session;
1101         MsgInfo *msginfo = NULL;
1102         gchar buf[NNTPBUFSIZE];
1103
1104         session = news_session_get(folder);
1105         g_return_val_if_fail(session != NULL, NULL);
1106         g_return_val_if_fail(item != NULL, NULL);
1107         g_return_val_if_fail(item->folder != NULL, NULL);
1108         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1109
1110         log_message(_("getting xover %d in %s...\n"),
1111                     num, item->path);
1112         if (nntp_xover(session->nntp_sock, num, num) != NN_SUCCESS) {
1113                 log_warning(_("can't get xover\n"));
1114                 return NULL;
1115         }
1116         
1117         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1118                 log_warning(_("error occurred while getting xover.\n"));
1119                 return NULL;
1120         }
1121         
1122         msginfo = news_parse_xover(buf);
1123         if (!msginfo) {
1124                 log_warning(_("invalid xover line: %s\n"), buf);
1125         }
1126
1127         READ_TO_LISTEND("xover");
1128
1129         if(!msginfo)
1130                 return NULL;
1131         
1132         msginfo->folder = item;
1133         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1134         msginfo->flags.tmp_flags = MSG_NEWS;
1135         msginfo->newsgroups = g_strdup(item->path);
1136
1137         if (nntp_xhdr(session->nntp_sock, "to", num, num) != NN_SUCCESS) {
1138                 log_warning(_("can't get xhdr\n"));
1139                 return msginfo;
1140         }
1141
1142         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1143                 log_warning(_("error occurred while getting xhdr.\n"));
1144                 return msginfo;
1145         }
1146
1147         msginfo->to = news_parse_xhdr(buf, msginfo);
1148
1149         READ_TO_LISTEND("xhdr (to)");
1150
1151         if (nntp_xhdr(session->nntp_sock, "cc", num, num) != NN_SUCCESS) {
1152                 log_warning(_("can't get xhdr\n"));
1153                 return msginfo;
1154         }
1155
1156         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1157                 log_warning(_("error occurred while getting xhdr.\n"));
1158                 return msginfo;
1159         }
1160
1161         msginfo->cc = news_parse_xhdr(buf, msginfo);
1162
1163         READ_TO_LISTEND("xhdr (cc)");
1164
1165         return msginfo;
1166 }
1167
1168 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
1169 {
1170         gchar buf[NNTPBUFSIZE];
1171         GSList *newlist = NULL;
1172         GSList *llast = NULL;
1173         MsgInfo *msginfo;
1174
1175         g_return_val_if_fail(session != NULL, NULL);
1176         g_return_val_if_fail(item != NULL, NULL);
1177
1178         log_message(_("getting xover %d - %d in %s...\n"),
1179                     begin, end, item->path);
1180         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
1181                 log_warning(_("can't get xover\n"));
1182                 return NULL;
1183         }
1184
1185         for (;;) {
1186                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1187                         log_warning(_("error occurred while getting xover.\n"));
1188                         return newlist;
1189                 }
1190
1191                 if (buf[0] == '.' && buf[1] == '\r') break;
1192
1193                 msginfo = news_parse_xover(buf);
1194                 if (!msginfo) {
1195                         log_warning(_("invalid xover line: %s\n"), buf);
1196                         continue;
1197                 }
1198
1199                 msginfo->folder = item;
1200                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1201                 msginfo->flags.tmp_flags = MSG_NEWS;
1202                 msginfo->newsgroups = g_strdup(item->path);
1203
1204                 if (!newlist)
1205                         llast = newlist = g_slist_append(newlist, msginfo);
1206                 else {
1207                         llast = g_slist_append(llast, msginfo);
1208                         llast = llast->next;
1209                 }
1210         }
1211
1212         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
1213                 log_warning(_("can't get xhdr\n"));
1214                 return newlist;
1215         }
1216
1217         llast = newlist;
1218
1219         for (;;) {
1220                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1221                         log_warning(_("error occurred while getting xhdr.\n"));
1222                         return newlist;
1223                 }
1224
1225                 if (buf[0] == '.' && buf[1] == '\r') break;
1226                 if (!llast) {
1227                         g_warning("llast == NULL\n");
1228                         continue;
1229                 }
1230
1231                 msginfo = (MsgInfo *)llast->data;
1232                 msginfo->to = news_parse_xhdr(buf, msginfo);
1233
1234                 llast = llast->next;
1235         }
1236
1237         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
1238                 log_warning(_("can't get xhdr\n"));
1239                 return newlist;
1240         }
1241
1242         llast = newlist;
1243
1244         for (;;) {
1245                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1246                         log_warning(_("error occurred while getting xhdr.\n"));
1247                         return newlist;
1248                 }
1249
1250                 if (buf[0] == '.' && buf[1] == '\r') break;
1251                 if (!llast) {
1252                         g_warning("llast == NULL\n");
1253                         continue;
1254                 }
1255
1256                 msginfo = (MsgInfo *)llast->data;
1257                 msginfo->cc = news_parse_xhdr(buf, msginfo);
1258
1259                 llast = llast->next;
1260         }
1261
1262         return newlist;
1263 }
1264
1265 gint news_fetch_msgnum_sort(gconstpointer a, gconstpointer b)
1266 {
1267         return (GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b));
1268 }
1269
1270 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1271 {
1272         NNTPSession *session;
1273         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1274         guint first, last, next;
1275         
1276         g_return_val_if_fail(folder != NULL, NULL);
1277         g_return_val_if_fail(folder->type == F_NEWS, NULL);
1278         g_return_val_if_fail(msgnum_list != NULL, NULL);
1279         g_return_val_if_fail(item != NULL, NULL);
1280         
1281         session = news_session_get(folder);
1282         g_return_val_if_fail(session != NULL, NULL);
1283
1284         tmp_msgnum_list = g_slist_copy(msgnum_list);
1285         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, news_fetch_msgnum_sort);
1286
1287         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1288         last = first;
1289         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1290                 next = GPOINTER_TO_INT(elem->data);
1291                 if(next != (last + 1)) {
1292                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1293                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1294                         first = next;
1295                 }
1296                 last = next;
1297         }
1298         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1299         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1300
1301         g_slist_free(tmp_msgnum_list);
1302         
1303         return msginfo_list;
1304 }