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