2007-11-16 [colin] 3.0.2cvs138
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 #endif
23
24 #ifdef HAVE_LIBETPAN
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <dirent.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <libetpan/libetpan.h>
37
38 #include "nntp-thread.h"
39 #include "news.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 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
60 #  include "ssl.h"
61 #endif
62
63 #define NNTP_PORT       119
64 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
65 #define NNTPS_PORT      563
66 #endif
67
68 typedef struct _NewsFolder      NewsFolder;
69 typedef struct _NewsSession     NewsSession;
70
71 #define NEWS_FOLDER(obj)        ((NewsFolder *)obj)
72 #define NEWS_SESSION(obj)       ((NewsSession *)obj)
73
74 struct _NewsFolder
75 {
76         RemoteFolder rfolder;
77
78         gboolean use_auth;
79         gboolean lock_count;
80         guint refcnt;
81 };
82
83 struct _NewsSession
84 {
85         Session session;
86
87         gchar *group;
88 };
89
90 static void news_folder_init(Folder *folder, const gchar *name,
91                              const gchar *path);
92
93 static Folder   *news_folder_new        (const gchar    *name,
94                                          const gchar    *folder);
95 static void      news_folder_destroy    (Folder         *folder);
96
97 static gchar *news_fetch_msg            (Folder         *folder,
98                                          FolderItem     *item,
99                                          gint            num);
100 static void news_remove_cached_msg      (Folder         *folder, 
101                                          FolderItem     *item, 
102                                          MsgInfo        *msginfo);
103 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
104 static Session *news_session_new         (Folder        *folder,
105                                           const gchar   *server,
106                                           gushort        port,
107                                           const gchar   *userid,
108                                           const gchar   *passwd,
109                                           SSLType        ssl_type);
110 #else
111 static Session *news_session_new         (Folder        *folder,
112                                           const gchar   *server,
113                                           gushort        port,
114                                           const gchar   *userid,
115                                           const gchar   *passwd);
116 #endif
117
118 static gint news_get_article             (Folder        *folder,
119                                           gint           num,
120                                           gchar         *filename);
121
122 static gint news_select_group            (Folder        *folder,
123                                           const gchar   *group,
124                                           gint          *num,
125                                           gint          *first,
126                                           gint          *last);
127 static MsgInfo *news_parse_xover         (struct newsnntp_xover_resp_item *item);
128 static gchar *news_parse_xhdr            (clist         *list,
129                                           MsgInfo       *msginfo);
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_remove_folder           (Folder        *folder,
151                                           FolderItem    *item);
152 static FolderClass news_class;
153
154 FolderClass *news_get_class(void)
155 {
156         if (news_class.idstr == NULL) {
157                 news_class.type = F_NEWS;
158                 news_class.idstr = "news";
159                 news_class.uistr = "News";
160
161                 /* Folder functions */
162                 news_class.new_folder = news_folder_new;
163                 news_class.destroy_folder = news_folder_destroy;
164
165                 /* FolderItem functions */
166                 news_class.item_get_path = news_item_get_path;
167                 news_class.get_num_list = news_get_num_list;
168                 news_class.scan_required = news_scan_required;
169                 news_class.remove_folder = news_remove_folder;
170
171                 /* Message functions */
172                 news_class.get_msginfo = news_get_msginfo;
173                 news_class.get_msginfos = news_get_msginfos;
174                 news_class.fetch_msg = news_fetch_msg;
175                 news_class.synchronise = news_synchronise;
176                 news_class.remove_msg = news_remove_msg;
177                 news_class.remove_cached_msg = news_remove_cached_msg;
178         };
179
180         return &news_class;
181 }
182
183 guint nntp_folder_get_refcnt(Folder *folder)
184 {
185         return ((NewsFolder *)folder)->refcnt;
186 }
187
188 void nntp_folder_ref(Folder *folder)
189 {
190         ((NewsFolder *)folder)->refcnt++;
191 }
192
193 void nntp_folder_unref(Folder *folder)
194 {
195         if (((NewsFolder *)folder)->refcnt > 0)
196                 ((NewsFolder *)folder)->refcnt--;
197 }
198
199 static int news_remove_msg               (Folder        *folder, 
200                                           FolderItem    *item, 
201                                           gint           msgnum)
202 {
203         gchar *path, *filename;
204
205         g_return_val_if_fail(folder != NULL, -1);
206         g_return_val_if_fail(item != NULL, -1);
207
208         path = folder_item_get_path(item);
209         if (!is_dir_exist(path))
210                 make_dir_hier(path);
211         
212         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
213         g_free(path);
214         g_unlink(filename);
215         g_free(filename);
216         return 0;
217 }
218
219 static void news_folder_lock(NewsFolder *folder)
220 {
221         folder->lock_count++;
222 }
223
224 static void news_folder_unlock(NewsFolder *folder)
225 {
226         if (folder->lock_count > 0)
227                 folder->lock_count--;
228 }
229
230 int news_folder_locked(Folder *folder)
231 {
232         if (folder == NULL)
233                 return 0;
234
235         return NEWS_FOLDER(folder)->lock_count;
236 }
237
238 static Folder *news_folder_new(const gchar *name, const gchar *path)
239 {
240         Folder *folder;
241
242         folder = (Folder *)g_new0(NewsFolder, 1);
243         folder->klass = &news_class;
244         news_folder_init(folder, name, path);
245
246         return folder;
247 }
248
249 static void news_folder_destroy(Folder *folder)
250 {
251         gchar *dir;
252
253         while (nntp_folder_get_refcnt(folder) > 0)
254                 gtk_main_iteration();
255
256         dir = news_folder_get_path(folder);
257         if (is_dir_exist(dir))
258                 remove_dir_recursive(dir);
259         g_free(dir);
260
261         nntp_done(folder);
262         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
263 }
264
265 static void news_folder_init(Folder *folder, const gchar *name,
266                              const gchar *path)
267 {
268         folder_remote_folder_init(folder, name, path);
269 }
270
271 static void news_session_destroy(Session *session)
272 {
273         NewsSession *news_session = NEWS_SESSION(session);
274
275         g_return_if_fail(session != NULL);
276
277         if (news_session->group)
278                 g_free(news_session->group);
279 }
280
281 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
282 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
283                                  const gchar *userid, const gchar *passwd,
284                                  SSLType ssl_type)
285 #else
286 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
287                                  const gchar *userid, const gchar *passwd)
288 #endif
289 {
290         NewsSession *session;
291         int r = 0;
292         g_return_val_if_fail(server != NULL, NULL);
293
294         log_message(LOG_PROTOCOL, _("creating NNTP connection to %s:%d ...\n"), server, port);
295
296         session = g_new0(NewsSession, 1);
297         session_init(SESSION(session));
298         SESSION(session)->type             = SESSION_NEWS;
299         SESSION(session)->server           = g_strdup(server);
300         SESSION(session)->sock             = NULL;
301         SESSION(session)->destroy          = news_session_destroy;
302         
303         nntp_init(folder);
304
305 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
306         if (ssl_type != SSL_NONE)
307                 r = nntp_threaded_connect_ssl(folder, server, port);
308         else
309 #endif
310                 r = nntp_threaded_connect(folder, server, port);
311         
312         if (r != NEWSNNTP_NO_ERROR) {
313                 log_error(LOG_PROTOCOL, _("Error logging in to %s:%d ...\n"), server, port);
314                 session_destroy(SESSION(session));
315                 return NULL;
316         }
317         
318         return SESSION(session);
319 }
320
321 static Session *news_session_new_for_folder(Folder *folder)
322 {
323         Session *session;
324         PrefsAccount *ac;
325         const gchar *userid = NULL;
326         gchar *passwd = NULL;
327         gushort port;
328
329         g_return_val_if_fail(folder != NULL, NULL);
330         g_return_val_if_fail(folder->account != NULL, NULL);
331
332         ac = folder->account;
333         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
334                 userid = ac->userid;
335                 if (ac->passwd && ac->passwd[0])
336                         passwd = g_strdup(ac->passwd);
337                 else
338                         passwd = input_dialog_query_password(ac->nntp_server,
339                                                              userid);
340         }
341
342 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
343         port = ac->set_nntpport ? ac->nntpport
344                 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
345         session = news_session_new(folder, ac->nntp_server, port, userid, passwd,
346                                    ac->ssl_nntp);
347 #else
348         if (ac->ssl_nntp != SSL_NONE) {
349                 if (alertpanel_full(_("Insecure connection"),
350                         _("This connection is configured to be secured "
351                           "using SSL, but SSL is not available in this "
352                           "build of Claws Mail. \n\n"
353                           "Do you want to continue connecting to this "
354                           "server? The communication would not be "
355                           "secure."),
356                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
357                           NULL, FALSE, NULL, ALERT_WARNING,
358                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
359                         return NULL;
360         }
361         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
362         session = news_session_new(folder, ac->nntp_server, port, userid, passwd);
363 #endif
364
365         if ((session != NULL) && ac->use_nntp_auth) { /* FIXME:  && ac->use_nntp_auth_onconnect */
366                 if (nntp_threaded_login(folder, userid, passwd) !=
367                         NEWSNNTP_NO_ERROR) {
368                         log_error(LOG_PROTOCOL, _("Error authenticating to %s:%d ...\n"), ac->nntp_server, port);
369                         session_destroy(SESSION(session));
370                         g_free(passwd);
371                         return NULL;
372                 }
373         }
374         g_free(passwd);
375
376         return session;
377 }
378
379 static NewsSession *news_session_get(Folder *folder)
380 {
381         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
382         struct tm lt;
383         int r;
384         
385         g_return_val_if_fail(folder != NULL, NULL);
386         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
387         g_return_val_if_fail(folder->account != NULL, NULL);
388
389         if (prefs_common.work_offline && 
390             !inc_offline_should_override(FALSE,
391                 _("Claws Mail needs network access in order "
392                   "to access the News server."))) {
393                 return NULL;
394         }
395
396         if (!rfolder->session) {
397                 rfolder->session = news_session_new_for_folder(folder);
398                 return NEWS_SESSION(rfolder->session);
399         }
400
401         if (time(NULL) - rfolder->session->last_access_time <
402                 SESSION_TIMEOUT_INTERVAL) {
403                 return NEWS_SESSION(rfolder->session);
404         }
405
406         if ((r = nntp_threaded_date(folder, &lt)) != NEWSNNTP_NO_ERROR) {
407                 if (r != NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED &&
408                     r != NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD) {
409                         log_warning(LOG_PROTOCOL, _("NNTP connection to %s:%d has been"
410                               " disconnected. Reconnecting...\n"),
411                             folder->account->nntp_server,
412                             folder->account->set_nntpport ?
413                             folder->account->nntpport : NNTP_PORT);
414                         session_destroy(rfolder->session);
415                         rfolder->session = news_session_new_for_folder(folder);
416                 }
417         }
418         
419         if (rfolder->session)
420                 session_set_access_time(rfolder->session);
421
422         return NEWS_SESSION(rfolder->session);
423 }
424
425 static void news_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
426 {
427         gchar *path, *filename;
428
429         path = folder_item_get_path(item);
430
431         if (!is_dir_exist(path)) {
432                 g_free(path);
433                 return;
434         }
435
436         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
437         g_free(path);
438
439         if (is_file_exist(filename)) {
440                 g_unlink(filename);
441         }
442         g_free(filename);
443 }
444
445 static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
446 {
447         gchar *path, *filename;
448         NewsSession *session;
449         gint ok;
450
451         g_return_val_if_fail(folder != NULL, NULL);
452         g_return_val_if_fail(item != NULL, NULL);
453
454         path = folder_item_get_path(item);
455         if (!is_dir_exist(path))
456                 make_dir_hier(path);
457         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
458         g_free(path);
459
460         if (is_file_exist(filename)) {
461                 debug_print("article %d has been already cached.\n", num);
462                 return filename;
463         }
464
465         session = news_session_get(folder);
466         if (!session) {
467                 g_free(filename);
468                 return NULL;
469         }
470
471         ok = news_select_group(folder, item->path, NULL, NULL, NULL);
472         if (ok != NEWSNNTP_NO_ERROR) {
473                 if (ok == NEWSNNTP_ERROR_STREAM) {
474                         session_destroy(SESSION(session));
475                         REMOTE_FOLDER(folder)->session = NULL;
476                 }
477                 g_free(filename);
478                 return NULL;
479         }
480
481         debug_print("getting article %d...\n", num);
482         ok = news_get_article(folder,
483                               num, filename);
484         if (ok != NEWSNNTP_NO_ERROR) {
485                 g_warning("can't read article %d\n", num);
486                 if (ok == NEWSNNTP_ERROR_STREAM) {
487                         session_destroy(SESSION(session));
488                         REMOTE_FOLDER(folder)->session = NULL;
489                 }
490                 g_free(filename);
491                 return NULL;
492         }
493         GTK_EVENTS_FLUSH();
494         return filename;
495 }
496
497 static NewsGroupInfo *news_group_info_new(const gchar *name,
498                                           gint first, gint last, gchar type)
499 {
500         NewsGroupInfo *ginfo;
501
502         ginfo = g_new(NewsGroupInfo, 1);
503         ginfo->name = g_strdup(name);
504         ginfo->first = first;
505         ginfo->last = last;
506         ginfo->type = type;
507
508         return ginfo;
509 }
510
511 static void news_group_info_free(NewsGroupInfo *ginfo)
512 {
513         g_free(ginfo->name);
514         g_free(ginfo);
515 }
516
517 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
518                                     NewsGroupInfo *ginfo2)
519 {
520         return g_ascii_strcasecmp(ginfo1->name, ginfo2->name);
521 }
522
523 GSList *news_get_group_list(Folder *folder)
524 {
525         gchar *path, *filename;
526         FILE *fp;
527         GSList *list = NULL;
528         GSList *last = NULL;
529         gchar buf[BUFFSIZE];
530
531         g_return_val_if_fail(folder != NULL, NULL);
532         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
533
534         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
535         if (!is_dir_exist(path))
536                 make_dir_hier(path);
537         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
538         g_free(path);
539
540         if ((fp = g_fopen(filename, "rb")) == NULL) {
541                 NewsSession *session;
542                 gint ok;
543                 clist *grouplist = NULL;
544                 clistiter *cur;
545                 fp = fopen(filename, "wb");
546                 
547                 if (!fp) {
548                         g_free(filename);
549                         return NULL;
550                 }
551                 session = news_session_get(folder);
552                 if (!session) {
553                         fclose(fp);
554                         g_free(filename);
555                         return NULL;
556                 }
557
558                 ok = nntp_threaded_list(folder, &grouplist);
559                 
560                 if (ok != NEWSNNTP_NO_ERROR) {
561                         if (ok == NEWSNNTP_ERROR_STREAM) {
562                                 session_destroy(SESSION(session));
563                                 REMOTE_FOLDER(folder)->session = NULL;
564                         }
565                         fclose(fp);
566                         g_free(filename);
567                         return NULL;
568                 }
569                 
570                 for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) {
571                         struct newsnntp_group_info *info = (struct newsnntp_group_info *)
572                                                                 clist_content(cur);
573                         if (fprintf(fp, "%s %d %d %c\n",
574                                 info->grp_name,
575                                 info->grp_last,
576                                 info->grp_first,
577                                 info->grp_type) < 0) {
578                                 log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
579                                 session_destroy(SESSION(session));
580                                 REMOTE_FOLDER(folder)->session = NULL;
581                                 fclose(fp);
582                                 g_free(filename);
583                                 newsnntp_list_free(grouplist);
584                                 return NULL;
585                         }
586                 }
587                 newsnntp_list_free(grouplist);
588
589                 if (fclose(fp) == EOF) {
590                         log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
591                         session_destroy(SESSION(session));
592                         REMOTE_FOLDER(folder)->session = NULL;
593                         g_free(filename);
594                         return NULL;
595                 }
596
597                 if ((fp = g_fopen(filename, "rb")) == NULL) {
598                         FILE_OP_ERROR(filename, "fopen");
599                         g_free(filename);
600                         return NULL;
601                 }
602         }
603
604         while (fgets(buf, sizeof(buf), fp) != NULL) {
605                 gchar *p = buf;
606                 gchar *name;
607                 gint last_num;
608                 gint first_num;
609                 gchar type;
610                 NewsGroupInfo *ginfo;
611
612                 p = strchr(p, ' ');
613                 if (!p) continue;
614                 *p = '\0';
615                 p++;
616                 name = buf;
617
618                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
619                         continue;
620
621                 ginfo = news_group_info_new(name, first_num, last_num, type);
622
623                 if (!last)
624                         last = list = g_slist_append(NULL, ginfo);
625                 else {
626                         last = g_slist_append(last, ginfo);
627                         last = last->next;
628                 }
629         }
630
631         fclose(fp);
632         g_free(filename);
633
634         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
635
636         return list;
637 }
638
639 void news_group_list_free(GSList *group_list)
640 {
641         GSList *cur;
642
643         if (!group_list) return;
644
645         for (cur = group_list; cur != NULL; cur = cur->next)
646                 news_group_info_free((NewsGroupInfo *)cur->data);
647         g_slist_free(group_list);
648 }
649
650 void news_remove_group_list_cache(Folder *folder)
651 {
652         gchar *path, *filename;
653
654         g_return_if_fail(folder != NULL);
655         g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
656
657         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
658         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
659         g_free(path);
660
661         if (is_file_exist(filename)) {
662                 if (remove(filename) < 0)
663                         FILE_OP_ERROR(filename, "remove");
664         }
665         g_free(filename);
666 }
667
668 gint news_post(Folder *folder, const gchar *file)
669 {
670         gint ok;
671         char *contents = file_read_to_str(file);
672         NewsSession *session;
673
674         g_return_val_if_fail(folder != NULL, -1);
675         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
676         g_return_val_if_fail(contents != NULL, -1);
677         
678         session = news_session_get(folder);
679         if (!session)  {
680                 g_free(contents);
681                 return -1;
682         }
683         
684         ok = nntp_threaded_post(folder, contents, strlen(contents));
685         
686         g_free(contents);
687         if (ok == NEWSNNTP_ERROR_STREAM) {
688                 session_destroy(SESSION(session));
689                 REMOTE_FOLDER(folder)->session = NULL;
690         }
691
692         return ok;
693 }
694
695 static gint news_get_article(Folder *folder, gint num, gchar *filename)
696 {
697         size_t len;
698         char *result = NULL;
699         int r;
700         
701         r = nntp_threaded_article(folder, num, &result, &len);
702         
703         if (r == NEWSNNTP_NO_ERROR) {
704                 if (str_write_to_file(result, filename) < 0)
705                         return -1;
706         }
707         
708         g_free(result);
709         return r;
710 }
711
712 /**
713  * news_select_group:
714  * @session: Active NNTP session.
715  * @group: Newsgroup name.
716  * @num: Estimated number of articles.
717  * @first: First article number.
718  * @last: Last article number.
719  *
720  * Select newsgroup @group with the GROUP command if it is not already
721  * selected in @session, or article numbers need to be returned.
722  *
723  * Return value: NNTP result code.
724  **/
725 static gint news_select_group(Folder *folder, const gchar *group,
726                               gint *num, gint *first, gint *last)
727 {
728         gint ok;
729         gint num_, first_, last_;
730         struct newsnntp_group_info *info = NULL;
731         NewsSession *session = NEWS_SESSION(news_session_get(folder));
732
733         g_return_val_if_fail(session != NULL, -1);
734         
735         if (!num || !first || !last) {
736                 if (session->group && g_ascii_strcasecmp(session->group, group) == 0)
737                         return NEWSNNTP_NO_ERROR;
738                 num = &num_;
739                 first = &first_;
740                 last = &last_;
741         }
742
743         g_free(session->group);
744         session->group = NULL;
745
746         ok = nntp_threaded_group(folder, group, &info);
747         
748         if (ok != NEWSNNTP_NO_ERROR && 
749             ok != NEWSNNTP_ERROR_STREAM && 
750             ok != NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME) {
751                 ok = nntp_threaded_mode_reader(folder);
752                 if (ok == NEWSNNTP_NO_ERROR)
753                         ok = nntp_threaded_group(folder, group, &info);
754         }
755
756         if (ok == NEWSNNTP_NO_ERROR && info) {
757                 session->group = g_strdup(group);
758                 *num = info->grp_first;
759                 *first = info->grp_first;
760                 *last = info->grp_last;
761                 newsnntp_group_free(info);
762         } else {
763                 log_warning(LOG_PROTOCOL, _("couldn't select group: %s\n"), group);
764         }
765         return ok;
766 }
767
768 static MsgInfo *news_parse_xover(struct newsnntp_xover_resp_item *item)
769 {
770         MsgInfo *msginfo;
771
772         /* set MsgInfo */
773         msginfo = procmsg_msginfo_new();
774         msginfo->msgnum = item->ovr_article;
775         msginfo->size = item->ovr_size;
776
777         msginfo->date = g_strdup(item->ovr_date);
778         msginfo->date_t = procheader_date_parse(NULL, item->ovr_date, 0);
779
780         msginfo->from = conv_unmime_header(item->ovr_author, NULL);
781         msginfo->fromname = procheader_get_fromname(msginfo->from);
782
783         msginfo->subject = conv_unmime_header(item->ovr_subject, NULL);
784
785         remove_return(msginfo->from);
786         remove_return(msginfo->fromname);
787         remove_return(msginfo->subject);
788
789         if (item->ovr_message_id) {
790                 gchar *tmp = g_strdup(item->ovr_message_id);
791                 extract_parenthesis(tmp, '<', '>');
792                 remove_space(tmp);
793                 if (*tmp != '\0')
794                         msginfo->msgid = g_strdup(tmp);
795                 g_free(tmp);
796         }                        
797
798         /* FIXME: this is a quick fix; references' meaning was changed
799          * into having the actual list of references in the References: header.
800          * We need a GSList here, so msginfo_free() and msginfo_copy() can do 
801          * their things properly. */ 
802         if (item->ovr_references && *(item->ovr_references)) {   
803                 gchar **ref_tokens = g_strsplit(item->ovr_references, " ", -1);
804                 guint i = 0;
805                 char *tmp;
806                 char *p;
807                 while (ref_tokens[i]) {
808                         gchar *cur_ref = ref_tokens[i];
809                         msginfo->references = references_list_append(msginfo->references, 
810                                         cur_ref);
811                         i++;
812                 }
813                 g_strfreev(ref_tokens);
814                 
815                 tmp = g_strdup(item->ovr_references);
816                 eliminate_parenthesis(tmp, '(', ')');
817                 if ((p = strrchr(tmp, '<')) != NULL) {
818                         extract_parenthesis(p, '<', '>');
819                         remove_space(p);
820                         if (*p != '\0')
821                                 msginfo->inreplyto = g_strdup(p);
822                 }
823                 g_free(tmp);
824         } 
825
826         return msginfo;
827 }
828
829 static gchar *news_parse_xhdr(clist *hdrlist, MsgInfo *msginfo)
830 {
831         struct newsnntp_xhdr_resp_item *hdr;
832         
833         hdr = clist_content(clist_begin(hdrlist));
834         if (hdr->hdr_article != msginfo->msgnum)
835                 return NULL;
836         return g_strdup(hdr->hdr_value);
837 }
838
839 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
840 {
841         gchar * tmp;
842         FILE * tmpfp;
843         gchar buf[BUFFSIZE];
844
845         tmp = g_strdup_printf("%s%ctmp%p", g_get_tmp_dir(),
846                               G_DIR_SEPARATOR, msginfo);
847         if (tmp == NULL)
848                 return -1;
849
850         if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
851                 FILE_OP_ERROR(tmp, "fopen");
852                 return -1;
853         }
854         if (change_file_mode_rw(tmpfp, tmp) < 0) {
855                 FILE_OP_ERROR(tmp, "chmod");
856                 g_warning("can't change file mode\n");
857         }
858         
859         get_rfc822_date(buf, sizeof(buf));
860         if (fprintf(tmpfp, "From: %s\r\n"
861                        "Newsgroups: %s\r\n"
862                        "Subject: cmsg cancel <%s>\r\n"
863                        "Control: cancel <%s>\r\n"
864                        "Approved: %s\r\n"
865                        "X-Cancelled-by: %s\r\n"
866                        "Date: %s\r\n"
867                        "\r\n"
868                        "removed with Claws Mail\r\n",
869                        msginfo->from,
870                        msginfo->newsgroups,
871                        msginfo->msgid,
872                        msginfo->msgid,
873                        msginfo->from,
874                        msginfo->from,
875                        buf) < 0) {
876                 FILE_OP_ERROR(tmp, "fprintf");
877                 fclose(tmpfp);
878                 g_unlink(tmp);
879                 g_free(tmp);
880                 return -1;
881         }
882
883         if (fclose(tmpfp) == EOF) {
884                 FILE_OP_ERROR(tmp, "fclose");
885                 g_unlink(tmp);
886                 g_free(tmp);
887                 return -1;
888         }
889
890         news_post(folder, tmp);
891         remove(tmp);
892
893         g_free(tmp);
894
895         return 0;
896 }
897
898 static gchar *news_folder_get_path(Folder *folder)
899 {
900         gchar *folder_path;
901
902         g_return_val_if_fail(folder->account != NULL, NULL);
903
904         folder_path = g_strconcat(get_news_cache_dir(),
905                                   G_DIR_SEPARATOR_S,
906                                   folder->account->nntp_server,
907                                   NULL);
908         return folder_path;
909 }
910
911 static gchar *news_item_get_path(Folder *folder, FolderItem *item)
912 {
913         gchar *folder_path, *path;
914
915         g_return_val_if_fail(folder != NULL, NULL);
916         g_return_val_if_fail(item != NULL, NULL);
917         folder_path = news_folder_get_path(folder);
918
919         g_return_val_if_fail(folder_path != NULL, NULL);
920         if (folder_path[0] == G_DIR_SEPARATOR) {
921                 if (item->path)
922                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
923                                            item->path, NULL);
924                 else
925                         path = g_strdup(folder_path);
926         } else {
927                 if (item->path)
928                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
929                                            folder_path, G_DIR_SEPARATOR_S,
930                                            item->path, NULL);
931                 else
932                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
933                                            folder_path, NULL);
934         }
935         g_free(folder_path);
936
937         return path;
938 }
939
940 static gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
941 {
942         NewsSession *session;
943         gint i, ok, num, first, last, nummsgs = 0;
944         gchar *dir;
945
946         g_return_val_if_fail(item != NULL, -1);
947         g_return_val_if_fail(item->folder != NULL, -1);
948         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
949
950         session = news_session_get(folder);
951         g_return_val_if_fail(session != NULL, -1);
952
953         *old_uids_valid = TRUE;
954         
955         news_folder_lock(NEWS_FOLDER(item->folder));
956
957         ok = news_select_group(folder, item->path, &num, &first, &last);
958         if (ok != NEWSNNTP_NO_ERROR) {
959                 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
960                 news_folder_unlock(NEWS_FOLDER(item->folder));
961                 return -1;
962         }
963
964         dir = news_folder_get_path(folder);
965         if (num <= 0)
966                 remove_all_numbered_files(dir);
967         else if (last < first)
968                 log_warning(LOG_PROTOCOL, _("invalid article range: %d - %d\n"),
969                             first, last);
970         else {
971                 for (i = first; i <= last; i++) {
972                         *msgnum_list = g_slist_prepend(*msgnum_list, 
973                                                        GINT_TO_POINTER(i));
974                         nummsgs++;
975                 }
976                 debug_print("removing old messages from %d to %d in %s\n",
977                             first, last, dir);
978                 remove_numbered_files(dir, 1, first - 1);
979         }
980         g_free(dir);
981         news_folder_unlock(NEWS_FOLDER(item->folder));
982         return nummsgs;
983 }
984
985 static void news_set_msg_flags(FolderItem *item, MsgInfo *msginfo)
986 {
987         msginfo->flags.tmp_flags = 0;
988         if (item->folder->account->mark_crosspost_read && msginfo->msgid) {
989                 if (item->folder->newsart &&
990                     g_hash_table_lookup(item->folder->newsart, msginfo->msgid) != NULL) {
991                         msginfo->flags.perm_flags = MSG_COLORLABEL_TO_FLAGS(item->folder->account->crosspost_col);
992                                 
993                 } else {
994                         if (!item->folder->newsart) 
995                                 item->folder->newsart = g_hash_table_new(g_str_hash, g_str_equal);
996                         g_hash_table_insert(item->folder->newsart,
997                                         g_strdup(msginfo->msgid), GINT_TO_POINTER(1));
998                         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
999                 }
1000         } else {
1001                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1002         }
1003 }
1004
1005 static MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
1006 {
1007         NewsSession *session;
1008         MsgInfo *msginfo = NULL;
1009         gint ok;
1010         struct newsnntp_xover_resp_item *result = NULL;
1011         clist *hdrlist = NULL;
1012
1013         session = news_session_get(folder);
1014         g_return_val_if_fail(session != NULL, NULL);
1015         g_return_val_if_fail(item != NULL, NULL);
1016         g_return_val_if_fail(item->folder != NULL, NULL);
1017         g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
1018
1019         log_message(LOG_PROTOCOL, _("getting xover %d in %s...\n"),
1020                     num, item->path);
1021         news_folder_lock(NEWS_FOLDER(item->folder));
1022
1023         ok = nntp_threaded_xover(folder, num, num, &result, NULL);
1024         if (ok != NEWSNNTP_NO_ERROR) {
1025                 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1026                 if (ok == NEWSNNTP_ERROR_STREAM) {
1027                         session_destroy(SESSION(session));
1028                         REMOTE_FOLDER(item->folder)->session = NULL;
1029                 }
1030                 news_folder_unlock(NEWS_FOLDER(item->folder));
1031                 return NULL;
1032         }
1033         
1034         msginfo = news_parse_xover(result);
1035         xover_resp_item_free(result);
1036         if (!msginfo) {
1037                 log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1038         }
1039
1040         if(!msginfo) {
1041                 news_folder_unlock(NEWS_FOLDER(item->folder));
1042                 return NULL;
1043         }
1044
1045         msginfo->folder = item;
1046         
1047         news_set_msg_flags(item, msginfo);
1048         msginfo->flags.tmp_flags |= MSG_NEWS;
1049         msginfo->newsgroups = g_strdup(item->path);
1050
1051         ok = nntp_threaded_xhdr(folder, "to", num, num, &hdrlist);
1052         
1053         if (ok != NEWSNNTP_NO_ERROR) {
1054                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1055                 if (ok == NEWSNNTP_ERROR_STREAM) {
1056                         session_destroy(SESSION(session));
1057                         REMOTE_FOLDER(item->folder)->session = NULL;
1058                 }
1059                 news_folder_unlock(NEWS_FOLDER(item->folder));
1060                 return msginfo;
1061         }
1062
1063         msginfo->to = news_parse_xhdr(hdrlist, msginfo);
1064         newsnntp_xhdr_free(hdrlist);
1065
1066         ok = nntp_threaded_xhdr(folder, "cc", num, num, &hdrlist);
1067         
1068         if (ok != NEWSNNTP_NO_ERROR) {
1069                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1070                 if (ok == NEWSNNTP_ERROR_STREAM) {
1071                         session_destroy(SESSION(session));
1072                         REMOTE_FOLDER(item->folder)->session = NULL;
1073                 }
1074                 news_folder_unlock(NEWS_FOLDER(item->folder));
1075                 return msginfo;
1076         }
1077
1078         msginfo->cc = news_parse_xhdr(hdrlist, msginfo);
1079         newsnntp_xhdr_free(hdrlist);
1080
1081         news_folder_unlock(NEWS_FOLDER(item->folder));
1082         return msginfo;
1083 }
1084
1085 static GSList *news_get_msginfos_for_range(NewsSession *session, FolderItem *item, guint begin, guint end)
1086 {
1087         GSList *newlist = NULL;
1088         GSList *llast = NULL;
1089         MsgInfo *msginfo;
1090         gint ok;
1091         clist *msglist = NULL;
1092         clistiter *cur;
1093         g_return_val_if_fail(session != NULL, NULL);
1094         g_return_val_if_fail(item != NULL, NULL);
1095
1096         log_message(LOG_PROTOCOL, _("getting xover %d - %d in %s...\n"),
1097                     begin, end, item->path);
1098
1099         news_folder_lock(NEWS_FOLDER(item->folder));
1100         
1101         ok = nntp_threaded_xover(item->folder, begin, end, NULL, &msglist);
1102         
1103         if (ok != NEWSNNTP_NO_ERROR) {
1104                 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1105                 if (ok == NEWSNNTP_ERROR_STREAM) {
1106                         session_destroy(SESSION(session));
1107                         REMOTE_FOLDER(item->folder)->session = NULL;
1108                 }
1109                 news_folder_unlock(NEWS_FOLDER(item->folder));
1110                 return NULL;
1111         }
1112
1113         for (cur = clist_begin(msglist); cur; cur = clist_next(cur)) {
1114                 struct newsnntp_xover_resp_item *ritem = (struct newsnntp_xover_resp_item *)clist_content(cur);
1115                 msginfo = news_parse_xover(ritem);
1116                 
1117                 if (!msginfo) {
1118                         log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1119                         continue;
1120                 }
1121
1122                 msginfo->folder = item;
1123                 news_set_msg_flags(item, msginfo);
1124                 msginfo->flags.tmp_flags |= MSG_NEWS;
1125                 msginfo->newsgroups = g_strdup(item->path);
1126
1127                 if (!newlist)
1128                         llast = newlist = g_slist_append(newlist, msginfo);
1129                 else {
1130                         llast = g_slist_append(llast, msginfo);
1131                         llast = llast->next;
1132                 }
1133         }
1134         newsnntp_xover_resp_list_free(msglist);
1135
1136         news_folder_unlock(NEWS_FOLDER(item->folder));
1137
1138         session_set_access_time(SESSION(session));
1139
1140         return newlist;
1141 }
1142
1143 static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1144 {
1145         NewsSession *session;
1146         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1147         guint first, last, next;
1148         guint tofetch, fetched;
1149         
1150         g_return_val_if_fail(folder != NULL, NULL);
1151         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1152         g_return_val_if_fail(msgnum_list != NULL, NULL);
1153         g_return_val_if_fail(item != NULL, NULL);
1154         
1155         session = news_session_get(folder);
1156         g_return_val_if_fail(session != NULL, NULL);
1157
1158         tmp_msgnum_list = g_slist_copy(msgnum_list);
1159         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1160
1161         progressindicator_start(PROGRESS_TYPE_NETWORK);
1162         tofetch = g_slist_length(tmp_msgnum_list);
1163         fetched = 0;
1164
1165         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1166         last = first;
1167         
1168         news_folder_lock(NEWS_FOLDER(item->folder));
1169         
1170         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1171                 next = GPOINTER_TO_INT(elem->data);
1172                 if(next != (last + 1)) {
1173 /*                      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1174                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1175 */
1176                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1177                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1178                         fetched = last - first + 1;
1179                         first = next;
1180                 }
1181                 last = next;
1182         }
1183         
1184         news_folder_unlock(NEWS_FOLDER(item->folder));
1185         
1186 /*      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1187         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1188 */
1189         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1190         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1191
1192         g_slist_free(tmp_msgnum_list);
1193         
1194         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1195
1196         return msginfo_list;
1197 }
1198
1199 static gboolean news_scan_required(Folder *folder, FolderItem *item)
1200 {
1201         return TRUE;
1202 }
1203
1204 void news_synchronise(FolderItem *item, gint days) 
1205 {
1206         news_gtk_synchronise(item, days);
1207 }
1208
1209 static gint news_remove_folder(Folder *folder, FolderItem *item)
1210 {
1211         gchar *path;
1212
1213         g_return_val_if_fail(folder != NULL, -1);
1214         g_return_val_if_fail(item != NULL, -1);
1215         g_return_val_if_fail(item->path != NULL, -1);
1216
1217         path = folder_item_get_path(item);
1218         if (remove_dir_recursive(path) < 0) {
1219                 g_warning("can't remove directory `%s'\n", path);
1220                 g_free(path);
1221                 return -1;
1222         }
1223
1224         g_free(path);
1225         folder_item_remove(item);
1226         return 0;
1227 }
1228
1229 #else
1230 #include <glib.h>
1231 #include <glib/gi18n.h>
1232 #include <gtk/gtk.h>
1233 #include "folder.h"
1234 #include "alertpanel.h"
1235
1236 static FolderClass news_class;
1237
1238 static void warn_etpan(void)
1239 {
1240         static gboolean missing_news_warning = TRUE;
1241         if (missing_news_warning) {
1242                 missing_news_warning = FALSE;
1243                 alertpanel_error(
1244                         _("You have one or more News accounts "
1245                           "defined. However this version of "
1246                           "Claws Mail has been built without "
1247                           "News support; your News account(s) are "
1248                           "disabled.\n\n"
1249                           "You probably need to "
1250                           "install libetpan and recompile "
1251                           "Claws Mail."));
1252         }
1253 }
1254 static Folder *news_folder_new(const gchar *name, const gchar *path)
1255 {
1256         warn_etpan();
1257         return NULL;
1258 }
1259 void news_group_list_free(GSList *group_list)
1260 {
1261         warn_etpan();
1262 }
1263 void news_remove_group_list_cache(Folder *folder)
1264 {
1265         warn_etpan();
1266 }
1267 int news_folder_locked(Folder *folder)
1268 {
1269         warn_etpan();
1270         return 0;
1271 }
1272 gint news_post(Folder *folder, const gchar *file)
1273 {
1274         warn_etpan();
1275         return -1;
1276 }
1277
1278 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
1279 {
1280         warn_etpan();
1281         return -1;
1282 }
1283
1284 GSList *news_get_group_list(Folder *folder)
1285 {
1286         warn_etpan();
1287         return NULL;
1288 }
1289
1290
1291 FolderClass *news_get_class(void)
1292 {
1293         if (news_class.idstr == NULL) {
1294                 news_class.type = F_NEWS;
1295                 news_class.idstr = "news";
1296                 news_class.uistr = "News";
1297
1298                 /* Folder functions */
1299                 news_class.new_folder = news_folder_new;
1300         };
1301
1302         return &news_class;
1303 }
1304
1305
1306 #endif