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