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