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