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