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