2005-10-09 [colin] 1.9.15cvs25
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <dirent.h>
32 #include <unistd.h>
33 #include <time.h>
34
35 #include "news.h"
36 #include "news_gtk.h"
37 #include "nntp.h"
38 #include "socket.h"
39 #include "recv.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "folder.h"
43 #include "session.h"
44 #include "statusbar.h"
45 #include "codeconv.h"
46 #include "utils.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
49 #include "inputdialog.h"
50 #include "log.h"
51 #include "progressindicator.h"
52 #include "remotefolder.h"
53 #include "alertpanel.h"
54 #include "inc.h"
55
56 #if USE_OPENSSL
57 #  include "ssl.h"
58 #endif
59
60 #define NNTP_PORT       119
61 #if USE_OPENSSL
62 #define NNTPS_PORT      563
63 #endif
64
65 typedef struct _NewsFolder      NewsFolder;
66
67 #define NEWS_FOLDER(obj)        ((NewsFolder *)obj)
68
69 struct _NewsFolder
70 {
71         RemoteFolder rfolder;
72
73         gboolean use_auth;
74 };
75
76 static void news_folder_init             (Folder        *folder,
77                                           const gchar   *name,
78                                           const gchar   *path);
79
80 static Folder   *news_folder_new        (const gchar    *name,
81                                          const gchar    *folder);
82 static void      news_folder_destroy    (Folder         *folder);
83
84 static gchar *news_fetch_msg            (Folder         *folder,
85                                          FolderItem     *item,
86                                          gint            num);
87
88
89 #if USE_OPENSSL
90 static Session *news_session_new         (const gchar   *server,
91                                           gushort        port,
92                                           const gchar   *userid,
93                                           const gchar   *passwd,
94                                           SSLType        ssl_type);
95 #else
96 static Session *news_session_new         (const gchar   *server,
97                                           gushort        port,
98                                           const gchar   *userid,
99                                           const gchar   *passwd);
100 #endif
101
102 static gint news_get_article_cmd         (NNTPSession   *session,
103                                           const gchar   *cmd,
104                                           gint           num,
105                                           gchar         *filename);
106 static gint news_get_article             (NNTPSession   *session,
107                                           gint           num,
108                                           gchar         *filename);
109
110 static gint news_select_group            (NNTPSession   *session,
111                                           const gchar   *group,
112                                           gint          *num,
113                                           gint          *first,
114                                           gint          *last);
115 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
116 static gchar *news_parse_xhdr            (const gchar   *xhdr_str,
117                                           MsgInfo       *msginfo);
118 gint news_get_num_list                   (Folder        *folder, 
119                                           FolderItem    *item,
120                                           GSList       **list,
121                                           gboolean      *old_uids_valid);
122 MsgInfo *news_get_msginfo                (Folder        *folder, 
123                                           FolderItem    *item,
124                                           gint           num);
125 GSList *news_get_msginfos                (Folder        *folder,
126                                           FolderItem    *item,
127                                           GSList        *msgnum_list);
128 gboolean news_scan_required              (Folder        *folder,
129                                           FolderItem    *item);
130
131 gint news_post_stream                    (Folder        *folder, 
132                                           FILE          *fp);
133 static gchar *news_folder_get_path       (Folder        *folder);
134 gchar *news_item_get_path                (Folder        *folder,
135                                           FolderItem    *item);
136 static void news_synchronise             (FolderItem    *item);
137
138 static FolderClass news_class;
139
140 FolderClass *news_get_class(void)
141 {
142         if (news_class.idstr == NULL) {
143                 news_class.type = F_NEWS;
144                 news_class.idstr = "news";
145                 news_class.uistr = "News";
146
147                 /* Folder functions */
148                 news_class.new_folder = news_folder_new;
149                 news_class.destroy_folder = news_folder_destroy;
150
151                 /* FolderItem functions */
152                 news_class.item_get_path = news_item_get_path;
153                 news_class.get_num_list = news_get_num_list;
154                 news_class.scan_required = news_scan_required;
155
156                 /* Message functions */
157                 news_class.get_msginfo = news_get_msginfo;
158                 news_class.get_msginfos = news_get_msginfos;
159                 news_class.fetch_msg = news_fetch_msg;
160                 news_class.synchronise = news_synchronise;
161         };
162
163         return &news_class;
164 }
165
166 static Folder *news_folder_new(const gchar *name, const gchar *path)
167 {
168         Folder *folder;
169
170         folder = (Folder *)g_new0(NewsFolder, 1);
171         folder->klass = &news_class;
172         news_folder_init(folder, name, path);
173
174         return folder;
175 }
176
177 static void news_folder_destroy(Folder *folder)
178 {
179         gchar *dir;
180
181         dir = news_folder_get_path(folder);
182         if (is_dir_exist(dir))
183                 remove_dir_recursive(dir);
184         g_free(dir);
185
186         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
187 }
188
189 static void news_folder_init(Folder *folder, const gchar *name,
190                              const gchar *path)
191 {
192         folder_remote_folder_init(folder, name, path);
193 }
194
195 #if USE_OPENSSL
196 static Session *news_session_new(const gchar *server, gushort port,
197                                  const gchar *userid, const gchar *passwd,
198                                  SSLType ssl_type)
199 #else
200 static Session *news_session_new(const gchar *server, gushort port,
201                                  const gchar *userid, const gchar *passwd)
202 #endif
203 {
204         gchar buf[NNTPBUFSIZE];
205         Session *session;
206
207         g_return_val_if_fail(server != NULL, NULL);
208
209         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
210
211 #if USE_OPENSSL
212         session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
213 #else
214         session = nntp_session_new(server, port, buf, userid, passwd);
215 #endif
216
217         return session;
218 }
219
220 static Session *news_session_new_for_folder(Folder *folder)
221 {
222         Session *session;
223         PrefsAccount *ac;
224         const gchar *userid = NULL;
225         gchar *passwd = NULL;
226         gushort port;
227         gchar buf[NNTPBUFSIZE];
228
229         g_return_val_if_fail(folder != NULL, NULL);
230         g_return_val_if_fail(folder->account != NULL, NULL);
231
232         ac = folder->account;
233         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
234                 userid = ac->userid;
235                 if (ac->passwd && ac->passwd[0])
236                         passwd = g_strdup(ac->passwd);
237                 else
238                         passwd = input_dialog_query_password(ac->nntp_server,
239                                                              userid);
240         }
241
242 #if USE_OPENSSL
243         port = ac->set_nntpport ? ac->nntpport
244                 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
245         session = news_session_new(ac->nntp_server, port, userid, passwd,
246                                    ac->ssl_nntp);
247 #else
248         if (ac->ssl_nntp != SSL_NONE) {
249                 if (alertpanel_full(_("Insecure connection"),
250                         _("This connection is configured to be secured "
251                           "using SSL, but SSL is not available in this "
252                           "build of Sylpheed-Claws. \n\n"
253                           "Do you want to continue connecting to this "
254                           "server? The communication would not be "
255                           "secure."),
256                           _("Con_tinue connecting"), 
257                           GTK_STOCK_CANCEL, NULL,
258                           FALSE, NULL, ALERT_WARNING,
259                           G_ALERTALTERNATE) != G_ALERTDEFAULT)
260                         return NULL;
261         }
262         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
263         session = news_session_new(ac->nntp_server, port, userid, passwd);
264 #endif
265         if ((session != NULL) && ac->use_nntp_auth && ac->use_nntp_auth_onconnect)
266                 nntp_forceauth(NNTP_SESSION(session), buf, userid, passwd);
267
268         g_free(passwd);
269
270         return session;
271 }
272
273 static NNTPSession *news_session_get(Folder *folder)
274 {
275         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
276
277         g_return_val_if_fail(folder != NULL, NULL);
278         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
279         g_return_val_if_fail(folder->account != NULL, NULL);
280
281         if (prefs_common.work_offline && !inc_offline_should_override()) {
282                 return NULL;
283         }
284
285         if (!rfolder->session) {
286                 rfolder->session = news_session_new_for_folder(folder);
287                 return NNTP_SESSION(rfolder->session);
288         }
289
290         if (time(NULL) - rfolder->session->last_access_time <
291                 SESSION_TIMEOUT_INTERVAL) {
292                 return NNTP_SESSION(rfolder->session);
293         }
294
295         if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
296             != NN_SUCCESS) {
297                 log_warning(_("NNTP connection to %s:%d has been"
298                               " disconnected. Reconnecting...\n"),
299                             folder->account->nntp_server,
300                             folder->account->set_nntpport ?
301                             folder->account->nntpport : NNTP_PORT);
302                 session_destroy(rfolder->session);
303                 rfolder->session = news_session_new_for_folder(folder);
304         }
305
306         if (rfolder->session)
307                 session_set_access_time(rfolder->session);
308
309         return NNTP_SESSION(rfolder->session);
310 }
311
312 static 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                 if (ok == NN_SOCKET) {
341                         session_destroy(SESSION(session));
342                         REMOTE_FOLDER(folder)->session = NULL;
343                 }
344                 g_free(filename);
345                 return NULL;
346         }
347
348         debug_print("getting article %d...\n", num);
349         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
350                               num, filename);
351         if (ok != NN_SUCCESS) {
352                 g_warning("can't read article %d\n", num);
353                 if (ok == NN_SOCKET) {
354                         session_destroy(SESSION(session));
355                         REMOTE_FOLDER(folder)->session = NULL;
356                 }
357                 g_free(filename);
358                 return NULL;
359         }
360
361         return filename;
362 }
363
364 static NewsGroupInfo *news_group_info_new(const gchar *name,
365                                           gint first, gint last, gchar type)
366 {
367         NewsGroupInfo *ginfo;
368
369         ginfo = g_new(NewsGroupInfo, 1);
370         ginfo->name = g_strdup(name);
371         ginfo->first = first;
372         ginfo->last = last;
373         ginfo->type = type;
374
375         return ginfo;
376 }
377
378 static void news_group_info_free(NewsGroupInfo *ginfo)
379 {
380         g_free(ginfo->name);
381         g_free(ginfo);
382 }
383
384 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
385                                     NewsGroupInfo *ginfo2)
386 {
387         return g_ascii_strcasecmp(ginfo1->name, ginfo2->name);
388 }
389
390 GSList *news_get_group_list(Folder *folder)
391 {
392         gchar *path, *filename;
393         FILE *fp;
394         GSList *list = NULL;
395         GSList *last = NULL;
396         gchar buf[NNTPBUFSIZE];
397
398         g_return_val_if_fail(folder != NULL, NULL);
399         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
400
401         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
402         if (!is_dir_exist(path))
403                 make_dir_hier(path);
404         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
405         g_free(path);
406
407         if ((fp = g_fopen(filename, "rb")) == NULL) {
408                 NNTPSession *session;
409                 gint ok;
410
411                 session = news_session_get(folder);
412                 if (!session) {
413                         g_free(filename);
414                         return NULL;
415                 }
416
417                 ok = nntp_list(session);
418                 if (ok != NN_SUCCESS) {
419                         if (ok == NN_SOCKET) {
420                                 session_destroy(SESSION(session));
421                                 REMOTE_FOLDER(folder)->session = NULL;
422                         }
423                         g_free(filename);
424                         return NULL;
425                 }
426                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
427                         log_warning(_("can't retrieve newsgroup list\n"));
428                         session_destroy(SESSION(session));
429                         REMOTE_FOLDER(folder)->session = NULL;
430                         g_free(filename);
431                         return NULL;
432                 }
433
434                 if ((fp = g_fopen(filename, "rb")) == NULL) {
435                         FILE_OP_ERROR(filename, "fopen");
436                         g_free(filename);
437                         return NULL;
438                 }
439         }
440
441         while (fgets(buf, sizeof(buf), fp) != NULL) {
442                 gchar *p = buf;
443                 gchar *name;
444                 gint last_num;
445                 gint first_num;
446                 gchar type;
447                 NewsGroupInfo *ginfo;
448
449                 p = strchr(p, ' ');
450                 if (!p) continue;
451                 *p = '\0';
452                 p++;
453                 name = buf;
454
455                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
456                         continue;
457
458                 ginfo = news_group_info_new(name, first_num, last_num, type);
459
460                 if (!last)
461                         last = list = g_slist_append(NULL, ginfo);
462                 else {
463                         last = g_slist_append(last, ginfo);
464                         last = last->next;
465                 }
466         }
467
468         fclose(fp);
469         g_free(filename);
470
471         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
472
473         return list;
474 }
475
476 void news_group_list_free(GSList *group_list)
477 {
478         GSList *cur;
479
480         if (!group_list) return;
481
482         for (cur = group_list; cur != NULL; cur = cur->next)
483                 news_group_info_free((NewsGroupInfo *)cur->data);
484         g_slist_free(group_list);
485 }
486
487 void news_remove_group_list_cache(Folder *folder)
488 {
489         gchar *path, *filename;
490
491         g_return_if_fail(folder != NULL);
492         g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
493
494         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
495         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
496         g_free(path);
497
498         if (is_file_exist(filename)) {
499                 if (remove(filename) < 0)
500                         FILE_OP_ERROR(filename, "remove");
501         }
502         g_free(filename);
503 }
504
505 gint news_post(Folder *folder, const gchar *file)
506 {
507         FILE *fp;
508         gint ok;
509
510         g_return_val_if_fail(folder != NULL, -1);
511         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
512         g_return_val_if_fail(file != NULL, -1);
513
514         if ((fp = g_fopen(file, "rb")) == NULL) {
515                 FILE_OP_ERROR(file, "fopen");
516                 return -1;
517         }
518
519         ok = news_post_stream(folder, fp);
520
521         fclose(fp);
522
523         return ok;
524 }
525
526 gint news_post_stream(Folder *folder, FILE *fp)
527 {
528         NNTPSession *session;
529         gint ok;
530
531         g_return_val_if_fail(folder != NULL, -1);
532         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
533         g_return_val_if_fail(fp != NULL, -1);
534
535         session = news_session_get(folder);
536         if (!session) return -1;
537
538         ok = nntp_post(session, fp);
539         if (ok != NN_SUCCESS) {
540                 log_warning(_("can't post article.\n"));
541                 if (ok == NN_SOCKET) {
542                         session_destroy(SESSION(session));
543                         REMOTE_FOLDER(folder)->session = NULL;
544                 }
545                 return -1;
546         }
547
548         return 0;
549 }
550
551 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
552                                  gint num, gchar *filename)
553 {
554         gchar *msgid;
555         gint ok;
556
557         ok = nntp_get_article(session, cmd, num, &msgid);
558         if (ok != NN_SUCCESS)
559                 return ok;
560
561         debug_print("Message-ID = %s, num = %d\n", msgid, num);
562         g_free(msgid);
563
564         ok = recv_write_to_file(SESSION(session)->sock, filename);
565         if (ok < 0) {
566                 log_warning(_("can't retrieve article %d\n"), num);
567                 if (ok == -2)
568                         return NN_SOCKET;
569                 else
570                         return NN_IOERR;
571         }
572
573         return NN_SUCCESS;
574 }
575
576 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
577 {
578         return news_get_article_cmd(session, "ARTICLE", num, filename);
579 }
580
581 /**
582  * news_select_group:
583  * @session: Active NNTP session.
584  * @group: Newsgroup name.
585  * @num: Estimated number of articles.
586  * @first: First article number.
587  * @last: Last article number.
588  *
589  * Select newsgroup @group with the GROUP command if it is not already
590  * selected in @session, or article numbers need to be returned.
591  *
592  * Return value: NNTP result code.
593  **/
594 static gint news_select_group(NNTPSession *session, const gchar *group,
595                               gint *num, gint *first, gint *last)
596 {
597         gint ok;
598         gint num_, first_, last_;
599
600         if (!num || !first || !last) {
601                 if (session->group && g_ascii_strcasecmp(session->group, group) == 0)
602                         return NN_SUCCESS;
603                 num = &num_;
604                 first = &first_;
605                 last = &last_;
606         }
607
608         g_free(session->group);
609         session->group = NULL;
610
611         ok = nntp_group(session, group, num, first, last);
612         if (ok == NN_SUCCESS)
613                 session->group = g_strdup(group);
614         else
615                 log_warning(_("can't select group: %s\n"), group);
616
617         return ok;
618 }
619
620 #define PARSE_ONE_PARAM(p, srcp) \
621 { \
622         p = strchr(srcp, '\t'); \
623         if (!p) return NULL; \
624         else \
625                 *p++ = '\0'; \
626 }
627
628 static MsgInfo *news_parse_xover(const gchar *xover_str)
629 {
630         MsgInfo *msginfo;
631         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
632         gchar *p;
633         gint num, size_int, line_int;
634         gchar *xover_buf;
635
636         Xstrdup_a(xover_buf, xover_str, return NULL);
637
638         PARSE_ONE_PARAM(subject, xover_buf);
639         PARSE_ONE_PARAM(sender, subject);
640         PARSE_ONE_PARAM(date, sender);
641         PARSE_ONE_PARAM(msgid, date);
642         PARSE_ONE_PARAM(ref, msgid);
643         PARSE_ONE_PARAM(size, ref);
644         PARSE_ONE_PARAM(line, size);
645         /*
646          * PARSE_ONE_PARAM(xref, line);
647          *
648          * if we parse extra headers we should first examine the
649          * LIST OVERVIEW.FMT response from the server. See
650          * RFC2980 for details
651          */
652
653         tmp = strchr(line, '\t');
654         if (!tmp) tmp = strchr(line, '\r');
655         if (!tmp) tmp = strchr(line, '\n');
656         if (tmp) *tmp = '\0';
657
658         num = atoi(xover_str);
659         size_int = atoi(size);
660         line_int = atoi(line);
661
662         /* set MsgInfo */
663         msginfo = procmsg_msginfo_new();
664         msginfo->msgnum = num;
665         msginfo->size = size_int;
666
667         msginfo->date = g_strdup(date);
668         msginfo->date_t = procheader_date_parse(NULL, date, 0);
669
670         msginfo->from = conv_unmime_header(sender, NULL);
671         msginfo->fromname = procheader_get_fromname(msginfo->from);
672
673         msginfo->subject = conv_unmime_header(subject, NULL);
674
675         if (msgid) {
676                 extract_parenthesis(msgid, '<', '>');
677                 remove_space(msgid);
678                 if (*msgid != '\0')
679                         msginfo->msgid = g_strdup(msgid);
680         }                        
681
682         /* FIXME: this is a quick fix; references' meaning was changed
683          * into having the actual list of references in the References: header.
684          * We need a GSList here, so msginfo_free() and msginfo_copy() can do 
685          * their things properly. */ 
686         if (ref && strlen(ref)) {       
687                 gchar **ref_tokens = g_strsplit(ref, " ", -1);
688                 guint i = 0;
689                 
690                 while (ref_tokens[i]) {
691                         gchar *cur_ref = ref_tokens[i];
692                         msginfo->references = references_list_append(msginfo->references, 
693                                         g_strdup(cur_ref));
694                         i++;
695                 }
696                 g_strfreev(ref_tokens);
697                 
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
707         /*
708         msginfo->xref = g_strdup(xref);
709         p = msginfo->xref+strlen(msginfo->xref) - 1;
710         while (*p == '\r' || *p == '\n') {
711                 *p = '\0';
712                 p--;
713         }
714         */
715
716         return msginfo;
717 }
718
719 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
720 {
721         gchar *p;
722         gchar *tmp;
723         gint num;
724
725         p = strchr(xhdr_str, ' ');
726         if (!p)
727                 return NULL;
728         else
729                 p++;
730
731         num = atoi(xhdr_str);
732         if (msginfo->msgnum != num) return NULL;
733
734         tmp = strchr(p, '\r');
735         if (!tmp) tmp = strchr(p, '\n');
736
737         if (tmp)
738                 return g_strndup(p, tmp - p);
739         else
740                 return g_strdup(p);
741 }
742
743 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
744 {
745         gchar * tmp;
746         FILE * tmpfp;
747         gchar buf[BUFFSIZE];
748
749         tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
750                               G_DIR_SEPARATOR, (gint)msginfo);
751         if (tmp == NULL)
752                 return -1;
753
754         if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
755                 FILE_OP_ERROR(tmp, "fopen");
756                 return -1;
757         }
758         if (change_file_mode_rw(tmpfp, tmp) < 0) {
759                 FILE_OP_ERROR(tmp, "chmod");
760                 g_warning("can't change file mode\n");
761         }
762         
763         fprintf(tmpfp, "From: %s\r\n", msginfo->from);
764         fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
765         fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
766         fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
767         fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
768         fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
769         get_rfc822_date(buf, sizeof(buf));
770         fprintf(tmpfp, "Date: %s\r\n", buf);
771         fprintf(tmpfp, "\r\n");
772         fprintf(tmpfp, "removed with sylpheed\r\n");
773
774         fclose(tmpfp);
775
776         news_post(folder, tmp);
777         remove(tmp);
778
779         g_free(tmp);
780
781         return 0;
782 }
783
784 static gchar *news_folder_get_path(Folder *folder)
785 {
786         gchar *folder_path;
787
788         g_return_val_if_fail(folder->account != NULL, NULL);
789
790         folder_path = g_strconcat(get_news_cache_dir(),
791                                   G_DIR_SEPARATOR_S,
792                                   folder->account->nntp_server,
793                                   NULL);
794         return folder_path;
795 }
796
797 gchar *news_item_get_path(Folder *folder, FolderItem *item)
798 {
799         gchar *folder_path, *path;
800
801         g_return_val_if_fail(folder != NULL, NULL);
802         g_return_val_if_fail(item != NULL, NULL);
803         folder_path = news_folder_get_path(folder);
804
805         g_return_val_if_fail(folder_path != NULL, NULL);
806         if (folder_path[0] == G_DIR_SEPARATOR) {
807                 if (item->path)
808                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
809                                            item->path, NULL);
810                 else
811                         path = g_strdup(folder_path);
812         } else {
813                 if (item->path)
814                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
815                                            folder_path, G_DIR_SEPARATOR_S,
816                                            item->path, NULL);
817                 else
818                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
819                                            folder_path, NULL);
820         }
821         g_free(folder_path);
822
823         return path;
824 }
825
826 gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
827 {
828         NNTPSession *session;
829         gint i, ok, num, first, last, nummsgs = 0;
830         gchar *dir;
831
832         g_return_val_if_fail(item != NULL, -1);
833         g_return_val_if_fail(item->folder != NULL, -1);
834         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
835
836         session = news_session_get(folder);
837         g_return_val_if_fail(session != NULL, -1);
838
839         *old_uids_valid = TRUE;
840
841         ok = news_select_group(session, item->path, &num, &first, &last);
842         if (ok != NN_SUCCESS) {
843                 log_warning(_("can't set group: %s\n"), item->path);
844                 return -1;
845         }
846
847         dir = news_folder_get_path(folder);
848         if (num <= 0)
849                 remove_all_numbered_files(dir);
850         else if (last < first)
851                 log_warning(_("invalid article range: %d - %d\n"),
852                             first, last);
853         else {
854                 for (i = first; i <= last; i++) {
855                         *msgnum_list = g_slist_prepend(*msgnum_list, 
856                                                        GINT_TO_POINTER(i));
857                         nummsgs++;
858                 }
859                 debug_print("removing old messages from %d to %d in %s\n",
860                             first, last, dir);
861                 remove_numbered_files(dir, 1, first - 1);
862         }
863         g_free(dir);
864
865         return nummsgs;
866 }
867
868 #define READ_TO_LISTEND(hdr) \
869         while (!(buf[0] == '.' && buf[1] == '\r')) { \
870                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) { \
871                         log_warning(_("error occurred while getting %s.\n"), hdr); \
872                         return msginfo; \
873                 } \
874         }
875
876 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
877 {
878         NNTPSession *session;
879         MsgInfo *msginfo = NULL;
880         gchar buf[NNTPBUFSIZE];
881         gint ok;
882
883         session = news_session_get(folder);
884         g_return_val_if_fail(session != NULL, NULL);
885         g_return_val_if_fail(item != NULL, NULL);
886         g_return_val_if_fail(item->folder != NULL, NULL);
887         g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
888
889         log_message(_("getting xover %d in %s...\n"),
890                     num, item->path);
891         ok = nntp_xover(session, num, num);
892         if (ok != NN_SUCCESS) {
893                 log_warning(_("can't get xover\n"));
894                 if (ok == NN_SOCKET) {
895                         session_destroy(SESSION(session));
896                         REMOTE_FOLDER(item->folder)->session = NULL;
897                 }
898                 return NULL;
899         }
900         
901         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
902                 log_warning(_("error occurred while getting xover.\n"));
903                 return NULL;
904         }
905         
906         msginfo = news_parse_xover(buf);
907         if (!msginfo) {
908                 log_warning(_("invalid xover line: %s\n"), buf);
909         }
910
911         READ_TO_LISTEND("xover");
912
913         if(!msginfo)
914                 return NULL;
915         
916         msginfo->folder = item;
917         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
918         msginfo->flags.tmp_flags = MSG_NEWS;
919         msginfo->newsgroups = g_strdup(item->path);
920
921         ok = nntp_xhdr(session, "to", num, num);
922         if (ok != NN_SUCCESS) {
923                 log_warning(_("can't get xhdr\n"));
924                 if (ok == NN_SOCKET) {
925                         session_destroy(SESSION(session));
926                         REMOTE_FOLDER(item->folder)->session = NULL;
927                 }
928                 return msginfo;
929         }
930
931         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
932                 log_warning(_("error occurred while getting xhdr.\n"));
933                 return msginfo;
934         }
935
936         msginfo->to = news_parse_xhdr(buf, msginfo);
937
938         READ_TO_LISTEND("xhdr (to)");
939
940         ok = nntp_xhdr(session, "cc", num, num);
941         if (ok != NN_SUCCESS) {
942                 log_warning(_("can't get xhdr\n"));
943                 if (ok == NN_SOCKET) {
944                         session_destroy(SESSION(session));
945                         REMOTE_FOLDER(item->folder)->session = NULL;
946                 }
947                 return msginfo;
948         }
949
950         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
951                 log_warning(_("error occurred while getting xhdr.\n"));
952                 return msginfo;
953         }
954
955         msginfo->cc = news_parse_xhdr(buf, msginfo);
956
957         READ_TO_LISTEND("xhdr (cc)");
958
959         return msginfo;
960 }
961
962 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
963 {
964         gchar buf[NNTPBUFSIZE];
965         GSList *newlist = NULL;
966         GSList *llast = NULL;
967         MsgInfo *msginfo;
968         guint count = 0, lines = (end - begin + 2) * 3;
969         gint ok;
970
971         g_return_val_if_fail(session != NULL, NULL);
972         g_return_val_if_fail(item != NULL, NULL);
973
974         log_message(_("getting xover %d - %d in %s...\n"),
975                     begin, end, item->path);
976         ok = nntp_xover(session, begin, end);
977         if (ok != NN_SUCCESS) {
978                 log_warning(_("can't get xover\n"));
979                 if (ok == NN_SOCKET) {
980                         session_destroy(SESSION(session));
981                         REMOTE_FOLDER(item->folder)->session = NULL;
982                 }
983                 return NULL;
984         }
985
986         for (;;) {
987                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
988                         log_warning(_("error occurred while getting xover.\n"));
989                         return newlist;
990                 }
991                 count++;
992                 progressindicator_set_percentage
993                         (PROGRESS_TYPE_NETWORK,
994                          session->fetch_base_percentage +
995                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
996
997                 if (buf[0] == '.' && buf[1] == '\r') break;
998
999                 msginfo = news_parse_xover(buf);
1000                 if (!msginfo) {
1001                         log_warning(_("invalid xover line: %s\n"), buf);
1002                         continue;
1003                 }
1004
1005                 msginfo->folder = item;
1006                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1007                 msginfo->flags.tmp_flags = MSG_NEWS;
1008                 msginfo->newsgroups = g_strdup(item->path);
1009
1010                 if (!newlist)
1011                         llast = newlist = g_slist_append(newlist, msginfo);
1012                 else {
1013                         llast = g_slist_append(llast, msginfo);
1014                         llast = llast->next;
1015                 }
1016         }
1017
1018         ok = nntp_xhdr(session, "to", begin, end);
1019         if (ok != NN_SUCCESS) {
1020                 log_warning(_("can't get xhdr\n"));
1021                 if (ok == NN_SOCKET) {
1022                         session_destroy(SESSION(session));
1023                         REMOTE_FOLDER(item->folder)->session = NULL;
1024                 }
1025                 return newlist;
1026         }
1027
1028         llast = newlist;
1029
1030         for (;;) {
1031                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1032                         log_warning(_("error occurred while getting xhdr.\n"));
1033                         return newlist;
1034                 }
1035                 count++;
1036                 progressindicator_set_percentage
1037                         (PROGRESS_TYPE_NETWORK,
1038                          session->fetch_base_percentage +
1039                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
1040
1041                 if (buf[0] == '.' && buf[1] == '\r') break;
1042                 if (!llast) {
1043                         g_warning("llast == NULL\n");
1044                         continue;
1045                 }
1046
1047                 msginfo = (MsgInfo *)llast->data;
1048                 msginfo->to = news_parse_xhdr(buf, msginfo);
1049
1050                 llast = llast->next;
1051         }
1052
1053         ok = nntp_xhdr(session, "cc", begin, end);
1054         if (ok != NN_SUCCESS) {
1055                 log_warning(_("can't get xhdr\n"));
1056                 if (ok == NN_SOCKET) {
1057                         session_destroy(SESSION(session));
1058                         REMOTE_FOLDER(item->folder)->session = NULL;
1059                 }
1060                 return newlist;
1061         }
1062
1063         llast = newlist;
1064
1065         for (;;) {
1066                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1067                         log_warning(_("error occurred while getting xhdr.\n"));
1068                         return newlist;
1069                 }
1070                 count++;
1071                 progressindicator_set_percentage
1072                         (PROGRESS_TYPE_NETWORK,
1073                          session->fetch_base_percentage +
1074                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
1075
1076                 if (buf[0] == '.' && buf[1] == '\r') break;
1077                 if (!llast) {
1078                         g_warning("llast == NULL\n");
1079                         continue;
1080                 }
1081
1082                 msginfo = (MsgInfo *)llast->data;
1083                 msginfo->cc = news_parse_xhdr(buf, msginfo);
1084
1085                 llast = llast->next;
1086         }
1087
1088         session_set_access_time(SESSION(session));
1089
1090         return newlist;
1091 }
1092
1093 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1094 {
1095         NNTPSession *session;
1096         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1097         guint first, last, next;
1098         guint tofetch, fetched;
1099         
1100         g_return_val_if_fail(folder != NULL, NULL);
1101         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1102         g_return_val_if_fail(msgnum_list != NULL, NULL);
1103         g_return_val_if_fail(item != NULL, NULL);
1104         
1105         session = news_session_get(folder);
1106         g_return_val_if_fail(session != NULL, NULL);
1107
1108         tmp_msgnum_list = g_slist_copy(msgnum_list);
1109         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1110
1111         progressindicator_start(PROGRESS_TYPE_NETWORK);
1112         tofetch = g_slist_length(tmp_msgnum_list);
1113         fetched = 0;
1114
1115         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1116         last = first;
1117         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1118                 next = GPOINTER_TO_INT(elem->data);
1119                 if(next != (last + 1)) {
1120                         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1121                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1122                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1123                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1124                         fetched = last - first + 1;
1125                         first = next;
1126                 }
1127                 last = next;
1128         }
1129         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1130         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1131         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1132         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1133
1134         g_slist_free(tmp_msgnum_list);
1135         
1136         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1137
1138         return msginfo_list;
1139 }
1140
1141 gboolean news_scan_required(Folder *folder, FolderItem *item)
1142 {
1143         return TRUE;
1144 }
1145
1146 void news_synchronise(FolderItem *item) 
1147 {
1148         news_gtk_synchronise(item);
1149 }