637a549fb74d67180188b5583ba8c24de7608765
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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
33 #include "intl.h"
34 #include "news.h"
35 #include "nntp.h"
36 #include "socket.h"
37 #include "recv.h"
38 #include "procmsg.h"
39 #include "procheader.h"
40 #include "folder.h"
41 #include "session.h"
42 #include "statusbar.h"
43 #include "codeconv.h"
44 #include "utils.h"
45 #include "prefs_common.h"
46 #include "prefs_account.h"
47 #include "inputdialog.h"
48 #include "alertpanel.h"
49
50 #define NNTP_PORT       119
51
52 static Session *news_session_new         (const gchar   *server,
53                                           gushort        port,
54                                           const gchar   *userid,
55                                           const gchar   *passwd);
56
57 static gint news_get_article_cmd         (NNTPSession   *session,
58                                           const gchar   *cmd,
59                                           gint           num,
60                                           gchar         *filename);
61 static gint news_get_article             (NNTPSession   *session,
62                                           gint           num,
63                                           gchar         *filename);
64 static gint news_get_header              (NNTPSession   *session,
65                                           gint           num,
66                                           gchar         *filename);
67
68 static gint news_select_group            (NNTPSession   *session,
69                                           const gchar   *group);
70 static GSList *news_get_uncached_articles(NNTPSession   *session,
71                                           FolderItem    *item,
72                                           gint           cache_last,
73                                           gint          *rfirst,
74                                           gint          *rlast);
75 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
76 static gchar *news_parse_xhdr            (const gchar   *xhdr_str,
77                                           MsgInfo       *msginfo);
78 static GSList *news_delete_old_articles  (GSList        *alist,
79                                           FolderItem    *item,
80                                           gint           first);
81 static void news_delete_all_articles     (FolderItem    *item);
82
83
84 static Session *news_session_new(const gchar *server, gushort port,
85                                  const gchar *userid, const gchar *passwd)
86 {
87         gchar buf[NNTPBUFSIZE];
88         NNTPSession *session;
89         NNTPSockInfo *nntp_sock;
90
91         g_return_val_if_fail(server != NULL, NULL);
92
93         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
94
95         if (userid && passwd)
96                 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
97         else
98                 nntp_sock = nntp_open(server, port, buf);
99         if (nntp_sock == NULL)
100                 return NULL;
101
102         session = g_new(NNTPSession, 1);
103         SESSION(session)->type      = SESSION_NEWS;
104         SESSION(session)->server    = g_strdup(server);
105         session->nntp_sock          = nntp_sock;
106         SESSION(session)->sock      = nntp_sock->sock;
107         SESSION(session)->connected = TRUE;
108         SESSION(session)->phase     = SESSION_READY;
109         SESSION(session)->data      = NULL;
110         session->group = NULL;
111
112         return SESSION(session);
113 }
114
115 void news_session_destroy(NNTPSession *session)
116 {
117         nntp_close(session->nntp_sock);
118         session->nntp_sock = NULL;
119         SESSION(session)->sock = NULL;
120
121         g_free(session->group);
122 }
123
124 static gchar *news_query_password(const gchar *server, const gchar *user)
125 {
126         gchar *message;
127         gchar *pass;
128
129         message = g_strdup_printf(_("Input password for %s on %s:"),
130                                   user, server);
131         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
132         g_free(message);
133
134         return pass;
135 }
136
137 static Session *news_session_new_for_folder(Folder *folder)
138 {
139         Session *session;
140         PrefsAccount *ac;
141         const gchar *userid = NULL;
142         gchar *passwd = NULL;
143
144         g_return_val_if_fail(folder != NULL, NULL);
145         g_return_val_if_fail(folder->account != NULL, NULL);
146
147         ac = folder->account;
148         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
149                 userid = ac->userid;
150                 if (ac->passwd && ac->passwd[0])
151                         passwd = g_strdup(ac->passwd);
152                 else
153                         passwd = news_query_password(ac->nntp_server, userid);
154         }
155
156         session = news_session_new(ac->nntp_server,
157                                    ac->set_nntpport ? ac->nntpport : NNTP_PORT,
158                                    userid, passwd);
159         g_free(passwd);
160
161         return session;
162 }
163
164 NNTPSession *news_session_get(Folder *folder)
165 {
166         NNTPSession *session;
167
168         g_return_val_if_fail(folder != NULL, NULL);
169         g_return_val_if_fail(folder->type == F_NEWS, NULL);
170         g_return_val_if_fail(folder->account != NULL, NULL);
171
172         if (!REMOTE_FOLDER(folder)->session) {
173                 REMOTE_FOLDER(folder)->session =
174                         news_session_new_for_folder(folder);
175                 statusbar_pop_all();
176                 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
177         }
178
179         session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
180
181         if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
182                 log_warning(_("NNTP connection to %s:%d has been"
183                               " disconnected. Reconnecting...\n"),
184                             folder->account->nntp_server,
185                             folder->account->set_nntpport ?
186                             folder->account->nntpport : NNTP_PORT);
187                 session_destroy(REMOTE_FOLDER(folder)->session);
188                 REMOTE_FOLDER(folder)->session =
189                         news_session_new_for_folder(folder);
190         }
191
192         statusbar_pop_all();
193         return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
194 }
195
196 GSList *news_get_article_list(Folder *folder, FolderItem *item,
197                               gboolean use_cache)
198 {
199         GSList *alist;
200         NNTPSession *session;
201
202         g_return_val_if_fail(folder != NULL, NULL);
203         g_return_val_if_fail(item != NULL, NULL);
204         g_return_val_if_fail(folder->type == F_NEWS, NULL);
205
206         session = news_session_get(folder);
207
208         if (!session) {
209                 alist = procmsg_read_cache(item, FALSE);
210                 item->last_num = procmsg_get_last_num_in_cache(alist);
211         } else if (use_cache) {
212                 GSList *newlist;
213                 gint cache_last;
214                 gint first, last;
215
216                 alist = procmsg_read_cache(item, FALSE);
217
218                 cache_last = procmsg_get_last_num_in_cache(alist);
219                 newlist = news_get_uncached_articles
220                         (session, item, cache_last, &first, &last);
221                 alist = news_delete_old_articles(alist, item, first);
222
223                 alist = g_slist_concat(alist, newlist);
224                 item->last_num = last;
225         } else {
226                 gint last;
227
228                 alist = news_get_uncached_articles
229                         (session, item, 0, NULL, &last);
230                 news_delete_all_articles(item);
231                 item->last_num = last;
232         }
233
234         procmsg_set_flags(alist, item);
235
236         statusbar_pop_all();
237
238         return alist;
239 }
240
241 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
242 {
243         gchar *path, *filename;
244         NNTPSession *session;
245         gint ok;
246
247         g_return_val_if_fail(folder != NULL, NULL);
248         g_return_val_if_fail(item != NULL, NULL);
249
250         path = folder_item_get_path(item);
251         if (!is_dir_exist(path))
252                 make_dir_hier(path);
253         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
254         g_free(path);
255
256         if (is_file_exist(filename)) {
257                 debug_print(_("article %d has been already cached.\n"), num);
258                 return filename;
259         }
260
261         session = news_session_get(folder);
262         if (!session) {
263                 g_free(filename);
264                 return NULL;
265         }
266
267         ok = news_select_group(session, item->path);
268         statusbar_pop_all();
269         if (ok != NN_SUCCESS) {
270                 g_warning(_("can't select group %s\n"), item->path);
271                 g_free(filename);
272                 return NULL;
273         }
274
275         debug_print(_("getting article %d...\n"), num);
276         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
277                               num, filename);
278         statusbar_pop_all();
279         if (ok < 0) {
280                 g_warning(_("can't read article %d\n"), num);
281                 g_free(filename);
282                 return NULL;
283         }
284
285         return filename;
286 }
287
288 void news_scan_group(Folder *folder, FolderItem *item)
289 {
290 }
291
292 static struct NNTPGroupInfo * group_info_new(gchar * name,
293                                              gint first, gint last,
294                                              gchar type)
295 {
296         struct NNTPGroupInfo * info;
297
298         info = g_new(struct NNTPGroupInfo, 1);
299         if (info == NULL)
300                 return NULL;
301         info->name = g_strdup(name);
302         info->first = first;
303         info->last = last;
304         info->type = type;
305
306         return info;
307 }
308
309 static void group_info_free(struct NNTPGroupInfo * info)
310 {
311   g_free(info->name);
312   g_free(info);
313 }
314
315 void news_group_list_free(GSList * list)
316 {
317   g_slist_foreach(list, (GFunc) group_info_free, NULL);
318   g_slist_free(list);
319 }
320
321 gint news_group_info_compare(struct NNTPGroupInfo * info1,
322                              struct NNTPGroupInfo * info2)
323 {
324   return g_strcasecmp(info1->name, info2->name);
325 }
326
327 GSList *news_get_group_list(Folder *folder)
328 {
329         gchar *path, *filename;
330         FILE *fp;
331         GSList *list = NULL;
332         GSList *last = NULL;
333         gchar buf[NNTPBUFSIZE];
334
335         g_return_val_if_fail(folder != NULL, NULL);
336         g_return_val_if_fail(folder->type == F_NEWS, NULL);
337
338         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
339         if (!is_dir_exist(path))
340                 make_dir_hier(path);
341         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
342         g_free(path);
343
344         if ((fp = fopen(filename, "r")) == NULL) {
345                 NNTPSession *session;
346
347                 session = news_session_get(folder);
348                 if (!session) {
349                         g_free(filename);
350                         return NULL;
351                 }
352
353                 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
354                         g_free(filename);
355                         statusbar_pop_all();
356                         return NULL;
357                 }
358                 statusbar_pop_all();
359                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
360                         log_warning(_("can't retrieve newsgroup list\n"));
361                         g_free(filename);
362                         return NULL;
363                 }
364
365                 if ((fp = fopen(filename, "r")) == NULL) {
366                         FILE_OP_ERROR(filename, "fopen");
367                         g_free(filename);
368                         return NULL;
369                 }
370         }
371
372         while (fgets(buf, sizeof(buf), fp) != NULL) {
373                 gchar * p;
374                 gchar * cur;
375                 gchar * name;
376                 gint last_article;
377                 gint first_article;
378                 gchar type;
379                 struct NNTPGroupInfo * info;
380
381                 cur = buf;
382                 p = strchr(cur, ' ');
383                 if (p == NULL)
384                         continue;
385                 * p = 0;
386
387                 name = cur;
388                 
389                 cur = p + 1;
390                 p = strchr(cur, ' ');
391                 if (p == NULL)
392                         continue;
393                 * p = 0;
394                 last_article = atoi(cur);
395
396                 cur = p + 1;
397                 p = strchr(cur, ' ');
398                 if (p == NULL)
399                         continue;
400                 * p = 0;
401                 first_article = atoi(cur);
402
403                 cur = p + 1;
404                 type = * cur;
405
406                 info = group_info_new(name, first_article,
407                                       last_article, type);
408                 if (info == NULL)
409                         continue;
410
411                 if (!last)
412                         last = list = g_slist_append(NULL, info);
413                 else {
414                         last = g_slist_append(last, info);
415                         last = last->next;
416                 }
417         }
418
419         fclose(fp);
420         g_free(filename);
421
422         list = g_slist_sort(list, (GCompareFunc) news_group_info_compare);
423
424         statusbar_pop_all();
425
426         return list;
427 }
428
429 void news_remove_group_list(Folder *folder)
430 {
431         gchar *path, *filename;
432
433         g_return_if_fail(folder != NULL);
434         g_return_if_fail(folder->type == F_NEWS);
435
436         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
437         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
438         g_free(path);
439
440         if (is_file_exist(filename)) {
441                 if (remove(filename) < 0)
442                         FILE_OP_ERROR(filename, "remove");
443         }
444         g_free(filename);
445 }
446
447 gint news_post(Folder *folder, const gchar *file)
448 {
449         NNTPSession *session;
450         FILE *fp;
451         gint ok;
452
453         g_return_val_if_fail(folder != NULL, -1);
454         g_return_val_if_fail(folder->type == F_NEWS, -1);
455         g_return_val_if_fail(file != NULL, -1);
456
457         session = news_session_get(folder);
458         if (!session) return -1;
459
460         if ((fp = fopen(file, "r")) == NULL) {
461                 FILE_OP_ERROR(file, "fopen");
462                 return -1;
463         }
464
465         ok = nntp_post(session->nntp_sock, fp);
466         if (ok != NN_SUCCESS) {
467                 log_warning(_("can't post article.\n"));
468                 return -1;
469         }
470
471         fclose(fp);
472
473         statusbar_pop_all();
474
475         return 0;
476 }
477
478 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
479                                  gint num, gchar *filename)
480 {
481         gchar *msgid;
482
483         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
484             != NN_SUCCESS)
485                 return -1;
486
487         debug_print("Message-Id = %s, num = %d\n", msgid, num);
488         g_free(msgid);
489
490         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
491                 log_warning(_("can't retrieve article %d\n"), num);
492                 return -1;
493         }
494
495         return 0;
496 }
497
498 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
499 {
500         return news_get_article_cmd(session, "ARTICLE", num, filename);
501 }
502
503 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
504 {
505         return news_get_article_cmd(session, "HEAD", num, filename);
506 }
507
508 /**
509  * news_select_group:
510  * @session: Active NNTP session.
511  * @group: Newsgroup name.
512  *
513  * Select newsgroup @group with the GROUP command if it is not already
514  * selected in @session.
515  *
516  * Return value: NNTP result code.
517  **/
518 static gint news_select_group(NNTPSession *session, const gchar *group)
519 {
520         gint ok;
521         gint num, first, last;
522
523         if (session->group && g_strcasecmp(session->group, group) == 0)
524                 return NN_SUCCESS;
525
526         g_free(session->group);
527         session->group = NULL;
528
529         ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
530         if (ok == NN_SUCCESS)
531                 session->group = g_strdup(group);
532
533         return ok;
534 }
535
536 static GSList *news_get_uncached_articles(NNTPSession *session,
537                                           FolderItem *item, gint cache_last,
538                                           gint *rfirst, gint *rlast)
539 {
540         gint ok;
541         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
542         gchar buf[NNTPBUFSIZE];
543         GSList *newlist = NULL;
544         GSList *llast = NULL;
545         MsgInfo *msginfo;
546
547         if (rfirst) *rfirst = 0;
548         if (rlast)  *rlast  = 0;
549
550         g_return_val_if_fail(session != NULL, NULL);
551         g_return_val_if_fail(item != NULL, NULL);
552         g_return_val_if_fail(item->folder != NULL, NULL);
553         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
554
555         g_free(session->group);
556         session->group = NULL;
557
558         ok = nntp_group(session->nntp_sock, item->path,
559                         &num, &first, &last);
560         if (ok != NN_SUCCESS) {
561                 log_warning(_("can't set group: %s\n"), item->path);
562                 return NULL;
563         }
564         session->group = g_strdup(item->path);
565
566         /* calculate getting overview range */
567         if (first > last) {
568                 log_warning(_("invalid article range: %d - %d\n"),
569                             first, last);
570                 return NULL;
571         }
572         if (cache_last < first)
573                 begin = first;
574         else if (last < cache_last)
575                 begin = first;
576         else if (last == cache_last) {
577                 debug_print(_("no new articles.\n"));
578                 return NULL;
579         } else
580                 begin = cache_last + 1;
581         end = last;
582
583         if (rfirst) *rfirst = first;
584         if (rlast)  *rlast  = last;
585
586         if (prefs_common.max_articles > 0 &&
587             end - begin + 1 > prefs_common.max_articles)
588                 begin = end - prefs_common.max_articles + 1;
589
590         log_message(_("getting xover %d - %d in %s...\n"),
591                     begin, end, item->path);
592         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
593                 log_warning(_("can't get xover\n"));
594                 return NULL;
595         }
596
597         for (;;) {
598                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
599                         log_warning(_("error occurred while getting xover.\n"));
600                         return newlist;
601                 }
602
603                 if (buf[0] == '.' && buf[1] == '\r') break;
604
605                 msginfo = news_parse_xover(buf);
606                 if (!msginfo) {
607                         log_warning(_("invalid xover line: %s\n"), buf);
608                         continue;
609                 }
610
611                 msginfo->folder = item;
612                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
613                 msginfo->flags.tmp_flags = MSG_NEWS;
614                 msginfo->newsgroups = g_strdup(item->path);
615
616                 if (!newlist)
617                         llast = newlist = g_slist_append(newlist, msginfo);
618                 else {
619                         llast = g_slist_append(llast, msginfo);
620                         llast = llast->next;
621                 }
622         }
623
624         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
625                 log_warning(_("can't get xhdr\n"));
626                 return newlist;
627         }
628
629         llast = newlist;
630
631         for (;;) {
632                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
633                         log_warning(_("error occurred while getting xhdr.\n"));
634                         return newlist;
635                 }
636
637                 if (buf[0] == '.' && buf[1] == '\r') break;
638                 if (!llast) {
639                         g_warning("llast == NULL\n");
640                         continue;
641                 }
642
643                 msginfo = (MsgInfo *)llast->data;
644                 msginfo->to = news_parse_xhdr(buf, msginfo);
645
646                 llast = llast->next;
647         }
648
649         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
650                 log_warning(_("can't get xhdr\n"));
651                 return newlist;
652         }
653
654         llast = newlist;
655
656         for (;;) {
657                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
658                         log_warning(_("error occurred while getting xhdr.\n"));
659                         return newlist;
660                 }
661
662                 if (buf[0] == '.' && buf[1] == '\r') break;
663                 if (!llast) {
664                         g_warning("llast == NULL\n");
665                         continue;
666                 }
667
668                 msginfo = (MsgInfo *)llast->data;
669                 msginfo->cc = news_parse_xhdr(buf, msginfo);
670
671                 llast = llast->next;
672         }
673
674         return newlist;
675 }
676
677 #define PARSE_ONE_PARAM(p, srcp) \
678 { \
679         p = strchr(srcp, '\t'); \
680         if (!p) return NULL; \
681         else \
682                 *p++ = '\0'; \
683 }
684
685 static MsgInfo *news_parse_xover(const gchar *xover_str)
686 {
687         MsgInfo *msginfo;
688         gchar buf[NNTPBUFSIZE];
689         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
690         gchar *p;
691         gint num, size_int, line_int;
692         gchar *xover_buf;
693
694         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
695         strcpy(xover_buf, xover_str);
696
697         PARSE_ONE_PARAM(subject, xover_buf);
698         PARSE_ONE_PARAM(sender, subject);
699         PARSE_ONE_PARAM(date, sender);
700         PARSE_ONE_PARAM(msgid, date);
701         PARSE_ONE_PARAM(ref, msgid);
702         PARSE_ONE_PARAM(size, ref);
703         PARSE_ONE_PARAM(line, size);
704
705         tmp = strchr(line, '\t');
706         if (!tmp) tmp = strchr(line, '\r');
707         if (!tmp) tmp = strchr(line, '\n');
708         if (tmp) *tmp = '\0';
709
710         num = atoi(xover_str);
711         size_int = atoi(size);
712         line_int = atoi(line);
713
714         /* set MsgInfo */
715         msginfo = g_new0(MsgInfo, 1);
716         msginfo->msgnum = num;
717         msginfo->size = size_int;
718
719         msginfo->date = g_strdup(date);
720         msginfo->date_t = procheader_date_parse(NULL, date, 0);
721
722         conv_unmime_header(buf, sizeof(buf), sender, NULL);
723         msginfo->from = g_strdup(buf);
724         msginfo->fromname = procheader_get_fromname(buf);
725
726         conv_unmime_header(buf, sizeof(buf), subject, NULL);
727         msginfo->subject = g_strdup(buf);
728
729         extract_parenthesis(msgid, '<', '>');
730         remove_space(msgid);
731         if (*msgid != '\0')
732                 msginfo->msgid = g_strdup(msgid);
733
734         msginfo->references = g_strdup(ref);
735         eliminate_parenthesis(ref, '(', ')');
736         if ((p = strrchr(ref, '<')) != NULL) {
737                 extract_parenthesis(p, '<', '>');
738                 remove_space(p);
739                 if (*p != '\0')
740                         msginfo->inreplyto = g_strdup(p);
741         }
742
743         return msginfo;
744 }
745
746 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
747 {
748         gchar *p;
749         gchar *tmp;
750         gint num;
751
752         p = strchr(xhdr_str, ' ');
753         if (!p)
754                 return NULL;
755         else
756                 p++;
757
758         num = atoi(xhdr_str);
759         if (msginfo->msgnum != num) return NULL;
760
761         tmp = strchr(p, '\r');
762         if (!tmp) tmp = strchr(p, '\n');
763
764         if (tmp)
765                 return g_strndup(p, tmp - p);
766         else
767                 return g_strdup(p);
768 }
769
770 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
771                                         gint first)
772 {
773         GSList *cur, *next;
774         MsgInfo *msginfo;
775         gchar *dir;
776
777         g_return_val_if_fail(item != NULL, alist);
778         g_return_val_if_fail(item->folder != NULL, alist);
779         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
780
781         if (first < 2) return alist;
782
783         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
784
785         dir = folder_item_get_path(item);
786         remove_numbered_files(dir, 1, first - 1);
787         g_free(dir);
788
789         for (cur = alist; cur != NULL; ) {
790                 next = cur->next;
791
792                 msginfo = (MsgInfo *)cur->data;
793                 if (msginfo && msginfo->msgnum < first) {
794                         procmsg_msginfo_free(msginfo);
795                         alist = g_slist_remove(alist, msginfo);
796                 }
797
798                 cur = next;
799         }
800
801         return alist;
802 }
803
804 static void news_delete_all_articles(FolderItem *item)
805 {
806         gchar *dir;
807
808         g_return_if_fail(item != NULL);
809         g_return_if_fail(item->folder != NULL);
810         g_return_if_fail(item->folder->type == F_NEWS);
811
812         debug_print(_("\tDeleting all cached articles... "));
813
814         dir = folder_item_get_path(item);
815         remove_all_numbered_files(dir);
816         g_free(dir);
817
818         debug_print(_("done.\n"));
819 }