7b7a7e04f8da73dd0dd485df7727b8eab83cbc26
[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 static void news_set_msg_flags(FolderItem *item, MsgInfo *msginfo)
923 {
924         msginfo->flags.tmp_flags = 0;
925         if (item->folder->account->mark_crosspost_read) {
926                 if (item->folder->newsart && msginfo->msgid &&
927                     g_hash_table_lookup(item->folder->newsart, msginfo->msgid) != NULL) {
928                         msginfo->flags.perm_flags = MSG_COLORLABEL_TO_FLAGS(item->folder->account->crosspost_col);
929                                 
930                 } else {
931                         if (!item->folder->newsart) 
932                                 item->folder->newsart = g_hash_table_new(g_str_hash, g_str_equal);
933                         g_hash_table_insert(item->folder->newsart,
934                                         g_strdup(msginfo->msgid), GINT_TO_POINTER(1));
935                         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
936                 }
937         } else {
938                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
939         }
940 }
941
942 MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
943 {
944         NNTPSession *session;
945         MsgInfo *msginfo = NULL;
946         gchar buf[NNTPBUFSIZE];
947         gint ok;
948
949         session = news_session_get(folder);
950         g_return_val_if_fail(session != NULL, NULL);
951         g_return_val_if_fail(item != NULL, NULL);
952         g_return_val_if_fail(item->folder != NULL, NULL);
953         g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
954
955         log_message(_("getting xover %d in %s...\n"),
956                     num, item->path);
957         ok = nntp_xover(session, num, num);
958         
959         news_folder_lock(NEWS_FOLDER(item->folder));
960         
961         if (ok != NN_SUCCESS) {
962                 log_warning(_("can't get xover\n"));
963                 if (ok == NN_SOCKET) {
964                         session_destroy(SESSION(session));
965                         REMOTE_FOLDER(item->folder)->session = NULL;
966                 }
967                 news_folder_unlock(NEWS_FOLDER(item->folder));
968                 return NULL;
969         }
970         
971         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
972                 log_warning(_("error occurred while getting xover.\n"));
973                 news_folder_unlock(NEWS_FOLDER(item->folder));
974                 return NULL;
975         }
976         
977         msginfo = news_parse_xover(buf);
978         if (!msginfo) {
979                 log_warning(_("invalid xover line: %s\n"), buf);
980         }
981
982         READ_TO_LISTEND("xover");
983
984         if(!msginfo) {
985                 news_folder_unlock(NEWS_FOLDER(item->folder));
986                 return NULL;
987         }
988
989         msginfo->folder = item;
990         
991         news_set_msg_flags(item, msginfo);
992         msginfo->flags.tmp_flags |= MSG_NEWS;
993         msginfo->newsgroups = g_strdup(item->path);
994
995         ok = nntp_xhdr(session, "to", 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->to = news_parse_xhdr(buf, msginfo);
1013
1014         READ_TO_LISTEND("xhdr (to)");
1015
1016         ok = nntp_xhdr(session, "cc", num, num);
1017         if (ok != NN_SUCCESS) {
1018                 log_warning(_("can't get xhdr\n"));
1019                 if (ok == NN_SOCKET) {
1020                         session_destroy(SESSION(session));
1021                         REMOTE_FOLDER(item->folder)->session = NULL;
1022                 }
1023                 news_folder_unlock(NEWS_FOLDER(item->folder));
1024                 return msginfo;
1025         }
1026
1027         if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1028                 log_warning(_("error occurred while getting xhdr.\n"));
1029                 news_folder_unlock(NEWS_FOLDER(item->folder));
1030                 return msginfo;
1031         }
1032
1033         msginfo->cc = news_parse_xhdr(buf, msginfo);
1034
1035         READ_TO_LISTEND("xhdr (cc)");
1036         news_folder_unlock(NEWS_FOLDER(item->folder));
1037         return msginfo;
1038 }
1039
1040 static GSList *news_get_msginfos_for_range(NNTPSession *session, FolderItem *item, guint begin, guint end)
1041 {
1042         gchar buf[NNTPBUFSIZE];
1043         GSList *newlist = NULL;
1044         GSList *llast = NULL;
1045         MsgInfo *msginfo;
1046         guint count = 0, lines = (end - begin) + 2;
1047         gint ok;
1048
1049         g_return_val_if_fail(session != NULL, NULL);
1050         g_return_val_if_fail(item != NULL, NULL);
1051
1052         log_message(_("getting xover %d - %d in %s...\n"),
1053                     begin, end, item->path);
1054         ok = nntp_xover(session, begin, end);
1055         if (ok != NN_SUCCESS) {
1056                 log_warning(_("can't get xover\n"));
1057                 if (ok == NN_SOCKET) {
1058                         session_destroy(SESSION(session));
1059                         REMOTE_FOLDER(item->folder)->session = NULL;
1060                 }
1061                 return NULL;
1062         }
1063
1064         news_folder_lock(NEWS_FOLDER(item->folder));
1065         
1066         for (;;) {
1067                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1068                         log_warning(_("error occurred while getting xover.\n"));
1069                         news_folder_unlock(NEWS_FOLDER(item->folder));
1070                         return newlist;
1071                 }
1072                 count++;
1073                 progressindicator_set_percentage
1074                         (PROGRESS_TYPE_NETWORK,
1075                          session->fetch_base_percentage +
1076                          (((gfloat) count) / ((gfloat) lines)) * session->fetch_total_percentage);
1077
1078                 if (buf[0] == '.' && buf[1] == '\r') break;
1079
1080                 msginfo = news_parse_xover(buf);
1081                 if (!msginfo) {
1082                         log_warning(_("invalid xover line: %s\n"), buf);
1083                         continue;
1084                 }
1085
1086                 msginfo->folder = item;
1087                 news_set_msg_flags(item, msginfo);
1088                 msginfo->flags.tmp_flags |= MSG_NEWS;
1089                 msginfo->newsgroups = g_strdup(item->path);
1090
1091                 if (!newlist)
1092                         llast = newlist = g_slist_append(newlist, msginfo);
1093                 else {
1094                         llast = g_slist_append(llast, msginfo);
1095                         llast = llast->next;
1096                 }
1097         }
1098
1099         ok = nntp_xhdr(session, "to", begin, end);
1100         if (ok != NN_SUCCESS) {
1101                 log_warning(_("can't get xhdr\n"));
1102                 if (ok == NN_SOCKET) {
1103                         session_destroy(SESSION(session));
1104                         REMOTE_FOLDER(item->folder)->session = NULL;
1105                 }
1106                 news_folder_unlock(NEWS_FOLDER(item->folder));
1107                 return newlist;
1108         }
1109
1110         llast = newlist;
1111
1112         for (;;) {
1113                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1114                         log_warning(_("error occurred while getting xhdr.\n"));
1115                         news_folder_unlock(NEWS_FOLDER(item->folder));
1116                         return newlist;
1117                 }
1118
1119                 if (buf[0] == '.' && buf[1] == '\r') break;
1120                 if (!llast) {
1121                         g_warning("llast == NULL\n");
1122                         continue;
1123                 }
1124
1125                 msginfo = (MsgInfo *)llast->data;
1126                 msginfo->to = news_parse_xhdr(buf, msginfo);
1127
1128                 llast = llast->next;
1129         }
1130
1131         ok = nntp_xhdr(session, "cc", begin, end);
1132         if (ok != NN_SUCCESS) {
1133                 log_warning(_("can't get xhdr\n"));
1134                 if (ok == NN_SOCKET) {
1135                         session_destroy(SESSION(session));
1136                         REMOTE_FOLDER(item->folder)->session = NULL;
1137                 }
1138                 news_folder_unlock(NEWS_FOLDER(item->folder));
1139                 return newlist;
1140         }
1141
1142         llast = newlist;
1143
1144         for (;;) {
1145                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
1146                         log_warning(_("error occurred while getting xhdr.\n"));
1147                         news_folder_unlock(NEWS_FOLDER(item->folder));
1148                         return newlist;
1149                 }
1150
1151                 if (buf[0] == '.' && buf[1] == '\r') break;
1152                 if (!llast) {
1153                         g_warning("llast == NULL\n");
1154                         continue;
1155                 }
1156
1157                 msginfo = (MsgInfo *)llast->data;
1158                 msginfo->cc = news_parse_xhdr(buf, msginfo);
1159
1160                 llast = llast->next;
1161         }
1162         news_folder_unlock(NEWS_FOLDER(item->folder));
1163
1164         session_set_access_time(SESSION(session));
1165
1166         return newlist;
1167 }
1168
1169 GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1170 {
1171         NNTPSession *session;
1172         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1173         guint first, last, next;
1174         guint tofetch, fetched;
1175         
1176         g_return_val_if_fail(folder != NULL, NULL);
1177         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1178         g_return_val_if_fail(msgnum_list != NULL, NULL);
1179         g_return_val_if_fail(item != NULL, NULL);
1180         
1181         session = news_session_get(folder);
1182         g_return_val_if_fail(session != NULL, NULL);
1183
1184         tmp_msgnum_list = g_slist_copy(msgnum_list);
1185         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1186
1187         progressindicator_start(PROGRESS_TYPE_NETWORK);
1188         tofetch = g_slist_length(tmp_msgnum_list);
1189         fetched = 0;
1190
1191         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1192         last = first;
1193         
1194         news_folder_lock(NEWS_FOLDER(item->folder));
1195         
1196         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1197                 next = GPOINTER_TO_INT(elem->data);
1198                 if(next != (last + 1)) {
1199                         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1200                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1201                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1202                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1203                         fetched = last - first + 1;
1204                         first = next;
1205                 }
1206                 last = next;
1207         }
1208         
1209         news_folder_unlock(NEWS_FOLDER(item->folder));
1210         
1211         session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1212         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1213         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1214         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1215
1216         g_slist_free(tmp_msgnum_list);
1217         
1218         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1219
1220         return msginfo_list;
1221 }
1222
1223 gboolean news_scan_required(Folder *folder, FolderItem *item)
1224 {
1225         return TRUE;
1226 }
1227
1228 void news_synchronise(FolderItem *item) 
1229 {
1230         news_gtk_synchronise(item);
1231 }
1232
1233 static gint news_remove_folder(Folder *folder, FolderItem *item)
1234 {
1235         gchar *path;
1236
1237         g_return_val_if_fail(folder != NULL, -1);
1238         g_return_val_if_fail(item != NULL, -1);
1239         g_return_val_if_fail(item->path != NULL, -1);
1240
1241         path = folder_item_get_path(item);
1242         if (remove_dir_recursive(path) < 0) {
1243                 g_warning("can't remove directory `%s'\n", path);
1244                 g_free(path);
1245                 return -1;
1246         }
1247
1248         g_free(path);
1249         folder_item_remove(item);
1250         return 0;
1251 }