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