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