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