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