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