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