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