cancel news is finally implemented
[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         MsgInfo * msginfo;
563         gchar * filename;
564         MsgFlags msgflags = { 0, 0 };
565         gint r;
566
567         filename = folder_item_fetch_msg(item, num);
568         if (filename == NULL)
569                 return -1;
570
571         msginfo = procheader_parse(filename, msgflags, FALSE, FALSE);
572         if (msginfo == NULL)
573                 return -1;
574
575         r = news_cancel_article(folder, msginfo);
576
577         procmsg_msginfo_free(msginfo);
578
579         return r;
580 }
581
582 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
583 {
584         return news_get_article_cmd(session, "ARTICLE", num, filename);
585 }
586
587 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
588 {
589         return news_get_article_cmd(session, "HEAD", num, filename);
590 }
591
592 /**
593  * news_select_group:
594  * @session: Active NNTP session.
595  * @group: Newsgroup name.
596  * @num: Estimated number of articles.
597  * @first: First article number.
598  * @last: Last article number.
599  *
600  * Select newsgroup @group with the GROUP command if it is not already
601  * selected in @session, or article numbers need to be returned.
602  *
603  * Return value: NNTP result code.
604  **/
605 static gint news_select_group(NNTPSession *session, const gchar *group,
606                               gint *num, gint *first, gint *last)
607 {
608         gint ok;
609         gint num_, first_, last_;
610
611         if (!num || !first || !last) {
612                 if (session->group && g_strcasecmp(session->group, group) == 0)
613                         return NN_SUCCESS;
614                 num = &num_;
615                 first = &first_;
616                 last = &last_;
617         }
618
619         g_free(session->group);
620         session->group = NULL;
621
622         ok = nntp_group(session->nntp_sock, group, num, first, last);
623         if (ok == NN_SUCCESS)
624                 session->group = g_strdup(group);
625
626         return ok;
627 }
628
629 static GSList *news_get_uncached_articles(NNTPSession *session,
630                                           FolderItem *item, gint cache_last,
631                                           gint *rfirst, gint *rlast)
632 {
633         gint ok;
634         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
635         gchar buf[NNTPBUFSIZE];
636         GSList *newlist = NULL;
637         GSList *llast = NULL;
638         MsgInfo *msginfo;
639
640         if (rfirst) *rfirst = 0;
641         if (rlast)  *rlast  = 0;
642
643         g_return_val_if_fail(session != NULL, NULL);
644         g_return_val_if_fail(item != NULL, NULL);
645         g_return_val_if_fail(item->folder != NULL, NULL);
646         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
647
648         ok = news_select_group(session, item->path, &num, &first, &last);
649         if (ok != NN_SUCCESS) {
650                 log_warning(_("can't set group: %s\n"), item->path);
651                 return NULL;
652         }
653
654         /* calculate getting overview range */
655         if (first > last) {
656                 log_warning(_("invalid article range: %d - %d\n"),
657                             first, last);
658                 return NULL;
659         }
660         if (cache_last < first)
661                 begin = first;
662         else if (last < cache_last)
663                 begin = first;
664         else if (last == cache_last) {
665                 debug_print(_("no new articles.\n"));
666                 return NULL;
667         } else
668                 begin = cache_last + 1;
669         end = last;
670
671         if (rfirst) *rfirst = first;
672         if (rlast)  *rlast  = last;
673
674         if (prefs_common.max_articles > 0 &&
675             end - begin + 1 > prefs_common.max_articles)
676                 begin = end - prefs_common.max_articles + 1;
677
678         log_message(_("getting xover %d - %d in %s...\n"),
679                     begin, end, item->path);
680         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
681                 log_warning(_("can't get xover\n"));
682                 return NULL;
683         }
684
685         for (;;) {
686                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
687                         log_warning(_("error occurred while getting xover.\n"));
688                         return newlist;
689                 }
690
691                 if (buf[0] == '.' && buf[1] == '\r') break;
692
693                 msginfo = news_parse_xover(buf);
694                 if (!msginfo) {
695                         log_warning(_("invalid xover line: %s\n"), buf);
696                         continue;
697                 }
698
699                 msginfo->folder = item;
700                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
701                 msginfo->flags.tmp_flags = MSG_NEWS;
702                 msginfo->newsgroups = g_strdup(item->path);
703
704                 if (!newlist)
705                         llast = newlist = g_slist_append(newlist, msginfo);
706                 else {
707                         llast = g_slist_append(llast, msginfo);
708                         llast = llast->next;
709                 }
710         }
711
712         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
713                 log_warning(_("can't get xhdr\n"));
714                 return newlist;
715         }
716
717         llast = newlist;
718
719         for (;;) {
720                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
721                         log_warning(_("error occurred while getting xhdr.\n"));
722                         return newlist;
723                 }
724
725                 if (buf[0] == '.' && buf[1] == '\r') break;
726                 if (!llast) {
727                         g_warning("llast == NULL\n");
728                         continue;
729                 }
730
731                 msginfo = (MsgInfo *)llast->data;
732                 msginfo->to = news_parse_xhdr(buf, msginfo);
733
734                 llast = llast->next;
735         }
736
737         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
738                 log_warning(_("can't get xhdr\n"));
739                 return newlist;
740         }
741
742         llast = newlist;
743
744         for (;;) {
745                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
746                         log_warning(_("error occurred while getting xhdr.\n"));
747                         return newlist;
748                 }
749
750                 if (buf[0] == '.' && buf[1] == '\r') break;
751                 if (!llast) {
752                         g_warning("llast == NULL\n");
753                         continue;
754                 }
755
756                 msginfo = (MsgInfo *)llast->data;
757                 msginfo->cc = news_parse_xhdr(buf, msginfo);
758
759                 llast = llast->next;
760         }
761
762         return newlist;
763 }
764
765 #define PARSE_ONE_PARAM(p, srcp) \
766 { \
767         p = strchr(srcp, '\t'); \
768         if (!p) return NULL; \
769         else \
770                 *p++ = '\0'; \
771 }
772
773 static MsgInfo *news_parse_xover(const gchar *xover_str)
774 {
775         MsgInfo *msginfo;
776         gchar buf[NNTPBUFSIZE];
777         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
778         gchar *p;
779         gint num, size_int, line_int;
780         gchar *xover_buf;
781
782         Xstrdup_a(xover_buf, xover_str, return NULL);
783
784         PARSE_ONE_PARAM(subject, xover_buf);
785         PARSE_ONE_PARAM(sender, subject);
786         PARSE_ONE_PARAM(date, sender);
787         PARSE_ONE_PARAM(msgid, date);
788         PARSE_ONE_PARAM(ref, msgid);
789         PARSE_ONE_PARAM(size, ref);
790         PARSE_ONE_PARAM(line, size);
791
792         tmp = strchr(line, '\t');
793         if (!tmp) tmp = strchr(line, '\r');
794         if (!tmp) tmp = strchr(line, '\n');
795         if (tmp) *tmp = '\0';
796
797         num = atoi(xover_str);
798         size_int = atoi(size);
799         line_int = atoi(line);
800
801         /* set MsgInfo */
802         msginfo = g_new0(MsgInfo, 1);
803         msginfo->msgnum = num;
804         msginfo->size = size_int;
805
806         msginfo->date = g_strdup(date);
807         msginfo->date_t = procheader_date_parse(NULL, date, 0);
808
809         conv_unmime_header(buf, sizeof(buf), sender, NULL);
810         msginfo->from = g_strdup(buf);
811         msginfo->fromname = procheader_get_fromname(buf);
812
813         conv_unmime_header(buf, sizeof(buf), subject, NULL);
814         msginfo->subject = g_strdup(buf);
815
816         extract_parenthesis(msgid, '<', '>');
817         remove_space(msgid);
818         if (*msgid != '\0')
819                 msginfo->msgid = g_strdup(msgid);
820
821         msginfo->references = g_strdup(ref);
822         eliminate_parenthesis(ref, '(', ')');
823         if ((p = strrchr(ref, '<')) != NULL) {
824                 extract_parenthesis(p, '<', '>');
825                 remove_space(p);
826                 if (*p != '\0')
827                         msginfo->inreplyto = g_strdup(p);
828         }
829
830         return msginfo;
831 }
832
833 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
834 {
835         gchar *p;
836         gchar *tmp;
837         gint num;
838
839         p = strchr(xhdr_str, ' ');
840         if (!p)
841                 return NULL;
842         else
843                 p++;
844
845         num = atoi(xhdr_str);
846         if (msginfo->msgnum != num) return NULL;
847
848         tmp = strchr(p, '\r');
849         if (!tmp) tmp = strchr(p, '\n');
850
851         if (tmp)
852                 return g_strndup(p, tmp - p);
853         else
854                 return g_strdup(p);
855 }
856
857 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
858                                         gint first)
859 {
860         GSList *cur, *next;
861         MsgInfo *msginfo;
862         gchar *dir;
863
864         g_return_val_if_fail(item != NULL, alist);
865         g_return_val_if_fail(item->folder != NULL, alist);
866         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
867
868         if (first < 2) return alist;
869
870         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
871
872         dir = folder_item_get_path(item);
873         remove_numbered_files(dir, 1, first - 1);
874         g_free(dir);
875
876         for (cur = alist; cur != NULL; ) {
877                 next = cur->next;
878
879                 msginfo = (MsgInfo *)cur->data;
880                 if (msginfo && msginfo->msgnum < first) {
881                         procmsg_msginfo_free(msginfo);
882                         alist = g_slist_remove(alist, msginfo);
883                 }
884
885                 cur = next;
886         }
887
888         return alist;
889 }
890
891 static void news_delete_all_articles(FolderItem *item)
892 {
893         gchar *dir;
894
895         g_return_if_fail(item != NULL);
896         g_return_if_fail(item->folder != NULL);
897         g_return_if_fail(item->folder->type == F_NEWS);
898
899         debug_print(_("\tDeleting all cached articles... "));
900
901         dir = folder_item_get_path(item);
902         remove_all_numbered_files(dir);
903         g_free(dir);
904
905         debug_print(_("done.\n"));
906 }
907
908 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
909 {
910         gchar * tmp;
911         FILE * tmpfp;
912         gchar buf[BUFFSIZE];
913
914         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
915                               G_DIR_SEPARATOR, (gint)msginfo);
916         if (tmp == NULL)
917                 return -1;
918
919         if ((tmpfp = fopen(tmp, "w")) == NULL) {
920                 FILE_OP_ERROR(tmp, "fopen");
921                 return -1;
922         }
923         if (change_file_mode_rw(tmpfp, tmp) < 0) {
924                 FILE_OP_ERROR(tmp, "chmod");
925                 g_warning(_("can't change file mode\n"));
926         }
927         
928         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
929         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
930         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
931         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
932         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
933         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
934         get_rfc822_date(buf, sizeof(buf));
935         fprintf(tmpfp, "Date: %s\r\n", buf);
936         fprintf(tmpfp, "\r\n");
937         fprintf(tmpfp, "removed with sylpheed\r\n");
938
939         fclose(tmpfp);
940
941         news_post(folder, tmp);
942         remove(tmp);
943
944         g_free(tmp);
945
946         return 0;
947 }