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