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