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