* Update year
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifdef HAVE_LIBETPAN
26
27 #include "defs.h"
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <dirent.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <libetpan/libetpan.h>
38
39 #include "nntp-thread.h"
40 #include "news.h"
41 #include "news_gtk.h"
42 #include "socket.h"
43 #include "recv.h"
44 #include "procmsg.h"
45 #include "procheader.h"
46 #include "folder.h"
47 #include "session.h"
48 #include "statusbar.h"
49 #include "codeconv.h"
50 #include "utils.h"
51 #include "prefs_common.h"
52 #include "prefs_account.h"
53 #include "inputdialog.h"
54 #include "log.h"
55 #include "progressindicator.h"
56 #include "remotefolder.h"
57 #include "alertpanel.h"
58 #include "inc.h"
59 #include "account.h"
60 #ifdef USE_GNUTLS
61 #  include "ssl.h"
62 #endif
63 #include "main.h"
64
65 #define NNTP_PORT       119
66 #ifdef USE_GNUTLS
67 #define NNTPS_PORT      563
68 #endif
69
70 typedef struct _NewsFolder      NewsFolder;
71 typedef struct _NewsSession     NewsSession;
72
73 #define NEWS_FOLDER(obj)        ((NewsFolder *)obj)
74 #define NEWS_SESSION(obj)       ((NewsSession *)obj)
75
76 struct _NewsFolder
77 {
78         RemoteFolder rfolder;
79
80         gboolean use_auth;
81         gboolean lock_count;
82         guint refcnt;
83 };
84
85 struct _NewsSession
86 {
87         Session session;
88         Folder *folder;
89         gchar *group;
90 };
91
92 static void news_folder_init(Folder *folder, const gchar *name,
93                              const gchar *path);
94
95 static Folder   *news_folder_new        (const gchar    *name,
96                                          const gchar    *folder);
97 static void      news_folder_destroy    (Folder         *folder);
98
99 static gchar *news_fetch_msg            (Folder         *folder,
100                                          FolderItem     *item,
101                                          gint            num);
102 static void news_remove_cached_msg      (Folder         *folder, 
103                                          FolderItem     *item, 
104                                          MsgInfo        *msginfo);
105 #ifdef USE_GNUTLS
106 static Session *news_session_new         (Folder        *folder,
107                                           const gchar   *server,
108                                           gushort        port,
109                                           const gchar   *userid,
110                                           const gchar   *passwd,
111                                           SSLType        ssl_type);
112 #else
113 static Session *news_session_new         (Folder        *folder,
114                                           const gchar   *server,
115                                           gushort        port,
116                                           const gchar   *userid,
117                                           const gchar   *passwd);
118 #endif
119
120 static gint news_get_article             (Folder        *folder,
121                                           gint           num,
122                                           gchar         *filename);
123
124 static gint news_select_group            (Folder        *folder,
125                                           const gchar   *group,
126                                           gint          *num,
127                                           gint          *first,
128                                           gint          *last);
129 static MsgInfo *news_parse_xover         (struct newsnntp_xover_resp_item *item);
130 static gint news_get_num_list                    (Folder        *folder, 
131                                           FolderItem    *item,
132                                           GSList       **list,
133                                           gboolean      *old_uids_valid);
134 static MsgInfo *news_get_msginfo                 (Folder        *folder, 
135                                           FolderItem    *item,
136                                           gint           num);
137 static GSList *news_get_msginfos                 (Folder        *folder,
138                                           FolderItem    *item,
139                                           GSList        *msgnum_list);
140 static gboolean news_scan_required               (Folder        *folder,
141                                           FolderItem    *item);
142
143 static gchar *news_folder_get_path       (Folder        *folder);
144 static gchar *news_item_get_path                 (Folder        *folder,
145                                           FolderItem    *item);
146 static void news_synchronise             (FolderItem    *item, gint days);
147 static int news_remove_msg               (Folder        *folder, 
148                                           FolderItem    *item, 
149                                           gint           msgnum);
150 static gint news_rename_folder           (Folder *folder,
151                                           FolderItem *item,
152                                           const gchar *name);
153 static gint news_remove_folder           (Folder        *folder,
154                                           FolderItem    *item);
155 static FolderClass news_class;
156
157 FolderClass *news_get_class(void)
158 {
159         if (news_class.idstr == NULL) {
160                 news_class.type = F_NEWS;
161                 news_class.idstr = "news";
162                 news_class.uistr = "News";
163                 news_class.supports_server_search = FALSE;
164
165                 /* Folder functions */
166                 news_class.new_folder = news_folder_new;
167                 news_class.destroy_folder = news_folder_destroy;
168
169                 /* FolderItem functions */
170                 news_class.item_get_path = news_item_get_path;
171                 news_class.get_num_list = news_get_num_list;
172                 news_class.scan_required = news_scan_required;
173                 news_class.rename_folder = news_rename_folder;
174                 news_class.remove_folder = news_remove_folder;
175
176                 /* Message functions */
177                 news_class.get_msginfo = news_get_msginfo;
178                 news_class.get_msginfos = news_get_msginfos;
179                 news_class.fetch_msg = news_fetch_msg;
180                 news_class.synchronise = news_synchronise;
181                 news_class.search_msgs = folder_item_search_msgs_local;
182                 news_class.remove_msg = news_remove_msg;
183                 news_class.remove_cached_msg = news_remove_cached_msg;
184         };
185
186         return &news_class;
187 }
188
189 guint nntp_folder_get_refcnt(Folder *folder)
190 {
191         return ((NewsFolder *)folder)->refcnt;
192 }
193
194 void nntp_folder_ref(Folder *folder)
195 {
196         ((NewsFolder *)folder)->refcnt++;
197 }
198
199 void nntp_folder_unref(Folder *folder)
200 {
201         if (((NewsFolder *)folder)->refcnt > 0)
202                 ((NewsFolder *)folder)->refcnt--;
203 }
204
205 static int news_remove_msg               (Folder        *folder, 
206                                           FolderItem    *item, 
207                                           gint           msgnum)
208 {
209         gchar *path, *filename;
210
211         cm_return_val_if_fail(folder != NULL, -1);
212         cm_return_val_if_fail(item != NULL, -1);
213
214         path = folder_item_get_path(item);
215         if (!is_dir_exist(path))
216                 make_dir_hier(path);
217         
218         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
219         g_free(path);
220         claws_unlink(filename);
221         g_free(filename);
222         return 0;
223 }
224
225 static void news_folder_lock(NewsFolder *folder)
226 {
227         folder->lock_count++;
228 }
229
230 static void news_folder_unlock(NewsFolder *folder)
231 {
232         if (folder->lock_count > 0)
233                 folder->lock_count--;
234 }
235
236 int news_folder_locked(Folder *folder)
237 {
238         if (folder == NULL)
239                 return 0;
240
241         return NEWS_FOLDER(folder)->lock_count;
242 }
243
244 static Folder *news_folder_new(const gchar *name, const gchar *path)
245 {
246         Folder *folder;
247
248         folder = (Folder *)g_new0(NewsFolder, 1);
249         folder->klass = &news_class;
250         news_folder_init(folder, name, path);
251
252         return folder;
253 }
254
255 static void news_folder_destroy(Folder *folder)
256 {
257         gchar *dir;
258
259         while (nntp_folder_get_refcnt(folder) > 0)
260                 gtk_main_iteration();
261
262         dir = news_folder_get_path(folder);
263         if (is_dir_exist(dir))
264                 remove_dir_recursive(dir);
265         g_free(dir);
266
267         nntp_done(folder);
268         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
269 }
270
271 static void news_folder_init(Folder *folder, const gchar *name,
272                              const gchar *path)
273 {
274         folder_remote_folder_init(folder, name, path);
275 }
276
277 static void news_session_destroy(Session *session)
278 {
279         NewsSession *news_session = NEWS_SESSION(session);
280
281         cm_return_if_fail(session != NULL);
282
283         if (news_session->group)
284                 g_free(news_session->group);
285 }
286
287 static gboolean nntp_ping(gpointer data)
288 {
289         Session *session = (Session *)data;
290         NewsSession *news_session = NEWS_SESSION(session);
291         int r;
292         struct tm lt;
293
294         if (session->state != SESSION_READY || news_folder_locked(news_session->folder))
295                 return FALSE;
296         
297         news_folder_lock(NEWS_FOLDER(news_session->folder));
298
299         if ((r = nntp_threaded_date(news_session->folder, &lt)) != NEWSNNTP_NO_ERROR) {
300                 if (r != NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED &&
301                     r != NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD) {
302                         log_warning(LOG_PROTOCOL, _("NNTP connection to %s:%d has been"
303                               " disconnected.\n"),
304                             news_session->folder->account->nntp_server,
305                             news_session->folder->account->set_nntpport ?
306                             news_session->folder->account->nntpport : NNTP_PORT);
307                         REMOTE_FOLDER(news_session->folder)->session = NULL;
308                         news_folder_unlock(NEWS_FOLDER(news_session->folder));
309                         session->state = SESSION_DISCONNECTED;
310                         session->sock = NULL;
311                         session_destroy(session);
312                         return FALSE;
313                 }
314         }
315
316         news_folder_unlock(NEWS_FOLDER(news_session->folder));
317         session_set_access_time(session);
318         return TRUE;
319 }
320
321
322 #ifdef USE_GNUTLS
323 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
324                                  const gchar *userid, const gchar *passwd,
325                                  SSLType ssl_type)
326 #else
327 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
328                                  const gchar *userid, const gchar *passwd)
329 #endif
330 {
331         NewsSession *session;
332         int r = 0;
333         cm_return_val_if_fail(server != NULL, NULL);
334
335         log_message(LOG_PROTOCOL, _("Account '%s': Connecting to NNTP server: %s:%d...\n"),
336                                     folder->account->account_name, server, port);
337
338         session = g_new0(NewsSession, 1);
339         session_init(SESSION(session), folder->account, FALSE);
340         SESSION(session)->type             = SESSION_NEWS;
341         SESSION(session)->server           = g_strdup(server);
342         SESSION(session)->port             = port;
343         SESSION(session)->sock             = NULL;
344         SESSION(session)->destroy          = news_session_destroy;
345         
346         nntp_init(folder);
347
348 #ifdef USE_GNUTLS
349         if (ssl_type != SSL_NONE)
350                 r = nntp_threaded_connect_ssl(folder, server, port);
351         else
352 #endif
353                 r = nntp_threaded_connect(folder, server, port);
354         
355         if (r != NEWSNNTP_NO_ERROR) {
356                 log_error(LOG_PROTOCOL, _("Error logging in to %s:%d...\n"), server, port);
357                 if (session != NULL)
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, but SSL is not available in this "
391                           "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 (ac->passwd && ac->passwd[0])
409                         passwd = g_strdup(ac->passwd);
410                 else
411                         passwd = input_dialog_query_password_keep(ac->nntp_server,
412                                                                   userid,
413                                                                   &(ac->session_passwd));
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\n", 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 (remove(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 buf[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\n");
946         }
947         
948         get_rfc822_date(buf, sizeof(buf));
949         if (fprintf(tmpfp, "From: %s\r\n"
950                        "Newsgroups: %s\r\n"
951                        "Subject: cmsg cancel <%s>\r\n"
952                        "Control: cancel <%s>\r\n"
953                        "Approved: %s\r\n"
954                        "X-Cancelled-by: %s\r\n"
955                        "Date: %s\r\n"
956                        "\r\n"
957                        "removed with Claws Mail\r\n",
958                        msginfo->from,
959                        msginfo->newsgroups,
960                        msginfo->msgid,
961                        msginfo->msgid,
962                        msginfo->from,
963                        msginfo->from,
964                        buf) < 0) {
965                 FILE_OP_ERROR(tmp, "fprintf");
966                 fclose(tmpfp);
967                 claws_unlink(tmp);
968                 g_free(tmp);
969                 return -1;
970         }
971
972         if (fclose(tmpfp) == EOF) {
973                 FILE_OP_ERROR(tmp, "fclose");
974                 claws_unlink(tmp);
975                 g_free(tmp);
976                 return -1;
977         }
978
979         news_post(folder, tmp);
980         remove(tmp);
981
982         g_free(tmp);
983
984         return 0;
985 }
986
987 static gchar *news_folder_get_path(Folder *folder)
988 {
989         gchar *folder_path;
990
991         cm_return_val_if_fail(folder->account != NULL, NULL);
992
993         folder_path = g_strconcat(get_news_cache_dir(),
994                                   G_DIR_SEPARATOR_S,
995                                   folder->account->nntp_server,
996                                   NULL);
997         return folder_path;
998 }
999
1000 static gchar *news_item_get_path(Folder *folder, FolderItem *item)
1001 {
1002         gchar *folder_path, *path;
1003
1004         cm_return_val_if_fail(folder != NULL, NULL);
1005         cm_return_val_if_fail(item != NULL, NULL);
1006         folder_path = news_folder_get_path(folder);
1007
1008         cm_return_val_if_fail(folder_path != NULL, NULL);
1009         if (g_path_is_absolute(folder_path)) {
1010                 if (item->path)
1011                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
1012                                            item->path, NULL);
1013                 else
1014                         path = g_strdup(folder_path);
1015         } else {
1016                 if (item->path)
1017                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1018                                            folder_path, G_DIR_SEPARATOR_S,
1019                                            item->path, NULL);
1020                 else
1021                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1022                                            folder_path, NULL);
1023         }
1024         g_free(folder_path);
1025 #ifdef G_OS_WIN32
1026         while (strchr(path, '/'))
1027                 *strchr(path, '/') = '\\';
1028 #endif
1029         return path;
1030 }
1031
1032 static gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
1033 {
1034         NewsSession *session;
1035         gint i, ok, num, first, last, nummsgs = 0;
1036         gchar *dir;
1037
1038         cm_return_val_if_fail(item != NULL, -1);
1039         cm_return_val_if_fail(item->folder != NULL, -1);
1040         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
1041
1042         session = news_session_get(folder);
1043         cm_return_val_if_fail(session != NULL, -1);
1044
1045         *old_uids_valid = TRUE;
1046         
1047         news_folder_lock(NEWS_FOLDER(item->folder));
1048
1049         ok = news_select_group(folder, item->path, &num, &first, &last);
1050         if (ok != NEWSNNTP_NO_ERROR) {
1051                 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
1052                 news_folder_unlock(NEWS_FOLDER(item->folder));
1053                 return -1;
1054         }
1055
1056         dir = news_folder_get_path(folder);
1057         if (num <= 0)
1058                 remove_all_numbered_files(dir);
1059         else if (last < first)
1060                 log_warning(LOG_PROTOCOL, _("invalid article range: %d - %d\n"),
1061                             first, last);
1062         else {
1063                 for (i = first; i <= last; i++) {
1064                         *msgnum_list = g_slist_prepend(*msgnum_list, 
1065                                                        GINT_TO_POINTER(i));
1066                         nummsgs++;
1067                 }
1068                 debug_print("removing old messages from %d to %d in %s\n",
1069                             first, last, dir);
1070                 remove_numbered_files(dir, 1, first - 1);
1071         }
1072         g_free(dir);
1073         news_folder_unlock(NEWS_FOLDER(item->folder));
1074         return nummsgs;
1075 }
1076
1077 static void news_set_msg_flags(FolderItem *item, MsgInfo *msginfo)
1078 {
1079         msginfo->flags.tmp_flags = 0;
1080         if (item->folder->account->mark_crosspost_read && msginfo->msgid) {
1081                 if (item->folder->newsart &&
1082                     g_hash_table_lookup(item->folder->newsart, msginfo->msgid) != NULL) {
1083                         msginfo->flags.perm_flags = MSG_COLORLABEL_TO_FLAGS(item->folder->account->crosspost_col);
1084                                 
1085                 } else {
1086                         if (!item->folder->newsart) 
1087                                 item->folder->newsart = g_hash_table_new(g_str_hash, g_str_equal);
1088                         g_hash_table_insert(item->folder->newsart,
1089                                         g_strdup(msginfo->msgid), GINT_TO_POINTER(1));
1090                         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1091                 }
1092         } else {
1093                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1094         }
1095 }
1096
1097 static void news_get_extra_fields(NewsSession *session, FolderItem *item, GSList *msglist)
1098 {
1099         MsgInfo *msginfo = NULL;
1100         gint ok;
1101         GSList *cur;
1102         clist *hdrlist = NULL;
1103         clistiter *hdr;
1104         gint first = -1, last = -1;
1105         GHashTable *hash_table;
1106         
1107         cm_return_if_fail(session != NULL);
1108         cm_return_if_fail(item != NULL);
1109         cm_return_if_fail(item->folder != NULL);
1110         cm_return_if_fail(FOLDER_CLASS(item->folder) == &news_class);
1111
1112         news_folder_lock(NEWS_FOLDER(item->folder));
1113
1114         hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1115         
1116         for (cur = msglist; cur; cur = cur->next) {
1117                 msginfo = (MsgInfo *)cur->data;
1118                 if (first == -1 || msginfo->msgnum < first)
1119                         first = msginfo->msgnum;
1120                 if (last == -1 || msginfo->msgnum > last)
1121                         last = msginfo->msgnum;
1122                 g_hash_table_insert(hash_table,
1123                                 GINT_TO_POINTER(msginfo->msgnum), msginfo);
1124         }
1125
1126 /* Newsgroups */
1127         ok = nntp_threaded_xhdr(item->folder, "newsgroups", first, last, &hdrlist);
1128
1129         if (ok != NEWSNNTP_NO_ERROR) {
1130                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1131                 if (ok == NEWSNNTP_ERROR_STREAM) {
1132                         session_destroy(SESSION(session));
1133                         REMOTE_FOLDER(item->folder)->session = NULL;
1134                 }
1135                 news_folder_unlock(NEWS_FOLDER(item->folder));
1136                 return;
1137         }
1138
1139         for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1140                 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1141                 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1142                 if (msginfo) {
1143                         if (msginfo->newsgroups)
1144                                 g_free(msginfo->newsgroups);
1145                         msginfo->newsgroups = g_strdup(hdrval->hdr_value);
1146                 }
1147         }
1148         newsnntp_xhdr_free(hdrlist);
1149         
1150 /* To */
1151         ok = nntp_threaded_xhdr(item->folder, "to", first, last, &hdrlist);
1152
1153         if (ok != NEWSNNTP_NO_ERROR) {
1154                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1155                 if (ok == NEWSNNTP_ERROR_STREAM) {
1156                         session_destroy(SESSION(session));
1157                         REMOTE_FOLDER(item->folder)->session = NULL;
1158                 }
1159                 news_folder_unlock(NEWS_FOLDER(item->folder));
1160                 return;
1161         }
1162
1163         for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1164                 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1165                 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1166                 if (msginfo) {
1167                         if (msginfo->to)
1168                                 g_free(msginfo->to);
1169                         msginfo->to = g_strdup(hdrval->hdr_value);
1170                 }
1171         }
1172         newsnntp_xhdr_free(hdrlist);
1173         
1174 /* Cc */
1175         ok = nntp_threaded_xhdr(item->folder, "cc", first, last, &hdrlist);
1176
1177         if (ok != NEWSNNTP_NO_ERROR) {
1178                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1179                 if (ok == NEWSNNTP_ERROR_STREAM) {
1180                         session_destroy(SESSION(session));
1181                         REMOTE_FOLDER(item->folder)->session = NULL;
1182                 }
1183                 news_folder_unlock(NEWS_FOLDER(item->folder));
1184                 return;
1185         }
1186
1187         for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1188                 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1189                 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1190                 if (msginfo) {
1191                         if (msginfo->cc)
1192                                 g_free(msginfo->cc);
1193                         msginfo->cc = g_strdup(hdrval->hdr_value);
1194                 }
1195         }
1196         newsnntp_xhdr_free(hdrlist);
1197         
1198
1199         g_hash_table_destroy(hash_table);
1200         news_folder_unlock(NEWS_FOLDER(item->folder));
1201 }
1202
1203 static GSList *news_get_msginfos_for_range(NewsSession *session, FolderItem *item, guint begin, guint end)
1204 {
1205         GSList *newlist = NULL;
1206         GSList *llast = NULL;
1207         MsgInfo *msginfo;
1208         gint ok;
1209         clist *msglist = NULL;
1210         clistiter *cur;
1211         cm_return_val_if_fail(session != NULL, NULL);
1212         cm_return_val_if_fail(item != NULL, NULL);
1213
1214         log_message(LOG_PROTOCOL, _("getting xover %d - %d in %s...\n"),
1215                     begin, end, item->path);
1216
1217         news_folder_lock(NEWS_FOLDER(item->folder));
1218         
1219         ok = news_select_group(item->folder, item->path, NULL, NULL, NULL);
1220         if (ok != NEWSNNTP_NO_ERROR) {
1221                 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
1222                 news_folder_unlock(NEWS_FOLDER(item->folder));
1223                 return NULL;
1224         }
1225
1226         ok = nntp_threaded_xover(item->folder, begin, end, NULL, &msglist);
1227         
1228         if (ok != NEWSNNTP_NO_ERROR) {
1229                 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1230                 if (ok == NEWSNNTP_ERROR_STREAM) {
1231                         session_destroy(SESSION(session));
1232                         REMOTE_FOLDER(item->folder)->session = NULL;
1233                 }
1234                 news_folder_unlock(NEWS_FOLDER(item->folder));
1235                 return NULL;
1236         }
1237
1238         if (msglist) {
1239                 for (cur = clist_begin(msglist); cur; cur = clist_next(cur)) {
1240                         struct newsnntp_xover_resp_item *ritem = (struct newsnntp_xover_resp_item *)clist_content(cur);
1241                         msginfo = news_parse_xover(ritem);
1242                         
1243                         if (!msginfo) {
1244                                 log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1245                                 continue;
1246                         }
1247
1248                         msginfo->folder = item;
1249                         news_set_msg_flags(item, msginfo);
1250                         msginfo->flags.tmp_flags |= MSG_NEWS;
1251
1252                         if (!newlist)
1253                                 llast = newlist = g_slist_append(newlist, msginfo);
1254                         else {
1255                                 llast = g_slist_append(llast, msginfo);
1256                                 llast = llast->next;
1257                         }
1258                 }
1259                 newsnntp_xover_resp_list_free(msglist);
1260         }
1261
1262         news_folder_unlock(NEWS_FOLDER(item->folder));
1263
1264         session_set_access_time(SESSION(session));
1265
1266         news_get_extra_fields(session, item, newlist);
1267         
1268         return newlist;
1269 }
1270
1271 static MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
1272 {
1273         GSList *msglist = NULL;
1274         NewsSession *session;
1275         MsgInfo *msginfo = NULL;
1276
1277         session = news_session_get(folder);
1278         cm_return_val_if_fail(session != NULL, NULL);
1279         cm_return_val_if_fail(item != NULL, NULL);
1280         cm_return_val_if_fail(item->folder != NULL, NULL);
1281         cm_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
1282
1283         msglist = news_get_msginfos_for_range(session, item, num, num);
1284  
1285         if (msglist)
1286                 msginfo = msglist->data;
1287         
1288         g_slist_free(msglist);
1289         
1290         return msginfo;
1291 }
1292
1293 static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1294 {
1295         NewsSession *session;
1296         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1297         guint first, last, next;
1298 /*      guint tofetch, fetched;
1299 */
1300         
1301         cm_return_val_if_fail(folder != NULL, NULL);
1302         cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1303         cm_return_val_if_fail(msgnum_list != NULL, NULL);
1304         cm_return_val_if_fail(item != NULL, NULL);
1305         
1306         session = news_session_get(folder);
1307         cm_return_val_if_fail(session != NULL, NULL);
1308
1309         tmp_msgnum_list = g_slist_copy(msgnum_list);
1310         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1311
1312         progressindicator_start(PROGRESS_TYPE_NETWORK);
1313 /*      tofetch = g_slist_length(tmp_msgnum_list);
1314         fetched = 0;
1315 */
1316
1317         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1318         last = first;
1319         
1320         news_folder_lock(NEWS_FOLDER(item->folder));
1321         
1322         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1323                 next = GPOINTER_TO_INT(elem->data);
1324                 if(next != (last + 1)) {
1325 /*                      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1326                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1327 */
1328                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1329                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1330 /*                      fetched = last - first + 1;
1331 */
1332                         first = next;
1333                 }
1334                 last = next;
1335         }
1336         
1337         news_folder_unlock(NEWS_FOLDER(item->folder));
1338         
1339 /*      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1340         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
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'\n", 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 account(s) 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