implement news->remove_msg()
[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         gchar *file;
513
514         g_return_val_if_fail(item != NULL, -1);
515
516         file = news_fetch_msg(folder, item, num);
517         g_return_val_if_fail(file != NULL, -1);
518
519         if (unlink(file) < 0) {
520                 FILE_OP_ERROR(file, "unlink");
521                 g_free(file);
522                 return -1;
523         }
524
525         g_free(file);
526         return 0;
527 }
528
529 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
530 {
531         return news_get_article_cmd(session, "ARTICLE", num, filename);
532 }
533
534 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
535 {
536         return news_get_article_cmd(session, "HEAD", num, filename);
537 }
538
539 /**
540  * news_select_group:
541  * @session: Active NNTP session.
542  * @group: Newsgroup name.
543  *
544  * Select newsgroup @group with the GROUP command if it is not already
545  * selected in @session.
546  *
547  * Return value: NNTP result code.
548  **/
549 static gint news_select_group(NNTPSession *session, const gchar *group)
550 {
551         gint ok;
552         gint num, first, last;
553
554         if (session->group && g_strcasecmp(session->group, group) == 0)
555                 return NN_SUCCESS;
556
557         g_free(session->group);
558         session->group = NULL;
559
560         ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
561         if (ok == NN_SUCCESS)
562                 session->group = g_strdup(group);
563
564         return ok;
565 }
566
567 static GSList *news_get_uncached_articles(NNTPSession *session,
568                                           FolderItem *item, gint cache_last,
569                                           gint *rfirst, gint *rlast)
570 {
571         gint ok;
572         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
573         gchar buf[NNTPBUFSIZE];
574         GSList *newlist = NULL;
575         GSList *llast = NULL;
576         MsgInfo *msginfo;
577
578         if (rfirst) *rfirst = 0;
579         if (rlast)  *rlast  = 0;
580
581         g_return_val_if_fail(session != NULL, NULL);
582         g_return_val_if_fail(item != NULL, NULL);
583         g_return_val_if_fail(item->folder != NULL, NULL);
584         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
585
586         g_free(session->group);
587         session->group = NULL;
588
589         ok = nntp_group(session->nntp_sock, item->path,
590                         &num, &first, &last);
591         if (ok != NN_SUCCESS) {
592                 log_warning(_("can't set group: %s\n"), item->path);
593                 return NULL;
594         }
595         session->group = g_strdup(item->path);
596
597         /* calculate getting overview range */
598         if (first > last) {
599                 log_warning(_("invalid article range: %d - %d\n"),
600                             first, last);
601                 return NULL;
602         }
603         if (cache_last < first)
604                 begin = first;
605         else if (last < cache_last)
606                 begin = first;
607         else if (last == cache_last) {
608                 debug_print(_("no new articles.\n"));
609                 return NULL;
610         } else
611                 begin = cache_last + 1;
612         end = last;
613
614         if (rfirst) *rfirst = first;
615         if (rlast)  *rlast  = last;
616
617         if (prefs_common.max_articles > 0 &&
618             end - begin + 1 > prefs_common.max_articles)
619                 begin = end - prefs_common.max_articles + 1;
620
621         log_message(_("getting xover %d - %d in %s...\n"),
622                     begin, end, item->path);
623         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
624                 log_warning(_("can't get xover\n"));
625                 return NULL;
626         }
627
628         for (;;) {
629                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
630                         log_warning(_("error occurred while getting xover.\n"));
631                         return newlist;
632                 }
633
634                 if (buf[0] == '.' && buf[1] == '\r') break;
635
636                 msginfo = news_parse_xover(buf);
637                 if (!msginfo) {
638                         log_warning(_("invalid xover line: %s\n"), buf);
639                         continue;
640                 }
641
642                 msginfo->folder = item;
643                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
644                 msginfo->flags.tmp_flags = MSG_NEWS;
645                 msginfo->newsgroups = g_strdup(item->path);
646
647                 if (!newlist)
648                         llast = newlist = g_slist_append(newlist, msginfo);
649                 else {
650                         llast = g_slist_append(llast, msginfo);
651                         llast = llast->next;
652                 }
653         }
654
655         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
656                 log_warning(_("can't get xhdr\n"));
657                 return newlist;
658         }
659
660         llast = newlist;
661
662         for (;;) {
663                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
664                         log_warning(_("error occurred while getting xhdr.\n"));
665                         return newlist;
666                 }
667
668                 if (buf[0] == '.' && buf[1] == '\r') break;
669                 if (!llast) {
670                         g_warning("llast == NULL\n");
671                         continue;
672                 }
673
674                 msginfo = (MsgInfo *)llast->data;
675                 msginfo->to = news_parse_xhdr(buf, msginfo);
676
677                 llast = llast->next;
678         }
679
680         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
681                 log_warning(_("can't get xhdr\n"));
682                 return newlist;
683         }
684
685         llast = newlist;
686
687         for (;;) {
688                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
689                         log_warning(_("error occurred while getting xhdr.\n"));
690                         return newlist;
691                 }
692
693                 if (buf[0] == '.' && buf[1] == '\r') break;
694                 if (!llast) {
695                         g_warning("llast == NULL\n");
696                         continue;
697                 }
698
699                 msginfo = (MsgInfo *)llast->data;
700                 msginfo->cc = news_parse_xhdr(buf, msginfo);
701
702                 llast = llast->next;
703         }
704
705         return newlist;
706 }
707
708 #define PARSE_ONE_PARAM(p, srcp) \
709 { \
710         p = strchr(srcp, '\t'); \
711         if (!p) return NULL; \
712         else \
713                 *p++ = '\0'; \
714 }
715
716 static MsgInfo *news_parse_xover(const gchar *xover_str)
717 {
718         MsgInfo *msginfo;
719         gchar buf[NNTPBUFSIZE];
720         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
721         gchar *p;
722         gint num, size_int, line_int;
723         gchar *xover_buf;
724
725         Xstrdup_a(xover_buf, xover_str, return NULL);
726
727         PARSE_ONE_PARAM(subject, xover_buf);
728         PARSE_ONE_PARAM(sender, subject);
729         PARSE_ONE_PARAM(date, sender);
730         PARSE_ONE_PARAM(msgid, date);
731         PARSE_ONE_PARAM(ref, msgid);
732         PARSE_ONE_PARAM(size, ref);
733         PARSE_ONE_PARAM(line, size);
734
735         tmp = strchr(line, '\t');
736         if (!tmp) tmp = strchr(line, '\r');
737         if (!tmp) tmp = strchr(line, '\n');
738         if (tmp) *tmp = '\0';
739
740         num = atoi(xover_str);
741         size_int = atoi(size);
742         line_int = atoi(line);
743
744         /* set MsgInfo */
745         msginfo = g_new0(MsgInfo, 1);
746         msginfo->msgnum = num;
747         msginfo->size = size_int;
748
749         msginfo->date = g_strdup(date);
750         msginfo->date_t = procheader_date_parse(NULL, date, 0);
751
752         conv_unmime_header(buf, sizeof(buf), sender, NULL);
753         msginfo->from = g_strdup(buf);
754         msginfo->fromname = procheader_get_fromname(buf);
755
756         conv_unmime_header(buf, sizeof(buf), subject, NULL);
757         msginfo->subject = g_strdup(buf);
758
759         extract_parenthesis(msgid, '<', '>');
760         remove_space(msgid);
761         if (*msgid != '\0')
762                 msginfo->msgid = g_strdup(msgid);
763
764         msginfo->references = g_strdup(ref);
765         eliminate_parenthesis(ref, '(', ')');
766         if ((p = strrchr(ref, '<')) != NULL) {
767                 extract_parenthesis(p, '<', '>');
768                 remove_space(p);
769                 if (*p != '\0')
770                         msginfo->inreplyto = g_strdup(p);
771         }
772
773         return msginfo;
774 }
775
776 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
777 {
778         gchar *p;
779         gchar *tmp;
780         gint num;
781
782         p = strchr(xhdr_str, ' ');
783         if (!p)
784                 return NULL;
785         else
786                 p++;
787
788         num = atoi(xhdr_str);
789         if (msginfo->msgnum != num) return NULL;
790
791         tmp = strchr(p, '\r');
792         if (!tmp) tmp = strchr(p, '\n');
793
794         if (tmp)
795                 return g_strndup(p, tmp - p);
796         else
797                 return g_strdup(p);
798 }
799
800 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
801                                         gint first)
802 {
803         GSList *cur, *next;
804         MsgInfo *msginfo;
805         gchar *dir;
806
807         g_return_val_if_fail(item != NULL, alist);
808         g_return_val_if_fail(item->folder != NULL, alist);
809         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
810
811         if (first < 2) return alist;
812
813         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
814
815         dir = folder_item_get_path(item);
816         remove_numbered_files(dir, 1, first - 1);
817         g_free(dir);
818
819         for (cur = alist; cur != NULL; ) {
820                 next = cur->next;
821
822                 msginfo = (MsgInfo *)cur->data;
823                 if (msginfo && msginfo->msgnum < first) {
824                         procmsg_msginfo_free(msginfo);
825                         alist = g_slist_remove(alist, msginfo);
826                 }
827
828                 cur = next;
829         }
830
831         return alist;
832 }
833
834 static void news_delete_all_articles(FolderItem *item)
835 {
836         gchar *dir;
837
838         g_return_if_fail(item != NULL);
839         g_return_if_fail(item->folder != NULL);
840         g_return_if_fail(item->folder->type == F_NEWS);
841
842         debug_print(_("\tDeleting all cached articles... "));
843
844         dir = folder_item_get_path(item);
845         remove_all_numbered_files(dir);
846         g_free(dir);
847
848         debug_print(_("done.\n"));
849 }