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