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