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