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