sync with 0.8.2cvs8
[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         NNTPSession *session;
589         FILE *fp;
590         gint ok;
591
592         g_return_val_if_fail(folder != NULL, -1);
593         g_return_val_if_fail(folder->type == F_NEWS, -1);
594         g_return_val_if_fail(file != NULL, -1);
595
596         session = news_session_get(folder);
597         if (!session) return -1;
598
599         if ((fp = fopen(file, "rb")) == NULL) {
600                 FILE_OP_ERROR(file, "fopen");
601                 return -1;
602         }
603
604         ok = nntp_post(session->nntp_sock, fp);
605         if (ok != NN_SUCCESS) {
606                 log_warning(_("can't post article.\n"));
607                 return -1;
608         }
609
610         fclose(fp);
611
612         statusbar_pop_all();
613
614         return 0;
615 }
616
617 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
618                                  gint num, gchar *filename)
619 {
620         gchar *msgid;
621
622         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
623             != NN_SUCCESS)
624                 return -1;
625
626         debug_print("Message-Id = %s, num = %d\n", msgid, num);
627         g_free(msgid);
628
629         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
630                 log_warning(_("can't retrieve article %d\n"), num);
631                 return -1;
632         }
633
634         return 0;
635 }
636
637 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
638 {
639         gchar * dir;
640         gint r;
641
642         dir = folder_item_get_path(item);
643         printf("removing %d in %s\n",num,dir);
644         r = remove_numbered_files(dir, num, num);
645         g_free(dir);
646
647         return r;
648 }
649
650 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
651 {
652         return news_get_article_cmd(session, "ARTICLE", num, filename);
653 }
654
655 /**
656  * news_select_group:
657  * @session: Active NNTP session.
658  * @group: Newsgroup name.
659  * @num: Estimated number of articles.
660  * @first: First article number.
661  * @last: Last article number.
662  *
663  * Select newsgroup @group with the GROUP command if it is not already
664  * selected in @session, or article numbers need to be returned.
665  *
666  * Return value: NNTP result code.
667  **/
668 static gint news_select_group(NNTPSession *session, const gchar *group,
669                               gint *num, gint *first, gint *last)
670 {
671         gint ok;
672         gint num_, first_, last_;
673
674         if (!num || !first || !last) {
675                 if (session->group && g_strcasecmp(session->group, group) == 0)
676                         return NN_SUCCESS;
677                 num = &num_;
678                 first = &first_;
679                 last = &last_;
680         }
681
682         g_free(session->group);
683         session->group = NULL;
684
685         ok = nntp_group(session->nntp_sock, group, num, first, last);
686         if (ok == NN_SUCCESS)
687                 session->group = g_strdup(group);
688
689         return ok;
690 }
691
692 static GSList *news_get_uncached_articles(NNTPSession *session,
693                                           FolderItem *item, gint cache_last,
694                                           gint *rfirst, gint *rlast)
695 {
696         gint ok;
697         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
698         gchar buf[NNTPBUFSIZE];
699         GSList *newlist = NULL;
700         GSList *llast = NULL;
701         MsgInfo *msginfo;
702
703         if (rfirst) *rfirst = -1;
704         if (rlast)  *rlast  = -1;
705
706         g_return_val_if_fail(session != NULL, NULL);
707         g_return_val_if_fail(item != NULL, NULL);
708         g_return_val_if_fail(item->folder != NULL, NULL);
709         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
710
711         ok = news_select_group(session, item->path, &num, &first, &last);
712         if (ok != NN_SUCCESS) {
713                 log_warning(_("can't set group: %s\n"), item->path);
714                 return NULL;
715         }
716
717         /* calculate getting overview range */
718         if (first > last) {
719                 log_warning(_("invalid article range: %d - %d\n"),
720                             first, last);
721                 return NULL;
722         }
723
724         if (rfirst) *rfirst = first;
725         if (rlast)  *rlast  = last;
726
727         if (cache_last < first)
728                 begin = first;
729         else if (last < cache_last)
730                 begin = first;
731         else if (last == cache_last) {
732                 debug_print("no new articles.\n");
733                 return NULL;
734         } else
735                 begin = cache_last + 1;
736         end = last;
737
738         if (prefs_common.max_articles > 0 &&
739             end - begin + 1 > prefs_common.max_articles)
740                 begin = end - prefs_common.max_articles + 1;
741
742         log_message(_("getting xover %d - %d in %s...\n"),
743                     begin, end, item->path);
744         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
745                 log_warning(_("can't get xover\n"));
746                 return NULL;
747         }
748
749         for (;;) {
750                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
751                         log_warning(_("error occurred while getting xover.\n"));
752                         return newlist;
753                 }
754
755                 if (buf[0] == '.' && buf[1] == '\r') break;
756
757                 msginfo = news_parse_xover(buf);
758                 if (!msginfo) {
759                         log_warning(_("invalid xover line: %s\n"), buf);
760                         continue;
761                 }
762
763                 msginfo->folder = item;
764                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
765                 msginfo->flags.tmp_flags = MSG_NEWS;
766                 msginfo->newsgroups = g_strdup(item->path);
767
768                 if (!newlist)
769                         llast = newlist = g_slist_append(newlist, msginfo);
770                 else {
771                         llast = g_slist_append(llast, msginfo);
772                         llast = llast->next;
773                 }
774         }
775
776         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
777                 log_warning(_("can't get xhdr\n"));
778                 return newlist;
779         }
780
781         llast = newlist;
782
783         for (;;) {
784                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
785                         log_warning(_("error occurred while getting xhdr.\n"));
786                         return newlist;
787                 }
788
789                 if (buf[0] == '.' && buf[1] == '\r') break;
790                 if (!llast) {
791                         g_warning("llast == NULL\n");
792                         continue;
793                 }
794
795                 msginfo = (MsgInfo *)llast->data;
796                 msginfo->to = news_parse_xhdr(buf, msginfo);
797
798                 llast = llast->next;
799         }
800
801         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
802                 log_warning(_("can't get xhdr\n"));
803                 return newlist;
804         }
805
806         llast = newlist;
807
808         for (;;) {
809                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
810                         log_warning(_("error occurred while getting xhdr.\n"));
811                         return newlist;
812                 }
813
814                 if (buf[0] == '.' && buf[1] == '\r') break;
815                 if (!llast) {
816                         g_warning("llast == NULL\n");
817                         continue;
818                 }
819
820                 msginfo = (MsgInfo *)llast->data;
821                 msginfo->cc = news_parse_xhdr(buf, msginfo);
822
823                 llast = llast->next;
824         }
825
826         return newlist;
827 }
828
829 #define PARSE_ONE_PARAM(p, srcp) \
830 { \
831         p = strchr(srcp, '\t'); \
832         if (!p) return NULL; \
833         else \
834                 *p++ = '\0'; \
835 }
836
837 static MsgInfo *news_parse_xover(const gchar *xover_str)
838 {
839         MsgInfo *msginfo;
840         gchar buf[NNTPBUFSIZE];
841         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
842         gchar *p;
843         gint num, size_int, line_int;
844         gchar *xover_buf;
845
846         Xstrdup_a(xover_buf, xover_str, return NULL);
847
848         PARSE_ONE_PARAM(subject, xover_buf);
849         PARSE_ONE_PARAM(sender, subject);
850         PARSE_ONE_PARAM(date, sender);
851         PARSE_ONE_PARAM(msgid, date);
852         PARSE_ONE_PARAM(ref, msgid);
853         PARSE_ONE_PARAM(size, ref);
854         PARSE_ONE_PARAM(line, size);
855         PARSE_ONE_PARAM(xref, line);
856
857         tmp = strchr(xref, '\t');
858         if (!tmp) tmp = strchr(line, '\r');
859         if (!tmp) tmp = strchr(line, '\n');
860         if (tmp) *tmp = '\0';
861
862         num = atoi(xover_str);
863         size_int = atoi(size);
864         line_int = atoi(line);
865
866         /* set MsgInfo */
867         msginfo = procmsg_msginfo_new();
868         msginfo->msgnum = num;
869         msginfo->size = size_int;
870
871         msginfo->date = g_strdup(date);
872         msginfo->date_t = procheader_date_parse(NULL, date, 0);
873
874         conv_unmime_header(buf, sizeof(buf), sender, NULL);
875         msginfo->from = g_strdup(buf);
876         msginfo->fromname = procheader_get_fromname(buf);
877
878         conv_unmime_header(buf, sizeof(buf), subject, NULL);
879         msginfo->subject = g_strdup(buf);
880
881         extract_parenthesis(msgid, '<', '>');
882         remove_space(msgid);
883         if (*msgid != '\0')
884                 msginfo->msgid = g_strdup(msgid);
885
886         msginfo->references = g_strdup(ref);
887         eliminate_parenthesis(ref, '(', ')');
888         if ((p = strrchr(ref, '<')) != NULL) {
889                 extract_parenthesis(p, '<', '>');
890                 remove_space(p);
891                 if (*p != '\0')
892                         msginfo->inreplyto = g_strdup(p);
893         }
894
895         msginfo->xref = g_strdup(xref);
896         p = msginfo->xref+strlen(msginfo->xref) - 1;
897         while (*p == '\r' || *p == '\n') {
898                 *p = '\0';
899                 p--;
900         }
901
902         return msginfo;
903 }
904
905 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
906 {
907         gchar *p;
908         gchar *tmp;
909         gint num;
910
911         p = strchr(xhdr_str, ' ');
912         if (!p)
913                 return NULL;
914         else
915                 p++;
916
917         num = atoi(xhdr_str);
918         if (msginfo->msgnum != num) return NULL;
919
920         tmp = strchr(p, '\r');
921         if (!tmp) tmp = strchr(p, '\n');
922
923         if (tmp)
924                 return g_strndup(p, tmp - p);
925         else
926                 return g_strdup(p);
927 }
928
929 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
930                                         gint first)
931 {
932         GSList *cur, *next;
933         MsgInfo *msginfo;
934         gchar *dir;
935
936         g_return_val_if_fail(item != NULL, alist);
937         g_return_val_if_fail(item->folder != NULL, alist);
938         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
939
940         if (first < 2) return alist;
941
942         debug_print("Deleting cached articles 1 - %d ...\n", first - 1);
943
944         dir = folder_item_get_path(item);
945         remove_numbered_files(dir, 1, first - 1);
946         g_free(dir);
947
948         for (cur = alist; cur != NULL; ) {
949                 next = cur->next;
950
951                 msginfo = (MsgInfo *)cur->data;
952                 if (msginfo && msginfo->msgnum < first) {
953                         procmsg_msginfo_free(msginfo);
954                         alist = g_slist_remove(alist, msginfo);
955                 }
956
957                 cur = next;
958         }
959
960         return alist;
961 }
962
963 static void news_delete_all_articles(FolderItem *item)
964 {
965         gchar *dir;
966
967         g_return_if_fail(item != NULL);
968         g_return_if_fail(item->folder != NULL);
969         g_return_if_fail(item->folder->type == F_NEWS);
970
971         debug_print("Deleting all cached articles...\n");
972
973         dir = folder_item_get_path(item);
974         remove_all_numbered_files(dir);
975         g_free(dir);
976 }
977
978 static void news_delete_expired_caches(GSList *alist, FolderItem *item)
979 {
980         gchar *dir;
981
982         g_return_if_fail(item != NULL);
983         g_return_if_fail(item->folder != NULL);
984         g_return_if_fail(item->folder->type == F_NEWS);
985
986         debug_print("Deleting expired cached articles...\n");
987
988         dir = folder_item_get_path(item);
989         remove_expired_files(dir, 24 * 7);
990         g_free(dir);
991 }
992
993 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
994 {
995         gchar * tmp;
996         FILE * tmpfp;
997         gchar buf[BUFFSIZE];
998
999         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
1000                               G_DIR_SEPARATOR, (gint)msginfo);
1001         if (tmp == NULL)
1002                 return -1;
1003
1004         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
1005                 FILE_OP_ERROR(tmp, "fopen");
1006                 return -1;
1007         }
1008         if (change_file_mode_rw(tmpfp, tmp) < 0) {
1009                 FILE_OP_ERROR(tmp, "chmod");
1010                 g_warning(_("can't change file mode\n"));
1011         }
1012         
1013         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
1014         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
1015         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
1016         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
1017         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
1018         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
1019         get_rfc822_date(buf, sizeof(buf));
1020         fprintf(tmpfp, "Date: %s\r\n", buf);
1021         fprintf(tmpfp, "\r\n");
1022         fprintf(tmpfp, "removed with sylpheed\r\n");
1023
1024         fclose(tmpfp);
1025
1026         news_post(folder, tmp);
1027         remove(tmp);
1028
1029         g_free(tmp);
1030
1031         return 0;
1032 }
1033
1034 GSList *news_get_num_list(Folder *folder, FolderItem *item)
1035 {
1036         NNTPSession *session;
1037         gint i, ok, num, first, last;
1038         GSList *msgnum_list = NULL;
1039
1040         session = news_session_get(folder);
1041         g_return_val_if_fail(session != NULL, NULL);
1042         g_return_val_if_fail(item != NULL, NULL);
1043         g_return_val_if_fail(item->folder != NULL, NULL);
1044         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1045
1046         ok = news_select_group(session, item->path, &num, &first, &last);
1047         if (ok != NN_SUCCESS) {
1048                 log_warning(_("can't set group: %s\n"), item->path);
1049                 return NULL;
1050         }
1051
1052         if(last < first) {
1053                 log_warning(_("invalid article range: %d - %d\n"),
1054                             first, last);
1055                 return NULL;
1056         }
1057
1058         for(i = first; i <= last; i++) {
1059                 msgnum_list = g_slist_prepend(msgnum_list, GINT_TO_POINTER(i));
1060         }
1061
1062         return msgnum_list;
1063 }
1064
1065 #define READ_TO_LISTEND(hdr) \
1066         while (!(buf[0] == '.' && buf[1] == '\r')) { \
1067                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
1068                         log_warning(_("error occurred while getting %s.\n"), hdr); \
1069                         return msginfo; \
1070                 } \
1071         }
1072
1073 MsgInfo *news_fetch_msginfo(Folder *folder, FolderItem *item, gint num)
1074 {
1075         NNTPSession *session;
1076         MsgInfo *msginfo = NULL;
1077         gchar buf[NNTPBUFSIZE];
1078
1079         session = news_session_get(folder);
1080         g_return_val_if_fail(session != NULL, NULL);
1081         g_return_val_if_fail(item != NULL, NULL);
1082         g_return_val_if_fail(item->folder != NULL, NULL);
1083         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
1084
1085         log_message(_("getting xover %d in %s...\n"),
1086                     num, item->path);
1087         if (nntp_xover(session->nntp_sock, num, num) != NN_SUCCESS) {
1088                 log_warning(_("can't get xover\n"));
1089                 return NULL;
1090         }
1091         
1092         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1093                 log_warning(_("error occurred while getting xover.\n"));
1094                 return NULL;
1095         }
1096         
1097         msginfo = news_parse_xover(buf);
1098         if (!msginfo) {
1099                 log_warning(_("invalid xover line: %s\n"), buf);
1100         }
1101
1102         READ_TO_LISTEND("xover");
1103
1104         if(!msginfo)
1105                 return NULL;
1106         
1107         msginfo->folder = item;
1108         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1109         msginfo->flags.tmp_flags = MSG_NEWS;
1110         msginfo->newsgroups = g_strdup(item->path);
1111
1112         if (nntp_xhdr(session->nntp_sock, "to", num, num) != NN_SUCCESS) {
1113                 log_warning(_("can't get xhdr\n"));
1114                 return msginfo;
1115         }
1116
1117         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1118                 log_warning(_("error occurred while getting xhdr.\n"));
1119                 return msginfo;
1120         }
1121
1122         msginfo->to = news_parse_xhdr(buf, msginfo);
1123
1124         READ_TO_LISTEND("xhdr (to)");
1125
1126         if (nntp_xhdr(session->nntp_sock, "cc", num, num) != NN_SUCCESS) {
1127                 log_warning(_("can't get xhdr\n"));
1128                 return msginfo;
1129         }
1130
1131         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1132                 log_warning(_("error occurred while getting xhdr.\n"));
1133                 return msginfo;
1134         }
1135
1136         msginfo->cc = news_parse_xhdr(buf, msginfo);
1137
1138         READ_TO_LISTEND("xhdr (cc)");
1139
1140         return msginfo;
1141 }
1142
1143 static GSList *news_fetch_msginfo_from_to(NNTPSession *session, FolderItem *item, guint begin, guint end)
1144 {
1145         gchar buf[NNTPBUFSIZE];
1146         GSList *newlist = NULL;
1147         GSList *llast = NULL;
1148         MsgInfo *msginfo;
1149
1150         g_return_val_if_fail(session != NULL, NULL);
1151         g_return_val_if_fail(item != NULL, NULL);
1152
1153         log_message(_("getting xover %d - %d in %s...\n"),
1154                     begin, end, item->path);
1155         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
1156                 log_warning(_("can't get xover\n"));
1157                 return NULL;
1158         }
1159
1160         for (;;) {
1161                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1162                         log_warning(_("error occurred while getting xover.\n"));
1163                         return newlist;
1164                 }
1165
1166                 if (buf[0] == '.' && buf[1] == '\r') break;
1167
1168                 msginfo = news_parse_xover(buf);
1169                 if (!msginfo) {
1170                         log_warning(_("invalid xover line: %s\n"), buf);
1171                         continue;
1172                 }
1173
1174                 msginfo->folder = item;
1175                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1176                 msginfo->flags.tmp_flags = MSG_NEWS;
1177                 msginfo->newsgroups = g_strdup(item->path);
1178
1179                 if (!newlist)
1180                         llast = newlist = g_slist_append(newlist, msginfo);
1181                 else {
1182                         llast = g_slist_append(llast, msginfo);
1183                         llast = llast->next;
1184                 }
1185         }
1186
1187         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
1188                 log_warning(_("can't get xhdr\n"));
1189                 return newlist;
1190         }
1191
1192         llast = newlist;
1193
1194         for (;;) {
1195                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1196                         log_warning(_("error occurred while getting xhdr.\n"));
1197                         return newlist;
1198                 }
1199
1200                 if (buf[0] == '.' && buf[1] == '\r') break;
1201                 if (!llast) {
1202                         g_warning("llast == NULL\n");
1203                         continue;
1204                 }
1205
1206                 msginfo = (MsgInfo *)llast->data;
1207                 msginfo->to = news_parse_xhdr(buf, msginfo);
1208
1209                 llast = llast->next;
1210         }
1211
1212         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
1213                 log_warning(_("can't get xhdr\n"));
1214                 return newlist;
1215         }
1216
1217         llast = newlist;
1218
1219         for (;;) {
1220                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1221                         log_warning(_("error occurred while getting xhdr.\n"));
1222                         return newlist;
1223                 }
1224
1225                 if (buf[0] == '.' && buf[1] == '\r') break;
1226                 if (!llast) {
1227                         g_warning("llast == NULL\n");
1228                         continue;
1229                 }
1230
1231                 msginfo = (MsgInfo *)llast->data;
1232                 msginfo->cc = news_parse_xhdr(buf, msginfo);
1233
1234                 llast = llast->next;
1235         }
1236
1237         return newlist;
1238 }
1239
1240 gint news_fetch_msgnum_sort(gconstpointer a, gconstpointer b)
1241 {
1242         return (GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b));
1243 }
1244
1245 GSList *news_fetch_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1246 {
1247         NNTPSession *session;
1248         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1249         guint first, last, next;
1250         
1251         g_return_val_if_fail(folder != NULL, NULL);
1252         g_return_val_if_fail(folder->type == F_NEWS, NULL);
1253         g_return_val_if_fail(msgnum_list != NULL, NULL);
1254         g_return_val_if_fail(item != NULL, NULL);
1255         
1256         session = news_session_get(folder);
1257         g_return_val_if_fail(session != NULL, NULL);
1258
1259         tmp_msgnum_list = g_slist_copy(msgnum_list);
1260         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, news_fetch_msgnum_sort);
1261
1262         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1263         last = first;
1264         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1265                 next = GPOINTER_TO_INT(elem->data);
1266                 if(next != (last + 1)) {
1267                         tmp_msginfo_list = news_fetch_msginfo_from_to(session, item, first, last);
1268                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1269                         first = next;
1270                 }
1271                 last = next;
1272         }
1273         tmp_msginfo_list = news_fetch_msginfo_from_to(session, item, first, last);
1274         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1275
1276         g_slist_free(tmp_msgnum_list);
1277         
1278         return msginfo_list;
1279 }