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