Do not try to destroy a NULL session after an unsuccesful NNTP connect attempt.
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 #include "claws-features.h"
23 #endif
24
25 #ifdef HAVE_LIBETPAN
26
27 #include "defs.h"
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <dirent.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <libetpan/libetpan.h>
38
39 #include "nntp-thread.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 #ifdef USE_GNUTLS
60 #  include "ssl.h"
61 #endif
62
63 #define NNTP_PORT       119
64 #ifdef 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         Folder *folder;
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 #ifdef 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 gint news_get_num_list                    (Folder        *folder, 
129                                           FolderItem    *item,
130                                           GSList       **list,
131                                           gboolean      *old_uids_valid);
132 static MsgInfo *news_get_msginfo                 (Folder        *folder, 
133                                           FolderItem    *item,
134                                           gint           num);
135 static GSList *news_get_msginfos                 (Folder        *folder,
136                                           FolderItem    *item,
137                                           GSList        *msgnum_list);
138 static gboolean news_scan_required               (Folder        *folder,
139                                           FolderItem    *item);
140
141 static gchar *news_folder_get_path       (Folder        *folder);
142 static gchar *news_item_get_path                 (Folder        *folder,
143                                           FolderItem    *item);
144 static void news_synchronise             (FolderItem    *item, gint days);
145 static int news_remove_msg               (Folder        *folder, 
146                                           FolderItem    *item, 
147                                           gint           msgnum);
148 static gint news_rename_folder           (Folder *folder,
149                                           FolderItem *item,
150                                           const gchar *name);
151 static gint news_remove_folder           (Folder        *folder,
152                                           FolderItem    *item);
153 static FolderClass news_class;
154
155 FolderClass *news_get_class(void)
156 {
157         if (news_class.idstr == NULL) {
158                 news_class.type = F_NEWS;
159                 news_class.idstr = "news";
160                 news_class.uistr = "News";
161                 news_class.supports_server_search = FALSE;
162
163                 /* Folder functions */
164                 news_class.new_folder = news_folder_new;
165                 news_class.destroy_folder = news_folder_destroy;
166
167                 /* FolderItem functions */
168                 news_class.item_get_path = news_item_get_path;
169                 news_class.get_num_list = news_get_num_list;
170                 news_class.scan_required = news_scan_required;
171                 news_class.rename_folder = news_rename_folder;
172                 news_class.remove_folder = news_remove_folder;
173
174                 /* Message functions */
175                 news_class.get_msginfo = news_get_msginfo;
176                 news_class.get_msginfos = news_get_msginfos;
177                 news_class.fetch_msg = news_fetch_msg;
178                 news_class.synchronise = news_synchronise;
179                 news_class.search_msgs = folder_item_search_msgs_local;
180                 news_class.remove_msg = news_remove_msg;
181                 news_class.remove_cached_msg = news_remove_cached_msg;
182         };
183
184         return &news_class;
185 }
186
187 guint nntp_folder_get_refcnt(Folder *folder)
188 {
189         return ((NewsFolder *)folder)->refcnt;
190 }
191
192 void nntp_folder_ref(Folder *folder)
193 {
194         ((NewsFolder *)folder)->refcnt++;
195 }
196
197 void nntp_folder_unref(Folder *folder)
198 {
199         if (((NewsFolder *)folder)->refcnt > 0)
200                 ((NewsFolder *)folder)->refcnt--;
201 }
202
203 static int news_remove_msg               (Folder        *folder, 
204                                           FolderItem    *item, 
205                                           gint           msgnum)
206 {
207         gchar *path, *filename;
208
209         cm_return_val_if_fail(folder != NULL, -1);
210         cm_return_val_if_fail(item != NULL, -1);
211
212         path = folder_item_get_path(item);
213         if (!is_dir_exist(path))
214                 make_dir_hier(path);
215         
216         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
217         g_free(path);
218         claws_unlink(filename);
219         g_free(filename);
220         return 0;
221 }
222
223 static void news_folder_lock(NewsFolder *folder)
224 {
225         folder->lock_count++;
226 }
227
228 static void news_folder_unlock(NewsFolder *folder)
229 {
230         if (folder->lock_count > 0)
231                 folder->lock_count--;
232 }
233
234 int news_folder_locked(Folder *folder)
235 {
236         if (folder == NULL)
237                 return 0;
238
239         return NEWS_FOLDER(folder)->lock_count;
240 }
241
242 static Folder *news_folder_new(const gchar *name, const gchar *path)
243 {
244         Folder *folder;
245
246         folder = (Folder *)g_new0(NewsFolder, 1);
247         folder->klass = &news_class;
248         news_folder_init(folder, name, path);
249
250         return folder;
251 }
252
253 static void news_folder_destroy(Folder *folder)
254 {
255         gchar *dir;
256
257         while (nntp_folder_get_refcnt(folder) > 0)
258                 gtk_main_iteration();
259
260         dir = news_folder_get_path(folder);
261         if (is_dir_exist(dir))
262                 remove_dir_recursive(dir);
263         g_free(dir);
264
265         nntp_done(folder);
266         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
267 }
268
269 static void news_folder_init(Folder *folder, const gchar *name,
270                              const gchar *path)
271 {
272         folder_remote_folder_init(folder, name, path);
273 }
274
275 static void news_session_destroy(Session *session)
276 {
277         NewsSession *news_session = NEWS_SESSION(session);
278
279         cm_return_if_fail(session != NULL);
280
281         if (news_session->group)
282                 g_free(news_session->group);
283 }
284
285 static gboolean nntp_ping(gpointer data)
286 {
287         Session *session = (Session *)data;
288         NewsSession *news_session = NEWS_SESSION(session);
289         int r;
290         struct tm lt;
291
292         if (session->state != SESSION_READY || news_folder_locked(news_session->folder))
293                 return FALSE;
294         
295         news_folder_lock(NEWS_FOLDER(news_session->folder));
296
297         if ((r = nntp_threaded_date(news_session->folder, &lt)) != NEWSNNTP_NO_ERROR) {
298                 if (r != NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED &&
299                     r != NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD) {
300                         log_warning(LOG_PROTOCOL, _("NNTP connection to %s:%d has been"
301                               " disconnected.\n"),
302                             news_session->folder->account->nntp_server,
303                             news_session->folder->account->set_nntpport ?
304                             news_session->folder->account->nntpport : NNTP_PORT);
305                         REMOTE_FOLDER(news_session->folder)->session = NULL;
306                         news_folder_unlock(NEWS_FOLDER(news_session->folder));
307                         session->state = SESSION_DISCONNECTED;
308                         session->sock = NULL;
309                         session_destroy(session);
310                         return FALSE;
311                 }
312         }
313
314         news_folder_unlock(NEWS_FOLDER(news_session->folder));
315         session_set_access_time(session);
316         return TRUE;
317 }
318
319
320 #ifdef USE_GNUTLS
321 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
322                                  const gchar *userid, const gchar *passwd,
323                                  SSLType ssl_type)
324 #else
325 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
326                                  const gchar *userid, const gchar *passwd)
327 #endif
328 {
329         NewsSession *session;
330         int r = 0;
331         cm_return_val_if_fail(server != NULL, NULL);
332
333         log_message(LOG_PROTOCOL, _("Account '%s': Connecting to NNTP server: %s:%d...\n"),
334                                     folder->account->account_name, server, port);
335
336         session = g_new0(NewsSession, 1);
337         session_init(SESSION(session), folder->account, FALSE);
338         SESSION(session)->type             = SESSION_NEWS;
339         SESSION(session)->server           = g_strdup(server);
340         SESSION(session)->port             = port;
341         SESSION(session)->sock             = NULL;
342         SESSION(session)->destroy          = news_session_destroy;
343         
344         nntp_init(folder);
345
346 #ifdef USE_GNUTLS
347         if (ssl_type != SSL_NONE)
348                 r = nntp_threaded_connect_ssl(folder, server, port);
349         else
350 #endif
351                 r = nntp_threaded_connect(folder, server, port);
352         
353         if (r != NEWSNNTP_NO_ERROR) {
354                 log_error(LOG_PROTOCOL, _("Error logging in to %s:%d...\n"), server, port);
355                 if (session != NULL)
356                         session_destroy(SESSION(session));
357                 return NULL;
358         }
359
360         session->folder = folder;
361
362         return SESSION(session);
363 }
364
365 static Session *news_session_new_for_folder(Folder *folder)
366 {
367         Session *session;
368         PrefsAccount *ac;
369         const gchar *userid = NULL;
370         gchar *passwd = NULL;
371         gushort port;
372         int r;
373
374         cm_return_val_if_fail(folder != NULL, NULL);
375         cm_return_val_if_fail(folder->account != NULL, NULL);
376
377         ac = folder->account;
378         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
379                 userid = ac->userid;
380                 if (ac->passwd && ac->passwd[0])
381                         passwd = g_strdup(ac->passwd);
382                 else
383                         passwd = input_dialog_query_password_keep(ac->nntp_server,
384                                                                   userid,
385                                                                   &(ac->session_passwd));
386         }
387
388 #ifdef USE_GNUTLS
389         port = ac->set_nntpport ? ac->nntpport
390                 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
391         session = news_session_new(folder, ac->nntp_server, port, userid, passwd,
392                                    ac->ssl_nntp);
393 #else
394         if (ac->ssl_nntp != SSL_NONE) {
395                 if (alertpanel_full(_("Insecure connection"),
396                         _("This connection is configured to be secured "
397                           "using SSL, but SSL is not available in this "
398                           "build of Claws Mail. \n\n"
399                           "Do you want to continue connecting to this "
400                           "server? The communication would not be "
401                           "secure."),
402                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
403                           NULL, FALSE, NULL, ALERT_WARNING,
404                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
405                         return NULL;
406         }
407         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
408         session = news_session_new(folder, ac->nntp_server, port, userid, passwd);
409 #endif
410
411         if (session != NULL)
412                 r = nntp_threaded_mode_reader(folder);
413         else
414                 r = NEWSNNTP_ERROR_CONNECTION_REFUSED;
415
416         if (r != NEWSNNTP_NO_ERROR) {
417             if (r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME) {
418                 /*
419                    FIX ME when libetpan implements 480 to indicate authorization
420                    is required to use this capability. Libetpan treats a 480 as a
421                    381 which is clearly wrong.
422                    RFC 4643 section 2.
423                    Response code 480
424                    Generic response
425                    Meaning: command unavailable until the client
426                    has authenticated itself.
427                 */
428                 /* if the server does not advertise the capability MODE-READER,
429                    we normally should not send MODE READER. However this can't
430                    hurt: a transit-only server returns 502 and closes the cnx.
431                    Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
432                 */
433                 log_error(LOG_PROTOCOL, _("Libetpan does not support return code 480 "
434                 "so for now we choose to continue\n"));
435             }
436             else if (r == NEWSNNTP_ERROR_UNEXPECTED_RESPONSE) {
437                 /* if the server does not advertise the capability MODE-READER,
438                    we normally should not send MODE READER. However this can't
439                    hurt: a transit-only server returns 502 and closes the cnx.
440                    Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
441                 */
442                 log_error(LOG_PROTOCOL, _("Mode reader failed, continuing nevertheless\n")); 
443             }
444             else {
445                 /* An error state bail out */
446                 log_error(LOG_PROTOCOL, _("Error creating session with %s:%d\n"), ac->nntp_server, port);
447                 if (session != NULL)
448                         session_destroy(SESSION(session));
449                 g_free(passwd);
450                 if (ac->session_passwd) {
451                         g_free(ac->session_passwd);
452                         ac->session_passwd = NULL;
453                 }
454                 return NULL;
455             }
456         }
457
458         if ((session != NULL) && ac->use_nntp_auth) { /* FIXME:  && ac->use_nntp_auth_onconnect */
459                 if (nntp_threaded_login(folder, userid, passwd) !=
460                         NEWSNNTP_NO_ERROR) {
461                         log_error(LOG_PROTOCOL, _("Error authenticating to %s:%d...\n"), ac->nntp_server, port);
462                         session_destroy(SESSION(session));
463                         g_free(passwd);
464                         if (ac->session_passwd) {
465                                 g_free(ac->session_passwd);
466                                 ac->session_passwd = NULL;
467                         }
468                         return NULL;
469                 }
470         }
471         g_free(passwd);
472
473         return session;
474 }
475
476 static NewsSession *news_session_get(Folder *folder)
477 {
478         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
479         
480         cm_return_val_if_fail(folder != NULL, NULL);
481         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
482         cm_return_val_if_fail(folder->account != NULL, NULL);
483
484         if (prefs_common.work_offline && 
485             !inc_offline_should_override(FALSE,
486                 _("Claws Mail needs network access in order "
487                   "to access the News server."))) {
488                 return NULL;
489         }
490
491         if (!rfolder->session) {
492                 rfolder->session = news_session_new_for_folder(folder);
493                 session_register_ping(SESSION(rfolder->session), nntp_ping);
494                 return NEWS_SESSION(rfolder->session);
495         }
496
497         /* Handle port change (also ssl/nossl change) without needing to
498          * restart application. */
499         if (rfolder->session->port != folder->account->nntpport) {
500                 session_destroy(rfolder->session);
501                 rfolder->session = news_session_new_for_folder(folder);
502                 session_register_ping(SESSION(rfolder->session), nntp_ping);
503                 goto newsession;
504         }
505         
506         if (time(NULL) - rfolder->session->last_access_time <
507                 SESSION_TIMEOUT_INTERVAL) {
508                 return NEWS_SESSION(rfolder->session);
509         }
510
511         if (!nntp_ping(rfolder->session)) {
512                 rfolder->session = news_session_new_for_folder(folder);
513                 session_register_ping(SESSION(rfolder->session), nntp_ping);
514         }
515
516 newsession:
517         if (rfolder->session)
518                 session_set_access_time(rfolder->session);
519
520         return NEWS_SESSION(rfolder->session);
521 }
522
523 static void news_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
524 {
525         gchar *path, *filename;
526
527         path = folder_item_get_path(item);
528
529         if (!is_dir_exist(path)) {
530                 g_free(path);
531                 return;
532         }
533
534         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
535         g_free(path);
536
537         if (is_file_exist(filename)) {
538                 claws_unlink(filename);
539         }
540         g_free(filename);
541 }
542
543 static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
544 {
545         gchar *path, *filename;
546         NewsSession *session;
547         gint ok;
548
549         cm_return_val_if_fail(folder != NULL, NULL);
550         cm_return_val_if_fail(item != NULL, NULL);
551
552         path = folder_item_get_path(item);
553         if (!is_dir_exist(path))
554                 make_dir_hier(path);
555         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
556         g_free(path);
557
558         if (is_file_exist(filename)) {
559                 debug_print("article %d has been already cached.\n", num);
560                 return filename;
561         }
562
563         session = news_session_get(folder);
564         if (!session) {
565                 g_free(filename);
566                 return NULL;
567         }
568
569         ok = news_select_group(folder, item->path, NULL, NULL, NULL);
570         if (ok != NEWSNNTP_NO_ERROR) {
571                 if (ok == NEWSNNTP_ERROR_STREAM) {
572                         session_destroy(SESSION(session));
573                         REMOTE_FOLDER(folder)->session = NULL;
574                 }
575                 g_free(filename);
576                 return NULL;
577         }
578
579         debug_print("getting article %d...\n", num);
580         ok = news_get_article(folder,
581                               num, filename);
582         if (ok != NEWSNNTP_NO_ERROR) {
583                 g_warning("can't read article %d\n", num);
584                 if (ok == NEWSNNTP_ERROR_STREAM) {
585                         session_destroy(SESSION(session));
586                         REMOTE_FOLDER(folder)->session = NULL;
587                 }
588                 g_free(filename);
589                 return NULL;
590         }
591         GTK_EVENTS_FLUSH();
592         return filename;
593 }
594
595 static NewsGroupInfo *news_group_info_new(const gchar *name,
596                                           gint first, gint last, gchar type)
597 {
598         NewsGroupInfo *ginfo;
599
600         ginfo = g_new(NewsGroupInfo, 1);
601         ginfo->name = g_strdup(name);
602         ginfo->first = first;
603         ginfo->last = last;
604         ginfo->type = type;
605
606         return ginfo;
607 }
608
609 static void news_group_info_free(NewsGroupInfo *ginfo)
610 {
611         g_free(ginfo->name);
612         g_free(ginfo);
613 }
614
615 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
616                                     NewsGroupInfo *ginfo2)
617 {
618         return g_ascii_strcasecmp(ginfo1->name, ginfo2->name);
619 }
620
621 GSList *news_get_group_list(Folder *folder)
622 {
623         gchar *path, *filename;
624         FILE *fp;
625         GSList *list = NULL;
626         GSList *last = NULL;
627         gchar buf[BUFFSIZE];
628
629         cm_return_val_if_fail(folder != NULL, NULL);
630         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
631
632         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
633         if (!is_dir_exist(path))
634                 make_dir_hier(path);
635         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
636         g_free(path);
637
638         if ((fp = g_fopen(filename, "rb")) == NULL) {
639                 NewsSession *session;
640                 gint ok;
641                 clist *grouplist = NULL;
642                 clistiter *cur;
643                 fp = g_fopen(filename, "wb");
644                 
645                 if (!fp) {
646                         g_free(filename);
647                         return NULL;
648                 }
649                 session = news_session_get(folder);
650                 if (!session) {
651                         fclose(fp);
652                         g_free(filename);
653                         return NULL;
654                 }
655
656                 ok = nntp_threaded_list(folder, &grouplist);
657                 
658                 if (ok != NEWSNNTP_NO_ERROR) {
659                         if (ok == NEWSNNTP_ERROR_STREAM) {
660                                 session_destroy(SESSION(session));
661                                 REMOTE_FOLDER(folder)->session = NULL;
662                         }
663                         fclose(fp);
664                         g_free(filename);
665                         return NULL;
666                 }
667                 
668                 if (grouplist) {
669                         for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) {
670                                 struct newsnntp_group_info *info = (struct newsnntp_group_info *)
671                                                                         clist_content(cur);
672                                 if (fprintf(fp, "%s %d %d %c\n",
673                                         info->grp_name,
674                                         info->grp_last,
675                                         info->grp_first,
676                                         info->grp_type) < 0) {
677                                         log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
678                                         session_destroy(SESSION(session));
679                                         REMOTE_FOLDER(folder)->session = NULL;
680                                         fclose(fp);
681                                         g_free(filename);
682                                         newsnntp_list_free(grouplist);
683                                         return NULL;
684                                 }
685                         }
686                         newsnntp_list_free(grouplist);
687                 }
688                 if (fclose(fp) == EOF) {
689                         log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
690                         session_destroy(SESSION(session));
691                         REMOTE_FOLDER(folder)->session = NULL;
692                         g_free(filename);
693                         return NULL;
694                 }
695
696                 if ((fp = g_fopen(filename, "rb")) == NULL) {
697                         FILE_OP_ERROR(filename, "fopen");
698                         g_free(filename);
699                         return NULL;
700                 }
701         }
702
703         while (fgets(buf, sizeof(buf), fp) != NULL) {
704                 gchar *p = buf;
705                 gchar *name;
706                 gint last_num;
707                 gint first_num;
708                 gchar type;
709                 NewsGroupInfo *ginfo;
710
711                 p = strchr(p, ' ');
712                 if (!p) continue;
713                 *p = '\0';
714                 p++;
715                 name = buf;
716
717                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
718                         continue;
719
720                 ginfo = news_group_info_new(name, first_num, last_num, type);
721
722                 if (!last)
723                         last = list = g_slist_append(NULL, ginfo);
724                 else {
725                         last = g_slist_append(last, ginfo);
726                         last = last->next;
727                 }
728         }
729
730         fclose(fp);
731         g_free(filename);
732
733         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
734
735         return list;
736 }
737
738 void news_group_list_free(GSList *group_list)
739 {
740         GSList *cur;
741
742         if (!group_list) return;
743
744         for (cur = group_list; cur != NULL; cur = cur->next)
745                 news_group_info_free((NewsGroupInfo *)cur->data);
746         g_slist_free(group_list);
747 }
748
749 void news_remove_group_list_cache(Folder *folder)
750 {
751         gchar *path, *filename;
752
753         cm_return_if_fail(folder != NULL);
754         cm_return_if_fail(FOLDER_CLASS(folder) == &news_class);
755
756         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
757         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
758         g_free(path);
759
760         if (is_file_exist(filename)) {
761                 if (remove(filename) < 0)
762                         FILE_OP_ERROR(filename, "remove");
763         }
764         g_free(filename);
765 }
766
767 gint news_post(Folder *folder, const gchar *file)
768 {
769         gint ok;
770         char *contents = file_read_to_str_no_recode(file);
771         NewsSession *session;
772
773         cm_return_val_if_fail(folder != NULL, -1);
774         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
775         cm_return_val_if_fail(contents != NULL, -1);
776         
777         session = news_session_get(folder);
778         if (!session)  {
779                 g_free(contents);
780                 return -1;
781         }
782         
783         ok = nntp_threaded_post(folder, contents, strlen(contents));
784
785         g_free(contents);
786
787         if (ok == NEWSNNTP_ERROR_STREAM) {
788                 session_destroy(SESSION(session));
789                 REMOTE_FOLDER(folder)->session = NULL;
790         }
791
792         return (ok == NEWSNNTP_NO_ERROR ? 0 : -1);
793 }
794
795 static gint news_get_article(Folder *folder, gint num, gchar *filename)
796 {
797         size_t len;
798         char *result = NULL;
799         int r;
800         
801         r = nntp_threaded_article(folder, num, &result, &len);
802         
803         if (r == NEWSNNTP_NO_ERROR) {
804                 if (str_write_to_file(result, filename) < 0) {
805                         mmap_string_unref(result);
806                         return -1;
807                 }
808                 mmap_string_unref(result);
809         }
810         
811         return r;
812 }
813
814 /**
815  * news_select_group:
816  * @session: Active NNTP session.
817  * @group: Newsgroup name.
818  * @num: Estimated number of articles.
819  * @first: First article number.
820  * @last: Last article number.
821  *
822  * Select newsgroup @group with the GROUP command if it is not already
823  * selected in @session, or article numbers need to be returned.
824  *
825  * Return value: NNTP result code.
826  **/
827 static gint news_select_group(Folder *folder, const gchar *group,
828                               gint *num, gint *first, gint *last)
829 {
830         gint ok;
831         gint num_, first_, last_;
832         struct newsnntp_group_info *info = NULL;
833         NewsSession *session = NEWS_SESSION(news_session_get(folder));
834
835         cm_return_val_if_fail(session != NULL, -1);
836         
837         if (!num || !first || !last) {
838                 if (session->group && g_ascii_strcasecmp(session->group, group) == 0)
839                         return NEWSNNTP_NO_ERROR;
840                 num = &num_;
841                 first = &first_;
842                 last = &last_;
843         }
844
845         g_free(session->group);
846         session->group = NULL;
847
848         ok = nntp_threaded_group(folder, group, &info);
849         
850         if (ok == NEWSNNTP_NO_ERROR && info) {
851                 session->group = g_strdup(group);
852                 *num = info->grp_first;
853                 *first = info->grp_first;
854                 *last = info->grp_last;
855                 newsnntp_group_free(info);
856         } else {
857                 log_warning(LOG_PROTOCOL, _("couldn't select group: %s\n"), group);
858         }
859         return ok;
860 }
861
862 static MsgInfo *news_parse_xover(struct newsnntp_xover_resp_item *item)
863 {
864         MsgInfo *msginfo;
865
866         /* set MsgInfo */
867         msginfo = procmsg_msginfo_new();
868         msginfo->msgnum = item->ovr_article;
869         msginfo->size = item->ovr_size;
870
871         msginfo->date = g_strdup(item->ovr_date);
872         msginfo->date_t = procheader_date_parse(NULL, item->ovr_date, 0);
873
874         msginfo->from = conv_unmime_header(item->ovr_author, NULL, TRUE);
875         msginfo->fromname = procheader_get_fromname(msginfo->from);
876
877         msginfo->subject = conv_unmime_header(item->ovr_subject, NULL, TRUE);
878
879         remove_return(msginfo->from);
880         remove_return(msginfo->fromname);
881         remove_return(msginfo->subject);
882
883         if (item->ovr_message_id) {
884                 gchar *tmp = g_strdup(item->ovr_message_id);
885                 extract_parenthesis(tmp, '<', '>');
886                 remove_space(tmp);
887                 if (*tmp != '\0')
888                         msginfo->msgid = g_strdup(tmp);
889                 g_free(tmp);
890         }                        
891
892         /* FIXME: this is a quick fix; references' meaning was changed
893          * into having the actual list of references in the References: header.
894          * We need a GSList here, so msginfo_free() and msginfo_copy() can do 
895          * their things properly. */ 
896         if (item->ovr_references && *(item->ovr_references)) {   
897                 gchar **ref_tokens = g_strsplit(item->ovr_references, " ", -1);
898                 guint i = 0;
899                 char *tmp;
900                 char *p;
901                 while (ref_tokens[i]) {
902                         gchar *cur_ref = ref_tokens[i];
903                         msginfo->references = references_list_append(msginfo->references, 
904                                         cur_ref);
905                         i++;
906                 }
907                 g_strfreev(ref_tokens);
908                 
909                 tmp = g_strdup(item->ovr_references);
910                 eliminate_parenthesis(tmp, '(', ')');
911                 if ((p = strrchr(tmp, '<')) != NULL) {
912                         extract_parenthesis(p, '<', '>');
913                         remove_space(p);
914                         if (*p != '\0')
915                                 msginfo->inreplyto = g_strdup(p);
916                 }
917                 g_free(tmp);
918         } 
919
920         return msginfo;
921 }
922
923 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
924 {
925         gchar * tmp;
926         FILE * tmpfp;
927         gchar buf[BUFFSIZE];
928
929         tmp = g_strdup_printf("%s%ccancel%p", get_tmp_dir(),
930                               G_DIR_SEPARATOR, msginfo);
931         if (tmp == NULL)
932                 return -1;
933
934         if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
935                 FILE_OP_ERROR(tmp, "fopen");
936                 return -1;
937         }
938         if (change_file_mode_rw(tmpfp, tmp) < 0) {
939                 FILE_OP_ERROR(tmp, "chmod");
940                 g_warning("can't change file mode\n");
941         }
942         
943         get_rfc822_date(buf, sizeof(buf));
944         if (fprintf(tmpfp, "From: %s\r\n"
945                        "Newsgroups: %s\r\n"
946                        "Subject: cmsg cancel <%s>\r\n"
947                        "Control: cancel <%s>\r\n"
948                        "Approved: %s\r\n"
949                        "X-Cancelled-by: %s\r\n"
950                        "Date: %s\r\n"
951                        "\r\n"
952                        "removed with Claws Mail\r\n",
953                        msginfo->from,
954                        msginfo->newsgroups,
955                        msginfo->msgid,
956                        msginfo->msgid,
957                        msginfo->from,
958                        msginfo->from,
959                        buf) < 0) {
960                 FILE_OP_ERROR(tmp, "fprintf");
961                 fclose(tmpfp);
962                 claws_unlink(tmp);
963                 g_free(tmp);
964                 return -1;
965         }
966
967         if (fclose(tmpfp) == EOF) {
968                 FILE_OP_ERROR(tmp, "fclose");
969                 claws_unlink(tmp);
970                 g_free(tmp);
971                 return -1;
972         }
973
974         news_post(folder, tmp);
975         remove(tmp);
976
977         g_free(tmp);
978
979         return 0;
980 }
981
982 static gchar *news_folder_get_path(Folder *folder)
983 {
984         gchar *folder_path;
985
986         cm_return_val_if_fail(folder->account != NULL, NULL);
987
988         folder_path = g_strconcat(get_news_cache_dir(),
989                                   G_DIR_SEPARATOR_S,
990                                   folder->account->nntp_server,
991                                   NULL);
992         return folder_path;
993 }
994
995 static gchar *news_item_get_path(Folder *folder, FolderItem *item)
996 {
997         gchar *folder_path, *path;
998
999         cm_return_val_if_fail(folder != NULL, NULL);
1000         cm_return_val_if_fail(item != NULL, NULL);
1001         folder_path = news_folder_get_path(folder);
1002
1003         cm_return_val_if_fail(folder_path != NULL, NULL);
1004         if (g_path_is_absolute(folder_path)) {
1005                 if (item->path)
1006                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
1007                                            item->path, NULL);
1008                 else
1009                         path = g_strdup(folder_path);
1010         } else {
1011                 if (item->path)
1012                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1013                                            folder_path, G_DIR_SEPARATOR_S,
1014                                            item->path, NULL);
1015                 else
1016                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1017                                            folder_path, NULL);
1018         }
1019         g_free(folder_path);
1020 #ifdef G_OS_WIN32
1021         while (strchr(path, '/'))
1022                 *strchr(path, '/') = '\\';
1023 #endif
1024         return path;
1025 }
1026
1027 static gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
1028 {
1029         NewsSession *session;
1030         gint i, ok, num, first, last, nummsgs = 0;
1031         gchar *dir;
1032
1033         cm_return_val_if_fail(item != NULL, -1);
1034         cm_return_val_if_fail(item->folder != NULL, -1);
1035         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
1036
1037         session = news_session_get(folder);
1038         cm_return_val_if_fail(session != NULL, -1);
1039
1040         *old_uids_valid = TRUE;
1041         
1042         news_folder_lock(NEWS_FOLDER(item->folder));
1043
1044         ok = news_select_group(folder, item->path, &num, &first, &last);
1045         if (ok != NEWSNNTP_NO_ERROR) {
1046                 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
1047                 news_folder_unlock(NEWS_FOLDER(item->folder));
1048                 return -1;
1049         }
1050
1051         dir = news_folder_get_path(folder);
1052         if (num <= 0)
1053                 remove_all_numbered_files(dir);
1054         else if (last < first)
1055                 log_warning(LOG_PROTOCOL, _("invalid article range: %d - %d\n"),
1056                             first, last);
1057         else {
1058                 for (i = first; i <= last; i++) {
1059                         *msgnum_list = g_slist_prepend(*msgnum_list, 
1060                                                        GINT_TO_POINTER(i));
1061                         nummsgs++;
1062                 }
1063                 debug_print("removing old messages from %d to %d in %s\n",
1064                             first, last, dir);
1065                 remove_numbered_files(dir, 1, first - 1);
1066         }
1067         g_free(dir);
1068         news_folder_unlock(NEWS_FOLDER(item->folder));
1069         return nummsgs;
1070 }
1071
1072 static void news_set_msg_flags(FolderItem *item, MsgInfo *msginfo)
1073 {
1074         msginfo->flags.tmp_flags = 0;
1075         if (item->folder->account->mark_crosspost_read && msginfo->msgid) {
1076                 if (item->folder->newsart &&
1077                     g_hash_table_lookup(item->folder->newsart, msginfo->msgid) != NULL) {
1078                         msginfo->flags.perm_flags = MSG_COLORLABEL_TO_FLAGS(item->folder->account->crosspost_col);
1079                                 
1080                 } else {
1081                         if (!item->folder->newsart) 
1082                                 item->folder->newsart = g_hash_table_new(g_str_hash, g_str_equal);
1083                         g_hash_table_insert(item->folder->newsart,
1084                                         g_strdup(msginfo->msgid), GINT_TO_POINTER(1));
1085                         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1086                 }
1087         } else {
1088                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1089         }
1090 }
1091
1092 static void news_get_extra_fields(NewsSession *session, FolderItem *item, GSList *msglist)
1093 {
1094         MsgInfo *msginfo = NULL;
1095         gint ok;
1096         GSList *cur;
1097         clist *hdrlist = NULL;
1098         clistiter *hdr;
1099         gint first = -1, last = -1;
1100         GHashTable *hash_table;
1101         
1102         cm_return_if_fail(session != NULL);
1103         cm_return_if_fail(item != NULL);
1104         cm_return_if_fail(item->folder != NULL);
1105         cm_return_if_fail(FOLDER_CLASS(item->folder) == &news_class);
1106
1107         news_folder_lock(NEWS_FOLDER(item->folder));
1108
1109         hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1110         
1111         for (cur = msglist; cur; cur = cur->next) {
1112                 msginfo = (MsgInfo *)cur->data;
1113                 if (first == -1 || msginfo->msgnum < first)
1114                         first = msginfo->msgnum;
1115                 if (last == -1 || msginfo->msgnum > last)
1116                         last = msginfo->msgnum;
1117                 g_hash_table_insert(hash_table,
1118                                 GINT_TO_POINTER(msginfo->msgnum), msginfo);
1119         }
1120
1121 /* Newsgroups */
1122         ok = nntp_threaded_xhdr(item->folder, "newsgroups", first, last, &hdrlist);
1123
1124         if (ok != NEWSNNTP_NO_ERROR) {
1125                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1126                 if (ok == NEWSNNTP_ERROR_STREAM) {
1127                         session_destroy(SESSION(session));
1128                         REMOTE_FOLDER(item->folder)->session = NULL;
1129                 }
1130                 news_folder_unlock(NEWS_FOLDER(item->folder));
1131                 return;
1132         }
1133
1134         for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1135                 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1136                 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1137                 if (msginfo) {
1138                         if (msginfo->newsgroups)
1139                                 g_free(msginfo->newsgroups);
1140                         msginfo->newsgroups = g_strdup(hdrval->hdr_value);
1141                 }
1142         }
1143         newsnntp_xhdr_free(hdrlist);
1144         
1145 /* To */
1146         ok = nntp_threaded_xhdr(item->folder, "to", first, last, &hdrlist);
1147
1148         if (ok != NEWSNNTP_NO_ERROR) {
1149                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1150                 if (ok == NEWSNNTP_ERROR_STREAM) {
1151                         session_destroy(SESSION(session));
1152                         REMOTE_FOLDER(item->folder)->session = NULL;
1153                 }
1154                 news_folder_unlock(NEWS_FOLDER(item->folder));
1155                 return;
1156         }
1157
1158         for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1159                 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1160                 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1161                 if (msginfo) {
1162                         if (msginfo->to)
1163                                 g_free(msginfo->to);
1164                         msginfo->to = g_strdup(hdrval->hdr_value);
1165                 }
1166         }
1167         newsnntp_xhdr_free(hdrlist);
1168         
1169 /* Cc */
1170         ok = nntp_threaded_xhdr(item->folder, "cc", first, last, &hdrlist);
1171
1172         if (ok != NEWSNNTP_NO_ERROR) {
1173                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1174                 if (ok == NEWSNNTP_ERROR_STREAM) {
1175                         session_destroy(SESSION(session));
1176                         REMOTE_FOLDER(item->folder)->session = NULL;
1177                 }
1178                 news_folder_unlock(NEWS_FOLDER(item->folder));
1179                 return;
1180         }
1181
1182         for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1183                 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1184                 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1185                 if (msginfo) {
1186                         if (msginfo->cc)
1187                                 g_free(msginfo->cc);
1188                         msginfo->cc = g_strdup(hdrval->hdr_value);
1189                 }
1190         }
1191         newsnntp_xhdr_free(hdrlist);
1192         
1193
1194         g_hash_table_destroy(hash_table);
1195         news_folder_unlock(NEWS_FOLDER(item->folder));
1196 }
1197
1198 static GSList *news_get_msginfos_for_range(NewsSession *session, FolderItem *item, guint begin, guint end)
1199 {
1200         GSList *newlist = NULL;
1201         GSList *llast = NULL;
1202         MsgInfo *msginfo;
1203         gint ok;
1204         clist *msglist = NULL;
1205         clistiter *cur;
1206         cm_return_val_if_fail(session != NULL, NULL);
1207         cm_return_val_if_fail(item != NULL, NULL);
1208
1209         log_message(LOG_PROTOCOL, _("getting xover %d - %d in %s...\n"),
1210                     begin, end, item->path);
1211
1212         news_folder_lock(NEWS_FOLDER(item->folder));
1213         
1214         ok = news_select_group(item->folder, item->path, NULL, NULL, NULL);
1215         if (ok != NEWSNNTP_NO_ERROR) {
1216                 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
1217                 news_folder_unlock(NEWS_FOLDER(item->folder));
1218                 return NULL;
1219         }
1220
1221         ok = nntp_threaded_xover(item->folder, begin, end, NULL, &msglist);
1222         
1223         if (ok != NEWSNNTP_NO_ERROR) {
1224                 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1225                 if (ok == NEWSNNTP_ERROR_STREAM) {
1226                         session_destroy(SESSION(session));
1227                         REMOTE_FOLDER(item->folder)->session = NULL;
1228                 }
1229                 news_folder_unlock(NEWS_FOLDER(item->folder));
1230                 return NULL;
1231         }
1232
1233         if (msglist) {
1234                 for (cur = clist_begin(msglist); cur; cur = clist_next(cur)) {
1235                         struct newsnntp_xover_resp_item *ritem = (struct newsnntp_xover_resp_item *)clist_content(cur);
1236                         msginfo = news_parse_xover(ritem);
1237                         
1238                         if (!msginfo) {
1239                                 log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1240                                 continue;
1241                         }
1242
1243                         msginfo->folder = item;
1244                         news_set_msg_flags(item, msginfo);
1245                         msginfo->flags.tmp_flags |= MSG_NEWS;
1246
1247                         if (!newlist)
1248                                 llast = newlist = g_slist_append(newlist, msginfo);
1249                         else {
1250                                 llast = g_slist_append(llast, msginfo);
1251                                 llast = llast->next;
1252                         }
1253                 }
1254                 newsnntp_xover_resp_list_free(msglist);
1255         }
1256
1257         news_folder_unlock(NEWS_FOLDER(item->folder));
1258
1259         session_set_access_time(SESSION(session));
1260
1261         news_get_extra_fields(session, item, newlist);
1262         
1263         return newlist;
1264 }
1265
1266 static MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
1267 {
1268         GSList *msglist = NULL;
1269         NewsSession *session;
1270         MsgInfo *msginfo = NULL;
1271
1272         session = news_session_get(folder);
1273         cm_return_val_if_fail(session != NULL, NULL);
1274         cm_return_val_if_fail(item != NULL, NULL);
1275         cm_return_val_if_fail(item->folder != NULL, NULL);
1276         cm_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
1277
1278         msglist = news_get_msginfos_for_range(session, item, num, num);
1279  
1280         if (msglist)
1281                 msginfo = msglist->data;
1282         
1283         g_slist_free(msglist);
1284         
1285         return msginfo;
1286 }
1287
1288 static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1289 {
1290         NewsSession *session;
1291         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1292         guint first, last, next;
1293 /*      guint tofetch, fetched;
1294 */
1295         
1296         cm_return_val_if_fail(folder != NULL, NULL);
1297         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1298         cm_return_val_if_fail(msgnum_list != NULL, NULL);
1299         cm_return_val_if_fail(item != NULL, NULL);
1300         
1301         session = news_session_get(folder);
1302         cm_return_val_if_fail(session != NULL, NULL);
1303
1304         tmp_msgnum_list = g_slist_copy(msgnum_list);
1305         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1306
1307         progressindicator_start(PROGRESS_TYPE_NETWORK);
1308 /*      tofetch = g_slist_length(tmp_msgnum_list);
1309         fetched = 0;
1310 */
1311
1312         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1313         last = first;
1314         
1315         news_folder_lock(NEWS_FOLDER(item->folder));
1316         
1317         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1318                 next = GPOINTER_TO_INT(elem->data);
1319                 if(next != (last + 1)) {
1320 /*                      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1321                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1322 */
1323                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1324                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1325 /*                      fetched = last - first + 1;
1326 */
1327                         first = next;
1328                 }
1329                 last = next;
1330         }
1331         
1332         news_folder_unlock(NEWS_FOLDER(item->folder));
1333         
1334 /*      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1335         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1336 */
1337         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1338         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1339
1340         g_slist_free(tmp_msgnum_list);
1341         
1342         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1343
1344         return msginfo_list;
1345 }
1346
1347 static gboolean news_scan_required(Folder *folder, FolderItem *item)
1348 {
1349         return TRUE;
1350 }
1351
1352 void news_synchronise(FolderItem *item, gint days) 
1353 {
1354         news_gtk_synchronise(item, days);
1355 }
1356
1357 static gint news_rename_folder(Folder *folder, FolderItem *item,
1358                                 const gchar *name)
1359 {
1360         gchar *path;
1361          
1362         cm_return_val_if_fail(folder != NULL, -1);
1363         cm_return_val_if_fail(item != NULL, -1);
1364         cm_return_val_if_fail(item->path != NULL, -1);
1365         cm_return_val_if_fail(name != NULL, -1);
1366
1367         path = folder_item_get_path(item);
1368         if (!is_dir_exist(path))
1369                 make_dir_hier(path);
1370
1371         g_free(item->name);
1372         item->name = g_strdup(name);
1373
1374         return 0;
1375 }
1376
1377 static gint news_remove_folder(Folder *folder, FolderItem *item)
1378 {
1379         gchar *path;
1380
1381         cm_return_val_if_fail(folder != NULL, -1);
1382         cm_return_val_if_fail(item != NULL, -1);
1383         cm_return_val_if_fail(item->path != NULL, -1);
1384
1385         path = folder_item_get_path(item);
1386         if (remove_dir_recursive(path) < 0) {
1387                 g_warning("can't remove directory `%s'\n", path);
1388                 g_free(path);
1389                 return -1;
1390         }
1391
1392         g_free(path);
1393         folder_item_remove(item);
1394         return 0;
1395 }
1396
1397 void nntp_disconnect_all(gboolean have_connectivity)
1398 {
1399         GList *list;
1400         gboolean short_timeout;
1401 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1402         GError *error;
1403 #endif
1404
1405 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1406         error = NULL;
1407         short_timeout = !networkmanager_is_online(&error);
1408         if(error) {
1409                 short_timeout = TRUE;
1410                 g_error_free(error);
1411         }
1412 #else
1413         short_timeout = TRUE;
1414 #endif
1415
1416         if(short_timeout)
1417                 nntp_main_set_timeout(1);
1418
1419         for (list = account_get_list(); list != NULL; list = list->next) {
1420                 PrefsAccount *account = list->data;
1421                 if (account->protocol == A_NNTP) {
1422                         RemoteFolder *folder = (RemoteFolder *)account->folder;
1423                         if (folder && folder->session) {
1424                                 NewsSession *session = (NewsSession *)folder->session;
1425                                 if (have_connectivity)
1426                                         nntp_threaded_disconnect(FOLDER(folder));
1427                                 SESSION(session)->state = SESSION_DISCONNECTED;
1428                                 SESSION(session)->sock = NULL;
1429                                 session_destroy(SESSION(session));
1430                                 folder->session = NULL;
1431                         }
1432                 }
1433         }
1434
1435         if(short_timeout)
1436                 nntp_main_set_timeout(prefs_common.io_timeout_secs);
1437 }
1438
1439 #else
1440 #include <glib.h>
1441 #include <glib/gi18n.h>
1442 #include <gtk/gtk.h>
1443 #include "folder.h"
1444 #include "alertpanel.h"
1445
1446 static FolderClass news_class;
1447
1448 static void warn_etpan(void)
1449 {
1450         static gboolean missing_news_warning = TRUE;
1451         if (missing_news_warning) {
1452                 missing_news_warning = FALSE;
1453                 alertpanel_error(
1454                         _("You have one or more News accounts "
1455                           "defined. However this version of "
1456                           "Claws Mail has been built without "
1457                           "News support; your News account(s) are "
1458                           "disabled.\n\n"
1459                           "You probably need to "
1460                           "install libetpan and recompile "
1461                           "Claws Mail."));
1462         }
1463 }
1464 static Folder *news_folder_new(const gchar *name, const gchar *path)
1465 {
1466         warn_etpan();
1467         return NULL;
1468 }
1469 void news_group_list_free(GSList *group_list)
1470 {
1471         warn_etpan();
1472 }
1473 void news_remove_group_list_cache(Folder *folder)
1474 {
1475         warn_etpan();
1476 }
1477 int news_folder_locked(Folder *folder)
1478 {
1479         warn_etpan();
1480         return 0;
1481 }
1482 gint news_post(Folder *folder, const gchar *file)
1483 {
1484         warn_etpan();
1485         return -1;
1486 }
1487
1488 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
1489 {
1490         warn_etpan();
1491         return -1;
1492 }
1493
1494 GSList *news_get_group_list(Folder *folder)
1495 {
1496         warn_etpan();
1497         return NULL;
1498 }
1499
1500
1501 FolderClass *news_get_class(void)
1502 {
1503         if (news_class.idstr == NULL) {
1504                 news_class.type = F_NEWS;
1505                 news_class.idstr = "news";
1506                 news_class.uistr = "News";
1507
1508                 /* Folder functions */
1509                 news_class.new_folder = news_folder_new;
1510         };
1511
1512         return &news_class;
1513 }
1514
1515 void nntp_disconnect_all(gboolean have_connectivity)
1516 {
1517 }
1518
1519 #endif