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