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