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