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