9bff6fc469940dc6b9f4f52124a84f97cc0e1d59
[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 (last >= max) {
355                 new += last - max;
356                 unread += last - max;
357                 if (new > num) new = num;
358                 if (unread > num) unread = num;
359         }
360
361         item->new = new;
362         item->unread = unread;
363         item->total = num;
364         item->last_num = last;
365 }
366
367 static NewsGroupInfo *news_group_info_new(const gchar *name,
368                                           gint first, gint last, gchar type)
369 {
370         NewsGroupInfo *ginfo;
371
372         ginfo = g_new(NewsGroupInfo, 1);
373         ginfo->name = g_strdup(name);
374         ginfo->first = first;
375         ginfo->last = last;
376         ginfo->type = type;
377
378         return ginfo;
379 }
380
381 static void news_group_info_free(NewsGroupInfo *ginfo)
382 {
383         g_free(ginfo->name);
384         g_free(ginfo);
385 }
386
387 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
388                                     NewsGroupInfo *ginfo2)
389 {
390         return g_strcasecmp(ginfo1->name, ginfo2->name);
391 }
392
393 GSList *news_get_group_list(Folder *folder)
394 {
395         gchar *path, *filename;
396         FILE *fp;
397         GSList *list = NULL;
398         GSList *last = NULL;
399         gchar buf[NNTPBUFSIZE];
400
401         g_return_val_if_fail(folder != NULL, NULL);
402         g_return_val_if_fail(folder->type == F_NEWS, NULL);
403
404         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
405         if (!is_dir_exist(path))
406                 make_dir_hier(path);
407         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
408         g_free(path);
409
410         if ((fp = fopen(filename, "rb")) == NULL) {
411                 NNTPSession *session;
412
413                 session = news_session_get(folder);
414                 if (!session) {
415                         g_free(filename);
416                         return NULL;
417                 }
418
419                 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
420                         g_free(filename);
421                         statusbar_pop_all();
422                         return NULL;
423                 }
424                 statusbar_pop_all();
425                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
426                         log_warning(_("can't retrieve newsgroup list\n"));
427                         session_destroy(SESSION(session));
428                         REMOTE_FOLDER(folder)->session = NULL;
429                         g_free(filename);
430                         return NULL;
431                 }
432
433                 if ((fp = fopen(filename, "rb")) == NULL) {
434                         FILE_OP_ERROR(filename, "fopen");
435                         g_free(filename);
436                         return NULL;
437                 }
438         }
439
440         while (fgets(buf, sizeof(buf), fp) != NULL) {
441                 gchar *p = buf;
442                 gchar *name;
443                 gint last_num;
444                 gint first_num;
445                 gchar type;
446                 NewsGroupInfo *ginfo;
447
448                 p = strchr(p, ' ');
449                 if (!p) continue;
450                 *p = '\0';
451                 p++;
452                 name = buf;
453
454                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
455                         continue;
456
457                 ginfo = news_group_info_new(name, first_num, last_num, type);
458
459                 if (!last)
460                         last = list = g_slist_append(NULL, ginfo);
461                 else {
462                         last = g_slist_append(last, ginfo);
463                         last = last->next;
464                 }
465         }
466
467         fclose(fp);
468         g_free(filename);
469
470         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
471
472         statusbar_pop_all();
473
474         return list;
475 }
476
477 void news_group_list_free(GSList *group_list)
478 {
479         GSList *cur;
480
481         if (!group_list) return;
482
483         for (cur = group_list; cur != NULL; cur = cur->next)
484                 news_group_info_free((NewsGroupInfo *)cur->data);
485         g_slist_free(group_list);
486 }
487
488 void news_remove_group_list_cache(Folder *folder)
489 {
490         gchar *path, *filename;
491
492         g_return_if_fail(folder != NULL);
493         g_return_if_fail(folder->type == F_NEWS);
494
495         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
496         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
497         g_free(path);
498
499         if (is_file_exist(filename)) {
500                 if (remove(filename) < 0)
501                         FILE_OP_ERROR(filename, "remove");
502         }
503         g_free(filename);
504 }
505
506 gint news_post(Folder *folder, const gchar *file)
507 {
508         NNTPSession *session;
509         FILE *fp;
510         gint ok;
511
512         g_return_val_if_fail(folder != NULL, -1);
513         g_return_val_if_fail(folder->type == F_NEWS, -1);
514         g_return_val_if_fail(file != NULL, -1);
515
516         session = news_session_get(folder);
517         if (!session) return -1;
518
519         if ((fp = fopen(file, "rb")) == NULL) {
520                 FILE_OP_ERROR(file, "fopen");
521                 return -1;
522         }
523
524         ok = nntp_post(session->nntp_sock, fp);
525         if (ok != NN_SUCCESS) {
526                 log_warning(_("can't post article.\n"));
527                 return -1;
528         }
529
530         fclose(fp);
531
532         statusbar_pop_all();
533
534         return 0;
535 }
536
537 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
538                                  gint num, gchar *filename)
539 {
540         gchar *msgid;
541
542         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
543             != NN_SUCCESS)
544                 return -1;
545
546         debug_print("Message-Id = %s, num = %d\n", msgid, num);
547         g_free(msgid);
548
549         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
550                 log_warning(_("can't retrieve article %d\n"), num);
551                 return -1;
552         }
553
554         return 0;
555 }
556
557 static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
558 {
559         MsgInfo * msginfo;
560         gchar * filename;
561         MsgFlags msgflags = { 0, 0 };
562         gint r;
563
564         filename = folder_item_fetch_msg(item, num);
565         if (filename == NULL)
566                 return -1;
567
568         msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
569         if (msginfo == NULL)
570                 return -1;
571
572         r = news_cancel_article(folder, msginfo);
573
574         procmsg_msginfo_free(msginfo);
575
576         return r;
577 }
578
579 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
580 {
581         return news_get_article_cmd(session, "ARTICLE", num, filename);
582 }
583
584 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
585 {
586         return news_get_article_cmd(session, "HEAD", num, filename);
587 }
588
589 /**
590  * news_select_group:
591  * @session: Active NNTP session.
592  * @group: Newsgroup name.
593  * @num: Estimated number of articles.
594  * @first: First article number.
595  * @last: Last article number.
596  *
597  * Select newsgroup @group with the GROUP command if it is not already
598  * selected in @session, or article numbers need to be returned.
599  *
600  * Return value: NNTP result code.
601  **/
602 static gint news_select_group(NNTPSession *session, const gchar *group,
603                               gint *num, gint *first, gint *last)
604 {
605         gint ok;
606         gint num_, first_, last_;
607
608         if (!num || !first || !last) {
609                 if (session->group && g_strcasecmp(session->group, group) == 0)
610                         return NN_SUCCESS;
611                 num = &num_;
612                 first = &first_;
613                 last = &last_;
614         }
615
616         g_free(session->group);
617         session->group = NULL;
618
619         ok = nntp_group(session->nntp_sock, group, num, first, last);
620         if (ok == NN_SUCCESS)
621                 session->group = g_strdup(group);
622
623         return ok;
624 }
625
626 static GSList *news_get_uncached_articles(NNTPSession *session,
627                                           FolderItem *item, gint cache_last,
628                                           gint *rfirst, gint *rlast)
629 {
630         gint ok;
631         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
632         gchar buf[NNTPBUFSIZE];
633         GSList *newlist = NULL;
634         GSList *llast = NULL;
635         MsgInfo *msginfo;
636
637         if (rfirst) *rfirst = 0;
638         if (rlast)  *rlast  = 0;
639
640         g_return_val_if_fail(session != NULL, NULL);
641         g_return_val_if_fail(item != NULL, NULL);
642         g_return_val_if_fail(item->folder != NULL, NULL);
643         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
644
645         ok = news_select_group(session, item->path, &num, &first, &last);
646         if (ok != NN_SUCCESS) {
647                 log_warning(_("can't set group: %s\n"), item->path);
648                 return NULL;
649         }
650
651         /* calculate getting overview range */
652         if (first > last) {
653                 log_warning(_("invalid article range: %d - %d\n"),
654                             first, last);
655                 return NULL;
656         }
657         if (cache_last < first)
658                 begin = first;
659         else if (last < cache_last)
660                 begin = first;
661         else if (last == cache_last) {
662                 debug_print(_("no new articles.\n"));
663                 return NULL;
664         } else
665                 begin = cache_last + 1;
666         end = last;
667
668         if (rfirst) *rfirst = first;
669         if (rlast)  *rlast  = last;
670
671         if (prefs_common.max_articles > 0 &&
672             end - begin + 1 > prefs_common.max_articles)
673                 begin = end - prefs_common.max_articles + 1;
674
675         log_message(_("getting xover %d - %d in %s...\n"),
676                     begin, end, item->path);
677         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
678                 log_warning(_("can't get xover\n"));
679                 return NULL;
680         }
681
682         for (;;) {
683                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
684                         log_warning(_("error occurred while getting xover.\n"));
685                         return newlist;
686                 }
687
688                 if (buf[0] == '.' && buf[1] == '\r') break;
689
690                 msginfo = news_parse_xover(buf);
691                 if (!msginfo) {
692                         log_warning(_("invalid xover line: %s\n"), buf);
693                         continue;
694                 }
695
696                 msginfo->folder = item;
697                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
698                 msginfo->flags.tmp_flags = MSG_NEWS;
699                 msginfo->newsgroups = g_strdup(item->path);
700
701                 if (!newlist)
702                         llast = newlist = g_slist_append(newlist, msginfo);
703                 else {
704                         llast = g_slist_append(llast, msginfo);
705                         llast = llast->next;
706                 }
707         }
708
709         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
710                 log_warning(_("can't get xhdr\n"));
711                 return newlist;
712         }
713
714         llast = newlist;
715
716         for (;;) {
717                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
718                         log_warning(_("error occurred while getting xhdr.\n"));
719                         return newlist;
720                 }
721
722                 if (buf[0] == '.' && buf[1] == '\r') break;
723                 if (!llast) {
724                         g_warning("llast == NULL\n");
725                         continue;
726                 }
727
728                 msginfo = (MsgInfo *)llast->data;
729                 msginfo->to = news_parse_xhdr(buf, msginfo);
730
731                 llast = llast->next;
732         }
733
734         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
735                 log_warning(_("can't get xhdr\n"));
736                 return newlist;
737         }
738
739         llast = newlist;
740
741         for (;;) {
742                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
743                         log_warning(_("error occurred while getting xhdr.\n"));
744                         return newlist;
745                 }
746
747                 if (buf[0] == '.' && buf[1] == '\r') break;
748                 if (!llast) {
749                         g_warning("llast == NULL\n");
750                         continue;
751                 }
752
753                 msginfo = (MsgInfo *)llast->data;
754                 msginfo->cc = news_parse_xhdr(buf, msginfo);
755
756                 llast = llast->next;
757         }
758
759         return newlist;
760 }
761
762 #define PARSE_ONE_PARAM(p, srcp) \
763 { \
764         p = strchr(srcp, '\t'); \
765         if (!p) return NULL; \
766         else \
767                 *p++ = '\0'; \
768 }
769
770 static MsgInfo *news_parse_xover(const gchar *xover_str)
771 {
772         MsgInfo *msginfo;
773         gchar buf[NNTPBUFSIZE];
774         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
775         gchar *p;
776         gint num, size_int, line_int;
777         gchar *xover_buf;
778
779         Xstrdup_a(xover_buf, xover_str, return NULL);
780
781         PARSE_ONE_PARAM(subject, xover_buf);
782         PARSE_ONE_PARAM(sender, subject);
783         PARSE_ONE_PARAM(date, sender);
784         PARSE_ONE_PARAM(msgid, date);
785         PARSE_ONE_PARAM(ref, msgid);
786         PARSE_ONE_PARAM(size, ref);
787         PARSE_ONE_PARAM(line, size);
788         PARSE_ONE_PARAM(xref, line);
789
790         tmp = strchr(xref, '\t');
791         if (!tmp) tmp = strchr(line, '\r');
792         if (!tmp) tmp = strchr(line, '\n');
793         if (tmp) *tmp = '\0';
794
795         num = atoi(xover_str);
796         size_int = atoi(size);
797         line_int = atoi(line);
798
799         /* set MsgInfo */
800         msginfo = g_new0(MsgInfo, 1);
801         msginfo->msgnum = num;
802         msginfo->size = size_int;
803
804         msginfo->date = g_strdup(date);
805         msginfo->date_t = procheader_date_parse(NULL, date, 0);
806
807         conv_unmime_header(buf, sizeof(buf), sender, NULL);
808         msginfo->from = g_strdup(buf);
809         msginfo->fromname = procheader_get_fromname(buf);
810
811         conv_unmime_header(buf, sizeof(buf), subject, NULL);
812         msginfo->subject = g_strdup(buf);
813
814         extract_parenthesis(msgid, '<', '>');
815         remove_space(msgid);
816         if (*msgid != '\0')
817                 msginfo->msgid = g_strdup(msgid);
818
819         msginfo->references = g_strdup(ref);
820         eliminate_parenthesis(ref, '(', ')');
821         if ((p = strrchr(ref, '<')) != NULL) {
822                 extract_parenthesis(p, '<', '>');
823                 remove_space(p);
824                 if (*p != '\0')
825                         msginfo->inreplyto = g_strdup(p);
826         }
827
828         msginfo->xref = g_strdup(xref);
829         p = msginfo->xref+strlen(msginfo->xref) - 1;
830         while (*p == '\r' || *p == '\n') {
831                 *p = '\0';
832                 p--;
833         }
834
835         return msginfo;
836 }
837
838 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
839 {
840         gchar *p;
841         gchar *tmp;
842         gint num;
843
844         p = strchr(xhdr_str, ' ');
845         if (!p)
846                 return NULL;
847         else
848                 p++;
849
850         num = atoi(xhdr_str);
851         if (msginfo->msgnum != num) return NULL;
852
853         tmp = strchr(p, '\r');
854         if (!tmp) tmp = strchr(p, '\n');
855
856         if (tmp)
857                 return g_strndup(p, tmp - p);
858         else
859                 return g_strdup(p);
860 }
861
862 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
863                                         gint first)
864 {
865         GSList *cur, *next;
866         MsgInfo *msginfo;
867         gchar *dir;
868
869         g_return_val_if_fail(item != NULL, alist);
870         g_return_val_if_fail(item->folder != NULL, alist);
871         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
872
873         if (first < 2) return alist;
874
875         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
876
877         dir = folder_item_get_path(item);
878         remove_numbered_files(dir, 1, first - 1);
879         g_free(dir);
880
881         for (cur = alist; cur != NULL; ) {
882                 next = cur->next;
883
884                 msginfo = (MsgInfo *)cur->data;
885                 if (msginfo && msginfo->msgnum < first) {
886                         procmsg_msginfo_free(msginfo);
887                         alist = g_slist_remove(alist, msginfo);
888                 }
889
890                 cur = next;
891         }
892
893         return alist;
894 }
895
896 static void news_delete_all_articles(FolderItem *item)
897 {
898         gchar *dir;
899
900         g_return_if_fail(item != NULL);
901         g_return_if_fail(item->folder != NULL);
902         g_return_if_fail(item->folder->type == F_NEWS);
903
904         debug_print(_("\tDeleting all cached articles... "));
905
906         dir = folder_item_get_path(item);
907         remove_all_numbered_files(dir);
908         g_free(dir);
909
910         debug_print(_("done.\n"));
911 }
912
913 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
914 {
915         gchar * tmp;
916         FILE * tmpfp;
917         gchar buf[BUFFSIZE];
918
919         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
920                               G_DIR_SEPARATOR, (gint)msginfo);
921         if (tmp == NULL)
922                 return -1;
923
924         if ((tmpfp = fopen(tmp, "wb")) == NULL) {
925                 FILE_OP_ERROR(tmp, "fopen");
926                 return -1;
927         }
928         if (change_file_mode_rw(tmpfp, tmp) < 0) {
929                 FILE_OP_ERROR(tmp, "chmod");
930                 g_warning(_("can't change file mode\n"));
931         }
932         
933         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
934         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
935         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
936         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
937         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
938         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
939         get_rfc822_date(buf, sizeof(buf));
940         fprintf(tmpfp, "Date: %s\r\n", buf);
941         fprintf(tmpfp, "\r\n");
942         fprintf(tmpfp, "removed with sylpheed\r\n");
943
944         fclose(tmpfp);
945
946         news_post(folder, tmp);
947         remove(tmp);
948
949         g_free(tmp);
950
951         return 0;
952 }